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,290 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _CMD_RESET = 0x1E;
|
|
4
|
+
const _CMD_CONVERT_D1 = 0x40;
|
|
5
|
+
const _CMD_CONVERT_D2 = 0x50;
|
|
6
|
+
const _CMD_ADC_READ = 0x00;
|
|
7
|
+
const _CMD_PROM_READ = 0xA0;
|
|
8
|
+
const _OSR_VALUES = [256, 512, 1024, 2048, 4096];
|
|
9
|
+
// maximum conversion time per OSR (s), datasheet
|
|
10
|
+
const _T_CONV = [0.00060, 0.00117, 0.00228, 0.00454, 0.00904];
|
|
11
|
+
const _MARGIN_MS = 1;
|
|
12
|
+
|
|
13
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* CRC-4 of the MS5607 PROM (application note AN520): polynomial x^4 + x^3 + 1 over the
|
|
17
|
+
* 16 bytes of the eight PROM words, MSB first, with the low byte of word 7 treated as zero.
|
|
18
|
+
* @param {number[]} words - The eight 16-bit PROM words.
|
|
19
|
+
* @returns {number} The 4-bit CRC.
|
|
20
|
+
*/
|
|
21
|
+
function crc4(words) {
|
|
22
|
+
const prom = words.slice();
|
|
23
|
+
prom[7] &= 0xFF00;
|
|
24
|
+
let rem = 0;
|
|
25
|
+
for (let cnt = 0; cnt < 16; cnt++) {
|
|
26
|
+
rem ^= (cnt & 1) ? (prom[cnt >> 1] & 0x00FF) : (prom[cnt >> 1] >> 8);
|
|
27
|
+
for (let b = 0; b < 8; b++) rem = (rem & 0x8000) ? ((rem << 1) ^ 0x3000) & 0xFFFF : (rem << 1) & 0xFFFF;
|
|
28
|
+
}
|
|
29
|
+
return (rem >> 12) & 0x0F;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Integer division truncating toward zero (BigInt division already does).
|
|
33
|
+
const _big = BigInt;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* MS5607 barometric pressure sensor — minimal interface.
|
|
37
|
+
*
|
|
38
|
+
* `init()` resets the chip, reads the eight PROM words and verifies the CRC-4. Each `read()`
|
|
39
|
+
* converts the temperature (D2) and then the pressure (D1) at oversampling ratio 4096 (waiting
|
|
40
|
+
* 10 ms each) and applies the first- and second-order compensation. Pressure is in pascals
|
|
41
|
+
* (1 Pa = 0.01 mbar, the chip's native unit).
|
|
42
|
+
*
|
|
43
|
+
* The device is command driven (no register pointer), so a plain I²C connection is used.
|
|
44
|
+
* The constructor does not touch the bus.
|
|
45
|
+
*/
|
|
46
|
+
class MS5607Minimal {
|
|
47
|
+
/**
|
|
48
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (address 0x77, or 0x76 with CSB high).
|
|
49
|
+
*/
|
|
50
|
+
constructor(connection) {
|
|
51
|
+
this._conn = connection;
|
|
52
|
+
this._osrIndex = 4;
|
|
53
|
+
this._prom = [0, 0, 0, 0, 0, 0, 0, 0];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async _readPromWords() {
|
|
57
|
+
const words = [];
|
|
58
|
+
for (let n = 0; n < 8; n++) {
|
|
59
|
+
await this._conn.write(Buffer.from([_CMD_PROM_READ + 2 * n]));
|
|
60
|
+
const data = Buffer.from(await this._conn.read(2));
|
|
61
|
+
words.push((data[0] << 8) | data[1]);
|
|
62
|
+
}
|
|
63
|
+
return words;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async _loadProm() {
|
|
67
|
+
const words = await this._readPromWords();
|
|
68
|
+
if (crc4(words) !== (words[7] & 0x0F)) throw new Error('MS5607 PROM CRC mismatch');
|
|
69
|
+
this._prom = words;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async _readAdc() {
|
|
73
|
+
await this._conn.write(Buffer.from([_CMD_ADC_READ]));
|
|
74
|
+
const data = Buffer.from(await this._conn.read(3));
|
|
75
|
+
const value = (data[0] << 16) | (data[1] << 8) | data[2];
|
|
76
|
+
if (value === 0) throw new Error('MS5607 ADC returned 0 (conversion not finished or not started)');
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async _convert(kind) {
|
|
81
|
+
await this._conn.write(Buffer.from([(kind ? _CMD_CONVERT_D2 : _CMD_CONVERT_D1) | (this._osrIndex << 1)]));
|
|
82
|
+
await _sleep(Math.ceil(_T_CONV[this._osrIndex] * 1000) + _MARGIN_MS);
|
|
83
|
+
return this._readAdc();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
_temperatureTerms(d2) {
|
|
87
|
+
const dt = _big(d2) - _big(this._prom[5]) * 256n;
|
|
88
|
+
const temp = 2000n + (dt * _big(this._prom[6])) / 8388608n;
|
|
89
|
+
return [dt, temp];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
_secondOrder(dt, temp) {
|
|
93
|
+
let t2 = 0n, off2 = 0n, sens2 = 0n;
|
|
94
|
+
if (temp < 2000n) {
|
|
95
|
+
const d = temp - 2000n;
|
|
96
|
+
t2 = (dt * dt) / 2147483648n;
|
|
97
|
+
off2 = (61n * d * d) / 16n;
|
|
98
|
+
sens2 = 2n * d * d;
|
|
99
|
+
if (temp < -1500n) {
|
|
100
|
+
const e = temp + 1500n;
|
|
101
|
+
off2 += 15n * e * e;
|
|
102
|
+
sens2 += 8n * e * e;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return [t2, off2, sens2];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
_compensate(d1, d2, secondOrder = true) {
|
|
109
|
+
const c = this._prom;
|
|
110
|
+
const [dt, t0] = this._temperatureTerms(d2);
|
|
111
|
+
let temp = t0;
|
|
112
|
+
let off = _big(c[2]) * 131072n + (_big(c[4]) * dt) / 64n;
|
|
113
|
+
let sens = _big(c[1]) * 65536n + (_big(c[3]) * dt) / 128n;
|
|
114
|
+
if (secondOrder) {
|
|
115
|
+
const [t2, off2, sens2] = this._secondOrder(dt, temp);
|
|
116
|
+
temp -= t2;
|
|
117
|
+
off -= off2;
|
|
118
|
+
sens -= sens2;
|
|
119
|
+
}
|
|
120
|
+
const p = ((_big(d1) * sens) / 2097152n - off) / 32768n;
|
|
121
|
+
return { temperature_c: Number(temp) / 100.0, pressure_pa: Number(p) };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Reset (0x1E), wait 3 ms, read the PROM and verify its CRC-4.
|
|
126
|
+
* @throws {Error} If the PROM CRC does not match.
|
|
127
|
+
*/
|
|
128
|
+
async init() {
|
|
129
|
+
await this._conn.write(Buffer.from([_CMD_RESET]));
|
|
130
|
+
await _sleep(3);
|
|
131
|
+
await this._loadProm();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Measure temperature and pressure (OSR 4096, second-order compensation).
|
|
136
|
+
* @returns {Promise<{temperature_c: number, pressure_pa: number}>} °C and Pa
|
|
137
|
+
*/
|
|
138
|
+
async read() {
|
|
139
|
+
const d2 = await this._convert(1);
|
|
140
|
+
const d1 = await this._convert(0);
|
|
141
|
+
return this._compensate(d1, d2);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* MS5607 full interface — single values, raw codes, oversampling, non-blocking conversion, PROM access, altitude.
|
|
147
|
+
*
|
|
148
|
+
* Extends MS5607Minimal with single values, raw codes, the oversampling ratio, explicit
|
|
149
|
+
* conversion start / ADC read, pure compensation, reset, PROM and coefficient access and the
|
|
150
|
+
* barometric altitude.
|
|
151
|
+
*/
|
|
152
|
+
class MS5607Full extends MS5607Minimal {
|
|
153
|
+
/**
|
|
154
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (address 0x77, or 0x76 with CSB high).
|
|
155
|
+
*/
|
|
156
|
+
constructor(connection) {
|
|
157
|
+
super(connection);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Convert D2 and D1 at the current OSR and return the raw ADC codes.
|
|
162
|
+
* @returns {Promise<number[]>} [d1, d2], both 24-bit
|
|
163
|
+
*/
|
|
164
|
+
async readRaw() {
|
|
165
|
+
const d2 = await this._convert(1);
|
|
166
|
+
const d1 = await this._convert(0);
|
|
167
|
+
return [d1, d2];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Measure and return the compensated pressure.
|
|
172
|
+
* @returns {Promise<number>} Pressure in pascals
|
|
173
|
+
*/
|
|
174
|
+
async readPressure() {
|
|
175
|
+
return (await this.read()).pressure_pa;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Convert D2 only and return the temperature (with the low-temperature correction).
|
|
180
|
+
* @returns {Promise<number>} Temperature in degrees Celsius
|
|
181
|
+
*/
|
|
182
|
+
async readTemperature() {
|
|
183
|
+
const [dt, temp] = this._temperatureTerms(await this._convert(1));
|
|
184
|
+
return Number(temp - this._secondOrder(dt, temp)[0]) / 100.0;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Set the oversampling ratio used for both conversions.
|
|
189
|
+
* @param {number} osr - 256, 512, 1024, 2048 or 4096.
|
|
190
|
+
* @throws {Error} For any other value.
|
|
191
|
+
*/
|
|
192
|
+
setOversampling(osr) {
|
|
193
|
+
const i = _OSR_VALUES.indexOf(osr);
|
|
194
|
+
if (i < 0) throw new Error('OSR must be 256, 512, 1024, 2048 or 4096');
|
|
195
|
+
this._osrIndex = i;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** @returns {number} The current oversampling ratio (256 ... 4096). */
|
|
199
|
+
getOversampling() {
|
|
200
|
+
return _OSR_VALUES[this._osrIndex];
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Maximum conversion time for an oversampling ratio.
|
|
205
|
+
* @param {number} [osr] - 256 ... 4096; the current ratio if omitted.
|
|
206
|
+
* @returns {number} Seconds (0.60 / 1.17 / 2.28 / 4.54 / 9.04 ms)
|
|
207
|
+
* @throws {Error} For an invalid ratio.
|
|
208
|
+
*/
|
|
209
|
+
conversionTime(osr) {
|
|
210
|
+
if (osr === undefined || osr === null) return _T_CONV[this._osrIndex];
|
|
211
|
+
const i = _OSR_VALUES.indexOf(osr);
|
|
212
|
+
if (i < 0) throw new Error('OSR must be 256, 512, 1024, 2048 or 4096');
|
|
213
|
+
return _T_CONV[i];
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Write a conversion command without waiting (non-blocking use); wait at least
|
|
218
|
+
* `conversionTime()` before `readAdc()`.
|
|
219
|
+
* @param {number} kind - MS5607Full.PRESSURE (D1) or MS5607Full.TEMPERATURE (D2).
|
|
220
|
+
* @param {number} [osr] - Oversampling ratio; the current one if omitted.
|
|
221
|
+
*/
|
|
222
|
+
async startConversion(kind, osr) {
|
|
223
|
+
let i = this._osrIndex;
|
|
224
|
+
if (osr !== undefined && osr !== null) {
|
|
225
|
+
i = _OSR_VALUES.indexOf(osr);
|
|
226
|
+
if (i < 0) throw new Error('OSR must be 256, 512, 1024, 2048 or 4096');
|
|
227
|
+
}
|
|
228
|
+
await this._conn.write(Buffer.from([(kind === MS5607Full.TEMPERATURE ? _CMD_CONVERT_D2 : _CMD_CONVERT_D1) | (i << 1)]));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* ADC read command plus 3 bytes: the result of the last conversion. Only valid after the
|
|
233
|
+
* conversion time; a read during the conversion or a repeated read returns 0.
|
|
234
|
+
* @returns {Promise<number>} 24-bit result
|
|
235
|
+
* @throws {Error} If the chip returns 0.
|
|
236
|
+
*/
|
|
237
|
+
async readAdc() {
|
|
238
|
+
return this._readAdc();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Pure computation with the stored coefficients (no bus access).
|
|
243
|
+
* @param {number} d1 - 24-bit raw pressure.
|
|
244
|
+
* @param {number} d2 - 24-bit raw temperature.
|
|
245
|
+
* @param {boolean} [secondOrder=true] - false for the first-order result only.
|
|
246
|
+
* @returns {{temperature_c: number, pressure_pa: number}}
|
|
247
|
+
*/
|
|
248
|
+
compensate(d1, d2, secondOrder = true) {
|
|
249
|
+
return this._compensate(d1, d2, secondOrder);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Reset (0x1E), wait 3 ms and reload and verify the PROM. */
|
|
253
|
+
async reset() {
|
|
254
|
+
await this.init();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** @returns {Promise<number[]>} The eight PROM words read from the chip. */
|
|
258
|
+
async readProm() {
|
|
259
|
+
return this._readPromWords();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** @returns {number[]} The calibration coefficients [c1, ..., c6]. */
|
|
263
|
+
coefficients() {
|
|
264
|
+
return this._prom.slice(1, 7);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** @returns {boolean} True if the CRC-4 of the PROM words read at init matches (no bus access). */
|
|
268
|
+
promCrcOk() {
|
|
269
|
+
return crc4(this._prom) === (this._prom[7] & 0x0F);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** @returns {number} The serial code (PROM word 7, bits 15:4). */
|
|
273
|
+
serialCode() {
|
|
274
|
+
return (this._prom[7] >> 4) & 0x0FFF;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Altitude from one pressure reading (international barometric formula).
|
|
279
|
+
* @param {number} [seaLevelPa=101325] - Reference sea-level pressure in pascals.
|
|
280
|
+
* @returns {Promise<number>} Altitude in metres
|
|
281
|
+
*/
|
|
282
|
+
async altitude(seaLevelPa = 101325.0) {
|
|
283
|
+
return 44330.0 * (1.0 - Math.pow((await this.readPressure()) / seaLevelPa, 1.0 / 5.255));
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
MS5607Full.PRESSURE = 0;
|
|
288
|
+
MS5607Full.TEMPERATURE = 1;
|
|
289
|
+
|
|
290
|
+
module.exports = { MS5607Minimal, MS5607Full, crc4 };
|
|
@@ -0,0 +1,290 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _CMD_RESET = 0x1E;
|
|
4
|
+
const _CMD_CONVERT_D1 = 0x40;
|
|
5
|
+
const _CMD_CONVERT_D2 = 0x50;
|
|
6
|
+
const _CMD_ADC_READ = 0x00;
|
|
7
|
+
const _CMD_PROM_READ = 0xA0;
|
|
8
|
+
const _OSR_VALUES = [256, 512, 1024, 2048, 4096];
|
|
9
|
+
// maximum conversion time per OSR (s), datasheet
|
|
10
|
+
const _T_CONV = [0.00060, 0.00117, 0.00228, 0.00454, 0.00904];
|
|
11
|
+
const _MARGIN_MS = 1;
|
|
12
|
+
|
|
13
|
+
const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* CRC-4 of the MS5611 PROM (application note AN520): polynomial x^4 + x^3 + 1 over the
|
|
17
|
+
* 16 bytes of the eight PROM words, MSB first, with the low byte of word 7 treated as zero.
|
|
18
|
+
* @param {number[]} words - The eight 16-bit PROM words.
|
|
19
|
+
* @returns {number} The 4-bit CRC.
|
|
20
|
+
*/
|
|
21
|
+
function crc4(words) {
|
|
22
|
+
const prom = words.slice();
|
|
23
|
+
prom[7] &= 0xFF00;
|
|
24
|
+
let rem = 0;
|
|
25
|
+
for (let cnt = 0; cnt < 16; cnt++) {
|
|
26
|
+
rem ^= (cnt & 1) ? (prom[cnt >> 1] & 0x00FF) : (prom[cnt >> 1] >> 8);
|
|
27
|
+
for (let b = 0; b < 8; b++) rem = (rem & 0x8000) ? ((rem << 1) ^ 0x3000) & 0xFFFF : (rem << 1) & 0xFFFF;
|
|
28
|
+
}
|
|
29
|
+
return (rem >> 12) & 0x0F;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Integer division truncating toward zero (BigInt division already does).
|
|
33
|
+
const _big = BigInt;
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* MS5611 barometric pressure sensor — minimal interface.
|
|
37
|
+
*
|
|
38
|
+
* `init()` resets the chip, reads the eight PROM words and verifies the CRC-4. Each `read()`
|
|
39
|
+
* converts the temperature (D2) and then the pressure (D1) at oversampling ratio 4096 (waiting
|
|
40
|
+
* 10 ms each) and applies the first- and second-order compensation. Pressure is in pascals
|
|
41
|
+
* (1 Pa = 0.01 mbar, the chip's native unit).
|
|
42
|
+
*
|
|
43
|
+
* The device is command driven (no register pointer), so a plain I²C connection is used.
|
|
44
|
+
* The constructor does not touch the bus.
|
|
45
|
+
*/
|
|
46
|
+
class MS5611Minimal {
|
|
47
|
+
/**
|
|
48
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (address 0x77, or 0x76 with CSB high).
|
|
49
|
+
*/
|
|
50
|
+
constructor(connection) {
|
|
51
|
+
this._conn = connection;
|
|
52
|
+
this._osrIndex = 4;
|
|
53
|
+
this._prom = [0, 0, 0, 0, 0, 0, 0, 0];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async _readPromWords() {
|
|
57
|
+
const words = [];
|
|
58
|
+
for (let n = 0; n < 8; n++) {
|
|
59
|
+
await this._conn.write(Buffer.from([_CMD_PROM_READ + 2 * n]));
|
|
60
|
+
const data = Buffer.from(await this._conn.read(2));
|
|
61
|
+
words.push((data[0] << 8) | data[1]);
|
|
62
|
+
}
|
|
63
|
+
return words;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
async _loadProm() {
|
|
67
|
+
const words = await this._readPromWords();
|
|
68
|
+
if (crc4(words) !== (words[7] & 0x0F)) throw new Error('MS5611 PROM CRC mismatch');
|
|
69
|
+
this._prom = words;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async _readAdc() {
|
|
73
|
+
await this._conn.write(Buffer.from([_CMD_ADC_READ]));
|
|
74
|
+
const data = Buffer.from(await this._conn.read(3));
|
|
75
|
+
const value = (data[0] << 16) | (data[1] << 8) | data[2];
|
|
76
|
+
if (value === 0) throw new Error('MS5611 ADC returned 0 (conversion not finished or not started)');
|
|
77
|
+
return value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async _convert(kind) {
|
|
81
|
+
await this._conn.write(Buffer.from([(kind ? _CMD_CONVERT_D2 : _CMD_CONVERT_D1) | (this._osrIndex << 1)]));
|
|
82
|
+
await _sleep(Math.ceil(_T_CONV[this._osrIndex] * 1000) + _MARGIN_MS);
|
|
83
|
+
return this._readAdc();
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
_temperatureTerms(d2) {
|
|
87
|
+
const dt = _big(d2) - _big(this._prom[5]) * 256n;
|
|
88
|
+
const temp = 2000n + (dt * _big(this._prom[6])) / 8388608n;
|
|
89
|
+
return [dt, temp];
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
_secondOrder(dt, temp) {
|
|
93
|
+
let t2 = 0n, off2 = 0n, sens2 = 0n;
|
|
94
|
+
if (temp < 2000n) {
|
|
95
|
+
const d = temp - 2000n;
|
|
96
|
+
t2 = (dt * dt) / 2147483648n;
|
|
97
|
+
off2 = (5n * d * d) / 2n;
|
|
98
|
+
sens2 = (5n * d * d) / 4n;
|
|
99
|
+
if (temp < -1500n) {
|
|
100
|
+
const e = temp + 1500n;
|
|
101
|
+
off2 += 7n * e * e;
|
|
102
|
+
sens2 += (11n * e * e) / 2n;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return [t2, off2, sens2];
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
_compensate(d1, d2, secondOrder = true) {
|
|
109
|
+
const c = this._prom;
|
|
110
|
+
const [dt, t0] = this._temperatureTerms(d2);
|
|
111
|
+
let temp = t0;
|
|
112
|
+
let off = _big(c[2]) * 65536n + (_big(c[4]) * dt) / 128n;
|
|
113
|
+
let sens = _big(c[1]) * 32768n + (_big(c[3]) * dt) / 256n;
|
|
114
|
+
if (secondOrder) {
|
|
115
|
+
const [t2, off2, sens2] = this._secondOrder(dt, temp);
|
|
116
|
+
temp -= t2;
|
|
117
|
+
off -= off2;
|
|
118
|
+
sens -= sens2;
|
|
119
|
+
}
|
|
120
|
+
const p = ((_big(d1) * sens) / 2097152n - off) / 32768n;
|
|
121
|
+
return { temperature_c: Number(temp) / 100.0, pressure_pa: Number(p) };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Reset (0x1E), wait 3 ms, read the PROM and verify its CRC-4.
|
|
126
|
+
* @throws {Error} If the PROM CRC does not match.
|
|
127
|
+
*/
|
|
128
|
+
async init() {
|
|
129
|
+
await this._conn.write(Buffer.from([_CMD_RESET]));
|
|
130
|
+
await _sleep(3);
|
|
131
|
+
await this._loadProm();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Measure temperature and pressure (OSR 4096, second-order compensation).
|
|
136
|
+
* @returns {Promise<{temperature_c: number, pressure_pa: number}>} °C and Pa
|
|
137
|
+
*/
|
|
138
|
+
async read() {
|
|
139
|
+
const d2 = await this._convert(1);
|
|
140
|
+
const d1 = await this._convert(0);
|
|
141
|
+
return this._compensate(d1, d2);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* MS5611 full interface — single values, raw codes, oversampling, non-blocking conversion, PROM access, altitude.
|
|
147
|
+
*
|
|
148
|
+
* Extends MS5611Minimal with single values, raw codes, the oversampling ratio, explicit
|
|
149
|
+
* conversion start / ADC read, pure compensation, reset, PROM and coefficient access and the
|
|
150
|
+
* barometric altitude.
|
|
151
|
+
*/
|
|
152
|
+
class MS5611Full extends MS5611Minimal {
|
|
153
|
+
/**
|
|
154
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (address 0x77, or 0x76 with CSB high).
|
|
155
|
+
*/
|
|
156
|
+
constructor(connection) {
|
|
157
|
+
super(connection);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Convert D2 and D1 at the current OSR and return the raw ADC codes.
|
|
162
|
+
* @returns {Promise<number[]>} [d1, d2], both 24-bit
|
|
163
|
+
*/
|
|
164
|
+
async readRaw() {
|
|
165
|
+
const d2 = await this._convert(1);
|
|
166
|
+
const d1 = await this._convert(0);
|
|
167
|
+
return [d1, d2];
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Measure and return the compensated pressure.
|
|
172
|
+
* @returns {Promise<number>} Pressure in pascals
|
|
173
|
+
*/
|
|
174
|
+
async readPressure() {
|
|
175
|
+
return (await this.read()).pressure_pa;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
/**
|
|
179
|
+
* Convert D2 only and return the temperature (with the low-temperature correction).
|
|
180
|
+
* @returns {Promise<number>} Temperature in degrees Celsius
|
|
181
|
+
*/
|
|
182
|
+
async readTemperature() {
|
|
183
|
+
const [dt, temp] = this._temperatureTerms(await this._convert(1));
|
|
184
|
+
return Number(temp - this._secondOrder(dt, temp)[0]) / 100.0;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Set the oversampling ratio used for both conversions.
|
|
189
|
+
* @param {number} osr - 256, 512, 1024, 2048 or 4096.
|
|
190
|
+
* @throws {Error} For any other value.
|
|
191
|
+
*/
|
|
192
|
+
setOversampling(osr) {
|
|
193
|
+
const i = _OSR_VALUES.indexOf(osr);
|
|
194
|
+
if (i < 0) throw new Error('OSR must be 256, 512, 1024, 2048 or 4096');
|
|
195
|
+
this._osrIndex = i;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** @returns {number} The current oversampling ratio (256 ... 4096). */
|
|
199
|
+
getOversampling() {
|
|
200
|
+
return _OSR_VALUES[this._osrIndex];
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Maximum conversion time for an oversampling ratio.
|
|
205
|
+
* @param {number} [osr] - 256 ... 4096; the current ratio if omitted.
|
|
206
|
+
* @returns {number} Seconds (0.60 / 1.17 / 2.28 / 4.54 / 9.04 ms)
|
|
207
|
+
* @throws {Error} For an invalid ratio.
|
|
208
|
+
*/
|
|
209
|
+
conversionTime(osr) {
|
|
210
|
+
if (osr === undefined || osr === null) return _T_CONV[this._osrIndex];
|
|
211
|
+
const i = _OSR_VALUES.indexOf(osr);
|
|
212
|
+
if (i < 0) throw new Error('OSR must be 256, 512, 1024, 2048 or 4096');
|
|
213
|
+
return _T_CONV[i];
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Write a conversion command without waiting (non-blocking use); wait at least
|
|
218
|
+
* `conversionTime()` before `readAdc()`.
|
|
219
|
+
* @param {number} kind - MS5611Full.PRESSURE (D1) or MS5611Full.TEMPERATURE (D2).
|
|
220
|
+
* @param {number} [osr] - Oversampling ratio; the current one if omitted.
|
|
221
|
+
*/
|
|
222
|
+
async startConversion(kind, osr) {
|
|
223
|
+
let i = this._osrIndex;
|
|
224
|
+
if (osr !== undefined && osr !== null) {
|
|
225
|
+
i = _OSR_VALUES.indexOf(osr);
|
|
226
|
+
if (i < 0) throw new Error('OSR must be 256, 512, 1024, 2048 or 4096');
|
|
227
|
+
}
|
|
228
|
+
await this._conn.write(Buffer.from([(kind === MS5611Full.TEMPERATURE ? _CMD_CONVERT_D2 : _CMD_CONVERT_D1) | (i << 1)]));
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* ADC read command plus 3 bytes: the result of the last conversion. Only valid after the
|
|
233
|
+
* conversion time; a read during the conversion or a repeated read returns 0.
|
|
234
|
+
* @returns {Promise<number>} 24-bit result
|
|
235
|
+
* @throws {Error} If the chip returns 0.
|
|
236
|
+
*/
|
|
237
|
+
async readAdc() {
|
|
238
|
+
return this._readAdc();
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
/**
|
|
242
|
+
* Pure computation with the stored coefficients (no bus access).
|
|
243
|
+
* @param {number} d1 - 24-bit raw pressure.
|
|
244
|
+
* @param {number} d2 - 24-bit raw temperature.
|
|
245
|
+
* @param {boolean} [secondOrder=true] - false for the first-order result only.
|
|
246
|
+
* @returns {{temperature_c: number, pressure_pa: number}}
|
|
247
|
+
*/
|
|
248
|
+
compensate(d1, d2, secondOrder = true) {
|
|
249
|
+
return this._compensate(d1, d2, secondOrder);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** Reset (0x1E), wait 3 ms and reload and verify the PROM. */
|
|
253
|
+
async reset() {
|
|
254
|
+
await this.init();
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** @returns {Promise<number[]>} The eight PROM words read from the chip. */
|
|
258
|
+
async readProm() {
|
|
259
|
+
return this._readPromWords();
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/** @returns {number[]} The calibration coefficients [c1, ..., c6]. */
|
|
263
|
+
coefficients() {
|
|
264
|
+
return this._prom.slice(1, 7);
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
/** @returns {boolean} True if the CRC-4 of the PROM words read at init matches (no bus access). */
|
|
268
|
+
promCrcOk() {
|
|
269
|
+
return crc4(this._prom) === (this._prom[7] & 0x0F);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** @returns {number} The serial code (PROM word 7, bits 15:4). */
|
|
273
|
+
serialCode() {
|
|
274
|
+
return (this._prom[7] >> 4) & 0x0FFF;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* Altitude from one pressure reading (international barometric formula).
|
|
279
|
+
* @param {number} [seaLevelPa=101325] - Reference sea-level pressure in pascals.
|
|
280
|
+
* @returns {Promise<number>} Altitude in metres
|
|
281
|
+
*/
|
|
282
|
+
async altitude(seaLevelPa = 101325.0) {
|
|
283
|
+
return 44330.0 * (1.0 - Math.pow((await this.readPressure()) / seaLevelPa, 1.0 / 5.255));
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
MS5611Full.PRESSURE = 0;
|
|
288
|
+
MS5611Full.TEMPERATURE = 1;
|
|
289
|
+
|
|
290
|
+
module.exports = { MS5611Minimal, MS5611Full, crc4 };
|