Move Firmata into Arduino node so we don't rely on their version of serialport

This commit is contained in:
Dave Conway-Jones
2023-11-17 15:07:38 +00:00
parent 4c3e7bff91
commit 81501dfbdf
8 changed files with 2854 additions and 7 deletions

View 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;
}
};