periph 1.1.0 → 1.2.1

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.
Files changed (266) hide show
  1. package/README.md +61 -12
  2. package/examples/accelerometer/adxl345/complete.js +52 -0
  3. package/examples/accelerometer/adxl345/demo.js +34 -0
  4. package/examples/accelerometer/adxl345/minimal.js +18 -0
  5. package/examples/accelerometer/adxl362/complete.js +57 -0
  6. package/examples/accelerometer/adxl362/demo.js +51 -0
  7. package/examples/accelerometer/adxl362/minimal.js +21 -0
  8. package/examples/adc_dac/ad7705/complete.js +32 -0
  9. package/examples/adc_dac/ad7705/demo.js +42 -0
  10. package/examples/adc_dac/ad7705/minimal.js +16 -0
  11. package/examples/adc_dac/ad7706/complete.js +35 -0
  12. package/examples/adc_dac/ad7706/demo.js +44 -0
  13. package/examples/adc_dac/ad7706/minimal.js +16 -0
  14. package/examples/adc_dac/hx710a/complete.js +45 -0
  15. package/examples/adc_dac/hx710a/demo.js +52 -0
  16. package/examples/adc_dac/hx710a/minimal.js +16 -0
  17. package/examples/adc_dac/hx710b/complete.js +45 -0
  18. package/examples/adc_dac/hx710b/demo.js +64 -0
  19. package/examples/adc_dac/hx710b/minimal.js +16 -0
  20. package/examples/adc_dac/hx711/complete.js +7 -7
  21. package/examples/adc_dac/hx711/demo.js +6 -6
  22. package/examples/adc_dac/hx711/minimal.js +7 -7
  23. package/examples/adc_dac/mcp4725/complete.js +19 -17
  24. package/examples/adc_dac/mcp4725/demo.js +5 -5
  25. package/examples/adc_dac/mcp4725/minimal.js +8 -6
  26. package/examples/adc_dac/mcp4728/complete.js +24 -22
  27. package/examples/adc_dac/mcp4728/demo.js +22 -22
  28. package/examples/adc_dac/mcp4728/minimal.js +8 -6
  29. package/examples/adc_dac/pcf8591/complete.js +41 -0
  30. package/examples/adc_dac/pcf8591/demo.js +39 -0
  31. package/examples/adc_dac/pcf8591/minimal.js +19 -0
  32. package/examples/comms/mcp2515/complete.js +75 -0
  33. package/examples/comms/mcp2515/demo.js +66 -0
  34. package/examples/comms/mcp2515/minimal.js +27 -0
  35. package/examples/comms/rda5807m/complete.js +55 -0
  36. package/examples/comms/rda5807m/demo.js +68 -0
  37. package/examples/comms/rda5807m/minimal.js +15 -0
  38. package/examples/comms/rfm9x/complete.js +50 -0
  39. package/examples/comms/rfm9x/demo.js +55 -0
  40. package/examples/comms/rfm9x/minimal.js +20 -0
  41. package/examples/display/pcf8576/complete.js +18 -15
  42. package/examples/display/pcf8576/demo.js +6 -6
  43. package/examples/display/pcf8576/minimal.js +12 -10
  44. package/examples/environmental/aht21/complete.js +15 -13
  45. package/examples/environmental/aht21/demo.js +8 -6
  46. package/examples/environmental/aht21/minimal.js +5 -5
  47. package/examples/environmental/bme280/complete.js +23 -20
  48. package/examples/environmental/bme280/demo.js +14 -14
  49. package/examples/environmental/bme280/minimal.js +13 -11
  50. package/examples/environmental/bme680/complete.js +25 -22
  51. package/examples/environmental/bme680/demo.js +38 -36
  52. package/examples/environmental/bme680/minimal.js +14 -12
  53. package/examples/gas/ens160/complete.js +29 -27
  54. package/examples/gas/ens160/demo.js +39 -37
  55. package/examples/gas/ens160/minimal.js +17 -15
  56. package/examples/gnss/neo6/complete.js +49 -0
  57. package/examples/gnss/neo6/demo.js +54 -0
  58. package/examples/gnss/neo6/minimal.js +30 -0
  59. package/examples/gyroscope/l3g4200d/complete.js +56 -0
  60. package/examples/gyroscope/l3g4200d/demo.js +49 -0
  61. package/examples/gyroscope/l3g4200d/minimal.js +19 -0
  62. package/examples/gyroscope/l3gd20h/complete.js +42 -0
  63. package/examples/gyroscope/l3gd20h/demo.js +29 -0
  64. package/examples/gyroscope/l3gd20h/minimal.js +13 -0
  65. package/examples/humidity/dht11/complete.js +22 -0
  66. package/examples/humidity/dht11/demo.js +38 -0
  67. package/examples/humidity/dht11/minimal.js +18 -0
  68. package/examples/imu/mpu6050/complete.js +27 -25
  69. package/examples/imu/mpu6050/demo.js +29 -26
  70. package/examples/imu/mpu6050/minimal.js +6 -6
  71. package/examples/imu/mpu9250/complete.js +65 -0
  72. package/examples/imu/mpu9250/demo.js +53 -0
  73. package/examples/imu/mpu9250/minimal.js +21 -0
  74. package/examples/imu/mpu9255/complete.js +73 -0
  75. package/examples/imu/mpu9255/demo.js +73 -0
  76. package/examples/imu/mpu9255/minimal.js +21 -0
  77. package/examples/io_expander/mcp23017/complete.js +70 -77
  78. package/examples/io_expander/mcp23017/demo.js +37 -33
  79. package/examples/io_expander/mcp23017/minimal.js +15 -11
  80. package/examples/io_expander/pcf8574/complete.js +60 -65
  81. package/examples/io_expander/pcf8574/demo.js +30 -25
  82. package/examples/io_expander/pcf8574/minimal.js +15 -11
  83. package/examples/io_expander/pcf8575/complete.js +24 -20
  84. package/examples/io_expander/pcf8575/demo.js +27 -23
  85. package/examples/io_expander/pcf8575/minimal.js +16 -12
  86. package/examples/io_expander/tpic6b595/complete.js +57 -0
  87. package/examples/io_expander/tpic6b595/demo.js +75 -0
  88. package/examples/io_expander/tpic6b595/minimal.js +26 -0
  89. package/examples/led/apa102/complete.js +72 -0
  90. package/examples/led/apa102/demo.js +92 -0
  91. package/examples/led/apa102/minimal.js +21 -0
  92. package/examples/led/sk6812rgbw/complete.js +42 -40
  93. package/examples/led/sk6812rgbw/demo.js +16 -16
  94. package/examples/led/sk6812rgbw/minimal.js +15 -17
  95. package/examples/led/ws2812b/complete.js +40 -38
  96. package/examples/led/ws2812b/demo.js +16 -16
  97. package/examples/led/ws2812b/minimal.js +13 -14
  98. package/examples/led/ws2814/complete.js +71 -0
  99. package/examples/led/ws2814/demo.js +82 -0
  100. package/examples/led/ws2814/minimal.js +23 -0
  101. package/examples/light/apds-9930/complete.js +46 -0
  102. package/examples/light/apds-9930/demo.js +31 -0
  103. package/examples/light/apds-9930/minimal.js +20 -0
  104. package/examples/light/apds9960/complete.js +45 -43
  105. package/examples/light/apds9960/demo.js +11 -9
  106. package/examples/light/apds9960/minimal.js +5 -5
  107. package/examples/magnetometer/as5600/complete.js +31 -29
  108. package/examples/magnetometer/as5600/demo.js +35 -33
  109. package/examples/magnetometer/as5600/minimal.js +7 -7
  110. package/examples/magnetometer/hmc5883l/complete.js +47 -0
  111. package/examples/magnetometer/hmc5883l/demo.js +50 -0
  112. package/examples/magnetometer/hmc5883l/minimal.js +15 -0
  113. package/examples/memory/24aa02uid/complete.js +23 -21
  114. package/examples/memory/24aa02uid/demo.js +32 -30
  115. package/examples/memory/24aa02uid/minimal.js +5 -5
  116. package/examples/motor/drv8830/complete.js +50 -0
  117. package/examples/motor/drv8830/demo.js +57 -0
  118. package/examples/motor/drv8830/minimal.js +23 -0
  119. package/examples/other/mpr121/complete.js +50 -0
  120. package/examples/other/mpr121/demo.js +30 -0
  121. package/examples/other/mpr121/minimal.js +17 -0
  122. package/examples/power/ade7953/complete.js +44 -0
  123. package/examples/power/ade7953/demo.js +28 -0
  124. package/examples/power/ade7953/minimal.js +15 -0
  125. package/examples/power/ina219/complete.js +20 -18
  126. package/examples/power/ina219/demo.js +15 -13
  127. package/examples/power/ina219/minimal.js +8 -8
  128. package/examples/power/ina226/complete.js +24 -22
  129. package/examples/power/ina226/demo.js +22 -20
  130. package/examples/power/ina226/minimal.js +5 -5
  131. package/examples/power/ina3221/complete.js +51 -49
  132. package/examples/power/ina3221/demo.js +13 -13
  133. package/examples/power/ina3221/minimal.js +8 -8
  134. package/examples/pressure/bmp085/complete.js +29 -0
  135. package/examples/pressure/bmp085/demo.js +34 -0
  136. package/examples/pressure/bmp085/minimal.js +19 -0
  137. package/examples/pressure/bmp180/complete.js +20 -17
  138. package/examples/pressure/bmp180/demo.js +24 -22
  139. package/examples/pressure/bmp180/minimal.js +13 -11
  140. package/examples/pressure/bmp280/complete.js +23 -20
  141. package/examples/pressure/bmp280/demo.js +44 -41
  142. package/examples/pressure/bmp280/minimal.js +13 -11
  143. package/examples/pressure/bmp384/complete.js +29 -0
  144. package/examples/pressure/bmp384/demo.js +40 -0
  145. package/examples/pressure/bmp384/minimal.js +18 -0
  146. package/examples/pressure/bmp581/complete.js +34 -0
  147. package/examples/pressure/bmp581/demo.js +56 -0
  148. package/examples/pressure/bmp581/minimal.js +19 -0
  149. package/examples/pressure/lps22df/complete.js +50 -0
  150. package/examples/pressure/lps22df/demo.js +41 -0
  151. package/examples/pressure/lps22df/minimal.js +19 -0
  152. package/examples/pressure/lps28dfw/complete.js +37 -0
  153. package/examples/pressure/lps28dfw/demo.js +33 -0
  154. package/examples/pressure/lps28dfw/minimal.js +19 -0
  155. package/examples/pressure/lps33hw/complete.js +43 -0
  156. package/examples/pressure/lps33hw/demo.js +43 -0
  157. package/examples/pressure/lps33hw/minimal.js +19 -0
  158. package/examples/rfid/mfrc522/complete.js +44 -0
  159. package/examples/rfid/mfrc522/demo.js +70 -0
  160. package/examples/rfid/mfrc522/minimal.js +17 -0
  161. package/examples/rtc/ds3231/complete.js +77 -0
  162. package/examples/rtc/ds3231/demo.js +60 -0
  163. package/examples/rtc/ds3231/minimal.js +23 -0
  164. package/examples/rtc/pcf8523/complete.js +66 -0
  165. package/examples/rtc/pcf8523/demo.js +65 -0
  166. package/examples/rtc/pcf8523/minimal.js +20 -0
  167. package/examples/temperature/mcp9808/complete.js +89 -0
  168. package/examples/temperature/mcp9808/demo.js +62 -0
  169. package/examples/temperature/mcp9808/minimal.js +21 -0
  170. package/examples/temperature/tmp117/complete.js +90 -0
  171. package/examples/temperature/tmp117/demo.js +73 -0
  172. package/examples/temperature/tmp117/minimal.js +21 -0
  173. package/examples/tof/vl53l0x/complete.js +118 -0
  174. package/examples/tof/vl53l0x/demo.js +83 -0
  175. package/examples/tof/vl53l0x/minimal.js +25 -0
  176. package/examples/tof/vl53l1x/complete.js +151 -0
  177. package/examples/tof/vl53l1x/demo.js +100 -0
  178. package/examples/tof/vl53l1x/minimal.js +25 -0
  179. package/package.json +5 -1
  180. package/src/chips/accelerometer/adxl345.js +482 -0
  181. package/src/chips/accelerometer/adxl362.js +594 -0
  182. package/src/chips/adc_dac/ad7705.js +345 -0
  183. package/src/chips/adc_dac/ad7706.js +331 -0
  184. package/src/chips/adc_dac/hx710a.js +184 -0
  185. package/src/chips/adc_dac/hx710b.js +186 -0
  186. package/src/chips/adc_dac/hx711.js +15 -15
  187. package/src/chips/adc_dac/mcp4725.js +48 -40
  188. package/src/chips/adc_dac/mcp4728.js +57 -44
  189. package/src/chips/adc_dac/pcf8591.js +196 -0
  190. package/src/chips/comms/mcp2515.js +659 -0
  191. package/src/chips/comms/rda5807m.js +393 -0
  192. package/src/chips/comms/rfm9x.js +590 -0
  193. package/src/chips/display/pcf8576.js +42 -33
  194. package/src/chips/environmental/aht21.js +52 -37
  195. package/src/chips/environmental/bme280.js +85 -66
  196. package/src/chips/environmental/bme680.js +110 -87
  197. package/src/chips/gas/ens160.js +82 -64
  198. package/src/chips/gnss/neo6.js +438 -0
  199. package/src/chips/gyroscope/l3g4200d.js +366 -0
  200. package/src/chips/gyroscope/l3gd20h.js +314 -0
  201. package/src/chips/humidity/dht11.js +135 -0
  202. package/src/chips/imu/mpu6050.js +89 -66
  203. package/src/chips/imu/mpu9250.js +394 -0
  204. package/src/chips/imu/mpu9255.js +447 -0
  205. package/src/chips/io_expander/mcp23017.js +240 -157
  206. package/src/chips/io_expander/pcf8574.js +149 -124
  207. package/src/chips/io_expander/pcf8575.js +164 -135
  208. package/src/chips/io_expander/tpic6b595.js +242 -0
  209. package/src/chips/led/_NeoPixelRGBBase.js +204 -0
  210. package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
  211. package/src/chips/led/apa102.js +236 -0
  212. package/src/chips/led/sk6812rgbw.js +15 -149
  213. package/src/chips/led/ws2812b.js +14 -139
  214. package/src/chips/led/ws2814.js +49 -0
  215. package/src/chips/light/apds-9930.js +342 -0
  216. package/src/chips/light/apds9960.js +158 -123
  217. package/src/chips/magnetometer/as5600.js +102 -82
  218. package/src/chips/magnetometer/hmc5883l.js +222 -0
  219. package/src/chips/memory/_24aa02uid.js +32 -29
  220. package/src/chips/motor/drv8830.js +239 -0
  221. package/src/chips/other/mpr121.js +375 -0
  222. package/src/chips/power/ade7953.js +427 -0
  223. package/src/chips/power/ina219.js +45 -40
  224. package/src/chips/power/ina226.js +50 -45
  225. package/src/chips/power/ina3221.js +95 -76
  226. package/src/chips/pressure/bmp085.js +251 -0
  227. package/src/chips/pressure/bmp180.js +54 -37
  228. package/src/chips/pressure/bmp280.js +72 -53
  229. package/src/chips/pressure/bmp384.js +410 -0
  230. package/src/chips/pressure/bmp581.js +489 -0
  231. package/src/chips/pressure/lps22df.js +341 -0
  232. package/src/chips/pressure/lps28dfw.js +314 -0
  233. package/src/chips/pressure/lps33hw.js +255 -0
  234. package/src/chips/rfid/mfrc522.js +674 -0
  235. package/src/chips/rtc/ds3231.js +504 -0
  236. package/src/chips/rtc/pcf8523.js +539 -0
  237. package/src/chips/temperature/mcp9808.js +488 -0
  238. package/src/chips/temperature/tmp117.js +471 -0
  239. package/src/chips/tof/_vl53_base.js +160 -0
  240. package/src/chips/tof/vl53l0x.js +816 -0
  241. package/src/chips/tof/vl53l1x.js +816 -0
  242. package/src/connection/connection.js +111 -0
  243. package/src/connection/dhtxx.js +138 -0
  244. package/src/connection/dhtxx_mock.js +39 -0
  245. package/src/{transport → connection}/hx711.js +49 -22
  246. package/src/connection/hx711_mock.js +52 -0
  247. package/src/{transport → connection}/i2c.js +13 -9
  248. package/src/connection/i2c_mock.js +91 -0
  249. package/src/connection/input_pin.js +156 -0
  250. package/src/connection/neo6_mock.js +45 -0
  251. package/src/{transport → connection}/neopixel.js +23 -4
  252. package/src/connection/output_pin.js +41 -0
  253. package/src/connection/sipo.js +159 -0
  254. package/src/{transport → connection}/smbus.js +14 -10
  255. package/src/{transport → connection}/spi.js +12 -8
  256. package/src/connection/spi_mock.js +151 -0
  257. package/src/{transport → connection}/uart.js +33 -26
  258. package/examples/gnss/.gitkeep +0 -0
  259. package/examples/humidity/.gitkeep +0 -0
  260. package/examples/motor/.gitkeep +0 -0
  261. package/examples/temperature/.gitkeep +0 -0
  262. package/examples/tof/.gitkeep +0 -0
  263. package/src/chips/gnss/.gitkeep +0 -0
  264. package/src/chips/motor/.gitkeep +0 -0
  265. package/src/chips/temperature/.gitkeep +0 -0
  266. package/src/chips/tof/.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 transport for Node.js (wraps the `serialport` npm package).
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 de_pin_num and install the `onoff` package.
13
- * The GPIO is asserted high before each write and deasserted after drain.
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 UARTTransport {
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.de_pin_num=null] - GPIO line for RS-485 DE; null disables.
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
- 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
+ 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._de_pin_num !== null) {
48
+ if (this._de_gpio !== null) {
42
49
  try {
43
- const { Gpio } = require('onoff');
44
- this._de = new Gpio(this._de_pin_num, 'out');
45
- this._de.writeSync(0);
50
+ const { Default } = require('opengpio');
51
+ this._de = Default.output(this._de_gpio);
52
+ this._de.value = false;
46
53
  } catch (_) {
47
- // onoff unavailable — RS-485 DE toggling disabled.
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 write(data) {
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.writeSync(1);
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.writeSync(0);
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 read(n) {
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 writeRead(data, n) {
125
- await this.write(data);
126
- return this.read(n);
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.writeSync(0);
139
- this._de.unexport();
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 = { UARTTransport };
152
+ module.exports = { UARTConnection };
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes