periph 1.3.2 → 1.4.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.
Files changed (114) hide show
  1. package/README.md +27 -0
  2. package/examples/accelerometer/bma180/complete.js +83 -0
  3. package/examples/accelerometer/bma180/demo.js +47 -0
  4. package/examples/accelerometer/bma180/minimal.js +21 -0
  5. package/examples/accelerometer/fxos8700/complete.js +111 -0
  6. package/examples/accelerometer/fxos8700/demo.js +80 -0
  7. package/examples/accelerometer/fxos8700/minimal.js +20 -0
  8. package/examples/accelerometer/lis3dh/complete.js +123 -0
  9. package/examples/accelerometer/lis3dh/demo.js +99 -0
  10. package/examples/accelerometer/lis3dh/minimal.js +19 -0
  11. package/examples/accelerometer/lsm303/complete.js +82 -0
  12. package/examples/accelerometer/lsm303/demo.js +96 -0
  13. package/examples/accelerometer/lsm303/minimal.js +23 -0
  14. package/examples/accelerometer/mma8451q/complete.js +96 -0
  15. package/examples/accelerometer/mma8451q/demo.js +120 -0
  16. package/examples/accelerometer/mma8451q/minimal.js +20 -0
  17. package/examples/adc_dac/ad5243/complete.js +52 -0
  18. package/examples/adc_dac/ad5243/demo.js +50 -0
  19. package/examples/adc_dac/ad5243/minimal.js +23 -0
  20. package/examples/adc_dac/ad5248/complete.js +49 -0
  21. package/examples/adc_dac/ad5248/demo.js +42 -0
  22. package/examples/adc_dac/ad5248/minimal.js +21 -0
  23. package/examples/adc_dac/ads1013/complete.js +51 -0
  24. package/examples/adc_dac/ads1013/demo.js +51 -0
  25. package/examples/adc_dac/ads1013/minimal.js +21 -0
  26. package/examples/adc_dac/ads1014/complete.js +88 -0
  27. package/examples/adc_dac/ads1014/demo.js +52 -0
  28. package/examples/adc_dac/ads1014/minimal.js +21 -0
  29. package/examples/adc_dac/ads1113/complete.js +47 -0
  30. package/examples/adc_dac/ads1113/demo.js +39 -0
  31. package/examples/adc_dac/ads1113/minimal.js +20 -0
  32. package/examples/adc_dac/ads1114/complete.js +77 -0
  33. package/examples/adc_dac/ads1114/demo.js +47 -0
  34. package/examples/adc_dac/ads1114/minimal.js +20 -0
  35. package/examples/adc_dac/ads1115/complete.js +82 -0
  36. package/examples/adc_dac/ads1115/demo.js +55 -0
  37. package/examples/adc_dac/ads1115/minimal.js +20 -0
  38. package/examples/adc_dac/es8311/complete.js +80 -0
  39. package/examples/adc_dac/es8311/demo.js +59 -0
  40. package/examples/adc_dac/es8311/minimal.js +18 -0
  41. package/examples/adc_dac/mcp4xxx/complete.js +97 -0
  42. package/examples/adc_dac/mcp4xxx/demo.js +76 -0
  43. package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
  44. package/examples/display/pcf8578/complete.js +58 -0
  45. package/examples/display/pcf8578/demo.js +62 -0
  46. package/examples/display/pcf8578/minimal.js +19 -0
  47. package/examples/display/pcf8579/complete.js +58 -0
  48. package/examples/display/pcf8579/demo.js +61 -0
  49. package/examples/display/pcf8579/minimal.js +19 -0
  50. package/examples/imu/icm20948/complete.js +114 -0
  51. package/examples/imu/icm20948/demo.js +79 -0
  52. package/examples/imu/icm20948/minimal.js +22 -0
  53. package/examples/imu/qmi8658a/complete.js +134 -0
  54. package/examples/imu/qmi8658a/demo.js +81 -0
  55. package/examples/imu/qmi8658a/minimal.js +22 -0
  56. package/examples/io_expander/mcp23008/complete.js +70 -0
  57. package/examples/io_expander/mcp23008/demo.js +58 -0
  58. package/examples/io_expander/mcp23008/minimal.js +20 -0
  59. package/examples/memory/mb85rc/complete.js +40 -0
  60. package/examples/memory/mb85rc/demo.js +58 -0
  61. package/examples/memory/mb85rc/minimal.js +15 -0
  62. package/examples/motor/drv2605/complete.js +100 -0
  63. package/examples/motor/drv2605/demo.js +55 -0
  64. package/examples/motor/drv2605/minimal.js +24 -0
  65. package/examples/motor/tmc2130/complete.js +138 -0
  66. package/examples/motor/tmc2130/demo.js +96 -0
  67. package/examples/motor/tmc2130/minimal.js +30 -0
  68. package/examples/motor/tmc2208/complete.js +92 -0
  69. package/examples/motor/tmc2208/demo.js +85 -0
  70. package/examples/motor/tmc2208/minimal.js +25 -0
  71. package/examples/motor/tmc2209/complete.js +106 -0
  72. package/examples/motor/tmc2209/demo.js +97 -0
  73. package/examples/motor/tmc2209/minimal.js +25 -0
  74. package/examples/motor/tmc5160/complete.js +205 -0
  75. package/examples/motor/tmc5160/demo.js +104 -0
  76. package/examples/motor/tmc5160/minimal.js +35 -0
  77. package/examples/power/axp2101/complete.js +135 -0
  78. package/examples/power/axp2101/demo.js +68 -0
  79. package/examples/power/axp2101/minimal.js +24 -0
  80. package/examples/rtc/pcf85063a/complete.js +68 -0
  81. package/examples/rtc/pcf85063a/demo.js +76 -0
  82. package/examples/rtc/pcf85063a/minimal.js +20 -0
  83. package/package.json +1 -1
  84. package/src/chips/accelerometer/bma180.js +573 -0
  85. package/src/chips/accelerometer/fxos8700.js +1600 -0
  86. package/src/chips/accelerometer/lis3dh.js +1341 -0
  87. package/src/chips/accelerometer/lsm303.js +1471 -0
  88. package/src/chips/accelerometer/mma8451q.js +1406 -0
  89. package/src/chips/adc_dac/_ads111x_base.js +272 -0
  90. package/src/chips/adc_dac/ad5243.js +231 -0
  91. package/src/chips/adc_dac/ad5248.js +194 -0
  92. package/src/chips/adc_dac/ads1013.js +219 -0
  93. package/src/chips/adc_dac/ads1014.js +509 -0
  94. package/src/chips/adc_dac/ads1113.js +112 -0
  95. package/src/chips/adc_dac/ads1114.js +201 -0
  96. package/src/chips/adc_dac/ads1115.js +232 -0
  97. package/src/chips/adc_dac/es8311.js +621 -0
  98. package/src/chips/adc_dac/mcp4xxx.js +982 -0
  99. package/src/chips/display/pcf8578.js +452 -0
  100. package/src/chips/display/pcf8579.js +451 -0
  101. package/src/chips/imu/icm20948.js +849 -0
  102. package/src/chips/imu/qmi8658a.js +1011 -0
  103. package/src/chips/io_expander/mcp23008.js +558 -0
  104. package/src/chips/memory/mb85rc.js +166 -0
  105. package/src/chips/motor/drv2605.js +592 -0
  106. package/src/chips/motor/tmc2130.js +537 -0
  107. package/src/chips/motor/tmc2208.js +490 -0
  108. package/src/chips/motor/tmc2209.js +565 -0
  109. package/src/chips/motor/tmc5160.js +900 -0
  110. package/src/chips/power/axp2101.js +968 -0
  111. package/src/chips/rtc/pcf85063a.js +482 -0
  112. package/src/connection/uart.js +9 -0
  113. package/src/connection/uart_mock.js +65 -0
  114. package/src/discovery/registry.js +45 -19
