Last active
September 10, 2019 01:40
-
-
Save natcl/17e57da27cb6495a9e24eba092601c6d to your computer and use it in GitHub Desktop.
Puck.js NFC
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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