-
-
Save oesterle/4328c0b47d82b2ce0f0738b5fa1c5edb to your computer and use it in GitHub Desktop.
PowerPoint presenter using a Puck.js beacon's button and magnetic sensor.
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
var kb = require("ble_hid_keyboard"); | |
var next = "n"; | |
var prev = "p"; | |
var magIsOn = false; | |
var magTimeoutID = null; | |
// Restart BLE when Puck is powered on, and/or code is saved to flash | |
// And give the device a catchy name. :-) | |
E.on('init', function(){ | |
NRF.setServices(undefined, { hid : kb.report }); | |
NRF.setAdvertising({},{name:"Attila's PPP"}); | |
magIsOn = false; | |
magTimeoutID = null; | |
}); | |
function sendPrev(){ | |
sendChar(prev); | |
LED2.set(); | |
setTimeout("LED2.reset()",1000); | |
setMagTimeout(); | |
} | |
// We can't query if magnetometer is on, so we | |
// keep track of this in magIsOn variable | |
function sendNext(){ | |
if (!magIsOn){ | |
Puck.magOn(); | |
magIsOn = true; | |
} | |
sendChar(next); | |
LED2.set(); | |
setTimeout("LED2.reset()",1000); | |
setMagTimeout(); | |
} | |
function sendChar(chr){ | |
var sk = 0x02; | |
if (chr == chr.toLowerCase()){ | |
sk = 0; | |
} | |
// send the keyboard character | |
kb.tap(kb.KEY[chr.toUpperCase()], sk); | |
} | |
// watch the button press, action happens on button release (rising) | |
// debounce set to 50 to prevent unintended mechanical "bounce" triggers | |
setWatch(function(e) { | |
sendNext(); // fire the "n" character | |
}, BTN, {edge:"rising", debounce:50, repeat:true}); | |
// trigger the "p" character when the x magnetic axis value is over 0 | |
Puck.on('mag', function(value) { | |
if(value.x > 0){ | |
sendPrev(); // fire the "p" character | |
} | |
}); | |
// Turn off magnetic sensor after 30 minutes to save power. | |
// Each button press or magnetic gesture clears and restarts the timer. | |
// That way, we won't lose functionality if user is still interacting. | |
function setMagTimeout() { | |
if (magTimeoutID){ | |
try{ | |
clearTimeout(magTimeoutID); | |
} catch(err) { | |
// timeout already cleared; ignore error | |
} | |
} | |
magTimeoutID = setTimeout(function(){ | |
Puck.magOff(); | |
magIsOn = false; | |
}, 1800000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Changes: