Skip to content

Commit d744d4d

Browse files
committed
Refactored to use sched API
1 parent 85fdc4b commit d744d4d

File tree

2 files changed

+57
-31
lines changed

2 files changed

+57
-31
lines changed

apps/ateatimer/app.js

Lines changed: 55 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
// Tea Timer Application for Bangle.js 2
1+
// Tea Timer Application for Bangle.js 2 using sched library
2+
23
let timerDuration = (() => {
34
let file = require("Storage").open("ateatimer.data", "r");
45
let data = file.read(4); // Assuming 4 bytes for storage
56
return data ? parseInt(data, 10) : 4 * 60; // Default to 4 minutes
6-
})();
7+
})();
78
let timeRemaining = timerDuration;
89
let timerRunning = false;
9-
let buzzInterval = null; // Interval for buzzing when timer reaches 0
10-
let timerInterval = null; // Interval for timer countdown
1110

1211
function saveDefaultDuration() {
1312
let file = require("Storage").open("ateatimer.data", "w");
@@ -51,42 +50,53 @@ function startTimer() {
5150
timerDuration = timeRemaining;
5251
saveDefaultDuration();
5352

54-
timerInterval = setInterval(() => {
55-
timeRemaining--;
56-
drawTime();
53+
// Schedule a new timer using the sched library
54+
require("sched").setAlarm("mytimer", {
55+
msg: "Tea is ready!",
56+
timer: timeRemaining * 1000, // Convert to milliseconds
57+
vibrate: ".." // Default vibration pattern
58+
});
5759

58-
if (timeRemaining === 0 && !buzzInterval) {
59-
// Start continuous vibration when timer reaches 0
60-
buzzInterval = setInterval(() => Bangle.buzz(500), 1000);
61-
}
62-
}, 1000);
60+
// Ensure the scheduler updates
61+
require("sched").reload();
62+
63+
// Start the secondary timer to update the display
64+
setInterval(updateDisplay, 1000);
6365
}
6466

6567
function resetTimer() {
66-
if (timerInterval) {
67-
clearInterval(timerInterval);
68-
timerInterval = null;
69-
}
68+
// Cancel the existing timer
69+
require("sched").setAlarm("mytimer", undefined);
70+
require("sched").reload();
71+
7072
timerRunning = false;
7173
timeRemaining = timerDuration;
72-
stopBuzzing();
7374
drawTime();
7475
}
7576

76-
function stopBuzzing() {
77-
if (buzzInterval) {
78-
clearInterval(buzzInterval);
79-
buzzInterval = null;
80-
}
81-
}
82-
8377
function adjustTime(amount) {
84-
if (!timerRunning) {
85-
timeRemaining += amount;
86-
timeRemaining = Math.floor(timeRemaining / 60) * 60; // Round to full minutes
87-
} else {
88-
timeRemaining += amount; // Allow adjustments during running
78+
timeRemaining += amount;
79+
timeRemaining = Math.max(1, timeRemaining); // Ensure time doesn't go negative
80+
print(timeRemaining);
81+
if (timerRunning) {
82+
// Update the existing timer with the new remaining time
83+
let alarm = require("sched").getAlarm("mytimer");
84+
if (alarm) {
85+
// Cancel the current alarm
86+
require("sched").setAlarm("mytimer", undefined);
87+
88+
// Set a new alarm with the updated time
89+
require("sched").setAlarm("mytimer", {
90+
msg: "Tea is ready!",
91+
timer: timeRemaining * 1000, // Convert to milliseconds
92+
vibrate: ".." // Default vibration pattern
93+
});
94+
95+
// Reload the scheduler to apply changes
96+
require("sched").reload();
97+
}
8998
}
99+
90100
drawTime();
91101
}
92102

@@ -107,6 +117,22 @@ function handleTouch(x, y) {
107117
}
108118
}
109119

120+
// Function to update the display every second
121+
function updateDisplay() {
122+
if (timerRunning) {
123+
let alarm = require("sched").getAlarm("mytimer");
124+
if (alarm) {
125+
timeRemaining = Math.ceil(require("sched").getTimeToAlarm(alarm) / 1000);
126+
}
127+
drawTime();
128+
if (timeRemaining <= 0) {
129+
timeRemaining = 0
130+
clearInterval(updateDisplay);
131+
timerRunning = false;
132+
}
133+
}
134+
}
135+
110136
// Handle physical button press for resetting timer
111137
setWatch(() => {
112138
resetTimer();
@@ -119,4 +145,3 @@ Bangle.on("touch", (zone, xy) => {
119145

120146
// Draw the initial timer display
121147
drawTime();
122-

apps/ateatimer/metadata.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
"storage": [
1010
{"name":"ateatimer.app.js","url":"app.js"},
1111
{"name":"ateatimer.img","url":"app-icon.js","evaluate":true}
12-
]
12+
],
13+
"dependencies": {"scheduler":"type"}
1314
}

0 commit comments

Comments
 (0)