2020-01-03 07:18:44 +00:00
|
|
|
(() => {
|
2020-04-08 20:30:28 +00:00
|
|
|
const PEDOMFILE = "wpedom.json"
|
2020-02-07 13:44:55 +00:00
|
|
|
let lastUpdate = new Date();
|
|
|
|
let stp_today = 0;
|
2020-04-08 20:30:28 +00:00
|
|
|
let settings;
|
|
|
|
|
|
|
|
function loadSettings() {
|
2020-04-09 10:05:25 +00:00
|
|
|
const d = require('Storage').readJSON(PEDOMFILE, 1) || {};
|
|
|
|
settings = d.settings || {};
|
2020-04-08 20:30:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function setting(key) {
|
|
|
|
if (!settings) { loadSettings() }
|
2021-06-22 08:36:57 +00:00
|
|
|
const DEFAULTS = {
|
|
|
|
'goal': 10000,
|
|
|
|
'progress': false,
|
|
|
|
}
|
2020-04-08 20:30:28 +00:00
|
|
|
return (key in settings) ? settings[key] : DEFAULTS[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
function drawProgress(stps) {
|
2020-04-09 10:05:25 +00:00
|
|
|
const width = 24, half = width/2;
|
|
|
|
const goal = setting('goal'), left = Math.max(goal-stps,0);
|
2021-05-26 20:06:48 +00:00
|
|
|
const c = left ? "#00f" : "#090"; // blue or dark green
|
2020-04-09 10:05:25 +00:00
|
|
|
g.setColor(c).fillCircle(this.x + half, this.y + half, half);
|
2021-06-22 08:36:57 +00:00
|
|
|
const TAU = Math.PI*2;
|
2020-04-09 10:05:25 +00:00
|
|
|
if (left) {
|
|
|
|
const f = left/goal; // fraction to blank out
|
|
|
|
let p = [];
|
|
|
|
p.push(half,half);
|
|
|
|
p.push(half,0);
|
|
|
|
if(f>1/8) p.push(0,0);
|
|
|
|
if(f>2/8) p.push(0,half);
|
|
|
|
if(f>3/8) p.push(0,width);
|
|
|
|
if(f>4/8) p.push(half,width);
|
|
|
|
if(f>5/8) p.push(width,width);
|
|
|
|
if(f>6/8) p.push(width,half);
|
|
|
|
if(f>7/8) p.push(width,0);
|
|
|
|
p.push(half - Math.sin(f * TAU) * half);
|
|
|
|
p.push(half - Math.cos(f * TAU) * half);
|
|
|
|
for (let i = p.length; i; i -= 2) {
|
|
|
|
p[i - 2] += this.x;
|
|
|
|
p[i - 1] += this.y;
|
2020-04-08 20:30:28 +00:00
|
|
|
}
|
2021-05-26 20:06:48 +00:00
|
|
|
g.setColor(g.theme.bg).fillPoly(p);
|
2020-04-08 20:30:28 +00:00
|
|
|
}
|
|
|
|
}
|
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();
|
2021-05-26 20:06:48 +00:00
|
|
|
g.reset().clearRect(this.x, this.y, this.x + width, this.y + 23); // erase background
|
2020-04-09 10:05:25 +00:00
|
|
|
if (setting('progress')){ drawProgress.call(this, stps); }
|
2021-05-26 20:06:48 +00:00
|
|
|
g.setColor(g.theme.fg);
|
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.drawString(stps, this.x+width/2, this.y+19);
|
2021-06-22 08:36:57 +00:00
|
|
|
// on low bpp screens, draw 1 bit. Currently there is no getBPP so we just do it based on resolution
|
2020-03-05 13:15:27 +00:00
|
|
|
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-04-08 20:30:28 +00:00
|
|
|
function reload() {
|
|
|
|
loadSettings()
|
|
|
|
draw()
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2021-03-20 18:24:45 +00:00
|
|
|
if (stp_today === setting('goal')
|
|
|
|
&& !(require('Storage').readJSON('setting.json',1)||{}).quiet) {
|
2020-04-08 20:30:28 +00:00
|
|
|
let b = 3, buzz = () => {
|
|
|
|
if (b--) Bangle.buzz().then(() => setTimeout(buzz, 100))
|
|
|
|
}
|
|
|
|
buzz()
|
|
|
|
}
|
|
|
|
lastUpdate = date
|
2020-02-07 13:44:55 +00:00
|
|
|
//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', () => {
|
2020-04-09 10:05:25 +00:00
|
|
|
if (!settings) { loadSettings() }
|
2020-02-07 13:44:55 +00:00
|
|
|
let d = {
|
2020-02-13 09:28:14 +00:00
|
|
|
lastUpdate : lastUpdate.toISOString(),
|
2020-04-09 10:05:25 +00:00
|
|
|
stepsToday : stp_today,
|
|
|
|
settings : settings,
|
2020-02-07 13:44:55 +00:00
|
|
|
};
|
|
|
|
require("Storage").write(PEDOMFILE,d);
|
|
|
|
});
|
2020-01-03 07:18:44 +00:00
|
|
|
|
2020-02-07 13:44:55 +00:00
|
|
|
// add your widget
|
2021-01-25 12:57:08 +00:00
|
|
|
WIDGETS["wpedom"]={area:"tl",width:26,
|
2021-01-26 20:12:12 +00:00
|
|
|
draw:draw,
|
|
|
|
reload:reload,
|
|
|
|
getSteps:()=>stp_today
|
|
|
|
};
|
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;
|
2021-06-22 08:36:57 +00:00
|
|
|
delete pedomData;
|
2020-02-07 13:44:55 +00:00
|
|
|
}
|
2020-01-03 07:18:44 +00:00
|
|
|
})()
|