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