2020-01-03 07:18:44 +00:00
|
|
|
(() => {
|
2020-02-28 11:44:25 +00:00
|
|
|
const PEDOMFILE = "wpedom.json";
|
2020-02-07 13:44:55 +00:00
|
|
|
let lastUpdate = new Date();
|
|
|
|
let stp_today = 0;
|
2020-01-03 07:18:44 +00:00
|
|
|
|
2020-03-05 13:15:27 +00:00
|
|
|
// draw your widget
|
2020-02-07 13:44:55 +00:00
|
|
|
function draw() {
|
2020-03-05 13:15:27 +00:00
|
|
|
var width = 24;
|
2020-02-07 13:44:55 +00:00
|
|
|
if (stp_today > 99999){
|
|
|
|
stp_today = stp_today % 100000; // cap to five digits + comma = 6 characters
|
2020-01-03 07:18:44 +00:00
|
|
|
}
|
2020-02-07 13:44:55 +00:00
|
|
|
let stps = stp_today.toString();
|
2020-02-13 10:39:57 +00:00
|
|
|
g.reset();
|
2020-02-07 13:44:55 +00:00
|
|
|
if (stps.length > 3){
|
|
|
|
stps = stps.slice(0,-3) + "," + stps.slice(-3);
|
2020-02-13 10:39:57 +00:00
|
|
|
g.setFont("4x6", 1); // if big, shrink text to fix
|
|
|
|
} else {
|
|
|
|
g.setFont("6x8", 1);
|
2020-02-07 13:44:55 +00:00
|
|
|
}
|
2020-02-13 12:08:43 +00:00
|
|
|
g.setFontAlign(0, 0); // align to x: center, y: center
|
2020-03-05 13:15:27 +00:00
|
|
|
g.clearRect(this.x,this.y+15,this.x+width,this.y+23); // erase background
|
|
|
|
g.drawString(stps, this.x+width/2, this.y+19);
|
|
|
|
g.drawImage(atob("CgoCLguH9f2/7+v6/79f56CtAAAD9fw/n8Hx9A=="),this.x+(width-10)/2,this.y+2);
|
2020-02-07 13:44:55 +00:00
|
|
|
}
|
2020-01-03 07:18:44 +00:00
|
|
|
|
2020-02-07 13:44:55 +00:00
|
|
|
Bangle.on('step', (up) => {
|
|
|
|
let date = new Date();
|
|
|
|
if (lastUpdate.getDate() == date.getDate()){
|
2020-02-13 10:39:57 +00:00
|
|
|
stp_today ++;
|
2020-02-07 13:44:55 +00:00
|
|
|
} else {
|
2020-02-13 10:39:57 +00:00
|
|
|
// TODO: could save this to PEDOMFILE for lastUpdate's day?
|
2020-02-07 13:44:55 +00:00
|
|
|
stp_today = 1;
|
2020-01-03 07:18:44 +00:00
|
|
|
}
|
2020-02-07 13:44:55 +00:00
|
|
|
lastUpdate = date;
|
|
|
|
//console.log("up: " + up + " stp: " + stp_today + " " + date.toString());
|
2020-03-09 14:30:56 +00:00
|
|
|
if (Bangle.isLCDOn()) WIDGETS["wpedom"].draw();
|
2020-02-13 09:13:41 +00:00
|
|
|
});
|
|
|
|
// redraw when the LCD turns on
|
|
|
|
Bangle.on('lcdPower', function(on) {
|
2020-03-09 14:30:56 +00:00
|
|
|
if (on) WIDGETS["wpedom"].draw();
|
2020-02-07 13:44:55 +00:00
|
|
|
});
|
|
|
|
// When unloading, save state
|
|
|
|
E.on('kill', () => {
|
|
|
|
let d = {
|
2020-02-13 09:28:14 +00:00
|
|
|
lastUpdate : lastUpdate.toISOString(),
|
2020-02-07 13:44:55 +00:00
|
|
|
stepsToday : stp_today
|
|
|
|
};
|
|
|
|
require("Storage").write(PEDOMFILE,d);
|
|
|
|
});
|
2020-01-03 07:18:44 +00:00
|
|
|
|
2020-02-07 13:44:55 +00:00
|
|
|
// add your widget
|
2020-03-05 13:15:27 +00:00
|
|
|
WIDGETS["wpedom"]={area:"tl",width:26,draw:draw};
|
2020-02-07 13:44:55 +00:00
|
|
|
// Load data at startup
|
2020-02-28 17:02:26 +00:00
|
|
|
let pedomData = require("Storage").readJSON(PEDOMFILE,1);
|
2020-02-07 13:44:55 +00:00
|
|
|
if (pedomData) {
|
|
|
|
if (pedomData.lastUpdate)
|
|
|
|
lastUpdate = new Date(pedomData.lastUpdate);
|
|
|
|
stp_today = pedomData.stepsToday|0;
|
|
|
|
}
|
2020-01-03 07:18:44 +00:00
|
|
|
})()
|