Refactored to use sched API

master
vovcia 2024-12-13 22:26:50 +01:00
parent 85fdc4be5c
commit d744d4d9d9
2 changed files with 57 additions and 31 deletions

View File

@ -1,13 +1,12 @@
// Tea Timer Application for Bangle.js 2 // Tea Timer Application for Bangle.js 2 using sched library
let timerDuration = (() => { let timerDuration = (() => {
let file = require("Storage").open("ateatimer.data", "r"); let file = require("Storage").open("ateatimer.data", "r");
let data = file.read(4); // Assuming 4 bytes for storage let data = file.read(4); // Assuming 4 bytes for storage
return data ? parseInt(data, 10) : 4 * 60; // Default to 4 minutes return data ? parseInt(data, 10) : 4 * 60; // Default to 4 minutes
})(); })();
let timeRemaining = timerDuration; let timeRemaining = timerDuration;
let timerRunning = false; let timerRunning = false;
let buzzInterval = null; // Interval for buzzing when timer reaches 0
let timerInterval = null; // Interval for timer countdown
function saveDefaultDuration() { function saveDefaultDuration() {
let file = require("Storage").open("ateatimer.data", "w"); let file = require("Storage").open("ateatimer.data", "w");
@ -51,42 +50,53 @@ function startTimer() {
timerDuration = timeRemaining; timerDuration = timeRemaining;
saveDefaultDuration(); saveDefaultDuration();
timerInterval = setInterval(() => { // Schedule a new timer using the sched library
timeRemaining--; require("sched").setAlarm("mytimer", {
drawTime(); msg: "Tea is ready!",
timer: timeRemaining * 1000, // Convert to milliseconds
vibrate: ".." // Default vibration pattern
});
if (timeRemaining === 0 && !buzzInterval) { // Ensure the scheduler updates
// Start continuous vibration when timer reaches 0 require("sched").reload();
buzzInterval = setInterval(() => Bangle.buzz(500), 1000);
} // Start the secondary timer to update the display
}, 1000); setInterval(updateDisplay, 1000);
} }
function resetTimer() { function resetTimer() {
if (timerInterval) { // Cancel the existing timer
clearInterval(timerInterval); require("sched").setAlarm("mytimer", undefined);
timerInterval = null; require("sched").reload();
}
timerRunning = false; timerRunning = false;
timeRemaining = timerDuration; timeRemaining = timerDuration;
stopBuzzing();
drawTime(); drawTime();
} }
function stopBuzzing() {
if (buzzInterval) {
clearInterval(buzzInterval);
buzzInterval = null;
}
}
function adjustTime(amount) { function adjustTime(amount) {
if (!timerRunning) { timeRemaining += amount;
timeRemaining += amount; timeRemaining = Math.max(1, timeRemaining); // Ensure time doesn't go negative
timeRemaining = Math.floor(timeRemaining / 60) * 60; // Round to full minutes print(timeRemaining);
} else { if (timerRunning) {
timeRemaining += amount; // Allow adjustments during running // Update the existing timer with the new remaining time
let alarm = require("sched").getAlarm("mytimer");
if (alarm) {
// Cancel the current alarm
require("sched").setAlarm("mytimer", undefined);
// Set a new alarm with the updated time
require("sched").setAlarm("mytimer", {
msg: "Tea is ready!",
timer: timeRemaining * 1000, // Convert to milliseconds
vibrate: ".." // Default vibration pattern
});
// Reload the scheduler to apply changes
require("sched").reload();
}
} }
drawTime(); drawTime();
} }
@ -107,6 +117,22 @@ function handleTouch(x, y) {
} }
} }
// Function to update the display every second
function updateDisplay() {
if (timerRunning) {
let alarm = require("sched").getAlarm("mytimer");
if (alarm) {
timeRemaining = Math.ceil(require("sched").getTimeToAlarm(alarm) / 1000);
}
drawTime();
if (timeRemaining <= 0) {
timeRemaining = 0
clearInterval(updateDisplay);
timerRunning = false;
}
}
}
// Handle physical button press for resetting timer // Handle physical button press for resetting timer
setWatch(() => { setWatch(() => {
resetTimer(); resetTimer();
@ -119,4 +145,3 @@ Bangle.on("touch", (zone, xy) => {
// Draw the initial timer display // Draw the initial timer display
drawTime(); drawTime();

View File

@ -9,5 +9,6 @@
"storage": [ "storage": [
{"name":"ateatimer.app.js","url":"app.js"}, {"name":"ateatimer.app.js","url":"app.js"},
{"name":"ateatimer.img","url":"app-icon.js","evaluate":true} {"name":"ateatimer.img","url":"app-icon.js","evaluate":true}
] ],
"dependencies": {"scheduler":"type"}
} }