Created
October 12, 2022 16:29
-
-
Save myoung34/4a957bea3eaa8becf6563dc41d4b9ac4 to your computer and use it in GitHub Desktop.
puck.js iBeacon button
This file contains hidden or 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
function doAdvertising() { | |
console.log("starting advertise"); | |
require("ble_ibeacon").advertise({ | |
uuid : ['a', 2, 3, 4, 'b', 6, 'c', 3, 0, 'd', 8, 'e', 7, 'f', 3, 'g'], // ibeacon uuid | |
major : 0x0001, // optional | |
minor : 0x0001, // optional | |
rssi : -59 // optional RSSI at 1 meter distance in dBm | |
}); | |
console.log("started"); | |
} | |
// Upon button press, we do something. All other times, we sleep, This is an interrupt. | |
// Note in sleep, NRF is awake and advertising, but JS is not executing, until the button interrupts sleep. | |
// In sleep and not advertising, 12uA power is used. | |
// In sleep and advertising at 375ms (0dBm), 25uA power is used. | |
setWatch(function() { | |
digitalPulse(LED1,1,100); | |
digitalPulse(LED3,1,100); // Blink Purple when the button is pressed | |
setTimeout(function () { // Then, show our battery status as a red through to green for low to high. | |
batteryLED(); | |
}, 500); | |
setTimeout(function () { | |
toggleAdvertising(); // Finally, toggle eddystone advertising On/Off - blue blink for adv on, red for adv off | |
}, 500); | |
}, BTN, {edge:"rising", debounce:50, repeat:true}); | |
function blinkRed(time) { | |
digitalPulse(LED1,1,time); | |
} | |
function blinkGreen(time) { | |
digitalPulse(LED2,1,time); | |
} | |
function blinkBlue(time) { | |
digitalPulse(LED3,1,time); | |
} | |
function blinkYellow(time) { // It seems to multiplex these - even though espruino is single-threaded | |
digitalPulse(LED1,1,time); | |
digitalPulse(LED2,1,time); | |
} | |
// This function blinks the LEDs depending on battery status. | |
// Puck.getBatteryPercentage is the nicer way to check your battery status | |
// For the curious: NRF.getBattery() will give you raw voltage read from the NRF IC. | |
function batteryLED(){ | |
blvl=Puck.getBatteryPercentage(); | |
if (blvl >= 75) { // If battery is above 75%, blink green | |
blinkGreen(100); | |
} | |
if (blvl < 75 && blvl > 25 ) { // If battery is lower than 75%, blink yellow | |
blinkYellow(100); | |
} | |
if (blvl < 25) { // If battery is lower than 25%, blink red! | |
blinkRed(100); | |
} | |
} | |
var sleep = function (miliseconds) { | |
var currentTime = new Date().getTime(); | |
while (currentTime + miliseconds >= new Date().getTime()) { | |
} | |
}; | |
//This function toggles whether advertising is on or off. | |
function toggleAdvertising() { | |
// Enable Advertising, blink blue LED to reflect this. | |
setTimeout(function () { | |
blinkBlue(500); | |
doAdvertising(); //Call doAdvertising function at top | |
sleep(500); | |
blinkRed(500); | |
console.log("stopping"); | |
NRF.setAdvertising({}); //Cancel all advertising. | |
console.log("stopped"); | |
}, 400); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment