From e6501112210d8fb84f075a3df8d6db4ca4d093ef Mon Sep 17 00:00:00 2001 From: Erin Peach Date: Wed, 11 Dec 2019 17:04:17 -0800 Subject: [PATCH 1/3] Allow serialport to be overridden --- client/MBFirmataClient.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/client/MBFirmataClient.js b/client/MBFirmataClient.js index 8c20668..f038d02 100644 --- a/client/MBFirmataClient.js +++ b/client/MBFirmataClient.js @@ -32,11 +32,12 @@ SOFTWARE. * Clients can listen for DAL events, digital pin changes, or analog channel updates. */ -const serialport = require('serialport'); +let serialport; const {TextEncoder, TextDecoder} = require('util'); class MicrobitFirmataClient { - constructor() { + constructor(port) { + serialport = port ? port : require('serialport'); this.addConstants(); this.myPort = null; this.inbuf = new Uint8Array(1000); From f8dffd61aa5d3fe918559727e41b47f1572c68f8 Mon Sep 17 00:00:00 2001 From: Erin Peach Date: Wed, 11 Dec 2019 17:07:24 -0800 Subject: [PATCH 2/3] Allow the TextEncoder, TextDecoder to work in node and browser --- client/MBFirmataClient.js | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/client/MBFirmataClient.js b/client/MBFirmataClient.js index f038d02..7ab9a30 100644 --- a/client/MBFirmataClient.js +++ b/client/MBFirmataClient.js @@ -33,7 +33,16 @@ SOFTWARE. */ let serialport; -const {TextEncoder, TextDecoder} = require('util'); +let TextEncoder, TextDecoder; + +// If running outside of the browser, create the TextEncoder/TextDecoder +if (typeof window === 'undefined' || !window.TextEncoder) { + TextEncoder = require('util').TextEncoder; + TextDecoder = require('util').TextDecoder; +} else { + TextEncoder = window.TextEncoder; + TextDecoder = window.TextDecoder; +} class MicrobitFirmataClient { constructor(port) { From 119d2e149620c76da6af3033222cbd9e600baba5 Mon Sep 17 00:00:00 2001 From: Erin Peach Date: Wed, 11 Dec 2019 17:14:51 -0800 Subject: [PATCH 3/3] Connecting to port return promises --- client/MBFirmataClient.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/client/MBFirmataClient.js b/client/MBFirmataClient.js index 7ab9a30..44faef9 100644 --- a/client/MBFirmataClient.js +++ b/client/MBFirmataClient.js @@ -119,10 +119,13 @@ class MicrobitFirmataClient { // Connecting/Disconnecting + /** + * @returns {Promise} when port has been opened or if no port found + */ connect() { // Search serial port list for a connected micro:bit and, if found, open that port. - serialport.list() + return serialport.list() .then((ports) => { for (var i = 0; i < ports.length; i++) { var p = ports[i]; @@ -137,13 +140,17 @@ class MicrobitFirmataClient { // Attempt to open the serial port on the given port name. // If this fails it will fail with an UnhandledPromiseRejectionWarning. console.log("Opening", portName); - this.setSerialPort(new serialport(portName, { baudRate: 57600 })); + return this.setSerialPort(new serialport(portName, { baudRate: 57600 })); } else { console.log("No micro:bit found; is your board plugged in?"); + return null; } }); } + /** + * @returns {Promise} when board version is set + */ setSerialPort(port) { // Use the given port. Assume the port has been opened by the caller. @@ -161,7 +168,7 @@ class MicrobitFirmataClient { // get the board serial number (used to determine board version) this.boardVersion = ''; - serialport.list() + return serialport.list() .then((ports) => { for (var i = 0; i < ports.length; i++) { var p = ports[i];