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
@@ -26,7 +26,12 @@ const REG_OLATB = 0x15;
26
26
  * MCP23017 16-bit bidirectional I/O port expander — minimal interface.
27
27
  *
28
28
  * Provides access to 16 GPIO pins (GPA0–GPA7 and GPB0–GPB7) via a pin()
29
- * factory. Each returned pin implements the onoff Gpio interface subset.
29
+ * factory. Each returned pin exposes async read()/write() (not onoff's
30
+ * synchronous readSync()/writeSync() — Connection is async everywhere, so
31
+ * a synchronous pin proxy can no longer correctly return a value). Call
32
+ * pin.asGpio() for a synchronous opengpio-shaped facade when a pin needs
33
+ * to be passed somewhere a real opengpio Input/Output is expected — see
34
+ * _Pin.asGpio() below.
30
35
  *
31
36
  * At construction, all pins initialise as inputs except GPA7 and GPB7
32
37
  * which are output-only on the hardware and are forced to output mode.
@@ -37,14 +42,15 @@ const REG_OLATB = 0x15;
37
42
  */
38
43
  class Mcp23017Minimal {
39
44
  /**
40
- * @param {object} transport - I²C transport with write, read, writeRead methods.
45
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
41
46
  * @param {number} [addr=0x20] - 7-bit I²C address (default 0x20, range 0x20–0x27).
42
47
  */
43
- constructor(transport, addr = 0x20) {
44
- this._transport = transport;
45
- this._addr = addr;
46
- this._shadow = [0, 0];
48
+ constructor(connection, addr = 0x20) {
49
+ this._conn = connection;
50
+ this._addr = addr;
51
+ this._shadow = [0, 0];
47
52
 
53
+ // fire-and-forget: underlying I2C write is synchronous
48
54
  this._writeReg(REG_OLATA, 0x00);
49
55
  this._writeReg(REG_OLATB, 0x00);
50
56
  this._writeReg(REG_IODIRA, 0x7F);
@@ -55,28 +61,29 @@ class Mcp23017Minimal {
55
61
  this._writeReg(REG_GPPUB, 0x00);
56
62
  }
57
63
 
58
- _writeReg(reg, value) {
59
- this._transport.write(Buffer.from([reg, value & 0xFF]));
64
+ async _writeReg(reg, value) {
65
+ await this._conn.write(Buffer.from([reg, value & 0xFF]));
60
66
  }
61
67
 
62
- _readReg(reg) {
63
- return this._transport.writeRead(Buffer.from([reg]), 1)[0];
68
+ async _readReg(reg) {
69
+ const buf = await this._conn.writeRead(Buffer.from([reg]), 1);
70
+ return buf[0];
64
71
  }
65
72
 
66
- _writePort(port, mask) {
67
- this._writeReg(REG_OLATA + port, mask & 0xFF);
73
+ async _writePort(port, mask) {
74
+ await this._writeReg(REG_OLATA + port, mask & 0xFF);
68
75
  }
69
76
 
70
- _readPort(port) {
77
+ async _readPort(port) {
71
78
  return this._readReg(REG_GPIOA + port);
72
79
  }
73
80
 
74
- _setPin(n, value) {
81
+ async _setPin(n, value) {
75
82
  const port = n >> 3;
76
83
  const bit = n & 7;
77
84
  if (value) this._shadow[port] |= (1 << bit);
78
85
  else this._shadow[port] &= ~(1 << bit);
79
- this._writePort(port, this._shadow[port]);
86
+ await this._writePort(port, this._shadow[port]);
80
87
  }
81
88
 
82
89
  /**
@@ -89,7 +96,7 @@ class Mcp23017Minimal {
89
96
  *
90
97
  * @param {number} n - Pin index (0–15).
91
98
  * @param {string} [direction='in'] - Initial direction: 'in' or 'out'.
92
- * @returns {_Pin} Pin proxy implementing the onoff Gpio subset.
99
+ * @returns {_Pin} Pin proxy.
93
100
  */
94
101
  pin(n, direction = 'in') {
95
102
  return new _Pin(this, n, direction);
@@ -99,9 +106,9 @@ class Mcp23017Minimal {
99
106
  * Read all 8 pins of a port as a bitmask.
100
107
  *
101
108
  * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
102
- * @returns {number} 8-bit bitmask (bit 0 = pin 0 of the port).
109
+ * @returns {Promise<number>} 8-bit bitmask (bit 0 = pin 0 of the port).
103
110
  */
104
- readPort(port = 0) {
111
+ async readPort(port = 0) {
105
112
  return this._readPort(port);
106
113
  }
107
114
 
@@ -111,24 +118,26 @@ class Mcp23017Minimal {
111
118
  *
112
119
  * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
113
120
  * @param {number} mask - 8-bit output mask.
121
+ * @returns {Promise<void>}
114
122
  */
115
- writePort(port = 0, mask = 0x00) {
123
+ async writePort(port = 0, mask = 0x00) {
116
124
  this._shadow[port] = mask & 0xFF;
117
- this._writePort(port, this._shadow[port]);
125
+ await this._writePort(port, this._shadow[port]);
118
126
  }
119
127
 
120
128
  /**
121
129
  * Configure the direction (IODIR) of a full port.
122
130
  * @param {number} port - 0 = PORTA (IODIRA), 1 = PORTB (IODIRB).
123
131
  * @param {number} mask - 8-bit mask; bit = 1 → input, 0 → output.
132
+ * @returns {Promise<void>}
124
133
  */
125
- configureDirection(port, mask) {
126
- this._writeReg(REG_IODIRA + (port & 1), mask & 0xFF);
134
+ async configureDirection(port, mask) {
135
+ await this._writeReg(REG_IODIRA + (port & 1), mask & 0xFF);
127
136
  }
128
137
  }
129
138
 
130
139
  /**
131
- * GPIO proxy for a single MCP23017 pin — onoff Gpio interface subset.
140
+ * GPIO proxy for a single MCP23017 pin.
132
141
  *
133
142
  * Obtain via Mcp23017Minimal.pin(n). Do not instantiate directly.
134
143
  */
@@ -136,7 +145,7 @@ class _Pin {
136
145
  /**
137
146
  * @param {Mcp23017Minimal} chip - Parent driver instance.
138
147
  * @param {number} n - Pin index (0–15).
139
- * @param {string} direction - 'in' or 'out'.
148
+ * @param {string} direction - 'in' or 'out'; fixed for the pin's lifetime.
140
149
  */
141
150
  constructor(chip, n, direction) {
142
151
  this._chip = chip;
@@ -144,78 +153,104 @@ class _Pin {
144
153
  this._direction = direction;
145
154
  }
146
155
 
147
- /** @type {string} Current pin direction ('in' or 'out'). */
156
+ /** @type {string} Pin direction ('in' or 'out'). */
148
157
  get direction() { return this._direction; }
149
158
 
150
159
  /**
151
- * Read pin synchronously.
152
- * @returns {number} 0 or 1.
160
+ * Read pin.
161
+ * @returns {Promise<number>} 0 or 1.
153
162
  */
154
- readSync() {
155
- return (this._chip._readPort(this._n >> 3) >> (this._n & 7)) & 1;
163
+ async read() {
164
+ return (await this._chip._readPort(this._n >> 3) >> (this._n & 7)) & 1;
156
165
  }
157
166
 
158
167
  /**
159
- * Write pin synchronously.
168
+ * Write pin.
160
169
  * @param {number} value - 0 or 1.
170
+ * @returns {Promise<void>}
161
171
  */
162
- writeSync(value) {
163
- this._chip._setPin(this._n, value ? 1 : 0);
172
+ async write(value) {
173
+ await this._chip._setPin(this._n, value ? 1 : 0);
164
174
  }
165
175
 
166
176
  /**
167
- * Read pin asynchronously.
168
- * @param {function} callback - Node-style callback(err, value).
169
- */
170
- read(callback) {
171
- try { callback(null, this.readSync()); } catch (e) { callback(e); }
172
- }
173
-
174
- /**
175
- * Write pin asynchronously.
176
- * @param {number} value - 0 or 1.
177
- * @param {function} callback - Node-style callback(err).
177
+ * Set pin direction.
178
+ * @param {string} direction - 'in' or 'out'.
179
+ * @returns {Promise<void>}
178
180
  */
179
- write(value, callback) {
180
- try { this.writeSync(value); callback(null); } catch (e) { callback(e); }
181
+ async setDirection(direction) {
182
+ this._direction = direction;
181
183
  }
182
184
 
183
185
  /**
184
- * Set pin direction.
185
- * @param {string} direction - 'in' or 'out'.
186
- * @param {function} callback - Node-style callback(err).
186
+ * Return a synchronous opengpio-shaped Input/Output facade for this
187
+ * pin — a boolean `.value` getter/setter, fixed `direction`, and
188
+ * `stop()` — so it can be passed anywhere code expects a real
189
+ * opengpio GPIO line.
190
+ *
191
+ * Output-direction facades read back the shadow register directly
192
+ * (authoritative) and write through fire-and-forget. Input-direction
193
+ * facades are backed by a 5 ms background poll that refreshes a
194
+ * cached value — `.value` is therefore eventually consistent, not a
195
+ * live bus read on every access like this pin's own async read().
196
+ * Call stop() to release the background poll when done.
197
+ *
198
+ * @returns {{direction: string, value: boolean, stop: function}} opengpio-shaped facade.
187
199
  */
188
- setDirection(direction, callback) {
189
- try {
190
- this._direction = direction;
191
- if (callback) callback(null);
192
- } catch (e) { if (callback) callback(e); }
200
+ asGpio() {
201
+ const pin = this;
202
+ if (this._direction === 'out') {
203
+ const port = this._n >> 3;
204
+ const bit = this._n & 7;
205
+ return {
206
+ get direction() { return 'out'; },
207
+ get value() { return ((pin._chip._shadow[port] >> bit) & 1) === 1; },
208
+ set value(v) { pin._chip._setPin(pin._n, v ? 1 : 0); }, // fire-and-forget
209
+ stop() {},
210
+ };
211
+ }
212
+ let cached = false;
213
+ const timer = setInterval(async () => {
214
+ cached = (await pin.read()) === 1;
215
+ }, 5);
216
+ return {
217
+ get direction() { return 'in'; },
218
+ get value() { return cached; },
219
+ set value(_v) { throw new Error('cannot set value on an input pin'); },
220
+ stop() { clearInterval(timer); },
221
+ };
193
222
  }
194
223
 
195
224
  /** Release the pin (no-op; shadow state preserved). */
196
- unexport() {}
225
+ stop() {}
197
226
  }
198
227
 
199
228
  /**
200
229
  * MCP23017 full interface — extends Minimal with interrupt support,
201
230
  * per-pin pull-ups, and interrupt-on-change configuration.
202
231
  *
203
- * configureInterrupt() attaches a callback to the chip's INT line;
204
- * clearInterrupt() returns the changed-pin bitmask for a port and
205
- * clears the hardware interrupt. Per-pin watch() / unwatch() handlers
206
- * are also available.
232
+ * Level 3: the chip has two independent INT lines (INTA/INTB), one per
233
+ * port. onInterrupt()/offInterrupt() subscribe/unsubscribe both ports at
234
+ * once (delivered via one shared InputPin — wire IOCON.MIRROR or external
235
+ * wiring so a single physical line carries both); onInterruptPort()/
236
+ * offInterruptPort() target one port with its own InputPin. pollInterrupt()
237
+ * reads and clears INTF; readCapture() reads INTCAP (the latched pin state)
238
+ * separately. Per-pin watch()/unwatch() are also available.
207
239
  */
208
240
  class Mcp23017Full extends Mcp23017Minimal {
209
241
  /**
210
- * @param {object} transport - I²C transport with write, read, writeRead methods.
242
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
211
243
  * @param {number} [addr=0x20] - 7-bit I²C address.
212
244
  */
213
- constructor(transport, addr = 0x20) {
214
- super(transport, addr);
215
- this._prev = [0, 0];
216
- this._callback = null;
217
- this._pollTimer = null;
218
- this._watchers = {};
245
+ constructor(connection, addr = 0x20) {
246
+ super(connection, addr);
247
+ this._callbackBoth = null;
248
+ this._callbackPort = [null, null];
249
+ this._intPinUsed = [null, null];
250
+ this._pollTimer = [null, null];
251
+ this._watchers = [{}, {}]; // per port: { [bit]: { handler, trigger, lastState } }
252
+ this._edgeHandlerA = () => { this._handleEdge(0); };
253
+ this._edgeHandlerB = () => { this._handleEdge(1); };
219
254
  }
220
255
 
221
256
  /**
@@ -237,9 +272,10 @@ class Mcp23017Full extends Mcp23017Minimal {
237
272
  *
238
273
  * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
239
274
  * @param {number} mask - 8-bit mask: 1 = enable 100 kΩ pull-up.
275
+ * @returns {Promise<void>}
240
276
  */
241
- configurePullup(port = 0, mask = 0x00) {
242
- this._writeReg(REG_GPPUA + port, mask & 0xFF);
277
+ async configurePullup(port = 0, mask = 0x00) {
278
+ await this._writeReg(REG_GPPUA + port, mask & 0xFF);
243
279
  }
244
280
 
245
281
  /**
@@ -247,105 +283,156 @@ class Mcp23017Full extends Mcp23017Minimal {
247
283
  *
248
284
  * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
249
285
  * @param {number} mask - 8-bit mask: 1 = invert GPIO read.
286
+ * @returns {Promise<void>}
250
287
  */
251
- configurePolarity(port = 0, mask = 0x00) {
252
- this._writeReg(REG_IPOLA + port, mask & 0xFF);
288
+ async configurePolarity(port = 0, mask = 0x00) {
289
+ await this._writeReg(REG_IPOLA + port, mask & 0xFF);
253
290
  }
254
291
 
255
292
  /**
256
- * Attach an interrupt callback to a port's INT line.
257
- *
258
- * When intGpioPath is provided (sysfs GPIO value file path), edge
259
- * detection via epoll is used. Otherwise a 5 ms polling loop drives
260
- * delivery. The callback receives the changed-pin bitmask for the port.
261
- *
262
- * @param {number} [port=0] - Port index: 0 = PORTA (INTA), 1 = PORTB (INTB).
263
- * @param {string|null} intGpioPath - Sysfs GPIO value file for edge delivery, or null for polling.
264
- * @param {function} callback - Called with (changedMask) on any input change.
293
+ * Set DEFVAL register for default-compare interrupt mode.
294
+ * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
295
+ * @param {number} mask - 8-bit default compare value.
296
+ * @returns {Promise<void>}
265
297
  */
266
- configureInterrupt(port = 0, intGpioPath = null, callback) {
267
- this._callback = callback;
268
- if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
269
-
270
- this._writeReg(REG_GPINTENA + port, 0xFF);
271
- this._writeReg(REG_INTCONA + port, 0x00);
272
-
273
- if (intGpioPath) {
274
- try {
275
- const fs = require('fs');
276
- const ep = require('epoll').Epoll;
277
- const fd = fs.openSync(intGpioPath, 'r');
278
- const poll = new ep((err, fd2) => {
279
- fs.readSync(fd2, Buffer.alloc(1), 0, 1, 0);
280
- const changed = this.clearInterrupt(port);
281
- if (changed) this._dispatch(port, changed);
282
- });
283
- poll.add(fd, ep.EPOLLPRI);
284
- } catch (_) {
285
- this._startPolling();
286
- }
287
- } else {
288
- this._startPolling();
289
- }
298
+ async setDefaultValue(port = 0, mask = 0x00) {
299
+ await this._writeReg(REG_DEFVALA + (port & 1), mask & 0xFF);
290
300
  }
291
301
 
292
- _startPolling() {
293
- this._pollTimer = setInterval(() => {
294
- const changed = this.clearInterrupt(0) | (this.clearInterrupt(1) << 8);
295
- if (changed) this._dispatch(0, changed & 0xFF);
296
- if (changed >> 8) this._dispatch(1, (changed >> 8) & 0xFF);
297
- }, 5);
302
+ async _armPort(port, intPin) {
303
+ await this._writeReg(REG_INTCONA + port, 0x00); // interrupt-on-change mode
304
+ await this._writeReg(REG_GPINTENA + port, 0xFF);
305
+ this._intPinUsed[port] = intPin;
306
+ if (this._pollTimer[port]) { clearInterval(this._pollTimer[port]); this._pollTimer[port] = null; }
307
+ if (intPin) {
308
+ await intPin.onEdge(port === 0 ? this._edgeHandlerA : this._edgeHandlerB, 'falling');
309
+ } else {
310
+ this._pollTimer[port] = setInterval(() => { this._handleEdge(port); }, 5);
311
+ }
298
312
  }
299
313
 
300
- _dispatch(port, changed) {
301
- if (this._callback) this._callback(port, changed);
302
- for (const [n, handlers] of Object.entries(this._watchers)) {
303
- const pinPort = Number(n) >> 3;
304
- if (pinPort === port && ((changed >> (Number(n) & 7)) & 1)) {
305
- handlers.forEach(h => h(null, this._chip ? this._chip._readPort(port) : 0));
306
- }
307
- }
314
+ /**
315
+ * Subscribe to INT assertions on both ports.
316
+ *
317
+ * The common case: both INT lines share one physical GPIO, either via
318
+ * IOCON.MIRROR (options.mirror = true) or external wiring. Wires
319
+ * options.intPin (or connection.intPin if omitted) to poll both ports
320
+ * on every edge. Falls back to two independent 5 ms polling loops if no
321
+ * InputPin is available.
322
+ *
323
+ * @param {function} callback - Called with (port, status) on any input change.
324
+ * @param {object} [options]
325
+ * @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
326
+ * @param {boolean} [options.mirror=false] - Set IOCON.MIRROR so either port's interrupt activates both INTA and INTB.
327
+ * @returns {Promise<void>}
328
+ */
329
+ async onInterrupt(callback, options = {}) {
330
+ this._callbackBoth = callback;
331
+ const iocon = await this._readReg(REG_IOCON);
332
+ await this._writeReg(REG_IOCON, options.mirror ? (iocon | (1 << 6)) : iocon);
333
+ const pin = options.intPin ?? this._conn.intPin ?? null;
334
+ await this._armPort(0, pin);
335
+ await this._armPort(1, pin);
308
336
  }
309
337
 
310
338
  /**
311
- * Read and clear the interrupt for a port, returning the changed-pin bitmask.
339
+ * Subscribe to INT assertions on a single port.
312
340
  *
313
- * Also updates the per-port previous-state tracker used by the polling loop.
341
+ * Use this (with a dedicated InputPin per call) when INTA and INTB are
342
+ * wired to two separate GPIOs.
314
343
  *
315
- * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
316
- * @returns {number} 8-bit changed-pin bitmask for the port.
344
+ * @param {number} port - Port index: 0 = PORTA (INTA), 1 = PORTB (INTB).
345
+ * @param {function} callback - Called with (status) on any input change.
346
+ * @param {object} [options]
347
+ * @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
348
+ * @returns {Promise<void>}
317
349
  */
318
- clearInterrupt(port = 0) {
319
- const captured = this._readReg(REG_INTCAPA + port);
320
- const current = this._readReg(REG_GPIOA + port);
321
- const changed = (current ^ this._prev[port]) & 0xFF;
322
- this._prev[port] = current;
323
- return changed;
350
+ async onInterruptPort(port, callback, options = {}) {
351
+ port &= 1;
352
+ this._callbackPort[port] = callback;
353
+ await this._armPort(port, options.intPin ?? this._conn.intPin ?? null);
354
+ }
355
+
356
+ /**
357
+ * Unsubscribe and stop delivery on both ports.
358
+ * @returns {Promise<void>}
359
+ */
360
+ async offInterrupt() {
361
+ await this.offInterruptPort(0);
362
+ await this.offInterruptPort(1);
363
+ this._callbackBoth = null;
364
+ }
365
+
366
+ /**
367
+ * Unsubscribe and stop delivery on a single port.
368
+ * @param {number} port - Port index: 0 = PORTA, 1 = PORTB.
369
+ * @returns {Promise<void>}
370
+ */
371
+ async offInterruptPort(port) {
372
+ port &= 1;
373
+ await this._writeReg(REG_GPINTENA + port, 0x00);
374
+ if (this._intPinUsed[port]) {
375
+ await this._intPinUsed[port].offEdge(port === 0 ? this._edgeHandlerA : this._edgeHandlerB);
376
+ this._intPinUsed[port] = null;
377
+ }
378
+ if (this._pollTimer[port]) { clearInterval(this._pollTimer[port]); this._pollTimer[port] = null; }
379
+ this._callbackPort[port] = null;
380
+ }
381
+
382
+ async _handleEdge(port) {
383
+ const status = await this.pollInterrupt(port);
384
+ if (!status) return;
385
+ if (this._callbackBoth) this._callbackBoth(port, status);
386
+ if (this._callbackPort[port]) this._callbackPort[port](status);
387
+
388
+ const raw = await this._readPort(port);
389
+ for (const [bit, w] of Object.entries(this._watchers[port])) {
390
+ if (!((status >> bit) & 1)) continue;
391
+ const current = (raw >> bit) & 1;
392
+ const rising = current === 1 && w.lastState === 0;
393
+ const falling = current === 0 && w.lastState === 1;
394
+ w.lastState = current;
395
+ if (w.trigger === 'change' || (w.trigger === 'rising' && rising) || (w.trigger === 'falling' && falling)) {
396
+ w.handler(this.pin(port * 8 + Number(bit)));
397
+ }
398
+ }
324
399
  }
325
400
 
326
401
  /**
327
- * Read interrupt flags without clearing the interrupt.
402
+ * Read & clear interrupt status; returns the raw INTF flag register.
403
+ *
404
+ * Reads INTFA/INTFB (which pins triggered) then INTCAPA/INTCAPB (which
405
+ * clears the interrupt latch and re-arms it). Note that reading either
406
+ * INTCAP or GPIO clears the interrupt condition on this chip — call
407
+ * pollInterrupt() or readCapture() per event, not both.
328
408
  *
329
409
  * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
330
- * @returns {number} 8-bit interrupt-flag bitmask.
410
+ * @returns {Promise<number>} 8-bit interrupt flag mask.
331
411
  */
332
- readInterruptFlags(port = 0) {
333
- return this._readReg(REG_INTFA + port);
412
+ async pollInterrupt(port = 0) {
413
+ port &= 1;
414
+ const flags = await this._readReg(REG_INTFA + port);
415
+ await this._readReg(REG_INTCAPA + port); // clears and re-arms the interrupt; value discarded
416
+ return flags;
334
417
  }
335
418
 
336
419
  /**
337
- * Disable interrupt generation for a port.
420
+ * Read INTCAP: the port state latched at the moment of the interrupt.
421
+ *
422
+ * Also clears the interrupt latch (see pollInterrupt() doc) — call this
423
+ * instead of pollInterrupt() when the captured pin state is wanted
424
+ * rather than the flag register.
338
425
  *
339
426
  * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
427
+ * @returns {Promise<number>} 8-bit captured port bitmask at the moment of interrupt.
340
428
  */
341
- stopInterrupt(port = 0) {
342
- this._writeReg(REG_GPINTENA + port, 0x00);
343
- if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
429
+ async readCapture(port = 0) {
430
+ return this._readReg(REG_INTCAPA + (port & 1));
344
431
  }
345
432
  }
346
433
 
347
434
  /**
348
- * Full GPIO proxy — adds watch/unwatch for interrupt-driven input.
435
+ * Full GPIO proxy — adds watch() for interrupt-driven input.
349
436
  */
350
437
  class _FullPin extends _Pin {
351
438
  /**
@@ -358,30 +445,26 @@ class _FullPin extends _Pin {
358
445
  }
359
446
 
360
447
  /**
361
- * Register a handler for pin state changes.
362
- * Requires configureInterrupt() to have been called on the driver.
363
- * @param {function} handler - Called on pin change.
364
- */
365
- watch(handler) {
366
- const n = this._n;
367
- if (!this._chip._watchers[n]) this._chip._watchers[n] = [];
368
- this._chip._watchers[n].push(handler);
369
- }
370
-
371
- /**
372
- * Remove a previously registered handler.
373
- * @param {function} handler - The handler to remove.
448
+ * Subscribe to this pin's edge events.
449
+ *
450
+ * At most one handler per pin at a time; a second watch() call replaces
451
+ * the first. Call the chip's onInterrupt()/onInterruptPort() (for this
452
+ * pin's port) first to arm delivery.
453
+ *
454
+ * @param {function} handler - Called with this pin when its state matches trigger.
455
+ * @param {string} [trigger='change'] - 'rising', 'falling', or 'change'.
374
456
  */
375
- unwatch(handler) {
376
- const n = this._n;
377
- if (this._chip._watchers[n]) {
378
- this._chip._watchers[n] = this._chip._watchers[n].filter(h => h !== handler);
379
- }
457
+ watch(handler, trigger = 'change') {
458
+ const port = this._n >> 3;
459
+ const bit = this._n & 7;
460
+ this._chip._watchers[port][bit] = { handler, trigger, lastState: 1 };
380
461
  }
381
462
 
382
- /** Remove all handlers for this pin. */
383
- unwatchAll() {
384
- this._chip._watchers[this._n] = [];
463
+ /** Unsubscribe this pin's handler. No-op if not registered. */
464
+ unwatch() {
465
+ const port = this._n >> 3;
466
+ const bit = this._n & 7;
467
+ delete this._chip._watchers[port][bit];
385
468
  }
386
469
 
387
470
  /**
@@ -393,4 +476,4 @@ class _FullPin extends _Pin {
393
476
  }
394
477
  }
395
478
 
396
- module.exports = { Mcp23017Minimal, Mcp23017Full };
479
+ module.exports = { Mcp23017Minimal, Mcp23017Full };