1+ var sosInterval ;
2+ var currentColor = "#ffffff" ; // last selected color
3+
14function draw ( color ) {
25 if ( color == undefined ) {
36 color = - 1 ;
47 }
58 g . clear ( ) ;
69 g . setColor ( color ) ;
710 g . fillRect ( 0 , 0 , g . getWidth ( ) , g . getHeight ( ) ) ;
8- }
911
10- function draw2Pattern ( ) {
11- const colors = [ "ff0000" , "8080ff" , "00ff00" ,
12- "ffffff" ] ;
13- drawPattern ( 2 , colors ) ;
12+ // Store the current color only if it is a valid user-selected color
13+ // "#000000" is used internally when blinking (SOS off state)
14+ if ( typeof color === "string" && color !== "#000000" ) currentColor = color ;
1415}
1516
1617function draw3Pattern ( ) {
@@ -24,22 +25,62 @@ function drawPattern(size, colors) {
2425 g . clear ( ) ;
2526 var w = g . getWidth ( ) / size ;
2627 var h = g . getHeight ( ) / size ;
28+
29+ // Draw selectable color blocks
2730 for ( var i = 0 ; i < size ; i ++ ) {
2831 for ( var j = 0 ; j < size ; j ++ ) {
2932 var color = colors [ i * size + j ] ;
3033 g . setColor ( "#" + color ) ;
3134 g . fillRect ( j * w , i * h , j * w + w , i * h + h ) ;
3235 }
3336 }
37+
38+ // Touching a block sets the main screen color
3439 Bangle . on ( "touch" , function ( btn , xy ) {
40+ // If SOS mode is active, stop it when user interacts
41+ if ( sosInterval ) {
42+ clearInterval ( sosInterval ) ;
43+ sosInterval = undefined ;
44+ }
45+
3546 var x = parseInt ( ( xy . x ) / w ) ;
3647 var y = parseInt ( ( xy . y ) / h ) ;
3748 draw ( "#" + colors [ y * size + x ] ) ;
3849 } ) ;
3950}
4051
41- // Clear the screen once, at startup
42- // draw immediately at first
52+ // Start SOS blinking using the selected color
53+ function startSOS ( ) {
54+ if ( sosInterval ) return ;
55+ var on = false ;
56+ sosInterval = setInterval ( function ( ) {
57+ on = ! on ;
58+ if ( on ) {
59+ draw ( currentColor ) ; // active/on state
60+ } else {
61+ draw ( "#000000" ) ; // off/black state
62+ }
63+ } , 200 ) ; // blinking frequency
64+ }
65+
66+ // Stop SOS blinking
67+ function stopSOS ( ) {
68+ if ( sosInterval ) {
69+ clearInterval ( sosInterval ) ;
70+ sosInterval = undefined ;
71+ }
72+ }
73+
74+ // Toggle SOS mode using the hardware button
75+ setWatch ( function ( ) {
76+ if ( sosInterval ) {
77+ stopSOS ( ) ;
78+ } else {
79+ startSOS ( ) ;
80+ }
81+ } , BTN1 , { repeat :true , edge :"rising" } ) ;
82+
83+ // Show the color grid at startup
4384draw3Pattern ( ) ;
4485
4586/*
0 commit comments