Created a speed module (based on GPS data)
This commit is contained in:
parent
8b4a325e49
commit
215ad65f3d
98
speed.trailsense.js
Normal file
98
speed.trailsense.js
Normal file
@ -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 };
|
||||
})();
|
Loading…
Reference in New Issue
Block a user