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.
Files changed (79) hide show
  1. package/README.md +19 -0
  2. package/examples/color/as7262/complete.js +60 -0
  3. package/examples/color/as7262/demo.js +65 -0
  4. package/examples/color/as7262/minimal.js +23 -0
  5. package/examples/environmental/aht10/complete.js +37 -0
  6. package/examples/environmental/aht10/demo.js +43 -0
  7. package/examples/environmental/aht10/minimal.js +12 -0
  8. package/examples/environmental/aht20/complete.js +41 -0
  9. package/examples/environmental/aht20/demo.js +49 -0
  10. package/examples/environmental/aht20/minimal.js +12 -0
  11. package/examples/environmental/am2320/complete.js +34 -0
  12. package/examples/environmental/am2320/demo.js +46 -0
  13. package/examples/environmental/am2320/minimal.js +21 -0
  14. package/examples/humidity/hdc1008/complete.js +39 -0
  15. package/examples/humidity/hdc1008/demo.js +46 -0
  16. package/examples/humidity/hdc1008/minimal.js +22 -0
  17. package/examples/humidity/hdc1080/complete.js +34 -0
  18. package/examples/humidity/hdc1080/demo.js +46 -0
  19. package/examples/humidity/hdc1080/minimal.js +22 -0
  20. package/examples/humidity/hs30xx/complete.js +43 -0
  21. package/examples/humidity/hs30xx/demo.js +40 -0
  22. package/examples/humidity/hs30xx/minimal.js +21 -0
  23. package/examples/humidity/hs40xx/complete.js +54 -0
  24. package/examples/humidity/hs40xx/demo.js +38 -0
  25. package/examples/humidity/hs40xx/minimal.js +21 -0
  26. package/examples/io_expander/tca9554/complete.js +63 -0
  27. package/examples/io_expander/tca9554/demo.js +58 -0
  28. package/examples/io_expander/tca9554/minimal.js +20 -0
  29. package/examples/led/ht16k33/complete.js +45 -0
  30. package/examples/led/ht16k33/demo.js +54 -0
  31. package/examples/led/ht16k33/minimal.js +24 -0
  32. package/examples/led/is31fl3731/complete.js +60 -0
  33. package/examples/led/is31fl3731/demo.js +72 -0
  34. package/examples/led/is31fl3731/minimal.js +26 -0
  35. package/examples/led/pca9685/complete.js +43 -0
  36. package/examples/led/pca9685/demo.js +77 -0
  37. package/examples/led/pca9685/minimal.js +24 -0
  38. package/examples/power/ina260/complete.js +38 -0
  39. package/examples/power/ina260/demo.js +32 -0
  40. package/examples/power/ina260/minimal.js +11 -0
  41. package/examples/power/lc709203f/complete.js +57 -0
  42. package/examples/power/lc709203f/demo.js +34 -0
  43. package/examples/power/lc709203f/minimal.js +11 -0
  44. package/examples/power/lm25066/complete.js +54 -0
  45. package/examples/power/lm25066/demo.js +41 -0
  46. package/examples/power/lm25066/minimal.js +12 -0
  47. package/examples/power/ltc4151/complete.js +40 -0
  48. package/examples/power/ltc4151/demo.js +49 -0
  49. package/examples/power/ltc4151/minimal.js +11 -0
  50. package/examples/power/max17048/complete.js +68 -0
  51. package/examples/power/max17048/demo.js +56 -0
  52. package/examples/power/max17048/minimal.js +19 -0
  53. package/examples/pressure/lps22hb/complete.js +43 -0
  54. package/examples/pressure/lps22hb/demo.js +43 -0
  55. package/examples/pressure/lps22hb/minimal.js +19 -0
  56. package/examples/pressure/mpl115a2/complete.js +25 -0
  57. package/examples/pressure/mpl115a2/demo.js +30 -0
  58. package/examples/pressure/mpl115a2/minimal.js +20 -0
  59. package/package.json +1 -1
  60. package/src/chips/color/as7262.js +419 -0
  61. package/src/chips/environmental/aht10.js +186 -0
  62. package/src/chips/environmental/aht20.js +226 -0
  63. package/src/chips/environmental/am2320.js +237 -0
  64. package/src/chips/humidity/hdc1008.js +262 -0
  65. package/src/chips/humidity/hdc1080.js +208 -0
  66. package/src/chips/humidity/hs30xx.js +254 -0
  67. package/src/chips/humidity/hs40xx.js +382 -0
  68. package/src/chips/io_expander/tca9554.js +407 -0
  69. package/src/chips/led/ht16k33.js +273 -0
  70. package/src/chips/led/is31fl3731.js +501 -0
  71. package/src/chips/led/pca9685.js +350 -0
  72. package/src/chips/power/ina260.js +229 -0
  73. package/src/chips/power/lc709203f.js +376 -0
  74. package/src/chips/power/lm25066.js +370 -0
  75. package/src/chips/power/ltc4151.js +297 -0
  76. package/src/chips/power/max17048.js +302 -0
  77. package/src/chips/pressure/lps22hb.js +257 -0
  78. package/src/chips/pressure/mpl115a2.js +171 -0
  79. package/src/discovery/registry.js +56 -14
