periph 1.6.0 → 1.6.1
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 +11 -0
- package/examples/gas/rrh62000/complete.js +74 -0
- package/examples/gas/rrh62000/demo.js +87 -0
- package/examples/gas/rrh62000/minimal.js +22 -0
- package/examples/gas/scd30/complete.js +73 -0
- package/examples/gas/scd30/demo.js +102 -0
- package/examples/gas/scd30/minimal.js +20 -0
- package/examples/gas/scd40/complete.js +76 -0
- package/examples/gas/scd40/demo.js +109 -0
- package/examples/gas/scd40/minimal.js +20 -0
- package/examples/light/tsl2561/complete.js +74 -0
- package/examples/light/tsl2561/demo.js +46 -0
- package/examples/light/tsl2561/minimal.js +22 -0
- package/examples/light/tsl2591/complete.js +90 -0
- package/examples/light/tsl2591/demo.js +119 -0
- package/examples/light/tsl2591/minimal.js +22 -0
- package/examples/light/vcnl4040/complete.js +102 -0
- package/examples/light/vcnl4040/demo.js +125 -0
- package/examples/light/vcnl4040/minimal.js +25 -0
- package/examples/light/vcnl4200/complete.js +101 -0
- package/examples/light/vcnl4200/demo.js +126 -0
- package/examples/light/vcnl4200/minimal.js +25 -0
- package/examples/rfid/st25dv/complete.js +94 -0
- package/examples/rfid/st25dv/demo.js +121 -0
- package/examples/rfid/st25dv/minimal.js +21 -0
- package/examples/rtc/bq32000/complete.js +53 -0
- package/examples/rtc/bq32000/demo.js +77 -0
- package/examples/rtc/bq32000/minimal.js +20 -0
- package/examples/rtc/ds1307/complete.js +54 -0
- package/examples/rtc/ds1307/demo.js +96 -0
- package/examples/rtc/ds1307/minimal.js +20 -0
- package/examples/rtc/ds1371/complete.js +53 -0
- package/examples/rtc/ds1371/demo.js +85 -0
- package/examples/rtc/ds1371/minimal.js +25 -0
- package/package.json +1 -1
- package/src/chips/gas/rrh62000.js +418 -0
- package/src/chips/gas/scd30.js +368 -0
- package/src/chips/gas/scd40.js +451 -0
- package/src/chips/light/tsl2561.js +445 -0
- package/src/chips/light/tsl2591.js +561 -0
- package/src/chips/light/vcnl4040.js +576 -0
- package/src/chips/light/vcnl4200.js +592 -0
- package/src/chips/rfid/st25dv.js +628 -0
- package/src/chips/rtc/bq32000.js +334 -0
- package/src/chips/rtc/ds1307.js +327 -0
- package/src/chips/rtc/ds1371.js +453 -0
- package/src/discovery/registry.js +11 -11
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const CMD = 0xA0;
|
|
4
|
+
const CMD_FORCE_INT = 0xE4;
|
|
5
|
+
const CMD_CLEAR_ALS = 0xE6;
|
|
6
|
+
const CMD_CLEAR_BOTH = 0xE7;
|
|
7
|
+
const CMD_CLEAR_NP = 0xEA;
|
|
8
|
+
|
|
9
|
+
const REG_ENABLE = 0x00;
|
|
10
|
+
const REG_CONFIG = 0x01;
|
|
11
|
+
const REG_AILTL = 0x04;
|
|
12
|
+
const REG_NPAILTL = 0x08;
|
|
13
|
+
const REG_PERSIST = 0x0C;
|
|
14
|
+
const REG_ID = 0x12;
|
|
15
|
+
const REG_STATUS = 0x13;
|
|
16
|
+
const REG_C0DATAL = 0x14;
|
|
17
|
+
|
|
18
|
+
const ENABLE_PON = 0x01;
|
|
19
|
+
const ENABLE_AEN = 0x02;
|
|
20
|
+
const ENABLE_AIEN = 0x10;
|
|
21
|
+
const ENABLE_SAI = 0x40;
|
|
22
|
+
const ENABLE_NPIEN = 0x80;
|
|
23
|
+
|
|
24
|
+
const CONFIG_SRESET = 0x80;
|
|
25
|
+
|
|
26
|
+
const STATUS_AVALID = 0x01;
|
|
27
|
+
|
|
28
|
+
const SOURCE_ALS = 0x10;
|
|
29
|
+
const SOURCE_NO_PERSIST = 0x20;
|
|
30
|
+
|
|
31
|
+
const AUTO_GAIN_MIN_COUNT = 100;
|
|
32
|
+
const AUTO_GAIN_MAX_COUNT = 30000;
|
|
33
|
+
|
|
34
|
+
// PERSIST.APERS code -> consecutive out-of-range cycles.
|
|
35
|
+
const PERSIST_CYCLES = [0, 1, 2, 3, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60];
|
|
36
|
+
|
|
37
|
+
const GAIN_LOW = 0x00;
|
|
38
|
+
const GAIN_MEDIUM = 0x10;
|
|
39
|
+
const GAIN_HIGH = 0x20;
|
|
40
|
+
const GAIN_MAX = 0x30;
|
|
41
|
+
|
|
42
|
+
const ATIME_100MS = 0;
|
|
43
|
+
|
|
44
|
+
// Gains from least to most sensitive, used by autoGain().
|
|
45
|
+
const GAINS = [GAIN_LOW, GAIN_MEDIUM, GAIN_HIGH, GAIN_MAX];
|
|
46
|
+
|
|
47
|
+
// Nominal gain factors of the ams formula, by CONFIG.AGAIN value.
|
|
48
|
+
const GAIN_FACTOR = { [GAIN_LOW]: 1, [GAIN_MEDIUM]: 25, [GAIN_HIGH]: 428, [GAIN_MAX]: 9876 };
|
|
49
|
+
// Glass attenuation × device factor of the ams DN40 formula.
|
|
50
|
+
const LUX_DF = 408;
|
|
51
|
+
|
|
52
|
+
// AVALID poll step; the timeout is one integration time + 10 % + 10 ms.
|
|
53
|
+
const POLL_MS = 5;
|
|
54
|
+
|
|
55
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Full-scale ADC count of an integration time.
|
|
59
|
+
* @param {number} atime - ATIME_100MS … ATIME_600MS (0–5).
|
|
60
|
+
* @returns {number} 36863 for 100 ms, 65535 for 200 ms and longer.
|
|
61
|
+
*/
|
|
62
|
+
function saturationCount(atime) {
|
|
63
|
+
return atime === ATIME_100MS ? 36863 : 65535;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* Convert raw channel counts into lux with the ams DN40 formula:
|
|
68
|
+
* cpl = atime_ms × again / 408, lux = (ch0 − ch1)(1 − ch1/ch0) / cpl with the
|
|
69
|
+
* nominal gain factors 1 / 25 / 428 / 9876.
|
|
70
|
+
* @param {number} ch0 - Channel 0 count (visible + infrared).
|
|
71
|
+
* @param {number} ch1 - Channel 1 count (infrared).
|
|
72
|
+
* @param {number} gain - GAIN_LOW, GAIN_MEDIUM, GAIN_HIGH or GAIN_MAX.
|
|
73
|
+
* @param {number} atime - ATIME_100MS … ATIME_600MS (0–5).
|
|
74
|
+
* @returns {number} Illuminance in lux; 0 for ch0 = 0.
|
|
75
|
+
* @throws {RangeError} If gain or atime is not a valid setting.
|
|
76
|
+
*/
|
|
77
|
+
function calculateLux(ch0, ch1, gain, atime) {
|
|
78
|
+
if (!(gain in GAIN_FACTOR)) throw new RangeError('gain must be GAIN_LOW, GAIN_MEDIUM, GAIN_HIGH or GAIN_MAX');
|
|
79
|
+
if (!Number.isInteger(atime) || atime < 0 || atime > 5) throw new RangeError('atime must be 0-5');
|
|
80
|
+
if (ch0 <= 0) return 0;
|
|
81
|
+
const cpl = ((atime + 1) * 100 * GAIN_FACTOR[gain]) / LUX_DF;
|
|
82
|
+
return ((ch0 - ch1) * (1 - ch1 / ch0)) / cpl;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async function _wait(ms) {
|
|
86
|
+
const scaled = ms * TSL2591Minimal.waitFactor;
|
|
87
|
+
if (scaled > 0) await sleep(scaled);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* TSL2591 very-high-sensitivity ambient light sensor (ams OSRAM) — minimal interface.
|
|
92
|
+
*
|
|
93
|
+
* Channel 0 responds to visible and infrared light, channel 1 to infrared;
|
|
94
|
+
* the two 16-bit integrating ADC counts are combined with the ams DN40
|
|
95
|
+
* formula into lux (dynamic range about 600 000 000 : 1). The I²C address is
|
|
96
|
+
* fixed at 0x29. Every register access starts with the command byte
|
|
97
|
+
* `0xA0 | register`; both channels are read in one 4-byte block read (`0xB4`)
|
|
98
|
+
* so the pair belongs to one integration.
|
|
99
|
+
*/
|
|
100
|
+
class TSL2591Minimal {
|
|
101
|
+
/**
|
|
102
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (1-byte register address).
|
|
103
|
+
*/
|
|
104
|
+
constructor(connection) {
|
|
105
|
+
this._conn = connection;
|
|
106
|
+
this._gain = GAIN_MEDIUM;
|
|
107
|
+
this._atime = ATIME_100MS;
|
|
108
|
+
this._enable = ENABLE_PON | ENABLE_AEN;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** 7-bit I²C address (fixed). */
|
|
112
|
+
static get I2C_ADDRESS() { return 0x29; }
|
|
113
|
+
|
|
114
|
+
/** Value of the ID register. */
|
|
115
|
+
static get DEVICE_ID() { return 0x50; }
|
|
116
|
+
|
|
117
|
+
/** Low gain (1×). */
|
|
118
|
+
static get GAIN_LOW() { return GAIN_LOW; }
|
|
119
|
+
|
|
120
|
+
/** Medium gain (≈ 25×). */
|
|
121
|
+
static get GAIN_MEDIUM() { return GAIN_MEDIUM; }
|
|
122
|
+
|
|
123
|
+
/** High gain (≈ 428×). */
|
|
124
|
+
static get GAIN_HIGH() { return GAIN_HIGH; }
|
|
125
|
+
|
|
126
|
+
/** Maximum gain (≈ 9876×). */
|
|
127
|
+
static get GAIN_MAX() { return GAIN_MAX; }
|
|
128
|
+
|
|
129
|
+
/** Interrupt source: STATUS.AINT (persist window). */
|
|
130
|
+
static get SOURCE_ALS() { return SOURCE_ALS; }
|
|
131
|
+
|
|
132
|
+
/** Interrupt source: STATUS.NPINTR (no-persist window). */
|
|
133
|
+
static get SOURCE_NO_PERSIST() { return SOURCE_NO_PERSIST; }
|
|
134
|
+
|
|
135
|
+
/** Default lower channel-0 limit of autoGain(). */
|
|
136
|
+
static get AUTO_GAIN_MIN_COUNT() { return AUTO_GAIN_MIN_COUNT; }
|
|
137
|
+
|
|
138
|
+
/** Default upper channel-0 limit of autoGain(). */
|
|
139
|
+
static get AUTO_GAIN_MAX_COUNT() { return AUTO_GAIN_MAX_COUNT; }
|
|
140
|
+
|
|
141
|
+
/** 100 ms integration (full scale 36863). */
|
|
142
|
+
static get ATIME_100MS() { return 0; }
|
|
143
|
+
|
|
144
|
+
/** 200 ms integration. */
|
|
145
|
+
static get ATIME_200MS() { return 1; }
|
|
146
|
+
|
|
147
|
+
/** 300 ms integration. */
|
|
148
|
+
static get ATIME_300MS() { return 2; }
|
|
149
|
+
|
|
150
|
+
/** 400 ms integration. */
|
|
151
|
+
static get ATIME_400MS() { return 3; }
|
|
152
|
+
|
|
153
|
+
/** 500 ms integration. */
|
|
154
|
+
static get ATIME_500MS() { return 4; }
|
|
155
|
+
|
|
156
|
+
/** 600 ms integration. */
|
|
157
|
+
static get ATIME_600MS() { return 5; }
|
|
158
|
+
|
|
159
|
+
async _readByte(reg) {
|
|
160
|
+
return (await this._conn.readReg(CMD | reg, 1))[0];
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async _writeByte(reg, value) {
|
|
164
|
+
await this._conn.writeReg(CMD | reg, Buffer.from([value & 0xFF]));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
async _applyDefaults() {
|
|
168
|
+
this._gain = GAIN_MEDIUM;
|
|
169
|
+
this._atime = ATIME_100MS;
|
|
170
|
+
this._enable = ENABLE_PON | ENABLE_AEN;
|
|
171
|
+
await this._writeByte(REG_CONFIG, this._gain | this._atime);
|
|
172
|
+
await this._writeByte(REG_ENABLE, this._enable);
|
|
173
|
+
await this._waitValid();
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async _waitValid() {
|
|
177
|
+
const timeoutMs = (this._atime + 1) * 110 + 10;
|
|
178
|
+
const steps = Math.floor(timeoutMs / POLL_MS) + 1;
|
|
179
|
+
for (let i = 0; i < steps; i++) {
|
|
180
|
+
if ((await this._readByte(REG_STATUS)) & STATUS_AVALID) return;
|
|
181
|
+
await _wait(POLL_MS);
|
|
182
|
+
}
|
|
183
|
+
throw new Error('TSL2591: timeout waiting for AVALID');
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async _readChannels() {
|
|
187
|
+
await this._waitValid();
|
|
188
|
+
const b = await this._conn.readReg(CMD | REG_C0DATAL, 4);
|
|
189
|
+
return { ch0: b[0] | (b[1] << 8), ch1: b[2] | (b[3] << 8) };
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
_luxFrom(ch0, ch1) {
|
|
193
|
+
const limit = saturationCount(this._atime);
|
|
194
|
+
if (ch0 >= limit || ch1 >= limit) {
|
|
195
|
+
throw new Error(`TSL2591: channel saturated (ch0=${ch0}, ch1=${ch1})`);
|
|
196
|
+
}
|
|
197
|
+
return calculateLux(ch0, ch1, this._gain, this._atime);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Run the initialization sequence: check ID = 0x50, CONFIG ← 0x10 (medium
|
|
202
|
+
* gain, 100 ms), ENABLE ← 0x03 (PON | AEN) and wait for STATUS.AVALID
|
|
203
|
+
* (polling scaled by `TSL2591Minimal.waitFactor`).
|
|
204
|
+
* @returns {Promise<void>}
|
|
205
|
+
* @throws {Error} If the ID is not 0x50 or AVALID does not come up.
|
|
206
|
+
*/
|
|
207
|
+
async init() {
|
|
208
|
+
if ((await this._readByte(REG_ID)) !== TSL2591Minimal.DEVICE_ID) {
|
|
209
|
+
throw new Error('TSL2591: unexpected device ID');
|
|
210
|
+
}
|
|
211
|
+
await this._applyDefaults();
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Read the illuminance: waits for AVALID, block-reads both channels and
|
|
216
|
+
* applies the ams formula.
|
|
217
|
+
* @returns {Promise<number>} Lux; 0 when channel 0 reads 0.
|
|
218
|
+
* @throws {Error} If a channel is saturated (count at or above 36863 at 100 ms, 65535 otherwise) or AVALID times out.
|
|
219
|
+
*/
|
|
220
|
+
async read() {
|
|
221
|
+
const { ch0, ch1 } = await this._readChannels();
|
|
222
|
+
return this._luxFrom(ch0, ch1);
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Multiplier for every wait (tests set it to 0 to skip the sleeps). */
|
|
227
|
+
TSL2591Minimal.waitFactor = 1;
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* TSL2591 full interface — extends Minimal with raw counts, gain and
|
|
231
|
+
* integration time, auto-gain, power control, the persist and no-persist
|
|
232
|
+
* threshold windows, persistence, sleep-after-interrupt, special-function
|
|
233
|
+
* interrupt commands, status, reset and interrupt delivery.
|
|
234
|
+
*/
|
|
235
|
+
class TSL2591Full extends TSL2591Minimal {
|
|
236
|
+
/**
|
|
237
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (1-byte register address).
|
|
238
|
+
*/
|
|
239
|
+
constructor(connection) {
|
|
240
|
+
super(connection);
|
|
241
|
+
this._callback = null;
|
|
242
|
+
this._intPinUsed = null;
|
|
243
|
+
this._pollTimer = null;
|
|
244
|
+
this._polling = false;
|
|
245
|
+
this._edgeHandler = () => { this._service(); };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
async _writeEnable() {
|
|
249
|
+
await this._writeByte(REG_ENABLE, this._enable);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
async _writeConfig() {
|
|
253
|
+
await this._writeByte(REG_CONFIG, this._gain | this._atime);
|
|
254
|
+
if (this._enable & ENABLE_AEN) {
|
|
255
|
+
// Restart the integration so AVALID only reports data taken with the new setting.
|
|
256
|
+
await this._writeByte(REG_ENABLE, this._enable & ~ENABLE_AEN);
|
|
257
|
+
await this._writeEnable();
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
async _command(cmd) {
|
|
262
|
+
await this._conn.write(Buffer.from([cmd]));
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Read both ADC channels in one block read (waits for AVALID).
|
|
267
|
+
* @returns {Promise<{ch0: number, ch1: number}>} Channel 0 (visible + IR) and channel 1 (IR) counts.
|
|
268
|
+
*/
|
|
269
|
+
async readRaw() {
|
|
270
|
+
return this._readChannels();
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Read the illuminance; same as read().
|
|
275
|
+
* @returns {Promise<number>} Lux.
|
|
276
|
+
* @throws {Error} If a channel is saturated.
|
|
277
|
+
*/
|
|
278
|
+
async readLux() {
|
|
279
|
+
return this.read();
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Set the ADC gain (CONFIG.AGAIN); while the ALS is enabled the integration restarts.
|
|
284
|
+
* @param {number} gain - GAIN_LOW, GAIN_MEDIUM, GAIN_HIGH or GAIN_MAX.
|
|
285
|
+
* @returns {Promise<void>}
|
|
286
|
+
* @throws {RangeError} On any other value.
|
|
287
|
+
*/
|
|
288
|
+
async setGain(gain) {
|
|
289
|
+
if (!GAINS.includes(gain)) throw new RangeError('gain must be GAIN_LOW, GAIN_MEDIUM, GAIN_HIGH or GAIN_MAX');
|
|
290
|
+
this._gain = gain;
|
|
291
|
+
await this._writeConfig();
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Current ADC gain.
|
|
296
|
+
* @returns {number} GAIN_LOW … GAIN_MAX.
|
|
297
|
+
*/
|
|
298
|
+
getGain() { return this._gain; }
|
|
299
|
+
|
|
300
|
+
/**
|
|
301
|
+
* Set the integration time (CONFIG.ATIME); while the ALS is enabled the integration restarts.
|
|
302
|
+
* @param {number} atime - ATIME_100MS … ATIME_600MS (0–5).
|
|
303
|
+
* @returns {Promise<void>}
|
|
304
|
+
* @throws {RangeError} On any other value.
|
|
305
|
+
*/
|
|
306
|
+
async setIntegrationTime(atime) {
|
|
307
|
+
if (!Number.isInteger(atime) || atime < 0 || atime > 5) throw new RangeError('atime must be 0-5');
|
|
308
|
+
this._atime = atime;
|
|
309
|
+
await this._writeConfig();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Current integration time setting.
|
|
314
|
+
* @returns {number} ATIME_100MS … ATIME_600MS (0–5).
|
|
315
|
+
*/
|
|
316
|
+
getIntegrationTime() { return this._atime; }
|
|
317
|
+
|
|
318
|
+
/**
|
|
319
|
+
* Current integration time.
|
|
320
|
+
* @returns {number} Integration time in s, (ATIME + 1) × 0.1.
|
|
321
|
+
*/
|
|
322
|
+
integrationTime() { return (this._atime + 1) * 0.1; }
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Raise or lower the gain until channel 0 lies within the limits: one gain
|
|
326
|
+
* up while channel 0 is below minCount, one gain down while it is above
|
|
327
|
+
* maxCount or a channel saturates, waiting for a fresh integration after
|
|
328
|
+
* every change; stops at the end of the gain range. The integration time
|
|
329
|
+
* is not changed.
|
|
330
|
+
* @param {number} [minCount=100] - Lower channel-0 limit in counts.
|
|
331
|
+
* @param {number} [maxCount=30000] - Upper channel-0 limit in counts.
|
|
332
|
+
* @returns {Promise<number>} The selected gain.
|
|
333
|
+
*/
|
|
334
|
+
async autoGain(minCount = AUTO_GAIN_MIN_COUNT, maxCount = AUTO_GAIN_MAX_COUNT) {
|
|
335
|
+
let index = GAINS.indexOf(this._gain);
|
|
336
|
+
for (let step = 0; step < 2 * GAINS.length; step++) {
|
|
337
|
+
const { ch0, ch1 } = await this._readChannels();
|
|
338
|
+
const limit = saturationCount(this._atime);
|
|
339
|
+
if ((ch0 > maxCount || ch0 >= limit || ch1 >= limit) && index > 0) index--;
|
|
340
|
+
else if (ch0 < minCount && index < GAINS.length - 1) index++;
|
|
341
|
+
else break;
|
|
342
|
+
await this.setGain(GAINS[index]);
|
|
343
|
+
}
|
|
344
|
+
return this._gain;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Power up and enable the ALS (ENABLE.PON | AEN); keeps the interrupt enables.
|
|
349
|
+
* @returns {Promise<void>}
|
|
350
|
+
*/
|
|
351
|
+
async enable() {
|
|
352
|
+
this._enable |= ENABLE_PON | ENABLE_AEN;
|
|
353
|
+
await this._writeEnable();
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Power down (ENABLE.PON = AEN = 0); keeps the interrupt enables.
|
|
358
|
+
* @returns {Promise<void>}
|
|
359
|
+
*/
|
|
360
|
+
async disable() {
|
|
361
|
+
this._enable &= ~(ENABLE_PON | ENABLE_AEN);
|
|
362
|
+
await this._writeEnable();
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
/**
|
|
366
|
+
* Whether an integration has completed since the ALS was enabled.
|
|
367
|
+
* @returns {Promise<boolean>} STATUS.AVALID.
|
|
368
|
+
*/
|
|
369
|
+
async dataValid() {
|
|
370
|
+
return ((await this._readByte(REG_STATUS)) & STATUS_AVALID) !== 0;
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Set a channel-0 interrupt window (one 4-byte auto-increment write).
|
|
375
|
+
* @param {number} low - Low threshold, channel-0 counts 0–65535.
|
|
376
|
+
* @param {number} high - High threshold, channel-0 counts 0–65535.
|
|
377
|
+
* @param {boolean} [noPersist=false] - false for AILT/AIHT (persist filter), true for NPAILT/NPAIHT.
|
|
378
|
+
* @returns {Promise<void>}
|
|
379
|
+
* @throws {RangeError} If a value is outside 0–65535.
|
|
380
|
+
*/
|
|
381
|
+
async setThresholds(low, high, noPersist = false) {
|
|
382
|
+
for (const v of [low, high]) {
|
|
383
|
+
if (!Number.isInteger(v) || v < 0 || v > 0xFFFF) throw new RangeError('threshold must be 0-65535');
|
|
384
|
+
}
|
|
385
|
+
const reg = noPersist ? REG_NPAILTL : REG_AILTL;
|
|
386
|
+
await this._conn.writeReg(CMD | reg, Buffer.from([low & 0xFF, low >> 8, high & 0xFF, high >> 8]));
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Set the persist filter (PERSIST.APERS).
|
|
391
|
+
* @param {number} cycles - 0–60 consecutive out-of-range cycles, rounded to
|
|
392
|
+
* the nearest valid value (0, 1, 2, 3, 5, 10, 15, …, 60; ties go down).
|
|
393
|
+
* @returns {Promise<void>}
|
|
394
|
+
* @throws {RangeError} If cycles is outside 0–60.
|
|
395
|
+
*/
|
|
396
|
+
async setPersistence(cycles) {
|
|
397
|
+
if (!Number.isInteger(cycles) || cycles < 0 || cycles > 60) throw new RangeError('cycles must be 0-60');
|
|
398
|
+
let best = 0;
|
|
399
|
+
PERSIST_CYCLES.forEach((value, code) => {
|
|
400
|
+
if (Math.abs(value - cycles) < Math.abs(PERSIST_CYCLES[best] - cycles)) best = code;
|
|
401
|
+
});
|
|
402
|
+
await this._writeByte(REG_PERSIST, best);
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
/**
|
|
406
|
+
* Set ENABLE.SAI: power down at the end of the ALS cycle that raised an interrupt.
|
|
407
|
+
* @param {boolean} enabled - true to sleep after an interrupt.
|
|
408
|
+
* @returns {Promise<void>}
|
|
409
|
+
*/
|
|
410
|
+
async setSleepAfterInterrupt(enabled) {
|
|
411
|
+
if (enabled) this._enable |= ENABLE_SAI;
|
|
412
|
+
else this._enable &= ~ENABLE_SAI;
|
|
413
|
+
await this._writeEnable();
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
/**
|
|
417
|
+
* Force AINT and NPINTR (special function 0xE4); clear with clearInterrupt({ both: true }).
|
|
418
|
+
* @returns {Promise<void>}
|
|
419
|
+
*/
|
|
420
|
+
async forceInterrupt() {
|
|
421
|
+
await this._command(CMD_FORCE_INT);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/**
|
|
425
|
+
* Clear interrupts with a special function: 0xE6 (ALS), 0xEA (no-persist) or 0xE7 (both).
|
|
426
|
+
* @param {object} [options]
|
|
427
|
+
* @param {boolean} [options.noPersist=false] - Clear the no-persist interrupt instead of the ALS one.
|
|
428
|
+
* @param {boolean} [options.both=false] - Clear both; overrides noPersist.
|
|
429
|
+
* @returns {Promise<void>}
|
|
430
|
+
*/
|
|
431
|
+
async clearInterrupt({ noPersist = false, both = false } = {}) {
|
|
432
|
+
await this._command(both ? CMD_CLEAR_BOTH : noPersist ? CMD_CLEAR_NP : CMD_CLEAR_ALS);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
/**
|
|
436
|
+
* Read and decode STATUS.
|
|
437
|
+
* @returns {Promise<{avalid: boolean, aint: boolean, npintr: boolean}>}
|
|
438
|
+
*/
|
|
439
|
+
async status() {
|
|
440
|
+
const v = await this._readByte(REG_STATUS);
|
|
441
|
+
return { avalid: (v & STATUS_AVALID) !== 0, aint: (v & SOURCE_ALS) !== 0, npintr: (v & SOURCE_NO_PERSIST) !== 0 };
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* Read the ID register.
|
|
446
|
+
* @returns {Promise<number>} Device ID (0x50).
|
|
447
|
+
*/
|
|
448
|
+
async deviceId() {
|
|
449
|
+
return this._readByte(REG_ID);
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
/**
|
|
453
|
+
* Reset the chip (CONFIG.SRESET; a missing acknowledge is ignored) and
|
|
454
|
+
* re-apply the Minimal defaults: medium gain, 100 ms, ALS enabled,
|
|
455
|
+
* interrupts off; waits for AVALID.
|
|
456
|
+
* @returns {Promise<void>}
|
|
457
|
+
*/
|
|
458
|
+
async reset() {
|
|
459
|
+
try {
|
|
460
|
+
await this._writeByte(REG_CONFIG, CONFIG_SRESET);
|
|
461
|
+
} catch (e) {
|
|
462
|
+
// the chip may not acknowledge the reset write
|
|
463
|
+
}
|
|
464
|
+
await _wait(10);
|
|
465
|
+
await this._applyDefaults();
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
/**
|
|
469
|
+
* Compute lux from raw counts (no bus access); see calculateLux().
|
|
470
|
+
* @param {number} ch0 - Channel 0 count.
|
|
471
|
+
* @param {number} ch1 - Channel 1 count.
|
|
472
|
+
* @param {number} gain - GAIN_LOW … GAIN_MAX.
|
|
473
|
+
* @param {number} atime - ATIME_100MS … ATIME_600MS.
|
|
474
|
+
* @returns {number} Lux; 0 for ch0 = 0.
|
|
475
|
+
*/
|
|
476
|
+
luxFromRaw(ch0, ch1, gain, atime) {
|
|
477
|
+
return calculateLux(ch0, ch1, gain, atime);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Enable interrupt sources on INT.
|
|
482
|
+
* @param {number} source - SOURCE_ALS (ENABLE.AIEN), SOURCE_NO_PERSIST (ENABLE.NPIEN) or both or-ed.
|
|
483
|
+
* @returns {Promise<void>}
|
|
484
|
+
*/
|
|
485
|
+
async enableInterrupt(source) {
|
|
486
|
+
if (source & SOURCE_ALS) this._enable |= ENABLE_AIEN;
|
|
487
|
+
if (source & SOURCE_NO_PERSIST) this._enable |= ENABLE_NPIEN;
|
|
488
|
+
await this._writeEnable();
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Disable interrupt sources.
|
|
493
|
+
* @param {number} source - SOURCE_ALS, SOURCE_NO_PERSIST or both or-ed.
|
|
494
|
+
* @returns {Promise<void>}
|
|
495
|
+
*/
|
|
496
|
+
async disableInterrupt(source) {
|
|
497
|
+
if (source & SOURCE_ALS) this._enable &= ~ENABLE_AIEN;
|
|
498
|
+
if (source & SOURCE_NO_PERSIST) this._enable &= ~ENABLE_NPIEN;
|
|
499
|
+
await this._writeEnable();
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Read STATUS and clear the reported interrupts with the matching special function.
|
|
504
|
+
* @returns {Promise<number>} Pending sources (SOURCE_ALS | SOURCE_NO_PERSIST), 0 if none.
|
|
505
|
+
*/
|
|
506
|
+
async pollInterrupt() {
|
|
507
|
+
const sources = (await this._readByte(REG_STATUS)) & (SOURCE_ALS | SOURCE_NO_PERSIST);
|
|
508
|
+
if (sources === (SOURCE_ALS | SOURCE_NO_PERSIST)) await this.clearInterrupt({ both: true });
|
|
509
|
+
else if (sources === SOURCE_NO_PERSIST) await this.clearInterrupt({ noPersist: true });
|
|
510
|
+
else if (sources === SOURCE_ALS) await this.clearInterrupt();
|
|
511
|
+
return sources;
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Subscribe to INT (falling edge). `callback(status)` receives the pending
|
|
516
|
+
* sources as returned by pollInterrupt(), which also clears them. With a
|
|
517
|
+
* pin (options.intPin or connection.intPin) the handler runs on the edge;
|
|
518
|
+
* without one a timer calls pollInterrupt() once per integration time.
|
|
519
|
+
* @param {function} callback - Called with (status).
|
|
520
|
+
* @param {object} [options]
|
|
521
|
+
* @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
|
|
522
|
+
* @returns {Promise<void>}
|
|
523
|
+
*/
|
|
524
|
+
async onInterrupt(callback, options = {}) {
|
|
525
|
+
this._callback = callback;
|
|
526
|
+
const intPin = options.intPin ?? this._conn.intPin ?? null;
|
|
527
|
+
this._intPinUsed = intPin;
|
|
528
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
529
|
+
if (intPin) {
|
|
530
|
+
await intPin.onEdge(this._edgeHandler, 'falling');
|
|
531
|
+
} else {
|
|
532
|
+
this._pollTimer = setInterval(() => { this._service(); }, (this._atime + 1) * 100);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Unsubscribe and stop delivery.
|
|
538
|
+
* @returns {Promise<void>}
|
|
539
|
+
*/
|
|
540
|
+
async offInterrupt() {
|
|
541
|
+
if (this._intPinUsed) {
|
|
542
|
+
await this._intPinUsed.offEdge(this._edgeHandler);
|
|
543
|
+
this._intPinUsed = null;
|
|
544
|
+
}
|
|
545
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
546
|
+
this._callback = null;
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
async _service() {
|
|
550
|
+
if (this._polling) return;
|
|
551
|
+
this._polling = true;
|
|
552
|
+
try {
|
|
553
|
+
const sources = await this.pollInterrupt();
|
|
554
|
+
if (sources && this._callback) this._callback(sources);
|
|
555
|
+
} finally {
|
|
556
|
+
this._polling = false;
|
|
557
|
+
}
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
|
|
561
|
+
module.exports = { TSL2591Minimal, TSL2591Full, calculateLux, saturationCount };
|