From 804f86e6c983305ee35ac57f2d558159b48d2312 Mon Sep 17 00:00:00 2001 From: Pavel Machek Date: Mon, 3 Jun 2024 22:09:28 +0200 Subject: [PATCH 01/12] waypoints: attempt to fix marking. --- apps/waypoints/waypoints.app.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/apps/waypoints/waypoints.app.js b/apps/waypoints/waypoints.app.js index 0c63e9183..45a10d0dd 100644 --- a/apps/waypoints/waypoints.app.js +++ b/apps/waypoints/waypoints.app.js @@ -39,7 +39,7 @@ function mainMenu() { } menu["Remove"]=removeCard; menu["Format"]=setFormat; - if (textInputInstalled) { + if (textInputInstalled && BANGLEJS2) { menu["Mark GPS"]=markGps; } g.clear(); @@ -47,17 +47,16 @@ function mainMenu() { } function updateGps() { - let lat = "lat", lon = "lon", alt = "alt", speed = "speed"; + let pos = "lat/lon", alt = "alt", speed = "speed"; if (cancel_gps) return; fix = Bangle.getGPSFix(); - speed = "no fix for " + (getTime() - gps_start).toFixed(0) + "s"; + speed = "no fix (" + (getTime() - gps_start).toFixed(0) + "s)"; if (fix && fix.fix && fix.lat) { - lat = "" + lat(fix.lat); - lon = "" + lon(fix.lon); + pos = "" + lat(fix.lat) + " " + lon(fix.lon); alt = "alt " + fix.alt.toFixed(0) + "m"; speed = "speed " + fix.speed.toFixed(1) + "kt"; } @@ -67,8 +66,7 @@ function updateGps() { .fillRect(0, 0, 176, 120) .setColor(0,0,0) .drawString(key, 0, 0) - .drawString(lat, 0, 20) - .drawString(lon, 0, 40) + .drawString(pos, 0, 20) .drawString(alt, 0, 60) .drawString(speed, 0, 80); From da63f87ddea6cf5399dcedbc039fe10c8d8ac507 Mon Sep 17 00:00:00 2001 From: Pavel Machek Date: Mon, 24 Jun 2024 19:31:45 +0200 Subject: [PATCH 02/12] [] waypoints: start using gps/fmt libraries. --- apps/waypoints/waypoints.app.js | 177 ++++++++++++++++++++++++++------ 1 file changed, 143 insertions(+), 34 deletions(-) diff --git a/apps/waypoints/waypoints.app.js b/apps/waypoints/waypoints.app.js index 45a10d0dd..b77526ce9 100644 --- a/apps/waypoints/waypoints.app.js +++ b/apps/waypoints/waypoints.app.js @@ -3,14 +3,117 @@ var Layout = require("Layout"); const BANGLEJS2 = process.env.HWVERSION == 2; // check for bangle 2 +/* fmt library v0.1 */ +let fmt = { + icon_alt : "\0\x08\x1a\1\x00\x00\x00\x20\x30\x78\x7C\xFE\xFF\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", + icon_m : "\0\x08\x1a\1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", + icon_km : "\0\x08\x1a\1\xC3\xC6\xCC\xD8\xF0\xD8\xCC\xC6\xC3\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", + icon_kph : "\0\x08\x1a\1\xC3\xC6\xCC\xD8\xF0\xD8\xCC\xC6\xC3\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\xFF\x00\xC3\xC3\xFF\xC3\xC3", + icon_c : "\0\x08\x1a\1\x00\x00\x60\x90\x90\x60\x00\x7F\xFF\xC0\xC0\xC0\xC0\xC0\xFF\x7F\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + + /* 0 .. DD.ddddd + 1 .. DD MM.mmm' + 2 .. DD MM'ss" + */ + geo_mode : 1, + + init: function() {}, + fmtDist: function(km) { return km.toFixed(1) + this.icon_km; }, + fmtSteps: function(n) { return fmtDist(0.001 * 0.719 * n); }, + fmtAlt: function(m) { return m.toFixed(0) + this.icon_alt; }, + fmtTimeDiff: function(d) { + if (d < 180) + return ""+d.toFixed(0); + d = d/60; + return ""+d.toFixed(0)+"m"; + }, + fmtAngle: function(x) { + switch (this.geo_mode) { + case 0: + return "" + x; + case 1: { + let d = Math.floor(x); + let m = x - d; + m = m*60; + return "" + d + " " + m.toFixed(3) + "'"; + } + case 2: { + let d = Math.floor(x); + let m = x - d; + m = m*60; + let mf = Math.floor(m); + let s = m - mf; + s = s*60; + return "" + d + " " + mf + "'" + s.toFixed(0) + '"'; + } + } + return "bad mode?"; + }, + fmtPos: function(pos) { + let x = pos.lat; + let c = "N"; + if (x<0) { + c = "S"; + x = -x; + } + let s = c+this.fmtAngle(pos.lat) + "\n"; + c = "E"; + if (x<0) { + c = "W"; + x = -x; + } + return s + c + this.fmtAngle(pos.lon); + }, +}; + +/* gps library v0.1 */ +let gps = { + emulator: -1, + init: function(x) { + this.emulator = (process.env.BOARD=="EMSCRIPTEN" + || process.env.BOARD=="EMSCRIPTEN2")?1:0; + }, + state: {}, + on_gps: function(f) { + let fix = this.getGPSFix(); + f(fix); + + /* + "lat": number, // Latitude in degrees + "lon": number, // Longitude in degrees + "alt": number, // altitude in M + "speed": number, // Speed in kph + "course": number, // Course in degrees + "time": Date, // Current Time (or undefined if not known) + "satellites": 7, // Number of satellites + "fix": 1 // NMEA Fix state - 0 is no fix + "hdop": number, // Horizontal Dilution of Precision + */ + this.state.timeout = setTimeout(this.on_gps, 1000, f); + }, + off_gps: function() { + clearTimeout(gps_state.timeout); + }, + getGPSFix: function() { + if (!this.emulator) + return Bangle.getGPSFix(); + let fix = {}; + fix.fix = 1; + fix.lat = 50; + fix.lon = 14; + fix.alt = 200; + fix.speed = 5; + fix.course = 30; + fix.time = Date(); + fix.satellites = 5; + fix.hdop = 12; + return fix; + } +}; + var wp = require('Storage').readJSON("waypoints.json", true) || []; // Use this with corrupted waypoints //var wp = []; -/* 0 .. DD.ddddd - 1 .. DD MM.mmm' - 2 .. DD MM'ss" -*/ -var mode = 1; var key; /* Shared between functions, typically wp name */ var fix; /* GPS fix */ var cancel_gps; @@ -47,30 +150,33 @@ function mainMenu() { } function updateGps() { - let pos = "lat/lon", alt = "alt", speed = "speed"; - - if (cancel_gps) - return; - fix = Bangle.getGPSFix(); - - speed = "no fix (" + (getTime() - gps_start).toFixed(0) + "s)"; - - if (fix && fix.fix && fix.lat) { - pos = "" + lat(fix.lat) + " " + lon(fix.lon); - alt = "alt " + fix.alt.toFixed(0) + "m"; - speed = "speed " + fix.speed.toFixed(1) + "kt"; - } - - g.reset().setFont("Vector", 20) - .setColor(1,1,1) - .fillRect(0, 0, 176, 120) - .setColor(0,0,0) - .drawString(key, 0, 0) - .drawString(pos, 0, 20) - .drawString(alt, 0, 60) - .drawString(speed, 0, 80); + let have = false, lat = "lat ", alt = "?", + speed = "speed ", hdop = "?", adelta = "adelta ", + tdelta = "tdelta "; - setTimeout(updateGps, 100); + if (cancel_gps) + return; + fix = gps.getGPSFix(); + + if (fix && fix.fix && fix.lat) { + lat = "" + fmt.fmtPos(fix); + alt = "" + fix.alt.toFixed(0); + speed = "" + fix.speed.toFixed(1); + hdop = "" + fix.hdop.toFixed(0); + have = true; + } else { + lat = "NO FIX\n" + + "" + (getTime() - gps_start).toFixed(0) + "s "; + } + + let msg = ""; + msg = lat + "\n"+ alt + "m"; + g.reset().setFont("Vector", 31) + .setColor(1,1,1) + .fillRect(0, 24, 176, 100) + .setColor(0,0,0) + .drawString(msg, 3, 25); + setTimeout(updateGps, 1000); } function stopGps() { @@ -110,16 +216,16 @@ function setFormat() { var la = new Layout ( {type:"v", c: [ {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:"Format"}, - {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "DD.dddd", cb:l=>{ mode = 0; mainMenu(); }}, - {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "DD MM.mmm'", cb:l=>{ mode = 1; mainMenu(); }}, - {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "DD MM'ss"+'"', cb:l=>{ mode = 2; mainMenu(); }}, + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "DD.dddd", cb:l=>{ fmt.geo_mode = 0; mainMenu(); }}, + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "DD MM.mmm'", cb:l=>{ fmt.geo_mode = 1; mainMenu(); }}, + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "DD MM'ss"+'"', cb:l=>{ fmt.geo_mode = 2; mainMenu(); }}, ], lazy:true}); g.clear(); la.render(); } function format(x) { - switch (mode) { + switch (fmt.geo_mode) { case 0: return "" + x; case 1: @@ -262,13 +368,13 @@ function ask01(t, cb) { function askCoordinate(t1, t2, callback) { //let sign = 1; ask01(t1, function(sign) { - switch (mode) { + switch (fmt.geo_mode) { case 0: s = "DDD.dddd"; break; case 1: s = "DDD MM.mmm"; break; case 2: s = "DDD MM'ss"+'"'; break; } showNumpad(s, t2, function() { - switch (mode) { + switch (fmt.geo_mode) { case 0: res = parseFloat(key); break; @@ -338,5 +444,8 @@ function addCard() { }); } +fmt.init(); +gps.init(); + g.reset(); mainMenu(); From affdb2f32cdb0573c6b2c4cbc900315c44576434 Mon Sep 17 00:00:00 2001 From: Pavel Machek Date: Mon, 24 Jun 2024 22:56:39 +0200 Subject: [PATCH 03/12] [] waypoints: remove waypoint names from main menu --- apps/waypoints/waypoints.app.js | 53 +++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 6 deletions(-) diff --git a/apps/waypoints/waypoints.app.js b/apps/waypoints/waypoints.app.js index b77526ce9..2d2c9aa75 100644 --- a/apps/waypoints/waypoints.app.js +++ b/apps/waypoints/waypoints.app.js @@ -56,13 +56,13 @@ let fmt = { c = "S"; x = -x; } - let s = c+this.fmtAngle(pos.lat) + "\n"; + let s = c+this.fmtAngle(x) + "\n"; c = "E"; if (x<0) { c = "W"; x = -x; } - return s + c + this.fmtAngle(pos.lon); + return s + c + this.fmtAngle(x); }, }; @@ -133,13 +133,10 @@ function mainMenu() { var menu = { "< Back" : () => load() }; - for (let id in wp) { - let i = id; - menu[wp[id]["name"]]=()=>{ show(i); }; - } if (textInputInstalled && BANGLEJS2) { menu["Add"]=addCard; } + menu["Show"]=showCard; menu["Remove"]=removeCard; menu["Format"]=setFormat; if (textInputInstalled && BANGLEJS2) { @@ -327,6 +324,50 @@ function showNumpad(text, key_, callback) { update(); } +function showCard() { + var menu = { + "" : {title : "Select WP"}, + "< Back" : mainMenu + }; + if (Object.keys(wp).length==0) Object.assign(menu, {"No WPs":""}); + else { + wp.forEach((val, card) => { + const name = wp[card].name; + menu[name]= () => show(card); + }); + } + E.showMenu(menu); +} + + +function removeCard() { + var menu = { + "" : {title : "Select WP"}, + "< Back" : mainMenu + }; + if (Object.keys(wp).length==0) Object.assign(menu, {"No WPs":""}); + else { + wp.forEach((val, card) => { + const name = wp[card].name; + menu[name]=()=>{ + E.showPrompt(name,{ + title:"Delete", + }).then(function(v) { + if (v) { + wp.splice(card, 1); + writeWP(); + mainMenu(); + } else { + mainMenu(); + } + }); + }; + }); + } + E.showMenu(menu); +} + + function removeCard() { var menu = { "" : {title : "Select WP"}, From 75f64e80047361288987ca7fed32a4e6ff1e58d6 Mon Sep 17 00:00:00 2001 From: Pavel Machek Date: Mon, 24 Jun 2024 23:07:31 +0200 Subject: [PATCH 04/12] [] waypoints: handle removal in similar way to show. --- apps/waypoints/waypoints.app.js | 61 +++++++++++---------------------- 1 file changed, 20 insertions(+), 41 deletions(-) diff --git a/apps/waypoints/waypoints.app.js b/apps/waypoints/waypoints.app.js index 2d2c9aa75..ac90eac71 100644 --- a/apps/waypoints/waypoints.app.js +++ b/apps/waypoints/waypoints.app.js @@ -339,6 +339,25 @@ function showCard() { E.showMenu(menu); } +function remove(pin) +{ + let card = wp[pin]; + let name = card["name"]; + print("Remove?", card, name); + + E.showPrompt(name,{ + title:"Delete", + }).then(function(v) { + if (v) { + wp.splice(pin, 1); + writeWP(); + mainMenu(); + } else { + mainMenu(); + } + } + ); + } function removeCard() { var menu = { @@ -349,47 +368,7 @@ function removeCard() { else { wp.forEach((val, card) => { const name = wp[card].name; - menu[name]=()=>{ - E.showPrompt(name,{ - title:"Delete", - }).then(function(v) { - if (v) { - wp.splice(card, 1); - writeWP(); - mainMenu(); - } else { - mainMenu(); - } - }); - }; - }); - } - E.showMenu(menu); -} - - -function removeCard() { - var menu = { - "" : {title : "Select WP"}, - "< Back" : mainMenu - }; - if (Object.keys(wp).length==0) Object.assign(menu, {"No WPs":""}); - else { - wp.forEach((val, card) => { - const name = wp[card].name; - menu[name]=()=>{ - E.showPrompt(name,{ - title:"Delete", - }).then(function(v) { - if (v) { - wp.splice(card, 1); - writeWP(); - mainMenu(); - } else { - mainMenu(); - } - }); - }; + menu[name]=()=> remove(card); }); } E.showMenu(menu); From f55056b69c161611753209861073f8e05edce810 Mon Sep 17 00:00:00 2001 From: Pavel Machek Date: Mon, 24 Jun 2024 23:20:23 +0200 Subject: [PATCH 05/12] [] waypoints: use fmt to format position. --- apps/waypoints/waypoints.app.js | 163 ++++++++++++-------------------- 1 file changed, 63 insertions(+), 100 deletions(-) diff --git a/apps/waypoints/waypoints.app.js b/apps/waypoints/waypoints.app.js index ac90eac71..b9aae3c20 100644 --- a/apps/waypoints/waypoints.app.js +++ b/apps/waypoints/waypoints.app.js @@ -178,31 +178,31 @@ function updateGps() { function stopGps() { cancel_gps=true; - Bangle.setGPSPower(0, "waypoint_editor"); + Bangle.setGPSPower(0, "waypoints"); } function confirmGps(s) { - key = s; - var la = new Layout ( - {type:"v", c: [ - {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, - {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, - {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, - {type:"h", c: [ - {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "YES", cb:l=>{ - print("should mark", key, fix); createWP(fix.lat, fix.lon, key); cancel_gps=true; mainMenu(); - }}, - {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " NO", cb:l=>{ cancel_gps=true; mainMenu(); }} - ]} - ], lazy:true}); - g.clear(); - la.render(); - updateGps(); + key = s; + var la = new Layout ( + {type:"v", c: [ + {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, + {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, + {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, + {type:"h", c: [ + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "YES", cb:l=>{ + print("should mark", key, fix); createWP(fix.lat, fix.lon, key); cancel_gps=true; mainMenu(); + }}, + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " NO", cb:l=>{ cancel_gps=true; mainMenu(); }} + ]} + ], lazy:true}); + g.clear(); + la.render(); + updateGps(); } function markGps() { cancel_gps = false; - Bangle.setGPSPower(1, "waypoint_editor"); + Bangle.setGPSPower(1, "waypoints"); gps_start = getTime(); require("textinput").input({text:"wp"}).then(key => { confirmGps(key); @@ -221,53 +221,6 @@ function setFormat() { la.render(); } -function format(x) { - switch (fmt.geo_mode) { - case 0: - return "" + x; - case 1: - d = Math.floor(x); - m = x - d; - m = m*60; - return "" + d + " " + m + "'"; - case 2: - d = Math.floor(x); - m = x - d; - m = m*60; - mf = Math.floor(m); - s = m - mf; - s = s*60; - return "" + d + " " + mf + "'" + s + '"'; - } -} -function lat(x) { - c = "N"; - if (x<0) { - c = "S"; - x = -x; - } - return c+format(x); -} -function lon(x) { - c = "E"; - if (x<0) { - c = "W"; - x = -x; - } - return c+format(x); -} - -function show(pin) { - var i = wp[pin]; - var l = lat(i["lat"]) + "\n" + lon(i["lon"]); - E.showPrompt(l,{ - title:i["name"], - buttons : {"Ok":true} - }).then(function(v) { - mainMenu(); - }); -} - function showNumpad(text, key_, callback) { key = key_; E.showMenu(); @@ -324,54 +277,64 @@ function showNumpad(text, key_, callback) { update(); } -function showCard() { - var menu = { - "" : {title : "Select WP"}, - "< Back" : mainMenu - }; - if (Object.keys(wp).length==0) Object.assign(menu, {"No WPs":""}); - else { - wp.forEach((val, card) => { - const name = wp[card].name; - menu[name]= () => show(card); +function show(pin) { + var i = wp[pin]; + var l = fmt.fmtPos(i); + E.showPrompt(l,{ + title:i["name"], + buttons : {"Ok":true} + }).then(function(v) { + mainMenu(); }); - } - E.showMenu(menu); +} + +function showCard() { + var menu = { + "" : {title : "Select WP"}, + "< Back" : mainMenu + }; + if (Object.keys(wp).length==0) Object.assign(menu, {"No WPs":""}); + else { + wp.forEach((val, card) => { + const name = wp[card].name; + menu[name]= () => show(card); + }); + } + E.showMenu(menu); } function remove(pin) { - let card = wp[pin]; - let name = card["name"]; - print("Remove?", card, name); + let card = wp[pin]; + let name = card["name"]; + print("Remove?", card, name); - E.showPrompt(name,{ - title:"Delete", - }).then(function(v) { - if (v) { + E.showPrompt(name,{ + title:"Delete", + }).then(function(v) { + if (v) { wp.splice(pin, 1); writeWP(); mainMenu(); - } else { + } else { mainMenu(); - } } - ); - } + }); +} function removeCard() { - var menu = { - "" : {title : "Select WP"}, - "< Back" : mainMenu - }; - if (Object.keys(wp).length==0) Object.assign(menu, {"No WPs":""}); - else { - wp.forEach((val, card) => { - const name = wp[card].name; - menu[name]=()=> remove(card); - }); - } - E.showMenu(menu); + var menu = { + "" : {title : "Select WP"}, + "< Back" : mainMenu + }; + if (Object.keys(wp).length==0) Object.assign(menu, {"No WPs":""}); + else { + wp.forEach((val, card) => { + const name = wp[card].name; + menu[name]=()=> remove(card); + }); + } + E.showMenu(menu); } function ask01(t, cb) { From 6d2f3c371988d1e2279db95c99343d845f66ada1 Mon Sep 17 00:00:00 2001 From: Pavel Machek Date: Mon, 1 Jul 2024 22:25:20 +0200 Subject: [PATCH 06/12] [] waypoints: I updated the library --- apps/waypoints/waypoints.app.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/waypoints/waypoints.app.js b/apps/waypoints/waypoints.app.js index b9aae3c20..de538986d 100644 --- a/apps/waypoints/waypoints.app.js +++ b/apps/waypoints/waypoints.app.js @@ -3,7 +3,7 @@ var Layout = require("Layout"); const BANGLEJS2 = process.env.HWVERSION == 2; // check for bangle 2 -/* fmt library v0.1 */ +/* fmt library v0.1.1 */ let fmt = { icon_alt : "\0\x08\x1a\1\x00\x00\x00\x20\x30\x78\x7C\xFE\xFF\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", icon_m : "\0\x08\x1a\1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", From 85afccc18294b8ae086868633bd9d1b3b26901b4 Mon Sep 17 00:00:00 2001 From: Pavel Machek Date: Fri, 2 Aug 2024 20:11:37 +0200 Subject: [PATCH 07/12] waypoints: save altitude from GPS --- apps/waypoints/waypoints.app.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/apps/waypoints/waypoints.app.js b/apps/waypoints/waypoints.app.js index de538986d..334bf1c08 100644 --- a/apps/waypoints/waypoints.app.js +++ b/apps/waypoints/waypoints.app.js @@ -190,7 +190,7 @@ function confirmGps(s) { {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, {type:"h", c: [ {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "YES", cb:l=>{ - print("should mark", key, fix); createWP(fix.lat, fix.lon, key); cancel_gps=true; mainMenu(); + print("should mark", key, fix); createWP(fix.lat, fix.lon, fix.alt, key); cancel_gps=true; mainMenu(); }}, {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " NO", cb:l=>{ cancel_gps=true; mainMenu(); }} ]} @@ -387,11 +387,13 @@ function askPosition(callback) { }); } -function createWP(lat, lon, name) { +function createWP(lat, lon, alt, name) { let n = {}; n["name"] = name; n["lat"] = lat; n["lon"] = lon; + if (alt != -9999) + n["alt"] = alt; wp.push(n); print("add -- waypoints", wp); writeWP(); @@ -401,7 +403,7 @@ function addCardName(name) { g.clear(); askPosition(function(lat, lon) { print("position -- ", lat, lon); - createWP(lat, lon, result); + createWP(lat, lon, -9999, result); mainMenu(); }); } From 2b7062939ed39198e79d5187bcf3af30e210e62a Mon Sep 17 00:00:00 2001 From: Pavel Machek Date: Tue, 12 Nov 2024 13:29:04 +0100 Subject: [PATCH 08/12] waypoints: update to newer libraries --- apps/waypoints/waypoints.app.js | 378 ++++++++++++++++++++++++-------- 1 file changed, 282 insertions(+), 96 deletions(-) diff --git a/apps/waypoints/waypoints.app.js b/apps/waypoints/waypoints.app.js index 334bf1c08..6c1cbf633 100644 --- a/apps/waypoints/waypoints.app.js +++ b/apps/waypoints/waypoints.app.js @@ -3,112 +3,298 @@ var Layout = require("Layout"); const BANGLEJS2 = process.env.HWVERSION == 2; // check for bangle 2 -/* fmt library v0.1.1 */ +/* fmt library v0.2.2 */ let fmt = { - icon_alt : "\0\x08\x1a\1\x00\x00\x00\x20\x30\x78\x7C\xFE\xFF\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", - icon_m : "\0\x08\x1a\1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", - icon_km : "\0\x08\x1a\1\xC3\xC6\xCC\xD8\xF0\xD8\xCC\xC6\xC3\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", - icon_kph : "\0\x08\x1a\1\xC3\xC6\xCC\xD8\xF0\xD8\xCC\xC6\xC3\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\xFF\x00\xC3\xC3\xFF\xC3\xC3", - icon_c : "\0\x08\x1a\1\x00\x00\x60\x90\x90\x60\x00\x7F\xFF\xC0\xC0\xC0\xC0\xC0\xFF\x7F\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + icon_alt : "\0\x08\x1a\1\x00\x00\x00\x20\x30\x78\x7C\xFE\xFF\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", + icon_m : "\0\x08\x1a\1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", + icon_km : "\0\x08\x1a\1\xC3\xC6\xCC\xD8\xF0\xD8\xCC\xC6\xC3\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", + icon_kph : "\0\x08\x1a\1\xC3\xC6\xCC\xD8\xF0\xD8\xCC\xC6\xC3\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\xFF\x00\xC3\xC3\xFF\xC3\xC3", + icon_c : "\0\x08\x1a\1\x00\x00\x60\x90\x90\x60\x00\x7F\xFF\xC0\xC0\xC0\xC0\xC0\xFF\x7F\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + icon_hpa : "\x00\x08\x16\x01\x00\x80\xb0\xc8\x88\x88\x88\x00\xf0\x88\x84\x84\x88\xf0\x80\x8c\x92\x22\x25\x19\x00\x00", + icon_9 : "\x00\x08\x16\x01\x00\x00\x00\x00\x38\x44\x44\x4c\x34\x04\x04\x38\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", + icon_10 : "\x00\x08\x16\x01\x00\x08\x18\x28\x08\x08\x08\x00\x00\x18\x24\x24\x24\x24\x18\x00\x00\x00\x00\x00\x00\x00", /* 0 .. DD.ddddd 1 .. DD MM.mmm' 2 .. DD MM'ss" - */ - geo_mode : 1, - - init: function() {}, - fmtDist: function(km) { return km.toFixed(1) + this.icon_km; }, - fmtSteps: function(n) { return fmtDist(0.001 * 0.719 * n); }, - fmtAlt: function(m) { return m.toFixed(0) + this.icon_alt; }, - fmtTimeDiff: function(d) { - if (d < 180) - return ""+d.toFixed(0); - d = d/60; - return ""+d.toFixed(0)+"m"; - }, - fmtAngle: function(x) { - switch (this.geo_mode) { - case 0: - return "" + x; - case 1: { - let d = Math.floor(x); - let m = x - d; - m = m*60; - return "" + d + " " + m.toFixed(3) + "'"; - } - case 2: { - let d = Math.floor(x); - let m = x - d; - m = m*60; - let mf = Math.floor(m); - let s = m - mf; - s = s*60; - return "" + d + " " + mf + "'" + s.toFixed(0) + '"'; - } - } - return "bad mode?"; - }, - fmtPos: function(pos) { - let x = pos.lat; - let c = "N"; - if (x<0) { - c = "S"; - x = -x; - } - let s = c+this.fmtAngle(x) + "\n"; - c = "E"; - if (x<0) { - c = "W"; - x = -x; - } - return s + c + this.fmtAngle(x); - }, + */ + geo_mode : 1, + + init: function() {}, + fmtDist: function(km) { + if (km >= 1.0) return km.toFixed(1) + this.icon_km; + return (km*1000).toFixed(0) + this.icon_m; + }, + fmtSteps: function(n) { return this.fmtDist(0.001 * 0.719 * n); }, + fmtAlt: function(m) { return m.toFixed(0) + this.icon_alt; }, + fmtTemp: function(c) { return c.toFixed(1) + this.icon_c; }, + fmtPress: function(p) { + if (p < 900 || p > 1100) + return p.toFixed(0) + this.icon_hpa; + if (p < 1000) { + p -= 900; + return this.icon_9 + this.add0(p.toFixed(0)) + this.icon_hpa; + } + p -= 1000; + return this.icon_10 + this.add0(p.toFixed(0)) + this.icon_hpa; + }, + draw_dot : 1, + add0: function(i) { + if (i > 9) { + return ""+i; + } else { + return "0"+i; + } + }, + fmtTOD: function(now) { + this.draw_dot = !this.draw_dot; + let dot = ":"; + if (!this.draw_dot) + dot = "."; + return now.getHours() + dot + this.add0(now.getMinutes()); + }, + fmtNow: function() { return this.fmtTOD(new Date()); }, + fmtTimeDiff: function(d) { + if (d < 180) + return ""+d.toFixed(0); + d = d/60; + return ""+d.toFixed(0)+"m"; + }, + fmtAngle: function(x) { + switch (this.geo_mode) { + case 0: + return "" + x; + case 1: { + let d = Math.floor(x); + let m = x - d; + m = m*60; + return "" + d + " " + m.toFixed(3) + "'"; + } + case 2: { + let d = Math.floor(x); + let m = x - d; + m = m*60; + let mf = Math.floor(m); + let s = m - mf; + s = s*60; + return "" + d + " " + mf + "'" + s.toFixed(0) + '"'; + } + } + return "bad mode?"; + }, + fmtPos: function(pos) { + let x = pos.lat; + let c = "N"; + if (x<0) { + c = "S"; + x = -x; + } + let s = c+this.fmtAngle(x) + "\n"; + c = "E"; + if (x<0) { + c = "W"; + x = -x; + } + return s + c + this.fmtAngle(x); + }, + fmtFix: function(fix, t) { + if (fix && fix.fix && fix.lat) { + return this.fmtSpeed(fix.speed) + " " + + this.fmtAlt(fix.alt); + } else { + return "N/FIX " + this.fmtTimeDiff(t); + } + }, + fmtSpeed: function(kph) { + return kph.toFixed(1) + this.icon_kph; + }, + radians: function(a) { return a*Math.PI/180; }, + degrees: function(a) { return a*180/Math.PI; }, + // distance between 2 lat and lons, in meters, Mean Earth Radius = 6371km + // https://www.movable-type.co.uk/scripts/latlong.html + // (Equirectangular approximation) + // returns value in meters + distance: function(a,b) { + var x = this.radians(b.lon-a.lon) * Math.cos(this.radians((a.lat+b.lat)/2)); + var y = this.radians(b.lat-a.lat); + return Math.sqrt(x*x + y*y) * 6371000; + }, + // thanks to waypointer + bearing: function(a,b) { + var delta = this.radians(b.lon-a.lon); + var alat = this.radians(a.lat); + var blat = this.radians(b.lat); + var y = Math.sin(delta) * Math.cos(blat); + var x = Math.cos(alat) * Math.sin(blat) - + Math.sin(alat)*Math.cos(blat)*Math.cos(delta); + return Math.round(this.degrees(Math.atan2(y, x))); + }, }; -/* gps library v0.1 */ +/* gps library v0.1.4 */ let gps = { - emulator: -1, - init: function(x) { - this.emulator = (process.env.BOARD=="EMSCRIPTEN" - || process.env.BOARD=="EMSCRIPTEN2")?1:0; - }, - state: {}, - on_gps: function(f) { - let fix = this.getGPSFix(); - f(fix); + emulator: -1, + init: function(x) { + this.emulator = (process.env.BOARD=="EMSCRIPTEN" + || process.env.BOARD=="EMSCRIPTEN2")?1:0; + }, + state: {}, + on_gps: function(f) { + let fix = this.getGPSFix(); + f(fix); - /* - "lat": number, // Latitude in degrees - "lon": number, // Longitude in degrees - "alt": number, // altitude in M - "speed": number, // Speed in kph - "course": number, // Course in degrees - "time": Date, // Current Time (or undefined if not known) - "satellites": 7, // Number of satellites - "fix": 1 // NMEA Fix state - 0 is no fix - "hdop": number, // Horizontal Dilution of Precision - */ - this.state.timeout = setTimeout(this.on_gps, 1000, f); - }, - off_gps: function() { - clearTimeout(gps_state.timeout); - }, - getGPSFix: function() { - if (!this.emulator) - return Bangle.getGPSFix(); - let fix = {}; - fix.fix = 1; - fix.lat = 50; - fix.lon = 14; - fix.alt = 200; - fix.speed = 5; - fix.course = 30; - fix.time = Date(); - fix.satellites = 5; - fix.hdop = 12; - return fix; + /* + "lat": number, // Latitude in degrees + "lon": number, // Longitude in degrees + "alt": number, // altitude in M + "speed": number, // Speed in kph + "course": number, // Course in degrees + "time": Date, // Current Time (or undefined if not known) + "satellites": 7, // Number of satellites + "fix": 1 // NMEA Fix state - 0 is no fix + "hdop": number, // Horizontal Dilution of Precision + */ + this.state.timeout = setTimeout(this.on_gps, 1000, f); + }, + off_gps: function() { + clearTimeout(this.state.timeout); + }, + getGPSFix: function() { + if (!this.emulator) + return Bangle.getGPSFix(); + let fix = {}; + fix.fix = 1; + fix.lat = 50; + fix.lon = 14-(getTime()-this.gps_start) / 1000; /* Go West! */ + fix.alt = 200; + fix.speed = 5; + fix.course = 30; + fix.time = Date(); + fix.satellites = 5; + fix.hdop = 12; + return fix; + }, + gps_start : -1, + start_gps: function() { + Bangle.setGPSPower(1, "libgps"); + this.gps_start = getTime(); + }, + stop_gps: function() { + Bangle.setGPSPower(0, "libgps"); + }, +}; + +/* arrow library v0.0.1 */ +let arrow = { + waypoint: { lat: 0, lon: 0 }, + updateWaypoint: function(lat, lon) { + this.waypoint.lat = lat; + this.waypoint.lon = lon; + }, + + // Calculate the bearing to the waypoint + bearingToWaypoint: function(currentPos) { + return fmt.bearing(currentPos, this.waypoint); + }, + + // Calculate distance to the waypoint + distanceToWaypoint: function(currentPos) { + return fmt.distance(currentPos, this.waypoint); + }, + + // Calculate compass heading from current GPS data + compassHeading: function(currentHeading) { + return currentHeading || Bangle.getCompass().heading; + }, + + // Display function to show arrows for waypoint, north, and sun + displayNavigation: function(currentPos, currentHeading) { + g.clear().setFont("Vector", 22).setFontAlign(0, 0); + + // Calculate bearings + let waypointBearing = this.bearingToWaypoint(currentPos); + let distance = this.distanceToWaypoint(currentPos); + let northHeading = this.compassHeading(currentHeading); + + // Format distance + let distStr = fmt.fmtDist(distance/1000); + + northHeading = 45; + // Draw compass arrow for north + this.drawArrow(northHeading, "N", 1); + + // Draw arrow towards waypoint + if (1) + this.drawArrow(waypointBearing, `${distStr}`, 3); + + let s; + + s = sun.sunPos(); + // Draw sun arrow if sun is visible + if (s.altitude > 0) { + this.drawArrow(s.azimuth, "Sun", 1); } + s = sun.moonPos(); + // Draw sun arrow if sun is visible + if (s.altitude > 0) { + this.drawArrow(s.azimuth, "Moon", 1); + } + }, + + drawArrow: function(angle, label, width) { + // Convert angle to radians + let rad = angle * Math.PI / 180; + + // Arrow parameters + let centerX = 88; + let centerY = 88; + let length = 60; // Arrow length + + g.drawCircle(centerX, centerY, length); + + // Calculate the rectangle's corner points for the rotated arrow + let dx = Math.sin(rad) * length; + let dy = -Math.cos(rad) * length; + let px = Math.sin(rad + Math.PI / 2) * (width / 2); + let py = -Math.cos(rad + Math.PI / 2) * (width / 2); + + // Calculate each corner of the rectangle (arrow body) + let x1 = centerX + px; + let y1 = centerY + py; + let x2 = centerX - px; + let y2 = centerY - py; + let x3 = x2 + dx * 0.9; + let y3 = y2 + dy * 0.9; + let x4 = x1 + dx * 0.9; + let y4 = y1 + dy * 0.9; + + // Draw the filled rectangle for the arrow body + g.fillPoly([x1, y1, x2, y2, x3, y3, x4, y4]); + + // Draw the label at the arrow's endpoint + g.setFontAlign(0, 0); + g.drawString(label, x3 + dx * 0.4, y3 + dy * 0.4); + + // Draw arrowhead + this.drawArrowHead(rad, centerX + dx, centerY + dy); + }, + + drawArrowHead: function(rad, x, y) { + // Arrowhead parameters + let headLength = 16; // Length of each arrowhead side + let headAngle = Math.PI / 6; // Arrowhead angle + + let angle = rad - Math.PI/2; + + // Calculate positions for arrowhead points + let xHead1 = x - headLength * Math.cos(angle - headAngle); + let yHead1 = y - headLength * Math.sin(angle - headAngle); + let xHead2 = x - headLength * Math.cos(angle + headAngle); + let yHead2 = y - headLength * Math.sin(angle + headAngle); + + // Draw the arrowhead as a filled triangle + g.fillPoly([x, y, xHead1, yHead1, xHead2, yHead2]); + } }; var wp = require('Storage').readJSON("waypoints.json", true) || []; From de755fc78bc4b1482ab6ecb14a076f3a422fe387 Mon Sep 17 00:00:00 2001 From: Pavel Machek Date: Wed, 13 Nov 2024 11:28:07 +0100 Subject: [PATCH 09/12] waypoints: cleanups, mostly formatting / whitespace. --- apps/waypoints/waypoints.app.js | 223 ++++++++++++++++---------------- 1 file changed, 110 insertions(+), 113 deletions(-) diff --git a/apps/waypoints/waypoints.app.js b/apps/waypoints/waypoints.app.js index 6c1cbf633..ff991da11 100644 --- a/apps/waypoints/waypoints.app.js +++ b/apps/waypoints/waypoints.app.js @@ -1,7 +1,6 @@ /* Thanks to pinsafe from BangleApps repository */ var Layout = require("Layout"); -const BANGLEJS2 = process.env.HWVERSION == 2; // check for bangle 2 /* fmt library v0.2.2 */ let fmt = { @@ -28,9 +27,9 @@ let fmt = { fmtSteps: function(n) { return this.fmtDist(0.001 * 0.719 * n); }, fmtAlt: function(m) { return m.toFixed(0) + this.icon_alt; }, fmtTemp: function(c) { return c.toFixed(1) + this.icon_c; }, - fmtPress: function(p) { + fmtPress: function(p) { if (p < 900 || p > 1100) - return p.toFixed(0) + this.icon_hpa; + return p.toFixed(0) + this.icon_hpa; if (p < 1000) { p -= 900; return this.icon_9 + this.add0(p.toFixed(0)) + this.icon_hpa; @@ -135,7 +134,7 @@ let fmt = { let gps = { emulator: -1, init: function(x) { - this.emulator = (process.env.BOARD=="EMSCRIPTEN" + this.emulator = (process.env.BOARD=="EMSCRIPTEN" || process.env.BOARD=="EMSCRIPTEN2")?1:0; }, state: {}, @@ -191,7 +190,7 @@ let arrow = { this.waypoint.lat = lat; this.waypoint.lon = lon; }, - + // Calculate the bearing to the waypoint bearingToWaypoint: function(currentPos) { return fmt.bearing(currentPos, this.waypoint); @@ -210,7 +209,7 @@ let arrow = { // Display function to show arrows for waypoint, north, and sun displayNavigation: function(currentPos, currentHeading) { g.clear().setFont("Vector", 22).setFontAlign(0, 0); - + // Calculate bearings let waypointBearing = this.bearingToWaypoint(currentPos); let distance = this.distanceToWaypoint(currentPos); @@ -228,7 +227,7 @@ let arrow = { this.drawArrow(waypointBearing, `${distStr}`, 3); let s; - + s = sun.sunPos(); // Draw sun arrow if sun is visible if (s.altitude > 0) { @@ -249,7 +248,7 @@ let arrow = { let centerX = 88; let centerY = 88; let length = 60; // Arrow length - + g.drawCircle(centerX, centerY, length); // Calculate the rectangle's corner points for the rotated arrow @@ -319,47 +318,45 @@ function mainMenu() { var menu = { "< Back" : () => load() }; - if (textInputInstalled && BANGLEJS2) { - menu["Add"]=addCard; - } menu["Show"]=showCard; - menu["Remove"]=removeCard; - menu["Format"]=setFormat; - if (textInputInstalled && BANGLEJS2) { + if (textInputInstalled) { + menu["Add"]=addCard; menu["Mark GPS"]=markGps; } + menu["Remove"]=removeCard; + menu["Format"]=setFormat; g.clear(); E.showMenu(menu); } function updateGps() { - let have = false, lat = "lat ", alt = "?", - speed = "speed ", hdop = "?", adelta = "adelta ", - tdelta = "tdelta "; + let have = false, lat = "lat ", alt = "?", + speed = "speed ", hdop = "?", adelta = "adelta ", + tdelta = "tdelta "; - if (cancel_gps) - return; - fix = gps.getGPSFix(); + if (cancel_gps) + return; + fix = gps.getGPSFix(); - if (fix && fix.fix && fix.lat) { - lat = "" + fmt.fmtPos(fix); - alt = "" + fix.alt.toFixed(0); - speed = "" + fix.speed.toFixed(1); - hdop = "" + fix.hdop.toFixed(0); - have = true; - } else { - lat = "NO FIX\n" - + "" + (getTime() - gps_start).toFixed(0) + "s "; - } + if (fix && fix.fix && fix.lat) { + lat = "" + fmt.fmtPos(fix); + alt = "" + fix.alt.toFixed(0); + speed = "" + fix.speed.toFixed(1); + hdop = "" + fix.hdop.toFixed(0); + have = true; + } else { + lat = "NO FIX\n" + + "" + (getTime() - gps_start).toFixed(0) + "s "; + } - let msg = ""; - msg = lat + "\n"+ alt + "m"; - g.reset().setFont("Vector", 31) - .setColor(1,1,1) - .fillRect(0, 24, 176, 100) - .setColor(0,0,0) - .drawString(msg, 3, 25); - setTimeout(updateGps, 1000); + let msg = ""; + msg = lat + "\n"+ alt + "m"; + g.reset().setFont("Vector", 31) + .setColor(1,1,1) + .fillRect(0, 24, 176, 100) + .setColor(0,0,0) + .drawString(msg, 3, 25); + setTimeout(updateGps, 1000); } function stopGps() { @@ -368,22 +365,22 @@ function stopGps() { } function confirmGps(s) { - key = s; - var la = new Layout ( - {type:"v", c: [ - {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, - {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, - {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, - {type:"h", c: [ - {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "YES", cb:l=>{ - print("should mark", key, fix); createWP(fix.lat, fix.lon, fix.alt, key); cancel_gps=true; mainMenu(); - }}, - {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " NO", cb:l=>{ cancel_gps=true; mainMenu(); }} - ]} - ], lazy:true}); - g.clear(); - la.render(); - updateGps(); + key = s; + var la = new Layout ( + {type:"v", c: [ + {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, + {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, + {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, + {type:"h", c: [ + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "YES", cb:l=>{ + print("should mark", key, fix); createWP(fix.lat, fix.lon, fix.alt, key); cancel_gps=true; mainMenu(); + }}, + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " NO", cb:l=>{ cancel_gps=true; mainMenu(); }} + ]} + ], lazy:true}); + g.clear(); + la.render(); + updateGps(); } function markGps() { @@ -459,68 +456,68 @@ function showNumpad(text, key_, callback) { ]} ], lazy:true}); g.clear(); - numPad.render(); + numPad.render(); update(); } function show(pin) { - var i = wp[pin]; - var l = fmt.fmtPos(i); - E.showPrompt(l,{ - title:i["name"], - buttons : {"Ok":true} - }).then(function(v) { - mainMenu(); - }); + var i = wp[pin]; + var l = fmt.fmtPos(i); + E.showPrompt(l,{ + title:i["name"], + buttons : {"Ok":true} + }).then(function(v) { + mainMenu(); + }); } function showCard() { - var menu = { - "" : {title : "Select WP"}, - "< Back" : mainMenu - }; - if (Object.keys(wp).length==0) Object.assign(menu, {"No WPs":""}); - else { - wp.forEach((val, card) => { - const name = wp[card].name; - menu[name]= () => show(card); - }); - } - E.showMenu(menu); + var menu = { + "" : {title : "Select WP"}, + "< Back" : mainMenu + }; + if (Object.keys(wp).length==0) Object.assign(menu, {"No WPs":""}); + else { + wp.forEach((val, card) => { + const name = wp[card].name; + menu[name]= () => show(card); + }); + } + E.showMenu(menu); } function remove(pin) { - let card = wp[pin]; - let name = card["name"]; - print("Remove?", card, name); + let card = wp[pin]; + let name = card["name"]; + print("Remove?", card, name); - E.showPrompt(name,{ - title:"Delete", - }).then(function(v) { - if (v) { - wp.splice(pin, 1); - writeWP(); - mainMenu(); - } else { - mainMenu(); - } - }); + E.showPrompt(name,{ + title:"Delete", + }).then(function(v) { + if (v) { + wp.splice(pin, 1); + writeWP(); + mainMenu(); + } else { + mainMenu(); + } + }); } function removeCard() { - var menu = { - "" : {title : "Select WP"}, - "< Back" : mainMenu - }; - if (Object.keys(wp).length==0) Object.assign(menu, {"No WPs":""}); - else { - wp.forEach((val, card) => { - const name = wp[card].name; - menu[name]=()=> remove(card); - }); - } - E.showMenu(menu); + var menu = { + "" : {title : "Select WP"}, + "< Back" : mainMenu + }; + if (Object.keys(wp).length==0) Object.assign(menu, {"No WPs":""}); + else { + wp.forEach((val, card) => { + const name = wp[card].name; + menu[name]=()=> remove(card); + }); + } + E.showMenu(menu); } function ask01(t, cb) { @@ -598,18 +595,18 @@ function addCard() { require("textinput").input({text:"wp"}).then(key => { result = key; if (wp[result]!=undefined) { - E.showMenu(); - var alreadyExists = new Layout ( - {type:"v", c: [ - {type:"txt", font:Math.min(15,100/result.length)+"%", pad:1, fillx:1, filly:1, label:result}, - {type:"txt", font:"12%", pad:1, fillx:1, filly:1, label:"already exists."}, - {type:"h", c: [ - {type:"btn", font:"10%", pad:1, fillx:1, filly:1, label: "REPLACE", cb:l=>{addCardName(result);}}, - {type:"btn", font:"10%", pad:1, fillx:1, filly:1, label: "CANCEL", cb:l=>{mainMenu();}} - ]} - ], lazy:true}); - g.clear(); - alreadyExists.render(); + E.showMenu(); + var alreadyExists = new Layout ( + {type:"v", c: [ + {type:"txt", font:Math.min(15,100/result.length)+"%", pad:1, fillx:1, filly:1, label:result}, + {type:"txt", font:"12%", pad:1, fillx:1, filly:1, label:"already exists."}, + {type:"h", c: [ + {type:"btn", font:"10%", pad:1, fillx:1, filly:1, label: "REPLACE", cb:l=>{addCardName(result);}}, + {type:"btn", font:"10%", pad:1, fillx:1, filly:1, label: "CANCEL", cb:l=>{mainMenu();}} + ]} + ], lazy:true}); + g.clear(); + alreadyExists.render(); } addCardName(result); }); From 5219dc25dfb9ebe8e58a5ff982f2d9eb8ed923dd Mon Sep 17 00:00:00 2001 From: Pavel Machek Date: Wed, 13 Nov 2024 12:42:53 +0100 Subject: [PATCH 10/12] waypoints: implement goto functionality. --- apps/waypoints/waypoints.app.js | 114 +++++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 23 deletions(-) diff --git a/apps/waypoints/waypoints.app.js b/apps/waypoints/waypoints.app.js index ff991da11..468a43724 100644 --- a/apps/waypoints/waypoints.app.js +++ b/apps/waypoints/waypoints.app.js @@ -2,7 +2,7 @@ var Layout = require("Layout"); -/* fmt library v0.2.2 */ +/* fmt library v0.2.3 */ let fmt = { icon_alt : "\0\x08\x1a\1\x00\x00\x00\x20\x30\x78\x7C\xFE\xFF\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", icon_m : "\0\x08\x1a\1\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xC3\xE7\xFF\xDB\xC3\xC3\xC3\xC3\x00\x00\x00\x00\x00\x00\x00\x00", @@ -89,6 +89,7 @@ let fmt = { x = -x; } let s = c+this.fmtAngle(x) + "\n"; + x = pos.lon; c = "E"; if (x<0) { c = "W"; @@ -183,8 +184,9 @@ let gps = { }, }; -/* arrow library v0.0.1 */ +/* arrow library v0.0.2 */ let arrow = { + name: "(unset)", waypoint: { lat: 0, lon: 0 }, updateWaypoint: function(lat, lon) { this.waypoint.lat = lat; @@ -207,7 +209,7 @@ let arrow = { }, // Display function to show arrows for waypoint, north, and sun - displayNavigation: function(currentPos, currentHeading) { + draw: function(currentPos, currentHeading) { g.clear().setFont("Vector", 22).setFontAlign(0, 0); // Calculate bearings @@ -218,7 +220,7 @@ let arrow = { // Format distance let distStr = fmt.fmtDist(distance/1000); - northHeading = 45; + northHeading = 0; // Draw compass arrow for north this.drawArrow(northHeading, "N", 1); @@ -226,17 +228,18 @@ let arrow = { if (1) this.drawArrow(waypointBearing, `${distStr}`, 3); - let s; - - s = sun.sunPos(); - // Draw sun arrow if sun is visible - if (s.altitude > 0) { - this.drawArrow(s.azimuth, "Sun", 1); - } - s = sun.moonPos(); - // Draw sun arrow if sun is visible - if (s.altitude > 0) { - this.drawArrow(s.azimuth, "Moon", 1); + if (0) { + let s; + s = sun.sunPos(); + // Draw sun arrow if sun is visible + if (s.altitude > 0) { + this.drawArrow(s.azimuth, "Sun", 1); + } + s = sun.moonPos(); + // Draw sun arrow if sun is visible + if (s.altitude > 0) { + this.drawArrow(s.azimuth, "Moon", 1); + } } }, @@ -359,6 +362,40 @@ function updateGps() { setTimeout(updateGps, 1000); } +function updateGoto() { + let have = false, lat = "lat ", alt = "?", + speed = "speed ", hdop = "?", adelta = "adelta ", + tdelta = "tdelta "; + + if (cancel_gps) + return; + fix = gps.getGPSFix(); + + if (fix && fix.fix && fix.lat) { + lat = "" + fmt.fmtPos(fix); + alt = "" + fix.alt.toFixed(0); + speed = "" + fix.speed.toFixed(1); + hdop = "" + fix.hdop.toFixed(0); + have = true; + } else { + lat = "NO FIX\n" + + "" + (getTime() - gps_start).toFixed(0) + "s "; + } + + let msg = arrow.name + "\n"; + msg = lat + "\n"+ alt + "m"; + if (!have) { + g.reset().setFont("Vector", 31) + .setColor(1,1,1) + .fillRect(0, 24, 176, 100) + .setColor(0,0,0) + .drawString(msg, 3, 25); + } else { + arrow.draw(fix, fix.course); + } + setTimeout(updateGoto, 1000); +} + function stopGps() { cancel_gps=true; Bangle.setGPSPower(0, "waypoints"); @@ -372,10 +409,10 @@ function confirmGps(s) { {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, {type:"h", c: [ - {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "YES", cb:l=>{ + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "Yes", cb:l=>{ print("should mark", key, fix); createWP(fix.lat, fix.lon, fix.alt, key); cancel_gps=true; mainMenu(); }}, - {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " NO", cb:l=>{ cancel_gps=true; mainMenu(); }} + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " No", cb:l=>{ cancel_gps=true; mainMenu(); }} ]} ], lazy:true}); g.clear(); @@ -460,15 +497,46 @@ function showNumpad(text, key_, callback) { update(); } +function goTo() { + cancel_gps = false; + Bangle.setGPSPower(1, "waypoints"); + gps.gps_start = getTime(); + gps_start = getTime(); + + var la = new Layout ( + {type:"v", c: [ + {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, + {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, + {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, + {type:"h", c: [ + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " Done", cb:l=>{ cancel_gps=true; mainMenu(); }} + ]} + ], lazy:true}); + g.clear(); + la.render(); + + + updateGoto(); +} + function show(pin) { var i = wp[pin]; var l = fmt.fmtPos(i); - E.showPrompt(l,{ - title:i["name"], - buttons : {"Ok":true} - }).then(function(v) { - mainMenu(); - }); + arrow.name = i.name; + arrow.waypoint = i; + + var la = new Layout ( + {type:"v", c: [ + {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:arrow.name }, + {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:l }, + {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, + {type:"h", c: [ + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "Go", cb:l=>{ goTo(); }}, + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "Ok", cb:l=>{ mainMenu(); }} + ]} + ], lazy:true}); + g.clear(); + la.render(); } function showCard() { From 93076f12d5d0028107abbbb0ff8e04f0edcc9c0e Mon Sep 17 00:00:00 2001 From: Pavel Machek Date: Wed, 13 Nov 2024 12:53:52 +0100 Subject: [PATCH 11/12] waypoints: mark this as 0.05 version --- apps/waypoints/ChangeLog | 1 + apps/waypoints/metadata.json | 2 +- apps/waypoints/waypoints.app.js | 5 ++--- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/waypoints/ChangeLog b/apps/waypoints/ChangeLog index f8863ebaf..e613e1227 100644 --- a/apps/waypoints/ChangeLog +++ b/apps/waypoints/ChangeLog @@ -3,3 +3,4 @@ 0.03: Do not register as type waypoint - show in launcher Fixes for Bangle.js 1 & not installed textinput 0.04: Minor code improvements +0.05: Implement navigation to waypoint diff --git a/apps/waypoints/metadata.json b/apps/waypoints/metadata.json index bc88cff2e..8833c228d 100644 --- a/apps/waypoints/metadata.json +++ b/apps/waypoints/metadata.json @@ -1,6 +1,6 @@ { "id": "waypoints", "name": "Waypoints", - "version": "0.04", + "version": "0.05", "description": "Provides 'waypoints.json' used by various navigation apps, as well as a way to edit it from the App Loader or from the device", "icon": "app.png", "tags": "tool,outdoors,gps", diff --git a/apps/waypoints/waypoints.app.js b/apps/waypoints/waypoints.app.js index 468a43724..3398ce14d 100644 --- a/apps/waypoints/waypoints.app.js +++ b/apps/waypoints/waypoints.app.js @@ -502,7 +502,7 @@ function goTo() { Bangle.setGPSPower(1, "waypoints"); gps.gps_start = getTime(); gps_start = getTime(); - + var la = new Layout ( {type:"v", c: [ {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, @@ -515,7 +515,6 @@ function goTo() { g.clear(); la.render(); - updateGoto(); } @@ -524,7 +523,7 @@ function show(pin) { var l = fmt.fmtPos(i); arrow.name = i.name; arrow.waypoint = i; - + var la = new Layout ( {type:"v", c: [ {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:arrow.name }, From e6312218a9687696686e225b84b10a59645b9d8d Mon Sep 17 00:00:00 2001 From: Pavel Machek Date: Wed, 13 Nov 2024 13:52:05 +0100 Subject: [PATCH 12/12] waypoints: fix/workaround warnings. The code still needs refactoring, but make lint happy for now. --- apps/waypoints/waypoints.app.js | 82 +++++++++++++++++---------------- 1 file changed, 42 insertions(+), 40 deletions(-) diff --git a/apps/waypoints/waypoints.app.js b/apps/waypoints/waypoints.app.js index 3398ce14d..238dd2fa1 100644 --- a/apps/waypoints/waypoints.app.js +++ b/apps/waypoints/waypoints.app.js @@ -184,6 +184,8 @@ let gps = { }, }; +let sun = {}; /* To get rid of warnings */ + /* arrow library v0.0.2 */ let arrow = { name: "(unset)", @@ -333,9 +335,7 @@ function mainMenu() { } function updateGps() { - let have = false, lat = "lat ", alt = "?", - speed = "speed ", hdop = "?", adelta = "adelta ", - tdelta = "tdelta "; + let lat = "lat ", alt = "?"; if (cancel_gps) return; @@ -344,9 +344,8 @@ function updateGps() { if (fix && fix.fix && fix.lat) { lat = "" + fmt.fmtPos(fix); alt = "" + fix.alt.toFixed(0); - speed = "" + fix.speed.toFixed(1); - hdop = "" + fix.hdop.toFixed(0); - have = true; + // speed = "" + fix.speed.toFixed(1); + // hdop = "" + fix.hdop.toFixed(0); } else { lat = "NO FIX\n" + "" + (getTime() - gps_start).toFixed(0) + "s "; @@ -363,9 +362,7 @@ function updateGps() { } function updateGoto() { - let have = false, lat = "lat ", alt = "?", - speed = "speed ", hdop = "?", adelta = "adelta ", - tdelta = "tdelta "; + let have = false, lat = "lat ", alt = "?"; if (cancel_gps) return; @@ -374,8 +371,8 @@ function updateGoto() { if (fix && fix.fix && fix.lat) { lat = "" + fmt.fmtPos(fix); alt = "" + fix.alt.toFixed(0); - speed = "" + fix.speed.toFixed(1); - hdop = "" + fix.hdop.toFixed(0); + // speed = "" + fix.speed.toFixed(1); + // hdop = "" + fix.hdop.toFixed(0); have = true; } else { lat = "NO FIX\n" @@ -409,10 +406,10 @@ function confirmGps(s) { {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, {type:"h", c: [ - {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "Yes", cb:l=>{ - print("should mark", key, fix); createWP(fix.lat, fix.lon, fix.alt, key); cancel_gps=true; mainMenu(); - }}, - {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " No", cb:l=>{ cancel_gps=true; mainMenu(); }} + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "Yes", cb:l=>{ + print("should mark", key, fix); createWP(fix.lat, fix.lon, fix.alt, key); stopGps(); mainMenu(); + }}, + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " No", cb:l=>{ stopGps(); mainMenu(); }} ]} ], lazy:true}); g.clear(); @@ -447,7 +444,7 @@ function showNumpad(text, key_, callback) { function addDigit(digit) { key+=digit; if (1) { - l = text[key.length]; + let l = text[key.length]; switch (l) { case '.': case ' ': case "'": key+=l; @@ -462,10 +459,10 @@ function showNumpad(text, key_, callback) { function update() { g.reset(); g.clearRect(0,0,g.getWidth(),23); - s = key + text.substr(key.length, 999); + let s = key + text.substr(key.length, 999); g.setFont("Vector:24").setFontAlign(1,0).drawString(s,g.getWidth(),12); } - ds="12%"; + let ds="12%"; var numPad = new Layout ({ type:"v", c: [{ type:"v", c: [ @@ -509,7 +506,7 @@ function goTo() { {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, {type:"h", c: [ - {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " Done", cb:l=>{ cancel_gps=true; mainMenu(); }} + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: " Done", cb:l=>{ stopGps(); mainMenu(); }} ]} ], lazy:true}); g.clear(); @@ -530,8 +527,8 @@ function show(pin) { {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:l }, {type:"txt", font:"15%", pad:1, fillx:1, filly:1, label:""}, {type:"h", c: [ - {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "Go", cb:l=>{ goTo(); }}, - {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "Ok", cb:l=>{ mainMenu(); }} + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "Go", cb:l=>{ goTo(); }}, + {type:"btn", font:"15%", pad:1, fillx:1, filly:1, label: "Ok", cb:l=>{ mainMenu(); }} ]} ], lazy:true}); g.clear(); @@ -598,29 +595,32 @@ function ask01(t, cb) { la.render(); } +var res; + function askCoordinate(t1, t2, callback) { //let sign = 1; ask01(t1, function(sign) { + let d, m, s; + switch (fmt.geo_mode) { + case 0: s = "DDD.dddd"; break; + case 1: s = "DDD MM.mmm"; break; + case 2: s = "DDD MM'ss"+'"'; break; + } + showNumpad(s, t2, function() { switch (fmt.geo_mode) { - case 0: s = "DDD.dddd"; break; - case 1: s = "DDD MM.mmm"; break; - case 2: s = "DDD MM'ss"+'"'; break; - } - showNumpad(s, t2, function() { - switch (fmt.geo_mode) { - case 0: - res = parseFloat(key); - break; - case 1: - d = parseInt(key.substr(0, 3)); - m = parseFloat(key.substr(3,99)); - res = d + m/60.0; - break; - case 2: - d = parseInt(key.substr(0, 3)); - m = parseInt(key.substr(4, 2)); - s = parseInt(key.substr(7, 2)); - res = d + m/60.0 + s/3600.0; + case 0: + res = parseFloat(key); + break; + case 1: + d = parseInt(key.substr(0, 3)); + m = parseFloat(key.substr(3,99)); + res = d + m/60.0; + break; + case 2: + d = parseInt(key.substr(0, 3)); + m = parseInt(key.substr(4, 2)); + s = parseInt(key.substr(7, 2)); + res = d + m/60.0 + s/3600.0; } res = sign * res; print("Coordinate", res); @@ -649,6 +649,8 @@ function createWP(lat, lon, alt, name) { writeWP(); } +var result; + function addCardName(name) { g.clear(); askPosition(function(lat, lon) {