diff --git a/lib/firmata.js b/lib/firmata.js index 5a00ba0..0b4b250 100644 --- a/lib/firmata.js +++ b/lib/firmata.js @@ -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]); } @@ -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]); } diff --git a/test/unit/firmata.test.js b/test/unit/firmata.test.js index d29c44c..976c3a7 100644 --- a/test/unit/firmata.test.js +++ b/test/unit/firmata.test.js @@ -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); @@ -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];