periph 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/examples/accelerometer/.gitkeep +0 -0
- package/examples/adc_dac/.gitkeep +0 -0
- package/examples/adc_dac/mcp4725/complete.js +34 -0
- package/examples/adc_dac/mcp4725/demo.js +43 -0
- package/examples/adc_dac/mcp4725/minimal.js +14 -0
- package/examples/color/.gitkeep +0 -0
- package/examples/comms/.gitkeep +0 -0
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/gas/.gitkeep +0 -0
- package/examples/gnss/.gitkeep +0 -0
- package/examples/gpio/.gitkeep +0 -0
- package/examples/gyroscope/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/imu/.gitkeep +0 -0
- package/examples/io_expander/mcp23017/complete.js +80 -0
- package/examples/io_expander/mcp23017/demo.js +59 -0
- package/examples/io_expander/mcp23017/minimal.js +16 -0
- package/examples/io_expander/pcf8574/complete.js +68 -0
- package/examples/io_expander/pcf8574/demo.js +41 -0
- package/examples/io_expander/pcf8574/minimal.js +16 -0
- package/examples/io_expander/pcf8575/complete.js +31 -0
- package/examples/io_expander/pcf8575/demo.js +33 -0
- package/examples/io_expander/pcf8575/minimal.js +20 -0
- package/examples/led/.gitkeep +0 -0
- package/examples/led/sk6812rgbw/complete.js +69 -0
- package/examples/led/sk6812rgbw/demo.js +85 -0
- package/examples/led/sk6812rgbw/minimal.js +25 -0
- package/examples/led/ws2812b/complete.js +64 -0
- package/examples/led/ws2812b/demo.js +101 -0
- package/examples/led/ws2812b/minimal.js +22 -0
- package/examples/light/.gitkeep +0 -0
- package/examples/magnetometer/.gitkeep +0 -0
- package/examples/magnetometer/as5600/complete.js +43 -0
- package/examples/magnetometer/as5600/demo.js +49 -0
- package/examples/magnetometer/as5600/minimal.js +16 -0
- package/examples/memory/.gitkeep +0 -0
- package/examples/motor/.gitkeep +0 -0
- package/examples/other/.gitkeep +0 -0
- package/examples/power/ina219/complete.js +30 -0
- package/examples/power/ina219/demo.js +26 -0
- package/examples/power/ina219/minimal.js +17 -0
- package/examples/power/ina226/complete.js +33 -0
- package/examples/power/ina226/demo.js +30 -0
- package/examples/power/ina226/minimal.js +11 -0
- package/examples/power/ina3221/complete.js +65 -0
- package/examples/power/ina3221/demo.js +56 -0
- package/examples/power/ina3221/minimal.js +16 -0
- package/examples/pressure/.gitkeep +0 -0
- package/examples/pressure/bmp180/complete.js +26 -0
- package/examples/pressure/bmp180/demo.js +32 -0
- package/examples/pressure/bmp180/minimal.js +17 -0
- package/examples/pressure/bmp280/complete.js +28 -0
- package/examples/pressure/bmp280/demo.js +34 -0
- package/examples/pressure/bmp280/minimal.js +17 -0
- package/examples/rfid/.gitkeep +0 -0
- package/examples/rtc/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/package.json +20 -0
- package/src/chips/accelerometer/.gitkeep +0 -0
- package/src/chips/adc_dac/.gitkeep +0 -0
- package/src/chips/adc_dac/mcp4725.js +161 -0
- package/src/chips/color/.gitkeep +0 -0
- package/src/chips/comms/.gitkeep +0 -0
- package/src/chips/display/.gitkeep +0 -0
- package/src/chips/environmental/.gitkeep +0 -0
- package/src/chips/gas/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/gpio/.gitkeep +0 -0
- package/src/chips/gyroscope/.gitkeep +0 -0
- package/src/chips/humidity/.gitkeep +0 -0
- package/src/chips/imu/.gitkeep +0 -0
- package/src/chips/io_expander/mcp23017.js +396 -0
- package/src/chips/io_expander/pcf8574.js +298 -0
- package/src/chips/io_expander/pcf8575.js +295 -0
- package/src/chips/led/.gitkeep +0 -0
- package/src/chips/led/_color.js +25 -0
- package/src/chips/led/sk6812rgbw.js +183 -0
- package/src/chips/led/ws2812b.js +171 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/magnetometer/as5600.js +293 -0
- package/src/chips/memory/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/other/.gitkeep +0 -0
- package/src/chips/power/ina219.js +154 -0
- package/src/chips/power/ina226.js +208 -0
- package/src/chips/power/ina3221.js +341 -0
- package/src/chips/pressure/.gitkeep +0 -0
- package/src/chips/pressure/bmp180.js +234 -0
- package/src/chips/pressure/bmp280.js +328 -0
- package/src/chips/rfid/.gitkeep +0 -0
- package/src/chips/rtc/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
- package/src/index.js +2 -0
- package/src/transport/i2c.js +67 -0
- package/src/transport/neopixel.js +37 -0
- package/src/transport/smbus.js +117 -0
- package/src/transport/spi.js +72 -0
- package/src/transport/uart.js +145 -0
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { SerialPort } = require('serialport');
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* UART transport for Node.js (wraps the `serialport` npm package).
|
|
7
|
+
*
|
|
8
|
+
* Opens the serial port asynchronously at construction and accumulates
|
|
9
|
+
* incoming bytes in an internal buffer. All operations return Promises.
|
|
10
|
+
* Call close() when done to release the port.
|
|
11
|
+
*
|
|
12
|
+
* For RS-485 DE toggling, pass de_pin_num and install the `onoff` package.
|
|
13
|
+
* The GPIO is asserted high before each write and deasserted after drain.
|
|
14
|
+
*/
|
|
15
|
+
class UARTTransport {
|
|
16
|
+
/**
|
|
17
|
+
* @param {string} path - Serial device path (e.g. '/dev/ttyS0').
|
|
18
|
+
* @param {object} [options]
|
|
19
|
+
* @param {number} [options.baudRate=9600] - Baud rate.
|
|
20
|
+
* @param {number} [options.timeoutMs=1000] - Read timeout in milliseconds.
|
|
21
|
+
* @param {number|null} [options.de_pin_num=null] - GPIO line for RS-485 DE; null disables.
|
|
22
|
+
*/
|
|
23
|
+
constructor(path, options = {}) {
|
|
24
|
+
this._baudRate = options.baudRate ?? 9600;
|
|
25
|
+
this._timeoutMs = options.timeoutMs ?? 1000;
|
|
26
|
+
this._de_pin_num = options.de_pin_num ?? null;
|
|
27
|
+
this._rxBuf = Buffer.alloc(0);
|
|
28
|
+
this._rxWaiters = [];
|
|
29
|
+
this._de = null;
|
|
30
|
+
|
|
31
|
+
this._port = new SerialPort({ path, baudRate: this._baudRate, autoOpen: false });
|
|
32
|
+
this._openPromise = new Promise((resolve, reject) => {
|
|
33
|
+
this._port.open(err => { if (err) reject(err); else resolve(); });
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
this._port.on('data', chunk => {
|
|
37
|
+
this._rxBuf = Buffer.concat([this._rxBuf, chunk]);
|
|
38
|
+
this._drainWaiters();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
if (this._de_pin_num !== null) {
|
|
42
|
+
try {
|
|
43
|
+
const { Gpio } = require('onoff');
|
|
44
|
+
this._de = new Gpio(this._de_pin_num, 'out');
|
|
45
|
+
this._de.writeSync(0);
|
|
46
|
+
} catch (_) {
|
|
47
|
+
// onoff unavailable — RS-485 DE toggling disabled.
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
_drainWaiters() {
|
|
53
|
+
while (this._rxWaiters.length > 0) {
|
|
54
|
+
const [n, resolve] = this._rxWaiters[0];
|
|
55
|
+
if (this._rxBuf.length < n) break;
|
|
56
|
+
this._rxWaiters.shift();
|
|
57
|
+
const chunk = this._rxBuf.subarray(0, n);
|
|
58
|
+
this._rxBuf = this._rxBuf.subarray(n);
|
|
59
|
+
resolve(Buffer.from(chunk));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_waitForBytes(n) {
|
|
64
|
+
return new Promise((resolve, reject) => {
|
|
65
|
+
if (this._rxBuf.length >= n) {
|
|
66
|
+
const chunk = this._rxBuf.subarray(0, n);
|
|
67
|
+
this._rxBuf = this._rxBuf.subarray(n);
|
|
68
|
+
resolve(Buffer.from(chunk));
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
const timer = setTimeout(() => {
|
|
72
|
+
this._rxWaiters = this._rxWaiters.filter(w => w[1] !== resolve);
|
|
73
|
+
reject(new Error('UART read timeout'));
|
|
74
|
+
}, this._timeoutMs);
|
|
75
|
+
this._rxWaiters.push([n, (buf) => { clearTimeout(timer); resolve(buf); }]);
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Wait for the port to finish opening. Must be awaited before first use.
|
|
81
|
+
* @returns {Promise<void>}
|
|
82
|
+
*/
|
|
83
|
+
async open() {
|
|
84
|
+
return this._openPromise;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Send bytes to the device; in RS-485 mode asserts DE, drains the OS
|
|
89
|
+
* TX buffer, then deasserts DE.
|
|
90
|
+
* @param {Buffer|Uint8Array} data - Bytes to send.
|
|
91
|
+
* @returns {Promise<void>}
|
|
92
|
+
*/
|
|
93
|
+
async write(data) {
|
|
94
|
+
await this._openPromise;
|
|
95
|
+
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
96
|
+
if (this._de) this._de.writeSync(1);
|
|
97
|
+
await new Promise((resolve, reject) => {
|
|
98
|
+
this._port.write(buf, err => { if (err) reject(err); else resolve(); });
|
|
99
|
+
});
|
|
100
|
+
await new Promise((resolve, reject) => {
|
|
101
|
+
this._port.drain(err => { if (err) reject(err); else resolve(); });
|
|
102
|
+
});
|
|
103
|
+
if (this._de) this._de.writeSync(0);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* Receive n bytes; resolves when n bytes have accumulated in the buffer
|
|
108
|
+
* or rejects on timeout.
|
|
109
|
+
* @param {number} n - Number of bytes to read.
|
|
110
|
+
* @returns {Promise<Buffer>} Data received from the device.
|
|
111
|
+
*/
|
|
112
|
+
async read(n) {
|
|
113
|
+
await this._openPromise;
|
|
114
|
+
return this._waitForBytes(n);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Send bytes then receive n bytes. In RS-485 mode DE is asserted only
|
|
119
|
+
* during the write phase.
|
|
120
|
+
* @param {Buffer|Uint8Array} data - Bytes to send.
|
|
121
|
+
* @param {number} n - Number of bytes to read.
|
|
122
|
+
* @returns {Promise<Buffer>} Data received from the device.
|
|
123
|
+
*/
|
|
124
|
+
async writeRead(data, n) {
|
|
125
|
+
await this.write(data);
|
|
126
|
+
return this.read(n);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Close the serial port and release the DE GPIO if applicable.
|
|
131
|
+
* @returns {Promise<void>}
|
|
132
|
+
*/
|
|
133
|
+
async close() {
|
|
134
|
+
await new Promise((resolve, reject) => {
|
|
135
|
+
this._port.close(err => { if (err) reject(err); else resolve(); });
|
|
136
|
+
});
|
|
137
|
+
if (this._de) {
|
|
138
|
+
this._de.writeSync(0);
|
|
139
|
+
this._de.unexport();
|
|
140
|
+
this._de = null;
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
module.exports = { UARTTransport };
|