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 "adjust": digitalWrite([LED3,LED2,LED1],WHITE); setTimeout(function(){digitalWrite([LED3,LED2,LED1],BLACK);}, 5); break; case "start": 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; } } var fsmState = "reset"; var fsmCount = 0; var fsmIv = null; var fsmTm = null; function fsmPressed() { switch (fsmState) { case "setting": ledsAnim("adjust"); clearTimeout(fsmTm); fsmTm = setInterval(function() {fsmTick();}, 5000); fsmCount += 30; broadcast(["Set to ", fsmCount.toString(),"s"].join('')); break; } } function fsmLongPressed() { switch (fsmState) { case "reset": ledsAnim("wakeup"); fsmTm = setTimeout(function() {fsmTick();}, 5000); fsmState = "setting"; fsmCount = 30; broadcast(["Set to ", fsmCount.toString(),"s"].join('')); break; case "countdown": 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) { broadcast(null); clearInterval(fsmIv); fsmState = "reset"; } 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; } }