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