periph 1.5.3 → 1.6.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (79) hide show
  1. package/README.md +18 -0
  2. package/examples/color/isl29125/complete.js +57 -0
  3. package/examples/color/isl29125/demo.js +77 -0
  4. package/examples/color/isl29125/minimal.js +23 -0
  5. package/examples/environmental/bme688/complete.js +48 -0
  6. package/examples/environmental/bme688/demo.js +47 -0
  7. package/examples/environmental/bme688/minimal.js +21 -0
  8. package/examples/gas/ccs811/complete.js +87 -0
  9. package/examples/gas/ccs811/demo.js +108 -0
  10. package/examples/gas/ccs811/minimal.js +20 -0
  11. package/examples/gas/rrh46410/complete.js +78 -0
  12. package/examples/gas/rrh46410/demo.js +86 -0
  13. package/examples/gas/rrh46410/minimal.js +21 -0
  14. package/examples/humidity/hts221/complete.js +83 -0
  15. package/examples/humidity/hts221/demo.js +94 -0
  16. package/examples/humidity/hts221/minimal.js +22 -0
  17. package/examples/humidity/htu31d/complete.js +50 -0
  18. package/examples/humidity/htu31d/demo.js +65 -0
  19. package/examples/humidity/htu31d/minimal.js +22 -0
  20. package/examples/humidity/shtc3/complete.js +48 -0
  21. package/examples/humidity/shtc3/demo.js +45 -0
  22. package/examples/humidity/shtc3/minimal.js +22 -0
  23. package/examples/led/saa1064/complete.js +51 -0
  24. package/examples/led/saa1064/demo.js +54 -0
  25. package/examples/led/saa1064/minimal.js +23 -0
  26. package/examples/light/apds9250/complete.js +71 -0
  27. package/examples/light/apds9250/demo.js +79 -0
  28. package/examples/light/apds9250/minimal.js +23 -0
  29. package/examples/light/bh1750/complete.js +46 -0
  30. package/examples/light/bh1750/demo.js +52 -0
  31. package/examples/light/bh1750/minimal.js +22 -0
  32. package/examples/light/si1132/complete.js +70 -0
  33. package/examples/light/si1132/demo.js +73 -0
  34. package/examples/light/si1132/minimal.js +22 -0
  35. package/examples/light/si1133/complete.js +89 -0
  36. package/examples/light/si1133/demo.js +93 -0
  37. package/examples/light/si1133/minimal.js +22 -0
  38. package/examples/light/si1145/complete.js +93 -0
  39. package/examples/light/si1145/demo.js +98 -0
  40. package/examples/light/si1145/minimal.js +22 -0
  41. package/examples/pressure/mpl3115a2/complete.js +49 -0
  42. package/examples/pressure/mpl3115a2/demo.js +48 -0
  43. package/examples/pressure/mpl3115a2/minimal.js +22 -0
  44. package/examples/pressure/ms5607/complete.js +49 -0
  45. package/examples/pressure/ms5607/demo.js +65 -0
  46. package/examples/pressure/ms5607/minimal.js +22 -0
  47. package/examples/pressure/ms5611/complete.js +49 -0
  48. package/examples/pressure/ms5611/demo.js +65 -0
  49. package/examples/pressure/ms5611/minimal.js +22 -0
  50. package/examples/pressure/spl06/complete.js +81 -0
  51. package/examples/pressure/spl06/demo.js +106 -0
  52. package/examples/pressure/spl06/minimal.js +22 -0
  53. package/examples/rfid/pn532/complete.js +99 -0
  54. package/examples/rfid/pn532/demo.js +121 -0
  55. package/examples/rfid/pn532/minimal.js +22 -0
  56. package/package.json +1 -1
  57. package/src/chips/color/isl29125.js +379 -0
  58. package/src/chips/environmental/bme688.js +573 -0
  59. package/src/chips/gas/ccs811.js +400 -0
  60. package/src/chips/gas/rrh46410.js +534 -0
  61. package/src/chips/humidity/hts221.js +369 -0
  62. package/src/chips/humidity/htu31d.js +242 -0
  63. package/src/chips/humidity/shtc3.js +239 -0
  64. package/src/chips/led/saa1064.js +241 -0
  65. package/src/chips/light/apds9250.js +431 -0
  66. package/src/chips/light/bh1750.js +196 -0
  67. package/src/chips/light/si1132.js +416 -0
  68. package/src/chips/light/si1133.js +583 -0
  69. package/src/chips/light/si1145.js +630 -0
  70. package/src/chips/pressure/mpl3115a2.js +459 -0
  71. package/src/chips/pressure/ms5607.js +290 -0
  72. package/src/chips/pressure/ms5611.js +290 -0
  73. package/src/chips/pressure/spl06.js +461 -0
  74. package/src/chips/rfid/pn532.js +513 -0
  75. package/src/connection/i2c.js +46 -13
  76. package/src/connection/smbus.js +21 -8
  77. package/src/discovery/registry.js +18 -18
  78. package/examples/gpio/.gitkeep +0 -0
  79. package/src/chips/gpio/.gitkeep +0 -0
