Skip to content

Instantly share code, notes, and snippets.

@adambutler
Last active June 12, 2017 18:44
Show Gist options
  • Save adambutler/eafb26405463bd2449e3f63009069f1e to your computer and use it in GitHub Desktop.
Save adambutler/eafb26405463bd2449e3f63009069f1e to your computer and use it in GitHub Desktop.
var ble = require("ble_eddystone");
const urls = [
{ url: "https://goo.gl/j9dDcH", led: LED2 },
{ url: "https://goo.gl/kkpdbq", led: LED3 },
];
let currentUrl = undefined;
let downTime = undefined;
let allowPush = true;
function setLed(led, duration, callback) {
if (duration === undefined) {
duration = 1500;
}
digitalWrite(led, true);
setTimeout(function(){
digitalWrite(led, false);
if (callback) {
callback();
}
}, duration);
}
function checkBattery() {
if(Puck.getBatteryPercentage() <= 20) {
alert(LED3);
} else {
allowPush = true;
}
}
function toggleBeacon() {
if (currentUrl >= 0) {
currentUrl++;
} else {
currentUrl = 0;
}
if (urls[currentUrl]) {
ble.advertise(urls[currentUrl].url);
setLed(urls[currentUrl].led, undefined, afterToggleBeacon);
} else {
currentUrl = undefined;
NRF.setAdvertising({});
alert(LED1, 500, 1000);
}
}
function afterToggleBeacon() {
checkBattery();
}
function alert(led, onDuration, gap) {
if (onDuration === undefined) {
onDuration = 50;
}
if (gap === undefined) {
gap = 100;
}
setLed(led, onDuration);
setTimeout(function(){ setLed(led, onDuration); }, gap);
setTimeout(function(){ setLed(led, onDuration); allowPush = true; }, gap * 2);
}
function onUp() {
const delta = Date().ms - downTime;
if (delta > 750 && delta < 1500) {
toggleBeacon();
} else {
alert(LED1);
}
}
function onDown() {
if (allowPush) {
allowPush = false;
downTime = Date().ms;
setWatch(onUp, BTN, { edge: 'falling', debounce: 50 });
}
}
setWatch(onDown, BTN, { repeat: true, edge: 'rising', debounce: 50 });
save();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment