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,366 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_WHO_AM_I = 0x0F;
|
|
4
|
+
const _REG_CTRL_REG1 = 0x20;
|
|
5
|
+
const _REG_CTRL_REG2 = 0x21;
|
|
6
|
+
const _REG_CTRL_REG3 = 0x22;
|
|
7
|
+
const _REG_CTRL_REG4 = 0x23;
|
|
8
|
+
const _REG_CTRL_REG5 = 0x24;
|
|
9
|
+
const _REG_OUT_TEMP = 0x26;
|
|
10
|
+
const _REG_STATUS = 0x27;
|
|
11
|
+
const _REG_OUT_X_L = 0x28;
|
|
12
|
+
const _REG_OUT_X_H = 0x29;
|
|
13
|
+
const _REG_OUT_Y_L = 0x2A;
|
|
14
|
+
const _REG_OUT_Y_H = 0x2B;
|
|
15
|
+
const _REG_OUT_Z_L = 0x2C;
|
|
16
|
+
const _REG_OUT_Z_H = 0x2D;
|
|
17
|
+
const _REG_FIFO_CTRL = 0x2E;
|
|
18
|
+
const _REG_FIFO_SRC = 0x2F;
|
|
19
|
+
const _REG_INT1_CFG = 0x30;
|
|
20
|
+
const _REG_INT1_SRC = 0x31;
|
|
21
|
+
const _REG_INT1_THS_XH = 0x32;
|
|
22
|
+
const _REG_INT1_THS_XL = 0x33;
|
|
23
|
+
const _REG_INT1_THS_YH = 0x34;
|
|
24
|
+
const _REG_INT1_THS_YL = 0x35;
|
|
25
|
+
const _REG_INT1_THS_ZH = 0x36;
|
|
26
|
+
const _REG_INT1_THS_ZL = 0x37;
|
|
27
|
+
const _REG_INT1_DURATION = 0x38;
|
|
28
|
+
|
|
29
|
+
const _WHO_AM_I_EXPECTED = 0xD3;
|
|
30
|
+
const _CTRL_REG1_DEFAULT = 0x0F;
|
|
31
|
+
const _CTRL_REG4_DEFAULT = 0x80;
|
|
32
|
+
|
|
33
|
+
const _SENSITIVITY = {
|
|
34
|
+
250: 8.75e-3,
|
|
35
|
+
500: 17.5e-3,
|
|
36
|
+
2000: 70.0e-3,
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const _DPS_TO_RAD = Math.PI / 180.0;
|
|
40
|
+
|
|
41
|
+
function _int16Le(data, offset) {
|
|
42
|
+
let v = data[offset] | (data[offset + 1] << 8);
|
|
43
|
+
if (v & 0x8000) v -= 0x10000;
|
|
44
|
+
return v;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class L3G4200DMinimal {
|
|
48
|
+
/**
|
|
49
|
+
* L3G4200D three-axis MEMS gyroscope — minimal interface.
|
|
50
|
+
*
|
|
51
|
+
* Provides angular rate readings on the X, Y, and Z axes with no
|
|
52
|
+
* configuration beyond the connection. I²C address is 0x68 (SA0=GND) or
|
|
53
|
+
* 0x69 (SA0=VDD). SPI uses Mode 3 (CPOL=CPHA=1) by default.
|
|
54
|
+
*
|
|
55
|
+
* Default configuration (baked in at construction):
|
|
56
|
+
* - 100 Hz ODR, 12.5 Hz LPF2 cutoff (DR=00, BW=00)
|
|
57
|
+
* - ±250 dps full scale
|
|
58
|
+
* - BDU=1
|
|
59
|
+
* - All axes enabled, normal power mode
|
|
60
|
+
* - FIFO disabled
|
|
61
|
+
* - HPF disabled
|
|
62
|
+
*
|
|
63
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
|
|
64
|
+
* @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
|
|
65
|
+
*/
|
|
66
|
+
constructor(connection, busType = 'i2c') {
|
|
67
|
+
this._conn = connection;
|
|
68
|
+
this._busType = busType;
|
|
69
|
+
this._fullScale = 250;
|
|
70
|
+
this._init();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async _init() {
|
|
74
|
+
try {
|
|
75
|
+
const who = (await this._readReg(_REG_WHO_AM_I, 1))[0];
|
|
76
|
+
if (who !== _WHO_AM_I_EXPECTED) return;
|
|
77
|
+
await this._writeReg(_REG_CTRL_REG4, _CTRL_REG4_DEFAULT);
|
|
78
|
+
await this._writeReg(_REG_CTRL_REG1, _CTRL_REG1_DEFAULT);
|
|
79
|
+
} catch (e) { /* bus may be idle */ }
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
async _writeReg(reg, value) {
|
|
83
|
+
const addr = this._busType === 'spi' ? (reg & 0x3F) : reg;
|
|
84
|
+
await this._conn.write(Buffer.from([addr, value & 0xFF]));
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async _readReg(reg, n) {
|
|
88
|
+
let addr;
|
|
89
|
+
if (this._busType === 'spi') {
|
|
90
|
+
addr = reg | 0xC0; // READ=1, MS=1 (auto-increment)
|
|
91
|
+
} else if (n > 1) {
|
|
92
|
+
addr = reg | 0x80; // MSB set = I²C multi-byte auto-increment
|
|
93
|
+
} else {
|
|
94
|
+
addr = reg;
|
|
95
|
+
}
|
|
96
|
+
return this._conn.writeRead(Buffer.from([addr & 0xFF]), n);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
_sensitivity() {
|
|
100
|
+
return _SENSITIVITY[this._fullScale];
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Read angular rate on all three axes as a single burst transaction.
|
|
105
|
+
* @returns {Promise<[number, number, number]>} (x_rad_s, y_rad_s, z_rad_s).
|
|
106
|
+
*/
|
|
107
|
+
async angularRate() {
|
|
108
|
+
const buf = await this._readReg(_REG_OUT_X_L, 6);
|
|
109
|
+
const sens = this._sensitivity();
|
|
110
|
+
const x_dps = _int16Le(buf, 0) * sens;
|
|
111
|
+
const y_dps = _int16Le(buf, 2) * sens;
|
|
112
|
+
const z_dps = _int16Le(buf, 4) * sens;
|
|
113
|
+
return [x_dps * _DPS_TO_RAD, y_dps * _DPS_TO_RAD, z_dps * _DPS_TO_RAD];
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
class L3G4200DFull extends L3G4200DMinimal {
|
|
118
|
+
/**
|
|
119
|
+
* L3G4200D full interface — extends L3G4200DMinimal with full configuration,
|
|
120
|
+
* FIFO, high-pass filter, interrupts, axis-enable, and power-mode control.
|
|
121
|
+
*
|
|
122
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
|
|
123
|
+
* @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
|
|
124
|
+
*/
|
|
125
|
+
constructor(connection, busType = 'i2c') {
|
|
126
|
+
super(connection, busType);
|
|
127
|
+
this._odr = 0;
|
|
128
|
+
this._bw = 0;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** Output data rate codes (DR[1:0] in CTRL_REG1). */
|
|
132
|
+
static ODR_100_HZ = 0;
|
|
133
|
+
static ODR_200_HZ = 1;
|
|
134
|
+
static ODR_400_HZ = 2;
|
|
135
|
+
static ODR_800_HZ = 3;
|
|
136
|
+
|
|
137
|
+
/** Full-scale ranges. */
|
|
138
|
+
static FS_250_DPS = 250;
|
|
139
|
+
static FS_500_DPS = 500;
|
|
140
|
+
static FS_2000_DPS = 2000;
|
|
141
|
+
|
|
142
|
+
/** FIFO modes (FM[2:0] in FIFO_CTRL_REG). */
|
|
143
|
+
static FIFO_BYPASS = 0;
|
|
144
|
+
static FIFO_FIFO = 1;
|
|
145
|
+
static FIFO_STREAM = 2;
|
|
146
|
+
static FIFO_STREAM_TO_FIFO = 3;
|
|
147
|
+
static FIFO_BYPASS_TO_STREAM = 4;
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Configure ODR, LPF2 bandwidth, and full scale.
|
|
151
|
+
* @param {number} odr ODR code (0=100, 1=200, 2=400, 3=800 Hz).
|
|
152
|
+
* @param {number} bandwidth LPF2 bandwidth code 0-3.
|
|
153
|
+
* @param {number} fullScale Full-scale in dps (250, 500, or 2000).
|
|
154
|
+
*/
|
|
155
|
+
async configure(odr, bandwidth, fullScale) {
|
|
156
|
+
if (![250, 500, 2000].includes(fullScale)) {
|
|
157
|
+
throw new Error('fullScale must be 250, 500 or 2000 dps');
|
|
158
|
+
}
|
|
159
|
+
this._odr = odr & 0x3;
|
|
160
|
+
this._bw = bandwidth & 0x3;
|
|
161
|
+
this._fullScale = fullScale;
|
|
162
|
+
const ctrl1 = _CTRL_REG1_DEFAULT | ((this._odr & 0x3) << 6) | ((this._bw & 0x3) << 4);
|
|
163
|
+
await this._writeReg(_REG_CTRL_REG1, ctrl1);
|
|
164
|
+
const fsBits = fullScale === 250 ? 0 : (fullScale === 500 ? 1 : 2);
|
|
165
|
+
await this._writeReg(_REG_CTRL_REG4, _CTRL_REG4_DEFAULT | ((fsBits & 0x3) << 4));
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Update the full-scale range.
|
|
170
|
+
* @param {number} fullScale 250, 500, or 2000 dps.
|
|
171
|
+
*/
|
|
172
|
+
async setFullScale(fullScale) {
|
|
173
|
+
if (![250, 500, 2000].includes(fullScale)) {
|
|
174
|
+
throw new Error('fullScale must be 250, 500 or 2000 dps');
|
|
175
|
+
}
|
|
176
|
+
this._fullScale = fullScale;
|
|
177
|
+
const fsBits = fullScale === 250 ? 0 : (fullScale === 500 ? 1 : 2);
|
|
178
|
+
const ctrl4 = (await this._readReg(_REG_CTRL_REG4, 1))[0];
|
|
179
|
+
await this._writeReg(_REG_CTRL_REG4, (ctrl4 & 0xCF) | ((fsBits & 0x3) << 4));
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/** @returns {Promise<number>} WHO_AM_I (0xD3 for genuine L3G4200D). */
|
|
183
|
+
async whoAmI() {
|
|
184
|
+
return (await this._readReg(_REG_WHO_AM_I, 1))[0];
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/** @returns {Promise<number>} Raw STATUS_REG byte. */
|
|
188
|
+
async status() {
|
|
189
|
+
return (await this._readReg(_REG_STATUS, 1))[0];
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** @returns {Promise<boolean>} True if STATUS_REG.ZYXDA (bit 3) is set. */
|
|
193
|
+
async dataReady() {
|
|
194
|
+
return Boolean((await this.status()) & 0x08);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** @returns {Promise<number>} Signed 8-bit temperature count (-1 °C/digit). */
|
|
198
|
+
async temperature() {
|
|
199
|
+
let raw = (await this._readReg(_REG_OUT_TEMP, 1))[0];
|
|
200
|
+
if (raw & 0x80) raw -= 0x100;
|
|
201
|
+
return raw;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/** Enter power-down mode (PD=0 in CTRL_REG1). */
|
|
205
|
+
async powerDown() {
|
|
206
|
+
const ctrl1 = (await this._readReg(_REG_CTRL_REG1, 1))[0] & 0xF7;
|
|
207
|
+
await this._writeReg(_REG_CTRL_REG1, ctrl1);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/** Wake from power-down (PD=1); previously enabled axes restored. */
|
|
211
|
+
async wakeUp() {
|
|
212
|
+
const ctrl1 = (await this._readReg(_REG_CTRL_REG1, 1))[0] | 0x08;
|
|
213
|
+
await this._writeReg(_REG_CTRL_REG1, ctrl1);
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** Enter sleep mode (PD=1, all axes disabled). */
|
|
217
|
+
async sleep() {
|
|
218
|
+
await this._writeReg(_REG_CTRL_REG1, 0x08);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Enable or disable individual axes (Xen/Yen/Zen in CTRL_REG1).
|
|
223
|
+
* @param {boolean} x
|
|
224
|
+
* @param {boolean} y
|
|
225
|
+
* @param {boolean} z
|
|
226
|
+
*/
|
|
227
|
+
async enableAxes(x, y, z) {
|
|
228
|
+
const ctrl1 = (await this._readReg(_REG_CTRL_REG1, 1))[0] & 0xF8;
|
|
229
|
+
const val = ctrl1 | (z ? 0x04 : 0) | (y ? 0x02 : 0) | (x ? 0x01 : 0);
|
|
230
|
+
await this._writeReg(_REG_CTRL_REG1, val);
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/**
|
|
234
|
+
* Configure and enable the FIFO.
|
|
235
|
+
* @param {number} mode FIFO mode 0-4.
|
|
236
|
+
* @param {number} watermark Watermark threshold 0-31.
|
|
237
|
+
*/
|
|
238
|
+
async enableFifo(mode, watermark) {
|
|
239
|
+
if (mode < 0 || mode > 4) throw new Error('mode must be 0..4');
|
|
240
|
+
if (watermark < 0 || watermark > 31) throw new Error('watermark must be 0..31');
|
|
241
|
+
const ctrl5 = (await this._readReg(_REG_CTRL_REG5, 1))[0] | 0x40;
|
|
242
|
+
await this._writeReg(_REG_CTRL_REG5, ctrl5);
|
|
243
|
+
await this._writeReg(_REG_FIFO_CTRL, ((mode & 0x7) << 5) | (watermark & 0x1F));
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/** Disable the FIFO. */
|
|
247
|
+
async disableFifo() {
|
|
248
|
+
const ctrl5 = (await this._readReg(_REG_CTRL_REG5, 1))[0] & ~0x40;
|
|
249
|
+
await this._writeReg(_REG_CTRL_REG5, ctrl5);
|
|
250
|
+
await this._writeReg(_REG_FIFO_CTRL, 0x00);
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/** @returns {Promise<number>} FSS[4:0] from FIFO_SRC_REG. */
|
|
254
|
+
async fifoSamples() {
|
|
255
|
+
return (await this._readReg(_REG_FIFO_SRC, 1))[0] & 0x1F;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
/**
|
|
259
|
+
* Read all stored FIFO samples.
|
|
260
|
+
* @returns {Promise<Array<[number, number, number]>>} List of (x_rad_s, y_rad_s, z_rad_s).
|
|
261
|
+
*/
|
|
262
|
+
async readFifo() {
|
|
263
|
+
const n = await this.fifoSamples();
|
|
264
|
+
if (n === 0) return [];
|
|
265
|
+
const sens = this._sensitivity();
|
|
266
|
+
const buf = await this._readReg(_REG_OUT_X_L, n * 6);
|
|
267
|
+
const out = [];
|
|
268
|
+
for (let i = 0; i < n; i++) {
|
|
269
|
+
const o = i * 6;
|
|
270
|
+
const x = _int16Le(buf, o) * sens * _DPS_TO_RAD;
|
|
271
|
+
const y = _int16Le(buf, o + 2) * sens * _DPS_TO_RAD;
|
|
272
|
+
const z = _int16Le(buf, o + 4) * sens * _DPS_TO_RAD;
|
|
273
|
+
out.push([x, y, z]);
|
|
274
|
+
}
|
|
275
|
+
return out;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Enable the high-pass filter.
|
|
280
|
+
* @param {number} mode HPF mode 0-3.
|
|
281
|
+
* @param {number} cutoff HPF cutoff code 0-9.
|
|
282
|
+
*/
|
|
283
|
+
async enableHighpass(mode, cutoff) {
|
|
284
|
+
if (mode < 0 || mode > 3) throw new Error('mode must be 0..3');
|
|
285
|
+
if (cutoff < 0 || cutoff > 9) throw new Error('cutoff must be 0..9');
|
|
286
|
+
await this._writeReg(_REG_CTRL_REG2, ((mode & 0x3) << 4) | (cutoff & 0x0F));
|
|
287
|
+
const ctrl5 = (await this._readReg(_REG_CTRL_REG5, 1))[0] | 0x10;
|
|
288
|
+
await this._writeReg(_REG_CTRL_REG5, ctrl5);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
/** Disable the high-pass filter. */
|
|
292
|
+
async disableHighpass() {
|
|
293
|
+
const ctrl5 = (await this._readReg(_REG_CTRL_REG5, 1))[0] & ~0x10;
|
|
294
|
+
await this._writeReg(_REG_CTRL_REG5, ctrl5);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
/**
|
|
298
|
+
* Configure INT1_CFG axis/direction events.
|
|
299
|
+
* @param {boolean} xHigh
|
|
300
|
+
* @param {boolean} xLow
|
|
301
|
+
* @param {boolean} yHigh
|
|
302
|
+
* @param {boolean} yLow
|
|
303
|
+
* @param {boolean} zHigh
|
|
304
|
+
* @param {boolean} zLow
|
|
305
|
+
* @param {boolean} andMode
|
|
306
|
+
* @param {boolean} latch
|
|
307
|
+
*/
|
|
308
|
+
async setInterrupt(xHigh, xLow, yHigh, yLow, zHigh, zLow, andMode, latch) {
|
|
309
|
+
let cfg = 0;
|
|
310
|
+
if (andMode) cfg |= 0x80;
|
|
311
|
+
if (latch) cfg |= 0x40;
|
|
312
|
+
if (zHigh) cfg |= 0x20;
|
|
313
|
+
if (zLow) cfg |= 0x10;
|
|
314
|
+
if (yHigh) cfg |= 0x08;
|
|
315
|
+
if (yLow) cfg |= 0x04;
|
|
316
|
+
if (xHigh) cfg |= 0x02;
|
|
317
|
+
if (xLow) cfg |= 0x01;
|
|
318
|
+
await this._writeReg(_REG_INT1_CFG, cfg);
|
|
319
|
+
if (cfg & 0x3F) {
|
|
320
|
+
const ctrl3 = (await this._readReg(_REG_CTRL_REG3, 1))[0] | 0x80;
|
|
321
|
+
await this._writeReg(_REG_CTRL_REG3, ctrl3);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Set the interrupt threshold for one axis.
|
|
327
|
+
* @param {string} axis 'x', 'y', or 'z'.
|
|
328
|
+
* @param {number} thresholdDps Threshold in dps.
|
|
329
|
+
*/
|
|
330
|
+
async setThreshold(axis, thresholdDps) {
|
|
331
|
+
const raw = Math.trunc(thresholdDps / this._sensitivity()) & 0x7FFF;
|
|
332
|
+
let hi, lo;
|
|
333
|
+
if (axis === 'x') { hi = _REG_INT1_THS_XH; lo = _REG_INT1_THS_XL; }
|
|
334
|
+
else if (axis === 'y') { hi = _REG_INT1_THS_YH; lo = _REG_INT1_THS_YL; }
|
|
335
|
+
else if (axis === 'z') { hi = _REG_INT1_THS_ZH; lo = _REG_INT1_THS_ZL; }
|
|
336
|
+
else throw new Error("axis must be 'x', 'y' or 'z'");
|
|
337
|
+
await this._writeReg(hi, (raw >> 8) & 0x7F);
|
|
338
|
+
await this._writeReg(lo, raw & 0xFF);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Set INT1_DURATION.
|
|
343
|
+
* @param {number} samples Duration 0-127.
|
|
344
|
+
* @param {boolean} wait If true, INT1 stays asserted until INT1_SRC is read.
|
|
345
|
+
*/
|
|
346
|
+
async setDuration(samples, wait) {
|
|
347
|
+
if (samples < 0 || samples > 127) throw new Error('samples must be 0..127');
|
|
348
|
+
await this._writeReg(_REG_INT1_DURATION, ((wait ? 1 : 0) << 7) | (samples & 0x7F));
|
|
349
|
+
}
|
|
350
|
+
|
|
351
|
+
/** @returns {Promise<number>} Raw INT1_SRC byte; reading clears the interrupt-active bit. */
|
|
352
|
+
async readIntSource() {
|
|
353
|
+
return (await this._readReg(_REG_INT1_SRC, 1))[0];
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/**
|
|
357
|
+
* Route the data-ready signal to the DRDY/INT2 pin.
|
|
358
|
+
* @param {boolean} enable
|
|
359
|
+
*/
|
|
360
|
+
async setDataReadyPin(enable) {
|
|
361
|
+
const ctrl3 = (await this._readReg(_REG_CTRL_REG3, 1))[0];
|
|
362
|
+
await this._writeReg(_REG_CTRL_REG3, enable ? (ctrl3 | 0x08) : (ctrl3 & ~0x08));
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
module.exports = { L3G4200DMinimal, L3G4200DFull };
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_WHO_AM_I = 0x0F;
|
|
4
|
+
const _REG_CTRL_REG1 = 0x20;
|
|
5
|
+
const _REG_CTRL_REG2 = 0x21;
|
|
6
|
+
const _REG_CTRL_REG3 = 0x22;
|
|
7
|
+
const _REG_CTRL_REG4 = 0x23;
|
|
8
|
+
const _REG_CTRL_REG5 = 0x24;
|
|
9
|
+
const _REG_OUT_TEMP = 0x26;
|
|
10
|
+
const _REG_STATUS = 0x27;
|
|
11
|
+
const _REG_OUT_X_L = 0x28;
|
|
12
|
+
const _REG_OUT_X_H = 0x29;
|
|
13
|
+
const _REG_OUT_Y_L = 0x2A;
|
|
14
|
+
const _REG_OUT_Y_H = 0x2B;
|
|
15
|
+
const _REG_OUT_Z_L = 0x2C;
|
|
16
|
+
const _REG_OUT_Z_H = 0x2D;
|
|
17
|
+
const _REG_FIFO_CTRL = 0x2E;
|
|
18
|
+
const _REG_FIFO_SRC = 0x2F;
|
|
19
|
+
const _REG_INT1_CFG = 0x30;
|
|
20
|
+
const _REG_INT1_SRC = 0x31;
|
|
21
|
+
const _REG_INT1_TSH_XH = 0x32;
|
|
22
|
+
const _REG_INT1_TSH_XL = 0x33;
|
|
23
|
+
const _REG_INT1_TSH_YH = 0x34;
|
|
24
|
+
const _REG_INT1_TSH_YL = 0x35;
|
|
25
|
+
const _REG_INT1_TSH_ZH = 0x36;
|
|
26
|
+
const _REG_INT1_TSH_ZL = 0x37;
|
|
27
|
+
const _REG_INT1_DURATION = 0x38;
|
|
28
|
+
|
|
29
|
+
const _WHO_AM_I_L3GD20 = 0xD4;
|
|
30
|
+
const _WHO_AM_I_L3GD20H = 0xD7;
|
|
31
|
+
const _CTRL_REG1_DEFAULT = 0x0F;
|
|
32
|
+
const _CTRL_REG4_DEFAULT = 0x80;
|
|
33
|
+
|
|
34
|
+
const _SENSITIVITY = {
|
|
35
|
+
250: 8.75e-3,
|
|
36
|
+
500: 17.5e-3,
|
|
37
|
+
2000: 70.0e-3,
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const _DPS_TO_RAD = Math.PI / 180.0;
|
|
41
|
+
|
|
42
|
+
function _int16Le(data, offset) {
|
|
43
|
+
let v = data[offset] | (data[offset + 1] << 8);
|
|
44
|
+
if (v & 0x8000) v -= 0x10000;
|
|
45
|
+
return v;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
class L3GD20HMinimal {
|
|
49
|
+
/**
|
|
50
|
+
* L3GD20H (and L3GD20) three-axis MEMS gyroscope — minimal interface.
|
|
51
|
+
*
|
|
52
|
+
* Provides angular rate readings on the X, Y, and Z axes with no
|
|
53
|
+
* configuration beyond the connection. I²C address is 0x6A (SA0/SDO=GND) or
|
|
54
|
+
* 0x6B (SA0/SDO=VCC). SPI uses Mode 3 (CPOL=CPHA=1) by default.
|
|
55
|
+
*
|
|
56
|
+
* Default configuration (baked in at construction):
|
|
57
|
+
* - 95 Hz ODR, default bandwidth (DR=00, BW=00)
|
|
58
|
+
* - ±250 dps full scale (sensitivity 8.75 mdps/digit)
|
|
59
|
+
* - BDU=1 (block data update — hold registers until MSB+LSB read)
|
|
60
|
+
* - All axes enabled, normal power mode
|
|
61
|
+
* - 250 ms startup delay for gyroscope stabilization
|
|
62
|
+
*
|
|
63
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
|
|
64
|
+
* @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
|
|
65
|
+
*/
|
|
66
|
+
constructor(connection, busType = 'i2c') {
|
|
67
|
+
this._conn = connection;
|
|
68
|
+
this._busType = busType;
|
|
69
|
+
this._fullScale = 250;
|
|
70
|
+
this._init();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async _init() {
|
|
74
|
+
try {
|
|
75
|
+
const who = (await this._readReg(_REG_WHO_AM_I, 1))[0];
|
|
76
|
+
if (who !== _WHO_AM_I_L3GD20 && who !== _WHO_AM_I_L3GD20H) return;
|
|
77
|
+
await this._writeReg(_REG_CTRL_REG4, _CTRL_REG4_DEFAULT);
|
|
78
|
+
await this._writeReg(_REG_CTRL_REG1, _CTRL_REG1_DEFAULT);
|
|
79
|
+
// 250 ms startup delay
|
|
80
|
+
await new Promise(resolve => setTimeout(resolve, 250));
|
|
81
|
+
} catch (e) { /* bus may be idle */ }
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async _writeReg(reg, value) {
|
|
85
|
+
const addr = this._busType === 'spi' ? (reg & 0x3F) : reg;
|
|
86
|
+
await this._conn.write(Buffer.from([addr, value & 0xFF]));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async _readReg(reg, n) {
|
|
90
|
+
let addr;
|
|
91
|
+
if (this._busType === 'spi') {
|
|
92
|
+
addr = reg | 0xC0; // READ=1, MS=1 (auto-increment)
|
|
93
|
+
} else if (n > 1) {
|
|
94
|
+
addr = reg | 0x80; // MSB set = I²C multi-byte auto-increment
|
|
95
|
+
} else {
|
|
96
|
+
addr = reg;
|
|
97
|
+
}
|
|
98
|
+
return this._conn.writeRead(Buffer.from([addr & 0xFF]), n);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
_sensitivity() {
|
|
102
|
+
return _SENSITIVITY[this._fullScale];
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Read angular rate on all three axes as a single burst transaction.
|
|
107
|
+
* @returns {Promise<[number, number, number]>} (x_rad_s, y_rad_s, z_rad_s).
|
|
108
|
+
*/
|
|
109
|
+
async gyro() {
|
|
110
|
+
const buf = await this._readReg(_REG_OUT_X_L, 6);
|
|
111
|
+
const sens = this._sensitivity();
|
|
112
|
+
const x_dps = _int16Le(buf, 0) * sens;
|
|
113
|
+
const y_dps = _int16Le(buf, 2) * sens;
|
|
114
|
+
const z_dps = _int16Le(buf, 4) * sens;
|
|
115
|
+
return [x_dps * _DPS_TO_RAD, y_dps * _DPS_TO_RAD, z_dps * _DPS_TO_RAD];
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
class L3GD20HFull extends L3GD20HMinimal {
|
|
120
|
+
/**
|
|
121
|
+
* L3GD20H full interface — extends L3GD20HMinimal with full configuration,
|
|
122
|
+
* FIFO, high-pass filter, interrupts, axis-enable, and power-mode control.
|
|
123
|
+
*
|
|
124
|
+
* Adds ODR/bandwidth/full-scale configuration, FIFO with all five modes
|
|
125
|
+
* and watermark, high-pass filter with selectable cutoff, per-axis
|
|
126
|
+
* interrupt generation with threshold and duration, INT1 pin routing,
|
|
127
|
+
* and access to temperature and status registers.
|
|
128
|
+
*
|
|
129
|
+
* @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
|
|
130
|
+
* @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
|
|
131
|
+
*/
|
|
132
|
+
constructor(connection, busType = 'i2c') {
|
|
133
|
+
super(connection, busType);
|
|
134
|
+
this._odr = 0;
|
|
135
|
+
this._bw = 0;
|
|
136
|
+
this._thresholdRaw = 0;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/** Output data rate codes (DR[1:0] in CTRL_REG1). */
|
|
140
|
+
static ODR_95_HZ = 0;
|
|
141
|
+
static ODR_190_HZ = 1;
|
|
142
|
+
static ODR_380_HZ = 2;
|
|
143
|
+
static ODR_760_HZ = 3;
|
|
144
|
+
|
|
145
|
+
/** Full-scale codes (0=±250, 1=±500, 2=±2000 dps). */
|
|
146
|
+
static FS_250_DPS = 0;
|
|
147
|
+
static FS_500_DPS = 1;
|
|
148
|
+
static FS_2000_DPS = 2;
|
|
149
|
+
|
|
150
|
+
/** FIFO modes (FM[2:0] in FIFO_CTRL_REG). */
|
|
151
|
+
static FIFO_BYPASS = 0;
|
|
152
|
+
static FIFO_FIFO = 1;
|
|
153
|
+
static FIFO_STREAM = 2;
|
|
154
|
+
static FIFO_BYPASS_TO_STREAM = 3;
|
|
155
|
+
static FIFO_STREAM_TO_FIFO = 7;
|
|
156
|
+
|
|
157
|
+
/** HPF modes (HPM[1:0] in CTRL_REG2). */
|
|
158
|
+
static HPM_NORMAL = 0;
|
|
159
|
+
static HPM_REFERENCE = 1;
|
|
160
|
+
static HPM_NORMAL_ALT = 2;
|
|
161
|
+
static HPM_AUTORESET = 3;
|
|
162
|
+
|
|
163
|
+
/** Power mode strings. */
|
|
164
|
+
static POWER_NORMAL = 'normal';
|
|
165
|
+
static POWER_SLEEP = 'sleep';
|
|
166
|
+
static POWER_POWERDOWN = 'power_down';
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Configure ODR, bandwidth, and full scale in one call.
|
|
170
|
+
* @param {number} odr ODR code 0-3 (95/190/380/760 Hz).
|
|
171
|
+
* @param {number} bw Bandwidth code 0-3 (ODR-dependent).
|
|
172
|
+
* @param {number} fullScale Full-scale code 0=±250, 1=±500, 2=±2000 dps.
|
|
173
|
+
*/
|
|
174
|
+
async configure(odr, bw, fullScale) {
|
|
175
|
+
if (odr < 0 || odr > 3) throw new Error('odr must be 0..3');
|
|
176
|
+
if (bw < 0 || bw > 3) throw new Error('bw must be 0..3');
|
|
177
|
+
if (fullScale < 0 || fullScale > 2) throw new Error('fullScale must be 0..2');
|
|
178
|
+
this._odr = odr;
|
|
179
|
+
this._bw = bw;
|
|
180
|
+
const fsMap = [250, 500, 2000];
|
|
181
|
+
this._fullScale = fsMap[fullScale];
|
|
182
|
+
const ctrl1 = _CTRL_REG1_DEFAULT | ((this._odr & 0x3) << 6) | ((this._bw & 0x3) << 4);
|
|
183
|
+
await this._writeReg(_REG_CTRL_REG1, ctrl1);
|
|
184
|
+
await this._writeReg(_REG_CTRL_REG4, _CTRL_REG4_DEFAULT | ((fullScale & 0x3) << 4));
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Read raw 16-bit signed angular rate values.
|
|
189
|
+
* @returns {Promise<[number, number, number]>} (x_raw, y_raw, z_raw).
|
|
190
|
+
*/
|
|
191
|
+
async gyroRaw() {
|
|
192
|
+
const buf = await this._readReg(_REG_OUT_X_L, 6);
|
|
193
|
+
return [_int16Le(buf, 0), _int16Le(buf, 2), _int16Le(buf, 4)];
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Read the relative temperature count.
|
|
198
|
+
* OUT_TEMP is an 8-bit signed value with 1 LSB/°C sensitivity. There is
|
|
199
|
+
* no absolute calibration — it represents change from the device's
|
|
200
|
+
* power-on temperature baseline. Do not convert to absolute Celsius.
|
|
201
|
+
* @returns {Promise<number>} Signed 8-bit temperature count.
|
|
202
|
+
*/
|
|
203
|
+
async temperature() {
|
|
204
|
+
let raw = (await this._readReg(_REG_OUT_TEMP, 1))[0];
|
|
205
|
+
if (raw & 0x80) raw -= 0x100;
|
|
206
|
+
return raw;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/**
|
|
210
|
+
* Check whether a new X/Y/Z sample is ready.
|
|
211
|
+
* @returns {Promise<boolean>} True if STATUS_REG.ZYXDA (bit 3) is set.
|
|
212
|
+
*/
|
|
213
|
+
async dataReady() {
|
|
214
|
+
const status = (await this._readReg(_REG_STATUS, 1))[0];
|
|
215
|
+
return Boolean(status & 0x08);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Configure the high-pass filter (CTRL_REG2).
|
|
220
|
+
* @param {number} mode HPF mode 0-3 (HPM[1:0]).
|
|
221
|
+
* @param {number} cutoff HPF cutoff code 0-15 (HPCF[3:0]).
|
|
222
|
+
*/
|
|
223
|
+
async configureHpFilter(mode, cutoff) {
|
|
224
|
+
if (mode < 0 || mode > 3) throw new Error('mode must be 0..3');
|
|
225
|
+
if (cutoff < 0 || cutoff > 15) throw new Error('cutoff must be 0..15');
|
|
226
|
+
await this._writeReg(_REG_CTRL_REG2, ((mode & 0x3) << 4) | (cutoff & 0x0F));
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Enable or disable the high-pass filter on the output path.
|
|
231
|
+
* @param {boolean} enable True to enable (sets HPen in CTRL_REG5), False to disable.
|
|
232
|
+
*/
|
|
233
|
+
async enableHpFilter(enable) {
|
|
234
|
+
const ctrl5 = (await this._readReg(_REG_CTRL_REG5, 1))[0];
|
|
235
|
+
await this._writeReg(_REG_CTRL_REG5, enable ? (ctrl5 | 0x10) : (ctrl5 & ~0x10));
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* Configure the FIFO (FIFO_CTRL_REG).
|
|
240
|
+
* @param {number} mode FIFO mode 0=Bypass, 1=FIFO, 2=Stream, 3=Bypass-to-Stream, 7=Stream-to-FIFO.
|
|
241
|
+
* @param {number} watermark Watermark threshold 0-31 (WTM[4:0]).
|
|
242
|
+
*/
|
|
243
|
+
async configureFifo(mode, watermark) {
|
|
244
|
+
const validModes = [0, 1, 2, 3, 7];
|
|
245
|
+
if (!validModes.includes(mode)) throw new Error('mode must be 0, 1, 2, 3, or 7');
|
|
246
|
+
if (watermark < 0 || watermark > 31) throw new Error('watermark must be 0..31');
|
|
247
|
+
const ctrl5 = (await this._readReg(_REG_CTRL_REG5, 1))[0] | 0x40;
|
|
248
|
+
await this._writeReg(_REG_CTRL_REG5, ctrl5);
|
|
249
|
+
await this._writeReg(_REG_FIFO_CTRL, ((mode & 0x7) << 5) | (watermark & 0x1F));
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
/**
|
|
253
|
+
* Enable or disable the FIFO (FIFO_EN bit in CTRL_REG5).
|
|
254
|
+
* @param {boolean} enable True to enable FIFO, False to disable and clear to bypass.
|
|
255
|
+
*/
|
|
256
|
+
async enableFifo(enable) {
|
|
257
|
+
const ctrl5 = (await this._readReg(_REG_CTRL_REG5, 1))[0];
|
|
258
|
+
if (enable) {
|
|
259
|
+
await this._writeReg(_REG_CTRL_REG5, ctrl5 | 0x40);
|
|
260
|
+
} else {
|
|
261
|
+
await this._writeReg(_REG_CTRL_REG5, ctrl5 & ~0x40);
|
|
262
|
+
await this._writeReg(_REG_FIFO_CTRL, 0x00);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Read number of unread samples in FIFO (FIFO_SRC_REG FSS[4:0]).
|
|
268
|
+
* @returns {Promise<number>} Number of stored samples (0-31).
|
|
269
|
+
*/
|
|
270
|
+
async fifoLevel() {
|
|
271
|
+
return (await this._readReg(_REG_FIFO_SRC, 1))[0] & 0x1F;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Read all available FIFO samples and return as rad/s tuples.
|
|
276
|
+
* @returns {Promise<Array<[number, number, number]>>} List of (x_rad_s, y_rad_s, z_rad_s).
|
|
277
|
+
*/
|
|
278
|
+
async readFifo() {
|
|
279
|
+
const n = await this.fifoLevel();
|
|
280
|
+
if (n === 0) return [];
|
|
281
|
+
const sens = this._sensitivity();
|
|
282
|
+
const buf = await this._readReg(_REG_OUT_X_L, n * 6);
|
|
283
|
+
const out = [];
|
|
284
|
+
for (let i = 0; i < n; i++) {
|
|
285
|
+
const o = i * 6;
|
|
286
|
+
const x = _int16Le(buf, o) * sens * _DPS_TO_RAD;
|
|
287
|
+
const y = _int16Le(buf, o + 2) * sens * _DPS_TO_RAD;
|
|
288
|
+
const z = _int16Le(buf, o + 4) * sens * _DPS_TO_RAD;
|
|
289
|
+
out.push([x, y, z]);
|
|
290
|
+
}
|
|
291
|
+
return out;
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Set the power mode (CTRL_REG1 PD and axis enable bits).
|
|
296
|
+
* @param {string} mode 'normal' (PD=1, all axes on), 'sleep' (PD=1, all axes off),
|
|
297
|
+
* or 'power_down' (PD=0).
|
|
298
|
+
*/
|
|
299
|
+
async setPowerMode(mode) {
|
|
300
|
+
let ctrl1 = (await this._readReg(_REG_CTRL_REG1, 1))[0];
|
|
301
|
+
if (mode === L3GD20HFull.POWER_NORMAL) {
|
|
302
|
+
ctrl1 = (ctrl1 & 0xF0) | 0x0F;
|
|
303
|
+
} else if (mode === L3GD20HFull.POWER_SLEEP) {
|
|
304
|
+
ctrl1 = (ctrl1 & 0xF8) | 0x08;
|
|
305
|
+
} else if (mode === L3GD20HFull.POWER_POWERDOWN) {
|
|
306
|
+
ctrl1 &= 0xF7;
|
|
307
|
+
} else {
|
|
308
|
+
throw new Error("mode must be 'normal', 'sleep', or 'power_down'");
|
|
309
|
+
}
|
|
310
|
+
await this._writeReg(_REG_CTRL_REG1, ctrl1);
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
module.exports = { L3GD20HMinimal, L3GD20HFull };
|