diff --git a/apps/twenties/ChangeLog b/apps/twenties/ChangeLog index 89eb9547f1..d96dec678f 100644 --- a/apps/twenties/ChangeLog +++ b/apps/twenties/ChangeLog @@ -1,4 +1,5 @@ 0.01: New Widget! 0.02: Fix calling null on draw 0.03: Only vibrate during work -0.04: Convert to boot code \ No newline at end of file +0.04: Convert to boot code +0.05: Improve energy efficiency \ No newline at end of file diff --git a/apps/twenties/README.md b/apps/twenties/README.md index 8ee917b0e6..f008b1b3f4 100644 --- a/apps/twenties/README.md +++ b/apps/twenties/README.md @@ -1,6 +1,6 @@ # Twenties -Follow the [20-20-20 rule](https://www.aoa.org/AOA/Images/Patients/Eye%20Conditions/20-20-20-rule.pdf) with discrete reminders. Your Bangle will buzz every 20 minutes for you to look away from your screen, and then buzz 20 seconds later to look back. Additionally, alternate between standing and sitting every 20 minutes to be standing for [more than 30 minutes](https://uwaterloo.ca/kinesiology-health-sciences/how-long-should-you-stand-rather-sit-your-work-station) per hour. +Follow the [20-20-20 rule](https://www.aoa.org/AOA/Images/Patients/Eye%20Conditions/20-20-20-rule.pdf) with discrete reminders. Your Bangle will buzz every 20 minutes for you to look away from your screen, and then buzz 20 seconds later to look back. Additionally, alternate between standing and sitting every 20 minutes to be standing for [more than 30 minutes](https://uwaterloo.ca/news/how-long-should-you-stand-rather-sit-your-work-station) per hour. ## Usage diff --git a/apps/twenties/boot.js b/apps/twenties/boot.js index 722af43bcb..7bd591b1b8 100644 --- a/apps/twenties/boot.js +++ b/apps/twenties/boot.js @@ -1,19 +1,22 @@ (() => { - const move = 20 * 60 * 1000; // 20 minutes - const look = 20 * 1000; // 20 seconds + const LOOP_INTERVAL = 1.2e6; // 20 minutes + const BUZZ_INTERVAL = 2e4; // 20 seconds - const buzz = _ => { - const date = new Date(); - const day = date.getDay(); - const hour = date.getHours(); - // buzz at work - if (day >= 1 && day <= 5 && - hour >= 8 && hour <= 17) { - Bangle.buzz().then(_ => { - setTimeout(Bangle.buzz, look); - }); + 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); } }; - setInterval(buzz, move); // buzz to stand / sit + scheduleNext(); })(); diff --git a/apps/twenties/metadata.json b/apps/twenties/metadata.json index dcbe0b565f..6927d5d70a 100644 --- a/apps/twenties/metadata.json +++ b/apps/twenties/metadata.json @@ -1,13 +1,14 @@ { "id": "twenties", "name": "Twenties", - "shortName": "twenties", - "version": "0.04", + "shortName": "Twenties", + "version": "0.05", "description": "Buzzes every 20m to stand / sit and look 20ft away for 20s.", "icon": "app.png", "type": "bootloader", "tags": "alarm,tool,health", "supports": ["BANGLEJS", "BANGLEJS2"], + "allow_emulator": true, "readme": "README.md", "storage": [{ "name": "twenties.boot.js", "url": "boot.js" }] }