2023-02-19 20:48:35 +00:00
|
|
|
/* run widgets in their own function scope so they don't interfere with
|
|
|
|
currently-running apps */
|
|
|
|
(() => {
|
2023-02-20 19:56:12 +00:00
|
|
|
const s = require("Storage").readJSON("powermanager.json") || {};
|
|
|
|
|
|
|
|
if (!s.widget) return;
|
|
|
|
|
2023-02-22 17:44:49 +00:00
|
|
|
const SYSTICKMAX = peek32(0xE000E014);
|
|
|
|
const SYSTICKWAIT = SYSTICKMAX/64000; // 64 MHz clock rate, Systick counting down on every non idle clock
|
2023-02-20 23:00:43 +00:00
|
|
|
|
2023-02-20 18:14:02 +00:00
|
|
|
const GU = require("graphics_utils");
|
2023-02-19 20:48:35 +00:00
|
|
|
const APPROX_IDLE = 0.3;
|
|
|
|
const APPROX_HIGH_BW_BLE = 0.5;
|
|
|
|
const APPROX_COMPASS = process.HWVERSION == 2 ? 5.5 : 2;
|
|
|
|
const APPROX_HRM = process.HWVERSION == 2 ? 1 : 2.5;
|
|
|
|
const APPROX_CPU = 3;
|
|
|
|
const APPROX_GPS = process.HWVERSION == 2 ? 25 : 30;
|
|
|
|
const APPROX_TOUCH = 2.5;
|
|
|
|
const APPROX_BACKLIGHT = process.HWVERSION == 2 ? 16 : 40;
|
|
|
|
const MAX = APPROX_IDLE + APPROX_HIGH_BW_BLE + APPROX_COMPASS + APPROX_HRM + APPROX_CPU + APPROX_GPS + APPROX_TOUCH + APPROX_BACKLIGHT;
|
|
|
|
|
2023-02-20 18:39:31 +00:00
|
|
|
let settings = require("Storage").readJSON("setting.json") || {};
|
|
|
|
|
|
|
|
let brightnessSetting = settings.brightness || 1;
|
|
|
|
Bangle.setLCDBrightness = ((o) => (a) => {
|
|
|
|
brightnessSetting = a;
|
2023-02-25 12:13:04 +00:00
|
|
|
WIDGETS.powermanager.draw(WIDGETS.powermanager);
|
2023-02-20 18:39:31 +00:00
|
|
|
return o(a);
|
|
|
|
})(Bangle.setLCDBrightness);
|
|
|
|
|
|
|
|
let brightness = () => {
|
|
|
|
return process.HWVERSION == 2 ? (brightnessSetting * APPROX_BACKLIGHT) : (brightnessSetting * 0.9 * 33 + 7);
|
|
|
|
};
|
|
|
|
|
2023-02-22 17:44:49 +00:00
|
|
|
function doDraw(w, cpu){
|
2023-02-20 18:14:02 +00:00
|
|
|
g.reset();
|
2023-02-19 20:48:35 +00:00
|
|
|
|
2023-02-20 23:00:43 +00:00
|
|
|
let current = APPROX_IDLE + cpu * APPROX_CPU;
|
2023-02-20 19:05:28 +00:00
|
|
|
let mostExpensive = "P";
|
|
|
|
|
2023-02-20 18:39:31 +00:00
|
|
|
if (!Bangle.isLocked()) current += APPROX_TOUCH + brightness();
|
2023-02-20 19:05:28 +00:00
|
|
|
if (Bangle.isCompassOn()) {
|
|
|
|
current += APPROX_COMPASS;
|
|
|
|
mostExpensive = "C";
|
|
|
|
}
|
|
|
|
if (Bangle.isHRMOn()) {
|
|
|
|
current += APPROX_HRM;
|
|
|
|
mostExpensive = "H";
|
|
|
|
}
|
|
|
|
if (Bangle.isGPSOn()) {
|
|
|
|
current += APPROX_GPS;
|
|
|
|
mostExpensive = "G";
|
|
|
|
}
|
2023-02-20 18:14:02 +00:00
|
|
|
|
2023-02-20 18:39:31 +00:00
|
|
|
current = current / MAX;
|
|
|
|
|
2023-02-22 17:44:49 +00:00
|
|
|
g.clearRect(w.x, w.y, w.x + 23, w.y + 23);
|
2023-02-20 23:00:43 +00:00
|
|
|
|
2023-02-19 20:48:35 +00:00
|
|
|
g.setColor(g.theme.fg);
|
2023-02-20 18:14:02 +00:00
|
|
|
|
2023-02-19 20:48:35 +00:00
|
|
|
g.setFont6x15();
|
2023-02-20 18:39:31 +00:00
|
|
|
g.setFontAlign(0, 0);
|
2023-02-22 17:44:49 +00:00
|
|
|
g.drawString(mostExpensive, w.x + 12, w.y + 15);
|
2023-02-20 18:39:31 +00:00
|
|
|
let end = 135 + (current * (405 - 135));
|
2023-02-20 18:14:02 +00:00
|
|
|
g.setColor(current > 0.7 ? "#f00" : (current > 0.3 ? "#ff0" : "#0f0"));
|
2023-02-22 17:44:49 +00:00
|
|
|
GU.fillArc(g, w.x + 12, w.y + 12, 9, 12, GU.degreesToRadians(135), GU.degreesToRadians(end), GU.degreesToRadians(30));
|
2023-02-20 23:00:43 +00:00
|
|
|
|
|
|
|
g.setColor(g.theme.fg);
|
|
|
|
let endCpu = 135 + (cpu * (405 - 135));
|
2023-02-22 17:44:49 +00:00
|
|
|
GU.fillArc(g, w.x + 12, w.y + 12, 5.5, 8, GU.degreesToRadians(135), GU.degreesToRadians(endCpu), GU.degreesToRadians(30));
|
|
|
|
}
|
2023-02-22 21:00:31 +00:00
|
|
|
let sTimeout;
|
2023-02-22 21:57:49 +00:00
|
|
|
let s2Timeout;
|
|
|
|
let systickDiff;
|
2023-02-22 17:44:49 +00:00
|
|
|
function draw(w) {
|
2023-02-22 21:57:49 +00:00
|
|
|
let nextRefresh = Bangle.isLocked() ? ((s.refreshLocked || 60) * 1000 ): ((s.refreshUnlocked || 1) * 1000)
|
|
|
|
|
|
|
|
if (s2Timeout) clearTimeout(s2Timeout);
|
2023-02-22 21:00:31 +00:00
|
|
|
if (sTimeout) clearTimeout(sTimeout);
|
2023-02-22 21:57:49 +00:00
|
|
|
|
|
|
|
let t,systickNow;
|
|
|
|
sTimeout = setTimeout(()=>{
|
|
|
|
systickNow = peek32(0xE000E018);
|
|
|
|
t = Date.now();
|
|
|
|
}, nextRefresh - SYSTICKWAIT - 100);
|
|
|
|
|
|
|
|
s2Timeout = setTimeout(() => {
|
2023-02-22 17:44:49 +00:00
|
|
|
let tLater = Date.now();
|
|
|
|
let systickLater = peek32(0xE000E018);
|
2023-02-22 21:57:49 +00:00
|
|
|
systickDiff = systickLater - systickNow;
|
2023-02-22 17:44:49 +00:00
|
|
|
if (systickDiff < 0) systickDiff += SYSTICKMAX;
|
2023-02-22 21:57:49 +00:00
|
|
|
}, nextRefresh - 100);
|
|
|
|
|
|
|
|
doDraw(w, systickDiff ? (1 - systickDiff/SYSTICKMAX) : 0);
|
2023-02-22 17:44:49 +00:00
|
|
|
|
2023-02-22 21:00:31 +00:00
|
|
|
if (w.timeoutId !== undefined) {
|
|
|
|
clearTimeout(w.timeoutId);
|
|
|
|
}
|
|
|
|
w.timeoutId = setTimeout(() => {
|
|
|
|
w.timeoutId = undefined;
|
|
|
|
w.draw(w);
|
2023-02-22 21:57:49 +00:00
|
|
|
}, nextRefresh);
|
2023-02-19 20:48:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// add your widget
|
2023-02-20 18:39:31 +00:00
|
|
|
WIDGETS.powermanager = {
|
|
|
|
area: "tl",
|
2023-02-20 18:14:02 +00:00
|
|
|
width: 24,
|
2023-02-20 18:39:31 +00:00
|
|
|
draw: draw
|
2023-02-19 20:48:35 +00:00
|
|
|
};
|
2023-02-20 18:39:31 +00:00
|
|
|
|
2023-02-22 21:57:49 +00:00
|
|
|
Bangle.on("lock", ()=>{WIDGETS.powermanager.draw(WIDGETS.powermanager);});
|
2023-02-20 19:05:28 +00:00
|
|
|
|
2023-02-20 18:39:31 +00:00
|
|
|
// conserve memory
|
|
|
|
delete settings;
|
|
|
|
})();
|