Skip to content

Commit 1ff8162

Browse files
authored
Merge pull request #3944 from RKBoss6/smartbatt-module
Create Smart Battery Module
2 parents 2869298 + 175e2ac commit 1ff8162

File tree

7 files changed

+332
-0
lines changed

7 files changed

+332
-0
lines changed

apps/smartbatt/ChangeLog

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

apps/smartbatt/README.md

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
# Smart Battery Module
2+
A module for providing a truly accurate battery life in terms of days. The module learns from daily usage and drainage patterns, and extrapolates that. As you use it more, and the battery keeps draining, the predictions should become more accurate.
3+
4+
Because the Bangle.js battery percent fluctuates naturally, it is highly recomended to use the `Power Manager` app and enable monotonic/stable percentage to stabilize the percentage, and reduce fluctuations. This may help provide more accurate readings.
5+
## Upon Install
6+
Use an app that needs this module, like `Smart Battery Widget`.
7+
When this app is installed, <i><b>do not rely on it for the first 24-30 hours.</b></i>
8+
The module might return different data than expected, or a severely low prediction. Give it time. It will learn from drainage rates, which needs the battery to drain. If your watch normally lasts for a long time on one charge, it will take longer for the module to return an accurate reading.
9+
10+
If you think something is wrong with the predictions after 3 days, try clearing the data, and let it rebuild again from scratch.
11+
## Clock Infos
12+
The module provides two clockInfos:
13+
- Days left
14+
- Learned drainage rate per hour
15+
16+
## Settings
17+
### Clear Data - Clears all learned data.
18+
Use this when you switch to a new clock or change the battery drainage in a fundamental way. The app averages drainage over time, and so you might just want to restart the learned data to be more accurate for the new configurations you have implemented.
19+
### Logging - Enables logging for stats that this module uses.
20+
To view the log file, go to the [Web IDE](https://www.espruino.com/ide/#), click on the storage icon (4 discs), and scroll to the file named `smartbattlog.json`. From there, you can view the file, copy to editor, or save it to your computer.
21+
Logs:
22+
* The time in a human-readable format (hh:mm:ss, mm:dd:yy) when the record event was triggered
23+
* The current battery percentage
24+
* The last saved battery percentage
25+
* The change in hours between the time last recorded and now
26+
* The average or learned drainage for battery per hour
27+
* The status of that record event:
28+
* Recorded
29+
* Skipped due to battery fluctuations or no change
30+
* Invalid time between the two periods (first record)
31+
## Functions
32+
From any app, you can call `require("smartbatt")` and then one of the functions below:
33+
* `require("smartbatt").record()` - Attempts to record the battery and push it to the average.
34+
* `require("smartbatt").get()` - Returns an object that contains:
35+
36+
37+
* `hrsRemaining` - Hours remaining
38+
* `avgDrainage` - Learned battery drainage per hour
39+
* `totalCycles` - Total times the battery has been recorded and averaged
40+
* `totalHours` - Total hours recorded
41+
* `batt` - Current battery level
42+
43+
44+
* `require("smartbatt").deleteData()` - Deletes all learned data. (Automatically re-learns)
45+
## Creator
46+
- RKBoss6
47+
## Contributors
48+
- RelapsingCertainly

apps/smartbatt/clkinfo.js

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
(function() {
2+
var batt;
3+
//updates values
4+
5+
6+
function getHrsFormatted(hrsLeft){
7+
8+
var daysLeft = hrsLeft / 24;
9+
daysLeft = Math.round(daysLeft);
10+
if(daysLeft >= 1) {
11+
return daysLeft+"d";
12+
}
13+
14+
else {
15+
return Math.round(hrsLeft)+"h";
16+
}
17+
}
18+
19+
//draws battery icon and fill bar
20+
function drawBatt(){
21+
batt =E.getBattery();
22+
var s=24,g=Graphics.createArrayBuffer(24,24,1,{msb:true});
23+
g.fillRect(0,6,s-3,18).clearRect(2,8,s-5,16).fillRect(s-2,10,s,15).fillRect(3,9,3+batt*(s-9)/100,15);
24+
g.transparent=0;
25+
return g.asImage("string");
26+
}
27+
28+
//calls both updates for values and icons.
29+
//might split in the future since values updates once every five minutes so we dont need to call it in every minute while the battery can be updated once a minute.
30+
function updateDisplay(){
31+
drawBatt();
32+
}
33+
34+
return {
35+
name: "SmartBatt",
36+
items: [
37+
{ name : "BattStatus",
38+
get : () => {
39+
40+
var img = drawBatt();
41+
var data=require("smartbatt").get();
42+
43+
//update clock info according to batt state
44+
if (Bangle.isCharging()) {
45+
return { text: batt+"%", img };
46+
}
47+
else{
48+
return { text: getHrsFormatted(data.hrsLeft), img };
49+
}
50+
},
51+
52+
show : function() {
53+
this.interval = setInterval(()=>{
54+
updateDisplay();
55+
this.emit('redraw');
56+
}, 300000);
57+
},
58+
59+
hide : function() {
60+
clearInterval(this.interval);
61+
this.interval = undefined;
62+
}
63+
},
64+
{ name : "AvgDrainage",
65+
get : () => {
66+
var img = drawBatt()
67+
var data=require("smartbatt").get();
68+
return { text: data.avgDrainage.toFixed(2)+"/h", img };
69+
},
70+
71+
show : function() {
72+
this.interval = setInterval(()=>{
73+
this.emit('redraw');
74+
}, 300000);
75+
},
76+
77+
hide : function() {
78+
clearInterval(this.interval);
79+
this.interval = undefined;
80+
}
81+
}
82+
]
83+
};
84+
})

apps/smartbatt/icon.png

403 KB
Loading

apps/smartbatt/metadata.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"id": "smartbatt",
3+
"name": "Smart Battery Module",
4+
"shortName": "Smart Battery",
5+
"version": "0.01",
6+
"description": "Provides a `smartbatt` module that returns the battery in days, and learns from daily usage over time for accurate predictions.",
7+
"icon": "icon.png",
8+
"type": "module",
9+
"tags": "tool,system,clkinfo",
10+
"supports": ["BANGLEJS","BANGLEJS2"],
11+
"provides_modules" : ["smartbatt"],
12+
"readme": "README.md",
13+
"storage": [
14+
{"name":"smartbatt","url":"module.js"},
15+
{"name":"smartbatt.settings.js","url":"settings.js"},
16+
{"name":"smartbatt.clkinfo.js","url":"clkinfo.js"}
17+
],
18+
"data": [
19+
{"name":"smartbatt.settings.json"}
20+
]
21+
}

