Merge pull request #784 from rigrig/qmsched

Quiet mode schedule: add widget, simplify logic
pull/757/head^2
Gordon Williams 2021-08-12 09:52:02 +01:00 committed by GitHub
commit 1c706121e4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 79 additions and 47 deletions

View File

@ -185,7 +185,7 @@
{ "id": "setting",
"name": "Settings",
"icon": "settings.png",
"version":"0.27",
"version":"0.28",
"description": "A menu for setting up Bangle.js",
"tags": "tool,system,b2",
"readme": "README.md",
@ -3242,18 +3242,19 @@
]
},
{ "id": "qmsched",
"name": "Quiet Mode Schedule",
"name": "Quiet Mode Schedule and Widget",
"shortName":"Quiet Mode",
"icon": "app.png",
"version":"0.01",
"version":"0.02",
"description": "Automatically turn Quiet Mode on or off at set times",
"readme": "README.md",
"tags": "tool",
"tags": "tool,widget",
"storage": [
{"name":"qmsched","url":"lib.js"},
{"name":"qmsched.app.js","url":"app.js"},
{"name":"qmsched.boot.js","url":"boot.js"},
{"name":"qmsched.img","url":"icon.js","evaluate":true}
{"name":"qmsched.img","url":"icon.js","evaluate":true},
{"name":"qmsched.wid.js","url":"widget.js"}
],
"data": [
{"name":"qmsched.json"}

View File

@ -1 +1,3 @@
0.01: First version
0.02: Add widget

View File

@ -1,5 +1,9 @@
# Quiet Mode Schedule
# Quiet Mode Schedule and Widget
Automatically turn Quiet Mode on or off at set times.
Automatically turn Quiet Mode on or off at set times, and display a widget when enabled.
### Edit Schedule:
![Main menu](screenshot_main.png) ![Edit Schedule menu](screenshot_edit.png)
### Widget:
![Widget, quiet mode: silent](screenshot_widget_silent.png) ![Widget, quiet mode: alarms](screenshot_widget_alarms.png)

View File

@ -5,18 +5,25 @@ const modeNames = ["Off", "Alarms", "Silent"];
let scheds = require("Storage").readJSON("qmsched.json", 1);
/*scheds = [
{ hr : 6.5, // hours + minutes/60
last : 0, // last day of the month we fired on - so we don't switch twice in one day!
mode : 1, // quiet mode (0/1/2)
}
];*/
if (!scheds) {
// set default schedule on first load of app
scheds = [
{"hr": 8, "mode": 0, "last": 25},
{"hr": 22, "mode": 1, "last": 25},
{"hr": 8, "mode": 0},
{"hr": 22, "mode": 1},
];
require("Storage").writeJSON("qmsched.json", scheds);
}
if (scheds.length && scheds.some(s => "last" in s)) {
// cleanup: remove "last" values (used by old versions)
scheds = scheds.map(s => {
delete s.last;
return s;
});
require("Storage").writeJSON("qmsched.json", scheds);
}
function formatTime(t) {
const hrs = 0|t;
@ -24,11 +31,6 @@ function formatTime(t) {
return (" "+hrs).substr(-2)+":"+("0"+mins).substr(-2);
}
function getCurrentHr() {
const time = new Date();
return time.getHours()+(time.getMinutes()/60)+(time.getSeconds()/3600);
}
function showMainMenu() {
const menu = {
"": {"title": "Quiet Mode"},
@ -36,8 +38,8 @@ function showMainMenu() {
value: (require("Storage").readJSON("setting.json", 1) || {}).quiet|0,
format: v => modeNames[v],
onchange: function(v) {
if (v<0) v = 2;
if (v>2) v = 0;
if (v<0) {v = 2;}
if (v>2) {v = 0;}
require("qmsched").setMode(v);
this.value = v;
},
@ -71,8 +73,8 @@ function showEditMenu(index) {
"Hours": {
value: hrs,
onchange: function(v) {
if (v<0) v = 23;
if (v>23) v = 0;
if (v<0) {v = 23;}
if (v>23) {v = 0;}
hrs = v;
this.value = v;
}, // no arrow fn -> preserve 'this'
@ -80,8 +82,8 @@ function showEditMenu(index) {
"Minutes": {
value: mins,
onchange: function(v) {
if (v<0) v = 59;
if (v>59) v = 0;
if (v<0) {v = 59;}
if (v>59) {v = 0;}
mins = v;
this.value = v;
}, // no arrow fn -> preserve 'this'
@ -90,24 +92,17 @@ function showEditMenu(index) {
value: mode,
format: v => modeNames[v],
onchange: function(v) {
if (v<0) v = 2;
if (v>2) v = 0;
if (v<0) {v = 2;}
if (v>2) {v = 0;}
mode = v;
this.value = v;
}, // no arrow fn -> preserve 'this'
},
};
function getSched() {
const hr = hrs+(mins/60);
let day = 0;
// If schedule is for tomorrow not today (eg, in the past), set day
if (hr<getCurrentHr()) {
day = (new Date()).getDate();
}
return {
hr: hr,
hr: hrs+(mins/60),
mode: mode,
last: day,
};
}
menu["> Save"] = function() {

View File

@ -1,24 +1,19 @@
// apply Quiet Mode schedules
(function qm() {
let scheds = require("Storage").readJSON("qmsched.json", 1) || [];
if (!scheds.length) return;
let next,idx;
scheds.forEach(function(s, i) {
if (!next || (s.hr+s.last*24)<(next.hr+next.last*24)) {
next = s;
idx = i;
}
});
if (!scheds.length) { return;}
const now = new Date(),
hr = now.getHours()+(now.getMinutes()/60)+(now.getSeconds()/3600);
let t = 3600000*(next.hr-hr);
if (next.last===now.getDate()) t += 86400000;
hr = now.getHours()+(now.getMinutes()/60)+(now.getSeconds()/3600); // current (decimal) hour
scheds.sort((a, b) => a.hr-b.hr);
const tday = scheds.filter(s => s.hr>hr), // scheduled for today
tmor = scheds.filter(s => s.hr<=hr); // scheduled for tomorrow
const next = tday.length ? tday[0] : tmor[0],
mode = next.mode;
let t = 3600000*(next.hr-hr); // timeout in milliseconds
if (t<0) {t += 86400000;} // scheduled for tomorrow: add a day
/* update quiet mode at the correct time. */
setTimeout(function() {
let scheds = require("Storage").readJSON("qmsched.json", 1) || [];
require("qmsched").setMode(scheds[idx].mode);
scheds[idx].last = (new Date()).getDate();
require("Storage").writeJSON("qmsched.json", scheds);
setTimeout(() => {
require("qmsched").setMode(mode);
qm(); // schedule next update
}, t);
})();

View File

@ -14,4 +14,5 @@ exports.setMode = function(mode) {
if (s.brightness && s.brightness!=1) Bangle.setLCDBrightness(s.brightness);
}
if (mode && s.qmTimeout) Bangle.setLCDTimeout(s.qmTimeout);
if (typeof (WIDGETS)!=="undefined" && "qmsched" in WIDGETS) {WIDGETS["qmsched"].draw();}
};

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.8 KiB

32
apps/qmsched/widget.js Normal file
View File

@ -0,0 +1,32 @@
WIDGETS["qmsched"] = {
area: "tl", width: 24, draw: function() {
const mode = (require("Storage").readJSON("setting.json", 1) || {}).quiet|0;
if (mode===0) { // Off
if (this.width!==0) {
this.width = 0;
Bangle.drawWidgets();
}
return;
}
// not Off: make sure width is correct
if (this.width!==24) {
this.width = 24;
Bangle.drawWidgets();
return; // drawWidgets will call draw again
}
let x = this.x, y = this.y;
g.clearRect(x, y, x+23, y+23);
// quiet mode: draw dim red one-way-street sign
x = this.x+11;y = this.y+11; // center of widget
g.setColor(0.8, 0, 0).fillCircle(x, y, 8);
g.setColor(g.theme.bg).fillRect(x-6, y-2, x+6, y+2);
if (mode>1) {return;} // no alarms
// alarms still on: draw alarm icon in bottom-right corner
x = this.x+18;y = this.y+17; // center of alarm
g.setColor(1, 1, 0)
.fillCircle(x, y, 3) // alarm body
.fillRect(x-5, y+2, x+5, y+3) // bottom ridge
.fillRect(x-1, y-5, x+1, y+5).drawLine(x, y-6, x, y+6) // top+bottom
.drawLine(x+5, y-3, x+3, y-5).drawLine(x-5, y-3, x-3, y-5); // wriggles
},
};

View File

@ -30,3 +30,4 @@
0.25: Move boot.js code into 'boot' app itself
0.26: Use Bangle.softOff if available as this keeps the time
0.27: Add Theme menu
0.28: Update Quiet Mode widget (if present)

View File

@ -387,6 +387,7 @@ function showQuietModeMenu() {
settings.quiet = v%3;
updateSettings();
updateOptions();
if ("qmsched" in WIDGETS) {WIDGETS["qmsched"].draw();}
},
},
"LCD Brightness": {