periph 1.4.0 → 1.5.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 +27 -0
- package/examples/adc_dac/ad5251/complete.js +66 -0
- package/examples/adc_dac/ad5251/demo.js +69 -0
- package/examples/adc_dac/ad5251/minimal.js +23 -0
- package/examples/adc_dac/ad5252/complete.js +66 -0
- package/examples/adc_dac/ad5252/demo.js +69 -0
- package/examples/adc_dac/ad5252/minimal.js +23 -0
- package/examples/adc_dac/ads7828/complete.js +46 -0
- package/examples/adc_dac/ads7828/demo.js +45 -0
- package/examples/adc_dac/ads7828/minimal.js +17 -0
- package/examples/adc_dac/cat5171/complete.js +52 -0
- package/examples/adc_dac/cat5171/demo.js +59 -0
- package/examples/adc_dac/cat5171/minimal.js +23 -0
- package/examples/adc_dac/cs43l22/complete.js +88 -0
- package/examples/adc_dac/cs43l22/demo.js +56 -0
- package/examples/adc_dac/cs43l22/minimal.js +20 -0
- package/examples/adc_dac/ds1881/complete.js +62 -0
- package/examples/adc_dac/ds1881/demo.js +61 -0
- package/examples/adc_dac/ds1881/minimal.js +25 -0
- package/examples/adc_dac/ds3502/complete.js +46 -0
- package/examples/adc_dac/ds3502/demo.js +54 -0
- package/examples/adc_dac/ds3502/minimal.js +28 -0
- package/examples/adc_dac/mcp3422/complete.js +48 -0
- package/examples/adc_dac/mcp3422/demo.js +49 -0
- package/examples/adc_dac/mcp3422/minimal.js +25 -0
- package/examples/display/sh1106/complete.js +101 -0
- package/examples/display/sh1106/demo.js +68 -0
- package/examples/display/sh1106/minimal.js +25 -0
- package/examples/display/ssd1305/complete.js +129 -0
- package/examples/display/ssd1305/demo.js +71 -0
- package/examples/display/ssd1305/minimal.js +25 -0
- package/examples/display/ssd1306/complete.js +115 -0
- package/examples/display/ssd1306/demo.js +70 -0
- package/examples/display/ssd1306/minimal.js +25 -0
- package/examples/environmental/am2315/complete.js +34 -0
- package/examples/environmental/am2315/demo.js +46 -0
- package/examples/environmental/am2315/minimal.js +21 -0
- package/examples/humidity/htu21df/complete.js +49 -0
- package/examples/humidity/htu21df/demo.js +50 -0
- package/examples/humidity/htu21df/minimal.js +22 -0
- package/examples/humidity/sht31/complete.js +53 -0
- package/examples/humidity/sht31/demo.js +53 -0
- package/examples/humidity/sht31/minimal.js +21 -0
- package/examples/humidity/si7021/complete.js +55 -0
- package/examples/humidity/si7021/demo.js +58 -0
- package/examples/humidity/si7021/minimal.js +21 -0
- package/examples/imu/bno055/complete.js +85 -0
- package/examples/imu/bno055/demo.js +70 -0
- package/examples/imu/bno055/minimal.js +20 -0
- package/examples/io_expander/pca6408a/complete.js +63 -0
- package/examples/io_expander/pca6408a/demo.js +58 -0
- package/examples/io_expander/pca6408a/minimal.js +20 -0
- package/examples/io_expander/pca6416a/complete.js +68 -0
- package/examples/io_expander/pca6416a/demo.js +60 -0
- package/examples/io_expander/pca6416a/minimal.js +20 -0
- package/examples/io_expander/pca9536/complete.js +63 -0
- package/examples/io_expander/pca9536/demo.js +57 -0
- package/examples/io_expander/pca9536/minimal.js +20 -0
- package/examples/io_expander/pca9539/complete.js +68 -0
- package/examples/io_expander/pca9539/demo.js +60 -0
- package/examples/io_expander/pca9539/minimal.js +20 -0
- package/examples/light/max44009/complete.js +56 -0
- package/examples/light/max44009/demo.js +49 -0
- package/examples/light/max44009/minimal.js +22 -0
- package/examples/memory/at24c02n/complete.js +39 -0
- package/examples/memory/at24c02n/demo.js +39 -0
- package/examples/memory/at24c02n/minimal.js +15 -0
- package/examples/memory/at24c64/complete.js +39 -0
- package/examples/memory/at24c64/demo.js +39 -0
- package/examples/memory/at24c64/minimal.js +15 -0
- package/examples/memory/cat24c512/complete.js +39 -0
- package/examples/memory/cat24c512/demo.js +39 -0
- package/examples/memory/cat24c512/minimal.js +15 -0
- package/examples/other/pca9541/complete.js +52 -0
- package/examples/other/pca9541/demo.js +61 -0
- package/examples/other/pca9541/minimal.js +23 -0
- package/examples/temperature/tmp102/complete.js +74 -0
- package/examples/temperature/tmp102/demo.js +71 -0
- package/examples/temperature/tmp102/minimal.js +20 -0
- package/examples/tof/vl6180x/complete.js +62 -0
- package/examples/tof/vl6180x/demo.js +77 -0
- package/examples/tof/vl6180x/minimal.js +23 -0
- package/package.json +1 -1
- package/src/chips/adc_dac/ad5251.js +345 -0
- package/src/chips/adc_dac/ad5252.js +345 -0
- package/src/chips/adc_dac/ads7828.js +225 -0
- package/src/chips/adc_dac/cat5171.js +213 -0
- package/src/chips/adc_dac/cs43l22.js +645 -0
- package/src/chips/adc_dac/ds1881.js +357 -0
- package/src/chips/adc_dac/ds3502.js +241 -0
- package/src/chips/adc_dac/mcp3422.js +312 -0
- package/src/chips/display/mono_oled.js +182 -0
- package/src/chips/display/sh1106.js +496 -0
- package/src/chips/display/ssd1305.js +655 -0
- package/src/chips/display/ssd1306.js +458 -0
- package/src/chips/environmental/am2315.js +237 -0
- package/src/chips/humidity/htu21df.js +222 -0
- package/src/chips/humidity/sht31.js +284 -0
- package/src/chips/humidity/si7021.js +282 -0
- package/src/chips/imu/bno055.js +496 -0
- package/src/chips/io_expander/pca6408a.js +407 -0
- package/src/chips/io_expander/pca6416a.js +455 -0
- package/src/chips/io_expander/pca9536.js +398 -0
- package/src/chips/io_expander/pca9539.js +457 -0
- package/src/chips/light/max44009.js +336 -0
- package/src/chips/memory/at24c02n.js +220 -0
- package/src/chips/memory/at24c64.js +220 -0
- package/src/chips/memory/cat24c512.js +221 -0
- package/src/chips/other/pca9541.js +315 -0
- package/src/chips/temperature/tmp102.js +361 -0
- package/src/chips/tof/vl6180x.js +580 -0
- package/src/discovery/registry.js +65 -13
|
@@ -0,0 +1,580 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* VL6180X — proximity and ambient-light sensor (STMicroelectronics).
|
|
5
|
+
*
|
|
6
|
+
* VCSEL emitter, SPAD time-of-flight receiver and ambient light sensor (ALS)
|
|
7
|
+
* behind one aperture. Measures absolute distance 0-100 mm (8-bit result in
|
|
8
|
+
* mm) and ambient light (16-bit count, converted to lux). Registers use a
|
|
9
|
+
* 16-bit big-endian index; 16-bit results and thresholds are MSB first.
|
|
10
|
+
*
|
|
11
|
+
* The connection must be created with `regBytes` = 2 and point at address
|
|
12
|
+
* 0x29. The mandatory private-register settings applied by `init()` come from
|
|
13
|
+
* ST application note AN4545 and are not documented in the datasheet.
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const _REG_MODEL_ID = 0x000;
|
|
17
|
+
const _REG_MODEL_REV_MAJOR = 0x001;
|
|
18
|
+
const _REG_MODEL_REV_MINOR = 0x002;
|
|
19
|
+
const _REG_MODE_GPIO1 = 0x011;
|
|
20
|
+
const _REG_INTERRUPT_CONFIG = 0x014;
|
|
21
|
+
const _REG_INTERRUPT_CLEAR = 0x015;
|
|
22
|
+
const _REG_FRESH_OUT_OF_RESET = 0x016;
|
|
23
|
+
const _REG_GROUPED_PARAMETER_HOLD = 0x017;
|
|
24
|
+
const _REG_RANGE_START = 0x018;
|
|
25
|
+
const _REG_RANGE_THRESH_HIGH = 0x019;
|
|
26
|
+
const _REG_RANGE_THRESH_LOW = 0x01A;
|
|
27
|
+
const _REG_RANGE_INTERMEASUREMENT = 0x01B;
|
|
28
|
+
const _REG_RANGE_MAX_CONVERGENCE = 0x01C;
|
|
29
|
+
const _REG_RANGE_CROSSTALK_RATE = 0x01E;
|
|
30
|
+
const _REG_RANGE_OFFSET = 0x024;
|
|
31
|
+
const _REG_VHV_RECALIBRATE = 0x02E;
|
|
32
|
+
const _REG_VHV_REPEAT_RATE = 0x031;
|
|
33
|
+
const _REG_ALS_START = 0x038;
|
|
34
|
+
const _REG_ALS_THRESH_HIGH = 0x03A;
|
|
35
|
+
const _REG_ALS_THRESH_LOW = 0x03C;
|
|
36
|
+
const _REG_ALS_INTERMEASUREMENT = 0x03E;
|
|
37
|
+
const _REG_ALS_GAIN = 0x03F;
|
|
38
|
+
const _REG_ALS_INTEGRATION = 0x040;
|
|
39
|
+
const _REG_RANGE_STATUS = 0x04D;
|
|
40
|
+
const _REG_INTERRUPT_STATUS = 0x04F;
|
|
41
|
+
const _REG_ALS_VAL = 0x050;
|
|
42
|
+
const _REG_HISTORY_BUFFER = 0x052;
|
|
43
|
+
const _REG_RANGE_VAL = 0x062;
|
|
44
|
+
const _REG_AVERAGING_PERIOD = 0x10A;
|
|
45
|
+
const _REG_RESULT_SCALER = 0x120;
|
|
46
|
+
const _REG_I2C_ADDRESS = 0x212;
|
|
47
|
+
const _REG_INTERLEAVED_ENABLE = 0x2A3;
|
|
48
|
+
|
|
49
|
+
// Mandatory private-register settings (ST AN4545), applied while
|
|
50
|
+
// SYSTEM__FRESH_OUT_OF_RESET = 1.
|
|
51
|
+
const _PRIVATE_SETTINGS = [
|
|
52
|
+
[0x0207, 0x01], [0x0208, 0x01], [0x0096, 0x00], [0x0097, 0xFD],
|
|
53
|
+
[0x00E3, 0x01], [0x00E4, 0x03], [0x00E5, 0x02], [0x00E6, 0x01],
|
|
54
|
+
[0x00E7, 0x03], [0x00F5, 0x02], [0x00D9, 0x05], [0x00DB, 0xCE],
|
|
55
|
+
[0x00DC, 0x03], [0x00DD, 0xF8], [0x009F, 0x00], [0x00A3, 0x3C],
|
|
56
|
+
[0x00B7, 0x00], [0x00BB, 0x3C], [0x00B2, 0x09], [0x00CA, 0x09],
|
|
57
|
+
[0x0198, 0x01], [0x01B0, 0x17], [0x01AD, 0x00], [0x00FF, 0x05],
|
|
58
|
+
[0x0100, 0x05], [0x0199, 0x05], [0x01A6, 0x1B], [0x01AC, 0x3E],
|
|
59
|
+
[0x01A7, 0x1F], [0x0030, 0x00],
|
|
60
|
+
];
|
|
61
|
+
|
|
62
|
+
// Recommended public settings (datasheet / AN4545).
|
|
63
|
+
const _RECOMMENDED_SETTINGS = [
|
|
64
|
+
[_REG_MODE_GPIO1, 0x10], [_REG_AVERAGING_PERIOD, 0x30], [_REG_ALS_GAIN, 0x46],
|
|
65
|
+
[_REG_VHV_REPEAT_RATE, 0xFF], [_REG_VHV_RECALIBRATE, 0x01],
|
|
66
|
+
[_REG_RANGE_INTERMEASUREMENT, 0x09], [_REG_ALS_INTERMEASUREMENT, 0x31],
|
|
67
|
+
[_REG_INTERRUPT_CONFIG, 0x24],
|
|
68
|
+
];
|
|
69
|
+
|
|
70
|
+
// Actual ALS gain per gain code 0-7.
|
|
71
|
+
const _GAIN = [20.0, 10.32, 5.21, 2.60, 1.72, 1.28, 1.01, 40.0];
|
|
72
|
+
|
|
73
|
+
const _RANGE_TIMEOUT_MS = 100;
|
|
74
|
+
const _VHV_TIMEOUT_MS = 100;
|
|
75
|
+
|
|
76
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* VL6180X proximity sensor — minimal interface.
|
|
80
|
+
*
|
|
81
|
+
* `init()` waits 1 ms for boot, then — only while SYSTEM__FRESH_OUT_OF_RESET
|
|
82
|
+
* is 1 — applies the mandatory private settings, then the recommended
|
|
83
|
+
* settings (averaging period 48, max convergence 49 ms, ALS gain 1, 100 ms
|
|
84
|
+
* ALS integration, new-sample interrupt configuration). `readRange()` then
|
|
85
|
+
* takes one single-shot measurement per call.
|
|
86
|
+
*/
|
|
87
|
+
class VL6180XMinimal {
|
|
88
|
+
/**
|
|
89
|
+
* @param {object} connection - I2C RegisterConnection (regBytes = 2) at 0x29.
|
|
90
|
+
*/
|
|
91
|
+
constructor(connection) {
|
|
92
|
+
this._conn = connection;
|
|
93
|
+
this._gainCode = 6;
|
|
94
|
+
this._integrationMs = 100;
|
|
95
|
+
this._callback = null;
|
|
96
|
+
this._edgeHandler = null;
|
|
97
|
+
this._pollTimer = null;
|
|
98
|
+
this._busy = Promise.resolve();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async _rd8(reg) {
|
|
102
|
+
return (await this._conn.readReg(reg, 1))[0];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async _wr8(reg, value) {
|
|
106
|
+
await this._conn.writeReg(reg, Buffer.from([value & 0xFF]));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async _rd16(reg) {
|
|
110
|
+
const d = await this._conn.readReg(reg, 2);
|
|
111
|
+
return (d[0] << 8) | d[1];
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async _wr16(reg, value) {
|
|
115
|
+
await this._conn.writeReg(reg, Buffer.from([(value >> 8) & 0xFF, value & 0xFF]));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
// Serialises multi-register sequences against the interrupt poller.
|
|
119
|
+
_locked(fn) {
|
|
120
|
+
const run = this._busy.then(fn);
|
|
121
|
+
this._busy = run.catch(() => {});
|
|
122
|
+
return run;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
async _waitSample(shift, timeoutMs) {
|
|
126
|
+
const start = Date.now();
|
|
127
|
+
for (;;) {
|
|
128
|
+
if ((((await this._rd8(_REG_INTERRUPT_STATUS)) >> shift) & 0x07) === 4) return true;
|
|
129
|
+
if (Date.now() - start > timeoutMs) return false;
|
|
130
|
+
await _sleep(1);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Run the initialization sequence (1 ms boot wait, private settings while
|
|
136
|
+
* fresh out of reset, recommended settings).
|
|
137
|
+
* @returns {Promise<void>}
|
|
138
|
+
*/
|
|
139
|
+
async init() {
|
|
140
|
+
await _sleep(1);
|
|
141
|
+
if ((await this._rd8(_REG_FRESH_OUT_OF_RESET)) === 1) {
|
|
142
|
+
for (const [reg, value] of _PRIVATE_SETTINGS) await this._wr8(reg, value);
|
|
143
|
+
await this._wr8(_REG_FRESH_OUT_OF_RESET, 0);
|
|
144
|
+
}
|
|
145
|
+
for (const [reg, value] of _RECOMMENDED_SETTINGS) await this._wr8(reg, value);
|
|
146
|
+
await this._wr16(_REG_ALS_INTEGRATION, 0x0063);
|
|
147
|
+
this._gainCode = 6;
|
|
148
|
+
this._integrationMs = 100;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async _readRangeResult() {
|
|
152
|
+
const error = (await this._rd8(_REG_RANGE_STATUS)) >> 4;
|
|
153
|
+
const mm = await this._rd8(_REG_RANGE_VAL);
|
|
154
|
+
await this._wr8(_REG_INTERRUPT_CLEAR, 0x07);
|
|
155
|
+
return { mm, error };
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async _measureRange() {
|
|
159
|
+
await this._wr8(_REG_RANGE_START, 0x01);
|
|
160
|
+
if (!(await this._waitSample(0, _RANGE_TIMEOUT_MS))) {
|
|
161
|
+
await this._wr8(_REG_INTERRUPT_CLEAR, 0x07);
|
|
162
|
+
throw new Error('VL6180X timeout waiting for range result');
|
|
163
|
+
}
|
|
164
|
+
return this._readRangeResult();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Take one single-shot range measurement.
|
|
169
|
+
* @returns {Promise<number>} Distance in mm (0-255; reliable up to about 100 mm).
|
|
170
|
+
* @throws {Error} On a non-zero range error code or if no result arrives within 100 ms.
|
|
171
|
+
*/
|
|
172
|
+
async readRange() {
|
|
173
|
+
const { mm, error } = await this._locked(() => this._measureRange());
|
|
174
|
+
if (error !== 0) throw new Error(`VL6180X range error ${error}`);
|
|
175
|
+
return mm;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async _pollInterruptStatus() {
|
|
179
|
+
return this._locked(async () => {
|
|
180
|
+
const status = await this._rd8(_REG_INTERRUPT_STATUS);
|
|
181
|
+
let sources = 0;
|
|
182
|
+
if (status & 0x07) sources |= VL6180XMinimal.SOURCE_RANGE;
|
|
183
|
+
if (status & 0x38) sources |= VL6180XMinimal.SOURCE_ALS;
|
|
184
|
+
if (status & 0xC0) sources |= VL6180XMinimal.SOURCE_ERROR;
|
|
185
|
+
if (sources) await this._wr8(_REG_INTERRUPT_CLEAR, 0x07);
|
|
186
|
+
return sources;
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** Default 7-bit I2C address. */
|
|
192
|
+
VL6180XMinimal.I2C_ADDRESS = 0x29;
|
|
193
|
+
/** Expected model ID. */
|
|
194
|
+
VL6180XMinimal.MODEL_ID = 0xB4;
|
|
195
|
+
/** Interrupt source: range event. */
|
|
196
|
+
VL6180XMinimal.SOURCE_RANGE = 0x01;
|
|
197
|
+
/** Interrupt source: ALS event. */
|
|
198
|
+
VL6180XMinimal.SOURCE_ALS = 0x02;
|
|
199
|
+
/** Interrupt source: error event. */
|
|
200
|
+
VL6180XMinimal.SOURCE_ERROR = 0x04;
|
|
201
|
+
/** Interrupt mode: value below the low threshold. */
|
|
202
|
+
VL6180XMinimal.MODE_LEVEL_LOW = 1;
|
|
203
|
+
/** Interrupt mode: value above the high threshold. */
|
|
204
|
+
VL6180XMinimal.MODE_LEVEL_HIGH = 2;
|
|
205
|
+
/** Interrupt mode: value outside the threshold window. */
|
|
206
|
+
VL6180XMinimal.MODE_OUT_OF_WINDOW = 3;
|
|
207
|
+
/** Interrupt mode: new sample ready (default). */
|
|
208
|
+
VL6180XMinimal.MODE_NEW_SAMPLE_READY = 4;
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* VL6180X full interface — extends Minimal with ALS (lux), continuous and
|
|
212
|
+
* interleaved modes, thresholds, gain / integration / scaler, convergence and
|
|
213
|
+
* averaging, range offset and crosstalk, VHV recalibration, address change,
|
|
214
|
+
* identification, the history buffer and the Level-2 interrupt API.
|
|
215
|
+
*/
|
|
216
|
+
class VL6180XFull extends VL6180XMinimal {
|
|
217
|
+
/**
|
|
218
|
+
* Take one single-shot range measurement without throwing on range errors.
|
|
219
|
+
* @returns {Promise<{mm: number, error: number}>} Range in mm and the error code (0 = valid).
|
|
220
|
+
* @throws {Error} If no result arrives within 100 ms.
|
|
221
|
+
*/
|
|
222
|
+
async readRangeStatus() {
|
|
223
|
+
return this._locked(() => this._measureRange());
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Start continuous ranging.
|
|
228
|
+
* @param {number} periodMs - Period in ms, 10-2550 in 10 ms steps.
|
|
229
|
+
* @throws {RangeError} If out of range.
|
|
230
|
+
*/
|
|
231
|
+
async startContinuousRange(periodMs) {
|
|
232
|
+
if (periodMs < 10 || periodMs > 2550) throw new RangeError('period must be 10 to 2550 ms');
|
|
233
|
+
await this._locked(async () => {
|
|
234
|
+
await this._wr8(_REG_RANGE_INTERMEASUREMENT, Math.floor(periodMs / 10) - 1);
|
|
235
|
+
await this._wr8(_REG_INTERRUPT_CLEAR, 0x07);
|
|
236
|
+
await this._wr8(_REG_RANGE_START, 0x03);
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** Stop continuous ranging (the start/stop bit toggles the running mode). */
|
|
241
|
+
async stopContinuousRange() {
|
|
242
|
+
await this._wr8(_REG_RANGE_START, 0x01);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Wait for the next continuous-mode range sample and read it.
|
|
247
|
+
* @returns {Promise<number>} Distance in mm.
|
|
248
|
+
* @throws {Error} On a non-zero range error code or a timeout.
|
|
249
|
+
*/
|
|
250
|
+
async readRangeContinuous() {
|
|
251
|
+
const { mm, error } = await this._locked(async () => {
|
|
252
|
+
const period = ((await this._rd8(_REG_RANGE_INTERMEASUREMENT)) + 1) * 10;
|
|
253
|
+
if (!(await this._waitSample(0, _RANGE_TIMEOUT_MS + period))) {
|
|
254
|
+
throw new Error('VL6180X timeout waiting for range sample');
|
|
255
|
+
}
|
|
256
|
+
return this._readRangeResult();
|
|
257
|
+
});
|
|
258
|
+
if (error !== 0) throw new Error(`VL6180X range error ${error}`);
|
|
259
|
+
return mm;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Take one single-shot ALS measurement.
|
|
264
|
+
* @returns {Promise<number>} 16-bit ALS count.
|
|
265
|
+
* @throws {Error} If no result arrives within the integration time + 100 ms.
|
|
266
|
+
*/
|
|
267
|
+
async readAlsRaw() {
|
|
268
|
+
return this._locked(async () => {
|
|
269
|
+
await this._wr8(_REG_ALS_START, 0x01);
|
|
270
|
+
if (!(await this._waitSample(3, this._integrationMs + 100))) {
|
|
271
|
+
await this._wr8(_REG_INTERRUPT_CLEAR, 0x07);
|
|
272
|
+
throw new Error('VL6180X timeout waiting for ALS result');
|
|
273
|
+
}
|
|
274
|
+
const raw = await this._rd16(_REG_ALS_VAL);
|
|
275
|
+
await this._wr8(_REG_INTERRUPT_CLEAR, 0x07);
|
|
276
|
+
return raw;
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Take one single-shot ALS measurement and convert it to lux:
|
|
282
|
+
* 0.32 * (count / gain) * (100 / integration_ms), actual gain of the current code.
|
|
283
|
+
* @returns {Promise<number>} Illuminance in lux (without cover glass).
|
|
284
|
+
*/
|
|
285
|
+
async readLux() {
|
|
286
|
+
const raw = await this.readAlsRaw();
|
|
287
|
+
return 0.32 * (raw / _GAIN[this._gainCode]) * (100.0 / this._integrationMs);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Start continuous ALS measurements.
|
|
292
|
+
* @param {number} periodMs - Period in ms, 10-2550 in 10 ms steps.
|
|
293
|
+
* @throws {RangeError} If out of range.
|
|
294
|
+
*/
|
|
295
|
+
async startContinuousAls(periodMs) {
|
|
296
|
+
if (periodMs < 10 || periodMs > 2550) throw new RangeError('period must be 10 to 2550 ms');
|
|
297
|
+
await this._locked(async () => {
|
|
298
|
+
await this._wr8(_REG_ALS_INTERMEASUREMENT, Math.floor(periodMs / 10) - 1);
|
|
299
|
+
await this._wr8(_REG_INTERRUPT_CLEAR, 0x07);
|
|
300
|
+
await this._wr8(_REG_ALS_START, 0x03);
|
|
301
|
+
});
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/** Stop continuous ALS measurements (the start/stop bit toggles the running mode). */
|
|
305
|
+
async stopContinuousAls() {
|
|
306
|
+
await this._wr8(_REG_ALS_START, 0x01);
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Enable or disable interleaved ALS + range mode (started with startContinuousAls()).
|
|
311
|
+
* @param {boolean} on
|
|
312
|
+
*/
|
|
313
|
+
async setInterleaved(on) {
|
|
314
|
+
await this._wr8(_REG_INTERLEAVED_ENABLE, on ? 1 : 0);
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Set the ALS analogue gain.
|
|
319
|
+
* @param {number} code - 0-7 (0 = 20, 1 = 10, 2 = 5, 3 = 2.5, 4 = 1.67, 5 = 1.25, 6 = 1, 7 = 40).
|
|
320
|
+
* @throws {RangeError} If out of range.
|
|
321
|
+
*/
|
|
322
|
+
async setAlsGain(code) {
|
|
323
|
+
if (code < 0 || code > 7) throw new RangeError('gain code must be 0 to 7');
|
|
324
|
+
await this._locked(async () => {
|
|
325
|
+
await this._wr8(_REG_GROUPED_PARAMETER_HOLD, 1);
|
|
326
|
+
await this._wr8(_REG_ALS_GAIN, 0x40 | code);
|
|
327
|
+
await this._wr8(_REG_GROUPED_PARAMETER_HOLD, 0);
|
|
328
|
+
this._gainCode = code;
|
|
329
|
+
});
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Set the ALS integration time.
|
|
334
|
+
* @param {number} ms - 1-512 ms.
|
|
335
|
+
* @throws {RangeError} If out of range.
|
|
336
|
+
*/
|
|
337
|
+
async setAlsIntegrationTime(ms) {
|
|
338
|
+
if (ms < 1 || ms > 512) throw new RangeError('integration time must be 1 to 512 ms');
|
|
339
|
+
await this._locked(async () => {
|
|
340
|
+
await this._wr8(_REG_GROUPED_PARAMETER_HOLD, 1);
|
|
341
|
+
await this._wr16(_REG_ALS_INTEGRATION, ms - 1);
|
|
342
|
+
await this._wr8(_REG_GROUPED_PARAMETER_HOLD, 0);
|
|
343
|
+
this._integrationMs = ms;
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
/**
|
|
348
|
+
* Set the ALS result scaler (FIRMWARE__RESULT_SCALER).
|
|
349
|
+
* @param {number} scaler - 1-16.
|
|
350
|
+
* @throws {RangeError} If out of range.
|
|
351
|
+
*/
|
|
352
|
+
async setAlsScaler(scaler) {
|
|
353
|
+
if (scaler < 1 || scaler > 16) throw new RangeError('scaler must be 1 to 16');
|
|
354
|
+
await this._wr8(_REG_RESULT_SCALER, scaler);
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Set the range interrupt thresholds (grouped-parameter-hold protocol).
|
|
359
|
+
* @param {number} low - Low threshold in mm, 0-255.
|
|
360
|
+
* @param {number} high - High threshold in mm, low <= high <= 255.
|
|
361
|
+
* @throws {RangeError} If out of range.
|
|
362
|
+
*/
|
|
363
|
+
async setRangeThresholds(low, high) {
|
|
364
|
+
if (low < 0 || high > 255 || low > high) throw new RangeError('thresholds must satisfy 0 <= low <= high <= 255');
|
|
365
|
+
await this._locked(async () => {
|
|
366
|
+
await this._wr8(_REG_GROUPED_PARAMETER_HOLD, 1);
|
|
367
|
+
await this._wr8(_REG_RANGE_THRESH_HIGH, high);
|
|
368
|
+
await this._wr8(_REG_RANGE_THRESH_LOW, low);
|
|
369
|
+
await this._wr8(_REG_GROUPED_PARAMETER_HOLD, 0);
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Set the ALS interrupt thresholds (grouped-parameter-hold protocol).
|
|
375
|
+
* @param {number} low - Low threshold, ALS counts.
|
|
376
|
+
* @param {number} high - High threshold, low <= high <= 65535.
|
|
377
|
+
* @throws {RangeError} If out of range.
|
|
378
|
+
*/
|
|
379
|
+
async setAlsThresholds(low, high) {
|
|
380
|
+
if (low < 0 || high > 65535 || low > high) throw new RangeError('thresholds must satisfy 0 <= low <= high <= 65535');
|
|
381
|
+
await this._locked(async () => {
|
|
382
|
+
await this._wr8(_REG_GROUPED_PARAMETER_HOLD, 1);
|
|
383
|
+
await this._wr16(_REG_ALS_THRESH_HIGH, high);
|
|
384
|
+
await this._wr16(_REG_ALS_THRESH_LOW, low);
|
|
385
|
+
await this._wr8(_REG_GROUPED_PARAMETER_HOLD, 0);
|
|
386
|
+
});
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
/**
|
|
390
|
+
* Set the maximum ranging convergence time.
|
|
391
|
+
* @param {number} ms - 1-63 ms.
|
|
392
|
+
* @throws {RangeError} If out of range.
|
|
393
|
+
*/
|
|
394
|
+
async setMaxConvergenceTime(ms) {
|
|
395
|
+
if (ms < 1 || ms > 63) throw new RangeError('convergence time must be 1 to 63 ms');
|
|
396
|
+
await this._wr8(_REG_RANGE_MAX_CONVERGENCE, ms);
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
/**
|
|
400
|
+
* Set the ranging averaging sample period.
|
|
401
|
+
* @param {number} units - 0-255; about 64.5 us per unit (48 is the default).
|
|
402
|
+
* @throws {RangeError} If out of range.
|
|
403
|
+
*/
|
|
404
|
+
async setAveragingPeriod(units) {
|
|
405
|
+
if (units < 0 || units > 255) throw new RangeError('averaging period must be 0 to 255');
|
|
406
|
+
await this._wr8(_REG_AVERAGING_PERIOD, units);
|
|
407
|
+
}
|
|
408
|
+
|
|
409
|
+
/**
|
|
410
|
+
* Set the part-to-part range offset.
|
|
411
|
+
* @param {number} mm - -128 to 127 (two's complement on the chip).
|
|
412
|
+
* @throws {RangeError} If out of range.
|
|
413
|
+
*/
|
|
414
|
+
async setRangeOffset(mm) {
|
|
415
|
+
if (mm < -128 || mm > 127) throw new RangeError('offset must be -128 to 127 mm');
|
|
416
|
+
await this._wr8(_REG_RANGE_OFFSET, mm & 0xFF);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Read the part-to-part range offset.
|
|
421
|
+
* @returns {Promise<number>} Offset in mm (signed).
|
|
422
|
+
*/
|
|
423
|
+
async getRangeOffset() {
|
|
424
|
+
const v = await this._rd8(_REG_RANGE_OFFSET);
|
|
425
|
+
return v & 0x80 ? v - 256 : v;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Set the crosstalk compensation rate.
|
|
430
|
+
* @param {number} rate - Rate in Mcps (stored as 9.7 fixed point).
|
|
431
|
+
* @throws {RangeError} If out of range.
|
|
432
|
+
*/
|
|
433
|
+
async setCrosstalkRate(rate) {
|
|
434
|
+
const code = Math.floor(rate * 128 + 0.5);
|
|
435
|
+
if (!(rate >= 0) || code > 0xFFFF) throw new RangeError('crosstalk rate out of range');
|
|
436
|
+
await this._wr16(_REG_RANGE_CROSSTALK_RATE, code);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
/**
|
|
440
|
+
* Start a VHV recalibration and wait for it to finish.
|
|
441
|
+
* @throws {Error} If the start bit does not clear within 100 ms.
|
|
442
|
+
*/
|
|
443
|
+
async recalibrateVhv() {
|
|
444
|
+
await this._locked(async () => {
|
|
445
|
+
await this._wr8(_REG_VHV_RECALIBRATE, 0x01);
|
|
446
|
+
const start = Date.now();
|
|
447
|
+
while (((await this._rd8(_REG_VHV_RECALIBRATE)) & 0x01) !== 0) {
|
|
448
|
+
if (Date.now() - start > _VHV_TIMEOUT_MS) throw new Error('VL6180X timeout waiting for VHV recalibration');
|
|
449
|
+
await _sleep(1);
|
|
450
|
+
}
|
|
451
|
+
});
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* Change the chip's I2C address (volatile). The connection passed at
|
|
456
|
+
* construction still points at the old address; create a new one.
|
|
457
|
+
* @param {number} address - New 7-bit address, 0x08-0x77.
|
|
458
|
+
* @throws {RangeError} If out of range.
|
|
459
|
+
*/
|
|
460
|
+
async setAddress(address) {
|
|
461
|
+
if (address < 0x08 || address > 0x77) throw new RangeError('address must be 0x08 to 0x77');
|
|
462
|
+
await this._wr8(_REG_I2C_ADDRESS, address & 0x7F);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
/**
|
|
466
|
+
* Read the model ID.
|
|
467
|
+
* @returns {Promise<number>} 0xB4 for the VL6180X.
|
|
468
|
+
*/
|
|
469
|
+
async modelId() {
|
|
470
|
+
return this._rd8(_REG_MODEL_ID);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Read the model revision.
|
|
475
|
+
* @returns {Promise<{major: number, minor: number}>} Bits 2:0 of each revision register.
|
|
476
|
+
*/
|
|
477
|
+
async revision() {
|
|
478
|
+
return {
|
|
479
|
+
major: (await this._rd8(_REG_MODEL_REV_MAJOR)) & 0x07,
|
|
480
|
+
minor: (await this._rd8(_REG_MODEL_REV_MINOR)) & 0x07,
|
|
481
|
+
};
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
/**
|
|
485
|
+
* Read the eight-entry history buffer (enable it through the history control register first).
|
|
486
|
+
* @returns {Promise<number[]>} Eight 16-bit entries.
|
|
487
|
+
*/
|
|
488
|
+
async readHistory() {
|
|
489
|
+
const d = await this._conn.readReg(_REG_HISTORY_BUFFER, 16);
|
|
490
|
+
const out = [];
|
|
491
|
+
for (let i = 0; i < 8; i++) out.push((d[2 * i] << 8) | d[2 * i + 1]);
|
|
492
|
+
return out;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
async _setInterruptMode(source, mode) {
|
|
496
|
+
let shift;
|
|
497
|
+
if (source === VL6180XMinimal.SOURCE_RANGE) shift = 0;
|
|
498
|
+
else if (source === VL6180XMinimal.SOURCE_ALS) shift = 3;
|
|
499
|
+
else throw new RangeError('source must be SOURCE_RANGE or SOURCE_ALS');
|
|
500
|
+
await this._locked(async () => {
|
|
501
|
+
let value = await this._rd8(_REG_INTERRUPT_CONFIG);
|
|
502
|
+
value = (value & ~(0x07 << shift) & 0xFF) | (mode << shift);
|
|
503
|
+
await this._wr8(_REG_GROUPED_PARAMETER_HOLD, 1);
|
|
504
|
+
await this._wr8(_REG_INTERRUPT_CONFIG, value);
|
|
505
|
+
await this._wr8(_REG_GROUPED_PARAMETER_HOLD, 0);
|
|
506
|
+
});
|
|
507
|
+
}
|
|
508
|
+
|
|
509
|
+
/**
|
|
510
|
+
* Select the interrupt mode of the range or ALS source.
|
|
511
|
+
* @param {number} source - SOURCE_RANGE or SOURCE_ALS.
|
|
512
|
+
* @param {number} mode - MODE_LEVEL_LOW, MODE_LEVEL_HIGH, MODE_OUT_OF_WINDOW or MODE_NEW_SAMPLE_READY.
|
|
513
|
+
* @throws {RangeError} If source or mode is invalid.
|
|
514
|
+
*/
|
|
515
|
+
async enableInterrupt(source, mode) {
|
|
516
|
+
if (mode < 1 || mode > 4) throw new RangeError('mode must be 1 to 4');
|
|
517
|
+
await this._setInterruptMode(source, mode);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/**
|
|
521
|
+
* Disable the interrupt of the range or ALS source (mode 0).
|
|
522
|
+
* @param {number} source - SOURCE_RANGE or SOURCE_ALS.
|
|
523
|
+
* @throws {RangeError} If source is invalid.
|
|
524
|
+
*/
|
|
525
|
+
async disableInterrupt(source) {
|
|
526
|
+
await this._setInterruptMode(source, 0);
|
|
527
|
+
}
|
|
528
|
+
|
|
529
|
+
/**
|
|
530
|
+
* Read the interrupt status and clear all pending sources.
|
|
531
|
+
* @returns {Promise<number>} Bit mask of SOURCE_RANGE, SOURCE_ALS, SOURCE_ERROR (0 if none).
|
|
532
|
+
*/
|
|
533
|
+
async pollInterrupt() {
|
|
534
|
+
return this._pollInterruptStatus();
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
/**
|
|
538
|
+
* Subscribe to GPIO1 interrupt events: falling edge on `connection.intPin`,
|
|
539
|
+
* otherwise a 5 ms polling timer. Pending sources are cleared before the
|
|
540
|
+
* callback runs.
|
|
541
|
+
* @param {function(number): void} callback - Called with the SOURCE_* bit mask.
|
|
542
|
+
*/
|
|
543
|
+
async onInterrupt(callback) {
|
|
544
|
+
this._callback = callback;
|
|
545
|
+
if (this._conn.intPin) {
|
|
546
|
+
this._edgeHandler = async () => {
|
|
547
|
+
const status = await this._pollInterruptStatus();
|
|
548
|
+
if (status && this._callback) this._callback(status);
|
|
549
|
+
};
|
|
550
|
+
await this._conn.intPin.onEdge(this._edgeHandler, 'falling');
|
|
551
|
+
} else {
|
|
552
|
+
let busy = false;
|
|
553
|
+
this._pollTimer = setInterval(async () => {
|
|
554
|
+
if (busy) return;
|
|
555
|
+
busy = true;
|
|
556
|
+
try {
|
|
557
|
+
const status = await this._pollInterruptStatus();
|
|
558
|
+
if (status && this._callback) this._callback(status);
|
|
559
|
+
} finally {
|
|
560
|
+
busy = false;
|
|
561
|
+
}
|
|
562
|
+
}, 5);
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
/** Unsubscribe and stop delivery. */
|
|
567
|
+
async offInterrupt() {
|
|
568
|
+
if (this._conn.intPin && this._edgeHandler) {
|
|
569
|
+
await this._conn.intPin.offEdge(this._edgeHandler);
|
|
570
|
+
this._edgeHandler = null;
|
|
571
|
+
}
|
|
572
|
+
if (this._pollTimer) {
|
|
573
|
+
clearInterval(this._pollTimer);
|
|
574
|
+
this._pollTimer = null;
|
|
575
|
+
}
|
|
576
|
+
this._callback = null;
|
|
577
|
+
}
|
|
578
|
+
}
|
|
579
|
+
|
|
580
|
+
module.exports = { VL6180XMinimal, VL6180XFull };
|