2020-04-02 07:34:47 +00:00
|
|
|
(function(){
|
2022-01-06 00:59:22 +00:00
|
|
|
const intervalLow = 60000; // update time when not charging
|
|
|
|
const intervalHigh = 2000; // update time when charging
|
|
|
|
|
2023-01-28 09:06:01 +00:00
|
|
|
let prevMin = 100;
|
|
|
|
|
2021-09-18 14:59:58 +00:00
|
|
|
let COLORS = {};
|
2022-01-06 00:59:22 +00:00
|
|
|
|
2021-09-18 14:59:58 +00:00
|
|
|
if (process.env.HWVERSION == 1) {
|
|
|
|
COLORS = {
|
|
|
|
'white': -1, // White
|
|
|
|
'charging': 0x07E0, // Green
|
|
|
|
'high': 0x05E0, // lightly darker green
|
|
|
|
'ok': 0xFD20, // Orange
|
|
|
|
'low': 0xF800, // Red
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
// bangle 2 is only 7 bit colors
|
|
|
|
COLORS = {
|
|
|
|
'white': "#fff", // White
|
|
|
|
'charging': "#0f0", // Green
|
|
|
|
'high': "#0f0", // Green
|
|
|
|
'ok': "#ff0", // Orange
|
|
|
|
'low': "#f00", // Red
|
2022-01-06 00:59:22 +00:00
|
|
|
};
|
2020-05-23 21:32:33 +00:00
|
|
|
}
|
2022-01-06 00:59:22 +00:00
|
|
|
const SETTINGS_FILE = 'widbatpc.json';
|
2020-04-02 23:41:57 +00:00
|
|
|
|
2022-01-06 00:59:22 +00:00
|
|
|
let settings;
|
2020-05-23 21:32:33 +00:00
|
|
|
function loadSettings() {
|
2022-01-06 00:59:22 +00:00
|
|
|
settings = require('Storage').readJSON(SETTINGS_FILE, 1) || {};
|
2020-05-23 21:32:33 +00:00
|
|
|
const DEFAULTS = {
|
|
|
|
'color': 'By Level',
|
|
|
|
'percentage': true,
|
|
|
|
'charger': true,
|
|
|
|
'hideifmorethan': 100,
|
2022-01-03 18:57:42 +00:00
|
|
|
'alwaysoncharge': false,
|
2023-01-28 09:06:01 +00:00
|
|
|
'removejitter': 0, // 0 == off, 1 == downwards only
|
2023-01-29 10:00:28 +00:00
|
|
|
'buzzoncharge': true,
|
2020-05-23 21:32:33 +00:00
|
|
|
};
|
|
|
|
Object.keys(DEFAULTS).forEach(k=>{
|
2022-01-06 00:59:22 +00:00
|
|
|
if (settings[k]===undefined) settings[k]=DEFAULTS[k];
|
2020-05-23 21:32:33 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
function setting(key) {
|
2022-01-06 00:59:22 +00:00
|
|
|
if (!settings) { loadSettings(); }
|
2020-05-23 21:32:33 +00:00
|
|
|
return settings[key];
|
|
|
|
}
|
2020-04-02 23:41:57 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
const levelColor = (l) => {
|
2020-04-02 23:41:57 +00:00
|
|
|
// "charging" is very bright -> percentage is hard to read, "high" is ok(ish)
|
2022-01-06 00:59:22 +00:00
|
|
|
const green = setting('percentage') ? COLORS.high : COLORS.charging;
|
2020-05-23 21:32:33 +00:00
|
|
|
switch (setting('color')) {
|
|
|
|
case 'Monochrome': return COLORS.white; // no chance of reading the percentage here :-(
|
|
|
|
case 'Green': return green;
|
|
|
|
case 'By Level': // fall through
|
|
|
|
default:
|
|
|
|
if (setting('charger')) {
|
2020-04-02 23:41:57 +00:00
|
|
|
// charger icon -> always make percentage readable
|
2020-05-23 21:32:33 +00:00
|
|
|
if (Bangle.isCharging() || l >= 50) return green;
|
|
|
|
} else {
|
2020-04-02 23:41:57 +00:00
|
|
|
// no icon -> brightest green to indicate charging, even when showing percentage
|
2020-05-23 21:32:33 +00:00
|
|
|
if (Bangle.isCharging()) return COLORS.charging;
|
|
|
|
if (l >= 50) return COLORS.high;
|
|
|
|
}
|
|
|
|
if (l >= 15) return COLORS.ok;
|
|
|
|
return COLORS.low;
|
|
|
|
}
|
2022-01-06 00:59:22 +00:00
|
|
|
};
|
2020-05-23 21:32:33 +00:00
|
|
|
const chargerColor = () => {
|
2022-01-06 00:59:22 +00:00
|
|
|
return (setting('color') === 'Monochrome') ? COLORS.white : COLORS.charging;
|
|
|
|
};
|
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
// sets width, returns true if it changed
|
|
|
|
function setWidth() {
|
|
|
|
var w = 40;
|
|
|
|
if (Bangle.isCharging() && setting('charger'))
|
|
|
|
w += 16;
|
2022-01-03 21:41:40 +00:00
|
|
|
if (E.getBattery() > setting('hideifmorethan')) {
|
2020-05-23 21:32:33 +00:00
|
|
|
w = 0;
|
2022-01-03 21:41:40 +00:00
|
|
|
if( Bangle.isCharging() && setting('alwaysoncharge') === true)
|
|
|
|
w = 56;
|
|
|
|
}
|
2020-05-23 21:32:33 +00:00
|
|
|
var changed = WIDGETS["batpc"].width != w;
|
|
|
|
WIDGETS["batpc"].width = w;
|
|
|
|
return changed;
|
2020-04-02 23:41:57 +00:00
|
|
|
}
|
2022-01-06 00:59:22 +00:00
|
|
|
|
2023-02-26 09:28:18 +00:00
|
|
|
function draw(fromInterval) {
|
2020-04-20 09:06:23 +00:00
|
|
|
// if hidden, don't draw
|
2020-05-23 21:32:33 +00:00
|
|
|
if (!WIDGETS["batpc"].width) return;
|
|
|
|
// else...
|
|
|
|
var s = 39;
|
|
|
|
var x = this.x, y = this.y;
|
2023-01-28 09:06:01 +00:00
|
|
|
let l = E.getBattery();
|
|
|
|
if (setting('removejitter') === 1) {
|
|
|
|
// if we have seen a battery percentage that was lower than current, use lower
|
|
|
|
if (Bangle.isCharging()) {
|
|
|
|
prevMin = l; // charging is the only way to increase percentage
|
|
|
|
} else if (prevMin >= l) {
|
|
|
|
prevMin = l;
|
|
|
|
} else {
|
|
|
|
l = prevMin;
|
|
|
|
}
|
|
|
|
}
|
2023-02-26 09:06:43 +00:00
|
|
|
|
2023-02-26 09:28:18 +00:00
|
|
|
if (fromInterval === true && this.prevLevel === l && this.prevCharging === Bangle.isCharging()) {
|
2023-02-26 09:06:43 +00:00
|
|
|
return; // unchanged, do nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
this.prevLevel = l;
|
|
|
|
this.prevCharging = Bangle.isCharging();
|
|
|
|
|
2023-01-28 09:06:01 +00:00
|
|
|
const c = levelColor(l);
|
2021-10-11 13:31:10 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
if (Bangle.isCharging() && setting('charger')) {
|
|
|
|
g.setColor(chargerColor()).drawImage(atob(
|
|
|
|
"DhgBHOBzgc4HOP////////////////////3/4HgB4AeAHgB4AeAHgB4AeAHg"),x,y);
|
|
|
|
x+=16;
|
|
|
|
}
|
2021-11-30 21:30:28 +00:00
|
|
|
|
|
|
|
let xl = x+4+l*(s-12)/100;
|
|
|
|
// show bar full in the level color, as you can't see the color if the bar is too small
|
|
|
|
if (setting('fillbar'))
|
|
|
|
xl = x+4+100*(s-12)/100;
|
|
|
|
|
2021-09-18 14:59:58 +00:00
|
|
|
g.setColor(g.theme.fg);
|
2020-05-23 21:32:33 +00:00
|
|
|
g.fillRect(x,y+2,x+s-4,y+21);
|
|
|
|
g.clearRect(x+2,y+4,x+s-6,y+19);
|
|
|
|
g.fillRect(x+s-3,y+10,x+s,y+14);
|
|
|
|
g.setColor(c).fillRect(x+4,y+6,xl,y+17);
|
2021-09-18 14:59:58 +00:00
|
|
|
g.setColor(g.theme.fg);
|
2020-05-23 21:32:33 +00:00
|
|
|
if (!setting('percentage')) {
|
|
|
|
return;
|
|
|
|
}
|
2022-01-06 00:59:22 +00:00
|
|
|
let gfx = g;
|
2020-05-23 21:32:33 +00:00
|
|
|
if (setting('color') === 'Monochrome') {
|
2020-04-04 19:17:30 +00:00
|
|
|
// draw text inverted on battery level
|
2021-09-18 14:59:58 +00:00
|
|
|
gfx = Graphics.createCallback(g.getWidth(),g.getHeight(), 1,
|
2022-01-06 00:59:22 +00:00
|
|
|
(x,y) => {g.setPixel(x,y,x<=xl?0:-1);});
|
2020-05-23 21:32:33 +00:00
|
|
|
}
|
|
|
|
gfx.setFontAlign(-1,-1);
|
|
|
|
if (l >= 100) {
|
|
|
|
gfx.setFont('4x6', 2);
|
|
|
|
gfx.drawString(l, x + 6, y + 7);
|
|
|
|
} else {
|
|
|
|
if (l < 10) x+=6;
|
|
|
|
gfx.setFont('6x8', 2);
|
|
|
|
gfx.drawString(l, x + 6, y + 4);
|
|
|
|
}
|
|
|
|
}
|
2022-01-06 00:59:22 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
// reload widget, e.g. when settings have changed
|
|
|
|
function reload() {
|
2022-01-06 00:59:22 +00:00
|
|
|
loadSettings();
|
2020-05-23 21:32:33 +00:00
|
|
|
// need to redraw all widgets, because changing the "charger" setting
|
|
|
|
// can affect the width and mess with the whole widget layout
|
2022-01-06 00:59:22 +00:00
|
|
|
setWidth();
|
2020-05-23 21:32:33 +00:00
|
|
|
Bangle.drawWidgets();
|
2020-04-04 19:17:30 +00:00
|
|
|
}
|
2022-01-06 00:59:22 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
// update widget - redraw just widget, or all widgets if size changed
|
|
|
|
function update() {
|
|
|
|
if (setWidth()) Bangle.drawWidgets();
|
|
|
|
else WIDGETS["batpc"].draw();
|
2022-01-06 00:59:22 +00:00
|
|
|
|
|
|
|
if (Bangle.isCharging()) changeInterval(id, intervalHigh);
|
|
|
|
else changeInterval(id, intervalLow);
|
2020-04-02 07:34:47 +00:00
|
|
|
}
|
2020-04-02 23:41:57 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
Bangle.on('charging',function(charging) {
|
2023-01-29 10:00:28 +00:00
|
|
|
if (setting('buzzoncharge')) {
|
|
|
|
if(charging) Bangle.buzz();
|
|
|
|
}
|
2020-05-23 21:32:33 +00:00
|
|
|
update();
|
|
|
|
g.flip();
|
|
|
|
});
|
2022-01-06 00:59:22 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
Bangle.on('lcdPower', function(on) {
|
2022-01-06 00:59:22 +00:00
|
|
|
if (on) update();
|
2020-05-23 21:32:33 +00:00
|
|
|
});
|
2022-01-06 00:59:22 +00:00
|
|
|
|
2023-04-25 21:09:05 +00:00
|
|
|
var id = setInterval(()=>WIDGETS["batpc"].draw(WIDGETS["batpc"], true), intervalLow);
|
2022-01-06 00:59:22 +00:00
|
|
|
|
2020-05-23 21:32:33 +00:00
|
|
|
WIDGETS["batpc"]={area:"tr",width:40,draw:draw,reload:reload};
|
|
|
|
setWidth();
|
2022-01-06 00:59:22 +00:00
|
|
|
})();
|