periph 1.1.0 → 1.2.1
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 +61 -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/motor/drv8830/complete.js +50 -0
- package/examples/motor/drv8830/demo.js +57 -0
- package/examples/motor/drv8830/minimal.js +23 -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 +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/examples/rtc/ds3231/complete.js +77 -0
- package/examples/rtc/ds3231/demo.js +60 -0
- package/examples/rtc/ds3231/minimal.js +23 -0
- package/examples/rtc/pcf8523/complete.js +66 -0
- package/examples/rtc/pcf8523/demo.js +65 -0
- package/examples/rtc/pcf8523/minimal.js +20 -0
- package/examples/temperature/mcp9808/complete.js +89 -0
- package/examples/temperature/mcp9808/demo.js +62 -0
- package/examples/temperature/mcp9808/minimal.js +21 -0
- package/examples/temperature/tmp117/complete.js +90 -0
- package/examples/temperature/tmp117/demo.js +73 -0
- package/examples/temperature/tmp117/minimal.js +21 -0
- package/examples/tof/vl53l0x/complete.js +118 -0
- package/examples/tof/vl53l0x/demo.js +83 -0
- package/examples/tof/vl53l0x/minimal.js +25 -0
- package/examples/tof/vl53l1x/complete.js +151 -0
- package/examples/tof/vl53l1x/demo.js +100 -0
- package/examples/tof/vl53l1x/minimal.js +25 -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/motor/drv8830.js +239 -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 +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/chips/rtc/ds3231.js +504 -0
- package/src/chips/rtc/pcf8523.js +539 -0
- package/src/chips/temperature/mcp9808.js +488 -0
- package/src/chips/temperature/tmp117.js +471 -0
- package/src/chips/tof/_vl53_base.js +160 -0
- package/src/chips/tof/vl53l0x.js +816 -0
- package/src/chips/tof/vl53l1x.js +816 -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/examples/motor/.gitkeep +0 -0
- package/examples/temperature/.gitkeep +0 -0
- package/examples/tof/.gitkeep +0 -0
- package/src/chips/gnss/.gitkeep +0 -0
- package/src/chips/motor/.gitkeep +0 -0
- package/src/chips/temperature/.gitkeep +0 -0
- package/src/chips/tof/.gitkeep +0 -0
|
@@ -0,0 +1,489 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_CHIP_ID = 0x01;
|
|
4
|
+
const _REG_REV_ID = 0x02;
|
|
5
|
+
const _REG_INT_STATUS = 0x27;
|
|
6
|
+
const _REG_STATUS = 0x28;
|
|
7
|
+
const _REG_INT_SOURCE = 0x15;
|
|
8
|
+
const _REG_INT_CONFIG = 0x14;
|
|
9
|
+
const _REG_FIFO_SEL = 0x18;
|
|
10
|
+
const _REG_FIFO_CONFIG = 0x16;
|
|
11
|
+
const _REG_FIFO_COUNT = 0x17;
|
|
12
|
+
const _REG_FIFO_DATA = 0x29;
|
|
13
|
+
const _REG_DSP_CONFIG = 0x30;
|
|
14
|
+
const _REG_DSP_IIR = 0x31;
|
|
15
|
+
const _REG_OOR_THR_P_LSB = 0x32;
|
|
16
|
+
const _REG_OOR_THR_P_MSB = 0x33;
|
|
17
|
+
const _REG_OOR_RANGE = 0x34;
|
|
18
|
+
const _REG_OOR_CONFIG = 0x35;
|
|
19
|
+
const _REG_OSR_CONFIG = 0x36;
|
|
20
|
+
const _REG_ODR_CONFIG = 0x37;
|
|
21
|
+
const _REG_OSR_EFF = 0x38;
|
|
22
|
+
const _REG_NVM_ADDR = 0x2B;
|
|
23
|
+
const _REG_NVM_DATA_LSB = 0x2C;
|
|
24
|
+
const _REG_NVM_DATA_MSB = 0x2D;
|
|
25
|
+
const _REG_TEMP_XLSB = 0x1D;
|
|
26
|
+
const _REG_PRESS_XLSB = 0x20;
|
|
27
|
+
const _REG_CMD = 0x7E;
|
|
28
|
+
|
|
29
|
+
const _CHIP_ID_EXPECTED = 0x50;
|
|
30
|
+
const _SOFT_RESET_CMD = 0xB6;
|
|
31
|
+
const _STATUS_NVM_RDY = 0x02;
|
|
32
|
+
const _STATUS_NVM_ERR = 0x04;
|
|
33
|
+
const _INT_STATUS_DRDY = 0x01;
|
|
34
|
+
|
|
35
|
+
function _delay(ms) {
|
|
36
|
+
const start = Date.now();
|
|
37
|
+
while (Date.now() - start < ms) { /* spin */ }
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function _u24(data, offset) {
|
|
41
|
+
let raw = (data[offset + 2] << 16) | (data[offset + 1] << 8) | data[offset];
|
|
42
|
+
if (raw & 0x800000) raw -= 0x1000000;
|
|
43
|
+
return raw;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* BMP581 MEMS barometric pressure + temperature sensor — minimal interface.
|
|
48
|
+
*
|
|
49
|
+
* Provides calibrated temperature (°C) and pressure (Pa) with no
|
|
50
|
+
* configuration beyond the connection. I²C address is 0x46 (SDO=GND)
|
|
51
|
+
* or 0x47 (SDO=VDDIO).
|
|
52
|
+
*
|
|
53
|
+
* Default: NORMAL mode, ODR 1 Hz, press_en=1, osr_p=x1, osr_t=x1,
|
|
54
|
+
* IIR bypass, FIFO disabled, INT_SOURCE=0.
|
|
55
|
+
*
|
|
56
|
+
* Constructor caveat: JS constructors cannot be async. The init sequence
|
|
57
|
+
* (chip-ID check, soft reset, status poll) is fired off unawaited,
|
|
58
|
+
* matching the fire-and-forget convention used in the BMP280 driver;
|
|
59
|
+
* in practice these settle before any subsequent `await` in caller code
|
|
60
|
+
* is scheduled since the underlying bus is synchronous, but call
|
|
61
|
+
* `await sensor.temperature()` (or any other async method) once before
|
|
62
|
+
* relying on calibrated readings if this matters.
|
|
63
|
+
*
|
|
64
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
|
|
65
|
+
* @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
|
|
66
|
+
*/
|
|
67
|
+
class BMP581Minimal {
|
|
68
|
+
constructor(connection, busType = 'i2c') {
|
|
69
|
+
this._conn = connection;
|
|
70
|
+
this._busType = busType;
|
|
71
|
+
this._odr = 0x1C;
|
|
72
|
+
this._pwrMode = 0x01;
|
|
73
|
+
this._osrP = 0;
|
|
74
|
+
this._osrT = 0;
|
|
75
|
+
this._pressEn = true;
|
|
76
|
+
this._init();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async _init() {
|
|
80
|
+
try {
|
|
81
|
+
const cid = (await this._readReg(_REG_CHIP_ID, 1))[0];
|
|
82
|
+
if (cid !== _CHIP_ID_EXPECTED) return;
|
|
83
|
+
for (let i = 0; i < 50; i++) {
|
|
84
|
+
const st = (await this._readReg(_REG_STATUS, 1))[0];
|
|
85
|
+
if ((st & _STATUS_NVM_RDY) && !(st & _STATUS_NVM_ERR)) break;
|
|
86
|
+
_delay(2);
|
|
87
|
+
}
|
|
88
|
+
await this._readReg(_REG_INT_STATUS, 1);
|
|
89
|
+
try {
|
|
90
|
+
await this._writeReg(_REG_CMD, _SOFT_RESET_CMD);
|
|
91
|
+
} catch (e) { /* expected NACK */ }
|
|
92
|
+
_delay(2);
|
|
93
|
+
for (let i = 0; i < 50; i++) {
|
|
94
|
+
const st = (await this._readReg(_REG_STATUS, 1))[0];
|
|
95
|
+
if ((st & _STATUS_NVM_RDY) && !(st & _STATUS_NVM_ERR)) break;
|
|
96
|
+
_delay(2);
|
|
97
|
+
}
|
|
98
|
+
await this._readReg(_REG_INT_STATUS, 1);
|
|
99
|
+
await this._writeReg(_REG_OSR_CONFIG, 0x40);
|
|
100
|
+
await this._writeReg(_REG_ODR_CONFIG, 0x71);
|
|
101
|
+
} catch (e) { /* bus may be idle */ }
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async _writeReg(reg, value) {
|
|
105
|
+
const addr = this._busType === 'spi' ? (reg & 0x7F) : reg;
|
|
106
|
+
await this._conn.write(Buffer.from([addr, value & 0xFF]));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async _readReg(reg, n) {
|
|
110
|
+
return this._conn.writeRead(Buffer.from([reg]), n);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async _spiDummyRead() {
|
|
114
|
+
if (this._busType !== 'spi') return;
|
|
115
|
+
try {
|
|
116
|
+
await this._conn.write(Buffer.from([_REG_CHIP_ID | 0x80]));
|
|
117
|
+
await this._conn.read(1);
|
|
118
|
+
} catch (e) { /* ignore */ }
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
async _waitForced() {
|
|
122
|
+
if (this._pwrMode !== 2) return;
|
|
123
|
+
for (let i = 0; i < 200; i++) {
|
|
124
|
+
const st = (await this._readReg(_REG_INT_STATUS, 1))[0];
|
|
125
|
+
if (st & _INT_STATUS_DRDY) return;
|
|
126
|
+
_delay(5);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* Read calibrated pressure.
|
|
132
|
+
* @returns {Promise<number>} Pressure in Pa.
|
|
133
|
+
*/
|
|
134
|
+
async pressure() {
|
|
135
|
+
await this._waitForced();
|
|
136
|
+
const buf = await this._readReg(_REG_PRESS_XLSB, 3);
|
|
137
|
+
return _u24(buf, 0) / 64.0;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Read calibrated temperature.
|
|
142
|
+
* @returns {Promise<number>} Temperature in degrees Celsius.
|
|
143
|
+
*/
|
|
144
|
+
async temperature() {
|
|
145
|
+
await this._waitForced();
|
|
146
|
+
const buf = await this._readReg(_REG_TEMP_XLSB, 3);
|
|
147
|
+
return _u24(buf, 0) / 65536.0;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Read both pressure and temperature atomically in a single burst.
|
|
152
|
+
* @returns {Promise<{pressure: number, temperature: number}>}
|
|
153
|
+
*/
|
|
154
|
+
async both() {
|
|
155
|
+
await this._waitForced();
|
|
156
|
+
const buf = await this._readReg(_REG_TEMP_XLSB, 6);
|
|
157
|
+
return {
|
|
158
|
+
pressure: _u24(buf, 3) / 64.0,
|
|
159
|
+
temperature: _u24(buf, 0) / 65536.0,
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* BMP581 full interface — extends BMP581Minimal with configuration, FIFO,
|
|
166
|
+
* interrupts, OOR detection, and NVM access.
|
|
167
|
+
*
|
|
168
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
|
|
169
|
+
* @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
|
|
170
|
+
*/
|
|
171
|
+
class BMP581Full extends BMP581Minimal {
|
|
172
|
+
static OSR_1X = 0;
|
|
173
|
+
static OSR_2X = 1;
|
|
174
|
+
static OSR_4X = 2;
|
|
175
|
+
static OSR_8X = 3;
|
|
176
|
+
static OSR_16X = 4;
|
|
177
|
+
static OSR_32X = 5;
|
|
178
|
+
static OSR_64X = 6;
|
|
179
|
+
static OSR_128X = 7;
|
|
180
|
+
|
|
181
|
+
static MODE_STANDBY = 0;
|
|
182
|
+
static MODE_NORMAL = 1;
|
|
183
|
+
static MODE_FORCED = 2;
|
|
184
|
+
static MODE_CONTINUOUS = 3;
|
|
185
|
+
|
|
186
|
+
static IIR_BYPASS = 0;
|
|
187
|
+
static IIR_COEFF_1 = 1;
|
|
188
|
+
static IIR_COEFF_3 = 2;
|
|
189
|
+
static IIR_COEFF_7 = 3;
|
|
190
|
+
static IIR_COEFF_15 = 4;
|
|
191
|
+
static IIR_COEFF_31 = 5;
|
|
192
|
+
static IIR_COEFF_63 = 6;
|
|
193
|
+
static IIR_COEFF_127 = 7;
|
|
194
|
+
|
|
195
|
+
static FIFO_DISABLED = 0;
|
|
196
|
+
static FIFO_TEMP = 1;
|
|
197
|
+
static FIFO_PRESS = 2;
|
|
198
|
+
static FIFO_BOTH = 3;
|
|
199
|
+
|
|
200
|
+
static FIFO_STREAM = 0;
|
|
201
|
+
static FIFO_STOP_ON_FULL = 1;
|
|
202
|
+
|
|
203
|
+
static INT_SOURCE_DRDY = 0x01;
|
|
204
|
+
static INT_SOURCE_FIFO_FULL = 0x02;
|
|
205
|
+
static INT_SOURCE_FIFO_THS = 0x04;
|
|
206
|
+
static INT_SOURCE_OOR_P = 0x08;
|
|
207
|
+
|
|
208
|
+
constructor(connection, busType = 'i2c') {
|
|
209
|
+
super(connection, busType);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
/**
|
|
213
|
+
* Write OSR_CONFIG and ODR_CONFIG atomically.
|
|
214
|
+
* @param {number} odr - ODR field 0x00-0x1F (default 0x1C = 1 Hz).
|
|
215
|
+
* @param {number} osrP - Pressure oversampling 0-7 (default 0 = x1).
|
|
216
|
+
* @param {number} osrT - Temperature oversampling 0-7 (default 0 = x1).
|
|
217
|
+
* @param {boolean} pressEn - True to enable pressure measurements.
|
|
218
|
+
* @returns {Promise<void>}
|
|
219
|
+
*/
|
|
220
|
+
async configure(odr, osrP, osrT, pressEn) {
|
|
221
|
+
this._odr = odr;
|
|
222
|
+
this._osrP = osrP;
|
|
223
|
+
this._osrT = osrT;
|
|
224
|
+
this._pressEn = pressEn;
|
|
225
|
+
const osr = (pressEn ? 0x40 : 0) | ((osrP & 0x7) << 3) | (osrT & 0x7);
|
|
226
|
+
await this._writeReg(_REG_OSR_CONFIG, osr);
|
|
227
|
+
const odrByte = ((odr & 0x1F) << 2) | (this._pwrMode & 0x3);
|
|
228
|
+
await this._writeReg(_REG_ODR_CONFIG, odrByte);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Set the power mode (preserves the current ODR setting).
|
|
233
|
+
* @param {number} mode MODE_STANDBY (0), MODE_NORMAL (1), MODE_FORCED (2), MODE_CONTINUOUS (3).
|
|
234
|
+
* @returns {Promise<void>}
|
|
235
|
+
*/
|
|
236
|
+
async setMode(mode) {
|
|
237
|
+
this._pwrMode = mode;
|
|
238
|
+
const odrByte = ((this._odr & 0x1F) << 2) | (mode & 0x3);
|
|
239
|
+
await this._writeReg(_REG_ODR_CONFIG, odrByte);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Trigger a single FORCED measurement, wait for completion, return readings.
|
|
244
|
+
* @returns {Promise<{pressure: number, temperature: number}>}
|
|
245
|
+
*/
|
|
246
|
+
async forced() {
|
|
247
|
+
const prev = this._pwrMode;
|
|
248
|
+
if (prev !== 2) await this.setMode(2);
|
|
249
|
+
for (let i = 0; i < 400; i++) {
|
|
250
|
+
const st = (await this._readReg(_REG_INT_STATUS, 1))[0];
|
|
251
|
+
if (st & _INT_STATUS_DRDY) break;
|
|
252
|
+
_delay(5);
|
|
253
|
+
}
|
|
254
|
+
const buf = await this._readReg(_REG_TEMP_XLSB, 6);
|
|
255
|
+
return {
|
|
256
|
+
pressure: _u24(buf, 3) / 64.0,
|
|
257
|
+
temperature: _u24(buf, 0) / 65536.0,
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* Compute altitude from the current pressure reading.
|
|
263
|
+
* @param {number} [seaLevelPa=101325] - Reference pressure in Pa.
|
|
264
|
+
* @returns {Promise<number>} Altitude in metres.
|
|
265
|
+
*/
|
|
266
|
+
async altitude(seaLevelPa = 101325) {
|
|
267
|
+
const p = await this.pressure();
|
|
268
|
+
if (p <= 0) return 0.0;
|
|
269
|
+
return 44330 * (1 - Math.pow(p / seaLevelPa, 1 / 5.255));
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Issue a soft reset and re-initialise the chip. @returns {Promise<void>} */
|
|
273
|
+
async softwareReset() {
|
|
274
|
+
try {
|
|
275
|
+
await this._writeReg(_REG_CMD, _SOFT_RESET_CMD);
|
|
276
|
+
} catch (e) { /* expected NACK */ }
|
|
277
|
+
_delay(2);
|
|
278
|
+
await this._init();
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* Read CHIP_ID.
|
|
283
|
+
* @returns {Promise<number>} 0x50 for a genuine BMP581.
|
|
284
|
+
*/
|
|
285
|
+
async chipId() {
|
|
286
|
+
const buf = await this._readReg(_REG_CHIP_ID, 1);
|
|
287
|
+
return buf[0];
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Read REV_ID.
|
|
292
|
+
* @returns {Promise<number>} ASIC revision identifier.
|
|
293
|
+
*/
|
|
294
|
+
async revId() {
|
|
295
|
+
const buf = await this._readReg(_REG_REV_ID, 1);
|
|
296
|
+
return buf[0];
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Read STATUS.
|
|
301
|
+
* @returns {Promise<number>} Raw status byte.
|
|
302
|
+
*/
|
|
303
|
+
async status() {
|
|
304
|
+
const buf = await this._readReg(_REG_STATUS, 1);
|
|
305
|
+
return buf[0];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Read INT_STATUS (clear-on-read).
|
|
310
|
+
* @returns {Promise<number>} Raw interrupt status byte.
|
|
311
|
+
*/
|
|
312
|
+
async interruptStatus() {
|
|
313
|
+
const buf = await this._readReg(_REG_INT_STATUS, 1);
|
|
314
|
+
return buf[0];
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Check whether a new data sample is available.
|
|
319
|
+
* @returns {Promise<boolean>} True if drdy_data_reg is set.
|
|
320
|
+
*/
|
|
321
|
+
async dataReady() {
|
|
322
|
+
return (await this.interruptStatus() & _INT_STATUS_DRDY) !== 0;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Configure INT pin: latching, polarity, drive mode, pin enable.
|
|
327
|
+
* @param {number} mode - 0 = pulsed, 1 = latched.
|
|
328
|
+
* @param {number} polarity - 0 = active-low, 1 = active-high.
|
|
329
|
+
* @param {boolean} openDrain - true for open-drain output.
|
|
330
|
+
* @param {boolean} enable - true to enable the INT pin driver.
|
|
331
|
+
* @returns {Promise<void>}
|
|
332
|
+
*/
|
|
333
|
+
async configureInterrupt(mode, polarity, openDrain, enable) {
|
|
334
|
+
let val = enable ? 0x08 : 0;
|
|
335
|
+
if (openDrain) val |= 0x04;
|
|
336
|
+
if (polarity) val |= 0x02;
|
|
337
|
+
if (mode) val |= 0x01;
|
|
338
|
+
await this._writeReg(_REG_INT_CONFIG, val);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
async _setIntSource(source, enable) {
|
|
342
|
+
const buf = await this._readReg(_REG_INT_SOURCE, 1);
|
|
343
|
+
let cur = buf[0];
|
|
344
|
+
if (enable) cur |= source;
|
|
345
|
+
else cur &= ~source;
|
|
346
|
+
await this._writeReg(_REG_INT_SOURCE, cur);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/** Enable/disable the data-ready interrupt source. @returns {Promise<void>} */
|
|
350
|
+
async enableDrdyInterrupt(enable) {
|
|
351
|
+
return this._setIntSource(BMP581Full.INT_SOURCE_DRDY, enable);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** Enable/disable FIFO threshold and FIFO-full interrupt sources. @returns {Promise<void>} */
|
|
355
|
+
async enableFifoInterrupt(threshold, full) {
|
|
356
|
+
const buf = await this._readReg(_REG_INT_SOURCE, 1);
|
|
357
|
+
let cur = buf[0] & ~(BMP581Full.INT_SOURCE_FIFO_FULL | BMP581Full.INT_SOURCE_FIFO_THS);
|
|
358
|
+
if (threshold) cur |= BMP581Full.INT_SOURCE_FIFO_THS;
|
|
359
|
+
if (full) cur |= BMP581Full.INT_SOURCE_FIFO_FULL;
|
|
360
|
+
await this._writeReg(_REG_INT_SOURCE, cur);
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/** Enable/disable the pressure out-of-range interrupt source. @returns {Promise<void>} */
|
|
364
|
+
async enableOorInterrupt(enable) {
|
|
365
|
+
return this._setIntSource(BMP581Full.INT_SOURCE_OOR_P, enable);
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
/**
|
|
369
|
+
* Set IIR filter coefficients for pressure and temperature.
|
|
370
|
+
* Also sets shdw_sel_iir_p/t in DSP_CONFIG.
|
|
371
|
+
* @param {number} coeffP - Pressure filter coefficient 0-7.
|
|
372
|
+
* @param {number} coeffT - Temperature filter coefficient 0-7.
|
|
373
|
+
* @returns {Promise<void>}
|
|
374
|
+
*/
|
|
375
|
+
async setIirFilter(coeffP, coeffT) {
|
|
376
|
+
const buf = await this._readReg(_REG_DSP_CONFIG, 1);
|
|
377
|
+
let dsp = buf[0] | 0x28;
|
|
378
|
+
await this._writeReg(_REG_DSP_CONFIG, dsp);
|
|
379
|
+
const iirVal = ((coeffP & 0x7) << 3) | (coeffT & 0x7);
|
|
380
|
+
await this._writeReg(_REG_DSP_IIR, iirVal);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
/**
|
|
384
|
+
* Configure FIFO source, mode, and threshold. Must be called in STANDBY mode.
|
|
385
|
+
* @param {number} frameSel - FIFO_DISABLED (0), FIFO_TEMP (1), FIFO_PRESS (2), FIFO_BOTH (3).
|
|
386
|
+
* @param {number} mode - FIFO_STREAM (0) or FIFO_STOP_ON_FULL (1).
|
|
387
|
+
* @param {number} threshold - 0-31 frames (0 = disabled).
|
|
388
|
+
* @returns {Promise<void>}
|
|
389
|
+
*/
|
|
390
|
+
async configureFifo(frameSel, mode, threshold) {
|
|
391
|
+
const prev = this._pwrMode;
|
|
392
|
+
if (prev !== 0) await this.setMode(0);
|
|
393
|
+
await this._writeReg(_REG_FIFO_SEL, frameSel & 0x3);
|
|
394
|
+
await this._writeReg(_REG_FIFO_CONFIG, ((mode & 0x1) << 5) | (threshold & 0x1F));
|
|
395
|
+
if (prev !== 0) await this.setMode(prev);
|
|
396
|
+
}
|
|
397
|
+
|
|
398
|
+
/**
|
|
399
|
+
* Read the number of frames currently in the FIFO.
|
|
400
|
+
* @returns {Promise<number>} Frame count 0-32.
|
|
401
|
+
*/
|
|
402
|
+
async fifoCount() {
|
|
403
|
+
const buf = await this._readReg(_REG_FIFO_COUNT, 1);
|
|
404
|
+
return buf[0] & 0x3F;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
/**
|
|
408
|
+
* Read OSR_EFF.
|
|
409
|
+
* @returns {Promise<{osrP: number, osrT: number}>}
|
|
410
|
+
*/
|
|
411
|
+
async effectiveOsr() {
|
|
412
|
+
const buf = await this._readReg(_REG_OSR_EFF, 1);
|
|
413
|
+
return {
|
|
414
|
+
osrP: (buf[0] >> 3) & 0x7,
|
|
415
|
+
osrT: buf[0] & 0x7,
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* Check whether the current ODR/OSR combination is valid.
|
|
421
|
+
* @returns {Promise<boolean>}
|
|
422
|
+
*/
|
|
423
|
+
async odrIsValid() {
|
|
424
|
+
const buf = await this._readReg(_REG_OSR_EFF, 1);
|
|
425
|
+
return (buf[0] & 0x80) !== 0;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Configure the out-of-range pressure detector.
|
|
430
|
+
* @param {number} thresholdPa - Pressure threshold in Pa.
|
|
431
|
+
* @param {number} rangePa - Symmetric +/- window around the threshold in Pa.
|
|
432
|
+
* @param {number} countLimit - 0-3 successive over-threshold events required.
|
|
433
|
+
* @returns {Promise<void>}
|
|
434
|
+
*/
|
|
435
|
+
async setOorThreshold(thresholdPa, rangePa, countLimit) {
|
|
436
|
+
let thr17 = Math.floor(thresholdPa * 64.0) >> 7;
|
|
437
|
+
await this._writeReg(_REG_OOR_THR_P_LSB, thr17 & 0xFF);
|
|
438
|
+
await this._writeReg(_REG_OOR_THR_P_MSB, (thr17 >> 8) & 0xFF);
|
|
439
|
+
const range8 = (Math.floor(rangePa * 64.0) >> 7) & 0xFF;
|
|
440
|
+
await this._writeReg(_REG_OOR_RANGE, range8);
|
|
441
|
+
const oorCfg = ((countLimit & 0x3) << 6) | ((thr17 >> 16) & 0x01);
|
|
442
|
+
await this._writeReg(_REG_OOR_CONFIG, oorCfg);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
/**
|
|
446
|
+
* Read one user NVM row.
|
|
447
|
+
* @param {number} row - NVM row address 0x20-0x22.
|
|
448
|
+
* @returns {Promise<number>} 16-bit value.
|
|
449
|
+
*/
|
|
450
|
+
async nvmRead(row) {
|
|
451
|
+
const prev = this._pwrMode;
|
|
452
|
+
if (prev !== 0) await this.setMode(0);
|
|
453
|
+
try {
|
|
454
|
+
await this._writeReg(_REG_NVM_ADDR, 0x5D);
|
|
455
|
+
await this._writeReg(_REG_CMD, 0xA5);
|
|
456
|
+
_delay(2);
|
|
457
|
+
await this._writeReg(_REG_NVM_ADDR, 0x40 | (row & 0x3F));
|
|
458
|
+
await this._writeReg(_REG_CMD, 0xA5);
|
|
459
|
+
_delay(2);
|
|
460
|
+
const buf = await this._readReg(_REG_NVM_DATA_LSB, 2);
|
|
461
|
+
return (buf[1] << 8) | buf[0];
|
|
462
|
+
} finally {
|
|
463
|
+
if (prev !== 0) await this.setMode(prev);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
/**
|
|
468
|
+
* Write one user NVM row. Limited to 10,000 total write cycles.
|
|
469
|
+
* @param {number} row - NVM row address 0x20-0x22.
|
|
470
|
+
* @param {number} value - 16-bit value to store.
|
|
471
|
+
* @returns {Promise<void>}
|
|
472
|
+
*/
|
|
473
|
+
async nvmWrite(row, value) {
|
|
474
|
+
const prev = this._pwrMode;
|
|
475
|
+
if (prev !== 0) await this.setMode(0);
|
|
476
|
+
try {
|
|
477
|
+
await this._writeReg(_REG_NVM_ADDR, 0x40 | (row & 0x3F));
|
|
478
|
+
await this._writeReg(_REG_NVM_DATA_LSB, value & 0xFF);
|
|
479
|
+
await this._writeReg(_REG_NVM_DATA_MSB, (value >> 8) & 0xFF);
|
|
480
|
+
await this._writeReg(_REG_NVM_ADDR, 0x5D);
|
|
481
|
+
await this._writeReg(_REG_CMD, 0xA0);
|
|
482
|
+
_delay(5);
|
|
483
|
+
} finally {
|
|
484
|
+
if (prev !== 0) await this.setMode(prev);
|
|
485
|
+
}
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
module.exports = { BMP581Minimal, BMP581Full };
|