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,1406 @@
1
+ 'use strict';
2
+
3
+ // Register map (datasheet Rev 8, Table 8).
4
+ const _REG_STATUS = 0x00; // STATUS / F_STATUS
5
+ const _REG_OUT_X_MSB = 0x01;
6
+ const _REG_OUT_X_LSB = 0x02;
7
+ const _REG_OUT_Y_MSB = 0x03;
8
+ const _REG_OUT_Y_LSB = 0x04;
9
+ const _REG_OUT_Z_MSB = 0x05;
10
+ const _REG_OUT_Z_LSB = 0x06;
11
+ const _REG_F_SETUP = 0x09;
12
+ const _REG_TRIG_CFG = 0x0A;
13
+ const _REG_SYSMOD = 0x0B;
14
+ const _REG_INT_SOURCE = 0x0C;
15
+ const _REG_WHO_AM_I = 0x0D;
16
+ const _REG_XYZ_DATA_CFG = 0x0E;
17
+ const _REG_HP_FILTER = 0x0F;
18
+ const _REG_PL_STATUS = 0x10;
19
+ const _REG_PL_CFG = 0x11;
20
+ const _REG_PL_COUNT = 0x12;
21
+ const _REG_PL_BF_ZCOMP = 0x13;
22
+ const _REG_PL_THS = 0x14;
23
+ const _REG_FF_MT_CFG = 0x15;
24
+ const _REG_FF_MT_SRC = 0x16;
25
+ const _REG_FF_MT_THS = 0x17;
26
+ const _REG_FF_MT_COUNT = 0x18;
27
+ const _REG_TRANSIENT_CFG = 0x1D;
28
+ const _REG_TRANSIENT_SRC = 0x1E;
29
+ const _REG_TRANSIENT_THS = 0x1F;
30
+ const _REG_TRANSIENT_COUNT = 0x20;
31
+ const _REG_PULSE_CFG = 0x21;
32
+ const _REG_PULSE_SRC = 0x22;
33
+ const _REG_PULSE_THSX = 0x23;
34
+ const _REG_PULSE_THSY = 0x24;
35
+ const _REG_PULSE_THSZ = 0x25;
36
+ const _REG_PULSE_TMLT = 0x26;
37
+ const _REG_PULSE_LTCY = 0x27;
38
+ const _REG_PULSE_WIND = 0x28;
39
+ const _REG_ASLP_COUNT = 0x29;
40
+ const _REG_CTRL_REG1 = 0x2A;
41
+ const _REG_CTRL_REG2 = 0x2B;
42
+ const _REG_CTRL_REG3 = 0x2C;
43
+ const _REG_CTRL_REG4 = 0x2D;
44
+ const _REG_CTRL_REG5 = 0x2E;
45
+ const _REG_OFF_X = 0x2F;
46
+ const _REG_OFF_Y = 0x30;
47
+ const _REG_OFF_Z = 0x31;
48
+
49
+ const _WHO_AM_I_VALUE = 0x1A;
50
+
51
+ // CTRL_REG1 bit positions.
52
+ const _CR1_ASLP_RATE_MASK = 0xC0;
53
+ const _CR1_DR_MASK = 0x38;
54
+ const _CR1_LNOISE = 0x04;
55
+ const _CR1_F_READ = 0x02;
56
+ const _CR1_ACTIVE = 0x01;
57
+
58
+ // CTRL_REG2 bit positions.
59
+ const _CR2_ST = 0x80;
60
+ const _CR2_RST = 0x40;
61
+ const _CR2_SMODS = 0x18;
62
+ const _CR2_SLPE = 0x04;
63
+ const _CR2_MODS = 0x03;
64
+
65
+ // CTRL_REG3 bit positions.
66
+ const _CR3_FIFO_GATE = 0x80;
67
+ const _CR3_WAKE_MASK = 0x78;
68
+ const _CR3_WAKE_TRANS = 0x40;
69
+ const _CR3_WAKE_LNDPRT = 0x20;
70
+ const _CR3_WAKE_PULSE = 0x10;
71
+ const _CR3_WAKE_FF_MT = 0x08;
72
+ const _CR3_IPOL = 0x02;
73
+ const _CR3_PP_OD = 0x01;
74
+
75
+ // XYZ_DATA_CFG bits.
76
+ const _XDC_HPF_OUT = 0x10;
77
+ const _XDC_FS_MASK = 0x03;
78
+
79
+ // FF_MT_CFG bits.
80
+ const _FFMT_ELE = 0x80;
81
+ const _FFMT_OAE = 0x40;
82
+ const _FFMT_AXES = 0x38;
83
+
84
+ // TRANSIENT_CFG bits.
85
+ const _TRS_ELE = 0x10;
86
+ const _TRS_AXES = 0x0E;
87
+ const _TRS_HPF = 0x01;
88
+
89
+ // PULSE_CFG bits.
90
+ const _PULSE_DPA = 0x80;
91
+ const _PULSE_ELE = 0x40;
92
+ const _PULSE_AXES = 0x3F;
93
+
94
+ // PL_CFG bits.
95
+ const _PL_DBCNTM = 0x80;
96
+ const _PL_EN = 0x40;
97
+
98
+ // PL_BF_ZCOMP bits.
99
+ const _ZCOMP_BKFR_MASK = 0xC0;
100
+ const _ZCOMP_ZLOCK = 0x07;
101
+
102
+ // P_L_THS_REG bits.
103
+ const _PL_THS_MASK = 0xF8;
104
+ const _PL_THS_SHIFT = 3;
105
+ const _PL_HYS_MASK = 0x07;
106
+
107
+ // F_SETUP bits.
108
+ const _FSETUP_MODE_MASK = 0xC0;
109
+ const _FSETUP_MODE_SHIFT = 6;
110
+ const _FSETUP_WMRK_MASK = 0x3F;
111
+
112
+ // TRIG_CFG bits.
113
+ const _TRIG_TRANS = 0x20;
114
+ const _TRIG_LNDPRT = 0x10;
115
+ const _TRIG_PULSE = 0x08;
116
+ const _TRIG_FF_MT = 0x04;
117
+
118
+ // HP_FILTER_CUTOFF bits.
119
+ const _HPF_PULSE_HPF_BYP = 0x20;
120
+ const _HPF_PULSE_LPF_EN = 0x10;
121
+ const _HPF_SEL_MASK = 0x03;
122
+
123
+ // Full-scale range → XYZ_DATA_CFG FS code and counts per g (14-bit).
124
+ const _RANGE_FS = { 2: 0x00, 4: 0x01, 8: 0x02 };
125
+ const _RANGE_COUNTS_PER_G = { 2: 4096, 4: 2048, 8: 1024 };
126
+ // 8-bit (fast read) counts per g.
127
+ const _RANGE_COUNTS_PER_G_8BIT = { 2: 64, 4: 32, 8: 16 };
128
+
129
+ // Output data rate → CTRL_REG1 DR code.
130
+ const _ODR_CODES = [
131
+ [800, 0b000], [400, 0b001], [200, 0b010], [100, 0b011],
132
+ [50, 0b100], [12.5, 0b101], [6.25, 0b110], [1.56, 0b111],
133
+ ];
134
+
135
+ // Auto-SLEEP rate → CTRL_REG1 ASLP_RATE code.
136
+ const _ASLP_RATE_CODES = [[50, 0b00], [12.5, 0b01], [6.25, 0b10], [1.56, 0b11]];
137
+
138
+ // Debounce step (ms) per ODR and oversampling mode (datasheet Table 27).
139
+ // Columns: normal, low-noise low-power, high resolution, low power.
140
+ const _DEBOUNCE_STEPS = {
141
+ 800: [1.25, 1.25, 1.25, 1.25],
142
+ 400: [2.5, 2.5, 2.5, 2.5],
143
+ 200: [5, 5, 2.5, 5],
144
+ 100: [10, 10, 2.5, 10],
145
+ 50: [20, 20, 2.5, 20],
146
+ 12.5: [20, 80, 2.5, 80],
147
+ 6.25: [20, 80, 2.5, 160],
148
+ 1.56: [20, 80, 2.5, 160],
149
+ };
150
+
151
+ // Pulse time steps (ms): TMLT and LTCY/WIND per ODR, lpf setting and
152
+ // oversampling mode (datasheet Tables 50/51 and 53/54).
153
+ // [lpf ? TMLT : TMLT, lpf ? LTCY : LTCY] — each entry is
154
+ // [tmltNormal, tmltLpln, tmltHr, tmltLp, ltcyNormal, ltcyLpln, ltcyHr, ltcyLp].
155
+ const _PULSE_STEPS_LPF = {
156
+ 800: [1.25, 1.25, 1.25, 1.25, 2.5, 2.5, 2.5, 2.5],
157
+ 400: [2.5, 2.5, 2.5, 2.5, 5, 5, 5, 5],
158
+ 200: [5, 5, 2.5, 5, 10, 10, 5, 10],
159
+ 100: [10, 10, 2.5, 10, 20, 20, 5, 20],
160
+ 50: [20, 20, 2.5, 20, 40, 40, 5, 40],
161
+ 12.5: [20, 80, 2.5, 80, 40, 160, 5, 160],
162
+ 6.25: [20, 80, 2.5, 160, 40, 160, 5, 320],
163
+ 1.56: [20, 80, 2.5, 160, 40, 160, 5, 320],
164
+ };
165
+ const _PULSE_STEPS_NO_LPF = {
166
+ 800: [0.625, 0.625, 0.625, 0.625, 1.25, 1.25, 1.25, 1.25],
167
+ 400: [0.625, 0.625, 0.625, 1.25, 1.25, 1.25, 1.25, 2.5],
168
+ 200: [1.25, 1.25, 0.625, 2.5, 2.5, 2.5, 1.25, 5],
169
+ 100: [2.5, 2.5, 0.625, 5, 5, 5, 1.25, 10],
170
+ 50: [5, 5, 0.625, 10, 10, 10, 1.25, 20],
171
+ 12.5: [5, 20, 0.625, 40, 10, 40, 1.25, 80],
172
+ 6.25: [5, 20, 0.625, 40, 10, 40, 1.25, 80],
173
+ 1.56: [5, 20, 0.625, 40, 10, 40, 1.25, 80],
174
+ };
175
+
176
+ // Orientation trip angles (degrees) → P_L_THS code (datasheet Table 32).
177
+ const _ORIENTATION_THRESHOLDS = [
178
+ [15, 0x07], [20, 0x09], [30, 0x0C], [35, 0x0D], [40, 0x0F],
179
+ [45, 0x10], [55, 0x13], [60, 0x14], [70, 0x17], [75, 0x19],
180
+ ];
181
+
182
+ // Self-test typical count deltas (14-bit counts) per axis.
183
+ const _SELF_TEST_X = 181;
184
+ const _SELF_TEST_Y = 255;
185
+ const _SELF_TEST_Z = 1680;
186
+
187
+ function _clamp(value, lo, hi) {
188
+ if (value < lo) return lo;
189
+ if (value > hi) return hi;
190
+ return value;
191
+ }
192
+
193
+ function _delayMs(ms) {
194
+ if (!(ms > 0)) return;
195
+ const end = Date.now() + ms;
196
+ while (Date.now() < end) { /* spin */ }
197
+ }
198
+
199
+ /**
200
+ * MMA8451Q 3-axis MEMS accelerometer (NXP) — minimal interface.
201
+ *
202
+ * Low-power capacitive 3-axis accelerometer with 14-bit output (4096
203
+ * counts/*g* at ±2 *g*), selectable ±2/±4/±8 *g* full scale and output data
204
+ * rates from 1.56 Hz to 800 Hz. Communicates over I²C at address 0x1C
205
+ * (SA0 = GND) or 0x1D (SA0 = VDDIO).
206
+ *
207
+ * Default configuration (written by {@link MMA8451QMinimal#init}):
208
+ * - Range ±2 *g* (4096 counts/*g*)
209
+ * - Output data rate 100 Hz
210
+ * - 14-bit output, normal oversampling
211
+ * - Low-noise mode off, high-pass-filtered output off
212
+ * - FIFO disabled, all detectors and interrupt routing off
213
+ * - Interrupts active-low, push-pull
214
+ * - Offsets 0
215
+ *
216
+ * Constructor caveat: JS constructors cannot be async, so the init
217
+ * sequence is fired off unawaited from the constructor. A `WHO_AM_I`
218
+ * mismatch therefore surfaces as an *unhandled promise rejection* shortly
219
+ * after construction rather than a synchronous throw from
220
+ * `new MMA8451QMinimal(...)`. Callers that need deterministic behaviour
221
+ * should `await chip.init()` — the same sequence, exposed as a public
222
+ * async method — and handle its rejection.
223
+ */
224
+ class MMA8451QMinimal {
225
+ /**
226
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - Configured I²C connection.
227
+ */
228
+ constructor(connection) {
229
+ this._conn = connection;
230
+ this._rangeG = 2;
231
+ this._odrHz = 100;
232
+ this._oversampling = 0;
233
+ this._sleepOversampling = 0;
234
+ this._lowNoise = false;
235
+ this._fastRead = false;
236
+ this._hpfOut = false;
237
+ this._fifoMode = 0;
238
+ this._active = false;
239
+ // The init sequence is fired off unawaited — a JS constructor cannot
240
+ // await. `init()` exposes the same sequence so callers that need a
241
+ // deterministic result can await it.
242
+ this._ready = this._initSequence();
243
+ // Surface an init failure (e.g. wrong device) as an unhandled
244
+ // rejection rather than silently swallowing it.
245
+ this._ready.catch(() => {});
246
+ }
247
+
248
+ /**
249
+ * Run the initialization sequence.
250
+ *
251
+ * Waits for the I²C interface to come up, verifies `WHO_AM_I` is 0x1A,
252
+ * performs a software reset, then configures ±2 *g*, 100 Hz, 14-bit
253
+ * output with normal oversampling and puts the chip into ACTIVE mode.
254
+ *
255
+ * The constructor starts this sequence unawaited; call `init()` explicitly
256
+ * (`await chip.init()`) when the outcome must be observed. Awaiting it
257
+ * after the constructor has already started the sequence waits for that
258
+ * same run rather than starting a second one.
259
+ *
260
+ * @returns {Promise<void>} Resolves once the chip is in ACTIVE mode.
261
+ * @throws {Error} If `WHO_AM_I` does not read 0x1A, or the reset never completes.
262
+ */
263
+ async init() {
264
+ await this._ready;
265
+ }
266
+
267
+ /**
268
+ * The actual initialization sequence.
269
+ * @returns {Promise<void>}
270
+ */
271
+ async _initSequence() {
272
+ _delayMs(1);
273
+
274
+ const who = await this._readReg(_REG_WHO_AM_I);
275
+ if (who !== _WHO_AM_I_VALUE) {
276
+ throw new Error('MMA8451Q WHO_AM_I: expected 0x1a, got 0x' + who.toString(16));
277
+ }
278
+
279
+ // Software reset: the I²C interface is reset too, so the write may
280
+ // complete without ACK. Wait and re-read CTRL_REG2 until the
281
+ // meaningful bits are 0 again. RST is a self-clearing bit (reading
282
+ // it returns 0), so it is masked out of the check.
283
+ await this._writeReg(_REG_CTRL_REG2, _CR2_RST);
284
+ _delayMs(1);
285
+ const deadline = Date.now() + 10;
286
+ for (;;) {
287
+ let ctrl2 = 0;
288
+ try {
289
+ ctrl2 = await this._readReg(_REG_CTRL_REG2);
290
+ } catch (e) {
291
+ ctrl2 = 0xFF; // NACK: the interface is still resetting
292
+ }
293
+ if ((ctrl2 & ~_CR2_RST & 0xFF) === 0) break;
294
+ if (Date.now() > deadline) {
295
+ throw new Error('MMA8451Q reset did not complete (CTRL_REG2 = 0x' + ctrl2.toString(16) + ')');
296
+ }
297
+ _delayMs(1);
298
+ }
299
+
300
+ await this._writeReg(_REG_XYZ_DATA_CFG, 0x00); // ±2 g, no HPF output
301
+ await this._writeReg(_REG_CTRL_REG2, 0x00); // normal oversampling, Auto-SLEEP off
302
+ await this._writeReg(_REG_CTRL_REG1, 0x19); // DR = 011 (100 Hz), ACTIVE = 1
303
+
304
+ this._rangeG = 2;
305
+ this._odrHz = 100;
306
+ this._oversampling = 0;
307
+ this._sleepOversampling = 0;
308
+ this._lowNoise = false;
309
+ this._fastRead = false;
310
+ this._hpfOut = false;
311
+ this._fifoMode = 0;
312
+ this._active = true;
313
+
314
+ await this._waitTurnOn();
315
+ }
316
+
317
+ async _writeReg(reg, value) {
318
+ await this._conn.writeReg(reg, value & 0xFF);
319
+ }
320
+
321
+ async _readReg(reg) {
322
+ return (await this._conn.readReg(reg, 1))[0];
323
+ }
324
+
325
+ /**
326
+ * Wait for the STANDBY → ACTIVE turn-on time (2/ODR + 2 ms).
327
+ * @returns {Promise<void>}
328
+ */
329
+ async _waitTurnOn() {
330
+ _delayMs((2.0 / this._odrHz) * 1000.0 + 2.0);
331
+ }
332
+
333
+ /**
334
+ * Read 3-axis linear acceleration.
335
+ *
336
+ * Reads the six data bytes (0x01..0x06) in one burst so X, Y and Z come
337
+ * from a single measurement.
338
+ *
339
+ * @returns {Promise<number[]>} [x, y, z] acceleration in *g*.
340
+ */
341
+ async read() {
342
+ const counts = await this._readCounts();
343
+ const scale = this._fastRead
344
+ ? _RANGE_COUNTS_PER_G_8BIT[this._rangeG]
345
+ : _RANGE_COUNTS_PER_G[this._rangeG];
346
+ return [counts[0] / scale, counts[1] / scale, counts[2] / scale];
347
+ }
348
+
349
+ /**
350
+ * Read the raw signed counts of one 14-bit (or 8-bit) sample.
351
+ * @returns {Promise<number[]>} [x, y, z] signed counts.
352
+ */
353
+ async _readCounts() {
354
+ if (this._fastRead) {
355
+ const raw = await this._conn.readReg(_REG_OUT_X_MSB, 3);
356
+ return [(raw[0] << 24 >> 24), (raw[1] << 24 >> 24), (raw[2] << 24 >> 24)];
357
+ }
358
+ const raw = await this._conn.readReg(_REG_OUT_X_MSB, 6);
359
+ const counts = [];
360
+ for (let i = 0; i < 3; i++) {
361
+ const raw16 = (raw[i * 2] << 8) | raw[i * 2 + 1];
362
+ counts.push((raw16 << 16 >> 16) >> 2); // arithmetic shift: 14-bit signed
363
+ }
364
+ return counts;
365
+ }
366
+ }
367
+
368
+ /**
369
+ * MMA8451Q full interface — extends {@link MMA8451QMinimal} with the complete
370
+ * chip feature set: configurable range / data rate / oversampling, STANDBY
371
+ * and ACTIVE handling, the 32-sample FIFO, the freefall / motion, transient,
372
+ * tap (pulse) and portrait/landscape orientation detectors, Auto-SLEEP /
373
+ * Auto-WAKE, per-axis offset calibration, interrupt enable / routing, the
374
+ * interrupt API and the electrostatic self-test.
375
+ */
376
+ class MMA8451QFull extends MMA8451QMinimal {
377
+ /**
378
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
379
+ */
380
+ constructor(connection) {
381
+ super(connection);
382
+ this._callback = null;
383
+ this._edgeHandler = null;
384
+ this._pollTimer = null;
385
+ }
386
+
387
+ /**
388
+ * Read 3-axis linear acceleration in *g*.
389
+ *
390
+ * Delegates to {@link MMA8451QMinimal#read}.
391
+ * @returns {Promise<number[]>}
392
+ */
393
+ async read() {
394
+ return super.read();
395
+ }
396
+
397
+ // -- range, rate, oversampling ------------------------------------------
398
+
399
+ /**
400
+ * Set the full-scale measurement range.
401
+ *
402
+ * @param {number} rangeG - 2, 4 or 8 (*g*).
403
+ * @returns {Promise<void>}
404
+ * @throws {Error} If the range is not 2/4/8, or ±8 *g* is requested while low-noise mode is on.
405
+ */
406
+ async setRange(rangeG) {
407
+ if (!(rangeG in _RANGE_FS)) {
408
+ throw new Error('rangeG must be one of 2, 4, 8');
409
+ }
410
+ if (rangeG === 8 && this._lowNoise) {
411
+ throw new Error('range ±8 g is not available while low-noise mode is enabled');
412
+ }
413
+ this._rangeG = rangeG;
414
+ await this._withStandby(async () => {
415
+ const cfg = await this._readReg(_REG_XYZ_DATA_CFG);
416
+ const out = (cfg & ~_XDC_FS_MASK & 0xFF) | _RANGE_FS[rangeG];
417
+ await this._writeReg(_REG_XYZ_DATA_CFG, out);
418
+ });
419
+ }
420
+
421
+ /**
422
+ * Set the output data rate.
423
+ *
424
+ * @param {number} rateHz - One of 800, 400, 200, 100, 50, 12.5, 6.25, 1.56 (Hz).
425
+ * @returns {Promise<void>}
426
+ * @throws {Error} If the rate is not one of the supported values.
427
+ */
428
+ async setDataRate(rateHz) {
429
+ const entry = _ODR_CODES.find(e => Math.abs(e[0] - rateHz) < 1e-9);
430
+ if (!entry) {
431
+ throw new Error('rateHz must be one of 800, 400, 200, 100, 50, 12.5, 6.25, 1.56');
432
+ }
433
+ this._odrHz = entry[0];
434
+ await this._withStandby(async () => {
435
+ const cr1 = await this._readReg(_REG_CTRL_REG1);
436
+ const out = (cr1 & ~_CR1_DR_MASK & 0xFF) | (entry[1] << 3);
437
+ await this._writeReg(_REG_CTRL_REG1, out);
438
+ });
439
+ }
440
+
441
+ /**
442
+ * Set the oversampling mode used in WAKE/ACTIVE mode (and optionally in
443
+ * SLEEP mode).
444
+ *
445
+ * @param {number} mode - 0 normal, 1 low-noise low-power, 2 high resolution, 3 low power.
446
+ * @param {number} [sleepMode=null] - Optional SLEEP-mode oversampling (0..3).
447
+ * @returns {Promise<void>}
448
+ * @throws {Error} If a mode is out of range.
449
+ */
450
+ async setOversampling(mode, sleepMode = null) {
451
+ if (!(mode >= 0 && mode <= 3)) {
452
+ throw new Error('mode must be 0..3');
453
+ }
454
+ if (sleepMode !== null && !(sleepMode >= 0 && sleepMode <= 3)) {
455
+ throw new Error('sleepMode must be 0..3');
456
+ }
457
+ this._oversampling = mode;
458
+ await this._withStandby(async () => {
459
+ const cr2 = await this._readReg(_REG_CTRL_REG2);
460
+ let out = (cr2 & ~_CR2_MODS & 0xFF) | (mode & 0x03);
461
+ if (sleepMode !== null) {
462
+ this._sleepOversampling = sleepMode;
463
+ out = (out & ~_CR2_SMODS & 0xFF) | ((sleepMode & 0x03) << 3);
464
+ }
465
+ await this._writeReg(_REG_CTRL_REG2, out);
466
+ });
467
+ }
468
+
469
+ /**
470
+ * Enable or disable the reduced-noise (LNOISE) mode.
471
+ *
472
+ * Low-noise mode limits the dynamic range to ±4 *g*.
473
+ *
474
+ * @param {boolean} enabled - True to enable reduced-noise mode.
475
+ * @returns {Promise<void>}
476
+ * @throws {Error} If the range is currently ±8 *g*.
477
+ */
478
+ async setLowNoise(enabled) {
479
+ if (enabled && this._rangeG === 8) {
480
+ throw new Error('low-noise mode is not available with the ±8 g range');
481
+ }
482
+ this._lowNoise = !!enabled;
483
+ await this._withStandby(async () => {
484
+ const cr1 = await this._readReg(_REG_CTRL_REG1);
485
+ const out = enabled ? (cr1 | _CR1_LNOISE) : (cr1 & ~_CR1_LNOISE & 0xFF);
486
+ await this._writeReg(_REG_CTRL_REG1, out);
487
+ });
488
+ }
489
+
490
+ /**
491
+ * Select the output resolution.
492
+ *
493
+ * 14-bit gives 4096 counts/*g* at ±2 *g*; 8-bit (fast read) gives
494
+ * 64 counts/*g* and only the MSB registers carry data.
495
+ *
496
+ * @param {number} bits - 14 or 8.
497
+ * @returns {Promise<void>}
498
+ * @throws {Error} If the resolution is not 14 or 8, or the FIFO is enabled.
499
+ */
500
+ async setResolution(bits) {
501
+ if (bits !== 14 && bits !== 8) {
502
+ throw new Error('bits must be 14 or 8');
503
+ }
504
+ if (this._fifoMode !== 0) {
505
+ throw new Error('the resolution cannot be changed while the FIFO is enabled');
506
+ }
507
+ this._fastRead = (bits === 8);
508
+ await this._withStandby(async () => {
509
+ const cr1 = await this._readReg(_REG_CTRL_REG1);
510
+ const out = this._fastRead ? (cr1 | _CR1_F_READ) : (cr1 & ~_CR1_F_READ & 0xFF);
511
+ await this._writeReg(_REG_CTRL_REG1, out);
512
+ });
513
+ }
514
+
515
+ /**
516
+ * Enable or disable the high-pass-filtered output.
517
+ *
518
+ * The cutoff frequency in Hz depends on the data rate and the
519
+ * oversampling mode (datasheet Table 23); `cutoff` is the raw `SEL`
520
+ * code 0..3.
521
+ *
522
+ * @param {boolean} enabled - True to output high-pass-filtered data.
523
+ * @param {number} [cutoff=0] - High-pass cutoff selection code (0..3).
524
+ * @returns {Promise<void>}
525
+ * @throws {Error} If the cutoff code is out of range.
526
+ */
527
+ async setHighPassOutput(enabled, cutoff = 0) {
528
+ if (!(cutoff >= 0 && cutoff <= 3)) {
529
+ throw new Error('cutoff must be 0..3');
530
+ }
531
+ this._hpfOut = !!enabled;
532
+ await this._withStandby(async () => {
533
+ const xdc = await this._readReg(_REG_XYZ_DATA_CFG);
534
+ const xdcOut = enabled ? (xdc | _XDC_HPF_OUT) : (xdc & ~_XDC_HPF_OUT & 0xFF);
535
+ await this._writeReg(_REG_XYZ_DATA_CFG, xdcOut);
536
+
537
+ const hpf = await this._readReg(_REG_HP_FILTER);
538
+ const hpfOut = (hpf & ~_HPF_SEL_MASK & 0xFF) | (cutoff & 0x03);
539
+ await this._writeReg(_REG_HP_FILTER, hpfOut);
540
+ });
541
+ }
542
+
543
+ // -- power modes and reset ----------------------------------------------
544
+
545
+ /**
546
+ * Put the chip into STANDBY mode.
547
+ *
548
+ * @returns {Promise<void>}
549
+ */
550
+ async standby() {
551
+ const cr1 = await this._readReg(_REG_CTRL_REG1);
552
+ if ((cr1 & _CR1_ACTIVE) === 0) {
553
+ this._active = false;
554
+ return;
555
+ }
556
+ await this._writeReg(_REG_CTRL_REG1, cr1 & ~_CR1_ACTIVE & 0xFF);
557
+ this._active = false;
558
+ }
559
+
560
+ /**
561
+ * Put the chip into ACTIVE mode and wait for the turn-on time.
562
+ *
563
+ * @returns {Promise<void>}
564
+ */
565
+ async activate() {
566
+ const cr1 = await this._readReg(_REG_CTRL_REG1);
567
+ if ((cr1 & _CR1_ACTIVE) !== 0) {
568
+ this._active = true;
569
+ await this._waitTurnOn();
570
+ return;
571
+ }
572
+ await this._writeReg(_REG_CTRL_REG1, cr1 | _CR1_ACTIVE);
573
+ this._active = true;
574
+ await this._waitTurnOn();
575
+ }
576
+
577
+ /**
578
+ * Perform a software reset.
579
+ *
580
+ * All registers return to their reset values and the chip ends up in
581
+ * STANDBY; run {@link MMA8451QMinimal#init} afterwards to reconfigure it.
582
+ *
583
+ * @returns {Promise<void>}
584
+ */
585
+ async reset() {
586
+ await this._writeReg(_REG_CTRL_REG2, _CR2_RST);
587
+ _delayMs(1);
588
+ const deadline = Date.now() + 10;
589
+ for (;;) {
590
+ let ctrl2 = 0;
591
+ try {
592
+ ctrl2 = await this._readReg(_REG_CTRL_REG2);
593
+ } catch (e) {
594
+ ctrl2 = 0xFF;
595
+ }
596
+ if ((ctrl2 & ~_CR2_RST & 0xFF) === 0) break;
597
+ if (Date.now() > deadline) {
598
+ throw new Error('MMA8451Q reset did not complete (CTRL_REG2 = 0x' + ctrl2.toString(16) + ')');
599
+ }
600
+ _delayMs(1);
601
+ }
602
+ this._active = false;
603
+ this._rangeG = 2;
604
+ this._odrHz = 100;
605
+ this._oversampling = 0;
606
+ this._sleepOversampling = 0;
607
+ this._lowNoise = false;
608
+ this._fastRead = false;
609
+ this._hpfOut = false;
610
+ this._fifoMode = 0;
611
+ }
612
+
613
+ // -- status --------------------------------------------------------------
614
+
615
+ /**
616
+ * Read the STATUS register.
617
+ * @returns {Promise<number>} STATUS byte (data-ready and overwrite flags).
618
+ */
619
+ async readStatus() {
620
+ return this._readReg(_REG_STATUS);
621
+ }
622
+
623
+ /**
624
+ * Check whether a new data sample is available.
625
+ *
626
+ * Not valid while the FIFO is enabled — use {@link MMA8451QFull#fifoLevel}.
627
+ *
628
+ * @returns {Promise<boolean>} True when `ZYXDR` is set.
629
+ */
630
+ async newDataAvailable() {
631
+ const status = await this._readReg(_REG_STATUS);
632
+ return (status & 0x08) !== 0;
633
+ }
634
+
635
+ /**
636
+ * Read the current system mode.
637
+ *
638
+ * @returns {Promise<number>} SYSMOD bits 1:0 — 0 standby, 1 wake, 2 sleep.
639
+ */
640
+ async readSystemMode() {
641
+ const sysmod = await this._readReg(_REG_SYSMOD);
642
+ return sysmod & 0x03;
643
+ }
644
+
645
+ /**
646
+ * Read the signed acceleration counts of one sample.
647
+ *
648
+ * @returns {Promise<number[]>} [x, y, z] signed counts at the current resolution.
649
+ */
650
+ async readRaw() {
651
+ return this._readCounts();
652
+ }
653
+
654
+ // -- offsets -------------------------------------------------------------
655
+
656
+ /**
657
+ * Set the per-axis offset correction registers.
658
+ *
659
+ * The offsets are 2 mg/LSB and limited to ±128 counts (±0.256 *g*).
660
+ *
661
+ * @param {number} xG - X offset in *g*.
662
+ * @param {number} yG - Y offset in *g*.
663
+ * @param {number} zG - Z offset in *g*.
664
+ * @returns {Promise<void>}
665
+ */
666
+ async setOffset(xG, yG, zG) {
667
+ const x = _clamp(Math.round(xG / 0.002), -128, 127) & 0xFF;
668
+ const y = _clamp(Math.round(yG / 0.002), -128, 127) & 0xFF;
669
+ const z = _clamp(Math.round(zG / 0.002), -128, 127) & 0xFF;
670
+ await this._withStandby(async () => {
671
+ await this._writeReg(_REG_OFF_X, x);
672
+ await this._writeReg(_REG_OFF_Y, y);
673
+ await this._writeReg(_REG_OFF_Z, z);
674
+ });
675
+ }
676
+
677
+ /**
678
+ * Read the per-axis offset correction registers.
679
+ * @returns {Promise<number[]>} [x, y, z] offsets in *g*.
680
+ */
681
+ async getOffset() {
682
+ const x = (await this._readReg(_REG_OFF_X) << 24 >> 24) * 0.002;
683
+ const y = (await this._readReg(_REG_OFF_Y) << 24 >> 24) * 0.002;
684
+ const z = (await this._readReg(_REG_OFF_Z) << 24 >> 24) * 0.002;
685
+ return [x, y, z];
686
+ }
687
+
688
+ // -- FIFO ----------------------------------------------------------------
689
+
690
+ /**
691
+ * Configure the 32-sample FIFO.
692
+ *
693
+ * The mode must pass through "disabled" when switching between two
694
+ * enabled modes; the driver does that automatically.
695
+ *
696
+ * @param {number} mode - 0 disabled, 1 circular, 2 fill, 3 trigger.
697
+ * @param {number} [watermark=0] - Watermark 0..32 (0 disables the flag).
698
+ * @returns {Promise<void>}
699
+ * @throws {Error} If the mode or the watermark is out of range.
700
+ */
701
+ async setFifo(mode, watermark = 0) {
702
+ if (!(mode >= 0 && mode <= 3)) {
703
+ throw new Error('mode must be 0..3');
704
+ }
705
+ if (!(watermark >= 0 && watermark <= 32)) {
706
+ throw new Error('watermark must be 0..32');
707
+ }
708
+ const wmrk = watermark === 32 ? 0 : watermark;
709
+ const wasEnabled = this._fifoMode !== 0;
710
+ this._fifoMode = mode;
711
+ await this._withStandby(async () => {
712
+ if (wasEnabled && mode !== 0) {
713
+ await this._writeReg(_REG_F_SETUP, 0x00);
714
+ }
715
+ const value = (mode << _FSETUP_MODE_SHIFT) | (wmrk & _FSETUP_WMRK_MASK);
716
+ await this._writeReg(_REG_F_SETUP, value);
717
+ });
718
+ }
719
+
720
+ /**
721
+ * Configure the FIFO trigger sources.
722
+ *
723
+ * @param {boolean} [transient=false] - Trigger on the transient detector.
724
+ * @param {boolean} [orientation=false] - Trigger on the orientation detector.
725
+ * @param {boolean} [pulse=false] - Trigger on the tap detector.
726
+ * @param {boolean} [motion=false] - Trigger on the freefall/motion detector.
727
+ * @returns {Promise<void>}
728
+ */
729
+ async setFifoTrigger(transient = false, orientation = false, pulse = false, motion = false) {
730
+ let value = 0;
731
+ if (transient) value |= _TRIG_TRANS;
732
+ if (orientation) value |= _TRIG_LNDPRT;
733
+ if (pulse) value |= _TRIG_PULSE;
734
+ if (motion) value |= _TRIG_FF_MT;
735
+ await this._withStandby(async () => {
736
+ await this._writeReg(_REG_TRIG_CFG, value);
737
+ });
738
+ }
739
+
740
+ /**
741
+ * Read the number of samples currently in the FIFO.
742
+ * @returns {Promise<number>} FIFO level, 0..32.
743
+ */
744
+ async fifoLevel() {
745
+ const fstatus = await this._readReg(_REG_STATUS);
746
+ return fstatus & 0x3F;
747
+ }
748
+
749
+ /**
750
+ * Read the FIFO status.
751
+ *
752
+ * Reading this register clears the FIFO interrupt.
753
+ *
754
+ * @returns {Promise<{overflow: boolean, watermark: boolean, count: number}>} Decoded F_STATUS.
755
+ */
756
+ async readFifoStatus() {
757
+ const fstatus = await this._readReg(_REG_STATUS);
758
+ return {
759
+ overflow: (fstatus & 0x80) !== 0,
760
+ watermark: (fstatus & 0x40) !== 0,
761
+ count: fstatus & 0x3F,
762
+ };
763
+ }
764
+
765
+ /**
766
+ * Drain the FIFO.
767
+ *
768
+ * Each sample is read in its own transaction starting at register 0x01,
769
+ * which is what the datasheet prescribes for FIFO reads. The FIFO level
770
+ * is polled after every sample so the drain stops as soon as the FIFO
771
+ * runs dry.
772
+ *
773
+ * @param {number} [count=null] - Number of samples to read (default: all).
774
+ * @returns {Promise<number[][]>} Array of [x, y, z] samples in *g*.
775
+ */
776
+ async readFifo(count = null) {
777
+ const samples = [];
778
+ const bytesPerSample = this._fastRead ? 3 : 6;
779
+ // The FIFO holds at most 32 samples; that also bounds the drain.
780
+ const max = count === null ? 32 : Math.min(count, 32);
781
+ for (;;) {
782
+ if (samples.length >= max) break;
783
+ const raw = await this._conn.readReg(_REG_OUT_X_MSB, bytesPerSample);
784
+ if (this._fastRead) {
785
+ samples.push([
786
+ (raw[0] << 24 >> 24),
787
+ (raw[1] << 24 >> 24),
788
+ (raw[2] << 24 >> 24),
789
+ ]);
790
+ } else {
791
+ const s = [];
792
+ for (let a = 0; a < 3; a++) {
793
+ const raw16 = (raw[a * 2] << 8) | raw[a * 2 + 1];
794
+ s.push((raw16 << 16 >> 16) >> 2);
795
+ }
796
+ samples.push(s);
797
+ }
798
+ // Stop when the FIFO runs dry.
799
+ const level = await this.fifoLevel();
800
+ if (level === 0) break;
801
+ }
802
+ const scale = this._fastRead
803
+ ? _RANGE_COUNTS_PER_G_8BIT[this._rangeG]
804
+ : _RANGE_COUNTS_PER_G[this._rangeG];
805
+ return samples.map(s => [s[0] / scale, s[1] / scale, s[2] / scale]);
806
+ }
807
+
808
+ // -- freefall / motion ---------------------------------------------------
809
+
810
+ /**
811
+ * Configure the freefall / motion detector.
812
+ *
813
+ * @param {boolean} motion - False for freefall (AND of the enabled axes), true for motion (OR).
814
+ * @param {number} [axes=7] - Enabled axes mask (X = 1, Y = 2, Z = 4).
815
+ * @param {number} [thresholdG=0] - Threshold in *g* (0.063 *g* per LSB, valid to 8 *g*).
816
+ * @param {number} [debounceMs=0] - Debounce time in ms.
817
+ * @param {boolean} [latch=false] - Latch the event flags until the source register is read.
818
+ * @param {boolean} [debounceClear=false] - Clear the debounce counter when the condition is lost.
819
+ * @returns {Promise<void>}
820
+ */
821
+ async setFreefallMotion(motion, axes = 7, thresholdG = 0, debounceMs = 0, latch = false, debounceClear = false) {
822
+ const ths = _clamp(Math.round(thresholdG / 0.063), 0, 127);
823
+ const count = this._debounceCount(debounceMs);
824
+ await this._withStandby(async () => {
825
+ let cfg = 0;
826
+ if (latch) cfg |= _FFMT_ELE;
827
+ if (motion) cfg |= _FFMT_OAE;
828
+ cfg |= (axes & 0x07) << 3;
829
+ await this._writeReg(_REG_FF_MT_CFG, cfg);
830
+
831
+ const thsOut = (debounceClear ? 0x80 : 0x00) | (ths & 0x7F);
832
+ await this._writeReg(_REG_FF_MT_THS, thsOut);
833
+ await this._writeReg(_REG_FF_MT_COUNT, count);
834
+ });
835
+ }
836
+
837
+ /**
838
+ * Read the freefall / motion source flags.
839
+ *
840
+ * @returns {Promise<number>} FF_MT_SRC byte (clears the latched flags).
841
+ */
842
+ async readMotionSource() {
843
+ return this._readReg(_REG_FF_MT_SRC);
844
+ }
845
+
846
+ // -- transient -----------------------------------------------------------
847
+
848
+ /**
849
+ * Configure the transient (high-pass filtered motion) detector.
850
+ *
851
+ * @param {number} [axes=7] - Enabled axes mask (X = 1, Y = 2, Z = 4).
852
+ * @param {number} [thresholdG=0] - Threshold in *g* (0.063 *g* per LSB).
853
+ * @param {number} [debounceMs=0] - Debounce time in ms.
854
+ * @param {boolean} [bypassHpf=false] - Bypass the high-pass filter (behaves like motion detection).
855
+ * @param {boolean} [latch=false] - Latch the event flags until the source register is read.
856
+ * @returns {Promise<void>}
857
+ */
858
+ async setTransient(axes = 7, thresholdG = 0, debounceMs = 0, bypassHpf = false, latch = false) {
859
+ const ths = _clamp(Math.round(thresholdG / 0.063), 0, 127);
860
+ const count = this._debounceCount(debounceMs);
861
+ await this._withStandby(async () => {
862
+ let cfg = 0;
863
+ if (latch) cfg |= _TRS_ELE;
864
+ cfg |= (axes & 0x07) << 1;
865
+ if (bypassHpf) cfg |= _TRS_HPF;
866
+ await this._writeReg(_REG_TRANSIENT_CFG, cfg);
867
+ await this._writeReg(_REG_TRANSIENT_THS, ths & 0x7F);
868
+ await this._writeReg(_REG_TRANSIENT_COUNT, count);
869
+ });
870
+ }
871
+
872
+ /**
873
+ * Read the transient source flags.
874
+ *
875
+ * Reading this register clears the interrupt.
876
+ *
877
+ * @returns {Promise<number>} TRANSIENT_SRC byte.
878
+ */
879
+ async readTransientSource() {
880
+ return this._readReg(_REG_TRANSIENT_SRC);
881
+ }
882
+
883
+ // -- tap (pulse) ---------------------------------------------------------
884
+
885
+ /**
886
+ * Configure the single / double tap detector.
887
+ *
888
+ * @param {number} [single=0] - Single-tap enabled axes mask (X = 1, Y = 2, Z = 4).
889
+ * @param {number} [double=0] - Double-tap enabled axes mask (X = 1, Y = 2, Z = 4).
890
+ * @param {number[]} [thresholdG=[0.5, 0.5, 0.5]] - Per-axis thresholds in *g* (X, Y, Z).
891
+ * @param {number} [timeLimitMs=30] - Pulse time limit in ms.
892
+ * @param {number} [latencyMs=100] - Latency time in ms.
893
+ * @param {number} [windowMs=300] - Second-pulse window in ms.
894
+ * @param {boolean} [latch=false] - Latch the event flags until the source register is read.
895
+ * @param {boolean} [doubleAbort=false] - Abort double-tap detection when a pulse starts during the latency window.
896
+ * @param {boolean} [lpf=true] - Enable the low-pass filter for the pulse detector.
897
+ * @param {boolean} [hpf=true] - Enable the high-pass filter for the pulse detector.
898
+ * @returns {Promise<void>}
899
+ */
900
+ async setPulse(single = 0, double = 0, thresholdG = [0.5, 0.5, 0.5],
901
+ timeLimitMs = 30, latencyMs = 100, windowMs = 300,
902
+ latch = false, doubleAbort = false, lpf = true, hpf = true) {
903
+ const thsx = _clamp(Math.round(thresholdG[0] / 0.063), 0, 127);
904
+ const thsy = _clamp(Math.round(thresholdG[1] / 0.063), 0, 127);
905
+ const thsz = _clamp(Math.round(thresholdG[2] / 0.063), 0, 127);
906
+
907
+ const tmlt = this._pulseCount(timeLimitMs, lpf, 0);
908
+ const ltcy = this._pulseCount(latencyMs, lpf, 4);
909
+ const wind = this._pulseCount(windowMs, lpf, 4);
910
+
911
+ await this._withStandby(async () => {
912
+ let cfg = 0;
913
+ if (doubleAbort) cfg |= _PULSE_DPA;
914
+ if (latch) cfg |= _PULSE_ELE;
915
+ cfg |= (double & 0x07) | ((single & 0x07) << 3);
916
+ await this._writeReg(_REG_PULSE_CFG, cfg);
917
+
918
+ await this._writeReg(_REG_PULSE_THSX, thsx);
919
+ await this._writeReg(_REG_PULSE_THSY, thsy);
920
+ await this._writeReg(_REG_PULSE_THSZ, thsz);
921
+ await this._writeReg(_REG_PULSE_TMLT, tmlt);
922
+ await this._writeReg(_REG_PULSE_LTCY, ltcy);
923
+ await this._writeReg(_REG_PULSE_WIND, wind);
924
+
925
+ const hpfReg = await this._readReg(_REG_HP_FILTER);
926
+ let out = hpfReg & ~(_HPF_PULSE_HPF_BYP | _HPF_PULSE_LPF_EN) & 0xFF;
927
+ if (!hpf) out |= _HPF_PULSE_HPF_BYP;
928
+ if (lpf) out |= _HPF_PULSE_LPF_EN;
929
+ await this._writeReg(_REG_HP_FILTER, out);
930
+ });
931
+ }
932
+
933
+ /**
934
+ * Read the tap source flags.
935
+ *
936
+ * Reading this register clears the interrupt.
937
+ *
938
+ * @returns {Promise<number>} PULSE_SRC byte.
939
+ */
940
+ async readPulseSource() {
941
+ return this._readReg(_REG_PULSE_SRC);
942
+ }
943
+
944
+ // -- orientation ---------------------------------------------------------
945
+
946
+ /**
947
+ * Configure the portrait/landscape + back/front orientation detector.
948
+ *
949
+ * @param {boolean} [enabled=true] - Enable the orientation detector.
950
+ * @param {number} [debounceMs=0] - Debounce time in ms.
951
+ * @param {number} [thresholdDeg=45] - Trip angle: 15, 20, 30, 35, 40, 45, 55, 60, 70 or 75 degrees.
952
+ * @param {number} [hysteresis=4] - Hysteresis code 0..7 (±0/±4/±7/±11/±14/±17/±21/±24 degrees).
953
+ * @param {number} [zLock=4] - Z-lock code 0..7 (14/18/21/25/29/33/37/42 degrees).
954
+ * @param {number} [backFront=1] - Back/front threshold code 0..3.
955
+ * @returns {Promise<void>}
956
+ * @throws {Error} If the trip angle is not a Table 32 value, or `THS + HYS` is outside 1..31.
957
+ */
958
+ async setOrientation(enabled = true, debounceMs = 0, thresholdDeg = 45, hysteresis = 4,
959
+ zLock = 4, backFront = 1) {
960
+ const thsEntry = _ORIENTATION_THRESHOLDS.find(e => e[0] === thresholdDeg);
961
+ if (!thsEntry) {
962
+ throw new Error('thresholdDeg must be one of 15, 20, 30, 35, 40, 45, 55, 60, 70, 75');
963
+ }
964
+ if (!(hysteresis >= 0 && hysteresis <= 7)) {
965
+ throw new Error('hysteresis must be 0..7');
966
+ }
967
+ const ths = thsEntry[1];
968
+ if (ths + hysteresis < 1 || ths + hysteresis > 31) {
969
+ throw new Error('orientation threshold plus hysteresis must be in 1..31');
970
+ }
971
+ if (!(zLock >= 0 && zLock <= 7)) {
972
+ throw new Error('zLock must be 0..7');
973
+ }
974
+ if (!(backFront >= 0 && backFront <= 3)) {
975
+ throw new Error('backFront must be 0..3');
976
+ }
977
+ const count = this._debounceCount(debounceMs);
978
+ await this._withStandby(async () => {
979
+ const cfg = await this._readReg(_REG_PL_CFG);
980
+ const out = enabled ? (cfg | _PL_EN) : (cfg & ~_PL_EN & 0xFF);
981
+ await this._writeReg(_REG_PL_CFG, out);
982
+ await this._writeReg(_REG_PL_COUNT, count);
983
+ await this._writeReg(_REG_PL_BF_ZCOMP, ((backFront & 0x03) << 6) | (zLock & 0x07));
984
+ await this._writeReg(_REG_PL_THS, ((ths & 0x1F) << _PL_THS_SHIFT) | (hysteresis & 0x07));
985
+ });
986
+ }
987
+
988
+ /**
989
+ * Read the orientation status.
990
+ *
991
+ * Reading this register clears the `NEWLP` flag.
992
+ *
993
+ * @returns {Promise<{changed: boolean, lockout: boolean, portraitLandscape: number, back: boolean}>} Decoded PL_STATUS.
994
+ */
995
+ async readOrientation() {
996
+ const status = await this._readReg(_REG_PL_STATUS);
997
+ return {
998
+ changed: (status & 0x80) !== 0,
999
+ lockout: (status & 0x40) !== 0,
1000
+ portraitLandscape: (status >> 1) & 0x03,
1001
+ back: (status & 0x01) !== 0,
1002
+ };
1003
+ }
1004
+
1005
+ // -- Auto-SLEEP / Auto-WAKE ---------------------------------------------
1006
+
1007
+ /**
1008
+ * Configure Auto-SLEEP and the wake sources.
1009
+ *
1010
+ * @param {boolean} enabled - Enable Auto-SLEEP.
1011
+ * @param {number} [sleepRateHz=50] - Sleep-mode output rate: 50, 12.5, 6.25 or 1.56 Hz.
1012
+ * @param {number} [timeoutS=10] - Inactivity timeout in seconds (0..81 s; 0..162 s at 1.56 Hz).
1013
+ * @param {number} [wakeSources=0] - Mask of wake sources (SOURCE_TRANSIENT, SOURCE_ORIENTATION, SOURCE_PULSE, SOURCE_FF_MT).
1014
+ * @returns {Promise<void>}
1015
+ * @throws {Error} If the sleep rate or a wake source is invalid.
1016
+ */
1017
+ async setAutoSleep(enabled, sleepRateHz = 50, timeoutS = 10, wakeSources = 0) {
1018
+ const rateEntry = _ASLP_RATE_CODES.find(e => Math.abs(e[0] - sleepRateHz) < 1e-9);
1019
+ if (!rateEntry) {
1020
+ throw new Error('sleepRateHz must be one of 50, 12.5, 6.25, 1.56');
1021
+ }
1022
+ const valid = MMA8451QFull.SOURCE_TRANSIENT | MMA8451QFull.SOURCE_ORIENTATION
1023
+ | MMA8451QFull.SOURCE_PULSE | MMA8451QFull.SOURCE_FF_MT;
1024
+ if (wakeSources & ~valid) {
1025
+ throw new Error('wakeSources may only contain transient, orientation, pulse and freefall/motion');
1026
+ }
1027
+ const stepS = this._odrHz >= 6.25 ? 0.32 : 0.64;
1028
+ const count = _clamp(Math.round(timeoutS / stepS), 0, 255);
1029
+
1030
+ await this._withStandby(async () => {
1031
+ const cr2 = await this._readReg(_REG_CTRL_REG2);
1032
+ const cr2Out = enabled ? (cr2 | _CR2_SLPE) : (cr2 & ~_CR2_SLPE & 0xFF);
1033
+ await this._writeReg(_REG_CTRL_REG2, cr2Out);
1034
+
1035
+ const cr1 = await this._readReg(_REG_CTRL_REG1);
1036
+ const cr1Out = (cr1 & ~_CR1_ASLP_RATE_MASK & 0xFF) | (rateEntry[1] << 6);
1037
+ await this._writeReg(_REG_CTRL_REG1, cr1Out);
1038
+
1039
+ await this._writeReg(_REG_ASLP_COUNT, count);
1040
+
1041
+ const cr3 = await this._readReg(_REG_CTRL_REG3);
1042
+ let wake = 0;
1043
+ if (wakeSources & MMA8451QFull.SOURCE_TRANSIENT) wake |= _CR3_WAKE_TRANS;
1044
+ if (wakeSources & MMA8451QFull.SOURCE_ORIENTATION) wake |= _CR3_WAKE_LNDPRT;
1045
+ if (wakeSources & MMA8451QFull.SOURCE_PULSE) wake |= _CR3_WAKE_PULSE;
1046
+ if (wakeSources & MMA8451QFull.SOURCE_FF_MT) wake |= _CR3_WAKE_FF_MT;
1047
+ const cr3Out = (cr3 & ~_CR3_WAKE_MASK & 0xFF) | wake;
1048
+ await this._writeReg(_REG_CTRL_REG3, cr3Out);
1049
+ });
1050
+ }
1051
+
1052
+ /**
1053
+ * Enable or disable the FIFO gate.
1054
+ *
1055
+ * With the gate enabled the FIFO blocks new input around WAKE/SLEEP
1056
+ * transitions until it has been emptied.
1057
+ *
1058
+ * @param {boolean} enabled - True to gate the FIFO.
1059
+ * @returns {Promise<void>}
1060
+ */
1061
+ async setFifoGate(enabled) {
1062
+ await this._withStandby(async () => {
1063
+ const cr3 = await this._readReg(_REG_CTRL_REG3);
1064
+ const out = enabled ? (cr3 | _CR3_FIFO_GATE) : (cr3 & ~_CR3_FIFO_GATE & 0xFF);
1065
+ await this._writeReg(_REG_CTRL_REG3, out);
1066
+ });
1067
+ }
1068
+
1069
+ /**
1070
+ * Configure the interrupt pin polarity and drive.
1071
+ *
1072
+ * Both INT1 and INT2 share these settings.
1073
+ *
1074
+ * @param {boolean} [activeHigh=false] - True for active-high interrupts (default active-low).
1075
+ * @param {boolean} [openDrain=false] - True for open-drain output (default push-pull).
1076
+ * @returns {Promise<void>}
1077
+ */
1078
+ async setInterruptPins(activeHigh = false, openDrain = false) {
1079
+ await this._withStandby(async () => {
1080
+ const cr3 = await this._readReg(_REG_CTRL_REG3);
1081
+ let out = cr3 & ~(_CR3_IPOL | _CR3_PP_OD) & 0xFF;
1082
+ if (activeHigh) out |= _CR3_IPOL;
1083
+ if (openDrain) out |= _CR3_PP_OD;
1084
+ await this._writeReg(_REG_CTRL_REG3, out);
1085
+ });
1086
+ }
1087
+
1088
+ // -- interrupt API -------------------------------------------------------
1089
+
1090
+ /**
1091
+ * Enable an interrupt source and route it to INT1 or INT2.
1092
+ *
1093
+ * @param {number} source - One of the SOURCE_* constants.
1094
+ * @param {number} [pin=1] - 1 for INT1, 2 for INT2.
1095
+ * @returns {Promise<void>}
1096
+ * @throws {Error} If the source or the pin is invalid.
1097
+ */
1098
+ async enableInterrupt(source, pin = 1) {
1099
+ if (!(pin === 1 || pin === 2)) {
1100
+ throw new Error('pin must be 1 or 2');
1101
+ }
1102
+ if (!_isValidSource(source)) {
1103
+ throw new Error('unknown interrupt source');
1104
+ }
1105
+ await this._withStandby(async () => {
1106
+ const cr4 = await this._readReg(_REG_CTRL_REG4);
1107
+ await this._writeReg(_REG_CTRL_REG4, cr4 | source);
1108
+
1109
+ const cr5 = await this._readReg(_REG_CTRL_REG5);
1110
+ const out = pin === 1 ? (cr5 | source) : (cr5 & ~source & 0xFF);
1111
+ await this._writeReg(_REG_CTRL_REG5, out);
1112
+ });
1113
+ }
1114
+
1115
+ /**
1116
+ * Disable an interrupt source.
1117
+ *
1118
+ * @param {number} source - One of the SOURCE_* constants.
1119
+ * @returns {Promise<void>}
1120
+ * @throws {Error} If the source is invalid.
1121
+ */
1122
+ async disableInterrupt(source) {
1123
+ if (!_isValidSource(source)) {
1124
+ throw new Error('unknown interrupt source');
1125
+ }
1126
+ await this._withStandby(async () => {
1127
+ const cr4 = await this._readReg(_REG_CTRL_REG4);
1128
+ await this._writeReg(_REG_CTRL_REG4, cr4 & ~source & 0xFF);
1129
+ });
1130
+ }
1131
+
1132
+ /**
1133
+ * Read the interrupt status and clear the latched flags.
1134
+ *
1135
+ * Reads `INT_SOURCE` and returns it. For every set bit except
1136
+ * `SOURCE_DATA_READY` the matching source register is read as well, so
1137
+ * that the flag and the interrupt pin are cleared. `SOURCE_DATA_READY`
1138
+ * stays set — read the data registers to clear it.
1139
+ *
1140
+ * @returns {Promise<number>} INT_SOURCE bitmask.
1141
+ */
1142
+ async pollInterrupt() {
1143
+ const status = await this._readReg(_REG_INT_SOURCE);
1144
+ if (status & MMA8451QFull.SOURCE_FIFO) {
1145
+ await this._readReg(_REG_STATUS);
1146
+ }
1147
+ if (status & MMA8451QFull.SOURCE_FF_MT) {
1148
+ await this._readReg(_REG_FF_MT_SRC);
1149
+ }
1150
+ if (status & MMA8451QFull.SOURCE_PULSE) {
1151
+ await this._readReg(_REG_PULSE_SRC);
1152
+ }
1153
+ if (status & MMA8451QFull.SOURCE_ORIENTATION) {
1154
+ await this._readReg(_REG_PL_STATUS);
1155
+ }
1156
+ if (status & MMA8451QFull.SOURCE_TRANSIENT) {
1157
+ await this._readReg(_REG_TRANSIENT_SRC);
1158
+ }
1159
+ if (status & MMA8451QFull.SOURCE_AUTO_SLEEP) {
1160
+ await this._readReg(_REG_SYSMOD);
1161
+ }
1162
+ return status;
1163
+ }
1164
+
1165
+ /**
1166
+ * Subscribe to interrupt events on INT1 or INT2.
1167
+ *
1168
+ * Uses `connection.intPin.onEdge()` when an InputPin is wired — the chip
1169
+ * drives its interrupt pins active-low by default, so a falling edge is
1170
+ * expected unless {@link MMA8451QFull#setInterruptPins} changed the
1171
+ * polarity. Without an InputPin the driver falls back to a 5 ms polling
1172
+ * loop that reports every status change.
1173
+ *
1174
+ * @param {function(number): void} callback - Receives the `pollInterrupt()` mask.
1175
+ * @param {number} [pin=1] - 1 for INT1, 2 for INT2.
1176
+ * @returns {Promise<void>}
1177
+ */
1178
+ async onInterrupt(callback, pin = 1) {
1179
+ if (!(pin === 1 || pin === 2)) {
1180
+ throw new Error('pin must be 1 or 2');
1181
+ }
1182
+ await this.offInterrupt(pin);
1183
+ this._callback = callback;
1184
+ const intPin = this._conn.intPin;
1185
+ if (intPin) {
1186
+ let activeHigh = false;
1187
+ try {
1188
+ const cr3 = await this._readReg(_REG_CTRL_REG3);
1189
+ activeHigh = (cr3 & _CR3_IPOL) !== 0;
1190
+ } catch (e) { /* keep the default polarity */ }
1191
+ this._edgeHandler = async () => {
1192
+ const status = await this.pollInterrupt();
1193
+ if (this._callback) this._callback(status);
1194
+ };
1195
+ await intPin.onEdge(this._edgeHandler, activeHigh ? 'rising' : 'falling');
1196
+ } else {
1197
+ let last = await this.pollInterrupt();
1198
+ let busy = false;
1199
+ this._pollTimer = setInterval(async () => {
1200
+ if (busy) return;
1201
+ busy = true;
1202
+ try {
1203
+ const status = await this.pollInterrupt();
1204
+ if (status !== last) {
1205
+ last = status;
1206
+ if (this._callback) this._callback(status);
1207
+ }
1208
+ } finally {
1209
+ busy = false;
1210
+ }
1211
+ }, 5);
1212
+ }
1213
+ }
1214
+
1215
+ /**
1216
+ * Unsubscribe from interrupt events.
1217
+ *
1218
+ * @param {number} [pin=1] - 1 for INT1, 2 for INT2.
1219
+ * @returns {Promise<void>}
1220
+ */
1221
+ async offInterrupt(pin = 1) {
1222
+ if (this._conn.intPin && this._edgeHandler) {
1223
+ await this._conn.intPin.offEdge(this._edgeHandler);
1224
+ this._edgeHandler = null;
1225
+ }
1226
+ if (this._pollTimer) {
1227
+ clearInterval(this._pollTimer);
1228
+ this._pollTimer = null;
1229
+ }
1230
+ this._callback = null;
1231
+ }
1232
+
1233
+ // -- self-test -----------------------------------------------------------
1234
+
1235
+ /**
1236
+ * Run the electrostatic self-test.
1237
+ *
1238
+ * Puts the chip into STANDBY, configures ±4 *g* at 100 Hz with normal
1239
+ * oversampling, averages a few samples, applies the electrostatic force,
1240
+ * averages again and reports the count deltas. The previous configuration
1241
+ * is restored afterwards.
1242
+ *
1243
+ * @returns {Promise<{ok: boolean, dx: number, dy: number, dz: number}>} Self-test result.
1244
+ */
1245
+ async selfTest() {
1246
+ const savedRange = this._rangeG;
1247
+ const savedOdr = this._odrHz;
1248
+ const savedOversampling = this._oversampling;
1249
+
1250
+ await this.setRange(4);
1251
+ await this.setDataRate(100);
1252
+ await this.setOversampling(0);
1253
+ await this.activate();
1254
+
1255
+ const before = await this._averageCounts(4);
1256
+ await this._writeReg(_REG_CTRL_REG2, _CR2_ST);
1257
+ _delayMs((2.0 / this._odrHz) * 1000.0 + 2.0);
1258
+ const after = await this._averageCounts(4);
1259
+ await this._writeReg(_REG_CTRL_REG2, 0x00);
1260
+
1261
+ const dx = after[0] - before[0];
1262
+ const dy = after[1] - before[1];
1263
+ const dz = after[2] - before[2];
1264
+ const ok = dx > _SELF_TEST_X / 2 && dy > _SELF_TEST_Y / 2 && dz > _SELF_TEST_Z / 2;
1265
+
1266
+ await this.setRange(savedRange);
1267
+ await this.setDataRate(savedOdr);
1268
+ await this.setOversampling(savedOversampling);
1269
+
1270
+ return { ok, dx, dy, dz };
1271
+ }
1272
+
1273
+ /**
1274
+ * Average `n` samples of the raw counts.
1275
+ * @param {number} n - Number of samples.
1276
+ * @returns {Promise<number[]>} [x, y, z] mean counts.
1277
+ */
1278
+ async _averageCounts(n) {
1279
+ const sum = [0, 0, 0];
1280
+ for (let i = 0; i < n; i++) {
1281
+ const s = await this._readCounts();
1282
+ for (let a = 0; a < 3; a++) sum[a] += s[a];
1283
+ }
1284
+ return sum.map(v => v / n);
1285
+ }
1286
+
1287
+ // -- helpers -------------------------------------------------------------
1288
+
1289
+ /**
1290
+ * Run `fn` with the chip temporarily in STANDBY, then restore ACTIVE.
1291
+ *
1292
+ * Most configuration registers may only be written while
1293
+ * `CTRL_REG1.ACTIVE` is 0; this helper takes care of that and of the
1294
+ * turn-on wait afterwards.
1295
+ *
1296
+ * @param {function(): Promise<void>} fn - The register write(s) to perform.
1297
+ * @returns {Promise<void>}
1298
+ */
1299
+ async _withStandby(fn) {
1300
+ const cr1 = await this._readReg(_REG_CTRL_REG1);
1301
+ const wasActive = (cr1 & _CR1_ACTIVE) !== 0;
1302
+ if (wasActive) {
1303
+ await this._writeReg(_REG_CTRL_REG1, cr1 & ~_CR1_ACTIVE & 0xFF);
1304
+ }
1305
+ try {
1306
+ await fn();
1307
+ } finally {
1308
+ if (wasActive) {
1309
+ const now = await this._readReg(_REG_CTRL_REG1);
1310
+ await this._writeReg(_REG_CTRL_REG1, now | _CR1_ACTIVE);
1311
+ this._active = true;
1312
+ await this._waitTurnOn();
1313
+ } else {
1314
+ this._active = false;
1315
+ }
1316
+ }
1317
+ }
1318
+
1319
+ /**
1320
+ * Convert a debounce time in ms into a debounce counter value.
1321
+ * @param {number} debounceMs - Debounce time in ms.
1322
+ * @returns {number} Counter value 0..255.
1323
+ */
1324
+ _debounceCount(debounceMs) {
1325
+ const steps = _DEBOUNCE_STEPS[this._odrHz] || _DEBOUNCE_STEPS[100];
1326
+ const step = steps[this._oversampling] || steps[0];
1327
+ return _clamp(Math.round(debounceMs / step), 0, 255);
1328
+ }
1329
+
1330
+ /**
1331
+ * Convert a pulse time in ms into a PULSE_TMLT / PULSE_LTCY / PULSE_WIND
1332
+ * counter value.
1333
+ * @param {number} timeMs - Time in ms.
1334
+ * @param {boolean} lpf - True when the low-pass filter is enabled.
1335
+ * @param {number} offset - 0 for TMLT, 4 for LTCY/WIND within the step table.
1336
+ * @returns {number} Counter value 0..255.
1337
+ */
1338
+ _pulseCount(timeMs, lpf, offset) {
1339
+ const table = lpf ? _PULSE_STEPS_LPF : _PULSE_STEPS_NO_LPF;
1340
+ const steps = table[this._odrHz] || table[100];
1341
+ const step = steps[offset + this._oversampling];
1342
+ return _clamp(Math.round(timeMs / step), 0, 255);
1343
+ }
1344
+ }
1345
+
1346
+ /**
1347
+ * New XYZ sample (`ZYXDR` / `ZYXOW`).
1348
+ * @constant {number}
1349
+ */
1350
+ const SOURCE_DATA_READY = 0x01;
1351
+ /**
1352
+ * Freefall / motion event (`FF_MT_SRC.EA`).
1353
+ * @constant {number}
1354
+ */
1355
+ const SOURCE_FF_MT = 0x04;
1356
+ /**
1357
+ * Single / double tap event (`PULSE_SRC.EA`).
1358
+ * @constant {number}
1359
+ */
1360
+ const SOURCE_PULSE = 0x08;
1361
+ /**
1362
+ * Orientation change (`PL_STATUS.NEWLP`).
1363
+ * @constant {number}
1364
+ */
1365
+ const SOURCE_ORIENTATION = 0x10;
1366
+ /**
1367
+ * Transient event (`TRANSIENT_SRC.EA`).
1368
+ * @constant {number}
1369
+ */
1370
+ const SOURCE_TRANSIENT = 0x20;
1371
+ /**
1372
+ * FIFO overflow or watermark reached.
1373
+ * @constant {number}
1374
+ */
1375
+ const SOURCE_FIFO = 0x40;
1376
+ /**
1377
+ * WAKE ↔ SLEEP transition.
1378
+ * @constant {number}
1379
+ */
1380
+ const SOURCE_AUTO_SLEEP = 0x80;
1381
+
1382
+ function _isValidSource(source) {
1383
+ return source === SOURCE_DATA_READY || source === SOURCE_FF_MT || source === SOURCE_PULSE
1384
+ || source === SOURCE_ORIENTATION || source === SOURCE_TRANSIENT
1385
+ || source === SOURCE_FIFO || source === SOURCE_AUTO_SLEEP;
1386
+ }
1387
+
1388
+ MMA8451QFull.SOURCE_DATA_READY = SOURCE_DATA_READY;
1389
+ MMA8451QFull.SOURCE_FF_MT = SOURCE_FF_MT;
1390
+ MMA8451QFull.SOURCE_PULSE = SOURCE_PULSE;
1391
+ MMA8451QFull.SOURCE_ORIENTATION = SOURCE_ORIENTATION;
1392
+ MMA8451QFull.SOURCE_TRANSIENT = SOURCE_TRANSIENT;
1393
+ MMA8451QFull.SOURCE_FIFO = SOURCE_FIFO;
1394
+ MMA8451QFull.SOURCE_AUTO_SLEEP = SOURCE_AUTO_SLEEP;
1395
+
1396
+ module.exports = {
1397
+ MMA8451QMinimal,
1398
+ MMA8451QFull,
1399
+ SOURCE_DATA_READY,
1400
+ SOURCE_FF_MT,
1401
+ SOURCE_PULSE,
1402
+ SOURCE_ORIENTATION,
1403
+ SOURCE_TRANSIENT,
1404
+ SOURCE_FIFO,
1405
+ SOURCE_AUTO_SLEEP,
1406
+ };