Skip to content

Commit a9ebd29

Browse files
authored
Merge pull request #4231 from thyttan/twenties
Twenties: refactor to use the scheduling library instead of its own timeouts
2 parents afec122 + 805eec9 commit a9ebd29

5 files changed

Lines changed: 72 additions & 35 deletions

File tree

apps/twenties/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
0.05: Improve energy efficiency
66
0.06: Align with whole thirds of the hour.
77
0.07: Fix TypeError on scheduling for the next day.
8+
0.08: Refactor to use the scheduling library.

apps/twenties/README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,14 @@ Vibrates to remind you to stand up and look away for healthy living.
1212

1313
Only vibrates during work days and hours.
1414

15-
## Creator
15+
## Todo
1616

17-
[@splch](https://github.com/splch/)
17+
Add settings page.
18+
Add setting for active days and hours.
19+
Add setting for buzz strength/pattern.
20+
Add entry in settings to re-run setup.
21+
22+
## Contributors
23+
24+
[@splch](https://github.com/splch/) (Creator)
25+
[@thyttan](https://github.com/thyttan/)

apps/twenties/boot.js

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,6 @@
1-
(() => {
2-
const LOOP_INTERVAL = 1.2e6; // 20 minutes
3-
const BUZZ_INTERVAL = 2e4; // 20 seconds
4-
5-
const isWorkTime = (d) =>
6-
d.getDay() % 6 && d.getHours() >= 8 && d.getHours() < 18;
7-
8-
const scheduleNext = () => {
9-
const now = new Date();
10-
if (isWorkTime(now)) {
11-
Bangle.buzz().then(() => setTimeout(Bangle.buzz, BUZZ_INTERVAL));
12-
setTimeout(scheduleNext, LOOP_INTERVAL);
13-
} else {
14-
const next = new Date(now+864e5); // 24 hours
15-
next.setHours(8, 0, 0, 0);
16-
while (!isWorkTime(next)) next.setDate(next.getDate() + 1);
17-
setTimeout(scheduleNext, next - now);
18-
}
19-
};
20-
21-
// Align so we fire at whole hour, 20 min past and 40 min past - not at arbitrary times.
22-
const TIME_AT_BOOT = new Date();
23-
const TIME_SINCE_WHOLE_THIRD_OF_HOUR = (TIME_AT_BOOT.getMinutes() % 20) * 6e4 + TIME_AT_BOOT.getSeconds() * 1e3;
24-
setTimeout(scheduleNext,
25-
LOOP_INTERVAL - TIME_SINCE_WHOLE_THIRD_OF_HOUR);
26-
// Make sure we don't miss the 2nd buzz after 20 seconds if we rebooted during that interval.
27-
if (TIME_SINCE_WHOLE_THIRD_OF_HOUR <= BUZZ_INTERVAL) {
28-
setTimeout(Bangle.buzz,
29-
BUZZ_INTERVAL - TIME_SINCE_WHOLE_THIRD_OF_HOUR);
30-
}
31-
})();
1+
{ // This boot script is deleted once the companion alarm is set up.
2+
// The app will continue working by having the alarm rearm itself until
3+
// the app is uninstalled.
4+
require("twenties").setup();
5+
setTimeout(require("Storage").erase, 1000, "twenties.boot.js");
6+
}

apps/twenties/lib.js

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
// TODO:
3+
// - Add settings page.
4+
// - Add setting for active days and hours.
5+
// - Add setting for buzz strength/pattern.
6+
// - Add entry in settings to re-run setup.
7+
8+
const getTimeAtNextBuzz = () => {
9+
const isWorkTime = (d) =>
10+
d.getDay() % 6 && d.getHours() >= 8 && d.getHours() < 18;
11+
const isLookAwayTime = (d) =>
12+
d.getMinutes() % 20 === 0 && d.getSeconds() < 20;
13+
const NOW = new Date();
14+
let t = 8 * 3600000;
15+
if (isWorkTime(NOW)) {
16+
if (isLookAwayTime(NOW)) {
17+
t = NOW.getHours() * 3600000 +
18+
NOW.getMinutes() * 60000 + 20 * 1000;
19+
} else {
20+
t = NOW.getHours() * 3600000 +
21+
(NOW.getMinutes() + (20 - NOW.getMinutes() % 20)) * 60000;
22+
}
23+
}
24+
return t;
25+
};
26+
27+
exports.buzzAndSetup = function () {
28+
Bangle.buzz();
29+
exports.setup();
30+
};
31+
32+
const JS_DELETE_ALARM_THEN_BUZZ_AND_SETUP = `require("sched").setAlarm("twenties", undefined); require('twenties').buzzAndSetup();`;
33+
34+
const S = require("sched");
35+
36+
exports.setup = function () {
37+
const TIME_AT_NEXT_BUZZ = getTimeAtNextBuzz();
38+
let alarm = {
39+
on: true,
40+
t: TIME_AT_NEXT_BUZZ,
41+
dow: 0b0111110,
42+
hidden: true,
43+
msg: "Twenties",
44+
group: "Twenties",
45+
js: JS_DELETE_ALARM_THEN_BUZZ_AND_SETUP
46+
};
47+
S.setAlarm("twenties", alarm);
48+
S.reload();
49+
};
50+
}

apps/twenties/metadata.json

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "twenties",
33
"name": "Twenties",
44
"shortName": "Twenties",
5-
"version": "0.07",
5+
"version": "0.08",
66
"description": "Buzzes every 20m to stand / sit and look 20ft away for 20s.",
77
"icon": "app.png",
88
"type": "bootloader",
@@ -11,5 +11,8 @@
1111
"supports": ["BANGLEJS", "BANGLEJS2"],
1212
"allow_emulator": true,
1313
"readme": "README.md",
14-
"storage": [{ "name": "twenties.boot.js", "url": "boot.js" }]
14+
"storage": [
15+
{ "name": "twenties.boot.js", "url": "boot.js" },
16+
{ "name": "twenties", "url": "lib.js" }
17+
]
1518
}

0 commit comments

Comments
 (0)