Skip to content

Instantly share code, notes, and snippets.

@Narvey
Last active January 31, 2021 01:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Narvey/8594a392b4c91960b74656ec4be84344 to your computer and use it in GitHub Desktop.
Save Narvey/8594a392b4c91960b74656ec4be84344 to your computer and use it in GitHub Desktop.
SmartWatch script (Javascript for running on Espruino Pico)
var modeBtn = BTN1; //set the pin here used for mode button.
var actBtn = A2;
var flashDelay = 370; //milliman
pinMode(modeBtn, "input_pulldown");
pinMode(actBtn, "input_pulldown");
function blink(times, led) {
times = +times;
var count = 0;
var ivF = setInterval(function () {
led.write(true);
//then turn back off in 40 ms:
setTimeout(function () {
led.write(false);
}, 40);
count++;
if (count >= times) clearInterval(ivF);
}, flashDelay);
}
function timeBlink() {
var offSetMS = -7 * 60 * 60 * 1000;
var today = new Date(Date.now() + offSetMS);
print(today);
var hrs = today.getHours();
var min = today.getMinutes() / 5;
blink(hrs > 12 ? hrs - 12 : hrs, LED1);
blink(min, LED2);
}
function timer() {
min = 0;
while (digitalRead(actBtn)) {
min = (typeof min == "number" ? min : 0) + 1;
digitalPulse(LED2, true, 50);
setTimeout(function () {}, 1000 * 0.5);
}
if (min <= 1) {
LED1.write(false);
LED2.write(false);
} else
setTimeout(function () {
LED1.write(true);
LED2.write(true);
}, min * 60 * 1000.0);
}
///Modes
var modes = [
{ num: 0, name: "timeBlink", routine: timeBlink },
{ num: 1, name: "timer", routine: timer },
//{num: 2, name: "alarm", routine: ()=>blink(3,LED2)},
];
var currentMode = 0;
setWatch(
() => {
currentMode = currentMode >= modes.length - 1 ? 0 : currentMode + 1;
},
modeBtn,
{ repeat: true, edge: "rising", debounce: 10 }
);
setWatch(
() => {
modes[currentMode].routine();
},
actBtn,
{ repeat: true, edge: "rising", debounce: 10 }
);
E.setConsole("Serial1");//Use headphone for shell, but don't force it to stay there.
blink(1, LED2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment