mirror of https://github.com/espruino/BangleApps
commit
e0a0b4a54d
18
apps.json
18
apps.json
|
@ -456,6 +456,24 @@
|
|||
{"name":"widbatpc.json"}
|
||||
]
|
||||
},
|
||||
{ "id": "widbatwarn",
|
||||
"name": "Battery Warning",
|
||||
"shortName": "Battery Warning",
|
||||
"icon": "widget.png",
|
||||
"readme": "README.md",
|
||||
"version":"0.01",
|
||||
"description": "Show a warning when the battery runs low.",
|
||||
"tags": "tool,battery",
|
||||
"type":"widget",
|
||||
"dependencies": { "notify":"type" },
|
||||
"storage": [
|
||||
{"name":"widbatwarn.wid.js","url":"widget.js"},
|
||||
{"name":"widbatwarn.settings.js","url":"settings.js"}
|
||||
],
|
||||
"data": [
|
||||
{"name":"widbatwarn.json"}
|
||||
]
|
||||
},
|
||||
{ "id": "widbt",
|
||||
"name": "Bluetooth Widget",
|
||||
"icon": "widget.png",
|
||||
|
|
|
@ -0,0 +1 @@
|
|||
0.01: New Battery Warning!
|
|
@ -0,0 +1,14 @@
|
|||
# Battery Warning
|
||||
|
||||
Get a notification when your battery runs low.
|
||||
|
||||

|
||||
|
||||
## Settings:
|
||||
|
||||
* Percentage: Warn when battery reaches this level
|
||||
* Buzz: Enable/disable buzzer
|
||||
|
||||
## Creator
|
||||
|
||||
Richard de Boer <rigrig+banglejs@tubul.net>
|
Binary file not shown.
After Width: | Height: | Size: 127 B |
|
@ -0,0 +1,2 @@
|
|||
widget.png: https://icons8.com/icon/vga9edYpuB7a/warning-battery
|
||||
battery-low.png: https://icons8.com/icon/96282/low-battery (recolored)
|
Binary file not shown.
After Width: | Height: | Size: 5.0 KiB |
|
@ -0,0 +1,47 @@
|
|||
/**
|
||||
* @param {function} back Use back() to return to settings menu
|
||||
*/
|
||||
(function(back) {
|
||||
const SETTINGS_FILE = "widbatwarn.json",
|
||||
storage = require("Storage"),
|
||||
translate = require("locale").translate;
|
||||
|
||||
// initialize with default settings...
|
||||
let s = {
|
||||
buzz: true,
|
||||
percentage: 10,
|
||||
};
|
||||
// ...and overwrite them with any saved values
|
||||
// This way saved values are preserved if a new version adds more settings
|
||||
const saved = storage.readJSON(SETTINGS_FILE, 1) || {};
|
||||
for(const key in saved) {
|
||||
s[key] = saved[key];
|
||||
}
|
||||
|
||||
// creates a function to safe a specific setting, e.g. save('buzz')(true)
|
||||
function save(key) {
|
||||
return function(value) {
|
||||
s[key] = value;
|
||||
storage.write(SETTINGS_FILE, s);
|
||||
WIDGETS["batwarn"].reload();
|
||||
};
|
||||
}
|
||||
|
||||
const menu = {
|
||||
"": {"title": "Battery Warning"},
|
||||
"< Back": back,
|
||||
"Percentage": {
|
||||
value: s.percentage,
|
||||
min: 5,
|
||||
max: 100,
|
||||
step: 5,
|
||||
onchange: save("percentage"),
|
||||
},
|
||||
"Buzz": {
|
||||
value: s.buzz,
|
||||
format: b => translate(b?"Yes":"No"),
|
||||
onchange: save("buzz"),
|
||||
},
|
||||
};
|
||||
E.showMenu(menu);
|
||||
});
|
|
@ -0,0 +1,59 @@
|
|||
(function() {
|
||||
const SETTINGS_FILE = "widbatwarn.json";
|
||||
let settings;
|
||||
|
||||
function loadSettings() {
|
||||
settings = require("Storage").readJSON(SETTINGS_FILE, 1) || {};
|
||||
const DEFAULTS = {
|
||||
buzz: true,
|
||||
percentage: 10,
|
||||
};
|
||||
Object.keys(DEFAULTS).forEach(k => {
|
||||
if (settings[k]===undefined) settings[k] = DEFAULTS[k];
|
||||
});
|
||||
}
|
||||
|
||||
function setting(key) {
|
||||
if (!settings) { loadSettings(); }
|
||||
return settings[key];
|
||||
}
|
||||
|
||||
let warning = false; // did we show the warning already?
|
||||
function check() {
|
||||
if (Bangle.isCharging()
|
||||
|| E.getBattery()>setting("percentage")) {
|
||||
require("notify").hide({id: "widbatwarn"});
|
||||
warning = false;
|
||||
return;
|
||||
}
|
||||
if (warning) return; // already warned
|
||||
warning = true; // only show once (until we recharge)
|
||||
require("notify").show({
|
||||
size: 56, id: "widbatwarn",
|
||||
// battery-low.png
|
||||
icon: require("heatshrink").decompress(atob("jEYwgfchnM5nABaQJCBoQLSAhAL/Bf6bHAAYLpACgA==")),
|
||||
title: "Low Battery",
|
||||
render: a => {
|
||||
g.setFont("6x8", 2).setFontAlign(-1, 0)
|
||||
.setColor(-1).drawString("Battery: ", a.x+8, a.y+a.h/2)
|
||||
.setColor(0xF800).drawString(`${E.getBattery()}%`, a.x+8+100, a.y+a.h/2);
|
||||
},
|
||||
});
|
||||
if (setting("buzz")) Bangle.buzz();
|
||||
}
|
||||
|
||||
Bangle.on("charging", check);
|
||||
|
||||
function reload() {
|
||||
loadSettings();
|
||||
// check right away, so user sees it work when fiddling with settings
|
||||
check();
|
||||
}
|
||||
|
||||
// we never draw anything, but 'area' and 'draw' are required
|
||||
WIDGETS["batwarn"] = {width: 0, reload: reload, area: "tl", draw: () => {}};
|
||||
|
||||
// check every minute
|
||||
// deliberately not right away, to prevent instant notifications in settings
|
||||
setTimeout(check, 60000);
|
||||
})();
|
Binary file not shown.
After Width: | Height: | Size: 209 B |
Loading…
Reference in New Issue