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,576 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const REG_ALS_CONF = 0x00;
|
|
4
|
+
const REG_ALS_THDH = 0x01;
|
|
5
|
+
const REG_ALS_THDL = 0x02;
|
|
6
|
+
const REG_PS_CONF12 = 0x03;
|
|
7
|
+
const REG_PS_CONF3_MS = 0x04;
|
|
8
|
+
const REG_PS_CANC = 0x05;
|
|
9
|
+
const REG_PS_THDL = 0x06;
|
|
10
|
+
const REG_PS_THDH = 0x07;
|
|
11
|
+
const REG_PS_DATA = 0x08;
|
|
12
|
+
const REG_ALS_DATA = 0x09;
|
|
13
|
+
const REG_WHITE_DATA = 0x0A;
|
|
14
|
+
const REG_INT_FLAG = 0x0B;
|
|
15
|
+
const REG_ID = 0x0C;
|
|
16
|
+
|
|
17
|
+
// ALS_CONF (0x00, low byte)
|
|
18
|
+
const ALS_IT_MASK = 0x00C0;
|
|
19
|
+
const ALS_PERS_MASK = 0x000C;
|
|
20
|
+
const ALS_INT_EN = 0x0002;
|
|
21
|
+
const ALS_SD = 0x0001;
|
|
22
|
+
|
|
23
|
+
// PS_CONF1 (0x03 low byte) / PS_CONF2 (0x03 high byte)
|
|
24
|
+
const PS_DUTY_MASK = 0x00C0;
|
|
25
|
+
const PS_PERS_MASK = 0x0030;
|
|
26
|
+
const PS_IT_MASK = 0x000E;
|
|
27
|
+
const PS_SD = 0x0001;
|
|
28
|
+
const PS_HD = 0x0800;
|
|
29
|
+
const PS_INT_CLOSE = 0x0100;
|
|
30
|
+
const PS_INT_AWAY = 0x0200;
|
|
31
|
+
|
|
32
|
+
// PS_CONF3 (0x04 low byte) / PS_MS (0x04 high byte)
|
|
33
|
+
const PS_MPS_MASK = 0x0060;
|
|
34
|
+
const PS_SMART_PERS = 0x0010;
|
|
35
|
+
const PS_AF = 0x0008;
|
|
36
|
+
const PS_TRIG = 0x0004;
|
|
37
|
+
const PS_SC_EN = 0x0001;
|
|
38
|
+
const WHITE_EN = 0x8000; // active low: 0 = white channel enabled
|
|
39
|
+
const PS_MS = 0x4000;
|
|
40
|
+
const LED_I_MASK = 0x0700;
|
|
41
|
+
|
|
42
|
+
const SOURCE_PS_AWAY = 0x01;
|
|
43
|
+
const SOURCE_PS_CLOSE = 0x02;
|
|
44
|
+
const SOURCE_ALS_HIGH = 0x10;
|
|
45
|
+
const SOURCE_ALS_LOW = 0x20;
|
|
46
|
+
const FLAG_PROTECTION = 0x40;
|
|
47
|
+
const SOURCES_ALS = SOURCE_ALS_HIGH | SOURCE_ALS_LOW;
|
|
48
|
+
const SOURCES_PS = SOURCE_PS_AWAY | SOURCE_PS_CLOSE;
|
|
49
|
+
|
|
50
|
+
// Minimal defaults: ALS 80 ms on; PS duty 1/160, 1T, on, 16 bit; white off, 50 mA.
|
|
51
|
+
const DEFAULT_ALS_CONF = 0x0000;
|
|
52
|
+
const DEFAULT_PS_CONF12 = 0x0880;
|
|
53
|
+
const DEFAULT_PS_CONF3_MS = 0x8000;
|
|
54
|
+
|
|
55
|
+
const ALS_IT_MS = [80, 160, 320, 640];
|
|
56
|
+
const ALS_SENSITIVITY = { 80: 0.1, 160: 0.05, 320: 0.025, 640: 0.0125 };
|
|
57
|
+
const ALS_PERS = [1, 2, 4, 8];
|
|
58
|
+
const PS_DUTY = [40, 80, 160, 320];
|
|
59
|
+
const PS_IT = [1, 1.5, 2, 2.5, 3, 3.5, 4, 8];
|
|
60
|
+
const PS_MPS = [1, 2, 4, 8];
|
|
61
|
+
const LED_MA = [50, 75, 100, 120, 140, 160, 180, 200];
|
|
62
|
+
|
|
63
|
+
// First ALS result after power-on (80 ms integration + margin).
|
|
64
|
+
const INIT_WAIT_MS = 100;
|
|
65
|
+
// One active-force PS cycle; the datasheet gives no bound, 20 ms covers 8T x 8 pulses.
|
|
66
|
+
const TRIGGER_WAIT_MS = 20;
|
|
67
|
+
// Polling interval of onInterrupt() without a pin.
|
|
68
|
+
const POLL_INTERVAL_MS = 100;
|
|
69
|
+
|
|
70
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
71
|
+
|
|
72
|
+
async function _wait(ms) {
|
|
73
|
+
const scaled = ms * VCNL4040Minimal.waitFactor;
|
|
74
|
+
if (scaled > 0) await sleep(scaled);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function _code(table, value, message) {
|
|
78
|
+
const code = table.indexOf(value);
|
|
79
|
+
if (code < 0) throw new RangeError(`VCNL4040: ${message}`);
|
|
80
|
+
return code;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* ALS resolution of an integration time.
|
|
85
|
+
* @param {number} milliseconds - 80, 160, 320 or 640.
|
|
86
|
+
* @returns {number} lx per count (0.1, 0.05, 0.025 or 0.0125).
|
|
87
|
+
* @throws {RangeError} If `milliseconds` is not a valid integration time.
|
|
88
|
+
*/
|
|
89
|
+
function alsSensitivity(milliseconds) {
|
|
90
|
+
if (!(milliseconds in ALS_SENSITIVITY)) throw new RangeError('VCNL4040: milliseconds must be 80, 160, 320 or 640');
|
|
91
|
+
return ALS_SENSITIVITY[milliseconds];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Maximum ALS detection range of an integration time.
|
|
96
|
+
* @param {number} milliseconds - 80, 160, 320 or 640.
|
|
97
|
+
* @returns {number} 65535 counts × the sensitivity, in lx (6553.5 … 819.2).
|
|
98
|
+
* @throws {RangeError} If `milliseconds` is not a valid integration time.
|
|
99
|
+
*/
|
|
100
|
+
function alsMaxRange(milliseconds) {
|
|
101
|
+
return 65535 * alsSensitivity(milliseconds);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* VCNL4040 proximity sensor with ambient light sensor (Vishay) — minimal interface.
|
|
106
|
+
*
|
|
107
|
+
* Combines a proximity sensor (12- or 16-bit output, internal 940 nm IRED at
|
|
108
|
+
* 50–200 mA), a 16-bit ambient-light sensor (0.0125–0.1 lx/count) and a white
|
|
109
|
+
* channel. The I²C address is fixed at 0x60; every command code selects a
|
|
110
|
+
* 16-bit register transferred low byte first.
|
|
111
|
+
*/
|
|
112
|
+
class VCNL4040Minimal {
|
|
113
|
+
/**
|
|
114
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (1-byte command code).
|
|
115
|
+
*/
|
|
116
|
+
constructor(connection) {
|
|
117
|
+
this._conn = connection;
|
|
118
|
+
this._alsItMs = 80;
|
|
119
|
+
this._psBits = 16;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async _readWord(cmd) {
|
|
123
|
+
const b = await this._conn.readReg(cmd, 2);
|
|
124
|
+
return b[0] | (b[1] << 8);
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
async _writeWord(cmd, value) {
|
|
128
|
+
await this._conn.writeReg(cmd, Buffer.from([value & 0xFF, (value >> 8) & 0xFF]));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Check the ID (0x0186), power on the ALS (80 ms, 0.1 lx/count) and the PS
|
|
133
|
+
* (16 bit, duty 1/160, 1T, 50 mA, white channel off) and wait for the first
|
|
134
|
+
* ALS result (scaled by `VCNL4040Minimal.waitFactor`).
|
|
135
|
+
* @returns {Promise<void>}
|
|
136
|
+
* @throws {Error} If the ID register does not read 0x0186.
|
|
137
|
+
*/
|
|
138
|
+
async init() {
|
|
139
|
+
if ((await this._readWord(REG_ID)) !== VCNL4040Minimal.DEVICE_ID) {
|
|
140
|
+
throw new Error('VCNL4040: unexpected device ID');
|
|
141
|
+
}
|
|
142
|
+
this._alsItMs = 80;
|
|
143
|
+
this._psBits = 16;
|
|
144
|
+
await this._writeWord(REG_ALS_CONF, DEFAULT_ALS_CONF);
|
|
145
|
+
await this._writeWord(REG_PS_CONF12, DEFAULT_PS_CONF12);
|
|
146
|
+
await this._writeWord(REG_PS_CONF3_MS, DEFAULT_PS_CONF3_MS);
|
|
147
|
+
await _wait(INIT_WAIT_MS);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Read the proximity output.
|
|
152
|
+
* @returns {Promise<number>} PS_DATA counts (0–65535, 0–4095 at 12 bit); larger = closer.
|
|
153
|
+
*/
|
|
154
|
+
async readProximity() {
|
|
155
|
+
return this._readWord(REG_PS_DATA);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Read the ambient light.
|
|
160
|
+
* @returns {Promise<number>} ALS_DATA × the sensitivity of the current integration time, in lx.
|
|
161
|
+
*/
|
|
162
|
+
async readLux() {
|
|
163
|
+
return (await this._readWord(REG_ALS_DATA)) * ALS_SENSITIVITY[this._alsItMs];
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
VCNL4040Minimal.I2C_ADDRESS = 0x60;
|
|
168
|
+
VCNL4040Minimal.DEVICE_ID = 0x0186;
|
|
169
|
+
VCNL4040Minimal.SOURCE_PS_AWAY = SOURCE_PS_AWAY;
|
|
170
|
+
VCNL4040Minimal.SOURCE_PS_CLOSE = SOURCE_PS_CLOSE;
|
|
171
|
+
VCNL4040Minimal.SOURCE_ALS_HIGH = SOURCE_ALS_HIGH;
|
|
172
|
+
VCNL4040Minimal.SOURCE_ALS_LOW = SOURCE_ALS_LOW;
|
|
173
|
+
VCNL4040Minimal.FLAG_PROTECTION = FLAG_PROTECTION;
|
|
174
|
+
/** Multiplier for every wait (tests set it to 0 to skip the sleeps). */
|
|
175
|
+
VCNL4040Minimal.waitFactor = 1;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* VCNL4040 full interface — extends Minimal with the raw ALS and white
|
|
179
|
+
* channels, ALS and PS configuration, thresholds, cancellation, active-force
|
|
180
|
+
* and logic-output modes, power control and interrupt delivery. Fields that
|
|
181
|
+
* share a word (0x03, 0x04) are changed by read-modify-write.
|
|
182
|
+
*/
|
|
183
|
+
class VCNL4040Full extends VCNL4040Minimal {
|
|
184
|
+
/**
|
|
185
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (1-byte command code).
|
|
186
|
+
*/
|
|
187
|
+
constructor(connection) {
|
|
188
|
+
super(connection);
|
|
189
|
+
this._logicOutput = false;
|
|
190
|
+
this._callback = null;
|
|
191
|
+
this._intPinUsed = null;
|
|
192
|
+
this._pollTimer = null;
|
|
193
|
+
this._polling = false;
|
|
194
|
+
this._edgeHandler = () => { this._service(); };
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async _update(cmd, mask, value) {
|
|
198
|
+
const word = await this._readWord(cmd);
|
|
199
|
+
await this._writeWord(cmd, (word & ~mask & 0xFFFF) | (value & mask));
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
async _setBit(cmd, bit, on) {
|
|
203
|
+
await this._update(cmd, bit, on ? bit : 0);
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Read the raw ALS output.
|
|
208
|
+
* @returns {Promise<number>} ALS_DATA counts (0–65535).
|
|
209
|
+
*/
|
|
210
|
+
async readAls() {
|
|
211
|
+
return this._readWord(REG_ALS_DATA);
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* Read the raw white-channel output (enable it with setWhiteChannel()).
|
|
216
|
+
* @returns {Promise<number>} WHITE_DATA counts (0–65535).
|
|
217
|
+
*/
|
|
218
|
+
async readWhite() {
|
|
219
|
+
return this._readWord(REG_WHITE_DATA);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Set the ALS integration time (ALS_IT): 80 ms = 0.1 lx/count (6553.5 lx)
|
|
224
|
+
* … 640 ms = 0.0125 lx/count (819.2 lx).
|
|
225
|
+
* @param {number} milliseconds - 80, 160, 320 or 640.
|
|
226
|
+
* @returns {Promise<void>}
|
|
227
|
+
* @throws {RangeError} If `milliseconds` is not valid.
|
|
228
|
+
*/
|
|
229
|
+
async setAlsIntegrationTime(milliseconds) {
|
|
230
|
+
const code = _code(ALS_IT_MS, milliseconds, 'milliseconds must be 80, 160, 320 or 640');
|
|
231
|
+
await this._update(REG_ALS_CONF, ALS_IT_MASK, code << 6);
|
|
232
|
+
this._alsItMs = milliseconds;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Read the ALS integration time.
|
|
237
|
+
* @returns {Promise<number>} 80, 160, 320 or 640 ms.
|
|
238
|
+
*/
|
|
239
|
+
async getAlsIntegrationTime() {
|
|
240
|
+
this._alsItMs = ALS_IT_MS[((await this._readWord(REG_ALS_CONF)) & ALS_IT_MASK) >> 6];
|
|
241
|
+
return this._alsItMs;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Set how many consecutive out-of-window ALS results raise the interrupt (ALS_PERS).
|
|
246
|
+
* @param {number} count - 1, 2, 4 or 8.
|
|
247
|
+
* @returns {Promise<void>}
|
|
248
|
+
* @throws {RangeError} If `count` is not valid.
|
|
249
|
+
*/
|
|
250
|
+
async setAlsPersistence(count) {
|
|
251
|
+
const code = _code(ALS_PERS, count, 'count must be 1, 2, 4 or 8');
|
|
252
|
+
await this._update(REG_ALS_CONF, ALS_PERS_MASK, code << 2);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Power on the ALS (ALS_SD = 0).
|
|
257
|
+
* @returns {Promise<void>}
|
|
258
|
+
*/
|
|
259
|
+
async alsEnable() {
|
|
260
|
+
await this._setBit(REG_ALS_CONF, ALS_SD, false);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Shut down the ALS (ALS_SD = 1).
|
|
265
|
+
* @returns {Promise<void>}
|
|
266
|
+
*/
|
|
267
|
+
async alsDisable() {
|
|
268
|
+
await this._setBit(REG_ALS_CONF, ALS_SD, true);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/**
|
|
272
|
+
* Set the ALS interrupt window in counts (counts = lux / sensitivity).
|
|
273
|
+
* @param {number} low - ALS_THDL, 0–65535.
|
|
274
|
+
* @param {number} high - ALS_THDH, 0–65535.
|
|
275
|
+
* @returns {Promise<void>}
|
|
276
|
+
* @throws {RangeError} If a threshold is out of range.
|
|
277
|
+
*/
|
|
278
|
+
async setAlsThresholds(low, high) {
|
|
279
|
+
for (const v of [low, high]) {
|
|
280
|
+
if (!Number.isInteger(v) || v < 0 || v > 0xFFFF) throw new RangeError('VCNL4040: thresholds must be 0-65535');
|
|
281
|
+
}
|
|
282
|
+
await this._writeWord(REG_ALS_THDL, low);
|
|
283
|
+
await this._writeWord(REG_ALS_THDH, high);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Read the ALS interrupt window.
|
|
288
|
+
* @returns {Promise<{low: number, high: number}>} Counts.
|
|
289
|
+
*/
|
|
290
|
+
async getAlsThresholds() {
|
|
291
|
+
return { low: await this._readWord(REG_ALS_THDL), high: await this._readWord(REG_ALS_THDH) };
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Power on the PS (PS_SD = 0).
|
|
296
|
+
* @returns {Promise<void>}
|
|
297
|
+
*/
|
|
298
|
+
async psEnable() {
|
|
299
|
+
await this._setBit(REG_PS_CONF12, PS_SD, false);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
/**
|
|
303
|
+
* Shut down the PS (PS_SD = 1).
|
|
304
|
+
* @returns {Promise<void>}
|
|
305
|
+
*/
|
|
306
|
+
async psDisable() {
|
|
307
|
+
await this._setBit(REG_PS_CONF12, PS_SD, true);
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Select a 12- or 16-bit PS output (PS_HD); set it before the PS thresholds.
|
|
312
|
+
* @param {number} bits - 12 or 16.
|
|
313
|
+
* @returns {Promise<void>}
|
|
314
|
+
* @throws {RangeError} If `bits` is not 12 or 16.
|
|
315
|
+
*/
|
|
316
|
+
async setPsResolution(bits) {
|
|
317
|
+
if (bits !== 12 && bits !== 16) throw new RangeError('VCNL4040: bits must be 12 or 16');
|
|
318
|
+
await this._setBit(REG_PS_CONF12, PS_HD, bits === 16);
|
|
319
|
+
this._psBits = bits;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
/**
|
|
323
|
+
* Set the IRED on/off duty ratio to 1/ratio (PS_DUTY).
|
|
324
|
+
* @param {number} ratio - 40, 80, 160 or 320.
|
|
325
|
+
* @returns {Promise<void>}
|
|
326
|
+
* @throws {RangeError} If `ratio` is not valid.
|
|
327
|
+
*/
|
|
328
|
+
async setPsDuty(ratio) {
|
|
329
|
+
const code = _code(PS_DUTY, ratio, 'ratio must be 40, 80, 160 or 320');
|
|
330
|
+
await this._update(REG_PS_CONF12, PS_DUTY_MASK, code << 6);
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Set the PS integration time in units of T (PS_IT).
|
|
335
|
+
* @param {number} t - 1, 1.5, 2, 2.5, 3, 3.5, 4 or 8.
|
|
336
|
+
* @returns {Promise<void>}
|
|
337
|
+
* @throws {RangeError} If `t` is not valid.
|
|
338
|
+
*/
|
|
339
|
+
async setPsIntegrationTime(t) {
|
|
340
|
+
const code = _code(PS_IT, t, 't must be 1, 1.5, 2, 2.5, 3, 3.5, 4 or 8');
|
|
341
|
+
await this._update(REG_PS_CONF12, PS_IT_MASK, code << 1);
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Set how many consecutive PS hits raise the interrupt (PS_PERS).
|
|
346
|
+
* @param {number} count - 1–4.
|
|
347
|
+
* @returns {Promise<void>}
|
|
348
|
+
* @throws {RangeError} If `count` is out of range.
|
|
349
|
+
*/
|
|
350
|
+
async setPsPersistence(count) {
|
|
351
|
+
if (!Number.isInteger(count) || count < 1 || count > 4) throw new RangeError('VCNL4040: count must be 1-4');
|
|
352
|
+
await this._update(REG_PS_CONF12, PS_PERS_MASK, (count - 1) << 4);
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Set the number of IRED pulses per PS measurement (PS_MPS).
|
|
357
|
+
* @param {number} count - 1, 2, 4 or 8.
|
|
358
|
+
* @returns {Promise<void>}
|
|
359
|
+
* @throws {RangeError} If `count` is not valid.
|
|
360
|
+
*/
|
|
361
|
+
async setPsMultiPulse(count) {
|
|
362
|
+
const code = _code(PS_MPS, count, 'count must be 1, 2, 4 or 8');
|
|
363
|
+
await this._update(REG_PS_CONF3_MS, PS_MPS_MASK, code << 5);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* Set the IRED pulse current (LED_I).
|
|
368
|
+
* @param {number} milliamperes - 50, 75, 100, 120, 140, 160, 180 or 200.
|
|
369
|
+
* @returns {Promise<void>}
|
|
370
|
+
* @throws {RangeError} If `milliamperes` is not valid.
|
|
371
|
+
*/
|
|
372
|
+
async setLedCurrent(milliamperes) {
|
|
373
|
+
const code = _code(LED_MA, milliamperes, 'milliamperes must be 50, 75, 100, 120, 140, 160, 180 or 200');
|
|
374
|
+
await this._update(REG_PS_CONF3_MS, LED_I_MASK, code << 8);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Set the PS interrupt window in counts.
|
|
379
|
+
* @param {number} low - PS_THDL (away), 0–65535 (0–4095 at 12 bit).
|
|
380
|
+
* @param {number} high - PS_THDH (close), 0–65535 (0–4095 at 12 bit).
|
|
381
|
+
* @returns {Promise<void>}
|
|
382
|
+
* @throws {RangeError} If a threshold exceeds the current PS resolution.
|
|
383
|
+
*/
|
|
384
|
+
async setPsThresholds(low, high) {
|
|
385
|
+
const limit = (1 << this._psBits) - 1;
|
|
386
|
+
for (const v of [low, high]) {
|
|
387
|
+
if (!Number.isInteger(v) || v < 0 || v > limit) throw new RangeError(`VCNL4040: thresholds must be 0-${limit}`);
|
|
388
|
+
}
|
|
389
|
+
await this._writeWord(REG_PS_THDL, low);
|
|
390
|
+
await this._writeWord(REG_PS_THDH, high);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* Read the PS interrupt window.
|
|
395
|
+
* @returns {Promise<{low: number, high: number}>} Counts.
|
|
396
|
+
*/
|
|
397
|
+
async getPsThresholds() {
|
|
398
|
+
return { low: await this._readWord(REG_PS_THDL), high: await this._readWord(REG_PS_THDH) };
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
/**
|
|
402
|
+
* Set the PS cancellation level subtracted from every result (PS_CANC);
|
|
403
|
+
* calibrate it with no target in front of the sensor.
|
|
404
|
+
* @param {number} level - 0–65535 counts.
|
|
405
|
+
* @returns {Promise<void>}
|
|
406
|
+
* @throws {RangeError} If `level` is out of range.
|
|
407
|
+
*/
|
|
408
|
+
async setPsCancellation(level) {
|
|
409
|
+
if (!Number.isInteger(level) || level < 0 || level > 0xFFFF) throw new RangeError('VCNL4040: level must be 0-65535');
|
|
410
|
+
await this._writeWord(REG_PS_CANC, level);
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Enable or disable sunlight cancellation (PS_SC_EN).
|
|
415
|
+
* @param {boolean} on - true enables it.
|
|
416
|
+
* @returns {Promise<void>}
|
|
417
|
+
*/
|
|
418
|
+
async setPsSunlightCancel(on) {
|
|
419
|
+
await this._setBit(REG_PS_CONF3_MS, PS_SC_EN, on);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* Enable or disable the white channel (WHITE_EN, active low).
|
|
424
|
+
* @param {boolean} on - true enables it (writes WHITE_EN = 0).
|
|
425
|
+
* @returns {Promise<void>}
|
|
426
|
+
*/
|
|
427
|
+
async setWhiteChannel(on) {
|
|
428
|
+
await this._setBit(REG_PS_CONF3_MS, WHITE_EN, !on);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Enable or disable smart persistence (PS_SMART_PERS).
|
|
433
|
+
* @param {boolean} on - true enables it.
|
|
434
|
+
* @returns {Promise<void>}
|
|
435
|
+
*/
|
|
436
|
+
async setPsSmartPersistence(on) {
|
|
437
|
+
await this._setBit(REG_PS_CONF3_MS, PS_SMART_PERS, on);
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/**
|
|
441
|
+
* Enable or disable active-force mode (PS_AF); the PS then measures only on triggerPs().
|
|
442
|
+
* @param {boolean} on - true enables it.
|
|
443
|
+
* @returns {Promise<void>}
|
|
444
|
+
*/
|
|
445
|
+
async setPsActiveForce(on) {
|
|
446
|
+
await this._setBit(REG_PS_CONF3_MS, PS_AF, on);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Trigger one PS measurement in active-force mode: writes PS_TRIG
|
|
451
|
+
* (self-clearing), waits for the cycle and reads PS_DATA.
|
|
452
|
+
* @returns {Promise<number>} PS_DATA counts.
|
|
453
|
+
*/
|
|
454
|
+
async triggerPs() {
|
|
455
|
+
await this._setBit(REG_PS_CONF3_MS, PS_TRIG, true);
|
|
456
|
+
await _wait(TRIGGER_WAIT_MS);
|
|
457
|
+
return this._readWord(REG_PS_DATA);
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
/**
|
|
461
|
+
* Switch INT between interrupt and proximity logic output (PS_MS). In
|
|
462
|
+
* logic-output mode INT is low while a target is close and high once it is
|
|
463
|
+
* away; it cannot signal ALS interrupts then.
|
|
464
|
+
* @param {boolean} on - true selects the logic-output mode.
|
|
465
|
+
* @returns {Promise<void>}
|
|
466
|
+
*/
|
|
467
|
+
async setPsLogicOutput(on) {
|
|
468
|
+
await this._setBit(REG_PS_CONF3_MS, PS_MS, on);
|
|
469
|
+
this._logicOutput = Boolean(on);
|
|
470
|
+
}
|
|
471
|
+
|
|
472
|
+
/**
|
|
473
|
+
* Read the ID register.
|
|
474
|
+
* @returns {Promise<number>} ID word (0x0186).
|
|
475
|
+
*/
|
|
476
|
+
async deviceId() {
|
|
477
|
+
return this._readWord(REG_ID);
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/**
|
|
481
|
+
* Read and clear the interrupt flags.
|
|
482
|
+
* @returns {Promise<number>} INT_FLAG high byte (SOURCE_* bits and FLAG_PROTECTION).
|
|
483
|
+
*/
|
|
484
|
+
async status() {
|
|
485
|
+
return (await this._readWord(REG_INT_FLAG)) >> 8;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
async _setSources(source, on) {
|
|
489
|
+
if (!Number.isInteger(source) || source === 0 || (source & ~(SOURCES_ALS | SOURCES_PS))) {
|
|
490
|
+
throw new RangeError('VCNL4040: source must be an OR of SOURCE_* constants');
|
|
491
|
+
}
|
|
492
|
+
if (on && (source & SOURCES_ALS) && this._logicOutput) {
|
|
493
|
+
throw new Error('VCNL4040: ALS interrupts are unavailable in logic-output mode');
|
|
494
|
+
}
|
|
495
|
+
if (source & SOURCES_ALS) await this._setBit(REG_ALS_CONF, ALS_INT_EN, on);
|
|
496
|
+
let bits = 0;
|
|
497
|
+
if (source & SOURCE_PS_CLOSE) bits |= PS_INT_CLOSE;
|
|
498
|
+
if (source & SOURCE_PS_AWAY) bits |= PS_INT_AWAY;
|
|
499
|
+
if (bits) await this._update(REG_PS_CONF12, bits, on ? bits : 0);
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
/**
|
|
503
|
+
* Enable interrupt sources; either ALS source sets ALS_INT_EN (both), the
|
|
504
|
+
* PS sources set the PS_INT close/away bits.
|
|
505
|
+
* @param {number} source - OR of SOURCE_* constants.
|
|
506
|
+
* @returns {Promise<void>}
|
|
507
|
+
* @throws {RangeError} On unknown bits.
|
|
508
|
+
* @throws {Error} For ALS sources in logic-output mode.
|
|
509
|
+
*/
|
|
510
|
+
async enableInterrupt(source) {
|
|
511
|
+
await this._setSources(source, true);
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
/**
|
|
515
|
+
* Disable interrupt sources; either ALS source clears ALS_INT_EN (both).
|
|
516
|
+
* @param {number} source - OR of SOURCE_* constants.
|
|
517
|
+
* @returns {Promise<void>}
|
|
518
|
+
* @throws {RangeError} On unknown bits.
|
|
519
|
+
*/
|
|
520
|
+
async disableInterrupt(source) {
|
|
521
|
+
await this._setSources(source, false);
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Read and clear INT_FLAG.
|
|
526
|
+
* @returns {Promise<number>} Pending SOURCE_* / FLAG_PROTECTION bits (0 if none).
|
|
527
|
+
*/
|
|
528
|
+
async pollInterrupt() {
|
|
529
|
+
return this.status();
|
|
530
|
+
}
|
|
531
|
+
|
|
532
|
+
/**
|
|
533
|
+
* Subscribe to INT (falling edge). The callback receives the INT_FLAG byte
|
|
534
|
+
* read by pollInterrupt(); without a pin the flags are polled every 100 ms.
|
|
535
|
+
* @param {function(number): void} callback - Called with the flags.
|
|
536
|
+
* @param {{intPin?: object}} [options] - Optional pin overriding connection.intPin.
|
|
537
|
+
* @returns {Promise<void>}
|
|
538
|
+
*/
|
|
539
|
+
async onInterrupt(callback, options = {}) {
|
|
540
|
+
this._callback = callback;
|
|
541
|
+
const intPin = options.intPin ?? this._conn.intPin ?? null;
|
|
542
|
+
this._intPinUsed = intPin;
|
|
543
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
544
|
+
if (intPin) {
|
|
545
|
+
await intPin.onEdge(this._edgeHandler, 'falling');
|
|
546
|
+
} else {
|
|
547
|
+
this._pollTimer = setInterval(() => { this._service(); }, POLL_INTERVAL_MS);
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Unsubscribe and stop delivery.
|
|
553
|
+
* @returns {Promise<void>}
|
|
554
|
+
*/
|
|
555
|
+
async offInterrupt() {
|
|
556
|
+
if (this._intPinUsed) {
|
|
557
|
+
await this._intPinUsed.offEdge(this._edgeHandler);
|
|
558
|
+
this._intPinUsed = null;
|
|
559
|
+
}
|
|
560
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
561
|
+
this._callback = null;
|
|
562
|
+
}
|
|
563
|
+
|
|
564
|
+
async _service() {
|
|
565
|
+
if (this._polling) return;
|
|
566
|
+
this._polling = true;
|
|
567
|
+
try {
|
|
568
|
+
const flags = await this.pollInterrupt();
|
|
569
|
+
if (flags && this._callback) this._callback(flags);
|
|
570
|
+
} finally {
|
|
571
|
+
this._polling = false;
|
|
572
|
+
}
|
|
573
|
+
}
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
module.exports = { VCNL4040Minimal, VCNL4040Full, alsSensitivity, alsMaxRange };
|