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.
Files changed (53) hide show
  1. package/README.md +12 -0
  2. package/examples/color/isl29125/complete.js +57 -0
  3. package/examples/color/isl29125/demo.js +77 -0
  4. package/examples/color/isl29125/minimal.js +23 -0
  5. package/examples/environmental/bme688/complete.js +48 -0
  6. package/examples/environmental/bme688/demo.js +47 -0
  7. package/examples/environmental/bme688/minimal.js +21 -0
  8. package/examples/humidity/hts221/complete.js +83 -0
  9. package/examples/humidity/hts221/demo.js +94 -0
  10. package/examples/humidity/hts221/minimal.js +22 -0
  11. package/examples/humidity/htu31d/complete.js +50 -0
  12. package/examples/humidity/htu31d/demo.js +65 -0
  13. package/examples/humidity/htu31d/minimal.js +22 -0
  14. package/examples/humidity/shtc3/complete.js +48 -0
  15. package/examples/humidity/shtc3/demo.js +45 -0
  16. package/examples/humidity/shtc3/minimal.js +22 -0
  17. package/examples/led/saa1064/complete.js +51 -0
  18. package/examples/led/saa1064/demo.js +54 -0
  19. package/examples/led/saa1064/minimal.js +23 -0
  20. package/examples/light/apds9250/complete.js +71 -0
  21. package/examples/light/apds9250/demo.js +79 -0
  22. package/examples/light/apds9250/minimal.js +23 -0
  23. package/examples/light/bh1750/complete.js +46 -0
  24. package/examples/light/bh1750/demo.js +52 -0
  25. package/examples/light/bh1750/minimal.js +22 -0
  26. package/examples/light/si1132/complete.js +70 -0
  27. package/examples/light/si1132/demo.js +73 -0
  28. package/examples/light/si1132/minimal.js +22 -0
  29. package/examples/pressure/mpl3115a2/complete.js +49 -0
  30. package/examples/pressure/mpl3115a2/demo.js +48 -0
  31. package/examples/pressure/mpl3115a2/minimal.js +22 -0
  32. package/examples/pressure/ms5607/complete.js +49 -0
  33. package/examples/pressure/ms5607/demo.js +65 -0
  34. package/examples/pressure/ms5607/minimal.js +22 -0
  35. package/examples/pressure/ms5611/complete.js +49 -0
  36. package/examples/pressure/ms5611/demo.js +65 -0
  37. package/examples/pressure/ms5611/minimal.js +22 -0
  38. package/package.json +1 -1
  39. package/src/chips/color/isl29125.js +379 -0
  40. package/src/chips/environmental/bme688.js +573 -0
  41. package/src/chips/humidity/hts221.js +369 -0
  42. package/src/chips/humidity/htu31d.js +242 -0
  43. package/src/chips/humidity/shtc3.js +239 -0
  44. package/src/chips/led/saa1064.js +241 -0
  45. package/src/chips/light/apds9250.js +431 -0
  46. package/src/chips/light/bh1750.js +196 -0
  47. package/src/chips/light/si1132.js +416 -0
  48. package/src/chips/pressure/mpl3115a2.js +459 -0
  49. package/src/chips/pressure/ms5607.js +290 -0
  50. package/src/chips/pressure/ms5611.js +290 -0
  51. package/src/discovery/registry.js +12 -12
  52. package/examples/gpio/.gitkeep +0 -0
  53. 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 };
