Changed setInterval() to Bangle.on(GPS)

This commit is contained in:
Blue Fox 2022-06-06 13:30:48 +02:00
parent a6df06345b
commit ac46c10ccf

View File

@ -1,6 +1,5 @@
(() => { (() => {
let STOR = require("Storage"); let STOR = require("Storage");
let updateInterval;
let lastSpeed; let lastSpeed;
function drawFace() { function drawFace() {
@ -46,52 +45,50 @@ function drawFace() {
} }
updateInterval = setInterval( Bangle.on("GPS",
() => { (gps_fix) => {
var gps_fix = Bangle.getGPSFix(); var speed = round(gps_fix.speed, 1);
var speed = round(gps_fix.speed, 1); if(speed<3.4) speed="0.0";
if(speed<3.4) speed="0.0"; if(Number(speed)>maxSpeed) maxSpeed = speed;
if(Number(speed)>maxSpeed) maxSpeed = speed; if(maxSpeed > maxSpeedEver) { STOR.write("maxspeed.trailsense.json", maxSpeed.toString()); }
if(maxSpeed > maxSpeedEver) { STOR.write("maxspeed.trailsense.json", maxSpeed.toString()); } maxSpeedEver = Number(STOR.read("maxspeed.trailsense.json")) || 0;
maxSpeedEver = Number(STOR.read("maxspeed.trailsense.json")) || 0;
if((speed != lastSpeed || first_run) && !isNaN(speed)) { if((speed != lastSpeed || first_run) && !isNaN(speed)) {
first_run = false; first_run = false;
naNShown = false; // update naNShown for accidently signal loss (so that the loop down there can run again) naNShown = false; // update naNShown for accidently signal loss (so that the loop down there can run again)
// clear screen // clear screen
g.setColor(g.theme.bg); g.setColor(g.theme.bg);
g.fillRect(0, g.getHeight()/2-30, g.getWidth(), g.getHeight()/2+30); g.fillRect(0, g.getHeight()/2-30, g.getWidth(), g.getHeight()/2+30);
// draw speed // draw speed
g.setColor(g.theme.fg); g.setColor(g.theme.fg);
g.setFont(speedFont, speedSize); g.setFont(speedFont, speedSize);
g.drawString(speed, g.getWidth()/2, g.getHeight()/2); g.drawString(speed, g.getWidth()/2, g.getHeight()/2);
// draw speed highscore // draw speed highscore
g.setColor(g.theme.bg); g.setColor(g.theme.bg);
g.fillRect(0, g.getHeight()-maxSpeedSize*2, g.getWidth(), g.getHeight()-maxSpeedSize/2) g.fillRect(0, g.getHeight()-maxSpeedSize*2, g.getWidth(), g.getHeight()-maxSpeedSize/2)
g.setColor(g.theme.fg); g.setColor(g.theme.fg);
g.setFont(maxSpeedFont, maxSpeedSize); g.setFont(maxSpeedFont, maxSpeedSize);
g.drawString(round(maxSpeed, 1) + " / " + maxSpeedEver, g.getWidth()/2, g.getHeight()-maxSpeedSize); g.drawString(round(maxSpeed, 1) + " / " + maxSpeedEver, g.getWidth()/2, g.getHeight()-maxSpeedSize);
} else if(isNaN(speed) && !naNShown) { } else if(isNaN(speed) && !naNShown) {
g.setColor(g.theme.bg); g.setColor(g.theme.bg);
g.fillRect(0, g.getHeight()/2-30, g.getWidth(), g.getHeight()/2+30); g.fillRect(0, g.getHeight()/2-30, g.getWidth(), g.getHeight()/2+30);
g.setColor(g.theme.fg); g.setColor(g.theme.fg);
g.setFont(speedFont, speedNaNSize); g.setFont(speedFont, speedNaNSize);
g.drawString("Loading...", g.getWidth()/2, g.getHeight()/2); g.drawString("Loading...", g.getWidth()/2, g.getHeight()/2);
naNShown = true; naNShown = true;
} }
lastSpeed = speed; // set last speed var for next loop lastSpeed = speed; // set last speed var for next loop
}, 100) })
} }
function stop() { function stop() {
g.clear(); g.clear();
Bangle.removeAllListeners();
Bangle.setGPSPower(false); Bangle.setGPSPower(false);
print("[DEBUG] updateInterval=" + updateInterval);
clearInterval(updateInterval);
} }
return { drawFace:drawFace, stop:stop }; return { drawFace:drawFace, stop:stop };