periph 1.0.1 → 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 +42 -0
- package/examples/adc_dac/hx711/demo.js +35 -0
- package/examples/adc_dac/hx711/minimal.js +16 -0
- 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 +43 -0
- package/examples/adc_dac/mcp4728/demo.js +41 -0
- package/examples/adc_dac/mcp4728/minimal.js +13 -0
- 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 +35 -0
- package/examples/display/pcf8576/demo.js +48 -0
- package/examples/display/pcf8576/minimal.js +20 -0
- package/examples/environmental/aht21/complete.js +32 -0
- package/examples/environmental/aht21/demo.js +46 -0
- package/examples/environmental/aht21/minimal.js +12 -0
- package/examples/environmental/bme280/complete.js +37 -0
- package/examples/environmental/bme280/demo.js +67 -0
- package/examples/environmental/bme280/minimal.js +20 -0
- package/examples/environmental/bme680/complete.js +40 -0
- package/examples/environmental/bme680/demo.js +54 -0
- package/examples/environmental/bme680/minimal.js +21 -0
- package/examples/gas/ens160/complete.js +54 -0
- package/examples/gas/ens160/demo.js +54 -0
- package/examples/gas/ens160/minimal.js +29 -0
- 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 +58 -0
- package/examples/imu/mpu6050/demo.js +37 -0
- package/examples/imu/mpu6050/minimal.js +14 -0
- 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 +64 -0
- package/examples/light/apds9960/demo.js +34 -0
- package/examples/light/apds9960/minimal.js +12 -0
- 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 +42 -0
- package/examples/memory/24aa02uid/demo.js +42 -0
- package/examples/memory/24aa02uid/minimal.js +12 -0
- 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 +29 -22
- package/examples/pressure/bmp280/demo.js +42 -23
- 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 +165 -0
- package/src/chips/adc_dac/mcp4725.js +48 -40
- package/src/chips/adc_dac/mcp4728.js +297 -0
- 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 +232 -0
- package/src/chips/environmental/aht21.js +185 -0
- package/src/chips/environmental/bme280.js +385 -0
- package/src/chips/environmental/bme680.js +512 -0
- package/src/chips/gas/ens160.js +298 -0
- 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 +289 -0
- 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 +499 -0
- package/src/chips/magnetometer/as5600.js +102 -82
- package/src/chips/magnetometer/hmc5883l.js +222 -0
- package/src/chips/memory/_24aa02uid.js +182 -0
- 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 +188 -206
- 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/connection/hx711.js +132 -0
- 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/display/.gitkeep +0 -0
- package/examples/environmental/.gitkeep +0 -0
- package/examples/gnss/.gitkeep +0 -0
- package/examples/humidity/.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/gnss/.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,341 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_INTERRUPT_CFG = 0x0B;
|
|
4
|
+
const _REG_THS_P_L = 0x0C;
|
|
5
|
+
const _REG_THS_P_H = 0x0D;
|
|
6
|
+
const _REG_IF_CTRL = 0x0E;
|
|
7
|
+
const _REG_WHO_AM_I = 0x0F;
|
|
8
|
+
const _REG_CTRL_REG1 = 0x10;
|
|
9
|
+
const _REG_CTRL_REG2 = 0x11;
|
|
10
|
+
const _REG_CTRL_REG3 = 0x12;
|
|
11
|
+
const _REG_CTRL_REG4 = 0x13;
|
|
12
|
+
const _REG_FIFO_CTRL = 0x14;
|
|
13
|
+
const _REG_FIFO_WTM = 0x15;
|
|
14
|
+
const _REG_REF_P_L = 0x16;
|
|
15
|
+
const _REG_REF_P_H = 0x17;
|
|
16
|
+
const _REG_RPDS_L = 0x1A;
|
|
17
|
+
const _REG_RPDS_H = 0x1B;
|
|
18
|
+
const _REG_INT_SOURCE = 0x24;
|
|
19
|
+
const _REG_FIFO_STATUS1 = 0x25;
|
|
20
|
+
const _REG_STATUS = 0x27;
|
|
21
|
+
const _REG_PRESS_OUT_XL = 0x28;
|
|
22
|
+
const _REG_TEMP_OUT_L = 0x2B;
|
|
23
|
+
const _REG_FIFO_PRESS_XL = 0x78;
|
|
24
|
+
|
|
25
|
+
const _CHIP_ID = 0xB4;
|
|
26
|
+
|
|
27
|
+
function _delay(ms) {
|
|
28
|
+
const start = Date.now();
|
|
29
|
+
while (Date.now() - start < ms) { /* spin */ }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* LPS22DF absolute pressure and temperature sensor — minimal interface.
|
|
34
|
+
*
|
|
35
|
+
* Provides pressure (Pa) and temperature (°C) readings with no configuration
|
|
36
|
+
* beyond the connection. I²C address is 0x5C (SDO=GND) or 0x5D (SDO=VDDIO).
|
|
37
|
+
*
|
|
38
|
+
* Default: ODR=10 Hz, AVG=4 samples, BDU enabled, low-pass filter off,
|
|
39
|
+
* FIFO bypass mode.
|
|
40
|
+
*
|
|
41
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
|
|
42
|
+
* @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
|
|
43
|
+
*/
|
|
44
|
+
class LPS22DFMinimal {
|
|
45
|
+
constructor(connection, busType = 'i2c') {
|
|
46
|
+
this._conn = connection;
|
|
47
|
+
this._busType = busType;
|
|
48
|
+
this._init();
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async _init() {
|
|
52
|
+
const who = await this._readReg(_REG_WHO_AM_I, 1);
|
|
53
|
+
if (who[0] !== _CHIP_ID) {
|
|
54
|
+
throw new Error(`LPS22DF not found: WHO_AM_I expected 0x${_CHIP_ID.toString(16)}, got 0x${who[0].toString(16)}`);
|
|
55
|
+
}
|
|
56
|
+
await this._writeReg(_REG_CTRL_REG2, 0x04); // SWRESET=1
|
|
57
|
+
_delay(1);
|
|
58
|
+
await this._writeReg(_REG_CTRL_REG1, (3 << 3) | 0); // ODR=10 Hz, AVG=4
|
|
59
|
+
await this._writeReg(_REG_CTRL_REG2, 0x08); // BDU=1
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
async _writeReg(reg, value) {
|
|
63
|
+
const addr = this._busType === 'spi' ? (reg & 0x7F) : reg;
|
|
64
|
+
await this._conn.write(Buffer.from([addr, value & 0xFF]));
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async _readReg(reg, n) {
|
|
68
|
+
const addr = this._busType === 'spi' ? (reg | 0x80) : reg;
|
|
69
|
+
return this._conn.writeRead(Buffer.from([addr]), n);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async _waitPDa() {
|
|
73
|
+
while (true) {
|
|
74
|
+
const status = await this._readReg(_REG_STATUS, 1);
|
|
75
|
+
if (status[0] & 0x01) return;
|
|
76
|
+
_delay(1);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Read absolute pressure.
|
|
82
|
+
*
|
|
83
|
+
* Polls STATUS.P_DA then burst-reads PRESS_OUT_XL..H. Sign-extends the
|
|
84
|
+
* 24-bit two's complement value and converts to pascals (4096 LSB/hPa).
|
|
85
|
+
*
|
|
86
|
+
* @returns {Promise<number>} Pressure in pascals.
|
|
87
|
+
*/
|
|
88
|
+
async pressure() {
|
|
89
|
+
await this._waitPDa();
|
|
90
|
+
const raw = await this._readReg(_REG_PRESS_OUT_XL, 3);
|
|
91
|
+
let value = raw[0] | (raw[1] << 8) | (raw[2] << 16);
|
|
92
|
+
if (value & 0x800000) value -= 0x1000000;
|
|
93
|
+
return (value / 4096.0) * 100.0;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Read temperature.
|
|
98
|
+
*
|
|
99
|
+
* Reads TEMP_OUT_L..H. Sign-extends the 16-bit two's complement value
|
|
100
|
+
* and converts to °C (100 LSB/°C).
|
|
101
|
+
*
|
|
102
|
+
* @returns {Promise<number>} Temperature in degrees Celsius.
|
|
103
|
+
*/
|
|
104
|
+
async temperature() {
|
|
105
|
+
const raw = await this._readReg(_REG_TEMP_OUT_L, 2);
|
|
106
|
+
let value = raw[0] | (raw[1] << 8);
|
|
107
|
+
if (value & 0x8000) value -= 0x10000;
|
|
108
|
+
return value / 100.0;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* LPS22DF full interface — extends LPS22DFMinimal with configuration,
|
|
114
|
+
* threshold/offset calibration, FIFO, interrupts, and AUTOZERO/AUTOREFP.
|
|
115
|
+
*
|
|
116
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
|
|
117
|
+
* @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
|
|
118
|
+
*/
|
|
119
|
+
class LPS22DFFull extends LPS22DFMinimal {
|
|
120
|
+
static ODR_POWER_DOWN = 0;
|
|
121
|
+
static ODR_1_HZ = 1;
|
|
122
|
+
static ODR_4_HZ = 2;
|
|
123
|
+
static ODR_10_HZ = 3;
|
|
124
|
+
static ODR_25_HZ = 4;
|
|
125
|
+
static ODR_50_HZ = 5;
|
|
126
|
+
static ODR_75_HZ = 6;
|
|
127
|
+
static ODR_100_HZ = 7;
|
|
128
|
+
static ODR_200_HZ = 8;
|
|
129
|
+
|
|
130
|
+
static AVG_4 = 0;
|
|
131
|
+
static AVG_8 = 1;
|
|
132
|
+
static AVG_16 = 2;
|
|
133
|
+
static AVG_32 = 3;
|
|
134
|
+
static AVG_64 = 4;
|
|
135
|
+
static AVG_128 = 5;
|
|
136
|
+
static AVG_512 = 7;
|
|
137
|
+
|
|
138
|
+
static FIFO_BYPASS = 0;
|
|
139
|
+
static FIFO_FIFO = 1;
|
|
140
|
+
static FIFO_CONTINUOUS = 2;
|
|
141
|
+
static FIFO_BYPASS_TO_FIFO = 3;
|
|
142
|
+
static FIFO_BYPASS_TO_CONT = 4;
|
|
143
|
+
static FIFO_CONT_TO_FIFO = 5;
|
|
144
|
+
|
|
145
|
+
constructor(connection, busType = 'i2c') {
|
|
146
|
+
super(connection, busType);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Write CTRL_REG1 and CTRL_REG2.
|
|
151
|
+
* @param {number} odr - Output data rate (0=power-down, 1..7=1..100 Hz, 8=200 Hz).
|
|
152
|
+
* @param {number} avg - Averaging filter (0=4, 1=8, 2=16, 3=32, 4=64, 5=128, 7=512).
|
|
153
|
+
* @param {boolean} enLpfp - Enable low-pass filter on pressure output.
|
|
154
|
+
* @param {number} lfpfCfg - 0=ODR/4 cutoff, 1=ODR/9 cutoff.
|
|
155
|
+
* @param {boolean} bdu - Block data update.
|
|
156
|
+
* @returns {Promise<void>}
|
|
157
|
+
*/
|
|
158
|
+
async configure(odr, avg, enLpfp, lfpfCfg, bdu) {
|
|
159
|
+
const ctrl1 = ((odr & 0x0F) << 3) | (avg & 0x07);
|
|
160
|
+
let ctrl2 = 0;
|
|
161
|
+
if (enLpfp) ctrl2 |= 0x10;
|
|
162
|
+
if (lfpfCfg) ctrl2 |= 0x20;
|
|
163
|
+
if (bdu) ctrl2 |= 0x08;
|
|
164
|
+
await this._writeReg(_REG_CTRL_REG1, ctrl1);
|
|
165
|
+
await this._writeReg(_REG_CTRL_REG2, ctrl2);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** @returns {Promise<void>} */
|
|
169
|
+
async oneshot() {
|
|
170
|
+
await this._writeReg(_REG_CTRL_REG1, 0x00);
|
|
171
|
+
await this._writeReg(_REG_CTRL_REG2, 0x08 | 0x01);
|
|
172
|
+
await this._waitPDa();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Compute altitude above sea level from the current pressure.
|
|
177
|
+
* @param {number} [seaLevelPa=101325] - Reference sea-level pressure in pascals.
|
|
178
|
+
* @returns {Promise<number>} Altitude in metres.
|
|
179
|
+
*/
|
|
180
|
+
async altitude(seaLevelPa = 101325.0) {
|
|
181
|
+
const p = await this.pressure();
|
|
182
|
+
return 44330.0 * (1.0 - Math.pow(p / seaLevelPa, 1.0 / 5.255));
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/** @returns {Promise<void>} */
|
|
186
|
+
async softwareReset() {
|
|
187
|
+
await this._writeReg(_REG_CTRL_REG2, 0x04);
|
|
188
|
+
_delay(1);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Write a one-point calibration offset.
|
|
193
|
+
* @param {number} offsetPa - Offset in pascals (signed; persisted in NVM).
|
|
194
|
+
* @returns {Promise<void>}
|
|
195
|
+
*/
|
|
196
|
+
async setPressureOffset(offsetPa) {
|
|
197
|
+
const offsetHpa = offsetPa / 100.0;
|
|
198
|
+
let raw = Math.round(offsetHpa * 4096.0);
|
|
199
|
+
if (raw < 0) raw += 0x10000;
|
|
200
|
+
await this._writeReg(_REG_RPDS_L, raw & 0xFF);
|
|
201
|
+
await this._writeReg(_REG_RPDS_H, (raw >> 8) & 0xFF);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* Write a 15-bit unsigned pressure threshold.
|
|
206
|
+
* @param {number} thresholdPa - Threshold in pascals.
|
|
207
|
+
* @returns {Promise<void>}
|
|
208
|
+
*/
|
|
209
|
+
async setPressureThreshold(thresholdPa) {
|
|
210
|
+
const thresholdHpa = thresholdPa / 100.0;
|
|
211
|
+
const raw = (Math.round(thresholdHpa * 16.0)) & 0x7FFF;
|
|
212
|
+
await this._writeReg(_REG_THS_P_L, raw & 0xFF);
|
|
213
|
+
await this._writeReg(_REG_THS_P_H, (raw >> 8) & 0xFF);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Configure the INT pin and routing.
|
|
218
|
+
* @returns {Promise<void>}
|
|
219
|
+
*/
|
|
220
|
+
async configureInterrupt(intHL, ppOd, drdy, drdyPls, intEn, intFWtm, intFFull, intFOvr) {
|
|
221
|
+
let ctrl3 = 0x01; // IF_ADD_INC=1
|
|
222
|
+
if (intHL) ctrl3 |= 0x08;
|
|
223
|
+
if (ppOd) ctrl3 |= 0x02;
|
|
224
|
+
let ctrl4 = 0;
|
|
225
|
+
if (drdyPls) ctrl4 |= 0x40;
|
|
226
|
+
if (drdy) ctrl4 |= 0x20;
|
|
227
|
+
if (intEn) ctrl4 |= 0x10;
|
|
228
|
+
if (intFFull) ctrl4 |= 0x04;
|
|
229
|
+
if (intFWtm) ctrl4 |= 0x02;
|
|
230
|
+
if (intFOvr) ctrl4 |= 0x01;
|
|
231
|
+
await this._writeReg(_REG_CTRL_REG3, ctrl3);
|
|
232
|
+
await this._writeReg(_REG_CTRL_REG4, ctrl4);
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Configure pressure-event interrupts.
|
|
237
|
+
* @returns {Promise<void>}
|
|
238
|
+
*/
|
|
239
|
+
async configurePressureEvent(phe, ple, lir) {
|
|
240
|
+
let cfg = 0;
|
|
241
|
+
if (phe) cfg |= 0x01;
|
|
242
|
+
if (ple) cfg |= 0x02;
|
|
243
|
+
if (lir) cfg |= 0x04;
|
|
244
|
+
await this._writeReg(_REG_INTERRUPT_CFG, cfg);
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/** @returns {Promise<void>} */
|
|
248
|
+
async autozero() {
|
|
249
|
+
await this._writeReg(_REG_INTERRUPT_CFG, 0x20);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/** @returns {Promise<void>} */
|
|
253
|
+
async autorefp() {
|
|
254
|
+
await this._writeReg(_REG_INTERRUPT_CFG, 0x80);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/** @returns {Promise<void>} */
|
|
258
|
+
async resetReference() {
|
|
259
|
+
await this._writeReg(_REG_INTERRUPT_CFG, 0x50);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Read the stored AUTOZERO/AUTOREFP reference pressure.
|
|
264
|
+
* @returns {Promise<number>} Reference pressure in pascals.
|
|
265
|
+
*/
|
|
266
|
+
async referencePressure() {
|
|
267
|
+
const raw = await this._readReg(_REG_REF_P_L, 2);
|
|
268
|
+
let value = raw[0] | (raw[1] << 8);
|
|
269
|
+
if (value & 0x8000) value -= 0x10000;
|
|
270
|
+
return (value / 4096.0) * 100.0;
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Set the FIFO mode.
|
|
275
|
+
* @param {number} mode - 0=bypass, 1=FIFO, 2=continuous, 3=bypass-to-FIFO, 4=bypass-to-continuous, 5=continuous-to-FIFO.
|
|
276
|
+
* @returns {Promise<void>}
|
|
277
|
+
*/
|
|
278
|
+
async setFifoMode(mode) {
|
|
279
|
+
let trig = 0, fm = 0;
|
|
280
|
+
if (mode === 0) { trig = 0; fm = 0; }
|
|
281
|
+
else if (mode === 1) { trig = 0; fm = 1; }
|
|
282
|
+
else if (mode === 2) { trig = 0; fm = 2; }
|
|
283
|
+
else if (mode === 3) { trig = 1; fm = 1; }
|
|
284
|
+
else if (mode === 4) { trig = 1; fm = 2; }
|
|
285
|
+
else { trig = 1; fm = 3; }
|
|
286
|
+
await this._writeReg(_REG_FIFO_CTRL, (trig << 2) | (fm & 0x03));
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
/**
|
|
290
|
+
* Set the FIFO watermark level.
|
|
291
|
+
* @param {number} level - 0..127.
|
|
292
|
+
* @returns {Promise<void>}
|
|
293
|
+
*/
|
|
294
|
+
async setFifoWatermark(level) {
|
|
295
|
+
await this._writeReg(_REG_FIFO_WTM, level & 0x7F);
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Read the FIFO sample count.
|
|
300
|
+
* @returns {Promise<number>} Stored FIFO samples (0..127).
|
|
301
|
+
*/
|
|
302
|
+
async fifoSampleCount() {
|
|
303
|
+
const v = await this._readReg(_REG_FIFO_STATUS1, 1);
|
|
304
|
+
return v[0];
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* Read every available FIFO sample.
|
|
309
|
+
* @returns {Promise<number[]>} Pressure readings in pascals, oldest first.
|
|
310
|
+
*/
|
|
311
|
+
async readFifo() {
|
|
312
|
+
const count = await this.fifoSampleCount();
|
|
313
|
+
if (count === 0) return [];
|
|
314
|
+
const raw = await this._readReg(_REG_FIFO_PRESS_XL, count * 3);
|
|
315
|
+
const out = new Array(count);
|
|
316
|
+
for (let i = 0; i < count; i++) {
|
|
317
|
+
const base = i * 3;
|
|
318
|
+
let value = raw[base] | (raw[base + 1] << 8) | (raw[base + 2] << 16);
|
|
319
|
+
if (value & 0x800000) value -= 0x1000000;
|
|
320
|
+
out[i] = (value / 4096.0) * 100.0;
|
|
321
|
+
}
|
|
322
|
+
return out;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Read and clear the INT_SOURCE register.
|
|
327
|
+
* @returns {Promise<{boot_on:boolean,ia:boolean,ph:boolean,pl:boolean}>}
|
|
328
|
+
*/
|
|
329
|
+
async interruptSource() {
|
|
330
|
+
const raw = await this._readReg(_REG_INT_SOURCE, 1);
|
|
331
|
+
const v = raw[0];
|
|
332
|
+
return {
|
|
333
|
+
boot_on: !!(v & 0x80),
|
|
334
|
+
ia: !!(v & 0x04),
|
|
335
|
+
ph: !!(v & 0x01),
|
|
336
|
+
pl: !!(v & 0x02),
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
module.exports = { LPS22DFMinimal, LPS22DFFull };
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_INTERRUPT_CFG = 0x0B;
|
|
4
|
+
const _REG_THS_P_L = 0x0C;
|
|
5
|
+
const _REG_THS_P_H = 0x0D;
|
|
6
|
+
const _REG_WHO_AM_I = 0x0F;
|
|
7
|
+
const _REG_CTRL_REG1 = 0x10;
|
|
8
|
+
const _REG_CTRL_REG2 = 0x11;
|
|
9
|
+
const _REG_STATUS = 0x27;
|
|
10
|
+
const _REG_PRESS_OUT_XL = 0x28;
|
|
11
|
+
const _REG_PRESS_OUT_L = 0x29;
|
|
12
|
+
const _REG_PRESS_OUT_H = 0x2A;
|
|
13
|
+
const _REG_TEMP_OUT_L = 0x2B;
|
|
14
|
+
const _REG_TEMP_OUT_H = 0x2C;
|
|
15
|
+
const _REG_FIFO_DATA_PRESS_XL = 0x78;
|
|
16
|
+
|
|
17
|
+
const _CHIP_ID = 0xB4;
|
|
18
|
+
const _BOOT_WAIT_MS = 2;
|
|
19
|
+
const _SENSITIVITY_LSB_PER_HPA_MODE1 = 4096.0;
|
|
20
|
+
const _SENSITIVITY_LSB_PER_HPA_MODE2 = 2048.0;
|
|
21
|
+
|
|
22
|
+
function _delay(ms) {
|
|
23
|
+
const start = Date.now();
|
|
24
|
+
while (Date.now() - start < ms) { /* spin */ }
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* LPS28DFW dual full-scale digital barometer — minimal interface.
|
|
29
|
+
*
|
|
30
|
+
* Reads absolute pressure and temperature from the CCLGA-7L water-resistant
|
|
31
|
+
* sensor. I²C address is 0x5C (SA0=GND) or 0x5D (SA0=VDD).
|
|
32
|
+
*
|
|
33
|
+
* Default configuration (baked in at construction):
|
|
34
|
+
* - FS_MODE = 0 (Mode 1, 0–1260 hPa, 4096 LSB/hPa)
|
|
35
|
+
* - AVG = 010b (16 samples)
|
|
36
|
+
* - ODR = 0100b (25 Hz)
|
|
37
|
+
* - BDU = 1, EN_LPFP = 1, LFPF_CFG = 0 (ODR/4 bandwidth)
|
|
38
|
+
*
|
|
39
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
|
|
40
|
+
*/
|
|
41
|
+
class LPS28DFWMinimal {
|
|
42
|
+
constructor(connection) {
|
|
43
|
+
this._conn = connection;
|
|
44
|
+
this._fsMode = 0;
|
|
45
|
+
this._odr = 0x04;
|
|
46
|
+
this._avg = 0x02;
|
|
47
|
+
this._lpfEn = 1;
|
|
48
|
+
this._lpfCfg = 0;
|
|
49
|
+
this._bdu = 1;
|
|
50
|
+
this._init();
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
async _init() {
|
|
54
|
+
const who = await this._readReg(_REG_WHO_AM_I, 1);
|
|
55
|
+
if (who[0] !== _CHIP_ID) {
|
|
56
|
+
throw new Error('LPS28DFW WHO_AM_I mismatch: expected 0x' +
|
|
57
|
+
_CHIP_ID.toString(16).toUpperCase().padStart(2, '0') +
|
|
58
|
+
', got 0x' + who[0].toString(16).toUpperCase().padStart(2, '0'));
|
|
59
|
+
}
|
|
60
|
+
_delay(_BOOT_WAIT_MS);
|
|
61
|
+
const ctrl2 = (this._fsMode << 6) | (this._lpfCfg << 5) | (this._lpfEn << 4) | (this._bdu << 3);
|
|
62
|
+
await this._writeReg(_REG_CTRL_REG2, ctrl2);
|
|
63
|
+
const ctrl1 = (this._odr << 3) | (this._avg & 0x07);
|
|
64
|
+
await this._writeReg(_REG_CTRL_REG1, ctrl1);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
async _writeReg(reg, value) {
|
|
68
|
+
await this._conn.write(Buffer.from([reg, value]));
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async _readReg(reg, n) {
|
|
72
|
+
return this._conn.writeRead(Buffer.from([reg]), n);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Read absolute pressure.
|
|
77
|
+
* @returns {Promise<number>} Pressure in hPa.
|
|
78
|
+
*/
|
|
79
|
+
async readPressure() {
|
|
80
|
+
const raw = await this._readReg(_REG_PRESS_OUT_XL, 3);
|
|
81
|
+
let v = (raw[2] << 16) | (raw[1] << 8) | raw[0];
|
|
82
|
+
if (v & 0x800000) v |= 0xFF000000;
|
|
83
|
+
const sens = this._fsMode === 0 ? _SENSITIVITY_LSB_PER_HPA_MODE1 : _SENSITIVITY_LSB_PER_HPA_MODE2;
|
|
84
|
+
return v / sens;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Read sensor temperature.
|
|
89
|
+
* @returns {Promise<number>} Temperature in degrees Celsius.
|
|
90
|
+
*/
|
|
91
|
+
async readTemperature() {
|
|
92
|
+
const raw = await this._readReg(_REG_TEMP_OUT_L, 2);
|
|
93
|
+
const v = raw.readInt16BE(0);
|
|
94
|
+
return v / 100.0;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* LPS28DFW full interface — extends LPS28DFWMinimal with full configuration.
|
|
100
|
+
*
|
|
101
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
|
|
102
|
+
*/
|
|
103
|
+
class LPS28DFWFull extends LPS28DFWMinimal {
|
|
104
|
+
static ODR_POWER_DOWN = 0x00;
|
|
105
|
+
static ODR_1_HZ = 0x01;
|
|
106
|
+
static ODR_4_HZ = 0x02;
|
|
107
|
+
static ODR_10_HZ = 0x03;
|
|
108
|
+
static ODR_25_HZ = 0x04;
|
|
109
|
+
static ODR_50_HZ = 0x05;
|
|
110
|
+
static ODR_75_HZ = 0x06;
|
|
111
|
+
static ODR_100_HZ = 0x07;
|
|
112
|
+
static ODR_200_HZ = 0x08;
|
|
113
|
+
|
|
114
|
+
static AVG_4 = 0x00;
|
|
115
|
+
static AVG_8 = 0x01;
|
|
116
|
+
static AVG_16 = 0x02;
|
|
117
|
+
static AVG_32 = 0x03;
|
|
118
|
+
static AVG_64 = 0x04;
|
|
119
|
+
static AVG_128 = 0x05;
|
|
120
|
+
static AVG_512 = 0x07;
|
|
121
|
+
|
|
122
|
+
static FS_MODE_1 = 0;
|
|
123
|
+
static FS_MODE_2 = 1;
|
|
124
|
+
|
|
125
|
+
static LFPF_ODR_OVER_4 = 0;
|
|
126
|
+
static LFPF_ODR_OVER_9 = 1;
|
|
127
|
+
|
|
128
|
+
static FIFO_BYPASS = 0;
|
|
129
|
+
static FIFO_FIFO = 1;
|
|
130
|
+
static FIFO_CONTINUOUS = 2;
|
|
131
|
+
static FIFO_BYPASS_TO_FIFO = 4;
|
|
132
|
+
static FIFO_BYPASS_TO_CONTINUOUS = 5;
|
|
133
|
+
static FIFO_CONTINUOUS_TO_FIFO = 6;
|
|
134
|
+
|
|
135
|
+
static STATUS_P_DA = 0x01;
|
|
136
|
+
static STATUS_T_DA = 0x02;
|
|
137
|
+
static STATUS_P_OR = 0x10;
|
|
138
|
+
static STATUS_T_OR = 0x20;
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Set output data rate, averaging, full-scale mode, and IIR filter.
|
|
142
|
+
* @param {number} odr Output data rate code (0–8).
|
|
143
|
+
* @param {number} avg Averaging code (0–7).
|
|
144
|
+
* @param {number} fsMode 0=Mode 1, 1=Mode 2.
|
|
145
|
+
* @param {boolean} lpfEn True to enable the IIR low-pass filter.
|
|
146
|
+
* @param {number} lpfCfg 0=ODR/4, 1=ODR/9.
|
|
147
|
+
* @returns {Promise<void>}
|
|
148
|
+
*/
|
|
149
|
+
async configure(odr, avg, fsMode, lpfEn, lpfCfg) {
|
|
150
|
+
this._odr = odr;
|
|
151
|
+
this._avg = avg;
|
|
152
|
+
this._fsMode = fsMode;
|
|
153
|
+
this._lpfEn = lpfEn ? 1 : 0;
|
|
154
|
+
this._lpfCfg = lpfCfg;
|
|
155
|
+
const ctrl2 = (this._fsMode << 6) | (this._lpfCfg << 5) | (this._lpfEn << 4) | (this._bdu << 3);
|
|
156
|
+
await this._writeReg(_REG_CTRL_REG2, ctrl2);
|
|
157
|
+
const ctrl1 = (this._odr << 3) | (this._avg & 0x07);
|
|
158
|
+
await this._writeReg(_REG_CTRL_REG1, ctrl1);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Burst-read pressure and temperature.
|
|
163
|
+
* @returns {Promise<{pressure: number, temperature: number}>}
|
|
164
|
+
*/
|
|
165
|
+
async read() {
|
|
166
|
+
const raw = await this._readReg(_REG_PRESS_OUT_XL, 5);
|
|
167
|
+
let p = (raw[2] << 16) | (raw[1] << 8) | raw[0];
|
|
168
|
+
if (p & 0x800000) p |= 0xFF000000;
|
|
169
|
+
const t = raw.readInt16BE(3);
|
|
170
|
+
const sens = this._fsMode === 0 ? _SENSITIVITY_LSB_PER_HPA_MODE1 : _SENSITIVITY_LSB_PER_HPA_MODE2;
|
|
171
|
+
return { pressure: p / sens, temperature: t / 100.0 };
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* Trigger a one-shot measurement (with ODR=0000) and read the result.
|
|
176
|
+
* @returns {Promise<{pressure: number, temperature: number}>}
|
|
177
|
+
*/
|
|
178
|
+
async readOneshot() {
|
|
179
|
+
const saved = await this._readReg(_REG_CTRL_REG1, 1);
|
|
180
|
+
const savedOdr = saved[0] >> 3;
|
|
181
|
+
await this._writeReg(_REG_CTRL_REG1, (0 << 3) | (this._avg & 0x07));
|
|
182
|
+
const c2 = await this._readReg(_REG_CTRL_REG2, 1);
|
|
183
|
+
await this._writeReg(_REG_CTRL_REG2, c2[0] | 0x01);
|
|
184
|
+
for (let i = 0; i < 200; i++) {
|
|
185
|
+
const status = await this._readReg(_REG_STATUS, 1);
|
|
186
|
+
if (status[0] & LPS28DFWFull.STATUS_P_DA) break;
|
|
187
|
+
_delay(5);
|
|
188
|
+
}
|
|
189
|
+
const result = await this.read();
|
|
190
|
+
await this._writeReg(_REG_CTRL_REG1, (savedOdr << 3) | (this._avg & 0x07));
|
|
191
|
+
return result;
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Check whether STATUS.P_DA is set.
|
|
196
|
+
* @returns {Promise<boolean>}
|
|
197
|
+
*/
|
|
198
|
+
async isDataReady() {
|
|
199
|
+
const status = await this._readReg(_REG_STATUS, 1);
|
|
200
|
+
return (status[0] & LPS28DFWFull.STATUS_P_DA) !== 0;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* Program the one-point calibration offset (RPDS).
|
|
205
|
+
* @param {number} offsetHpa - Offset in hPa to subtract.
|
|
206
|
+
* @returns {Promise<void>}
|
|
207
|
+
*/
|
|
208
|
+
async setOffset(offsetHpa) {
|
|
209
|
+
const sens = this._fsMode === 0 ? _SENSITIVITY_LSB_PER_HPA_MODE1 : _SENSITIVITY_LSB_PER_HPA_MODE2;
|
|
210
|
+
let raw = Math.trunc(offsetHpa * sens);
|
|
211
|
+
if (raw < 0) raw += 0x10000;
|
|
212
|
+
await this._writeReg(0x1A, raw & 0xFF);
|
|
213
|
+
await this._writeReg(0x1B, (raw >> 8) & 0xFF);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Issue a software reset and wait for the chip to reboot (~2 ms).
|
|
218
|
+
* @returns {Promise<void>}
|
|
219
|
+
*/
|
|
220
|
+
async softreset() {
|
|
221
|
+
const c2 = await this._readReg(_REG_CTRL_REG2, 1);
|
|
222
|
+
await this._writeReg(_REG_CTRL_REG2, c2[0] | 0x02);
|
|
223
|
+
_delay(_BOOT_WAIT_MS);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Configure FIFO mode, watermark level, and stop-on-watermark.
|
|
228
|
+
* @param {number} mode - FIFO mode code (0–6).
|
|
229
|
+
* @param {number} wtm - Watermark level, 0–127.
|
|
230
|
+
* @param {boolean} stopOnWtm
|
|
231
|
+
* @returns {Promise<void>}
|
|
232
|
+
*/
|
|
233
|
+
async fifoConfigure(mode, wtm, stopOnWtm) {
|
|
234
|
+
if (mode === LPS28DFWFull.FIFO_BYPASS) {
|
|
235
|
+
await this._writeReg(0x14, 0x00);
|
|
236
|
+
}
|
|
237
|
+
const trig = mode >= 4 ? 1 : 0;
|
|
238
|
+
const fMode = mode & 0x03;
|
|
239
|
+
const ctrl = (trig << 2) | ((stopOnWtm ? 1 : 0) << 3) | fMode;
|
|
240
|
+
await this._writeReg(0x14, ctrl);
|
|
241
|
+
await this._writeReg(0x15, wtm & 0x7F);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Drain up to `count` pressure samples from the FIFO.
|
|
246
|
+
* @param {number} count - Number of samples to read.
|
|
247
|
+
* @returns {Promise<number[]>} Pressure values in hPa.
|
|
248
|
+
*/
|
|
249
|
+
async fifoRead(count) {
|
|
250
|
+
if (count <= 0) return [];
|
|
251
|
+
if (count > 128) count = 128;
|
|
252
|
+
const raw = await this._readReg(_REG_FIFO_DATA_PRESS_XL, count * 3);
|
|
253
|
+
const out = [];
|
|
254
|
+
const sens = this._fsMode === 0 ? _SENSITIVITY_LSB_PER_HPA_MODE1 : _SENSITIVITY_LSB_PER_HPA_MODE2;
|
|
255
|
+
for (let i = 0; i < count; i++) {
|
|
256
|
+
const b = i * 3;
|
|
257
|
+
let v = (raw[b + 2] << 16) | (raw[b + 1] << 8) | raw[b];
|
|
258
|
+
if (v & 0x800000) v |= 0xFF000000;
|
|
259
|
+
out.push(v / sens);
|
|
260
|
+
}
|
|
261
|
+
return out;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* Return the number of unread samples in the FIFO.
|
|
266
|
+
* @returns {Promise<number>}
|
|
267
|
+
*/
|
|
268
|
+
async fifoLevel() {
|
|
269
|
+
const buf = await this._readReg(0x25, 1);
|
|
270
|
+
return buf[0];
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Program the pressure threshold and enable interrupt sources.
|
|
275
|
+
* @param {number} thresholdHpa - Pressure threshold in hPa.
|
|
276
|
+
* @param {boolean} high - True to assert PH when pressure exceeds threshold.
|
|
277
|
+
* @param {boolean} low - True to assert PL when pressure falls below threshold.
|
|
278
|
+
* @returns {Promise<void>}
|
|
279
|
+
*/
|
|
280
|
+
async setThreshold(thresholdHpa, high, low) {
|
|
281
|
+
const sens = this._fsMode === 0 ? 16.0 : 8.0;
|
|
282
|
+
let raw = Math.trunc(thresholdHpa * sens);
|
|
283
|
+
if (raw < 0) raw = 0;
|
|
284
|
+
if (raw > 0x7FFF) raw = 0x7FFF;
|
|
285
|
+
await this._writeReg(0x0C, raw & 0xFF);
|
|
286
|
+
await this._writeReg(0x0D, (raw >> 8) & 0x7F);
|
|
287
|
+
const cfg = await this._readReg(_REG_INTERRUPT_CFG, 1);
|
|
288
|
+
let v = cfg[0] & ~0x03;
|
|
289
|
+
if (high) v |= 0x01;
|
|
290
|
+
if (low) v |= 0x02;
|
|
291
|
+
await this._writeReg(_REG_INTERRUPT_CFG, v);
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Read the WHO_AM_I register.
|
|
296
|
+
* @returns {Promise<number>} Chip ID; expect 0xB4 for LPS28DFW.
|
|
297
|
+
*/
|
|
298
|
+
async chipId() {
|
|
299
|
+
const buf = await this._readReg(_REG_WHO_AM_I, 1);
|
|
300
|
+
return buf[0];
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Compute altitude above sea level from the current pressure.
|
|
305
|
+
* @param {number} [seaLevelHpa=1013.25]
|
|
306
|
+
* @returns {Promise<number>} Altitude in metres.
|
|
307
|
+
*/
|
|
308
|
+
async altitude(seaLevelHpa = 1013.25) {
|
|
309
|
+
const p = await this.readPressure();
|
|
310
|
+
return 44330 * (1 - Math.pow(p / seaLevelHpa, 1 / 5.255));
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
module.exports = { LPS28DFWMinimal, LPS28DFWFull };
|