Skip to content

Instantly share code, notes, and snippets.

@natcl
Created November 6, 2019 03:37
Show Gist options
  • Save natcl/51eee081762b16ab1e79bb1a0cb7439f to your computer and use it in GitHub Desktop.
Save natcl/51eee081762b16ab1e79bb1a0cb7439f to your computer and use it in GitHub Desktop.
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");
NRF.setAdvertising([
require("ble_eddystone_uid").get([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // namespace
[0x00, 0x00, 0x00, 0x00, 0x00, 0x00]), // instance
{} // this will add a 'normal' advertising packet showing name/etc
], {interval:20});
} else {
flashLed(LED1, 200, Math.round(batteryPercentage/10));
NRF.nfcStop();
NRF.setAdvertising([
require("ble_eddystone_uid").get([0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00], // namespace
[0x01, 0x00, 0x00, 0x00, 0x00, 0x00]), // instance
{} // this will add a 'normal' advertising packet showing name/etc
], {interval:10000});
}
}, 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