periph 1.1.0 → 1.2.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 +54 -12
- package/examples/accelerometer/adxl345/complete.js +52 -0
- package/examples/accelerometer/adxl345/demo.js +34 -0
- package/examples/accelerometer/adxl345/minimal.js +18 -0
- package/examples/accelerometer/adxl362/complete.js +57 -0
- package/examples/accelerometer/adxl362/demo.js +51 -0
- package/examples/accelerometer/adxl362/minimal.js +21 -0
- package/examples/adc_dac/ad7705/complete.js +32 -0
- package/examples/adc_dac/ad7705/demo.js +42 -0
- package/examples/adc_dac/ad7705/minimal.js +16 -0
- package/examples/adc_dac/ad7706/complete.js +35 -0
- package/examples/adc_dac/ad7706/demo.js +44 -0
- package/examples/adc_dac/ad7706/minimal.js +16 -0
- package/examples/adc_dac/hx710a/complete.js +45 -0
- package/examples/adc_dac/hx710a/demo.js +52 -0
- package/examples/adc_dac/hx710a/minimal.js +16 -0
- package/examples/adc_dac/hx710b/complete.js +45 -0
- package/examples/adc_dac/hx710b/demo.js +64 -0
- package/examples/adc_dac/hx710b/minimal.js +16 -0
- package/examples/adc_dac/hx711/complete.js +7 -7
- package/examples/adc_dac/hx711/demo.js +6 -6
- package/examples/adc_dac/hx711/minimal.js +7 -7
- package/examples/adc_dac/mcp4725/complete.js +19 -17
- package/examples/adc_dac/mcp4725/demo.js +5 -5
- package/examples/adc_dac/mcp4725/minimal.js +8 -6
- package/examples/adc_dac/mcp4728/complete.js +24 -22
- package/examples/adc_dac/mcp4728/demo.js +22 -22
- package/examples/adc_dac/mcp4728/minimal.js +8 -6
- package/examples/adc_dac/pcf8591/complete.js +41 -0
- package/examples/adc_dac/pcf8591/demo.js +39 -0
- package/examples/adc_dac/pcf8591/minimal.js +19 -0
- package/examples/comms/mcp2515/complete.js +75 -0
- package/examples/comms/mcp2515/demo.js +66 -0
- package/examples/comms/mcp2515/minimal.js +27 -0
- package/examples/comms/rda5807m/complete.js +55 -0
- package/examples/comms/rda5807m/demo.js +68 -0
- package/examples/comms/rda5807m/minimal.js +15 -0
- package/examples/comms/rfm9x/complete.js +50 -0
- package/examples/comms/rfm9x/demo.js +55 -0
- package/examples/comms/rfm9x/minimal.js +20 -0
- package/examples/display/pcf8576/complete.js +18 -15
- package/examples/display/pcf8576/demo.js +6 -6
- package/examples/display/pcf8576/minimal.js +12 -10
- package/examples/environmental/aht21/complete.js +15 -13
- package/examples/environmental/aht21/demo.js +8 -6
- package/examples/environmental/aht21/minimal.js +5 -5
- package/examples/environmental/bme280/complete.js +23 -20
- package/examples/environmental/bme280/demo.js +14 -14
- package/examples/environmental/bme280/minimal.js +13 -11
- package/examples/environmental/bme680/complete.js +25 -22
- package/examples/environmental/bme680/demo.js +38 -36
- package/examples/environmental/bme680/minimal.js +14 -12
- package/examples/gas/ens160/complete.js +29 -27
- package/examples/gas/ens160/demo.js +39 -37
- package/examples/gas/ens160/minimal.js +17 -15
- package/examples/gnss/neo6/complete.js +49 -0
- package/examples/gnss/neo6/demo.js +54 -0
- package/examples/gnss/neo6/minimal.js +30 -0
- package/examples/gyroscope/l3g4200d/complete.js +56 -0
- package/examples/gyroscope/l3g4200d/demo.js +49 -0
- package/examples/gyroscope/l3g4200d/minimal.js +19 -0
- package/examples/gyroscope/l3gd20h/complete.js +42 -0
- package/examples/gyroscope/l3gd20h/demo.js +29 -0
- package/examples/gyroscope/l3gd20h/minimal.js +13 -0
- package/examples/humidity/dht11/complete.js +22 -0
- package/examples/humidity/dht11/demo.js +38 -0
- package/examples/humidity/dht11/minimal.js +18 -0
- package/examples/imu/mpu6050/complete.js +27 -25
- package/examples/imu/mpu6050/demo.js +29 -26
- package/examples/imu/mpu6050/minimal.js +6 -6
- package/examples/imu/mpu9250/complete.js +65 -0
- package/examples/imu/mpu9250/demo.js +53 -0
- package/examples/imu/mpu9250/minimal.js +21 -0
- package/examples/imu/mpu9255/complete.js +73 -0
- package/examples/imu/mpu9255/demo.js +73 -0
- package/examples/imu/mpu9255/minimal.js +21 -0
- package/examples/io_expander/mcp23017/complete.js +70 -77
- package/examples/io_expander/mcp23017/demo.js +37 -33
- package/examples/io_expander/mcp23017/minimal.js +15 -11
- package/examples/io_expander/pcf8574/complete.js +60 -65
- package/examples/io_expander/pcf8574/demo.js +30 -25
- package/examples/io_expander/pcf8574/minimal.js +15 -11
- package/examples/io_expander/pcf8575/complete.js +24 -20
- package/examples/io_expander/pcf8575/demo.js +27 -23
- package/examples/io_expander/pcf8575/minimal.js +16 -12
- package/examples/io_expander/tpic6b595/complete.js +57 -0
- package/examples/io_expander/tpic6b595/demo.js +75 -0
- package/examples/io_expander/tpic6b595/minimal.js +26 -0
- package/examples/led/apa102/complete.js +72 -0
- package/examples/led/apa102/demo.js +92 -0
- package/examples/led/apa102/minimal.js +21 -0
- package/examples/led/sk6812rgbw/complete.js +42 -40
- package/examples/led/sk6812rgbw/demo.js +16 -16
- package/examples/led/sk6812rgbw/minimal.js +15 -17
- package/examples/led/ws2812b/complete.js +40 -38
- package/examples/led/ws2812b/demo.js +16 -16
- package/examples/led/ws2812b/minimal.js +13 -14
- package/examples/led/ws2814/complete.js +71 -0
- package/examples/led/ws2814/demo.js +82 -0
- package/examples/led/ws2814/minimal.js +23 -0
- package/examples/light/apds-9930/complete.js +46 -0
- package/examples/light/apds-9930/demo.js +31 -0
- package/examples/light/apds-9930/minimal.js +20 -0
- package/examples/light/apds9960/complete.js +45 -43
- package/examples/light/apds9960/demo.js +11 -9
- package/examples/light/apds9960/minimal.js +5 -5
- package/examples/magnetometer/as5600/complete.js +31 -29
- package/examples/magnetometer/as5600/demo.js +35 -33
- package/examples/magnetometer/as5600/minimal.js +7 -7
- package/examples/magnetometer/hmc5883l/complete.js +47 -0
- package/examples/magnetometer/hmc5883l/demo.js +50 -0
- package/examples/magnetometer/hmc5883l/minimal.js +15 -0
- package/examples/memory/24aa02uid/complete.js +23 -21
- package/examples/memory/24aa02uid/demo.js +32 -30
- package/examples/memory/24aa02uid/minimal.js +5 -5
- package/examples/other/mpr121/complete.js +50 -0
- package/examples/other/mpr121/demo.js +30 -0
- package/examples/other/mpr121/minimal.js +17 -0
- package/examples/power/ade7953/complete.js +44 -0
- package/examples/power/ade7953/demo.js +28 -0
- package/examples/power/ade7953/minimal.js +15 -0
- package/examples/power/ina219/complete.js +20 -18
- package/examples/power/ina219/demo.js +15 -13
- package/examples/power/ina219/minimal.js +8 -8
- package/examples/power/ina226/complete.js +24 -22
- package/examples/power/ina226/demo.js +22 -20
- package/examples/power/ina226/minimal.js +5 -5
- package/examples/power/ina3221/complete.js +51 -49
- package/examples/power/ina3221/demo.js +13 -13
- package/examples/power/ina3221/minimal.js +8 -8
- package/examples/pressure/bmp085/complete.js +29 -0
- package/examples/pressure/bmp085/demo.js +34 -0
- package/examples/pressure/bmp085/minimal.js +19 -0
- package/examples/pressure/bmp180/complete.js +20 -17
- package/examples/pressure/bmp180/demo.js +24 -22
- package/examples/pressure/bmp180/minimal.js +13 -11
- package/examples/pressure/bmp280/complete.js +23 -20
- package/examples/pressure/bmp280/demo.js +44 -41
- package/examples/pressure/bmp280/minimal.js +13 -11
- package/examples/pressure/bmp384/complete.js +29 -0
- package/examples/pressure/bmp384/demo.js +40 -0
- package/examples/pressure/bmp384/minimal.js +18 -0
- package/examples/pressure/bmp581/complete.js +34 -0
- package/examples/pressure/bmp581/demo.js +56 -0
- package/examples/pressure/bmp581/minimal.js +19 -0
- package/examples/pressure/lps22df/complete.js +50 -0
- package/examples/pressure/lps22df/demo.js +41 -0
- package/examples/pressure/lps22df/minimal.js +19 -0
- package/examples/pressure/lps28dfw/complete.js +37 -0
- package/examples/pressure/lps28dfw/demo.js +33 -0
- package/examples/pressure/lps28dfw/minimal.js +19 -0
- package/examples/pressure/lps33hw/complete.js +43 -0
- package/examples/pressure/lps33hw/demo.js +43 -0
- package/examples/pressure/lps33hw/minimal.js +19 -0
- package/examples/rfid/mfrc522/complete.js +44 -0
- package/examples/rfid/mfrc522/demo.js +70 -0
- package/examples/rfid/mfrc522/minimal.js +17 -0
- package/package.json +5 -1
- package/src/chips/accelerometer/adxl345.js +482 -0
- package/src/chips/accelerometer/adxl362.js +594 -0
- package/src/chips/adc_dac/ad7705.js +345 -0
- package/src/chips/adc_dac/ad7706.js +331 -0
- package/src/chips/adc_dac/hx710a.js +184 -0
- package/src/chips/adc_dac/hx710b.js +186 -0
- package/src/chips/adc_dac/hx711.js +15 -15
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +57 -44
- package/src/chips/adc_dac/pcf8591.js +196 -0
- package/src/chips/comms/mcp2515.js +659 -0
- package/src/chips/comms/rda5807m.js +393 -0
- package/src/chips/comms/rfm9x.js +590 -0
- package/src/chips/display/pcf8576.js +42 -33
- package/src/chips/environmental/aht21.js +52 -37
- package/src/chips/environmental/bme280.js +85 -66
- package/src/chips/environmental/bme680.js +110 -87
- package/src/chips/gas/ens160.js +82 -64
- package/src/chips/gnss/neo6.js +438 -0
- package/src/chips/gyroscope/l3g4200d.js +366 -0
- package/src/chips/gyroscope/l3gd20h.js +314 -0
- package/src/chips/humidity/dht11.js +135 -0
- package/src/chips/imu/mpu6050.js +89 -66
- package/src/chips/imu/mpu9250.js +394 -0
- package/src/chips/imu/mpu9255.js +447 -0
- package/src/chips/io_expander/mcp23017.js +240 -157
- package/src/chips/io_expander/pcf8574.js +149 -124
- package/src/chips/io_expander/pcf8575.js +164 -135
- package/src/chips/io_expander/tpic6b595.js +242 -0
- package/src/chips/led/_NeoPixelRGBBase.js +204 -0
- package/src/chips/led/_NeoPixelRGBWBase.js +218 -0
- package/src/chips/led/apa102.js +236 -0
- package/src/chips/led/sk6812rgbw.js +15 -149
- package/src/chips/led/ws2812b.js +14 -139
- package/src/chips/led/ws2814.js +49 -0
- package/src/chips/light/apds-9930.js +342 -0
- package/src/chips/light/apds9960.js +158 -123
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +32 -29
- package/src/chips/other/mpr121.js +375 -0
- package/src/chips/power/ade7953.js +427 -0
- package/src/chips/power/ina219.js +45 -40
- package/src/chips/power/ina226.js +50 -45
- package/src/chips/power/ina3221.js +95 -76
- package/src/chips/pressure/bmp085.js +251 -0
- package/src/chips/pressure/bmp180.js +54 -37
- package/src/chips/pressure/bmp280.js +72 -53
- package/src/chips/pressure/bmp384.js +410 -0
- package/src/chips/pressure/bmp581.js +489 -0
- package/src/chips/pressure/lps22df.js +341 -0
- package/src/chips/pressure/lps28dfw.js +314 -0
- package/src/chips/pressure/lps33hw.js +255 -0
- package/src/chips/rfid/mfrc522.js +674 -0
- package/src/connection/connection.js +111 -0
- package/src/connection/dhtxx.js +138 -0
- package/src/connection/dhtxx_mock.js +39 -0
- package/src/{transport → connection}/hx711.js +49 -22
- package/src/connection/hx711_mock.js +52 -0
- package/src/{transport → connection}/i2c.js +13 -9
- package/src/connection/i2c_mock.js +91 -0
- package/src/connection/input_pin.js +156 -0
- package/src/connection/neo6_mock.js +45 -0
- package/src/{transport → connection}/neopixel.js +23 -4
- package/src/connection/output_pin.js +41 -0
- package/src/connection/sipo.js +159 -0
- package/src/{transport → connection}/smbus.js +14 -10
- package/src/{transport → connection}/spi.js +12 -8
- package/src/connection/spi_mock.js +151 -0
- package/src/{transport → connection}/uart.js +33 -26
- package/examples/gnss/.gitkeep +0 -0
- package/examples/humidity/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
|
@@ -22,16 +22,25 @@ function _delay(ms) {
|
|
|
22
22
|
* BMP280 piezo-resistive pressure + temperature sensor — minimal interface.
|
|
23
23
|
*
|
|
24
24
|
* Provides calibrated temperature (°C) and pressure (hPa) with no configuration
|
|
25
|
-
* beyond the
|
|
25
|
+
* beyond the connection. I²C address is 0x76 (SDO=GND) or 0x77 (SDO=VDDIO).
|
|
26
26
|
*
|
|
27
27
|
* Default: forced mode, osrs_t=×1, osrs_p=×1, IIR filter off.
|
|
28
28
|
*
|
|
29
|
-
*
|
|
29
|
+
* Constructor caveat: JS constructors cannot be async. The calibration read
|
|
30
|
+
* and initial register writes are fired off unawaited, matching the
|
|
31
|
+
* fire-and-forget convention used throughout this port — in practice they
|
|
32
|
+
* settle before any subsequent `await` in caller code is scheduled, since
|
|
33
|
+
* the underlying I2C/SPI connection is synchronous under the hood, but this
|
|
34
|
+
* is not a guaranteed blocking wait the way the pre-async version was. Call
|
|
35
|
+
* `await sensor.temperature()` (or any other async method) once before
|
|
36
|
+
* relying on calibrated readings if this matters for your use case.
|
|
37
|
+
*
|
|
38
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
|
|
30
39
|
* @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
|
|
31
40
|
*/
|
|
32
41
|
class BMP280Minimal {
|
|
33
|
-
constructor(
|
|
34
|
-
this.
|
|
42
|
+
constructor(connection, busType = 'i2c') {
|
|
43
|
+
this._conn = connection;
|
|
35
44
|
this._busType = busType;
|
|
36
45
|
this._mode = 0;
|
|
37
46
|
this._osrsT = 1;
|
|
@@ -39,13 +48,17 @@ class BMP280Minimal {
|
|
|
39
48
|
this._filter = 0;
|
|
40
49
|
this._tSb = 0;
|
|
41
50
|
this._tFine = 0;
|
|
42
|
-
this.
|
|
43
|
-
|
|
44
|
-
|
|
51
|
+
this._init();
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async _init() {
|
|
55
|
+
await this._readCalibration();
|
|
56
|
+
await this._writeReg(_REG_CTRL_MEAS, (1 << 5) | (1 << 2) | 0);
|
|
57
|
+
await this._writeReg(_REG_CONFIG, 0);
|
|
45
58
|
}
|
|
46
59
|
|
|
47
|
-
_readCalibration() {
|
|
48
|
-
const data = this.
|
|
60
|
+
async _readCalibration() {
|
|
61
|
+
const data = await this._conn.writeRead(Buffer.from([_REG_CAL_START]), 24);
|
|
49
62
|
this._digT1 = data.readUInt16LE(0);
|
|
50
63
|
this._digT2 = data.readInt16LE(2);
|
|
51
64
|
this._digT3 = data.readInt16LE(4);
|
|
@@ -60,22 +73,22 @@ class BMP280Minimal {
|
|
|
60
73
|
this._digP9 = data.readInt16LE(22);
|
|
61
74
|
}
|
|
62
75
|
|
|
63
|
-
_writeReg(reg, value) {
|
|
76
|
+
async _writeReg(reg, value) {
|
|
64
77
|
const addr = this._busType === 'spi' ? (reg & 0x7F) : reg;
|
|
65
|
-
this.
|
|
78
|
+
await this._conn.write(Buffer.from([addr, value]));
|
|
66
79
|
}
|
|
67
80
|
|
|
68
|
-
_readReg(reg, n) {
|
|
69
|
-
return this.
|
|
81
|
+
async _readReg(reg, n) {
|
|
82
|
+
return this._conn.writeRead(Buffer.from([reg]), n);
|
|
70
83
|
}
|
|
71
84
|
|
|
72
|
-
_triggerAndRead() {
|
|
85
|
+
async _triggerAndRead() {
|
|
73
86
|
if (this._mode !== 3) {
|
|
74
87
|
const ctrl = (this._osrsT << 5) | (this._osrsP << 2) | 1;
|
|
75
|
-
this._writeReg(_REG_CTRL_MEAS, ctrl);
|
|
88
|
+
await this._writeReg(_REG_CTRL_MEAS, ctrl);
|
|
76
89
|
_delay(_MEAS_TIME_MS);
|
|
77
90
|
}
|
|
78
|
-
const raw = this._readReg(_REG_DATA_START, 6);
|
|
91
|
+
const raw = await this._readReg(_REG_DATA_START, 6);
|
|
79
92
|
const adcP = (raw[0] << 12) | (raw[1] << 4) | (raw[2] >> 4);
|
|
80
93
|
const adcT = (raw[3] << 12) | (raw[4] << 4) | (raw[5] >> 4);
|
|
81
94
|
return { adcP, adcT };
|
|
@@ -120,10 +133,10 @@ class BMP280Minimal {
|
|
|
120
133
|
|
|
121
134
|
/**
|
|
122
135
|
* Read calibrated temperature.
|
|
123
|
-
* @returns {number} Temperature in degrees Celsius.
|
|
136
|
+
* @returns {Promise<number>} Temperature in degrees Celsius.
|
|
124
137
|
*/
|
|
125
|
-
temperature() {
|
|
126
|
-
const { adcP, adcT } = this._triggerAndRead();
|
|
138
|
+
async temperature() {
|
|
139
|
+
const { adcP, adcT } = await this._triggerAndRead();
|
|
127
140
|
return this._compensateTemp(adcT);
|
|
128
141
|
}
|
|
129
142
|
|
|
@@ -133,10 +146,10 @@ class BMP280Minimal {
|
|
|
133
146
|
* Reads both ADCs and refreshes t_fine.
|
|
134
147
|
* Self-contained — may be called without a prior temperature() call.
|
|
135
148
|
*
|
|
136
|
-
* @returns {number} Pressure in hPa.
|
|
149
|
+
* @returns {Promise<number>} Pressure in hPa.
|
|
137
150
|
*/
|
|
138
|
-
pressure() {
|
|
139
|
-
const { adcP, adcT } = this._triggerAndRead();
|
|
151
|
+
async pressure() {
|
|
152
|
+
const { adcP, adcT } = await this._triggerAndRead();
|
|
140
153
|
this._compensateTemp(adcT);
|
|
141
154
|
return this._compensatePressure(adcP);
|
|
142
155
|
}
|
|
@@ -145,7 +158,7 @@ class BMP280Minimal {
|
|
|
145
158
|
/**
|
|
146
159
|
* BMP280 full interface — extends BMP280Minimal with configuration and altitude helpers.
|
|
147
160
|
*
|
|
148
|
-
* @param {
|
|
161
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
|
|
149
162
|
* @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
|
|
150
163
|
*/
|
|
151
164
|
class BMP280Full extends BMP280Minimal {
|
|
@@ -178,8 +191,8 @@ class BMP280Full extends BMP280Minimal {
|
|
|
178
191
|
static STATUS_MEASURING = 0x08;
|
|
179
192
|
static STATUS_IM_UPDATE = 0x01;
|
|
180
193
|
|
|
181
|
-
constructor(
|
|
182
|
-
super(
|
|
194
|
+
constructor(connection, busType = 'i2c') {
|
|
195
|
+
super(connection, busType);
|
|
183
196
|
}
|
|
184
197
|
|
|
185
198
|
/**
|
|
@@ -189,102 +202,108 @@ class BMP280Full extends BMP280Minimal {
|
|
|
189
202
|
* @param {number} mode - Power mode (0=sleep, 1=forced, 3=normal).
|
|
190
203
|
* @param {number} filter - IIR filter coefficient (0–4).
|
|
191
204
|
* @param {number} tSb - Standby time in normal mode (0–7).
|
|
205
|
+
* @returns {Promise<void>}
|
|
192
206
|
*/
|
|
193
|
-
configure(osrsT, osrsP, mode, filter, tSb) {
|
|
207
|
+
async configure(osrsT, osrsP, mode, filter, tSb) {
|
|
194
208
|
this._osrsT = osrsT;
|
|
195
209
|
this._osrsP = osrsP;
|
|
196
210
|
this._mode = mode;
|
|
197
211
|
this._filter = filter;
|
|
198
212
|
this._tSb = tSb;
|
|
199
|
-
this._writeReg(_REG_CONFIG, (tSb << 5) | (filter << 2));
|
|
200
|
-
this._writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | mode);
|
|
213
|
+
await this._writeReg(_REG_CONFIG, (tSb << 5) | (filter << 2));
|
|
214
|
+
await this._writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | mode);
|
|
201
215
|
}
|
|
202
216
|
|
|
203
217
|
/**
|
|
204
218
|
* Update temperature and pressure oversampling.
|
|
205
219
|
* @param {number} osrsT - Temperature oversampling (0–5).
|
|
206
220
|
* @param {number} osrsP - Pressure oversampling (0–5).
|
|
221
|
+
* @returns {Promise<void>}
|
|
207
222
|
*/
|
|
208
|
-
setOversampling(osrsT, osrsP) {
|
|
223
|
+
async setOversampling(osrsT, osrsP) {
|
|
209
224
|
this._osrsT = osrsT;
|
|
210
225
|
this._osrsP = osrsP;
|
|
211
|
-
this._writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | this._mode);
|
|
226
|
+
await this._writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | this._mode);
|
|
212
227
|
}
|
|
213
228
|
|
|
214
229
|
/**
|
|
215
230
|
* Update power mode.
|
|
216
231
|
* @param {number} mode - Power mode (0=sleep, 1=forced, 3=normal).
|
|
232
|
+
* @returns {Promise<void>}
|
|
217
233
|
*/
|
|
218
|
-
setMode(mode) {
|
|
234
|
+
async setMode(mode) {
|
|
219
235
|
this._mode = mode;
|
|
220
|
-
this._writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | mode);
|
|
236
|
+
await this._writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | mode);
|
|
221
237
|
}
|
|
222
238
|
|
|
223
239
|
/**
|
|
224
240
|
* Update IIR filter coefficient.
|
|
225
241
|
* @param {number} coeff - Filter coefficient (0–4).
|
|
242
|
+
* @returns {Promise<void>}
|
|
226
243
|
*/
|
|
227
|
-
setFilter(coeff) {
|
|
244
|
+
async setFilter(coeff) {
|
|
228
245
|
this._filter = coeff;
|
|
229
|
-
this._writeReg(_REG_CONFIG, (this._tSb << 5) | (coeff << 2));
|
|
246
|
+
await this._writeReg(_REG_CONFIG, (this._tSb << 5) | (coeff << 2));
|
|
230
247
|
}
|
|
231
248
|
|
|
232
249
|
/**
|
|
233
250
|
* Update standby time for normal mode.
|
|
234
251
|
* @param {number} tSb - Standby time value (0–7).
|
|
252
|
+
* @returns {Promise<void>}
|
|
235
253
|
*/
|
|
236
|
-
setStandby(tSb) {
|
|
254
|
+
async setStandby(tSb) {
|
|
237
255
|
this._tSb = tSb;
|
|
238
|
-
this._writeReg(_REG_CONFIG, (tSb << 5) | (this._filter << 2));
|
|
256
|
+
await this._writeReg(_REG_CONFIG, (tSb << 5) | (this._filter << 2));
|
|
239
257
|
}
|
|
240
258
|
|
|
241
259
|
/**
|
|
242
260
|
* Read the status register.
|
|
243
|
-
* @returns {number} Status byte; bit 3 = measuring, bit 0 = im_update.
|
|
261
|
+
* @returns {Promise<number>} Status byte; bit 3 = measuring, bit 0 = im_update.
|
|
244
262
|
*/
|
|
245
|
-
status() {
|
|
246
|
-
const data = this._readReg(_REG_STATUS, 1);
|
|
263
|
+
async status() {
|
|
264
|
+
const data = await this._readReg(_REG_STATUS, 1);
|
|
247
265
|
return data[0];
|
|
248
266
|
}
|
|
249
267
|
|
|
250
268
|
/**
|
|
251
269
|
* Compute altitude above sea level from the current pressure.
|
|
252
270
|
* @param {number} [seaLevelHpa=1013.25] - Reference sea-level pressure in hPa.
|
|
253
|
-
* @returns {number} Altitude in metres.
|
|
271
|
+
* @returns {Promise<number>} Altitude in metres.
|
|
254
272
|
*/
|
|
255
|
-
altitude(seaLevelHpa = 1013.25) {
|
|
256
|
-
const p = this.pressure();
|
|
273
|
+
async altitude(seaLevelHpa = 1013.25) {
|
|
274
|
+
const p = await this.pressure();
|
|
257
275
|
return 44330 * (1 - Math.pow(p / seaLevelHpa, 1 / 5.255));
|
|
258
276
|
}
|
|
259
277
|
|
|
260
278
|
/**
|
|
261
279
|
* Compute sea-level pressure from current pressure and known altitude.
|
|
262
280
|
* @param {number} altitudeM - Altitude in metres.
|
|
263
|
-
* @returns {number} Sea-level pressure in hPa.
|
|
281
|
+
* @returns {Promise<number>} Sea-level pressure in hPa.
|
|
264
282
|
*/
|
|
265
|
-
seaLevelPressure(altitudeM) {
|
|
266
|
-
const p = this.pressure();
|
|
283
|
+
async seaLevelPressure(altitudeM) {
|
|
284
|
+
const p = await this.pressure();
|
|
267
285
|
return p / Math.pow(1 - altitudeM / 44330, 5.255);
|
|
268
286
|
}
|
|
269
287
|
|
|
270
288
|
/**
|
|
271
289
|
* Read the chip ID register.
|
|
272
|
-
* @returns {number} Chip ID; expect 0x58.
|
|
290
|
+
* @returns {Promise<number>} Chip ID; expect 0x58.
|
|
273
291
|
*/
|
|
274
|
-
chipId() {
|
|
275
|
-
const data = this._readReg(_REG_ID, 1);
|
|
292
|
+
async chipId() {
|
|
293
|
+
const data = await this._readReg(_REG_ID, 1);
|
|
276
294
|
return data[0];
|
|
277
295
|
}
|
|
278
296
|
|
|
279
297
|
/**
|
|
280
298
|
* Perform a soft reset, re-read calibration, and re-apply configuration.
|
|
299
|
+
* @returns {Promise<void>}
|
|
281
300
|
*/
|
|
282
|
-
reset() {
|
|
283
|
-
this._writeReg(_REG_RESET, _RESET_CMD);
|
|
301
|
+
async reset() {
|
|
302
|
+
await this._writeReg(_REG_RESET, _RESET_CMD);
|
|
284
303
|
_delay(2);
|
|
285
|
-
this._readCalibration();
|
|
286
|
-
this._writeReg(_REG_CONFIG, (this._tSb << 5) | (this._filter << 2));
|
|
287
|
-
this._writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | this._mode);
|
|
304
|
+
await this._readCalibration();
|
|
305
|
+
await this._writeReg(_REG_CONFIG, (this._tSb << 5) | (this._filter << 2));
|
|
306
|
+
await this._writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | this._mode);
|
|
288
307
|
}
|
|
289
308
|
}
|
|
290
309
|
|
|
@@ -0,0 +1,410 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_CHIP_ID = 0x00;
|
|
4
|
+
const _REG_STATUS = 0x03;
|
|
5
|
+
const _REG_DATA_0 = 0x04;
|
|
6
|
+
const _REG_PWR_CTRL = 0x1B;
|
|
7
|
+
const _REG_OSR = 0x1C;
|
|
8
|
+
const _REG_ODR = 0x1D;
|
|
9
|
+
const _REG_CONFIG = 0x1F;
|
|
10
|
+
const _REG_CMD = 0x7E;
|
|
11
|
+
const _REG_CAL_START = 0x31;
|
|
12
|
+
const _REG_CAL_LEN = 21;
|
|
13
|
+
|
|
14
|
+
const _CHIP_ID = 0x50;
|
|
15
|
+
const _SOFT_RESET_CMD = 0xB6;
|
|
16
|
+
const _FIFO_FLUSH_CMD = 0xB0;
|
|
17
|
+
|
|
18
|
+
const _MODE_SLEEP = 0x00;
|
|
19
|
+
const _MODE_FORCED = 0x01;
|
|
20
|
+
const _MODE_NORMAL = 0x03;
|
|
21
|
+
|
|
22
|
+
const _PWR_PRESS_EN = 0x01;
|
|
23
|
+
const _PWR_TEMP_EN = 0x02;
|
|
24
|
+
|
|
25
|
+
const _FIFO_HEADER_PRESS = 0x84;
|
|
26
|
+
const _FIFO_HEADER_TEMP = 0x90;
|
|
27
|
+
const _FIFO_HEADER_SENSORT = 0xA0;
|
|
28
|
+
const _FIFO_HEADER_ERROR = 0x44;
|
|
29
|
+
const _FIFO_HEADER_EMPTY = 0x80;
|
|
30
|
+
|
|
31
|
+
const _MEAS_TIME_MS = 40;
|
|
32
|
+
|
|
33
|
+
function _delay(ms) {
|
|
34
|
+
const start = Date.now();
|
|
35
|
+
while (Date.now() - start < ms) { /* spin */ }
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function _s8(b) {
|
|
39
|
+
return b >= 128 ? b - 256 : b;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* BMP384 high-precision barometric pressure and temperature sensor — minimal interface.
|
|
44
|
+
*
|
|
45
|
+
* Provides calibrated temperature (°C) and pressure (hPa) with no configuration
|
|
46
|
+
* beyond the connection. I²C address is 0x76 (SDO=GND) or 0x77 (SDO=VDD).
|
|
47
|
+
*
|
|
48
|
+
* Default: normal mode, osr_p=×16, osr_t=×2, iir=coef 3, ODR=25 Hz.
|
|
49
|
+
*
|
|
50
|
+
* Constructor caveat: JS constructors cannot be async. The calibration read
|
|
51
|
+
* and initial register writes are fired off unawaited, matching the
|
|
52
|
+
* fire-and-forget convention used throughout this port — in practice they
|
|
53
|
+
* settle before any subsequent `await` in caller code is scheduled, since
|
|
54
|
+
* the underlying I2C/SPI connection is synchronous under the hood, but this
|
|
55
|
+
* is not a guaranteed blocking wait the way the pre-async version was. Call
|
|
56
|
+
* `await sensor.temperature()` (or any other async method) once before
|
|
57
|
+
* relying on calibrated readings if this matters for your use case.
|
|
58
|
+
*
|
|
59
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
|
|
60
|
+
* @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
|
|
61
|
+
*/
|
|
62
|
+
class BMP384Minimal {
|
|
63
|
+
constructor(connection, busType = 'i2c') {
|
|
64
|
+
this._conn = connection;
|
|
65
|
+
this._busType = busType;
|
|
66
|
+
this._osrP = 4; // ×16
|
|
67
|
+
this._osrT = 1; // ×2
|
|
68
|
+
this._iir = 2; // coefficient 3
|
|
69
|
+
this._odr = 0x03; // 25 Hz
|
|
70
|
+
this._mode = _MODE_NORMAL;
|
|
71
|
+
this._init();
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async _init() {
|
|
75
|
+
await this._readCalibration();
|
|
76
|
+
await this._applyConfig();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async _readCalibration() {
|
|
80
|
+
const data = await this._conn.writeRead(Buffer.from([_REG_CAL_START]), _REG_CAL_LEN);
|
|
81
|
+
// NVM_PAR_T1 (u16 LE)
|
|
82
|
+
this._parT1 = data.readUInt16LE(0);
|
|
83
|
+
// NVM_PAR_T2 (u16 LE)
|
|
84
|
+
this._parT2 = data.readUInt16LE(2);
|
|
85
|
+
// NVM_PAR_T3 (s8)
|
|
86
|
+
this._parT3 = _s8(data[4]);
|
|
87
|
+
// NVM_PAR_P1 (s16 LE)
|
|
88
|
+
this._parP1 = data.readInt16LE(5);
|
|
89
|
+
// NVM_PAR_P2 (s16 LE)
|
|
90
|
+
this._parP2 = data.readInt16LE(7);
|
|
91
|
+
// NVM_PAR_P3 (s8)
|
|
92
|
+
this._parP3 = _s8(data[9]);
|
|
93
|
+
// NVM_PAR_P4 (s8)
|
|
94
|
+
this._parP4 = _s8(data[10]);
|
|
95
|
+
// NVM_PAR_P5 (u16 LE)
|
|
96
|
+
this._parP5 = data.readUInt16LE(11);
|
|
97
|
+
// NVM_PAR_P6 (u16 LE)
|
|
98
|
+
this._parP6 = data.readUInt16LE(13);
|
|
99
|
+
// NVM_PAR_P7 (s8)
|
|
100
|
+
this._parP7 = _s8(data[15]);
|
|
101
|
+
// NVM_PAR_P8 (s8)
|
|
102
|
+
this._parP8 = _s8(data[16]);
|
|
103
|
+
// NVM_PAR_P9 (s16 LE)
|
|
104
|
+
this._parP9 = data.readInt16LE(17);
|
|
105
|
+
// NVM_PAR_P10 (s8)
|
|
106
|
+
this._parP10 = _s8(data[19]);
|
|
107
|
+
// NVM_PAR_P11 (s8)
|
|
108
|
+
this._parP11 = _s8(data[20]);
|
|
109
|
+
|
|
110
|
+
// Convert raw NVM coefficients to floating-point PAR values per datasheet.
|
|
111
|
+
// PAR_T1 = NVM_PAR_T1 × 256
|
|
112
|
+
this._parT1 = this._parT1 * 256.0;
|
|
113
|
+
this._parT2 = this._parT2 / Math.pow(2, 30);
|
|
114
|
+
this._parT3 = this._parT3 / Math.pow(2, 48);
|
|
115
|
+
this._parP1 = (this._parP1 - Math.pow(2, 14)) / Math.pow(2, 20);
|
|
116
|
+
this._parP2 = (this._parP2 - Math.pow(2, 14)) / Math.pow(2, 29);
|
|
117
|
+
this._parP3 = this._parP3 / Math.pow(2, 32);
|
|
118
|
+
this._parP4 = this._parP4 / Math.pow(2, 37);
|
|
119
|
+
this._parP5 = this._parP5 * 8.0;
|
|
120
|
+
this._parP6 = this._parP6 / Math.pow(2, 6);
|
|
121
|
+
this._parP7 = this._parP7 / Math.pow(2, 8);
|
|
122
|
+
this._parP8 = this._parP8 / Math.pow(2, 15);
|
|
123
|
+
this._parP9 = this._parP9 / Math.pow(2, 48);
|
|
124
|
+
this._parP10 = this._parP10 / Math.pow(2, 48);
|
|
125
|
+
this._parP11 = this._parP11 / Math.pow(2, 65);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async _applyConfig() {
|
|
129
|
+
const osrReg = (this._osrT << 3) | (this._osrP << 0);
|
|
130
|
+
const configReg = (this._iir << 1);
|
|
131
|
+
const pwrReg = (this._mode << 4) | _PWR_TEMP_EN | _PWR_PRESS_EN;
|
|
132
|
+
await this._writeReg(_REG_OSR, osrReg);
|
|
133
|
+
await this._writeReg(_REG_CONFIG, configReg);
|
|
134
|
+
await this._writeReg(_REG_ODR, this._odr);
|
|
135
|
+
await this._writeReg(_REG_PWR_CTRL, pwrReg);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
async _writeReg(reg, value) {
|
|
139
|
+
const addr = this._busType === 'spi' ? (reg & 0x7F) : reg;
|
|
140
|
+
await this._conn.write(Buffer.from([addr, value]));
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
async _readReg(reg, n) {
|
|
144
|
+
return this._conn.writeRead(Buffer.from([reg]), n);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async _readBurst() {
|
|
148
|
+
// DATA_0..DATA_5 (0x04..0x09) = pressure XLSB/LSB/MSB, then temperature XLSB/LSB/MSB
|
|
149
|
+
const raw = await this._readReg(_REG_DATA_0, 6);
|
|
150
|
+
const uncompPress = (raw[2] << 16) | (raw[1] << 8) | raw[0];
|
|
151
|
+
const uncompTemp = (raw[5] << 16) | (raw[4] << 8) | raw[3];
|
|
152
|
+
return { uncompPress, uncompTemp };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
_compensateTemperature(uncompTemp) {
|
|
156
|
+
const partial1 = uncompTemp - this._parT1;
|
|
157
|
+
const partial2 = partial1 * this._parT2;
|
|
158
|
+
this._tLin = partial2 + (partial1 * partial1) * this._parT3;
|
|
159
|
+
return this._tLin;
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
_compensatePressure(uncompPress) {
|
|
163
|
+
const tLin = this._tLin;
|
|
164
|
+
const p = Number(uncompPress);
|
|
165
|
+
|
|
166
|
+
const partial1a = this._parP6 * tLin;
|
|
167
|
+
const partial2a = this._parP7 * tLin * tLin;
|
|
168
|
+
const partial3a = this._parP8 * tLin * tLin * tLin;
|
|
169
|
+
const partialOut1 = this._parP5 + partial1a + partial2a + partial3a;
|
|
170
|
+
|
|
171
|
+
const partial1b = this._parP2 * tLin;
|
|
172
|
+
const partial2b = this._parP3 * tLin * tLin;
|
|
173
|
+
const partial3b = this._parP4 * tLin * tLin * tLin;
|
|
174
|
+
const partialOut2 = p * (this._parP1 + partial1b + partial2b + partial3b);
|
|
175
|
+
|
|
176
|
+
const partial1c = p * p;
|
|
177
|
+
const partial2c = this._parP9 + this._parP10 * tLin;
|
|
178
|
+
const partial3c = partial1c * partial2c;
|
|
179
|
+
const partial4 = partial3c + (p * p * p) * this._parP11;
|
|
180
|
+
|
|
181
|
+
return partialOut1 + partialOut2 + partial4;
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Read calibrated temperature.
|
|
186
|
+
* @returns {Promise<number>} Temperature in degrees Celsius.
|
|
187
|
+
*/
|
|
188
|
+
async temperature() {
|
|
189
|
+
if (this._mode === _MODE_FORCED) {
|
|
190
|
+
const pwrReg = (_MODE_FORCED << 4) | _PWR_TEMP_EN | _PWR_PRESS_EN;
|
|
191
|
+
await this._writeReg(_REG_PWR_CTRL, pwrReg);
|
|
192
|
+
_delay(_MEAS_TIME_MS);
|
|
193
|
+
}
|
|
194
|
+
const { uncompTemp } = await this._readBurst();
|
|
195
|
+
return this._compensateTemperature(uncompTemp);
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Read calibrated pressure.
|
|
200
|
+
* @returns {Promise<number>} Pressure in hectopascals (hPa).
|
|
201
|
+
*/
|
|
202
|
+
async pressure() {
|
|
203
|
+
if (this._mode === _MODE_FORCED) {
|
|
204
|
+
const pwrReg = (_MODE_FORCED << 4) | _PWR_TEMP_EN | _PWR_PRESS_EN;
|
|
205
|
+
await this._writeReg(_REG_PWR_CTRL, pwrReg);
|
|
206
|
+
_delay(_MEAS_TIME_MS);
|
|
207
|
+
}
|
|
208
|
+
const { uncompPress, uncompTemp } = await this._readBurst();
|
|
209
|
+
this._compensateTemperature(uncompTemp);
|
|
210
|
+
return this._compensatePressure(uncompPress) / 100.0;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
/**
|
|
215
|
+
* BMP384 full interface — extends BMP384Minimal with configuration, mode control, and FIFO access.
|
|
216
|
+
*
|
|
217
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
|
|
218
|
+
* @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
|
|
219
|
+
*/
|
|
220
|
+
class BMP384Full extends BMP384Minimal {
|
|
221
|
+
static MODE_SLEEP = 0x00;
|
|
222
|
+
static MODE_FORCED = 0x01;
|
|
223
|
+
static MODE_NORMAL = 0x03;
|
|
224
|
+
|
|
225
|
+
constructor(connection, busType = 'i2c') {
|
|
226
|
+
super(connection, busType);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Write OSR, CONFIG, and ODR; does not enforce ODR ≥ T_conv at the JS layer.
|
|
231
|
+
* @param {number} osrP - Pressure oversampling (0–5).
|
|
232
|
+
* @param {number} osrT - Temperature oversampling (0–5).
|
|
233
|
+
* @param {number} iirFilter - IIR filter coefficient index (0–7).
|
|
234
|
+
* @param {number} odrSel - Output data rate selector (0x00–0x11).
|
|
235
|
+
* @returns {Promise<void>}
|
|
236
|
+
*/
|
|
237
|
+
async configure(osrP, osrT, iirFilter, odrSel) {
|
|
238
|
+
this._osrP = osrP;
|
|
239
|
+
this._osrT = osrT;
|
|
240
|
+
this._iir = iirFilter;
|
|
241
|
+
this._odr = odrSel;
|
|
242
|
+
await this._writeReg(_REG_OSR, (osrT << 3) | (osrP << 0));
|
|
243
|
+
await this._writeReg(_REG_CONFIG, (iirFilter << 1));
|
|
244
|
+
await this._writeReg(_REG_ODR, odrSel);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Read both pressure and temperature in a single burst.
|
|
249
|
+
* @returns {Promise<{pressure:number, temperature:number}>} pressure in hPa, temperature in °C.
|
|
250
|
+
*/
|
|
251
|
+
async read() {
|
|
252
|
+
if (this._mode === _MODE_FORCED) {
|
|
253
|
+
await this._triggerForced();
|
|
254
|
+
}
|
|
255
|
+
const { uncompPress, uncompTemp } = await this._readBurst();
|
|
256
|
+
const t = this._compensateTemperature(uncompTemp);
|
|
257
|
+
const p = this._compensatePressure(uncompPress) / 100.0;
|
|
258
|
+
return { pressure: p, temperature: t };
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Trigger a forced-mode measurement, wait T_conv, return both values.
|
|
263
|
+
* @returns {Promise<{pressure:number, temperature:number}>}
|
|
264
|
+
*/
|
|
265
|
+
async readForced() {
|
|
266
|
+
const prevMode = this._mode;
|
|
267
|
+
try {
|
|
268
|
+
await this.setMode(BMP384Full.MODE_FORCED);
|
|
269
|
+
await this._triggerForced();
|
|
270
|
+
const tConvMs = BMP384Full._computeTConvMs(this._osrP, this._osrT);
|
|
271
|
+
_delay(tConvMs);
|
|
272
|
+
const { uncompPress, uncompTemp } = await this._readBurst();
|
|
273
|
+
const t = this._compensateTemperature(uncompTemp);
|
|
274
|
+
const p = this._compensatePressure(uncompPress) / 100.0;
|
|
275
|
+
return { pressure: p, temperature: t };
|
|
276
|
+
} finally {
|
|
277
|
+
this._mode = prevMode;
|
|
278
|
+
await this._applyPwr();
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/**
|
|
283
|
+
* Set the power mode.
|
|
284
|
+
* @param {number} mode
|
|
285
|
+
* @returns {Promise<void>}
|
|
286
|
+
*/
|
|
287
|
+
async setMode(mode) {
|
|
288
|
+
this._mode = mode;
|
|
289
|
+
await this._applyPwr();
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* True if STATUS.drdy_press is set.
|
|
294
|
+
* @returns {Promise<boolean>}
|
|
295
|
+
*/
|
|
296
|
+
async isDataReady() {
|
|
297
|
+
const raw = await this._readReg(_REG_STATUS, 1);
|
|
298
|
+
return (raw[0] & (1 << 5)) !== 0;
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
/**
|
|
302
|
+
* Soft reset, re-read calibration, re-apply config.
|
|
303
|
+
* @returns {Promise<void>}
|
|
304
|
+
*/
|
|
305
|
+
async softreset() {
|
|
306
|
+
await this._writeReg(_REG_CMD, _SOFT_RESET_CMD);
|
|
307
|
+
_delay(3);
|
|
308
|
+
await this._readCalibration();
|
|
309
|
+
await this._applyConfig();
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Configure FIFO source, watermark, and stop-on-full behaviour.
|
|
314
|
+
* @param {boolean} pressEn - Store pressure frames.
|
|
315
|
+
* @param {boolean} tempEn - Store temperature frames.
|
|
316
|
+
* @param {number} wtm - Watermark in bytes (0–511).
|
|
317
|
+
* @param {boolean} [stopOnFull=false]
|
|
318
|
+
* @returns {Promise<void>}
|
|
319
|
+
*/
|
|
320
|
+
async fifoConfigure(pressEn, tempEn, wtm, stopOnFull = false) {
|
|
321
|
+
const cfg1 = (1 << 4)
|
|
322
|
+
| ((stopOnFull ? 1 : 0) << 3)
|
|
323
|
+
| ((tempEn ? 1 : 0) << 1)
|
|
324
|
+
| (pressEn ? 1 : 0);
|
|
325
|
+
await this._writeReg(0x17, cfg1);
|
|
326
|
+
await this._writeReg(0x15, wtm & 0xFF);
|
|
327
|
+
await this._writeReg(0x16, (wtm >> 8) & 0x01);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/**
|
|
331
|
+
* Read and parse every available FIFO frame.
|
|
332
|
+
* @returns {Promise<Array<{type:string, value:number|null}>>}
|
|
333
|
+
*/
|
|
334
|
+
async fifoRead() {
|
|
335
|
+
const lenLo = (await this._readReg(0x12, 1))[0];
|
|
336
|
+
const lenHi = (await this._readReg(0x13, 1))[0];
|
|
337
|
+
const length = (lenHi << 8) | lenLo;
|
|
338
|
+
if (length === 0) return [];
|
|
339
|
+
const buf = await this._readReg(0x14, length);
|
|
340
|
+
const frames = [];
|
|
341
|
+
let i = 0;
|
|
342
|
+
while (i < buf.length) {
|
|
343
|
+
const hdr = buf[i];
|
|
344
|
+
if (hdr === _FIFO_HEADER_PRESS) {
|
|
345
|
+
const uncomp = (buf[i + 3] << 16) | (buf[i + 2] << 8) | buf[i + 1];
|
|
346
|
+
const valuePa = this._compensatePressureWithTLin(uncomp, this._tLin);
|
|
347
|
+
frames.push({ type: 'pressure', value: valuePa / 100.0 });
|
|
348
|
+
i += 4;
|
|
349
|
+
} else if (hdr === _FIFO_HEADER_TEMP) {
|
|
350
|
+
const uncomp = (buf[i + 3] << 16) | (buf[i + 2] << 8) | buf[i + 1];
|
|
351
|
+
const t = this._compensateTemperature(uncomp);
|
|
352
|
+
frames.push({ type: 'temperature', value: t });
|
|
353
|
+
i += 4;
|
|
354
|
+
} else if (hdr === _FIFO_HEADER_SENSORT) {
|
|
355
|
+
const uncomp = (buf[i + 3] << 16) | (buf[i + 2] << 8) | buf[i + 1];
|
|
356
|
+
frames.push({ type: 'sensortime', value: uncomp });
|
|
357
|
+
i += 4;
|
|
358
|
+
} else if (hdr === _FIFO_HEADER_ERROR || hdr === _FIFO_HEADER_EMPTY) {
|
|
359
|
+
frames.push({ type: hdr === _FIFO_HEADER_ERROR ? 'error' : 'empty', value: null });
|
|
360
|
+
i += 1;
|
|
361
|
+
} else {
|
|
362
|
+
frames.push({ type: 'unknown', value: null });
|
|
363
|
+
i += 1;
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return frames;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
/**
|
|
370
|
+
* Flush the FIFO contents.
|
|
371
|
+
* @returns {Promise<void>}
|
|
372
|
+
*/
|
|
373
|
+
async fifoFlush() {
|
|
374
|
+
await this._writeReg(_REG_CMD, _FIFO_FLUSH_CMD);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Compute altitude above sea level from the current pressure.
|
|
379
|
+
* @param {number} [seaLevelHpa=1013.25]
|
|
380
|
+
* @returns {Promise<number>} Altitude in metres.
|
|
381
|
+
*/
|
|
382
|
+
async altitude(seaLevelHpa = 1013.25) {
|
|
383
|
+
const p = await this.pressure();
|
|
384
|
+
if (p <= 0) return 0.0;
|
|
385
|
+
return 44330 * (1 - Math.pow(p / seaLevelHpa, 1 / 5.255));
|
|
386
|
+
}
|
|
387
|
+
|
|
388
|
+
async _triggerForced() {
|
|
389
|
+
const pwrReg = (_MODE_FORCED << 4) | _PWR_TEMP_EN | _PWR_PRESS_EN;
|
|
390
|
+
await this._writeReg(_REG_PWR_CTRL, pwrReg);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
async _applyPwr() {
|
|
394
|
+
const pwrReg = (this._mode << 4) | _PWR_TEMP_EN | _PWR_PRESS_EN;
|
|
395
|
+
await this._writeReg(_REG_PWR_CTRL, pwrReg);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
_compensatePressureWithTLin(uncompPress, tLin) {
|
|
399
|
+
this._tLin = tLin;
|
|
400
|
+
return this._compensatePressure(uncompPress);
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
static _computeTConvMs(osrP, osrT) {
|
|
404
|
+
// T_conv per spec: 234 + 392 + 2^osr_p*2000 + 313 + 2^osr_t*2000 µs.
|
|
405
|
+
const tConvUs = 234 + 392 + (1 << osrP) * 2000 + 313 + (1 << osrT) * 2000;
|
|
406
|
+
return Math.ceil(tConvUs / 1000) + 1;
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
module.exports = { BMP384Minimal, BMP384Full };
|