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