Skip to content

Commit

Permalink
chore: Upgrade SerialPort
Browse files Browse the repository at this point in the history
SerialPort v8 brings support for the latest versions of node and electron. The api has had two major breaking changes. `.list()` no longer takes a callback and only returns a promise. And the `PortInfo` object now has `path` instead of `comName`.

This would probably be a breaking change for Firmata as well.
  • Loading branch information
reconbot authored and rwaldron committed Dec 28, 2019
1 parent a38f464 commit 1ccfac8
Show file tree
Hide file tree
Showing 24 changed files with 231 additions and 254 deletions.
30 changes: 13 additions & 17 deletions packages/firmata-io/lib/firmata.js
Original file line number Diff line number Diff line change
Expand Up @@ -2427,15 +2427,15 @@ class Firmata extends Emitter {
}

/**
* Firmata.isAcceptablePort Determines if a `port` object (from SerialPort.list(...))
* Firmata.isAcceptablePort Determines if a `port` object (from SerialPort.list())
* is a valid Arduino (or similar) device.
* @return {Boolean} true if port can be connected to by Firmata
*/

static isAcceptablePort(port) {
let rport = /usb|acm|^com/i;

if (rport.test(port.comName)) {
if (rport.test(port.path)) {
return true;
}

Expand All @@ -2452,22 +2452,18 @@ class Firmata extends Emitter {
process.nextTick(() => {
callback(new Error("No Transport provided"), null);
});
} else {
Transport.list((error, ports) => {
const port = ports.find(port => Firmata.isAcceptablePort(port) && port);

/* istanbul ignore if */
if (error) {
callback(error, null);
} else {
if (port) {
callback(null, port);
} else {
callback(new Error("No Acceptable Port Found"), null);
}
}
});
return
}
Transport.list().then((ports) => {
const port = ports.find(port => Firmata.isAcceptablePort(port) && port);
if (port) {
callback(null, port);
} else {
callback(new Error("No Acceptable Port Found"), null);
}
}).catch(error => {
callback(error, null);
});
}

// Expose encode/decode for custom sysex messages
Expand Down
16 changes: 8 additions & 8 deletions packages/firmata-io/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Install Firmata-io:
npm install firmata-io --save
```

Install a Transport:
Install a Transport:


```sh
Expand All @@ -30,11 +30,11 @@ Here's an example using the `Serialport` class:

```js
// Require your Transport!
const Serialport = require("serialport");
// Pass the Transport class to the transport binding
const Serialport = require("serialport");
// Pass the Transport class to the transport binding
// function exported by firmata-io. The transport binding
// function will return the Firmata class object with
// the Transport class bound in its scope.
// the Transport class bound in its scope.
const Firmata = require("firmata-io")(Serialport);

Firmata.requestPort((error, port) => {
Expand All @@ -43,7 +43,7 @@ Firmata.requestPort((error, port) => {
return;
}

const board = new Firmata(port.comName);
const board = new Firmata(port.path);

board.on("close", () => {
// Unplug the board to see this event!
Expand All @@ -59,15 +59,15 @@ Here's an example using a `Serialport` instance:
```js
// Require your Transport!
const Serialport = require("serialport");
// Get the Firmata class without a bound transport.
// Get the Firmata class without a bound transport.
const Firmata = require("firmata-io").Firmata;

Serialport.list().then(ports => {
// Figure which port to use...
const port = ports.find(port => port.manufacturer.startsWith("Arduino"));

// Instantiate an instance of your Transport class
const transport = new Serialport(port.comName);
const transport = new Serialport(port.path);

// Pass the new instance directly to the Firmata class
const board = new Firmata(transport);
Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/adxl345.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Board.requestPort((error, port) => {
READ: 0xB2,
};

const board = new Board(port.comName);
const board = new Board(port.path);

board.on("ready", function() {
console.log("Ready");
Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/blink.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Board.requestPort((error, port) => {
return;
}

const board = new Board(port.comName);
const board = new Board(port.path);

board.on("ready", () => {
const pin = 13;
Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/close-events.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Board.requestPort((error, port) => {
return;
}

const board = new Board(port.comName);
const board = new Board(port.path);

board.on("close", () => {
// Unplug the board to see this event!
Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/hw-serial-read-gps.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Board.requestPort((error, port) => {
return;
}

const board = new Board(port.comName);
const board = new Board(port.path);

board.on("ready", () => {
console.log("READY");
Expand Down
6 changes: 3 additions & 3 deletions packages/firmata.js/examples/johnny-five-io-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ const SerialPort = require("serialport");
const five = require("johnny-five");
const Firmata = require("../");

SerialPort.list((error, list) => {
const device = list.reduce((accum, item) => {
SerialPort.list().then(ports => {
const device = ports.reduce((accum, item) => {
if (item.manufacturer.indexOf("Arduino") === 0) {
return item;
}
Expand All @@ -17,7 +17,7 @@ SerialPort.list((error, list) => {
*/

const board = new five.Board({
io: new Firmata(device.comName)
io: new Firmata(device.path)
});

board.on("ready", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/k22.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Board.requestPort((error, port) => {
console.log(error);
return;
}
const board = new Board(port.comName);
const board = new Board(port.path);

board.on("ready", () => {
const k22 = 0x68;
Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/mma8452.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Board.requestPort((error, port) => {
READ_X_MSB: 0x01,
};

const board = new Board(port.comName);
const board = new Board(port.path);
// var board = new Board("/dev/cu.usbmodem1411");

board.on("ready", function() {
Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/reporting.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Board.requestPort((error, port) => {
console.log(error);
return;
}
const board = new Board(port.comName);
const board = new Board(port.path);

board.on("ready", function() {
const a = 6;
Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/servosweep.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Board.requestPort((error, port) => {
console.log(error);
return;
}
const board = new Board(port.comName);
const board = new Board(port.path);

board.on("ready", () => {
let degrees = 10;
Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/stepper-accel.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Board.requestPort((error, port) => {
return;
}

const board = new Board(port.comName);
const board = new Board(port.path);

board.on("ready", () => {

Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/stepper-driver.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Board.requestPort((error, port) => {
return;
}

const board = new Board(port.comName);
const board = new Board(port.path);

board.on("ready", () => {

Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/stepper-multi.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Board.requestPort((error, port) => {
return;
}

const board = new Board(port.comName);
const board = new Board(port.path);

board.on("ready", () => {

Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/stepper-three-wire.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Board.requestPort(function(error, port) {
return;
}

var board = new Board(port.comName);
var board = new Board(port.path);

board.on("ready", function() {

Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/sw-serial-read-gps.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Board.requestPort((error, port) => {
console.log(error);
return;
}
const board = new Board(port.comName);
const board = new Board(port.path);

board.on("ready", () => {
console.log("READY");
Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/test-analog-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Board.requestPort((error, port) => {
return;
}

const board = new Board(port.comName);
const board = new Board(port.path);

console.log(__filename);
console.log("------------------------------");
Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/test-i2c-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Board.requestPort((error, port) => {
return;
}

const board = new Board(port.comName);
const board = new Board(port.path);

console.log(__filename);
console.log("------------------------------");
Expand Down
2 changes: 1 addition & 1 deletion packages/firmata.js/examples/test-serial-read.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ Board.requestPort((error, port) => {
return;
}

const board = new Board(port.comName);
const board = new Board(port.path);

console.log(__filename);
console.log("------------------------------");
Expand Down
Loading

0 comments on commit 1ccfac8

Please sign in to comment.