diff --git a/apps/wristlight/ChangeLog b/apps/wristlight/ChangeLog index 62542be608..816315b76e 100644 --- a/apps/wristlight/ChangeLog +++ b/apps/wristlight/ChangeLog @@ -1,2 +1,3 @@ 0.01: New app! 0.02: Minor code improvements +0.03: Added SOS blinking functionality with user-selected color and touch interaction to set the main screen color. Minor code improvements diff --git a/apps/wristlight/app.js b/apps/wristlight/app.js index a0e883c043..620bb44652 100644 --- a/apps/wristlight/app.js +++ b/apps/wristlight/app.js @@ -1,3 +1,6 @@ +var sosInterval; +var currentColor = "#ffffff"; // last selected color + function draw(color) { if (color == undefined) { color = -1; @@ -5,12 +8,10 @@ function draw(color) { g.clear(); g.setColor(color); g.fillRect(0, 0, g.getWidth(), g.getHeight()); -} -function draw2Pattern() { - const colors = ["ff0000", "8080ff", "00ff00", - "ffffff"]; - drawPattern(2, colors); + // Store the current color only if it is a valid user-selected color + // "#000000" is used internally when blinking (SOS off state) + if (typeof color === "string" && color !== "#000000") currentColor = color; } function draw3Pattern() { @@ -24,6 +25,8 @@ function drawPattern(size, colors) { g.clear(); var w = g.getWidth() / size; var h = g.getHeight() / size; + + // Draw selectable color blocks for (var i = 0; i < size; i++) { for (var j = 0; j < size; j++) { var color = colors[i*size + j]; @@ -31,15 +34,53 @@ function drawPattern(size, colors) { g.fillRect(j * w, i * h, j * w + w, i * h + h); } } + + // Touching a block sets the main screen color Bangle.on("touch", function(btn, xy) { + // If SOS mode is active, stop it when user interacts + if (sosInterval) { + clearInterval(sosInterval); + sosInterval = undefined; + } + var x = parseInt((xy.x) / w); var y = parseInt((xy.y) / h); draw("#" + colors[y * size + x]); }); } -// Clear the screen once, at startup -// draw immediately at first +// Start SOS blinking using the selected color +function startSOS() { + if (sosInterval) return; + var on = false; + sosInterval = setInterval(function() { + on = !on; + if (on) { + draw(currentColor); // active/on state + } else { + draw("#000000"); // off/black state + } + }, 200); // blinking frequency +} + +// Stop SOS blinking +function stopSOS() { + if (sosInterval) { + clearInterval(sosInterval); + sosInterval = undefined; + } +} + +// Toggle SOS mode using the hardware button +setWatch(function() { + if (sosInterval) { + stopSOS(); + } else { + startSOS(); + } +}, BTN1, {repeat:true, edge:"rising"}); + +// Show the color grid at startup draw3Pattern(); /* diff --git a/apps/wristlight/metadata.json b/apps/wristlight/metadata.json index a4970afed2..476c41ca60 100644 --- a/apps/wristlight/metadata.json +++ b/apps/wristlight/metadata.json @@ -2,7 +2,7 @@ "name": "Wrist Light", "shortName":"Wrist Light", "icon": "wristlight48.png", - "version": "0.02", + "version": "0.03", "description": "A flash light with different colors on your wrist", "tags": "flash,light", "allow_emulator":true,