infuseureuse.js/puck.js
pierre a23bb8aca2 Actualiser puck.js
full standalone ux
2024-12-25 16:31:45 +01:00

180 lines
No EOL
5.3 KiB
JavaScript

var count = 0;
function ledsAnim(level) {
var BLACK = 0;
var RED = 1;
var GREEN = 2;
var BLUE = 4;
var WHITE = RED + GREEN + BLUE;
var YELLOW = RED + GREEN;
var CYAN = BLUE + GREEN;
var MAGENTA = BLUE + RED;
switch(level) {
case "stop":
digitalWrite([LED3,LED2,LED1],RED);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],BLACK);}, 5);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],RED);}, 200);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],BLACK);}, 205);
break;
case "wakeup":
digitalWrite([LED3,LED2,LED1],WHITE);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],BLACK);}, 5);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],WHITE);}, 200);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],BLACK);}, 205);
break;
case "add":
digitalWrite([LED3,LED2,LED1],WHITE);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],BLACK);}, 5);
break;
case "ring":
digitalWrite([LED3,LED2,LED1],RED);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],BLACK);}, 5);
break;
case "cannot_remove":
digitalWrite([LED3,LED2,LED1],RED);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],BLACK);}, 5);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],RED);}, 200);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],BLACK);}, 205);
break;
case "remove":
digitalWrite([LED3,LED2,LED1],WHITE);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],BLACK);}, 5);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],WHITE);}, 200);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],BLACK);}, 205);
break;
case "start":
digitalWrite([LED3,LED2,LED1],WHITE);
setTimeout(function(){digitalWrite([LED3,LED2,LED1],BLACK);}, 200);
break;
}
}
var fsmState = "reset";
var fsmCount = 0;
var fsmIv = null;
var fsmTm = null;
function fsmPressed() {
switch (fsmState) {
case "setting":
ledsAnim("add");
clearTimeout(fsmTm);
fsmTm = setInterval(function() {fsmTick();}, 5000);
fsmCount += 30;
broadcast(["Set to ", Math.floor(fsmCount / 60).toString().padStart(2, "0"), "'", (fsmCount % 60).toString().padStart(2, "0"),"\""].join(''));
break;
}
}
function fsmLongPressed() {
switch (fsmState) {
case "reset":
ledsAnim("wakeup");
fsmTm = setTimeout(function() {fsmTick();}, 5000);
fsmState = "setting";
fsmCount = 30;
broadcast(["Set to ", Math.floor(fsmCount / 60).toString().padStart(2, "0"), "'", (fsmCount % 60).toString().padStart(2, "0"),"\""].join(''));
break;
case "setting":
clearTimeout(fsmTm);
fsmTm = setInterval(function() {fsmTick();}, 5000);
if (fsmCount > 30) {
fsmCount -= 30;
ledsAnim("remove");
} else {
ledsAnim("cannot_remove");
}
broadcast(["Set to ", Math.floor(fsmCount / 60).toString().padStart(2, "0"), "'", (fsmCount % 60).toString().padStart(2, "0"),"\""].join(''));
break;
case "countdown":
ledsAnim("stop");
broadcast(null);
clearInterval(fsmIv);
fsmState = "reset";
break;
case "ring":
ledsAnim("stop");
broadcast(null);
clearInterval(fsmIv);
fsmState = "reset";
break;
}
}
function fsmTick() {
switch(fsmState) {
case "setting":
ledsAnim("start");
clearTimeout(fsmTm);
fsmTm = null;
fsmState = "countdown";
fsmIv = setInterval(function() {fsmTick();}, 1000);
break;
case "countdown":
fsmCount--;
var minutes = Math.floor(fsmCount / 60);
var minutes_tens = Math.floor(minutes / 10);
var minutes_units = minutes % 10;
var seconds = fsmCount % 60;
var seconds_tens = Math.floor(seconds / 10);
var seconds_units = seconds % 10;
var msg = [minutes_tens.toString(),minutes_units.toString(),":",seconds_tens.toString(),seconds_units.toString()].join('');
broadcast(msg);
if (!fsmCount) {
fsmState = "ring";
}
break;
case "ring":
ledsAnim("ring");
if (bcstMsg.charAt(0) == " ") {
broadcast("Time's up!");
} else {
broadcast(" Time's up!");
}
break;
}
}
var timoutObj = null;
var LONG_PRESS_MILLIS = 300;
function btnPressed() {
timoutObj = setTimeout(function(){
timoutObj = null;
fsmLongPressed();
}, LONG_PRESS_MILLIS);
}
function btnReleased() {
if (timoutObj) {
clearTimeout(timoutObj);
timoutObj = null;
fsmPressed();
}
}
setWatch(btnPressed, BTN, {edge:"rising", repeat:true,debounce:50});
setWatch(btnReleased, BTN, {edge:"falling",repeat:true,debounce:50});
var bcstIv = null;
var bcstMsg = null;
function broadcast(message) {
if (!message) {
if (bcstIv) {
clearInterval(bcstIv);
bcstIv = null;
NRF.setAdvertising({});
}
} else {
message = message.padEnd(16, " ");
if (!bcstIv) {
bcstIv = setInterval(function() {
require("ble_eddystone_uid").advertise(
[bcstMsg[0], bcstMsg[1], bcstMsg[2], bcstMsg[3], bcstMsg[4], bcstMsg[5], bcstMsg[6], bcstMsg[7], bcstMsg[8], bcstMsg[9], // namespace
bcstMsg[10], bcstMsg[11], bcstMsg[12], bcstMsg[13], bcstMsg[14], bcstMsg[15]]); // instance
}, 250);
}
bcstMsg = message;
}
}