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.
Files changed (79) hide show
  1. package/README.md +18 -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/gas/ccs811/complete.js +87 -0
  9. package/examples/gas/ccs811/demo.js +108 -0
  10. package/examples/gas/ccs811/minimal.js +20 -0
  11. package/examples/gas/rrh46410/complete.js +78 -0
  12. package/examples/gas/rrh46410/demo.js +86 -0
  13. package/examples/gas/rrh46410/minimal.js +21 -0
  14. package/examples/humidity/hts221/complete.js +83 -0
  15. package/examples/humidity/hts221/demo.js +94 -0
  16. package/examples/humidity/hts221/minimal.js +22 -0
  17. package/examples/humidity/htu31d/complete.js +50 -0
  18. package/examples/humidity/htu31d/demo.js +65 -0
  19. package/examples/humidity/htu31d/minimal.js +22 -0
  20. package/examples/humidity/shtc3/complete.js +48 -0
  21. package/examples/humidity/shtc3/demo.js +45 -0
  22. package/examples/humidity/shtc3/minimal.js +22 -0
  23. package/examples/led/saa1064/complete.js +51 -0
  24. package/examples/led/saa1064/demo.js +54 -0
  25. package/examples/led/saa1064/minimal.js +23 -0
  26. package/examples/light/apds9250/complete.js +71 -0
  27. package/examples/light/apds9250/demo.js +79 -0
  28. package/examples/light/apds9250/minimal.js +23 -0
  29. package/examples/light/bh1750/complete.js +46 -0
  30. package/examples/light/bh1750/demo.js +52 -0
  31. package/examples/light/bh1750/minimal.js +22 -0
  32. package/examples/light/si1132/complete.js +70 -0
  33. package/examples/light/si1132/demo.js +73 -0
  34. package/examples/light/si1132/minimal.js +22 -0
  35. package/examples/light/si1133/complete.js +89 -0
  36. package/examples/light/si1133/demo.js +93 -0
  37. package/examples/light/si1133/minimal.js +22 -0
  38. package/examples/light/si1145/complete.js +93 -0
  39. package/examples/light/si1145/demo.js +98 -0
  40. package/examples/light/si1145/minimal.js +22 -0
  41. package/examples/pressure/mpl3115a2/complete.js +49 -0
  42. package/examples/pressure/mpl3115a2/demo.js +48 -0
  43. package/examples/pressure/mpl3115a2/minimal.js +22 -0
  44. package/examples/pressure/ms5607/complete.js +49 -0
  45. package/examples/pressure/ms5607/demo.js +65 -0
  46. package/examples/pressure/ms5607/minimal.js +22 -0
  47. package/examples/pressure/ms5611/complete.js +49 -0
  48. package/examples/pressure/ms5611/demo.js +65 -0
  49. package/examples/pressure/ms5611/minimal.js +22 -0
  50. package/examples/pressure/spl06/complete.js +81 -0
  51. package/examples/pressure/spl06/demo.js +106 -0
  52. package/examples/pressure/spl06/minimal.js +22 -0
  53. package/examples/rfid/pn532/complete.js +99 -0
  54. package/examples/rfid/pn532/demo.js +121 -0
  55. package/examples/rfid/pn532/minimal.js +22 -0
  56. package/package.json +1 -1
  57. package/src/chips/color/isl29125.js +379 -0
  58. package/src/chips/environmental/bme688.js +573 -0
  59. package/src/chips/gas/ccs811.js +400 -0
  60. package/src/chips/gas/rrh46410.js +534 -0
  61. package/src/chips/humidity/hts221.js +369 -0
  62. package/src/chips/humidity/htu31d.js +242 -0
  63. package/src/chips/humidity/shtc3.js +239 -0
  64. package/src/chips/led/saa1064.js +241 -0
  65. package/src/chips/light/apds9250.js +431 -0
  66. package/src/chips/light/bh1750.js +196 -0
  67. package/src/chips/light/si1132.js +416 -0
  68. package/src/chips/light/si1133.js +583 -0
  69. package/src/chips/light/si1145.js +630 -0
  70. package/src/chips/pressure/mpl3115a2.js +459 -0
  71. package/src/chips/pressure/ms5607.js +290 -0
  72. package/src/chips/pressure/ms5611.js +290 -0
  73. package/src/chips/pressure/spl06.js +461 -0
  74. package/src/chips/rfid/pn532.js +513 -0
  75. package/src/connection/i2c.js +46 -13
  76. package/src/connection/smbus.js +21 -8
  77. package/src/discovery/registry.js +18 -18
  78. package/examples/gpio/.gitkeep +0 -0
  79. package/src/chips/gpio/.gitkeep +0 -0
