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,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 };
|
|
@@ -0,0 +1,289 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_SMPLRT_DIV = 0x19;
|
|
4
|
+
const _REG_CONFIG = 0x1A;
|
|
5
|
+
const _REG_GYRO_CONFIG = 0x1B;
|
|
6
|
+
const _REG_ACCEL_CONFIG = 0x1C;
|
|
7
|
+
const _REG_FIFO_EN = 0x23;
|
|
8
|
+
const _REG_INT_PIN_CFG = 0x37;
|
|
9
|
+
const _REG_INT_ENABLE = 0x38;
|
|
10
|
+
const _REG_INT_STATUS = 0x3A;
|
|
11
|
+
const _REG_ACCEL_XOUT_H = 0x3B;
|
|
12
|
+
const _REG_TEMP_OUT_H = 0x41;
|
|
13
|
+
const _REG_GYRO_XOUT_H = 0x43;
|
|
14
|
+
const _REG_USER_CTRL = 0x6A;
|
|
15
|
+
const _REG_PWR_MGMT_1 = 0x6B;
|
|
16
|
+
const _REG_PWR_MGMT_2 = 0x6C;
|
|
17
|
+
const _REG_FIFO_COUNTH = 0x72;
|
|
18
|
+
const _REG_FIFO_COUNTL = 0x73;
|
|
19
|
+
const _REG_FIFO_R_W = 0x74;
|
|
20
|
+
const _REG_WHO_AM_I = 0x75;
|
|
21
|
+
|
|
22
|
+
const _WHO_AM_I_VALUE = 0x68;
|
|
23
|
+
|
|
24
|
+
const _ACCEL_SENSITIVITY = [16384.0, 8192.0, 4096.0, 2048.0];
|
|
25
|
+
const _GYRO_SENSITIVITY = [131.0, 65.5, 32.8, 16.4];
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* MPU-6050 6-axis MotionTracking device (accelerometer + gyroscope) — minimal interface.
|
|
29
|
+
*
|
|
30
|
+
* Provides 3-axis acceleration and 3-axis angular rate readings with no
|
|
31
|
+
* configuration beyond the connection. Performs device reset, WHO_AM_I check,
|
|
32
|
+
* and enables all sensors at defaults during initialization.
|
|
33
|
+
*
|
|
34
|
+
* Default configuration (written at construction):
|
|
35
|
+
* - Gyroscope full-scale: ±250 dps (FS_SEL=0)
|
|
36
|
+
* - Accelerometer full-scale: ±2 g (AFS_SEL=0)
|
|
37
|
+
* - DLPF: 44 Hz bandwidth (CONFIG DLPF_CFG=3, 1 kHz gyro rate)
|
|
38
|
+
* - Sample rate: 200 Hz (SMPLRT_DIV=4)
|
|
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.
|
|
51
|
+
*/
|
|
52
|
+
class MPU6050Minimal {
|
|
53
|
+
/**
|
|
54
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection (writeRead, write).
|
|
55
|
+
*/
|
|
56
|
+
constructor(connection) {
|
|
57
|
+
this._conn = connection;
|
|
58
|
+
this._accelFs = 0;
|
|
59
|
+
this._gyroFs = 0;
|
|
60
|
+
this._init();
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async _init() {
|
|
64
|
+
await this._writeReg(_REG_PWR_MGMT_1, 0x80);
|
|
65
|
+
const end1 = Date.now() + 100;
|
|
66
|
+
while (Date.now() < end1) {}
|
|
67
|
+
await this._writeReg(_REG_PWR_MGMT_1, 0x01);
|
|
68
|
+
const who = await this._readReg(_REG_WHO_AM_I);
|
|
69
|
+
if (who !== _WHO_AM_I_VALUE) {
|
|
70
|
+
throw new Error('MPU6050 WHO_AM_I: expected 0x' + _WHO_AM_I_VALUE.toString(16) + ', got 0x' + who.toString(16));
|
|
71
|
+
}
|
|
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);
|
|
76
|
+
const end2 = Date.now() + 35;
|
|
77
|
+
while (Date.now() < end2) {}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async _writeReg(reg, value) {
|
|
81
|
+
await this._conn.write(Buffer.from([reg, value]));
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async _readReg(reg) {
|
|
85
|
+
return (await this._conn.writeRead(Buffer.from([reg]), 1))[0];
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
async _readReg16Signed(reg) {
|
|
89
|
+
return (await this._conn.writeRead(Buffer.from([reg]), 2)).readInt16BE(0);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
async _readBurst(reg, len) {
|
|
93
|
+
return this._conn.writeRead(Buffer.from([reg]), len);
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/**
|
|
97
|
+
* Read 3-axis linear acceleration.
|
|
98
|
+
* @returns {Promise<number[]>} [x, y, z] acceleration in m/s².
|
|
99
|
+
*/
|
|
100
|
+
async accel() {
|
|
101
|
+
const buf = await this._readBurst(_REG_ACCEL_XOUT_H, 6);
|
|
102
|
+
const ax = buf.readInt16BE(0);
|
|
103
|
+
const ay = buf.readInt16BE(2);
|
|
104
|
+
const az = buf.readInt16BE(4);
|
|
105
|
+
const sens = _ACCEL_SENSITIVITY[this._accelFs];
|
|
106
|
+
return [ax / sens * 9.80665, ay / sens * 9.80665, az / sens * 9.80665];
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Read 3-axis angular rate.
|
|
111
|
+
* @returns {Promise<number[]>} [x, y, z] angular rate in rad/s.
|
|
112
|
+
*/
|
|
113
|
+
async gyro() {
|
|
114
|
+
const buf = await this._readBurst(_REG_GYRO_XOUT_H, 6);
|
|
115
|
+
const gx = buf.readInt16BE(0);
|
|
116
|
+
const gy = buf.readInt16BE(2);
|
|
117
|
+
const gz = buf.readInt16BE(4);
|
|
118
|
+
const sens = _GYRO_SENSITIVITY[this._gyroFs];
|
|
119
|
+
return [gx / sens * Math.PI / 180.0,
|
|
120
|
+
gy / sens * Math.PI / 180.0,
|
|
121
|
+
gz / sens * Math.PI / 180.0];
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* MPU-6050 full interface — extends MPU6050Minimal with configuration and FIFO support.
|
|
127
|
+
*
|
|
128
|
+
* Adds gyroscope and accelerometer full-scale configuration, DLPF settings,
|
|
129
|
+
* sample rate control, temperature reading, raw data access, data-ready polling,
|
|
130
|
+
* sleep/standby control, and FIFO management.
|
|
131
|
+
*/
|
|
132
|
+
class MPU6050Full extends MPU6050Minimal {
|
|
133
|
+
/**
|
|
134
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
|
|
135
|
+
*/
|
|
136
|
+
constructor(connection) {
|
|
137
|
+
super(connection);
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
/**
|
|
141
|
+
* Set gyroscope full-scale range.
|
|
142
|
+
* @param {number} [fullScale=0] - Range selector 0–3 (0=±250, 1=±500, 2=±1000, 3=±2000 dps).
|
|
143
|
+
* @returns {Promise<void>}
|
|
144
|
+
*/
|
|
145
|
+
async configureGyro(fullScale = 0) {
|
|
146
|
+
this._gyroFs = fullScale & 0x03;
|
|
147
|
+
await this._writeReg(_REG_GYRO_CONFIG, (fullScale & 0x03) << 3);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* Set accelerometer full-scale range.
|
|
152
|
+
* @param {number} [fullScale=0] - Range selector 0–3 (0=±2g, 1=±4g, 2=±8g, 3=±16g).
|
|
153
|
+
* @returns {Promise<void>}
|
|
154
|
+
*/
|
|
155
|
+
async configureAccel(fullScale = 0) {
|
|
156
|
+
this._accelFs = fullScale & 0x03;
|
|
157
|
+
await this._writeReg(_REG_ACCEL_CONFIG, (fullScale & 0x03) << 3);
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Set digital low-pass filter bandwidth.
|
|
162
|
+
* @param {number} [dlpf=3] - Filter setting 0–6 (0=260/256 Hz, 1=184/188 Hz, 2=94/98 Hz,
|
|
163
|
+
* 3=44/42 Hz, 4=21/20 Hz, 5=10/10 Hz, 6=5/5 Hz; gyro/accel BW).
|
|
164
|
+
* @returns {Promise<void>}
|
|
165
|
+
*/
|
|
166
|
+
async configureDlpf(dlpf = 3) {
|
|
167
|
+
await this._writeReg(_REG_CONFIG, dlpf & 0x07);
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Set sample rate divider.
|
|
172
|
+
* @param {number} [divider=4] - SMPLRT_DIV value 0–255; output rate = 1 kHz / (1 + divider)
|
|
173
|
+
* when DLPF is active.
|
|
174
|
+
* @returns {Promise<void>}
|
|
175
|
+
*/
|
|
176
|
+
async configureSampleRate(divider = 4) {
|
|
177
|
+
await this._writeReg(_REG_SMPLRT_DIV, divider & 0xFF);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Read die temperature.
|
|
182
|
+
* @returns {Promise<number>} Temperature in °C.
|
|
183
|
+
*/
|
|
184
|
+
async temperature() {
|
|
185
|
+
const raw = await this._readReg16Signed(_REG_TEMP_OUT_H);
|
|
186
|
+
return raw / 340.0 + 36.53;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Read raw 3-axis accelerometer values.
|
|
191
|
+
* @returns {Promise<number[]>} [x, y, z] raw 16-bit signed values.
|
|
192
|
+
*/
|
|
193
|
+
async accelRaw() {
|
|
194
|
+
const buf = await this._readBurst(_REG_ACCEL_XOUT_H, 6);
|
|
195
|
+
return [buf.readInt16BE(0), buf.readInt16BE(2), buf.readInt16BE(4)];
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Read raw 3-axis gyroscope values.
|
|
200
|
+
* @returns {Promise<number[]>} [x, y, z] raw 16-bit signed values.
|
|
201
|
+
*/
|
|
202
|
+
async gyroRaw() {
|
|
203
|
+
const buf = await this._readBurst(_REG_GYRO_XOUT_H, 6);
|
|
204
|
+
return [buf.readInt16BE(0), buf.readInt16BE(2), buf.readInt16BE(4)];
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* Check if new sensor data is available.
|
|
209
|
+
* @returns {Promise<boolean>} True when DATA_RDY_INT is set in INT_STATUS.
|
|
210
|
+
*/
|
|
211
|
+
async dataReady() {
|
|
212
|
+
return !!((await this._readReg(_REG_INT_STATUS)) & 0x01);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Set or clear the SLEEP bit in PWR_MGMT_1.
|
|
217
|
+
* @param {boolean} [sleep=true] - True to enter sleep mode, false to wake.
|
|
218
|
+
* @returns {Promise<void>}
|
|
219
|
+
*/
|
|
220
|
+
async setSleep(sleep = true) {
|
|
221
|
+
let val = await this._readReg(_REG_PWR_MGMT_1);
|
|
222
|
+
if (sleep) {
|
|
223
|
+
val |= 0x40;
|
|
224
|
+
} else {
|
|
225
|
+
val &= ~0x40;
|
|
226
|
+
}
|
|
227
|
+
await this._writeReg(_REG_PWR_MGMT_1, val);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Put individual axes into standby mode.
|
|
232
|
+
* @param {boolean} [xa=false] - X accelerometer standby.
|
|
233
|
+
* @param {boolean} [ya=false] - Y accelerometer standby.
|
|
234
|
+
* @param {boolean} [za=false] - Z accelerometer standby.
|
|
235
|
+
* @param {boolean} [xg=false] - X gyroscope standby.
|
|
236
|
+
* @param {boolean} [yg=false] - Y gyroscope standby.
|
|
237
|
+
* @param {boolean} [zg=false] - Z gyroscope standby.
|
|
238
|
+
* @returns {Promise<void>}
|
|
239
|
+
*/
|
|
240
|
+
async setStandby(xa = false, ya = false, za = false, xg = false, yg = false, zg = false) {
|
|
241
|
+
const val = ((xa ? 1 : 0) << 5) | ((ya ? 1 : 0) << 4) | ((za ? 1 : 0) << 3) |
|
|
242
|
+
((xg ? 1 : 0) << 2) | ((yg ? 1 : 0) << 1) | (zg ? 1 : 0);
|
|
243
|
+
await this._writeReg(_REG_PWR_MGMT_2, val);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Read the number of bytes in the FIFO buffer.
|
|
248
|
+
* @returns {Promise<number>} FIFO byte count (0–1024).
|
|
249
|
+
*/
|
|
250
|
+
async fifoCount() {
|
|
251
|
+
const buf = await this._readBurst(_REG_FIFO_COUNTH, 2);
|
|
252
|
+
return ((buf[0] & 0x1F) << 8) | buf[1];
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Read all available data from the FIFO buffer.
|
|
257
|
+
* @returns {Promise<Buffer>} FIFO data.
|
|
258
|
+
*/
|
|
259
|
+
async readFifo() {
|
|
260
|
+
const count = await this.fifoCount();
|
|
261
|
+
if (count === 0) return Buffer.alloc(0);
|
|
262
|
+
return this._readBurst(_REG_FIFO_R_W, count);
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
/**
|
|
266
|
+
* Configure and enable FIFO sources.
|
|
267
|
+
* @param {boolean} [gyro=true] - Enable gyroscope data in FIFO.
|
|
268
|
+
* @param {boolean} [accel=true] - Enable accelerometer data in FIFO.
|
|
269
|
+
* @param {boolean} [temp=false] - Enable temperature data in FIFO.
|
|
270
|
+
* @returns {Promise<void>}
|
|
271
|
+
*/
|
|
272
|
+
async enableFifo(gyro = true, accel = true, temp = false) {
|
|
273
|
+
const fifoEn = ((accel ? 1 : 0) << 3) | ((temp ? 1 : 0) << 2) | ((gyro ? 1 : 0) << 4);
|
|
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);
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/**
|
|
280
|
+
* Reset the FIFO buffer by setting FIFO_RST in USER_CTRL.
|
|
281
|
+
* @returns {Promise<void>}
|
|
282
|
+
*/
|
|
283
|
+
async resetFifo() {
|
|
284
|
+
const userCtrl = await this._readReg(_REG_USER_CTRL);
|
|
285
|
+
await this._writeReg(_REG_USER_CTRL, userCtrl | 0x04);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
module.exports = { MPU6050Minimal, MPU6050Full };
|