mirror of https://github.com/espruino/BangleApps
hwid_batt: restore (forked to widbattpwr)
parent
82f4832aff
commit
1658636ad0
|
@ -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
|
||||
|
|
|
@ -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.
|
||||
|
||||

|
||||
|
||||
## Creator
|
||||
[@alainsaas](https://github.com/alainsaas)
|
||||
Mod by Hank
|
||||
Mod again by bobrippling
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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};
|
||||
})();
|
||||
|
|
Loading…
Reference in New Issue