periph 1.3.2 → 1.4.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 (114) hide show
  1. package/README.md +27 -0
  2. package/examples/accelerometer/bma180/complete.js +83 -0
  3. package/examples/accelerometer/bma180/demo.js +47 -0
  4. package/examples/accelerometer/bma180/minimal.js +21 -0
  5. package/examples/accelerometer/fxos8700/complete.js +111 -0
  6. package/examples/accelerometer/fxos8700/demo.js +80 -0
  7. package/examples/accelerometer/fxos8700/minimal.js +20 -0
  8. package/examples/accelerometer/lis3dh/complete.js +123 -0
  9. package/examples/accelerometer/lis3dh/demo.js +99 -0
  10. package/examples/accelerometer/lis3dh/minimal.js +19 -0
  11. package/examples/accelerometer/lsm303/complete.js +82 -0
  12. package/examples/accelerometer/lsm303/demo.js +96 -0
  13. package/examples/accelerometer/lsm303/minimal.js +23 -0
  14. package/examples/accelerometer/mma8451q/complete.js +96 -0
  15. package/examples/accelerometer/mma8451q/demo.js +120 -0
  16. package/examples/accelerometer/mma8451q/minimal.js +20 -0
  17. package/examples/adc_dac/ad5243/complete.js +52 -0
  18. package/examples/adc_dac/ad5243/demo.js +50 -0
  19. package/examples/adc_dac/ad5243/minimal.js +23 -0
  20. package/examples/adc_dac/ad5248/complete.js +49 -0
  21. package/examples/adc_dac/ad5248/demo.js +42 -0
  22. package/examples/adc_dac/ad5248/minimal.js +21 -0
  23. package/examples/adc_dac/ads1013/complete.js +51 -0
  24. package/examples/adc_dac/ads1013/demo.js +51 -0
  25. package/examples/adc_dac/ads1013/minimal.js +21 -0
  26. package/examples/adc_dac/ads1014/complete.js +88 -0
  27. package/examples/adc_dac/ads1014/demo.js +52 -0
  28. package/examples/adc_dac/ads1014/minimal.js +21 -0
  29. package/examples/adc_dac/ads1113/complete.js +47 -0
  30. package/examples/adc_dac/ads1113/demo.js +39 -0
  31. package/examples/adc_dac/ads1113/minimal.js +20 -0
  32. package/examples/adc_dac/ads1114/complete.js +77 -0
  33. package/examples/adc_dac/ads1114/demo.js +47 -0
  34. package/examples/adc_dac/ads1114/minimal.js +20 -0
  35. package/examples/adc_dac/ads1115/complete.js +82 -0
  36. package/examples/adc_dac/ads1115/demo.js +55 -0
  37. package/examples/adc_dac/ads1115/minimal.js +20 -0
  38. package/examples/adc_dac/es8311/complete.js +80 -0
  39. package/examples/adc_dac/es8311/demo.js +59 -0
  40. package/examples/adc_dac/es8311/minimal.js +18 -0
  41. package/examples/adc_dac/mcp4xxx/complete.js +97 -0
  42. package/examples/adc_dac/mcp4xxx/demo.js +76 -0
  43. package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
  44. package/examples/display/pcf8578/complete.js +58 -0
  45. package/examples/display/pcf8578/demo.js +62 -0
  46. package/examples/display/pcf8578/minimal.js +19 -0
  47. package/examples/display/pcf8579/complete.js +58 -0
  48. package/examples/display/pcf8579/demo.js +61 -0
  49. package/examples/display/pcf8579/minimal.js +19 -0
  50. package/examples/imu/icm20948/complete.js +114 -0
  51. package/examples/imu/icm20948/demo.js +79 -0
  52. package/examples/imu/icm20948/minimal.js +22 -0
  53. package/examples/imu/qmi8658a/complete.js +134 -0
  54. package/examples/imu/qmi8658a/demo.js +81 -0
  55. package/examples/imu/qmi8658a/minimal.js +22 -0
  56. package/examples/io_expander/mcp23008/complete.js +70 -0
  57. package/examples/io_expander/mcp23008/demo.js +58 -0
  58. package/examples/io_expander/mcp23008/minimal.js +20 -0
  59. package/examples/memory/mb85rc/complete.js +40 -0
  60. package/examples/memory/mb85rc/demo.js +58 -0
  61. package/examples/memory/mb85rc/minimal.js +15 -0
  62. package/examples/motor/drv2605/complete.js +100 -0
  63. package/examples/motor/drv2605/demo.js +55 -0
  64. package/examples/motor/drv2605/minimal.js +24 -0
  65. package/examples/motor/tmc2130/complete.js +138 -0
  66. package/examples/motor/tmc2130/demo.js +96 -0
  67. package/examples/motor/tmc2130/minimal.js +30 -0
  68. package/examples/motor/tmc2208/complete.js +92 -0
  69. package/examples/motor/tmc2208/demo.js +85 -0
  70. package/examples/motor/tmc2208/minimal.js +25 -0
  71. package/examples/motor/tmc2209/complete.js +106 -0
  72. package/examples/motor/tmc2209/demo.js +97 -0
  73. package/examples/motor/tmc2209/minimal.js +25 -0
  74. package/examples/motor/tmc5160/complete.js +205 -0
  75. package/examples/motor/tmc5160/demo.js +104 -0
  76. package/examples/motor/tmc5160/minimal.js +35 -0
  77. package/examples/power/axp2101/complete.js +135 -0
  78. package/examples/power/axp2101/demo.js +68 -0
  79. package/examples/power/axp2101/minimal.js +24 -0
  80. package/examples/rtc/pcf85063a/complete.js +68 -0
  81. package/examples/rtc/pcf85063a/demo.js +76 -0
  82. package/examples/rtc/pcf85063a/minimal.js +20 -0
  83. package/package.json +1 -1
  84. package/src/chips/accelerometer/bma180.js +573 -0
  85. package/src/chips/accelerometer/fxos8700.js +1600 -0
  86. package/src/chips/accelerometer/lis3dh.js +1341 -0
  87. package/src/chips/accelerometer/lsm303.js +1471 -0
  88. package/src/chips/accelerometer/mma8451q.js +1406 -0
  89. package/src/chips/adc_dac/_ads111x_base.js +272 -0
  90. package/src/chips/adc_dac/ad5243.js +231 -0
  91. package/src/chips/adc_dac/ad5248.js +194 -0
  92. package/src/chips/adc_dac/ads1013.js +219 -0
  93. package/src/chips/adc_dac/ads1014.js +509 -0
  94. package/src/chips/adc_dac/ads1113.js +112 -0
  95. package/src/chips/adc_dac/ads1114.js +201 -0
  96. package/src/chips/adc_dac/ads1115.js +232 -0
  97. package/src/chips/adc_dac/es8311.js +621 -0
  98. package/src/chips/adc_dac/mcp4xxx.js +982 -0
  99. package/src/chips/display/pcf8578.js +452 -0
  100. package/src/chips/display/pcf8579.js +451 -0
  101. package/src/chips/imu/icm20948.js +849 -0
  102. package/src/chips/imu/qmi8658a.js +1011 -0
  103. package/src/chips/io_expander/mcp23008.js +558 -0
  104. package/src/chips/memory/mb85rc.js +166 -0
  105. package/src/chips/motor/drv2605.js +592 -0
  106. package/src/chips/motor/tmc2130.js +537 -0
  107. package/src/chips/motor/tmc2208.js +490 -0
  108. package/src/chips/motor/tmc2209.js +565 -0
  109. package/src/chips/motor/tmc5160.js +900 -0
  110. package/src/chips/power/axp2101.js +968 -0
  111. package/src/chips/rtc/pcf85063a.js +482 -0
  112. package/src/connection/uart.js +9 -0
  113. package/src/connection/uart_mock.js +65 -0
  114. package/src/discovery/registry.js +45 -19
