periph 1.1.0 → 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.
Files changed (231) hide show
  1. package/README.md +54 -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/other/mpr121/complete.js +50 -0
  117. package/examples/other/mpr121/demo.js +30 -0
  118. package/examples/other/mpr121/minimal.js +17 -0
  119. package/examples/power/ade7953/complete.js +44 -0
  120. package/examples/power/ade7953/demo.js +28 -0
  121. package/examples/power/ade7953/minimal.js +15 -0
  122. package/examples/power/ina219/complete.js +20 -18
  123. package/examples/power/ina219/demo.js +15 -13
  124. package/examples/power/ina219/minimal.js +8 -8
  125. package/examples/power/ina226/complete.js +24 -22
  126. package/examples/power/ina226/demo.js +22 -20
  127. package/examples/power/ina226/minimal.js +5 -5
  128. package/examples/power/ina3221/complete.js +51 -49
  129. package/examples/power/ina3221/demo.js +13 -13
  130. package/examples/power/ina3221/minimal.js +8 -8
  131. package/examples/pressure/bmp085/complete.js +29 -0
  132. package/examples/pressure/bmp085/demo.js +34 -0
  133. package/examples/pressure/bmp085/minimal.js +19 -0
  134. package/examples/pressure/bmp180/complete.js +20 -17
  135. package/examples/pressure/bmp180/demo.js +24 -22
  136. package/examples/pressure/bmp180/minimal.js +13 -11
  137. package/examples/pressure/bmp280/complete.js +23 -20
  138. package/examples/pressure/bmp280/demo.js +44 -41
  139. package/examples/pressure/bmp280/minimal.js +13 -11
  140. package/examples/pressure/bmp384/complete.js +29 -0
  141. package/examples/pressure/bmp384/demo.js +40 -0
  142. package/examples/pressure/bmp384/minimal.js +18 -0
  143. package/examples/pressure/bmp581/complete.js +34 -0
  144. package/examples/pressure/bmp581/demo.js +56 -0
  145. package/examples/pressure/bmp581/minimal.js +19 -0
  146. package/examples/pressure/lps22df/complete.js +50 -0
  147. package/examples/pressure/lps22df/demo.js +41 -0
  148. package/examples/pressure/lps22df/minimal.js +19 -0
  149. package/examples/pressure/lps28dfw/complete.js +37 -0
  150. package/examples/pressure/lps28dfw/demo.js +33 -0
  151. package/examples/pressure/lps28dfw/minimal.js +19 -0
  152. package/examples/pressure/lps33hw/complete.js +43 -0
  153. package/examples/pressure/lps33hw/demo.js +43 -0
  154. package/examples/pressure/lps33hw/minimal.js +19 -0
  155. package/examples/rfid/mfrc522/complete.js +44 -0
  156. package/examples/rfid/mfrc522/demo.js +70 -0
  157. package/examples/rfid/mfrc522/minimal.js +17 -0
  158. package/package.json +5 -1
  159. package/src/chips/accelerometer/adxl345.js +482 -0
  160. package/src/chips/accelerometer/adxl362.js +594 -0
  161. package/src/chips/adc_dac/ad7705.js +345 -0
  162. package/src/chips/adc_dac/ad7706.js +331 -0
  163. package/src/chips/adc_dac/hx710a.js +184 -0
  164. package/src/chips/adc_dac/hx710b.js +186 -0
  165. package/src/chips/adc_dac/hx711.js +15 -15
  166. package/src/chips/adc_dac/mcp4725.js +48 -40
  167. package/src/chips/adc_dac/mcp4728.js +57 -44
  168. package/src/chips/adc_dac/pcf8591.js +196 -0
  169. package/src/chips/comms/mcp2515.js +659 -0
  170. package/src/chips/comms/rda5807m.js +393 -0
  171. package/src/chips/comms/rfm9x.js +590 -0
  172. package/src/chips/display/pcf8576.js +42 -33
  173. package/src/chips/environmental/aht21.js +52 -37
  174. package/src/chips/environmental/bme280.js +85 -66
  175. package/src/chips/environmental/bme680.js +110 -87
  176. package/src/chips/gas/ens160.js +82 -64
  177. package/src/chips/gnss/neo6.js +438 -0
  178. package/src/chips/gyroscope/l3g4200d.js +366 -0
  179. package/src/chips/gyroscope/l3gd20h.js +314 -0
  180. package/src/chips/humidity/dht11.js +135 -0
  181. package/src/chips/imu/mpu6050.js +89 -66
  182. package/src/chips/imu/mpu9250.js +394 -0
  183. package/src/chips/imu/mpu9255.js +447 -0
  184. package/src/chips/io_expander/mcp23017.js +240 -157
  185. package/src/chips/io_expander/pcf8574.js +149 -124
  186. package/src/chips/io_expander/pcf8575.js +164 -135
  187. package/src/chips/io_expander/tpic6b595.js +242 -0
  188. package/src/chips/led/_NeoPixelRGBBase.js +204 -0
  189. package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
  190. package/src/chips/led/apa102.js +236 -0
  191. package/src/chips/led/sk6812rgbw.js +15 -149
  192. package/src/chips/led/ws2812b.js +14 -139
  193. package/src/chips/led/ws2814.js +49 -0
  194. package/src/chips/light/apds-9930.js +342 -0
  195. package/src/chips/light/apds9960.js +158 -123
  196. package/src/chips/magnetometer/as5600.js +102 -82
  197. package/src/chips/magnetometer/hmc5883l.js +222 -0
  198. package/src/chips/memory/_24aa02uid.js +32 -29
  199. package/src/chips/other/mpr121.js +375 -0
  200. package/src/chips/power/ade7953.js +427 -0
  201. package/src/chips/power/ina219.js +45 -40
  202. package/src/chips/power/ina226.js +50 -45
  203. package/src/chips/power/ina3221.js +95 -76
  204. package/src/chips/pressure/bmp085.js +251 -0
  205. package/src/chips/pressure/bmp180.js +54 -37
  206. package/src/chips/pressure/bmp280.js +72 -53
  207. package/src/chips/pressure/bmp384.js +410 -0
  208. package/src/chips/pressure/bmp581.js +489 -0
  209. package/src/chips/pressure/lps22df.js +341 -0
  210. package/src/chips/pressure/lps28dfw.js +314 -0
  211. package/src/chips/pressure/lps33hw.js +255 -0
  212. package/src/chips/rfid/mfrc522.js +674 -0
  213. package/src/connection/connection.js +111 -0
  214. package/src/connection/dhtxx.js +138 -0
  215. package/src/connection/dhtxx_mock.js +39 -0
  216. package/src/{transport → connection}/hx711.js +49 -22
  217. package/src/connection/hx711_mock.js +52 -0
  218. package/src/{transport → connection}/i2c.js +13 -9
  219. package/src/connection/i2c_mock.js +91 -0
  220. package/src/connection/input_pin.js +156 -0
  221. package/src/connection/neo6_mock.js +45 -0
  222. package/src/{transport → connection}/neopixel.js +23 -4
  223. package/src/connection/output_pin.js +41 -0
  224. package/src/connection/sipo.js +159 -0
  225. package/src/{transport → connection}/smbus.js +14 -10
  226. package/src/{transport → connection}/spi.js +12 -8
  227. package/src/connection/spi_mock.js +151 -0
  228. package/src/{transport → connection}/uart.js +33 -26
  229. package/examples/gnss/.gitkeep +0 -0
  230. package/examples/humidity/.gitkeep +0 -0
  231. package/src/chips/gnss/.gitkeep +0 -0
