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,135 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Raised on checksum error when reading a DHT11 frame.
|
|
5
|
+
*/
|
|
6
|
+
class DHT11Error extends Error {
|
|
7
|
+
constructor(detail) {
|
|
8
|
+
super(detail);
|
|
9
|
+
}
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* DHT11 combined temperature and humidity sensor — minimal interface.
|
|
14
|
+
*
|
|
15
|
+
* The DHT11 returns a 40-bit reading (humidity integer + decimal,
|
|
16
|
+
* temperature integer + decimal, checksum) over a single bidirectional
|
|
17
|
+
* data line. The driver accepts a `DHTxxConnection` instance that handles
|
|
18
|
+
* the underlying single-wire protocol; this class is responsible only
|
|
19
|
+
* for validating the frame and converting it to engineering units.
|
|
20
|
+
*
|
|
21
|
+
* Default configuration (baked in at construction):
|
|
22
|
+
* - Single read attempt; throws on checksum mismatch
|
|
23
|
+
* - Caller responsible for respecting the ≥ 2 s sampling interval
|
|
24
|
+
*/
|
|
25
|
+
class DHT11Minimal {
|
|
26
|
+
/**
|
|
27
|
+
* @param {import('../../connection/dhtxx').DHTxxConnection} connection - DHTxx connection instance.
|
|
28
|
+
*/
|
|
29
|
+
constructor(connection) {
|
|
30
|
+
this._conn = connection;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Read both temperature and humidity in a single transaction.
|
|
35
|
+
*
|
|
36
|
+
* @returns {{temperature: number, humidity: number}} Temperature in °C
|
|
37
|
+
* and humidity in %RH.
|
|
38
|
+
* @throws {DHT11Error} If the frame's checksum is invalid.
|
|
39
|
+
*/
|
|
40
|
+
read() {
|
|
41
|
+
const frame = this._conn.read();
|
|
42
|
+
return DHT11Minimal._decode(frame);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
static _decode(frame) {
|
|
46
|
+
if (frame.length !== 5) {
|
|
47
|
+
throw new DHT11Error(`frame must be 5 bytes, got ${frame.length}`);
|
|
48
|
+
}
|
|
49
|
+
const humInt = frame[0];
|
|
50
|
+
const humDec = frame[1];
|
|
51
|
+
const tempInt = frame[2];
|
|
52
|
+
const tempDec = frame[3];
|
|
53
|
+
const checksum = frame[4];
|
|
54
|
+
const expected = (humInt + humDec + tempInt + tempDec) & 0xFF;
|
|
55
|
+
if (expected !== checksum) {
|
|
56
|
+
throw new DHT11Error(`checksum mismatch: expected 0x${expected.toString(16).padStart(2,'0').toUpperCase()}, got 0x${checksum.toString(16).padStart(2,'0').toUpperCase()}`);
|
|
57
|
+
}
|
|
58
|
+
const humidity = humInt + humDec / 10.0;
|
|
59
|
+
const sign = (tempDec & 0x80) ? -1 : 1;
|
|
60
|
+
const tempDecValue = tempDec & 0x7F;
|
|
61
|
+
const temperature = sign * (tempInt + tempDecValue / 10.0);
|
|
62
|
+
return { temperature, humidity };
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* DHT11 full interface — extends DHT11Minimal with retry, raw access, and
|
|
68
|
+
* convenience methods.
|
|
69
|
+
*/
|
|
70
|
+
class DHT11Full extends DHT11Minimal {
|
|
71
|
+
/**
|
|
72
|
+
* @param {import('../../connection/dhtxx').DHTxxConnection} connection - DHTxx connection instance.
|
|
73
|
+
* @param {number} [maxRetries=3] - Default retry count for `readRetry`.
|
|
74
|
+
*/
|
|
75
|
+
constructor(connection, maxRetries = 3) {
|
|
76
|
+
super(connection);
|
|
77
|
+
this._maxRetries = maxRetries;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Read temperature in a single transaction.
|
|
82
|
+
* @returns {number} Temperature in degrees Celsius.
|
|
83
|
+
*/
|
|
84
|
+
readTemperature() {
|
|
85
|
+
return this.read().temperature;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Read humidity in a single transaction.
|
|
90
|
+
* @returns {number} Humidity in %RH.
|
|
91
|
+
*/
|
|
92
|
+
readHumidity() {
|
|
93
|
+
return this.read().humidity;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Read both values, retrying on checksum error.
|
|
98
|
+
*
|
|
99
|
+
* @param {number} [maxRetries] - Override retry count (default: constructor value).
|
|
100
|
+
* @returns {{temperature: number, humidity: number}}
|
|
101
|
+
* @throws {DHT11Error} If all attempts fail with a checksum error.
|
|
102
|
+
*/
|
|
103
|
+
readRetry(maxRetries) {
|
|
104
|
+
const n = maxRetries || this._maxRetries;
|
|
105
|
+
let lastErr = null;
|
|
106
|
+
for (let i = 0; i < n; i++) {
|
|
107
|
+
try {
|
|
108
|
+
return this.read();
|
|
109
|
+
} catch (e) {
|
|
110
|
+
lastErr = e;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
throw new DHT11Error(`readRetry exhausted after ${n} attempts: ${lastErr && lastErr.message}`);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* Read the raw 5-byte frame (after validating checksum).
|
|
118
|
+
*
|
|
119
|
+
* @returns {Buffer} 5-byte frame.
|
|
120
|
+
* @throws {DHT11Error} If the frame's checksum is invalid.
|
|
121
|
+
*/
|
|
122
|
+
readRaw() {
|
|
123
|
+
const frame = this._conn.read();
|
|
124
|
+
if (frame.length !== 5) {
|
|
125
|
+
throw new DHT11Error(`frame must be 5 bytes, got ${frame.length}`);
|
|
126
|
+
}
|
|
127
|
+
const expected = (frame[0] + frame[1] + frame[2] + frame[3]) & 0xFF;
|
|
128
|
+
if (expected !== frame[4]) {
|
|
129
|
+
throw new DHT11Error(`checksum mismatch: expected 0x${expected.toString(16).padStart(2,'0').toUpperCase()}, got 0x${frame[4].toString(16).padStart(2,'0').toUpperCase()}`);
|
|
130
|
+
}
|
|
131
|
+
return frame;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
module.exports = { DHT11Minimal, DHT11Full, DHT11Error };
|
package/src/chips/imu/mpu6050.js
CHANGED
|
@@ -28,7 +28,7 @@ const _GYRO_SENSITIVITY = [131.0, 65.5, 32.8, 16.4];
|
|
|
28
28
|
* MPU-6050 6-axis MotionTracking device (accelerometer + gyroscope) — minimal interface.
|
|
29
29
|
*
|
|
30
30
|
* Provides 3-axis acceleration and 3-axis angular rate readings with no
|
|
31
|
-
* configuration beyond the
|
|
31
|
+
* configuration beyond the connection. Performs device reset, WHO_AM_I check,
|
|
32
32
|
* and enables all sensors at defaults during initialization.
|
|
33
33
|
*
|
|
34
34
|
* Default configuration (written at construction):
|
|
@@ -37,53 +37,68 @@ const _GYRO_SENSITIVITY = [131.0, 65.5, 32.8, 16.4];
|
|
|
37
37
|
* - DLPF: 44 Hz bandwidth (CONFIG DLPF_CFG=3, 1 kHz gyro rate)
|
|
38
38
|
* - Sample rate: 200 Hz (SMPLRT_DIV=4)
|
|
39
39
|
* - Clock: PLL with gyro X reference (CLKSEL=1)
|
|
40
|
+
*
|
|
41
|
+
* Constructor caveat: JS constructors cannot be async, but the original
|
|
42
|
+
* synchronous constructor validated WHO_AM_I and threw synchronously on
|
|
43
|
+
* mismatch — that guarantee cannot be preserved exactly. The whole init
|
|
44
|
+
* sequence is fired off unawaited (matching the fire-and-forget convention
|
|
45
|
+
* used throughout this port); a WHO_AM_I mismatch now surfaces as an
|
|
46
|
+
* *unhandled promise rejection* shortly after construction rather than a
|
|
47
|
+
* synchronous throw from `new MPU6050Minimal(...)`. Callers that need to
|
|
48
|
+
* detect "wrong or absent device" deterministically should call an async
|
|
49
|
+
* method (e.g. `await imu.accel()`) right after construction and handle its
|
|
50
|
+
* rejection.
|
|
40
51
|
*/
|
|
41
52
|
class MPU6050Minimal {
|
|
42
53
|
/**
|
|
43
|
-
* @param {
|
|
54
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection (writeRead, write).
|
|
44
55
|
*/
|
|
45
|
-
constructor(
|
|
46
|
-
this.
|
|
56
|
+
constructor(connection) {
|
|
57
|
+
this._conn = connection;
|
|
47
58
|
this._accelFs = 0;
|
|
48
59
|
this._gyroFs = 0;
|
|
49
|
-
this.
|
|
60
|
+
this._init();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async _init() {
|
|
64
|
+
await this._writeReg(_REG_PWR_MGMT_1, 0x80);
|
|
50
65
|
const end1 = Date.now() + 100;
|
|
51
66
|
while (Date.now() < end1) {}
|
|
52
|
-
this._writeReg(_REG_PWR_MGMT_1, 0x01);
|
|
53
|
-
const who = this._readReg(_REG_WHO_AM_I);
|
|
67
|
+
await this._writeReg(_REG_PWR_MGMT_1, 0x01);
|
|
68
|
+
const who = await this._readReg(_REG_WHO_AM_I);
|
|
54
69
|
if (who !== _WHO_AM_I_VALUE) {
|
|
55
70
|
throw new Error('MPU6050 WHO_AM_I: expected 0x' + _WHO_AM_I_VALUE.toString(16) + ', got 0x' + who.toString(16));
|
|
56
71
|
}
|
|
57
|
-
this._writeReg(_REG_GYRO_CONFIG, 0x00);
|
|
58
|
-
this._writeReg(_REG_ACCEL_CONFIG, 0x00);
|
|
59
|
-
this._writeReg(_REG_CONFIG, 0x03);
|
|
60
|
-
this._writeReg(_REG_SMPLRT_DIV, 0x04);
|
|
72
|
+
await this._writeReg(_REG_GYRO_CONFIG, 0x00);
|
|
73
|
+
await this._writeReg(_REG_ACCEL_CONFIG, 0x00);
|
|
74
|
+
await this._writeReg(_REG_CONFIG, 0x03);
|
|
75
|
+
await this._writeReg(_REG_SMPLRT_DIV, 0x04);
|
|
61
76
|
const end2 = Date.now() + 35;
|
|
62
77
|
while (Date.now() < end2) {}
|
|
63
78
|
}
|
|
64
79
|
|
|
65
|
-
_writeReg(reg, value) {
|
|
66
|
-
this.
|
|
80
|
+
async _writeReg(reg, value) {
|
|
81
|
+
await this._conn.write(Buffer.from([reg, value]));
|
|
67
82
|
}
|
|
68
83
|
|
|
69
|
-
_readReg(reg) {
|
|
70
|
-
return this.
|
|
84
|
+
async _readReg(reg) {
|
|
85
|
+
return (await this._conn.writeRead(Buffer.from([reg]), 1))[0];
|
|
71
86
|
}
|
|
72
87
|
|
|
73
|
-
_readReg16Signed(reg) {
|
|
74
|
-
return this.
|
|
88
|
+
async _readReg16Signed(reg) {
|
|
89
|
+
return (await this._conn.writeRead(Buffer.from([reg]), 2)).readInt16BE(0);
|
|
75
90
|
}
|
|
76
91
|
|
|
77
|
-
_readBurst(reg, len) {
|
|
78
|
-
return this.
|
|
92
|
+
async _readBurst(reg, len) {
|
|
93
|
+
return this._conn.writeRead(Buffer.from([reg]), len);
|
|
79
94
|
}
|
|
80
95
|
|
|
81
96
|
/**
|
|
82
97
|
* Read 3-axis linear acceleration.
|
|
83
|
-
* @returns {number[]} [x, y, z] acceleration in m/s².
|
|
98
|
+
* @returns {Promise<number[]>} [x, y, z] acceleration in m/s².
|
|
84
99
|
*/
|
|
85
|
-
accel() {
|
|
86
|
-
const buf = this._readBurst(_REG_ACCEL_XOUT_H, 6);
|
|
100
|
+
async accel() {
|
|
101
|
+
const buf = await this._readBurst(_REG_ACCEL_XOUT_H, 6);
|
|
87
102
|
const ax = buf.readInt16BE(0);
|
|
88
103
|
const ay = buf.readInt16BE(2);
|
|
89
104
|
const az = buf.readInt16BE(4);
|
|
@@ -93,10 +108,10 @@ class MPU6050Minimal {
|
|
|
93
108
|
|
|
94
109
|
/**
|
|
95
110
|
* Read 3-axis angular rate.
|
|
96
|
-
* @returns {number[]} [x, y, z] angular rate in rad/s.
|
|
111
|
+
* @returns {Promise<number[]>} [x, y, z] angular rate in rad/s.
|
|
97
112
|
*/
|
|
98
|
-
gyro() {
|
|
99
|
-
const buf = this._readBurst(_REG_GYRO_XOUT_H, 6);
|
|
113
|
+
async gyro() {
|
|
114
|
+
const buf = await this._readBurst(_REG_GYRO_XOUT_H, 6);
|
|
100
115
|
const gx = buf.readInt16BE(0);
|
|
101
116
|
const gy = buf.readInt16BE(2);
|
|
102
117
|
const gz = buf.readInt16BE(4);
|
|
@@ -116,95 +131,100 @@ class MPU6050Minimal {
|
|
|
116
131
|
*/
|
|
117
132
|
class MPU6050Full extends MPU6050Minimal {
|
|
118
133
|
/**
|
|
119
|
-
* @param {
|
|
134
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
|
|
120
135
|
*/
|
|
121
|
-
constructor(
|
|
122
|
-
super(
|
|
136
|
+
constructor(connection) {
|
|
137
|
+
super(connection);
|
|
123
138
|
}
|
|
124
139
|
|
|
125
140
|
/**
|
|
126
141
|
* Set gyroscope full-scale range.
|
|
127
142
|
* @param {number} [fullScale=0] - Range selector 0–3 (0=±250, 1=±500, 2=±1000, 3=±2000 dps).
|
|
143
|
+
* @returns {Promise<void>}
|
|
128
144
|
*/
|
|
129
|
-
configureGyro(fullScale = 0) {
|
|
145
|
+
async configureGyro(fullScale = 0) {
|
|
130
146
|
this._gyroFs = fullScale & 0x03;
|
|
131
|
-
this._writeReg(_REG_GYRO_CONFIG, (fullScale & 0x03) << 3);
|
|
147
|
+
await this._writeReg(_REG_GYRO_CONFIG, (fullScale & 0x03) << 3);
|
|
132
148
|
}
|
|
133
149
|
|
|
134
150
|
/**
|
|
135
151
|
* Set accelerometer full-scale range.
|
|
136
152
|
* @param {number} [fullScale=0] - Range selector 0–3 (0=±2g, 1=±4g, 2=±8g, 3=±16g).
|
|
153
|
+
* @returns {Promise<void>}
|
|
137
154
|
*/
|
|
138
|
-
configureAccel(fullScale = 0) {
|
|
155
|
+
async configureAccel(fullScale = 0) {
|
|
139
156
|
this._accelFs = fullScale & 0x03;
|
|
140
|
-
this._writeReg(_REG_ACCEL_CONFIG, (fullScale & 0x03) << 3);
|
|
157
|
+
await this._writeReg(_REG_ACCEL_CONFIG, (fullScale & 0x03) << 3);
|
|
141
158
|
}
|
|
142
159
|
|
|
143
160
|
/**
|
|
144
161
|
* Set digital low-pass filter bandwidth.
|
|
145
162
|
* @param {number} [dlpf=3] - Filter setting 0–6 (0=260/256 Hz, 1=184/188 Hz, 2=94/98 Hz,
|
|
146
163
|
* 3=44/42 Hz, 4=21/20 Hz, 5=10/10 Hz, 6=5/5 Hz; gyro/accel BW).
|
|
164
|
+
* @returns {Promise<void>}
|
|
147
165
|
*/
|
|
148
|
-
configureDlpf(dlpf = 3) {
|
|
149
|
-
this._writeReg(_REG_CONFIG, dlpf & 0x07);
|
|
166
|
+
async configureDlpf(dlpf = 3) {
|
|
167
|
+
await this._writeReg(_REG_CONFIG, dlpf & 0x07);
|
|
150
168
|
}
|
|
151
169
|
|
|
152
170
|
/**
|
|
153
171
|
* Set sample rate divider.
|
|
154
172
|
* @param {number} [divider=4] - SMPLRT_DIV value 0–255; output rate = 1 kHz / (1 + divider)
|
|
155
173
|
* when DLPF is active.
|
|
174
|
+
* @returns {Promise<void>}
|
|
156
175
|
*/
|
|
157
|
-
configureSampleRate(divider = 4) {
|
|
158
|
-
this._writeReg(_REG_SMPLRT_DIV, divider & 0xFF);
|
|
176
|
+
async configureSampleRate(divider = 4) {
|
|
177
|
+
await this._writeReg(_REG_SMPLRT_DIV, divider & 0xFF);
|
|
159
178
|
}
|
|
160
179
|
|
|
161
180
|
/**
|
|
162
181
|
* Read die temperature.
|
|
163
|
-
* @returns {number} Temperature in °C.
|
|
182
|
+
* @returns {Promise<number>} Temperature in °C.
|
|
164
183
|
*/
|
|
165
|
-
temperature() {
|
|
166
|
-
const raw = this._readReg16Signed(_REG_TEMP_OUT_H);
|
|
184
|
+
async temperature() {
|
|
185
|
+
const raw = await this._readReg16Signed(_REG_TEMP_OUT_H);
|
|
167
186
|
return raw / 340.0 + 36.53;
|
|
168
187
|
}
|
|
169
188
|
|
|
170
189
|
/**
|
|
171
190
|
* Read raw 3-axis accelerometer values.
|
|
172
|
-
* @returns {number[]} [x, y, z] raw 16-bit signed values.
|
|
191
|
+
* @returns {Promise<number[]>} [x, y, z] raw 16-bit signed values.
|
|
173
192
|
*/
|
|
174
|
-
accelRaw() {
|
|
175
|
-
const buf = this._readBurst(_REG_ACCEL_XOUT_H, 6);
|
|
193
|
+
async accelRaw() {
|
|
194
|
+
const buf = await this._readBurst(_REG_ACCEL_XOUT_H, 6);
|
|
176
195
|
return [buf.readInt16BE(0), buf.readInt16BE(2), buf.readInt16BE(4)];
|
|
177
196
|
}
|
|
178
197
|
|
|
179
198
|
/**
|
|
180
199
|
* Read raw 3-axis gyroscope values.
|
|
181
|
-
* @returns {number[]} [x, y, z] raw 16-bit signed values.
|
|
200
|
+
* @returns {Promise<number[]>} [x, y, z] raw 16-bit signed values.
|
|
182
201
|
*/
|
|
183
|
-
gyroRaw() {
|
|
184
|
-
const buf = this._readBurst(_REG_GYRO_XOUT_H, 6);
|
|
202
|
+
async gyroRaw() {
|
|
203
|
+
const buf = await this._readBurst(_REG_GYRO_XOUT_H, 6);
|
|
185
204
|
return [buf.readInt16BE(0), buf.readInt16BE(2), buf.readInt16BE(4)];
|
|
186
205
|
}
|
|
187
206
|
|
|
188
207
|
/**
|
|
189
208
|
* Check if new sensor data is available.
|
|
190
|
-
* @returns {boolean} True when DATA_RDY_INT is set in INT_STATUS.
|
|
209
|
+
* @returns {Promise<boolean>} True when DATA_RDY_INT is set in INT_STATUS.
|
|
191
210
|
*/
|
|
192
|
-
dataReady() {
|
|
193
|
-
return !!(this._readReg(_REG_INT_STATUS) & 0x01);
|
|
211
|
+
async dataReady() {
|
|
212
|
+
return !!((await this._readReg(_REG_INT_STATUS)) & 0x01);
|
|
194
213
|
}
|
|
195
214
|
|
|
196
215
|
/**
|
|
197
216
|
* Set or clear the SLEEP bit in PWR_MGMT_1.
|
|
198
217
|
* @param {boolean} [sleep=true] - True to enter sleep mode, false to wake.
|
|
218
|
+
* @returns {Promise<void>}
|
|
199
219
|
*/
|
|
200
|
-
setSleep(sleep = true) {
|
|
201
|
-
let val = this._readReg(_REG_PWR_MGMT_1);
|
|
220
|
+
async setSleep(sleep = true) {
|
|
221
|
+
let val = await this._readReg(_REG_PWR_MGMT_1);
|
|
202
222
|
if (sleep) {
|
|
203
223
|
val |= 0x40;
|
|
204
224
|
} else {
|
|
205
225
|
val &= ~0x40;
|
|
206
226
|
}
|
|
207
|
-
this._writeReg(_REG_PWR_MGMT_1, val);
|
|
227
|
+
await this._writeReg(_REG_PWR_MGMT_1, val);
|
|
208
228
|
}
|
|
209
229
|
|
|
210
230
|
/**
|
|
@@ -215,28 +235,29 @@ class MPU6050Full extends MPU6050Minimal {
|
|
|
215
235
|
* @param {boolean} [xg=false] - X gyroscope standby.
|
|
216
236
|
* @param {boolean} [yg=false] - Y gyroscope standby.
|
|
217
237
|
* @param {boolean} [zg=false] - Z gyroscope standby.
|
|
238
|
+
* @returns {Promise<void>}
|
|
218
239
|
*/
|
|
219
|
-
setStandby(xa = false, ya = false, za = false, xg = false, yg = false, zg = false) {
|
|
240
|
+
async setStandby(xa = false, ya = false, za = false, xg = false, yg = false, zg = false) {
|
|
220
241
|
const val = ((xa ? 1 : 0) << 5) | ((ya ? 1 : 0) << 4) | ((za ? 1 : 0) << 3) |
|
|
221
242
|
((xg ? 1 : 0) << 2) | ((yg ? 1 : 0) << 1) | (zg ? 1 : 0);
|
|
222
|
-
this._writeReg(_REG_PWR_MGMT_2, val);
|
|
243
|
+
await this._writeReg(_REG_PWR_MGMT_2, val);
|
|
223
244
|
}
|
|
224
245
|
|
|
225
246
|
/**
|
|
226
247
|
* Read the number of bytes in the FIFO buffer.
|
|
227
|
-
* @returns {number} FIFO byte count (0–1024).
|
|
248
|
+
* @returns {Promise<number>} FIFO byte count (0–1024).
|
|
228
249
|
*/
|
|
229
|
-
fifoCount() {
|
|
230
|
-
const buf = this._readBurst(_REG_FIFO_COUNTH, 2);
|
|
250
|
+
async fifoCount() {
|
|
251
|
+
const buf = await this._readBurst(_REG_FIFO_COUNTH, 2);
|
|
231
252
|
return ((buf[0] & 0x1F) << 8) | buf[1];
|
|
232
253
|
}
|
|
233
254
|
|
|
234
255
|
/**
|
|
235
256
|
* Read all available data from the FIFO buffer.
|
|
236
|
-
* @returns {Buffer} FIFO data.
|
|
257
|
+
* @returns {Promise<Buffer>} FIFO data.
|
|
237
258
|
*/
|
|
238
|
-
readFifo() {
|
|
239
|
-
const count = this.fifoCount();
|
|
259
|
+
async readFifo() {
|
|
260
|
+
const count = await this.fifoCount();
|
|
240
261
|
if (count === 0) return Buffer.alloc(0);
|
|
241
262
|
return this._readBurst(_REG_FIFO_R_W, count);
|
|
242
263
|
}
|
|
@@ -246,20 +267,22 @@ class MPU6050Full extends MPU6050Minimal {
|
|
|
246
267
|
* @param {boolean} [gyro=true] - Enable gyroscope data in FIFO.
|
|
247
268
|
* @param {boolean} [accel=true] - Enable accelerometer data in FIFO.
|
|
248
269
|
* @param {boolean} [temp=false] - Enable temperature data in FIFO.
|
|
270
|
+
* @returns {Promise<void>}
|
|
249
271
|
*/
|
|
250
|
-
enableFifo(gyro = true, accel = true, temp = false) {
|
|
272
|
+
async enableFifo(gyro = true, accel = true, temp = false) {
|
|
251
273
|
const fifoEn = ((accel ? 1 : 0) << 3) | ((temp ? 1 : 0) << 2) | ((gyro ? 1 : 0) << 4);
|
|
252
|
-
this._writeReg(_REG_FIFO_EN, fifoEn);
|
|
253
|
-
const userCtrl = this._readReg(_REG_USER_CTRL);
|
|
254
|
-
this._writeReg(_REG_USER_CTRL, userCtrl | 0x40);
|
|
274
|
+
await this._writeReg(_REG_FIFO_EN, fifoEn);
|
|
275
|
+
const userCtrl = await this._readReg(_REG_USER_CTRL);
|
|
276
|
+
await this._writeReg(_REG_USER_CTRL, userCtrl | 0x40);
|
|
255
277
|
}
|
|
256
278
|
|
|
257
279
|
/**
|
|
258
280
|
* Reset the FIFO buffer by setting FIFO_RST in USER_CTRL.
|
|
281
|
+
* @returns {Promise<void>}
|
|
259
282
|
*/
|
|
260
|
-
resetFifo() {
|
|
261
|
-
const userCtrl = this._readReg(_REG_USER_CTRL);
|
|
262
|
-
this._writeReg(_REG_USER_CTRL, userCtrl | 0x04);
|
|
283
|
+
async resetFifo() {
|
|
284
|
+
const userCtrl = await this._readReg(_REG_USER_CTRL);
|
|
285
|
+
await this._writeReg(_REG_USER_CTRL, userCtrl | 0x04);
|
|
263
286
|
}
|
|
264
287
|
}
|
|
265
288
|
|