Update widget.js
parent
faa6257974
commit
725d160442
|
|
@ -1,39 +1,11 @@
|
||||||
//Learns by averaging power usage every time it is updated
|
|
||||||
|
|
||||||
|
|
||||||
(function(){
|
(function(){
|
||||||
|
|
||||||
|
const intervalLow = 30000; // update interval when not charging
|
||||||
const storage = require("Storage");
|
const intervalHigh = 30000; // faster update when charging
|
||||||
const filename = "widsmartbatt.json";
|
|
||||||
let runningAvg;
|
|
||||||
var newJson=false;
|
|
||||||
|
|
||||||
//check if json file exists;
|
|
||||||
|
|
||||||
function CalcHoursRemaining (batt, usage) {
|
|
||||||
|
|
||||||
var hrsLeft = 175000 * batt / (100 * usage);
|
|
||||||
return hrsLeft;
|
|
||||||
|
|
||||||
};
|
|
||||||
|
|
||||||
function addValue(val) {
|
|
||||||
let summary = storage.readJSON(filename, 1) || { total: 0, count: 0 };
|
|
||||||
summary.total += val;
|
|
||||||
summary.count++;
|
|
||||||
storage.writeJSON(filename, summary); // SAVED!
|
|
||||||
|
|
||||||
let avg = summary.total / summary.count;
|
|
||||||
runningAvg=avg;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
const intervalLow = 60000; // update time when not charging
|
|
||||||
const intervalHigh = 2000; // update time when charging
|
|
||||||
var showPercent = false;
|
var showPercent = false;
|
||||||
const width = 40;
|
const width = 40;
|
||||||
const height = 24;
|
const height = 24;
|
||||||
|
|
||||||
let COLORS = {
|
let COLORS = {
|
||||||
'bg': g.theme.bg,
|
'bg': g.theme.bg,
|
||||||
'fg': g.theme.fg,
|
'fg': g.theme.fg,
|
||||||
|
|
@ -49,25 +21,22 @@
|
||||||
};
|
};
|
||||||
|
|
||||||
function draw() {
|
function draw() {
|
||||||
var _a = require("power_usage").get(), usage = _a.usage, batt = _a.batt;
|
let batt=E.getBattery();
|
||||||
var s = 29;
|
let data = require("smartbatt").get();
|
||||||
var x = this.x, y = this.y;
|
let hrsLeft=data.hrsLeft;
|
||||||
var txt;
|
let days = hrsLeft / 24;
|
||||||
|
|
||||||
//Add this to total and get the average
|
|
||||||
addValue(usage)
|
|
||||||
|
|
||||||
if(showPercent){
|
|
||||||
txt=batt;
|
|
||||||
}else{
|
|
||||||
var hrsLeft=CalcHoursRemaining(batt,runningAvg);
|
|
||||||
var days = hrsLeft / 24;
|
|
||||||
txt = days >= 1 ? "".concat(Math.round(Math.min(days, 99)), "d") : "".concat(Math.round(hrsLeft), "h");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
let txt = showPercent
|
||||||
|
? batt
|
||||||
|
: (days >= 1
|
||||||
|
? Math.round(Math.min(days, 99)) + "d"
|
||||||
|
: Math.round(hrsLeft) + "h");
|
||||||
|
if(Bangle.isCharging()) txt=E.getBattery();
|
||||||
|
let s = 29;
|
||||||
|
let x = this.x, y = this.y;
|
||||||
let xl = x + 4 + batt * (s - 12) / 100;
|
let xl = x + 4 + batt * (s - 12) / 100;
|
||||||
|
|
||||||
|
// Drawing code follows...
|
||||||
g.setColor(COLORS.bg);
|
g.setColor(COLORS.bg);
|
||||||
g.fillRect(x + 2, y + 5, x + s - 6, y + 18);
|
g.fillRect(x + 2, y + 5, x + s - 6, y + 18);
|
||||||
|
|
||||||
|
|
@ -76,8 +45,8 @@
|
||||||
g.fillRect(x + 1, y + 19, x + s - 5, y + 20);
|
g.fillRect(x + 1, y + 19, x + s - 5, y + 20);
|
||||||
g.fillRect(x, y + 4, x + 1, y + 19);
|
g.fillRect(x, y + 4, x + 1, y + 19);
|
||||||
g.fillRect(x + s - 5, y + 4, x + s - 4, y + 19);
|
g.fillRect(x + s - 5, y + 4, x + s - 4, y + 19);
|
||||||
g.fillRect(x+s-3,y+8,x+s-2,y+16); // tip of the battery
|
g.fillRect(x + s - 3, y + 8, x + s - 2, y + 16);
|
||||||
g.fillRect(x+4,y+15,xl,y+16); // charging bar
|
g.fillRect(x + 4, y + 15, xl, y + 16);
|
||||||
|
|
||||||
g.setColor(COLORS.fg);
|
g.setColor(COLORS.fg);
|
||||||
g.setFontAlign(0, 0);
|
g.setFontAlign(0, 0);
|
||||||
|
|
@ -89,22 +58,36 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
Bangle.on('charging',function(charging) { draw(); });
|
// Touch to temporarily show battery percent
|
||||||
var id = setInterval(()=>WIDGETS["widsmartbatt"].draw(), intervalLow);
|
|
||||||
Bangle.on("touch", function (_btn, xy) {
|
Bangle.on("touch", function (_btn, xy) {
|
||||||
if (WIDGETS["back"] || !xy)
|
if (WIDGETS["back"] || !xy) return;
|
||||||
return;
|
|
||||||
var oversize = 5;
|
|
||||||
|
|
||||||
|
var oversize = 5;
|
||||||
var w = WIDGETS["widsmartbatt"];
|
var w = WIDGETS["widsmartbatt"];
|
||||||
var x = xy.x, y = xy.y;
|
var x = xy.x, y = xy.y;
|
||||||
|
|
||||||
if (w.x - oversize <= x && x < w.x + width + oversize
|
if (w.x - oversize <= x && x < w.x + width + oversize
|
||||||
&& w.y - oversize <= y && y < w.y + height + oversize) {
|
&& w.y - oversize <= y && y < w.y + height + oversize) {
|
||||||
E.stopEventPropagation && E.stopEventPropagation();
|
E.stopEventPropagation && E.stopEventPropagation();
|
||||||
showPercent = true;
|
showPercent = true;
|
||||||
setTimeout(function () { return (showPercent = false, w.draw(w)); }, 3000);
|
setTimeout(() => {
|
||||||
|
showPercent = false;
|
||||||
|
w.draw(w);
|
||||||
|
}, 3000);
|
||||||
w.draw(w);
|
w.draw(w);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
WIDGETS["widsmartbatt"]={area:"tr",width:30,draw:draw};
|
|
||||||
|
// Update widget on charging state change
|
||||||
|
Bangle.on('charging', function () {
|
||||||
|
WIDGETS["widsmartbatt"].draw();
|
||||||
|
});
|
||||||
|
|
||||||
|
var id = setInterval(() => WIDGETS["widsmartbatt"].draw(), intervalLow);
|
||||||
|
|
||||||
|
WIDGETS["widsmartbatt"] = {
|
||||||
|
area: "tr",
|
||||||
|
width: 30,
|
||||||
|
draw: draw
|
||||||
|
};
|
||||||
})();
|
})();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue