Merge pull request #973 from rigrig/widbars

New widget: Widbars
pull/978/head
Gordon Williams 2021-12-01 09:00:19 +00:00 committed by GitHub
commit 013d7704a9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 98 additions and 0 deletions

View File

@ -4558,6 +4558,21 @@
{ "name": "sensible.app.js", "url": "sensible.js" },
{ "name": "sensible.img", "url": "sensible-icon.js", "evaluate": true }
]
},
{
"id": "widbars",
"name": "Bars Widget",
"version": "0.01",
"description": "Display several measurements as vertical bars.",
"icon": "icon.png",
"screenshots": [{"url":"screenshot.png"}],
"readme": "README.md",
"type": "widget",
"tags": "widget",
"supports": ["BANGLEJS","BANGLEJS2"],
"storage": [
{"name":"widbars.wid.js","url":"widget.js"}
]
},
{
"id":"a_speech_timer",

1
apps/widbars/ChangeLog Normal file
View File

@ -0,0 +1 @@
0.01: New Widget!

15
apps/widbars/README.md Normal file
View File

@ -0,0 +1,15 @@
# Bars Widget
A simple widget that display several measurements as vertical bars.
![Screenshot](screenshot.png)
## Measurements from left to right:
<!-- HRM and Temperature are commented out (they didn't seem very useful)
- Current heart rate, on a scale from 0-200 bpm (*red*)<br>
Only if available: this widget does not turn on HRM monitoring by itself.
- Device temperature, on a scale from 0-50 °C (*yellow*)
-->
- Flash storage space used (*blue/cyan*)
- Memory usage (*magenta*)
- Battery charge (*green*)

BIN
apps/widbars/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

BIN
apps/widbars/screenshot.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

67
apps/widbars/widget.js Normal file
View File

@ -0,0 +1,67 @@
(() => {
const h=24, // widget height
w=3, // width of single bar
bars=3; // number of bars
// Note: HRM/temperature are commented out (they didn't seem very useful)
// If re-adding them, also adjust `bars`
// ==HRM start==
// // We show HRM if available, but don't turn it on
// let bpm,rst,con=10; // always ignore HRM with confidence below 10%
// function noHrm() { // last value is no longer valid
// if (rst) clearTimeout(rst);
// rst=bpm=undefined; con=10;
// WIDGETS["bars"].draw();
// }
// Bangle.on('HRM', hrm=>{
// if (hrm.confidence>con || hrm.confidence>=80) {
// bpm=hrm.confidence;
// con=hrm.confidence;
// WIDGETS["bars"].draw();
// if (rst) clearTimeout(rst);
// rst = setTimeout(noHrm, 10*60*1000); // forget HRM after 10 minutes
// }
// });
// ==HRM end==
/**
* Draw a bar
*
* @param {int} x left
* @param {int} y top (of full bar)
* @param {string} col Color
* @param {number} f Fraction of bar to draw
*/
function bar(x,y, col,f) {
if (!f) f = 0; // for f=NaN: set it to 0 -> don't even draw the bottom pixel
if (f>1) f = 1;
if (f<0) f = 0;
const top = Math.round((h-1)*(1-f));
// use Math.min/max to make sure we stay within widget boundaries for f=0/f=1
if (top) g .clearRect(x,y, x+w-1,y+top-1); // erase above bar
if (f) g.setColor(col).fillRect(x,y+top, x+w-1,y+h-1); // even for f=0.001 this is still 1 pixel high
}
function draw() {
g.reset();
const x = this.x, y = this.y,
m = process.memory();
let b=0;
// ==HRM== bar(x+(w*b++),y,'#f00'/*red */,bpm/200); // >200 seems very unhealthy; if we have no valid bpm this will just be empty space
// ==Temperature== bar(x+(w*b++),y,'#ff0'/*yellow */,E.getTemperature()/50); // you really don't want to wear a watch that's hotter than 50°C
bar(x+(w*b++),y,g.theme.dark?'#0ff':'#00f'/*cyan/blue*/,1-(require('Storage').getFree() / process.env.STORAGE));
bar(x+(w*b++),y,'#f0f'/*magenta*/,m.usage/m.total);
bar(x+(w*b++),y,'#0f0'/*green */,E.getBattery()/100);
}
let redraw;
Bangle.on('lcdPower', on => {
if (redraw) clearInterval(redraw)
redraw = undefined;
if (on) {
WIDGETS["bars"].draw();
redraw = setInterval(()=>WIDGETS["bars"].draw, 10*1000); // redraw every 10 seconds
}
});
WIDGETS["bars"]={area:"tr",width: bars*w,draw:draw};
})()