periph 1.0.1 → 1.1.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/examples/adc_dac/hx711/complete.js +42 -0
- package/examples/adc_dac/hx711/demo.js +35 -0
- package/examples/adc_dac/hx711/minimal.js +16 -0
- package/examples/adc_dac/mcp4728/complete.js +41 -0
- package/examples/adc_dac/mcp4728/demo.js +41 -0
- package/examples/adc_dac/mcp4728/minimal.js +11 -0
- package/examples/display/pcf8576/complete.js +32 -0
- package/examples/display/pcf8576/demo.js +48 -0
- package/examples/display/pcf8576/minimal.js +18 -0
- package/examples/environmental/aht21/complete.js +30 -0
- package/examples/environmental/aht21/demo.js +44 -0
- package/examples/environmental/aht21/minimal.js +12 -0
- package/examples/environmental/bme280/complete.js +34 -0
- package/examples/environmental/bme280/demo.js +67 -0
- package/examples/environmental/bme280/minimal.js +18 -0
- package/examples/environmental/bme680/complete.js +37 -0
- package/examples/environmental/bme680/demo.js +52 -0
- package/examples/environmental/bme680/minimal.js +19 -0
- package/examples/gas/ens160/complete.js +52 -0
- package/examples/gas/ens160/demo.js +52 -0
- package/examples/gas/ens160/minimal.js +27 -0
- package/examples/imu/mpu6050/complete.js +56 -0
- package/examples/imu/mpu6050/demo.js +34 -0
- package/examples/imu/mpu6050/minimal.js +14 -0
- package/examples/light/apds9960/complete.js +62 -0
- package/examples/light/apds9960/demo.js +32 -0
- package/examples/light/apds9960/minimal.js +12 -0
- package/examples/memory/24aa02uid/complete.js +40 -0
- package/examples/memory/24aa02uid/demo.js +40 -0
- package/examples/memory/24aa02uid/minimal.js +12 -0
- package/examples/pressure/bmp280/complete.js +22 -18
- package/examples/pressure/bmp280/demo.js +34 -18
- package/examples/pressure/bmp280/minimal.js +4 -4
- package/package.json +1 -1
- package/src/chips/adc_dac/hx711.js +165 -0
- package/src/chips/adc_dac/mcp4728.js +284 -0
- package/src/chips/display/pcf8576.js +223 -0
- package/src/chips/environmental/aht21.js +170 -0
- package/src/chips/environmental/bme280.js +366 -0
- package/src/chips/environmental/bme680.js +489 -0
- package/src/chips/gas/ens160.js +280 -0
- package/src/chips/imu/mpu6050.js +266 -0
- package/src/chips/light/apds9960.js +464 -0
- package/src/chips/memory/_24aa02uid.js +179 -0
- package/src/chips/pressure/bmp280.js +154 -191
- package/src/transport/hx711.js +105 -0
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/imu/.gitkeep +0 -0
- package/examples/light/.gitkeep +0 -0
- package/examples/pressure/.gitkeep +0 -0
- package/src/chips/display/.gitkeep +0 -0
- package/src/chips/environmental/.gitkeep +0 -0
- package/src/chips/imu/.gitkeep +0 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/pressure/.gitkeep +0 -0
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const CONST_ARRAY1 = [
|
|
4
|
+
2147483647, 2147483647, 2147483647, 2147483647, 2147483647,
|
|
5
|
+
2126008810, 2147483647, 2130303777, 2147483647, 2147483647,
|
|
6
|
+
2143188679, 2136746228, 2147483647, 2126008810, 2147483647,
|
|
7
|
+
2147483647
|
|
8
|
+
];
|
|
9
|
+
|
|
10
|
+
const CONST_ARRAY2 = [
|
|
11
|
+
4096000000, 2048000000, 1024000000, 512000000, 255744255,
|
|
12
|
+
127110228, 64000000, 32258064, 16016016, 8000000,
|
|
13
|
+
4000000, 2000000, 1000000, 500000, 250000,
|
|
14
|
+
125000
|
|
15
|
+
];
|
|
16
|
+
|
|
17
|
+
const _REG_RES_HEAT_VAL = 0x00;
|
|
18
|
+
const _REG_RES_HEAT_RANGE = 0x02;
|
|
19
|
+
const _REG_RANGE_SW_ERR = 0x04;
|
|
20
|
+
const _REG_MEAS_STATUS = 0x1D;
|
|
21
|
+
const _REG_PRESS_MSB = 0x1F;
|
|
22
|
+
const _REG_CTRL_GAS_0 = 0x70;
|
|
23
|
+
const _REG_CTRL_GAS_1 = 0x71;
|
|
24
|
+
const _REG_CTRL_HUM = 0x72;
|
|
25
|
+
const _REG_CTRL_MEAS = 0x74;
|
|
26
|
+
const _REG_CONFIG = 0x75;
|
|
27
|
+
const _REG_CAL_BLOCK1 = 0x8A;
|
|
28
|
+
const _REG_ID = 0xD0;
|
|
29
|
+
const _REG_RESET = 0xE0;
|
|
30
|
+
const _REG_CAL_BLOCK2 = 0xE1;
|
|
31
|
+
|
|
32
|
+
const _CHIP_ID = 0x61;
|
|
33
|
+
const _RESET_CMD = 0xB6;
|
|
34
|
+
|
|
35
|
+
const _MEAS_TIME_MS = 200;
|
|
36
|
+
|
|
37
|
+
function _delay(ms) {
|
|
38
|
+
const start = Date.now();
|
|
39
|
+
while (Date.now() - start < ms) { /* spin */ }
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* BME680 4-in-1 environmental sensor: temperature, pressure, humidity, gas resistance — minimal interface.
|
|
44
|
+
*
|
|
45
|
+
* Provides calibrated readings with no configuration beyond the transport.
|
|
46
|
+
* I²C address is 0x76 (SDO=GND) or 0x77 (SDO=VDDIO).
|
|
47
|
+
*
|
|
48
|
+
* Default: forced mode, osrs_t=×1, osrs_p=×1, osrs_h=×1, IIR filter off,
|
|
49
|
+
* heater profile 0 at 320 °C / 150 ms.
|
|
50
|
+
*
|
|
51
|
+
* @param {object} transport - Configured I²C transport.
|
|
52
|
+
*/
|
|
53
|
+
class BME680Minimal {
|
|
54
|
+
constructor(transport) {
|
|
55
|
+
this._transport = transport;
|
|
56
|
+
this._osrsT = 1;
|
|
57
|
+
this._osrsP = 1;
|
|
58
|
+
this._osrsH = 1;
|
|
59
|
+
this._filter = 0;
|
|
60
|
+
this._tFine = 0;
|
|
61
|
+
this._ambientTemp = 25.0;
|
|
62
|
+
this._heatTemp = 320;
|
|
63
|
+
this._heatDur = 150;
|
|
64
|
+
this._gasEnabled = true;
|
|
65
|
+
this._nbConv = 0;
|
|
66
|
+
this._readCalibration();
|
|
67
|
+
this._writeReg(_REG_CTRL_HUM, this._osrsH);
|
|
68
|
+
this._writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | 0);
|
|
69
|
+
this._writeReg(_REG_CONFIG, 0);
|
|
70
|
+
this._setupHeater(0, this._heatTemp, this._heatDur);
|
|
71
|
+
this._writeReg(_REG_CTRL_GAS_1, (1 << 4) | 0);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
_readCalibration() {
|
|
75
|
+
const b1 = this._transport.writeRead(Buffer.from([_REG_CAL_BLOCK1]), 23);
|
|
76
|
+
const b2 = this._transport.writeRead(Buffer.from([_REG_CAL_BLOCK2]), 14);
|
|
77
|
+
const s1 = this._transport.writeRead(Buffer.from([_REG_RES_HEAT_VAL]), 1);
|
|
78
|
+
const s2 = this._transport.writeRead(Buffer.from([_REG_RES_HEAT_RANGE]), 1);
|
|
79
|
+
const s3 = this._transport.writeRead(Buffer.from([_REG_RANGE_SW_ERR]), 1);
|
|
80
|
+
|
|
81
|
+
this._parT2 = b1.readInt16LE(0);
|
|
82
|
+
this._parT3 = b1.readInt8(2);
|
|
83
|
+
this._parP1 = b1.readUInt16LE(4);
|
|
84
|
+
this._parP2 = b1.readInt16LE(6);
|
|
85
|
+
this._parP3 = b1.readInt8(8);
|
|
86
|
+
this._parP4 = b1.readInt16LE(10);
|
|
87
|
+
this._parP5 = b1.readInt16LE(12);
|
|
88
|
+
this._parP7 = b1.readInt8(14);
|
|
89
|
+
this._parP6 = b1.readInt8(15);
|
|
90
|
+
this._parP8 = b1.readInt16LE(18);
|
|
91
|
+
this._parP9 = b1.readInt16LE(20);
|
|
92
|
+
this._parP10 = b1[22];
|
|
93
|
+
|
|
94
|
+
this._parH2 = (b2[0] << 4) | (b2[1] >> 4);
|
|
95
|
+
this._parH1 = (b2[2] << 4) | (b2[1] & 0x0F);
|
|
96
|
+
this._parH3 = b2.readInt8(3);
|
|
97
|
+
this._parH4 = b2.readInt8(4);
|
|
98
|
+
this._parH5 = b2.readInt8(5);
|
|
99
|
+
this._parH6 = b2[6];
|
|
100
|
+
this._parH7 = b2.readInt8(7);
|
|
101
|
+
this._parT1 = b2.readUInt16LE(8);
|
|
102
|
+
this._parG2 = b2.readInt16LE(10);
|
|
103
|
+
this._parG1 = b2.readInt8(12);
|
|
104
|
+
this._parG3 = b2.readInt8(13);
|
|
105
|
+
|
|
106
|
+
this._resHeatVal = s1.readInt8(0);
|
|
107
|
+
this._resHeatRange = (s2[0] >> 4) & 0x03;
|
|
108
|
+
const rse = (s3[0] >> 4) & 0x0F;
|
|
109
|
+
this._rangeSwitchingError = rse < 8 ? rse : rse - 16;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
_writeReg(reg, value) {
|
|
113
|
+
this._transport.write(Buffer.from([reg, value]));
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
_readReg(reg, n) {
|
|
117
|
+
return this._transport.writeRead(Buffer.from([reg]), n);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
_calcHeaterResistance(targetTemp, ambientTemp) {
|
|
121
|
+
const parG1 = this._parG1;
|
|
122
|
+
const parG2 = this._parG2;
|
|
123
|
+
const parG3 = this._parG3;
|
|
124
|
+
const rhr = this._resHeatRange;
|
|
125
|
+
const rhv = this._resHeatVal;
|
|
126
|
+
|
|
127
|
+
const var1 = ((ambientTemp * parG3) / 10 | 0) << 8;
|
|
128
|
+
const var2 = (parG1 + 784) * ((((parG2 + 154009) * targetTemp * 5 / 100 | 0) + 3276800) / 10 | 0);
|
|
129
|
+
const var3 = var1 + (var2 >> 1);
|
|
130
|
+
const var4 = var3 / (rhr + 4) | 0;
|
|
131
|
+
const var5 = (131 * rhv) + 65536;
|
|
132
|
+
const resHeatX100 = ((var4 / var5 | 0) - 250) * 34;
|
|
133
|
+
const resHeatX = (resHeatX100 + 50) / 100 | 0;
|
|
134
|
+
return resHeatX & 0xFF;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
_calcGasWait(targetMs) {
|
|
138
|
+
if (targetMs <= 0x3F) return targetMs;
|
|
139
|
+
if (targetMs <= 0x3F * 4) return (1 << 6) | (targetMs / 4 | 0);
|
|
140
|
+
if (targetMs <= 0x3F * 16) return (2 << 6) | (targetMs / 16 | 0);
|
|
141
|
+
return (3 << 6) | Math.min(targetMs / 64 | 0, 0x3F);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
_setupHeater(index, tempC, durMs) {
|
|
145
|
+
const res = this._calcHeaterResistance(tempC, this._ambientTemp);
|
|
146
|
+
const gw = this._calcGasWait(durMs);
|
|
147
|
+
this._writeReg(0x5A + index, res);
|
|
148
|
+
this._writeReg(0x64 + index, gw);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
_triggerAndRead() {
|
|
152
|
+
this._writeReg(_REG_CTRL_HUM, this._osrsH);
|
|
153
|
+
const ctrl = (this._osrsT << 5) | (this._osrsP << 2) | 1;
|
|
154
|
+
this._writeReg(_REG_CTRL_MEAS, ctrl);
|
|
155
|
+
_delay(_MEAS_TIME_MS);
|
|
156
|
+
const raw = this._readReg(_REG_PRESS_MSB, 13);
|
|
157
|
+
const pressAdc = (raw[0] << 12) | (raw[1] << 4) | (raw[2] >> 4);
|
|
158
|
+
const tempAdc = (raw[3] << 12) | (raw[4] << 4) | (raw[5] >> 4);
|
|
159
|
+
const humAdc = (raw[6] << 8) | raw[7];
|
|
160
|
+
const gasAdc = (raw[11] << 2) | (raw[12] >> 6);
|
|
161
|
+
const gasRange = raw[12] & 0x0F;
|
|
162
|
+
const gasValid = (raw[12] >> 5) & 1;
|
|
163
|
+
const heatStab = (raw[12] >> 4) & 1;
|
|
164
|
+
return { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
_compensateTemp(adcT) {
|
|
168
|
+
const parT1 = this._parT1;
|
|
169
|
+
const parT2 = this._parT2;
|
|
170
|
+
const parT3 = this._parT3;
|
|
171
|
+
const var1 = (adcT >> 3) - (parT1 << 1);
|
|
172
|
+
const var2 = (var1 * parT2) >> 11;
|
|
173
|
+
const var3 = (((var1 >> 1) * (var1 >> 1)) >> 12) * (parT3 << 4) >> 14;
|
|
174
|
+
this._tFine = var2 + var3;
|
|
175
|
+
return ((this._tFine * 5 + 128) >> 8) / 100.0;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
_compensatePressure(adcP) {
|
|
179
|
+
const tFine = this._tFine;
|
|
180
|
+
const parP1 = this._parP1;
|
|
181
|
+
const parP2 = this._parP2;
|
|
182
|
+
const parP3 = this._parP3;
|
|
183
|
+
const parP4 = this._parP4;
|
|
184
|
+
const parP5 = this._parP5;
|
|
185
|
+
const parP6 = this._parP6;
|
|
186
|
+
const parP7 = this._parP7;
|
|
187
|
+
const parP8 = this._parP8;
|
|
188
|
+
const parP9 = this._parP9;
|
|
189
|
+
const parP10 = this._parP10;
|
|
190
|
+
|
|
191
|
+
let var1 = (tFine >> 1) - 64000;
|
|
192
|
+
let var2 = ((((var1 >> 2) * (var1 >> 2)) >> 11) * parP6) >> 2;
|
|
193
|
+
var2 = var2 + ((var1 * parP5) << 1);
|
|
194
|
+
var2 = (var2 >> 2) + (parP4 << 16);
|
|
195
|
+
var1 = (((((var1 >> 2) * (var1 >> 2)) >> 13) * (parP3 << 5)) >> 3) + ((parP2 * var1) >> 1);
|
|
196
|
+
var1 = var1 >> 18;
|
|
197
|
+
var1 = ((32768 + var1) * parP1) >> 15;
|
|
198
|
+
let pressComp = 1048576 - adcP;
|
|
199
|
+
pressComp = ((pressComp - (var2 >> 12)) * 3125);
|
|
200
|
+
if (pressComp >= (1 << 30)) {
|
|
201
|
+
pressComp = (pressComp / var1 | 0) << 1;
|
|
202
|
+
} else {
|
|
203
|
+
pressComp = (pressComp << 1) / var1 | 0;
|
|
204
|
+
}
|
|
205
|
+
var1 = (parP9 * (((pressComp >> 3) * (pressComp >> 3)) >> 13)) >> 12;
|
|
206
|
+
var2 = ((pressComp >> 2) * parP8) >> 13;
|
|
207
|
+
const var3 = ((pressComp >> 8) * (pressComp >> 8) * (pressComp >> 8) * parP10) >> 17;
|
|
208
|
+
pressComp = pressComp + ((var1 + var2 + var3 + (parP7 << 7)) >> 4);
|
|
209
|
+
return pressComp / 100.0;
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
_compensateHumidity(humAdc) {
|
|
213
|
+
const tempScaled = this._tFine;
|
|
214
|
+
const parH1 = this._parH1;
|
|
215
|
+
const parH2 = this._parH2;
|
|
216
|
+
const parH3 = this._parH3;
|
|
217
|
+
const parH4 = this._parH4;
|
|
218
|
+
const parH5 = this._parH5;
|
|
219
|
+
const parH6 = this._parH6;
|
|
220
|
+
const parH7 = this._parH7;
|
|
221
|
+
|
|
222
|
+
const var1 = humAdc - ((parH1 << 4) + (Math.floor((tempScaled * parH3) / 100) >> 1));
|
|
223
|
+
const h5 = Math.floor(tempScaled * Math.floor((tempScaled * parH5) / 100) / 64);
|
|
224
|
+
const var2 = Math.floor(parH2 * (Math.floor((tempScaled * parH4) / 100) +
|
|
225
|
+
Math.floor(h5 / 100) + (1 << 14)) / 1024);
|
|
226
|
+
const var3 = var1 * var2;
|
|
227
|
+
const var4 = Math.floor(((parH6 << 7) + Math.floor((tempScaled * parH7) / 100)) / 16);
|
|
228
|
+
const var5 = Math.floor(Math.floor(var3 / 16384) * Math.floor(var3 / 16384) / 1024);
|
|
229
|
+
const var6 = Math.floor(var4 * var5 / 2);
|
|
230
|
+
let humComp = Math.floor(Math.floor((var3 + var6) / 1024) * 1000 / 4096);
|
|
231
|
+
if (humComp < 0) humComp = 0;
|
|
232
|
+
if (humComp > 100000) humComp = 100000;
|
|
233
|
+
return humComp / 1000.0;
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
_compensateGas(gasAdc, gasRange) {
|
|
237
|
+
const rse = this._rangeSwitchingError;
|
|
238
|
+
const var1 = Math.floor((1340 + 5 * rse) * CONST_ARRAY1[gasRange] / 65536);
|
|
239
|
+
const var2 = ((gasAdc << 15) - (1 << 24)) + var1;
|
|
240
|
+
if (var2 === 0) return NaN;
|
|
241
|
+
const gasRes = Math.floor(CONST_ARRAY2[gasRange] * var1 / 512) + Math.floor(var2 / 2);
|
|
242
|
+
return Math.floor(gasRes / var2);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Read calibrated temperature.
|
|
247
|
+
* @returns {number} Temperature in degrees Celsius.
|
|
248
|
+
*/
|
|
249
|
+
temperature() {
|
|
250
|
+
const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = this._triggerAndRead();
|
|
251
|
+
const t = this._compensateTemp(tempAdc);
|
|
252
|
+
this._ambientTemp = t;
|
|
253
|
+
return t;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Read calibrated pressure.
|
|
258
|
+
* @returns {number} Pressure in hPa.
|
|
259
|
+
*/
|
|
260
|
+
pressure() {
|
|
261
|
+
const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = this._triggerAndRead();
|
|
262
|
+
this._compensateTemp(tempAdc);
|
|
263
|
+
return this._compensatePressure(pressAdc);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Read calibrated humidity.
|
|
268
|
+
* @returns {number} Relative humidity in %RH.
|
|
269
|
+
*/
|
|
270
|
+
humidity() {
|
|
271
|
+
const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = this._triggerAndRead();
|
|
272
|
+
this._compensateTemp(tempAdc);
|
|
273
|
+
return this._compensateHumidity(humAdc);
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/**
|
|
277
|
+
* Read gas sensor resistance.
|
|
278
|
+
* @returns {number} Gas resistance in Ohms, or NaN on invalid reading.
|
|
279
|
+
*/
|
|
280
|
+
gasResistance() {
|
|
281
|
+
const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = this._triggerAndRead();
|
|
282
|
+
this._compensateTemp(tempAdc);
|
|
283
|
+
if (!gasValid || !heatStab) return NaN;
|
|
284
|
+
return this._compensateGas(gasAdc, gasRange);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* BME680 full interface — extends BME680Minimal with configuration, multi-profile heater, and status.
|
|
290
|
+
*
|
|
291
|
+
* @param {object} transport - Configured I²C transport.
|
|
292
|
+
*/
|
|
293
|
+
class BME680Full extends BME680Minimal {
|
|
294
|
+
static OSRS_SKIP = 0;
|
|
295
|
+
static OSRS_X1 = 1;
|
|
296
|
+
static OSRS_X2 = 2;
|
|
297
|
+
static OSRS_X4 = 3;
|
|
298
|
+
static OSRS_X8 = 4;
|
|
299
|
+
static OSRS_X16 = 5;
|
|
300
|
+
|
|
301
|
+
static MODE_SLEEP = 0;
|
|
302
|
+
static MODE_FORCED = 1;
|
|
303
|
+
|
|
304
|
+
static FILTER_0 = 0;
|
|
305
|
+
static FILTER_1 = 1;
|
|
306
|
+
static FILTER_3 = 2;
|
|
307
|
+
static FILTER_7 = 3;
|
|
308
|
+
static FILTER_15 = 4;
|
|
309
|
+
static FILTER_31 = 5;
|
|
310
|
+
static FILTER_63 = 6;
|
|
311
|
+
static FILTER_127 = 7;
|
|
312
|
+
|
|
313
|
+
static STATUS_NEW_DATA = 0x80;
|
|
314
|
+
static STATUS_GAS_MEASURING = 0x40;
|
|
315
|
+
static STATUS_MEASURING = 0x20;
|
|
316
|
+
static STATUS_GAS_VALID = 0x20;
|
|
317
|
+
static STATUS_HEATER_STABLE = 0x10;
|
|
318
|
+
|
|
319
|
+
constructor(transport) {
|
|
320
|
+
super(transport);
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/**
|
|
324
|
+
* Write ctrl_hum, ctrl_meas, and config registers in the correct order.
|
|
325
|
+
* @param {number} osrsT - Temperature oversampling (0–5).
|
|
326
|
+
* @param {number} osrsP - Pressure oversampling (0–5).
|
|
327
|
+
* @param {number} osrsH - Humidity oversampling (0–5).
|
|
328
|
+
* @param {number} mode - Power mode (0=sleep, 1=forced).
|
|
329
|
+
* @param {number} filter - IIR filter coefficient (0–7).
|
|
330
|
+
*/
|
|
331
|
+
configure(osrsT, osrsP, osrsH, mode, filter) {
|
|
332
|
+
this._osrsT = osrsT;
|
|
333
|
+
this._osrsP = osrsP;
|
|
334
|
+
this._osrsH = osrsH;
|
|
335
|
+
this._filter = filter;
|
|
336
|
+
this._writeReg(_REG_CTRL_HUM, osrsH);
|
|
337
|
+
this._writeReg(_REG_CONFIG, filter << 2);
|
|
338
|
+
this._writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | mode);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Update oversampling for all three TPH channels.
|
|
343
|
+
* @param {number} osrsT - Temperature oversampling (0–5).
|
|
344
|
+
* @param {number} osrsP - Pressure oversampling (0–5).
|
|
345
|
+
* @param {number} osrsH - Humidity oversampling (0–5).
|
|
346
|
+
*/
|
|
347
|
+
setOversampling(osrsT, osrsP, osrsH) {
|
|
348
|
+
this._osrsT = osrsT;
|
|
349
|
+
this._osrsP = osrsP;
|
|
350
|
+
this._osrsH = osrsH;
|
|
351
|
+
this._writeReg(_REG_CTRL_HUM, osrsH);
|
|
352
|
+
this._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
|
+
*/
|
|
359
|
+
setFilter(coeff) {
|
|
360
|
+
this._filter = coeff;
|
|
361
|
+
this._writeReg(_REG_CONFIG, coeff << 2);
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
/**
|
|
365
|
+
* Configure heater profile 0 and activate it.
|
|
366
|
+
* @param {number} tempC - Target heater temperature in degrees Celsius.
|
|
367
|
+
* @param {number} durationMs - Heater on-time in milliseconds (1–4032).
|
|
368
|
+
*/
|
|
369
|
+
setHeater(tempC, durationMs) {
|
|
370
|
+
this._heatTemp = tempC;
|
|
371
|
+
this._heatDur = durationMs;
|
|
372
|
+
this._setupHeater(0, tempC, durationMs);
|
|
373
|
+
this._writeReg(_REG_CTRL_GAS_1, (1 << 4) | 0);
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Configure one of the 10 heater profiles.
|
|
378
|
+
* @param {number} index - Profile index (0–9).
|
|
379
|
+
* @param {number} tempC - Target heater temperature in degrees Celsius.
|
|
380
|
+
* @param {number} durationMs - Heater on-time in milliseconds (1–4032).
|
|
381
|
+
*/
|
|
382
|
+
setHeaterProfile(index, tempC, durationMs) {
|
|
383
|
+
this._setupHeater(index, tempC, durationMs);
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
/**
|
|
387
|
+
* Select which heater profile to use in the next forced cycle.
|
|
388
|
+
* @param {number} index - Profile index (0–9).
|
|
389
|
+
*/
|
|
390
|
+
selectHeaterProfile(index) {
|
|
391
|
+
this._nbConv = index;
|
|
392
|
+
const gas1 = this._gasEnabled ? ((1 << 4) | index) : index;
|
|
393
|
+
this._writeReg(_REG_CTRL_GAS_1, gas1);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Enable or disable gas conversion.
|
|
398
|
+
* @param {boolean} enabled - True to enable gas measurement.
|
|
399
|
+
*/
|
|
400
|
+
setGasEnabled(enabled) {
|
|
401
|
+
this._gasEnabled = enabled;
|
|
402
|
+
const gas1 = enabled ? ((1 << 4) | this._nbConv) : this._nbConv;
|
|
403
|
+
this._writeReg(_REG_CTRL_GAS_1, gas1);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Turn the heater off or on via ctrl_gas_0.
|
|
408
|
+
* @param {boolean} off - True to disable the heater.
|
|
409
|
+
*/
|
|
410
|
+
setHeaterOff(off) {
|
|
411
|
+
this._writeReg(_REG_CTRL_GAS_0, off ? 0x08 : 0x00);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
/**
|
|
415
|
+
* Override the ambient temperature used for heater-resistance calculation.
|
|
416
|
+
* @param {number} tempC - Ambient temperature in degrees Celsius.
|
|
417
|
+
*/
|
|
418
|
+
setAmbientTemperature(tempC) {
|
|
419
|
+
this._ambientTemp = tempC;
|
|
420
|
+
this._setupHeater(this._nbConv, this._heatTemp, this._heatDur);
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Read all four sensor values from a single TPHG cycle.
|
|
425
|
+
* @returns {{ temperature: number, pressure: number, humidity: number, gasResistance: number }}
|
|
426
|
+
*/
|
|
427
|
+
readAll() {
|
|
428
|
+
const { pressAdc, tempAdc, humAdc, gasAdc, gasRange, gasValid, heatStab } = this._triggerAndRead();
|
|
429
|
+
const t = this._compensateTemp(tempAdc);
|
|
430
|
+
this._ambientTemp = t;
|
|
431
|
+
const p = this._compensatePressure(pressAdc);
|
|
432
|
+
const h = this._compensateHumidity(humAdc);
|
|
433
|
+
const g = (gasValid && heatStab) ? this._compensateGas(gasAdc, gasRange) : NaN;
|
|
434
|
+
return { temperature: t, pressure: p, humidity: h, gasResistance: g };
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
/**
|
|
438
|
+
* Check if the most recent gas reading is valid.
|
|
439
|
+
* @returns {boolean} True if gas_valid_r was set.
|
|
440
|
+
*/
|
|
441
|
+
gasValid() {
|
|
442
|
+
const raw = this._readReg(0x2B, 1);
|
|
443
|
+
return ((raw[0] >> 5) & 1) === 1;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
/**
|
|
447
|
+
* Check if the heater reached its target temperature.
|
|
448
|
+
* @returns {boolean} True if heat_stab_r was set.
|
|
449
|
+
*/
|
|
450
|
+
heaterStable() {
|
|
451
|
+
const raw = this._readReg(0x2B, 1);
|
|
452
|
+
return ((raw[0] >> 4) & 1) === 1;
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
/**
|
|
456
|
+
* Read the measurement status register.
|
|
457
|
+
* @returns {number} Status byte with flags.
|
|
458
|
+
*/
|
|
459
|
+
status() {
|
|
460
|
+
const raw = this._readReg(_REG_MEAS_STATUS, 1);
|
|
461
|
+
return raw[0];
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
/**
|
|
465
|
+
* Read the chip ID register.
|
|
466
|
+
* @returns {number} Chip ID; expect 0x61.
|
|
467
|
+
*/
|
|
468
|
+
chipId() {
|
|
469
|
+
const raw = this._readReg(_REG_ID, 1);
|
|
470
|
+
return raw[0];
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
/**
|
|
474
|
+
* Perform a soft reset, re-read calibration, and re-apply configuration.
|
|
475
|
+
*/
|
|
476
|
+
reset() {
|
|
477
|
+
this._writeReg(_REG_RESET, _RESET_CMD);
|
|
478
|
+
_delay(2);
|
|
479
|
+
this._readCalibration();
|
|
480
|
+
this._writeReg(_REG_CTRL_HUM, this._osrsH);
|
|
481
|
+
this._writeReg(_REG_CONFIG, this._filter << 2);
|
|
482
|
+
this._writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | 0);
|
|
483
|
+
this._setupHeater(this._nbConv, this._heatTemp, this._heatDur);
|
|
484
|
+
const gas1 = this._gasEnabled ? ((1 << 4) | this._nbConv) : this._nbConv;
|
|
485
|
+
this._writeReg(_REG_CTRL_GAS_1, gas1);
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
module.exports = { BME680Minimal, BME680Full };
|