hwid batt: colour based on power use, highlight based on time left

pull/3477/head
Rob Pilling 2024-06-26 22:36:30 +01:00
parent c765f38197
commit f8e854403a
3 changed files with 54 additions and 14 deletions

View File

@ -9,3 +9,4 @@
0.09: Add option for showing battery high mark
0.10: Fix background color
0.11: Minor code improvements
0.12: Show power consumption and hours remaining via percentage shading and colour

View File

@ -3,7 +3,7 @@
"name": "A Battery Widget (with percentage) - Hanks Mod",
"shortName":"H Battery Widget",
"icon": "widget.png",
"version":"0.11",
"version":"0.12",
"type": "widget",
"supports": ["BANGLEJS", "BANGLEJS2"],
"readme": "README.md",

View File

@ -5,7 +5,6 @@
var old_x = this.x;
var old_y = this.y;
let COLORS = {
'white': g.theme.dark ? "#000" : "#fff",
'black': g.theme.dark ? "#fff" : "#000",
@ -20,25 +19,26 @@
return COLORS.low;
};
function draw() {
var s = width - 1;
var x = this.x;
var y = this.y;
if (x !== undefined && y !== undefined) {
function draw(_self, uu) {
let x = this.x;
let y = this.y;
if (x != null && y != null) {
g.reset();
g.setBgColor(COLORS.white);
g.clearRect(old_x, old_y, old_x + width, old_y + height);
const l = E.getBattery(); // debug: Math.floor(Math.random() * 101);
let s = width - 1;
let xl = x+4+l*(s-12)/100;
// Charging bar
g.setColor(levelColor(l));
g.fillRect(x+4,y+14+3,xl,y+16+3); // charging bar
g.fillRect(x+4,y+14+3,xl,y+16+3);
// Show percentage
g.setColor(COLORS.black);
g.setFontAlign(0,0);
g.setFont('Vector',16);
g.drawString(l, x + 14, y + 10);
this.drawText(l, uu);
}
old_x = this.x;
old_y = this.y;
@ -47,10 +47,49 @@
else changeInterval(id, intervalLow);
}
Bangle.on('charging',function(charging) { draw(); });
var id = setInterval(()=>WIDGETS["hwid_a_battery_widget"].draw(), intervalLow);
const drawString = function(l) {
g.drawString(l, this.x + 14, this.y + 10);
};
let drawText;
if(E.getPowerUsage){
drawText = function(l, uu) {
const u = uu == null ? E.getPowerUsage().total : uu;
// text colour is based off power usage
// colour height is based off time left, higher = more
g.setColor(COLORS.black);
drawString.call(this, l);
if(u >= 23000)
g.setColor("#f00"); // red, e.g. GPS ~20k
else if(u > 2000)
g.setColor("#fc0"); // yellow, e.g. CPU ~1k, HRM ~700
else
g.setColor("#0f0"); // green: ok
const hrs = 200000 / u;
const days = hrs / 24;
const dayPercent = Math.min(days / 16, 1);
const th = g.getFontHeight();
g.setClipRect(this.x, this.y + dayPercent * th, this.x + width, this.y + th);
drawString.call(this, l);
};
}else{
drawText = function(l) {
g.setColor(COLORS.black);
drawString.call(this, l);
};
}
const d = () => WIDGETS["hwid_a_battery_widget"].draw();
Bangle.on('charging', d);
var id = setInterval(d, intervalLow);
var width = 30;
var height = 19;
WIDGETS["hwid_a_battery_widget"]={area:"tr",width,draw:draw};
WIDGETS["hwid_a_battery_widget"]={area:"tr",width,draw,drawText};
})();