Skip to content

Instantly share code, notes, and snippets.

@natcl
Last active September 10, 2019 01:40
Show Gist options
  • Save natcl/17e57da27cb6495a9e24eba092601c6d to your computer and use it in GitHub Desktop.
Save natcl/17e57da27cb6495a9e24eba092601c6d to your computer and use it in GitHub Desktop.
Puck.js NFC
var buttonState = false;
var flashing = false;
setWatch(function () {
if (flashing) return;
var batteryPercentage = Puck.getBatteryPercentage();
buttonState = buttonState ? false : true;
if (buttonState) {
flashLed(LED2, 200, 1);
NRF.nfcURL("http://cosmiquecarrousel.com");
} else {
flashLed(LED1, 200, Math.round(batteryPercentage/10));
NRF.nfcStop();
}
}, BTN, { edge: "rising", repeat: 1, debounce: 20 });
function flashLed(led, delay, times) {
flashing = true;
var t = times ? times : 1;
var counter = 0;
var flashInterval = setInterval(function() {
if (counter % 2) {
led.reset();
} else {
led.set();
}
counter++;
if (counter >= t*2) {
clearInterval(flashInterval);
counter = 0;
flashing = false;
}
}, delay);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment