periph 1.3.1 → 1.3.3
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 +18 -0
- package/examples/accelerometer/bma180/complete.js +83 -0
- package/examples/accelerometer/bma180/demo.js +47 -0
- package/examples/accelerometer/bma180/minimal.js +21 -0
- package/examples/accelerometer/fxos8700/complete.js +111 -0
- package/examples/accelerometer/fxos8700/demo.js +80 -0
- package/examples/accelerometer/fxos8700/minimal.js +20 -0
- package/examples/accelerometer/lis3dh/complete.js +123 -0
- package/examples/accelerometer/lis3dh/demo.js +99 -0
- package/examples/accelerometer/lis3dh/minimal.js +19 -0
- package/examples/accelerometer/lsm303/complete.js +82 -0
- package/examples/accelerometer/lsm303/demo.js +96 -0
- package/examples/accelerometer/lsm303/minimal.js +23 -0
- package/examples/accelerometer/mma8451q/complete.js +96 -0
- package/examples/accelerometer/mma8451q/demo.js +120 -0
- package/examples/accelerometer/mma8451q/minimal.js +20 -0
- package/examples/adc_dac/ad5243/complete.js +52 -0
- package/examples/adc_dac/ad5243/demo.js +50 -0
- package/examples/adc_dac/ad5243/minimal.js +23 -0
- package/examples/adc_dac/ad5248/complete.js +49 -0
- package/examples/adc_dac/ad5248/demo.js +42 -0
- package/examples/adc_dac/ad5248/minimal.js +21 -0
- package/examples/adc_dac/ads1013/complete.js +51 -0
- package/examples/adc_dac/ads1013/demo.js +51 -0
- package/examples/adc_dac/ads1013/minimal.js +21 -0
- package/examples/adc_dac/ads1014/complete.js +88 -0
- package/examples/adc_dac/ads1014/demo.js +52 -0
- package/examples/adc_dac/ads1014/minimal.js +21 -0
- package/examples/adc_dac/ads1113/complete.js +47 -0
- package/examples/adc_dac/ads1113/demo.js +39 -0
- package/examples/adc_dac/ads1113/minimal.js +20 -0
- package/examples/adc_dac/ads1114/complete.js +77 -0
- package/examples/adc_dac/ads1114/demo.js +47 -0
- package/examples/adc_dac/ads1114/minimal.js +20 -0
- package/examples/adc_dac/ads1115/complete.js +82 -0
- package/examples/adc_dac/ads1115/demo.js +55 -0
- package/examples/adc_dac/ads1115/minimal.js +20 -0
- package/examples/display/pcf8578/complete.js +58 -0
- package/examples/display/pcf8578/demo.js +62 -0
- package/examples/display/pcf8578/minimal.js +19 -0
- package/examples/display/pcf8579/complete.js +58 -0
- package/examples/display/pcf8579/demo.js +61 -0
- package/examples/display/pcf8579/minimal.js +19 -0
- package/examples/imu/icm20948/complete.js +114 -0
- package/examples/imu/icm20948/demo.js +79 -0
- package/examples/imu/icm20948/minimal.js +22 -0
- package/examples/io_expander/mcp23008/complete.js +70 -0
- package/examples/io_expander/mcp23008/demo.js +58 -0
- package/examples/io_expander/mcp23008/minimal.js +20 -0
- package/examples/memory/mb85rc/complete.js +40 -0
- package/examples/memory/mb85rc/demo.js +58 -0
- package/examples/memory/mb85rc/minimal.js +15 -0
- package/examples/motor/drv2605/complete.js +100 -0
- package/examples/motor/drv2605/demo.js +55 -0
- package/examples/motor/drv2605/minimal.js +24 -0
- package/package.json +1 -1
- package/src/chips/accelerometer/bma180.js +573 -0
- package/src/chips/accelerometer/fxos8700.js +1600 -0
- package/src/chips/accelerometer/lis3dh.js +1341 -0
- package/src/chips/accelerometer/lsm303.js +1471 -0
- package/src/chips/accelerometer/mma8451q.js +1406 -0
- package/src/chips/adc_dac/_ads111x_base.js +272 -0
- package/src/chips/adc_dac/ad5243.js +231 -0
- package/src/chips/adc_dac/ad5248.js +194 -0
- package/src/chips/adc_dac/ads1013.js +219 -0
- package/src/chips/adc_dac/ads1014.js +509 -0
- package/src/chips/adc_dac/ads1113.js +112 -0
- package/src/chips/adc_dac/ads1114.js +201 -0
- package/src/chips/adc_dac/ads1115.js +232 -0
- package/src/chips/display/pcf8578.js +452 -0
- package/src/chips/display/pcf8579.js +451 -0
- package/src/chips/imu/icm20948.js +849 -0
- package/src/chips/io_expander/mcp23008.js +558 -0
- package/src/chips/memory/mb85rc.js +166 -0
- package/src/chips/motor/drv2605.js +592 -0
- package/src/discovery/registry.js +20 -19
|
@@ -0,0 +1,1341 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const { toSigned } = require('../../connection/register');
|
|
4
|
+
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// Register map
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
const _REG_STATUS_AUX = 0x07;
|
|
9
|
+
const _REG_OUT_ADC1_L = 0x08;
|
|
10
|
+
const _REG_OUT_ADC1_H = 0x09;
|
|
11
|
+
const _REG_OUT_ADC2_L = 0x0A;
|
|
12
|
+
const _REG_OUT_ADC2_H = 0x0B;
|
|
13
|
+
const _REG_OUT_ADC3_L = 0x0C;
|
|
14
|
+
const _REG_OUT_ADC3_H = 0x0D;
|
|
15
|
+
const _REG_WHO_AM_I = 0x0F;
|
|
16
|
+
const _REG_CTRL_REG0 = 0x1E;
|
|
17
|
+
const _REG_TEMP_CFG = 0x1F;
|
|
18
|
+
const _REG_CTRL_REG1 = 0x20;
|
|
19
|
+
const _REG_CTRL_REG2 = 0x21;
|
|
20
|
+
const _REG_CTRL_REG3 = 0x22;
|
|
21
|
+
const _REG_CTRL_REG4 = 0x23;
|
|
22
|
+
const _REG_CTRL_REG5 = 0x24;
|
|
23
|
+
const _REG_CTRL_REG6 = 0x25;
|
|
24
|
+
const _REG_REFERENCE = 0x26;
|
|
25
|
+
const _REG_STATUS = 0x27;
|
|
26
|
+
const _REG_OUT_X_L = 0x28;
|
|
27
|
+
const _REG_OUT_X_H = 0x29;
|
|
28
|
+
const _REG_OUT_Y_L = 0x2A;
|
|
29
|
+
const _REG_OUT_Y_H = 0x2B;
|
|
30
|
+
const _REG_OUT_Z_L = 0x2C;
|
|
31
|
+
const _REG_OUT_Z_H = 0x2D;
|
|
32
|
+
const _REG_FIFO_CTRL = 0x2E;
|
|
33
|
+
const _REG_FIFO_SRC = 0x2F;
|
|
34
|
+
const _REG_INT1_CFG = 0x30;
|
|
35
|
+
const _REG_INT1_SRC = 0x31;
|
|
36
|
+
const _REG_INT1_THS = 0x32;
|
|
37
|
+
const _REG_INT1_DUR = 0x33;
|
|
38
|
+
const _REG_INT2_CFG = 0x34;
|
|
39
|
+
const _REG_INT2_SRC = 0x35;
|
|
40
|
+
const _REG_INT2_THS = 0x36;
|
|
41
|
+
const _REG_INT2_DUR = 0x37;
|
|
42
|
+
const _REG_CLICK_CFG = 0x38;
|
|
43
|
+
const _REG_CLICK_SRC = 0x39;
|
|
44
|
+
const _REG_CLICK_THS = 0x3A;
|
|
45
|
+
const _REG_TIME_LIMIT = 0x3B;
|
|
46
|
+
const _REG_TIME_LATENCY = 0x3C;
|
|
47
|
+
const _REG_TIME_WINDOW = 0x3D;
|
|
48
|
+
const _REG_ACT_THS = 0x3E;
|
|
49
|
+
const _REG_ACT_DUR = 0x3F;
|
|
50
|
+
|
|
51
|
+
// WHO_AM_I (0x0F) reset value.
|
|
52
|
+
const _WHO_AM_I_VALUE = 0x33;
|
|
53
|
+
|
|
54
|
+
// Control-register defaults of the datasheet Initialization Sequence.
|
|
55
|
+
const _CTRL_REG4_DEFAULT = 0x88; // BDU=1, FS=00 (±2 g), HR=1
|
|
56
|
+
const _CTRL_REG1_DEFAULT = 0x57; // ODR=0101 (100 Hz), LPen=0, X/Y/Z enabled
|
|
57
|
+
|
|
58
|
+
// Defaults baked into the Minimal driver: ±2 g, 100 Hz, high-resolution.
|
|
59
|
+
const _DEFAULT_RANGE_G = 2;
|
|
60
|
+
const _DEFAULT_RATE_HZ = 100;
|
|
61
|
+
const _DEFAULT_MODE = 2;
|
|
62
|
+
|
|
63
|
+
// Full-scale ranges in g.
|
|
64
|
+
const _RANGES = [2, 4, 8, 16];
|
|
65
|
+
|
|
66
|
+
// Operating modes: 0 = low-power (8-bit), 1 = normal (10-bit),
|
|
67
|
+
// 2 = high-resolution (12-bit).
|
|
68
|
+
const _MODE_LP = 0;
|
|
69
|
+
const _MODE_NORMAL = 1;
|
|
70
|
+
const _MODE_HR = 2;
|
|
71
|
+
|
|
72
|
+
// Output data rates in Hz and their CTRL_REG1 ODR<3:0> codes (bits 7:4).
|
|
73
|
+
const _RATE_CODES = new Map([
|
|
74
|
+
[0, 0x00], // power-down
|
|
75
|
+
[1, 0x10],
|
|
76
|
+
[10, 0x20],
|
|
77
|
+
[25, 0x30],
|
|
78
|
+
[50, 0x40],
|
|
79
|
+
[100, 0x50],
|
|
80
|
+
[200, 0x60],
|
|
81
|
+
[400, 0x70],
|
|
82
|
+
[1620, 0x80], // low-power mode only
|
|
83
|
+
[1344, 0x90], // normal / high-resolution mode only
|
|
84
|
+
[5376, 0x90], // low-power mode only (same code as 1344)
|
|
85
|
+
]);
|
|
86
|
+
|
|
87
|
+
// Rates only legal in low-power mode, and only legal outside it.
|
|
88
|
+
const _LP_ONLY_RATES = [1620, 5376];
|
|
89
|
+
const _NON_LP_ONLY_RATES = [1344];
|
|
90
|
+
|
|
91
|
+
// Sensitivity in mg/LSB by mode and full-scale range (datasheet Table 4).
|
|
92
|
+
const _SENSITIVITY_MG = {
|
|
93
|
+
[_MODE_HR]: {2: 1, 4: 2, 8: 4, 16: 12},
|
|
94
|
+
[_MODE_NORMAL]: {2: 4, 4: 8, 8: 16, 16: 48},
|
|
95
|
+
[_MODE_LP]: {2: 16, 4: 32, 8: 64, 16: 192},
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
// Right-shift applied to the left-justified 16-bit output pair, by mode.
|
|
99
|
+
const _SHIFT = {[_MODE_HR]: 4, [_MODE_NORMAL]: 6, [_MODE_LP]: 8};
|
|
100
|
+
|
|
101
|
+
// Right-shift for the auxiliary ADC output (10-bit in normal and
|
|
102
|
+
// high-resolution mode, 8-bit in low-power mode).
|
|
103
|
+
const _ADC_SHIFT = {[_MODE_HR]: 6, [_MODE_NORMAL]: 6, [_MODE_LP]: 8};
|
|
104
|
+
|
|
105
|
+
// Interrupt-threshold LSB in mg by full-scale range (datasheet scale,
|
|
106
|
+
// full-scale / 128; the ±16 g value is taken as printed: 186 mg).
|
|
107
|
+
const _THRESHOLD_LSB_MG = {2: 16, 4: 32, 8: 62, 16: 186};
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Round half away from zero — matches the Python/C/Java/Go/Rust drivers, so
|
|
111
|
+
* every language computes the same register codes.
|
|
112
|
+
* @param {number} x - Value to round.
|
|
113
|
+
* @returns {number} The rounded value.
|
|
114
|
+
*/
|
|
115
|
+
function _round(x) {
|
|
116
|
+
return x >= 0 ? Math.floor(x + 0.5) : -Math.floor(-x + 0.5);
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Round a sub-millisecond delay up to a whole millisecond.
|
|
121
|
+
* @param {number} x - Delay in milliseconds.
|
|
122
|
+
* @returns {number} Whole milliseconds, 0 for a non-positive input.
|
|
123
|
+
*/
|
|
124
|
+
function _ceilMs(x) {
|
|
125
|
+
return x > 0 ? Math.ceil(x) : 0;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
/**
|
|
129
|
+
* Sleep without blocking the Node.js event loop.
|
|
130
|
+
* @param {number} ms - Delay in milliseconds.
|
|
131
|
+
* @returns {Promise<void>}
|
|
132
|
+
*/
|
|
133
|
+
function _sleep(ms) {
|
|
134
|
+
if (!(ms > 0)) return Promise.resolve();
|
|
135
|
+
return new Promise(resolve => setTimeout(resolve, ms));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/**
|
|
139
|
+
* Format a byte as 0xNN.
|
|
140
|
+
* @param {number} value - Byte value.
|
|
141
|
+
* @returns {string} Uppercase hexadecimal representation.
|
|
142
|
+
*/
|
|
143
|
+
function _hex2(value) {
|
|
144
|
+
return '0x' + (value & 0xFF).toString(16).toUpperCase().padStart(2, '0');
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Format a 16-bit value as 0xNNNN.
|
|
149
|
+
* @param {number} value - Value.
|
|
150
|
+
* @returns {string} Uppercase hexadecimal representation.
|
|
151
|
+
*/
|
|
152
|
+
function _hex4(value) {
|
|
153
|
+
return '0x' + (value & 0xFFFF).toString(16).toUpperCase().padStart(4, '0');
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* LIS3DH 3-axis ultra-low-power MEMS accelerometer (STMicroelectronics) —
|
|
158
|
+
* minimal interface.
|
|
159
|
+
*
|
|
160
|
+
* Triaxial accelerometer with 16-bit left-justified output and four
|
|
161
|
+
* selectable full-scale ranges (±2, ±4, ±8, ±16 *g*). Three operating modes
|
|
162
|
+
* trade resolution for current: low-power (8-bit), normal (10-bit) and
|
|
163
|
+
* high-resolution (12-bit), across output data rates from 1 Hz to 5.376 kHz.
|
|
164
|
+
*
|
|
165
|
+
* Communicates over I²C at address 0x18 (SA0 = GND) or 0x19 (SA0 = VDD_IO).
|
|
166
|
+
* The chip also supports 3-/4-wire SPI but that is out of scope for this
|
|
167
|
+
* driver. After the slave address the master sends an 8-bit sub-address:
|
|
168
|
+
* the 7 LSbs are the register address and the MSb enables address
|
|
169
|
+
* auto-increment, so every multi-byte transfer ORs 0x80 into the register
|
|
170
|
+
* address (single-register accesses do not).
|
|
171
|
+
*
|
|
172
|
+
* The driver is platform-agnostic — it only calls the connection interface.
|
|
173
|
+
*
|
|
174
|
+
* Default configuration (baked in at construction):
|
|
175
|
+
* - Range ±2 *g* (1 mg/LSB)
|
|
176
|
+
* - 100 Hz output data rate
|
|
177
|
+
* - High-resolution (12-bit) mode, all three axes enabled
|
|
178
|
+
* - `BDU` = 1 (block data update)
|
|
179
|
+
* - High-pass filter bypassed, FIFO bypassed
|
|
180
|
+
* - All interrupt routing cleared
|
|
181
|
+
* - Aux ADC and temperature sensor off
|
|
182
|
+
* - `CTRL_REG0` untouched
|
|
183
|
+
*
|
|
184
|
+
* Constructor caveat: JS constructors cannot be async, so the datasheet
|
|
185
|
+
* Initialization Sequence — including the `WHO_AM_I` identity check — starts
|
|
186
|
+
* in the constructor and every public method awaits it first. A wrong or
|
|
187
|
+
* absent device therefore rejects the first call made on the driver; call
|
|
188
|
+
* `await accel.init()` right after construction to surface it at a
|
|
189
|
+
* predictable point.
|
|
190
|
+
*/
|
|
191
|
+
class LIS3DHMinimal {
|
|
192
|
+
/**
|
|
193
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - Configured I²C connection (0x18 with SA0 = GND, 0x19 with SA0 = VDD_IO).
|
|
194
|
+
*/
|
|
195
|
+
constructor(connection) {
|
|
196
|
+
this._conn = connection;
|
|
197
|
+
this._rangeG = _DEFAULT_RANGE_G;
|
|
198
|
+
this._mode = _DEFAULT_MODE;
|
|
199
|
+
this._rateHz = _DEFAULT_RATE_HZ;
|
|
200
|
+
this._ready = this._runInitSequence();
|
|
201
|
+
// The rejection is re-raised by every public method; mark it handled
|
|
202
|
+
// here so it never surfaces as an unhandled rejection on its own.
|
|
203
|
+
this._ready.catch(() => {});
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Wait for the Initialization Sequence started by the constructor.
|
|
208
|
+
* @returns {Promise<void>}
|
|
209
|
+
* @throws {Error} If `WHO_AM_I` is not 0x33 (wrong chip, wrong address, or wiring problem).
|
|
210
|
+
*/
|
|
211
|
+
async init() {
|
|
212
|
+
await this._ready;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Run the datasheet Initialization Sequence (steps 2-6).
|
|
217
|
+
* @returns {Promise<void>}
|
|
218
|
+
* @private
|
|
219
|
+
*/
|
|
220
|
+
async _runInitSequence() {
|
|
221
|
+
// Step 2: verify WHO_AM_I.
|
|
222
|
+
const chipId = await this._readReg(_REG_WHO_AM_I);
|
|
223
|
+
if (chipId !== _WHO_AM_I_VALUE) {
|
|
224
|
+
throw new Error('LIS3DH WHO_AM_I: expected ' + _hex2(_WHO_AM_I_VALUE) +
|
|
225
|
+
', got ' + _hex2(chipId));
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Step 3: no soft reset — put the control registers in a known
|
|
229
|
+
// state. CTRL_REG0 and the interrupt-generator registers are left
|
|
230
|
+
// untouched (reserved registers are never written).
|
|
231
|
+
await this._writeReg(_REG_CTRL_REG2, 0x00);
|
|
232
|
+
await this._writeReg(_REG_CTRL_REG3, 0x00);
|
|
233
|
+
await this._writeReg(_REG_CTRL_REG5, 0x00);
|
|
234
|
+
await this._writeReg(_REG_CTRL_REG6, 0x00);
|
|
235
|
+
await this._writeReg(_REG_FIFO_CTRL, 0x00);
|
|
236
|
+
await this._writeReg(_REG_TEMP_CFG, 0x00);
|
|
237
|
+
|
|
238
|
+
// Step 4: block data update, ±2 g, high-resolution 12-bit mode.
|
|
239
|
+
await this._writeReg(_REG_CTRL_REG4, _CTRL_REG4_DEFAULT);
|
|
240
|
+
|
|
241
|
+
// Step 5: leave power-down — this starts the conversions.
|
|
242
|
+
await this._writeReg(_REG_CTRL_REG1, _CTRL_REG1_DEFAULT);
|
|
243
|
+
|
|
244
|
+
// Step 6: wait for the high-resolution turn-on (7/ODR = 70 ms at
|
|
245
|
+
// 100 Hz) — the longer of the turn-on time and one ODR period.
|
|
246
|
+
await _sleep(this._settleMs());
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* Write one register.
|
|
251
|
+
* @param {number} reg - Register address.
|
|
252
|
+
* @param {number} value - Byte value.
|
|
253
|
+
* @returns {Promise<void>}
|
|
254
|
+
* @private
|
|
255
|
+
*/
|
|
256
|
+
async _writeReg(reg, value) {
|
|
257
|
+
await this._conn.writeReg(reg, value & 0xFF);
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Read one register.
|
|
262
|
+
* @param {number} reg - Register address.
|
|
263
|
+
* @returns {Promise<number>} The register byte.
|
|
264
|
+
* @private
|
|
265
|
+
*/
|
|
266
|
+
async _readReg(reg) {
|
|
267
|
+
return (await this._conn.readReg(reg, 1))[0];
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Read one or more consecutive registers. Multi-byte I²C transfers set
|
|
272
|
+
* the sub-address MSb (address auto-increment).
|
|
273
|
+
* @param {number} reg - First register address.
|
|
274
|
+
* @param {number} length - Number of bytes to read.
|
|
275
|
+
* @returns {Promise<Buffer>} The register bytes.
|
|
276
|
+
* @private
|
|
277
|
+
*/
|
|
278
|
+
async _readBurst(reg, length) {
|
|
279
|
+
const sub = length > 1 ? (reg | 0x80) : reg;
|
|
280
|
+
return this._conn.readReg(sub, length);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/**
|
|
284
|
+
* Milliseconds to wait after an ODR / mode change: the longer of the
|
|
285
|
+
* mode's turn-on time (datasheet Table 10) and one ODR period.
|
|
286
|
+
* @returns {number} Delay in milliseconds.
|
|
287
|
+
* @private
|
|
288
|
+
*/
|
|
289
|
+
_settleMs() {
|
|
290
|
+
if (this._rateHz <= 0) return 0;
|
|
291
|
+
const periodMs = 1000.0 / this._rateHz;
|
|
292
|
+
let turnOnMs;
|
|
293
|
+
if (this._mode === _MODE_LP) {
|
|
294
|
+
turnOnMs = 1.0;
|
|
295
|
+
} else if (this._mode === _MODE_NORMAL) {
|
|
296
|
+
turnOnMs = 1.6;
|
|
297
|
+
} else {
|
|
298
|
+
turnOnMs = 7.0 * periodMs;
|
|
299
|
+
}
|
|
300
|
+
return _ceilMs(Math.max(turnOnMs, periodMs));
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Sign-extend the left-justified 16-bit output pair and shift it down to
|
|
305
|
+
* the current resolution.
|
|
306
|
+
* @param {number} low - Low byte (LSB first, `BLE` = 0).
|
|
307
|
+
* @param {number} high - High byte.
|
|
308
|
+
* @returns {number} Signed counts at the current resolution.
|
|
309
|
+
* @private
|
|
310
|
+
*/
|
|
311
|
+
_decodeAxis(low, high) {
|
|
312
|
+
const raw16 = toSigned(low | (high << 8), 16);
|
|
313
|
+
return raw16 >> _SHIFT[this._mode];
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* @returns {number} Sensitivity of the current mode and range in mg/LSB.
|
|
318
|
+
* @private
|
|
319
|
+
*/
|
|
320
|
+
_sensitivityMg() {
|
|
321
|
+
return _SENSITIVITY_MG[this._mode][this._rangeG];
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* Read 3-axis linear acceleration.
|
|
326
|
+
*
|
|
327
|
+
* Burst-reads the six output bytes (0x28–0x2D) with auto-increment so
|
|
328
|
+
* X, Y and Z come from a single sample (`BDU` = 1), sign-extends the
|
|
329
|
+
* left-justified 16-bit pairs and scales them with the sensitivity of
|
|
330
|
+
* the current mode and range.
|
|
331
|
+
*
|
|
332
|
+
* @returns {Promise<{x: number, y: number, z: number}>} Acceleration per axis in *g*.
|
|
333
|
+
*/
|
|
334
|
+
async read() {
|
|
335
|
+
await this._ready;
|
|
336
|
+
const raw = await this._readBurst(_REG_OUT_X_L, 6);
|
|
337
|
+
const scale = this._sensitivityMg() / 1000.0;
|
|
338
|
+
return {
|
|
339
|
+
x: this._decodeAxis(raw[0], raw[1]) * scale,
|
|
340
|
+
y: this._decodeAxis(raw[2], raw[3]) * scale,
|
|
341
|
+
z: this._decodeAxis(raw[4], raw[5]) * scale,
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Interrupt source constants for the LIS3DH Full driver.
|
|
348
|
+
*
|
|
349
|
+
* The values match the `enableInterrupt()` / `disableInterrupt()` source
|
|
350
|
+
* argument and the bit layout returned by `LIS3DHFull.pollInterrupt()` for
|
|
351
|
+
* bits 0-6. `SOURCE_ACTIVITY` and `SOURCE_BOOT` are pin-only: the chip has
|
|
352
|
+
* no register flag for them, so `pollInterrupt()` never reports them.
|
|
353
|
+
*/
|
|
354
|
+
class LIS3DHSource {
|
|
355
|
+
/** New XYZ sample set (`STATUS_REG.ZYXDA`) — INT1 only. */
|
|
356
|
+
static SOURCE_DATA_READY = 0x0001;
|
|
357
|
+
/** New aux ADC sample (`STATUS_REG_AUX.321DA`) — INT1 only. */
|
|
358
|
+
static SOURCE_AUX_DATA_READY = 0x0002;
|
|
359
|
+
/** FIFO content exceeds the watermark (`FIFO_SRC_REG.WTM`) — INT1 only. */
|
|
360
|
+
static SOURCE_WATERMARK = 0x0004;
|
|
361
|
+
/** FIFO overrun (`FIFO_SRC_REG.OVRN_FIFO`) — INT1 only. */
|
|
362
|
+
static SOURCE_OVERRUN = 0x0008;
|
|
363
|
+
/** Inertial generator 1 event (`INT1_SRC.IA`) — INT1 or INT2. */
|
|
364
|
+
static SOURCE_IA1 = 0x0010;
|
|
365
|
+
/** Inertial generator 2 event (`INT2_SRC.IA`) — INT1 or INT2. */
|
|
366
|
+
static SOURCE_IA2 = 0x0020;
|
|
367
|
+
/** Single / double click (`CLICK_SRC.IA`) — INT1 or INT2. */
|
|
368
|
+
static SOURCE_CLICK = 0x0040;
|
|
369
|
+
/** Sleep-to-wake activity — INT2 only. */
|
|
370
|
+
static SOURCE_ACTIVITY = 0x0080;
|
|
371
|
+
/** Boot procedure running — INT2 only. */
|
|
372
|
+
static SOURCE_BOOT = 0x0100;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* LIS3DH full interface — extends {@link LIS3DHMinimal} with the complete
|
|
377
|
+
* chip functionality.
|
|
378
|
+
*
|
|
379
|
+
* Adds range / data-rate / mode selection, raw counts, status and
|
|
380
|
+
* temperature reads, the auxiliary ADC, the high-pass filter, the 32-level
|
|
381
|
+
* FIFO, both inertial interrupt generators (free-fall, 6D / 4D orientation),
|
|
382
|
+
* single / double click detection, the sleep-to-wake activity function,
|
|
383
|
+
* interrupt routing and delivery on INT1 / INT2, memory reboot, SDO
|
|
384
|
+
* pull-up control and the electrostatic self-test.
|
|
385
|
+
*/
|
|
386
|
+
class LIS3DHFull extends LIS3DHMinimal {
|
|
387
|
+
/**
|
|
388
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - Configured I²C connection (0x18 with SA0 = GND, 0x19 with SA0 = VDD_IO).
|
|
389
|
+
*/
|
|
390
|
+
constructor(connection) {
|
|
391
|
+
super(connection);
|
|
392
|
+
this._enabled1 = 0;
|
|
393
|
+
this._enabled2 = 0;
|
|
394
|
+
this._intCallback = null;
|
|
395
|
+
this._intHandler = null;
|
|
396
|
+
this._pollTimer = null;
|
|
397
|
+
this._intActiveHigh = true;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
// ---- configuration ----------------------------------------------------
|
|
401
|
+
|
|
402
|
+
/**
|
|
403
|
+
* Set the full-scale measurement range.
|
|
404
|
+
*
|
|
405
|
+
* Read-modify-writes the `FS` field of `CTRL_REG4` and updates the
|
|
406
|
+
* cached sensitivity used by `read()` / `readRaw()` conversions.
|
|
407
|
+
*
|
|
408
|
+
* @param {number} rangeG - One of 2, 4, 8, 16 (*g*).
|
|
409
|
+
* @returns {Promise<void>}
|
|
410
|
+
* @throws {Error} If `rangeG` is not one of 2, 4, 8, 16.
|
|
411
|
+
*/
|
|
412
|
+
async setRange(rangeG) {
|
|
413
|
+
await this._ready;
|
|
414
|
+
if (!_RANGES.includes(rangeG)) {
|
|
415
|
+
throw new Error('rangeG must be one of 2, 4, 8, 16');
|
|
416
|
+
}
|
|
417
|
+
const fs = _RANGES.indexOf(rangeG) << 4;
|
|
418
|
+
const ctrl4 = await this._readReg(_REG_CTRL_REG4);
|
|
419
|
+
await this._writeReg(_REG_CTRL_REG4, (ctrl4 & ~0x30 & 0xFF) | fs);
|
|
420
|
+
this._rangeG = rangeG;
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Set the output data rate (or power down).
|
|
425
|
+
*
|
|
426
|
+
* Read-modify-writes the `ODR` field of `CTRL_REG1` and waits for the
|
|
427
|
+
* turn-on time before returning.
|
|
428
|
+
*
|
|
429
|
+
* @param {number} rateHz - One of 0 (power-down), 1, 10, 25, 50, 100, 200, 400, 1344, 1620, 5376. 1620 and 5376 exist in low-power mode only; 1344 in normal / high-resolution mode only.
|
|
430
|
+
* @returns {Promise<void>}
|
|
431
|
+
* @throws {Error} If the rate is unknown or not allowed in the current operating mode.
|
|
432
|
+
*/
|
|
433
|
+
async setDataRate(rateHz) {
|
|
434
|
+
await this._ready;
|
|
435
|
+
const code = _RATE_CODES.get(rateHz);
|
|
436
|
+
if (code === undefined) {
|
|
437
|
+
throw new Error('rateHz must be one of 0, 1, 10, 25, 50, 100, 200, 400, 1344, 1620, 5376');
|
|
438
|
+
}
|
|
439
|
+
if (_LP_ONLY_RATES.includes(rateHz) && this._mode !== _MODE_LP) {
|
|
440
|
+
throw new Error('rate ' + rateHz + ' Hz is low-power mode only');
|
|
441
|
+
}
|
|
442
|
+
if (_NON_LP_ONLY_RATES.includes(rateHz) && this._mode === _MODE_LP) {
|
|
443
|
+
throw new Error('rate ' + rateHz + ' Hz is not available in low-power mode');
|
|
444
|
+
}
|
|
445
|
+
const ctrl1 = await this._readReg(_REG_CTRL_REG1);
|
|
446
|
+
await this._writeReg(_REG_CTRL_REG1, (ctrl1 & 0x0F) | code);
|
|
447
|
+
this._rateHz = rateHz;
|
|
448
|
+
await _sleep(this._settleMs());
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
/**
|
|
452
|
+
* Set the operating mode (resolution / power trade-off).
|
|
453
|
+
*
|
|
454
|
+
* Read-modify-writes `LPen` in `CTRL_REG1` and `HR` in `CTRL_REG4`,
|
|
455
|
+
* updates the cached resolution and waits for the mode's turn-on time.
|
|
456
|
+
*
|
|
457
|
+
* @param {number} mode - 0 = low-power (8-bit), 1 = normal (10-bit), 2 = high-resolution (12-bit).
|
|
458
|
+
* @returns {Promise<void>}
|
|
459
|
+
* @throws {Error} If `mode` is not 0, 1 or 2, or if the current data rate is illegal in that mode (`LPen` = 1 with `HR` = 1 is never written).
|
|
460
|
+
*/
|
|
461
|
+
async setMode(mode) {
|
|
462
|
+
await this._ready;
|
|
463
|
+
if (mode !== _MODE_LP && mode !== _MODE_NORMAL && mode !== _MODE_HR) {
|
|
464
|
+
throw new Error('mode must be 0 (low-power), 1 (normal) or 2 (high-resolution)');
|
|
465
|
+
}
|
|
466
|
+
if (mode === _MODE_LP && _NON_LP_ONLY_RATES.includes(this._rateHz)) {
|
|
467
|
+
throw new Error('rate ' + this._rateHz + ' Hz is not available in low-power mode');
|
|
468
|
+
}
|
|
469
|
+
if (mode !== _MODE_LP && _LP_ONLY_RATES.includes(this._rateHz)) {
|
|
470
|
+
throw new Error('rate ' + this._rateHz + ' Hz is low-power mode only');
|
|
471
|
+
}
|
|
472
|
+
let ctrl1 = await this._readReg(_REG_CTRL_REG1);
|
|
473
|
+
let ctrl4 = await this._readReg(_REG_CTRL_REG4);
|
|
474
|
+
if (mode === _MODE_LP) {
|
|
475
|
+
ctrl1 |= 0x08; // LPen = 1
|
|
476
|
+
ctrl4 &= ~0x08 & 0xFF; // HR = 0 (LPen=1 with HR=1 not allowed)
|
|
477
|
+
} else {
|
|
478
|
+
ctrl1 &= ~0x08 & 0xFF; // LPen = 0
|
|
479
|
+
if (mode === _MODE_HR) {
|
|
480
|
+
ctrl4 |= 0x08; // HR = 1
|
|
481
|
+
} else {
|
|
482
|
+
ctrl4 &= ~0x08 & 0xFF; // HR = 0
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
await this._writeReg(_REG_CTRL_REG1, ctrl1);
|
|
486
|
+
await this._writeReg(_REG_CTRL_REG4, ctrl4);
|
|
487
|
+
this._mode = mode;
|
|
488
|
+
await _sleep(this._settleMs());
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
/**
|
|
492
|
+
* Enable or disable the individual measurement axes.
|
|
493
|
+
*
|
|
494
|
+
* @param {boolean} [x=true] - Enable the X axis.
|
|
495
|
+
* @param {boolean} [y=true] - Enable the Y axis.
|
|
496
|
+
* @param {boolean} [z=true] - Enable the Z axis.
|
|
497
|
+
* @returns {Promise<void>}
|
|
498
|
+
*/
|
|
499
|
+
async setAxes(x = true, y = true, z = true) {
|
|
500
|
+
await this._ready;
|
|
501
|
+
const ctrl1 = await this._readReg(_REG_CTRL_REG1);
|
|
502
|
+
const axes = (x ? 1 : 0) | ((y ? 1 : 0) << 1) | ((z ? 1 : 0) << 2);
|
|
503
|
+
await this._writeReg(_REG_CTRL_REG1, (ctrl1 & 0xF8) | axes);
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
/**
|
|
507
|
+
* Stop conversions by setting `ODR` = 0000; registers are retained.
|
|
508
|
+
* @returns {Promise<void>}
|
|
509
|
+
*/
|
|
510
|
+
async powerDown() {
|
|
511
|
+
await this._ready;
|
|
512
|
+
const ctrl1 = await this._readReg(_REG_CTRL_REG1);
|
|
513
|
+
await this._writeReg(_REG_CTRL_REG1, ctrl1 & 0x0F);
|
|
514
|
+
this._rateHz = 0;
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// ---- data reads -------------------------------------------------------
|
|
518
|
+
|
|
519
|
+
/**
|
|
520
|
+
* Read the raw acceleration counts at the current resolution.
|
|
521
|
+
* @returns {Promise<{x: number, y: number, z: number}>} Signed counts — 12-bit in high-resolution mode, 10-bit in normal mode, 8-bit in low-power mode.
|
|
522
|
+
*/
|
|
523
|
+
async readRaw() {
|
|
524
|
+
await this._ready;
|
|
525
|
+
const raw = await this._readBurst(_REG_OUT_X_L, 6);
|
|
526
|
+
return {
|
|
527
|
+
x: this._decodeAxis(raw[0], raw[1]),
|
|
528
|
+
y: this._decodeAxis(raw[2], raw[3]),
|
|
529
|
+
z: this._decodeAxis(raw[4], raw[5]),
|
|
530
|
+
};
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
/**
|
|
534
|
+
* Check whether a complete X/Y/Z sample set is ready.
|
|
535
|
+
* @returns {Promise<boolean>} `STATUS_REG.ZYXDA` — true when all three axes have new data.
|
|
536
|
+
*/
|
|
537
|
+
async newDataAvailable() {
|
|
538
|
+
await this._ready;
|
|
539
|
+
return (await this._readReg(_REG_STATUS) & 0x08) !== 0;
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
/**
|
|
543
|
+
* Read the STATUS register.
|
|
544
|
+
* @returns {Promise<number>} `STATUS_REG` byte — overrun (`ZYXOR`..`XOR`) and data-available (`ZYXDA`..`XDA`) flags.
|
|
545
|
+
*/
|
|
546
|
+
async readStatus() {
|
|
547
|
+
await this._ready;
|
|
548
|
+
return this._readReg(_REG_STATUS);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/**
|
|
552
|
+
* Read the relative on-chip temperature.
|
|
553
|
+
*
|
|
554
|
+
* Enables `ADC_EN` and `TEMP_EN` in `TEMP_CFG_REG` on first use (the ADC
|
|
555
|
+
* channel-3 conversions run through the same ADC) and waits one ODR
|
|
556
|
+
* period for the first conversion.
|
|
557
|
+
*
|
|
558
|
+
* The datasheet specifies only 1 digit/°C with no zero-degree reference,
|
|
559
|
+
* so the value is a relative delta, never an absolute temperature.
|
|
560
|
+
*
|
|
561
|
+
* @returns {Promise<number>} Relative temperature in °C (8-bit two's complement, 1 digit/°C).
|
|
562
|
+
*/
|
|
563
|
+
async readTemperatureDelta() {
|
|
564
|
+
await this._ready;
|
|
565
|
+
const cfg = await this._readReg(_REG_TEMP_CFG);
|
|
566
|
+
if ((cfg & 0xC0) !== 0xC0) {
|
|
567
|
+
await this._writeReg(_REG_TEMP_CFG, cfg | 0xC0);
|
|
568
|
+
if (this._rateHz > 0) {
|
|
569
|
+
await _sleep(_ceilMs(1000.0 / this._rateHz));
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
return toSigned(await this._readReg(_REG_OUT_ADC3_H), 8);
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
/**
|
|
576
|
+
* Read one auxiliary ADC channel.
|
|
577
|
+
*
|
|
578
|
+
* @param {number} channel - 1, 2 or 3. Channel 3 is the temperature-sensor input while `TEMP_EN` = 1.
|
|
579
|
+
* @returns {Promise<number>} Input voltage in V (1.200 V centre, ±400 mV range; the mapping is derived from the datasheet's stated input range).
|
|
580
|
+
* @throws {Error} If `channel` is not 1, 2 or 3, or if channel 3 is requested while `TEMP_EN` = 1.
|
|
581
|
+
*/
|
|
582
|
+
async readAdc(channel) {
|
|
583
|
+
await this._ready;
|
|
584
|
+
if (channel !== 1 && channel !== 2 && channel !== 3) {
|
|
585
|
+
throw new Error('channel must be 1, 2 or 3');
|
|
586
|
+
}
|
|
587
|
+
if (channel === 3) {
|
|
588
|
+
const cfg = await this._readReg(_REG_TEMP_CFG);
|
|
589
|
+
if (cfg & 0x40) {
|
|
590
|
+
throw new Error('channel 3 is the temperature sensor while TEMP_EN = 1');
|
|
591
|
+
}
|
|
592
|
+
}
|
|
593
|
+
const base = _REG_OUT_ADC1_L + (channel - 1) * 2;
|
|
594
|
+
const raw = await this._readBurst(base, 2);
|
|
595
|
+
const raw16 = toSigned(raw[0] | (raw[1] << 8), 16);
|
|
596
|
+
const counts = raw16 >> _ADC_SHIFT[this._mode];
|
|
597
|
+
return 1.200 + counts * (0.400 / 512.0);
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
/**
|
|
601
|
+
* Enable or disable the auxiliary ADC (`ADC_EN` in `TEMP_CFG_REG`).
|
|
602
|
+
* @param {boolean} enabled - True to enable the ADC, false to disable it.
|
|
603
|
+
* @returns {Promise<void>}
|
|
604
|
+
*/
|
|
605
|
+
async enableAdc(enabled) {
|
|
606
|
+
await this._ready;
|
|
607
|
+
const cfg = await this._readReg(_REG_TEMP_CFG);
|
|
608
|
+
await this._writeReg(_REG_TEMP_CFG, enabled ? (cfg | 0x80) : (cfg & ~0x80 & 0xFF));
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
// ---- high-pass filter -------------------------------------------------
|
|
612
|
+
|
|
613
|
+
/**
|
|
614
|
+
* Configure the high-pass filter (`CTRL_REG2`).
|
|
615
|
+
*
|
|
616
|
+
* @param {object|number} [options] - High-pass filter settings. A bare
|
|
617
|
+
* number is accepted as a shorthand for the `mode` option.
|
|
618
|
+
* @param {number} [options.mode=0] - `HPM` — 0 normal (reset by reading `REFERENCE`), 1 reference signal for filtering, 2 normal, 3 auto-reset on interrupt event.
|
|
619
|
+
* @param {number} [options.cutoff=0] - `HPCF<2:1>` code 0-3 (the datasheet tabulates no Hz values; they scale with ODR).
|
|
620
|
+
* @param {boolean} [options.filterOutput=false] - Route the filtered data to the output registers and FIFO (`FDS`).
|
|
621
|
+
* @param {boolean} [options.onIa1=false] - Apply the filter to interrupt generator 1.
|
|
622
|
+
* @param {boolean} [options.onIa2=false] - Apply the filter to interrupt generator 2.
|
|
623
|
+
* @param {boolean} [options.onClick=false] - Apply the filter to the click function.
|
|
624
|
+
* @returns {Promise<void>}
|
|
625
|
+
* @throws {Error} If `mode` is not 0-3 or `cutoff` not 0-3.
|
|
626
|
+
*/
|
|
627
|
+
async setHighPass(options = {}) {
|
|
628
|
+
await this._ready;
|
|
629
|
+
if (typeof options === 'number') options = {mode: options};
|
|
630
|
+
const mode = options.mode === undefined ? 0 : options.mode;
|
|
631
|
+
const cutoff = options.cutoff === undefined ? 0 : options.cutoff;
|
|
632
|
+
const filterOutput = options.filterOutput === undefined ? false : options.filterOutput;
|
|
633
|
+
const onIa1 = options.onIa1 === undefined ? false : options.onIa1;
|
|
634
|
+
const onIa2 = options.onIa2 === undefined ? false : options.onIa2;
|
|
635
|
+
const onClick = options.onClick === undefined ? false : options.onClick;
|
|
636
|
+
if (mode < 0 || mode > 3) {
|
|
637
|
+
throw new Error('mode must be 0-3');
|
|
638
|
+
}
|
|
639
|
+
if (cutoff < 0 || cutoff > 3) {
|
|
640
|
+
throw new Error('cutoff must be 0-3');
|
|
641
|
+
}
|
|
642
|
+
let value = (mode << 6) | (cutoff << 4);
|
|
643
|
+
if (filterOutput) value |= 0x08;
|
|
644
|
+
if (onClick) value |= 0x04;
|
|
645
|
+
if (onIa2) value |= 0x02;
|
|
646
|
+
if (onIa1) value |= 0x01;
|
|
647
|
+
await this._writeReg(_REG_CTRL_REG2, value);
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
/**
|
|
651
|
+
* Reset the filter by reading `REFERENCE` (`HPM` = 00).
|
|
652
|
+
* @returns {Promise<number>} The `REFERENCE` register value that was read.
|
|
653
|
+
*/
|
|
654
|
+
async resetHighPass() {
|
|
655
|
+
await this._ready;
|
|
656
|
+
return this._readReg(_REG_REFERENCE);
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/**
|
|
660
|
+
* Write the high-pass reference value (`HPM` = 01).
|
|
661
|
+
* @param {number} value - 0-255.
|
|
662
|
+
* @returns {Promise<void>}
|
|
663
|
+
* @throws {Error} If `value` is outside 0-255.
|
|
664
|
+
*/
|
|
665
|
+
async setReference(value) {
|
|
666
|
+
await this._ready;
|
|
667
|
+
if (!Number.isInteger(value) || value < 0 || value > 255) {
|
|
668
|
+
throw new Error('value must be 0-255');
|
|
669
|
+
}
|
|
670
|
+
await this._writeReg(_REG_REFERENCE, value);
|
|
671
|
+
}
|
|
672
|
+
|
|
673
|
+
// ---- FIFO -------------------------------------------------------------
|
|
674
|
+
|
|
675
|
+
/**
|
|
676
|
+
* Configure the 32-level FIFO (`FIFO_CTRL_REG`).
|
|
677
|
+
*
|
|
678
|
+
* Sets `FIFO_EN` in `CTRL_REG5` for modes 1-3; returning to bypass
|
|
679
|
+
* (mode 0) clears the buffer. Only bypass mode empties the buffer —
|
|
680
|
+
* restart a FIFO mode through bypass after a full drain.
|
|
681
|
+
*
|
|
682
|
+
* @param {number} mode - 0 = bypass, 1 = FIFO, 2 = stream, 3 = stream-to-FIFO.
|
|
683
|
+
* @param {number} [watermark=0] - Watermark level `FTH` 0-31.
|
|
684
|
+
* @param {boolean} [triggerInt2=false] - Trigger source for stream-to-FIFO: false = INT1, true = INT2 (`TR`).
|
|
685
|
+
* @returns {Promise<void>}
|
|
686
|
+
* @throws {Error} If `mode` is not 0-3 or `watermark` outside 0-31.
|
|
687
|
+
*/
|
|
688
|
+
async setFifo(mode, watermark = 0, triggerInt2 = false) {
|
|
689
|
+
await this._ready;
|
|
690
|
+
if (mode < 0 || mode > 3) {
|
|
691
|
+
throw new Error('mode must be 0 (bypass), 1 (FIFO), 2 (stream) or 3 (stream-to-FIFO)');
|
|
692
|
+
}
|
|
693
|
+
if (watermark < 0 || watermark > 31) {
|
|
694
|
+
throw new Error('watermark must be 0-31');
|
|
695
|
+
}
|
|
696
|
+
const value = (mode << 6) | (triggerInt2 ? 0x20 : 0x00) | watermark;
|
|
697
|
+
await this._writeReg(_REG_FIFO_CTRL, value);
|
|
698
|
+
const ctrl5 = await this._readReg(_REG_CTRL_REG5);
|
|
699
|
+
await this._writeReg(_REG_CTRL_REG5, mode === 0 ? (ctrl5 & ~0x40 & 0xFF) : (ctrl5 | 0x40));
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
/**
|
|
703
|
+
* Read the number of unread sample sets in the FIFO.
|
|
704
|
+
* @returns {Promise<number>} `FSS<4:0>` — 0-32 unread sample sets.
|
|
705
|
+
*/
|
|
706
|
+
async fifoLevel() {
|
|
707
|
+
await this._ready;
|
|
708
|
+
return (await this._readReg(_REG_FIFO_SRC)) & 0x1F;
|
|
709
|
+
}
|
|
710
|
+
|
|
711
|
+
/**
|
|
712
|
+
* Drain sample sets from the FIFO.
|
|
713
|
+
*
|
|
714
|
+
* Each sample set is one 6-byte burst of 0x28–0x2D; the address
|
|
715
|
+
* auto-increment wraps back to 0x28 so a full drain is 32 reads.
|
|
716
|
+
*
|
|
717
|
+
* @param {number} [count=null] - Maximum number of sample sets to read (default: all `FSS` entries currently in the FIFO). Never reads more than the FIFO actually holds.
|
|
718
|
+
* @returns {Promise<Array<{x: number, y: number, z: number}>>} Sample sets in *g*, oldest sample first.
|
|
719
|
+
*/
|
|
720
|
+
async readFifo(count = null) {
|
|
721
|
+
await this._ready;
|
|
722
|
+
const level = await this.fifoLevel();
|
|
723
|
+
const n = (count === null || count === undefined) ? level : Math.min(count, level);
|
|
724
|
+
const samples = [];
|
|
725
|
+
const scale = this._sensitivityMg() / 1000.0;
|
|
726
|
+
for (let i = 0; i < n; i++) {
|
|
727
|
+
const raw = await this._readBurst(_REG_OUT_X_L, 6);
|
|
728
|
+
samples.push({
|
|
729
|
+
x: this._decodeAxis(raw[0], raw[1]) * scale,
|
|
730
|
+
y: this._decodeAxis(raw[2], raw[3]) * scale,
|
|
731
|
+
z: this._decodeAxis(raw[4], raw[5]) * scale,
|
|
732
|
+
});
|
|
733
|
+
}
|
|
734
|
+
return samples;
|
|
735
|
+
}
|
|
736
|
+
|
|
737
|
+
/**
|
|
738
|
+
* Decode `FIFO_SRC_REG`.
|
|
739
|
+
* @returns {Promise<{watermark: boolean, overrun: boolean, empty: boolean, level: number}>} The three flags are booleans, `level` is `FSS<4:0>` (0-32).
|
|
740
|
+
*/
|
|
741
|
+
async readFifoStatus() {
|
|
742
|
+
await this._ready;
|
|
743
|
+
const raw = await this._readReg(_REG_FIFO_SRC);
|
|
744
|
+
return {
|
|
745
|
+
watermark: (raw & 0x80) !== 0,
|
|
746
|
+
overrun: (raw & 0x40) !== 0,
|
|
747
|
+
empty: (raw & 0x20) !== 0,
|
|
748
|
+
level: raw & 0x1F,
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
|
|
752
|
+
// ---- inertial interrupt generators -----------------------------------
|
|
753
|
+
|
|
754
|
+
/**
|
|
755
|
+
* Configure an inertial interrupt generator (motion / threshold).
|
|
756
|
+
*
|
|
757
|
+
* @param {number} generator - 1 or 2 — the `INT1` or `INT2` register block.
|
|
758
|
+
* @param {number} events - 6-bit `ZHIE`..`XLIE` event-enable mask (0-0x3F).
|
|
759
|
+
* @param {number} thresholdG - Threshold in *g*; scaled with the current full-scale range (16 / 32 / 62 / 186 mg per LSb).
|
|
760
|
+
* @param {number} [durationMs=0] - Minimum duration the events must hold, in ms (`INTx_DURATION`, 1 LSb = 1/ODR).
|
|
761
|
+
* @param {boolean} [andMode=false] - True for an AND combination of the enabled events (`AOI` = 1), false for OR.
|
|
762
|
+
* @returns {Promise<void>}
|
|
763
|
+
* @throws {Error} If `generator` is not 1 or 2 or `events` is outside 0-0x3F.
|
|
764
|
+
*/
|
|
765
|
+
async setMotionInterrupt(generator, events, thresholdG, durationMs = 0, andMode = false) {
|
|
766
|
+
await this._ready;
|
|
767
|
+
const regs = this._generatorRegs(generator);
|
|
768
|
+
if (events < 0 || events > 0x3F) {
|
|
769
|
+
throw new Error('events must be a 6-bit mask (0-0x3F)');
|
|
770
|
+
}
|
|
771
|
+
const cfg = (andMode ? 0x80 : 0x00) | (events & 0x3F);
|
|
772
|
+
await this._writeReg(regs.cfg, cfg);
|
|
773
|
+
await this._writeReg(regs.ths, this._thresholdCode(thresholdG));
|
|
774
|
+
await this._writeReg(regs.dur, this._durationCode(durationMs));
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
/**
|
|
778
|
+
* Configure the free-fall detector: all three axes low, AND mode.
|
|
779
|
+
*
|
|
780
|
+
* Convenience wrapper around the generator's `INTx_CFG` = 0x95
|
|
781
|
+
* (`AOI` + `XLIE`/`YLIE`/`ZLIE`).
|
|
782
|
+
*
|
|
783
|
+
* @param {number} [generator=1] - 1 or 2.
|
|
784
|
+
* @param {number} [thresholdG=0.35] - Free-fall threshold in *g*.
|
|
785
|
+
* @param {number} [durationMs=30] - Minimum time below the threshold in ms.
|
|
786
|
+
* @returns {Promise<void>}
|
|
787
|
+
* @throws {Error} If `generator` is not 1 or 2.
|
|
788
|
+
*/
|
|
789
|
+
async setFreeFall(generator = 1, thresholdG = 0.35, durationMs = 30) {
|
|
790
|
+
await this._ready;
|
|
791
|
+
const regs = this._generatorRegs(generator);
|
|
792
|
+
await this._writeReg(regs.cfg, 0x95);
|
|
793
|
+
await this._writeReg(regs.ths, this._thresholdCode(thresholdG));
|
|
794
|
+
await this._writeReg(regs.dur, this._durationCode(durationMs));
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
/**
|
|
798
|
+
* Configure 6D / 4D orientation detection.
|
|
799
|
+
*
|
|
800
|
+
* `AOI` / `6D` = 01 is *movement* recognition (a one-ODR pulse), 11 is
|
|
801
|
+
* *position* recognition (level, active while the orientation is inside
|
|
802
|
+
* a known zone).
|
|
803
|
+
*
|
|
804
|
+
* @param {number} [generator=1] - 1 or 2.
|
|
805
|
+
* @param {boolean} [position=false] - False = 6D movement recognition, true = 6D position recognition.
|
|
806
|
+
* @param {boolean} [fourD=false] - Enable 4D detection (`D4D_INTx`) — disables the Z-axis zones.
|
|
807
|
+
* @returns {Promise<void>}
|
|
808
|
+
* @throws {Error} If `generator` is not 1 or 2.
|
|
809
|
+
*/
|
|
810
|
+
async setOrientation(generator = 1, position = false, fourD = false) {
|
|
811
|
+
await this._ready;
|
|
812
|
+
const regs = this._generatorRegs(generator);
|
|
813
|
+
const axisZones = fourD ? 0x0F : 0x3F;
|
|
814
|
+
const cfg = (position ? 0xC0 : 0x40) | axisZones;
|
|
815
|
+
await this._writeReg(regs.cfg, cfg);
|
|
816
|
+
const ctrl5 = await this._readReg(_REG_CTRL_REG5);
|
|
817
|
+
const d4dBit = generator === 1 ? 0x04 : 0x01;
|
|
818
|
+
await this._writeReg(_REG_CTRL_REG5, fourD ? (ctrl5 | d4dBit) : (ctrl5 & ~d4dBit & 0xFF));
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
/**
|
|
822
|
+
* Latch a generator's interrupt until its `INTx_SRC` is read.
|
|
823
|
+
* @param {number} generator - 1 or 2.
|
|
824
|
+
* @param {boolean} enabled - True to latch (`LIR_INTx`), false for a self-resetting interrupt.
|
|
825
|
+
* @returns {Promise<void>}
|
|
826
|
+
* @throws {Error} If `generator` is not 1 or 2.
|
|
827
|
+
*/
|
|
828
|
+
async setLatch(generator, enabled) {
|
|
829
|
+
await this._ready;
|
|
830
|
+
if (generator !== 1 && generator !== 2) {
|
|
831
|
+
throw new Error('generator must be 1 or 2');
|
|
832
|
+
}
|
|
833
|
+
const bit = generator === 1 ? 0x08 : 0x02;
|
|
834
|
+
const ctrl5 = await this._readReg(_REG_CTRL_REG5);
|
|
835
|
+
await this._writeReg(_REG_CTRL_REG5, enabled ? (ctrl5 | bit) : (ctrl5 & ~bit & 0xFF));
|
|
836
|
+
}
|
|
837
|
+
|
|
838
|
+
/**
|
|
839
|
+
* Read a generator's `INTx_SRC` — **clears** the interrupt.
|
|
840
|
+
* @param {number} generator - 1 or 2.
|
|
841
|
+
* @returns {Promise<number>} `INTx_SRC` byte (`IA` = bit 6, per-axis event flags in bits 5-0).
|
|
842
|
+
* @throws {Error} If `generator` is not 1 or 2.
|
|
843
|
+
*/
|
|
844
|
+
async readInterruptSource(generator) {
|
|
845
|
+
await this._ready;
|
|
846
|
+
if (generator !== 1 && generator !== 2) {
|
|
847
|
+
throw new Error('generator must be 1 or 2');
|
|
848
|
+
}
|
|
849
|
+
return this._readReg(generator === 1 ? _REG_INT1_SRC : _REG_INT2_SRC);
|
|
850
|
+
}
|
|
851
|
+
|
|
852
|
+
// ---- click detection --------------------------------------------------
|
|
853
|
+
|
|
854
|
+
/**
|
|
855
|
+
* Configure single / double click detection (`CLICK_*`).
|
|
856
|
+
*
|
|
857
|
+
* @param {number} [single=0] - Axis mask of single-click enables (X = 1, Y = 2, Z = 4).
|
|
858
|
+
* @param {number} [double=0] - Axis mask of double-click enables (X = 1, Y = 2, Z = 4).
|
|
859
|
+
* @param {number} [thresholdG=0.5] - Click threshold in *g* (same scale as `INTx_THS`).
|
|
860
|
+
* @param {number} [timeLimitMs=10] - Maximum time between the two clicks of a double click.
|
|
861
|
+
* @param {number} [latencyMs=20] - Minimum time between two consecutive clicks.
|
|
862
|
+
* @param {number} [windowMs=50] - Maximum time after the first click of a double click.
|
|
863
|
+
* @param {boolean} [latch=false] - Keep the interrupt high until `CLICK_SRC` is read (`LIR_Click`).
|
|
864
|
+
* @returns {Promise<void>}
|
|
865
|
+
* @throws {Error} If either axis mask is outside 0-7.
|
|
866
|
+
*/
|
|
867
|
+
async setClick(single = 0, double = 0, thresholdG = 0.5, timeLimitMs = 10, latencyMs = 20, windowMs = 50, latch = false) {
|
|
868
|
+
await this._ready;
|
|
869
|
+
if (single < 0 || single > 7 || double < 0 || double > 7) {
|
|
870
|
+
throw new Error('single / double must be axis masks (0-7: X=1, Y=2, Z=4)');
|
|
871
|
+
}
|
|
872
|
+
const cfg = ((single & 0x01) | ((single & 0x02) << 1) | ((single & 0x04) << 2) |
|
|
873
|
+
((double & 0x01) << 1) | ((double & 0x02) << 2) | ((double & 0x04) << 3));
|
|
874
|
+
const ths = this._thresholdCode(thresholdG);
|
|
875
|
+
await this._writeReg(_REG_CLICK_CFG, cfg);
|
|
876
|
+
await this._writeReg(_REG_CLICK_THS, ths | (latch ? 0x80 : 0x00));
|
|
877
|
+
await this._writeReg(_REG_TIME_LIMIT, this._durationCode(timeLimitMs, 127));
|
|
878
|
+
await this._writeReg(_REG_TIME_LATENCY, this._durationCode(latencyMs, 255));
|
|
879
|
+
await this._writeReg(_REG_TIME_WINDOW, this._durationCode(windowMs, 255));
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
/**
|
|
883
|
+
* Read `CLICK_SRC` — **clears** the click interrupt.
|
|
884
|
+
* @returns {Promise<number>} `CLICK_SRC` byte (`IA` = bit 6, `DCLICK` = bit 5, `SCLICK` = bit 4, `Sign` = bit 3, axis flags in bits 2-0).
|
|
885
|
+
*/
|
|
886
|
+
async readClickSource() {
|
|
887
|
+
await this._ready;
|
|
888
|
+
return this._readReg(_REG_CLICK_SRC);
|
|
889
|
+
}
|
|
890
|
+
|
|
891
|
+
// ---- sleep-to-wake activity -------------------------------------------
|
|
892
|
+
|
|
893
|
+
/**
|
|
894
|
+
* Configure the sleep-to-wake / return-to-sleep activity function.
|
|
895
|
+
*
|
|
896
|
+
* While the function has dropped the chip into its 10 Hz low-power state
|
|
897
|
+
* the `ODR`, `LPen` and `HR` settings are ignored; the chip restores them
|
|
898
|
+
* on activity. Pair with `SOURCE_ACTIVITY`.
|
|
899
|
+
*
|
|
900
|
+
* @param {number} thresholdG - Activation threshold in *g* (`ACT_THS`).
|
|
901
|
+
* @param {number} durationMs - Activation duration in ms (`ACT_DUR`, where 1 LSb = (8·ACT_DUR + 1)/ODR).
|
|
902
|
+
* @returns {Promise<void>}
|
|
903
|
+
* @throws {Error} If the threshold rounds to 0 counts (`ACT_THS` must be non-zero).
|
|
904
|
+
*/
|
|
905
|
+
async setActivity(thresholdG, durationMs) {
|
|
906
|
+
await this._ready;
|
|
907
|
+
const code = this._thresholdCode(thresholdG);
|
|
908
|
+
if (code === 0) {
|
|
909
|
+
throw new Error('thresholdG is below 1 LSb — ACT_THS must be non-zero');
|
|
910
|
+
}
|
|
911
|
+
let dur = _round((durationMs * this._rateHz / 1000.0 - 1) / 8.0);
|
|
912
|
+
dur = Math.max(0, Math.min(255, dur));
|
|
913
|
+
await this._writeReg(_REG_ACT_THS, code);
|
|
914
|
+
await this._writeReg(_REG_ACT_DUR, dur);
|
|
915
|
+
}
|
|
916
|
+
|
|
917
|
+
/**
|
|
918
|
+
* Set the shared INT1 / INT2 output polarity (`INT_POLARITY`).
|
|
919
|
+
* @param {boolean} activeLow - True for active-low, false for active-high (the reset default).
|
|
920
|
+
* @returns {Promise<void>}
|
|
921
|
+
*/
|
|
922
|
+
async setInterruptPolarity(activeLow) {
|
|
923
|
+
await this._ready;
|
|
924
|
+
const ctrl6 = await this._readReg(_REG_CTRL_REG6);
|
|
925
|
+
await this._writeReg(_REG_CTRL_REG6, activeLow ? (ctrl6 | 0x02) : (ctrl6 & ~0x02 & 0xFF));
|
|
926
|
+
this._intActiveHigh = !activeLow;
|
|
927
|
+
// Re-arm the edge subscription so it follows the new polarity.
|
|
928
|
+
const intPin = this._conn ? this._conn.intPin : null;
|
|
929
|
+
if (intPin && this._intHandler) {
|
|
930
|
+
await intPin.offEdge(this._intHandler);
|
|
931
|
+
await intPin.onEdge(this._intHandler, this._intActiveHigh ? 'rising' : 'falling');
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
// ---- interrupt routing and delivery -----------------------------------
|
|
936
|
+
|
|
937
|
+
/**
|
|
938
|
+
* Route one interrupt source to INT`pin`.
|
|
939
|
+
*
|
|
940
|
+
* Only routes the source — the generator or function behind it is
|
|
941
|
+
* configured with the `set*` methods. `pin` is ignored for INT1-only
|
|
942
|
+
* (data-ready, aux data-ready, watermark, overrun) and INT2-only
|
|
943
|
+
* (activity, boot) sources.
|
|
944
|
+
*
|
|
945
|
+
* @param {number} source - One of the {@link LIS3DHSource} constants.
|
|
946
|
+
* @param {number} [pin=1] - 1 or 2.
|
|
947
|
+
* @returns {Promise<void>}
|
|
948
|
+
* @throws {Error} If `source` is unknown or `pin` is not 1 or 2.
|
|
949
|
+
*/
|
|
950
|
+
async enableInterrupt(source, pin = 1) {
|
|
951
|
+
await this._ready;
|
|
952
|
+
const route = this._route(source, pin);
|
|
953
|
+
const value = await this._readReg(route.reg);
|
|
954
|
+
await this._writeReg(route.reg, value | route.mask);
|
|
955
|
+
if (route.track === 1) {
|
|
956
|
+
this._enabled1 |= (1 << this._sourceBit(source));
|
|
957
|
+
} else {
|
|
958
|
+
this._enabled2 |= (1 << this._sourceBit(source));
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
|
|
962
|
+
/**
|
|
963
|
+
* Remove one interrupt source from INT`pin`.
|
|
964
|
+
* @param {number} source - One of the {@link LIS3DHSource} constants.
|
|
965
|
+
* @param {number} [pin=1] - 1 or 2.
|
|
966
|
+
* @returns {Promise<void>}
|
|
967
|
+
* @throws {Error} If `source` is unknown or `pin` is not 1 or 2.
|
|
968
|
+
*/
|
|
969
|
+
async disableInterrupt(source, pin = 1) {
|
|
970
|
+
await this._ready;
|
|
971
|
+
const route = this._route(source, pin);
|
|
972
|
+
const value = await this._readReg(route.reg);
|
|
973
|
+
await this._writeReg(route.reg, value & ~route.mask & 0xFF);
|
|
974
|
+
if (route.track === 1) {
|
|
975
|
+
this._enabled1 &= ~(1 << this._sourceBit(source));
|
|
976
|
+
} else {
|
|
977
|
+
this._enabled2 &= ~(1 << this._sourceBit(source));
|
|
978
|
+
}
|
|
979
|
+
}
|
|
980
|
+
|
|
981
|
+
/**
|
|
982
|
+
* Subscribe to INT`pin` assertions.
|
|
983
|
+
*
|
|
984
|
+
* The callback receives the combined status int returned by
|
|
985
|
+
* `pollInterrupt()`, and is only invoked for a non-zero status. Uses
|
|
986
|
+
* the connection's `intPin`; the edge follows the configured
|
|
987
|
+
* `INT_POLARITY` (rising for the active-high default). When no INT pin
|
|
988
|
+
* is wired the driver falls back to a 5 ms polling loop.
|
|
989
|
+
*
|
|
990
|
+
* @param {function(number): void} callback - Function called with the status int.
|
|
991
|
+
* @param {number} [pin=1] - 1 or 2 — the INT line the connection's `intPin` is wired to.
|
|
992
|
+
* @returns {Promise<void>}
|
|
993
|
+
* @throws {Error} If `pin` is not 1 or 2.
|
|
994
|
+
*/
|
|
995
|
+
async onInterrupt(callback, pin = 1) {
|
|
996
|
+
await this._ready;
|
|
997
|
+
if (pin !== 1 && pin !== 2) {
|
|
998
|
+
throw new Error('pin must be 1 or 2');
|
|
999
|
+
}
|
|
1000
|
+
this._intCallback = callback;
|
|
1001
|
+
const intPin = this._conn ? this._conn.intPin : null;
|
|
1002
|
+
if (intPin) {
|
|
1003
|
+
this._intHandler = () => { this._dispatchInt(); };
|
|
1004
|
+
await intPin.onEdge(this._intHandler, this._intActiveHigh ? 'rising' : 'falling');
|
|
1005
|
+
} else {
|
|
1006
|
+
this._startPollingFallback();
|
|
1007
|
+
}
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
/**
|
|
1011
|
+
* Unsubscribe from INT`pin` assertions.
|
|
1012
|
+
* @param {number} [pin=1] - 1 or 2.
|
|
1013
|
+
* @returns {Promise<void>}
|
|
1014
|
+
*/
|
|
1015
|
+
async offInterrupt(pin = 1) {
|
|
1016
|
+
this._intCallback = null;
|
|
1017
|
+
const intPin = this._conn ? this._conn.intPin : null;
|
|
1018
|
+
if (intPin && this._intHandler) {
|
|
1019
|
+
await intPin.offEdge(this._intHandler);
|
|
1020
|
+
}
|
|
1021
|
+
this._intHandler = null;
|
|
1022
|
+
if (this._pollTimer !== null) {
|
|
1023
|
+
clearInterval(this._pollTimer);
|
|
1024
|
+
this._pollTimer = null;
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
|
|
1028
|
+
/**
|
|
1029
|
+
* Read the source registers of every currently enabled source.
|
|
1030
|
+
*
|
|
1031
|
+
* **Reading `INT1_SRC` / `INT2_SRC` / `CLICK_SRC` clears their latched
|
|
1032
|
+
* flags** (and the INT pin).
|
|
1033
|
+
*
|
|
1034
|
+
* @returns {Promise<number>} Combined status int — bit 0 `SOURCE_DATA_READY`, bit 1 `SOURCE_AUX_DATA_READY`, bit 2 `SOURCE_WATERMARK`, bit 3 `SOURCE_OVERRUN`, bit 4 `SOURCE_IA1`, bit 5 `SOURCE_IA2`, bit 6 `SOURCE_CLICK`. Sources that are not enabled read as 0; `SOURCE_ACTIVITY` and `SOURCE_BOOT` have no register flag and are never reported.
|
|
1035
|
+
*/
|
|
1036
|
+
async pollInterrupt() {
|
|
1037
|
+
await this._ready;
|
|
1038
|
+
const e1 = this._enabled1;
|
|
1039
|
+
const e2 = this._enabled2;
|
|
1040
|
+
const enabled = e1 | e2;
|
|
1041
|
+
let status = 0;
|
|
1042
|
+
// INT1-only sources are routed through CTRL_REG3 (e1).
|
|
1043
|
+
if ((e1 & 0x01) && ((await this._readReg(_REG_STATUS)) & 0x08)) {
|
|
1044
|
+
status |= LIS3DHSource.SOURCE_DATA_READY;
|
|
1045
|
+
}
|
|
1046
|
+
if ((e1 & 0x02) && ((await this._readReg(_REG_STATUS_AUX)) & 0x08)) {
|
|
1047
|
+
status |= LIS3DHSource.SOURCE_AUX_DATA_READY;
|
|
1048
|
+
}
|
|
1049
|
+
if ((e1 & 0x04) || (e1 & 0x08)) {
|
|
1050
|
+
const fifo = await this._readReg(_REG_FIFO_SRC);
|
|
1051
|
+
if ((e1 & 0x04) && (fifo & 0x80)) {
|
|
1052
|
+
status |= LIS3DHSource.SOURCE_WATERMARK;
|
|
1053
|
+
}
|
|
1054
|
+
if ((e1 & 0x08) && (fifo & 0x40)) {
|
|
1055
|
+
status |= LIS3DHSource.SOURCE_OVERRUN;
|
|
1056
|
+
}
|
|
1057
|
+
}
|
|
1058
|
+
// Generator flags live in their own source register no matter which
|
|
1059
|
+
// pin the generator is routed to.
|
|
1060
|
+
if ((enabled & 0x10) && ((await this._readReg(_REG_INT1_SRC)) & 0x40)) {
|
|
1061
|
+
status |= LIS3DHSource.SOURCE_IA1;
|
|
1062
|
+
}
|
|
1063
|
+
if ((enabled & 0x20) && ((await this._readReg(_REG_INT2_SRC)) & 0x40)) {
|
|
1064
|
+
status |= LIS3DHSource.SOURCE_IA2;
|
|
1065
|
+
}
|
|
1066
|
+
if ((enabled & 0x40) && ((await this._readReg(_REG_CLICK_SRC)) & 0x40)) {
|
|
1067
|
+
status |= LIS3DHSource.SOURCE_CLICK;
|
|
1068
|
+
}
|
|
1069
|
+
return status;
|
|
1070
|
+
}
|
|
1071
|
+
|
|
1072
|
+
// ---- system ------------------------------------------------------------
|
|
1073
|
+
|
|
1074
|
+
/**
|
|
1075
|
+
* Reboot the memory content (`BOOT` = 1) and re-run the Initialization
|
|
1076
|
+
* Sequence.
|
|
1077
|
+
*
|
|
1078
|
+
* Reloads the factory trim and waits 5 ms. The datasheet does not say
|
|
1079
|
+
* whether the control registers survive, so the Initialization Sequence
|
|
1080
|
+
* is re-run afterwards — the configuration returns to the Minimal
|
|
1081
|
+
* defaults.
|
|
1082
|
+
*
|
|
1083
|
+
* @returns {Promise<void>}
|
|
1084
|
+
*/
|
|
1085
|
+
async reboot() {
|
|
1086
|
+
await this._ready;
|
|
1087
|
+
const ctrl5 = await this._readReg(_REG_CTRL_REG5);
|
|
1088
|
+
await this._writeReg(_REG_CTRL_REG5, ctrl5 | 0x80);
|
|
1089
|
+
await _sleep(5);
|
|
1090
|
+
this._rangeG = _DEFAULT_RANGE_G;
|
|
1091
|
+
this._mode = _DEFAULT_MODE;
|
|
1092
|
+
this._rateHz = _DEFAULT_RATE_HZ;
|
|
1093
|
+
this._enabled1 = 0;
|
|
1094
|
+
this._enabled2 = 0;
|
|
1095
|
+
await this._runInitSequence();
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
/**
|
|
1099
|
+
* Connect or disconnect the pull-up on SDO/SA0 (`CTRL_REG0`).
|
|
1100
|
+
*
|
|
1101
|
+
* Read-modify-write of bit 7 only; bit 4 is forced to 1 and the
|
|
1102
|
+
* reserved bits stay 0 (`CTRL_REG0` is not a normal register).
|
|
1103
|
+
*
|
|
1104
|
+
* @param {boolean} connected - True to connect the pull-up (`SDO_PU_DISC` = 0), false to disconnect it.
|
|
1105
|
+
* @returns {Promise<void>}
|
|
1106
|
+
*/
|
|
1107
|
+
async setSdoPullup(connected) {
|
|
1108
|
+
await this._ready;
|
|
1109
|
+
const value = 0x10 | (connected ? 0x00 : 0x80);
|
|
1110
|
+
await this._writeReg(_REG_CTRL_REG0, value);
|
|
1111
|
+
}
|
|
1112
|
+
|
|
1113
|
+
/**
|
|
1114
|
+
* Run the datasheet electrostatic self-test.
|
|
1115
|
+
*
|
|
1116
|
+
* Configures ±2 *g*, normal (10-bit) mode, 50 Hz with `BDU` on,
|
|
1117
|
+
* averages a few samples with `ST` = 00, enables `ST` (01 = self-test
|
|
1118
|
+
* 0, 10 = self-test 1), waits 2 samples and averages again. Every axis
|
|
1119
|
+
* must show a 17-360 LSb change (1 LSb = 4 mg at 10-bit / ±2 *g*). The
|
|
1120
|
+
* previous `CTRL_REG1` / `CTRL_REG4` configuration is restored
|
|
1121
|
+
* afterwards.
|
|
1122
|
+
*
|
|
1123
|
+
* @param {number} [mode=1] - 1 = self-test 0, 2 = self-test 1.
|
|
1124
|
+
* @returns {Promise<boolean>} True if every axis showed a change inside 17-360 LSb.
|
|
1125
|
+
* @throws {Error} If `mode` is not 1 or 2.
|
|
1126
|
+
*/
|
|
1127
|
+
async selfTest(mode = 1) {
|
|
1128
|
+
await this._ready;
|
|
1129
|
+
if (mode !== 1 && mode !== 2) {
|
|
1130
|
+
throw new Error('mode must be 1 (self-test 0) or 2 (self-test 1)');
|
|
1131
|
+
}
|
|
1132
|
+
const savedCtrl1 = await this._readReg(_REG_CTRL_REG1);
|
|
1133
|
+
const savedCtrl4 = await this._readReg(_REG_CTRL_REG4);
|
|
1134
|
+
const savedRange = this._rangeG;
|
|
1135
|
+
const savedMode = this._mode;
|
|
1136
|
+
const savedRate = this._rateHz;
|
|
1137
|
+
const sampleMs = 20; // 50 Hz
|
|
1138
|
+
const configure = async (stBits) => {
|
|
1139
|
+
await this._writeReg(_REG_CTRL_REG4, 0x80 | stBits);
|
|
1140
|
+
await this._writeReg(_REG_CTRL_REG1, 0x47); // 50 Hz, LPen=0, X/Y/Z
|
|
1141
|
+
this._rangeG = _DEFAULT_RANGE_G;
|
|
1142
|
+
this._mode = _MODE_NORMAL;
|
|
1143
|
+
this._rateHz = 50;
|
|
1144
|
+
};
|
|
1145
|
+
let before = [0, 0, 0];
|
|
1146
|
+
let after = [0, 0, 0];
|
|
1147
|
+
try {
|
|
1148
|
+
await configure(0x00);
|
|
1149
|
+
await _sleep(10);
|
|
1150
|
+
before = await this._averageRaw(5, sampleMs);
|
|
1151
|
+
await configure(mode === 1 ? 0x02 : 0x04);
|
|
1152
|
+
await _sleep(40); // 2 samples at 50 Hz
|
|
1153
|
+
after = await this._averageRaw(5, sampleMs);
|
|
1154
|
+
} finally {
|
|
1155
|
+
await this._writeReg(_REG_CTRL_REG1, savedCtrl1);
|
|
1156
|
+
await this._writeReg(_REG_CTRL_REG4, savedCtrl4);
|
|
1157
|
+
this._rangeG = savedRange;
|
|
1158
|
+
this._mode = savedMode;
|
|
1159
|
+
this._rateHz = savedRate;
|
|
1160
|
+
}
|
|
1161
|
+
for (let i = 0; i < 3; i++) {
|
|
1162
|
+
const delta = Math.abs(after[i] - before[i]);
|
|
1163
|
+
if (delta < 17 || delta > 360) return false;
|
|
1164
|
+
}
|
|
1165
|
+
return true;
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
// ---- internal helpers --------------------------------------------------
|
|
1169
|
+
|
|
1170
|
+
/**
|
|
1171
|
+
* Average `samples` raw readings, `sampleMs` apart.
|
|
1172
|
+
* @param {number} samples - Number of samples.
|
|
1173
|
+
* @param {number} sampleMs - Delay between samples in ms.
|
|
1174
|
+
* @returns {Promise<number[]>} Averaged (x, y, z) counts.
|
|
1175
|
+
* @private
|
|
1176
|
+
*/
|
|
1177
|
+
async _averageRaw(samples, sampleMs) {
|
|
1178
|
+
const total = [0, 0, 0];
|
|
1179
|
+
for (let i = 0; i < samples; i++) {
|
|
1180
|
+
const raw = await this.readRaw();
|
|
1181
|
+
total[0] += raw.x;
|
|
1182
|
+
total[1] += raw.y;
|
|
1183
|
+
total[2] += raw.z;
|
|
1184
|
+
await _sleep(sampleMs);
|
|
1185
|
+
}
|
|
1186
|
+
return [total[0] / samples, total[1] / samples, total[2] / samples];
|
|
1187
|
+
}
|
|
1188
|
+
|
|
1189
|
+
/**
|
|
1190
|
+
* Register addresses of an inertial interrupt generator.
|
|
1191
|
+
* @param {number} generator - 1 or 2.
|
|
1192
|
+
* @returns {{cfg: number, ths: number, dur: number}} The register addresses.
|
|
1193
|
+
* @private
|
|
1194
|
+
* @throws {Error} If `generator` is not 1 or 2.
|
|
1195
|
+
*/
|
|
1196
|
+
_generatorRegs(generator) {
|
|
1197
|
+
if (generator === 1) {
|
|
1198
|
+
return {cfg: _REG_INT1_CFG, ths: _REG_INT1_THS, dur: _REG_INT1_DUR};
|
|
1199
|
+
}
|
|
1200
|
+
if (generator === 2) {
|
|
1201
|
+
return {cfg: _REG_INT2_CFG, ths: _REG_INT2_THS, dur: _REG_INT2_DUR};
|
|
1202
|
+
}
|
|
1203
|
+
throw new Error('generator must be 1 or 2');
|
|
1204
|
+
}
|
|
1205
|
+
|
|
1206
|
+
/**
|
|
1207
|
+
* Convert a threshold in *g* into the 7-bit `INTx_THS` / `ACT_THS` /
|
|
1208
|
+
* `CLICK_THS` code of the current full-scale range.
|
|
1209
|
+
* @param {number} thresholdG - Threshold in *g*.
|
|
1210
|
+
* @returns {number} The 7-bit register code.
|
|
1211
|
+
* @private
|
|
1212
|
+
*/
|
|
1213
|
+
_thresholdCode(thresholdG) {
|
|
1214
|
+
const lsbMg = _THRESHOLD_LSB_MG[this._rangeG];
|
|
1215
|
+
const code = _round(thresholdG * 1000.0 / lsbMg);
|
|
1216
|
+
return Math.max(0, Math.min(127, code));
|
|
1217
|
+
}
|
|
1218
|
+
|
|
1219
|
+
/**
|
|
1220
|
+
* Convert a duration in ms into the `INTx_DURATION` / `TIME_*` code
|
|
1221
|
+
* (1 LSb = 1/ODR).
|
|
1222
|
+
* @param {number} durationMs - Duration in ms.
|
|
1223
|
+
* @param {number} [limit=127] - Maximum register code.
|
|
1224
|
+
* @returns {number} The register code.
|
|
1225
|
+
* @private
|
|
1226
|
+
*/
|
|
1227
|
+
_durationCode(durationMs, limit = 127) {
|
|
1228
|
+
// 1 LSb = 1/ODR; while powered down there is no ODR to convert
|
|
1229
|
+
// with, so the code is 0.
|
|
1230
|
+
if (this._rateHz <= 0) return 0;
|
|
1231
|
+
const code = _round(durationMs * this._rateHz / 1000.0);
|
|
1232
|
+
return Math.max(0, Math.min(limit, code));
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
/**
|
|
1236
|
+
* Bit index of an interrupt source constant.
|
|
1237
|
+
* @param {number} source - One of the {@link LIS3DHSource} constants.
|
|
1238
|
+
* @returns {number} The bit index (0-8).
|
|
1239
|
+
* @private
|
|
1240
|
+
*/
|
|
1241
|
+
_sourceBit(source) {
|
|
1242
|
+
let bit = 0;
|
|
1243
|
+
let value = source;
|
|
1244
|
+
while (value > 1) {
|
|
1245
|
+
value >>= 1;
|
|
1246
|
+
bit++;
|
|
1247
|
+
}
|
|
1248
|
+
return bit;
|
|
1249
|
+
}
|
|
1250
|
+
|
|
1251
|
+
/**
|
|
1252
|
+
* Routing information of an interrupt source.
|
|
1253
|
+
* @param {number} source - One of the {@link LIS3DHSource} constants.
|
|
1254
|
+
* @param {number} pin - 1 or 2.
|
|
1255
|
+
* @returns {{reg: number, mask: number, track: number}} The routing register, its bit mask and the tracked pin (1 or 2).
|
|
1256
|
+
* @private
|
|
1257
|
+
* @throws {Error} If `source` is unknown or `pin` is not 1 or 2.
|
|
1258
|
+
*/
|
|
1259
|
+
_route(source, pin) {
|
|
1260
|
+
if (pin !== 1 && pin !== 2) {
|
|
1261
|
+
throw new Error('pin must be 1 or 2');
|
|
1262
|
+
}
|
|
1263
|
+
const entry = _ROUTE[source];
|
|
1264
|
+
if (entry === undefined) {
|
|
1265
|
+
throw new Error('unknown interrupt source: ' + _hex4(source));
|
|
1266
|
+
}
|
|
1267
|
+
if (entry.kind === 'int1') {
|
|
1268
|
+
return {reg: _REG_CTRL_REG3, mask: entry.mask, track: 1};
|
|
1269
|
+
}
|
|
1270
|
+
if (entry.kind === 'int2') {
|
|
1271
|
+
return {reg: _REG_CTRL_REG6, mask: entry.mask, track: 2};
|
|
1272
|
+
}
|
|
1273
|
+
if (pin === 1) {
|
|
1274
|
+
return {reg: _REG_CTRL_REG3, mask: entry.mask, track: 1};
|
|
1275
|
+
}
|
|
1276
|
+
return {reg: _REG_CTRL_REG6, mask: entry.mask, track: 2};
|
|
1277
|
+
}
|
|
1278
|
+
|
|
1279
|
+
/**
|
|
1280
|
+
* Read the interrupt sources and hand the combined status to the
|
|
1281
|
+
* registered callback.
|
|
1282
|
+
* @returns {Promise<void>}
|
|
1283
|
+
* @private
|
|
1284
|
+
*/
|
|
1285
|
+
async _dispatchInt() {
|
|
1286
|
+
const callback = this._intCallback;
|
|
1287
|
+
if (callback === null || callback === undefined) return;
|
|
1288
|
+
try {
|
|
1289
|
+
const status = await this.pollInterrupt();
|
|
1290
|
+
if (status) callback(status);
|
|
1291
|
+
} catch (e) {
|
|
1292
|
+
// A failing bus read must not kill the event loop.
|
|
1293
|
+
}
|
|
1294
|
+
}
|
|
1295
|
+
|
|
1296
|
+
/**
|
|
1297
|
+
* Start the 5 ms polling fallback used when no INT pin is wired.
|
|
1298
|
+
* @private
|
|
1299
|
+
*/
|
|
1300
|
+
_startPollingFallback() {
|
|
1301
|
+
if (this._pollTimer !== null) return;
|
|
1302
|
+
let busy = false;
|
|
1303
|
+
this._pollTimer = setInterval(async () => {
|
|
1304
|
+
if (busy) return;
|
|
1305
|
+
busy = true;
|
|
1306
|
+
try {
|
|
1307
|
+
await this._dispatchInt();
|
|
1308
|
+
} finally {
|
|
1309
|
+
busy = false;
|
|
1310
|
+
}
|
|
1311
|
+
}, 5);
|
|
1312
|
+
}
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1315
|
+
// Source routing: 'int1' (INT1 only), 'int2' (INT2 only), 'both'.
|
|
1316
|
+
// The value is the bit in CTRL_REG3 (INT1) / CTRL_REG6 (INT2).
|
|
1317
|
+
const _ROUTE = {
|
|
1318
|
+
[LIS3DHSource.SOURCE_DATA_READY]: {kind: 'int1', mask: 0x10},
|
|
1319
|
+
[LIS3DHSource.SOURCE_AUX_DATA_READY]: {kind: 'int1', mask: 0x08},
|
|
1320
|
+
[LIS3DHSource.SOURCE_WATERMARK]: {kind: 'int1', mask: 0x04},
|
|
1321
|
+
[LIS3DHSource.SOURCE_OVERRUN]: {kind: 'int1', mask: 0x02},
|
|
1322
|
+
[LIS3DHSource.SOURCE_IA1]: {kind: 'both', mask: 0x40},
|
|
1323
|
+
[LIS3DHSource.SOURCE_IA2]: {kind: 'both', mask: 0x20},
|
|
1324
|
+
[LIS3DHSource.SOURCE_CLICK]: {kind: 'both', mask: 0x80},
|
|
1325
|
+
[LIS3DHSource.SOURCE_ACTIVITY]: {kind: 'int2', mask: 0x08},
|
|
1326
|
+
[LIS3DHSource.SOURCE_BOOT]: {kind: 'int2', mask: 0x10},
|
|
1327
|
+
};
|
|
1328
|
+
|
|
1329
|
+
// Convenience aliases so the source constants are reachable from the Full
|
|
1330
|
+
// driver as well.
|
|
1331
|
+
LIS3DHFull.SOURCE_DATA_READY = LIS3DHSource.SOURCE_DATA_READY;
|
|
1332
|
+
LIS3DHFull.SOURCE_AUX_DATA_READY = LIS3DHSource.SOURCE_AUX_DATA_READY;
|
|
1333
|
+
LIS3DHFull.SOURCE_WATERMARK = LIS3DHSource.SOURCE_WATERMARK;
|
|
1334
|
+
LIS3DHFull.SOURCE_OVERRUN = LIS3DHSource.SOURCE_OVERRUN;
|
|
1335
|
+
LIS3DHFull.SOURCE_IA1 = LIS3DHSource.SOURCE_IA1;
|
|
1336
|
+
LIS3DHFull.SOURCE_IA2 = LIS3DHSource.SOURCE_IA2;
|
|
1337
|
+
LIS3DHFull.SOURCE_CLICK = LIS3DHSource.SOURCE_CLICK;
|
|
1338
|
+
LIS3DHFull.SOURCE_ACTIVITY = LIS3DHSource.SOURCE_ACTIVITY;
|
|
1339
|
+
LIS3DHFull.SOURCE_BOOT = LIS3DHSource.SOURCE_BOOT;
|
|
1340
|
+
|
|
1341
|
+
module.exports = { LIS3DHMinimal, LIS3DHFull, LIS3DHSource };
|