periph 1.4.0 → 1.5.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.
- package/README.md +27 -0
- package/examples/adc_dac/ad5251/complete.js +66 -0
- package/examples/adc_dac/ad5251/demo.js +69 -0
- package/examples/adc_dac/ad5251/minimal.js +23 -0
- package/examples/adc_dac/ad5252/complete.js +66 -0
- package/examples/adc_dac/ad5252/demo.js +69 -0
- package/examples/adc_dac/ad5252/minimal.js +23 -0
- package/examples/adc_dac/ads7828/complete.js +46 -0
- package/examples/adc_dac/ads7828/demo.js +45 -0
- package/examples/adc_dac/ads7828/minimal.js +17 -0
- package/examples/adc_dac/cat5171/complete.js +52 -0
- package/examples/adc_dac/cat5171/demo.js +59 -0
- package/examples/adc_dac/cat5171/minimal.js +23 -0
- package/examples/adc_dac/cs43l22/complete.js +88 -0
- package/examples/adc_dac/cs43l22/demo.js +56 -0
- package/examples/adc_dac/cs43l22/minimal.js +20 -0
- package/examples/adc_dac/ds1881/complete.js +62 -0
- package/examples/adc_dac/ds1881/demo.js +61 -0
- package/examples/adc_dac/ds1881/minimal.js +25 -0
- package/examples/adc_dac/ds3502/complete.js +46 -0
- package/examples/adc_dac/ds3502/demo.js +54 -0
- package/examples/adc_dac/ds3502/minimal.js +28 -0
- package/examples/adc_dac/mcp3422/complete.js +48 -0
- package/examples/adc_dac/mcp3422/demo.js +49 -0
- package/examples/adc_dac/mcp3422/minimal.js +25 -0
- package/examples/display/sh1106/complete.js +101 -0
- package/examples/display/sh1106/demo.js +68 -0
- package/examples/display/sh1106/minimal.js +25 -0
- package/examples/display/ssd1305/complete.js +129 -0
- package/examples/display/ssd1305/demo.js +71 -0
- package/examples/display/ssd1305/minimal.js +25 -0
- package/examples/display/ssd1306/complete.js +115 -0
- package/examples/display/ssd1306/demo.js +70 -0
- package/examples/display/ssd1306/minimal.js +25 -0
- package/examples/environmental/am2315/complete.js +34 -0
- package/examples/environmental/am2315/demo.js +46 -0
- package/examples/environmental/am2315/minimal.js +21 -0
- package/examples/humidity/htu21df/complete.js +49 -0
- package/examples/humidity/htu21df/demo.js +50 -0
- package/examples/humidity/htu21df/minimal.js +22 -0
- package/examples/humidity/sht31/complete.js +53 -0
- package/examples/humidity/sht31/demo.js +53 -0
- package/examples/humidity/sht31/minimal.js +21 -0
- package/examples/humidity/si7021/complete.js +55 -0
- package/examples/humidity/si7021/demo.js +58 -0
- package/examples/humidity/si7021/minimal.js +21 -0
- package/examples/imu/bno055/complete.js +85 -0
- package/examples/imu/bno055/demo.js +70 -0
- package/examples/imu/bno055/minimal.js +20 -0
- package/examples/io_expander/pca6408a/complete.js +63 -0
- package/examples/io_expander/pca6408a/demo.js +58 -0
- package/examples/io_expander/pca6408a/minimal.js +20 -0
- package/examples/io_expander/pca6416a/complete.js +68 -0
- package/examples/io_expander/pca6416a/demo.js +60 -0
- package/examples/io_expander/pca6416a/minimal.js +20 -0
- package/examples/io_expander/pca9536/complete.js +63 -0
- package/examples/io_expander/pca9536/demo.js +57 -0
- package/examples/io_expander/pca9536/minimal.js +20 -0
- package/examples/io_expander/pca9539/complete.js +68 -0
- package/examples/io_expander/pca9539/demo.js +60 -0
- package/examples/io_expander/pca9539/minimal.js +20 -0
- package/examples/light/max44009/complete.js +56 -0
- package/examples/light/max44009/demo.js +49 -0
- package/examples/light/max44009/minimal.js +22 -0
- package/examples/memory/at24c02n/complete.js +39 -0
- package/examples/memory/at24c02n/demo.js +39 -0
- package/examples/memory/at24c02n/minimal.js +15 -0
- package/examples/memory/at24c64/complete.js +39 -0
- package/examples/memory/at24c64/demo.js +39 -0
- package/examples/memory/at24c64/minimal.js +15 -0
- package/examples/memory/cat24c512/complete.js +39 -0
- package/examples/memory/cat24c512/demo.js +39 -0
- package/examples/memory/cat24c512/minimal.js +15 -0
- package/examples/other/pca9541/complete.js +52 -0
- package/examples/other/pca9541/demo.js +61 -0
- package/examples/other/pca9541/minimal.js +23 -0
- package/examples/temperature/tmp102/complete.js +74 -0
- package/examples/temperature/tmp102/demo.js +71 -0
- package/examples/temperature/tmp102/minimal.js +20 -0
- package/examples/tof/vl6180x/complete.js +62 -0
- package/examples/tof/vl6180x/demo.js +77 -0
- package/examples/tof/vl6180x/minimal.js +23 -0
- package/package.json +1 -1
- package/src/chips/adc_dac/ad5251.js +345 -0
- package/src/chips/adc_dac/ad5252.js +345 -0
- package/src/chips/adc_dac/ads7828.js +225 -0
- package/src/chips/adc_dac/cat5171.js +213 -0
- package/src/chips/adc_dac/cs43l22.js +645 -0
- package/src/chips/adc_dac/ds1881.js +357 -0
- package/src/chips/adc_dac/ds3502.js +241 -0
- package/src/chips/adc_dac/mcp3422.js +312 -0
- package/src/chips/display/mono_oled.js +182 -0
- package/src/chips/display/sh1106.js +496 -0
- package/src/chips/display/ssd1305.js +655 -0
- package/src/chips/display/ssd1306.js +458 -0
- package/src/chips/environmental/am2315.js +237 -0
- package/src/chips/humidity/htu21df.js +222 -0
- package/src/chips/humidity/sht31.js +284 -0
- package/src/chips/humidity/si7021.js +282 -0
- package/src/chips/imu/bno055.js +496 -0
- package/src/chips/io_expander/pca6408a.js +407 -0
- package/src/chips/io_expander/pca6416a.js +455 -0
- package/src/chips/io_expander/pca9536.js +398 -0
- package/src/chips/io_expander/pca9539.js +457 -0
- package/src/chips/light/max44009.js +336 -0
- package/src/chips/memory/at24c02n.js +220 -0
- package/src/chips/memory/at24c64.js +220 -0
- package/src/chips/memory/cat24c512.js +221 -0
- package/src/chips/other/pca9541.js +315 -0
- package/src/chips/temperature/tmp102.js +361 -0
- package/src/chips/tof/vl6180x.js +580 -0
- package/src/discovery/registry.js +65 -13
|
@@ -0,0 +1,407 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const REG_INPUT = 0x00;
|
|
4
|
+
const REG_OUTPUT = 0x01;
|
|
5
|
+
const REG_POLARITY = 0x02;
|
|
6
|
+
const REG_CONFIG = 0x03;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* PCA6408A 8-bit I/O expander (I²C / SMBus) — minimal interface.
|
|
10
|
+
*
|
|
11
|
+
* Provides 8 bidirectional GPIO pins (P0–P7) via a pin() factory. Each
|
|
12
|
+
* returned pin exposes async read()/write() (Connection is async everywhere,
|
|
13
|
+
* so a synchronous pin proxy cannot return a value); pin.asGpio() gives a
|
|
14
|
+
* synchronous opengpio-shaped facade. Shadows of OUTPUT and CONFIG make a
|
|
15
|
+
* single-pin change one register write. The chip has no internal pull-ups and
|
|
16
|
+
* no register auto-increment: every register is accessed one byte at a time.
|
|
17
|
+
*
|
|
18
|
+
* Registers are accessed after the constructor's initialisation sequence
|
|
19
|
+
* (`this.ready`) has finished.
|
|
20
|
+
*/
|
|
21
|
+
class Pca6408aMinimal {
|
|
22
|
+
/**
|
|
23
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus (no PEC) register connection.
|
|
24
|
+
* @param {number} [addr=0x20] - 7-bit I²C address (0x20 ADDR = VSS, 0x21 ADDR = VDD(P)).
|
|
25
|
+
*/
|
|
26
|
+
constructor(connection, addr = 0x20) {
|
|
27
|
+
this._conn = connection;
|
|
28
|
+
this._addr = addr;
|
|
29
|
+
this._shadow = 0xFF;
|
|
30
|
+
this._direction = 0xFF;
|
|
31
|
+
this._polarity = 0x00;
|
|
32
|
+
this._lastInput = 0x00;
|
|
33
|
+
this._ready = this._init();
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Wait for the init sequence started by the constructor. @returns {Promise<void>} */
|
|
37
|
+
get ready() { return this._ready; }
|
|
38
|
+
|
|
39
|
+
async _init() {
|
|
40
|
+
await this._conn.writeReg(REG_OUTPUT, 0xFF);
|
|
41
|
+
await this._conn.writeReg(REG_POLARITY, 0x00);
|
|
42
|
+
await this._conn.writeReg(REG_CONFIG, 0xFF);
|
|
43
|
+
this._lastInput = (await this._conn.readReg(REG_INPUT, 1))[0];
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async _regRead(reg) {
|
|
47
|
+
await this._ready;
|
|
48
|
+
return (await this._conn.readReg(reg, 1))[0];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async _regWrite(reg, value) {
|
|
52
|
+
await this._ready;
|
|
53
|
+
await this._conn.writeReg(reg, value & 0xFF);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async _writeOutput(mask) {
|
|
57
|
+
this._shadow = mask & 0xFF;
|
|
58
|
+
await this._regWrite(REG_OUTPUT, this._shadow);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async _setPin(n, value) {
|
|
62
|
+
if (value) this._shadow |= (1 << n);
|
|
63
|
+
else this._shadow &= ~(1 << n);
|
|
64
|
+
await this._writeOutput(this._shadow);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async _setDirection(n, input) {
|
|
68
|
+
if (input) {
|
|
69
|
+
this._direction |= (1 << n);
|
|
70
|
+
} else {
|
|
71
|
+
// Latch the output level first so the pin never glitches.
|
|
72
|
+
await this._regWrite(REG_OUTPUT, this._shadow);
|
|
73
|
+
this._direction &= ~(1 << n);
|
|
74
|
+
}
|
|
75
|
+
await this._regWrite(REG_CONFIG, this._direction);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Return a Pin proxy for pin n (0–7). All eight pins are bidirectional.
|
|
80
|
+
*
|
|
81
|
+
* @param {number} n - Pin index (0–7).
|
|
82
|
+
* @param {string} [direction='in'] - Initial direction: 'in' or 'out'.
|
|
83
|
+
* @returns {_Pin} Pin proxy.
|
|
84
|
+
*/
|
|
85
|
+
pin(n, direction = 'in') {
|
|
86
|
+
return new _Pin(this, n, direction);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Read all 8 pins as a bitmask (INPUT register, actual pin levels).
|
|
91
|
+
*
|
|
92
|
+
* @param {number} [port=0] - Ignored; the chip has a single port.
|
|
93
|
+
* @returns {Promise<number>} 8-bit bitmask (bit 0 = P0).
|
|
94
|
+
*/
|
|
95
|
+
async readPort(port = 0) {
|
|
96
|
+
return this._regRead(REG_INPUT);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Write all 8 output bits via OUTPUT and update the shadow. Bits of input
|
|
101
|
+
* pins are stored but have no effect until the pin becomes an output.
|
|
102
|
+
*
|
|
103
|
+
* @param {number} [port=0] - Ignored; the chip has a single port.
|
|
104
|
+
* @param {number} mask - 8-bit output mask.
|
|
105
|
+
* @returns {Promise<void>}
|
|
106
|
+
*/
|
|
107
|
+
async writePort(port = 0, mask = 0xFF) {
|
|
108
|
+
await this._writeOutput(mask);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* GPIO proxy for a single pin. Obtain via pin(n); do not instantiate directly.
|
|
114
|
+
*/
|
|
115
|
+
class _Pin {
|
|
116
|
+
/**
|
|
117
|
+
* @param {Pca6408aMinimal} chip - Parent driver instance.
|
|
118
|
+
* @param {number} n - Pin index (0–7).
|
|
119
|
+
* @param {string} direction - 'in' or 'out'.
|
|
120
|
+
*/
|
|
121
|
+
constructor(chip, n, direction) {
|
|
122
|
+
this._chip = chip;
|
|
123
|
+
this._n = n & 7;
|
|
124
|
+
this._direction = direction;
|
|
125
|
+
this._ready = direction === 'out' ? chip._setDirection(this._n, false) : Promise.resolve();
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/** @type {string} Pin direction ('in' or 'out'). */
|
|
129
|
+
get direction() { return this._direction; }
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Read the pin (actual logic level).
|
|
133
|
+
* @returns {Promise<number>} 0 or 1.
|
|
134
|
+
*/
|
|
135
|
+
async read() {
|
|
136
|
+
await this._ready;
|
|
137
|
+
return ((await this._chip.readPort()) >> this._n) & 1;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Write the pin's output latch.
|
|
142
|
+
* @param {number} value - 0 or 1.
|
|
143
|
+
* @returns {Promise<void>}
|
|
144
|
+
*/
|
|
145
|
+
async write(value) {
|
|
146
|
+
await this._ready;
|
|
147
|
+
await this._chip._setPin(this._n, value ? 1 : 0);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Set pin direction (writes CONFIG).
|
|
152
|
+
* @param {string} direction - 'in' or 'out'.
|
|
153
|
+
* @returns {Promise<void>}
|
|
154
|
+
*/
|
|
155
|
+
async setDirection(direction) {
|
|
156
|
+
await this._ready;
|
|
157
|
+
this._direction = direction;
|
|
158
|
+
await this._chip._setDirection(this._n, direction !== 'out');
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Return a synchronous opengpio-shaped Input/Output facade for this pin.
|
|
163
|
+
*
|
|
164
|
+
* Output facades read back the shadow register (authoritative) and write
|
|
165
|
+
* through fire-and-forget. Input facades are backed by a 5 ms background
|
|
166
|
+
* poll and are therefore eventually consistent; call stop() to release it.
|
|
167
|
+
*
|
|
168
|
+
* @returns {{direction: string, value: boolean, stop: function}} opengpio-shaped facade.
|
|
169
|
+
*/
|
|
170
|
+
asGpio() {
|
|
171
|
+
const pin = this;
|
|
172
|
+
if (this._direction === 'out') {
|
|
173
|
+
return {
|
|
174
|
+
get direction() { return 'out'; },
|
|
175
|
+
get value() { return ((pin._chip._shadow >> pin._n) & 1) === 1; },
|
|
176
|
+
set value(v) { pin._chip._setPin(pin._n, v ? 1 : 0); },
|
|
177
|
+
stop() {},
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
let cached = false;
|
|
181
|
+
const timer = setInterval(async () => { cached = (await pin.read()) === 1; }, 5);
|
|
182
|
+
return {
|
|
183
|
+
get direction() { return 'in'; },
|
|
184
|
+
get value() { return cached; },
|
|
185
|
+
set value(_v) { throw new Error('cannot set value on an input pin'); },
|
|
186
|
+
stop() { clearInterval(timer); },
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Release the pin (no-op; shadow state preserved). */
|
|
191
|
+
stop() {}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Full GPIO proxy — adds watch()/unwatch() for interrupt-driven input.
|
|
196
|
+
*/
|
|
197
|
+
class _FullPin extends _Pin {
|
|
198
|
+
/**
|
|
199
|
+
* Subscribe to this pin's edge events.
|
|
200
|
+
*
|
|
201
|
+
* At most one handler per pin; a second call replaces the first. The chip
|
|
202
|
+
* reports changes only; rising/falling is filtered in software. Delivery
|
|
203
|
+
* starts when the chip's onInterrupt() is called.
|
|
204
|
+
*
|
|
205
|
+
* @param {function} handler - Called with this pin when its state matches trigger.
|
|
206
|
+
* @param {string} [trigger='change'] - 'rising', 'falling', or 'change'.
|
|
207
|
+
* @returns {Promise<void>}
|
|
208
|
+
* @throws {Error} if the pin is configured as an output.
|
|
209
|
+
*/
|
|
210
|
+
async watch(handler, trigger = 'change') {
|
|
211
|
+
await this._ready;
|
|
212
|
+
if (((this._chip._direction >> this._n) & 1) === 0) throw new Error('cannot watch an output pin');
|
|
213
|
+
this._chip._watchers[this._n] = { handler, trigger };
|
|
214
|
+
if (!this._chip._intPinUsed && !this._chip._pollTimer && this._chip._conn.intPin) {
|
|
215
|
+
await this._chip._arm(this._chip._conn.intPin);
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Unsubscribe this pin's handler.
|
|
221
|
+
* @returns {Promise<void>}
|
|
222
|
+
*/
|
|
223
|
+
async unwatch() {
|
|
224
|
+
delete this._chip._watchers[this._n];
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* PCA6408A full interface — polarity, whole-port direction, interrupts, raw access.
|
|
230
|
+
*/
|
|
231
|
+
class Pca6408aFull extends Pca6408aMinimal {
|
|
232
|
+
/**
|
|
233
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus (no PEC) register connection.
|
|
234
|
+
* @param {number} [addr=0x20] - 7-bit I²C address.
|
|
235
|
+
*/
|
|
236
|
+
constructor(connection, addr = 0x20) {
|
|
237
|
+
super(connection, addr);
|
|
238
|
+
this._callback = null;
|
|
239
|
+
this._intPinUsed = null;
|
|
240
|
+
this._pollTimer = null;
|
|
241
|
+
this._polling = false;
|
|
242
|
+
this._level = 0x00;
|
|
243
|
+
this._watchers = {};
|
|
244
|
+
this._edgeHandler = () => { this._service(); };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Return a Full Pin proxy for pin n (0–7).
|
|
249
|
+
* @param {number} n - Pin index.
|
|
250
|
+
* @param {string} [direction='in'] - Initial direction.
|
|
251
|
+
* @returns {_FullPin} Full pin proxy with watch/unwatch support.
|
|
252
|
+
*/
|
|
253
|
+
pin(n, direction = 'in') {
|
|
254
|
+
return new _FullPin(this, n, direction);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Configure input polarity inversion (POLARITY ← mask).
|
|
259
|
+
* @param {number} mask - 8-bit mask: 1 = invert the INPUT bit.
|
|
260
|
+
* @returns {Promise<void>}
|
|
261
|
+
*/
|
|
262
|
+
async configurePolarity(mask) {
|
|
263
|
+
this._polarity = mask & 0xFF;
|
|
264
|
+
await this._regWrite(REG_POLARITY, this._polarity);
|
|
265
|
+
this._lastInput = await this._regRead(REG_INPUT);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Return the shadow CONFIG register (1 = input).
|
|
270
|
+
* @returns {number} 8-bit direction mask.
|
|
271
|
+
*/
|
|
272
|
+
getDirection() {
|
|
273
|
+
return this._direction;
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Write CONFIG in one transaction (1 = input, 0 = output). OUTPUT is
|
|
278
|
+
* written first so pins turning into outputs never glitch; the cached
|
|
279
|
+
* input value is refreshed afterwards to drop the false interrupt an
|
|
280
|
+
* output→input change can raise.
|
|
281
|
+
* @param {number} mask - 8-bit direction mask.
|
|
282
|
+
* @returns {Promise<void>}
|
|
283
|
+
*/
|
|
284
|
+
async setDirection(mask) {
|
|
285
|
+
await this._regWrite(REG_OUTPUT, this._shadow);
|
|
286
|
+
this._direction = mask & 0xFF;
|
|
287
|
+
await this._regWrite(REG_CONFIG, this._direction);
|
|
288
|
+
this._lastInput = await this._regRead(REG_INPUT);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/**
|
|
292
|
+
* Raw register read (debug / escape hatch).
|
|
293
|
+
* @param {number} reg - Register address 0–3.
|
|
294
|
+
* @returns {Promise<number>}
|
|
295
|
+
* @throws {Error} if reg is outside 0–3.
|
|
296
|
+
*/
|
|
297
|
+
async readRegister(reg) {
|
|
298
|
+
if (reg < 0 || reg > 3) throw new Error('register must be 0-3');
|
|
299
|
+
return this._regRead(reg);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Raw register write (1–3); the OUTPUT / POLARITY / CONFIG shadows are kept in sync.
|
|
304
|
+
* @param {number} reg - Register address 1–3 (INPUT is read-only).
|
|
305
|
+
* @param {number} value - 8-bit value.
|
|
306
|
+
* @returns {Promise<void>}
|
|
307
|
+
* @throws {Error} if reg is 0 or outside 1–3.
|
|
308
|
+
*/
|
|
309
|
+
async writeRegister(reg, value) {
|
|
310
|
+
if (reg < 1 || reg > 3) throw new Error('register must be 1-3 (INPUT is read-only)');
|
|
311
|
+
value &= 0xFF;
|
|
312
|
+
if (reg === REG_OUTPUT) this._shadow = value;
|
|
313
|
+
else if (reg === REG_POLARITY) this._polarity = value;
|
|
314
|
+
else this._direction = value;
|
|
315
|
+
await this._regWrite(reg, value);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Write the reset values (OUTPUT 0xFF, POLARITY 0x00, CONFIG 0xFF) and
|
|
320
|
+
* refresh the input cache. The RESET pin is external and not driven.
|
|
321
|
+
* @returns {Promise<void>}
|
|
322
|
+
*/
|
|
323
|
+
async reset() {
|
|
324
|
+
await this.offInterrupt();
|
|
325
|
+
await this._regWrite(REG_OUTPUT, 0xFF);
|
|
326
|
+
await this._regWrite(REG_POLARITY, 0x00);
|
|
327
|
+
await this._regWrite(REG_CONFIG, 0xFF);
|
|
328
|
+
this._shadow = 0xFF;
|
|
329
|
+
this._polarity = 0x00;
|
|
330
|
+
this._direction = 0xFF;
|
|
331
|
+
this._lastInput = await this._regRead(REG_INPUT);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
async _arm(intPin) {
|
|
335
|
+
this._intPinUsed = intPin;
|
|
336
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
337
|
+
if (intPin) {
|
|
338
|
+
await intPin.onEdge(this._edgeHandler, 'falling');
|
|
339
|
+
} else {
|
|
340
|
+
this._pollTimer = setInterval(() => { this._service(); }, 5);
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Subscribe to INT assertions. `callback(changedMask)` receives the mask
|
|
346
|
+
* of input pins whose level changed since the previous INPUT read.
|
|
347
|
+
* Delivery is via the INT line (options.intPin or connection.intPin,
|
|
348
|
+
* falling edge) or a 5 ms polling timer.
|
|
349
|
+
*
|
|
350
|
+
* @param {function} callback - Called with (changedMask).
|
|
351
|
+
* @param {object} [options]
|
|
352
|
+
* @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
|
|
353
|
+
* @returns {Promise<void>}
|
|
354
|
+
*/
|
|
355
|
+
async onInterrupt(callback, options = {}) {
|
|
356
|
+
this._callback = callback;
|
|
357
|
+
await this._arm(options.intPin ?? this._conn.intPin ?? null);
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
/**
|
|
361
|
+
* Unsubscribe and stop delivery.
|
|
362
|
+
* @returns {Promise<void>}
|
|
363
|
+
*/
|
|
364
|
+
async offInterrupt() {
|
|
365
|
+
if (this._intPinUsed) {
|
|
366
|
+
await this._intPinUsed.offEdge(this._edgeHandler);
|
|
367
|
+
this._intPinUsed = null;
|
|
368
|
+
}
|
|
369
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
370
|
+
this._callback = null;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
async _service() {
|
|
374
|
+
if (this._polling) return;
|
|
375
|
+
this._polling = true;
|
|
376
|
+
try {
|
|
377
|
+
const changed = await this.pollInterrupt();
|
|
378
|
+
if (!changed) return;
|
|
379
|
+
if (this._callback) this._callback(changed);
|
|
380
|
+
for (const [bit, w] of Object.entries(this._watchers)) {
|
|
381
|
+
if (!((changed >> bit) & 1)) continue;
|
|
382
|
+
const current = (this._level >> bit) & 1;
|
|
383
|
+
if (w.trigger === 'change' || (w.trigger === 'rising' && current === 1) ||
|
|
384
|
+
(w.trigger === 'falling' && current === 0)) {
|
|
385
|
+
w.handler(this.pin(Number(bit)));
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
} finally {
|
|
389
|
+
this._polling = false;
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Read INPUT (clearing INT) and return the changed-pin mask: the XOR of
|
|
395
|
+
* the new and the cached INPUT value, limited to pins configured as inputs.
|
|
396
|
+
* @returns {Promise<number>} 8-bit mask; bit n = 1 if input Pn changed.
|
|
397
|
+
*/
|
|
398
|
+
async pollInterrupt() {
|
|
399
|
+
const now = await this._regRead(REG_INPUT);
|
|
400
|
+
const changed = (now ^ this._lastInput) & this._direction;
|
|
401
|
+
this._lastInput = now;
|
|
402
|
+
this._level = now;
|
|
403
|
+
return changed;
|
|
404
|
+
}
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
module.exports = { Pca6408aMinimal, Pca6408aFull };
|