periph 1.5.0 → 1.5.2
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 +19 -0
- package/examples/color/as7262/complete.js +60 -0
- package/examples/color/as7262/demo.js +65 -0
- package/examples/color/as7262/minimal.js +23 -0
- package/examples/environmental/aht10/complete.js +37 -0
- package/examples/environmental/aht10/demo.js +43 -0
- package/examples/environmental/aht10/minimal.js +12 -0
- package/examples/environmental/aht20/complete.js +41 -0
- package/examples/environmental/aht20/demo.js +49 -0
- package/examples/environmental/aht20/minimal.js +12 -0
- package/examples/environmental/am2320/complete.js +34 -0
- package/examples/environmental/am2320/demo.js +46 -0
- package/examples/environmental/am2320/minimal.js +21 -0
- package/examples/humidity/hdc1008/complete.js +39 -0
- package/examples/humidity/hdc1008/demo.js +46 -0
- package/examples/humidity/hdc1008/minimal.js +22 -0
- package/examples/humidity/hdc1080/complete.js +34 -0
- package/examples/humidity/hdc1080/demo.js +46 -0
- package/examples/humidity/hdc1080/minimal.js +22 -0
- package/examples/humidity/hs30xx/complete.js +43 -0
- package/examples/humidity/hs30xx/demo.js +40 -0
- package/examples/humidity/hs30xx/minimal.js +21 -0
- package/examples/humidity/hs40xx/complete.js +54 -0
- package/examples/humidity/hs40xx/demo.js +38 -0
- package/examples/humidity/hs40xx/minimal.js +21 -0
- package/examples/io_expander/tca9554/complete.js +63 -0
- package/examples/io_expander/tca9554/demo.js +58 -0
- package/examples/io_expander/tca9554/minimal.js +20 -0
- package/examples/led/ht16k33/complete.js +45 -0
- package/examples/led/ht16k33/demo.js +54 -0
- package/examples/led/ht16k33/minimal.js +24 -0
- package/examples/led/is31fl3731/complete.js +60 -0
- package/examples/led/is31fl3731/demo.js +72 -0
- package/examples/led/is31fl3731/minimal.js +26 -0
- package/examples/led/pca9685/complete.js +43 -0
- package/examples/led/pca9685/demo.js +77 -0
- package/examples/led/pca9685/minimal.js +24 -0
- package/examples/power/ina260/complete.js +38 -0
- package/examples/power/ina260/demo.js +32 -0
- package/examples/power/ina260/minimal.js +11 -0
- package/examples/power/lc709203f/complete.js +57 -0
- package/examples/power/lc709203f/demo.js +34 -0
- package/examples/power/lc709203f/minimal.js +11 -0
- package/examples/power/lm25066/complete.js +54 -0
- package/examples/power/lm25066/demo.js +41 -0
- package/examples/power/lm25066/minimal.js +12 -0
- package/examples/power/ltc4151/complete.js +40 -0
- package/examples/power/ltc4151/demo.js +49 -0
- package/examples/power/ltc4151/minimal.js +11 -0
- package/examples/power/max17048/complete.js +68 -0
- package/examples/power/max17048/demo.js +56 -0
- package/examples/power/max17048/minimal.js +19 -0
- package/examples/pressure/lps22hb/complete.js +43 -0
- package/examples/pressure/lps22hb/demo.js +43 -0
- package/examples/pressure/lps22hb/minimal.js +19 -0
- package/examples/pressure/mpl115a2/complete.js +25 -0
- package/examples/pressure/mpl115a2/demo.js +30 -0
- package/examples/pressure/mpl115a2/minimal.js +20 -0
- package/package.json +1 -1
- package/src/chips/color/as7262.js +419 -0
- package/src/chips/environmental/aht10.js +186 -0
- package/src/chips/environmental/aht20.js +226 -0
- package/src/chips/environmental/am2320.js +237 -0
- package/src/chips/humidity/hdc1008.js +262 -0
- package/src/chips/humidity/hdc1080.js +208 -0
- package/src/chips/humidity/hs30xx.js +254 -0
- package/src/chips/humidity/hs40xx.js +382 -0
- package/src/chips/io_expander/tca9554.js +407 -0
- package/src/chips/led/ht16k33.js +273 -0
- package/src/chips/led/is31fl3731.js +501 -0
- package/src/chips/led/pca9685.js +350 -0
- package/src/chips/power/ina260.js +229 -0
- package/src/chips/power/lc709203f.js +376 -0
- package/src/chips/power/lm25066.js +370 -0
- package/src/chips/power/ltc4151.js +297 -0
- package/src/chips/power/max17048.js +302 -0
- package/src/chips/pressure/lps22hb.js +257 -0
- package/src/chips/pressure/mpl115a2.js +171 -0
- package/src/discovery/registry.js +56 -14
|
@@ -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
|
+
* TCA9554 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. Every input has a fixed 100 kΩ pull-up to VCC 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 Tca9554Minimal {
|
|
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–0x27 from A2..A0).
|
|
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 {Tca9554Minimal} 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
|
+
* TCA9554 full interface — polarity, whole-port direction, interrupts, raw access.
|
|
230
|
+
*/
|
|
231
|
+
class Tca9554Full extends Tca9554Minimal {
|
|
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. There is no RESET pin; only a VCC power cycle resets the chip.
|
|
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 = { Tca9554Minimal, Tca9554Full };
|
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _CMD_SYSTEM = 0x20;
|
|
4
|
+
const _CMD_DISPLAY = 0x80;
|
|
5
|
+
const _CMD_DIMMING = 0xE0;
|
|
6
|
+
const _CMD_KEY_PTR = 0x40;
|
|
7
|
+
const _CMD_INT_PTR = 0x60;
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* HT16K33 16x8 LED matrix driver — minimal interface.
|
|
11
|
+
*
|
|
12
|
+
* Drives up to 16 ROW x 8 COM = 128 LEDs through a 16-byte display RAM
|
|
13
|
+
* shadowed in the driver. I2C address 0x70 (A0-A2 select 0x70-0x77).
|
|
14
|
+
* The constructor starts the initialization sequence (oscillator on, ROW15
|
|
15
|
+
* as a ROW output, brightness 16/16, blink off, RAM cleared, display on);
|
|
16
|
+
* every bus method waits for it, and `ready` exposes it.
|
|
17
|
+
*
|
|
18
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device.
|
|
19
|
+
*/
|
|
20
|
+
class HT16K33Minimal {
|
|
21
|
+
constructor(connection) {
|
|
22
|
+
this._conn = connection;
|
|
23
|
+
this._ram = Buffer.alloc(16);
|
|
24
|
+
this._brightness = 15;
|
|
25
|
+
this._blink = 0;
|
|
26
|
+
this._display = true;
|
|
27
|
+
this._rowInt = 0xA0;
|
|
28
|
+
this._ready = this._init();
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/** Wait for the init sequence started by the constructor. @returns {Promise<void>} */
|
|
32
|
+
get ready() { return this._ready; }
|
|
33
|
+
|
|
34
|
+
async _init() {
|
|
35
|
+
await this._command(_CMD_SYSTEM | 0x01);
|
|
36
|
+
await this._command(this._rowInt);
|
|
37
|
+
await this._command(_CMD_DIMMING | this._brightness);
|
|
38
|
+
await this._conn.write(Buffer.concat([Buffer.from([0x00]), this._ram]));
|
|
39
|
+
await this._applyDisplay();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async _command(byte) {
|
|
43
|
+
await this._conn.write(Buffer.from([byte]));
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
async _applyDisplay() {
|
|
47
|
+
await this._command(_CMD_DISPLAY | (this._blink << 1) | (this._display ? 1 : 0));
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Zero the shadow RAM and write it to the chip.
|
|
52
|
+
* @returns {Promise<void>}
|
|
53
|
+
*/
|
|
54
|
+
async clear() {
|
|
55
|
+
await this._ready;
|
|
56
|
+
this._ram.fill(0);
|
|
57
|
+
await this.show();
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Set or clear one LED in the shadow RAM (does not write to the chip).
|
|
62
|
+
* @param {number} com - COM line, 0-7.
|
|
63
|
+
* @param {number} row - ROW line, 0-15.
|
|
64
|
+
* @param {boolean} [on=true] - true to light the LED.
|
|
65
|
+
*/
|
|
66
|
+
setPixel(com, row, on = true) {
|
|
67
|
+
if (!Number.isInteger(com) || com < 0 || com > 7) throw new RangeError('com must be in 0..7');
|
|
68
|
+
if (!Number.isInteger(row) || row < 0 || row > 15) throw new RangeError('row must be in 0..15');
|
|
69
|
+
const idx = 2 * com + (row >> 3);
|
|
70
|
+
const mask = 1 << (row & 7);
|
|
71
|
+
if (on) this._ram[idx] |= mask;
|
|
72
|
+
else this._ram[idx] &= ~mask & 0xFF;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Write raw RAM bytes at `address` and update the shadow RAM.
|
|
77
|
+
* @param {number[]|Buffer} data - 1-16 bytes of display data.
|
|
78
|
+
* @param {number} [address=0] - Start RAM address, 0-15.
|
|
79
|
+
* @returns {Promise<void>}
|
|
80
|
+
*/
|
|
81
|
+
async writeRaw(data, address = 0) {
|
|
82
|
+
const buf = Buffer.from(data);
|
|
83
|
+
if (address < 0 || buf.length < 1 || address + buf.length > 16) {
|
|
84
|
+
throw new RangeError('address + data.length must be within 0..16');
|
|
85
|
+
}
|
|
86
|
+
await this._ready;
|
|
87
|
+
buf.copy(this._ram, address);
|
|
88
|
+
await this._conn.write(Buffer.concat([Buffer.from([address]), buf]));
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Write the whole 16-byte shadow RAM to the chip.
|
|
93
|
+
* @returns {Promise<void>}
|
|
94
|
+
*/
|
|
95
|
+
async show() {
|
|
96
|
+
await this._ready;
|
|
97
|
+
await this._conn.write(Buffer.concat([Buffer.from([0x00]), this._ram]));
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Set the dimming level.
|
|
102
|
+
* @param {number} level - 0-15, giving (level + 1)/16 duty.
|
|
103
|
+
* @returns {Promise<void>}
|
|
104
|
+
*/
|
|
105
|
+
async setBrightness(level) {
|
|
106
|
+
if (!Number.isInteger(level) || level < 0 || level > 15) throw new RangeError('level must be in 0..15');
|
|
107
|
+
await this._ready;
|
|
108
|
+
this._brightness = level;
|
|
109
|
+
await this._command(_CMD_DIMMING | level);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
HT16K33Minimal.COMMONS = 8;
|
|
114
|
+
HT16K33Minimal.ROWS = 16;
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* HT16K33 full interface — display control, standby, key scan, INT output.
|
|
118
|
+
*
|
|
119
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I2C connection pointing at the device.
|
|
120
|
+
*/
|
|
121
|
+
class HT16K33Full extends HT16K33Minimal {
|
|
122
|
+
constructor(connection) {
|
|
123
|
+
super(connection);
|
|
124
|
+
this._callback = null;
|
|
125
|
+
this._intPinUsed = null;
|
|
126
|
+
this._pollTimer = null;
|
|
127
|
+
this._polling = false;
|
|
128
|
+
this._edgeHandler = () => { this._service(); };
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Turn the display output on (keeps the blink setting). @returns {Promise<void>} */
|
|
132
|
+
async displayOn() {
|
|
133
|
+
await this._ready;
|
|
134
|
+
this._display = true;
|
|
135
|
+
await this._applyDisplay();
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Turn the display output off (keeps the blink setting). @returns {Promise<void>} */
|
|
139
|
+
async displayOff() {
|
|
140
|
+
await this._ready;
|
|
141
|
+
this._display = false;
|
|
142
|
+
await this._applyDisplay();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Set the blink frequency.
|
|
147
|
+
* @param {number} frequency - 0 (off), 2, 1 or 0.5 Hz.
|
|
148
|
+
* @returns {Promise<void>}
|
|
149
|
+
*/
|
|
150
|
+
async setBlink(frequency) {
|
|
151
|
+
const codes = new Map([[0, 0], [2, 1], [1, 2], [0.5, 3]]);
|
|
152
|
+
if (!codes.has(frequency)) throw new RangeError('frequency must be 0, 2, 1 or 0.5');
|
|
153
|
+
await this._ready;
|
|
154
|
+
this._blink = codes.get(frequency);
|
|
155
|
+
await this._applyDisplay();
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** Stop the oscillator (display off, key scan stopped). @returns {Promise<void>} */
|
|
159
|
+
async standby() {
|
|
160
|
+
await this._ready;
|
|
161
|
+
await this._command(_CMD_SYSTEM);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/** Start the oscillator and restore display, dimming, ROW/INT and RAM. @returns {Promise<void>} */
|
|
165
|
+
async wake() {
|
|
166
|
+
await this._ready;
|
|
167
|
+
await this._command(_CMD_SYSTEM | 0x01);
|
|
168
|
+
await this._command(this._rowInt);
|
|
169
|
+
await this._command(_CMD_DIMMING | this._brightness);
|
|
170
|
+
await this.show();
|
|
171
|
+
await this._applyDisplay();
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Select the function of the ROW15/INT pin.
|
|
176
|
+
* @param {boolean} enable - true makes the pin an interrupt output (ROW15/K13 unusable).
|
|
177
|
+
* @param {boolean} [activeHigh=false] - INT polarity when enabled.
|
|
178
|
+
* @returns {Promise<void>}
|
|
179
|
+
*/
|
|
180
|
+
async setIntOutput(enable, activeHigh = false) {
|
|
181
|
+
await this._ready;
|
|
182
|
+
this._rowInt = enable ? (activeHigh ? 0xA3 : 0xA1) : 0xA0;
|
|
183
|
+
await this._command(this._rowInt);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Read and clear the key data RAM.
|
|
188
|
+
* @returns {Promise<number>} Key bitmask, bit k*13 + (K-1) for key input K (1-13) on key source KSk (0-2).
|
|
189
|
+
*/
|
|
190
|
+
async readKeys() {
|
|
191
|
+
await this._ready;
|
|
192
|
+
await this._conn.write(Buffer.from([_CMD_KEY_PTR]));
|
|
193
|
+
const b = await this._conn.read(6);
|
|
194
|
+
let mask = 0;
|
|
195
|
+
for (let k = 0; k < 3; k++) {
|
|
196
|
+
const word = (b[2 * k] | (b[2 * k + 1] << 8)) & 0x1FFF;
|
|
197
|
+
mask += word * Math.pow(2, 13 * k);
|
|
198
|
+
}
|
|
199
|
+
return mask;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Read the INT flag. @returns {Promise<boolean>} true if a key press is pending. */
|
|
203
|
+
async keyPressed() {
|
|
204
|
+
await this._ready;
|
|
205
|
+
await this._conn.write(Buffer.from([_CMD_INT_PTR]));
|
|
206
|
+
const b = await this._conn.read(1);
|
|
207
|
+
return (b[0] & 0x01) !== 0;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Read one LED state from the shadow RAM.
|
|
212
|
+
* @param {number} com - COM line, 0-7.
|
|
213
|
+
* @param {number} row - ROW line, 0-15.
|
|
214
|
+
* @returns {boolean}
|
|
215
|
+
*/
|
|
216
|
+
getPixel(com, row) {
|
|
217
|
+
if (com < 0 || com > 7 || row < 0 || row > 15) throw new RangeError('com 0..7, row 0..15');
|
|
218
|
+
return ((this._ram[2 * com + (row >> 3)] >> (row & 7)) & 1) === 1;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Check the INT flag and, if set, read (and clear) the key data.
|
|
223
|
+
* @returns {Promise<number>} Key bitmask, or 0 if no key is pending.
|
|
224
|
+
*/
|
|
225
|
+
async pollInterrupt() {
|
|
226
|
+
if (!(await this.keyPressed())) return 0;
|
|
227
|
+
return this.readKeys();
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Subscribe to key-press events. Delivery is via the INT line
|
|
232
|
+
* (options.intPin or connection.intPin, edge per the configured
|
|
233
|
+
* polarity) or a 20 ms polling timer.
|
|
234
|
+
* @param {function} callback - Called with (keys).
|
|
235
|
+
* @param {object} [options]
|
|
236
|
+
* @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
|
|
237
|
+
* @returns {Promise<void>}
|
|
238
|
+
*/
|
|
239
|
+
async onInterrupt(callback, options = {}) {
|
|
240
|
+
this._callback = callback;
|
|
241
|
+
const intPin = options.intPin ?? this._conn.intPin ?? null;
|
|
242
|
+
this._intPinUsed = intPin;
|
|
243
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
244
|
+
if (intPin) {
|
|
245
|
+
await intPin.onEdge(this._edgeHandler, this._rowInt === 0xA3 ? 'rising' : 'falling');
|
|
246
|
+
} else {
|
|
247
|
+
this._pollTimer = setInterval(() => { this._service(); }, 20);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/** Unsubscribe and stop delivery. @returns {Promise<void>} */
|
|
252
|
+
async offInterrupt() {
|
|
253
|
+
if (this._intPinUsed) {
|
|
254
|
+
await this._intPinUsed.offEdge(this._edgeHandler);
|
|
255
|
+
this._intPinUsed = null;
|
|
256
|
+
}
|
|
257
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
258
|
+
this._callback = null;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async _service() {
|
|
262
|
+
if (this._polling) return;
|
|
263
|
+
this._polling = true;
|
|
264
|
+
try {
|
|
265
|
+
const keys = await this.pollInterrupt();
|
|
266
|
+
if (keys && this._callback) this._callback(keys);
|
|
267
|
+
} finally {
|
|
268
|
+
this._polling = false;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
module.exports = { HT16K33Minimal, HT16K33Full };
|