@@ -145,12 +145,12 @@ const CHIPS = [
145
145
  {"id":"vl53l1x","driver":"vl53l1x","writeSensitive":false,"aliased":false,"addresses":[41],"probe":{"register":271,"regBytes":2,"length":2,"order":"big","mask":65535,"expected":[60108]}},
146
146
  {"id":"vl6180x","driver":"vl6180x","writeSensitive":false,"aliased":false,"addresses":[41],"probe":{"register":0,"regBytes":2,"length":1,"order":"big","mask":255,"expected":[180]}},
147
147
  {"id":"veml7700","driver":null,"writeSensitive":false,"aliased":false,"addresses":[16],"probe":{"register":7,"regBytes":1,"length":2,"order":"little","mask":65535,"expected":[50305]}},
148
- {"id":"mpl3115a2","driver":null,"writeSensitive":false,"aliased":false,"addresses":[96],"probe":{"register":12,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[196]}},
148
+ {"id":"mpl3115a2","driver":"mpl3115a2","writeSensitive":false,"aliased":false,"addresses":[96],"probe":{"register":12,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[196]}},
149
149
  {"id":"sht31","driver":"sht31","writeSensitive":true,"aliased":false,"addresses":[68,69],"probe":null},
150
- {"id":"shtc3","driver":null,"writeSensitive":true,"aliased":false,"addresses":[112],"probe":{"register":61384,"regBytes":2,"length":2,"order":"big","mask":2111,"expected":[2055]}},
150
+ {"id":"shtc3","driver":"shtc3","writeSensitive":true,"aliased":false,"addresses":[112],"probe":{"register":61384,"regBytes":2,"length":2,"order":"big","mask":2111,"expected":[2055]}},
151
151
  {"id":"tsl2561","driver":null,"writeSensitive":false,"aliased":false,"addresses":[41,57,73],"probe":{"register":138,"regBytes":1,"length":1,"order":"big","mask":240,"expected":[16]}},
152
152
  {"id":"bq32000","driver":null,"writeSensitive":false,"aliased":false,"addresses":[104],"probe":null},
153
- {"id":"bme688","driver":null,"writeSensitive":false,"aliased":false,"addresses":[118,119],"probe":{"register":240,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[1]}},
153
+ {"id":"bme688","driver":"bme688","writeSensitive":false,"aliased":false,"addresses":[118,119],"probe":{"register":240,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[1]}},
154
154
  {"id":"htu21df","driver":"htu21df","writeSensitive":false,"aliased":false,"addresses":[64],"probe":null},
155
155
  {"id":"hdc1008","driver":"hdc1008","writeSensitive":false,"aliased":false,"addresses":[64,65,66,67],"probe":null},
156
156
  {"id":"hdc1080","driver":"hdc1080","writeSensitive":false,"aliased":false,"addresses":[64],"probe":{"register":255,"regBytes":1,"length":2,"order":"big","mask":65535,"expected":[4176]}},
@@ -159,7 +159,7 @@ const CHIPS = [
159
159
  {"id":"hs40xx","driver":"hs40xx","writeSensitive":true,"aliased":false,"addresses":[84],"probe":null},
160
160
  {"id":"si7021","driver":"si7021","writeSensitive":true,"aliased":false,"addresses":[64],"probe":{"register":64713,"regBytes":2,"length":4,"order":"big","mask":4278190080,"expected":[352321536]}},
161
161
  {"id":"lps22hb","driver":"lps22hb","writeSensitive":false,"aliased":false,"addresses":[92,93],"probe":{"register":15,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[177]}},
162
- {"id":"saa1064","driver":null,"writeSensitive":true,"aliased":false,"addresses":[56,57,58,59],"probe":null},
162
+ {"id":"saa1064","driver":"saa1064","writeSensitive":true,"aliased":false,"addresses":[56,57,58,59],"probe":null},
163
163
  {"id":"pcf8563","driver":null,"writeSensitive":false,"aliased":false,"addresses":[81],"probe":null},
164
164
  {"id":"mcp7940n","driver":null,"writeSensitive":false,"aliased":false,"addresses":[111],"probe":null},
165
165
  {"id":"pcf8573","driver":null,"writeSensitive":true,"aliased":false,"addresses":[104,105,106,107],"probe":null},
@@ -167,14 +167,14 @@ const CHIPS = [
167
167
  {"id":"max17048","driver":"max17048","writeSensitive":false,"aliased":false,"addresses":[54],"probe":{"register":8,"regBytes":1,"length":2,"order":"big","mask":65520,"expected":[16]}},
168
168
  {"id":"is31fl3731","driver":"is31fl3731","writeSensitive":false,"aliased":false,"addresses":[116,117,118,119],"probe":null},
169
169
  {"id":"as7262","driver":"as7262","writeSensitive":true,"aliased":false,"addresses":[73],"probe":null},
170
- {"id":"hts221","driver":null,"writeSensitive":false,"aliased":false,"addresses":[95],"probe":{"register":15,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[188]}},
171
- {"id":"htu31d","driver":null,"writeSensitive":true,"aliased":false,"addresses":[64,65],"probe":null},
172
- {"id":"bh1750","driver":null,"writeSensitive":true,"aliased":false,"addresses":[35,92],"probe":null},
173
- {"id":"ms5607","driver":null,"writeSensitive":true,"aliased":false,"addresses":[118,119],"probe":null},
174
- {"id":"ms5611","driver":null,"writeSensitive":true,"aliased":false,"addresses":[118,119],"probe":null},
170
+ {"id":"hts221","driver":"hts221","writeSensitive":false,"aliased":false,"addresses":[95],"probe":{"register":15,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[188]}},
171
+ {"id":"htu31d","driver":"htu31d","writeSensitive":true,"aliased":false,"addresses":[64,65],"probe":null},
172
+ {"id":"bh1750","driver":"bh1750","writeSensitive":true,"aliased":false,"addresses":[35,92],"probe":null},
173
+ {"id":"ms5607","driver":"ms5607","writeSensitive":true,"aliased":false,"addresses":[118,119],"probe":null},
174
+ {"id":"ms5611","driver":"ms5611","writeSensitive":true,"aliased":false,"addresses":[118,119],"probe":null},
175
175
  {"id":"pca9685","driver":"pca9685","writeSensitive":false,"aliased":false,"addresses":[64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79,80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99,100,101,102,103,104,105,106,107,108,109,110,111,113,114,115,116,117,118,119],"probe":null},
176
176
  {"id":"spl06","driver":null,"writeSensitive":false,"aliased":false,"addresses":[118,119],"probe":{"register":13,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[16]}},
177
- {"id":"si1132","driver":null,"writeSensitive":false,"aliased":false,"addresses":[96],"probe":{"register":0,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[50]}},
177
+ {"id":"si1132","driver":"si1132","writeSensitive":false,"aliased":false,"addresses":[96],"probe":{"register":0,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[50]}},
178
178
  {"id":"si1133","driver":null,"writeSensitive":false,"aliased":false,"addresses":[82,85],"probe":{"register":0,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[51]}},
179
179
  {"id":"si1145","driver":null,"writeSensitive":false,"aliased":false,"addresses":[96],"probe":{"register":0,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[69,70,71]}},
180
180
  {"id":"tsl2591","driver":null,"writeSensitive":true,"aliased":false,"addresses":[41],"probe":{"register":178,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[80]}},
@@ -182,7 +182,7 @@ const CHIPS = [
182
182
  {"id":"rrh46410","driver":null,"writeSensitive":true,"aliased":false,"addresses":[56],"probe":null},
183
183
  {"id":"pn532","driver":null,"writeSensitive":true,"aliased":false,"addresses":[36],"probe":null},
184
184
  {"id":"st25dv","driver":null,"writeSensitive":false,"aliased":false,"addresses":[87],"probe":{"register":23,"regBytes":2,"length":1,"order":"big","mask":255,"expected":[36,38]}},
185
- {"id":"apds9250","driver":null,"writeSensitive":false,"aliased":false,"addresses":[82],"probe":{"register":6,"regBytes":1,"length":1,"order":"big","mask":240,"expected":[176]}},
185
+ {"id":"apds9250","driver":"apds9250","writeSensitive":false,"aliased":false,"addresses":[82],"probe":{"register":6,"regBytes":1,"length":1,"order":"big","mask":240,"expected":[176]}},
186
186
  {"id":"rrh62000","driver":null,"writeSensitive":true,"aliased":false,"addresses":[105],"probe":null},
187
187
  {"id":"ds1307","driver":null,"writeSensitive":false,"aliased":false,"addresses":[104],"probe":null},
188
188
  {"id":"scd40","driver":null,"writeSensitive":true,"aliased":false,"addresses":[98],"probe":null},
@@ -192,7 +192,7 @@ const CHIPS = [
192
192
  {"id":"max31341","driver":null,"writeSensitive":false,"aliased":false,"addresses":[105],"probe":{"register":89,"regBytes":1,"length":1,"order":"big","mask":240,"expected":[16]}},
193
193
  {"id":"veml6075","driver":null,"writeSensitive":false,"aliased":false,"addresses":[16],"probe":{"register":12,"regBytes":1,"length":2,"order":"little","mask":4095,"expected":[38]}},
194
194
  {"id":"sgp40","driver":null,"writeSensitive":true,"aliased":false,"addresses":[89],"probe":null},
195
- {"id":"isl29125","driver":null,"writeSensitive":false,"aliased":false,"addresses":[68],"probe":{"register":0,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[125]}},
195
+ {"id":"isl29125","driver":"isl29125","writeSensitive":false,"aliased":false,"addresses":[68],"probe":{"register":0,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[125]}},
196
196
  {"id":"ccs811","driver":null,"writeSensitive":false,"aliased":false,"addresses":[90,91],"probe":{"register":32,"regBytes":1,"length":1,"order":"big","mask":255,"expected":[129]}},
197
197
  {"id":"vcnl4040","driver":null,"writeSensitive":false,"aliased":false,"addresses":[96],"probe":{"register":12,"regBytes":1,"length":2,"order":"little","mask":65535,"expected":[390]}},
198
198
  {"id":"veml6070","driver":null,"writeSensitive":true,"aliased":false,"addresses":[56,57],"probe":null},
File without changes
File without changes