periph 1.5.3 → 1.5.4
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 +12 -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/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/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/package.json +1 -1
- package/src/chips/color/isl29125.js +379 -0
- package/src/chips/environmental/bme688.js +573 -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/pressure/mpl3115a2.js +459 -0
- package/src/chips/pressure/ms5607.js +290 -0
- package/src/chips/pressure/ms5611.js +290 -0
- package/src/discovery/registry.js +12 -12
- package/examples/gpio/.gitkeep +0 -0
- package/src/chips/gpio/.gitkeep +0 -0
|
@@ -0,0 +1,459 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { toSigned } = require('../../connection/register');
|
|
4
|
+
|
|
5
|
+
const _REG_DR_STATUS = 0x06;
|
|
6
|
+
const _REG_OUT_P_MSB = 0x01;
|
|
7
|
+
const _REG_OUT_P_DELTA = 0x07;
|
|
8
|
+
const _REG_WHO_AM_I = 0x0C;
|
|
9
|
+
const _REG_F_STATUS = 0x0D;
|
|
10
|
+
const _REG_F_DATA = 0x0E;
|
|
11
|
+
const _REG_F_SETUP = 0x0F;
|
|
12
|
+
const _REG_SYSMOD = 0x11;
|
|
13
|
+
const _REG_INT_SOURCE = 0x12;
|
|
14
|
+
const _REG_PT_DATA_CFG = 0x13;
|
|
15
|
+
const _REG_BAR_IN_MSB = 0x14;
|
|
16
|
+
const _REG_P_TGT_MSB = 0x16;
|
|
17
|
+
const _REG_T_TGT = 0x18;
|
|
18
|
+
const _REG_P_WND_MSB = 0x19;
|
|
19
|
+
const _REG_T_WND = 0x1B;
|
|
20
|
+
const _REG_P_MIN = 0x1C;
|
|
21
|
+
const _REG_T_MIN = 0x1F;
|
|
22
|
+
const _REG_P_MAX = 0x21;
|
|
23
|
+
const _REG_T_MAX = 0x24;
|
|
24
|
+
const _REG_CTRL_REG1 = 0x26;
|
|
25
|
+
const _REG_CTRL_REG2 = 0x27;
|
|
26
|
+
const _REG_CTRL_REG3 = 0x28;
|
|
27
|
+
const _REG_CTRL_REG4 = 0x29;
|
|
28
|
+
const _REG_CTRL_REG5 = 0x2A;
|
|
29
|
+
const _REG_OFF_P = 0x2B;
|
|
30
|
+
const _REG_OFF_T = 0x2C;
|
|
31
|
+
const _REG_OFF_H = 0x2D;
|
|
32
|
+
|
|
33
|
+
const _CHIP_ID = 0xC4;
|
|
34
|
+
const _CTRL1_ALT = 0x80;
|
|
35
|
+
const _CTRL1_RST = 0x04;
|
|
36
|
+
const _CTRL1_OST = 0x02;
|
|
37
|
+
const _CTRL1_SBYB = 0x01;
|
|
38
|
+
const _DR_PTDR = 0x08;
|
|
39
|
+
|
|
40
|
+
// minimum time between samples (ms) for OS = 0..7
|
|
41
|
+
const _OSR_TIME_MS = [6, 10, 18, 34, 66, 130, 258, 512];
|
|
42
|
+
|
|
43
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
44
|
+
|
|
45
|
+
function _pressureFrom(b) { return ((b[0] << 12) | (b[1] << 4) | (b[2] >> 4)) / 4.0; }
|
|
46
|
+
function _altitudeFrom(b) { return toSigned((b[0] << 8) | b[1], 16) + (b[2] >> 4) / 16.0; }
|
|
47
|
+
function _temperatureFrom(b) { return toSigned(b[0], 8) + (b[1] >> 4) / 16.0; }
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* MPL3115A2 barometric pressure / altitude / temperature sensor — minimal interface.
|
|
51
|
+
*
|
|
52
|
+
* One-shot barometer readings with no configuration beyond the connection. The I²C address is
|
|
53
|
+
* fixed at 0x60. `init()` resets the chip, enables the data-ready flags (PT_DATA_CFG = 0x07) and
|
|
54
|
+
* selects barometer mode with oversample ratio 128 (about 512 ms per sample) in STANDBY.
|
|
55
|
+
* The constructor does not touch the bus.
|
|
56
|
+
*
|
|
57
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection.
|
|
58
|
+
*/
|
|
59
|
+
class MPL3115A2Minimal {
|
|
60
|
+
constructor(connection) {
|
|
61
|
+
this._conn = connection;
|
|
62
|
+
this._os = 7;
|
|
63
|
+
this._alt = false;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async _writeReg(reg, value) { await this._conn.writeReg(reg, value); }
|
|
67
|
+
async _readReg(reg, n = 1) { return this._conn.readReg(reg, n); }
|
|
68
|
+
|
|
69
|
+
_ctrl1() { return (this._alt ? _CTRL1_ALT : 0) | (this._os << 3); }
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Check WHO_AM_I (0xC4), soft reset and apply the default configuration.
|
|
73
|
+
* @returns {Promise<void>}
|
|
74
|
+
* @throws {Error} If the chip ID is not 0xC4 or the reset does not complete.
|
|
75
|
+
*/
|
|
76
|
+
async init() {
|
|
77
|
+
const id = (await this._readReg(_REG_WHO_AM_I))[0];
|
|
78
|
+
if (id !== _CHIP_ID) throw new Error(`Unexpected chip ID 0x${id.toString(16)}, expected 0xC4`);
|
|
79
|
+
await this._softReset();
|
|
80
|
+
this._os = 7;
|
|
81
|
+
this._alt = false;
|
|
82
|
+
await this._applyConfig();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async _softReset() {
|
|
86
|
+
try { await this._writeReg(_REG_CTRL_REG1, _CTRL1_RST); } catch (e) { /* no ACK while rebooting */ }
|
|
87
|
+
for (let i = 0; i < 100; i++) {
|
|
88
|
+
await _sleep(2);
|
|
89
|
+
try {
|
|
90
|
+
if (!((await this._readReg(_REG_CTRL_REG1))[0] & _CTRL1_RST)) return;
|
|
91
|
+
} catch (e) { /* still rebooting */ }
|
|
92
|
+
}
|
|
93
|
+
throw new Error('MPL3115A2 reset timeout');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
async _applyConfig() {
|
|
97
|
+
await this._writeReg(_REG_PT_DATA_CFG, 0x07);
|
|
98
|
+
await this._writeReg(_REG_CTRL_REG1, this._ctrl1());
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async _waitPtdr() {
|
|
102
|
+
const deadline = Date.now() + _OSR_TIME_MS[this._os] * 2 + 1000;
|
|
103
|
+
for (;;) {
|
|
104
|
+
await _sleep(3);
|
|
105
|
+
if ((await this._readReg(_REG_DR_STATUS))[0] & _DR_PTDR) return;
|
|
106
|
+
if (Date.now() > deadline) throw new Error('MPL3115A2 data not ready');
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
async _oneShot() {
|
|
111
|
+
await this._writeReg(_REG_CTRL_REG1, this._ctrl1()); // OST must be 0 before it is set
|
|
112
|
+
await this._writeReg(_REG_CTRL_REG1, this._ctrl1() | _CTRL1_OST);
|
|
113
|
+
await this._waitPtdr();
|
|
114
|
+
return this._readReg(_REG_OUT_P_MSB, 5);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Run one conversion and return pressure and temperature together.
|
|
119
|
+
* @returns {Promise<{pressure_pa: number, temperature_c: number}>} Pascals and degrees Celsius.
|
|
120
|
+
*/
|
|
121
|
+
async read() {
|
|
122
|
+
const raw = await this._oneShot();
|
|
123
|
+
return {
|
|
124
|
+
pressure_pa: _pressureFrom(raw.slice(0, 3)),
|
|
125
|
+
temperature_c: _temperatureFrom(raw.slice(3, 5)),
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** Run one conversion and return the pressure. @returns {Promise<number>} Pascals. */
|
|
130
|
+
async readPressure() { return (await this.read()).pressure_pa; }
|
|
131
|
+
|
|
132
|
+
/** Run one conversion and return the temperature. @returns {Promise<number>} Degrees Celsius. */
|
|
133
|
+
async readTemperature() { return (await this.read()).temperature_c; }
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* MPL3115A2 full interface — extends MPL3115A2Minimal with altimeter mode, oversampling,
|
|
138
|
+
* continuous acquisition, offsets, sea-level pressure, targets and windows, min/max capture,
|
|
139
|
+
* the 32-sample FIFO and interrupts.
|
|
140
|
+
*/
|
|
141
|
+
class MPL3115A2Full extends MPL3115A2Minimal {
|
|
142
|
+
constructor(connection) {
|
|
143
|
+
super(connection);
|
|
144
|
+
this._fifoMode = 0;
|
|
145
|
+
this._callback = null;
|
|
146
|
+
this._intPinNo = 1;
|
|
147
|
+
this._intPinUsed = null;
|
|
148
|
+
this._pollTimer = null;
|
|
149
|
+
this._edgeHandler = () => { this._service(); };
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
async _standby() {
|
|
153
|
+
const c1 = (await this._readReg(_REG_CTRL_REG1))[0];
|
|
154
|
+
const wasActive = !!(c1 & _CTRL1_SBYB);
|
|
155
|
+
if (wasActive) await this._writeReg(_REG_CTRL_REG1, c1 & ~_CTRL1_SBYB & 0xFF);
|
|
156
|
+
return wasActive;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
async _restore(wasActive) {
|
|
160
|
+
if (wasActive) await this._writeReg(_REG_CTRL_REG1, this._ctrl1() | _CTRL1_SBYB);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Select barometer or altimeter mode (the chip is dropped to STANDBY and restored).
|
|
165
|
+
* @param {boolean} altimeter - True for altimeter (metres), false for barometer (pascals).
|
|
166
|
+
*/
|
|
167
|
+
async setMode(altimeter) {
|
|
168
|
+
const was = await this._standby();
|
|
169
|
+
this._alt = !!altimeter;
|
|
170
|
+
await this._writeReg(_REG_CTRL_REG1, this._ctrl1());
|
|
171
|
+
await this._restore(was);
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Switch to altimeter mode if needed and run one conversion. @returns {Promise<number>} Altitude in metres. */
|
|
175
|
+
async readAltitude() {
|
|
176
|
+
if (!this._alt) await this.setMode(true);
|
|
177
|
+
return _altitudeFrom((await this._oneShot()).slice(0, 3));
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Run one barometer conversion (switches back from altimeter mode if needed). @returns {Promise<{pressure_pa: number, temperature_c: number}>} */
|
|
181
|
+
async read() {
|
|
182
|
+
if (this._alt) await this.setMode(false);
|
|
183
|
+
return super.read();
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Set the oversample ratio.
|
|
188
|
+
* @param {number} ratio - 1, 2, 4, 8, 16, 32, 64 or 128.
|
|
189
|
+
* @throws {RangeError} For any other value.
|
|
190
|
+
*/
|
|
191
|
+
async setOversample(ratio) {
|
|
192
|
+
const os = [1, 2, 4, 8, 16, 32, 64, 128].indexOf(ratio);
|
|
193
|
+
if (os < 0) throw new RangeError('ratio must be 1, 2, 4, ..., 128');
|
|
194
|
+
const was = await this._standby();
|
|
195
|
+
this._os = os;
|
|
196
|
+
await this._writeReg(_REG_CTRL_REG1, this._ctrl1());
|
|
197
|
+
await this._restore(was);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
/** Current oversample ratio (1..128). @returns {number} */
|
|
201
|
+
getOversample() { return 1 << this._os; }
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Start periodic acquisition (ACTIVE mode).
|
|
205
|
+
* @param {number} [stepExp=0] - Auto-acquisition step exponent 0-15 (2^n seconds).
|
|
206
|
+
* @throws {RangeError} If stepExp is outside 0-15.
|
|
207
|
+
*/
|
|
208
|
+
async startContinuous(stepExp = 0) {
|
|
209
|
+
if (!(stepExp >= 0 && stepExp <= 15)) throw new RangeError('stepExp must be 0-15');
|
|
210
|
+
await this._standby();
|
|
211
|
+
const c2 = (await this._readReg(_REG_CTRL_REG2))[0];
|
|
212
|
+
await this._writeReg(_REG_CTRL_REG2, (c2 & 0xF0) | stepExp);
|
|
213
|
+
await this._writeReg(_REG_CTRL_REG1, this._ctrl1() | _CTRL1_SBYB);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Stop periodic acquisition (back to STANDBY). */
|
|
217
|
+
async stopContinuous() { await this._writeReg(_REG_CTRL_REG1, this._ctrl1()); }
|
|
218
|
+
|
|
219
|
+
/** True if new pressure/altitude or temperature data is ready (PTDR). @returns {Promise<boolean>} */
|
|
220
|
+
async dataReady() { return !!((await this._readReg(_REG_DR_STATUS))[0] & _DR_PTDR); }
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* Wait for PTDR and read the latest sample.
|
|
224
|
+
* @returns {Promise<{value: number, temperature_c: number}>} value is pascals or metres per mode.
|
|
225
|
+
*/
|
|
226
|
+
async readContinuous() {
|
|
227
|
+
await this._waitPtdr();
|
|
228
|
+
const raw = await this._readReg(_REG_OUT_P_MSB, 5);
|
|
229
|
+
const v = this._alt ? _altitudeFrom(raw.slice(0, 3)) : _pressureFrom(raw.slice(0, 3));
|
|
230
|
+
return { value: v, temperature_c: _temperatureFrom(raw.slice(3, 5)) };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** Change since the previous sample. @returns {Promise<[number, number]>} [delta value (Pa or m), delta temperature (°C)]. */
|
|
234
|
+
async readDelta() {
|
|
235
|
+
const raw = await this._readReg(_REG_OUT_P_DELTA, 5);
|
|
236
|
+
let dv;
|
|
237
|
+
if (this._alt) dv = _altitudeFrom(raw.slice(0, 3));
|
|
238
|
+
else {
|
|
239
|
+
dv = _pressureFrom(raw.slice(0, 3));
|
|
240
|
+
if (dv >= 262144) dv -= 524288;
|
|
241
|
+
}
|
|
242
|
+
return [dv, _temperatureFrom(raw.slice(3, 5))];
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/** Write BAR_IN, the altitude reference. @param {number} pa - Sea-level pressure in pascals (2 Pa steps, default 101326). */
|
|
246
|
+
async setSeaLevelPressure(pa) {
|
|
247
|
+
const raw = Math.round(pa / 2) & 0xFFFF;
|
|
248
|
+
await this._writeReg(_REG_BAR_IN_MSB, Buffer.from([raw >> 8, raw & 0xFF]));
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* Write the offset trims.
|
|
253
|
+
* @param {number} [pressurePa=0] - -512..+508 Pa (4 Pa steps).
|
|
254
|
+
* @param {number} [temperatureC=0] - -8..+7.9375 °C (0.0625 steps).
|
|
255
|
+
* @param {number} [altitudeM=0] - -128..+127 m.
|
|
256
|
+
* @throws {RangeError} If a value is outside its register range.
|
|
257
|
+
*/
|
|
258
|
+
async setOffsets(pressurePa = 0, temperatureC = 0, altitudeM = 0) {
|
|
259
|
+
const op = Math.round(pressurePa / 4);
|
|
260
|
+
const ot = Math.round(temperatureC / 0.0625);
|
|
261
|
+
const oh = Math.round(altitudeM);
|
|
262
|
+
for (const v of [op, ot, oh]) if (v < -128 || v > 127) throw new RangeError('offset out of range');
|
|
263
|
+
await this._writeReg(_REG_OFF_P, op & 0xFF);
|
|
264
|
+
await this._writeReg(_REG_OFF_T, ot & 0xFF);
|
|
265
|
+
await this._writeReg(_REG_OFF_H, oh & 0xFF);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
/**
|
|
269
|
+
* Program threshold/window targets.
|
|
270
|
+
* @param {number} value - Pressure target in pascals or altitude in metres (per mode).
|
|
271
|
+
* @param {number} window - Pressure window in pascals or altitude window in metres.
|
|
272
|
+
* @param {number} [temperatureC=0] - Temperature target (signed integer °C).
|
|
273
|
+
* @param {number} [temperatureWindow=0] - Temperature window (unsigned integer °C).
|
|
274
|
+
*/
|
|
275
|
+
async setTarget(value, window, temperatureC = 0, temperatureWindow = 0) {
|
|
276
|
+
const tgt = (this._alt ? Math.round(value) : Math.round(value / 2)) & 0xFFFF;
|
|
277
|
+
const wnd = (this._alt ? Math.round(window) : Math.round(window / 2)) & 0xFFFF;
|
|
278
|
+
const was = await this._standby();
|
|
279
|
+
await this._writeReg(_REG_P_TGT_MSB, Buffer.from([tgt >> 8, tgt & 0xFF]));
|
|
280
|
+
await this._writeReg(_REG_T_TGT, Math.round(temperatureC) & 0xFF);
|
|
281
|
+
await this._writeReg(_REG_P_WND_MSB, Buffer.from([wnd >> 8, wnd & 0xFF]));
|
|
282
|
+
await this._writeReg(_REG_T_WND, Math.round(temperatureWindow) & 0xFF);
|
|
283
|
+
await this._restore(was);
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/** Use the current output as the target (ALARM_SEL = 1, pulse LOAD_OUTPUT). */
|
|
287
|
+
async loadCurrentAsTarget() {
|
|
288
|
+
const c2 = (await this._readReg(_REG_CTRL_REG2))[0];
|
|
289
|
+
await this._writeReg(_REG_CTRL_REG2, (c2 | 0x10) & ~0x20 & 0xFF);
|
|
290
|
+
await this._writeReg(_REG_CTRL_REG2, c2 | 0x30);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/** Captured extremes. @returns {Promise<{p_min: number, p_max: number, t_min: number, t_max: number}>} */
|
|
294
|
+
async readMinMax() {
|
|
295
|
+
const pm = await this._readReg(_REG_P_MIN, 3);
|
|
296
|
+
const px = await this._readReg(_REG_P_MAX, 3);
|
|
297
|
+
const tm = await this._readReg(_REG_T_MIN, 2);
|
|
298
|
+
const tx = await this._readReg(_REG_T_MAX, 2);
|
|
299
|
+
const conv = this._alt ? _altitudeFrom : _pressureFrom;
|
|
300
|
+
return { p_min: conv(pm), p_max: conv(px), t_min: _temperatureFrom(tm), t_max: _temperatureFrom(tx) };
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** Clear the min/max registers. */
|
|
304
|
+
async clearMinMax() {
|
|
305
|
+
await this._writeReg(_REG_P_MIN, Buffer.alloc(3));
|
|
306
|
+
await this._writeReg(_REG_T_MIN, Buffer.alloc(2));
|
|
307
|
+
await this._writeReg(_REG_P_MAX, Buffer.alloc(3));
|
|
308
|
+
await this._writeReg(_REG_T_MAX, Buffer.alloc(2));
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* Configure the FIFO.
|
|
313
|
+
* @param {number} mode - 0 off, 1 circular, 2 stop when full.
|
|
314
|
+
* @param {number} [watermark=0] - Watermark sample count 0-63 (0 disables the flag).
|
|
315
|
+
* @throws {RangeError} For an invalid mode or a direct switch between modes 1 and 2.
|
|
316
|
+
*/
|
|
317
|
+
async fifoSetup(mode, watermark = 0) {
|
|
318
|
+
if (![0, 1, 2].includes(mode)) throw new RangeError('mode must be 0, 1 or 2');
|
|
319
|
+
if (this._fifoMode !== 0 && mode !== 0 && mode !== this._fifoMode) {
|
|
320
|
+
throw new RangeError('disable the FIFO before switching between circular and stop modes');
|
|
321
|
+
}
|
|
322
|
+
const was = await this._standby();
|
|
323
|
+
await this._writeReg(_REG_F_SETUP, (mode << 6) | (watermark & 0x3F));
|
|
324
|
+
this._fifoMode = mode;
|
|
325
|
+
await this._restore(was);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
/** FIFO state. @returns {Promise<[boolean, boolean, number]>} [overflow, watermark, count]. */
|
|
329
|
+
async fifoStatus() {
|
|
330
|
+
const s = (await this._readReg(_REG_F_STATUS))[0];
|
|
331
|
+
return [!!(s & 0x80), !!(s & 0x40), s & 0x3F];
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/** Drain the FIFO. @returns {Promise<Array<[number, number]>>} [value (Pa or m), temperature (°C)] per sample. */
|
|
335
|
+
async fifoRead() {
|
|
336
|
+
const count = (await this.fifoStatus())[2];
|
|
337
|
+
const out = [];
|
|
338
|
+
for (let i = 0; i < count; i++) {
|
|
339
|
+
const raw = await this._readReg(_REG_F_DATA, 5);
|
|
340
|
+
const v = this._alt ? _altitudeFrom(raw.slice(0, 3)) : _pressureFrom(raw.slice(0, 3));
|
|
341
|
+
out.push([v, _temperatureFrom(raw.slice(3, 5))]);
|
|
342
|
+
}
|
|
343
|
+
return out;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/** Software reset, then restore the default configuration (as `init()`). */
|
|
347
|
+
async reset() {
|
|
348
|
+
await this._softReset();
|
|
349
|
+
this._os = 7;
|
|
350
|
+
this._alt = false;
|
|
351
|
+
this._fifoMode = 0;
|
|
352
|
+
await this._applyConfig();
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/** WHO_AM_I register (0xC4). @returns {Promise<number>} */
|
|
356
|
+
async whoAmI() { return (await this._readReg(_REG_WHO_AM_I))[0]; }
|
|
357
|
+
|
|
358
|
+
/** True if SYSMOD reports ACTIVE mode. @returns {Promise<boolean>} */
|
|
359
|
+
async isActive() { return !!((await this._readReg(_REG_SYSMOD))[0] & 0x01); }
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Write CTRL_REG3: interrupt polarity (true = active high) and open-drain per pin.
|
|
363
|
+
*/
|
|
364
|
+
async setInterruptPins(polarity1 = false, openDrain1 = false, polarity2 = false, openDrain2 = false) {
|
|
365
|
+
const was = await this._standby();
|
|
366
|
+
await this._writeReg(_REG_CTRL_REG3, (polarity1 ? 0x20 : 0) | (openDrain1 ? 0x10 : 0)
|
|
367
|
+
| (polarity2 ? 0x02 : 0) | (openDrain2 ? 0x01 : 0));
|
|
368
|
+
await this._restore(was);
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
/**
|
|
372
|
+
* Enable an interrupt source and route it to INT1 or INT2.
|
|
373
|
+
* @param {number} source - A SOURCE_* constant (or an OR of them).
|
|
374
|
+
* @param {number} [pin=1] - 1 or 2.
|
|
375
|
+
* @throws {RangeError} If pin is not 1 or 2.
|
|
376
|
+
*/
|
|
377
|
+
async enableInterrupt(source, pin = 1) {
|
|
378
|
+
if (pin !== 1 && pin !== 2) throw new RangeError('pin must be 1 or 2');
|
|
379
|
+
const was = await this._standby();
|
|
380
|
+
const c4 = (await this._readReg(_REG_CTRL_REG4))[0];
|
|
381
|
+
const c5 = (await this._readReg(_REG_CTRL_REG5))[0];
|
|
382
|
+
await this._writeReg(_REG_CTRL_REG4, c4 | source);
|
|
383
|
+
await this._writeReg(_REG_CTRL_REG5, pin === 1 ? (c5 | source) : (c5 & ~source & 0xFF));
|
|
384
|
+
await this._restore(was);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/** Disable an interrupt source (clears it in CTRL_REG4). @param {number} source */
|
|
388
|
+
async disableInterrupt(source) {
|
|
389
|
+
const was = await this._standby();
|
|
390
|
+
const c4 = (await this._readReg(_REG_CTRL_REG4))[0];
|
|
391
|
+
await this._writeReg(_REG_CTRL_REG4, c4 & ~source & 0xFF);
|
|
392
|
+
await this._restore(was);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/** Read INT_SOURCE. @returns {Promise<number>} Bitmask of SOURCE_* flags. */
|
|
396
|
+
async pollInterrupt() { return (await this._readReg(_REG_INT_SOURCE))[0]; }
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Subscribe to INT1 or INT2 (falling edge). `callback(status)` receives INT_SOURCE masked to the
|
|
400
|
+
* sources routed to that pin. Delivery is via the INT line (options.intPin or connection.intPin)
|
|
401
|
+
* or a 100 ms polling timer.
|
|
402
|
+
* @param {function} callback - Called with (status).
|
|
403
|
+
* @param {object} [options]
|
|
404
|
+
* @param {number} [options.pin=1] - 1 or 2.
|
|
405
|
+
* @param {import('../../connection/input_pin').InputPin|null} [options.intPin] - Overrides connection.intPin.
|
|
406
|
+
*/
|
|
407
|
+
async onInterrupt(callback, options = {}) {
|
|
408
|
+
const pin = options.pin ?? 1;
|
|
409
|
+
if (pin !== 1 && pin !== 2) throw new RangeError('pin must be 1 or 2');
|
|
410
|
+
this._callback = callback;
|
|
411
|
+
this._intPinNo = pin;
|
|
412
|
+
const ipin = options.intPin ?? this._conn.intPin ?? null;
|
|
413
|
+
this._intPinUsed = ipin;
|
|
414
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
415
|
+
if (ipin) {
|
|
416
|
+
await ipin.onEdge(this._edgeHandler, 'falling');
|
|
417
|
+
} else {
|
|
418
|
+
this._pollTimer = setInterval(() => { this._service(); }, 100);
|
|
419
|
+
if (this._pollTimer.unref) this._pollTimer.unref();
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/** Unsubscribe and stop delivery. */
|
|
424
|
+
async offInterrupt() {
|
|
425
|
+
if (this._intPinUsed) {
|
|
426
|
+
await this._intPinUsed.offEdge(this._edgeHandler);
|
|
427
|
+
this._intPinUsed = null;
|
|
428
|
+
}
|
|
429
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
430
|
+
this._callback = null;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
async _service() {
|
|
434
|
+
if (this._polling) return;
|
|
435
|
+
this._polling = true;
|
|
436
|
+
try {
|
|
437
|
+
let status = await this.pollInterrupt();
|
|
438
|
+
const c5 = (await this._readReg(_REG_CTRL_REG5))[0];
|
|
439
|
+
status &= this._intPinNo === 1 ? c5 : (~c5 & 0xFF);
|
|
440
|
+
if (status && this._callback) this._callback(status);
|
|
441
|
+
} finally {
|
|
442
|
+
this._polling = false;
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
MPL3115A2Full.SOURCE_DRDY = 0x80;
|
|
448
|
+
MPL3115A2Full.SOURCE_FIFO = 0x40;
|
|
449
|
+
MPL3115A2Full.SOURCE_PW = 0x20;
|
|
450
|
+
MPL3115A2Full.SOURCE_TW = 0x10;
|
|
451
|
+
MPL3115A2Full.SOURCE_PTH = 0x08;
|
|
452
|
+
MPL3115A2Full.SOURCE_TTH = 0x04;
|
|
453
|
+
MPL3115A2Full.SOURCE_PCHG = 0x02;
|
|
454
|
+
MPL3115A2Full.SOURCE_TCHG = 0x01;
|
|
455
|
+
MPL3115A2Full.FIFO_OFF = 0;
|
|
456
|
+
MPL3115A2Full.FIFO_CIRCULAR = 1;
|
|
457
|
+
MPL3115A2Full.FIFO_STOP = 2;
|
|
458
|
+
|
|
459
|
+
module.exports = { MPL3115A2Minimal, MPL3115A2Full };
|