Skip to content

Commit 85fdc4b

Browse files
uservovcia2
authored andcommitted
A Tea Timer application
1 parent 1ec19d4 commit 85fdc4b

File tree

6 files changed

+138
-0
lines changed

6 files changed

+138
-0
lines changed

apps/ateatimer/ChangeLog

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.01: First release

apps/ateatimer/app-icon.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/ateatimer/app.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
// Tea Timer Application for Bangle.js 2
2+
let timerDuration = (() => {
3+
let file = require("Storage").open("ateatimer.data", "r");
4+
let data = file.read(4); // Assuming 4 bytes for storage
5+
return data ? parseInt(data, 10) : 4 * 60; // Default to 4 minutes
6+
})();
7+
let timeRemaining = timerDuration;
8+
let timerRunning = false;
9+
let buzzInterval = null; // Interval for buzzing when timer reaches 0
10+
let timerInterval = null; // Interval for timer countdown
11+
12+
function saveDefaultDuration() {
13+
let file = require("Storage").open("ateatimer.data", "w");
14+
file.write(timerDuration.toString());
15+
}
16+
17+
function drawTime() {
18+
g.clear();
19+
g.setFont("Vector", 40);
20+
g.setFontAlign(0, 0); // Center align
21+
22+
const minutes = Math.floor(Math.abs(timeRemaining) / 60);
23+
const seconds = Math.abs(timeRemaining) % 60;
24+
const sign = timeRemaining < 0 ? "-" : "";
25+
const timeStr = `${sign}${minutes}:${seconds.toString().padStart(2, '0')}`;
26+
27+
g.drawString(timeStr, g.getWidth() / 2, g.getHeight() / 2);
28+
29+
// Draw Increase button (triangle pointing up)
30+
g.fillPoly([
31+
g.getWidth() / 2, g.getHeight() / 2 - 80, // Top vertex
32+
g.getWidth() / 2 - 20, g.getHeight() / 2 - 60, // Bottom-left vertex
33+
g.getWidth() / 2 + 20, g.getHeight() / 2 - 60 // Bottom-right vertex
34+
]);
35+
36+
// Draw Decrease button (triangle pointing down)
37+
g.fillPoly([
38+
g.getWidth() / 2, g.getHeight() / 2 + 80, // Bottom vertex
39+
g.getWidth() / 2 - 20, g.getHeight() / 2 + 60, // Top-left vertex
40+
g.getWidth() / 2 + 20, g.getHeight() / 2 + 60 // Top-right vertex
41+
]);
42+
43+
g.flip();
44+
}
45+
46+
function startTimer() {
47+
if (timerRunning) return;
48+
timerRunning = true;
49+
50+
// Save the default duration on timer start
51+
timerDuration = timeRemaining;
52+
saveDefaultDuration();
53+
54+
timerInterval = setInterval(() => {
55+
timeRemaining--;
56+
drawTime();
57+
58+
if (timeRemaining === 0 && !buzzInterval) {
59+
// Start continuous vibration when timer reaches 0
60+
buzzInterval = setInterval(() => Bangle.buzz(500), 1000);
61+
}
62+
}, 1000);
63+
}
64+
65+
function resetTimer() {
66+
if (timerInterval) {
67+
clearInterval(timerInterval);
68+
timerInterval = null;
69+
}
70+
timerRunning = false;
71+
timeRemaining = timerDuration;
72+
stopBuzzing();
73+
drawTime();
74+
}
75+
76+
function stopBuzzing() {
77+
if (buzzInterval) {
78+
clearInterval(buzzInterval);
79+
buzzInterval = null;
80+
}
81+
}
82+
83+
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
89+
}
90+
drawTime();
91+
}
92+
93+
function handleTouch(x, y) {
94+
const centerY = g.getHeight() / 2;
95+
96+
if (y < centerY - 40) {
97+
// Increase button area
98+
adjustTime(60);
99+
} else if (y > centerY + 40) {
100+
// Decrease button area
101+
adjustTime(-60);
102+
} else {
103+
// Center area
104+
if (!timerRunning) {
105+
startTimer();
106+
}
107+
}
108+
}
109+
110+
// Handle physical button press for resetting timer
111+
setWatch(() => {
112+
resetTimer();
113+
}, BTN1, { repeat: true, edge: "falling" });
114+
115+
// Handle touch
116+
Bangle.on("touch", (zone, xy) => {
117+
handleTouch(xy.x, xy.y, false);
118+
});
119+
120+
// Draw the initial timer display
121+
drawTime();
122+

apps/ateatimer/app.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "duration": 240 }

apps/ateatimer/app.png

4.93 KB
Loading

apps/ateatimer/metadata.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{ "id": "ateatimer",
2+
"name": "A Tea Timer",
3+
"shortName":"A Tea Timer",
4+
"icon": "app.png",
5+
"version":"0.01",
6+
"description": "Simple app for setting countdown timers for tea. Press up and down to change time, and touch time to start counting. Button will stop countdown and reset counter to last used value.",
7+
"tags": "timer",
8+
"supports": ["BANGLEJS2"],
9+
"storage": [
10+
{"name":"ateatimer.app.js","url":"app.js"},
11+
{"name":"ateatimer.img","url":"app-icon.js","evaluate":true}
12+
]
13+
}

0 commit comments

Comments
 (0)