@@ -0,0 +1,1011 @@
1
+ 'use strict';
2
+
3
+ const _REG_WHO_AM_I = 0x00;
4
+ const _REG_REVISION_ID = 0x01;
5
+ const _REG_CTRL1 = 0x02;
6
+ const _REG_CTRL2 = 0x03;
7
+ const _REG_CTRL3 = 0x04;
8
+ const _REG_CTRL5 = 0x06;
9
+ const _REG_CTRL7 = 0x08;
10
+ const _REG_CTRL8 = 0x09;
11
+ const _REG_CTRL9 = 0x0A;
12
+ const _REG_CAL1_L = 0x0B;
13
+ const _REG_FIFO_WTM_TH = 0x13;
14
+ const _REG_FIFO_CTRL = 0x14;
15
+ const _REG_FIFO_SMPL_CNT = 0x15;
16
+ const _REG_FIFO_STATUS = 0x16;
17
+ const _REG_FIFO_DATA = 0x17;
18
+ const _REG_STATUSINT = 0x2D;
19
+ const _REG_STATUS0 = 0x2E;
20
+ const _REG_TIMESTAMP = 0x30;
21
+ const _REG_TEMP_L = 0x33;
22
+ const _REG_AX_L = 0x35;
23
+ const _REG_GX_L = 0x3B;
24
+ const _REG_COD_STATUS = 0x46;
25
+ const _REG_FW_VERSION = 0x49;
26
+ const _REG_RESET_STATUS = 0x4D;
27
+ const _REG_DVX_L = 0x51;
28
+ const _REG_TAP_STATUS = 0x59;
29
+ const _REG_STEP_CNT = 0x5A;
30
+ const _REG_RESET = 0x60;
31
+
32
+ const _WHO_AM_I_VALUE = 0x05;
33
+ const _RESET_CMD = 0xB0;
34
+
35
+ const _CMD_ACK = 0x00;
36
+ const _CMD_RST_FIFO = 0x04;
37
+ const _CMD_REQ_FIFO = 0x05;
38
+ const _CMD_WRITE_WOM = 0x08;
39
+ const _CMD_ACCEL_OFFSET = 0x09;
40
+ const _CMD_GYRO_OFFSET = 0x0A;
41
+ const _CMD_CONFIG_TAP = 0x0C;
42
+ const _CMD_CONFIG_PEDOMETER = 0x0D;
43
+ const _CMD_CONFIG_MOTION = 0x0E;
44
+ const _CMD_RESET_PEDOMETER = 0x0F;
45
+ const _CMD_COPY_USID = 0x10;
46
+ const _CMD_SET_RPU = 0x11;
47
+ const _CMD_AHB_CLOCK_GATING = 0x12;
48
+ const _CMD_COD = 0xA2;
49
+ const _CMD_APPLY_GYRO_GAINS = 0xAA;
50
+
51
+ const _G = 9.80665;
52
+ const _DEG_TO_RAD = Math.PI / 180.0;
53
+
54
+ const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
55
+ const _round = (v) => Math.round(v);
56
+ const _clampU8 = (v) => Math.max(0, Math.min(255, v));
57
+
58
+ /**
59
+ * Interrupt source bit masks used by `QMI8658AFull` (`pollInterrupt()` return value
60
+ * and the argument of `enableInterrupt()` / `disableInterrupt()`).
61
+ */
62
+ const QMI8658ASource = {
63
+ DATA_READY: 0x0001,
64
+ FIFO_WATERMARK: 0x0002,
65
+ FIFO_FULL: 0x0004,
66
+ ANY_MOTION: 0x0008,
67
+ NO_MOTION: 0x0010,
68
+ SIGNIFICANT_MOTION: 0x0020,
69
+ STEP: 0x0040,
70
+ TAP: 0x0080,
71
+ WAKE_ON_MOTION: 0x0100,
72
+ };
73
+
74
+ const _ENGINE_BIT = {
75
+ [QMI8658ASource.ANY_MOTION]: 0x02,
76
+ [QMI8658ASource.NO_MOTION]: 0x04,
77
+ [QMI8658ASource.SIGNIFICANT_MOTION]: 0x08,
78
+ [QMI8658ASource.STEP]: 0x10,
79
+ [QMI8658ASource.TAP]: 0x01,
80
+ };
81
+
82
+ /**
83
+ * QMI8658A 6-axis IMU (QST) — minimal interface.
84
+ *
85
+ * Provides 3-axis acceleration and 3-axis angular rate with no configuration
86
+ * beyond the connection. Runs the datasheet initialisation sequence: soft
87
+ * reset, `WHO_AM_I` check, address auto-increment on, little-endian reads,
88
+ * CTRL9 handshake by polling, both sensors on.
89
+ *
90
+ * Default configuration (written at construction):
91
+ * - Accelerometer ±2 g, gyroscope ±256 dps
92
+ * - Both output data rates code 5 (224.2 Hz in 6DOF mode)
93
+ * - Both low-pass filters on, mode 3 (13.37 % of ODR)
94
+ * - FIFO bypass (data-ready active), all INT pins high-Z, engines off
95
+ *
96
+ * Constructor caveat: JS constructors cannot be async, so the initialisation
97
+ * sequence — including the `WHO_AM_I` identity check — starts in the
98
+ * constructor and every public method awaits it first. A wrong or absent
99
+ * device therefore rejects the first call made on the driver; call
100
+ * `await imu.init()` right after construction to surface it at a predictable
101
+ * point.
102
+ */
103
+ class QMI8658AMinimal {
104
+ /**
105
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C (0x6B with SA0 = GND, 0x6A with SA0 = VDDIO), SMBus or SPI (read bit 0x80) register connection.
106
+ */
107
+ constructor(connection) {
108
+ this._conn = connection;
109
+ this._ctrl1 = 0x40;
110
+ this._ctrl7 = 0x03;
111
+ this._ctrl8 = 0x80;
112
+ this._accelFs = 0;
113
+ this._gyroFs = 4;
114
+ this._accelOdr = 5;
115
+ this._gyroOdr = 5;
116
+ this._accelSens = 16384;
117
+ this._gyroSens = 128;
118
+ this._fifoCtrl = 0;
119
+ this._fifoWtm = 0;
120
+ this._sync = false;
121
+ this._ready = this._runInitSequence();
122
+ // Re-raised by every public method; mark handled so it never surfaces on its own.
123
+ this._ready.catch(() => {});
124
+ }
125
+
126
+ /**
127
+ * Wait for the initialisation sequence started by the constructor.
128
+ * @returns {Promise<void>}
129
+ * @throws {Error} If `WHO_AM_I` is not 0x05 (wrong chip, wrong address, or wiring problem).
130
+ */
131
+ async init() {
132
+ await this._ready;
133
+ }
134
+
135
+ async _runInitSequence() {
136
+ await _sleep(15);
137
+ await this._conn.writeReg(_REG_RESET, _RESET_CMD);
138
+ await _sleep(15);
139
+ await this._conn.readReg(_REG_RESET_STATUS, 1); // 0x80 after reset, informational
140
+ const who = (await this._conn.readReg(_REG_WHO_AM_I, 1))[0];
141
+ if (who !== _WHO_AM_I_VALUE) {
142
+ throw new Error('QMI8658A WHO_AM_I: expected 0x' + _WHO_AM_I_VALUE.toString(16).padStart(2, '0') +
143
+ ', got 0x' + who.toString(16).padStart(2, '0'));
144
+ }
145
+ this._ctrl1 = 0x40; this._ctrl7 = 0x03; this._ctrl8 = 0x80;
146
+ this._fifoCtrl = 0; this._fifoWtm = 0; this._sync = false;
147
+ await this._conn.writeReg(_REG_CTRL1, this._ctrl1); // ADDR_AI = 1, BE = 0
148
+ await this._conn.writeReg(_REG_CTRL8, this._ctrl8); // handshake by polling
149
+ this._accelFs = 0; this._gyroFs = 4; this._accelOdr = 5; this._gyroOdr = 5;
150
+ this._updateSens();
151
+ await this._conn.writeReg(_REG_CTRL2, 0x05);
152
+ await this._conn.writeReg(_REG_CTRL3, 0x45);
153
+ await this._conn.writeReg(_REG_CTRL5, 0x77);
154
+ await this._conn.writeReg(_REG_CTRL7, this._ctrl7);
155
+ await _sleep(165); // gyroscope turn-on
156
+ }
157
+
158
+ _updateSens() {
159
+ this._accelSens = 16384 >> this._accelFs;
160
+ this._gyroSens = 2048 >> this._gyroFs;
161
+ }
162
+
163
+ async _readData(reg, length) {
164
+ return this._conn.readReg(reg, length);
165
+ }
166
+
167
+ async _vec(reg, sens, scale) {
168
+ const b = await this._readData(reg, 6);
169
+ return [0, 2, 4].map((o) => b.readInt16LE(o) / sens * scale);
170
+ }
171
+
172
+ /**
173
+ * Read 3-axis linear acceleration.
174
+ * @returns {Promise<number[]>} `[x, y, z]` in m/s².
175
+ */
176
+ async accel() {
177
+ await this._ready;
178
+ return this._vec(_REG_AX_L, this._accelSens, _G);
179
+ }
180
+
181
+ /**
182
+ * Read 3-axis angular rate.
183
+ * @returns {Promise<number[]>} `[x, y, z]` in rad/s.
184
+ */
185
+ async gyro() {
186
+ await this._ready;
187
+ return this._vec(_REG_GX_L, this._gyroSens, _DEG_TO_RAD);
188
+ }
189
+ }
190
+
191
+ /**
192
+ * QMI8658A full interface — extends `QMI8658AMinimal`.
193
+ *
194
+ * Adds raw and burst reads, temperature, sample counter, full-scale / ODR /
195
+ * filter configuration, power control, SyncSample data locking, the FIFO, the
196
+ * motion engines (any / no / significant motion, tap, pedometer,
197
+ * wake-on-motion), offsets, calibration-on-demand, self-test, device info,
198
+ * pull-up control and interrupts.
199
+ */
200
+ class QMI8658AFull extends QMI8658AMinimal {
201
+ /**
202
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C, SMBus or SPI register connection.
203
+ */
204
+ constructor(connection) {
205
+ super(connection);
206
+ this._any = { thr: 0.25, win: 3, axes: 0x07, and: false };
207
+ this._no = { thr: 0.125, win: 30, axes: 0x07, and: true };
208
+ this._sig = { wait: 150, confirm: 600 };
209
+ this._intSources = 0;
210
+ this._fifoPin = 2;
211
+ this._activityPin = 2;
212
+ this._womPin = 1;
213
+ this._intCallback = null;
214
+ this._intHandler = null;
215
+ this._pollTimer = null;
216
+ }
217
+
218
+ async _w(reg, value) {
219
+ await this._conn.writeReg(reg, value);
220
+ }
221
+
222
+ async _r1(reg) {
223
+ return (await this._conn.readReg(reg, 1))[0];
224
+ }
225
+
226
+ async _setCtrl1(v) { this._ctrl1 = v & 0xFF; await this._w(_REG_CTRL1, this._ctrl1); }
227
+ async _setCtrl7(v) { this._ctrl7 = v & 0xFF; await this._w(_REG_CTRL7, this._ctrl7); }
228
+ async _setCtrl8(v) { this._ctrl8 = v & 0xFF; await this._w(_REG_CTRL8, this._ctrl8); }
229
+
230
+ async _waitStatus(mask, want, timeoutMs, what) {
231
+ const deadline = Date.now() + timeoutMs;
232
+ for (;;) {
233
+ const st = await this._r1(_REG_STATUSINT);
234
+ if (((st & mask) !== 0) === want) return st;
235
+ if (Date.now() > deadline) throw new Error('QMI8658A timeout waiting for ' + what);
236
+ await _sleep(1);
237
+ }
238
+ }
239
+
240
+ async _ctrl9(cmd, timeoutMs = 100) {
241
+ await this._w(_REG_CTRL9, cmd);
242
+ await this._waitStatus(0x80, true, timeoutMs, 'CTRL9 CmdDone');
243
+ await this._w(_REG_CTRL9, _CMD_ACK);
244
+ await this._waitStatus(0x80, false, timeoutMs, 'CTRL9 ack');
245
+ }
246
+
247
+ async _calCmd(cmd, params, timeoutMs = 100) {
248
+ for (let i = 0; i < 8; i++) await this._w(_REG_CAL1_L + i, (params[i] || 0) & 0xFF);
249
+ await this._ctrl9(cmd, timeoutMs);
250
+ }
251
+
252
+ async _twoSets(cmd, set1, set2) {
253
+ await this._calCmd(cmd, [...set1, ...new Array(7 - set1.length).fill(0), 0x10]);
254
+ await this._calCmd(cmd, [...set2, ...new Array(7 - set2.length).fill(0), 0x20]);
255
+ }
256
+
257
+ async _sensorsOff() {
258
+ const prev = this._ctrl7 & 0x03;
259
+ if (prev) {
260
+ await this._setCtrl7(this._ctrl7 & ~0x03);
261
+ await _sleep(5);
262
+ }
263
+ return prev;
264
+ }
265
+
266
+ async _sensorsRestore(prev) {
267
+ if (prev) {
268
+ await this._setCtrl7(this._ctrl7 | prev);
269
+ await _sleep((prev & 0x02) ? 165 : 10);
270
+ }
271
+ }
272
+
273
+ async _readData(reg, length) {
274
+ if (this._sync) {
275
+ await this._waitStatus(0x01, true, 100, 'SyncSample Avail');
276
+ await this._waitStatus(0x02, true, 100, 'SyncSample Locked');
277
+ }
278
+ return this._conn.readReg(reg, length);
279
+ }
280
+
281
+ /**
282
+ * Read raw accelerometer counts.
283
+ * @returns {Promise<number[]>} Signed 16-bit `[x, y, z]`.
284
+ */
285
+ async accelRaw() {
286
+ await this._ready;
287
+ const b = await this._readData(_REG_AX_L, 6);
288
+ return [b.readInt16LE(0), b.readInt16LE(2), b.readInt16LE(4)];
289
+ }
290
+
291
+ /**
292
+ * Read raw gyroscope counts.
293
+ * @returns {Promise<number[]>} Signed 16-bit `[x, y, z]`.
294
+ */
295
+ async gyroRaw() {
296
+ await this._ready;
297
+ const b = await this._readData(_REG_GX_L, 6);
298
+ return [b.readInt16LE(0), b.readInt16LE(2), b.readInt16LE(4)];
299
+ }
300
+
301
+ /**
302
+ * Burst-read temperature, acceleration and angular rate in one sample.
303
+ * @returns {Promise<Array>} `[[ax, ay, az], [gx, gy, gz], tempC]` — m/s², rad/s, °C.
304
+ */
305
+ async readAll() {
306
+ await this._ready;
307
+ const b = await this._readData(_REG_TEMP_L, 14);
308
+ const accel = [2, 4, 6].map((o) => b.readInt16LE(o) / this._accelSens * _G);
309
+ const gyro = [8, 10, 12].map((o) => b.readInt16LE(o) / this._gyroSens * _DEG_TO_RAD);
310
+ return [accel, gyro, b.readInt16LE(0) / 256.0];
311
+ }
312
+
313
+ /**
314
+ * Read the die temperature.
315
+ * @returns {Promise<number>} Temperature in °C.
316
+ */
317
+ async temperature() {
318
+ await this._ready;
319
+ const b = await this._readData(_REG_TEMP_L, 2);
320
+ return b.readInt16LE(0) / 256.0;
321
+ }
322
+
323
+ /**
324
+ * Read the 24-bit sample counter.
325
+ * @returns {Promise<number>} Counter value, wraps at 0xFFFFFF.
326
+ */
327
+ async timestamp() {
328
+ await this._ready;
329
+ const b = await this._conn.readReg(_REG_TIMESTAMP, 3);
330
+ return b[0] | (b[1] << 8) | (b[2] << 16);
331
+ }
332
+
333
+ /**
334
+ * Check for new sensor data (`STATUS0` aDA / gDA, `STATUSINT.Avail` while SyncSample is on).
335
+ * @returns {Promise<boolean>}
336
+ */
337
+ async dataReady() {
338
+ await this._ready;
339
+ if (this._sync) return (await this._r1(_REG_STATUSINT) & 0x01) !== 0;
340
+ return (await this._r1(_REG_STATUS0) & 0x03) !== 0;
341
+ }
342
+
343
+ /**
344
+ * Configure the accelerometer (CTRL2).
345
+ * @param {number} [fullScale=0] - 0 = ±2 g, 1 = ±4 g, 2 = ±8 g, 3 = ±16 g.
346
+ * @param {number} [odr=5] - ODR code 0–8 or 12–15 (12–15 only with the gyroscope off).
347
+ * @returns {Promise<void>}
348
+ */
349
+ async configureAccel(fullScale = 0, odr = 5) {
350
+ await this._ready;
351
+ if (!(fullScale >= 0 && fullScale <= 3)) throw new RangeError('fullScale must be 0-3');
352
+ if (!((odr >= 0 && odr <= 8) || (odr >= 12 && odr <= 15))) throw new RangeError('odr must be 0-8 or 12-15');
353
+ this._accelFs = fullScale;
354
+ this._accelOdr = odr;
355
+ this._updateSens();
356
+ await this._w(_REG_CTRL2, (fullScale << 4) | odr);
357
+ }
358
+
359
+ /**
360
+ * Configure the gyroscope (CTRL3).
361
+ * @param {number} [fullScale=4] - 0 = ±16 dps … 7 = ±2048 dps (doubling).
362
+ * @param {number} [odr=5] - ODR code 0–8.
363
+ * @returns {Promise<void>}
364
+ */
365
+ async configureGyro(fullScale = 4, odr = 5) {
366
+ await this._ready;
367
+ if (!(fullScale >= 0 && fullScale <= 7)) throw new RangeError('fullScale must be 0-7');
368
+ if (!(odr >= 0 && odr <= 8)) throw new RangeError('odr must be 0-8');
369
+ this._gyroFs = fullScale;
370
+ this._gyroOdr = odr;
371
+ this._updateSens();
372
+ await this._w(_REG_CTRL3, (fullScale << 4) | odr);
373
+ }
374
+
375
+ /**
376
+ * Configure the low-pass filters (CTRL5).
377
+ * @param {boolean} [accelFilter=true] - Enable the accelerometer filter.
378
+ * @param {number} [accelMode=3] - 0–3 = 2.66 / 3.63 / 5.39 / 13.37 % of ODR.
379
+ * @param {boolean} [gyroFilter=true] - Enable the gyroscope filter.
380
+ * @param {number} [gyroMode=3] - 0–3, same bandwidths.
381
+ * @returns {Promise<void>}
382
+ */
383
+ async configureFilters(accelFilter = true, accelMode = 3, gyroFilter = true, gyroMode = 3) {
384
+ await this._ready;
385
+ if (!(accelMode >= 0 && accelMode <= 3 && gyroMode >= 0 && gyroMode <= 3)) {
386
+ throw new RangeError('filter mode must be 0-3');
387
+ }
388
+ await this._w(_REG_CTRL5, ((gyroMode & 3) << 5) | (gyroFilter ? 0x10 : 0) |
389
+ ((accelMode & 3) << 1) | (accelFilter ? 1 : 0));
390
+ }
391
+
392
+ /**
393
+ * Enable or disable the sensors (CTRL7 aEN / gEN / gSN). The gyroscope needs
394
+ * 150 ms + 3/ODR, the accelerometer 3 ms + 3/ODR before data is valid.
395
+ * @param {boolean} [accel=true]
396
+ * @param {boolean} [gyro=true]
397
+ * @param {boolean} [gyroSnooze=false]
398
+ * @returns {Promise<void>}
399
+ */
400
+ async setSensors(accel = true, gyro = true, gyroSnooze = false) {
401
+ await this._ready;
402
+ const v = (this._ctrl7 & ~0x13) | (accel ? 0x01 : 0) | (gyro ? 0x02 : 0) | (gyroSnooze ? 0x10 : 0);
403
+ await this._setCtrl7(v);
404
+ }
405
+
406
+ /**
407
+ * Power the chip down (CTRL1.SensorDisable, sensors disabled first) or wake it.
408
+ * @param {boolean} [down=true]
409
+ * @returns {Promise<void>}
410
+ */
411
+ async setPowerDown(down = true) {
412
+ await this._ready;
413
+ if (down) {
414
+ await this._setCtrl7(this._ctrl7 & ~0x03);
415
+ await this._setCtrl1(this._ctrl1 | 0x01);
416
+ } else {
417
+ await this._setCtrl1(this._ctrl1 & ~0x01);
418
+ await _sleep(15);
419
+ }
420
+ }
421
+
422
+ /**
423
+ * Enable or disable SyncSample (data-locking) mode; incompatible with the FIFO and motion engines.
424
+ * @param {boolean} enabled
425
+ * @returns {Promise<void>}
426
+ */
427
+ async setSyncSample(enabled) {
428
+ await this._ready;
429
+ if (enabled) {
430
+ await this._calCmd(_CMD_AHB_CLOCK_GATING, [1]);
431
+ await this._setCtrl7(this._ctrl7 | 0x80);
432
+ } else {
433
+ await this._setCtrl7(this._ctrl7 & ~0x80);
434
+ await this._calCmd(_CMD_AHB_CLOCK_GATING, [0]);
435
+ }
436
+ this._sync = !!enabled;
437
+ }
438
+
439
+ /**
440
+ * Configure the FIFO.
441
+ * @param {number} [mode=2] - 0 = bypass, 1 = FIFO (stops when full), 2 = stream.
442
+ * @param {number} [size=3] - 0–3 = 16 / 32 / 64 / 128 samples.
443
+ * @param {number} [watermark=0] - Watermark in samples (0 disables).
444
+ * @returns {Promise<void>}
445
+ */
446
+ async configureFifo(mode = 2, size = 3, watermark = 0) {
447
+ await this._ready;
448
+ if (!(mode >= 0 && mode <= 2 && size >= 0 && size <= 3)) throw new RangeError('mode must be 0-2 and size 0-3');
449
+ this._fifoWtm = watermark & 0xFF;
450
+ await this._w(_REG_FIFO_WTM_TH, this._fifoWtm);
451
+ this._fifoCtrl = (size << 2) | mode;
452
+ await this._w(_REG_FIFO_CTRL, this._fifoCtrl);
453
+ if (mode === 0) await this._setCtrl7(this._ctrl7 & ~0x20); // DRDY back on
454
+ }
455
+
456
+ /**
457
+ * Read the raw `FIFO_STATUS` byte (full 7, watermark 6, overflow 5, not-empty 4).
458
+ * @returns {Promise<number>}
459
+ */
460
+ async fifoStatus() {
461
+ await this._ready;
462
+ return this._r1(_REG_FIFO_STATUS);
463
+ }
464
+
465
+ /**
466
+ * Number of bytes in the FIFO.
467
+ * @returns {Promise<number>} 2 × the sample-word count.
468
+ */
469
+ async fifoCount() {
470
+ await this._ready;
471
+ const st = await this._r1(_REG_FIFO_STATUS);
472
+ const lo = await this._r1(_REG_FIFO_SMPL_CNT);
473
+ return 2 * (((st & 0x03) << 8) | lo);
474
+ }
475
+
476
+ /**
477
+ * Drain the FIFO with the read-mode procedure (little-endian 6-byte frames, accelerometer first).
478
+ * @param {number} [count] - Maximum number of bytes (default: all available).
479
+ * @returns {Promise<Buffer>}
480
+ */
481
+ async readFifo(count) {
482
+ await this._ready;
483
+ const total = await this.fifoCount();
484
+ const n = count === undefined || count === null ? total : Math.min(count, total);
485
+ if (n <= 0) return Buffer.alloc(0);
486
+ await this._ctrl9(_CMD_REQ_FIFO);
487
+ await this._w(_REG_CTRL1, this._ctrl1 & ~0x40); // ADDR_AI off: FIFO_DATA repeats
488
+ const chunks = [];
489
+ let got = 0;
490
+ try {
491
+ while (got < n) {
492
+ const chunk = Math.min(32, n - got);
493
+ chunks.push(Buffer.from(await this._conn.readReg(_REG_FIFO_DATA, chunk)));
494
+ got += chunk;
495
+ }
496
+ } finally {
497
+ await this._w(_REG_CTRL1, this._ctrl1);
498
+ await this._w(_REG_FIFO_CTRL, this._fifoCtrl & 0x7F);
499
+ }
500
+ return Buffer.concat(chunks);
501
+ }
502
+
503
+ /**
504
+ * Reset the FIFO (CTRL9 RST_FIFO).
505
+ * @returns {Promise<void>}
506
+ */
507
+ async resetFifo() {
508
+ await this._ready;
509
+ await this._ctrl9(_CMD_RST_FIFO);
510
+ }
511
+
512
+ async _sendMotion() {
513
+ const prev = await this._sensorsOff();
514
+ try {
515
+ const a = _clampU8(_round(this._any.thr * 32));
516
+ const n = _clampU8(_round(this._no.thr * 32));
517
+ const mode = (this._no.and ? 0x80 : 0) | ((this._no.axes & 7) << 4) |
518
+ (this._any.and ? 0x08 : 0) | (this._any.axes & 7);
519
+ const sw = this._sig.wait, cw = this._sig.confirm;
520
+ await this._twoSets(_CMD_CONFIG_MOTION,
521
+ [a, a, a, n, n, n, mode],
522
+ [this._any.win & 0xFF, this._no.win & 0xFF, sw & 0xFF, (sw >> 8) & 0xFF, cw & 0xFF, (cw >> 8) & 0xFF]);
523
+ } finally {
524
+ await this._sensorsRestore(prev);
525
+ }
526
+ }
527
+
528
+ /**
529
+ * Configure the any-motion engine.
530
+ * @param {number} [thresholdG=0.25] - Slope threshold in g (1/32 g steps, 0–7.97).
531
+ * @param {number} [window=3] - Window in samples.
532
+ * @param {number} [axes=0b111] - Axis mask (bit 0 X, 1 Y, 2 Z).
533
+ * @param {boolean} [logicAnd=false] - AND instead of OR over the enabled axes.
534
+ * @returns {Promise<void>}
535
+ */
536
+ async configureAnyMotion(thresholdG = 0.25, window = 3, axes = 0b111, logicAnd = false) {
537
+ await this._ready;
538
+ this._any = { thr: thresholdG, win: window, axes, and: logicAnd };
539
+ await this._sendMotion();
540
+ }
541
+
542
+ /**
543
+ * Configure the no-motion engine (arguments as for `configureAnyMotion`).
544
+ * @param {number} [thresholdG=0.125]
545
+ * @param {number} [window=30]
546
+ * @param {number} [axes=0b111]
547
+ * @param {boolean} [logicAnd=true]
548
+ * @returns {Promise<void>}
549
+ */
550
+ async configureNoMotion(thresholdG = 0.125, window = 30, axes = 0b111, logicAnd = true) {
551
+ await this._ready;
552
+ this._no = { thr: thresholdG, win: window, axes, and: logicAnd };
553
+ await this._sendMotion();
554
+ }
555
+
556
+ /**
557
+ * Configure the significant-motion engine; requires any- and no-motion configured and enabled.
558
+ * @param {number} [waitWindow=150] - Wait window in samples (16-bit).
559
+ * @param {number} [confirmWindow=600] - Confirmation window in samples (16-bit).
560
+ * @returns {Promise<void>}
561
+ */
562
+ async configureSignificantMotion(waitWindow = 150, confirmWindow = 600) {
563
+ await this._ready;
564
+ this._sig = { wait: waitWindow, confirm: confirmWindow };
565
+ await this._sendMotion();
566
+ }
567
+
568
+ /**
569
+ * Enable the motion engines (CTRL8 bits 1–3).
570
+ * @param {boolean} [anyMotion=false]
571
+ * @param {boolean} [noMotion=false]
572
+ * @param {boolean} [significant=false]
573
+ * @returns {Promise<void>}
574
+ */
575
+ async enableMotion(anyMotion = false, noMotion = false, significant = false) {
576
+ await this._ready;
577
+ await this._setCtrl8((this._ctrl8 & ~0x0E) | (anyMotion ? 0x02 : 0) | (noMotion ? 0x04 : 0) |
578
+ (significant ? 0x08 : 0));
579
+ }
580
+
581
+ /**
582
+ * Configure the tap engine (two CTRL9 CONFIGURE_TAP commands).
583
+ * @param {number} [priority=0] - Axis priority 0–5.
584
+ * @param {number} [peakWindow=20] - Peak window in samples.
585
+ * @param {number} [tapWindow=50] - Quiet window in samples.
586
+ * @param {number} [doubleTapWindow=250] - Double-tap window in samples.
587
+ * @param {number} [alpha=0.0625] - Acceleration averaging weight (1/128 steps).
588
+ * @param {number} [gamma=0.25] - Magnitude averaging weight (1/128 steps).
589
+ * @param {number} [peakThreshold=0.8] - Peak threshold in g² (1/1024 steps).
590
+ * @param {number} [quietThreshold=0.4] - Undefined-motion threshold in g² (1/1024 steps).
591
+ * @returns {Promise<void>}
592
+ */
593
+ async configureTap(priority = 0, peakWindow = 20, tapWindow = 50, doubleTapWindow = 250,
594
+ alpha = 0.0625, gamma = 0.25, peakThreshold = 0.8, quietThreshold = 0.4) {
595
+ await this._ready;
596
+ const peak = _round(peakThreshold * 1024);
597
+ const quiet = _round(quietThreshold * 1024);
598
+ const prev = await this._sensorsOff();
599
+ try {
600
+ await this._twoSets(_CMD_CONFIG_TAP,
601
+ [peakWindow & 0xFF, priority & 0xFF, tapWindow & 0xFF, (tapWindow >> 8) & 0xFF,
602
+ doubleTapWindow & 0xFF, (doubleTapWindow >> 8) & 0xFF],
603
+ [_round(alpha * 128) & 0x7F, _round(gamma * 128) & 0x7F, peak & 0xFF, (peak >> 8) & 0xFF,
604
+ quiet & 0xFF, (quiet >> 8) & 0xFF]);
605
+ } finally {
606
+ await this._sensorsRestore(prev);
607
+ }
608
+ }
609
+
610
+ /**
611
+ * Enable or disable the tap engine (CTRL8.Tap_EN). Use an accelerometer ODR above 200 Hz.
612
+ * @param {boolean} [enabled=true]
613
+ * @returns {Promise<void>}
614
+ */
615
+ async enableTap(enabled = true) {
616
+ await this._ready;
617
+ await this._setCtrl8(enabled ? (this._ctrl8 | 0x01) : (this._ctrl8 & ~0x01));
618
+ }
619
+
620
+ /**
621
+ * Read the last tap event.
622
+ * @returns {Promise<Array>} `[count, axis, negative]` — count 0/1/2, axis 0 none / 1 X / 2 Y / 3 Z.
623
+ */
624
+ async tapStatus() {
625
+ await this._ready;
626
+ const v = await this._r1(_REG_TAP_STATUS);
627
+ return [v & 0x03, (v >> 4) & 0x03, (v & 0x80) !== 0];
628
+ }
629
+
630
+ /**
631
+ * Configure the pedometer (two CTRL9 CONFIGURE_PEDOMETER commands; defaults for a 50 Hz ODR).
632
+ * @param {number} [sampleCount=50]
633
+ * @param {number} [peakToPeakMg=200]
634
+ * @param {number} [peakMg=100]
635
+ * @param {number} [timeUp=200]
636
+ * @param {number} [timeLow=20]
637
+ * @param {number} [entryCount=10]
638
+ * @param {number} [precision=0]
639
+ * @param {number} [reportEvery=4] - Steps per pedometer event / count update.
640
+ * @returns {Promise<void>}
641
+ */
642
+ async configurePedometer(sampleCount = 50, peakToPeakMg = 200, peakMg = 100, timeUp = 200,
643
+ timeLow = 20, entryCount = 10, precision = 0, reportEvery = 4) {
644
+ await this._ready;
645
+ const p2p = _round(peakToPeakMg / 1000 * 1024);
646
+ const pk = _round(peakMg / 1000 * 1024);
647
+ const prev = await this._sensorsOff();
648
+ try {
649
+ await this._twoSets(_CMD_CONFIG_PEDOMETER,
650
+ [sampleCount & 0xFF, (sampleCount >> 8) & 0xFF, p2p & 0xFF, (p2p >> 8) & 0xFF, pk & 0xFF, (pk >> 8) & 0xFF],
651
+ [timeUp & 0xFF, (timeUp >> 8) & 0xFF, timeLow & 0xFF, entryCount & 0xFF, precision & 0xFF, reportEvery & 0xFF]);
652
+ } finally {
653
+ await this._sensorsRestore(prev);
654
+ }
655
+ }
656
+
657
+ /**
658
+ * Enable or disable the pedometer (CTRL8.Pedo_EN); a 0→1 transition resets the count.
659
+ * @param {boolean} [enabled=true]
660
+ * @returns {Promise<void>}
661
+ */
662
+ async enablePedometer(enabled = true) {
663
+ await this._ready;
664
+ await this._setCtrl8(enabled ? (this._ctrl8 | 0x10) : (this._ctrl8 & ~0x10));
665
+ }
666
+
667
+ /**
668
+ * Read the 24-bit step count.
669
+ * @returns {Promise<number>}
670
+ */
671
+ async stepCount() {
672
+ await this._ready;
673
+ const b = await this._conn.readReg(_REG_STEP_CNT, 3);
674
+ return b[0] | (b[1] << 8) | (b[2] << 16);
675
+ }
676
+
677
+ /**
678
+ * Clear the step count (CTRL9 RESET_PEDOMETER).
679
+ * @returns {Promise<void>}
680
+ */
681
+ async resetStepCount() {
682
+ await this._ready;
683
+ await this._ctrl9(_CMD_RESET_PEDOMETER);
684
+ }
685
+
686
+ /**
687
+ * Enter or leave wake-on-motion mode. Uses accelerometer ±2 g at ODR code 12 (128 Hz).
688
+ * @param {number} thresholdMg - 1–255 mg enables, 0 exits WoM mode.
689
+ * @param {number} [interruptPin=1] - 1 or 2.
690
+ * @param {number} [initialLevel=0] - Initial pin level 0 or 1.
691
+ * @param {number} [blankingSamples=0] - Samples ignored after enabling (0–63).
692
+ * @returns {Promise<void>}
693
+ */
694
+ async setWakeOnMotion(thresholdMg, interruptPin = 1, initialLevel = 0, blankingSamples = 0) {
695
+ await this._ready;
696
+ await this._sensorsOff();
697
+ if (thresholdMg === 0) {
698
+ await this._calCmd(_CMD_WRITE_WOM, [0, 0]);
699
+ this._intSources &= ~QMI8658ASource.WAKE_ON_MOTION;
700
+ await this._updateIntPins();
701
+ return;
702
+ }
703
+ this._accelFs = 0; this._accelOdr = 12;
704
+ this._updateSens();
705
+ await this._w(_REG_CTRL2, 0x0C);
706
+ this._womPin = interruptPin === 2 ? 2 : 1;
707
+ const sel = (this._womPin === 2 ? 0x40 : 0x00) | (initialLevel ? 0x80 : 0x00);
708
+ await this._calCmd(_CMD_WRITE_WOM, [Math.min(255, thresholdMg), sel | (blankingSamples & 0x3F)]);
709
+ this._intSources |= QMI8658ASource.WAKE_ON_MOTION;
710
+ await this._updateIntPins();
711
+ await this._setCtrl7((this._ctrl7 & ~0x03) | 0x01);
712
+ await _sleep(10);
713
+ }
714
+
715
+ async _offsetCmd(cmd, codes) {
716
+ const prev = await this._sensorsOff();
717
+ try {
718
+ const params = [];
719
+ for (const c of codes) params.push(c & 0xFF, (c >> 8) & 0xFF);
720
+ await this._calCmd(cmd, params);
721
+ } finally {
722
+ await this._sensorsRestore(prev);
723
+ }
724
+ }
725
+
726
+ /**
727
+ * Apply an accelerometer offset (signed 4.12 g; volatile).
728
+ * @param {number} x - X offset in m/s².
729
+ * @param {number} y - Y offset in m/s².
730
+ * @param {number} z - Z offset in m/s².
731
+ * @returns {Promise<void>}
732
+ */
733
+ async setAccelOffset(x, y, z) {
734
+ await this._ready;
735
+ await this._offsetCmd(_CMD_ACCEL_OFFSET, [x, y, z].map((v) => _round(v / _G * 4096) & 0xFFFF));
736
+ }
737
+
738
+ /**
739
+ * Apply a gyroscope offset (signed 11.5 dps; volatile).
740
+ * @param {number} x - X offset in rad/s.
741
+ * @param {number} y - Y offset in rad/s.
742
+ * @param {number} z - Z offset in rad/s.
743
+ * @returns {Promise<void>}
744
+ */
745
+ async setGyroOffset(x, y, z) {
746
+ await this._ready;
747
+ await this._offsetCmd(_CMD_GYRO_OFFSET, [x, y, z].map((v) => _round(v / _DEG_TO_RAD * 32) & 0xFFFF));
748
+ }
749
+
750
+ /**
751
+ * Run gyroscope calibration-on-demand (about 1.5 s; keep the device still).
752
+ * @returns {Promise<Array>} `[success, codStatus]`.
753
+ */
754
+ async calibrateGyro() {
755
+ await this._ready;
756
+ const prev = await this._sensorsOff();
757
+ let status;
758
+ try {
759
+ await this._ctrl9(_CMD_COD, 3000);
760
+ status = await this._r1(_REG_COD_STATUS);
761
+ } finally {
762
+ await this._sensorsRestore(prev);
763
+ }
764
+ return [status === 0, status];
765
+ }
766
+
767
+ /**
768
+ * Read the raw gyroscope gains, valid right after `calibrateGyro()`.
769
+ * @returns {Promise<number[]>} Raw 16-bit `[x, y, z]` gains.
770
+ */
771
+ async getGyroGains() {
772
+ await this._ready;
773
+ const b = await this._conn.readReg(_REG_DVX_L, 6);
774
+ return [b.readUInt16LE(0), b.readUInt16LE(2), b.readUInt16LE(4)];
775
+ }
776
+
777
+ /**
778
+ * Restore previously saved gyroscope gains.
779
+ * @param {number} x - Raw X gain.
780
+ * @param {number} y - Raw Y gain.
781
+ * @param {number} z - Raw Z gain.
782
+ * @returns {Promise<void>}
783
+ */
784
+ async setGyroGains(x, y, z) {
785
+ await this._ready;
786
+ const prev = await this._sensorsOff();
787
+ try {
788
+ await this._calCmd(_CMD_APPLY_GYRO_GAINS,
789
+ [x & 0xFF, (x >> 8) & 0xFF, y & 0xFF, (y >> 8) & 0xFF, z & 0xFF, (z >> 8) & 0xFF]);
790
+ } finally {
791
+ await this._sensorsRestore(prev);
792
+ }
793
+ }
794
+
795
+ async _selfTest(ctrlReg, saved, scale) {
796
+ const prev = await this._sensorsOff();
797
+ let b;
798
+ try {
799
+ await this._w(ctrlReg, saved | 0x80);
800
+ await this._waitStatus(0x01, true, 3000, 'self-test Avail');
801
+ b = await this._conn.readReg(_REG_DVX_L, 6);
802
+ await this._w(ctrlReg, saved);
803
+ await this._waitStatus(0x01, false, 3000, 'self-test end');
804
+ } finally {
805
+ await this._sensorsRestore(prev);
806
+ }
807
+ return [0, 2, 4].map((o) => b.readInt16LE(o) / scale);
808
+ }
809
+
810
+ /**
811
+ * Run the accelerometer self-test.
812
+ * @returns {Promise<Array>} `[passed, [x, y, z]]` — values in m/s²; passes when every axis exceeds 200 mg.
813
+ */
814
+ async selfTestAccel() {
815
+ await this._ready;
816
+ const g = await this._selfTest(_REG_CTRL2, (this._accelFs << 4) | this._accelOdr, 2048);
817
+ return [g.every((v) => Math.abs(v) > 0.2), g.map((v) => v * _G)];
818
+ }
819
+
820
+ /**
821
+ * Run the gyroscope self-test.
822
+ * @returns {Promise<Array>} `[passed, [x, y, z]]` — values in rad/s; passes when every axis exceeds 300 dps.
823
+ */
824
+ async selfTestGyro() {
825
+ await this._ready;
826
+ const d = await this._selfTest(_REG_CTRL3, (this._gyroFs << 4) | this._gyroOdr, 16);
827
+ return [d.every((v) => Math.abs(v) > 300), d.map((v) => v * _DEG_TO_RAD)];
828
+ }
829
+
830
+ /**
831
+ * Read revision, firmware version and USID (CTRL9 COPY_USID).
832
+ * @returns {Promise<{revision: number, firmware: Buffer, usid: Buffer}>}
833
+ */
834
+ async deviceInfo() {
835
+ await this._ready;
836
+ await this._ctrl9(_CMD_COPY_USID);
837
+ const revision = await this._r1(_REG_REVISION_ID);
838
+ const firmware = Buffer.concat([Buffer.from(await this._conn.readReg(_REG_FW_VERSION, 2)),
839
+ Buffer.from(await this._conn.readReg(_REG_FW_VERSION + 2, 1))]);
840
+ const usid = Buffer.from(await this._conn.readReg(_REG_DVX_L, 6));
841
+ return { revision, firmware, usid };
842
+ }
843
+
844
+ /**
845
+ * Control the internal 200 kΩ pull-ups (CTRL9 SET_RPU); `false` disables a group.
846
+ * Disabling `i2c` on a bus without external pull-ups breaks the bus.
847
+ * @param {boolean} [aux=true]
848
+ * @param {boolean} [sdx=true]
849
+ * @param {boolean} [cs=true]
850
+ * @param {boolean} [i2c=true]
851
+ * @returns {Promise<void>}
852
+ */
853
+ async setPullups(aux = true, sdx = true, cs = true, i2c = true) {
854
+ await this._ready;
855
+ await this._calCmd(_CMD_SET_RPU, [(aux ? 0 : 1) | (sdx ? 0 : 2) | (cs ? 0 : 4) | (i2c ? 0 : 8)]);
856
+ }
857
+
858
+ /**
859
+ * Soft reset and wait 15 ms; the sensors are off afterwards.
860
+ * @returns {Promise<void>}
861
+ */
862
+ async reset() {
863
+ await this._ready;
864
+ await this._w(_REG_RESET, _RESET_CMD);
865
+ await _sleep(15);
866
+ this._ctrl1 = 0x40; this._ctrl7 = 0x00; this._ctrl8 = 0x80;
867
+ await this._w(_REG_CTRL1, this._ctrl1);
868
+ await this._w(_REG_CTRL8, this._ctrl8);
869
+ this._sync = false;
870
+ this._intSources = 0;
871
+ }
872
+
873
+ _sourcePin(source) {
874
+ if (source === QMI8658ASource.DATA_READY) return 2;
875
+ if (source === QMI8658ASource.FIFO_WATERMARK || source === QMI8658ASource.FIFO_FULL) return this._fifoPin;
876
+ if (source === QMI8658ASource.WAKE_ON_MOTION) return this._womPin;
877
+ return this._activityPin;
878
+ }
879
+
880
+ async _updateIntPins() {
881
+ let pins = 0;
882
+ for (let bit = 0; bit < 9; bit++) {
883
+ const s = 1 << bit;
884
+ if (this._intSources & s) pins |= 1 << (this._sourcePin(s) - 1);
885
+ }
886
+ let v = this._ctrl1 & ~0x18;
887
+ v |= ((pins & 1) ? 0x08 : 0) | ((pins & 2) ? 0x10 : 0);
888
+ v = this._fifoPin === 1 ? (v | 0x04) : (v & ~0x04);
889
+ if (v !== this._ctrl1) await this._setCtrl1(v);
890
+ }
891
+
892
+ /**
893
+ * Route the FIFO interrupt (CTRL1.FIFO_INT_SEL) and the motion-engine group (CTRL8.ACTIVITY_INT_SEL).
894
+ * @param {number} [fifoPin=2] - 1 or 2.
895
+ * @param {number} [activityPin=2] - 1 or 2.
896
+ * @returns {Promise<void>}
897
+ */
898
+ async setInterruptPins(fifoPin = 2, activityPin = 2) {
899
+ await this._ready;
900
+ this._fifoPin = fifoPin === 1 ? 1 : 2;
901
+ this._activityPin = activityPin === 1 ? 1 : 2;
902
+ await this._setCtrl8(this._activityPin === 1 ? (this._ctrl8 | 0x40) : (this._ctrl8 & ~0x40));
903
+ await this._updateIntPins();
904
+ }
905
+
906
+ /**
907
+ * Enable one interrupt source.
908
+ * @param {number} source - One of the `QMI8658ASource` constants.
909
+ * @returns {Promise<void>}
910
+ */
911
+ async enableInterrupt(source) {
912
+ await this._ready;
913
+ if (source === QMI8658ASource.DATA_READY) await this._setCtrl7(this._ctrl7 & ~0x20);
914
+ else if (source === QMI8658ASource.FIFO_WATERMARK) await this._w(_REG_FIFO_WTM_TH, this._fifoWtm);
915
+ else if (_ENGINE_BIT[source]) await this._setCtrl8(this._ctrl8 | _ENGINE_BIT[source]);
916
+ this._intSources |= source;
917
+ await this._updateIntPins();
918
+ }
919
+
920
+ /**
921
+ * Disable one interrupt source.
922
+ * @param {number} source - One of the `QMI8658ASource` constants.
923
+ * @returns {Promise<void>}
924
+ */
925
+ async disableInterrupt(source) {
926
+ await this._ready;
927
+ if (source === QMI8658ASource.DATA_READY) await this._setCtrl7(this._ctrl7 | 0x20);
928
+ else if (source === QMI8658ASource.FIFO_WATERMARK) await this._w(_REG_FIFO_WTM_TH, 0);
929
+ else if (_ENGINE_BIT[source]) await this._setCtrl8(this._ctrl8 & ~_ENGINE_BIT[source]);
930
+ this._intSources &= ~source;
931
+ await this._updateIntPins();
932
+ }
933
+
934
+ /**
935
+ * Read STATUS0, STATUS1 and FIFO_STATUS (clearing what clears on read).
936
+ * @returns {Promise<number>} Combined status using the `QMI8658ASource` bit layout.
937
+ */
938
+ async pollInterrupt() {
939
+ await this._ready;
940
+ const s = await this._conn.readReg(_REG_STATUS0, 2);
941
+ const fs = await this._r1(_REG_FIFO_STATUS);
942
+ let status = 0;
943
+ if (s[0] & 0x03) status |= QMI8658ASource.DATA_READY;
944
+ if (fs & 0x40) status |= QMI8658ASource.FIFO_WATERMARK;
945
+ if (fs & 0x80) status |= QMI8658ASource.FIFO_FULL;
946
+ if (s[1] & 0x20) status |= QMI8658ASource.ANY_MOTION;
947
+ if (s[1] & 0x40) status |= QMI8658ASource.NO_MOTION;
948
+ if (s[1] & 0x80) status |= QMI8658ASource.SIGNIFICANT_MOTION;
949
+ if (s[1] & 0x10) status |= QMI8658ASource.STEP;
950
+ if (s[1] & 0x02) status |= QMI8658ASource.TAP;
951
+ if (s[1] & 0x04) status |= QMI8658ASource.WAKE_ON_MOTION;
952
+ return status;
953
+ }
954
+
955
+ /**
956
+ * Subscribe to rising edges on the connection's interrupt pin (a 5 ms polling timer when none is attached).
957
+ * @param {function(number): void} callback - Called with the `pollInterrupt()` status.
958
+ * @returns {Promise<void>}
959
+ */
960
+ async onInterrupt(callback) {
961
+ await this._ready;
962
+ this._intCallback = callback;
963
+ const intPin = this._conn ? this._conn.intPin : null;
964
+ if (intPin) {
965
+ this._intHandler = () => { this._dispatchInt(); };
966
+ await intPin.onEdge(this._intHandler, 'rising');
967
+ } else {
968
+ this._pollTimer = setInterval(() => { this._dispatchInt(); }, 5);
969
+ }
970
+ }
971
+
972
+ /**
973
+ * Unsubscribe from interrupt-pin edges.
974
+ * @returns {Promise<void>}
975
+ */
976
+ async offInterrupt() {
977
+ this._intCallback = null;
978
+ const intPin = this._conn ? this._conn.intPin : null;
979
+ if (intPin && this._intHandler) await intPin.offEdge(this._intHandler);
980
+ this._intHandler = null;
981
+ if (this._pollTimer !== null) {
982
+ clearInterval(this._pollTimer);
983
+ this._pollTimer = null;
984
+ }
985
+ }
986
+
987
+ async _dispatchInt() {
988
+ const cb = this._intCallback;
989
+ if (!cb) return;
990
+ try {
991
+ const status = await this.pollInterrupt();
992
+ if (status) cb(status);
993
+ } catch (err) {
994
+ // ignore: the next edge or poll retries
995
+ }
996
+ }
997
+ }
998
+
999
+ Object.assign(QMI8658AFull, {
1000
+ SOURCE_DATA_READY: QMI8658ASource.DATA_READY,
1001
+ SOURCE_FIFO_WATERMARK: QMI8658ASource.FIFO_WATERMARK,
1002
+ SOURCE_FIFO_FULL: QMI8658ASource.FIFO_FULL,
1003
+ SOURCE_ANY_MOTION: QMI8658ASource.ANY_MOTION,
1004
+ SOURCE_NO_MOTION: QMI8658ASource.NO_MOTION,
1005
+ SOURCE_SIGNIFICANT_MOTION: QMI8658ASource.SIGNIFICANT_MOTION,
1006
+ SOURCE_STEP: QMI8658ASource.STEP,
1007
+ SOURCE_TAP: QMI8658ASource.TAP,
1008
+ SOURCE_WAKE_ON_MOTION: QMI8658ASource.WAKE_ON_MOTION,
1009
+ });
1010
+
1011
+ module.exports = { QMI8658AMinimal, QMI8658AFull, QMI8658ASource };