periph 1.0.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 (101) hide show
  1. package/examples/accelerometer/.gitkeep +0 -0
  2. package/examples/adc_dac/.gitkeep +0 -0
  3. package/examples/adc_dac/mcp4725/complete.js +34 -0
  4. package/examples/adc_dac/mcp4725/demo.js +43 -0
  5. package/examples/adc_dac/mcp4725/minimal.js +14 -0
  6. package/examples/color/.gitkeep +0 -0
  7. package/examples/comms/.gitkeep +0 -0
  8. package/examples/display/.gitkeep +0 -0
  9. package/examples/environmental/.gitkeep +0 -0
  10. package/examples/gas/.gitkeep +0 -0
  11. package/examples/gnss/.gitkeep +0 -0
  12. package/examples/gpio/.gitkeep +0 -0
  13. package/examples/gyroscope/.gitkeep +0 -0
  14. package/examples/humidity/.gitkeep +0 -0
  15. package/examples/imu/.gitkeep +0 -0
  16. package/examples/io_expander/mcp23017/complete.js +80 -0
  17. package/examples/io_expander/mcp23017/demo.js +59 -0
  18. package/examples/io_expander/mcp23017/minimal.js +16 -0
  19. package/examples/io_expander/pcf8574/complete.js +68 -0
  20. package/examples/io_expander/pcf8574/demo.js +41 -0
  21. package/examples/io_expander/pcf8574/minimal.js +16 -0
  22. package/examples/io_expander/pcf8575/complete.js +31 -0
  23. package/examples/io_expander/pcf8575/demo.js +33 -0
  24. package/examples/io_expander/pcf8575/minimal.js +20 -0
  25. package/examples/led/.gitkeep +0 -0
  26. package/examples/led/sk6812rgbw/complete.js +69 -0
  27. package/examples/led/sk6812rgbw/demo.js +85 -0
  28. package/examples/led/sk6812rgbw/minimal.js +25 -0
  29. package/examples/led/ws2812b/complete.js +64 -0
  30. package/examples/led/ws2812b/demo.js +101 -0
  31. package/examples/led/ws2812b/minimal.js +22 -0
  32. package/examples/light/.gitkeep +0 -0
  33. package/examples/magnetometer/.gitkeep +0 -0
  34. package/examples/magnetometer/as5600/complete.js +43 -0
  35. package/examples/magnetometer/as5600/demo.js +49 -0
  36. package/examples/magnetometer/as5600/minimal.js +16 -0
  37. package/examples/memory/.gitkeep +0 -0
  38. package/examples/motor/.gitkeep +0 -0
  39. package/examples/other/.gitkeep +0 -0
  40. package/examples/power/ina219/complete.js +30 -0
  41. package/examples/power/ina219/demo.js +26 -0
  42. package/examples/power/ina219/minimal.js +17 -0
  43. package/examples/power/ina226/complete.js +33 -0
  44. package/examples/power/ina226/demo.js +30 -0
  45. package/examples/power/ina226/minimal.js +11 -0
  46. package/examples/power/ina3221/complete.js +65 -0
  47. package/examples/power/ina3221/demo.js +56 -0
  48. package/examples/power/ina3221/minimal.js +16 -0
  49. package/examples/pressure/.gitkeep +0 -0
  50. package/examples/pressure/bmp180/complete.js +26 -0
  51. package/examples/pressure/bmp180/demo.js +32 -0
  52. package/examples/pressure/bmp180/minimal.js +17 -0
  53. package/examples/pressure/bmp280/complete.js +28 -0
  54. package/examples/pressure/bmp280/demo.js +34 -0
  55. package/examples/pressure/bmp280/minimal.js +17 -0
  56. package/examples/rfid/.gitkeep +0 -0
  57. package/examples/rtc/.gitkeep +0 -0
  58. package/examples/temperature/.gitkeep +0 -0
  59. package/examples/tof/.gitkeep +0 -0
  60. package/package.json +20 -0
  61. package/src/chips/accelerometer/.gitkeep +0 -0
  62. package/src/chips/adc_dac/.gitkeep +0 -0
  63. package/src/chips/adc_dac/mcp4725.js +161 -0
  64. package/src/chips/color/.gitkeep +0 -0
  65. package/src/chips/comms/.gitkeep +0 -0
  66. package/src/chips/display/.gitkeep +0 -0
  67. package/src/chips/environmental/.gitkeep +0 -0
  68. package/src/chips/gas/.gitkeep +0 -0
  69. package/src/chips/gnss/.gitkeep +0 -0
  70. package/src/chips/gpio/.gitkeep +0 -0
  71. package/src/chips/gyroscope/.gitkeep +0 -0
  72. package/src/chips/humidity/.gitkeep +0 -0
  73. package/src/chips/imu/.gitkeep +0 -0
  74. package/src/chips/io_expander/mcp23017.js +396 -0
  75. package/src/chips/io_expander/pcf8574.js +298 -0
  76. package/src/chips/io_expander/pcf8575.js +295 -0
  77. package/src/chips/led/.gitkeep +0 -0
  78. package/src/chips/led/_color.js +25 -0
  79. package/src/chips/led/sk6812rgbw.js +183 -0
  80. package/src/chips/led/ws2812b.js +171 -0
  81. package/src/chips/light/.gitkeep +0 -0
  82. package/src/chips/magnetometer/as5600.js +293 -0
  83. package/src/chips/memory/.gitkeep +0 -0
  84. package/src/chips/motor/.gitkeep +0 -0
  85. package/src/chips/other/.gitkeep +0 -0
  86. package/src/chips/power/ina219.js +154 -0
  87. package/src/chips/power/ina226.js +208 -0
  88. package/src/chips/power/ina3221.js +341 -0
  89. package/src/chips/pressure/.gitkeep +0 -0
  90. package/src/chips/pressure/bmp180.js +234 -0
  91. package/src/chips/pressure/bmp280.js +328 -0
  92. package/src/chips/rfid/.gitkeep +0 -0
  93. package/src/chips/rtc/.gitkeep +0 -0
  94. package/src/chips/temperature/.gitkeep +0 -0
  95. package/src/chips/tof/.gitkeep +0 -0
  96. package/src/index.js +2 -0
  97. package/src/transport/i2c.js +67 -0
  98. package/src/transport/neopixel.js +37 -0
  99. package/src/transport/smbus.js +117 -0
  100. package/src/transport/spi.js +72 -0
  101. package/src/transport/uart.js +145 -0
