-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3691 from vovcia-clug/master
A Tea Timer application
- Loading branch information
Showing
6 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
0.01: First release | ||
0.02: Fix icon, utilize sched, show running timer on app relaunch |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
// Tea Timer Application for Bangle.js 2 using sched library | ||
|
||
let timerDuration = (() => { | ||
let file = require("Storage").open("ateatimer.data", "r"); | ||
let data = file.read(4); // Assuming 4 bytes for storage | ||
return data ? parseInt(data, 10) : 4 * 60; // Default to 4 minutes | ||
})(); | ||
let timeRemaining = timerDuration; | ||
let timerRunning = false; | ||
|
||
function saveDefaultDuration() { | ||
let file = require("Storage").open("ateatimer.data", "w"); | ||
file.write(timerDuration.toString()); | ||
} | ||
|
||
function drawTime() { | ||
g.clear(); | ||
g.setFont("Vector", 40); | ||
g.setFontAlign(0, 0); // Center align | ||
|
||
const minutes = Math.floor(Math.abs(timeRemaining) / 60); | ||
const seconds = Math.abs(timeRemaining) % 60; | ||
const sign = timeRemaining < 0 ? "-" : ""; | ||
const timeStr = `${sign}${minutes}:${seconds.toString().padStart(2, '0')}`; | ||
|
||
g.drawString(timeStr, g.getWidth() / 2, g.getHeight() / 2); | ||
|
||
// Draw Increase button (triangle pointing up) | ||
g.fillPoly([ | ||
g.getWidth() / 2, g.getHeight() / 2 - 80, // Top vertex | ||
g.getWidth() / 2 - 20, g.getHeight() / 2 - 60, // Bottom-left vertex | ||
g.getWidth() / 2 + 20, g.getHeight() / 2 - 60 // Bottom-right vertex | ||
]); | ||
|
||
// Draw Decrease button (triangle pointing down) | ||
g.fillPoly([ | ||
g.getWidth() / 2, g.getHeight() / 2 + 80, // Bottom vertex | ||
g.getWidth() / 2 - 20, g.getHeight() / 2 + 60, // Top-left vertex | ||
g.getWidth() / 2 + 20, g.getHeight() / 2 + 60 // Top-right vertex | ||
]); | ||
|
||
g.flip(); | ||
} | ||
|
||
function startTimer() { | ||
if (timerRunning) return; | ||
if (timeRemaining == 0) return; | ||
timerRunning = true; | ||
|
||
// Save the default duration on timer start | ||
timerDuration = timeRemaining; | ||
saveDefaultDuration(); | ||
scheduleTimer(); | ||
|
||
// Start the secondary timer to update the display | ||
setInterval(updateDisplay, 1000); | ||
} | ||
|
||
function scheduleTimer() { | ||
// Schedule a new timer using the sched library | ||
require("sched").setAlarm("ateatimer", { | ||
msg: "Tea is ready!", | ||
timer: timeRemaining * 1000, // Convert to milliseconds | ||
vibrate: ".." // Default vibration pattern | ||
}); | ||
|
||
// Ensure the scheduler updates | ||
require("sched").reload(); | ||
} | ||
|
||
function resetTimer() { | ||
// Cancel the existing timer | ||
require("sched").setAlarm("ateatimer", undefined); | ||
require("sched").reload(); | ||
|
||
timerRunning = false; | ||
timeRemaining = timerDuration; | ||
drawTime(); | ||
} | ||
|
||
function adjustTime(amount) { | ||
if (-amount > timeRemaining) { | ||
// Return if result will be negative | ||
return; | ||
} | ||
timeRemaining += amount; | ||
timeRemaining = Math.max(0, timeRemaining); // Ensure time doesn't go negative | ||
if (timerRunning) { | ||
// Update the existing timer with the new remaining time | ||
let alarm = require("sched").getAlarm("ateatimer"); | ||
if (alarm) { | ||
// Cancel the current alarm | ||
require("sched").setAlarm("ateatimer", undefined); | ||
|
||
// Set a new alarm with the updated time | ||
scheduleTimer(); | ||
} | ||
} | ||
|
||
drawTime(); | ||
} | ||
|
||
function handleTouch(x, y) { | ||
const centerY = g.getHeight() / 2; | ||
|
||
if (y < centerY - 40) { | ||
// Increase button area | ||
adjustTime(60); | ||
} else if (y > centerY + 40) { | ||
// Decrease button area | ||
adjustTime(-60); | ||
} else { | ||
// Center area | ||
if (!timerRunning) { | ||
startTimer(); | ||
} | ||
} | ||
} | ||
|
||
// Function to update the display every second | ||
function updateDisplay() { | ||
if (timerRunning) { | ||
let alarm = require("sched").getAlarm("ateatimer"); | ||
timeRemaining = Math.floor(require("sched").getTimeToAlarm(alarm) / 1000); | ||
drawTime(); | ||
if (timeRemaining <= 0) { | ||
timeRemaining = 0; | ||
clearInterval(updateDisplay); | ||
timerRunning = false; | ||
} | ||
} | ||
} | ||
|
||
// Handle physical button press for resetting timer | ||
setWatch(() => { | ||
if (timerRunning) { | ||
resetTimer(); | ||
} else { | ||
startTimer(); | ||
} | ||
}, BTN1, { repeat: true, edge: "falling" }); | ||
|
||
// Handle touch | ||
Bangle.on("touch", (zone, xy) => { | ||
handleTouch(xy.x, xy.y, false); | ||
}); | ||
|
||
let isRunning = require("sched").getAlarm("ateatimer"); | ||
if (isRunning) { | ||
timerRunning = true; | ||
// Start the timer to update the display | ||
setInterval(updateDisplay, 1000); | ||
} else { | ||
// Draw the initial timer display | ||
drawTime(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{ "duration": 240 } |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ "id": "ateatimer", | ||
"name": "A Tea Timer", | ||
"shortName":"A Tea Timer", | ||
"icon": "app.png", | ||
"version":"0.02", | ||
"description": "Simple app for setting timers for tea. Touch up and down to change time, and time or button to start counting. When timer is running, button will stop timer and reset counter to last used value.", | ||
"tags": "timer", | ||
"supports": ["BANGLEJS2"], | ||
"storage": [ | ||
{"name":"ateatimer.app.js","url":"app.js"}, | ||
{"name":"ateatimer.img","url":"app-icon.js","evaluate":true} | ||
], | ||
"dependencies": {"scheduler":"type"} | ||
} |