1- // Tea Timer Application for Bangle.js 2
1+ // Tea Timer Application for Bangle.js 2 using sched library
2+
23let 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+ } ) ( ) ;
78let timeRemaining = timerDuration ;
89let timerRunning = false ;
9- let buzzInterval = null ; // Interval for buzzing when timer reaches 0
10- let timerInterval = null ; // Interval for timer countdown
1110
1211function 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
6567function 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-
8377function 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
111137setWatch ( ( ) => {
112138 resetTimer ( ) ;
@@ -119,4 +145,3 @@ Bangle.on("touch", (zone, xy) => {
119145
120146// Draw the initial timer display
121147drawTime ( ) ;
122-
0 commit comments