From aabfb6b7dbb7ba3db43cfa32032d95ca35f01b5c Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Wed, 19 Jun 2024 18:38:45 +0100 Subject: [PATCH 01/11] hwid batt: update README --- apps/hwid_a_battery_widget/README.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/apps/hwid_a_battery_widget/README.md b/apps/hwid_a_battery_widget/README.md index fd7bbec67..2a1bb1400 100644 --- a/apps/hwid_a_battery_widget/README.md +++ b/apps/hwid_a_battery_widget/README.md @@ -4,14 +4,18 @@ Show the current battery level and charging status in the top right of the clock * Works with Bangle 2 * Simple design, no settings - * Red when the batterly level is below 30% + * Red when the battery level is below 30% * Blue when charging * 40 pixels wide -The high-level marker (a little bar at the 100% point) can be toggled in settings. +This is a copy of `wid_a_battery_widget`, with the main changes being: +* Reduced constants in code +* Reduced `fillRect` calls / improved efficiency +* Removal of the high-level marker ![](a_battery_widget-pic.jpg) ## Creator [@alainsaas](https://github.com/alainsaas) Mod by Hank +Mod again by bobrippling From c765f38197998a0420105950f113f46a8ed00b97 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Wed, 19 Jun 2024 18:40:00 +0100 Subject: [PATCH 02/11] hwid batt: whitespace --- apps/hwid_a_battery_widget/widget.js | 36 ++++++++++++++-------------- 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/apps/hwid_a_battery_widget/widget.js b/apps/hwid_a_battery_widget/widget.js index 7c76364a0..84fa32e0c 100644 --- a/apps/hwid_a_battery_widget/widget.js +++ b/apps/hwid_a_battery_widget/widget.js @@ -21,30 +21,30 @@ }; function draw() { - var s = width - 1; - var x = this.x; - var y = this.y; - if (x !== undefined && y !== undefined) { - g.setBgColor(COLORS.white); - g.clearRect(old_x, old_y, old_x + width, old_y + height); + var s = width - 1; + var x = this.x; + var y = this.y; + if (x !== undefined && y !== undefined) { + 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 xl = x+4+l*(s-12)/100; + const l = E.getBattery(); // debug: Math.floor(Math.random() * 101); + let xl = x+4+l*(s-12)/100; - g.setColor(levelColor(l)); - g.fillRect(x+4,y+14+3,xl,y+16+3); // charging bar - // Show percentage - g.setColor(COLORS.black); - g.setFontAlign(0,0); - g.setFont('Vector',16); - g.drawString(l, x + 14, y + 10); + g.setColor(levelColor(l)); + g.fillRect(x+4,y+14+3,xl,y+16+3); // charging bar + // Show percentage + g.setColor(COLORS.black); + g.setFontAlign(0,0); + g.setFont('Vector',16); + g.drawString(l, x + 14, y + 10); - } + } old_x = this.x; old_y = this.y; - if (Bangle.isCharging()) changeInterval(id, intervalHigh); - else changeInterval(id, intervalLow); + if (Bangle.isCharging()) changeInterval(id, intervalHigh); + else changeInterval(id, intervalLow); } Bangle.on('charging',function(charging) { draw(); }); From f8e854403a104dc97ad810371c2609f4a85acc9f Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Wed, 26 Jun 2024 22:36:30 +0100 Subject: [PATCH 03/11] hwid batt: colour based on power use, highlight based on time left --- apps/hwid_a_battery_widget/ChangeLog | 1 + apps/hwid_a_battery_widget/metadata.json | 2 +- apps/hwid_a_battery_widget/widget.js | 65 +++++++++++++++++++----- 3 files changed, 54 insertions(+), 14 deletions(-) diff --git a/apps/hwid_a_battery_widget/ChangeLog b/apps/hwid_a_battery_widget/ChangeLog index 99601c31b..e12d070f6 100644 --- a/apps/hwid_a_battery_widget/ChangeLog +++ b/apps/hwid_a_battery_widget/ChangeLog @@ -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 diff --git a/apps/hwid_a_battery_widget/metadata.json b/apps/hwid_a_battery_widget/metadata.json index 2abc57a9b..7b7e18f20 100644 --- a/apps/hwid_a_battery_widget/metadata.json +++ b/apps/hwid_a_battery_widget/metadata.json @@ -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", diff --git a/apps/hwid_a_battery_widget/widget.js b/apps/hwid_a_battery_widget/widget.js index 84fa32e0c..95d283d05 100644 --- a/apps/hwid_a_battery_widget/widget.js +++ b/apps/hwid_a_battery_widget/widget.js @@ -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}; })(); From acd9b3be949aea1fb7adb5e44a0daf6206d79251 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Tue, 2 Jul 2024 08:42:50 +0100 Subject: [PATCH 04/11] Rename `uu` --- apps/hwid_a_battery_widget/widget.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/apps/hwid_a_battery_widget/widget.js b/apps/hwid_a_battery_widget/widget.js index 95d283d05..0f02f4138 100644 --- a/apps/hwid_a_battery_widget/widget.js +++ b/apps/hwid_a_battery_widget/widget.js @@ -19,7 +19,7 @@ return COLORS.low; }; - function draw(_self, uu) { + function draw(_self, pwrOverride) { let x = this.x; let y = this.y; if (x != null && y != null) { @@ -38,7 +38,7 @@ // Show percentage g.setFontAlign(0,0); g.setFont('Vector',16); - this.drawText(l, uu); + this.drawText(l, pwrOverride); } old_x = this.x; old_y = this.y; @@ -53,8 +53,8 @@ let drawText; if(E.getPowerUsage){ - drawText = function(l, uu) { - const u = uu == null ? E.getPowerUsage().total : uu; + drawText = function(l, pwrOverride) { + const u = pwrOverride == null ? E.getPowerUsage().total : pwrOverride; // text colour is based off power usage // colour height is based off time left, higher = more From 07c4a52aa9463d95876b027781ccb34997508c8f Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Tue, 2 Jul 2024 08:43:00 +0100 Subject: [PATCH 05/11] Sum power ignoring LCD --- apps/hwid_a_battery_widget/widget.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/apps/hwid_a_battery_widget/widget.js b/apps/hwid_a_battery_widget/widget.js index 0f02f4138..7886faafd 100644 --- a/apps/hwid_a_battery_widget/widget.js +++ b/apps/hwid_a_battery_widget/widget.js @@ -54,7 +54,13 @@ if(E.getPowerUsage){ drawText = function(l, pwrOverride) { - const u = pwrOverride == null ? E.getPowerUsage().total : pwrOverride; + const pwr = E.getPowerUsage(); + let total = 0; + for(const key in pwr){ + if(!key.startsWith("LCD")) + total += pwr[key]; + } + const u = pwrOverride == null ? total : pwrOverride; // text colour is based off power usage // colour height is based off time left, higher = more From 4cbcbe7277218e77fd24550f7daca0fa1652a9c3 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Tue, 2 Jul 2024 22:08:12 +0100 Subject: [PATCH 06/11] hwid batt: draw time remaining, shade in battery% --- apps/hwid_a_battery_widget/widget.js | 46 +++++++++++++--------------- 1 file changed, 21 insertions(+), 25 deletions(-) diff --git a/apps/hwid_a_battery_widget/widget.js b/apps/hwid_a_battery_widget/widget.js index 7886faafd..a45ac18d5 100644 --- a/apps/hwid_a_battery_widget/widget.js +++ b/apps/hwid_a_battery_widget/widget.js @@ -19,21 +19,20 @@ return COLORS.low; }; - function draw(_self, pwrOverride) { + function draw(_self, pwrOverride, battOvr) { 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); + g.clearRect(old_x, old_y, old_x + width, old_y + height - 1); - const l = E.getBattery(); // debug: Math.floor(Math.random() * 101); - let s = width - 1; - let xl = x+4+l*(s-12)/100; + const l = battOvr != null ? battOvr : E.getBattery(); // Charging bar g.setColor(levelColor(l)); - g.fillRect(x+4,y+14+3,xl,y+16+3); + const xl = x+1+(width - 1)*l/100; + g.fillRect(x+1,y+height-3,xl,y+height-1); // Show percentage g.setFontAlign(0,0); @@ -53,41 +52,38 @@ let drawText; if(E.getPowerUsage){ - drawText = function(l, pwrOverride) { + drawText = function(batt, pwrOverride) { const pwr = E.getPowerUsage(); let total = 0; - for(const key in pwr){ - if(!key.startsWith("LCD")) - total += pwr[key]; + for(const key in pwr.device){ + if(!/^(LCD|LED)/.test(key)) + total += pwr.device[key]; } const u = pwrOverride == null ? total : pwrOverride; - // text colour is based off power usage - // colour height is based off time left, higher = more + const hrs = 200000 / u; + const days = hrs / 24; + const txt = days >= 1 ? `${Math.round(days)}d` : `${Math.round(hrs)}h`; + // draw time left, then shade it based on batt % + const th = 14; g.setColor(COLORS.black); - drawString.call(this, l); + g.setClipRect(this.x, this.y, this.x + width, this.y + th); + drawString.call(this, txt); + g.setClipRect(this.x, this.y + th * (1 - batt / 100), this.x + width, this.y + th); 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); + drawString.call(this, txt); }; }else{ - drawText = function(l) { + drawText = function(batt) { g.setColor(COLORS.black); - drawString.call(this, l); + drawString.call(this, batt); }; } @@ -95,7 +91,7 @@ Bangle.on('charging', d); var id = setInterval(d, intervalLow); var width = 30; - var height = 19; + var height = 24; WIDGETS["hwid_a_battery_widget"]={area:"tr",width,draw,drawText}; })(); From 3b9c0b7a41561688aa1de23d65886790c1297156 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Tue, 2 Jul 2024 22:08:22 +0100 Subject: [PATCH 07/11] hwid batt: ditch override/debug code --- apps/hwid_a_battery_widget/widget.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/apps/hwid_a_battery_widget/widget.js b/apps/hwid_a_battery_widget/widget.js index a45ac18d5..b03e80278 100644 --- a/apps/hwid_a_battery_widget/widget.js +++ b/apps/hwid_a_battery_widget/widget.js @@ -19,7 +19,7 @@ return COLORS.low; }; - function draw(_self, pwrOverride, battOvr) { + function draw() { let x = this.x; let y = this.y; if (x != null && y != null) { @@ -27,7 +27,7 @@ g.setBgColor(COLORS.white); g.clearRect(old_x, old_y, old_x + width, old_y + height - 1); - const l = battOvr != null ? battOvr : E.getBattery(); + const l = E.getBattery(); // Charging bar g.setColor(levelColor(l)); @@ -37,7 +37,7 @@ // Show percentage g.setFontAlign(0,0); g.setFont('Vector',16); - this.drawText(l, pwrOverride); + this.drawText(l); } old_x = this.x; old_y = this.y; @@ -52,16 +52,15 @@ let drawText; if(E.getPowerUsage){ - drawText = function(batt, pwrOverride) { + drawText = function(batt) { const pwr = E.getPowerUsage(); let total = 0; for(const key in pwr.device){ if(!/^(LCD|LED)/.test(key)) total += pwr.device[key]; } - const u = pwrOverride == null ? total : pwrOverride; - const hrs = 200000 / u; + const hrs = 200000 / total; const days = hrs / 24; const txt = days >= 1 ? `${Math.round(days)}d` : `${Math.round(hrs)}h`; @@ -72,9 +71,9 @@ drawString.call(this, txt); g.setClipRect(this.x, this.y + th * (1 - batt / 100), this.x + width, this.y + th); - if(u >= 23000) + if(total >= 23000) g.setColor("#f00"); // red, e.g. GPS ~20k - else if(u > 2000) + else if(total > 2000) g.setColor("#fc0"); // yellow, e.g. CPU ~1k, HRM ~700 else g.setColor("#0f0"); // green: ok From 82f4832aff788c202e6d0f41c8e7fa25fa26f260 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Wed, 3 Jul 2024 18:23:12 +0100 Subject: [PATCH 08/11] widbattpwr: fork from hwid_a_battery_widget --- apps/widbattpwr/ChangeLog | 1 + apps/widbattpwr/README.md | 11 ++++++ apps/widbattpwr/metadata.json | 19 +++++++++ apps/widbattpwr/widget.js | 60 ++++++++++++++++++++++++++++ apps/widbattpwr/widget.png | Bin 0 -> 987 bytes apps/widbattpwr/widget.ts | 71 ++++++++++++++++++++++++++++++++++ 6 files changed, 162 insertions(+) create mode 100644 apps/widbattpwr/ChangeLog create mode 100644 apps/widbattpwr/README.md create mode 100644 apps/widbattpwr/metadata.json create mode 100644 apps/widbattpwr/widget.js create mode 100644 apps/widbattpwr/widget.png create mode 100644 apps/widbattpwr/widget.ts diff --git a/apps/widbattpwr/ChangeLog b/apps/widbattpwr/ChangeLog new file mode 100644 index 000000000..b1bdaa6fc --- /dev/null +++ b/apps/widbattpwr/ChangeLog @@ -0,0 +1 @@ +0.01: Initial fork from hwid_a_battery_widget diff --git a/apps/widbattpwr/README.md b/apps/widbattpwr/README.md new file mode 100644 index 000000000..4dd4c8a5a --- /dev/null +++ b/apps/widbattpwr/README.md @@ -0,0 +1,11 @@ +# Battery Power Widget + +Show the time remaining at the current power consumption, and battery percentage via shading of the text and a percentage bar. + +Requires firmware 2v23 or above. + +This is a copy of `hwid_a_battery_widget` (that being a copy of `wid_a_battery_widget`). + +## Creator + +[@bobrippling](https://github.com/bobrippling) diff --git a/apps/widbattpwr/metadata.json b/apps/widbattpwr/metadata.json new file mode 100644 index 000000000..667485666 --- /dev/null +++ b/apps/widbattpwr/metadata.json @@ -0,0 +1,19 @@ +{ + "id": "widbattpwr", + "name": "Battery power and percentage widget", + "shortName": "Batt Pwr", + "icon": "widget.png", + "version": "0.01", + "type": "widget", + "supports": ["BANGLEJS2"], + "readme": "README.md", + "description": "A battery widget showing percentage (via shading) and time remaining at current power consumption", + "tags": "widget,battery", + "provides_widgets": ["battery"], + "storage": [ + { + "name": "widbattpwr.wid.js", + "url": "widget.js" + } + ] +} diff --git a/apps/widbattpwr/widget.js b/apps/widbattpwr/widget.js new file mode 100644 index 000000000..b6c0924d1 --- /dev/null +++ b/apps/widbattpwr/widget.js @@ -0,0 +1,60 @@ +(function () { + var intervalLow = 60000; + var intervalHigh = 2000; + var width = 30; + var height = 24; + var powerColour = function (pwr) { + return pwr >= 23000 + ? "#f00" + : pwr > 2000 + ? "#fc0" + : "#0f0"; + }; + var drawBar = function (x, y, batt) { + return g.fillRect(x + 1, y + height - 3, x + 1 + (width - 2) * batt / 100, y + height - 1); + }; + var drawString = function (x, y, txt) { + return g.drawString(txt, x + 14, y + 10); + }; + function draw() { + var x = this.x; + var y = this.y; + var batt = E.getBattery(); + var pwr = E.getPowerUsage(); + var usage = 0; + for (var key in pwr.device) { + if (!/^(LCD|LED)/.test(key)) + usage += pwr.device[key]; + } + var pwrColour = powerColour(usage); + g.reset() + .setBgColor(g.theme.bg) + .clearRect(x, y, x + width - 1, y + height - 1); + g.setColor(g.theme.fg); + drawBar(x, y, 100); + g.setColor(pwrColour); + drawBar(x, y, batt); + g.setFontAlign(0, 0); + g.setFont("Vector", 16); + { + var hrs = 200000 / usage; + var days = hrs / 24; + var txt = days >= 1 ? "".concat(Math.round(Math.min(days, 99)), "d") : "".concat(Math.round(hrs), "h"); + var txth = 14; + g.setColor(g.theme.fg); + g.setClipRect(x, y, x + width, y + txth); + drawString(x, y, txt); + g.setColor(pwrColour); + g.setClipRect(x, y + txth * (1 - batt / 100), x + width, y + txth); + drawString(x, y, txt); + } + } + var id = setInterval(function () { + var w = WIDGETS["battpwr"]; + w.draw(w); + }, intervalLow); + Bangle.on("charging", function (charging) { + changeInterval(id, charging ? intervalHigh : intervalLow); + }); + WIDGETS["battpwr"] = { area: "tr", width: width, draw: draw }; +})(); diff --git a/apps/widbattpwr/widget.png b/apps/widbattpwr/widget.png new file mode 100644 index 0000000000000000000000000000000000000000..e5a8284a1a7494cf510e06ce2764deb126fbd8e0 GIT binary patch literal 987 zcmeAS@N?(olHy`uVBq!ia0vp^ia;#I!2~3?SuY9$QY`6?zK#qG8~eHcB(ehe3dtTp zz6=aiY77hwEes65fI&u`8WOFbu zF$ys-16hnf$k3k2zyfA7FaS*g0_Fvb2-yWpa9PC#%y708Na3X%^-cx`CZo)dh>{3j zAFJg2T)o7U{G?R9irfN_0tTB3D*7iAWdWaj57fXq!y$}cUkRZ;?3 z1P2gzrdDJkY|1N!dJE`5eUOa4p`L+0Hf<%DX*jfjWFXqW{(@L%12W7iAhRMhC(!q`k+un3S~$Xf<=LGXvbxv4^IzvT*U^Bw}46HjHioZhzIZ6DUE#1 z3Op`HfBfHn#!Kt#isLJ~^$ZgYg?D_DTl;9!nfNWtoXo}Ntb9AyCvWJ9ov|vb=~7lE z+lHyT*=9G_D?YrY_pslpZkxoXZ&peSIv-{FS1mR>zb)g}mAjw*X>HjlQOw8i^+jm* zfwW1>rguHdzReVWrr+>F)F$h=qtiVN+1|bSWpQjmUPO9JV{&^+(AwTi)xEEdTvzd8 zH4S4w#3gy7WL5X)`mzTn`3_{?u~rhDe^PQ~)PKGB7axAU*v&Jw-k@!Eh35hGITzJc TcL_ee4odr;u6{1-oD!M<$*?)d literal 0 HcmV?d00001 diff --git a/apps/widbattpwr/widget.ts b/apps/widbattpwr/widget.ts new file mode 100644 index 000000000..b39412179 --- /dev/null +++ b/apps/widbattpwr/widget.ts @@ -0,0 +1,71 @@ +(() => { + const intervalLow = 60000; + const intervalHigh = 2000; + const width = 30; + const height = 24; + + const powerColour = (pwr: number) => + pwr >= 23000 + ? "#f00" // red, e.g. GPS ~20k + : pwr > 2000 + ? "#fc0" // yellow, e.g. CPU ~1k, HRM ~700 + : "#0f0"; // green: ok + + const drawBar = (x: number, y: number, batt: number) => + g.fillRect(x+1, y+height-3, x+1+(width-2)*batt/100, y+height-1); + + const drawString = (x: number, y: number, txt: string) => + g.drawString(txt, x + 14, y + 10); + + function draw(this: Widget) { + let x = this.x!; + let y = this.y!; + + const batt = E.getBattery(); + const pwr = E.getPowerUsage(); + let usage = 0; + for(const key in pwr.device){ + if(!/^(LCD|LED)/.test(key)) + usage += pwr.device[key]; + } + const pwrColour = powerColour(usage); + + g.reset() + .setBgColor(g.theme.bg) + .clearRect(x, y, x + width - 1, y + height - 1); + + g.setColor(g.theme.fg); + drawBar(x, y, 100); + g.setColor(pwrColour); + drawBar(x, y, batt); + + g.setFontAlign(0, 0); + g.setFont("Vector", 16); + { + const hrs = 200000 / usage; + const days = hrs / 24; + const txt = days >= 1 ? `${Math.round(Math.min(days, 99))}d` : `${Math.round(hrs)}h`; + + // draw time remaining, then shade it based on batt % + const txth = 14; + g.setColor(g.theme.fg); + g.setClipRect(x, y, x + width, y + txth); + drawString(x, y, txt); + + g.setColor(pwrColour); + g.setClipRect(x, y + txth * (1 - batt / 100), x + width, y + txth); + drawString(x, y, txt); + } + } + + const id = setInterval(() => { + const w = WIDGETS["battpwr"]!; + w.draw(w); + }, intervalLow); + + Bangle.on("charging", charging => { + changeInterval(id, charging ? intervalHigh : intervalLow); + }); + + WIDGETS["battpwr"] = { area: "tr", width, draw }; +})(); From 1658636ad00454478944c31845b059fadc07752d Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Wed, 3 Jul 2024 18:26:28 +0100 Subject: [PATCH 09/11] hwid_batt: restore (forked to widbattpwr) --- apps/hwid_a_battery_widget/ChangeLog | 1 - apps/hwid_a_battery_widget/README.md | 8 +-- apps/hwid_a_battery_widget/metadata.json | 2 +- apps/hwid_a_battery_widget/widget.js | 86 +++++++----------------- 4 files changed, 26 insertions(+), 71 deletions(-) diff --git a/apps/hwid_a_battery_widget/ChangeLog b/apps/hwid_a_battery_widget/ChangeLog index e12d070f6..99601c31b 100644 --- a/apps/hwid_a_battery_widget/ChangeLog +++ b/apps/hwid_a_battery_widget/ChangeLog @@ -9,4 +9,3 @@ 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 diff --git a/apps/hwid_a_battery_widget/README.md b/apps/hwid_a_battery_widget/README.md index 2a1bb1400..fd7bbec67 100644 --- a/apps/hwid_a_battery_widget/README.md +++ b/apps/hwid_a_battery_widget/README.md @@ -4,18 +4,14 @@ Show the current battery level and charging status in the top right of the clock * Works with Bangle 2 * Simple design, no settings - * Red when the battery level is below 30% + * Red when the batterly level is below 30% * Blue when charging * 40 pixels wide -This is a copy of `wid_a_battery_widget`, with the main changes being: -* Reduced constants in code -* Reduced `fillRect` calls / improved efficiency -* Removal of the high-level marker +The high-level marker (a little bar at the 100% point) can be toggled in settings. ![](a_battery_widget-pic.jpg) ## Creator [@alainsaas](https://github.com/alainsaas) Mod by Hank -Mod again by bobrippling diff --git a/apps/hwid_a_battery_widget/metadata.json b/apps/hwid_a_battery_widget/metadata.json index 7b7e18f20..2abc57a9b 100644 --- a/apps/hwid_a_battery_widget/metadata.json +++ b/apps/hwid_a_battery_widget/metadata.json @@ -3,7 +3,7 @@ "name": "A Battery Widget (with percentage) - Hanks Mod", "shortName":"H Battery Widget", "icon": "widget.png", - "version":"0.12", + "version":"0.11", "type": "widget", "supports": ["BANGLEJS", "BANGLEJS2"], "readme": "README.md", diff --git a/apps/hwid_a_battery_widget/widget.js b/apps/hwid_a_battery_widget/widget.js index b03e80278..7c76364a0 100644 --- a/apps/hwid_a_battery_widget/widget.js +++ b/apps/hwid_a_battery_widget/widget.js @@ -5,6 +5,7 @@ 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,77 +21,36 @@ }; function draw() { - 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 - 1); + var s = width - 1; + var x = this.x; + var y = this.y; + if (x !== undefined && y !== undefined) { + g.setBgColor(COLORS.white); + g.clearRect(old_x, old_y, old_x + width, old_y + height); - const l = E.getBattery(); + const l = E.getBattery(); // debug: Math.floor(Math.random() * 101); + let xl = x+4+l*(s-12)/100; - // Charging bar - g.setColor(levelColor(l)); - const xl = x+1+(width - 1)*l/100; - g.fillRect(x+1,y+height-3,xl,y+height-1); + g.setColor(levelColor(l)); + g.fillRect(x+4,y+14+3,xl,y+16+3); // charging bar + // Show percentage + g.setColor(COLORS.black); + g.setFontAlign(0,0); + g.setFont('Vector',16); + g.drawString(l, x + 14, y + 10); - // Show percentage - g.setFontAlign(0,0); - g.setFont('Vector',16); - this.drawText(l); - } + } old_x = this.x; old_y = this.y; - if (Bangle.isCharging()) changeInterval(id, intervalHigh); - else changeInterval(id, intervalLow); + if (Bangle.isCharging()) changeInterval(id, intervalHigh); + else changeInterval(id, intervalLow); } - const drawString = function(l) { - g.drawString(l, this.x + 14, this.y + 10); - }; - let drawText; - - if(E.getPowerUsage){ - drawText = function(batt) { - const pwr = E.getPowerUsage(); - let total = 0; - for(const key in pwr.device){ - if(!/^(LCD|LED)/.test(key)) - total += pwr.device[key]; - } - - const hrs = 200000 / total; - const days = hrs / 24; - const txt = days >= 1 ? `${Math.round(days)}d` : `${Math.round(hrs)}h`; - - // draw time left, then shade it based on batt % - const th = 14; - g.setColor(COLORS.black); - g.setClipRect(this.x, this.y, this.x + width, this.y + th); - drawString.call(this, txt); - - g.setClipRect(this.x, this.y + th * (1 - batt / 100), this.x + width, this.y + th); - if(total >= 23000) - g.setColor("#f00"); // red, e.g. GPS ~20k - else if(total > 2000) - g.setColor("#fc0"); // yellow, e.g. CPU ~1k, HRM ~700 - else - g.setColor("#0f0"); // green: ok - drawString.call(this, txt); - }; - }else{ - drawText = function(batt) { - g.setColor(COLORS.black); - drawString.call(this, batt); - }; - } - - const d = () => WIDGETS["hwid_a_battery_widget"].draw(); - Bangle.on('charging', d); - var id = setInterval(d, intervalLow); + Bangle.on('charging',function(charging) { draw(); }); + var id = setInterval(()=>WIDGETS["hwid_a_battery_widget"].draw(), intervalLow); var width = 30; - var height = 24; + var height = 19; - WIDGETS["hwid_a_battery_widget"]={area:"tr",width,draw,drawText}; + WIDGETS["hwid_a_battery_widget"]={area:"tr",width,draw:draw}; })(); From 487652cc1a4a74d08fb7b2e50ae619f1c06225c7 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Thu, 4 Jul 2024 22:42:42 +0100 Subject: [PATCH 10/11] widbattpwr: show percentage temporarily on tap --- apps/widbattpwr/widget.js | 27 ++++++++++++++++++++++++--- apps/widbattpwr/widget.ts | 30 +++++++++++++++++++++++++++--- 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/apps/widbattpwr/widget.js b/apps/widbattpwr/widget.js index b6c0924d1..c54f73ca2 100644 --- a/apps/widbattpwr/widget.js +++ b/apps/widbattpwr/widget.js @@ -3,6 +3,7 @@ var intervalHigh = 2000; var width = 30; var height = 24; + var showPct = false; var powerColour = function (pwr) { return pwr >= 23000 ? "#f00" @@ -37,9 +38,15 @@ g.setFontAlign(0, 0); g.setFont("Vector", 16); { - var hrs = 200000 / usage; - var days = hrs / 24; - var txt = days >= 1 ? "".concat(Math.round(Math.min(days, 99)), "d") : "".concat(Math.round(hrs), "h"); + var txt = void 0; + if (showPct) { + txt = "".concat(batt, "%"); + } + else { + var hrs = 200000 / usage; + var days = hrs / 24; + txt = days >= 1 ? "".concat(Math.round(Math.min(days, 99)), "d") : "".concat(Math.round(hrs), "h"); + } var txth = 14; g.setColor(g.theme.fg); g.setClipRect(x, y, x + width, y + txth); @@ -56,5 +63,19 @@ Bangle.on("charging", function (charging) { changeInterval(id, charging ? intervalHigh : intervalLow); }); + Bangle.on("touch", function (_btn, xy) { + if (WIDGETS["back"] || !xy) + return; + var oversize = 5; + var w = WIDGETS["battpwr"]; + var x = xy.x, y = xy.y; + if (w.x - oversize <= x && x < w.x + width + oversize + && w.y - oversize <= y && y < w.y + height + oversize) { + E.stopEventPropagation && E.stopEventPropagation(); + showPct = true; + setTimeout(function () { return (showPct = false, w.draw(w)); }, 1000); + w.draw(w); + } + }); WIDGETS["battpwr"] = { area: "tr", width: width, draw: draw }; })(); diff --git a/apps/widbattpwr/widget.ts b/apps/widbattpwr/widget.ts index b39412179..0507fb57d 100644 --- a/apps/widbattpwr/widget.ts +++ b/apps/widbattpwr/widget.ts @@ -3,6 +3,7 @@ const intervalHigh = 2000; const width = 30; const height = 24; + let showPct = false; const powerColour = (pwr: number) => pwr >= 23000 @@ -42,9 +43,14 @@ g.setFontAlign(0, 0); g.setFont("Vector", 16); { - const hrs = 200000 / usage; - const days = hrs / 24; - const txt = days >= 1 ? `${Math.round(Math.min(days, 99))}d` : `${Math.round(hrs)}h`; + let txt; + if(showPct){ + txt = `${batt}%`; + }else{ + const hrs = 200000 / usage; + const days = hrs / 24; + txt = days >= 1 ? `${Math.round(Math.min(days, 99))}d` : `${Math.round(hrs)}h`; + } // draw time remaining, then shade it based on batt % const txth = 14; @@ -67,5 +73,23 @@ changeInterval(id, charging ? intervalHigh : intervalLow); }); + Bangle.on("touch", (_btn, xy) => { + if(WIDGETS["back"] || !xy) return; + + const oversize = 5; + const w = WIDGETS["battpwr"]!; + const { x, y } = xy; + + if(w.x! - oversize <= x && x < w.x! + width + oversize + && w.y! - oversize <= y && y < w.y! + height + oversize) + { + E.stopEventPropagation && E.stopEventPropagation(); + + showPct = true; + setTimeout(() => (showPct = false, w.draw(w)), 1000); + w.draw(w); + } + }); + WIDGETS["battpwr"] = { area: "tr", width, draw }; })(); From f943effe3921f1b3d1bcc5868cf31ae1de9c9915 Mon Sep 17 00:00:00 2001 From: Rob Pilling Date: Fri, 5 Jul 2024 12:42:27 +0100 Subject: [PATCH 11/11] widbattpwr: scale time based on (estimated) remaining battery mAh --- apps/widbattpwr/widget.js | 2 +- apps/widbattpwr/widget.ts | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/apps/widbattpwr/widget.js b/apps/widbattpwr/widget.js index c54f73ca2..58f89dc34 100644 --- a/apps/widbattpwr/widget.js +++ b/apps/widbattpwr/widget.js @@ -43,7 +43,7 @@ txt = "".concat(batt, "%"); } else { - var hrs = 200000 / usage; + var hrs = 175000 * batt / (100 * usage); var days = hrs / 24; txt = days >= 1 ? "".concat(Math.round(Math.min(days, 99)), "d") : "".concat(Math.round(hrs), "h"); } diff --git a/apps/widbattpwr/widget.ts b/apps/widbattpwr/widget.ts index 0507fb57d..de5352f32 100644 --- a/apps/widbattpwr/widget.ts +++ b/apps/widbattpwr/widget.ts @@ -47,7 +47,8 @@ if(showPct){ txt = `${batt}%`; }else{ - const hrs = 200000 / usage; + // 175mAh, scaled based on battery (batt/100), scaled down based on usage + const hrs = 175000 * batt / (100 * usage); const days = hrs / 24; txt = days >= 1 ? `${Math.round(Math.min(days, 99))}d` : `${Math.round(hrs)}h`; }