Skip to content

Commit

Permalink
packages/firmata-io: use Symbols instead of underscore prefixed prope…
Browse files Browse the repository at this point in the history
…rties
  • Loading branch information
rwaldron committed Jan 15, 2019
1 parent 69a21e8 commit 44ac991
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 14 deletions.
37 changes: 25 additions & 12 deletions packages/firmata-io/lib/firmata.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,10 @@ const SYSTEM_RESET = 0xFF;

const MAX_PIN_COUNT = 128;


const sendOneWireSearch = Symbol("sendOneWireSearch");
const sendOneWireRequest = Symbol("sendOneWireRequest");

/**
* MIDI_RESPONSE contains functions to be called when we receive a MIDI message from the arduino.
* used as a switch object as seen here http://james.padolsey.com/javascript/how-to-avoid-switch-case-syndrome/
Expand Down Expand Up @@ -1482,7 +1486,7 @@ class Firmata extends Emitter {
*/

sendOneWireSearch(pin, callback) {
this._sendOneWireSearch(
this[sendOneWireSearch](
ONEWIRE_SEARCH_REQUEST,
`1-wire-search-reply-${pin}`,
pin,
Expand All @@ -1498,15 +1502,15 @@ class Firmata extends Emitter {
*/

sendOneWireAlarmsSearch(pin, callback) {
this._sendOneWireSearch(
this[sendOneWireSearch](
ONEWIRE_SEARCH_ALARMS_REQUEST,
`1-wire-search-alarms-reply-${pin}`,
pin,
callback
);
}

_sendOneWireSearch(type, event, pin, callback) {
[sendOneWireSearch](type, event, pin, callback) {
writeToTransport(this, [
START_SYSEX,
ONEWIRE_DATA,
Expand Down Expand Up @@ -1542,7 +1546,7 @@ class Firmata extends Emitter {
/* istanbul ignore next */
callback(new Error("1-Wire device read timeout - are you running ConfigurableFirmata?"));
}, 5000);
this._sendOneWireRequest(
this[sendOneWireRequest](
pin,
ONEWIRE_READ_REQUEST_BIT,
device,
Expand All @@ -1564,7 +1568,7 @@ class Firmata extends Emitter {
*/

sendOneWireReset(pin) {
this._sendOneWireRequest(
this[sendOneWireRequest](
pin,
ONEWIRE_RESET_REQUEST_BIT
);
Expand All @@ -1581,7 +1585,7 @@ class Firmata extends Emitter {
*/

sendOneWireWrite(pin, device, data) {
this._sendOneWireRequest(
this[sendOneWireRequest](
pin,
ONEWIRE_WRITE_REQUEST_BIT,
device,
Expand All @@ -1599,7 +1603,7 @@ class Firmata extends Emitter {
*/

sendOneWireDelay(pin, delay) {
this._sendOneWireRequest(
this[sendOneWireRequest](
pin,
ONEWIRE_DELAY_REQUEST_BIT,
null,
Expand Down Expand Up @@ -1628,7 +1632,7 @@ class Firmata extends Emitter {
/* istanbul ignore next */
callback(new Error("1-Wire device read timeout - are you running ConfigurableFirmata?"));
}, 5000);
this._sendOneWireRequest(
this[sendOneWireRequest](
pin,
ONEWIRE_WRITE_REQUEST_BIT | ONEWIRE_READ_REQUEST_BIT,
device,
Expand All @@ -1645,7 +1649,7 @@ class Firmata extends Emitter {
}

// see http://firmata.org/wiki/Proposals#OneWire_Proposal
_sendOneWireRequest(pin, subcommand, device, numBytesToRead, correlationId, delay, dataToWrite, event, callback) {
[sendOneWireRequest](pin, subcommand, device, numBytesToRead, correlationId, delay, dataToWrite, event, callback) {
const bytes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];

if (device || numBytesToRead || correlationId || delay || dataToWrite) {
Expand Down Expand Up @@ -2453,10 +2457,14 @@ class Firmata extends Emitter {
Transport.list((error, ports) => {
const port = ports.find(port => Firmata.isAcceptablePort(port) && port);

if (port) {
callback(null, port);
if (error) {
callback(error, null);
} else {
callback(new Error("No Acceptable Port Found"), null);
if (port) {
callback(null, port);
} else {
callback(new Error("No Acceptable Port Found"), null);
}
}
});
}
Expand Down Expand Up @@ -2660,6 +2668,11 @@ if (process.env.IS_TEST_MODE) {
encodeCustomFloat,
decodeCustomFloat,
writeToTransport,

symbols: {
sendOneWireRequest,
sendOneWireSearch,
}
};
}

Expand Down
4 changes: 2 additions & 2 deletions packages/firmata.js/test/unit/firmata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2480,10 +2480,10 @@ describe("Board: lifecycle", function() {
transport.emit("data", [START_SYSEX, PULSE_OUT, ONEWIRE_SEARCH_ALARMS_REPLY, ONEWIRE_RESET_REQUEST_BIT, 0x28, 0x36, 0x3F, 0x0F, 0x52, 0x00, 0x00, 0x00, 0x5D, 0x00, END_SYSEX]);
});
it("must be able to send a 1-wire write read", done => {
sandbox.spy(board, "_sendOneWireRequest");
sandbox.spy(board, Board.test.symbols.sendOneWireRequest);
board.sendOneWireRead(1, 1, 1, () => {});

board._sendOneWireRequest.lastCall.args[8]();
board[Board.test.symbols.sendOneWireRequest].lastCall.args[8]();
done();
});
it("must be able to send a 1-wire reset request", done => {
Expand Down

0 comments on commit 44ac991

Please sign in to comment.