@@ -0,0 +1,583 @@
1
+ 'use strict';
2
+
3
+ const _REG_PART_ID = 0x00;
4
+ const _REG_HOSTIN0 = 0x0A;
5
+ const _REG_COMMAND = 0x0B;
6
+ const _REG_IRQ_ENABLE = 0x0F;
7
+ const _REG_RESPONSE1 = 0x10;
8
+ const _REG_RESPONSE0 = 0x11;
9
+ const _REG_IRQ_STATUS = 0x12;
10
+ const _REG_HOSTOUT0 = 0x13;
11
+
12
+ const _PART_ID = 0x33;
13
+
14
+ const _CMD_RESET_CMD_CTR = 0x00;
15
+ const _CMD_RESET_SW = 0x01;
16
+ const _CMD_FORCE = 0x11;
17
+ const _CMD_PAUSE = 0x12;
18
+ const _CMD_START = 0x13;
19
+ const _CMD_PARAM_QUERY = 0x40;
20
+ const _CMD_PARAM_SET = 0x80;
21
+
22
+ const _PARAM_I2C_ADDR = 0x00;
23
+ const _PARAM_CHAN_LIST = 0x01;
24
+ const _PARAM_ADCCONFIG0 = 0x02;
25
+ const _PARAM_ADCSENS0 = 0x03;
26
+ const _PARAM_ADCPOST0 = 0x04;
27
+ const _PARAM_MEASCONFIG0 = 0x05;
28
+ const _PARAM_MEASRATE_H = 0x1A;
29
+ const _PARAM_MEASRATE_L = 0x1B;
30
+ const _PARAM_MEASCOUNT0 = 0x1C;
31
+ const _PARAM_THRESHOLD0_H = 0x25;
32
+ const _PARAM_BURST = 0x2B;
33
+
34
+ const _CMD_POLLS = 50; // 1 ms each
35
+ const _MEASURE_POLLS = 15000; // 1 ms each; covers HW_GAIN 11 with SW_GAIN 128
36
+ const _SCRATCH = 5;
37
+
38
+ const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
39
+
40
+ function _check(name, value, lo, hi) {
41
+ if (!Number.isInteger(value) || value < lo || value > hi) {
42
+ throw new RangeError(`${name} must be ${lo}..${hi}`);
43
+ }
44
+ }
45
+
46
+ /**
47
+ * Si1133 UV index and ambient light sensor (Silicon Labs) — minimal interface.
48
+ *
49
+ * One call runs a forced measurement and returns the UV and white-light counts. `init()` waits
50
+ * 25 ms, verifies PART_ID = 0x33, sends RESET_SW and configures channel 0 = UV (ADCMUX 0x18,
51
+ * HW_GAIN 9, 24-bit output), channel 1 = white (ADCMUX 0x0B, HW_GAIN 7, 24-bit), CHAN_LIST = 0x03
52
+ * and IRQ_ENABLE = 0x3F (so IRQ_STATUS flags finished channels).
53
+ *
54
+ * All configuration goes through the indirect parameter table (PARAM_SET / PARAM_QUERY); every
55
+ * command is confirmed by the RESPONSE0 counter. Results are raw ADC counts — the datasheet gives
56
+ * no lux or UV-index formula. INT is open-drain and must be high at power-up. I²C address 0x55
57
+ * (AD pulled up) or 0x52 (AD pulled down). The constructor does not touch the bus.
58
+ *
59
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection.
60
+ */
61
+ class Si1133Minimal {
62
+ constructor(connection) {
63
+ this._conn = connection;
64
+ this._chanList = 0;
65
+ this._bits24 = [false, false, false, false, false, false];
66
+ this._irqEnable = 0;
67
+ }
68
+
69
+ /**
70
+ * Verify the chip, reset it and configure the UV and white channels.
71
+ * @returns {Promise<void>}
72
+ * @throws {Error} If PART_ID is not 0x33 or a command fails.
73
+ */
74
+ async init() {
75
+ await _sleep(25);
76
+ const part = await this._readReg(_REG_PART_ID);
77
+ if (part !== _PART_ID) {
78
+ throw new Error(`Unexpected Si1133 PART_ID 0x${part.toString(16)}, expected 0x33`);
79
+ }
80
+ await this._softwareReset();
81
+ await this._applyDefaults();
82
+ }
83
+
84
+ async _readReg(reg) {
85
+ return (await this._conn.readReg(reg, 1))[0];
86
+ }
87
+
88
+ async _writeReg(reg, value) {
89
+ await this._conn.writeReg(reg, [value & 0xFF]);
90
+ }
91
+
92
+ async _softwareReset() {
93
+ await this._writeReg(_REG_COMMAND, _CMD_RESET_SW);
94
+ await _sleep(10);
95
+ this._chanList = 0;
96
+ this._bits24 = [false, false, false, false, false, false];
97
+ this._irqEnable = 0;
98
+ for (let i = 0; i < _CMD_POLLS; i++) {
99
+ if ((await this._readReg(_REG_RESPONSE0)) & Si1133Minimal.RESP_SLEEP) return;
100
+ await _sleep(1);
101
+ }
102
+ throw new Error('Si1133 reset timeout');
103
+ }
104
+
105
+ async _applyDefaults() {
106
+ await this._setChannel(0, Si1133Minimal.MUX_UV, 9, 0, 0, false, true, 0);
107
+ await this._setChannel(1, Si1133Minimal.MUX_WHITE, 7, 0, 0, false, true, 0);
108
+ await this._paramSet(_PARAM_CHAN_LIST, 0x03);
109
+ this._chanList = 0x03;
110
+ this._irqEnable = 0x3F;
111
+ await this._writeReg(_REG_IRQ_ENABLE, this._irqEnable);
112
+ }
113
+
114
+ async _counter() {
115
+ let r = await this._readReg(_REG_RESPONSE0);
116
+ if (r & Si1133Minimal.RESP_CMD_ERR) {
117
+ await this._writeReg(_REG_COMMAND, _CMD_RESET_CMD_CTR);
118
+ r = await this._readReg(_REG_RESPONSE0);
119
+ }
120
+ return r & 0x0F;
121
+ }
122
+
123
+ async _waitCounter(before) {
124
+ for (let i = 0; i < _CMD_POLLS; i++) {
125
+ const r = await this._readReg(_REG_RESPONSE0);
126
+ if (r & Si1133Minimal.RESP_CMD_ERR) {
127
+ await this._writeReg(_REG_COMMAND, _CMD_RESET_CMD_CTR);
128
+ throw new Error(`Si1133 command error 0x${(r & 0x1F).toString(16).padStart(2, '0')}`);
129
+ }
130
+ if ((r & 0x0F) !== before) return;
131
+ await _sleep(1);
132
+ }
133
+ throw new Error('Si1133 command timeout');
134
+ }
135
+
136
+ async _command(cmd) {
137
+ const before = await this._counter();
138
+ await this._writeReg(_REG_COMMAND, cmd);
139
+ await this._waitCounter(before);
140
+ }
141
+
142
+ async _paramSet(address, value) {
143
+ const before = await this._counter();
144
+ await this._conn.writeReg(_REG_HOSTIN0, [value & 0xFF, _CMD_PARAM_SET | (address & 0x3F)]);
145
+ await this._waitCounter(before);
146
+ }
147
+
148
+ async _paramQuery(address) {
149
+ await this._command(_CMD_PARAM_QUERY | (address & 0x3F));
150
+ return this._readReg(_REG_RESPONSE1);
151
+ }
152
+
153
+ async _setChannel(channel, adcmux, hwGain, swGain, decimRate, highSignal, bits24, postshift) {
154
+ const base = 4 * channel;
155
+ await this._paramSet(_PARAM_ADCCONFIG0 + base, (decimRate << 5) | adcmux);
156
+ await this._paramSet(_PARAM_ADCSENS0 + base, (highSignal ? 0x80 : 0) | (swGain << 4) | hwGain);
157
+ await this._paramSet(_PARAM_ADCPOST0 + base, (bits24 ? 0x40 : 0) | (postshift << 3));
158
+ this._bits24[channel] = !!bits24;
159
+ }
160
+
161
+ async _readOutputs() {
162
+ let n = 0;
163
+ for (let ch = 0; ch < 6; ch++) {
164
+ if (this._chanList & (1 << ch)) n += this._bits24[ch] ? 3 : 2;
165
+ }
166
+ if (n === 0) return [];
167
+ const d = await this._conn.readReg(_REG_HOSTOUT0, n);
168
+ const values = [];
169
+ let pos = 0;
170
+ for (let ch = 0; ch < 6; ch++) {
171
+ if (!(this._chanList & (1 << ch))) continue;
172
+ if (this._bits24[ch]) {
173
+ let v = (d[pos] << 16) | (d[pos + 1] << 8) | d[pos + 2];
174
+ if (v & 0x800000) v -= 0x1000000;
175
+ values.push(v);
176
+ pos += 3;
177
+ } else {
178
+ values.push((d[pos] << 8) | d[pos + 1]);
179
+ pos += 2;
180
+ }
181
+ }
182
+ return values;
183
+ }
184
+
185
+ async _measure() {
186
+ await this._readReg(_REG_IRQ_STATUS); // clear stale flags
187
+ await this._command(_CMD_FORCE);
188
+ const wait = this._chanList & this._irqEnable;
189
+ if (wait === 0) {
190
+ await _sleep(100);
191
+ } else {
192
+ let seen = 0;
193
+ let i = 0;
194
+ for (;;) {
195
+ seen |= await this._readReg(_REG_IRQ_STATUS);
196
+ if ((seen & wait) === wait) break;
197
+ if (++i >= _MEASURE_POLLS) throw new Error('Si1133 measurement timeout');
198
+ await _sleep(1);
199
+ }
200
+ }
201
+ return this._readOutputs();
202
+ }
203
+
204
+ /**
205
+ * Run one forced measurement and return the UV and white counts.
206
+ * @returns {Promise<{uv: number, white: number}>} Signed 24-bit raw counts of channels 0 and 1
207
+ * (near zero or slightly negative in the dark).
208
+ * @throws {Error} If channel 0 or 1 is disabled, a command fails or the measurement does not finish.
209
+ */
210
+ async read() {
211
+ if ((this._chanList & 0x03) !== 0x03) throw new Error('Si1133 channels 0 and 1 must be enabled for read()');
212
+ const v = await this._measure();
213
+ return { uv: v[0], white: v[1] };
214
+ }
215
+ }
216
+
217
+ /** RESPONSE0: sequencer running. */
218
+ Si1133Minimal.RESP_RUNNING = 0x80;
219
+ /** RESPONSE0: suspended. */
220
+ Si1133Minimal.RESP_SUSPEND = 0x40;
221
+ /** RESPONSE0: sleeping (idle). */
222
+ Si1133Minimal.RESP_SLEEP = 0x20;
223
+ /** RESPONSE0: command error. */
224
+ Si1133Minimal.RESP_CMD_ERR = 0x10;
225
+ /** ADCMUX: small IR photodiode. */
226
+ Si1133Minimal.MUX_SMALL_IR = 0x00;
227
+ /** ADCMUX: medium IR photodiode. */
228
+ Si1133Minimal.MUX_MEDIUM_IR = 0x01;
229
+ /** ADCMUX: large IR photodiode. */
230
+ Si1133Minimal.MUX_LARGE_IR = 0x02;
231
+ /** ADCMUX: white photodiode. */
232
+ Si1133Minimal.MUX_WHITE = 0x0B;
233
+ /** ADCMUX: large white photodiode. */
234
+ Si1133Minimal.MUX_LARGE_WHITE = 0x0D;
235
+ /** ADCMUX: UV photodiode. */
236
+ Si1133Minimal.MUX_UV = 0x18;
237
+ /** ADCMUX: deep UV photodiode. */
238
+ Si1133Minimal.MUX_UV_DEEP = 0x19;
239
+
240
+ /**
241
+ * Si1133 full interface — extends Si1133Minimal with per-channel configuration,
242
+ * single-photodiode reads, autonomous and burst modes, thresholds, direct parameter access, the
243
+ * I²C address and the INT interrupt (active low, open-drain, released when IRQ_STATUS is read).
244
+ *
245
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection.
246
+ */
247
+ class Si1133Full extends Si1133Minimal {
248
+ constructor(connection) {
249
+ super(connection);
250
+ this._counterIndex = [0, 0, 0, 0, 0, 0];
251
+ this._callback = null;
252
+ this._intPinUsed = null;
253
+ this._pollTimer = null;
254
+ this._polling = false;
255
+ this._edgeHandler = () => { this._service(); };
256
+ }
257
+
258
+ /**
259
+ * Read PART_ID, HW_ID and REV_ID.
260
+ * @returns {Promise<{partId: number, hwId: number, revId: number}>} 0x33, implementation /
261
+ * silicon revision code and product revision.
262
+ */
263
+ async readId() {
264
+ const b = await this._conn.readReg(_REG_PART_ID, 3);
265
+ return { partId: b[0], hwId: b[1], revId: b[2] };
266
+ }
267
+
268
+ /**
269
+ * Configure one measurement channel (ADCCONFIG, ADCSENS, ADCPOST); clears its threshold select.
270
+ * @param {number} channel - Channel 0–5.
271
+ * @param {number} adcmux - Photodiode (MUX_* constant).
272
+ * @param {object} [options]
273
+ * @param {number} [options.hwGain=0] - Integration time code 0–11 (48.8 µs × 2^n at decimation 0).
274
+ * @param {number} [options.swGain=0] - Accumulate 2^n samples, n = 0–7.
275
+ * @param {number} [options.decimRate=0] - Decimation rate 0–3.
276
+ * @param {boolean} [options.highSignal=false] - Divide the sensitivity by 14.5 (direct sunlight).
277
+ * @param {boolean} [options.bits24=true] - 24-bit signed output, otherwise 16-bit unsigned.
278
+ * @param {number} [options.postshift=0] - Right shift 0–7 of the accumulated result.
279
+ * @returns {Promise<void>}
280
+ * @throws {RangeError} If a value is out of range.
281
+ */
282
+ async configureChannel(channel, adcmux, { hwGain = 0, swGain = 0, decimRate = 0, highSignal = false,
283
+ bits24 = true, postshift = 0 } = {}) {
284
+ _check('channel', channel, 0, 5);
285
+ _check('adcmux', adcmux, 0, 0x1F);
286
+ _check('hwGain', hwGain, 0, 11);
287
+ _check('swGain', swGain, 0, 7);
288
+ _check('decimRate', decimRate, 0, 3);
289
+ _check('postshift', postshift, 0, 7);
290
+ await this._setChannel(channel, adcmux, hwGain, swGain, decimRate, highSignal, bits24, postshift);
291
+ }
292
+
293
+ /**
294
+ * Select the channels that run (CHAN_LIST).
295
+ * @param {number} mask - Bit c enables channel c (0x00–0x3F).
296
+ * @returns {Promise<void>}
297
+ */
298
+ async enableChannels(mask) {
299
+ _check('mask', mask, 0, 0x3F);
300
+ await this._paramSet(_PARAM_CHAN_LIST, mask);
301
+ this._chanList = mask;
302
+ }
303
+
304
+ /**
305
+ * Read the results of all enabled channels in channel order.
306
+ * @param {boolean} [force=true] - Run a forced measurement first, otherwise only decode HOSTOUT.
307
+ * @returns {Promise<number[]>} One value per enabled channel (24-bit signed or 16-bit unsigned).
308
+ */
309
+ async readChannels(force = true) {
310
+ return force ? this._measure() : this._readOutputs();
311
+ }
312
+
313
+ async _scratchRead(adcmux, hwGain) {
314
+ const saved = this._chanList;
315
+ await this._setChannel(_SCRATCH, adcmux, hwGain, 0, 0, false, true, 0);
316
+ if (this._counterIndex[_SCRATCH]) {
317
+ await this._paramSet(_PARAM_MEASCONFIG0 + 4 * _SCRATCH, 0);
318
+ this._counterIndex[_SCRATCH] = 0;
319
+ }
320
+ await this._paramSet(_PARAM_CHAN_LIST, 1 << _SCRATCH);
321
+ this._chanList = 1 << _SCRATCH;
322
+ try {
323
+ return (await this._measure())[0];
324
+ } finally {
325
+ await this._paramSet(_PARAM_CHAN_LIST, saved);
326
+ this._chanList = saved;
327
+ }
328
+ }
329
+
330
+ /**
331
+ * Measure the UV photodiode once (channel 5 used as scratch, HW_GAIN 9).
332
+ * @returns {Promise<number>} Signed 24-bit raw count.
333
+ */
334
+ async readUv() {
335
+ return this._scratchRead(Si1133Minimal.MUX_UV, 9);
336
+ }
337
+
338
+ /**
339
+ * Measure the white photodiode once (channel 5 used as scratch, HW_GAIN 7).
340
+ * @returns {Promise<number>} Signed 24-bit raw count.
341
+ */
342
+ async readWhite() {
343
+ return this._scratchRead(Si1133Minimal.MUX_WHITE, 7);
344
+ }
345
+
346
+ /**
347
+ * Measure an IR photodiode once (channel 5 used as scratch, HW_GAIN 7).
348
+ * @param {number} [photodiode=MUX_SMALL_IR] - MUX_SMALL_IR, MUX_MEDIUM_IR or MUX_LARGE_IR.
349
+ * @returns {Promise<number>} Signed 24-bit raw count.
350
+ */
351
+ async readIr(photodiode = Si1133Minimal.MUX_SMALL_IR) {
352
+ _check('photodiode', photodiode, 0, 2);
353
+ return this._scratchRead(photodiode, 7);
354
+ }
355
+
356
+ /** Send FORCE without waiting for the results. @returns {Promise<void>} */
357
+ async force() {
358
+ await this._command(_CMD_FORCE);
359
+ }
360
+
361
+ /**
362
+ * Assign a rate counter to a channel (MEASCONFIG.COUNTER_INDEX).
363
+ * @param {number} channel - Channel 0–5.
364
+ * @param {number} index - 0 = none (forced only), 1–3 = MEASCOUNT0..2.
365
+ * @returns {Promise<void>}
366
+ */
367
+ async setChannelCounter(channel, index) {
368
+ _check('channel', channel, 0, 5);
369
+ _check('index', index, 0, 3);
370
+ await this._paramSet(_PARAM_MEASCONFIG0 + 4 * channel, index << 6);
371
+ this._counterIndex[channel] = index;
372
+ }
373
+
374
+ /**
375
+ * Start autonomous measurements: MEASRATE (800 µs units), MEASCOUNT0..2, counter 1 for every
376
+ * enabled channel without one, then START. Channel period = rate × MEASCOUNTk.
377
+ * @param {number} rate - Main period in seconds (800 µs … 52.4 s).
378
+ * @param {number[]} [counters=[1, 1, 1]] - MEASCOUNT0..2, 1–255 each.
379
+ * @returns {Promise<void>}
380
+ */
381
+ async startAutonomous(rate, counters = [1, 1, 1]) {
382
+ const ticks = Math.round(rate / 800e-6);
383
+ _check('rate ticks', ticks, 1, 0xFFFF);
384
+ if (!Array.isArray(counters) || counters.length !== 3) throw new RangeError('counters must have three entries');
385
+ counters.forEach((c) => _check('counter', c, 1, 0xFF));
386
+ await this._paramSet(_PARAM_MEASRATE_H, ticks >> 8);
387
+ await this._paramSet(_PARAM_MEASRATE_L, ticks & 0xFF);
388
+ for (let i = 0; i < 3; i++) await this._paramSet(_PARAM_MEASCOUNT0 + i, counters[i]);
389
+ for (let ch = 0; ch < 6; ch++) {
390
+ if ((this._chanList & (1 << ch)) && !this._counterIndex[ch]) await this.setChannelCounter(ch, 1);
391
+ }
392
+ await this._command(_CMD_START);
393
+ }
394
+
395
+ /** Pause autonomous measurements (PAUSE). @returns {Promise<void>} */
396
+ async pause() {
397
+ await this._command(_CMD_PAUSE);
398
+ }
399
+
400
+ /**
401
+ * Configure burst mode (BURST).
402
+ * @param {number} count - Readings 1–127; 0 disables burst mode.
403
+ * @returns {Promise<void>}
404
+ */
405
+ async setBurst(count) {
406
+ _check('count', count, 0, 127);
407
+ await this._paramSet(_PARAM_BURST, count ? (0x80 | count) : 0);
408
+ }
409
+
410
+ /**
411
+ * Set one of the three 16-bit thresholds.
412
+ * @param {number} index - Threshold 0–2.
413
+ * @param {number} value - Threshold count 0–65535.
414
+ * @returns {Promise<void>}
415
+ */
416
+ async setThreshold(index, value) {
417
+ _check('index', index, 0, 2);
418
+ _check('value', value, 0, 0xFFFF);
419
+ await this._paramSet(_PARAM_THRESHOLD0_H + 2 * index, value >> 8);
420
+ await this._paramSet(_PARAM_THRESHOLD0_H + 2 * index + 1, value & 0xFF);
421
+ }
422
+
423
+ /**
424
+ * Select the threshold a channel's interrupt compares against (ADCPOST.THRESH_SEL).
425
+ * @param {number} channel - Channel 0–5.
426
+ * @param {number} index - 0 = off, 1–3 = THRESHOLD0..2.
427
+ * @returns {Promise<void>}
428
+ */
429
+ async setChannelThreshold(channel, index) {
430
+ _check('channel', channel, 0, 5);
431
+ _check('index', index, 0, 3);
432
+ const address = _PARAM_ADCPOST0 + 4 * channel;
433
+ const v = await this._paramQuery(address);
434
+ await this._paramSet(address, (v & 0xFC) | index);
435
+ }
436
+
437
+ /**
438
+ * Read a parameter-table byte (PARAM_QUERY).
439
+ * @param {number} address - Parameter address 0x00–0x3F.
440
+ * @returns {Promise<number>} Parameter value.
441
+ */
442
+ async readParameter(address) {
443
+ _check('address', address, 0, 0x3F);
444
+ return this._paramQuery(address);
445
+ }
446
+
447
+ /**
448
+ * Write a parameter-table byte (PARAM_SET). Writing channel parameters directly bypasses the
449
+ * driver's record of the channel layout; prefer configureChannel().
450
+ * @param {number} address - Parameter address 0x00–0x3F.
451
+ * @param {number} value - Byte value.
452
+ * @returns {Promise<void>}
453
+ */
454
+ async writeParameter(address, value) {
455
+ _check('address', address, 0, 0x3F);
456
+ _check('value', value, 0, 0xFF);
457
+ await this._paramSet(address, value);
458
+ }
459
+
460
+ /**
461
+ * Decode RESPONSE0.
462
+ * @returns {Promise<{state: number, error: boolean, counter: number}>} State bits (RESP_RUNNING,
463
+ * RESP_SUSPEND, RESP_SLEEP), the CMD_ERR flag and the 4-bit counter (error code when error).
464
+ */
465
+ async readResponse() {
466
+ const r = await this._readReg(_REG_RESPONSE0);
467
+ return { state: r & 0xE0, error: !!(r & Si1133Minimal.RESP_CMD_ERR), counter: r & 0x0F };
468
+ }
469
+
470
+ /**
471
+ * Read IRQ_STATUS (clears it).
472
+ * @returns {Promise<number>} Bit c set when channel c has a new result.
473
+ */
474
+ async dataReady() {
475
+ return (await this._readReg(_REG_IRQ_STATUS)) & 0x3F;
476
+ }
477
+
478
+ /**
479
+ * Change the I²C address (volatile). The chip answers on the new address at once, so no
480
+ * completion check is made; open a new connection afterwards.
481
+ * @param {number} address - New 7-bit address 0x08–0x77.
482
+ * @returns {Promise<void>}
483
+ */
484
+ async setI2cAddress(address) {
485
+ _check('address', address, 0x08, 0x77);
486
+ await this._readReg(_REG_RESPONSE0);
487
+ await this._conn.writeReg(_REG_HOSTIN0, [address, _CMD_PARAM_SET | _PARAM_I2C_ADDR]);
488
+ }
489
+
490
+ /** Software reset (RESET_SW), wait for SLEEP, re-apply the Minimal defaults. @returns {Promise<void>} */
491
+ async reset() {
492
+ await this._softwareReset();
493
+ this._counterIndex = [0, 0, 0, 0, 0, 0];
494
+ await this._applyDefaults();
495
+ }
496
+
497
+ /**
498
+ * Enable INT for channels (IRQ_ENABLE).
499
+ * @param {number} source - SOURCE_CHANNEL* mask.
500
+ * @returns {Promise<void>}
501
+ */
502
+ async enableInterrupt(source) {
503
+ this._irqEnable = (this._irqEnable | source) & 0x3F;
504
+ await this._writeReg(_REG_IRQ_ENABLE, this._irqEnable);
505
+ }
506
+
507
+ /**
508
+ * Disable INT for channels. Forced reads only wait for channels whose interrupt is enabled
509
+ * (none: a fixed 100 ms wait).
510
+ * @param {number} source - SOURCE_CHANNEL* mask.
511
+ * @returns {Promise<void>}
512
+ */
513
+ async disableInterrupt(source) {
514
+ this._irqEnable &= ~source & 0x3F;
515
+ await this._writeReg(_REG_IRQ_ENABLE, this._irqEnable);
516
+ }
517
+
518
+ /**
519
+ * Read IRQ_STATUS (clears it and releases INT).
520
+ * @returns {Promise<number>} Channel mask, 0 = nothing pending.
521
+ */
522
+ async pollInterrupt() {
523
+ return this.dataReady();
524
+ }
525
+
526
+ /**
527
+ * Subscribe to channel-ready events. Uses `intPin` (or the connection's `intPin`) on its
528
+ * falling edge, otherwise a 10 ms polling timer on IRQ_STATUS.
529
+ * @param {function(number): void} callback - Called with the IRQ_STATUS channel mask.
530
+ * @param {object|null} [intPin=null] - Optional InputPin wired to INT.
531
+ * @returns {Promise<void>}
532
+ */
533
+ async onInterrupt(callback, intPin = null) {
534
+ this._callback = callback;
535
+ const ipin = intPin ?? this._conn.intPin ?? null;
536
+ this._intPinUsed = ipin;
537
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
538
+ if (ipin) {
539
+ await ipin.onEdge(this._edgeHandler, 'falling');
540
+ } else {
541
+ this._pollTimer = setInterval(() => { this._service(); }, 10);
542
+ if (this._pollTimer.unref) this._pollTimer.unref();
543
+ }
544
+ }
545
+
546
+ /** Unsubscribe and stop delivery. @returns {Promise<void>} */
547
+ async offInterrupt() {
548
+ if (this._intPinUsed) {
549
+ await this._intPinUsed.offEdge(this._edgeHandler);
550
+ this._intPinUsed = null;
551
+ }
552
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
553
+ this._callback = null;
554
+ }
555
+
556
+ async _service() {
557
+ if (this._polling) return;
558
+ this._polling = true;
559
+ try {
560
+ const status = await this.pollInterrupt();
561
+ if (status && this._callback) this._callback(status);
562
+ } catch (e) {
563
+ /* a failed poll is retried on the next edge / tick */
564
+ } finally {
565
+ this._polling = false;
566
+ }
567
+ }
568
+ }
569
+
570
+ /** Interrupt source: channel 0 result ready. */
571
+ Si1133Full.SOURCE_CHANNEL0 = 0x01;
572
+ /** Interrupt source: channel 1 result ready. */
573
+ Si1133Full.SOURCE_CHANNEL1 = 0x02;
574
+ /** Interrupt source: channel 2 result ready. */
575
+ Si1133Full.SOURCE_CHANNEL2 = 0x04;
576
+ /** Interrupt source: channel 3 result ready. */
577
+ Si1133Full.SOURCE_CHANNEL3 = 0x08;
578
+ /** Interrupt source: channel 4 result ready. */
579
+ Si1133Full.SOURCE_CHANNEL4 = 0x10;
580
+ /** Interrupt source: channel 5 result ready. */
581
+ Si1133Full.SOURCE_CHANNEL5 = 0x20;
582
+
583
+ module.exports = { Si1133Minimal, Si1133Full };