1
0
Fork 0

powermanager - New option to automatically calibrate the battery

master
Martin Boonk 2022-11-04 18:34:15 +01:00
parent 5c110a89fb
commit a79b130e88
6 changed files with 36 additions and 34 deletions

View File

@ -1,3 +1,5 @@
0.01: New App!
0.02: Allow forcing monotonic battery voltage/percentage
0.03: Use default Bangle formatter for booleans
0.04: Remove calibration with current voltage (Calibrate->Auto) as it is now handled by settings app
Allow automatic calibration on every charge longer than 3 hours

View File

@ -3,8 +3,9 @@
Manages settings for charging.
Features:
* Warning threshold to be able to disconnect the charger at a given percentage
* Set the battery calibration offset.
* Set the battery calibration offset
* Force monotonic battery percentage or voltage
* Automatic calibration on charging uninterrupted longer than 3 hours (reloads of the watch reset the timer).
## Internals

View File

@ -48,4 +48,13 @@
return v;
};
}
if (settings.autoCalibration){
let chargeStart;
Bangle.on("charging", (charging)=>{
if (charging) chargeStart = Date.now();
if (chargeStart && !charging && (Date.now() - chargeStart > 1000*60*60*3)) require("powermanager").setCalibration();
if (!charging) chargeStart = undefined;
});
}
})();

6
apps/powermanager/lib.js Normal file
View File

@ -0,0 +1,6 @@
// set battery calibration value by either applying the given value or setting the currently read battery voltage
exports.setCalibration = function(calibration){
let s = require('Storage').readJSON("setting.json", true) || {};
s.batFullVoltage = calibration?calibration:((analogRead(D3) + analogRead(D3) + analogRead(D3) + analogRead(D3)) / 4);
require('Storage').writeJSON("setting.json", s);
}

View File

@ -2,7 +2,7 @@
"id": "powermanager",
"name": "Power Manager",
"shortName": "Power Manager",
"version": "0.03",
"version": "0.04",
"description": "Allow configuration of warnings and thresholds for battery charging and display.",
"icon": "app.png",
"type": "bootloader",
@ -12,6 +12,7 @@
"storage": [
{"name":"powermanager.boot.js","url":"boot.js"},
{"name":"powermanager.settings.js","url":"settings.js"},
{"name":"powermanager","url":"lib.js"},
{"name":"powermanager.default.json","url":"default.json"}
]
}

View File

@ -23,7 +23,6 @@
'': {
'title': 'Power Manager'
},
'< Back': back,
'Monotonic percentage': {
value: !!settings.forceMonoPercentage,
onchange: v => {
@ -44,30 +43,30 @@
}
};
function roundToDigits(number, stepsize) {
return Math.round(number / stepsize) * stepsize;
}
function getCurrentVoltageDirect() {
return (analogRead(D3) + analogRead(D3) + analogRead(D3) + analogRead(D3)) / 4;
}
var stepsize = 0.0002;
var full = 0.32;
var full = 0.3144;
function getInitialCalibrationOffset() {
return roundToDigits(systemsettings.batFullVoltage - full, stepsize) || 0;
}
var submenu_calibrate = {
'': {
title: "Calibrate"
},
'< Back': function() {
title: "Calibrate",
back: function() {
E.showMenu(mainmenu);
},
},
'Autodetect': {
value: !!settings.autoCalibration,
onchange: v => {
writeSettings("autoCalibration", v);
}
},
'Offset': {
min: -0.05,
max: 0.05,
@ -75,25 +74,9 @@
value: getInitialCalibrationOffset(),
format: v => roundToDigits(v, stepsize).toFixed((""+stepsize).length - 2),
onchange: v => {
print(typeof v);
systemsettings.batFullVoltage = v + full;
require("Storage").writeJSON("setting.json", systemsettings);
require("powermanager").setCalibration(v + full);
}
},
'Auto': function() {
var newVoltage = getCurrentVoltageDirect();
E.showAlert("Please charge fully before auto setting").then(() => {
E.showPrompt("Set current charge as full").then((r) => {
if (r) {
systemsettings.batFullVoltage = newVoltage;
require("Storage").writeJSON("setting.json", systemsettings);
//reset value shown in menu to the newly set one
submenu_calibrate.Offset.value = getInitialCalibrationOffset();
E.showMenu(mainmenu);
}
});
});
},
'Clear': function() {
E.showPrompt("Clear charging offset?").then((r) => {
if (r) {
@ -109,11 +92,11 @@
var submenu_chargewarn = {
'': {
title: "Charge warning"
},
'< Back': function() {
title: "Charge warning",
back: function() {
E.showMenu(mainmenu);
},
},
'Enabled': {
value: !!settings.warnEnabled,
format: v => settings.warnEnabled ? "On" : "Off",