Skip to content

Commit

Permalink
Keypad/Switch: debouncing
Browse files Browse the repository at this point in the history
Signed-off-by: Rick Waldron <[email protected]>
  • Loading branch information
rwaldron committed Apr 9, 2016
1 parent 62c9afc commit 8e1ea51
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 28 deletions.
33 changes: 14 additions & 19 deletions lib/keypad.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,6 @@ var aliases = {
hold: ["hold"]
};

var trigger = function(type, value) {
var event = {
type: type,
which: value,
timestamp: Date.now()
};
aliases[type].forEach(function(type) {
this.emit(type, event);
}, this);

this.emit("change", Object.assign({}, event));
};


function flatKeys(opts) {
var keys = [];

Expand Down Expand Up @@ -524,6 +510,20 @@ function Keypad(opts) {
holdtime: null,
};

var trigger = Fn.debounce(function(type, value) {
var event = {
type: type,
which: value,
timestamp: Date.now()
};
aliases[type].forEach(function(type) {
this.emit(type, event);
}, this);

this.emit("change", Object.assign({}, event));
}, 5);


if (opts.controller && typeof opts.controller === "string") {
controller = Controllers[opts.controller.toUpperCase()];
} else {
Expand Down Expand Up @@ -607,11 +607,6 @@ function Keypad(opts) {
return raw;
}
},
target: {
get: function() {
return state.keys[this.toIndices(this.value)];
}
}
});
}

Expand Down
4 changes: 2 additions & 2 deletions lib/switch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Board = require("./board");
var __ = require("./fn");
var Fn = require("./fn");
var Emitter = require("events").EventEmitter;
var util = require("util");

Expand Down Expand Up @@ -33,7 +33,7 @@ function Switch(opts) {
// Create a 5 ms debounce boundary on event triggers
// this avoids button events firing on
// press noise and false positives
var trigger = __.debounce(function(key) {
var trigger = Fn.debounce(function(key) {
aliases[key].forEach(function(type) {
this.emit(type, null);
}, this);
Expand Down
34 changes: 27 additions & 7 deletions test/keypad.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
var mocks = require("mock-firmata"),
MockFirmata = mocks.Firmata,
five = require("../lib/johnny-five.js"),
sinon = require("sinon"),
Board = five.Board,
Keypad = five.Keypad;
var mocks = require("mock-firmata");
var MockFirmata = mocks.Firmata;
var five = require("../lib/johnny-five.js");
var sinon = require("sinon");
var Board = five.Board;
var Fn = five.Fn;
var Keypad = five.Keypad;


var mpr121 = require("../lib/definitions/mpr121");
Expand All @@ -27,6 +28,9 @@ exports["Keypad: Analog"] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
this.board = newBoard();
this.debounce = this.sandbox.stub(Fn, "debounce", function(fn) {
return fn;
});
this.clock = this.sandbox.useFakeTimers();
this.analogRead = this.sandbox.spy(MockFirmata.prototype, "analogRead");
this.keypad = new Keypad({
Expand Down Expand Up @@ -249,15 +253,19 @@ exports["Keypad: Analog"] = {
test.equal(this, keypad);
test.done();
});

callback(403);
},

}
};

exports["Keypad: VKey"] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
this.board = newBoard();
this.debounce = this.sandbox.stub(Fn, "debounce", function(fn) {
return fn;
});
this.clock = this.sandbox.useFakeTimers();
this.analogRead = this.sandbox.spy(MockFirmata.prototype, "analogRead");
this.keypad = new Keypad({
Expand Down Expand Up @@ -572,6 +580,9 @@ exports["Keypad: MPR121"] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
this.board = newBoard();
this.debounce = this.sandbox.stub(Fn, "debounce", function(fn) {
return fn;
});
this.clock = this.sandbox.useFakeTimers();
this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");
this.i2cWrite = this.sandbox.spy(MockFirmata.prototype, "i2cWrite");
Expand Down Expand Up @@ -1028,6 +1039,9 @@ exports["Keypad: MPR121_KEYPAD"] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
this.board = newBoard();
this.debounce = this.sandbox.stub(Fn, "debounce", function(fn) {
return fn;
});
this.clock = this.sandbox.useFakeTimers();
this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");
this.i2cWrite = this.sandbox.spy(MockFirmata.prototype, "i2cWrite");
Expand Down Expand Up @@ -1318,6 +1332,9 @@ exports["Keypad: QTOUCH"] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
this.board = newBoard();
this.debounce = this.sandbox.stub(Fn, "debounce", function(fn) {
return fn;
});
this.clock = this.sandbox.useFakeTimers();
this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");
this.i2cRead = this.sandbox.spy(MockFirmata.prototype, "i2cRead");
Expand Down Expand Up @@ -1520,6 +1537,9 @@ exports["Keypad: 3X4_I2C_NANO_BACKPACK"] = {
setUp: function(done) {
this.sandbox = sinon.sandbox.create();
this.board = newBoard();
this.debounce = this.sandbox.stub(Fn, "debounce", function(fn) {
return fn;
});
this.clock = this.sandbox.useFakeTimers();
this.i2cConfig = this.sandbox.spy(MockFirmata.prototype, "i2cConfig");
this.i2cRead = this.sandbox.spy(MockFirmata.prototype, "i2cRead");
Expand Down

0 comments on commit 8e1ea51

Please sign in to comment.