@@ -0,0 +1,558 @@
1
+ 'use strict';
2
+
3
+ const REG_IODIR = 0x00;
4
+ const REG_IPOL = 0x01;
5
+ const REG_GPINTEN = 0x02;
6
+ const REG_DEFVAL = 0x03;
7
+ const REG_INTCON = 0x04;
8
+ const REG_IOCON = 0x05;
9
+ const REG_GPPU = 0x06;
10
+ const REG_INTF = 0x07;
11
+ const REG_INTCAP = 0x08;
12
+ const REG_GPIO = 0x09;
13
+ const REG_OLAT = 0x0A;
14
+
15
+ const IOCON_SEQOP = 0x20;
16
+ const IOCON_HAEN = 0x08;
17
+ const IOCON_ODR = 0x04;
18
+ const IOCON_INTPOL = 0x02;
19
+
20
+ /**
21
+ * Shared implementation of the MCP23008 (I²C) and MCP23S08 (SPI) drivers —
22
+ * minimal interface. Use {@link Mcp23008Minimal} or {@link Mcp23S08Minimal}.
23
+ *
24
+ * Provides 8 bidirectional GPIO pins (GP0–GP7) via a pin() factory. Each
25
+ * returned pin exposes async read()/write() (Connection is async everywhere,
26
+ * so a synchronous pin proxy cannot return a value); pin.asGpio() gives a
27
+ * synchronous opengpio-shaped facade. Shadows of OLAT and IODIR make a
28
+ * single-pin change one register write.
29
+ *
30
+ * Subclasses implement `_regRead(reg, n)` and `_regWrite(reg, data)`; both wait
31
+ * for the constructor's initialisation sequence (`this.ready`) to finish first.
32
+ */
33
+ class Mcp23x08Minimal {
34
+ constructor() {
35
+ this._shadow = 0x00;
36
+ this._direction = 0xFF;
37
+ this._iocon = 0x00;
38
+ }
39
+
40
+ /** Wait for the init sequence started by the constructor. @returns {Promise<void>} */
41
+ get ready() { return this._ready; }
42
+
43
+ async _init() {
44
+ await this._rawWrite(REG_OLAT, 0x00);
45
+ }
46
+
47
+ async _writeOlat(mask) {
48
+ this._shadow = mask & 0xFF;
49
+ await this._regWrite(REG_OLAT, this._shadow);
50
+ }
51
+
52
+ async _setPin(n, value) {
53
+ if (value) this._shadow |= (1 << n);
54
+ else this._shadow &= ~(1 << n);
55
+ await this._writeOlat(this._shadow);
56
+ }
57
+
58
+ async _setDirection(n, input) {
59
+ if (input) {
60
+ this._direction |= (1 << n);
61
+ } else {
62
+ // Latch the output level first so the pin never glitches.
63
+ await this._regWrite(REG_OLAT, this._shadow);
64
+ this._direction &= ~(1 << n);
65
+ }
66
+ await this._regWrite(REG_IODIR, this._direction);
67
+ }
68
+
69
+ /**
70
+ * Return a Pin proxy for pin n (0–7). All eight pins are bidirectional.
71
+ *
72
+ * @param {number} n - Pin index (0–7).
73
+ * @param {string} [direction='in'] - Initial direction: 'in' or 'out'.
74
+ * @returns {_Pin} Pin proxy.
75
+ */
76
+ pin(n, direction = 'in') {
77
+ return new _Pin(this, n, direction);
78
+ }
79
+
80
+ /**
81
+ * Read all 8 pins as a bitmask (GPIO register, actual pin levels).
82
+ *
83
+ * @param {number} [port=0] - Ignored; the chip has a single port.
84
+ * @returns {Promise<number>} 8-bit bitmask (bit 0 = GP0).
85
+ */
86
+ async readPort(port = 0) {
87
+ return (await this._regRead(REG_GPIO, 1))[0];
88
+ }
89
+
90
+ /**
91
+ * Write all 8 output bits via OLAT and update the shadow. Bits of input
92
+ * pins are stored but have no effect until the pin becomes an output.
93
+ *
94
+ * @param {number} [port=0] - Ignored; the chip has a single port.
95
+ * @param {number} mask - 8-bit output mask.
96
+ * @returns {Promise<void>}
97
+ */
98
+ async writePort(port = 0, mask = 0x00) {
99
+ await this._writeOlat(mask);
100
+ }
101
+ }
102
+
103
+ /**
104
+ * GPIO proxy for a single pin. Obtain via pin(n); do not instantiate directly.
105
+ */
106
+ class _Pin {
107
+ /**
108
+ * @param {Mcp23x08Minimal} chip - Parent driver instance.
109
+ * @param {number} n - Pin index (0–7).
110
+ * @param {string} direction - 'in' or 'out'.
111
+ */
112
+ constructor(chip, n, direction) {
113
+ this._chip = chip;
114
+ this._n = n & 7;
115
+ this._direction = direction;
116
+ this._ready = direction === 'out' ? chip._setDirection(this._n, false) : Promise.resolve();
117
+ }
118
+
119
+ /** @type {string} Pin direction ('in' or 'out'). */
120
+ get direction() { return this._direction; }
121
+
122
+ /**
123
+ * Read the pin (actual logic level).
124
+ * @returns {Promise<number>} 0 or 1.
125
+ */
126
+ async read() {
127
+ await this._ready;
128
+ return ((await this._chip.readPort()) >> this._n) & 1;
129
+ }
130
+
131
+ /**
132
+ * Write the pin's output latch.
133
+ * @param {number} value - 0 or 1.
134
+ * @returns {Promise<void>}
135
+ */
136
+ async write(value) {
137
+ await this._ready;
138
+ await this._chip._setPin(this._n, value ? 1 : 0);
139
+ }
140
+
141
+ /**
142
+ * Set pin direction (writes IODIR).
143
+ * @param {string} direction - 'in' or 'out'.
144
+ * @returns {Promise<void>}
145
+ */
146
+ async setDirection(direction) {
147
+ await this._ready;
148
+ this._direction = direction;
149
+ await this._chip._setDirection(this._n, direction !== 'out');
150
+ }
151
+
152
+ /**
153
+ * Return a synchronous opengpio-shaped Input/Output facade for this pin.
154
+ *
155
+ * Output facades read back the shadow register (authoritative) and write
156
+ * through fire-and-forget. Input facades are backed by a 5 ms background
157
+ * poll and are therefore eventually consistent; call stop() to release it.
158
+ *
159
+ * @returns {{direction: string, value: boolean, stop: function}} opengpio-shaped facade.
160
+ */
161
+ asGpio() {
162
+ const pin = this;
163
+ if (this._direction === 'out') {
164
+ return {
165
+ get direction() { return 'out'; },
166
+ get value() { return ((pin._chip._shadow >> pin._n) & 1) === 1; },
167
+ set value(v) { pin._chip._setPin(pin._n, v ? 1 : 0); },
168
+ stop() {},
169
+ };
170
+ }
171
+ let cached = false;
172
+ const timer = setInterval(async () => { cached = (await pin.read()) === 1; }, 5);
173
+ return {
174
+ get direction() { return 'in'; },
175
+ get value() { return cached; },
176
+ set value(_v) { throw new Error('cannot set value on an input pin'); },
177
+ stop() { clearInterval(timer); },
178
+ };
179
+ }
180
+
181
+ /** Release the pin (no-op; shadow state preserved). */
182
+ stop() {}
183
+ }
184
+
185
+ /**
186
+ * MCP23008 8-bit I/O port expander (I²C) — minimal interface.
187
+ */
188
+ class Mcp23008Minimal extends Mcp23x08Minimal {
189
+ /**
190
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
191
+ * @param {number} [addr=0x20] - 7-bit I²C address (0x20–0x27).
192
+ */
193
+ constructor(connection, addr = 0x20) {
194
+ super();
195
+ this._conn = connection;
196
+ this._addr = addr;
197
+ this._ready = this._init();
198
+ }
199
+
200
+ async _rawWrite(reg, value) { await this._conn.writeReg(reg, value & 0xFF); }
201
+
202
+ async _regRead(reg, n) {
203
+ await this._ready;
204
+ return this._conn.readReg(reg, n);
205
+ }
206
+
207
+ async _regWrite(reg, value) {
208
+ await this._ready;
209
+ await this._rawWrite(reg, value);
210
+ }
211
+ }
212
+
213
+ /**
214
+ * MCP23S08 8-bit I/O port expander (SPI) — minimal interface.
215
+ *
216
+ * Every transaction is `[0x40 | (addr << 1) | R/W] [reg] [data…]`; the driver
217
+ * builds the two header bytes itself and talks to a plain Connection
218
+ * (`write` / `writeRead`). IOCON.HAEN is set during init so up to four devices
219
+ * can share one chip select.
220
+ */
221
+ class Mcp23S08Minimal extends Mcp23x08Minimal {
222
+ /**
223
+ * @param {import('../../connection/connection').Connection} connection - SPI Connection (mode 0, MSB first).
224
+ * @param {number} [addr=0] - Hardware device address 0–3 (A1:A0 pins).
225
+ */
226
+ constructor(connection, addr = 0) {
227
+ super();
228
+ this._conn = connection;
229
+ this._addr = addr & 3;
230
+ this._iocon = IOCON_HAEN;
231
+ this._ready = this._initSpi();
232
+ }
233
+
234
+ async _initSpi() {
235
+ // HAEN resets to 0, so an un-addressed device decodes address 00: send
236
+ // the first IOCON write with the address-0 opcode.
237
+ await this._conn.write(Buffer.from([0x40, REG_IOCON, IOCON_HAEN]));
238
+ await this._init();
239
+ }
240
+
241
+ async _rawWrite(reg, value) {
242
+ await this._conn.write(Buffer.from([0x40 | (this._addr << 1), reg, value & 0xFF]));
243
+ }
244
+
245
+ async _regRead(reg, n) {
246
+ await this._ready;
247
+ return this._conn.writeRead(Buffer.from([0x40 | (this._addr << 1) | 1, reg]), n);
248
+ }
249
+
250
+ async _regWrite(reg, value) {
251
+ await this._ready;
252
+ await this._rawWrite(reg, value);
253
+ }
254
+ }
255
+
256
+ /**
257
+ * Full-stage methods shared by Mcp23008Full and Mcp23S08Full (copied onto each
258
+ * class's prototype).
259
+ */
260
+ const FullMethods = {
261
+ _initFull() {
262
+ this._pullup = 0x00;
263
+ this._gpinten = 0x00;
264
+ this._activeHigh = false;
265
+ this._callback = null;
266
+ this._intPinUsed = null;
267
+ this._pollTimer = null;
268
+ this._polling = false;
269
+ this._watchers = {};
270
+ this._edgeHandler = () => { this._service(); };
271
+ },
272
+
273
+ /**
274
+ * Return a Full Pin proxy for pin n (0–7).
275
+ * @param {number} n - Pin index.
276
+ * @param {string} [direction='in'] - Initial direction.
277
+ * @returns {_FullPin} Full pin proxy with watch/unwatch support.
278
+ */
279
+ pin(n, direction = 'in') {
280
+ return new _FullPin(this, n, direction);
281
+ },
282
+
283
+ /**
284
+ * Enable/disable per-pin 100 kΩ pull-ups (GPPU ← mask). Only effective on inputs.
285
+ * @param {number} mask - 8-bit mask: 1 = pull-up on.
286
+ * @returns {Promise<void>}
287
+ */
288
+ async configurePullup(mask) {
289
+ this._pullup = mask & 0xFF;
290
+ await this._regWrite(REG_GPPU, this._pullup);
291
+ },
292
+
293
+ /**
294
+ * Configure input polarity inversion (IPOL ← mask).
295
+ * @param {number} mask - 8-bit mask: 1 = invert the GPIO read.
296
+ * @returns {Promise<void>}
297
+ */
298
+ async configurePolarity(mask) {
299
+ await this._regWrite(REG_IPOL, mask & 0xFF);
300
+ },
301
+
302
+ /**
303
+ * Set DEFVAL for default-compare interrupts.
304
+ * @param {number} mask - 8-bit default compare value.
305
+ * @returns {Promise<void>}
306
+ */
307
+ async setDefaultValue(mask) {
308
+ await this._regWrite(REG_DEFVAL, mask & 0xFF);
309
+ },
310
+
311
+ /**
312
+ * Arm interrupt-on-change pins (GPINTEN ← mask). Only inputs can interrupt.
313
+ * @param {number} mask - 8-bit mask: bit n arms GPn.
314
+ * @returns {Promise<void>}
315
+ */
316
+ async enablePinInterrupt(mask) {
317
+ this._gpinten = mask & 0xFF;
318
+ await this._regWrite(REG_GPINTEN, this._gpinten);
319
+ },
320
+
321
+ /**
322
+ * Configure the INT output (IOCON ODR / INTPOL).
323
+ * @param {object} [options]
324
+ * @param {boolean} [options.openDrain=false] - Open-drain INT (overrides activeHigh).
325
+ * @param {boolean} [options.activeHigh=false] - Active-high when push-pull.
326
+ * @returns {Promise<void>}
327
+ */
328
+ async configureIntPin(options = {}) {
329
+ const openDrain = !!options.openDrain;
330
+ const activeHigh = !!options.activeHigh;
331
+ this._activeHigh = activeHigh && !openDrain;
332
+ let iocon = this._iocon & ~(IOCON_ODR | IOCON_INTPOL);
333
+ if (openDrain) iocon |= IOCON_ODR;
334
+ else if (activeHigh) iocon |= IOCON_INTPOL;
335
+ this._iocon = iocon;
336
+ await this._regWrite(REG_IOCON, iocon);
337
+ },
338
+
339
+ /**
340
+ * Enable or disable sequential register access (IOCON SEQOP). The driver
341
+ * itself never relies on a value other than enabled.
342
+ * @param {boolean} enabled - true (default, auto-increment) or false.
343
+ * @returns {Promise<void>}
344
+ */
345
+ async setSequential(enabled) {
346
+ let iocon = this._iocon & ~IOCON_SEQOP;
347
+ if (!enabled) iocon |= IOCON_SEQOP;
348
+ this._iocon = iocon;
349
+ await this._regWrite(REG_IOCON, iocon);
350
+ },
351
+
352
+ /**
353
+ * Raw register read (debug / escape hatch).
354
+ * @param {number} reg - Register address 0x00–0x0A.
355
+ * @returns {Promise<number>}
356
+ */
357
+ async readRegister(reg) {
358
+ return (await this._regRead(reg, 1))[0];
359
+ },
360
+
361
+ /**
362
+ * Raw register write; the IODIR / OLAT / IOCON / GPPU / GPINTEN shadows are kept in sync.
363
+ * @param {number} reg - Register address 0x00–0x0A.
364
+ * @param {number} value - 8-bit value.
365
+ * @returns {Promise<void>}
366
+ */
367
+ async writeRegister(reg, value) {
368
+ value &= 0xFF;
369
+ if (reg === REG_IODIR) this._direction = value;
370
+ else if (reg === REG_OLAT) this._shadow = value;
371
+ else if (reg === REG_IOCON) this._iocon = value;
372
+ else if (reg === REG_GPPU) this._pullup = value;
373
+ else if (reg === REG_GPINTEN) this._gpinten = value;
374
+ await this._regWrite(reg, value);
375
+ },
376
+
377
+ /**
378
+ * Rewrite every register to its reset value and clear the shadows. The chip
379
+ * has no reset command (RESET is an external pin). On the MCP23S08 IOCON is
380
+ * rewritten to HAEN (not 0) so addressed access keeps working.
381
+ * @returns {Promise<void>}
382
+ */
383
+ async reset() {
384
+ await this.offInterrupt();
385
+ for (const reg of [REG_IPOL, REG_GPINTEN, REG_DEFVAL, REG_INTCON, REG_GPPU, REG_OLAT]) {
386
+ await this._regWrite(reg, 0x00);
387
+ }
388
+ await this._regWrite(REG_IODIR, 0xFF);
389
+ this._iocon = this._baseIocon();
390
+ await this._regWrite(REG_IOCON, this._iocon);
391
+ this._shadow = 0x00;
392
+ this._direction = 0xFF;
393
+ this._pullup = 0x00;
394
+ this._gpinten = 0x00;
395
+ this._activeHigh = false;
396
+ },
397
+
398
+ async _arm(intPin) {
399
+ this._intPinUsed = intPin;
400
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
401
+ if (intPin) {
402
+ await intPin.onEdge(this._edgeHandler, this._activeHigh ? 'rising' : 'falling');
403
+ } else {
404
+ this._pollTimer = setInterval(() => { this._service(); }, 5);
405
+ }
406
+ },
407
+
408
+ /**
409
+ * Subscribe to INT assertions. `callback(status)` receives the INTF mask of
410
+ * the pins that caused the interrupt. Every pin armed via
411
+ * enablePinInterrupt() or watch() is used; delivery is via the INT line
412
+ * (options.intPin or connection.intPin) or a 5 ms polling timer.
413
+ *
414
+ * @param {function} callback - Called with (status).
415
+ * @param {object} [options]
416
+ * @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
417
+ * @param {string} [options.mode='change'] - 'change' (INTCON = 0) or 'default' (compare against DEFVAL).
418
+ * @returns {Promise<void>}
419
+ * @throws {Error} if no pin has been armed.
420
+ */
421
+ async onInterrupt(callback, options = {}) {
422
+ if (!this._gpinten) throw new Error('no pins armed: call enablePinInterrupt() first');
423
+ this._callback = callback;
424
+ await this._regWrite(REG_INTCON, options.mode === 'default' ? this._gpinten : 0x00);
425
+ await this._arm(options.intPin ?? this._conn.intPin ?? null);
426
+ },
427
+
428
+ /**
429
+ * Unsubscribe and stop delivery (GPINTEN is left untouched).
430
+ * @returns {Promise<void>}
431
+ */
432
+ async offInterrupt() {
433
+ if (this._intPinUsed) {
434
+ await this._intPinUsed.offEdge(this._edgeHandler);
435
+ this._intPinUsed = null;
436
+ }
437
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
438
+ this._callback = null;
439
+ },
440
+
441
+ async _service() {
442
+ if (this._polling) return;
443
+ this._polling = true;
444
+ try {
445
+ const status = (await this._regRead(REG_INTF, 1))[0];
446
+ if (!status) return;
447
+ const captured = (await this._regRead(REG_INTCAP, 1))[0]; // clears the interrupt
448
+ if (this._callback) this._callback(status);
449
+ for (const [bit, w] of Object.entries(this._watchers)) {
450
+ if (!((status >> bit) & 1)) continue;
451
+ const current = (captured >> bit) & 1;
452
+ if (w.trigger === 'change' || (w.trigger === 'rising' && current === 1) ||
453
+ (w.trigger === 'falling' && current === 0)) {
454
+ w.handler(this.pin(Number(bit)));
455
+ }
456
+ }
457
+ } finally {
458
+ this._polling = false;
459
+ }
460
+ },
461
+
462
+ /**
463
+ * Read INTF, then INTCAP (clearing the interrupt). The captured port value
464
+ * is discarded — use readCapture().
465
+ * @returns {Promise<number>} INTF bitmask.
466
+ */
467
+ async pollInterrupt() {
468
+ const flags = (await this._regRead(REG_INTF, 1))[0];
469
+ await this._regRead(REG_INTCAP, 1);
470
+ return flags;
471
+ },
472
+
473
+ /**
474
+ * Read INTCAP: the port state captured at the interrupt. Also clears the interrupt.
475
+ * @returns {Promise<number>} 8-bit captured port value.
476
+ */
477
+ async readCapture() {
478
+ return (await this._regRead(REG_INTCAP, 1))[0];
479
+ },
480
+ };
481
+
482
+ /**
483
+ * Full GPIO proxy — adds watch()/unwatch() for interrupt-driven input.
484
+ */
485
+ class _FullPin extends _Pin {
486
+ /**
487
+ * Subscribe to this pin's edge events (sets its GPINTEN bit).
488
+ *
489
+ * At most one handler per pin; a second call replaces the first. The chip
490
+ * reports changes only; rising/falling is filtered in software. Delivery
491
+ * starts when the chip's onInterrupt() is called.
492
+ *
493
+ * @param {function} handler - Called with this pin when its state matches trigger.
494
+ * @param {string} [trigger='change'] - 'rising', 'falling', or 'change'.
495
+ * @returns {Promise<void>}
496
+ * @throws {Error} if the pin is configured as an output.
497
+ */
498
+ async watch(handler, trigger = 'change') {
499
+ await this._ready;
500
+ if (((this._chip._direction >> this._n) & 1) === 0) throw new Error('cannot watch an output pin');
501
+ this._chip._watchers[this._n] = { handler, trigger };
502
+ await this._chip.enablePinInterrupt(this._chip._gpinten | (1 << this._n));
503
+ }
504
+
505
+ /**
506
+ * Unsubscribe this pin's handler and clear its GPINTEN bit.
507
+ * @returns {Promise<void>}
508
+ */
509
+ async unwatch() {
510
+ delete this._chip._watchers[this._n];
511
+ await this._chip.enablePinInterrupt(this._chip._gpinten & ~(1 << this._n));
512
+ }
513
+
514
+ /**
515
+ * Invert active-low polarity for this pin (IPOL bit).
516
+ * @param {boolean} invert - true to invert the read value.
517
+ * @returns {Promise<void>}
518
+ */
519
+ async setActiveLow(invert) {
520
+ this._activeLow = !!invert;
521
+ const ipol = (await this._chip.readRegister(REG_IPOL) & ~(1 << this._n)) | (invert ? (1 << this._n) : 0);
522
+ await this._chip.configurePolarity(ipol);
523
+ }
524
+ }
525
+
526
+ /** MCP23008 full interface (I²C) — pull-ups, polarity, interrupts, raw access. */
527
+ class Mcp23008Full extends Mcp23008Minimal {
528
+ /**
529
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
530
+ * @param {number} [addr=0x20] - 7-bit I²C address.
531
+ */
532
+ constructor(connection, addr = 0x20) {
533
+ super(connection, addr);
534
+ this._initFull();
535
+ }
536
+
537
+ _baseIocon() { return 0x00; }
538
+ }
539
+
540
+ /** MCP23S08 full interface (SPI) — pull-ups, polarity, interrupts, raw access. */
541
+ class Mcp23S08Full extends Mcp23S08Minimal {
542
+ /**
543
+ * @param {import('../../connection/connection').Connection} connection - SPI Connection (mode 0, MSB first).
544
+ * @param {number} [addr=0] - Hardware device address 0–3 (A1:A0 pins).
545
+ */
546
+ constructor(connection, addr = 0) {
547
+ super(connection, addr);
548
+ this._initFull();
549
+ }
550
+
551
+ _baseIocon() { return IOCON_HAEN; }
552
+ }
553
+
554
+ for (const Cls of [Mcp23008Full, Mcp23S08Full]) {
555
+ Object.defineProperties(Cls.prototype, Object.getOwnPropertyDescriptors(FullMethods));
556
+ }
557
+
558
+ module.exports = { Mcp23008Minimal, Mcp23008Full, Mcp23S08Minimal, Mcp23S08Full };