@@ -0,0 +1,573 @@
1
+ 'use strict';
2
+
3
+ const _REG_RES_HEAT_VAL = 0x00;
4
+ const _REG_RES_HEAT_RANGE = 0x02;
5
+ const _REG_MEAS_STATUS = 0x1D;
6
+ const _REG_VARIANT_ID = 0xF0;
7
+ const _FIELD_LEN = 17;
8
+ const _RUN_GAS = 0x20;
9
+ const _REG_CTRL_GAS_0 = 0x70;
10
+ const _REG_CTRL_GAS_1 = 0x71;
11
+ const _REG_CTRL_HUM = 0x72;
12
+ const _REG_CTRL_MEAS = 0x74;
13
+ const _REG_CONFIG = 0x75;
14
+ const _REG_CAL_BLOCK1 = 0x8A;
15
+ const _REG_ID = 0xD0;
16
+ const _REG_RESET = 0xE0;
17
+ const _REG_CAL_BLOCK2 = 0xE1;
18
+
19
+ const _CHIP_ID = 0x61;
20
+ const _VARIANT_ID = 0x01;
21
+ const _RESET_CMD = 0xB6;
22
+
23
+ function _delay(ms) {
24
+ const start = Date.now();
25
+ while (Date.now() - start < ms) { /* spin */ }
26
+ }
27
+
28
+ /**
29
+ * BME688 4-in-1 environmental sensor: temperature, pressure, humidity, gas resistance — minimal interface.
30
+ *
31
+ * Provides calibrated readings with no configuration beyond the connection.
32
+ * I²C address is 0x76 (SDO=GND) or 0x77 (SDO=VDDIO).
33
+ *
34
+ * Default: forced mode, osrs_t=×1, osrs_p=×1, osrs_h=×1, IIR filter off,
35
+ * heater profile 0 at 320 °C / 150 ms.
36
+ *
37
+ * Constructor caveat: JS constructors cannot be async. The calibration read
38
+ * and initial register writes are fired off unawaited, matching the
39
+ * fire-and-forget convention used throughout this port — in practice they
40
+ * settle before any subsequent `await` in caller code is scheduled, since
41
+ * the underlying I2C connection is synchronous under the hood, but this is
42
+ * not a guaranteed blocking wait the way the pre-async version was. Call
43
+ * `await sensor.temperature()` (or any other async method) once before
44
+ * relying on calibrated readings if this matters for your use case.
45
+ *
46
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
47
+ */
48
+ class BME688Minimal {
49
+ constructor(connection) {
50
+ this._conn = connection;
51
+ this._osrsT = 1;
52
+ this._osrsP = 1;
53
+ this._osrsH = 1;
54
+ this._filter = 0;
55
+ this._tFine = 0;
56
+ this._ambientTemp = 25.0;
57
+ this._heatTemp = 320;
58
+ this._heatDur = 150;
59
+ this._gasEnabled = true;
60
+ this._nbConv = 0;
61
+ this._init();
62
+ }
63
+
64
+ async _init() {
65
+ await this._readCalibration();
66
+ await this._conn.writeReg(_REG_CTRL_HUM, this._osrsH);
67
+ await this._conn.writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | 0);
68
+ await this._conn.writeReg(_REG_CONFIG, 0);
69
+ await this._setupHeater(0, this._heatTemp, this._heatDur);
70
+ await this._conn.writeReg(_REG_CTRL_GAS_1, _RUN_GAS | 0);
71
+ }
72
+
73
+ async _readCalibration() {
74
+ const b1 = await this._conn.readReg(_REG_CAL_BLOCK1, 23);
75
+ const b2 = await this._conn.readReg(_REG_CAL_BLOCK2, 14);
76
+ const s1 = await this._conn.readReg(_REG_RES_HEAT_VAL, 1);
77
+ const s2 = await this._conn.readReg(_REG_RES_HEAT_RANGE, 1);
78
+
79
+ this._parT2 = b1.readInt16LE(0);
80
+ this._parT3 = b1.readInt8(2);
81
+ this._parP1 = b1.readUInt16LE(4);
82
+ this._parP2 = b1.readInt16LE(6);
83
+ this._parP3 = b1.readInt8(8);
84
+ this._parP4 = b1.readInt16LE(10);
85
+ this._parP5 = b1.readInt16LE(12);
86
+ this._parP7 = b1.readInt8(14);
87
+ this._parP6 = b1.readInt8(15);
88
+ this._parP8 = b1.readInt16LE(18);
89
+ this._parP9 = b1.readInt16LE(20);
90
+ this._parP10 = b1[22];
91
+
92
+ this._parH2 = (b2[0] << 4) | (b2[1] >> 4);
93
+ this._parH1 = (b2[2] << 4) | (b2[1] & 0x0F);
94
+ this._parH3 = b2.readInt8(3);
95
+ this._parH4 = b2.readInt8(4);
96
+ this._parH5 = b2.readInt8(5);
97
+ this._parH6 = b2[6];
98
+ this._parH7 = b2.readInt8(7);
99
+ this._parT1 = b2.readUInt16LE(8);
100
+ this._parG2 = b2.readInt16LE(10);
101
+ this._parG1 = b2.readInt8(12);
102
+ this._parG3 = b2.readInt8(13);
103
+
104
+ this._resHeatVal = s1.readInt8(0);
105
+ this._resHeatRange = (s2[0] >> 4) & 0x03;
106
+ }
107
+
108
+
109
+ _calcHeaterResistance(targetTemp, ambientTemp) {
110
+ const parG1 = this._parG1;
111
+ const parG2 = this._parG2;
112
+ const parG3 = this._parG3;
113
+ const rhr = this._resHeatRange;
114
+ const rhv = this._resHeatVal;
115
+
116
+ const var1 = ((ambientTemp * parG3) / 10 | 0) << 8;
117
+ const var2 = (parG1 + 784) * ((((parG2 + 154009) * targetTemp * 5 / 100 | 0) + 3276800) / 10 | 0);
118
+ const var3 = var1 + (var2 >> 1);
119
+ const var4 = var3 / (rhr + 4) | 0;
120
+ const var5 = (131 * rhv) + 65536;
121
+ const resHeatX100 = ((var4 / var5 | 0) - 250) * 34;
122
+ const resHeatX = (resHeatX100 + 50) / 100 | 0;
123
+ return resHeatX & 0xFF;
124
+ }
125
+
126
+ _calcGasWait(targetMs) {
127
+ if (targetMs <= 0x3F) return targetMs;
128
+ if (targetMs <= 0x3F * 4) return (1 << 6) | (targetMs / 4 | 0);
129
+ if (targetMs <= 0x3F * 16) return (2 << 6) | (targetMs / 16 | 0);
130
+ return (3 << 6) | Math.min(targetMs / 64 | 0, 0x3F);
131
+ }
132
+
133
+ async _setupHeater(index, tempC, durMs) {
134
+ const res = this._calcHeaterResistance(tempC, this._ambientTemp);
135
+ const gw = this._calcGasWait(durMs);
136
+ await this._conn.writeReg(0x5A + index, res);
137
+ await this._conn.writeReg(0x64 + index, gw);
138
+ }
139
+
140
+ static _parseField(raw, off = 0) {
141
+ const pressAdc = (raw[off + 2] << 12) | (raw[off + 3] << 4) | (raw[off + 4] >> 4);
142
+ const tempAdc = (raw[off + 5] << 12) | (raw[off + 6] << 4) | (raw[off + 7] >> 4);
143
+ const humAdc = (raw[off + 8] << 8) | raw[off + 9];
144
+ const gasAdc = (raw[off + 15] << 2) | (raw[off + 16] >> 6);
145
+ const gasRange = raw[off + 16] & 0x0F;
146
+ const gasValid = (raw[off + 16] >> 5) & 1;
147
+ const heatStab = (raw[off + 16] >> 4) & 1;
148
+ return { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab };
149
+ }
150
+
151
+ async _triggerAndRead() {
152
+ await this._conn.writeReg(_REG_CTRL_HUM, this._osrsH);
153
+ const ctrl = (this._osrsT << 5) | (this._osrsP << 2) | 1;
154
+ await this._conn.writeReg(_REG_CTRL_MEAS, ctrl);
155
+ const start = Date.now();
156
+ for (;;) {
157
+ const st = (await this._conn.readReg(_REG_MEAS_STATUS, 1))[0];
158
+ if ((st & 0x80) && !(st & 0x20)) break;
159
+ if (Date.now() - start > this._heatDur + 500) throw new Error('BME688 measurement timeout');
160
+ await new Promise((r) => setTimeout(r, 5));
161
+ }
162
+ const raw = await this._conn.readReg(_REG_MEAS_STATUS, _FIELD_LEN);
163
+ return BME688Minimal._parseField(raw);
164
+ }
165
+
166
+ _compensateTemp(adcT) {
167
+ const parT1 = this._parT1;
168
+ const parT2 = this._parT2;
169
+ const parT3 = this._parT3;
170
+ const var1 = (adcT >> 3) - (parT1 << 1);
171
+ const var2 = (var1 * parT2) >> 11;
172
+ const var3 = (((var1 >> 1) * (var1 >> 1)) >> 12) * (parT3 << 4) >> 14;
173
+ this._tFine = var2 + var3;
174
+ return ((this._tFine * 5 + 128) >> 8) / 100.0;
175
+ }
176
+
177
+ _compensatePressure(adcP) {
178
+ const tFine = this._tFine;
179
+ const parP1 = this._parP1;
180
+ const parP2 = this._parP2;
181
+ const parP3 = this._parP3;
182
+ const parP4 = this._parP4;
183
+ const parP5 = this._parP5;
184
+ const parP6 = this._parP6;
185
+ const parP7 = this._parP7;
186
+ const parP8 = this._parP8;
187
+ const parP9 = this._parP9;
188
+ const parP10 = this._parP10;
189
+
190
+ let var1 = (tFine >> 1) - 64000;
191
+ let var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * parP6) >> 2;
192
+ var2 = var2 + ((var1 * parP5) << 1);
193
+ var2 = (var2 >> 2) + (parP4 << 16);
194
+ var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) * (parP3 << 5)) >> 3) + ((parP2 * var1) >> 1);
195
+ var1 = var1 >> 18;
196
+ var1 = ((32768 + var1) * parP1) >> 15;
197
+ let pressComp = 1048576 - adcP;
198
+ pressComp = ((pressComp - (var2 >> 12)) * 3125);
199
+ if (pressComp >= (1 << 30)) {
200
+ pressComp = (pressComp / var1 | 0) << 1;
201
+ } else {
202
+ pressComp = (pressComp << 1) / var1 | 0;
203
+ }
204
+ var1 = (parP9 * (((pressComp >> 3) * (pressComp >> 3)) >> 13)) >> 12;
205
+ var2 = ((pressComp >> 2) * parP8) >> 13;
206
+ const var3 = ((pressComp >> 8) * (pressComp >> 8) * (pressComp >> 8) * parP10) >> 17;
207
+ pressComp = pressComp + ((var1 + var2 + var3 + (parP7 << 7)) >> 4);
208
+ return pressComp / 100.0;
209
+ }
210
+
211
+ _compensateHumidity(humAdc) {
212
+ const tempScaled = this._tFine;
213
+ const parH1 = this._parH1;
214
+ const parH2 = this._parH2;
215
+ const parH3 = this._parH3;
216
+ const parH4 = this._parH4;
217
+ const parH5 = this._parH5;
218
+ const parH6 = this._parH6;
219
+ const parH7 = this._parH7;
220
+
221
+ const var1 = humAdc - ((parH1 << 4) + (Math.floor((tempScaled * parH3) / 100) >> 1));
222
+ const h5 = Math.floor(tempScaled * Math.floor((tempScaled * parH5) / 100) / 64);
223
+ const var2 = Math.floor(parH2 * (Math.floor((tempScaled * parH4) / 100) +
224
+ Math.floor(h5 / 100) + (1 << 14)) / 1024);
225
+ const var3 = var1 * var2;
226
+ const var4 = Math.floor(((parH6 << 7) + Math.floor((tempScaled * parH7) / 100)) / 16);
227
+ const var5 = Math.floor(Math.floor(var3 / 16384) * Math.floor(var3 / 16384) / 1024);
228
+ const var6 = Math.floor(var4 * var5 / 2);
229
+ let humComp = Math.floor(Math.floor((var3 + var6) / 1024) * 1000 / 4096);
230
+ if (humComp < 0) humComp = 0;
231
+ if (humComp > 100000) humComp = 100000;
232
+ return humComp / 1000.0;
233
+ }
234
+
235
+ _compensateGas(gasAdc, gasRange) {
236
+ const var1 = 262144 >> gasRange;
237
+ const var2 = 4096 + 3 * (gasAdc - 512);
238
+ if (var2 <= 0) return NaN;
239
+ return Math.floor(10000 * var1 / var2) * 100;
240
+ }
241
+
242
+ /**
243
+ * Read calibrated temperature.
244
+ * @returns {Promise<number>} Temperature in degrees Celsius.
245
+ */
246
+ async temperature() {
247
+ const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = await this._triggerAndRead();
248
+ const t = this._compensateTemp(tempAdc);
249
+ this._ambientTemp = t;
250
+ return t;
251
+ }
252
+
253
+ /**
254
+ * Read calibrated pressure.
255
+ * @returns {Promise<number>} Pressure in hPa.
256
+ */
257
+ async pressure() {
258
+ const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = await this._triggerAndRead();
259
+ this._compensateTemp(tempAdc);
260
+ return this._compensatePressure(pressAdc);
261
+ }
262
+
263
+ /**
264
+ * Read calibrated humidity.
265
+ * @returns {Promise<number>} Relative humidity in %RH.
266
+ */
267
+ async humidity() {
268
+ const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = await this._triggerAndRead();
269
+ this._compensateTemp(tempAdc);
270
+ return this._compensateHumidity(humAdc);
271
+ }
272
+
273
+ /**
274
+ * Read gas sensor resistance.
275
+ * @returns {Promise<number>} Gas resistance in Ohms, or NaN on invalid reading.
276
+ */
277
+ async gasResistance() {
278
+ const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = await this._triggerAndRead();
279
+ this._compensateTemp(tempAdc);
280
+ if (!gasValid || !heatStab) return NaN;
281
+ return this._compensateGas(gasAdc, gasRange);
282
+ }
283
+ }
284
+
285
+ /**
286
+ * BME688 full interface — extends BME688Minimal with configuration, multi-profile heater, status and parallel mode.
287
+ *
288
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
289
+ */
290
+ class BME688Full extends BME688Minimal {
291
+ static OSRS_SKIP = 0;
292
+ static OSRS_X1 = 1;
293
+ static OSRS_X2 = 2;
294
+ static OSRS_X4 = 3;
295
+ static OSRS_X8 = 4;
296
+ static OSRS_X16 = 5;
297
+
298
+ static MODE_SLEEP = 0;
299
+ static MODE_FORCED = 1;
300
+ static MODE_PARALLEL = 2;
301
+
302
+ static FILTER_0 = 0;
303
+ static FILTER_1 = 1;
304
+ static FILTER_3 = 2;
305
+ static FILTER_7 = 3;
306
+ static FILTER_15 = 4;
307
+ static FILTER_31 = 5;
308
+ static FILTER_63 = 6;
309
+ static FILTER_127 = 7;
310
+
311
+ static STATUS_NEW_DATA = 0x80;
312
+ static STATUS_GAS_MEASURING = 0x40;
313
+ static STATUS_MEASURING = 0x20;
314
+ static STATUS_GAS_VALID = 0x20;
315
+ static STATUS_HEATER_STABLE = 0x10;
316
+
317
+ constructor(connection) {
318
+ super(connection);
319
+ }
320
+
321
+ /**
322
+ * Write ctrl_hum, ctrl_meas, and config registers in the correct order.
323
+ * @param {number} osrsT - Temperature oversampling (0–5).
324
+ * @param {number} osrsP - Pressure oversampling (0–5).
325
+ * @param {number} osrsH - Humidity oversampling (0–5).
326
+ * @param {number} mode - Power mode (0=sleep, 1=forced, 2=parallel).
327
+ * @param {number} filter - IIR filter coefficient (0–7).
328
+ * @returns {Promise<void>}
329
+ */
330
+ async configure(osrsT, osrsP, osrsH, mode, filter) {
331
+ this._osrsT = osrsT;
332
+ this._osrsP = osrsP;
333
+ this._osrsH = osrsH;
334
+ this._filter = filter;
335
+ await this._conn.writeReg(_REG_CTRL_HUM, osrsH);
336
+ await this._conn.writeReg(_REG_CONFIG, filter << 2);
337
+ await this._conn.writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | mode);
338
+ }
339
+
340
+ /**
341
+ * Update oversampling for all three TPH channels.
342
+ * @param {number} osrsT - Temperature oversampling (0–5).
343
+ * @param {number} osrsP - Pressure oversampling (0–5).
344
+ * @param {number} osrsH - Humidity oversampling (0–5).
345
+ * @returns {Promise<void>}
346
+ */
347
+ async setOversampling(osrsT, osrsP, osrsH) {
348
+ this._osrsT = osrsT;
349
+ this._osrsP = osrsP;
350
+ this._osrsH = osrsH;
351
+ await this._conn.writeReg(_REG_CTRL_HUM, osrsH);
352
+ await this._conn.writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | 0);
353
+ }
354
+
355
+ /**
356
+ * Update IIR filter coefficient.
357
+ * @param {number} coeff - Filter coefficient (0–7).
358
+ * @returns {Promise<void>}
359
+ */
360
+ async setFilter(coeff) {
361
+ this._filter = coeff;
362
+ await this._conn.writeReg(_REG_CONFIG, coeff << 2);
363
+ }
364
+
365
+ /**
366
+ * Configure heater profile 0 and activate it.
367
+ * @param {number} tempC - Target heater temperature in degrees Celsius.
368
+ * @param {number} durationMs - Heater on-time in milliseconds (1–4032).
369
+ * @returns {Promise<void>}
370
+ */
371
+ async setHeater(tempC, durationMs) {
372
+ this._heatTemp = tempC;
373
+ this._heatDur = durationMs;
374
+ await this._setupHeater(0, tempC, durationMs);
375
+ await this._conn.writeReg(_REG_CTRL_GAS_1, _RUN_GAS | 0);
376
+ }
377
+
378
+ /**
379
+ * Configure one of the 10 heater profiles.
380
+ * @param {number} index - Profile index (0–9).
381
+ * @param {number} tempC - Target heater temperature in degrees Celsius.
382
+ * @param {number} durationMs - Heater on-time in milliseconds (1–4032).
383
+ * @returns {Promise<void>}
384
+ */
385
+ async setHeaterProfile(index, tempC, durationMs) {
386
+ await this._setupHeater(index, tempC, durationMs);
387
+ }
388
+
389
+ /**
390
+ * Select which heater profile to use in the next forced cycle.
391
+ * @param {number} index - Profile index (0–9).
392
+ * @returns {Promise<void>}
393
+ */
394
+ async selectHeaterProfile(index) {
395
+ this._nbConv = index;
396
+ const gas1 = this._gasEnabled ? (_RUN_GAS | index) : index;
397
+ await this._conn.writeReg(_REG_CTRL_GAS_1, gas1);
398
+ }
399
+
400
+ /**
401
+ * Enable or disable gas conversion.
402
+ * @param {boolean} enabled - True to enable gas measurement.
403
+ * @returns {Promise<void>}
404
+ */
405
+ async setGasEnabled(enabled) {
406
+ this._gasEnabled = enabled;
407
+ const gas1 = enabled ? (_RUN_GAS | this._nbConv) : this._nbConv;
408
+ await this._conn.writeReg(_REG_CTRL_GAS_1, gas1);
409
+ }
410
+
411
+ /**
412
+ * Turn the heater off or on via ctrl_gas_0.
413
+ * @param {boolean} off - True to disable the heater.
414
+ * @returns {Promise<void>}
415
+ */
416
+ async setHeaterOff(off) {
417
+ await this._conn.writeReg(_REG_CTRL_GAS_0, off ? 0x08 : 0x00);
418
+ }
419
+
420
+ /**
421
+ * Override the ambient temperature used for heater-resistance calculation.
422
+ * @param {number} tempC - Ambient temperature in degrees Celsius.
423
+ * @returns {Promise<void>}
424
+ */
425
+ async setAmbientTemperature(tempC) {
426
+ this._ambientTemp = tempC;
427
+ await this._setupHeater(this._nbConv, this._heatTemp, this._heatDur);
428
+ }
429
+
430
+ /**
431
+ * Read all four sensor values from a single TPHG cycle.
432
+ * @returns {Promise<{ temperature: number, pressure: number, humidity: number, gasResistance: number }>}
433
+ */
434
+ async readAll() {
435
+ const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = await this._triggerAndRead();
436
+ const t = this._compensateTemp(tempAdc);
437
+ this._ambientTemp = t;
438
+ const p = this._compensatePressure(pressAdc);
439
+ const h = this._compensateHumidity(humAdc);
440
+ const g = (gasValid && heatStab) ? this._compensateGas(gasAdc, gasRange) : NaN;
441
+ return { temperature: t, pressure: p, humidity: h, gasResistance: g };
442
+ }
443
+
444
+ /**
445
+ * Check if the most recent gas reading is valid.
446
+ * @returns {Promise<boolean>} True if gas_valid_r was set.
447
+ */
448
+ async gasValid() {
449
+ const raw = await this._conn.readReg(0x2D, 1);
450
+ return ((raw[0] >> 5) & 1) === 1;
451
+ }
452
+
453
+ /**
454
+ * Check if the heater reached its target temperature.
455
+ * @returns {Promise<boolean>} True if heat_stab_r was set.
456
+ */
457
+ async heaterStable() {
458
+ const raw = await this._conn.readReg(0x2D, 1);
459
+ return ((raw[0] >> 4) & 1) === 1;
460
+ }
461
+
462
+ /**
463
+ * Read the measurement status register.
464
+ * @returns {Promise<number>} Status byte with flags.
465
+ */
466
+ async status() {
467
+ const raw = await this._conn.readReg(_REG_MEAS_STATUS, 1);
468
+ return raw[0];
469
+ }
470
+
471
+ /**
472
+ * Read the chip ID register.
473
+ * @returns {Promise<number>} Chip ID; expect 0x61.
474
+ */
475
+ async chipId() {
476
+ const raw = await this._conn.readReg(_REG_ID, 1);
477
+ return raw[0];
478
+ }
479
+
480
+ /**
481
+ * Read the variant ID register.
482
+ * @returns {Promise<number>} Variant ID; 0x01 for the BME688.
483
+ */
484
+ async variantId() {
485
+ const raw = await this._conn.readReg(_REG_VARIANT_ID, 1);
486
+ return raw[0];
487
+ }
488
+
489
+ /**
490
+ * Start parallel mode: the chip cycles TPHG through a heater profile on its own.
491
+ * Programs res_heat_x, gas_wait_x (repetitions), gas_wait_shared, nb_conv and
492
+ * run_gas, then sets mode = parallel. At least one of the T/P/H oversamplings
493
+ * must not be OSRS_SKIP.
494
+ * @param {Array<[number, number]>} steps - 1–10 entries of [tempC, repeats (1–255)].
495
+ * @param {number} [sharedWaitMs=100] - Wait before every TPHG sequence in ms (0.477 ms resolution).
496
+ * @returns {Promise<void>}
497
+ */
498
+ async startParallel(steps, sharedWaitMs = 100) {
499
+ if (steps.length < 1 || steps.length > 10) throw new RangeError('1-10 heater steps required');
500
+ for (let i = 0; i < steps.length; i++) {
501
+ const [tempC, repeats] = steps[i];
502
+ if (repeats < 1 || repeats > 255) throw new RangeError('repeats must be 1-255');
503
+ await this._conn.writeReg(0x5A + i, this._calcHeaterResistance(tempC, this._ambientTemp));
504
+ await this._conn.writeReg(0x64 + i, repeats);
505
+ }
506
+ await this._conn.writeReg(0x6E, BME688Full._calcSharedWait(sharedWaitMs));
507
+ await this._conn.writeReg(_REG_CTRL_GAS_1, _RUN_GAS | steps.length);
508
+ await this._conn.writeReg(_REG_CTRL_HUM, this._osrsH);
509
+ await this._conn.writeReg(_REG_CONFIG, this._filter << 2);
510
+ await this._conn.writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | BME688Full.MODE_PARALLEL);
511
+ }
512
+
513
+ static _calcSharedWait(ms) {
514
+ const units = Math.floor(ms / 0.477 + 0.5);
515
+ if (units <= 0x3F) return units;
516
+ if (units <= 0x3F * 4) return (1 << 6) | (units >> 2);
517
+ if (units <= 0x3F * 16) return (2 << 6) | (units >> 4);
518
+ return (3 << 6) | Math.min(units >> 6, 0x3F);
519
+ }
520
+
521
+ /**
522
+ * Burst-read the three data fields and return the ones holding new data.
523
+ * @returns {Promise<Array<{step: number, subIndex: number, temperature: number, pressure: number, humidity: number, gasResistance: number, gasValid: boolean, heaterStable: boolean}>>}
524
+ * gasResistance is NaN when the gas reading is not valid.
525
+ */
526
+ async readParallel() {
527
+ const raw = await this._conn.readReg(_REG_MEAS_STATUS, 3 * _FIELD_LEN);
528
+ const out = [];
529
+ for (let n = 0; n < 3; n++) {
530
+ const off = n * _FIELD_LEN;
531
+ if (!(raw[off] & 0x80)) continue;
532
+ const f = BME688Minimal._parseField(raw, off);
533
+ const t = this._compensateTemp(f.tempAdc);
534
+ out.push({
535
+ step: raw[off] & 0x0F,
536
+ subIndex: raw[off + 1],
537
+ temperature: t,
538
+ pressure: this._compensatePressure(f.pressAdc),
539
+ humidity: this._compensateHumidity(f.humAdc),
540
+ gasResistance: (f.gasValid && f.heatStab) ? this._compensateGas(f.gasAdc, f.gasRange) : NaN,
541
+ gasValid: !!f.gasValid,
542
+ heaterStable: !!f.heatStab,
543
+ });
544
+ }
545
+ return out;
546
+ }
547
+
548
+ /**
549
+ * Leave parallel mode (mode = sleep).
550
+ * @returns {Promise<void>}
551
+ */
552
+ async stopParallel() {
553
+ await this._conn.writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | BME688Full.MODE_SLEEP);
554
+ }
555
+
556
+ /**
557
+ * Perform a soft reset, re-read calibration, and re-apply configuration.
558
+ * @returns {Promise<void>}
559
+ */
560
+ async reset() {
561
+ await this._conn.writeReg(_REG_RESET, _RESET_CMD);
562
+ _delay(2);
563
+ await this._readCalibration();
564
+ await this._conn.writeReg(_REG_CTRL_HUM, this._osrsH);
565
+ await this._conn.writeReg(_REG_CONFIG, this._filter << 2);
566
+ await this._conn.writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | 0);
567
+ await this._setupHeater(this._nbConv, this._heatTemp, this._heatDur);
568
+ const gas1 = this._gasEnabled ? (_RUN_GAS | this._nbConv) : this._nbConv;
569
+ await this._conn.writeReg(_REG_CTRL_GAS_1, gas1);
570
+ }
571
+ }
572
+
573
+ module.exports = { BME688Minimal, BME688Full };