periph 1.5.4 → 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.
@@ -0,0 +1,630 @@
1
+ 'use strict';
2
+
3
+ const _REG_PART_ID = 0x00;
4
+ const _REG_SEQ_ID = 0x02;
5
+ const _REG_INT_CFG = 0x03;
6
+ const _REG_IRQ_ENABLE = 0x04;
7
+ const _REG_HW_KEY = 0x07;
8
+ const _REG_MEAS_RATE0 = 0x08;
9
+ const _REG_PS_RATE = 0x0A;
10
+ const _REG_PS_LED21 = 0x0F;
11
+ const _REG_PS_LED3 = 0x10;
12
+ const _REG_UCOEF0 = 0x13;
13
+ const _REG_PARAM_WR = 0x17;
14
+ const _REG_COMMAND = 0x18;
15
+ const _REG_RESPONSE = 0x20;
16
+ const _REG_IRQ_STATUS = 0x21;
17
+ const _REG_DATA = 0x22;
18
+ const _REG_PARAM_RD = 0x2E;
19
+
20
+ const _HW_KEY = 0x17;
21
+
22
+ const _CMD_NOP = 0x00;
23
+ const _CMD_RESET = 0x01;
24
+ const _CMD_BUSADDR = 0x02;
25
+ const _CMD_PS_FORCE = 0x05;
26
+ const _CMD_ALS_FORCE = 0x06;
27
+ const _CMD_PSALS_FORCE = 0x07;
28
+ const _CMD_PSALS_PAUSE = 0x0B;
29
+ const _CMD_PS_AUTO = 0x0D;
30
+ const _CMD_ALS_AUTO = 0x0E;
31
+ const _CMD_PSALS_AUTO = 0x0F;
32
+ const _CMD_GET_CAL = 0x12;
33
+ const _CMD_PARAM_QUERY = 0x80;
34
+ const _CMD_PARAM_SET = 0xA0;
35
+
36
+ const _PARAM_I2C_ADDR = 0x00;
37
+ const _PARAM_CHLIST = 0x01;
38
+ const _PARAM_PSLED12_SELECT = 0x02;
39
+ const _PARAM_PSLED3_SELECT = 0x03;
40
+ const _PARAM_PS_ENCODING = 0x05;
41
+ const _PARAM_ALS_ENCODING = 0x06;
42
+ const _PARAM_PS1_ADCMUX = 0x07;
43
+ const _PARAM_PS_ADC_COUNTER = 0x0A;
44
+ const _PARAM_PS_ADC_GAIN = 0x0B;
45
+ const _PARAM_PS_ADC_MISC = 0x0C;
46
+ const _PARAM_AUX_ADCMUX = 0x0F;
47
+ const _PARAM_ALS_VIS_ADC_COUNTER = 0x10;
48
+ const _PARAM_ALS_VIS_ADC_GAIN = 0x11;
49
+ const _PARAM_ALS_VIS_ADC_MISC = 0x12;
50
+ const _PARAM_ALS_IR_ADC_COUNTER = 0x1D;
51
+ const _PARAM_ALS_IR_ADC_GAIN = 0x1E;
52
+ const _PARAM_ALS_IR_ADC_MISC = 0x1F;
53
+
54
+ const _SOURCE_MASK = 0x3D;
55
+ const _CMD_POLLS = 50; // 1 ms each
56
+ const _MEASURE_POLLS = 1000; // 1 ms each; a gain-7 ALS measurement takes ~200 ms
57
+
58
+ const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
59
+
60
+ function _check(name, value, lo, hi) {
61
+ if (!Number.isInteger(value) || value < lo || value > hi) {
62
+ throw new RangeError(`${name} must be ${lo}..${hi}`);
63
+ }
64
+ }
65
+
66
+ /**
67
+ * Si1145 proximity / UV index / ambient light sensor (Silicon Labs) — minimal interface.
68
+ *
69
+ * One call runs a forced PS + ALS measurement and returns the proximity count, the UV index and
70
+ * the visible/infrared counts. `init()` waits 25 ms, verifies PART_ID ∈ {0x45, 0x46, 0x47},
71
+ * writes HW_KEY = 0x17, UCOEF0..3 = 7B 6B 01 00, CHLIST = 0xB1 (UV, ALS IR, ALS VIS, PS1),
72
+ * PSLED12_SELECT = 0x01, PS_LED21 = 0x03 (LED1 at level 3 of 15) and IRQ_ENABLE = ALS_IE | PS1_IE
73
+ * (INT stays undriven until `Si1145Full.enableInterrupt()`).
74
+ *
75
+ * Configuration is split between registers and the parameter RAM (PARAM_SET / PARAM_QUERY); every
76
+ * command increments the RESPONSE counter. I²C address 0x60 (older silicon: 0x5A). The
77
+ * constructor does not touch the bus.
78
+ *
79
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection.
80
+ */
81
+ class Si1145Minimal {
82
+ constructor(connection) {
83
+ this._conn = connection;
84
+ this._partId = 0x45;
85
+ this._seqId = 0;
86
+ this._chlist = 0;
87
+ this._irqEnable = 0;
88
+ }
89
+
90
+ /**
91
+ * Verify the chip, unlock it and configure UV, ALS and PS1.
92
+ * @returns {Promise<void>}
93
+ * @throws {Error} If PART_ID is not 0x45/0x46/0x47 or a command fails.
94
+ */
95
+ async init() {
96
+ await _sleep(25);
97
+ const part = await this._readReg(_REG_PART_ID);
98
+ if (part < 0x45 || part > 0x47) {
99
+ throw new Error(`Unexpected Si1145 PART_ID 0x${part.toString(16)}, expected 0x45/0x46/0x47`);
100
+ }
101
+ this._partId = part;
102
+ this._seqId = await this._readReg(_REG_SEQ_ID);
103
+ await this._applyDefaults();
104
+ }
105
+
106
+ async _readReg(reg) {
107
+ return (await this._conn.readReg(reg, 1))[0];
108
+ }
109
+
110
+ async _writeReg(reg, value) {
111
+ await this._conn.writeReg(reg, [value & 0xFF]);
112
+ }
113
+
114
+ async _applyDefaults() {
115
+ await this._writeReg(_REG_HW_KEY, _HW_KEY);
116
+ await this._conn.writeReg(_REG_UCOEF0, Si1145Minimal.UCOEF_DEFAULT);
117
+ const chlist = Si1145Minimal.CH_UV | Si1145Minimal.CH_ALS_IR | Si1145Minimal.CH_ALS_VIS | Si1145Minimal.CH_PS1;
118
+ await this._paramSet(_PARAM_CHLIST, chlist);
119
+ this._chlist = chlist;
120
+ await this._paramSet(_PARAM_PSLED12_SELECT, 0x01);
121
+ await this._writeReg(_REG_PS_LED21, 0x03);
122
+ this._irqEnable = Si1145Minimal.SOURCE_ALS | Si1145Minimal.SOURCE_PS1;
123
+ await this._writeReg(_REG_IRQ_ENABLE, this._irqEnable);
124
+ }
125
+
126
+ async _counter() {
127
+ let r = await this._readReg(_REG_RESPONSE);
128
+ if (r & 0x80) {
129
+ await this._writeReg(_REG_COMMAND, _CMD_NOP);
130
+ r = await this._readReg(_REG_RESPONSE);
131
+ }
132
+ return r & 0x0F;
133
+ }
134
+
135
+ async _command(cmd) {
136
+ const before = await this._counter();
137
+ await this._writeReg(_REG_COMMAND, cmd);
138
+ for (let i = 0; i < _CMD_POLLS; i++) {
139
+ const r = await this._readReg(_REG_RESPONSE);
140
+ if (r & 0x80) {
141
+ await this._writeReg(_REG_COMMAND, _CMD_NOP);
142
+ throw new Error(`Si1145 command 0x${cmd.toString(16)} error 0x${r.toString(16)}`);
143
+ }
144
+ if ((r & 0x0F) !== before) return;
145
+ await _sleep(1);
146
+ }
147
+ throw new Error(`Si1145 command 0x${cmd.toString(16)} timeout`);
148
+ }
149
+
150
+ async _paramSet(offset, value) {
151
+ await this._writeReg(_REG_PARAM_WR, value);
152
+ await this._command(_CMD_PARAM_SET | (offset & 0x1F));
153
+ }
154
+
155
+ async _paramQuery(offset) {
156
+ await this._command(_CMD_PARAM_QUERY | (offset & 0x1F));
157
+ return this._readReg(_REG_PARAM_RD);
158
+ }
159
+
160
+ async _clearIrq() {
161
+ const status = await this._readReg(_REG_IRQ_STATUS);
162
+ if (status) await this._writeReg(_REG_IRQ_STATUS, status);
163
+ return status;
164
+ }
165
+
166
+ async _measure(proximity, als) {
167
+ const cmd = proximity && als ? _CMD_PSALS_FORCE : proximity ? _CMD_PS_FORCE : _CMD_ALS_FORCE;
168
+ await this._clearIrq();
169
+ await this._counter();
170
+ await this._writeReg(_REG_COMMAND, cmd);
171
+ let wait = 0;
172
+ if (als && (this._chlist & 0xF0)) wait |= Si1145Minimal.SOURCE_ALS;
173
+ if (proximity) wait |= (this._chlist & 0x07) << 2;
174
+ wait &= this._irqEnable;
175
+ if (wait === 0) {
176
+ await _sleep(100);
177
+ return;
178
+ }
179
+ let seen = 0;
180
+ for (let i = 0; i < _MEASURE_POLLS; i++) {
181
+ seen |= await this._clearIrq();
182
+ if ((seen & wait) === wait) return;
183
+ await _sleep(1);
184
+ }
185
+ throw new Error('Si1145 measurement timeout');
186
+ }
187
+
188
+ async _readData() {
189
+ const b = await this._conn.readReg(_REG_DATA, 12);
190
+ const v = [];
191
+ for (let i = 0; i < 12; i += 2) v.push(b[i] | (b[i + 1] << 8));
192
+ return v;
193
+ }
194
+
195
+ /**
196
+ * Run one forced PS + ALS measurement (PSALS_FORCE).
197
+ * @returns {Promise<{proximity: number, uv_index: number, visible: number, infrared: number}>}
198
+ * Raw PS1 count, UV index (AUX_DATA / 100) and raw visible/infrared counts.
199
+ * @throws {Error} If a command fails or the measurement does not finish.
200
+ */
201
+ async read() {
202
+ await this._measure(true, true);
203
+ const v = await this._readData();
204
+ return { proximity: v[2], uv_index: v[5] / 100, visible: v[0], infrared: v[1] };
205
+ }
206
+ }
207
+
208
+ /** CHLIST.EN_PS1. */
209
+ Si1145Minimal.CH_PS1 = 0x01;
210
+ /** CHLIST.EN_PS2 (Si1146/47). */
211
+ Si1145Minimal.CH_PS2 = 0x02;
212
+ /** CHLIST.EN_PS3 (Si1147). */
213
+ Si1145Minimal.CH_PS3 = 0x04;
214
+ /** CHLIST.EN_ALS_VIS. */
215
+ Si1145Minimal.CH_ALS_VIS = 0x10;
216
+ /** CHLIST.EN_ALS_IR. */
217
+ Si1145Minimal.CH_ALS_IR = 0x20;
218
+ /** CHLIST.EN_AUX. */
219
+ Si1145Minimal.CH_AUX = 0x40;
220
+ /** CHLIST.EN_UV. */
221
+ Si1145Minimal.CH_UV = 0x80;
222
+ /** Interrupt source: ALS/UV measurement complete. */
223
+ Si1145Minimal.SOURCE_ALS = 0x01;
224
+ /** Interrupt source: PS1 complete. */
225
+ Si1145Minimal.SOURCE_PS1 = 0x04;
226
+ /** Interrupt source: PS2 complete (Si1146/47). */
227
+ Si1145Minimal.SOURCE_PS2 = 0x08;
228
+ /** Interrupt source: PS3 complete (Si1147). */
229
+ Si1145Minimal.SOURCE_PS3 = 0x10;
230
+ /** Interrupt source: command complete. */
231
+ Si1145Minimal.SOURCE_CMD = 0x20;
232
+ /** Default UV coefficients: AUX_DATA = UV index × 100. */
233
+ Si1145Minimal.UCOEF_DEFAULT = [0x7B, 0x6B, 0x01, 0x00];
234
+
235
+ /**
236
+ * Si1145 full interface — extends Si1145Minimal with per-channel reads, channel list, LED and
237
+ * photodiode selection, gains, ranges, alignment, UV coefficients, calibration, autonomous mode,
238
+ * the I²C address, reset and the INT interrupt.
239
+ *
240
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection.
241
+ */
242
+ class Si1145Full extends Si1145Minimal {
243
+ constructor(connection) {
244
+ super(connection);
245
+ this._callback = null;
246
+ this._intPinUsed = null;
247
+ this._pollTimer = null;
248
+ this._polling = false;
249
+ this._edgeHandler = () => { this._service(); };
250
+ }
251
+
252
+ _ledCount() {
253
+ return this._partId - 0x44;
254
+ }
255
+
256
+ /**
257
+ * Read PART_ID, REV_ID and SEQ_ID.
258
+ * @returns {Promise<{partId: number, revId: number, seqId: number}>} 0x45/0x46/0x47, hardware
259
+ * revision and sequencer firmware revision (0x01 swaps the MEAS_RATE bytes).
260
+ */
261
+ async readId() {
262
+ const b = await this._conn.readReg(_REG_PART_ID, 3);
263
+ return { partId: b[0], revId: b[1], seqId: b[2] };
264
+ }
265
+
266
+ /**
267
+ * Forced PS measurement (PS_FORCE) and one proximity channel.
268
+ * @param {number} [channel=1] - PS channel 1–3 (Si1145: 1); must be enabled in CHLIST.
269
+ * @returns {Promise<number>} Raw proximity count (rises as an object approaches).
270
+ * @throws {RangeError} If the channel is not supported or not enabled.
271
+ */
272
+ async readProximity(channel = 1) {
273
+ _check('channel', channel, 1, this._ledCount());
274
+ if (!(this._chlist & (1 << (channel - 1)))) throw new RangeError(`PS${channel} is not enabled (setChannels)`);
275
+ await this._measure(true, false);
276
+ return (await this._readData())[1 + channel];
277
+ }
278
+
279
+ /**
280
+ * Forced ALS measurement and the UV index.
281
+ * @returns {Promise<number>} AUX_DATA / 100.
282
+ * @throws {RangeError} If UV is not enabled.
283
+ */
284
+ async readUvIndex() {
285
+ if (!(this._chlist & Si1145Minimal.CH_UV)) throw new RangeError('UV is not enabled (setChannels)');
286
+ await this._measure(false, true);
287
+ return (await this._readData())[5] / 100;
288
+ }
289
+
290
+ /**
291
+ * Forced ALS measurement and the raw visible count.
292
+ * @returns {Promise<number>} ALS_VIS_DATA.
293
+ * @throws {RangeError} If the visible channel is not enabled.
294
+ */
295
+ async readVisible() {
296
+ if (!(this._chlist & Si1145Minimal.CH_ALS_VIS)) throw new RangeError('visible is not enabled (setChannels)');
297
+ await this._measure(false, true);
298
+ return (await this._readData())[0];
299
+ }
300
+
301
+ /**
302
+ * Forced ALS measurement and the raw infrared count.
303
+ * @returns {Promise<number>} ALS_IR_DATA.
304
+ * @throws {RangeError} If the infrared channel is not enabled.
305
+ */
306
+ async readInfrared() {
307
+ if (!(this._chlist & Si1145Minimal.CH_ALS_IR)) throw new RangeError('infrared is not enabled (setChannels)');
308
+ await this._measure(false, true);
309
+ return (await this._readData())[1];
310
+ }
311
+
312
+ /**
313
+ * Read the latest data registers without measuring.
314
+ * @returns {Promise<{visible: number, infrared: number, ps1: number, ps2: number, ps3: number, aux: number}>}
315
+ * Raw 16-bit values (`aux` is the UV index × 100 while UV is enabled).
316
+ */
317
+ async readRaw() {
318
+ const [visible, infrared, ps1, ps2, ps3, aux] = await this._readData();
319
+ return { visible, infrared, ps1, ps2, ps3, aux };
320
+ }
321
+
322
+ async _readAux(mux) {
323
+ const saved = this._chlist;
324
+ await this._paramSet(_PARAM_AUX_ADCMUX, mux);
325
+ await this._paramSet(_PARAM_CHLIST, Si1145Minimal.CH_AUX);
326
+ this._chlist = Si1145Minimal.CH_AUX;
327
+ try {
328
+ await this._measure(false, true);
329
+ return (await this._readData())[5];
330
+ } finally {
331
+ await this._paramSet(_PARAM_CHLIST, saved);
332
+ this._chlist = saved;
333
+ }
334
+ }
335
+
336
+ /**
337
+ * Die temperature on the auxiliary channel (AUX_ADCMUX 0x65); CHLIST is switched to EN_AUX
338
+ * temporarily and restored.
339
+ * @returns {Promise<number>} Raw relative temperature count.
340
+ */
341
+ async readTemperatureRaw() {
342
+ return this._readAux(Si1145Full.AUX_TEMPERATURE);
343
+ }
344
+
345
+ /**
346
+ * Supply voltage on the auxiliary channel (AUX_ADCMUX 0x75); CHLIST restored afterwards.
347
+ * @returns {Promise<number>} Raw VDD count.
348
+ */
349
+ async readVddRaw() {
350
+ return this._readAux(Si1145Full.AUX_VDD);
351
+ }
352
+
353
+ /**
354
+ * Select the measured channels (CHLIST).
355
+ * @param {boolean} uv - UV index (in AUX_DATA); excludes `aux`.
356
+ * @param {boolean} visible - Visible ambient light.
357
+ * @param {boolean} infrared - Infrared ambient light.
358
+ * @param {boolean} ps1 - Proximity channel 1.
359
+ * @param {boolean} [ps2=false] - Proximity channel 2 (Si1146/47).
360
+ * @param {boolean} [ps3=false] - Proximity channel 3 (Si1147).
361
+ * @param {boolean} [aux=false] - Auxiliary channel (AUX_ADCMUX).
362
+ * @returns {Promise<void>}
363
+ * @throws {RangeError} For uv together with aux or an unsupported PS channel.
364
+ */
365
+ async setChannels(uv, visible, infrared, ps1, ps2 = false, ps3 = false, aux = false) {
366
+ if (uv && aux) throw new RangeError('uv and aux share AUX_DATA; enable only one');
367
+ if ((ps2 && this._ledCount() < 2) || (ps3 && this._ledCount() < 3)) {
368
+ throw new RangeError('PS channel not available on this part');
369
+ }
370
+ const mask = (uv ? 0x80 : 0) | (aux ? 0x40 : 0) | (infrared ? 0x20 : 0) | (visible ? 0x10 : 0) |
371
+ (ps3 ? 0x04 : 0) | (ps2 ? 0x02 : 0) | (ps1 ? 0x01 : 0);
372
+ await this._paramSet(_PARAM_CHLIST, mask);
373
+ this._chlist = mask;
374
+ }
375
+
376
+ /**
377
+ * Set an LED drive current (PS_LED21 / PS_LED3).
378
+ * @param {number} led - LED 1–3 (Si1145: 1 only).
379
+ * @param {number} level - Current level 0 (off) … 15 (maximum, ~359 mA).
380
+ * @returns {Promise<void>}
381
+ */
382
+ async setLedCurrent(led, level) {
383
+ _check('led', led, 1, this._ledCount());
384
+ _check('level', level, 0, 15);
385
+ if (led === 3) {
386
+ await this._writeReg(_REG_PS_LED3, level);
387
+ return;
388
+ }
389
+ const cur = await this._readReg(_REG_PS_LED21);
390
+ await this._writeReg(_REG_PS_LED21, led === 1 ? (cur & 0xF0) | level : (cur & 0x0F) | (level << 4));
391
+ }
392
+
393
+ /**
394
+ * Select the LEDs a PS channel drives (PSLED12_SELECT / PSLED3_SELECT).
395
+ * @param {number} channel - PS channel 1–3.
396
+ * @param {number} leds - LED mask (bit 0 LED1, bit 1 LED2, bit 2 LED3), only the part's LEDs.
397
+ * @returns {Promise<void>}
398
+ */
399
+ async setPsLed(channel, leds) {
400
+ _check('channel', channel, 1, 3);
401
+ _check('leds', leds, 0, (1 << this._ledCount()) - 1);
402
+ if (channel === 3) {
403
+ await this._paramSet(_PARAM_PSLED3_SELECT, leds);
404
+ return;
405
+ }
406
+ const cur = await this._paramQuery(_PARAM_PSLED12_SELECT);
407
+ await this._paramSet(_PARAM_PSLED12_SELECT, channel === 1 ? (cur & 0x70) | leds : (cur & 0x07) | (leds << 4));
408
+ }
409
+
410
+ /**
411
+ * Select the photodiode of a PS channel (PSn_ADCMUX).
412
+ * @param {number} channel - PS channel 1–3.
413
+ * @param {boolean} large - Large IR photodiode (0x03) or small (0x00).
414
+ * @returns {Promise<void>}
415
+ */
416
+ async setPsPhotodiode(channel, large) {
417
+ _check('channel', channel, 1, 3);
418
+ await this._paramSet(_PARAM_PS1_ADCMUX + channel - 1, large ? 0x03 : 0x00);
419
+ }
420
+
421
+ /**
422
+ * Set the ADC gains (integration time × 2^n) and the recommended recovery counts (one's
423
+ * complement of the gain). Do not combine a high PS gain with a high LED current.
424
+ * @param {number} [visible=0] - ALS_VIS_ADC_GAIN 0–7.
425
+ * @param {number} [infrared=0] - ALS_IR_ADC_GAIN 0–7.
426
+ * @param {number} [proximity=0] - PS_ADC_GAIN 0–7.
427
+ * @returns {Promise<void>}
428
+ */
429
+ async setGain(visible = 0, infrared = 0, proximity = 0) {
430
+ _check('visible', visible, 0, 7);
431
+ _check('infrared', infrared, 0, 7);
432
+ _check('proximity', proximity, 0, 7);
433
+ const sets = [[visible, _PARAM_ALS_VIS_ADC_GAIN, _PARAM_ALS_VIS_ADC_COUNTER],
434
+ [infrared, _PARAM_ALS_IR_ADC_GAIN, _PARAM_ALS_IR_ADC_COUNTER],
435
+ [proximity, _PARAM_PS_ADC_GAIN, _PARAM_PS_ADC_COUNTER]];
436
+ for (const [gain, gOff, cOff] of sets) {
437
+ await this._paramSet(gOff, gain);
438
+ await this._paramSet(cOff, ((~gain) & 0x07) << 4);
439
+ }
440
+ }
441
+
442
+ /**
443
+ * Select the high signal range (sensitivity ÷ 14.5) per measurement.
444
+ * @param {boolean} visible - VIS_RANGE.
445
+ * @param {boolean} infrared - IR_RANGE.
446
+ * @param {boolean} proximity - PS_RANGE (PS_ADC_MODE stays 1).
447
+ * @returns {Promise<void>}
448
+ */
449
+ async setHighRange(visible, infrared, proximity) {
450
+ await this._paramSet(_PARAM_ALS_VIS_ADC_MISC, visible ? 0x20 : 0);
451
+ await this._paramSet(_PARAM_ALS_IR_ADC_MISC, infrared ? 0x20 : 0);
452
+ await this._paramSet(_PARAM_PS_ADC_MISC, proximity ? 0x24 : 0x04);
453
+ }
454
+
455
+ /**
456
+ * Report the 16 LSBs instead of the 16 MSBs of the ADC result.
457
+ * @param {{visible?: boolean, infrared?: boolean, ps1?: boolean, ps2?: boolean, ps3?: boolean}} [align={}]
458
+ * @returns {Promise<void>}
459
+ */
460
+ async setAlignment({ visible = false, infrared = false, ps1 = false, ps2 = false, ps3 = false } = {}) {
461
+ await this._paramSet(_PARAM_ALS_ENCODING, (infrared ? 0x20 : 0) | (visible ? 0x10 : 0));
462
+ await this._paramSet(_PARAM_PS_ENCODING, (ps3 ? 0x40 : 0) | (ps2 ? 0x20 : 0) | (ps1 ? 0x10 : 0));
463
+ }
464
+
465
+ /**
466
+ * Write UCOEF0..3.
467
+ * @param {number[]} ucoef - Four bytes; `UCOEF_DEFAULT` gives the UV index × 100.
468
+ * @returns {Promise<void>}
469
+ */
470
+ async setUvCoefficients(ucoef) {
471
+ if (!Array.isArray(ucoef) || ucoef.length !== 4) throw new RangeError('ucoef must have four entries');
472
+ ucoef.forEach((c) => _check('ucoef', c, 0, 0xFF));
473
+ await this._conn.writeReg(_REG_UCOEF0, ucoef);
474
+ }
475
+
476
+ /**
477
+ * Factory calibration (GET_CAL); overwrites the data registers.
478
+ * @returns {Promise<number[]>} 12 calibration bytes.
479
+ */
480
+ async readCalibration() {
481
+ await this._command(_CMD_GET_CAL);
482
+ return Array.from(await this._conn.readReg(_REG_DATA, 12));
483
+ }
484
+
485
+ /**
486
+ * Start autonomous measurements: MEAS_RATE = round(interval / 31.25 µs) (bytes swapped when
487
+ * SEQ_ID = 0x01), then PS_AUTO / ALS_AUTO / PSALS_AUTO.
488
+ * @param {number} interval - Interval in seconds (31.25 µs … 2.048 s).
489
+ * @param {boolean} [proximity=true] - Run the PS channels.
490
+ * @param {boolean} [als=true] - Run the ALS/UV channels.
491
+ * @returns {Promise<void>}
492
+ */
493
+ async startAutonomous(interval, proximity = true, als = true) {
494
+ const rate = Math.round(interval / 31.25e-6);
495
+ _check('rate ticks', rate, 1, 0xFFFF);
496
+ if (!(proximity || als)) throw new RangeError('select proximity and/or als');
497
+ if (this._seqId === 0x01) {
498
+ await this._writeReg(_REG_PS_RATE, rate & 0xFF);
499
+ await this._writeReg(_REG_MEAS_RATE0, rate >> 8);
500
+ } else {
501
+ await this._conn.writeReg(_REG_MEAS_RATE0, [rate & 0xFF, rate >> 8]);
502
+ }
503
+ await this._command(proximity && als ? _CMD_PSALS_AUTO : proximity ? _CMD_PS_AUTO : _CMD_ALS_AUTO);
504
+ }
505
+
506
+ /** Pause the autonomous loop (PSALS_PAUSE). @returns {Promise<void>} */
507
+ async pause() {
508
+ await this._command(_CMD_PSALS_PAUSE);
509
+ }
510
+
511
+ /**
512
+ * Start a forced measurement without waiting (PS/ALS/PSALS_FORCE).
513
+ * @param {boolean} [proximity=true] - Measure the PS channels.
514
+ * @param {boolean} [als=true] - Measure the ALS/UV channels.
515
+ * @returns {Promise<void>}
516
+ */
517
+ async force(proximity = true, als = true) {
518
+ if (!(proximity || als)) throw new RangeError('select proximity and/or als');
519
+ await this._command(proximity && als ? _CMD_PSALS_FORCE : proximity ? _CMD_PS_FORCE : _CMD_ALS_FORCE);
520
+ }
521
+
522
+ /**
523
+ * Decode RESPONSE.
524
+ * @returns {Promise<{counter: number, error: number}>} 4-bit command counter and the error
525
+ * code (0 = none, 0x80 invalid setting, 0x88–0x8E ADC overflow).
526
+ */
527
+ async readResponse() {
528
+ const r = await this._readReg(_REG_RESPONSE);
529
+ return r & 0x80 ? { counter: 0, error: r } : { counter: r & 0x0F, error: 0 };
530
+ }
531
+
532
+ /**
533
+ * Change the I²C address (I2C_ADDR, then BUSADDR; volatile). The chip answers on the new
534
+ * address at once; create a new connection afterwards.
535
+ * @param {number} address - New 7-bit address 0x08–0x77.
536
+ * @returns {Promise<void>}
537
+ */
538
+ async setI2cAddress(address) {
539
+ _check('address', address, 0x08, 0x77);
540
+ await this._paramSet(_PARAM_I2C_ADDR, address);
541
+ await this._writeReg(_REG_COMMAND, _CMD_BUSADDR);
542
+ }
543
+
544
+ /** Software reset, 10 ms wait, HW_KEY and the Minimal defaults again. @returns {Promise<void>} */
545
+ async reset() {
546
+ await this._writeReg(_REG_COMMAND, _CMD_RESET);
547
+ await _sleep(10);
548
+ await this._applyDefaults();
549
+ }
550
+
551
+ /**
552
+ * Drive INT for sources (INT_CFG.INT_OE and IRQ_ENABLE).
553
+ * @param {number} source - SOURCE_* mask.
554
+ * @returns {Promise<void>}
555
+ */
556
+ async enableInterrupt(source) {
557
+ this._irqEnable = (this._irqEnable | source) & _SOURCE_MASK;
558
+ await this._writeReg(_REG_INT_CFG, 0x01);
559
+ await this._writeReg(_REG_IRQ_ENABLE, this._irqEnable);
560
+ }
561
+
562
+ /**
563
+ * Clear sources from IRQ_ENABLE. Forced reads only wait for enabled sources (none: a fixed
564
+ * 100 ms wait).
565
+ * @param {number} source - SOURCE_* mask.
566
+ * @returns {Promise<void>}
567
+ */
568
+ async disableInterrupt(source) {
569
+ this._irqEnable &= ~source & _SOURCE_MASK;
570
+ await this._writeReg(_REG_IRQ_ENABLE, this._irqEnable);
571
+ }
572
+
573
+ /**
574
+ * Read IRQ_STATUS and write it back to clear it (releases INT).
575
+ * @returns {Promise<number>} Pending SOURCE_* mask among the enabled sources, 0 = none.
576
+ */
577
+ async pollInterrupt() {
578
+ return (await this._clearIrq()) & this._irqEnable;
579
+ }
580
+
581
+ /**
582
+ * Subscribe to measurement events: INT falling edge via `intPin` (or the connection's pin),
583
+ * otherwise a 10 ms polling timer on IRQ_STATUS.
584
+ * @param {function(number): void} callback - Called with the SOURCE_* mask.
585
+ * @param {object|null} [intPin=null] - Optional InputPin wired to INT.
586
+ * @returns {Promise<void>}
587
+ */
588
+ async onInterrupt(callback, intPin = null) {
589
+ await this.offInterrupt();
590
+ this._callback = callback;
591
+ const ipin = intPin ?? this._conn.intPin ?? null;
592
+ this._intPinUsed = ipin;
593
+ if (ipin) {
594
+ await ipin.onEdge(this._edgeHandler, 'falling');
595
+ } else {
596
+ this._pollTimer = setInterval(() => { this._service(); }, 10);
597
+ if (this._pollTimer.unref) this._pollTimer.unref();
598
+ }
599
+ }
600
+
601
+ /** Unsubscribe and stop delivery. @returns {Promise<void>} */
602
+ async offInterrupt() {
603
+ if (this._intPinUsed) {
604
+ await this._intPinUsed.offEdge(this._edgeHandler);
605
+ this._intPinUsed = null;
606
+ }
607
+ if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
608
+ this._callback = null;
609
+ }
610
+
611
+ async _service() {
612
+ if (this._polling) return;
613
+ this._polling = true;
614
+ try {
615
+ const status = await this.pollInterrupt();
616
+ if (status && this._callback) this._callback(status);
617
+ } catch (e) {
618
+ /* a failed poll is retried on the next edge / tick */
619
+ } finally {
620
+ this._polling = false;
621
+ }
622
+ }
623
+ }
624
+
625
+ /** AUX_ADCMUX: die temperature. */
626
+ Si1145Full.AUX_TEMPERATURE = 0x65;
627
+ /** AUX_ADCMUX: supply voltage. */
628
+ Si1145Full.AUX_VDD = 0x75;
629
+
630
+ module.exports = { Si1145Minimal, Si1145Full };