@@ -0,0 +1,396 @@
1
+ 'use strict';
2
+
3
+ const REG_IODIRA = 0x00;
4
+ const REG_IODIRB = 0x01;
5
+ const REG_IPOLA = 0x02;
6
+ const REG_IPOLB = 0x03;
7
+ const REG_GPINTENA = 0x04;
8
+ const REG_GPINTENB = 0x05;
9
+ const REG_DEFVALA = 0x06;
10
+ const REG_DEFVALB = 0x07;
11
+ const REG_INTCONA = 0x08;
12
+ const REG_INTCONB = 0x09;
13
+ const REG_IOCON = 0x0A;
14
+ const REG_GPPUA = 0x0C;
15
+ const REG_GPPUB = 0x0D;
16
+ const REG_INTFA = 0x0E;
17
+ const REG_INTFB = 0x0F;
18
+ const REG_INTCAPA = 0x10;
19
+ const REG_INTCAPB = 0x11;
20
+ const REG_GPIOA = 0x12;
21
+ const REG_GPIOB = 0x13;
22
+ const REG_OLATA = 0x14;
23
+ const REG_OLATB = 0x15;
24
+
25
+ /**
26
+ * MCP23017 16-bit bidirectional I/O port expander — minimal interface.
27
+ *
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.
30
+ *
31
+ * At construction, all pins initialise as inputs except GPA7 and GPB7
32
+ * which are output-only on the hardware and are forced to output mode.
33
+ * A shadow register tracks the output latch (OLATA/OLATB) to enable
34
+ * bit-level read-modify-write without an I²C read.
35
+ *
36
+ * IOCON.BANK is left at 0 (power-on default) throughout; do not alter it.
37
+ */
38
+ class Mcp23017Minimal {
39
+ /**
40
+ * @param {object} transport - I²C transport with write, read, writeRead methods.
41
+ * @param {number} [addr=0x20] - 7-bit I²C address (default 0x20, range 0x20–0x27).
42
+ */
43
+ constructor(transport, addr = 0x20) {
44
+ this._transport = transport;
45
+ this._addr = addr;
46
+ this._shadow = [0, 0];
47
+
48
+ this._writeReg(REG_OLATA, 0x00);
49
+ this._writeReg(REG_OLATB, 0x00);
50
+ this._writeReg(REG_IODIRA, 0x7F);
51
+ this._writeReg(REG_IODIRB, 0x7F);
52
+ this._writeReg(REG_IPOLA, 0x00);
53
+ this._writeReg(REG_IPOLB, 0x00);
54
+ this._writeReg(REG_GPPUA, 0x00);
55
+ this._writeReg(REG_GPPUB, 0x00);
56
+ }
57
+
58
+ _writeReg(reg, value) {
59
+ this._transport.write(Buffer.from([reg, value & 0xFF]));
60
+ }
61
+
62
+ _readReg(reg) {
63
+ return this._transport.writeRead(Buffer.from([reg]), 1)[0];
64
+ }
65
+
66
+ _writePort(port, mask) {
67
+ this._writeReg(REG_OLATA + port, mask & 0xFF);
68
+ }
69
+
70
+ _readPort(port) {
71
+ return this._readReg(REG_GPIOA + port);
72
+ }
73
+
74
+ _setPin(n, value) {
75
+ const port = n >> 3;
76
+ const bit = n & 7;
77
+ if (value) this._shadow[port] |= (1 << bit);
78
+ else this._shadow[port] &= ~(1 << bit);
79
+ this._writePort(port, this._shadow[port]);
80
+ }
81
+
82
+ /**
83
+ * Return a Pin proxy for pin n (0–15).
84
+ *
85
+ * Pins 0–7 map to PORTA (GPA0–GPA7); pins 8–15 map to PORTB (GPB0–GPB7).
86
+ * Pins 7 and 15 (GPA7/GPB7) are output-only; attempting to set them
87
+ * as input via direction='in' will still allow reading the output latch
88
+ * value, but the hardware forces them as outputs.
89
+ *
90
+ * @param {number} n - Pin index (0–15).
91
+ * @param {string} [direction='in'] - Initial direction: 'in' or 'out'.
92
+ * @returns {_Pin} Pin proxy implementing the onoff Gpio subset.
93
+ */
94
+ pin(n, direction = 'in') {
95
+ return new _Pin(this, n, direction);
96
+ }
97
+
98
+ /**
99
+ * Read all 8 pins of a port as a bitmask.
100
+ *
101
+ * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
102
+ * @returns {number} 8-bit bitmask (bit 0 = pin 0 of the port).
103
+ */
104
+ readPort(port = 0) {
105
+ return this._readPort(port);
106
+ }
107
+
108
+ /**
109
+ * Write all 8 output pins of a port via the output latch.
110
+ * Updates the internal shadow register.
111
+ *
112
+ * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
113
+ * @param {number} mask - 8-bit output mask.
114
+ */
115
+ writePort(port = 0, mask = 0x00) {
116
+ this._shadow[port] = mask & 0xFF;
117
+ this._writePort(port, this._shadow[port]);
118
+ }
119
+
120
+ /**
121
+ * Configure the direction (IODIR) of a full port.
122
+ * @param {number} port - 0 = PORTA (IODIRA), 1 = PORTB (IODIRB).
123
+ * @param {number} mask - 8-bit mask; bit = 1 → input, 0 → output.
124
+ */
125
+ configureDirection(port, mask) {
126
+ this._writeReg(REG_IODIRA + (port & 1), mask & 0xFF);
127
+ }
128
+ }
129
+
130
+ /**
131
+ * GPIO proxy for a single MCP23017 pin — onoff Gpio interface subset.
132
+ *
133
+ * Obtain via Mcp23017Minimal.pin(n). Do not instantiate directly.
134
+ */
135
+ class _Pin {
136
+ /**
137
+ * @param {Mcp23017Minimal} chip - Parent driver instance.
138
+ * @param {number} n - Pin index (0–15).
139
+ * @param {string} direction - 'in' or 'out'.
140
+ */
141
+ constructor(chip, n, direction) {
142
+ this._chip = chip;
143
+ this._n = n;
144
+ this._direction = direction;
145
+ }
146
+
147
+ /** @type {string} Current pin direction ('in' or 'out'). */
148
+ get direction() { return this._direction; }
149
+
150
+ /**
151
+ * Read pin synchronously.
152
+ * @returns {number} 0 or 1.
153
+ */
154
+ readSync() {
155
+ return (this._chip._readPort(this._n >> 3) >> (this._n & 7)) & 1;
156
+ }
157
+
158
+ /**
159
+ * Write pin synchronously.
160
+ * @param {number} value - 0 or 1.
161
+ */
162
+ writeSync(value) {
163
+ this._chip._setPin(this._n, value ? 1 : 0);
164
+ }
165
+
166
+ /**
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).
178
+ */
179
+ write(value, callback) {
180
+ try { this.writeSync(value); callback(null); } catch (e) { callback(e); }
181
+ }
182
+
183
+ /**
184
+ * Set pin direction.
185
+ * @param {string} direction - 'in' or 'out'.
186
+ * @param {function} callback - Node-style callback(err).
187
+ */
188
+ setDirection(direction, callback) {
189
+ try {
190
+ this._direction = direction;
191
+ if (callback) callback(null);
192
+ } catch (e) { if (callback) callback(e); }
193
+ }
194
+
195
+ /** Release the pin (no-op; shadow state preserved). */
196
+ unexport() {}
197
+ }
198
+
199
+ /**
200
+ * MCP23017 full interface — extends Minimal with interrupt support,
201
+ * per-pin pull-ups, and interrupt-on-change configuration.
202
+ *
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.
207
+ */
208
+ class Mcp23017Full extends Mcp23017Minimal {
209
+ /**
210
+ * @param {object} transport - I²C transport with write, read, writeRead methods.
211
+ * @param {number} [addr=0x20] - 7-bit I²C address.
212
+ */
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 = {};
219
+ }
220
+
221
+ /**
222
+ * Return a Full Pin proxy for pin n (0–15).
223
+ * @param {number} n - Pin index.
224
+ * @param {string} [direction='in'] - Initial direction.
225
+ * @returns {_FullPin} Full pin proxy with watch/unwatch support.
226
+ */
227
+ pin(n, direction = 'in') {
228
+ return new _FullPin(this, n, direction);
229
+ }
230
+
231
+ /**
232
+ * Enable/disable per-pin pull-ups on a port.
233
+ *
234
+ * Pull-ups are only electrically active on pins configured as inputs.
235
+ * Enabling them on output pins has no hardware effect but is allowed
236
+ * in Full (caller responsibility).
237
+ *
238
+ * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
239
+ * @param {number} mask - 8-bit mask: 1 = enable 100 kΩ pull-up.
240
+ */
241
+ configurePullup(port = 0, mask = 0x00) {
242
+ this._writeReg(REG_GPPUA + port, mask & 0xFF);
243
+ }
244
+
245
+ /**
246
+ * Configure input polarity inversion on a port.
247
+ *
248
+ * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
249
+ * @param {number} mask - 8-bit mask: 1 = invert GPIO read.
250
+ */
251
+ configurePolarity(port = 0, mask = 0x00) {
252
+ this._writeReg(REG_IPOLA + port, mask & 0xFF);
253
+ }
254
+
255
+ /**
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.
265
+ */
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
+ }
290
+ }
291
+
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);
298
+ }
299
+
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
+ }
308
+ }
309
+
310
+ /**
311
+ * Read and clear the interrupt for a port, returning the changed-pin bitmask.
312
+ *
313
+ * Also updates the per-port previous-state tracker used by the polling loop.
314
+ *
315
+ * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
316
+ * @returns {number} 8-bit changed-pin bitmask for the port.
317
+ */
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;
324
+ }
325
+
326
+ /**
327
+ * Read interrupt flags without clearing the interrupt.
328
+ *
329
+ * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
330
+ * @returns {number} 8-bit interrupt-flag bitmask.
331
+ */
332
+ readInterruptFlags(port = 0) {
333
+ return this._readReg(REG_INTFA + port);
334
+ }
335
+
336
+ /**
337
+ * Disable interrupt generation for a port.
338
+ *
339
+ * @param {number} [port=0] - Port index: 0 = PORTA, 1 = PORTB.
340
+ */
341
+ stopInterrupt(port = 0) {
342
+ this._writeReg(REG_GPINTENA + port, 0x00);
343
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
344
+ }
345
+ }
346
+
347
+ /**
348
+ * Full GPIO proxy — adds watch/unwatch for interrupt-driven input.
349
+ */
350
+ class _FullPin extends _Pin {
351
+ /**
352
+ * @param {Mcp23017Full} chip - Parent full driver instance.
353
+ * @param {number} n - Pin index (0–15).
354
+ * @param {string} direction - 'in' or 'out'.
355
+ */
356
+ constructor(chip, n, direction) {
357
+ super(chip, n, direction);
358
+ }
359
+
360
+ /**
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.
374
+ */
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
+ }
380
+ }
381
+
382
+ /** Remove all handlers for this pin. */
383
+ unwatchAll() {
384
+ this._chip._watchers[this._n] = [];
385
+ }
386
+
387
+ /**
388
+ * Invert active-low polarity for this pin.
389
+ * @param {boolean} invert - true to invert read/write values.
390
+ */
391
+ setActiveLow(invert) {
392
+ this._activeLow = !!invert;
393
+ }
394
+ }
395
+
396
+ module.exports = { Mcp23017Minimal, Mcp23017Full };
@@ -0,0 +1,298 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * PCF8574 8-bit quasi-bidirectional I/O port expander — minimal interface.
5
+ *
6
+ * Exposes all eight pins (P0–P7) as GPIO objects via the pin() factory.
7
+ * Pin objects implement the onoff Gpio interface subset so they are
8
+ * drop-in replacements for hardware GPIO in onoff-based code.
9
+ *
10
+ * Direction is implicit: writeSync(1) puts a pin in input mode (weak pull-up);
11
+ * writeSync(0) drives it low. A shadow register tracks the output latch.
12
+ *
13
+ * Initialises all pins to input mode (shadow = 0xFF) at construction.
14
+ */
15
+ class Pcf8574Minimal {
16
+ /**
17
+ * @param {object} transport - Configured I²C transport (write, read, writeRead).
18
+ * @param {number} [addr=0x20] - 7-bit I²C device address.
19
+ * PCF8574 default 0x20; PCF8574A default 0x38.
20
+ */
21
+ constructor(transport, addr = 0x20) {
22
+ this._transport = transport;
23
+ this._addr = addr;
24
+ this._shadow = 0xFF;
25
+ this._writePort(0xFF);
26
+ }
27
+
28
+ // ------------------------------------------------------------------
29
+ // Internal helpers
30
+ // ------------------------------------------------------------------
31
+
32
+ _writePort(mask) {
33
+ this._transport.write(Buffer.from([mask & 0xFF]));
34
+ }
35
+
36
+ _readPort() {
37
+ return this._transport.read(1)[0];
38
+ }
39
+
40
+ _setPin(n, value) {
41
+ if (value) this._shadow |= (1 << n);
42
+ else this._shadow &= ~(1 << n);
43
+ this._shadow &= 0xFF;
44
+ this._writePort(this._shadow);
45
+ }
46
+
47
+ // ------------------------------------------------------------------
48
+ // Public driver API
49
+ // ------------------------------------------------------------------
50
+
51
+ /**
52
+ * Return a Pin proxy object for pin n (0–7).
53
+ * @param {number} n - Pin index (0 = P0, 7 = P7).
54
+ * @param {string} [direction='in'] - Initial direction: 'in' or 'out'.
55
+ * @returns {_Pin} Pin proxy implementing the onoff Gpio subset.
56
+ */
57
+ pin(n, direction = 'in') {
58
+ const p = new _Pin(this, n, direction);
59
+ if (direction === 'out') this._setPin(n, 0);
60
+ else this._setPin(n, 1);
61
+ return p;
62
+ }
63
+
64
+ /**
65
+ * Read all 8 pins as a bitmask.
66
+ * @param {number} [port=0] - Port index (ignored; PCF8574 has one port).
67
+ * @returns {number} 8-bit bitmask of actual pin logic levels (bit 0 = P0).
68
+ */
69
+ readPort(port = 0) { return this._readPort(); }
70
+
71
+ /**
72
+ * Write all 8 pins at once and update the shadow register.
73
+ * @param {number} [port=0] - Port index (ignored).
74
+ * @param {number} mask - 8-bit output mask; 1 = input mode, 0 = drive low.
75
+ */
76
+ writePort(port = 0, mask = 0xFF) {
77
+ this._shadow = mask & 0xFF;
78
+ this._writePort(this._shadow);
79
+ }
80
+ }
81
+
82
+ /**
83
+ * GPIO proxy for a single PCF8574 pin — onoff Gpio interface subset.
84
+ *
85
+ * Obtain via Pcf8574Minimal.pin(n). Do not instantiate directly.
86
+ */
87
+ class _Pin {
88
+ /**
89
+ * @param {Pcf8574Minimal} chip - Parent driver instance.
90
+ * @param {number} n - Pin index (0–7).
91
+ * @param {string} direction - 'in' or 'out'.
92
+ */
93
+ constructor(chip, n, direction) {
94
+ this._chip = chip;
95
+ this._n = n;
96
+ this._direction = direction;
97
+ }
98
+
99
+ /** @type {string} Current pin direction ('in' or 'out'). */
100
+ get direction() { return this._direction; }
101
+
102
+ /**
103
+ * Read pin synchronously.
104
+ * @returns {number} 0 or 1.
105
+ */
106
+ readSync() {
107
+ return (this._chip._readPort() >> this._n) & 1;
108
+ }
109
+
110
+ /**
111
+ * Write pin synchronously.
112
+ * @param {number} value - 0 (drive low) or 1 (release to quasi-high input).
113
+ */
114
+ writeSync(value) {
115
+ this._chip._setPin(this._n, value ? 1 : 0);
116
+ }
117
+
118
+ /**
119
+ * Read pin asynchronously.
120
+ * @param {function} callback - Node-style callback(err, value).
121
+ */
122
+ read(callback) {
123
+ try { callback(null, this.readSync()); } catch (e) { callback(e); }
124
+ }
125
+
126
+ /**
127
+ * Write pin asynchronously.
128
+ * @param {number} value - 0 or 1.
129
+ * @param {function} callback - Node-style callback(err).
130
+ */
131
+ write(value, callback) {
132
+ try { this.writeSync(value); callback(null); } catch (e) { callback(e); }
133
+ }
134
+
135
+ /**
136
+ * Set pin direction.
137
+ * @param {string} direction - 'in' or 'out'.
138
+ * @param {function} callback - Node-style callback(err).
139
+ */
140
+ setDirection(direction, callback) {
141
+ try {
142
+ this._direction = direction;
143
+ this._chip._setPin(this._n, direction === 'in' ? 1 : 0);
144
+ if (callback) callback(null);
145
+ } catch (e) { if (callback) callback(e); }
146
+ }
147
+
148
+ /** Release the pin (no-op; shadow state preserved). */
149
+ unexport() {}
150
+ }
151
+
152
+ /**
153
+ * PCF8574 full interface — extends Pcf8574Minimal with interrupt support.
154
+ *
155
+ * Adds configureInterrupt() to attach a callback to the chip's INT line
156
+ * and clearInterrupt() to return the changed-pin bitmask.
157
+ * Pin objects gain watch() / unwatch() for per-pin interrupt handlers.
158
+ */
159
+ class Pcf8574Full extends Pcf8574Minimal {
160
+ /**
161
+ * @param {object} transport - Configured I²C transport.
162
+ * @param {number} [addr=0x20] - 7-bit I²C device address.
163
+ */
164
+ constructor(transport, addr = 0x20) {
165
+ super(transport, addr);
166
+ this._prev = this._readPort();
167
+ this._callback = null;
168
+ this._pollTimer = null;
169
+ this._watchers = {};
170
+ }
171
+
172
+ /**
173
+ * Return a Full pin proxy for pin n (0–7).
174
+ * @param {number} n - Pin index.
175
+ * @param {string} [direction='in'] - Initial direction.
176
+ * @returns {_FullPin} Full pin proxy with watch/unwatch support.
177
+ */
178
+ pin(n, direction = 'in') {
179
+ const p = new _FullPin(this, n, direction);
180
+ if (direction === 'out') this._setPin(n, 0);
181
+ else this._setPin(n, 1);
182
+ return p;
183
+ }
184
+
185
+ /**
186
+ * Attach a callback to the chip's INT output.
187
+ *
188
+ * On Linux, uses a 5 ms polling interval; pass intGpioPath to the
189
+ * sysfs value file (e.g. '/sys/class/gpio/gpio5/value') for edge-
190
+ * based delivery via epoll. Pass null to use polling.
191
+ *
192
+ * @param {string|null} intGpioPath - Sysfs GPIO value file path, or null for polling.
193
+ * @param {function} callback - Called with changed bitmask on any input change.
194
+ */
195
+ configureInterrupt(intGpioPath, callback) {
196
+ this._callback = callback;
197
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
198
+ if (intGpioPath) {
199
+ try {
200
+ const fs = require('fs');
201
+ const ep = require('epoll').Epoll;
202
+ const fd = fs.openSync(intGpioPath, 'r');
203
+ const poll = new ep((err, fd2) => {
204
+ fs.readSync(fd2, Buffer.alloc(1), 0, 1, 0);
205
+ const changed = this.clearInterrupt();
206
+ if (changed) this._dispatch(changed);
207
+ });
208
+ poll.add(fd, ep.EPOLLPRI);
209
+ } catch (_) {
210
+ this._startPolling();
211
+ }
212
+ } else {
213
+ this._startPolling();
214
+ }
215
+ }
216
+
217
+ _startPolling() {
218
+ this._pollTimer = setInterval(() => {
219
+ const changed = this.clearInterrupt();
220
+ if (changed) this._dispatch(changed);
221
+ }, 5);
222
+ }
223
+
224
+ _dispatch(changed) {
225
+ if (this._callback) this._callback(changed);
226
+ for (const [n, handlers] of Object.entries(this._watchers)) {
227
+ if ((changed >> n) & 1) {
228
+ handlers.forEach(h => h(null, this.readSync ? undefined : 0));
229
+ }
230
+ }
231
+ }
232
+
233
+ /**
234
+ * Read current pin states and return bitmask of pins that changed.
235
+ * Also clears the chip's INT line.
236
+ * @returns {number} 8-bit changed-pin bitmask.
237
+ */
238
+ clearInterrupt() {
239
+ const current = this._readPort();
240
+ const changed = (current ^ this._prev) & 0xFF;
241
+ this._prev = current;
242
+ return changed;
243
+ }
244
+ }
245
+
246
+ /**
247
+ * Full GPIO proxy — adds watch/unwatch for interrupt-driven input.
248
+ */
249
+ class _FullPin extends _Pin {
250
+ /**
251
+ * @param {Pcf8574Full} chip - Parent full driver instance.
252
+ * @param {number} n - Pin index.
253
+ * @param {string} direction - 'in' or 'out'.
254
+ */
255
+ constructor(chip, n, direction) {
256
+ super(chip, n, direction);
257
+ }
258
+
259
+ /**
260
+ * Register a handler for pin state changes.
261
+ * Requires configureInterrupt() to have been called on the driver.
262
+ * @param {function} handler - Called with (err, value) when pin changes.
263
+ */
264
+ watch(handler) {
265
+ const n = this._n;
266
+ if (!this._chip._watchers[n]) this._chip._watchers[n] = [];
267
+ const current = this.readSync();
268
+ this._chip._watchers[n].push((err) => {
269
+ handler(err, this.readSync());
270
+ });
271
+ }
272
+
273
+ /**
274
+ * Remove a previously registered handler.
275
+ * @param {function} handler - The handler to remove.
276
+ */
277
+ unwatch(handler) {
278
+ const n = this._n;
279
+ if (this._chip._watchers[n]) {
280
+ this._chip._watchers[n] = this._chip._watchers[n].filter(h => h !== handler);
281
+ }
282
+ }
283
+
284
+ /** Remove all handlers for this pin. */
285
+ unwatchAll() {
286
+ this._chip._watchers[this._n] = [];
287
+ }
288
+
289
+ /**
290
+ * Invert active-low polarity for this pin.
291
+ * @param {boolean} invert - true to invert read/write values.
292
+ */
293
+ setActiveLow(invert) {
294
+ this._activeLow = !!invert;
295
+ }
296
+ }
297
+
298
+ module.exports = { Pcf8574Minimal, Pcf8574Full };