Skip to content

Commit

Permalink
Continued work on movement sensor integration
Browse files Browse the repository at this point in the history
  • Loading branch information
thordy committed Sep 22, 2019
1 parent 8ff60da commit 15e6a18
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
22 changes: 19 additions & 3 deletions main.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
var debug = require('debug')('kcapp-smartboard:main');
var smartboard = require('./smartboard')("5cf8218da78e", 15);
var sensor = require('./movement-sensor')(40);

this.connected = false;
this.peripheral = {};

// TODO Get battery status
// TODO Add notification of board low power

/**
* Disconnect from the smartboard
* @param {object} data
*/
function disconnectListener(data) {
smartboard.disconnect(this.peripheral, () => {
debug("Disconnected");
Expand All @@ -29,8 +31,11 @@ function connectToMatch(data) {
smartboard.connect((peripheral) => {
this.connected = true;
this.peripheral = peripheral;

var lastBoardData = 0;
smartboard.initialize(peripheral,
(dart) => {
lastBoardData = Date.now();
var player = leg.currentPlayer;
if (dart.multiplier == 0) {
dart.multiplier = 1;
Expand All @@ -54,9 +59,20 @@ function connectToMatch(data) {
},
() => {
debug("Button pressed, sending visit");
lastBoardData = Date.now();
leg.emitVisit();
}
);

sensor.initialize(() => {
var last = Date.now() - lastBoardData;
debug(`Got movement. Last dart ${last}ms ago`);
if (last > 200) {
// TODO Send miss
debug("Movement sensor: Miss");
}
});

leg.on('leg_finished', (data) => {
debug(`Got leg_finished event!`);
var match = data.match;
Expand Down
24 changes: 19 additions & 5 deletions movement.js → movement-sensor.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,38 @@ var gpio = require('rpi-gpio');
*/
exports.initialize = (callback, bouncetime = 200) => {
this.callback = callback;
gpio.on('change', (pin, value) => {
var last = Date.now() - last;
var changeFunc = (pin, value) => {
var last = Date.now() - this.last_motion;
if (value && last > bouncetime) {
debug("Movement Detected!");
this.callback();
}
this.last_motion = Date.now();
});
debug(`Registering change callback on pin ${this.pin}`);
};
gpio.on('change', changeFunc);
this.changeFunc = changeFunc

debug('Registered movement callback');
gpio.setup(this.pin, gpio.DIR_IN, gpio.EDGE_RISING);
};

/**
* Remove the listener function
*/
exports.teardown = () => {
if (this.changeFunc) {
debug("Removing listener");
gpio.removeListener('çhange', this.changeFunc);
}
}

/**
* Configure the movement sensor module
* @param {int} pin - GPIO Pin of movement sensor (Board mode)
*/
module.exports = (pin) => {
this.pin = pin;
this.last_motion = Date.now();
this.last_motion = 0;
debug(`Movement Sensor configured on pin ${this.pin}`);
return this;
};

0 comments on commit 15e6a18

Please sign in to comment.