Skip to content

Instantly share code, notes, and snippets.

@oesterle
Forked from attilavago/puck_pp_presenter.js
Last active September 5, 2019 12:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oesterle/4328c0b47d82b2ce0f0738b5fa1c5edb to your computer and use it in GitHub Desktop.
Save oesterle/4328c0b47d82b2ce0f0738b5fa1c5edb to your computer and use it in GitHub Desktop.
PowerPoint presenter using a Puck.js beacon's button and magnetic sensor.
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);
}
@oesterle
Copy link
Author

Changes:

  • Ensure BLE is turned on when Puck is powered on or battery is replaced.
  • Ensure mag turning off doesn’t happen in the middle of presenting.
  • Puck.magOn() doesn’t return a boolean to check if on/off; added code to
  • fix this.
  • Simplified character sending code to remove duplicate code.
  • Added catchy keyboard Bluetooth device name. :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment