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