BangleApps/apps/run/app.js

100 lines
3.1 KiB
JavaScript
Raw Normal View History

var ExStats = require("exstats");
2022-03-01 05:54:27 +00:00
var B2 = process.env.HWVERSION===2;
2022-01-12 15:46:38 +00:00
var Layout = require("Layout");
var locale = require("locale");
2022-01-12 15:46:38 +00:00
var fontHeading = "6x8:2";
var fontValue = B2 ? "6x15:2" : "6x8:3";
var headingCol = "#888";
2022-01-22 00:14:10 +00:00
var fixCount = 0;
var isMenuDisplayed = false;
2022-01-12 15:46:38 +00:00
g.clear();
Bangle.loadWidgets();
Bangle.drawWidgets();
// ---------------------------
let settings = Object.assign({
2022-03-01 06:16:24 +00:00
record: true,
B1: "dist",
B2: "time",
B3: "pacea",
B4: "bpm",
B5: "step",
B6: "caden",
paceLength: 1000
}, require("Storage").readJSON("run.json", 1) || {});
2022-03-01 05:54:27 +00:00
var statIDs = [settings.B1,settings.B2,settings.B3,settings.B4,settings.B5,settings.B6].filter(s=>s!=="");
var exs = ExStats.getStats(statIDs, settings);
2022-01-12 15:46:38 +00:00
// ---------------------------
// Called to start/stop running
2022-01-12 15:46:38 +00:00
function onStartStop() {
var running = !exs.state.active;
2022-01-12 15:46:38 +00:00
if (running) {
exs.start();
} else {
exs.stop();
2022-01-12 15:46:38 +00:00
}
2022-01-12 15:59:30 +00:00
layout.button.label = running ? "STOP" : "START";
layout.status.label = running ? "RUN" : "STOP";
layout.status.bgCol = running ? "#0f0" : "#f00";
2022-01-12 15:46:38 +00:00
// if stopping running, don't clear state
// so we can at least refer to what we've done
layout.render();
// start/stop recording
if (settings.record && WIDGETS["recorder"]) {
if (running) {
isMenuDisplayed = true;
WIDGETS["recorder"].setRecording(true).then(() => {
isMenuDisplayed = false;
layout.forgetLazyState();
layout.render();
});
} else {
WIDGETS["recorder"].setRecording(false);
}
}
2022-01-12 15:46:38 +00:00
}
var lc = [];
// Load stats in pair by pair
for (var i=0;i<statIDs.length;i+=2) {
var sa = exs.stats[statIDs[i+0]];
var sb = exs.stats[statIDs[i+1]];
lc.push({ type:"h", filly:1, c:[
sa?{type:"txt", font:fontHeading, label:sa.title.toUpperCase(), fillx:1, col:headingCol }:{},
sb?{type:"txt", font:fontHeading, label:sb.title.toUpperCase(), fillx:1, col:headingCol }:{}
]}, { type:"h", filly:1, c:[
sa?{type:"txt", font:fontValue, label:sa.getString(), id:sa.id, fillx:1 }:{},
sb?{type:"txt", font:fontValue, label:sb.getString(), id:sb.id, fillx:1 }:{}
]});
if (sa) sa.on('changed', e=>layout[e.id].label = e.getString());
if (sb) sb.on('changed', e=>layout[e.id].label = e.getString());
}
// At the bottom put time/GPS state/etc
lc.push({ type:"h", filly:1, c:[
{type:"txt", font:fontHeading, label:"GPS", id:"gps", fillx:1, bgCol:"#f00" },
{type:"txt", font:fontHeading, label:"00:00", id:"clock", fillx:1, bgCol:g.theme.fg, col:g.theme.bg },
{type:"txt", font:fontHeading, label:"STOP", id:"status", fillx:1 }
]});
// Now calculate the layout
2022-01-12 15:46:38 +00:00
var layout = new Layout( {
type:"v", c: lc
2022-01-12 15:46:38 +00:00
},{lazy:true, btns:[{ label:"START", cb: onStartStop, id:"button"}]});
delete lc;
2022-01-12 15:46:38 +00:00
layout.render();
// Handle GPS state change for icon
2022-01-12 15:46:38 +00:00
Bangle.on("GPS", function(fix) {
layout.gps.bgCol = fix.fix ? "#0f0" : "#f00";
if (!fix.fix) return; // only process actual fixes
2022-03-01 05:54:27 +00:00
if (fixCount++ === 0) {
2022-01-22 00:14:10 +00:00
Bangle.buzz(); // first fix, does not need to respect quiet mode
2022-01-12 15:46:38 +00:00
}
});
// We always call ourselves once a second to update
setInterval(function() {
layout.clock.label = locale.time(new Date(),1);
if (!isMenuDisplayed) layout.render();
}, 1000);