periph 1.0.1 → 1.2.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/README.md +54 -12
- package/examples/accelerometer/adxl345/complete.js +52 -0
- package/examples/accelerometer/adxl345/demo.js +34 -0
- package/examples/accelerometer/adxl345/minimal.js +18 -0
- package/examples/accelerometer/adxl362/complete.js +57 -0
- package/examples/accelerometer/adxl362/demo.js +51 -0
- package/examples/accelerometer/adxl362/minimal.js +21 -0
- package/examples/adc_dac/ad7705/complete.js +32 -0
- package/examples/adc_dac/ad7705/demo.js +42 -0
- package/examples/adc_dac/ad7705/minimal.js +16 -0
- package/examples/adc_dac/ad7706/complete.js +35 -0
- package/examples/adc_dac/ad7706/demo.js +44 -0
- package/examples/adc_dac/ad7706/minimal.js +16 -0
- package/examples/adc_dac/hx710a/complete.js +45 -0
- package/examples/adc_dac/hx710a/demo.js +52 -0
- package/examples/adc_dac/hx710a/minimal.js +16 -0
- package/examples/adc_dac/hx710b/complete.js +45 -0
- package/examples/adc_dac/hx710b/demo.js +64 -0
- package/examples/adc_dac/hx710b/minimal.js +16 -0
- package/examples/adc_dac/hx711/complete.js +42 -0
- package/examples/adc_dac/hx711/demo.js +35 -0
- package/examples/adc_dac/hx711/minimal.js +16 -0
- package/examples/adc_dac/mcp4725/complete.js +19 -17
- package/examples/adc_dac/mcp4725/demo.js +5 -5
- package/examples/adc_dac/mcp4725/minimal.js +8 -6
- package/examples/adc_dac/mcp4728/complete.js +43 -0
- package/examples/adc_dac/mcp4728/demo.js +41 -0
- package/examples/adc_dac/mcp4728/minimal.js +13 -0
- package/examples/adc_dac/pcf8591/complete.js +41 -0
- package/examples/adc_dac/pcf8591/demo.js +39 -0
- package/examples/adc_dac/pcf8591/minimal.js +19 -0
- package/examples/comms/mcp2515/complete.js +75 -0
- package/examples/comms/mcp2515/demo.js +66 -0
- package/examples/comms/mcp2515/minimal.js +27 -0
- package/examples/comms/rda5807m/complete.js +55 -0
- package/examples/comms/rda5807m/demo.js +68 -0
- package/examples/comms/rda5807m/minimal.js +15 -0
- package/examples/comms/rfm9x/complete.js +50 -0
- package/examples/comms/rfm9x/demo.js +55 -0
- package/examples/comms/rfm9x/minimal.js +20 -0
- package/examples/display/pcf8576/complete.js +35 -0
- package/examples/display/pcf8576/demo.js +48 -0
- package/examples/display/pcf8576/minimal.js +20 -0
- package/examples/environmental/aht21/complete.js +32 -0
- package/examples/environmental/aht21/demo.js +46 -0
- package/examples/environmental/aht21/minimal.js +12 -0
- package/examples/environmental/bme280/complete.js +37 -0
- package/examples/environmental/bme280/demo.js +67 -0
- package/examples/environmental/bme280/minimal.js +20 -0
- package/examples/environmental/bme680/complete.js +40 -0
- package/examples/environmental/bme680/demo.js +54 -0
- package/examples/environmental/bme680/minimal.js +21 -0
- package/examples/gas/ens160/complete.js +54 -0
- package/examples/gas/ens160/demo.js +54 -0
- package/examples/gas/ens160/minimal.js +29 -0
- package/examples/gnss/neo6/complete.js +49 -0
- package/examples/gnss/neo6/demo.js +54 -0
- package/examples/gnss/neo6/minimal.js +30 -0
- package/examples/gyroscope/l3g4200d/complete.js +56 -0
- package/examples/gyroscope/l3g4200d/demo.js +49 -0
- package/examples/gyroscope/l3g4200d/minimal.js +19 -0
- package/examples/gyroscope/l3gd20h/complete.js +42 -0
- package/examples/gyroscope/l3gd20h/demo.js +29 -0
- package/examples/gyroscope/l3gd20h/minimal.js +13 -0
- package/examples/humidity/dht11/complete.js +22 -0
- package/examples/humidity/dht11/demo.js +38 -0
- package/examples/humidity/dht11/minimal.js +18 -0
- package/examples/imu/mpu6050/complete.js +58 -0
- package/examples/imu/mpu6050/demo.js +37 -0
- package/examples/imu/mpu6050/minimal.js +14 -0
- package/examples/imu/mpu9250/complete.js +65 -0
- package/examples/imu/mpu9250/demo.js +53 -0
- package/examples/imu/mpu9250/minimal.js +21 -0
- package/examples/imu/mpu9255/complete.js +73 -0
- package/examples/imu/mpu9255/demo.js +73 -0
- package/examples/imu/mpu9255/minimal.js +21 -0
- package/examples/io_expander/mcp23017/complete.js +70 -77
- package/examples/io_expander/mcp23017/demo.js +37 -33
- package/examples/io_expander/mcp23017/minimal.js +15 -11
- package/examples/io_expander/pcf8574/complete.js +60 -65
- package/examples/io_expander/pcf8574/demo.js +30 -25
- package/examples/io_expander/pcf8574/minimal.js +15 -11
- package/examples/io_expander/pcf8575/complete.js +24 -20
- package/examples/io_expander/pcf8575/demo.js +27 -23
- package/examples/io_expander/pcf8575/minimal.js +16 -12
- package/examples/io_expander/tpic6b595/complete.js +57 -0
- package/examples/io_expander/tpic6b595/demo.js +75 -0
- package/examples/io_expander/tpic6b595/minimal.js +26 -0
- package/examples/led/apa102/complete.js +72 -0
- package/examples/led/apa102/demo.js +92 -0
- package/examples/led/apa102/minimal.js +21 -0
- package/examples/led/sk6812rgbw/complete.js +42 -40
- package/examples/led/sk6812rgbw/demo.js +16 -16
- package/examples/led/sk6812rgbw/minimal.js +15 -17
- package/examples/led/ws2812b/complete.js +40 -38
- package/examples/led/ws2812b/demo.js +16 -16
- package/examples/led/ws2812b/minimal.js +13 -14
- package/examples/led/ws2814/complete.js +71 -0
- package/examples/led/ws2814/demo.js +82 -0
- package/examples/led/ws2814/minimal.js +23 -0
- package/examples/light/apds-9930/complete.js +46 -0
- package/examples/light/apds-9930/demo.js +31 -0
- package/examples/light/apds-9930/minimal.js +20 -0
- package/examples/light/apds9960/complete.js +64 -0
- package/examples/light/apds9960/demo.js +34 -0
- package/examples/light/apds9960/minimal.js +12 -0
- package/examples/magnetometer/as5600/complete.js +31 -29
- package/examples/magnetometer/as5600/demo.js +35 -33
- package/examples/magnetometer/as5600/minimal.js +7 -7
- package/examples/magnetometer/hmc5883l/complete.js +47 -0
- package/examples/magnetometer/hmc5883l/demo.js +50 -0
- package/examples/magnetometer/hmc5883l/minimal.js +15 -0
- package/examples/memory/24aa02uid/complete.js +42 -0
- package/examples/memory/24aa02uid/demo.js +42 -0
- package/examples/memory/24aa02uid/minimal.js +12 -0
- package/examples/other/mpr121/complete.js +50 -0
- package/examples/other/mpr121/demo.js +30 -0
- package/examples/other/mpr121/minimal.js +17 -0
- package/examples/power/ade7953/complete.js +44 -0
- package/examples/power/ade7953/demo.js +28 -0
- package/examples/power/ade7953/minimal.js +15 -0
- package/examples/power/ina219/complete.js +20 -18
- package/examples/power/ina219/demo.js +15 -13
- package/examples/power/ina219/minimal.js +8 -8
- package/examples/power/ina226/complete.js +24 -22
- package/examples/power/ina226/demo.js +22 -20
- package/examples/power/ina226/minimal.js +5 -5
- package/examples/power/ina3221/complete.js +51 -49
- package/examples/power/ina3221/demo.js +13 -13
- package/examples/power/ina3221/minimal.js +8 -8
- package/examples/pressure/bmp085/complete.js +29 -0
- package/examples/pressure/bmp085/demo.js +34 -0
- package/examples/pressure/bmp085/minimal.js +19 -0
- package/examples/pressure/bmp180/complete.js +20 -17
- package/examples/pressure/bmp180/demo.js +24 -22
- package/examples/pressure/bmp180/minimal.js +13 -11
- package/examples/pressure/bmp280/complete.js +29 -22
- package/examples/pressure/bmp280/demo.js +42 -23
- package/examples/pressure/bmp280/minimal.js +13 -11
- package/examples/pressure/bmp384/complete.js +29 -0
- package/examples/pressure/bmp384/demo.js +40 -0
- package/examples/pressure/bmp384/minimal.js +18 -0
- package/examples/pressure/bmp581/complete.js +34 -0
- package/examples/pressure/bmp581/demo.js +56 -0
- package/examples/pressure/bmp581/minimal.js +19 -0
- package/examples/pressure/lps22df/complete.js +50 -0
- package/examples/pressure/lps22df/demo.js +41 -0
- package/examples/pressure/lps22df/minimal.js +19 -0
- package/examples/pressure/lps28dfw/complete.js +37 -0
- package/examples/pressure/lps28dfw/demo.js +33 -0
- package/examples/pressure/lps28dfw/minimal.js +19 -0
- package/examples/pressure/lps33hw/complete.js +43 -0
- package/examples/pressure/lps33hw/demo.js +43 -0
- package/examples/pressure/lps33hw/minimal.js +19 -0
- package/examples/rfid/mfrc522/complete.js +44 -0
- package/examples/rfid/mfrc522/demo.js +70 -0
- package/examples/rfid/mfrc522/minimal.js +17 -0
- package/package.json +5 -1
- package/src/chips/accelerometer/adxl345.js +482 -0
- package/src/chips/accelerometer/adxl362.js +594 -0
- package/src/chips/adc_dac/ad7705.js +345 -0
- package/src/chips/adc_dac/ad7706.js +331 -0
- package/src/chips/adc_dac/hx710a.js +184 -0
- package/src/chips/adc_dac/hx710b.js +186 -0
- package/src/chips/adc_dac/hx711.js +165 -0
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +297 -0
- package/src/chips/adc_dac/pcf8591.js +196 -0
- package/src/chips/comms/mcp2515.js +659 -0
- package/src/chips/comms/rda5807m.js +393 -0
- package/src/chips/comms/rfm9x.js +590 -0
- package/src/chips/display/pcf8576.js +232 -0
- package/src/chips/environmental/aht21.js +185 -0
- package/src/chips/environmental/bme280.js +385 -0
- package/src/chips/environmental/bme680.js +512 -0
- package/src/chips/gas/ens160.js +298 -0
- package/src/chips/gnss/neo6.js +438 -0
- package/src/chips/gyroscope/l3g4200d.js +366 -0
- package/src/chips/gyroscope/l3gd20h.js +314 -0
- package/src/chips/humidity/dht11.js +135 -0
- package/src/chips/imu/mpu6050.js +289 -0
- package/src/chips/imu/mpu9250.js +394 -0
- package/src/chips/imu/mpu9255.js +447 -0
- package/src/chips/io_expander/mcp23017.js +240 -157
- package/src/chips/io_expander/pcf8574.js +149 -124
- package/src/chips/io_expander/pcf8575.js +164 -135
- package/src/chips/io_expander/tpic6b595.js +242 -0
- package/src/chips/led/_NeoPixelRGBBase.js +204 -0
- package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
- package/src/chips/led/apa102.js +236 -0
- package/src/chips/led/sk6812rgbw.js +15 -149
- package/src/chips/led/ws2812b.js +14 -139
- package/src/chips/led/ws2814.js +49 -0
- package/src/chips/light/apds-9930.js +342 -0
- package/src/chips/light/apds9960.js +499 -0
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +182 -0
- package/src/chips/other/mpr121.js +375 -0
- package/src/chips/power/ade7953.js +427 -0
- package/src/chips/power/ina219.js +45 -40
- package/src/chips/power/ina226.js +50 -45
- package/src/chips/power/ina3221.js +95 -76
- package/src/chips/pressure/bmp085.js +251 -0
- package/src/chips/pressure/bmp180.js +54 -37
- package/src/chips/pressure/bmp280.js +188 -206
- package/src/chips/pressure/bmp384.js +410 -0
- package/src/chips/pressure/bmp581.js +489 -0
- package/src/chips/pressure/lps22df.js +341 -0
- package/src/chips/pressure/lps28dfw.js +314 -0
- package/src/chips/pressure/lps33hw.js +255 -0
- package/src/chips/rfid/mfrc522.js +674 -0
- package/src/connection/connection.js +111 -0
- package/src/connection/dhtxx.js +138 -0
- package/src/connection/dhtxx_mock.js +39 -0
- package/src/connection/hx711.js +132 -0
- package/src/connection/hx711_mock.js +52 -0
- package/src/{transport → connection}/i2c.js +13 -9
- package/src/connection/i2c_mock.js +91 -0
- package/src/connection/input_pin.js +156 -0
- package/src/connection/neo6_mock.js +45 -0
- package/src/{transport → connection}/neopixel.js +23 -4
- package/src/connection/output_pin.js +41 -0
- package/src/connection/sipo.js +159 -0
- package/src/{transport → connection}/smbus.js +14 -10
- package/src/{transport → connection}/spi.js +12 -8
- package/src/connection/spi_mock.js +151 -0
- package/src/{transport → connection}/uart.js +33 -26
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/examples/imu/.gitkeep +0 -0
- package/examples/light/.gitkeep +0 -0
- package/examples/pressure/.gitkeep +0 -0
- package/src/chips/display/.gitkeep +0 -0
- package/src/chips/environmental/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/imu/.gitkeep +0 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/pressure/.gitkeep +0 -0
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* In-memory fake SPI connection for unit tests — no hardware, no bus.
|
|
5
|
+
*
|
|
6
|
+
* Models the same primitive that `SPIConnection` exposes to chip drivers:
|
|
7
|
+
*
|
|
8
|
+
* - `write(data)`: appends `data` to `writes` for assertions. If `data` is
|
|
9
|
+
* a command-address-plus-data packet (i.e. one of the MCP2515-style
|
|
10
|
+
* multi-byte commands — WRITE `0x02 addr byte...`, BIT MODIFY
|
|
11
|
+
* `0x05 addr mask data`, LOAD TX BUFFER `0x40|n payload...`), the
|
|
12
|
+
* trailing bytes are also written into the byte-addressable `registers`
|
|
13
|
+
* map starting at the address byte. Writes whose first byte is a
|
|
14
|
+
* standalone instruction (RESET `0xC0`, RTS `0x80|mask`) are recorded
|
|
15
|
+
* but do not touch `registers`.
|
|
16
|
+
* - `read(n)`: returns a `n`-byte FIFO-buffered payload, popped from
|
|
17
|
+
* `queueRead`. Falls back to `n` zero bytes if the queue is empty.
|
|
18
|
+
* - `writeRead(data, n)`: writes are first applied to `registers` when
|
|
19
|
+
* `data[0]` is a register-addressed instruction (READ `0x03 addr`,
|
|
20
|
+
* READ RX BUFFER `0x90|offset`, READ STATUS `0xA0`, RX STATUS `0xB0`).
|
|
21
|
+
* The returned `n` bytes are read from `registers` starting at
|
|
22
|
+
* `data[1]` (when the instruction is READ/READ RX BUFFER) or from
|
|
23
|
+
* address `0` (READ STATUS / RX STATUS, which return a single
|
|
24
|
+
* status byte regardless of address).
|
|
25
|
+
*
|
|
26
|
+
* Useful commands and addresses recognised by `setRegister` and the
|
|
27
|
+
* auto-into-registers `write` update:
|
|
28
|
+
*
|
|
29
|
+
* - RESET `0xC0` (no register update)
|
|
30
|
+
* - READ `0x03 addr` (command with 1 address byte)
|
|
31
|
+
* - WRITE `0x02 addr data...` (command + address + data)
|
|
32
|
+
* - LOAD TX BUFFER `0x40|offset payload` (command + 13 payload bytes;
|
|
33
|
+
* TX buffers map to addresses 0x31..0x3D / 0x41..0x4D /
|
|
34
|
+
* 0x51..0x5D — `0x40|0` → 0x31, `0x40|2` → 0x32…)
|
|
35
|
+
* - RTS `0x80|mask` (no register update)
|
|
36
|
+
* - READ STATUS `0xA0` (returns `registers.get(0)`
|
|
37
|
+
* or 0 if unset — works around the chip's "address ignored"
|
|
38
|
+
* behaviour by stashing the queued byte at address 0)
|
|
39
|
+
* - RX STATUS `0xB0` (likewise)
|
|
40
|
+
* - BIT MODIFY `0x05 addr mask data` (command + addr + 2 data bytes)
|
|
41
|
+
*/
|
|
42
|
+
class SPIConnectionMock {
|
|
43
|
+
constructor() {
|
|
44
|
+
this.registers = new Map();
|
|
45
|
+
this.writes = [];
|
|
46
|
+
this._readQueue = [];
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Preload consecutive register bytes starting at `reg`.
|
|
51
|
+
* @param {number} reg - First register address.
|
|
52
|
+
* @param {number[]|Buffer|Uint8Array} values - Bytes to set.
|
|
53
|
+
*/
|
|
54
|
+
setRegister(reg, values) {
|
|
55
|
+
for (let i = 0; i < values.length; i++) {
|
|
56
|
+
this.registers.set(reg + i, values[i]);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Queue bytes to be returned by the next plain `read(n)` call.
|
|
62
|
+
* @param {number[]|Buffer|Uint8Array} data - Bytes to queue.
|
|
63
|
+
*/
|
|
64
|
+
queueRead(data) {
|
|
65
|
+
this._readQueue.push(Buffer.from(data));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Apply a write command to the in-memory register map. Returns true if
|
|
70
|
+
* the command affected registers, false if it was a standalone
|
|
71
|
+
* instruction (RESET / RTS) or an unrecognised first byte.
|
|
72
|
+
* @param {Buffer} buf
|
|
73
|
+
* @returns {boolean}
|
|
74
|
+
*/
|
|
75
|
+
_applyWrite(buf) {
|
|
76
|
+
const cmd = buf[0];
|
|
77
|
+
if (cmd === 0xC0 || (cmd & 0x80) === 0x80) {
|
|
78
|
+
return false;
|
|
79
|
+
}
|
|
80
|
+
if (cmd === 0x02 && buf.length >= 3) {
|
|
81
|
+
const reg = buf[1];
|
|
82
|
+
for (let i = 2; i < buf.length; i++) {
|
|
83
|
+
this.registers.set(reg + i - 2, buf[i]);
|
|
84
|
+
}
|
|
85
|
+
return true;
|
|
86
|
+
}
|
|
87
|
+
if (cmd === 0x05 && buf.length >= 4) {
|
|
88
|
+
const reg = buf[1];
|
|
89
|
+
const mask = buf[2];
|
|
90
|
+
const data = buf[3];
|
|
91
|
+
const cur = this.registers.get(reg) || 0;
|
|
92
|
+
this.registers.set(reg, (cur & ~mask) | (data & mask));
|
|
93
|
+
return true;
|
|
94
|
+
}
|
|
95
|
+
if ((cmd & 0xE0) === 0x40) {
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** @param {Buffer|Uint8Array} data */
|
|
102
|
+
async write(data) {
|
|
103
|
+
const buf = Buffer.from(data);
|
|
104
|
+
this.writes.push(buf);
|
|
105
|
+
this._applyWrite(buf);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** @param {number} n */
|
|
109
|
+
async read(n) {
|
|
110
|
+
if (this._readQueue.length > 0) {
|
|
111
|
+
const front = this._readQueue.shift();
|
|
112
|
+
const out = Buffer.alloc(n);
|
|
113
|
+
front.copy(out, 0, 0, Math.min(n, front.length));
|
|
114
|
+
return out;
|
|
115
|
+
}
|
|
116
|
+
return Buffer.alloc(n);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** @param {Buffer|Uint8Array} data @param {number} n */
|
|
120
|
+
async writeRead(data, n) {
|
|
121
|
+
const buf = Buffer.from(data);
|
|
122
|
+
this.writes.push(buf);
|
|
123
|
+
const cmd = buf[0];
|
|
124
|
+
const out = Buffer.alloc(n);
|
|
125
|
+
let startReg = null;
|
|
126
|
+
if (cmd === 0x03 && buf.length >= 2) {
|
|
127
|
+
startReg = buf[1];
|
|
128
|
+
} else if ((cmd & 0xF0) === 0x90) {
|
|
129
|
+
const offset = cmd & 0x07;
|
|
130
|
+
// Per the spec: offset 0 → RXB0SIDH (0x61), offset 2 → RXB0D0
|
|
131
|
+
// (0x66); offset 4 → RXB1SIDH (0x71), offset 6 → RXB1D0 (0x76).
|
|
132
|
+
// The first byte of the returned burst is therefore the SIDH of
|
|
133
|
+
// the chosen RX buffer — never the control register.
|
|
134
|
+
if (offset < 4) startReg = 0x61 + (offset & 0x03);
|
|
135
|
+
else startReg = 0x71 + (offset & 0x03);
|
|
136
|
+
} else if (cmd === 0xA0 || cmd === 0xB0) {
|
|
137
|
+
startReg = 0;
|
|
138
|
+
}
|
|
139
|
+
if (startReg !== null) {
|
|
140
|
+
for (let i = 0; i < n; i++) {
|
|
141
|
+
out[i] = this.registers.get(startReg + i) || 0;
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return out;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** No-op. */
|
|
148
|
+
async close() {}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
module.exports = { SPIConnectionMock };
|
|
@@ -1,32 +1,39 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
3
|
const { SerialPort } = require('serialport');
|
|
4
|
+
const { Connection } = require('./connection');
|
|
4
5
|
|
|
5
6
|
/**
|
|
6
|
-
* UART
|
|
7
|
+
* UART connection for Node.js (wraps the `serialport` npm package).
|
|
7
8
|
*
|
|
8
9
|
* Opens the serial port asynchronously at construction and accumulates
|
|
9
10
|
* incoming bytes in an internal buffer. All operations return Promises.
|
|
10
11
|
* Call close() when done to release the port.
|
|
11
12
|
*
|
|
12
|
-
* For RS-485 DE toggling, pass
|
|
13
|
-
* The GPIO is asserted high before each write and
|
|
13
|
+
* For RS-485 DE toggling, pass de_gpio ({chip, line}) and install the
|
|
14
|
+
* `opengpio` package. The GPIO is asserted high before each write and
|
|
15
|
+
* deasserted after drain. This is separate from the standard EN pin
|
|
16
|
+
* (hardware enable/power) — DE controls RS-485 transceiver direction,
|
|
17
|
+
* not device power.
|
|
14
18
|
*/
|
|
15
|
-
class
|
|
19
|
+
class UARTConnection extends Connection {
|
|
16
20
|
/**
|
|
17
21
|
* @param {string} path - Serial device path (e.g. '/dev/ttyS0').
|
|
18
22
|
* @param {object} [options]
|
|
19
23
|
* @param {number} [options.baudRate=9600] - Baud rate.
|
|
20
24
|
* @param {number} [options.timeoutMs=1000] - Read timeout in milliseconds.
|
|
21
|
-
* @param {number|null} [options.
|
|
25
|
+
* @param {{chip: number, line: number}|null} [options.de_gpio=null] - gpiod chip+line for RS-485 DE; null disables.
|
|
26
|
+
* @param {import('./input_pin').InputPin|null} [options.intPin=null] - Optional INT-line InputPin.
|
|
27
|
+
* @param {import('./output_pin').OutputPin|null} [options.enPin=null] - Optional EN-pin OutputPin.
|
|
22
28
|
*/
|
|
23
29
|
constructor(path, options = {}) {
|
|
24
|
-
|
|
25
|
-
this.
|
|
26
|
-
this.
|
|
27
|
-
this.
|
|
28
|
-
this.
|
|
29
|
-
this.
|
|
30
|
+
super(options.intPin ?? null, options.enPin ?? null);
|
|
31
|
+
this._baudRate = options.baudRate ?? 9600;
|
|
32
|
+
this._timeoutMs = options.timeoutMs ?? 1000;
|
|
33
|
+
this._de_gpio = options.de_gpio ?? null;
|
|
34
|
+
this._rxBuf = Buffer.alloc(0);
|
|
35
|
+
this._rxWaiters = [];
|
|
36
|
+
this._de = null;
|
|
30
37
|
|
|
31
38
|
this._port = new SerialPort({ path, baudRate: this._baudRate, autoOpen: false });
|
|
32
39
|
this._openPromise = new Promise((resolve, reject) => {
|
|
@@ -38,13 +45,13 @@ class UARTTransport {
|
|
|
38
45
|
this._drainWaiters();
|
|
39
46
|
});
|
|
40
47
|
|
|
41
|
-
if (this.
|
|
48
|
+
if (this._de_gpio !== null) {
|
|
42
49
|
try {
|
|
43
|
-
const {
|
|
44
|
-
this._de =
|
|
45
|
-
this._de.
|
|
50
|
+
const { Default } = require('opengpio');
|
|
51
|
+
this._de = Default.output(this._de_gpio);
|
|
52
|
+
this._de.value = false;
|
|
46
53
|
} catch (_) {
|
|
47
|
-
//
|
|
54
|
+
// opengpio unavailable — RS-485 DE toggling disabled.
|
|
48
55
|
}
|
|
49
56
|
}
|
|
50
57
|
}
|
|
@@ -90,17 +97,17 @@ class UARTTransport {
|
|
|
90
97
|
* @param {Buffer|Uint8Array} data - Bytes to send.
|
|
91
98
|
* @returns {Promise<void>}
|
|
92
99
|
*/
|
|
93
|
-
async
|
|
100
|
+
async _write(data) {
|
|
94
101
|
await this._openPromise;
|
|
95
102
|
const buf = Buffer.isBuffer(data) ? data : Buffer.from(data);
|
|
96
|
-
if (this._de) this._de.
|
|
103
|
+
if (this._de) this._de.value = true;
|
|
97
104
|
await new Promise((resolve, reject) => {
|
|
98
105
|
this._port.write(buf, err => { if (err) reject(err); else resolve(); });
|
|
99
106
|
});
|
|
100
107
|
await new Promise((resolve, reject) => {
|
|
101
108
|
this._port.drain(err => { if (err) reject(err); else resolve(); });
|
|
102
109
|
});
|
|
103
|
-
if (this._de) this._de.
|
|
110
|
+
if (this._de) this._de.value = false;
|
|
104
111
|
}
|
|
105
112
|
|
|
106
113
|
/**
|
|
@@ -109,7 +116,7 @@ class UARTTransport {
|
|
|
109
116
|
* @param {number} n - Number of bytes to read.
|
|
110
117
|
* @returns {Promise<Buffer>} Data received from the device.
|
|
111
118
|
*/
|
|
112
|
-
async
|
|
119
|
+
async _read(n) {
|
|
113
120
|
await this._openPromise;
|
|
114
121
|
return this._waitForBytes(n);
|
|
115
122
|
}
|
|
@@ -121,9 +128,9 @@ class UARTTransport {
|
|
|
121
128
|
* @param {number} n - Number of bytes to read.
|
|
122
129
|
* @returns {Promise<Buffer>} Data received from the device.
|
|
123
130
|
*/
|
|
124
|
-
async
|
|
125
|
-
await this.
|
|
126
|
-
return this.
|
|
131
|
+
async _writeRead(data, n) {
|
|
132
|
+
await this._write(data);
|
|
133
|
+
return this._read(n);
|
|
127
134
|
}
|
|
128
135
|
|
|
129
136
|
/**
|
|
@@ -135,11 +142,11 @@ class UARTTransport {
|
|
|
135
142
|
this._port.close(err => { if (err) reject(err); else resolve(); });
|
|
136
143
|
});
|
|
137
144
|
if (this._de) {
|
|
138
|
-
this._de.
|
|
139
|
-
this._de.
|
|
145
|
+
this._de.value = false;
|
|
146
|
+
this._de.stop();
|
|
140
147
|
this._de = null;
|
|
141
148
|
}
|
|
142
149
|
}
|
|
143
150
|
}
|
|
144
151
|
|
|
145
|
-
module.exports = {
|
|
152
|
+
module.exports = { UARTConnection };
|
|
File without changes
|
|
File without changes
|
package/examples/gnss/.gitkeep
DELETED
|
File without changes
|
|
File without changes
|
package/examples/imu/.gitkeep
DELETED
|
File without changes
|
package/examples/light/.gitkeep
DELETED
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
package/src/chips/gnss/.gitkeep
DELETED
|
File without changes
|
package/src/chips/imu/.gitkeep
DELETED
|
File without changes
|
package/src/chips/light/.gitkeep
DELETED
|
File without changes
|
|
File without changes
|