apps/smartbatt/module.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
{
2+
var dataFile = "smartbattdata.json";
3+
var interval;
4+
var storage = require("Storage");
5+
6+
7+
var logFile = "smartbattlog.json";
8+
9+
function getSettings() {
10+
return Object.assign({
11+
//Record Interval stored in ms
12+
doLogging: false
13+
}, require('Storage').readJSON("smartbatt.settings.json", true) || {});
14+
}
15+
16+
function logBatterySample(entry) {
17+
let log = storage.readJSON(logFile, 1) || [];
18+
//get human-readable time
19+
let d = new Date();
20+
entry.time = d.getFullYear() + "-" +
21+
("0" + (d.getMonth() + 1)).slice(-2) + "-" +
22+
("0" + d.getDate()).slice(-2) + " " +
23+
("0" + d.getHours()).slice(-2) + ":" +
24+
("0" + d.getMinutes()).slice(-2) + ":" +
25+
("0" + d.getSeconds()).slice(-2);
26+
27+
log.push(entry);
28+
if (log.length > 100) log = log.slice(-100);
29+
30+
storage.writeJSON(logFile, log);
31+
}
32+
33+
34+
// Record current battery reading into current average
35+
function recordBattery() {
36+
let now = Date.now();
37+
let data = getData();
38+
let batt = E.getBattery();
39+
let battChange = data.battLastRecorded - batt;
40+
let deltaHours = (now - data.timeLastRecorded) / (1000 * 60 * 60);
41+
// Default reason (in case we skip)
42+
let reason = "Recorded";
43+
44+
45+
if (battChange <= 0) {
46+
reason = "Skipped: battery fluctuated or no change";
47+
if (Math.abs(battChange) < 5) {
48+
//less than 6% difference, average percents
49+
var newBatt = (batt + data.battLastRecorded) / 2;
50+
data.battLastRecorded = newBatt;
51+
} else {
52+
//probably charged, ignore average
53+
data.battLastRecorded = batt;
54+
}
55+
56+
storage.writeJSON(dataFile, data);
57+
} else if (deltaHours <= 0 || !isFinite(deltaHours)) {
58+
reason = "Skipped: invalid time delta";
59+
data.timeLastRecorded = now;
60+
data.battLastRecorded = batt;
61+
storage.writeJSON(dataFile, data);
62+
} else {
63+
let weightCoefficient = 1;
64+
let currentDrainage = battChange / deltaHours;
65+
let newAvg = weightedAverage(data.avgBattDrainage, data.totalHours, currentDrainage, deltaHours * weightCoefficient);
66+
data.avgBattDrainage = newAvg;
67+
data.timeLastRecorded = now;
68+
data.totalCycles += 1;
69+
data.totalHours += deltaHours;
70+
data.battLastRecorded = batt;
71+
storage.writeJSON(dataFile, data);
72+
73+
reason = "Drainage recorded: " + currentDrainage.toFixed(3) + "%/hr";
74+
}
75+
if (getSettings().doLogging) {
76+
// Always log the sample
77+
logBatterySample({
78+
battNow: batt,
79+
battLast: data.battLastRecorded,
80+
battChange: battChange,
81+
deltaHours: deltaHours,
82+
timeLastRecorded: data.timeLastRecorded,
83+
avgDrainage: data.avgBattDrainage,
84+
reason: reason
85+
});
86+
}
87+
}
88+
89+
function weightedAverage(oldValue, oldWeight, newValue, newWeight) {
90+
return (oldValue * oldWeight + newValue * newWeight) / (oldWeight + newWeight);
91+
}
92+
93+
94+
95+
function getData() {
96+
return storage.readJSON(dataFile, 1) || {
97+
avgBattDrainage: 0,
98+
battLastRecorded: E.getBattery(),
99+
timeLastRecorded: Date.now(),
100+
totalCycles: 0,
101+
totalHours: 0,
102+
};
103+
}
104+
105+
106+
107+
// Estimate hours remaining
108+
function estimateBatteryLife() {
109+
let data = getData();
110+
var batt = E.getBattery();
111+
var hrsLeft = Math.abs(batt / data.avgBattDrainage);
112+
return {
113+
batt: batt,
114+
hrsLeft: hrsLeft,
115+
avgDrainage:data.avgBattDrainage,
116+
totalHours:data.totalHours,
117+
cycles:data.totalCycles
118+
};
119+
}
120+
121+
function deleteData() {
122+
storage.erase(dataFile);
123+
storage.erase(logFile);
124+
}
125+
// Expose public API
126+
exports.record = recordBattery;
127+
exports.deleteData = deleteData;
128+
exports.get = estimateBatteryLife;
129+
exports.changeInterval = function (newInterval) {
130+
clearInterval(interval);
131+
interval = setInterval(recordBattery, newInterval);
132+
};
133+
// Start recording every 5 minutes
134+
interval = setInterval(recordBattery, 600000);
135+
recordBattery(); // Log immediately
136+
}

