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.
Files changed (241) 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 +42 -0
  21. package/examples/adc_dac/hx711/demo.js +35 -0
  22. package/examples/adc_dac/hx711/minimal.js +16 -0
  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 +43 -0
  27. package/examples/adc_dac/mcp4728/demo.js +41 -0
  28. package/examples/adc_dac/mcp4728/minimal.js +13 -0
  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 +35 -0
  42. package/examples/display/pcf8576/demo.js +48 -0
  43. package/examples/display/pcf8576/minimal.js +20 -0
  44. package/examples/environmental/aht21/complete.js +32 -0
  45. package/examples/environmental/aht21/demo.js +46 -0
  46. package/examples/environmental/aht21/minimal.js +12 -0
  47. package/examples/environmental/bme280/complete.js +37 -0
  48. package/examples/environmental/bme280/demo.js +67 -0
  49. package/examples/environmental/bme280/minimal.js +20 -0
  50. package/examples/environmental/bme680/complete.js +40 -0
  51. package/examples/environmental/bme680/demo.js +54 -0
  52. package/examples/environmental/bme680/minimal.js +21 -0
  53. package/examples/gas/ens160/complete.js +54 -0
  54. package/examples/gas/ens160/demo.js +54 -0
  55. package/examples/gas/ens160/minimal.js +29 -0
  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 +58 -0
  69. package/examples/imu/mpu6050/demo.js +37 -0
  70. package/examples/imu/mpu6050/minimal.js +14 -0
  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 +64 -0
  105. package/examples/light/apds9960/demo.js +34 -0
  106. package/examples/light/apds9960/minimal.js +12 -0
  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 +42 -0
  114. package/examples/memory/24aa02uid/demo.js +42 -0
  115. package/examples/memory/24aa02uid/minimal.js +12 -0
  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 +29 -22
  138. package/examples/pressure/bmp280/demo.js +42 -23
  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 +165 -0
  166. package/src/chips/adc_dac/mcp4725.js +48 -40
  167. package/src/chips/adc_dac/mcp4728.js +297 -0
  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 +232 -0
  173. package/src/chips/environmental/aht21.js +185 -0
  174. package/src/chips/environmental/bme280.js +385 -0
  175. package/src/chips/environmental/bme680.js +512 -0
  176. package/src/chips/gas/ens160.js +298 -0
  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 +289 -0
  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 +499 -0
  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 +182 -0
  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 +188 -206
  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/connection/hx711.js +132 -0
  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/display/.gitkeep +0 -0
  230. package/examples/environmental/.gitkeep +0 -0
  231. package/examples/gnss/.gitkeep +0 -0
  232. package/examples/humidity/.gitkeep +0 -0
  233. package/examples/imu/.gitkeep +0 -0
  234. package/examples/light/.gitkeep +0 -0
  235. package/examples/pressure/.gitkeep +0 -0
  236. package/src/chips/display/.gitkeep +0 -0
  237. package/src/chips/environmental/.gitkeep +0 -0
  238. package/src/chips/gnss/.gitkeep +0 -0
  239. package/src/chips/imu/.gitkeep +0 -0
  240. package/src/chips/light/.gitkeep +0 -0
  241. package/src/chips/pressure/.gitkeep +0 -0
@@ -4,44 +4,50 @@
4
4
  * PCF8574 8-bit quasi-bidirectional I/O port expander — minimal interface.
5
5
  *
6
6
  * Exposes all eight pins (P0–P7) as GPIO objects via the pin() factory.
