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,461 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { toSigned } = require('../../connection/register');
|
|
4
|
+
|
|
5
|
+
const _REG_PRS_B2 = 0x00;
|
|
6
|
+
const _REG_TMP_B2 = 0x03;
|
|
7
|
+
const _REG_PRS_CFG = 0x06;
|
|
8
|
+
const _REG_TMP_CFG = 0x07;
|
|
9
|
+
const _REG_MEAS_CFG = 0x08;
|
|
10
|
+
const _REG_CFG_REG = 0x09;
|
|
11
|
+
const _REG_INT_STS = 0x0A;
|
|
12
|
+
const _REG_FIFO_STS = 0x0B;
|
|
13
|
+
const _REG_RESET = 0x0C;
|
|
14
|
+
const _REG_ID = 0x0D;
|
|
15
|
+
const _REG_COEF = 0x10;
|
|
16
|
+
|
|
17
|
+
const _CHIP_ID = 0x10;
|
|
18
|
+
const _MEAS_COEF_RDY = 0x80;
|
|
19
|
+
const _MEAS_SENSOR_RDY = 0x40;
|
|
20
|
+
const _MEAS_TMP_RDY = 0x20;
|
|
21
|
+
const _MEAS_PRS_RDY = 0x10;
|
|
22
|
+
const _CMD_IDLE = 0x00;
|
|
23
|
+
const _CMD_PRESSURE = 0x01;
|
|
24
|
+
const _CMD_TEMPERATURE = 0x02;
|
|
25
|
+
const _CFG_T_SHIFT = 0x08;
|
|
26
|
+
const _CFG_P_SHIFT = 0x04;
|
|
27
|
+
const _CFG_INT_HL = 0x80;
|
|
28
|
+
const _CFG_INT_FIFO = 0x40;
|
|
29
|
+
const _CFG_INT_PRS = 0x20;
|
|
30
|
+
const _CFG_INT_TMP = 0x10;
|
|
31
|
+
const _CFG_FIFO_EN = 0x02;
|
|
32
|
+
const _TMP_EXT = 0x80;
|
|
33
|
+
|
|
34
|
+
// Scale factors kP / kT indexed by log2(oversampling) (datasheet Table 4)
|
|
35
|
+
const _SCALE = [524288, 1572864, 3670016, 7864320, 253952, 516096, 1040384, 2088960];
|
|
36
|
+
// Typical measurement time in ms indexed by log2(oversampling), rounded up
|
|
37
|
+
const _MEAS_MS = [4, 6, 9, 15, 28, 54, 105, 207];
|
|
38
|
+
const _VALID = [1, 2, 4, 8, 16, 32, 64, 128];
|
|
39
|
+
|
|
40
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
41
|
+
|
|
42
|
+
function _raw24(b) { return toSigned((b[0] << 16) | (b[1] << 8) | b[2], 24); }
|
|
43
|
+
|
|
44
|
+
function _log2(value, name) {
|
|
45
|
+
const i = _VALID.indexOf(value);
|
|
46
|
+
if (i < 0) throw new RangeError(`${name} must be 1, 2, 4, 8, 16, 32, 64 or 128`);
|
|
47
|
+
return i;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* SPL06-007 barometric pressure and temperature sensor (Goertek) — minimal interface.
|
|
52
|
+
*
|
|
53
|
+
* One call returns compensated temperature and pressure. The chip stays in standby between
|
|
54
|
+
* readings; each `read()` runs one temperature and one pressure measurement in command mode.
|
|
55
|
+
* `init()` verifies ID = 0x10, waits for SENSOR_RDY / COEF_RDY, reads the 18 coefficient bytes and
|
|
56
|
+
* writes TMP_CFG = 0x80 (external temperature sensor, 1×), PRS_CFG = 0x04 (16×, standard
|
|
57
|
+
* precision) and CFG_REG = 0x04 (P_SHIFT). I²C address 0x77 (SDO high or open) or 0x76 (SDO low).
|
|
58
|
+
* The constructor does not touch the bus.
|
|
59
|
+
*
|
|
60
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection.
|
|
61
|
+
*/
|
|
62
|
+
class SPL06Minimal {
|
|
63
|
+
constructor(connection) {
|
|
64
|
+
this._conn = connection;
|
|
65
|
+
this._coef = null;
|
|
66
|
+
this._pOs = 4;
|
|
67
|
+
this._tOs = 0;
|
|
68
|
+
this._cfg = _CFG_P_SHIFT;
|
|
69
|
+
this._lastTraw = 0;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Verify the chip, read the calibration coefficients and apply the defaults.
|
|
74
|
+
* @returns {Promise<void>}
|
|
75
|
+
* @throws {Error} If the ID is not 0x10 or the chip does not become ready.
|
|
76
|
+
*/
|
|
77
|
+
async init() {
|
|
78
|
+
const id = (await this._conn.readReg(_REG_ID, 1))[0];
|
|
79
|
+
if (id !== _CHIP_ID) throw new Error(`Unexpected chip ID 0x${id.toString(16)}, expected 0x10`);
|
|
80
|
+
await this._waitReady();
|
|
81
|
+
await this._readCoefficients();
|
|
82
|
+
await this._applyDefaults();
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
async _waitReady() {
|
|
86
|
+
const mask = _MEAS_COEF_RDY | _MEAS_SENSOR_RDY;
|
|
87
|
+
const deadline = Date.now() + 200;
|
|
88
|
+
for (;;) {
|
|
89
|
+
try {
|
|
90
|
+
if (((await this._conn.readReg(_REG_MEAS_CFG, 1))[0] & mask) === mask) return;
|
|
91
|
+
} catch (e) { /* no ACK right after a reset */ }
|
|
92
|
+
if (Date.now() > deadline) throw new Error('SPL06 not ready');
|
|
93
|
+
await _sleep(5);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async _readCoefficients() {
|
|
98
|
+
const b = await this._conn.readReg(_REG_COEF, 18);
|
|
99
|
+
this._coef = {
|
|
100
|
+
c0: toSigned((b[0] << 4) | (b[1] >> 4), 12),
|
|
101
|
+
c1: toSigned(((b[1] & 0x0F) << 8) | b[2], 12),
|
|
102
|
+
c00: toSigned((b[3] << 12) | (b[4] << 4) | (b[5] >> 4), 20),
|
|
103
|
+
c10: toSigned(((b[5] & 0x0F) << 16) | (b[6] << 8) | b[7], 20),
|
|
104
|
+
c01: toSigned((b[8] << 8) | b[9], 16),
|
|
105
|
+
c11: toSigned((b[10] << 8) | b[11], 16),
|
|
106
|
+
c20: toSigned((b[12] << 8) | b[13], 16),
|
|
107
|
+
c21: toSigned((b[14] << 8) | b[15], 16),
|
|
108
|
+
c30: toSigned((b[16] << 8) | b[17], 16),
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async _applyDefaults() {
|
|
113
|
+
this._pOs = 4;
|
|
114
|
+
this._tOs = 0;
|
|
115
|
+
this._cfg = _CFG_P_SHIFT;
|
|
116
|
+
await this._conn.writeReg(_REG_TMP_CFG, _TMP_EXT);
|
|
117
|
+
await this._conn.writeReg(_REG_PRS_CFG, 0x04);
|
|
118
|
+
await this._conn.writeReg(_REG_CFG_REG, this._cfg);
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async _measure(command, flag, osLog2) {
|
|
122
|
+
await this._conn.writeReg(_REG_MEAS_CFG, command);
|
|
123
|
+
await _sleep(_MEAS_MS[osLog2]);
|
|
124
|
+
const deadline = Date.now() + _MEAS_MS[osLog2] + 50;
|
|
125
|
+
while (!((await this._conn.readReg(_REG_MEAS_CFG, 1))[0] & flag)) {
|
|
126
|
+
if (Date.now() > deadline) throw new Error('SPL06 measurement timeout');
|
|
127
|
+
await _sleep(2);
|
|
128
|
+
}
|
|
129
|
+
return _raw24(await this._conn.readReg(command === _CMD_PRESSURE ? _REG_PRS_B2 : _REG_TMP_B2, 3));
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async _measureRaw() {
|
|
133
|
+
const traw = await this._measure(_CMD_TEMPERATURE, _MEAS_TMP_RDY, this._tOs);
|
|
134
|
+
const praw = await this._measure(_CMD_PRESSURE, _MEAS_PRS_RDY, this._pOs);
|
|
135
|
+
this._lastTraw = traw;
|
|
136
|
+
return [praw, traw];
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
_temperature(traw) {
|
|
140
|
+
return this._coef.c0 * 0.5 + this._coef.c1 * (traw / _SCALE[this._tOs]);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
_pressure(praw, traw) {
|
|
144
|
+
const c = this._coef;
|
|
145
|
+
const p = praw / _SCALE[this._pOs];
|
|
146
|
+
const t = traw / _SCALE[this._tOs];
|
|
147
|
+
return c.c00 + p * (c.c10 + p * (c.c20 + p * c.c30)) + t * c.c01 + t * p * (c.c11 + p * c.c21);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Measure temperature, then pressure (command mode), and compensate both.
|
|
152
|
+
* @returns {Promise<{temperature_c: number, pressure_pa: number}>} Degrees Celsius and pascals.
|
|
153
|
+
*/
|
|
154
|
+
async read() {
|
|
155
|
+
const [praw, traw] = await this._measureRaw();
|
|
156
|
+
return { temperature_c: this._temperature(traw), pressure_pa: this._pressure(praw, traw) };
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* SPL06-007 full interface — extends SPL06Minimal with oversampling and rate configuration,
|
|
162
|
+
* background (continuous) mode, the 32-entry FIFO, raw values and compensation, altitude,
|
|
163
|
+
* soft reset and the data-ready / FIFO-full interrupt.
|
|
164
|
+
*
|
|
165
|
+
* The interrupt output shares the SDO pin. INT_HL stays at its reset value 0 (active low), whose
|
|
166
|
+
* idle-high level matches the default address 0x77.
|
|
167
|
+
*
|
|
168
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection.
|
|
169
|
+
*/
|
|
170
|
+
class SPL06Full extends SPL06Minimal {
|
|
171
|
+
constructor(connection) {
|
|
172
|
+
super(connection);
|
|
173
|
+
this._pRate = 0;
|
|
174
|
+
this._tRate = 0;
|
|
175
|
+
this._continuous = 0;
|
|
176
|
+
this._callback = null;
|
|
177
|
+
this._intPinUsed = null;
|
|
178
|
+
this._pollTimer = null;
|
|
179
|
+
this._polling = false;
|
|
180
|
+
this._edgeHandler = () => { this._service(); };
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async _writeCfg() { await this._conn.writeReg(_REG_CFG_REG, this._cfg); }
|
|
184
|
+
|
|
185
|
+
static _intBits(source) {
|
|
186
|
+
let bits = 0;
|
|
187
|
+
if (source & SPL06Full.SOURCE_PRESSURE) bits |= _CFG_INT_PRS;
|
|
188
|
+
if (source & SPL06Full.SOURCE_TEMPERATURE) bits |= _CFG_INT_TMP;
|
|
189
|
+
if (source & SPL06Full.SOURCE_FIFO_FULL) bits |= _CFG_INT_FIFO;
|
|
190
|
+
return bits;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Measure temperature, then pressure (in background mode: the latest results).
|
|
195
|
+
* @returns {Promise<{temperature_c: number, pressure_pa: number}>} Degrees Celsius and pascals.
|
|
196
|
+
*/
|
|
197
|
+
async read() {
|
|
198
|
+
if (this._continuous) return this.readContinuous();
|
|
199
|
+
return super.read();
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** Measure temperature and pressure and return the compensated pressure. @returns {Promise<number>} Pascals. */
|
|
203
|
+
async readPressure() { return (await this.read()).pressure_pa; }
|
|
204
|
+
|
|
205
|
+
/** Measure and return the temperature. @returns {Promise<number>} Degrees Celsius. */
|
|
206
|
+
async readTemperature() {
|
|
207
|
+
if (this._continuous) return (await this.readContinuous()).temperature_c;
|
|
208
|
+
const traw = await this._measure(_CMD_TEMPERATURE, _MEAS_TMP_RDY, this._tOs);
|
|
209
|
+
this._lastTraw = traw;
|
|
210
|
+
return this._temperature(traw);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Raw 24-bit two's-complement results. Command mode measures temperature, then pressure;
|
|
215
|
+
* background mode reads the latest results without starting a measurement.
|
|
216
|
+
* @returns {Promise<number[]>} `[praw, traw]`.
|
|
217
|
+
*/
|
|
218
|
+
async readRaw() {
|
|
219
|
+
if (this._continuous) {
|
|
220
|
+
const b = await this._conn.readReg(_REG_PRS_B2, 6);
|
|
221
|
+
const praw = _raw24(b.slice(0, 3));
|
|
222
|
+
const traw = _raw24(b.slice(3, 6));
|
|
223
|
+
this._lastTraw = traw;
|
|
224
|
+
return [praw, traw];
|
|
225
|
+
}
|
|
226
|
+
return this._measureRaw();
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Write PRS_CFG and P_SHIFT (set above 8×).
|
|
231
|
+
* @param {number} [oversampling=16] - 1, 2, 4, 8, 16, 32, 64 or 128.
|
|
232
|
+
* @param {number} [rate=1] - Background measurements per second: 1, 2, 4, ..., 128.
|
|
233
|
+
* @throws {RangeError} For values other than powers of two 1-128.
|
|
234
|
+
*/
|
|
235
|
+
async setPressureConfig(oversampling = 16, rate = 1) {
|
|
236
|
+
const os = _log2(oversampling, 'oversampling');
|
|
237
|
+
const r = _log2(rate, 'rate');
|
|
238
|
+
await this._conn.writeReg(_REG_PRS_CFG, (r << 4) | os);
|
|
239
|
+
this._pOs = os;
|
|
240
|
+
this._pRate = r;
|
|
241
|
+
this._cfg = oversampling > 8 ? (this._cfg | _CFG_P_SHIFT) : (this._cfg & ~_CFG_P_SHIFT & 0xFF);
|
|
242
|
+
await this._writeCfg();
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Write TMP_CFG (always TMP_EXT = 1) and T_SHIFT (set above 8×).
|
|
247
|
+
* @param {number} [oversampling=1] - 1, 2, 4, 8, 16, 32, 64 or 128.
|
|
248
|
+
* @param {number} [rate=1] - Background measurements per second: 1, 2, 4, ..., 128.
|
|
249
|
+
* @throws {RangeError} For values other than powers of two 1-128.
|
|
250
|
+
*/
|
|
251
|
+
async setTemperatureConfig(oversampling = 1, rate = 1) {
|
|
252
|
+
const os = _log2(oversampling, 'oversampling');
|
|
253
|
+
const r = _log2(rate, 'rate');
|
|
254
|
+
await this._conn.writeReg(_REG_TMP_CFG, _TMP_EXT | (r << 4) | os);
|
|
255
|
+
this._tOs = os;
|
|
256
|
+
this._tRate = r;
|
|
257
|
+
this._cfg = oversampling > 8 ? (this._cfg | _CFG_T_SHIFT) : (this._cfg & ~_CFG_T_SHIFT & 0xFF);
|
|
258
|
+
await this._writeCfg();
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Start background mode (MEAS_CTRL = 101 / 110 / 111).
|
|
263
|
+
* @param {boolean} [pressure=true] - Measure pressure continuously.
|
|
264
|
+
* @param {boolean} [temperature=true] - Measure temperature continuously.
|
|
265
|
+
* @throws {RangeError} If both are false.
|
|
266
|
+
*/
|
|
267
|
+
async startContinuous(pressure = true, temperature = true) {
|
|
268
|
+
if (!pressure && !temperature) throw new RangeError('select pressure and/or temperature');
|
|
269
|
+
const mode = 0x04 | (pressure ? 0x01 : 0) | (temperature ? 0x02 : 0);
|
|
270
|
+
await this._conn.writeReg(_REG_MEAS_CFG, mode);
|
|
271
|
+
this._continuous = mode;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/** Stop background mode and return to standby (MEAS_CTRL = 000). */
|
|
275
|
+
async stop() {
|
|
276
|
+
await this._conn.writeReg(_REG_MEAS_CFG, _CMD_IDLE);
|
|
277
|
+
this._continuous = 0;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* Wait for the background ready flags and return the latest results (no new measurement).
|
|
282
|
+
* @returns {Promise<{temperature_c: number, pressure_pa: number}>} Degrees Celsius and pascals.
|
|
283
|
+
*/
|
|
284
|
+
async readContinuous() {
|
|
285
|
+
let flags = 0;
|
|
286
|
+
if (this._continuous & 0x01) flags |= _MEAS_PRS_RDY;
|
|
287
|
+
if (this._continuous & 0x02) flags |= _MEAS_TMP_RDY;
|
|
288
|
+
if (flags) {
|
|
289
|
+
const period = 1000 / (1 << Math.min(this._pRate, this._tRate));
|
|
290
|
+
const deadline = Date.now() + period * 2 + 500;
|
|
291
|
+
while (((await this._conn.readReg(_REG_MEAS_CFG, 1))[0] & flags) !== flags) {
|
|
292
|
+
if (Date.now() > deadline) throw new Error('SPL06 background data not ready');
|
|
293
|
+
await _sleep(2);
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
const b = await this._conn.readReg(_REG_PRS_B2, 6);
|
|
297
|
+
const praw = _raw24(b.slice(0, 3));
|
|
298
|
+
const traw = _raw24(b.slice(3, 6));
|
|
299
|
+
this._lastTraw = traw;
|
|
300
|
+
return { temperature_c: this._temperature(traw), pressure_pa: this._pressure(praw, traw) };
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/** Read the MEAS_CFG ready flags. @returns {Promise<boolean[]>} `[pressureReady, temperatureReady]`. */
|
|
304
|
+
async dataReady() {
|
|
305
|
+
const m = (await this._conn.readReg(_REG_MEAS_CFG, 1))[0];
|
|
306
|
+
return [!!(m & _MEAS_PRS_RDY), !!(m & _MEAS_TMP_RDY)];
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/** Enable the 32-entry result FIFO (CFG_REG.FIFO_EN). */
|
|
310
|
+
async enableFifo() {
|
|
311
|
+
this._cfg |= _CFG_FIFO_EN;
|
|
312
|
+
await this._writeCfg();
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
/** Disable the result FIFO. */
|
|
316
|
+
async disableFifo() {
|
|
317
|
+
this._cfg &= ~_CFG_FIFO_EN & 0xFF;
|
|
318
|
+
await this._writeCfg();
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
/**
|
|
322
|
+
* Drain the FIFO until FIFO_EMPTY (at most 32 entries). The LSB of each entry flags its kind
|
|
323
|
+
* (1 = pressure, 0 = temperature); pressure entries are compensated with the most recent
|
|
324
|
+
* temperature entry (or the last measured temperature).
|
|
325
|
+
* @returns {Promise<Array<{kind: string, value: number}>>} `kind` is "pressure" (Pa) or "temperature" (°C).
|
|
326
|
+
*/
|
|
327
|
+
async readFifo() {
|
|
328
|
+
const out = [];
|
|
329
|
+
for (let i = 0; i < 32; i++) {
|
|
330
|
+
if ((await this._conn.readReg(_REG_FIFO_STS, 1))[0] & 0x01) break;
|
|
331
|
+
const raw = _raw24(await this._conn.readReg(_REG_PRS_B2, 3));
|
|
332
|
+
if (raw === -0x800000) break;
|
|
333
|
+
if (raw & 1) {
|
|
334
|
+
out.push({ kind: 'pressure', value: this._pressure(raw & ~1, this._lastTraw) });
|
|
335
|
+
} else {
|
|
336
|
+
this._lastTraw = raw;
|
|
337
|
+
out.push({ kind: 'temperature', value: this._temperature(raw) });
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
return out;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
/** Discard the FIFO contents (RESET.FIFO_FLUSH). */
|
|
344
|
+
async flushFifo() { await this._conn.writeReg(_REG_RESET, 0x80); }
|
|
345
|
+
|
|
346
|
+
/** Read FIFO_STS. @returns {Promise<boolean[]>} `[empty, full]`. */
|
|
347
|
+
async fifoStatus() {
|
|
348
|
+
const s = (await this._conn.readReg(_REG_FIFO_STS, 1))[0];
|
|
349
|
+
return [!!(s & 0x01), !!(s & 0x02)];
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/** Soft reset (RESET = 0x09), wait for the ready flags, re-read the coefficients and restore the init defaults. */
|
|
353
|
+
async reset() {
|
|
354
|
+
try { await this._conn.writeReg(_REG_RESET, 0x09); } catch (e) { /* no ACK while resetting */ }
|
|
355
|
+
await _sleep(12);
|
|
356
|
+
await this._waitReady();
|
|
357
|
+
await this._readCoefficients();
|
|
358
|
+
await this._applyDefaults();
|
|
359
|
+
this._pRate = 0;
|
|
360
|
+
this._tRate = 0;
|
|
361
|
+
this._continuous = 0;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/** Decoded calibration coefficients. @returns {{c0: number, c1: number, c00: number, c10: number, c01: number, c11: number, c20: number, c21: number, c30: number}} */
|
|
365
|
+
coefficients() { return { ...this._coef }; }
|
|
366
|
+
|
|
367
|
+
/**
|
|
368
|
+
* Compensate raw values with the stored coefficients (no bus access).
|
|
369
|
+
* @param {number} praw - Signed raw pressure.
|
|
370
|
+
* @param {number} traw - Signed raw temperature.
|
|
371
|
+
* @param {number} pOversampling - Pressure oversampling of `praw`.
|
|
372
|
+
* @param {number} tOversampling - Temperature oversampling of `traw`.
|
|
373
|
+
* @returns {{temperature_c: number, pressure_pa: number}} Degrees Celsius and pascals.
|
|
374
|
+
* @throws {RangeError} For an oversampling other than a power of two 1-128.
|
|
375
|
+
*/
|
|
376
|
+
compensate(praw, traw, pOversampling, tOversampling) {
|
|
377
|
+
const c = this._coef;
|
|
378
|
+
const p = praw / _SCALE[_log2(pOversampling, 'pOversampling')];
|
|
379
|
+
const t = traw / _SCALE[_log2(tOversampling, 'tOversampling')];
|
|
380
|
+
return {
|
|
381
|
+
temperature_c: c.c0 * 0.5 + c.c1 * t,
|
|
382
|
+
pressure_pa: c.c00 + p * (c.c10 + p * (c.c20 + p * c.c30)) + t * c.c01 + t * p * (c.c11 + p * c.c21),
|
|
383
|
+
};
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Measure the pressure and convert it to altitude (international barometric formula).
|
|
388
|
+
* @param {number} [seaLevelPa=101325] - Reference sea-level pressure in pascals.
|
|
389
|
+
* @returns {Promise<number>} Altitude in metres.
|
|
390
|
+
*/
|
|
391
|
+
async altitude(seaLevelPa = 101325.0) {
|
|
392
|
+
const p = await this.readPressure();
|
|
393
|
+
return 44330.0 * (1.0 - Math.pow(p / seaLevelPa, 1.0 / 5.255));
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/** Enable interrupt sources on SDO (CFG_REG.INT_PRS / INT_TMP / INT_FIFO). @param {number} source - SOURCE_* or a bitwise OR. */
|
|
397
|
+
async enableInterrupt(source) {
|
|
398
|
+
this._cfg |= SPL06Full._intBits(source);
|
|
399
|
+
await this._writeCfg();
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
/** Disable interrupt sources. @param {number} source - SOURCE_* or a bitwise OR. */
|
|
403
|
+
async disableInterrupt(source) {
|
|
404
|
+
this._cfg &= ~SPL06Full._intBits(source) & 0xFF;
|
|
405
|
+
await this._writeCfg();
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
/** Read INT_STS (cleared on read). @returns {Promise<number>} Bitmask of SOURCE_* flags (0 = none). */
|
|
409
|
+
async pollInterrupt() { return (await this._conn.readReg(_REG_INT_STS, 1))[0] & 0x07; }
|
|
410
|
+
|
|
411
|
+
/**
|
|
412
|
+
* Subscribe to the interrupt. `callback(status)` receives the SOURCE_* bitmask. Delivery is via
|
|
413
|
+
* the INT line (`intPin` or connection.intPin, edge matching INT_HL) or a 5 ms polling timer.
|
|
414
|
+
* @param {function(number): void} callback - Called with the status bitmask.
|
|
415
|
+
* @param {import('../../connection/input_pin').InputPin|null} [intPin] - Overrides connection.intPin.
|
|
416
|
+
*/
|
|
417
|
+
async onInterrupt(callback, intPin = null) {
|
|
418
|
+
this._callback = callback;
|
|
419
|
+
const ipin = intPin ?? this._conn.intPin ?? null;
|
|
420
|
+
this._intPinUsed = ipin;
|
|
421
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
422
|
+
if (ipin) {
|
|
423
|
+
await ipin.onEdge(this._edgeHandler, (this._cfg & _CFG_INT_HL) ? 'rising' : 'falling');
|
|
424
|
+
} else {
|
|
425
|
+
this._pollTimer = setInterval(() => { this._service(); }, 5);
|
|
426
|
+
if (this._pollTimer.unref) this._pollTimer.unref();
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
/** Unsubscribe and stop delivery. */
|
|
431
|
+
async offInterrupt() {
|
|
432
|
+
if (this._intPinUsed) {
|
|
433
|
+
await this._intPinUsed.offEdge(this._edgeHandler);
|
|
434
|
+
this._intPinUsed = null;
|
|
435
|
+
}
|
|
436
|
+
if (this._pollTimer) { clearInterval(this._pollTimer); this._pollTimer = null; }
|
|
437
|
+
this._callback = null;
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
async _service() {
|
|
441
|
+
if (this._polling) return;
|
|
442
|
+
this._polling = true;
|
|
443
|
+
try {
|
|
444
|
+
const status = await this.pollInterrupt();
|
|
445
|
+
if (status && this._callback) this._callback(status);
|
|
446
|
+
} catch (e) {
|
|
447
|
+
/* a failed poll is retried on the next edge / tick */
|
|
448
|
+
} finally {
|
|
449
|
+
this._polling = false;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/** Pressure result ready (INT_STS.INT_PRS). */
|
|
455
|
+
SPL06Full.SOURCE_PRESSURE = 0x01;
|
|
456
|
+
/** Temperature result ready (INT_STS.INT_TMP). */
|
|
457
|
+
SPL06Full.SOURCE_TEMPERATURE = 0x02;
|
|
458
|
+
/** FIFO full (INT_STS.INT_FIFO_FULL). */
|
|
459
|
+
SPL06Full.SOURCE_FIFO_FULL = 0x04;
|
|
460
|
+
|
|
461
|
+
module.exports = { SPL06Minimal, SPL06Full };
|