apps/smartbatt/settings.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
(function(back) {
3+
var FILE = "smartbatt.settings.json";
4+
// Load settings
5+
var settings = Object.assign({
6+
//Record Interval stored in ms
7+
doLogging:false
8+
}, require('Storage').readJSON(FILE, true) || {});
9+
10+
function writeSettings() {
11+
require('Storage').writeJSON(FILE, settings);
12+
}
13+
14+
// Show the menu
15+
E.showMenu({
16+
"" : { "title" : "Smart Day Battery" },
17+
"< Back" : () => back(),
18+
19+
'Clear Data': function () {
20+
E.showPrompt("Are you sure you want to delete all learned data?", {title:"Confirm"})
21+
.then(function(v) {
22+
if (v) {
23+
require("smartbatt").deleteData();
24+
E.showMessage("Successfully cleared data!","Cleared");
25+
} else {
26+
eval(require("Storage").read("smartbatt.settings.js"))(()=>load());
27+
28+
}
29+
});
30+
},
31+
'Log Battery': {
32+
value: !!settings.doLogging, // !! converts undefined to false
33+
onchange: v => {
34+
settings.doLogging = v;
35+
writeSettings();
36+
}
37+
// format: ... may be specified as a function which converts the value to a string
38+
// if the value is a boolean, showMenu() will convert this automatically, which
39+
// keeps settings menus consistent
40+
},
41+
});
42+
})

0 commit comments

Comments
 (0)