Skip to content

Commit 725d160

Browse files
authored
Update widget.js
1 parent faa6257 commit 725d160

File tree

1 file changed

+70
-87
lines changed

1 file changed

+70
-87
lines changed

apps/widsmartbatt/widget.js

Lines changed: 70 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,17 @@
1-
//Learns by averaging power usage every time it is updated
2-
3-
41
(function(){
5-
6-
7-
const storage = require("Storage");
8-
const filename = "widsmartbatt.json";
9-
let runningAvg;
10-
var newJson=false;
11-
12-
//check if json file exists;
13-
14-
function CalcHoursRemaining (batt, usage) {
15-
16-
var hrsLeft = 175000 * batt / (100 * usage);
17-
return hrsLeft;
18-
19-
};
20-
21-
function addValue(val) {
22-
let summary = storage.readJSON(filename, 1) || { total: 0, count: 0 };
23-
summary.total += val;
24-
summary.count++;
25-
storage.writeJSON(filename, summary); // SAVED!
262

27-
let avg = summary.total / summary.count;
28-
runningAvg=avg;
29-
}
3+
const intervalLow = 30000; // update interval when not charging
4+
const intervalHigh = 30000; // faster update when charging
5+
var showPercent = false;
6+
const width = 40;
7+
const height = 24;
308

31-
32-
const intervalLow = 60000; // update time when not charging
33-
const intervalHigh = 2000; // update time when charging
34-
var showPercent=false;
35-
const width=40;
36-
const height=24;
379
let COLORS = {
38-
'bg': g.theme.bg,
39-
'fg': g.theme.fg,
10+
'bg': g.theme.bg,
11+
'fg': g.theme.fg,
4012
'charging': "#08f",
41-
'high': g.theme.dark ? "#fff" : "#000",
42-
'low': "#f00",
13+
'high': g.theme.dark ? "#fff" : "#000",
14+
'low': "#f00",
4315
};
4416

4517
const levelColor = (l) => {
@@ -49,62 +21,73 @@
4921
};
5022

5123
function draw() {
52-
var _a = require("power_usage").get(), usage = _a.usage, batt = _a.batt;
53-
var s = 29;
54-
var x = this.x, y = this.y;
55-
var txt;
56-
57-
//Add this to total and get the average
58-
addValue(usage)
59-
60-
if(showPercent){
61-
txt=batt;
62-
}else{
63-
var hrsLeft=CalcHoursRemaining(batt,runningAvg);
64-
var days = hrsLeft / 24;
65-
txt = days >= 1 ? "".concat(Math.round(Math.min(days, 99)), "d") : "".concat(Math.round(hrsLeft), "h");
66-
}
67-
68-
69-
let xl = x+4+batt*(s-12)/100;
24+
let batt=E.getBattery();
25+
let data = require("smartbatt").get();
26+
let hrsLeft=data.hrsLeft;
27+
let days = hrsLeft / 24;
7028

71-
g.setColor(COLORS.bg);
72-
g.fillRect(x+2,y+5,x+s-6,y+18);
29+
let txt = showPercent
30+
? batt
31+
: (days >= 1
32+
? Math.round(Math.min(days, 99)) + "d"
33+
: Math.round(hrsLeft) + "h");
34+
if(Bangle.isCharging()) txt=E.getBattery();
35+
let s = 29;
36+
let x = this.x, y = this.y;
37+
let xl = x + 4 + batt * (s - 12) / 100;
38+
39+
// Drawing code follows...
40+
g.setColor(COLORS.bg);
41+
g.fillRect(x + 2, y + 5, x + s - 6, y + 18);
7342

74-
g.setColor(levelColor(batt));
75-
g.fillRect(x+1,y+3,x+s-5,y+4);
76-
g.fillRect(x+1,y+19,x+s-5,y+20);
77-
g.fillRect(x,y+4,x+1,y+19);
78-
g.fillRect(x+s-5,y+4,x+s-4,y+19);
79-
g.fillRect(x+s-3,y+8,x+s-2,y+16); // tip of the battery
80-
g.fillRect(x+4,y+15,xl,y+16); // charging bar
43+
g.setColor(levelColor(batt));
44+
g.fillRect(x + 1, y + 3, x + s - 5, y + 4);
45+
g.fillRect(x + 1, y + 19, x + s - 5, y + 20);
46+
g.fillRect(x, y + 4, x + 1, y + 19);
47+
g.fillRect(x + s - 5, y + 4, x + s - 4, y + 19);
48+
g.fillRect(x + s - 3, y + 8, x + s - 2, y + 16);
49+
g.fillRect(x + 4, y + 15, xl, y + 16);
8150

82-
g.setColor(COLORS.fg);
83-
g.setFontAlign(0,0);
84-
g.setFont('6x8');
85-
g.drawString(txt, x + 14, y + 10);
51+
g.setColor(COLORS.fg);
52+
g.setFontAlign(0, 0);
53+
g.setFont('6x8');
54+
g.drawString(txt, x + 14, y + 10);
8655

87-
if (Bangle.isCharging()) changeInterval(id, intervalHigh);
88-
else changeInterval(id, intervalLow);
89-
}
56+
if (Bangle.isCharging()) changeInterval(id, intervalHigh);
57+
else changeInterval(id, intervalLow);
58+
}
9059

9160

92-
Bangle.on('charging',function(charging) { draw(); });
93-
var id = setInterval(()=>WIDGETS["widsmartbatt"].draw(), intervalLow);
61+
// Touch to temporarily show battery percent
9462
Bangle.on("touch", function (_btn, xy) {
95-
if (WIDGETS["back"] || !xy)
96-
return;
97-
var oversize = 5;
63+
if (WIDGETS["back"] || !xy) return;
9864

99-
var w = WIDGETS["widsmartbatt"];
100-
var x = xy.x, y = xy.y;
101-
if (w.x - oversize <= x && x < w.x + width + oversize
102-
&& w.y - oversize <= y && y < w.y + height + oversize) {
103-
E.stopEventPropagation && E.stopEventPropagation();
104-
showPercent = true;
105-
setTimeout(function () { return (showPercent = false, w.draw(w)); }, 3000);
106-
w.draw(w);
107-
}
108-
});
109-
WIDGETS["widsmartbatt"]={area:"tr",width:30,draw:draw};
65+
var oversize = 5;
66+
var w = WIDGETS["widsmartbatt"];
67+
var x = xy.x, y = xy.y;
68+
69+
if (w.x - oversize <= x && x < w.x + width + oversize
70+
&& w.y - oversize <= y && y < w.y + height + oversize) {
71+
E.stopEventPropagation && E.stopEventPropagation();
72+
showPercent = true;
73+
setTimeout(() => {
74+
showPercent = false;
75+
w.draw(w);
76+
}, 3000);
77+
w.draw(w);
78+
}
79+
});
80+
81+
// Update widget on charging state change
82+
Bangle.on('charging', function () {
83+
WIDGETS["widsmartbatt"].draw();
84+
});
85+
86+
var id = setInterval(() => WIDGETS["widsmartbatt"].draw(), intervalLow);
87+
88+
WIDGETS["widsmartbatt"] = {
89+
area: "tr",
90+
width: 30,
91+
draw: draw
92+
};
11093
})();

0 commit comments

Comments
 (0)