Update widget.js

Increase screen update rate when charging, as requested by issue https://github.com/espruino/BangleApps/issues/1137
master
Hilmar Strauch 2022-01-06 01:59:22 +01:00 committed by GitHub
parent b53a08fd9e
commit d305de0bdc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 31 additions and 28 deletions

View File

@ -1,6 +1,9 @@
(function(){ (function(){
const intervalLow = 60000; // update time when not charging
const intervalHigh = 2000; // update time when charging
let COLORS = {}; let COLORS = {};
if (process.env.HWVERSION == 1) { if (process.env.HWVERSION == 1) {
COLORS = { COLORS = {
'white': -1, // White 'white': -1, // White
@ -17,13 +20,13 @@
'high': "#0f0", // Green 'high': "#0f0", // Green
'ok': "#ff0", // Orange 'ok': "#ff0", // Orange
'low': "#f00", // Red 'low': "#f00", // Red
}; };
} }
const SETTINGS_FILE = 'widbatpc.json' const SETTINGS_FILE = 'widbatpc.json';
let settings let settings;
function loadSettings() { function loadSettings() {
settings = require('Storage').readJSON(SETTINGS_FILE, 1) || {} settings = require('Storage').readJSON(SETTINGS_FILE, 1) || {};
const DEFAULTS = { const DEFAULTS = {
'color': 'By Level', 'color': 'By Level',
'percentage': true, 'percentage': true,
@ -32,17 +35,17 @@
'alwaysoncharge': false, 'alwaysoncharge': false,
}; };
Object.keys(DEFAULTS).forEach(k=>{ Object.keys(DEFAULTS).forEach(k=>{
if (settings[k]===undefined) settings[k]=DEFAULTS[k] if (settings[k]===undefined) settings[k]=DEFAULTS[k];
}); });
} }
function setting(key) { function setting(key) {
if (!settings) { loadSettings() } if (!settings) { loadSettings(); }
return settings[key]; return settings[key];
} }
const levelColor = (l) => { const levelColor = (l) => {
// "charging" is very bright -> percentage is hard to read, "high" is ok(ish) // "charging" is very bright -> percentage is hard to read, "high" is ok(ish)
const green = setting('percentage') ? COLORS.high : COLORS.charging const green = setting('percentage') ? COLORS.high : COLORS.charging;
switch (setting('color')) { switch (setting('color')) {
case 'Monochrome': return COLORS.white; // no chance of reading the percentage here :-( case 'Monochrome': return COLORS.white; // no chance of reading the percentage here :-(
case 'Green': return green; case 'Green': return green;
@ -59,10 +62,11 @@
if (l >= 15) return COLORS.ok; if (l >= 15) return COLORS.ok;
return COLORS.low; return COLORS.low;
} }
} };
const chargerColor = () => { const chargerColor = () => {
return (setting('color') === 'Monochrome') ? COLORS.white : COLORS.charging return (setting('color') === 'Monochrome') ? COLORS.white : COLORS.charging;
} };
// sets width, returns true if it changed // sets width, returns true if it changed
function setWidth() { function setWidth() {
var w = 40; var w = 40;
@ -77,6 +81,7 @@
WIDGETS["batpc"].width = w; WIDGETS["batpc"].width = w;
return changed; return changed;
} }
function draw() { function draw() {
// if hidden, don't draw // if hidden, don't draw
if (!WIDGETS["batpc"].width) return; if (!WIDGETS["batpc"].width) return;
@ -106,11 +111,11 @@
if (!setting('percentage')) { if (!setting('percentage')) {
return; return;
} }
let gfx = g let gfx = g;
if (setting('color') === 'Monochrome') { if (setting('color') === 'Monochrome') {
// draw text inverted on battery level // draw text inverted on battery level
gfx = Graphics.createCallback(g.getWidth(),g.getHeight(), 1, gfx = Graphics.createCallback(g.getWidth(),g.getHeight(), 1,
(x,y) => {g.setPixel(x,y,x<=xl?0:-1)}) (x,y) => {g.setPixel(x,y,x<=xl?0:-1);});
} }
gfx.setFontAlign(-1,-1); gfx.setFontAlign(-1,-1);
if (l >= 100) { if (l >= 100) {
@ -122,19 +127,24 @@
gfx.drawString(l, x + 6, y + 4); gfx.drawString(l, x + 6, y + 4);
} }
} }
// reload widget, e.g. when settings have changed // reload widget, e.g. when settings have changed
function reload() { function reload() {
loadSettings() loadSettings();
// need to redraw all widgets, because changing the "charger" setting // need to redraw all widgets, because changing the "charger" setting
// can affect the width and mess with the whole widget layout // can affect the width and mess with the whole widget layout
setWidth() setWidth();
g.clear(); g.clear();
Bangle.drawWidgets(); Bangle.drawWidgets();
} }
// update widget - redraw just widget, or all widgets if size changed // update widget - redraw just widget, or all widgets if size changed
function update() { function update() {
if (setWidth()) Bangle.drawWidgets(); if (setWidth()) Bangle.drawWidgets();
else WIDGETS["batpc"].draw(); else WIDGETS["batpc"].draw();
if (Bangle.isCharging()) changeInterval(id, intervalHigh);
else changeInterval(id, intervalLow);
} }
Bangle.on('charging',function(charging) { Bangle.on('charging',function(charging) {
@ -142,20 +152,13 @@
update(); update();
g.flip(); g.flip();
}); });
var batteryInterval;
Bangle.on('lcdPower', function(on) { Bangle.on('lcdPower', function(on) {
if (on) { if (on) update();
update();
// refresh once a minute if LCD on
if (!batteryInterval)
batteryInterval = setInterval(update, 60000);
} else {
if (batteryInterval) {
clearInterval(batteryInterval);
batteryInterval = undefined;
}
}
}); });
var id = setInterval(()=>WIDGETS["batpc"].draw(), intervalLow);
WIDGETS["batpc"]={area:"tr",width:40,draw:draw,reload:reload}; WIDGETS["batpc"]={area:"tr",width:40,draw:draw,reload:reload};
setWidth(); setWidth();
})() })();