periph 1.5.3 → 1.6.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/examples/color/isl29125/complete.js +57 -0
- package/examples/color/isl29125/demo.js +77 -0
- package/examples/color/isl29125/minimal.js +23 -0
- package/examples/environmental/bme688/complete.js +48 -0
- package/examples/environmental/bme688/demo.js +47 -0
- package/examples/environmental/bme688/minimal.js +21 -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/humidity/hts221/complete.js +83 -0
- package/examples/humidity/hts221/demo.js +94 -0
- package/examples/humidity/hts221/minimal.js +22 -0
- package/examples/humidity/htu31d/complete.js +50 -0
- package/examples/humidity/htu31d/demo.js +65 -0
- package/examples/humidity/htu31d/minimal.js +22 -0
- package/examples/humidity/shtc3/complete.js +48 -0
- package/examples/humidity/shtc3/demo.js +45 -0
- package/examples/humidity/shtc3/minimal.js +22 -0
- package/examples/led/saa1064/complete.js +51 -0
- package/examples/led/saa1064/demo.js +54 -0
- package/examples/led/saa1064/minimal.js +23 -0
- package/examples/light/apds9250/complete.js +71 -0
- package/examples/light/apds9250/demo.js +79 -0
- package/examples/light/apds9250/minimal.js +23 -0
- package/examples/light/bh1750/complete.js +46 -0
- package/examples/light/bh1750/demo.js +52 -0
- package/examples/light/bh1750/minimal.js +22 -0
- package/examples/light/si1132/complete.js +70 -0
- package/examples/light/si1132/demo.js +73 -0
- package/examples/light/si1132/minimal.js +22 -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/pressure/mpl3115a2/complete.js +49 -0
- package/examples/pressure/mpl3115a2/demo.js +48 -0
- package/examples/pressure/mpl3115a2/minimal.js +22 -0
- package/examples/pressure/ms5607/complete.js +49 -0
- package/examples/pressure/ms5607/demo.js +65 -0
- package/examples/pressure/ms5607/minimal.js +22 -0
- package/examples/pressure/ms5611/complete.js +49 -0
- package/examples/pressure/ms5611/demo.js +65 -0
- package/examples/pressure/ms5611/minimal.js +22 -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/package.json +1 -1
- package/src/chips/color/isl29125.js +379 -0
- package/src/chips/environmental/bme688.js +573 -0
- package/src/chips/gas/ccs811.js +400 -0
- package/src/chips/gas/rrh46410.js +534 -0
- package/src/chips/humidity/hts221.js +369 -0
- package/src/chips/humidity/htu31d.js +242 -0
- package/src/chips/humidity/shtc3.js +239 -0
- package/src/chips/led/saa1064.js +241 -0
- package/src/chips/light/apds9250.js +431 -0
- package/src/chips/light/bh1750.js +196 -0
- package/src/chips/light/si1132.js +416 -0
- package/src/chips/light/si1133.js +583 -0
- package/src/chips/light/si1145.js +630 -0
- package/src/chips/pressure/mpl3115a2.js +459 -0
- package/src/chips/pressure/ms5607.js +290 -0
- package/src/chips/pressure/ms5611.js +290 -0
- package/src/chips/pressure/spl06.js +461 -0
- package/src/chips/rfid/pn532.js +513 -0
- package/src/connection/i2c.js +46 -13
- package/src/connection/smbus.js +21 -8
- package/src/discovery/registry.js +18 -18
- package/examples/gpio/.gitkeep +0 -0
- package/src/chips/gpio/.gitkeep +0 -0
|
@@ -0,0 +1,431 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const REG_MAIN_CTRL = 0x00;
|
|
4
|
+
const REG_LS_MEAS_RATE = 0x04;
|
|
5
|
+
const REG_LS_GAIN = 0x05;
|
|
6
|
+
const REG_PART_ID = 0x06;
|
|
7
|
+
const REG_MAIN_STATUS = 0x07;
|
|
8
|
+
const REG_DATA_IR = 0x0A;
|
|
9
|
+
const REG_DATA_GREEN = 0x0D;
|
|
10
|
+
const REG_DATA_BLUE = 0x10;
|
|
11
|
+
const REG_DATA_RED = 0x13;
|
|
12
|
+
const REG_INT_CFG = 0x19;
|
|
13
|
+
const REG_INT_PERSISTENCE = 0x1A;
|
|
14
|
+
const REG_THRES_UP = 0x21;
|
|
15
|
+
const REG_THRES_LOW = 0x24;
|
|
16
|
+
const REG_THRES_VAR = 0x27;
|
|
17
|
+
|
|
18
|
+
const CTRL_SW_RESET = 0x10;
|
|
19
|
+
const CTRL_CS_MODE = 0x04;
|
|
20
|
+
const CTRL_LS_EN = 0x02;
|
|
21
|
+
|
|
22
|
+
const INT_EN = 0x04;
|
|
23
|
+
const INT_VAR_MODE = 0x08;
|
|
24
|
+
const INT_SEL_MASK = 0x30;
|
|
25
|
+
|
|
26
|
+
const STATUS_POWER_ON = 0x20;
|
|
27
|
+
const STATUS_INTERRUPT = 0x10;
|
|
28
|
+
const STATUS_DATA_READY = 0x08;
|
|
29
|
+
|
|
30
|
+
const RESOLUTION_BITS = [20, 19, 18, 17, 16, 13];
|
|
31
|
+
const INTEGRATION_S = [0.4, 0.2, 0.1, 0.05, 0.025, 0.003125];
|
|
32
|
+
const RATE_CODE = { 25: 0, 50: 1, 100: 2, 200: 3, 500: 4, 1000: 5, 2000: 6 };
|
|
33
|
+
const RATE_MS = [25, 50, 100, 200, 500, 1000, 2000, 2000];
|
|
34
|
+
const GAINS = [1, 3, 6, 9, 18];
|
|
35
|
+
const CHANNEL_CODE = { ir: 0, green: 1, red: 2, blue: 3 };
|
|
36
|
+
|
|
37
|
+
const WAIT_MARGIN_S = 0.05;
|
|
38
|
+
const POLL_STEP_MS = 5;
|
|
39
|
+
const INT_POLL_INTERVAL_MS = 50;
|
|
40
|
+
|
|
41
|
+
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
42
|
+
|
|
43
|
+
function _count(b0, b1, b2) {
|
|
44
|
+
return b0 | (b1 << 8) | ((b2 & 0x0F) << 16);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* APDS-9250 digital RGB, IR and ambient light sensor (Broadcom) — minimal interface.
|
|
49
|
+
*
|
|
50
|
+
* Four simultaneously integrating channels (red, green, blue, IR) with a 13- to
|
|
51
|
+
* 20-bit ADC, five gains and a 25 ms to 2 s measurement rate. Results are
|
|
52
|
+
* unsigned counts in 3-byte registers (LSB first). The I²C address is fixed at
|
|
53
|
+
* 0x52. Reading MAIN_STATUS clears its flags, so the driver keeps the interrupt
|
|
54
|
+
* and power-on bits it has seen until `pollInterrupt()` / `status()` hands them out.
|
|
55
|
+
*/
|
|
56
|
+
class APDS9250Minimal {
|
|
57
|
+
/**
|
|
58
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (1-byte register address).
|
|
59
|
+
*/
|
|
60
|
+
constructor(connection) {
|
|
61
|
+
this._conn = connection;
|
|
62
|
+
this._pending = 0;
|
|
63
|
+
this._integrationS = 0.1;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** Fixed 7-bit I²C address. */
|
|
67
|
+
static get I2C_ADDRESS() { return 0x52; }
|
|
68
|
+
|
|
69
|
+
async _readReg(reg) {
|
|
70
|
+
return (await this._conn.readReg(reg, 1))[0];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async _writeReg(reg, value) {
|
|
74
|
+
await this._conn.writeReg(reg, value & 0xFF);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
async _readStatus() {
|
|
78
|
+
const value = await this._readReg(REG_MAIN_STATUS);
|
|
79
|
+
this._pending |= value & (STATUS_POWER_ON | STATUS_INTERRUPT);
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async _waitData() {
|
|
84
|
+
const limitMs = (this._integrationS + WAIT_MARGIN_S) * 1000;
|
|
85
|
+
for (let waited = 0; waited <= limitMs; waited += POLL_STEP_MS) {
|
|
86
|
+
if ((await this._readStatus()) & STATUS_DATA_READY) return true;
|
|
87
|
+
if (APDS9250Minimal.pollStepMs > 0) await sleep(APDS9250Minimal.pollStepMs);
|
|
88
|
+
}
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async _readBlock() {
|
|
93
|
+
const raw = await this._conn.readReg(REG_DATA_IR, 12);
|
|
94
|
+
return {
|
|
95
|
+
red: _count(raw[9], raw[10], raw[11]),
|
|
96
|
+
green: _count(raw[3], raw[4], raw[5]),
|
|
97
|
+
blue: _count(raw[6], raw[7], raw[8]),
|
|
98
|
+
ir: _count(raw[0], raw[1], raw[2]),
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* Start the sensor: read PART_ID (the upper nibble must be 0xB), write the
|
|
104
|
+
* default configuration (18 bit, 100 ms, gain x3, interrupt off) and set
|
|
105
|
+
* LS_EN and CS_MODE.
|
|
106
|
+
* @returns {Promise<void>}
|
|
107
|
+
* @throws {Error} if PART_ID does not identify an APDS-9250.
|
|
108
|
+
*/
|
|
109
|
+
async init() {
|
|
110
|
+
const part = await this._readReg(REG_PART_ID);
|
|
111
|
+
if ((part & 0xF0) !== 0xB0) {
|
|
112
|
+
throw new Error('APDS-9250 not found (PART_ID 0x' + part.toString(16).padStart(2, '0') + ')');
|
|
113
|
+
}
|
|
114
|
+
await this._writeReg(REG_LS_MEAS_RATE, 0x22);
|
|
115
|
+
await this._writeReg(REG_LS_GAIN, 0x01);
|
|
116
|
+
await this._writeReg(REG_INT_CFG, 0x10);
|
|
117
|
+
await this._writeReg(REG_MAIN_CTRL, CTRL_LS_EN | CTRL_CS_MODE);
|
|
118
|
+
this._integrationS = 0.1;
|
|
119
|
+
if (APDS9250Minimal.startWaitMs > 0) await sleep(APDS9250Minimal.startWaitMs);
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Read all four channels from one conversion. Waits for a new result (at
|
|
124
|
+
* most one integration time plus 50 ms), then block-reads 0x0A-0x15.
|
|
125
|
+
* @returns {Promise<{red: number, green: number, blue: number, ir: number}>} Unsigned counts.
|
|
126
|
+
*/
|
|
127
|
+
async read() {
|
|
128
|
+
await this._waitData();
|
|
129
|
+
return this._readBlock();
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** Wait after enabling the sensor so the support blocks settle, in ms (tests may set it to 0). */
|
|
134
|
+
APDS9250Minimal.startWaitMs = 5;
|
|
135
|
+
/** Poll step while waiting for LS_DATA, in ms (tests may set it to 0). */
|
|
136
|
+
APDS9250Minimal.pollStepMs = POLL_STEP_MS;
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* APDS-9250 full interface — extends Minimal with single-channel reads, mode,
|
|
140
|
+
* resolution, rate and gain configuration, thresholds, variation and
|
|
141
|
+
* persistence, and interrupt delivery.
|
|
142
|
+
*/
|
|
143
|
+
class APDS9250Full extends APDS9250Minimal {
|
|
144
|
+
constructor(connection) {
|
|
145
|
+
super(connection);
|
|
146
|
+
this._callback = null;
|
|
147
|
+
this._intPinUsed = null;
|
|
148
|
+
this._pollTimer = null;
|
|
149
|
+
this._polling = false;
|
|
150
|
+
this._edgeHandler = () => { this._service(); };
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Interrupt source: selected channel outside the threshold window. */
|
|
154
|
+
static get SOURCE_THRESHOLD() { return 0x01; }
|
|
155
|
+
/** Interrupt source: selected channel changed by more than the variance threshold. */
|
|
156
|
+
static get SOURCE_VARIATION() { return 0x02; }
|
|
157
|
+
/** MAIN_STATUS bit 5: power-up event occurred. */
|
|
158
|
+
static get STATUS_POWER_ON() { return STATUS_POWER_ON; }
|
|
159
|
+
/** MAIN_STATUS bit 4: interrupt condition fulfilled. */
|
|
160
|
+
static get STATUS_INTERRUPT() { return STATUS_INTERRUPT; }
|
|
161
|
+
/** MAIN_STATUS bit 3: new data available. */
|
|
162
|
+
static get STATUS_DATA_READY() { return STATUS_DATA_READY; }
|
|
163
|
+
|
|
164
|
+
async _readChannel(reg) {
|
|
165
|
+
const raw = await this._conn.readReg(reg, 3);
|
|
166
|
+
return _count(raw[0], raw[1], raw[2]);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** @returns {Promise<number>} Red count. */
|
|
170
|
+
async readRed() { return this._readChannel(REG_DATA_RED); }
|
|
171
|
+
|
|
172
|
+
/** @returns {Promise<number>} Green / ambient-light count. */
|
|
173
|
+
async readGreen() { return this._readChannel(REG_DATA_GREEN); }
|
|
174
|
+
|
|
175
|
+
/** @returns {Promise<number>} Blue count. */
|
|
176
|
+
async readBlue() { return this._readChannel(REG_DATA_BLUE); }
|
|
177
|
+
|
|
178
|
+
/** @returns {Promise<number>} IR count. */
|
|
179
|
+
async readIr() { return this._readChannel(REG_DATA_IR); }
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Block-read all four channels without waiting (call after `dataReady()`).
|
|
183
|
+
* @returns {Promise<{red: number, green: number, blue: number, ir: number}>} Counts.
|
|
184
|
+
*/
|
|
185
|
+
async readChannels() { return this._readBlock(); }
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Report whether a new result is available. Reads MAIN_STATUS, which clears
|
|
189
|
+
* its flags; the interrupt and power-on bits are kept for `pollInterrupt()`.
|
|
190
|
+
* @returns {Promise<boolean>} True if LS_DATA was set.
|
|
191
|
+
*/
|
|
192
|
+
async dataReady() {
|
|
193
|
+
return Boolean((await this._readStatus()) & STATUS_DATA_READY);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
async _updateReg(reg, setBits, clearBits) {
|
|
197
|
+
const value = await this._readReg(reg);
|
|
198
|
+
await this._writeReg(reg, (value & ~clearBits) | setBits);
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* Select colour-sensing or ALS/IR mode.
|
|
203
|
+
* @param {boolean} colour - True for CS mode (red, green, blue, IR), false for ALS/IR (green and IR only).
|
|
204
|
+
* @returns {Promise<void>}
|
|
205
|
+
*/
|
|
206
|
+
async setMode(colour) {
|
|
207
|
+
await this._updateReg(REG_MAIN_CTRL, colour ? CTRL_CS_MODE : 0, CTRL_CS_MODE);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Activate the light sensor (LS_EN = 1). @returns {Promise<void>} */
|
|
211
|
+
async enable() { await this._updateReg(REG_MAIN_CTRL, CTRL_LS_EN, 0); }
|
|
212
|
+
|
|
213
|
+
/** Put the light sensor in standby (LS_EN = 0). @returns {Promise<void>} */
|
|
214
|
+
async disable() { await this._updateReg(REG_MAIN_CTRL, 0, CTRL_LS_EN); }
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Set the ADC resolution (and the integration time). Restarts the measurement.
|
|
218
|
+
* @param {number} bits - 13, 16, 17, 18, 19 or 20.
|
|
219
|
+
* @returns {Promise<void>}
|
|
220
|
+
*/
|
|
221
|
+
async setResolution(bits) {
|
|
222
|
+
const code = RESOLUTION_BITS.indexOf(bits);
|
|
223
|
+
if (code < 0) throw new RangeError('resolution must be 13, 16, 17, 18, 19 or 20');
|
|
224
|
+
await this._updateReg(REG_LS_MEAS_RATE, code << 4, 0x70);
|
|
225
|
+
this._integrationS = INTEGRATION_S[code];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Set the measurement rate.
|
|
230
|
+
* @param {number} milliseconds - 25, 50, 100, 200, 500, 1000 or 2000.
|
|
231
|
+
* @returns {Promise<void>}
|
|
232
|
+
*/
|
|
233
|
+
async setMeasurementRate(milliseconds) {
|
|
234
|
+
if (!(milliseconds in RATE_CODE)) throw new RangeError('rate must be 25, 50, 100, 200, 500, 1000 or 2000 ms');
|
|
235
|
+
await this._updateReg(REG_LS_MEAS_RATE, RATE_CODE[milliseconds], 0x07);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Set the analog gain for all channels. Restarts the measurement.
|
|
240
|
+
* @param {number} gain - 1, 3, 6, 9 or 18.
|
|
241
|
+
* @returns {Promise<void>}
|
|
242
|
+
*/
|
|
243
|
+
async setGain(gain) {
|
|
244
|
+
const code = GAINS.indexOf(gain);
|
|
245
|
+
if (code < 0) throw new RangeError('gain must be 1, 3, 6, 9 or 18');
|
|
246
|
+
await this._writeReg(REG_LS_GAIN, code);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/** @returns {Promise<number>} Resolution in bits (13, 16-20). */
|
|
250
|
+
async getResolution() {
|
|
251
|
+
const code = ((await this._readReg(REG_LS_MEAS_RATE)) >> 4) & 0x07;
|
|
252
|
+
return code < 6 ? RESOLUTION_BITS[code] : 18;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/** @returns {Promise<number>} Measurement rate in milliseconds. */
|
|
256
|
+
async getMeasurementRate() {
|
|
257
|
+
return RATE_MS[(await this._readReg(REG_LS_MEAS_RATE)) & 0x07];
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/** @returns {Promise<number>} Gain (1, 3, 6, 9 or 18). */
|
|
261
|
+
async getGain() {
|
|
262
|
+
const code = (await this._readReg(REG_LS_GAIN)) & 0x07;
|
|
263
|
+
return code < 5 ? GAINS[code] : 18;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/** @returns {Promise<number>} Integration time of the current resolution in seconds. */
|
|
267
|
+
async integrationTime() {
|
|
268
|
+
const code = ((await this._readReg(REG_LS_MEAS_RATE)) >> 4) & 0x07;
|
|
269
|
+
return code < 6 ? INTEGRATION_S[code] : 0.1;
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/**
|
|
273
|
+
* Convert a count to counts per gain-second: `counts / (gain * integrationTime)`.
|
|
274
|
+
* @param {number} counts - Raw channel count.
|
|
275
|
+
* @returns {Promise<number>} Normalised count.
|
|
276
|
+
*/
|
|
277
|
+
async normalized(counts) {
|
|
278
|
+
return counts / ((await this.getGain()) * (await this.integrationTime()));
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/** @returns {Promise<{part: number, revision: number}>} PART_ID nibbles (0xB, 0x5). */
|
|
282
|
+
async partId() {
|
|
283
|
+
const value = await this._readReg(REG_PART_ID);
|
|
284
|
+
return { part: value >> 4, revision: value & 0x0F };
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Read MAIN_STATUS (clears the flags on the chip).
|
|
289
|
+
* @returns {Promise<number>} Raw status byte with interrupt/power-on bits seen since the last call merged in.
|
|
290
|
+
*/
|
|
291
|
+
async status() {
|
|
292
|
+
let value = await this._readStatus();
|
|
293
|
+
value |= this._pending;
|
|
294
|
+
this._pending = 0;
|
|
295
|
+
return value;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/** Trigger a software reset (all registers to defaults, sensor in standby). @returns {Promise<void>} */
|
|
299
|
+
async softReset() {
|
|
300
|
+
await this._writeReg(REG_MAIN_CTRL, CTRL_SW_RESET);
|
|
301
|
+
if (APDS9250Minimal.startWaitMs > 0) await sleep(10);
|
|
302
|
+
this._pending = 0;
|
|
303
|
+
this._integrationS = 0.1;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Set the 20-bit threshold window.
|
|
308
|
+
* @param {number} low - Lower threshold (0-1048575).
|
|
309
|
+
* @param {number} high - Upper threshold (0-1048575).
|
|
310
|
+
* @returns {Promise<void>}
|
|
311
|
+
*/
|
|
312
|
+
async setThresholds(low, high) {
|
|
313
|
+
if (low < 0 || high < 0 || low > 0xFFFFF || high > 0xFFFFF || low > high) {
|
|
314
|
+
throw new RangeError('thresholds must satisfy 0 <= low <= high <= 1048575');
|
|
315
|
+
}
|
|
316
|
+
await this._conn.writeReg(REG_THRES_LOW, Buffer.from([low & 0xFF, (low >> 8) & 0xFF, (low >> 16) & 0x0F]));
|
|
317
|
+
await this._conn.writeReg(REG_THRES_UP, Buffer.from([high & 0xFF, (high >> 8) & 0xFF, (high >> 16) & 0x0F]));
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/** @returns {Promise<{low: number, high: number}>} Threshold window in counts. */
|
|
321
|
+
async getThresholds() {
|
|
322
|
+
const low = await this._conn.readReg(REG_THRES_LOW, 3);
|
|
323
|
+
const high = await this._conn.readReg(REG_THRES_UP, 3);
|
|
324
|
+
return { low: _count(low[0], low[1], low[2]), high: _count(high[0], high[1], high[2]) };
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/**
|
|
328
|
+
* Select the channel the interrupt monitors.
|
|
329
|
+
* @param {string} channel - 'ir', 'green', 'red' or 'blue'.
|
|
330
|
+
* @returns {Promise<void>}
|
|
331
|
+
*/
|
|
332
|
+
async setInterruptChannel(channel) {
|
|
333
|
+
if (!(channel in CHANNEL_CODE)) throw new RangeError("channel must be 'ir', 'green', 'red' or 'blue'");
|
|
334
|
+
await this._updateReg(REG_INT_CFG, CHANNEL_CODE[channel] << 4, INT_SEL_MASK);
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
/**
|
|
338
|
+
* Set the number of consecutive events before the interrupt asserts.
|
|
339
|
+
* @param {number} count - 1-16.
|
|
340
|
+
* @returns {Promise<void>}
|
|
341
|
+
*/
|
|
342
|
+
async setPersistence(count) {
|
|
343
|
+
if (count < 1 || count > 16) throw new RangeError('count must be 1-16');
|
|
344
|
+
await this._writeReg(REG_INT_PERSISTENCE, (count - 1) << 4);
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Set the variation-interrupt threshold.
|
|
349
|
+
* @param {number} counts - 8, 16, 32, ... 1024.
|
|
350
|
+
* @returns {Promise<void>}
|
|
351
|
+
*/
|
|
352
|
+
async setVariance(counts) {
|
|
353
|
+
for (let n = 0; n < 8; n++) {
|
|
354
|
+
if (counts === (8 << n)) { await this._writeReg(REG_THRES_VAR, n); return; }
|
|
355
|
+
}
|
|
356
|
+
throw new RangeError('counts must be 8, 16, 32, ... 1024');
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Select threshold or variation mode and enable the interrupt.
|
|
361
|
+
* @param {number} [source] - `SOURCE_THRESHOLD` (default) or `SOURCE_VARIATION`.
|
|
362
|
+
* @returns {Promise<void>}
|
|
363
|
+
*/
|
|
364
|
+
async enableInterrupt(source = APDS9250Full.SOURCE_THRESHOLD) {
|
|
365
|
+
const mode = source === APDS9250Full.SOURCE_VARIATION ? INT_VAR_MODE : 0;
|
|
366
|
+
await this._updateReg(REG_INT_CFG, INT_EN | mode, INT_EN | INT_VAR_MODE);
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Disable the interrupt (clears LS_INT_EN; both sources share the bit).
|
|
371
|
+
* @returns {Promise<void>}
|
|
372
|
+
*/
|
|
373
|
+
async disableInterrupt() { await this._updateReg(REG_INT_CFG, 0, INT_EN); }
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Read MAIN_STATUS (clearing the flags) and return it.
|
|
377
|
+
* @returns {Promise<number>} Status byte; `STATUS_INTERRUPT` is set if the condition was fulfilled.
|
|
378
|
+
*/
|
|
379
|
+
async pollInterrupt() { return this.status(); }
|
|
380
|
+
|
|
381
|
+
async _arm(intPin) {
|
|
382
|
+
this._intPinUsed = intPin;
|
|
383
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
384
|
+
if (intPin) {
|
|
385
|
+
await intPin.onEdge(this._edgeHandler, 'falling');
|
|
386
|
+
} else {
|
|
387
|
+
this._pollTimer = setInterval(() => { this._service(); }, INT_POLL_INTERVAL_MS);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/**
|
|
392
|
+
* Subscribe to INT assertions. `callback(status)` is called with the
|
|
393
|
+
* MAIN_STATUS byte after the driver has read (and thereby cleared) it.
|
|
394
|
+
* Delivery is via the INT line (options.intPin or connection.intPin,
|
|
395
|
+
* falling edge) or a 50 ms polling timer.
|
|
396
|
+
* @param {function} callback - Called with (status).
|
|
397
|
+
* @param {object} [options]
|
|
398
|
+
* @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
|
|
399
|
+
* @returns {Promise<void>}
|
|
400
|
+
*/
|
|
401
|
+
async onInterrupt(callback, options = {}) {
|
|
402
|
+
this._callback = callback;
|
|
403
|
+
await this._arm(options.intPin ?? this._conn.intPin ?? null);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Unsubscribe and stop delivery.
|
|
408
|
+
* @returns {Promise<void>}
|
|
409
|
+
*/
|
|
410
|
+
async offInterrupt() {
|
|
411
|
+
if (this._intPinUsed) {
|
|
412
|
+
await this._intPinUsed.offEdge(this._edgeHandler);
|
|
413
|
+
this._intPinUsed = null;
|
|
414
|
+
}
|
|
415
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
416
|
+
this._callback = null;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
async _service() {
|
|
420
|
+
if (this._polling) return;
|
|
421
|
+
this._polling = true;
|
|
422
|
+
try {
|
|
423
|
+
const status = await this.pollInterrupt();
|
|
424
|
+
if ((status & STATUS_INTERRUPT) && this._callback) this._callback(status);
|
|
425
|
+
} finally {
|
|
426
|
+
this._polling = false;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
module.exports = { APDS9250Minimal, APDS9250Full };
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _OP_POWER_DOWN = 0x00;
|
|
4
|
+
const _OP_POWER_ON = 0x01;
|
|
5
|
+
const _OP_RESET = 0x07;
|
|
6
|
+
const _OP_CONTINUOUS = [0x10, 0x11, 0x13];
|
|
7
|
+
const _OP_ONE_TIME = [0x20, 0x21, 0x23];
|
|
8
|
+
const _OP_MT_HIGH = 0x40;
|
|
9
|
+
const _OP_MT_LOW = 0x60;
|
|
10
|
+
const _DEFAULT_MTREG = 69;
|
|
11
|
+
// maximum measurement time in milliseconds at MTreg = 69, indexed by mode
|
|
12
|
+
const _T_MAX_MS = [180, 180, 24];
|
|
13
|
+
|
|
14
|
+
const MODE_H = 0;
|
|
15
|
+
const MODE_H2 = 1;
|
|
16
|
+
const MODE_L = 2;
|
|
17
|
+
|
|
18
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
19
|
+
|
|
20
|
+
function _checkMode(mode) {
|
|
21
|
+
if (mode !== MODE_H && mode !== MODE_H2 && mode !== MODE_L) {
|
|
22
|
+
throw new RangeError('BH1750 mode must be MODE_H, MODE_H2 or MODE_L');
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* BH1750FVI ambient light sensor — minimal interface.
|
|
28
|
+
*
|
|
29
|
+
* Each `read()` starts one one-time H-resolution measurement (opcode 0x20, 1 lx steps), waits
|
|
30
|
+
* the datasheet maximum of 180 ms, reads the 16-bit result and returns the illuminance in lux
|
|
31
|
+
* (raw / 1.2). The chip powers down by itself after the measurement.
|
|
32
|
+
*
|
|
33
|
+
* The device is command driven (no register pointer), so a plain I²C connection is used.
|
|
34
|
+
* The constructor does not touch the bus; `init()` powers the chip on.
|
|
35
|
+
*/
|
|
36
|
+
class BH1750Minimal {
|
|
37
|
+
/**
|
|
38
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (address 0x23 or 0x5C).
|
|
39
|
+
*/
|
|
40
|
+
constructor(connection) {
|
|
41
|
+
this._conn = connection;
|
|
42
|
+
this._mtreg = _DEFAULT_MTREG;
|
|
43
|
+
this._mode = MODE_H;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Power the chip on (opcode 0x01). */
|
|
47
|
+
async init() {
|
|
48
|
+
await this._conn.write(Buffer.from([_OP_POWER_ON]));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
_waitMs(mode) {
|
|
52
|
+
return _T_MAX_MS[mode] * this._mtreg / _DEFAULT_MTREG;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
_toLux(raw, mode) {
|
|
56
|
+
const lux = raw / 1.2 * (_DEFAULT_MTREG / this._mtreg);
|
|
57
|
+
return mode === MODE_H2 ? lux / 2.0 : lux;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async _readWord() {
|
|
61
|
+
const data = Buffer.from(await this._conn.read(2));
|
|
62
|
+
return (data[0] << 8) | data[1];
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
async _measureRaw(mode) {
|
|
66
|
+
await this._conn.write(Buffer.from([_OP_ONE_TIME[mode]]));
|
|
67
|
+
await _sleep(this._waitMs(mode));
|
|
68
|
+
return this._readWord();
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Measure the illuminance once.
|
|
73
|
+
* @returns {Promise<number>} Illuminance in lux (one-time measurement in the current mode, H-resolution by default).
|
|
74
|
+
*/
|
|
75
|
+
async read() {
|
|
76
|
+
return this._toLux(await this._measureRaw(this._mode), this._mode);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* BH1750FVI full interface — all measurement modes, continuous mode, sensitivity, power control.
|
|
82
|
+
*
|
|
83
|
+
* Extends BH1750Minimal with raw codes, one-time measurements in each mode, continuous
|
|
84
|
+
* measurement, power-down/power-on, reset, the measurement-time register (MTreg) and sensitivity
|
|
85
|
+
* adjustment. `read()` uses the mode of the last `measureOnce()` or `startContinuous()` call
|
|
86
|
+
* (H-resolution by default).
|
|
87
|
+
*/
|
|
88
|
+
class BH1750Full extends BH1750Minimal {
|
|
89
|
+
/**
|
|
90
|
+
* Run one one-time measurement in the current mode and return the raw count.
|
|
91
|
+
* @returns {Promise<number>} 16-bit unsigned raw count.
|
|
92
|
+
*/
|
|
93
|
+
async readRaw() {
|
|
94
|
+
return this._measureRaw(this._mode);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Run one one-time measurement.
|
|
99
|
+
* @param {number} [mode=MODE_H] - MODE_H (1 lx), MODE_H2 (0.5 lx) or MODE_L (4 lx).
|
|
100
|
+
* @returns {Promise<number>} Illuminance in lux.
|
|
101
|
+
*/
|
|
102
|
+
async measureOnce(mode = MODE_H) {
|
|
103
|
+
_checkMode(mode);
|
|
104
|
+
this._mode = mode;
|
|
105
|
+
return this._toLux(await this._measureRaw(mode), mode);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Start continuous measurements; the first result is ready after `measurementTime(mode)`.
|
|
110
|
+
* @param {number} [mode=MODE_H] - MODE_H (1 lx), MODE_H2 (0.5 lx) or MODE_L (4 lx).
|
|
111
|
+
*/
|
|
112
|
+
async startContinuous(mode = MODE_H) {
|
|
113
|
+
_checkMode(mode);
|
|
114
|
+
this._mode = mode;
|
|
115
|
+
await this._conn.write(Buffer.from([_OP_CONTINUOUS[mode]]));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Read the latest continuous-mode result without waiting.
|
|
120
|
+
* @returns {Promise<number>} Illuminance in lux.
|
|
121
|
+
*/
|
|
122
|
+
async readContinuous() {
|
|
123
|
+
return this._toLux(await this._readWord(), this._mode);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/** Enter power-down (opcode 0x00). */
|
|
127
|
+
async powerDown() {
|
|
128
|
+
await this._conn.write(Buffer.from([_OP_POWER_DOWN]));
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Leave power-down and wait for a measurement command (opcode 0x01). */
|
|
132
|
+
async powerOn() {
|
|
133
|
+
await this._conn.write(Buffer.from([_OP_POWER_ON]));
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Clear the illuminance data register (opcode 0x07; powers on first, as reset is ignored in power-down). */
|
|
137
|
+
async reset() {
|
|
138
|
+
await this._conn.write(Buffer.from([_OP_POWER_ON]));
|
|
139
|
+
await this._conn.write(Buffer.from([_OP_RESET]));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Write the measurement-time register (two opcodes, high bits then low bits).
|
|
144
|
+
* @param {number} mtreg - Register value, 31-254 (default 69).
|
|
145
|
+
* @throws {RangeError} If mtreg is outside 31-254.
|
|
146
|
+
*/
|
|
147
|
+
async setMeasurementTime(mtreg) {
|
|
148
|
+
if (!Number.isInteger(mtreg) || mtreg < 31 || mtreg > 254) throw new RangeError('BH1750 MTreg must be 31-254');
|
|
149
|
+
await this._conn.write(Buffer.from([_OP_MT_HIGH | (mtreg >> 5)]));
|
|
150
|
+
await this._conn.write(Buffer.from([_OP_MT_LOW | (mtreg & 0x1F)]));
|
|
151
|
+
this._mtreg = mtreg;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/**
|
|
155
|
+
* The last written MTreg (the chip has no read-back; 69 after init).
|
|
156
|
+
* @returns {number}
|
|
157
|
+
*/
|
|
158
|
+
getMeasurementTimeRegister() {
|
|
159
|
+
return this._mtreg;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Set the sensitivity relative to the default (MTreg = round(69 * factor)).
|
|
164
|
+
* @param {number} factor - 0.45-3.68.
|
|
165
|
+
* @throws {RangeError} If the resulting MTreg is outside 31-254.
|
|
166
|
+
*/
|
|
167
|
+
async setSensitivity(factor) {
|
|
168
|
+
await this.setMeasurementTime(Math.round(_DEFAULT_MTREG * factor));
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Maximum measurement time of a mode at the current MTreg.
|
|
173
|
+
* @param {number} [mode=MODE_H]
|
|
174
|
+
* @returns {number} Seconds.
|
|
175
|
+
*/
|
|
176
|
+
measurementTime(mode = MODE_H) {
|
|
177
|
+
_checkMode(mode);
|
|
178
|
+
return this._waitMs(mode) / 1000.0;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
/**
|
|
182
|
+
* Illuminance represented by one raw count at the current MTreg.
|
|
183
|
+
* @param {number} [mode=MODE_H]
|
|
184
|
+
* @returns {number} Lux per count.
|
|
185
|
+
*/
|
|
186
|
+
luxPerCount(mode = MODE_H) {
|
|
187
|
+
_checkMode(mode);
|
|
188
|
+
return this._toLux(1, mode);
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
BH1750Minimal.MODE_H = MODE_H;
|
|
193
|
+
BH1750Minimal.MODE_H2 = MODE_H2;
|
|
194
|
+
BH1750Minimal.MODE_L = MODE_L;
|
|
195
|
+
|
|
196
|
+
module.exports = { BH1750Minimal, BH1750Full, MODE_H, MODE_H2, MODE_L };
|