periph 1.0.0 → 1.1.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 +49 -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/mcp4728/complete.js +41 -0
- package/examples/adc_dac/mcp4728/demo.js +41 -0
- package/examples/adc_dac/mcp4728/minimal.js +11 -0
- package/examples/display/pcf8576/complete.js +32 -0
- package/examples/display/pcf8576/demo.js +48 -0
- package/examples/display/pcf8576/minimal.js +18 -0
- package/examples/environmental/aht21/complete.js +30 -0
- package/examples/environmental/aht21/demo.js +44 -0
- package/examples/environmental/aht21/minimal.js +12 -0
- package/examples/environmental/bme280/complete.js +34 -0
- package/examples/environmental/bme280/demo.js +67 -0
- package/examples/environmental/bme280/minimal.js +18 -0
- package/examples/environmental/bme680/complete.js +37 -0
- package/examples/environmental/bme680/demo.js +52 -0
- package/examples/environmental/bme680/minimal.js +19 -0
- package/examples/gas/ens160/complete.js +52 -0
- package/examples/gas/ens160/demo.js +52 -0
- package/examples/gas/ens160/minimal.js +27 -0
- package/examples/imu/mpu6050/complete.js +56 -0
- package/examples/imu/mpu6050/demo.js +34 -0
- package/examples/imu/mpu6050/minimal.js +14 -0
- package/examples/light/apds9960/complete.js +62 -0
- package/examples/light/apds9960/demo.js +32 -0
- package/examples/light/apds9960/minimal.js +12 -0
- package/examples/memory/24aa02uid/complete.js +40 -0
- package/examples/memory/24aa02uid/demo.js +40 -0
- package/examples/memory/24aa02uid/minimal.js +12 -0
- package/examples/pressure/bmp280/complete.js +22 -18
- package/examples/pressure/bmp280/demo.js +34 -18
- package/examples/pressure/bmp280/minimal.js +4 -4
- package/package.json +1 -1
- package/src/chips/adc_dac/hx711.js +165 -0
- package/src/chips/adc_dac/mcp4728.js +284 -0
- package/src/chips/display/pcf8576.js +223 -0
- package/src/chips/environmental/aht21.js +170 -0
- package/src/chips/environmental/bme280.js +366 -0
- package/src/chips/environmental/bme680.js +489 -0
- package/src/chips/gas/ens160.js +280 -0
- package/src/chips/imu/mpu6050.js +266 -0
- package/src/chips/light/apds9960.js +464 -0
- package/src/chips/memory/_24aa02uid.js +179 -0
- package/src/chips/pressure/bmp280.js +154 -191
- package/src/transport/hx711.js +105 -0
- package/examples/display/.gitkeep +0 -0
- package/examples/environmental/.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/imu/.gitkeep +0 -0
- package/src/chips/light/.gitkeep +0 -0
- package/src/chips/pressure/.gitkeep +0 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _REG_PART_ID = 0x00;
|
|
4
|
+
const _REG_OPMODE = 0x10;
|
|
5
|
+
const _REG_CONFIG = 0x11;
|
|
6
|
+
const _REG_COMMAND = 0x12;
|
|
7
|
+
const _REG_TEMP_IN = 0x13;
|
|
8
|
+
const _REG_RH_IN = 0x15;
|
|
9
|
+
const _REG_DEVICE_STATUS = 0x20;
|
|
10
|
+
const _REG_DATA_AQI = 0x21;
|
|
11
|
+
const _REG_DATA_TVOC = 0x22;
|
|
12
|
+
const _REG_DATA_ECO2 = 0x24;
|
|
13
|
+
const _REG_DATA_T = 0x30;
|
|
14
|
+
const _REG_DATA_RH = 0x32;
|
|
15
|
+
const _REG_DATA_MISR = 0x38;
|
|
16
|
+
const _REG_GPR_WRITE = 0x40;
|
|
17
|
+
const _REG_GPR_READ = 0x48;
|
|
18
|
+
|
|
19
|
+
const _OPMODE_DEEP_SLEEP = 0x00;
|
|
20
|
+
const _OPMODE_IDLE = 0x01;
|
|
21
|
+
const _OPMODE_STANDARD = 0x02;
|
|
22
|
+
const _OPMODE_RESET = 0xF0;
|
|
23
|
+
|
|
24
|
+
const _PART_ID_EXPECTED = 0x0160;
|
|
25
|
+
|
|
26
|
+
function _delay(ms) {
|
|
27
|
+
const start = Date.now();
|
|
28
|
+
while (Date.now() - start < ms) { /* spin */ }
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* ENS160 digital multi-gas sensor — minimal interface.
|
|
33
|
+
*
|
|
34
|
+
* Provides calibrated air quality readings (AQI, TVOC, eCO2) with no
|
|
35
|
+
* configuration required beyond the transport. The sensor performs automatic
|
|
36
|
+
* baseline correction and on-chip signal processing.
|
|
37
|
+
*
|
|
38
|
+
* Default: STANDARD mode (gas sensing active), polling only, no external
|
|
39
|
+
* T/RH compensation.
|
|
40
|
+
*
|
|
41
|
+
* @param {object} transport - Configured I²C or SPI transport.
|
|
42
|
+
*/
|
|
43
|
+
class ENS160Minimal {
|
|
44
|
+
constructor(transport) {
|
|
45
|
+
this._transport = transport;
|
|
46
|
+
this._writeReg(_REG_OPMODE, _OPMODE_IDLE);
|
|
47
|
+
_delay(1);
|
|
48
|
+
const partId = this._readRegLE16(_REG_PART_ID);
|
|
49
|
+
if (partId !== _PART_ID_EXPECTED) {
|
|
50
|
+
throw new Error('ENS160 not found: expected PART_ID 0x0160, got 0x' + partId.toString(16).padStart(4, '0'));
|
|
51
|
+
}
|
|
52
|
+
this._writeReg(_REG_OPMODE, _OPMODE_STANDARD);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
_writeReg(reg, value) {
|
|
56
|
+
this._transport.write(Buffer.from([reg, value]));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
_writeRegLE16(reg, value) {
|
|
60
|
+
this._transport.write(Buffer.from([reg, value & 0xFF, (value >> 8) & 0xFF]));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
_readReg(reg, n) {
|
|
64
|
+
return this._transport.writeRead(Buffer.from([reg]), n);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
_readRegLE16(reg) {
|
|
68
|
+
const data = this._readReg(reg, 2);
|
|
69
|
+
return data[0] | (data[1] << 8);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
_readDeviceStatus() {
|
|
73
|
+
const data = this._readReg(_REG_DEVICE_STATUS, 1);
|
|
74
|
+
return data[0];
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
_waitForNewData(timeoutMs = 5000) {
|
|
78
|
+
const start = Date.now();
|
|
79
|
+
while (true) {
|
|
80
|
+
const status = this._readDeviceStatus();
|
|
81
|
+
if (status & 0x02) {
|
|
82
|
+
return status;
|
|
83
|
+
}
|
|
84
|
+
if (Date.now() - start > timeoutMs) {
|
|
85
|
+
throw new Error('ENS160: NEWDAT not set within ' + timeoutMs + ' ms');
|
|
86
|
+
}
|
|
87
|
+
_delay(10);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Read the VALIDITY_FLAG from DEVICE_STATUS.
|
|
93
|
+
* @returns {number} Validity flag (0=OK, 1=Warm-up, 2=Initial Start-up, 3=No valid output).
|
|
94
|
+
*/
|
|
95
|
+
status() {
|
|
96
|
+
const status = this._readDeviceStatus();
|
|
97
|
+
return (status >> 2) & 0x03;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Read calibrated air quality values.
|
|
102
|
+
*
|
|
103
|
+
* Polls until NEWDAT is set, then checks VALIDITY_FLAG. Only returns
|
|
104
|
+
* data when validity is 0 (OK). Reads AQI, TVOC, and eCO2 in a single
|
|
105
|
+
* burst to ensure consistency.
|
|
106
|
+
*
|
|
107
|
+
* @returns {object} Keys: aqi (number, 1–5), tvocPpb (number), eco2Ppm (number).
|
|
108
|
+
* @throws {Error} If VALIDITY_FLAG is not 0 after NEWDAT is set.
|
|
109
|
+
*/
|
|
110
|
+
readAirQuality() {
|
|
111
|
+
const status = this._waitForNewData();
|
|
112
|
+
const validity = (status >> 2) & 0x03;
|
|
113
|
+
if (validity !== 0) {
|
|
114
|
+
throw new Error('ENS160: data not valid (VALIDITY_FLAG=' + validity + ')');
|
|
115
|
+
}
|
|
116
|
+
const data = this._readReg(_REG_DATA_AQI, 5);
|
|
117
|
+
const aqi = data[0] & 0x07;
|
|
118
|
+
const tvocPpb = data[1] | (data[2] << 8);
|
|
119
|
+
const eco2Ppm = data[3] | (data[4] << 8);
|
|
120
|
+
return { aqi, tvocPpb, eco2Ppm };
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* ENS160 full interface — extends ENS160Minimal with compensation, raw readings, and power control.
|
|
126
|
+
*
|
|
127
|
+
* Adds external temperature/humidity compensation, individual gas readings,
|
|
128
|
+
* raw sensor resistance, firmware version query, interrupt configuration,
|
|
129
|
+
* and sleep/wake control.
|
|
130
|
+
*
|
|
131
|
+
* @param {object} transport - Configured I²C or SPI transport.
|
|
132
|
+
*/
|
|
133
|
+
class ENS160Full extends ENS160Minimal {
|
|
134
|
+
static VALIDITY_OK = 0;
|
|
135
|
+
static VALIDITY_WARMUP = 1;
|
|
136
|
+
static VALIDITY_INITIAL_STARTUP = 2;
|
|
137
|
+
static VALIDITY_INVALID = 3;
|
|
138
|
+
|
|
139
|
+
constructor(transport) {
|
|
140
|
+
super(transport);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Write external temperature and humidity for compensation.
|
|
145
|
+
* @param {number} tempCelsius - Ambient temperature in degrees Celsius.
|
|
146
|
+
* @param {number} rhPercent - Ambient relative humidity in percent (0–100).
|
|
147
|
+
*/
|
|
148
|
+
setCompensation(tempCelsius, rhPercent) {
|
|
149
|
+
const tempRaw = Math.round((tempCelsius + 273.15) * 64);
|
|
150
|
+
const rhRaw = Math.round(rhPercent * 512);
|
|
151
|
+
this._writeRegLE16(_REG_TEMP_IN, tempRaw);
|
|
152
|
+
this._writeRegLE16(_REG_RH_IN, rhRaw);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Read TVOC concentration.
|
|
157
|
+
* @returns {number} TVOC in ppb.
|
|
158
|
+
*/
|
|
159
|
+
readTvoc() {
|
|
160
|
+
this._waitForNewData();
|
|
161
|
+
return this._readRegLE16(_REG_DATA_TVOC);
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* Read equivalent CO2 concentration.
|
|
166
|
+
* @returns {number} eCO2 in ppm.
|
|
167
|
+
*/
|
|
168
|
+
readEco2() {
|
|
169
|
+
this._waitForNewData();
|
|
170
|
+
return this._readRegLE16(_REG_DATA_ECO2);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Read Air Quality Index (UBA scale).
|
|
175
|
+
* @returns {number} AQI value 1–5 (1=Excellent, 5=Unhealthy).
|
|
176
|
+
*/
|
|
177
|
+
readAqi() {
|
|
178
|
+
this._waitForNewData();
|
|
179
|
+
const data = this._readReg(_REG_DATA_AQI, 1);
|
|
180
|
+
return data[0] & 0x07;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Read ethanol concentration estimate.
|
|
185
|
+
* @returns {number} Ethanol estimate in ppb (alias of DATA_TVOC at 0x22).
|
|
186
|
+
*/
|
|
187
|
+
readEthanol() {
|
|
188
|
+
this._waitForNewData();
|
|
189
|
+
return this._readRegLE16(_REG_DATA_TVOC);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Read raw sensor resistance from GPR_READ registers.
|
|
194
|
+
* @param {number} sensor - Sensor number (1 or 4).
|
|
195
|
+
* @returns {number} Resistance in Ohms.
|
|
196
|
+
* @throws {Error} If sensor is not 1 or 4.
|
|
197
|
+
*/
|
|
198
|
+
readRawResistance(sensor) {
|
|
199
|
+
let offset;
|
|
200
|
+
if (sensor === 1) {
|
|
201
|
+
offset = 0;
|
|
202
|
+
} else if (sensor === 4) {
|
|
203
|
+
offset = 6;
|
|
204
|
+
} else {
|
|
205
|
+
throw new Error('sensor must be 1 or 4, got ' + sensor);
|
|
206
|
+
}
|
|
207
|
+
const raw = this._readRegLE16(_REG_GPR_READ + offset);
|
|
208
|
+
return Math.pow(2.0, raw / 2048.0);
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* Read the temperature and humidity values used by the sensor.
|
|
213
|
+
* @returns {object} Keys: tempCelsius (number), rhPercent (number).
|
|
214
|
+
*/
|
|
215
|
+
readCompensationActuals() {
|
|
216
|
+
const data = this._readReg(_REG_DATA_T, 4);
|
|
217
|
+
const tempRaw = data[0] | (data[1] << 8);
|
|
218
|
+
const rhRaw = data[2] | (data[3] << 8);
|
|
219
|
+
const tempCelsius = (tempRaw / 64.0) - 273.15;
|
|
220
|
+
const rhPercent = rhRaw / 512.0;
|
|
221
|
+
return { tempCelsius, rhPercent };
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Query firmware version (requires IDLE mode).
|
|
226
|
+
*
|
|
227
|
+
* Switches to IDLE, issues GET_APPVER command, reads GPR_READ, then
|
|
228
|
+
* returns to STANDARD mode.
|
|
229
|
+
*
|
|
230
|
+
* @returns {object} Keys: major (number), minor (number), release (number).
|
|
231
|
+
*/
|
|
232
|
+
getFirmwareVersion() {
|
|
233
|
+
this._writeReg(_REG_OPMODE, _OPMODE_IDLE);
|
|
234
|
+
_delay(1);
|
|
235
|
+
this._writeReg(_REG_COMMAND, 0x0E);
|
|
236
|
+
_delay(1);
|
|
237
|
+
const data = this._readReg(_REG_GPR_READ + 4, 3);
|
|
238
|
+
const major = data[0];
|
|
239
|
+
const minor = data[1];
|
|
240
|
+
const release = data[2];
|
|
241
|
+
this._writeReg(_REG_OPMODE, _OPMODE_STANDARD);
|
|
242
|
+
return { major, minor, release };
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Configure the INTn interrupt pin.
|
|
247
|
+
* @param {boolean} enabled - Enable interrupt pin.
|
|
248
|
+
* @param {boolean} activeHigh - True for active-high polarity, false for active-low.
|
|
249
|
+
* @param {boolean} pushPull - True for push-pull drive, false for open-drain.
|
|
250
|
+
* @param {boolean} onData - Assert on new DATA_xxx data.
|
|
251
|
+
* @param {boolean} onGpr - Assert on new GPR_READ data.
|
|
252
|
+
*/
|
|
253
|
+
configureInterrupt(enabled, activeHigh, pushPull, onData, onGpr) {
|
|
254
|
+
let config = 0;
|
|
255
|
+
if (enabled) config |= 0x01;
|
|
256
|
+
if (onData) config |= 0x02;
|
|
257
|
+
if (onGpr) config |= 0x08;
|
|
258
|
+
if (pushPull) config |= 0x20;
|
|
259
|
+
if (activeHigh) config |= 0x40;
|
|
260
|
+
this._writeReg(_REG_CONFIG, config);
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/**
|
|
264
|
+
* Enter DEEP SLEEP mode for power saving.
|
|
265
|
+
*/
|
|
266
|
+
sleep() {
|
|
267
|
+
this._writeReg(_REG_OPMODE, _OPMODE_DEEP_SLEEP);
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Wake from DEEP SLEEP and resume STANDARD gas sensing.
|
|
272
|
+
*/
|
|
273
|
+
wake() {
|
|
274
|
+
this._writeReg(_REG_OPMODE, _OPMODE_IDLE);
|
|
275
|
+
_delay(1);
|
|
276
|
+
this._writeReg(_REG_OPMODE, _OPMODE_STANDARD);
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
module.exports = { ENS160Minimal, ENS160Full };
|
|
@@ -0,0 +1,266 @@
|
|
|
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 transport. 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
|
+
class MPU6050Minimal {
|
|
42
|
+
/**
|
|
43
|
+
* @param {object} transport - Configured I²C transport (writeRead, write).
|
|
44
|
+
*/
|
|
45
|
+
constructor(transport) {
|
|
46
|
+
this._transport = transport;
|
|
47
|
+
this._accelFs = 0;
|
|
48
|
+
this._gyroFs = 0;
|
|
49
|
+
this._writeReg(_REG_PWR_MGMT_1, 0x80);
|
|
50
|
+
const end1 = Date.now() + 100;
|
|
51
|
+
while (Date.now() < end1) {}
|
|
52
|
+
this._writeReg(_REG_PWR_MGMT_1, 0x01);
|
|
53
|
+
const who = this._readReg(_REG_WHO_AM_I);
|
|
54
|
+
if (who !== _WHO_AM_I_VALUE) {
|
|
55
|
+
throw new Error('MPU6050 WHO_AM_I: expected 0x' + _WHO_AM_I_VALUE.toString(16) + ', got 0x' + who.toString(16));
|
|
56
|
+
}
|
|
57
|
+
this._writeReg(_REG_GYRO_CONFIG, 0x00);
|
|
58
|
+
this._writeReg(_REG_ACCEL_CONFIG, 0x00);
|
|
59
|
+
this._writeReg(_REG_CONFIG, 0x03);
|
|
60
|
+
this._writeReg(_REG_SMPLRT_DIV, 0x04);
|
|
61
|
+
const end2 = Date.now() + 35;
|
|
62
|
+
while (Date.now() < end2) {}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
_writeReg(reg, value) {
|
|
66
|
+
this._transport.write(Buffer.from([reg, value]));
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
_readReg(reg) {
|
|
70
|
+
return this._transport.writeRead(Buffer.from([reg]), 1)[0];
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
_readReg16Signed(reg) {
|
|
74
|
+
return this._transport.writeRead(Buffer.from([reg]), 2).readInt16BE(0);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
_readBurst(reg, len) {
|
|
78
|
+
return this._transport.writeRead(Buffer.from([reg]), len);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* Read 3-axis linear acceleration.
|
|
83
|
+
* @returns {number[]} [x, y, z] acceleration in m/s².
|
|
84
|
+
*/
|
|
85
|
+
accel() {
|
|
86
|
+
const buf = this._readBurst(_REG_ACCEL_XOUT_H, 6);
|
|
87
|
+
const ax = buf.readInt16BE(0);
|
|
88
|
+
const ay = buf.readInt16BE(2);
|
|
89
|
+
const az = buf.readInt16BE(4);
|
|
90
|
+
const sens = _ACCEL_SENSITIVITY[this._accelFs];
|
|
91
|
+
return [ax / sens * 9.80665, ay / sens * 9.80665, az / sens * 9.80665];
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Read 3-axis angular rate.
|
|
96
|
+
* @returns {number[]} [x, y, z] angular rate in rad/s.
|
|
97
|
+
*/
|
|
98
|
+
gyro() {
|
|
99
|
+
const buf = this._readBurst(_REG_GYRO_XOUT_H, 6);
|
|
100
|
+
const gx = buf.readInt16BE(0);
|
|
101
|
+
const gy = buf.readInt16BE(2);
|
|
102
|
+
const gz = buf.readInt16BE(4);
|
|
103
|
+
const sens = _GYRO_SENSITIVITY[this._gyroFs];
|
|
104
|
+
return [gx / sens * Math.PI / 180.0,
|
|
105
|
+
gy / sens * Math.PI / 180.0,
|
|
106
|
+
gz / sens * Math.PI / 180.0];
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* MPU-6050 full interface — extends MPU6050Minimal with configuration and FIFO support.
|
|
112
|
+
*
|
|
113
|
+
* Adds gyroscope and accelerometer full-scale configuration, DLPF settings,
|
|
114
|
+
* sample rate control, temperature reading, raw data access, data-ready polling,
|
|
115
|
+
* sleep/standby control, and FIFO management.
|
|
116
|
+
*/
|
|
117
|
+
class MPU6050Full extends MPU6050Minimal {
|
|
118
|
+
/**
|
|
119
|
+
* @param {object} transport - Configured I²C transport.
|
|
120
|
+
*/
|
|
121
|
+
constructor(transport) {
|
|
122
|
+
super(transport);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Set gyroscope full-scale range.
|
|
127
|
+
* @param {number} [fullScale=0] - Range selector 0–3 (0=±250, 1=±500, 2=±1000, 3=±2000 dps).
|
|
128
|
+
*/
|
|
129
|
+
configureGyro(fullScale = 0) {
|
|
130
|
+
this._gyroFs = fullScale & 0x03;
|
|
131
|
+
this._writeReg(_REG_GYRO_CONFIG, (fullScale & 0x03) << 3);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* Set accelerometer full-scale range.
|
|
136
|
+
* @param {number} [fullScale=0] - Range selector 0–3 (0=±2g, 1=±4g, 2=±8g, 3=±16g).
|
|
137
|
+
*/
|
|
138
|
+
configureAccel(fullScale = 0) {
|
|
139
|
+
this._accelFs = fullScale & 0x03;
|
|
140
|
+
this._writeReg(_REG_ACCEL_CONFIG, (fullScale & 0x03) << 3);
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Set digital low-pass filter bandwidth.
|
|
145
|
+
* @param {number} [dlpf=3] - Filter setting 0–6 (0=260/256 Hz, 1=184/188 Hz, 2=94/98 Hz,
|
|
146
|
+
* 3=44/42 Hz, 4=21/20 Hz, 5=10/10 Hz, 6=5/5 Hz; gyro/accel BW).
|
|
147
|
+
*/
|
|
148
|
+
configureDlpf(dlpf = 3) {
|
|
149
|
+
this._writeReg(_REG_CONFIG, dlpf & 0x07);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Set sample rate divider.
|
|
154
|
+
* @param {number} [divider=4] - SMPLRT_DIV value 0–255; output rate = 1 kHz / (1 + divider)
|
|
155
|
+
* when DLPF is active.
|
|
156
|
+
*/
|
|
157
|
+
configureSampleRate(divider = 4) {
|
|
158
|
+
this._writeReg(_REG_SMPLRT_DIV, divider & 0xFF);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/**
|
|
162
|
+
* Read die temperature.
|
|
163
|
+
* @returns {number} Temperature in °C.
|
|
164
|
+
*/
|
|
165
|
+
temperature() {
|
|
166
|
+
const raw = this._readReg16Signed(_REG_TEMP_OUT_H);
|
|
167
|
+
return raw / 340.0 + 36.53;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Read raw 3-axis accelerometer values.
|
|
172
|
+
* @returns {number[]} [x, y, z] raw 16-bit signed values.
|
|
173
|
+
*/
|
|
174
|
+
accelRaw() {
|
|
175
|
+
const buf = this._readBurst(_REG_ACCEL_XOUT_H, 6);
|
|
176
|
+
return [buf.readInt16BE(0), buf.readInt16BE(2), buf.readInt16BE(4)];
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Read raw 3-axis gyroscope values.
|
|
181
|
+
* @returns {number[]} [x, y, z] raw 16-bit signed values.
|
|
182
|
+
*/
|
|
183
|
+
gyroRaw() {
|
|
184
|
+
const buf = this._readBurst(_REG_GYRO_XOUT_H, 6);
|
|
185
|
+
return [buf.readInt16BE(0), buf.readInt16BE(2), buf.readInt16BE(4)];
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
/**
|
|
189
|
+
* Check if new sensor data is available.
|
|
190
|
+
* @returns {boolean} True when DATA_RDY_INT is set in INT_STATUS.
|
|
191
|
+
*/
|
|
192
|
+
dataReady() {
|
|
193
|
+
return !!(this._readReg(_REG_INT_STATUS) & 0x01);
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Set or clear the SLEEP bit in PWR_MGMT_1.
|
|
198
|
+
* @param {boolean} [sleep=true] - True to enter sleep mode, false to wake.
|
|
199
|
+
*/
|
|
200
|
+
setSleep(sleep = true) {
|
|
201
|
+
let val = this._readReg(_REG_PWR_MGMT_1);
|
|
202
|
+
if (sleep) {
|
|
203
|
+
val |= 0x40;
|
|
204
|
+
} else {
|
|
205
|
+
val &= ~0x40;
|
|
206
|
+
}
|
|
207
|
+
this._writeReg(_REG_PWR_MGMT_1, val);
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Put individual axes into standby mode.
|
|
212
|
+
* @param {boolean} [xa=false] - X accelerometer standby.
|
|
213
|
+
* @param {boolean} [ya=false] - Y accelerometer standby.
|
|
214
|
+
* @param {boolean} [za=false] - Z accelerometer standby.
|
|
215
|
+
* @param {boolean} [xg=false] - X gyroscope standby.
|
|
216
|
+
* @param {boolean} [yg=false] - Y gyroscope standby.
|
|
217
|
+
* @param {boolean} [zg=false] - Z gyroscope standby.
|
|
218
|
+
*/
|
|
219
|
+
setStandby(xa = false, ya = false, za = false, xg = false, yg = false, zg = false) {
|
|
220
|
+
const val = ((xa ? 1 : 0) << 5) | ((ya ? 1 : 0) << 4) | ((za ? 1 : 0) << 3) |
|
|
221
|
+
((xg ? 1 : 0) << 2) | ((yg ? 1 : 0) << 1) | (zg ? 1 : 0);
|
|
222
|
+
this._writeReg(_REG_PWR_MGMT_2, val);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Read the number of bytes in the FIFO buffer.
|
|
227
|
+
* @returns {number} FIFO byte count (0–1024).
|
|
228
|
+
*/
|
|
229
|
+
fifoCount() {
|
|
230
|
+
const buf = this._readBurst(_REG_FIFO_COUNTH, 2);
|
|
231
|
+
return ((buf[0] & 0x1F) << 8) | buf[1];
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Read all available data from the FIFO buffer.
|
|
236
|
+
* @returns {Buffer} FIFO data.
|
|
237
|
+
*/
|
|
238
|
+
readFifo() {
|
|
239
|
+
const count = this.fifoCount();
|
|
240
|
+
if (count === 0) return Buffer.alloc(0);
|
|
241
|
+
return this._readBurst(_REG_FIFO_R_W, count);
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Configure and enable FIFO sources.
|
|
246
|
+
* @param {boolean} [gyro=true] - Enable gyroscope data in FIFO.
|
|
247
|
+
* @param {boolean} [accel=true] - Enable accelerometer data in FIFO.
|
|
248
|
+
* @param {boolean} [temp=false] - Enable temperature data in FIFO.
|
|
249
|
+
*/
|
|
250
|
+
enableFifo(gyro = true, accel = true, temp = false) {
|
|
251
|
+
const fifoEn = ((accel ? 1 : 0) << 3) | ((temp ? 1 : 0) << 2) | ((gyro ? 1 : 0) << 4);
|
|
252
|
+
this._writeReg(_REG_FIFO_EN, fifoEn);
|
|
253
|
+
const userCtrl = this._readReg(_REG_USER_CTRL);
|
|
254
|
+
this._writeReg(_REG_USER_CTRL, userCtrl | 0x40);
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* Reset the FIFO buffer by setting FIFO_RST in USER_CTRL.
|
|
259
|
+
*/
|
|
260
|
+
resetFifo() {
|
|
261
|
+
const userCtrl = this._readReg(_REG_USER_CTRL);
|
|
262
|
+
this._writeReg(_REG_USER_CTRL, userCtrl | 0x04);
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
module.exports = { MPU6050Minimal, MPU6050Full };
|