diff --git a/speed.trailsense.js b/speed.trailsense.js new file mode 100644 index 0000000..40367d7 --- /dev/null +++ b/speed.trailsense.js @@ -0,0 +1,98 @@ +(() => { +let STOR = require("Storage"); +let updateInterval; +let lastSpeed; + +function drawFace() { + // variables + var first_run = true; + var naNShown = false; // just for performance; if the speed is unknown, then refresh the display once + var speedFont = "6x8"; + var speedSize = 8; + var speedNaNSize = 3; + var textFont = "Vector"; + var textSize = 14; + var linePadding = 50; + var lineWidth = 2; + var maxSpeedFont = "Vector"; + var maxSpeedSize = 20; + var maxSpeedTextSize = 10; + var maxSpeed = 0; // speed score + var maxSpeedEver = Number(STOR.read("maxspeed.trailsense.json")) || 0; + + // Some initialization + Bangle.setGPSPower(true); + g.setColor(g.theme.bg); + g.fillRect(0,0,g.getWidth(), g.getHeight()); + g.setColor(g.theme.fg2); + g.fillRect(0, linePadding-2, g.getWidth(), linePadding); + g.fillRect(0, g.getHeight()-linePadding, g.getWidth(), g.getHeight()-linePadding+2); + // text + g.setColor(g.theme.fg2); + g.setFont(textFont, textSize); + g.drawString("km/h", g.getWidth()/2, g.getHeight() - linePadding - 20); + g.drawString("Speed:", g.getWidth()/2, linePadding + 20); + + g.setColor(g.theme.fg2); + g.setFont(maxSpeedFont, maxSpeedTextSize); + g.drawString("Maxspeed [km/h]: NOW/EVER", g.getWidth()/2, g.getHeight()-maxSpeedTextSize/2); + + Bangle.loadWidgets(); + Bangle.drawWidgets(); + + // utilitiy functions + function round(value, decimals) { + return Number(Math.round(value+'e'+decimals)+'e-'+decimals); + } + + + 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; + + 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); + + // 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); + + naNShown = true; + } + lastSpeed = speed; // set last speed var for next loop + }, 100) +} + +function stop() { + g.clear(); + Bangle.setGPSPower(false); + print("[DEBUG] updateInterval=" + updateInterval); + clearInterval(updateInterval); +} + +return { drawFace:drawFace, stop:stop }; +})();