@@ -0,0 +1,501 @@
1
+ 'use strict';
2
+
3
+ const _REG_COMMAND = 0xFD;
4
+ const _PAGE_FUNCTION = 0x0B;
5
+ const _REG_CTRL = 0x00;
6
+ const _REG_BLINK = 0x12;
7
+ const _REG_PWM = 0x24;
8
+
9
+ const _FN_CONFIG = 0x00;
10
+ const _FN_AUTOPLAY1 = 0x02;
11
+ const _FN_DISPLAY = 0x05;
12
+ const _FN_AUDIO_SYNC = 0x06;
13
+ const _FN_FRAME_STATE = 0x07;
14
+ const _FN_BREATH1 = 0x08;
15
+ const _FN_SHUTDOWN = 0x0A;
16
+ const _FN_AGC = 0x0B;
17
+ const _FN_AUDIO_RATE = 0x0C;
18
+
19
+ function _check(value, name, max) {
20
+ if (!Number.isInteger(value) || value < 0 || value > max) throw new RangeError(`${name} must be in 0..${max}`);
21
+ }
22
+
23
+ function _checkPixel(row, col) {
24
+ _check(row, 'row', 8);
25
+ _check(col, 'col', 15);
26
+ }
27
+
28
+ /**
29
+ * IS31FL3731 144-LED matrix driver (CharliePlex) — minimal interface.
30
+ *
31
+ * Lights individual LEDs of the 9 x 16 matrix with 8-bit PWM in Picture mode, drawing into frame 1.
32
+ * The constructor starts the initialization sequence (software shutdown while configuring, Picture
33
+ * mode showing frame 1, frame 1 initialised with all LED-control bits on and PWM/blink cleared, then
34
+ * normal operation); every bus method waits for it and `ready` exposes it. LED current is set by
35
+ * R_EXT (3.2 mA at 20 kOhm). The driver keeps a shadow copy of every frame because the chip only
36
+ * allows frame register reads in software shutdown. Bus calls are serialised internally.
37
+ */
38
+ class IS31FL3731Minimal {
39
+ /**
40
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (address 0x74-0x77).
41
+ */
42
+ constructor(connection) {
43
+ this._conn = connection;
44
+ this._page = null;
45
+ this._frame = 0;
46
+ this._pwm = Array.from({ length: 8 }, () => Buffer.alloc(144));
47
+ this._ctrl = Array.from({ length: 8 }, () => Buffer.alloc(18, 0xFF));
48
+ this._blink = Array.from({ length: 8 }, () => Buffer.alloc(18));
49
+ this._ic = 0;
50
+ this._be = 0;
51
+ this._bpt = 0;
52
+ this._chain = Promise.resolve();
53
+ this._ready = this._exec(async () => {
54
+ await this._funcWrite(_FN_SHUTDOWN, 0x00);
55
+ await this._funcWrite(_FN_CONFIG, Buffer.from([0x00, 0x00]));
56
+ await this._funcWrite(_FN_DISPLAY, 0x00);
57
+ await this._initFrame(0);
58
+ await this._funcWrite(_FN_SHUTDOWN, 0x01);
59
+ });
60
+ }
61
+
62
+ /** Wait for the init sequence started by the constructor. @returns {Promise<void>} */
63
+ get ready() { return this._ready; }
64
+
65
+ _exec(fn) {
66
+ const p = this._chain.then(fn);
67
+ this._chain = p.catch(() => {});
68
+ return p;
69
+ }
70
+
71
+ async _select(page) {
72
+ if (this._page !== page) {
73
+ await this._conn.writeReg(_REG_COMMAND, page);
74
+ this._page = page;
75
+ }
76
+ }
77
+
78
+ async _funcWrite(reg, data) {
79
+ await this._select(_PAGE_FUNCTION);
80
+ await this._conn.writeReg(reg, data);
81
+ }
82
+
83
+ async _funcRead(reg) {
84
+ await this._select(_PAGE_FUNCTION);
85
+ return (await this._conn.readReg(reg, 1))[0];
86
+ }
87
+
88
+ async _initFrame(frame) {
89
+ this._ctrl[frame].fill(0xFF);
90
+ this._blink[frame].fill(0);
91
+ this._pwm[frame].fill(0);
92
+ await this._select(frame);
93
+ await this._conn.writeReg(_REG_CTRL, Buffer.from(this._ctrl[frame]));
94
+ await this._conn.writeReg(_REG_BLINK, Buffer.alloc(18));
95
+ await this._conn.writeReg(_REG_PWM, Buffer.alloc(144));
96
+ }
97
+
98
+ /**
99
+ * Set the PWM brightness of one LED in the frame being drawn.
100
+ * @param {number} row - Common line, 0-8.
101
+ * @param {number} col - Sink line, 0-15.
102
+ * @param {number} brightness - PWM value, 0-255.
103
+ * @returns {Promise<void>}
104
+ */
105
+ setPixel(row, col, brightness) {
106
+ _checkPixel(row, col);
107
+ _check(brightness, 'brightness', 255);
108
+ return this._exec(async () => {
109
+ this._pwm[this._frame][16 * row + col] = brightness;
110
+ await this._select(this._frame);
111
+ await this._conn.writeReg(_REG_PWM + 16 * row + col, brightness);
112
+ });
113
+ }
114
+
115
+ /** Set all 144 PWM values of the frame being drawn to 0 (one block write). @returns {Promise<void>} */
116
+ clear() { return this.fill(0); }
117
+
118
+ /**
119
+ * Set all 144 PWM values of the frame being drawn (one block write).
120
+ * @param {number} brightness - PWM value, 0-255.
121
+ * @returns {Promise<void>}
122
+ */
123
+ fill(brightness) {
124
+ _check(brightness, 'brightness', 255);
125
+ return this._exec(async () => {
126
+ this._pwm[this._frame].fill(brightness);
127
+ await this._select(this._frame);
128
+ await this._conn.writeReg(_REG_PWM, Buffer.alloc(144, brightness));
129
+ });
130
+ }
131
+ }
132
+
133
+ /**
134
+ * IS31FL3731 full interface — frames, auto-play, blink, breath, audio, shutdown, INTB.
135
+ */
136
+ class IS31FL3731Full extends IS31FL3731Minimal {
137
+ /**
138
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (address 0x74-0x77).
139
+ */
140
+ constructor(connection) {
141
+ super(connection);
142
+ this._callback = null;
143
+ this._intPinUsed = null;
144
+ this._pollTimer = null;
145
+ this._polling = false;
146
+ this._edgeHandler = () => { this._service(true); };
147
+ }
148
+
149
+ /**
150
+ * Read the PWM value of one LED from the shadow copy of the frame being drawn.
151
+ * @param {number} row - 0-8.
152
+ * @param {number} col - 0-15.
153
+ * @returns {number} PWM value, 0-255.
154
+ */
155
+ getPixel(row, col) {
156
+ _checkPixel(row, col);
157
+ return this._pwm[this._frame][16 * row + col];
158
+ }
159
+
160
+ /**
161
+ * Write a whole frame of PWM values (one block write).
162
+ * @param {Buffer|number[]} data - 144 PWM values, row-major (row * 16 + col).
163
+ * @returns {Promise<void>}
164
+ */
165
+ setPixels(data) {
166
+ const buf = Buffer.from(data);
167
+ if (buf.length !== 144) throw new RangeError('data must be 144 bytes');
168
+ return this._exec(async () => {
169
+ buf.copy(this._pwm[this._frame]);
170
+ await this._select(this._frame);
171
+ await this._conn.writeReg(_REG_PWM, buf);
172
+ });
173
+ }
174
+
175
+ _setBit(cache, base, row, col, on) {
176
+ _checkPixel(row, col);
177
+ return this._exec(async () => {
178
+ const idx = 2 * row + (col >> 3);
179
+ const mask = 1 << (col & 7);
180
+ const buf = cache[this._frame];
181
+ if (on) buf[idx] |= mask; else buf[idx] &= ~mask & 0xFF;
182
+ await this._select(this._frame);
183
+ await this._conn.writeReg(base + idx, buf[idx]);
184
+ });
185
+ }
186
+
187
+ /**
188
+ * Set the LED-control (on/off) bit of one LED in the frame being drawn.
189
+ * @param {number} row - 0-8.
190
+ * @param {number} col - 0-15.
191
+ * @param {boolean} on - true to enable the LED.
192
+ * @returns {Promise<void>}
193
+ */
194
+ setLedEnabled(row, col, on) { return this._setBit(this._ctrl, _REG_CTRL, row, col, on); }
195
+
196
+ /**
197
+ * Set the blink-enable bit of one LED in the frame being drawn.
198
+ * @param {number} row - 0-8.
199
+ * @param {number} col - 0-15.
200
+ * @param {boolean} on - true to let the LED blink (needs enableBlink()).
201
+ * @returns {Promise<void>}
202
+ */
203
+ setBlink(row, col, on) { return this._setBit(this._blink, _REG_BLINK, row, col, on); }
204
+
205
+ /**
206
+ * Set all 18 blink-control bytes of the frame being drawn.
207
+ * @param {boolean} on - true to enable blinking of every LED.
208
+ * @returns {Promise<void>}
209
+ */
210
+ setBlinkAll(on) {
211
+ return this._exec(async () => {
212
+ this._blink[this._frame].fill(on ? 0xFF : 0x00);
213
+ await this._select(this._frame);
214
+ await this._conn.writeReg(_REG_BLINK, Buffer.from(this._blink[this._frame]));
215
+ });
216
+ }
217
+
218
+ _writeDisplay() {
219
+ return this._funcWrite(_FN_DISPLAY, (this._ic << 5) | (this._be << 3) | this._bpt);
220
+ }
221
+
222
+ /**
223
+ * Enable or disable blinking (50 % duty).
224
+ * @param {boolean} enabled - true to enable.
225
+ * @param {number} [period=1] - Blink period in units of 0.27 s, 0-7.
226
+ * @returns {Promise<void>}
227
+ */
228
+ enableBlink(enabled, period = 1) {
229
+ _check(period, 'period', 7);
230
+ return this._exec(async () => {
231
+ this._be = enabled ? 1 : 0;
232
+ this._bpt = period;
233
+ await this._writeDisplay();
234
+ });
235
+ }
236
+
237
+ /**
238
+ * Select the frame subsequent drawing calls write to (does not change the display).
239
+ * @param {number} frame - 0-7 (frame 1-8).
240
+ * @returns {Promise<void>}
241
+ */
242
+ selectFrame(frame) {
243
+ _check(frame, 'frame', 7);
244
+ return this._exec(async () => {
245
+ this._frame = frame;
246
+ await this._select(frame);
247
+ });
248
+ }
249
+
250
+ /**
251
+ * Show a frame in Picture mode.
252
+ * @param {number} frame - 0-7 (frame 1-8).
253
+ * @returns {Promise<void>}
254
+ */
255
+ showFrame(frame) {
256
+ _check(frame, 'frame', 7);
257
+ return this._exec(() => this._funcWrite(_FN_CONFIG, Buffer.from([0x00, frame])));
258
+ }
259
+
260
+ /**
261
+ * Initialise a frame: LED-control 0xFF, PWM and blink 0.
262
+ * @param {number} frame - 0-7.
263
+ * @returns {Promise<void>}
264
+ */
265
+ clearFrame(frame) {
266
+ _check(frame, 'frame', 7);
267
+ return this._exec(() => this._initFrame(frame));
268
+ }
269
+
270
+ /** Initialise all eight frames. @returns {Promise<void>} */
271
+ clearAllFrames() {
272
+ return this._exec(async () => {
273
+ for (let f = 0; f < 8; f++) await this._initFrame(f);
274
+ });
275
+ }
276
+
277
+ /**
278
+ * Copy the PWM values of one frame to another.
279
+ * @param {number} src - Source frame, 0-7.
280
+ * @param {number} dst - Destination frame, 0-7.
281
+ * @returns {Promise<void>}
282
+ */
283
+ copyFrame(src, dst) {
284
+ _check(src, 'src', 7);
285
+ _check(dst, 'dst', 7);
286
+ return this._exec(async () => {
287
+ this._pwm[src].copy(this._pwm[dst]);
288
+ await this._select(dst);
289
+ await this._conn.writeReg(_REG_PWM, Buffer.from(this._pwm[dst]));
290
+ });
291
+ }
292
+
293
+ /** Switch to Picture mode (shows the frame selected by showFrame). @returns {Promise<void>} */
294
+ setPictureMode() { return this._exec(() => this._funcWrite(_FN_CONFIG, 0x00)); }
295
+
296
+ /**
297
+ * Start Auto Frame Play.
298
+ * @param {number} [frames=0] - Number of frames played, 1-7, or 0 for all eight.
299
+ * @param {number} [loops=0] - Number of loops, 1-7, or 0 for endless.
300
+ * @param {number} [delayMs=121] - Frame delay in ms (rounded to a multiple of 11 ms, 11-704).
301
+ * @param {number} [startFrame=0] - First frame, 0-7.
302
+ * @returns {Promise<void>}
303
+ */
304
+ startAutoPlay(frames = 0, loops = 0, delayMs = 121, startFrame = 0) {
305
+ _check(frames, 'frames', 7);
306
+ _check(loops, 'loops', 7);
307
+ _check(startFrame, 'startFrame', 7);
308
+ const a = Math.floor(delayMs / 11 + 0.5);
309
+ if (!(a >= 1 && a <= 64)) throw new RangeError('delayMs must be in 11..704');
310
+ return this._exec(async () => {
311
+ await this._funcWrite(_FN_AUTOPLAY1, Buffer.from([(loops << 4) | frames, a & 0x3F]));
312
+ await this._funcWrite(_FN_CONFIG, 0x08 | startFrame);
313
+ });
314
+ }
315
+
316
+ /**
317
+ * Stop Auto Frame Play and keep showing the current frame (Picture mode).
318
+ * Reading the current frame clears the movie-finished flag.
319
+ * @returns {Promise<void>}
320
+ */
321
+ stopAutoPlay() {
322
+ return this._exec(async () => {
323
+ const frame = (await this._funcRead(_FN_FRAME_STATE)) & 0x07;
324
+ await this._funcWrite(_FN_CONFIG, Buffer.from([0x00, frame]));
325
+ });
326
+ }
327
+
328
+ /**
329
+ * Switch Audio Frame Play on, or back to Picture mode.
330
+ * @param {boolean} enabled - true for Audio Frame Play.
331
+ * @returns {Promise<void>}
332
+ */
333
+ setAudioMode(enabled) { return this._exec(() => this._funcWrite(_FN_CONFIG, enabled ? 0x10 : 0x00)); }
334
+
335
+ /**
336
+ * Let the audio input modulate the matrix intensity.
337
+ * @param {boolean} enabled - true to enable.
338
+ * @returns {Promise<void>}
339
+ */
340
+ setAudioSync(enabled) { return this._exec(() => this._funcWrite(_FN_AUDIO_SYNC, enabled ? 1 : 0)); }
341
+
342
+ /**
343
+ * Configure the audio gain.
344
+ * @param {number} gainDb - 0, 3, ... 21 dB.
345
+ * @param {boolean} [agc=false] - true to enable automatic gain control.
346
+ * @param {boolean} [fast=false] - true for fast AGC, false for slow.
347
+ * @returns {Promise<void>}
348
+ */
349
+ setAudioGain(gainDb, agc = false, fast = false) {
350
+ if (!Number.isInteger(gainDb) || gainDb < 0 || gainDb > 21 || gainDb % 3) {
351
+ throw new RangeError('gainDb must be a multiple of 3 in 0..21');
352
+ }
353
+ return this._exec(() => this._funcWrite(_FN_AGC, ((fast ? 1 : 0) << 4) | ((agc ? 1 : 0) << 3) | (gainDb / 3)));
354
+ }
355
+
356
+ /**
357
+ * Set the audio ADC sample interval (46 us x a, 0 = 46 us x 256).
358
+ * @param {number} a - 0-255.
359
+ * @returns {Promise<void>}
360
+ */
361
+ setAudioRate(a) {
362
+ _check(a, 'a', 255);
363
+ return this._exec(() => this._funcWrite(_FN_AUDIO_RATE, a));
364
+ }
365
+
366
+ /**
367
+ * Configure the breath (fade) function.
368
+ * @param {boolean} enabled - true to enable.
369
+ * @param {number} [fadeIn=0] - Fade-in exponent, 0-7 (26 ms x 2^n).
370
+ * @param {number} [fadeOut=0] - Fade-out exponent, 0-7 (26 ms x 2^n).
371
+ * @param {number} [extinguish=0] - Extinguish exponent, 0-7 (3.5 ms x 2^n).
372
+ * @returns {Promise<void>}
373
+ */
374
+ setBreath(enabled, fadeIn = 0, fadeOut = 0, extinguish = 0) {
375
+ _check(fadeIn, 'fadeIn', 7);
376
+ _check(fadeOut, 'fadeOut', 7);
377
+ _check(extinguish, 'extinguish', 7);
378
+ return this._exec(() => this._funcWrite(_FN_BREATH1,
379
+ Buffer.from([(fadeOut << 4) | fadeIn, ((enabled ? 1 : 0) << 4) | extinguish])));
380
+ }
381
+
382
+ /**
383
+ * Choose whether frame 1's PWM values apply to all frames.
384
+ * @param {boolean} useFrame1 - true to use frame 1's intensity for every frame.
385
+ * @returns {Promise<void>}
386
+ */
387
+ setIntensityControl(useFrame1) {
388
+ return this._exec(async () => {
389
+ this._ic = useFrame1 ? 1 : 0;
390
+ await this._writeDisplay();
391
+ });
392
+ }
393
+
394
+ /**
395
+ * Enter or leave software shutdown (frame registers are kept).
396
+ * @param {boolean} [on=true] - true for shutdown, false for normal operation.
397
+ * @returns {Promise<void>}
398
+ */
399
+ shutdown(on = true) { return this._exec(() => this._funcWrite(_FN_SHUTDOWN, on ? 0 : 1)); }
400
+
401
+ /** Read FRAME_STATE (reading clears the movie-finished flag). @returns {Promise<number>} Bit 4 = movie finished, bits 2:0 = current frame. */
402
+ frameState() { return this._exec(() => this._funcRead(_FN_FRAME_STATE)); }
403
+
404
+ /** Return the frame being displayed, 0-7. @returns {Promise<number>} */
405
+ async currentFrame() { return (await this.frameState()) & 0x07; }
406
+
407
+ /** Return true if an Auto Frame Play movie has finished (clears the flag). @returns {Promise<boolean>} */
408
+ async movieFinished() { return ((await this.frameState()) & 0x10) !== 0; }
409
+
410
+ async _withRestore(fn) {
411
+ const prev = this._page;
412
+ const result = await fn();
413
+ if (prev !== null && prev !== this._page) await this._select(prev);
414
+ return result;
415
+ }
416
+
417
+ /**
418
+ * Read a function-page register and restore the selected page.
419
+ * @param {number} reg - Register address, 0-0x0C.
420
+ * @returns {Promise<number>}
421
+ */
422
+ readFunctionRegister(reg) {
423
+ _check(reg, 'reg', 0x0C);
424
+ return this._exec(() => this._withRestore(() => this._funcRead(reg)));
425
+ }
426
+
427
+ /**
428
+ * Write a function-page register and restore the selected page.
429
+ * @param {number} reg - Register address, 0-0x0C.
430
+ * @param {number} value - Byte to write.
431
+ * @returns {Promise<void>}
432
+ */
433
+ writeFunctionRegister(reg, value) {
434
+ _check(reg, 'reg', 0x0C);
435
+ _check(value, 'value', 255);
436
+ return this._exec(() => this._withRestore(() => this._funcWrite(reg, value)));
437
+ }
438
+
439
+ /**
440
+ * Read FRAME_STATE (clears INT) and restore the selected page.
441
+ * @returns {Promise<number>} FRAME_STATE (INT = 0x10, current frame in bits 2:0).
442
+ */
443
+ pollInterrupt() {
444
+ return this._exec(() => this._withRestore(() => this._funcRead(_FN_FRAME_STATE)));
445
+ }
446
+
447
+ /**
448
+ * Subscribe to the end of an Auto Frame Play movie. Delivery is via the INTB line
449
+ * (options.intPin or connection.intPin, falling edge) or a 50 ms polling timer.
450
+ * @param {function} callback - Called with (status) = FRAME_STATE.
451
+ * @param {object} [options]
452
+ * @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
453
+ * @returns {Promise<void>}
454
+ */
455
+ async onInterrupt(callback, options = {}) {
456
+ this._callback = callback;
457
+ const intPin = options.intPin ?? this._conn.intPin ?? null;
458
+ this._intPinUsed = intPin;
459
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
460
+ if (intPin) {
461
+ await intPin.onEdge(this._edgeHandler, 'falling');
462
+ } else {
463
+ this._pollTimer = setInterval(() => { this._service(false); }, 50);
464
+ }
465
+ }
466
+
467
+ /** Unsubscribe and stop delivery. @returns {Promise<void>} */
468
+ async offInterrupt() {
469
+ if (this._intPinUsed) {
470
+ await this._intPinUsed.offEdge(this._edgeHandler);
471
+ this._intPinUsed = null;
472
+ }
473
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
474
+ this._callback = null;
475
+ }
476
+
477
+ async _service(always) {
478
+ if (this._polling) return;
479
+ this._polling = true;
480
+ try {
481
+ const status = await this.pollInterrupt();
482
+ if ((always || (status & 0x10)) && this._callback) this._callback(status);
483
+ } catch (e) {
484
+ // bus error while polling; the next tick retries
485
+ } finally {
486
+ this._polling = false;
487
+ }
488
+ }
489
+ }
490
+
491
+ IS31FL3731Full.GAMMA_32 = Buffer.from([0, 1, 2, 4, 6, 10, 13, 18, 22, 28, 33, 39, 46, 53, 61, 69,
492
+ 78, 86, 96, 106, 116, 126, 138, 149, 161, 173, 186, 199, 212, 226, 240, 255]);
493
+ IS31FL3731Full.GAMMA_64 = Buffer.from([0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 12, 14, 16, 18, 20, 22,
494
+ 24, 26, 29, 32, 35, 38, 41, 44, 47, 50, 53, 57, 61, 65, 69, 73,
495
+ 77, 81, 85, 89, 94, 99, 104, 109, 114, 119, 124, 129, 134, 140, 146, 152,
496
+ 158, 164, 170, 176, 182, 188, 195, 202, 209, 216, 223, 230, 237, 244, 251, 255]);
497
+ IS31FL3731Minimal.ROWS = 9;
498
+ IS31FL3731Minimal.COLS = 16;
499
+ IS31FL3731Minimal.ADDRESS = 0x74;
500
+
501
+ module.exports = { IS31FL3731Minimal, IS31FL3731Full };