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}; })();