11// Tea Timer Application for Bangle.js 2 using sched library
22
3+ let appRect = Bangle . appRect ;
4+ let middleY = appRect . y + appRect . w / 2 ;
5+ let middleRect = { x :appRect . x , y :middleY - 20 , w :appRect . w , h :40 }
6+ let GREEN = g . theme . dark ?0x03E0 :0x07E0 ;
7+
8+ let intervalID ;
9+
310let timerDuration = ( ( ) => {
411 let file = require ( "Storage" ) . open ( "ateatimer.data" , "r" ) ;
512 let data = file . read ( 4 ) ; // Assuming 4 bytes for storage
@@ -14,7 +21,7 @@ function saveDefaultDuration() {
1421}
1522
1623function drawTime ( ) {
17- g . clear ( ) ;
24+ g . clearRect ( middleRect ) ;
1825 g . setFont ( "Vector" , 40 ) ;
1926 g . setFontAlign ( 0 , 0 ) ; // Center align
2027
@@ -25,6 +32,10 @@ function drawTime() {
2532
2633 g . drawString ( timeStr , g . getWidth ( ) / 2 , g . getHeight ( ) / 2 ) ;
2734
35+ g . flip ( ) ;
36+ }
37+
38+ function drawButtons ( ) {
2839 // Draw Increase button (triangle pointing up)
2940 g . fillPoly ( [
3041 g . getWidth ( ) / 2 , g . getHeight ( ) / 2 - 80 , // Top vertex
@@ -42,6 +53,12 @@ function drawTime() {
4253 g . flip ( ) ;
4354}
4455
56+ function drawInit ( ) {
57+ g . clear ( true ) ;
58+ drawButtons ( ) ;
59+ drawTime ( ) ;
60+ }
61+
4562function startTimer ( ) {
4663 if ( timerRunning ) return ;
4764 if ( timeRemaining == 0 ) return ;
@@ -52,8 +69,13 @@ function startTimer() {
5269 saveDefaultDuration ( ) ;
5370 scheduleTimer ( ) ;
5471
72+ // Flash the time in green to indicate the timer started
73+ g . setColor ( GREEN )
74+ drawTime ( ) ;
75+ g . reset ( ) ;
76+
5577 // Start the secondary timer to update the display
56- setInterval ( updateDisplay , 1000 ) ;
78+ intervalID = setInterval ( updateDisplay , 1000 ) ;
5779}
5880
5981function scheduleTimer ( ) {
@@ -125,7 +147,8 @@ function updateDisplay() {
125147 drawTime ( ) ;
126148 if ( timeRemaining <= 0 ) {
127149 timeRemaining = 0 ;
128- clearInterval ( updateDisplay ) ;
150+ clearInterval ( intervalID ) ;
151+ intervalID = undefined ;
129152 timerRunning = false ;
130153 }
131154 }
@@ -146,11 +169,10 @@ Bangle.on("touch", (zone, xy) => {
146169} ) ;
147170
148171let isRunning = require ( "sched" ) . getAlarm ( "ateatimer" ) ;
172+ // Draw the initial timer display
173+ drawInit ( ) ;
149174if ( isRunning ) {
150175 timerRunning = true ;
151176 // Start the timer to update the display
152- setInterval ( updateDisplay , 1000 ) ;
153- } else {
154- // Draw the initial timer display
155- drawTime ( ) ;
156- }
177+ intervalID = setInterval ( updateDisplay , 1000 ) ;
178+ }
0 commit comments