@@ -0,0 +1,111 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Abstract connection interface shared by all chip drivers.
5
+ *
6
+ * A connection instance represents one device on a bus, plus its optional
7
+ * INT pin (edge notifications from the chip's INT line), EN pin (hardware
8
+ * enable/power control), and a software enable gate. Subclasses wrap a
9
+ * platform-specific bus (i2c-bus, spi-device, serialport, etc.) and a device
10
+ * address or CS line, implementing the protected _read/_write/_writeRead
11
+ * hooks; this base class gates every call through the enabled flag so chip
12
+ * drivers never need to check it themselves.
13
+ *
14
+ * All methods are async — required so UART (inherently Promise-based via
15
+ * `serialport`) shares one contract with I2C/SPI/SMBus (synchronous
16
+ * underlying calls, wrapped in async methods for a uniform interface).
17
+ */
18
+ class Connection {
19
+ /**
20
+ * @param {InputPin|null} [intPin=null] - Optional InputPin for INT-line delivery.
21
+ * @param {OutputPin|null} [enPin=null] - Optional OutputPin for hardware enable/power control.
22
+ */
23
+ constructor(intPin = null, enPin = null) {
24
+ this._intPin = intPin;
25
+ this._enPin = enPin;
26
+ this._enabled = true;
27
+ }
28
+
29
+ /** Resume bus access; drives the hardware EN pin high if wired.
30
+ * @returns {Promise<void>}
31
+ */
32
+ async enable() {
33
+ this._enabled = true;
34
+ if (this._enPin) await this._enPin.set(true);
35
+ }
36
+
37
+ /**
38
+ * Gate all subsequent bus reads and writes; drives EN pin low if wired.
39
+ *
40
+ * Reads silently return zero-filled buffers; writes silently become no-ops.
41
+ * @returns {Promise<void>}
42
+ */
43
+ async disable() {
44
+ this._enabled = false;
45
+ if (this._enPin) await this._enPin.set(false);
46
+ }
47
+
48
+ /** @returns {boolean} The current software-gate state. */
49
+ isEnabled() { return this._enabled; }
50
+
51
+ /** @returns {InputPin|null} The INT-line InputPin, or null if none is wired. */
52
+ get intPin() { return this._intPin; }
53
+
54
+ /** @returns {OutputPin|null} The EN-pin OutputPin, or null if none is wired. */
55
+ get enPin() { return this._enPin; }
56
+
57
+ /**
58
+ * Send bytes to the device, unless disabled.
59
+ * @param {Buffer|Uint8Array} data - Bytes to send.
60
+ * @returns {Promise<void>}
61
+ */
62
+ async write(data) {
63
+ if (!this._enabled) return;
64
+ return this._write(data);
65
+ }
66
+
67
+ /**
68
+ * Read bytes from the device, unless disabled.
69
+ * @param {number} length - Number of bytes to read.
70
+ * @returns {Promise<Buffer>} Data received from the device, or a zero-filled buffer if disabled.
71
+ */
72
+ async read(length) {
73
+ if (!this._enabled) return Buffer.alloc(length);
74
+ return this._read(length);
75
+ }
76
+
77
+ /**
78
+ * Write then read without releasing the bus between phases, unless disabled.
79
+ * @param {Buffer|Uint8Array} data - Command/address bytes to send.
80
+ * @param {number} n - Number of bytes to read back.
81
+ * @returns {Promise<Buffer>} Data received from the device, or a zero-filled buffer if disabled.
82
+ */
83
+ async writeRead(data, n) {
84
+ if (!this._enabled) return Buffer.alloc(n);
85
+ return this._writeRead(data, n);
86
+ }
87
+
88
+ /** Subclass hook: send bytes to the device.
89
+ * @abstract
90
+ */
91
+ async _write(_data) { throw new Error('abstract'); }
92
+
93
+ /** Subclass hook: read bytes from the device.
94
+ * @abstract
95
+ */
96
+ async _read(_length) { throw new Error('abstract'); }
97
+
98
+ /** Subclass hook: write then read without releasing the bus.
99
+ * @abstract
100
+ */
101
+ async _writeRead(_data, _n) { throw new Error('abstract'); }
102
+
103
+ /**
104
+ * Close the underlying bus/device. Default no-op; subclasses that hold
105
+ * an OS resource (file descriptor, serial port) override this.
106
+ * @returns {Promise<void>}
107
+ */
108
+ async close() {}
109
+ }
110
+
111
+ module.exports = { Connection };
@@ -0,0 +1,138 @@
1
+ 'use strict';
2
+
3
+ const Gpio = require('onoff').Gpio;
4
+
5
+ /**
6
+ * Error thrown when the DHTxx connection cannot complete a read.
7
+ *
8
+ * `kind` is one of "timeout" (sensor did not respond) or "framing"
9
+ * (fewer than 40 bit pulses received).
10
+ */
11
+ class DHTxxError extends Error {
12
+ constructor(kind, detail) {
13
+ super(detail ? `${kind}: ${detail}` : kind);
14
+ this.kind = kind;
15
+ }
16
+ }
17
+
18
+ /**
19
+ * DHTxx single-wire connection for Node.js (wraps onoff Gpio).
20
+ *
21
+ * Implements the host side of the DHT11 / DHT22 single-wire protocol: a
22
+ * bidirectional DATA line, externally pulled up to VCC via a 4.7 kΩ resistor.
23
+ * The connection switches the pin's direction as needed via the onoff
24
+ * `reconfigureDirection: true` option. Timing uses `process.hrtime.bigint()`
25
+ * with busy-wait loops; V8's non-deterministic GC pauses make this the least
26
+ * timing-reliable of the Linux targets.
27
+ *
28
+ * The open-drain two-pin variant is not available on Node.js — `onoff` does
29
+ * not expose open-drain drive modes.
30
+ *
31
+ * This is a custom protocol with no generic byte read/write, so it does not
32
+ * extend the shared Connection base — it carries its own enabled flag and
33
+ * enPin instead.
34
+ */
35
+ class DHTxxConnection {
36
+ /**
37
+ * @param {number} dataPin - GPIO pin number for the DATA line.
38
+ * @param {import('./output_pin').OutputPin|null} [enPin=null] - Optional EN-pin OutputPin.
39
+ */
40
+ constructor(dataPin, enPin = null) {
41
+ this._pin = new Gpio(dataPin, 'in', 'both', { reconfigureDirection: true });
42
+ this.enPin = enPin;
43
+ this._enabled = true;
44
+ }
45
+
46
+ /** Resume reads; drives the hardware EN pin high if wired.
47
+ * @returns {Promise<void>}
48
+ */
49
+ async enable() {
50
+ this._enabled = true;
51
+ if (this.enPin) await this.enPin.set(true);
52
+ }
53
+
54
+ /** Gate read(); drives the hardware EN pin low if wired.
55
+ * @returns {Promise<void>}
56
+ */
57
+ async disable() {
58
+ this._enabled = false;
59
+ if (this.enPin) await this.enPin.set(false);
60
+ }
61
+
62
+ /** @returns {boolean} The current software-gate state. */
63
+ isEnabled() { return this._enabled; }
64
+
65
+ /**
66
+ * Execute the full DHTxx transaction and return the raw 5-byte frame.
67
+ *
68
+ * Returns a zero-filled buffer without touching the bus if this
69
+ * connection is disabled.
70
+ *
71
+ * @returns {Buffer} 5 bytes — [hum_int, hum_dec, temp_int, temp_dec, checksum].
72
+ * @throws {DHTxxError} On timeout or framing error.
73
+ */
74
+ read() {
75
+ if (!this._enabled) return Buffer.alloc(5);
76
+
77
+ const startLowMs = 20;
78
+ const responseTimeoutUs = 200;
79
+ const bitTimeoutUs = 200;
80
+ const bitThresholdUs = 40;
81
+
82
+ this._driveLow();
83
+ const until = Date.now() + startLowMs;
84
+ while (Date.now() < until) { /* busy-wait */ }
85
+ this._releaseBus();
86
+
87
+ let elapsed = this._measurePulse(0, responseTimeoutUs);
88
+ if (elapsed < 0) throw new DHTxxError('timeout', `sensor did not pull DATA low within ${responseTimeoutUs} us`);
89
+ elapsed = this._measurePulse(1, responseTimeoutUs);
90
+ if (elapsed < 0) throw new DHTxxError('timeout', 'sensor did not release after response low');
91
+
92
+ const frame = Buffer.alloc(5);
93
+ for (let byteIdx = 0; byteIdx < 5; byteIdx++) {
94
+ let byte = 0;
95
+ for (let bitIdx = 0; bitIdx < 8; bitIdx++) {
96
+ elapsed = this._measurePulse(0, bitTimeoutUs);
97
+ if (elapsed < 0) throw new DHTxxError('framing', `bit ${byteIdx * 8 + bitIdx} start-low missing`);
98
+ elapsed = this._measurePulse(1, bitTimeoutUs);
99
+ if (elapsed < 0) throw new DHTxxError('framing', `bit ${byteIdx * 8 + bitIdx} high-pulse missing`);
100
+ byte = (byte << 1) | (elapsed > bitThresholdUs ? 1 : 0);
101
+ }
102
+ frame[byteIdx] = byte;
103
+ }
104
+ return frame;
105
+ }
106
+
107
+ _driveLow() {
108
+ this._pin.unexport();
109
+ this._pin = new Gpio(this._pin.gpio, 'out');
110
+ this._pin.writeSync(0);
111
+ }
112
+
113
+ _releaseBus() {
114
+ this._pin.unexport();
115
+ this._pin = new Gpio(this._pin.gpio, 'in', 'both', { reconfigureDirection: true });
116
+ }
117
+
118
+ _measurePulse(level, timeoutUs) {
119
+ const deadline = process.hrtime.bigint() + BigInt(timeoutUs) * 1000n;
120
+ while (this._pin.readSync() !== level) {
121
+ if (process.hrtime.bigint() >= deadline) return -1;
122
+ }
123
+ const pulseStart = process.hrtime.bigint();
124
+ while (this._pin.readSync() === level) {
125
+ if (process.hrtime.bigint() >= deadline) return -1;
126
+ }
127
+ return Number((process.hrtime.bigint() - pulseStart) / 1000n);
128
+ }
129
+
130
+ /**
131
+ * Release the GPIO pin.
132
+ */
133
+ close() {
134
+ try { this._pin.unexport(); } catch (_) { /* already closed */ }
135
+ }
136
+ }
137
+
138
+ module.exports = { DHTxxConnection, DHTxxError };
@@ -0,0 +1,39 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * In-memory fake DHTxx connection for unit tests — no hardware, no GPIO.
5
+ *
6
+ * DHTxx has no register map and no framing to fake at this level: the chip
7
+ * driver only ever calls `read()` and gets back a raw 5-byte frame (or an
8
+ * exception propagating straight through, e.g. a real connection's
9
+ * timeout/framing `DHTxxError`). Preload frames with `queueRead`; preload an
10
+ * error to throw instead with `queueError`. Each `read()` call pops the next
11
+ * queued item (falling back to a 5 zero-byte buffer if the queue is empty,
12
+ * matching the real connections' disabled-state return value).
13
+ */
14
+ class DHTxxConnectionMock {
15
+ constructor() {
16
+ this._queue = [];
17
+ }
18
+
19
+ /** Queue a raw 5-byte frame to be returned by the next read(). */
20
+ queueRead(frame) {
21
+ this._queue.push({ frame: Buffer.from(frame) });
22
+ }
23
+
24
+ /** Queue an Error instance to be thrown by the next read(). */
25
+ queueError(err) {
26
+ this._queue.push({ err });
27
+ }
28
+
29
+ read() {
30
+ if (this._queue.length === 0) return Buffer.alloc(5);
31
+ const item = this._queue.shift();
32
+ if (item.err) throw item.err;
33
+ return item.frame;
34
+ }
35
+
36
+ close() {}
37
+ }
38
+
39
+ module.exports = { DHTxxConnectionMock };
@@ -1,29 +1,53 @@
1
1
  'use strict';
2
2
 
3
- const Gpio = require('onoff').Gpio;
4
-
5
3
  /**
6
- * HX711 GPIO bit-bang transport for Node.js (wraps onoff Gpio).
4
+ * HX711 GPIO bit-bang connection for Node.js (wraps opengpio Input/Output).
7
5
  *
8
6
  * Implements the 2-wire bit-bang protocol used exclusively by the HX711
9
7
  * 24-bit ADC. DOUT is sampled on each falling edge of PD_SCK; the pulse
10
8
  * count selects the channel and gain for the next conversion.
11
9
  *
12
- * The DOUT poll loop uses a synchronous spin (readSync) which is acceptable
10
+ * The DOUT poll loop uses a synchronous spin (`.value`) which is acceptable
13
11
  * for short waits during the clock cycle itself. The blocking wait before the
14
12
  * cycle uses setImmediate yielding to avoid monopolising the event loop.
13
+ *
14
+ * This is a custom protocol with no generic byte read/write, so it does not
15
+ * extend the shared Connection base — it carries its own enabled flag and
16
+ * enPin instead.
15
17
  */
16
- class HX711Transport {
18
+ class HX711Connection {
17
19
  /**
18
- * @param {object} dout - onoff Gpio instance configured as 'in'.
19
- * @param {object} pd_sck - onoff Gpio instance configured as 'out'.
20
+ * @param {object} dout - opengpio Input instance (boolean `.value`).
21
+ * @param {object} pdSck - opengpio Output instance (boolean `.value`).
22
+ * @param {import('./output_pin').OutputPin|null} [enPin=null] - Optional EN-pin OutputPin.
20
23
  */
21
- constructor(dout, pd_sck) {
24
+ constructor(dout, pdSck, enPin = null) {
22
25
  this._dout = dout;
23
- this._sck = pd_sck;
24
- this._sck.writeSync(0);
26
+ this._sck = pdSck;
27
+ this.enPin = enPin;
28
+ this._enabled = true;
29
+ this._sck.value = false;
30
+ }
31
+
32
+ /** Resume conversions; drives the hardware EN pin high if wired.
33
+ * @returns {Promise<void>}
34
+ */
35
+ async enable() {
36
+ this._enabled = true;
37
+ if (this.enPin) await this.enPin.set(true);
25
38
  }
26
39
 
40
+ /** Gate readRaw(); drives the hardware EN pin low if wired.
41
+ * @returns {Promise<void>}
42
+ */
43
+ async disable() {
44
+ this._enabled = false;
45
+ if (this.enPin) await this.enPin.set(false);
46
+ }
47
+
48
+ /** @returns {boolean} The current software-gate state. */
49
+ isEnabled() { return this._enabled; }
50
+
27
51
  /**
28
52
  * Return true if a conversion result is available (DOUT is LOW).
29
53
  *
@@ -32,7 +56,7 @@ class HX711Transport {
32
56
  * @returns {boolean} True when DOUT is LOW (data ready).
33
57
  */
34
58
  isReady() {
35
- return this._dout.readSync() === 0;
59
+ return this._dout.value === false;
36
60
  }
37
61
 
38
62
  /**
@@ -44,15 +68,18 @@ class HX711Transport {
44
68
  * channel and gain for the next conversion:
45
69
  * 25 → Channel A Gain 128, 26 → Channel B Gain 32, 27 → Channel A Gain 64.
46
70
  *
71
+ * Returns 0 without touching the bus if this connection is disabled.
72
+ *
47
73
  * @param {number} numPulses - Number of PD_SCK pulses (must be 25, 26, or 27).
48
- * @returns {number} Signed 24-bit ADC value.
74
+ * @returns {number} Signed 24-bit ADC value, or 0 if disabled.
49
75
  * @throws {Error} If numPulses is not 25, 26, or 27, or DOUT stays HIGH for >1 s.
50
76
  */
51
77
  readRaw(numPulses = 25) {
78
+ if (!this._enabled) return 0;
52
79
  if (numPulses !== 25 && numPulses !== 26 && numPulses !== 27)
53
80
  throw new Error('numPulses must be 25, 26, or 27');
54
81
  const deadline = Date.now() + 1000;
55
- while (this._dout.readSync() !== 0) {
82
+ while (this._dout.value !== false) {
56
83
  if (Date.now() >= deadline)
57
84
  throw new Error('HX711 DOUT did not go low within 1 second');
58
85
  const end = Date.now() + 1;
@@ -61,11 +88,11 @@ class HX711Transport {
61
88
  const endOf = (us) => process.hrtime.bigint() + BigInt(us * 1000);
62
89
  let raw = 0;
63
90
  for (let i = 0; i < numPulses; i++) {
64
- this._sck.writeSync(1);
91
+ this._sck.value = true;
65
92
  let t = endOf(1); while (process.hrtime.bigint() < t) {}
66
- this._sck.writeSync(0);
93
+ this._sck.value = false;
67
94
  t = endOf(1); while (process.hrtime.bigint() < t) {}
68
- raw = (raw << 1) | this._dout.readSync();
95
+ raw = (raw << 1) | (this._dout.value ? 1 : 0);
69
96
  }
70
97
  raw >>>= numPulses - 24;
71
98
  if (raw >= 0x800000) raw -= 0x1000000;
@@ -78,7 +105,7 @@ class HX711Transport {
78
105
  * Uses a busy-spin for the delay since Node.js has no µs sleep.
79
106
  */
80
107
  powerDown() {
81
- this._sck.writeSync(1);
108
+ this._sck.value = true;
82
109
  const end = Date.now() + 1; // 1 ms >> 60 µs, safe margin
83
110
  while (Date.now() < end) {}
84
111
  }
@@ -90,16 +117,16 @@ class HX711Transport {
90
117
  * conversion after power-up must be discarded.
91
118
  */
92
119
  powerUp() {
93
- this._sck.writeSync(0);
120
+ this._sck.value = false;
94
121
  }
95
122
 
96
123
  /**
97
- * Release both GPIO pins. Must be called when the transport is no longer needed.
124
+ * Release both GPIO pins. Must be called when the connection is no longer needed.
98
125
  */
99
126
  close() {
100
- this._dout.unexport();
101
- this._sck.unexport();
127
+ this._dout.stop();
128
+ this._sck.stop();
102
129
  }
103
130
  }
104
131
 
105
- module.exports = { HX711Transport };
132
+ module.exports = { HX711Connection };
@@ -0,0 +1,52 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * In-memory fake HX711 connection for unit tests — no hardware, no GPIO.
5
+ *
6
+ * HX711 has no register map; each conversion is instead selected by the
7
+ * pulse count (25/26/27) sent to `readRaw`. Preload signed 24-bit conversion
8
+ * results with `queueRead`; each `readRaw` call pops the next one (0 if the
9
+ * queue is empty) and, matching every real connection's contract, validates
10
+ * `numPulses` is one of {25, 26, 27}, throwing an `Error` otherwise. Every
11
+ * call's pulse count is appended to `reads` so tests can assert which
12
+ * channel/gain a driver call actually requested (e.g. that `setGain` drives
13
+ * the right pulse count). `powerDown`/`powerUp` calls are logged to
14
+ * `powerCalls`.
15
+ */
16
+ class HX711ConnectionMock {
17
+ constructor() {
18
+ this._queue = [];
19
+ this.reads = [];
20
+ this.powerCalls = [];
21
+ this.ready = true;
22
+ }
23
+
24
+ /** Queue a signed 24-bit value to be returned by the next readRaw() call. */
25
+ queueRead(value) {
26
+ this._queue.push(value);
27
+ }
28
+
29
+ isReady() {
30
+ return this.ready;
31
+ }
32
+
33
+ readRaw(numPulses) {
34
+ if (numPulses !== 25 && numPulses !== 26 && numPulses !== 27)
35
+ throw new Error('numPulses must be 25, 26, or 27');
36
+ this.reads.push(numPulses);
37
+ if (this._queue.length > 0) return this._queue.shift();
38
+ return 0;
39
+ }
40
+
41
+ powerDown() {
42
+ this.powerCalls.push('down');
43
+ }
44
+
45
+ powerUp() {
46
+ this.powerCalls.push('up');
47
+ }
48
+
49
+ close() {}
50
+ }
51
+
52
+ module.exports = { HX711ConnectionMock };
@@ -1,19 +1,23 @@
1
1
  'use strict';
2
2
 
3
3
  const i2c = require('i2c-bus');
4
+ const { Connection } = require('./connection');
4
5
 
5
6
  /**
6
- * I²C transport for Node.js (wraps i2c-bus, uses /dev/i2c-N on Linux).
7
+ * I²C connection for Node.js (wraps i2c-bus, uses /dev/i2c-N on Linux).
7
8
  *
8
9
  * One instance represents one device on the bus; the bus is opened
9
10
  * synchronously at construction. Call close() to release the bus when done.
10
11
  */
11
- class I2CTransport {
12
+ class I2CConnection extends Connection {
12
13
  /**
13
14
  * @param {number} busNumber - I²C bus number (opens /dev/i2c-{busNumber}).
14
15
  * @param {number} addr - 7-bit device address.
16
+ * @param {import('./input_pin').InputPin|null} [intPin=null] - Optional INT-line InputPin.
17
+ * @param {import('./output_pin').OutputPin|null} [enPin=null] - Optional EN-pin OutputPin.
15
18
  */
16
- constructor(busNumber, addr) {
19
+ constructor(busNumber, addr, intPin = null, enPin = null) {
20
+ super(intPin, enPin);
17
21
  this._bus = i2c.openSync(busNumber);
18
22
  this._addr = addr;
19
23
  }
@@ -22,7 +26,7 @@ class I2CTransport {
22
26
  * Send bytes to the device.
23
27
  * @param {Buffer|Uint8Array} data - Bytes to write.
24
28
  */
25
- write(data) {
29
+ async _write(data) {
26
30
  this._bus.i2cWriteSync(this._addr, data.length, data);
27
31
  }
28
32
 
@@ -31,7 +35,7 @@ class I2CTransport {
31
35
  * @param {number} n - Number of bytes to read.
32
36
  * @returns {Buffer} Data received from the device.
33
37
  */
34
- read(n) {
38
+ async _read(n) {
35
39
  const buf = Buffer.alloc(n);
36
40
  this._bus.i2cReadSync(this._addr, n, buf);
37
41
  return buf;
@@ -50,18 +54,18 @@ class I2CTransport {
50
54
  * @param {number} n - Number of bytes to read back.
51
55
  * @returns {Buffer} Data received from the device.
52
56
  */
53
- writeRead(data, n) {
57
+ async _writeRead(data, n) {
54
58
  const buf = Buffer.alloc(n);
55
59
  this._bus.readI2cBlockSync(this._addr, data[0], n, buf);
56
60
  return buf;
57
61
  }
58
62
 
59
63
  /**
60
- * Close the I²C bus. Must be called when the transport is no longer needed.
64
+ * Close the I²C bus. Must be called when the connection is no longer needed.
61
65
  */
62
- close() {
66
+ async close() {
63
67
  this._bus.closeSync();
64
68
  }
65
69
  }
66
70
 
67
- module.exports = { I2CTransport };
71
+ module.exports = { I2CConnection };
@@ -0,0 +1,91 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * In-memory fake I2C connection for unit tests — no hardware, no bus.
5
+ *
6
+ * Supports the two access patterns chip drivers in this repo use:
7
+ *
8
+ * - Register-addressed reads (`writeRead(Buffer.from([reg]), n)`): backed by
9
+ * a byte-addressable `registers` map. Preload it with `setRegister` before
10
+ * constructing the chip.
11
+ * - Plain streamed reads (`read(n)`, no register address): backed by a FIFO
12
+ * queue. Preload responses with `queueRead`; each `read(n)` call pops the
13
+ * next one. Falls back to `n` zero bytes if the queue is empty.
14
+ *
15
+ * Every `write()` call (register writes and plain command writes alike) is
16
+ * appended to `writes` for assertions, and 2+ byte writes are also applied
17
+ * to `registers` so a later `writeRead` sees them.
18
+ *
19
+ * The register address is assumed to be a single byte (the first byte sent)
20
+ * unless `setAddressWidth` is called with a wider value — needed by chips
21
+ * such as the ADE7953 that address registers with 2 bytes.
22
+ */
23
+ class I2CConnectionMock {
24
+ constructor() {
25
+ this.registers = new Map();
26
+ this.writes = [];
27
+ this._readQueue = [];
28
+ this._addressWidth = 1;
29
+ }
30
+
31
+ /** Set the register address width in bytes (default 1). */
32
+ setAddressWidth(addressWidth) {
33
+ this._addressWidth = addressWidth;
34
+ }
35
+
36
+ /** Preload consecutive register bytes starting at `reg`. */
37
+ setRegister(reg, values) {
38
+ for (let i = 0; i < values.length; i++) {
39
+ this.registers.set(reg + i, values[i]);
40
+ }
41
+ }
42
+
43
+ /** Queue bytes to be returned by the next plain `read(n)` call. */
44
+ queueRead(data) {
45
+ this._readQueue.push(Buffer.from(data));
46
+ }
47
+
48
+ _readAddress(buf) {
49
+ let reg = 0;
50
+ for (let i = 0; i < this._addressWidth; i++) {
51
+ reg = (reg << 8) | buf[i];
52
+ }
53
+ return reg;
54
+ }
55
+
56
+ async write(data) {
57
+ const buf = Buffer.from(data);
58
+ this.writes.push(buf);
59
+ if (buf.length > this._addressWidth) {
60
+ const reg = this._readAddress(buf);
61
+ for (let i = this._addressWidth; i < buf.length; i++) {
62
+ this.registers.set(reg + i - this._addressWidth, buf[i]);
63
+ }
64
+ }
65
+ }
66
+
67
+ async read(n) {
68
+ if (this._readQueue.length > 0) {
69
+ const front = this._readQueue.shift();
70
+ const out = Buffer.alloc(n);
71
+ front.copy(out, 0, 0, Math.min(n, front.length));
72
+ return out;
73
+ }
74
+ return Buffer.alloc(n);
75
+ }
76
+
77
+ async writeRead(data, n) {
78
+ const buf = Buffer.from(data);
79
+ this.writes.push(buf);
80
+ const reg = this._readAddress(buf);
81
+ const out = Buffer.alloc(n);
82
+ for (let i = 0; i < n; i++) {
83
+ out[i] = this.registers.get(reg + i) || 0;
84
+ }
85
+ return out;
86
+ }
87
+
88
+ async close() {}
89
+ }
90
+
91
+ module.exports = { I2CConnectionMock };