7
- * Pin objects implement the onoff Gpio interface subset so they are
8
- * drop-in replacements for hardware GPIO in onoff-based code.
7
+ * Pin objects expose async read()/write() (not onoff's synchronous
8
+ * readSync()/writeSync() — Connection is async everywhere, so a
9
+ * synchronous pin proxy can no longer correctly return a value). Call
10
+ * pin.asGpio() for a synchronous opengpio-shaped facade when a pin needs
11
+ * to be passed somewhere a real opengpio Input/Output is expected (e.g.
12
+ * SiPoConnection's SER/SRCK/RCK, HX711Connection's DOUT/PD_SCK,
13
+ * UARTConnection's DE pin) — see _Pin.asGpio() below.
9
14
  *
10
- * Direction is implicit: writeSync(1) puts a pin in input mode (weak pull-up);
11
- * writeSync(0) drives it low. A shadow register tracks the output latch.
15
+ * Direction is implicit: write(1) puts a pin in input mode (weak pull-up);
16
+ * write(0) drives it low. A shadow register tracks the output latch.
12
17
  *
13
18
  * Initialises all pins to input mode (shadow = 0xFF) at construction.
14
19
  */
15
20
  class Pcf8574Minimal {
16
21
  /**
17
- * @param {object} transport - Configured I²C transport (write, read, writeRead).
22
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
18
23
  * @param {number} [addr=0x20] - 7-bit I²C device address.
19
24
  * PCF8574 default 0x20; PCF8574A default 0x38.
20
25
  */
21
- constructor(transport, addr = 0x20) {
22
- this._transport = transport;
23
- this._addr = addr;
24
- this._shadow = 0xFF;
25
- this._writePort(0xFF);
26
+ constructor(connection, addr = 0x20) {
27
+ this._conn = connection;
28
+ this._addr = addr;
29
+ this._shadow = 0xFF;
30
+ this._writePort(0xFF); // fire-and-forget: underlying I2C write is synchronous
26
31
  }
27
32
 
28
33
  // ------------------------------------------------------------------
29
34
  // Internal helpers
30
35
  // ------------------------------------------------------------------
31
36
 
32
- _writePort(mask) {
33
- this._transport.write(Buffer.from([mask & 0xFF]));
37
+ async _writePort(mask) {
38
+ await this._conn.write(Buffer.from([mask & 0xFF]));
34
39
  }
35
40
 
36
- _readPort() {
37
- return this._transport.read(1)[0];
41
+ async _readPort() {
42
+ const buf = await this._conn.read(1);
43
+ return buf[0];
38
44
  }
39
45
 
40
- _setPin(n, value) {
46
+ async _setPin(n, value) {
41
47
  if (value) this._shadow |= (1 << n);
42
48
  else this._shadow &= ~(1 << n);
43
49
  this._shadow &= 0xFF;
44
- this._writePort(this._shadow);
50
+ await this._writePort(this._shadow);
45
51
  }
46
52
 
47
53
  // ------------------------------------------------------------------
@@ -52,35 +58,35 @@ class Pcf8574Minimal {
52
58
  * Return a Pin proxy object for pin n (0–7).
53
59
  * @param {number} n - Pin index (0 = P0, 7 = P7).
54
60
  * @param {string} [direction='in'] - Initial direction: 'in' or 'out'.
55
- * @returns {_Pin} Pin proxy implementing the onoff Gpio subset.
61
+ * @returns {_Pin} Pin proxy.
56
62
  */
57
63
  pin(n, direction = 'in') {
58
64
  const p = new _Pin(this, n, direction);
59
- if (direction === 'out') this._setPin(n, 0);
60
- else this._setPin(n, 1);
65
+ this._setPin(n, direction === 'out' ? 0 : 1); // fire-and-forget
61
66
  return p;
62
67
  }
63
68
 
64
69
  /**
65
70
  * Read all 8 pins as a bitmask.
66
71
  * @param {number} [port=0] - Port index (ignored; PCF8574 has one port).
67
- * @returns {number} 8-bit bitmask of actual pin logic levels (bit 0 = P0).
72
+ * @returns {Promise<number>} 8-bit bitmask of actual pin logic levels (bit 0 = P0).
68
73
  */
69
- readPort(port = 0) { return this._readPort(); }
74
+ async readPort(port = 0) { return this._readPort(); }
70
75
 
71
76
  /**
72
77
  * Write all 8 pins at once and update the shadow register.
73
78
  * @param {number} [port=0] - Port index (ignored).
74
79
  * @param {number} mask - 8-bit output mask; 1 = input mode, 0 = drive low.
80
+ * @returns {Promise<void>}
75
81
  */
76
- writePort(port = 0, mask = 0xFF) {
82
+ async writePort(port = 0, mask = 0xFF) {
77
83
  this._shadow = mask & 0xFF;
78
- this._writePort(this._shadow);
84
+ await this._writePort(this._shadow);
79
85
  }
80
86
  }
81
87
 
82
88
  /**
83
- * GPIO proxy for a single PCF8574 pin — onoff Gpio interface subset.
89
+ * GPIO proxy for a single PCF8574 pin.
84
90
  *
85
91
  * Obtain via Pcf8574Minimal.pin(n). Do not instantiate directly.
86
92
  */
@@ -88,7 +94,7 @@ class _Pin {
88
94
  /**
89
95
  * @param {Pcf8574Minimal} chip - Parent driver instance.
90
96
  * @param {number} n - Pin index (0–7).
91
- * @param {string} direction - 'in' or 'out'.
97
+ * @param {string} direction - 'in' or 'out'; fixed for the pin's lifetime.
92
98
  */
93
99
  constructor(chip, n, direction) {
94
100
  this._chip = chip;
@@ -96,77 +102,100 @@ class _Pin {
96
102
  this._direction = direction;
97
103
  }
98
104
 
99
- /** @type {string} Current pin direction ('in' or 'out'). */
105
+ /** @type {string} Pin direction ('in' or 'out'). */
100
106
  get direction() { return this._direction; }
101
107
 
102
108
  /**
103
- * Read pin synchronously.
104
- * @returns {number} 0 or 1.
109
+ * Read pin.
110
+ * @returns {Promise<number>} 0 or 1.
105
111
  */
106
- readSync() {
107
- return (this._chip._readPort() >> this._n) & 1;
112
+ async read() {
113
+ return (await this._chip._readPort() >> this._n) & 1;
108
114
  }
109
115
 
110
116
  /**
111
- * Write pin synchronously.
117
+ * Write pin.
112
118
  * @param {number} value - 0 (drive low) or 1 (release to quasi-high input).
119
+ * @returns {Promise<void>}
113
120
  */
114
- writeSync(value) {
115
- this._chip._setPin(this._n, value ? 1 : 0);
116
- }
117
-
118
- /**
119
- * Read pin asynchronously.
120
- * @param {function} callback - Node-style callback(err, value).
121
- */
122
- read(callback) {
123
- try { callback(null, this.readSync()); } catch (e) { callback(e); }
121
+ async write(value) {
122
+ await this._chip._setPin(this._n, value ? 1 : 0);
124
123
  }
125
124
 
126
125
  /**
127
- * Write pin asynchronously.
128
- * @param {number} value - 0 or 1.
129
- * @param {function} callback - Node-style callback(err).
126
+ * Set pin direction.
127
+ * @param {string} direction - 'in' or 'out'.
128
+ * @returns {Promise<void>}
130
129
  */
131
- write(value, callback) {
132
- try { this.writeSync(value); callback(null); } catch (e) { callback(e); }
130
+ async setDirection(direction) {
131
+ this._direction = direction;
132
+ await this._chip._setPin(this._n, direction === 'in' ? 1 : 0);
133
133
  }
134
134
 
135
135
  /**
136
- * Set pin direction.
137
- * @param {string} direction - 'in' or 'out'.
138
- * @param {function} callback - Node-style callback(err).
136
+ * Return a synchronous opengpio-shaped Input/Output facade for this
137
+ * pin — a boolean `.value` getter/setter, fixed `direction`, and
138
+ * `stop()` — so it can be passed anywhere code expects a real
139
+ * opengpio GPIO line.
140
+ *
141
+ * Output-direction facades read back the shadow register directly
142
+ * (authoritative — nothing else can change an output pin's logical
143
+ * state) and write through fire-and-forget, matching this class's own
144
+ * fire-and-forget writes elsewhere. Input-direction facades are
145
+ * backed by a 5 ms background poll that refreshes a cached value —
146
+ * `.value` is therefore eventually consistent, not a live bus read on
147
+ * every access like this pin's own async read(). Call stop() to
148
+ * release the background poll when the facade is no longer needed.
149
+ *
150
+ * @returns {{direction: string, value: boolean, stop: function}} opengpio-shaped facade.
139
151
  */
140
- setDirection(direction, callback) {
141
- try {
142
- this._direction = direction;
143
- this._chip._setPin(this._n, direction === 'in' ? 1 : 0);
144
- if (callback) callback(null);
145
- } catch (e) { if (callback) callback(e); }
152
+ asGpio() {
153
+ const pin = this;
154
+ if (this._direction === 'out') {
155
+ return {
156
+ get direction() { return 'out'; },
157
+ get value() { return ((pin._chip._shadow >> pin._n) & 1) === 1; },
158
+ set value(v) { pin._chip._setPin(pin._n, v ? 1 : 0); }, // fire-and-forget
159
+ stop() {},
160
+ };
161
+ }
162
+ let cached = false;
163
+ const timer = setInterval(async () => {
164
+ cached = (await pin.read()) === 1;
165
+ }, 5);
166
+ return {
167
+ get direction() { return 'in'; },
168
+ get value() { return cached; },
169
+ set value(_v) { throw new Error('cannot set value on an input pin'); },
170
+ stop() { clearInterval(timer); },
171
+ };
146
172
  }
147
173
 
148
174
  /** Release the pin (no-op; shadow state preserved). */
149
- unexport() {}
175
+ stop() {}
150
176
  }
151
177
 
152
178
  /**
153
179
  * PCF8574 full interface — extends Pcf8574Minimal with interrupt support.
154
180
  *
155
- * Adds configureInterrupt() to attach a callback to the chip's INT line
156
- * and clearInterrupt() to return the changed-pin bitmask.
157
- * Pin objects gain watch() / unwatch() for per-pin interrupt handlers.
181
+ * Adds onInterrupt()/offInterrupt() to subscribe to the chip's INT line and
182
+ * pollInterrupt() to read and clear the changed-pin bitmask. Pin objects
183
+ * gain watch()/unwatch() for per-pin interrupt handlers.
158
184
  */
159
185
  class Pcf8574Full extends Pcf8574Minimal {
160
186
  /**
161
- * @param {object} transport - Configured I²C transport.
187
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
162
188
  * @param {number} [addr=0x20] - 7-bit I²C device address.
163
189
  */
164
- constructor(transport, addr = 0x20) {
165
- super(transport, addr);
166
- this._prev = this._readPort();
190
+ constructor(connection, addr = 0x20) {
191
+ super(connection, addr);
192
+ this._prev = 0xFF;
193
+ this._readPort().then(v => { this._prev = v; }); // fire-and-forget refresh
167
194
  this._callback = null;
195
+ this._watchers = {}; // { [pin]: { handler, trigger, lastState } }
196
+ this._intPin = null;
168
197
  this._pollTimer = null;
169
- this._watchers = {};
198
+ this._edgeHandler = () => { this._handleEdge(); };
170
199
  }
171
200
 
172
201
  /**
@@ -177,55 +206,63 @@ class Pcf8574Full extends Pcf8574Minimal {
177
206
  */
178
207
  pin(n, direction = 'in') {
179
208
  const p = new _FullPin(this, n, direction);
180
- if (direction === 'out') this._setPin(n, 0);
181
- else this._setPin(n, 1);
209
+ this._setPin(n, direction === 'out' ? 0 : 1); // fire-and-forget
182
210
  return p;
183
211
  }
184
212
 
185
213
  /**
186
- * Attach a callback to the chip's INT output.
214
+ * Subscribe to INT assertions.
187
215
  *
188
- * On Linux, uses a 5 ms polling interval; pass intGpioPath to the
189
- * sysfs value file (e.g. '/sys/class/gpio/gpio5/value') for edge-
190
- * based delivery via epoll. Pass null to use polling.
216
+ * Wires options.intPin (or connection.intPin if omitted) to deliver
217
+ * edges. If neither is available, falls back to a 5 ms polling loop
218
+ * reading the port directly (no InputPin involved).
191
219
  *
192
- * @param {string|null} intGpioPath - Sysfs GPIO value file path, or null for polling.
193
- * @param {function} callback - Called with changed bitmask on any input change.
220
+ * @param {function} callback - Called with the changed-pin bitmask on any input change.
221
+ * @param {object} [options]
222
+ * @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
223
+ * @returns {Promise<void>}
194
224
  */
195
- configureInterrupt(intGpioPath, callback) {
225
+ async onInterrupt(callback, options = {}) {
196
226
  this._callback = callback;
197
- if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
198
- if (intGpioPath) {
199
- try {
200
- const fs = require('fs');
201
- const ep = require('epoll').Epoll;
202
- const fd = fs.openSync(intGpioPath, 'r');
203
- const poll = new ep((err, fd2) => {
204
- fs.readSync(fd2, Buffer.alloc(1), 0, 1, 0);
205
- const changed = this.clearInterrupt();
206
- if (changed) this._dispatch(changed);
207
- });
208
- poll.add(fd, ep.EPOLLPRI);
209
- } catch (_) {
210
- this._startPolling();
211
- }
227
+ const pin = options.intPin ?? this._conn.intPin ?? null;
228
+ if (pin) {
229
+ this._intPin = pin;
230
+ await pin.onEdge(this._edgeHandler, 'falling');
212
231
  } else {
213
232
  this._startPolling();
214
233
  }
215
234
  }
216
235
 
236
+ /**
237
+ * Unsubscribe and stop delivery.
238
+ * @returns {Promise<void>}
239
+ */
240
+ async offInterrupt() {
241
+ this._callback = null;
242
+ if (this._intPin) {
243
+ await this._intPin.offEdge(this._edgeHandler);
244
+ this._intPin = null;
245
+ }
246
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
247
+ }
248
+
217
249
  _startPolling() {
218
- this._pollTimer = setInterval(() => {
219
- const changed = this.clearInterrupt();
220
- if (changed) this._dispatch(changed);
221
- }, 5);
250
+ if (this._pollTimer) return;
251
+ this._pollTimer = setInterval(() => { this._handleEdge(); }, 5);
222
252
  }
223
253
 
224
- _dispatch(changed) {
254
+ async _handleEdge() {
255
+ const changed = await this.pollInterrupt();
256
+ if (!changed) return;
225
257
  if (this._callback) this._callback(changed);
226
- for (const [n, handlers] of Object.entries(this._watchers)) {
227
- if ((changed >> n) & 1) {
228
- handlers.forEach(h => h(null, this.readSync ? undefined : 0));
258
+ for (const [n, w] of Object.entries(this._watchers)) {
259
+ if (!((changed >> n) & 1)) continue;
260
+ const current = (await this._readPort() >> n) & 1;
261
+ const rising = current === 1 && w.lastState === 0;
262
+ const falling = current === 0 && w.lastState === 1;
263
+ w.lastState = current;
264
+ if (w.trigger === 'change' || (w.trigger === 'rising' && rising) || (w.trigger === 'falling' && falling)) {
265
+ w.handler(this.pin(Number(n)));
229
266
  }
230
267
  }
231
268
  }
@@ -233,12 +270,12 @@ class Pcf8574Full extends Pcf8574Minimal {
233
270
  /**
234
271
  * Read current pin states and return bitmask of pins that changed.
235
272
  * Also clears the chip's INT line.
236
- * @returns {number} 8-bit changed-pin bitmask.
273
+ * @returns {Promise<number>} 8-bit changed-pin bitmask.
237
274
  */
238
- clearInterrupt() {
239
- const current = this._readPort();
275
+ async pollInterrupt() {
276
+ const current = await this._readPort();
240
277
  const changed = (current ^ this._prev) & 0xFF;
241
- this._prev = current;
278
+ this._prev = current;
242
279
  return changed;
243
280
  }
244
281
  }
@@ -257,33 +294,21 @@ class _FullPin extends _Pin {
257
294
  }
258
295
 
259
296
  /**
260
- * Register a handler for pin state changes.
261
- * Requires configureInterrupt() to have been called on the driver.
262
- * @param {function} handler - Called with (err, value) when pin changes.
263
- */
264
- watch(handler) {
265
- const n = this._n;
266
- if (!this._chip._watchers[n]) this._chip._watchers[n] = [];
267
- const current = this.readSync();
268
- this._chip._watchers[n].push((err) => {
269
- handler(err, this.readSync());
270
- });
271
- }
272
-
273
- /**
274
- * Remove a previously registered handler.
275
- * @param {function} handler - The handler to remove.
297
+ * Subscribe to this pin's edge events.
298
+ *
299
+ * At most one handler per pin at a time; a second watch() call replaces
300
+ * the first. Call the chip's onInterrupt() first to arm delivery.
301
+ *
302
+ * @param {function} handler - Called with this pin when its state matches trigger.
303
+ * @param {string} [trigger='change'] - 'rising', 'falling', or 'change'.
276
304
  */
277
- unwatch(handler) {
278
- const n = this._n;
279
- if (this._chip._watchers[n]) {
280
- this._chip._watchers[n] = this._chip._watchers[n].filter(h => h !== handler);
281
- }
305
+ watch(handler, trigger = 'change') {
306
+ this._chip._watchers[this._n] = { handler, trigger, lastState: 1 };
282
307
  }
283
308
 
284
- /** Remove all handlers for this pin. */
285
- unwatchAll() {
286
- this._chip._watchers[this._n] = [];
309
+ /** Unsubscribe this pin's handler. No-op if not registered. */
310
+ unwatch() {
311
+ delete this._chip._watchers[this._n];
287
312
  }
288
313
 
289
314
  /**