mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
Move Firmata into Arduino node so we don't rely on their version of serialport
This commit is contained in:
67
hardware/Arduino/lib/com.js
Normal file
67
hardware/Arduino/lib/com.js
Normal file
@@ -0,0 +1,67 @@
|
||||
"use strict";
|
||||
|
||||
const Emitter = require("events");
|
||||
|
||||
class TransportStub extends Emitter {
|
||||
constructor(path /*, options, openCallback*/) {
|
||||
super();
|
||||
this.isOpen = true;
|
||||
this.baudRate = 0;
|
||||
this.path = path;
|
||||
}
|
||||
|
||||
write(buffer) {
|
||||
// Tests are written to work with arrays not buffers
|
||||
// this shouldn't impact the data, just the container
|
||||
// This also should be changed in future test rewrites
|
||||
/* istanbul ignore else */
|
||||
if (Buffer.isBuffer(buffer)) {
|
||||
buffer = Array.from(buffer);
|
||||
}
|
||||
|
||||
this.lastWrite = buffer;
|
||||
this.emit("write", buffer);
|
||||
}
|
||||
|
||||
static list() {
|
||||
/* istanbul ignore next */
|
||||
return Promise.resolve([]);
|
||||
}
|
||||
}
|
||||
|
||||
// This trash is necessary for stubbing with sinon.
|
||||
TransportStub.SerialPort = TransportStub;
|
||||
|
||||
let com;
|
||||
let error;
|
||||
let SerialPort;
|
||||
|
||||
try {
|
||||
/* istanbul ignore else */
|
||||
if (process.env.IS_TEST_MODE) {
|
||||
com = TransportStub;
|
||||
} else {
|
||||
SerialPort = require("serialport").SerialPort;
|
||||
com = SerialPort;
|
||||
}
|
||||
} catch (err) {
|
||||
/* istanbul ignore next */
|
||||
error = err;
|
||||
}
|
||||
|
||||
/* istanbul ignore if */
|
||||
if (com == null) {
|
||||
if (process.env.IS_TEST_MODE) {
|
||||
com = TransportStub;
|
||||
} else {
|
||||
console.log("It looks like serialport didn't install properly.");
|
||||
console.log(
|
||||
"More information can be found here https://serialport.io/docs/guide-installation"
|
||||
);
|
||||
console.log(`The result of requiring the package is: ${SerialPort}`);
|
||||
console.log(error);
|
||||
throw "Missing serialport dependency";
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = com;
|
49
hardware/Arduino/lib/encoder7bit.js
Normal file
49
hardware/Arduino/lib/encoder7bit.js
Normal file
@@ -0,0 +1,49 @@
|
||||
"use strict";
|
||||
/**
|
||||
* "Inspired" by Encoder7Bit.h/Encoder7Bit.cpp in the
|
||||
* Firmata source code.
|
||||
*/
|
||||
module.exports = {
|
||||
to7BitArray(data) {
|
||||
let shift = 0;
|
||||
let previous = 0;
|
||||
const output = [];
|
||||
|
||||
for (let byte of data) {
|
||||
if (shift === 0) {
|
||||
output.push(byte & 0x7f);
|
||||
shift++;
|
||||
previous = byte >> 7;
|
||||
} else {
|
||||
output.push(((byte << shift) & 0x7f) | previous);
|
||||
if (shift === 6) {
|
||||
output.push(byte >> 1);
|
||||
shift = 0;
|
||||
} else {
|
||||
shift++;
|
||||
previous = byte >> (8 - shift);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* istanbul ignore else */
|
||||
if (shift > 0) {
|
||||
output.push(previous);
|
||||
}
|
||||
|
||||
return output;
|
||||
},
|
||||
from7BitArray(encoded) {
|
||||
const expectedBytes = encoded.length * 7 >> 3;
|
||||
const decoded = [];
|
||||
|
||||
for (let i = 0; i < expectedBytes; i++) {
|
||||
const j = i << 3;
|
||||
const pos = (j / 7) >>> 0;
|
||||
const shift = j % 7;
|
||||
decoded[i] = (encoded[pos] >> shift) | ((encoded[pos + 1] << (7 - shift)) & 0xFF);
|
||||
}
|
||||
|
||||
return decoded;
|
||||
}
|
||||
};
|
2650
hardware/Arduino/lib/firmata-io.js
Normal file
2650
hardware/Arduino/lib/firmata-io.js
Normal file
File diff suppressed because it is too large
Load Diff
3
hardware/Arduino/lib/firmata.js
Normal file
3
hardware/Arduino/lib/firmata.js
Normal file
@@ -0,0 +1,3 @@
|
||||
"use strict";
|
||||
|
||||
module.exports = require("./firmata-io.js")(require("./com"));
|
47
hardware/Arduino/lib/onewireutils.js
Normal file
47
hardware/Arduino/lib/onewireutils.js
Normal file
@@ -0,0 +1,47 @@
|
||||
"use strict";
|
||||
const Encoder7Bit = require("./encoder7bit");
|
||||
const OneWireUtils = {
|
||||
crc8(data) {
|
||||
let crc = 0;
|
||||
|
||||
for (let inbyte of data) {
|
||||
for (let n = 8; n; n--) {
|
||||
const mix = (crc ^ inbyte) & 0x01;
|
||||
crc >>= 1;
|
||||
|
||||
if (mix) {
|
||||
crc ^= 0x8C;
|
||||
}
|
||||
|
||||
inbyte >>= 1;
|
||||
}
|
||||
}
|
||||
|
||||
return crc;
|
||||
},
|
||||
|
||||
readDevices(data) {
|
||||
const deviceBytes = Encoder7Bit.from7BitArray(data);
|
||||
const devices = [];
|
||||
|
||||
for (let i = 0; i < deviceBytes.length; i += 8) {
|
||||
const device = deviceBytes.slice(i, i + 8);
|
||||
|
||||
if (device.length !== 8) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const check = OneWireUtils.crc8(device.slice(0, 7));
|
||||
|
||||
if (check !== device[7]) {
|
||||
console.error("ROM invalid!");
|
||||
}
|
||||
|
||||
devices.push(device);
|
||||
}
|
||||
|
||||
return devices;
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = OneWireUtils;
|
Reference in New Issue
Block a user