Last active
September 7, 2021 17:28
-
-
Save jptrsn/d6cb9b9cdbcd41f3500708f8b694cad2 to your computer and use it in GitHub Desktop.
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 lightSwitch = 0; | |
var persist = 30000; | |
var beaconService = require("ble_ibeacon"); | |
var timerId = null; | |
var flasherId = null; | |
const blinkPattern = [100, 150, 100, 150]; | |
function beaconData() { | |
return { | |
uuid : [0xb1, 0xae, 0x51, 0x2b, 0x97, 0x03, 0x4a, 0xe2, 0xb9, 0xc3, 0x9e, 0x26, 0x0c, 0xa7, 0xb3, 0x99], | |
major : lightSwitch ? 0x0001 : 0x0000, | |
minor : 0x0000, | |
rssi : -67 | |
}; | |
} | |
function startLedFlashing() { | |
flasherId = setInterval(function() { | |
digitalPulse(4,0,blinkPattern); | |
}, 1000); | |
digitalPulse(4,0,blinkPattern); | |
} | |
function switchOn() { | |
console.log('switchOn'); | |
if (timerId) { | |
clearTimeout(timerId); | |
console.log('switchOn cleared timeout'); | |
return switchOff(); | |
} | |
startLedFlashing(); | |
lightSwitch = 1; | |
setAdvertising(); | |
timerId = setTimeout(switchOff, persist); | |
} | |
function switchOff() { | |
console.log('switchOff'); | |
lightSwitch = 0; | |
timerId = null; | |
if (flasherId) { | |
clearInterval(flasherId); | |
flasherId = null; | |
} | |
digitalPulse(3,1,500); | |
setAdvertising(); | |
} | |
setWatch(function() { | |
try { | |
console.log('timerId',timerId); | |
switchOn(); | |
} catch(e) { | |
console.log(e); | |
digitalPulse(1,1,500); | |
} | |
}, BTN, {edge:"rising", repeat:1, debounce:50}); | |
function setAdvertising() { | |
NRF.setAdvertising([beaconService.get(beaconData())], {name: 'PuckJS', interval: 100}); | |
} | |
setAdvertising(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment