Skip to content

Commit

Permalink
Ensure *-read-n handlers are removed when reporting is set to 0. Fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Apr 5, 2018
1 parent e1ca0a7 commit 4342eb0
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/firmata.js
Expand Up @@ -1654,6 +1654,9 @@ Board.prototype.getSamplingInterval = function() {
Board.prototype.reportAnalogPin = function(pin, value) {
/* istanbul ignore else */
if (value === 0 || value === 1) {
if (value === 0) {
this.removeAllListeners("analog-read-" + pin);
}
this.pins[this.analogPins[pin]].report = value;
writeToTransport(this, [REPORT_ANALOG | pin, value]);
}
Expand All @@ -1669,6 +1672,9 @@ Board.prototype.reportDigitalPin = function(pin, value) {
var port = pin >> 3;
/* istanbul ignore else */
if (value === 0 || value === 1) {
if (value === 0) {
this.removeAllListeners("digital-read-" + pin);
}
this.pins[pin].report = value;
writeToTransport(this, [REPORT_DIGITAL | port, value]);
}
Expand Down
30 changes: 30 additions & 0 deletions test/unit/firmata.test.js
Expand Up @@ -1238,6 +1238,21 @@ describe("Board: lifecycle", function() {
done();
});

it("must remove read handlers when reporting set to 0 for digital pin (INPUT)", function(done) {
board.on("removeListener", function(eventName) {
if (eventName === "digital-read-2") {
done();
}
});

board.digitalRead(2, function(value) {
assert.fail();
done();
});

board.reportDigitalPin(2, 0);
});

it("must be able to set pin mode on digital pin (INPUT)", function(done) {
board.pinMode(2, board.MODES.INPUT);
assert.equal(transport.lastWrite[0], PIN_MODE);
Expand Down Expand Up @@ -1337,6 +1352,21 @@ describe("Board: lifecycle", function() {
done();
});

it("must remove read handlers when reporting set to 0 for analog pin", function(done) {
board.on("removeListener", function(eventName) {
if (eventName === "analog-read-2") {
done();
}
});

board.analogRead(2, function(value) {
assert.fail();
done();
});

board.reportAnalogPin(2, 0);
});

it("must be able to read value of analog pin", function(done) {
var counter = 0;
var order = [1023, 0, 1023, 0];
Expand Down

0 comments on commit 4342eb0

Please sign in to comment.