Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/twenties/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
0.04: Convert to boot code
0.05: Improve energy efficiency
0.06: Align with whole thirds of the hour.
0.07: Refactor to use the scheduling library.
37 changes: 6 additions & 31 deletions apps/twenties/boot.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
(() => {
const LOOP_INTERVAL = 1.2e6; // 20 minutes
const BUZZ_INTERVAL = 2e4; // 20 seconds

const isWorkTime = (d) =>
d.getDay() % 6 && d.getHours() >= 8 && d.getHours() < 18;

const scheduleNext = () => {
const now = new Date();
if (isWorkTime(now)) {
Bangle.buzz().then(() => setTimeout(Bangle.buzz, BUZZ_INTERVAL));
setTimeout(scheduleNext, LOOP_INTERVAL);
} else {
const next = new Date(now);
next.setHours(8, 0, 0, 0);
while (!isWorkTime(next)) next.setDate(next.getDate() + 1);
setTimeout(scheduleNext, next - now);
}
};

// Align so we fire at whole hour, 20 min past and 40 min past - not at arbitrary times.
const TIME_AT_BOOT = new Date();
const TIME_SINCE_WHOLE_THIRD_OF_HOUR = (TIME_AT_BOOT.getMinutes() % 20) * 6e4 + TIME_AT_BOOT.getSeconds() * 1e3;
setTimeout(scheduleNext,
LOOP_INTERVAL - TIME_SINCE_WHOLE_THIRD_OF_HOUR);
// Make sure we don't miss the 2nd buzz after 20 seconds if we rebooted during that interval.
if (TIME_SINCE_WHOLE_THIRD_OF_HOUR <= BUZZ_INTERVAL) {
setTimeout(Bangle.buzz,
BUZZ_INTERVAL - TIME_SINCE_WHOLE_THIRD_OF_HOUR);
}
})();
{ // This boot script is deleted once the companion alarm is set up.
// The app will continue working by having the alarm rearm itself until
// the app is uninstalled.
require("twenties").setup();
setTimeout(require("Storage").erase, 1000, "twenties.boot.js");
}
53 changes: 53 additions & 0 deletions apps/twenties/lib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
exports.getTimeAtNextBuzz = () => {
const isWorkTime = (d) =>
d.getDay() % 6 && d.getHours() >= 8 && d.getHours() < 18;
const isLookAwayTime = (d) =>
d.getMinutes() % 20 === 0 && d.getSeconds() < 20;
const NOW = new Date();
let t = 8 * 3600000;
if (isWorkTime(NOW)) {
if (isLookAwayTime(NOW)) {
t = NOW.getHours() * 3600000 +
NOW.getMinutes() * 60000 + 20 * 1000;
} else {
t = NOW.getHours() * 3600000 +
(NOW.getMinutes() + (20 - NOW.getMinutes() % 20)) * 60000;
}
}
print(t);
return t;
};

exports.buzzAndSetup = function () {
setTimeout(() => { // Timeout to try and not interfere with `edgeclk` redrawing the time.
Bangle.buzz();
exports.setup();
}, 10)
};

const JS_DELETE_ALARM_THEN_BUZZ_AND_SETUP = `require("sched").setAlarm("twenties", undefined); require('twenties').buzzAndSetup();`;

const S = require("sched");

exports.setup = function () {
const TIME_AT_NEXT_BUZZ = exports.getTimeAtNextBuzz();
const TIME_AT_SETUP = new Date();
let alarm = {
on: true,
t: TIME_AT_NEXT_BUZZ,
dow: 0b0111110,
hidden: true,
group: "Hidden",
js: JS_DELETE_ALARM_THEN_BUZZ_AND_SETUP
};
if (TIME_AT_NEXT_BUZZ <
TIME_AT_SETUP.getHours() * 3600000 +
TIME_AT_SETUP.getMinutes() * 60000 +
TIME_AT_SETUP.getSeconds() * 1000) { // FIXME: this is done to work around a behavior in sched library. I think that is maybe a :BUG: that we should fix there. But unsure. It's around https://github.com/espruino/BangleApps/blob/master/apps/sched/boot.js#L21-L21
alarm.last = Date().getDate();
}
S.setAlarm("twenties", alarm);
S.reload();
};
}
7 changes: 5 additions & 2 deletions apps/twenties/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"id": "twenties",
"name": "Twenties",
"shortName": "Twenties",
"version": "0.06",
"version": "0.07",
"description": "Buzzes every 20m to stand / sit and look 20ft away for 20s.",
"icon": "app.png",
"type": "bootloader",
Expand All @@ -11,5 +11,8 @@
"supports": ["BANGLEJS", "BANGLEJS2"],
"allow_emulator": true,
"readme": "README.md",
"storage": [{ "name": "twenties.boot.js", "url": "boot.js" }]
"storage": [
{ "name": "twenties.boot.js", "url": "boot.js" },
{ "name": "twenties", "url": "lib.js" }
]
}
Loading