Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/wristlight/ChangeLog
Original file line number Diff line number Diff line change
@@ -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
55 changes: 48 additions & 7 deletions apps/wristlight/app.js
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
var sosInterval;
var currentColor = "#ffffff"; // last selected color

function draw(color) {
if (color == undefined) {
color = -1;
}
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() {
Expand All @@ -24,22 +25,62 @@ 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];
g.setColor("#" + color);
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();

/*
Expand Down
2 changes: 1 addition & 1 deletion apps/wristlight/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down