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,447 @@
|
|
|
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_ACCEL_CONFIG2 = 0x1D;
|
|
8
|
+
const _REG_LP_ACCEL_ODR = 0x1E;
|
|
9
|
+
const _REG_WOM_THR = 0x1F;
|
|
10
|
+
const _REG_FIFO_EN = 0x23;
|
|
11
|
+
const _REG_INT_PIN_CFG = 0x37;
|
|
12
|
+
const _REG_INT_ENABLE = 0x38;
|
|
13
|
+
const _REG_INT_STATUS = 0x3A;
|
|
14
|
+
const _REG_ACCEL_XOUT_H = 0x3B;
|
|
15
|
+
const _REG_TEMP_OUT_H = 0x41;
|
|
16
|
+
const _REG_GYRO_XOUT_H = 0x43;
|
|
17
|
+
const _REG_MOT_DETECT_CTRL = 0x69;
|
|
18
|
+
const _REG_USER_CTRL = 0x6A;
|
|
19
|
+
const _REG_PWR_MGMT_1 = 0x6B;
|
|
20
|
+
const _REG_PWR_MGMT_2 = 0x6C;
|
|
21
|
+
const _REG_FIFO_COUNTH = 0x72;
|
|
22
|
+
const _REG_FIFO_COUNTL = 0x73;
|
|
23
|
+
const _REG_FIFO_R_W = 0x74;
|
|
24
|
+
const _REG_WHO_AM_I = 0x75;
|
|
25
|
+
|
|
26
|
+
const _WHO_AM_I_VALUE = 0x73;
|
|
27
|
+
|
|
28
|
+
const _AK8963_ADDR = 0x0C;
|
|
29
|
+
|
|
30
|
+
const _AK8963_REG_WIA = 0x00;
|
|
31
|
+
const _AK8963_REG_ST1 = 0x02;
|
|
32
|
+
const _AK8963_REG_HXL = 0x03;
|
|
33
|
+
const _AK8963_REG_ST2 = 0x09;
|
|
34
|
+
const _AK8963_REG_CNTL1 = 0x0A;
|
|
35
|
+
const _AK8963_REG_CNTL2 = 0x0B;
|
|
36
|
+
const _AK8963_REG_ASAX = 0x10;
|
|
37
|
+
const _AK8963_REG_ASAY = 0x11;
|
|
38
|
+
const _AK8963_REG_ASAZ = 0x12;
|
|
39
|
+
|
|
40
|
+
const _AK8963_WIA_VALUE = 0x48;
|
|
41
|
+
|
|
42
|
+
const _ACCEL_SENSITIVITY = [16384.0, 8192.0, 4096.0, 2048.0];
|
|
43
|
+
const _GYRO_SENSITIVITY = [131.0, 65.5, 32.8, 16.4];
|
|
44
|
+
const _MAG_SENSITIVITY_14BIT = 0.6;
|
|
45
|
+
const _MAG_SENSITIVITY_16BIT = 0.15;
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* MPU-9255 9-axis MotionTracking device (accelerometer + gyroscope) — minimal interface.
|
|
49
|
+
*
|
|
50
|
+
* Provides 3-axis acceleration and 3-axis angular rate readings with no
|
|
51
|
+
* configuration beyond the connection. Performs device reset, WHO_AM_I check,
|
|
52
|
+
* and enables all sensors at defaults during initialization. Magnetometer
|
|
53
|
+
* and wake-on-motion are not included in Minimal — both require non-trivial
|
|
54
|
+
* secondary initialization paths.
|
|
55
|
+
*
|
|
56
|
+
* Default configuration (written at construction):
|
|
57
|
+
* - Gyroscope full-scale: ±250 dps (GYRO_FS_SEL=0)
|
|
58
|
+
* - Accelerometer full-scale: ±2 g (ACCEL_FS_SEL=0)
|
|
59
|
+
* - Gyroscope DLPF: 41 Hz bandwidth (CONFIG DLPF_CFG=3)
|
|
60
|
+
* - Accelerometer DLPF: 42 Hz bandwidth (ACCEL_CONFIG2 A_DLPFCFG=3)
|
|
61
|
+
* - Sample rate: 200 Hz (SMPLRT_DIV=4)
|
|
62
|
+
* - Clock: auto PLL (CLKSEL=1)
|
|
63
|
+
* - All six axes enabled
|
|
64
|
+
* - SPI only: I2C_IF_DIS set to prevent accidental I²C re-enable
|
|
65
|
+
*
|
|
66
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
|
|
67
|
+
*/
|
|
68
|
+
class MPU9255Minimal {
|
|
69
|
+
/**
|
|
70
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
|
|
71
|
+
*/
|
|
72
|
+
constructor(connection) {
|
|
73
|
+
this._conn = connection;
|
|
74
|
+
this._accelFs = 0;
|
|
75
|
+
this._gyroFs = 0;
|
|
76
|
+
this._init();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
async _init() {
|
|
80
|
+
await this._writeReg(_REG_PWR_MGMT_1, 0x80);
|
|
81
|
+
const end1 = Date.now() + 100;
|
|
82
|
+
while (Date.now() < end1) {}
|
|
83
|
+
await this._writeReg(_REG_PWR_MGMT_1, 0x01);
|
|
84
|
+
const who = await this._readReg(_REG_WHO_AM_I);
|
|
85
|
+
if (who !== _WHO_AM_I_VALUE) {
|
|
86
|
+
throw new Error('MPU9255 WHO_AM_I: expected 0x' + _WHO_AM_I_VALUE.toString(16) + ', got 0x' + who.toString(16));
|
|
87
|
+
}
|
|
88
|
+
await this._writeReg(_REG_GYRO_CONFIG, 0x00);
|
|
89
|
+
await this._writeReg(_REG_ACCEL_CONFIG, 0x00);
|
|
90
|
+
await this._writeReg(_REG_ACCEL_CONFIG2, 0x00);
|
|
91
|
+
await this._writeReg(_REG_CONFIG, 0x03);
|
|
92
|
+
await this._writeReg(_REG_SMPLRT_DIV, 0x04);
|
|
93
|
+
const end2 = Date.now() + 35;
|
|
94
|
+
while (Date.now() < end2) {}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
async _writeReg(reg, value) {
|
|
98
|
+
await this._conn.write(Buffer.from([reg, value]));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async _readReg(reg) {
|
|
102
|
+
return (await this._conn.writeRead(Buffer.from([reg]), 1))[0];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
async _readReg16Signed(reg) {
|
|
106
|
+
return (await this._conn.writeRead(Buffer.from([reg]), 2)).readInt16BE(0);
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async _readBurst(reg, len) {
|
|
110
|
+
return this._conn.writeRead(Buffer.from([reg]), len);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Read 3-axis linear acceleration.
|
|
115
|
+
* @returns {Promise<number[]>} [x, y, z] acceleration in m/s².
|
|
116
|
+
*/
|
|
117
|
+
async accel() {
|
|
118
|
+
const buf = await this._readBurst(_REG_ACCEL_XOUT_H, 6);
|
|
119
|
+
const ax = buf.readInt16BE(0);
|
|
120
|
+
const ay = buf.readInt16BE(2);
|
|
121
|
+
const az = buf.readInt16BE(4);
|
|
122
|
+
const sens = _ACCEL_SENSITIVITY[this._accelFs];
|
|
123
|
+
return [ax / sens * 9.80665, ay / sens * 9.80665, az / sens * 9.80665];
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Read 3-axis angular rate.
|
|
128
|
+
* @returns {Promise<number[]>} [x, y, z] angular rate in rad/s.
|
|
129
|
+
*/
|
|
130
|
+
async gyro() {
|
|
131
|
+
const buf = await this._readBurst(_REG_GYRO_XOUT_H, 6);
|
|
132
|
+
const gx = buf.readInt16BE(0);
|
|
133
|
+
const gy = buf.readInt16BE(2);
|
|
134
|
+
const gz = buf.readInt16BE(4);
|
|
135
|
+
const sens = _GYRO_SENSITIVITY[this._gyroFs];
|
|
136
|
+
return [gx / sens * Math.PI / 180.0,
|
|
137
|
+
gy / sens * Math.PI / 180.0,
|
|
138
|
+
gz / sens * Math.PI / 180.0];
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* MPU-9255 full interface — extends MPU9255Minimal with complete functionality.
|
|
144
|
+
*
|
|
145
|
+
* Adds gyroscope and accelerometer full-scale configuration, DLPF settings,
|
|
146
|
+
* sample rate control, temperature reading, magnetometer (AK8963) support,
|
|
147
|
+
* wake-on-motion, raw data access, data-ready polling, sleep/standby control,
|
|
148
|
+
* and FIFO management.
|
|
149
|
+
*
|
|
150
|
+
* The AK8963 magnetometer sits behind the MPU-9255's I²C bypass (BYPASS_EN)
|
|
151
|
+
* as its own device at address 0x0C, so it needs its own connection bound
|
|
152
|
+
* to that address on the same bus — it cannot be reached through the
|
|
153
|
+
* connection already bound to the MPU-9255's own address. Construct that
|
|
154
|
+
* second connection the same way as the primary one (e.g. on Linux,
|
|
155
|
+
* `new I2CConnection(1, 0x0C)` alongside `new I2CConnection(1, 0x68)`) and
|
|
156
|
+
* pass both in.
|
|
157
|
+
*/
|
|
158
|
+
class MPU9255Full extends MPU9255Minimal {
|
|
159
|
+
/**
|
|
160
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection pointing at the MPU-9255.
|
|
161
|
+
* @param {import('../../connection/connection').Connection} magConnection - Configured I²C connection bound to the AK8963's address (0x0C), on the same bus as connection.
|
|
162
|
+
*/
|
|
163
|
+
constructor(connection, magConnection) {
|
|
164
|
+
super(connection);
|
|
165
|
+
this._magConn = magConnection;
|
|
166
|
+
this._magEnabled = false;
|
|
167
|
+
this._magBits = 16;
|
|
168
|
+
this._magScaleX = 1.0;
|
|
169
|
+
this._magScaleY = 1.0;
|
|
170
|
+
this._magScaleZ = 1.0;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
async _ak8963Write(reg, value) {
|
|
174
|
+
await this._magConn.write(Buffer.from([reg, value]));
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
async _ak8963Read(reg) {
|
|
178
|
+
return (await this._magConn.writeRead(Buffer.from([reg]), 1))[0];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
async _ak8963ReadBurst(reg, len) {
|
|
182
|
+
return this._magConn.writeRead(Buffer.from([reg]), len);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Set gyroscope full-scale range.
|
|
187
|
+
* @param {number} [fullScale=0] - Range selector 0–3 (0=±250, 1=±500, 2=±1000, 3=±2000 dps).
|
|
188
|
+
* @returns {Promise<void>}
|
|
189
|
+
*/
|
|
190
|
+
async configureGyro(fullScale = 0) {
|
|
191
|
+
this._gyroFs = fullScale & 0x03;
|
|
192
|
+
await this._writeReg(_REG_GYRO_CONFIG, (fullScale & 0x03) << 3);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Set accelerometer full-scale range.
|
|
197
|
+
* @param {number} [fullScale=0] - Range selector 0–3 (0=±2g, 1=±4g, 2=±8g, 3=±16g).
|
|
198
|
+
* @returns {Promise<void>}
|
|
199
|
+
*/
|
|
200
|
+
async configureAccel(fullScale = 0) {
|
|
201
|
+
this._accelFs = fullScale & 0x03;
|
|
202
|
+
await this._writeReg(_REG_ACCEL_CONFIG, (fullScale & 0x03) << 3);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
/**
|
|
206
|
+
* Set digital low-pass filter bandwidth.
|
|
207
|
+
* @param {number} [gyroDlpf=3] - Gyro filter setting 0–6 (0=256 Hz, 1=188 Hz, 2=98 Hz, 3=41 Hz, 4=20 Hz, 5=10 Hz, 6=5 Hz).
|
|
208
|
+
* @param {number} [accelDlpf=3] - Accel filter setting 0–6 (0=460 Hz, 1=184 Hz, 2=92 Hz, 3=42 Hz, 5=20 Hz, 6=10 Hz). Note: 4 is not valid for accel.
|
|
209
|
+
* @returns {Promise<void>}
|
|
210
|
+
*/
|
|
211
|
+
async configureDlpf(gyroDlpf = 3, accelDlpf = 3) {
|
|
212
|
+
await this._writeReg(_REG_CONFIG, gyroDlpf & 0x07);
|
|
213
|
+
await this._writeReg(_REG_ACCEL_CONFIG2, accelDlpf & 0x07);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Set sample rate divider.
|
|
218
|
+
* @param {number} [divider=4] - SMPLRT_DIV value 0–255; output rate = 1 kHz / (1 + divider) when DLPF is active.
|
|
219
|
+
* @returns {Promise<void>}
|
|
220
|
+
*/
|
|
221
|
+
async configureSampleRate(divider = 4) {
|
|
222
|
+
await this._writeReg(_REG_SMPLRT_DIV, divider & 0xFF);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Read die temperature.
|
|
227
|
+
* @returns {Promise<number>} Temperature in °C.
|
|
228
|
+
*/
|
|
229
|
+
async temperature() {
|
|
230
|
+
const raw = await this._readReg16Signed(_REG_TEMP_OUT_H);
|
|
231
|
+
return raw / 333.87 + 21.0;
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Initialize AK8963 magnetometer via I²C bypass mode.
|
|
236
|
+
* @param {number} [bits=16] - Output resolution, 14 or 16.
|
|
237
|
+
* @param {number} [mode=6] - Operation mode (1=single, 2=8 Hz continuous, 6=100 Hz continuous).
|
|
238
|
+
* @returns {Promise<void>}
|
|
239
|
+
*/
|
|
240
|
+
async enableMag(bits = 16, mode = 6) {
|
|
241
|
+
await this._writeReg(_REG_INT_PIN_CFG, 0x22);
|
|
242
|
+
const end1 = Date.now() + 10;
|
|
243
|
+
while (Date.now() < end1) {}
|
|
244
|
+
|
|
245
|
+
await this._ak8963Write(_AK8963_REG_CNTL1, 0x00);
|
|
246
|
+
const end2 = Date.now() + 10;
|
|
247
|
+
while (Date.now() < end2) {}
|
|
248
|
+
|
|
249
|
+
await this._ak8963Write(_AK8963_REG_CNTL1, 0x0F);
|
|
250
|
+
const end3 = Date.now() + 10;
|
|
251
|
+
while (Date.now() < end3) {}
|
|
252
|
+
|
|
253
|
+
const asax = await this._ak8963Read(_AK8963_REG_ASAX);
|
|
254
|
+
const asay = await this._ak8963Read(_AK8963_REG_ASAY);
|
|
255
|
+
const asaz = await this._ak8963Read(_AK8963_REG_ASAZ);
|
|
256
|
+
|
|
257
|
+
this._magScaleX = (asax - 128) / 256.0 + 1.0;
|
|
258
|
+
this._magScaleY = (asay - 128) / 256.0 + 1.0;
|
|
259
|
+
this._magScaleZ = (asaz - 128) / 256.0 + 1.0;
|
|
260
|
+
|
|
261
|
+
await this._ak8963Write(_AK8963_REG_CNTL1, 0x00);
|
|
262
|
+
const end4 = Date.now() + 10;
|
|
263
|
+
while (Date.now() < end4) {}
|
|
264
|
+
|
|
265
|
+
let cntl1Val = 0;
|
|
266
|
+
if (bits === 16) {
|
|
267
|
+
cntl1Val |= 0x10;
|
|
268
|
+
}
|
|
269
|
+
cntl1Val |= (mode & 0x0F);
|
|
270
|
+
await this._ak8963Write(_AK8963_REG_CNTL1, cntl1Val);
|
|
271
|
+
const end5 = Date.now() + 10;
|
|
272
|
+
while (Date.now() < end5) {}
|
|
273
|
+
|
|
274
|
+
this._magEnabled = true;
|
|
275
|
+
this._magBits = bits;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Read 3-axis magnetic field.
|
|
280
|
+
* @returns {Promise<number[]>} [x, y, z] magnetic field in µT.
|
|
281
|
+
* @throws {Error} If magnetometer has not been enabled via enableMag().
|
|
282
|
+
*/
|
|
283
|
+
async mag() {
|
|
284
|
+
if (!this._magEnabled) {
|
|
285
|
+
throw new Error('Magnetometer not enabled. Call enableMag() first.');
|
|
286
|
+
}
|
|
287
|
+
const buf = await this._ak8963ReadBurst(_AK8963_REG_HXL, 7);
|
|
288
|
+
const mx = buf.readInt16LE(0);
|
|
289
|
+
const my = buf.readInt16LE(2);
|
|
290
|
+
const mz = buf.readInt16LE(4);
|
|
291
|
+
// ST2 at buf[6] must be read to unlock next measurement
|
|
292
|
+
|
|
293
|
+
const sens = (this._magBits === 16) ? _MAG_SENSITIVITY_16BIT : _MAG_SENSITIVITY_14BIT;
|
|
294
|
+
return [mx * sens * this._magScaleX,
|
|
295
|
+
my * sens * this._magScaleY,
|
|
296
|
+
mz * sens * this._magScaleZ];
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/**
|
|
300
|
+
* Read raw 3-axis accelerometer values.
|
|
301
|
+
* @returns {Promise<number[]>} [x, y, z] raw 16-bit signed values.
|
|
302
|
+
*/
|
|
303
|
+
async accelRaw() {
|
|
304
|
+
const buf = await this._readBurst(_REG_ACCEL_XOUT_H, 6);
|
|
305
|
+
return [buf.readInt16BE(0), buf.readInt16BE(2), buf.readInt16BE(4)];
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
/**
|
|
309
|
+
* Read raw 3-axis gyroscope values.
|
|
310
|
+
* @returns {Promise<number[]>} [x, y, z] raw 16-bit signed values.
|
|
311
|
+
*/
|
|
312
|
+
async gyroRaw() {
|
|
313
|
+
const buf = await this._readBurst(_REG_GYRO_XOUT_H, 6);
|
|
314
|
+
return [buf.readInt16BE(0), buf.readInt16BE(2), buf.readInt16BE(4)];
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
/**
|
|
318
|
+
* Read raw 3-axis magnetometer values.
|
|
319
|
+
* @returns {Promise<number[]>} [x, y, z] raw 16-bit signed values.
|
|
320
|
+
* @throws {Error} If magnetometer has not been enabled via enableMag().
|
|
321
|
+
*/
|
|
322
|
+
async magRaw() {
|
|
323
|
+
if (!this._magEnabled) {
|
|
324
|
+
throw new Error('Magnetometer not enabled. Call enableMag() first.');
|
|
325
|
+
}
|
|
326
|
+
// ST2 (buf[6]) is not used but must be read to unlock the next measurement.
|
|
327
|
+
const buf = await this._ak8963ReadBurst(_AK8963_REG_HXL, 7);
|
|
328
|
+
return [buf.readInt16LE(0), buf.readInt16LE(2), buf.readInt16LE(4)];
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* Check if new sensor data is available.
|
|
333
|
+
* @returns {Promise<boolean>} True when RAW_DATA_RDY_INT is set in INT_STATUS.
|
|
334
|
+
*/
|
|
335
|
+
async dataReady() {
|
|
336
|
+
return !!((await this._readReg(_REG_INT_STATUS)) & 0x01);
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
/**
|
|
340
|
+
* Set or clear the SLEEP bit in PWR_MGMT_1.
|
|
341
|
+
* @param {boolean} [sleep=true] - True to enter sleep mode, false to wake.
|
|
342
|
+
* @returns {Promise<void>}
|
|
343
|
+
*/
|
|
344
|
+
async setSleep(sleep = true) {
|
|
345
|
+
let val = await this._readReg(_REG_PWR_MGMT_1);
|
|
346
|
+
if (sleep) {
|
|
347
|
+
val |= 0x40;
|
|
348
|
+
} else {
|
|
349
|
+
val &= ~0x40;
|
|
350
|
+
}
|
|
351
|
+
await this._writeReg(_REG_PWR_MGMT_1, val);
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/**
|
|
355
|
+
* Read the number of bytes in the FIFO buffer.
|
|
356
|
+
* @returns {Promise<number>} FIFO byte count (0–512).
|
|
357
|
+
*/
|
|
358
|
+
async fifoCount() {
|
|
359
|
+
const buf = await this._readBurst(_REG_FIFO_COUNTH, 2);
|
|
360
|
+
return ((buf[0] & 0x1F) << 8) | buf[1];
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
/**
|
|
364
|
+
* Read all available data from the FIFO buffer.
|
|
365
|
+
* @returns {Promise<Buffer>} FIFO data.
|
|
366
|
+
*/
|
|
367
|
+
async readFifo() {
|
|
368
|
+
const count = await this.fifoCount();
|
|
369
|
+
if (count === 0) return Buffer.alloc(0);
|
|
370
|
+
return this._readBurst(_REG_FIFO_R_W, count);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Configure and enable FIFO sources.
|
|
375
|
+
* @param {boolean} [gyro=true] - Enable gyroscope data in FIFO.
|
|
376
|
+
* @param {boolean} [accel=true] - Enable accelerometer data in FIFO.
|
|
377
|
+
* @param {boolean} [temp=false] - Enable temperature data in FIFO.
|
|
378
|
+
* @returns {Promise<void>}
|
|
379
|
+
*/
|
|
380
|
+
async enableFifo(gyro = true, accel = true, temp = false) {
|
|
381
|
+
const fifoEn = ((accel ? 1 : 0) << 3) | ((temp ? 1 : 0) << 2) | ((gyro ? 1 : 0) << 4);
|
|
382
|
+
await this._writeReg(_REG_FIFO_EN, fifoEn);
|
|
383
|
+
const userCtrl = await this._readReg(_REG_USER_CTRL);
|
|
384
|
+
await this._writeReg(_REG_USER_CTRL, userCtrl | 0x40);
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* Reset the FIFO buffer by setting FIFO_RST in USER_CTRL.
|
|
389
|
+
* @returns {Promise<void>}
|
|
390
|
+
*/
|
|
391
|
+
async resetFifo() {
|
|
392
|
+
const userCtrl = await this._readReg(_REG_USER_CTRL);
|
|
393
|
+
await this._writeReg(_REG_USER_CTRL, userCtrl | 0x04);
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
/**
|
|
397
|
+
* Configure wake-on-motion and enter accelerometer-only low-power mode.
|
|
398
|
+
*
|
|
399
|
+
* Disables the gyroscope, sets the accelerometer DLPF to ~184 Hz, arms the
|
|
400
|
+
* hardware motion-detection logic, and enables CYCLE mode in PWR_MGMT_1
|
|
401
|
+
* so the accelerometer samples at the configured wake-up ODR. Re-enable
|
|
402
|
+
* the gyroscope (via PWR_MGMT_2) and clear CYCLE when motion is detected
|
|
403
|
+
* if full 6-axis data is needed again.
|
|
404
|
+
*
|
|
405
|
+
* @param {number} [thresholdMg=64] - Motion threshold in milligrams (4–1020 mg, quantized to 4 mg steps).
|
|
406
|
+
* @param {number} [odrHz=31.25] - Wake-up output data rate in Hz (0.24–500 Hz).
|
|
407
|
+
* @returns {Promise<void>}
|
|
408
|
+
*/
|
|
409
|
+
async configureWakeOnMotion(thresholdMg = 64, odrHz = 31.25) {
|
|
410
|
+
let thresholdLsb = Math.round(thresholdMg / 4.0);
|
|
411
|
+
if (thresholdLsb < 1) thresholdLsb = 1;
|
|
412
|
+
if (thresholdLsb > 255) thresholdLsb = 255;
|
|
413
|
+
|
|
414
|
+
const lposcTable = [
|
|
415
|
+
0.24, 0.49, 0.98, 1.95, 3.91, 7.81, 15.63, 31.25,
|
|
416
|
+
62.5, 125.0, 250.0, 500.0, 1000.0, 2000.0, 4000.0, 8000.0,
|
|
417
|
+
];
|
|
418
|
+
let bestSel = 0;
|
|
419
|
+
let bestDiff = Math.abs(odrHz - lposcTable[0]);
|
|
420
|
+
for (let sel = 1; sel < lposcTable.length; ++sel) {
|
|
421
|
+
const diff = Math.abs(odrHz - lposcTable[sel]);
|
|
422
|
+
if (diff < bestDiff) {
|
|
423
|
+
bestDiff = diff;
|
|
424
|
+
bestSel = sel;
|
|
425
|
+
}
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
await this._writeReg(_REG_PWR_MGMT_1, 0x01);
|
|
429
|
+
await this._writeReg(_REG_PWR_MGMT_2, 0x07);
|
|
430
|
+
await this._writeReg(_REG_ACCEL_CONFIG2, 0x09);
|
|
431
|
+
await this._writeReg(_REG_INT_ENABLE, 0x40);
|
|
432
|
+
await this._writeReg(_REG_MOT_DETECT_CTRL, 0xC0);
|
|
433
|
+
await this._writeReg(_REG_WOM_THR, thresholdLsb & 0xFF);
|
|
434
|
+
await this._writeReg(_REG_LP_ACCEL_ODR, bestSel & 0x0F);
|
|
435
|
+
await this._writeReg(_REG_PWR_MGMT_1, 0x21);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
/**
|
|
439
|
+
* Check if a wake-on-motion interrupt has fired.
|
|
440
|
+
* @returns {Promise<boolean>} True when WOM_INT (bit 6) is set in INT_STATUS.
|
|
441
|
+
*/
|
|
442
|
+
async motionDetected() {
|
|
443
|
+
return !!((await this._readReg(_REG_INT_STATUS)) & 0x40);
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
module.exports = { MPU9255Minimal, MPU9255Full };
|