Skip to content

Commit 2869298

Browse files
authored
Merge pull request #3947 from RKBoss6/Smart-Battery
Create Smart Battery Widget
2 parents 91a7d6e + 0fbcea8 commit 2869298

File tree

5 files changed

+120
-0
lines changed

5 files changed

+120
-0
lines changed

apps/widsmartbatt/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
v0.01: New app!

apps/widsmartbatt/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Smart Battery Widget
2+
Shows battery in terms of days (21 days, 12 hours), and uses the [`smartbatt`](https://banglejs.com/apps/?id=smartbatt) module to learn from daily battery drainage and provide accurate estimations.
3+
This app was modified from `wid_a_battery_widget`, by @alainsaas
4+
5+
When you install this widget for the first time, or clear the data, it will also install the [`smartbatt`](https://banglejs.com/apps/?id=smartbatt) module as a dependency. As it learns your battery usage for the first time the forecast will fluctate, and will not be reliable for a while. As it compunds many drainage values together, it will keep learning, and provide better predictions.
6+
The module learns by averaging all the battery drainage over a period of time, and saves it to a json, averaging it with many others, providing an accurate prediction. The module gives the best forecast when you use the watch relatively similar per day.
7+
8+
Tap on the widget to show the battery percentage. It will go back to the days left after 3 seconds.
9+
10+
When charging, only the percentage is shown.
11+
## Creator
12+
RKBoss6

apps/widsmartbatt/icon.png

24.3 KB
Loading

apps/widsmartbatt/metadata.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"id": "widsmartbatt",
3+
"name": "Smart Battery Widget",
4+
"shortName":"Smart Batt Wid",
5+
"icon": "icon.png",
6+
"version":"0.01",
7+
"type": "widget",
8+
"supports": ["BANGLEJS", "BANGLEJS2"],
9+
"readme": "README.md",
10+
"dependencies" : { "smartbatt":"module" },
11+
"description": "Simple and slim battery widget that shows days remaining, and uses the `smartbatt` module to learm from your usage and provide accurate predictions.",
12+
"tags": "widget,battery",
13+
"provides_widgets" : ["battery"],
14+
"storage": [
15+
{"name":"widsmartbatt.wid.js","url":"widget.js"}
16+
]
17+
}

apps/widsmartbatt/widget.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
(function(){
2+
3+
var showPercent = false;
4+
const width = 40;
5+
const height = 24;
6+
7+
let COLORS = {
8+
'bg': g.theme.bg,
9+
'fg': g.theme.fg,
10+
'charging': "#08f",
11+
'high': g.theme.dark ? "#fff" : "#000",
12+
'low': "#f00",
13+
};
14+
15+
const levelColor = (l) => {
16+
if (Bangle.isCharging()) return COLORS.charging;
17+
if (l >= 30) return COLORS.high;
18+
return COLORS.low;
19+
};
20+
21+
function draw() {
22+
let batt=E.getBattery();
23+
let data = require("smartbatt").get();
24+
let hrsLeft=data.hrsLeft;
25+
let days = hrsLeft / 24;
26+
27+
let txt = showPercent
28+
? batt
29+
: (days >= 1
30+
? Math.round(Math.min(days, 99)) + "d"
31+
: Math.round(hrsLeft) + "h");
32+
if(Bangle.isCharging()) txt=E.getBattery();
33+
let s = 29;
34+
let x = this.x, y = this.y;
35+
let xl = x + 4 + batt * (s - 12) / 100;
36+
37+
// Drawing code follows...
38+
g.setColor(COLORS.bg);
39+
g.fillRect(x + 2, y + 5, x + s - 6, y + 18);
40+
41+
g.setColor(levelColor(batt));
42+
g.fillRect(x + 1, y + 3, x + s - 5, y + 4);
43+
g.fillRect(x + 1, y + 19, x + s - 5, y + 20);
44+
g.fillRect(x, y + 4, x + 1, y + 19);
45+
g.fillRect(x + s - 5, y + 4, x + s - 4, y + 19);
46+
g.fillRect(x + s - 3, y + 8, x + s - 2, y + 16);
47+
g.fillRect(x + 4, y + 15, xl, y + 16);
48+
49+
g.setColor(COLORS.fg);
50+
g.setFontAlign(0, 0);
51+
g.setFont('6x8');
52+
g.drawString(txt, x + 14, y + 10);
53+
54+
55+
}
56+
WIDGETS["widsmartbatt"] = {
57+
area: "tr",
58+
width: 30,
59+
draw: draw
60+
};
61+
62+
// Touch to temporarily show battery percent
63+
Bangle.on("touch", function (_btn, xy) {
64+
if (WIDGETS["back"] || !xy) return;
65+
66+
var oversize = 5;
67+
var w = WIDGETS["widsmartbatt"];
68+
var x = xy.x, y = xy.y;
69+
70+
if (w.x - oversize <= x && x < w.x + width + oversize
71+
&& w.y - oversize <= y && y < w.y + height + oversize) {
72+
E.stopEventPropagation && E.stopEventPropagation();
73+
showPercent = true;
74+
setTimeout(() => {
75+
showPercent = false;
76+
w.draw(w);
77+
}, 3000);
78+
w.draw(w);
79+
}
80+
});
81+
82+
// Update widget on charging state change
83+
Bangle.on('charging', function () {
84+
WIDGETS["widsmartbatt"].draw();
85+
});
86+
87+
setInterval(() => WIDGETS["widsmartbatt"].draw(), 60000);
88+
89+
90+
})();

0 commit comments

Comments
 (0)