periph 1.3.1 → 1.3.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (76) hide show
  1. package/README.md +18 -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/display/pcf8578/complete.js +58 -0
  39. package/examples/display/pcf8578/demo.js +62 -0
  40. package/examples/display/pcf8578/minimal.js +19 -0
  41. package/examples/display/pcf8579/complete.js +58 -0
  42. package/examples/display/pcf8579/demo.js +61 -0
  43. package/examples/display/pcf8579/minimal.js +19 -0
  44. package/examples/imu/icm20948/complete.js +114 -0
  45. package/examples/imu/icm20948/demo.js +79 -0
  46. package/examples/imu/icm20948/minimal.js +22 -0
  47. package/examples/io_expander/mcp23008/complete.js +70 -0
  48. package/examples/io_expander/mcp23008/demo.js +58 -0
  49. package/examples/io_expander/mcp23008/minimal.js +20 -0
  50. package/examples/memory/mb85rc/complete.js +40 -0
  51. package/examples/memory/mb85rc/demo.js +58 -0
  52. package/examples/memory/mb85rc/minimal.js +15 -0
  53. package/examples/motor/drv2605/complete.js +100 -0
  54. package/examples/motor/drv2605/demo.js +55 -0
  55. package/examples/motor/drv2605/minimal.js +24 -0
  56. package/package.json +1 -1
  57. package/src/chips/accelerometer/bma180.js +573 -0
  58. package/src/chips/accelerometer/fxos8700.js +1600 -0
  59. package/src/chips/accelerometer/lis3dh.js +1341 -0
  60. package/src/chips/accelerometer/lsm303.js +1471 -0
  61. package/src/chips/accelerometer/mma8451q.js +1406 -0
  62. package/src/chips/adc_dac/_ads111x_base.js +272 -0
  63. package/src/chips/adc_dac/ad5243.js +231 -0
  64. package/src/chips/adc_dac/ad5248.js +194 -0
  65. package/src/chips/adc_dac/ads1013.js +219 -0
  66. package/src/chips/adc_dac/ads1014.js +509 -0
  67. package/src/chips/adc_dac/ads1113.js +112 -0
  68. package/src/chips/adc_dac/ads1114.js +201 -0
  69. package/src/chips/adc_dac/ads1115.js +232 -0
  70. package/src/chips/display/pcf8578.js +452 -0
  71. package/src/chips/display/pcf8579.js +451 -0
  72. package/src/chips/imu/icm20948.js +849 -0
  73. package/src/chips/io_expander/mcp23008.js +558 -0
  74. package/src/chips/memory/mb85rc.js +166 -0
  75. package/src/chips/motor/drv2605.js +592 -0
  76. package/src/discovery/registry.js +20 -19
@@ -0,0 +1,1471 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * LSM303 3-axis accelerometer + magnetometer e-compass driver (STMicroelectronics).
5
+ *
6
+ * The LSM303DLHC combines a three-axis MEMS accelerometer and a three-axis
7
+ * magnetometer in one package. The two dies sit behind **two independent I²C
8
+ * slave addresses** on the same bus: the accelerometer at 0x19 and the
9
+ * magnetometer at 0x1E. The driver therefore takes two connections.
10
+ *
11
+ * Accelerometer (0x19)
12
+ * Four full-scale ranges (±2 / ±4 / ±8 / ±16 g), 12-bit output at 1 mg/LSB
13
+ * in the default high-resolution mode, output data rates from 1 Hz to
14
+ * 5.376 kHz, a 32-level FIFO, a selectable high-pass filter, two
15
+ * programmable inertial interrupt generators (threshold, free-fall, 6D/4D
16
+ * orientation) and single/double click detection.
17
+ *
18
+ * Magnetometer (0x1E)
19
+ * Seven full-scale ranges (±130 µT to ±810 µT), 12-bit output, output data
20
+ * rates from 0.75 Hz to 220 Hz and a relative-only temperature sensor.
21
+ *
22
+ * The compass heading is computed in software from both sensors.
23
+ *
24
+ * I²C framing differs between the two slaves:
25
+ *
26
+ * - **Accelerometer** — after the slave address the master sends an 8-bit
27
+ * sub-address: the 7 LSbs are the register address and the **MSb enables
28
+ * address auto-increment**. A single-register access uses `reg`; a
29
+ * multi-byte access uses `reg | 0x80`.
30
+ * - **Magnetometer** — an address pointer without an auto-increment bit. The
31
+ * pointer always advances by one; after register 0x08 it rolls back to 0x03
32
+ * and from 0x0C upwards it wraps to 0x00. A 6-byte read from 0x03 therefore
33
+ * returns all six output registers in one transaction.
34
+ *
35
+ * The magnetometer output order is **X, Z, Y** (not X, Y, Z) and the byte order
36
+ * is **high byte first** — the opposite of the accelerometer.
37
+ */
38
+
39
+ // Fixed I²C slave addresses of the two dies.
40
+ const I2C_ADDRESS_ACCEL = 0x19;
41
+ const I2C_ADDRESS_MAG = 0x1E;
42
+
43
+ // Magnetometer identity (IRA_REG_M, IRB_REG_M, IRC_REG_M) — ASCII "H43".
44
+ const MAG_IDENTITY = Buffer.from([0x48, 0x34, 0x33]);
45
+
46
+ // --- Accelerometer registers -----------------------------------------------
47
+ const REG_CTRL1_A = 0x20;
48
+ const REG_CTRL2_A = 0x21;
49
+ const REG_CTRL3_A = 0x22;
50
+ const REG_CTRL4_A = 0x23;
51
+ const REG_CTRL5_A = 0x24;
52
+ const REG_CTRL6_A = 0x25;
53
+ const REG_REFERENCE_A = 0x26;
54
+ const REG_STATUS_A = 0x27;
55
+ const REG_OUT_X_L_A = 0x28;
56
+ const REG_FIFO_CTRL_A = 0x2E;
57
+ const REG_FIFO_SRC_A = 0x2F;
58
+ const REG_INT1_CFG_A = 0x30;
59
+ const REG_INT1_SOURCE_A = 0x31;
60
+ const REG_INT1_THS_A = 0x32;
61
+ const REG_INT1_DURATION_A = 0x33;
62
+ const REG_INT2_CFG_A = 0x34;
63
+ const REG_INT2_SOURCE_A = 0x35;
64
+ const REG_INT2_THS_A = 0x36;
65
+ const REG_INT2_DURATION_A = 0x37;
66
+ const REG_CLICK_CFG_A = 0x38;
67
+ const REG_CLICK_SRC_A = 0x39;
68
+ const REG_CLICK_THS_A = 0x3A;
69
+ const REG_TIME_LIMIT_A = 0x3B;
70
+ const REG_TIME_LATENCY_A = 0x3C;
71
+ const REG_TIME_WINDOW_A = 0x3D;
72
+
73
+ // --- Magnetometer registers -------------------------------------------------
74
+ const REG_CRA_M = 0x00;
75
+ const REG_CRB_M = 0x01;
76
+ const REG_MR_M = 0x02;
77
+ const REG_OUT_X_H_M = 0x03;
78
+ const REG_SR_M = 0x09;
79
+ const REG_IRA_M = 0x0A;
80
+ const REG_TEMP_OUT_H_M = 0x31;
81
+
82
+ // Bit 7 of the accelerometer sub-address enables address auto-increment.
83
+ const ACCEL_AUTO_INCREMENT = 0x80;
84
+
85
+ // Accelerometer operating modes.
86
+ const MODE_LOW_POWER = 0;
87
+ const MODE_NORMAL = 1;
88
+ const MODE_HIGH_RESOLUTION = 2;
89
+
90
+ // FIFO modes (FIFO_CTRL_REG_A.FM<1:0>).
91
+ const FIFO_BYPASS = 0;
92
+ const FIFO_MODE = 1;
93
+ const FIFO_STREAM = 2;
94
+ const FIFO_TRIGGER = 3;
95
+
96
+ // Magnetometer operating modes (MR_REG_M.MD<1:0>).
97
+ const MAG_MODE_CONTINUOUS = 0;
98
+ const MAG_MODE_SINGLE = 1;
99
+ const MAG_MODE_SLEEP = 2;
100
+
101
+ // Click axis masks.
102
+ const CLICK_AXIS_X = 0x01;
103
+ const CLICK_AXIS_Y = 0x02;
104
+ const CLICK_AXIS_Z = 0x04;
105
+
106
+ // CTRL_REG1_A bits.
107
+ const CTRL1_ODR_MASK = 0xF0;
108
+ const CTRL1_LPEN = 0x08;
109
+ const CTRL1_AXES_MASK = 0x07;
110
+
111
+ // CTRL_REG2_A bits.
112
+ const CTRL2_HPM_MASK = 0xC0;
113
+ const CTRL2_HPCF_MASK = 0x30;
114
+ const CTRL2_FDS = 0x08;
115
+ const CTRL2_HPCLICK = 0x04;
116
+ const CTRL2_HPIS2 = 0x02;
117
+ const CTRL2_HPIS1 = 0x01;
118
+
119
+ // CTRL_REG3_A bits (routing to INT1).
120
+ const CTRL3_I1_CLICK = 0x80;
121
+ const CTRL3_I1_AOI1 = 0x40;
122
+ const CTRL3_I1_AOI2 = 0x20;
123
+ const CTRL3_I1_DRDY1 = 0x10;
124
+ const CTRL3_I1_WTM = 0x04;
125
+ const CTRL3_I1_OVERRUN = 0x02;
126
+
127
+ // CTRL_REG4_A bits.
128
+ const CTRL4_BDU = 0x80;
129
+ const CTRL4_FS_MASK = 0x30;
130
+ const CTRL4_HR = 0x08;
131
+
132
+ // CTRL_REG5_A bits.
133
+ const CTRL5_BOOT = 0x80;
134
+ const CTRL5_FIFO_EN = 0x40;
135
+ const CTRL5_LIR_INT1 = 0x08;
136
+ const CTRL5_D4D_INT1 = 0x04;
137
+ const CTRL5_LIR_INT2 = 0x02;
138
+ const CTRL5_D4D_INT2 = 0x01;
139
+
140
+ // CTRL_REG6_A bits (routing to INT2).
141
+ const CTRL6_I2_CLICK = 0x80;
142
+ const CTRL6_I2_INT1 = 0x40;
143
+ const CTRL6_I2_INT2 = 0x20;
144
+ const CTRL6_BOOT_I1 = 0x10;
145
+ const CTRL6_H_LACTIVE = 0x02;
146
+
147
+ // STATUS_REG_A bits.
148
+ const STATUS_ZYXDA = 0x08;
149
+
150
+ // FIFO_SRC_REG_A bits.
151
+ const FIFO_SRC_WTM = 0x80;
152
+ const FIFO_SRC_OVERRUN = 0x40;
153
+ const FIFO_SRC_EMPTY = 0x20;
154
+ const FIFO_SRC_LEVEL_MASK = 0x1F;
155
+
156
+ // INTx_SOURCE_A / CLICK_SRC_A interrupt-activation bit.
157
+ const SOURCE_IA = 0x40;
158
+
159
+ // CRA_REG_M bits.
160
+ const CRA_TEMP_EN = 0x80;
161
+ const CRA_DO_MASK = 0x1C;
162
+
163
+ // CRB_REG_M bits.
164
+ const CRB_GN_MASK = 0xE0;
165
+
166
+ // SR_REG_M bits.
167
+ const SR_LOCK = 0x02;
168
+ const SR_DRDY = 0x01;
169
+
170
+ // Accelerometer output data rate codes (CTRL_REG1_A.ODR<3:0>).
171
+ const ODR_CODES = {
172
+ 0: 0x0, 1: 0x1, 10: 0x2, 25: 0x3, 50: 0x4,
173
+ 100: 0x5, 200: 0x6, 400: 0x7, 1620: 0x8, 1344: 0x9, 5376: 0x9,
174
+ };
175
+
176
+ // Magnetometer output data rates (CRA_REG_M.DO<2:0>).
177
+ const MAG_ODR_RATES = [0.75, 1.5, 3, 7.5, 15, 30, 75, 220];
178
+
179
+ /**
180
+ * Interrupt source constants for the LSM303.
181
+ *
182
+ * The values are the bit positions of `pollInterrupt()`'s return value.
183
+ */
184
+ const LSM303Source = {
185
+ /** New accelerometer XYZ sample (`STATUS_REG_A.ZYXDA`), INT1. */
186
+ SOURCE_DATA_READY: 0x0001,
187
+ /** FIFO content exceeds the watermark (`FIFO_SRC_REG_A.WTM`), INT1. */
188
+ SOURCE_WATERMARK: 0x0002,
189
+ /** FIFO overrun (`FIFO_SRC_REG_A.OVRN_FIFO`), INT1. */
190
+ SOURCE_OVERRUN: 0x0004,
191
+ /** Inertial generator 1 event (`INT1_SOURCE_A.IA`), INT1 or INT2. */
192
+ SOURCE_IA1: 0x0008,
193
+ /** Inertial generator 2 event (`INT2_SOURCE_A.IA`), INT1 or INT2. */
194
+ SOURCE_IA2: 0x0010,
195
+ /** Single / double click (`CLICK_SRC_A.IA`), INT1 or INT2. */
196
+ SOURCE_CLICK: 0x0020,
197
+ /** Boot procedure running — INT2 only, never reported by pollInterrupt(). */
198
+ SOURCE_BOOT: 0x0040,
199
+ /** New magnetometer sample (`SR_REG_M.DRDY`) — dedicated DRDY pin. */
200
+ SOURCE_MAG_DATA_READY: 0x0080,
201
+ };
202
+
203
+ /**
204
+ * Sleep `ms` milliseconds.
205
+ *
206
+ * @param {number} ms - Delay in milliseconds.
207
+ * @returns {Promise<void>}
208
+ */
209
+ function _delayMs(ms) {
210
+ return new Promise((resolve) => setTimeout(resolve, ms));
211
+ }
212
+
213
+ /**
214
+ * Convert an unsigned 16-bit value into a signed 16-bit integer.
215
+ *
216
+ * @param {number} value - Raw 16-bit value.
217
+ * @returns {number} Signed value.
218
+ */
219
+ function _toSigned16(value) {
220
+ return value & 0x8000 ? value - 0x10000 : value;
221
+ }
222
+
223
+ /**
224
+ * LSM303 minimal interface — acceleration and magnetic field.
225
+ *
226
+ * The constructor runs the full initialization sequence of the datasheet:
227
+ * it verifies the magnetometer identity (0x48 0x34 0x33), puts the
228
+ * accelerometer control registers into a known state and starts both dies.
229
+ *
230
+ * Default configuration:
231
+ * - Accelerometer: ±2 g, 100 Hz, high-resolution (12-bit), all three
232
+ * axes enabled, `BDU` set, high-pass filter bypassed, FIFO bypassed,
233
+ * all interrupt routing cleared.
234
+ * - Magnetometer: ±130 µT (±1.3 gauss), 15 Hz, continuous conversion,
235
+ * temperature sensor off.
236
+ */
237
+ class LSM303Minimal {
238
+ /**
239
+ * @param {object} accelConnection - Register connection (I²C) bound to 0x19.
240
+ * @param {object} magConnection - Register connection (I²C) bound to 0x1E.
241
+ *
242
+ * Both connections must sit on the same bus.
243
+ *
244
+ * @throws {Error} If the magnetometer identity does not match.
245
+ */
246
+ constructor(accelConnection, magConnection) {
247
+ this._accel = accelConnection;
248
+ this._mag = magConnection;
249
+ // Cached configuration, kept in sync with the registers.
250
+ this._mode = MODE_HIGH_RESOLUTION;
251
+ this._fsG = 2;
252
+ this._odrHz = 100.0;
253
+ this._magRangeUt = 130;
254
+ this._initPromise = this._init();
255
+ }
256
+
257
+ /**
258
+ * Run the datasheet initialization sequence.
259
+ *
260
+ * Verifies the magnetometer identity, clears the accelerometer's
261
+ * interrupt routing, FIFO and high-pass configuration, selects
262
+ * ±2 g / 100 Hz / high-resolution on the accelerometer and
263
+ * ±130 µT / 15 Hz / continuous conversion on the magnetometer, then
264
+ * waits for the first samples of both dies.
265
+ *
266
+ * @returns {Promise<void>}
267
+ */
268
+ async _init() {
269
+ // Boot download of the factory trim values.
270
+ await _delayMs(5);
271
+
272
+ const identity = await this._magRead(REG_IRA_M, 3);
273
+ if (!identity.equals(MAG_IDENTITY)) {
274
+ const value = (identity[0] << 16) | (identity[1] << 8) | identity[2];
275
+ throw new Error(
276
+ `LSM303 magnetometer not found: expected identity 0x483433, got 0x${value.toString(16)}`);
277
+ }
278
+
279
+ // Accelerometer: known state for the high-pass filter, the interrupt
280
+ // routing and the FIFO.
281
+ await this._accelWrite(REG_CTRL2_A, 0x00);
282
+ await this._accelWrite(REG_CTRL3_A, 0x00);
283
+ await this._accelWrite(REG_CTRL5_A, 0x00);
284
+ await this._accelWrite(REG_CTRL6_A, 0x00);
285
+ await this._accelWrite(REG_FIFO_CTRL_A, 0x00);
286
+
287
+ // Accelerometer: BDU = 1, ±2 g, high-resolution mode.
288
+ await this._accelWrite(REG_CTRL4_A, 0x88);
289
+ // Accelerometer: 100 Hz, X/Y/Z enabled.
290
+ await this._accelWrite(REG_CTRL1_A, 0x57);
291
+
292
+ // Magnetometer: 15 Hz, ±1.3 gauss, continuous conversion (the part
293
+ // resets into sleep mode).
294
+ await this._magWrite(REG_CRA_M, 0x10);
295
+ await this._magWrite(REG_CRB_M, 0x20);
296
+ await this._magWrite(REG_MR_M, 0x00);
297
+
298
+ // Accelerometer turn-on: 7/ODR = 70 ms at 100 Hz.
299
+ await _delayMs(70);
300
+ // Magnetometer first sample: one output period = 67 ms at 15 Hz.
301
+ await _delayMs(67);
302
+
303
+ this._mode = MODE_HIGH_RESOLUTION;
304
+ this._fsG = 2;
305
+ this._odrHz = 100.0;
306
+ }
307
+
308
+ // --- Low level register access -----------------------------------------
309
+
310
+ /**
311
+ * Read `length` bytes from an accelerometer register.
312
+ *
313
+ * Multi-byte reads set bit 7 of the sub-address so the accelerometer
314
+ * auto-increments the register pointer.
315
+ *
316
+ * @param {number} reg - Register address.
317
+ * @param {number} length - Number of bytes to read.
318
+ * @returns {Promise<Buffer>} The register contents.
319
+ */
320
+ async _accelRead(reg, length) {
321
+ let addr = reg & 0x7F;
322
+ if (length > 1) {
323
+ addr |= ACCEL_AUTO_INCREMENT;
324
+ }
325
+ return this._accel.readReg(addr, length);
326
+ }
327
+
328
+ /**
329
+ * Write one byte to an accelerometer register.
330
+ *
331
+ * @param {number} reg - Register address.
332
+ * @param {number} value - Value to write.
333
+ * @returns {Promise<void>}
334
+ */
335
+ async _accelWrite(reg, value) {
336
+ await this._accel.writeReg(reg, value & 0xFF);
337
+ }
338
+
339
+ /**
340
+ * Read `length` bytes from a magnetometer register.
341
+ *
342
+ * The magnetometer address pointer always auto-increments, so no
343
+ * auto-increment bit is needed.
344
+ *
345
+ * @param {number} reg - Register address.
346
+ * @param {number} length - Number of bytes to read.
347
+ * @returns {Promise<Buffer>} The register contents.
348
+ */
349
+ async _magRead(reg, length) {
350
+ return this._mag.readReg(reg, length);
351
+ }
352
+
353
+ /**
354
+ * Write one byte to a magnetometer register.
355
+ *
356
+ * @param {number} reg - Register address.
357
+ * @param {number} value - Value to write.
358
+ * @returns {Promise<void>}
359
+ */
360
+ async _magWrite(reg, value) {
361
+ await this._mag.writeReg(reg, value & 0xFF);
362
+ }
363
+
364
+ // --- Minimal API --------------------------------------------------------
365
+
366
+ /**
367
+ * Read the acceleration.
368
+ *
369
+ * Burst-reads the six output registers so all three axes belong to the
370
+ * same sample.
371
+ *
372
+ * @returns {Promise<{x:number, y:number, z:number}>} Acceleration in *g*.
373
+ */
374
+ async readAcceleration() {
375
+ const raw = await this._accelRead(REG_OUT_X_L_A, 6);
376
+ return this._convertAccel(raw);
377
+ }
378
+
379
+ /**
380
+ * Read the magnetic flux density.
381
+ *
382
+ * Burst-reads the six output registers (X, Z, Y on the wire) and
383
+ * reorders them to X, Y, Z.
384
+ *
385
+ * @returns {Promise<{x:number, y:number, z:number}>} Magnetic flux density in µT.
386
+ */
387
+ async readMagnetic() {
388
+ const raw = await this._magRead(REG_OUT_X_H_M, 6);
389
+ const { x, y, z } = this._reorderMag(raw);
390
+ const [gainX, gainZ] = this._magGains();
391
+ return { x: x / gainX * 100.0, y: y / gainX * 100.0, z: z / gainZ * 100.0 };
392
+ }
393
+
394
+ // --- Conversion helpers -------------------------------------------------
395
+
396
+ /**
397
+ * Return the right-shift that turns the raw 16-bit pair into counts.
398
+ *
399
+ * @returns {number} Shift in bits.
400
+ */
401
+ _shift() {
402
+ if (this._mode === MODE_LOW_POWER) return 8;
403
+ if (this._mode === MODE_NORMAL) return 6;
404
+ return 4;
405
+ }
406
+
407
+ /**
408
+ * Return the sensitivity in mg/LSB for the current mode and range.
409
+ *
410
+ * @returns {number} Sensitivity in mg/LSB.
411
+ */
412
+ _sensitivityMg() {
413
+ let table;
414
+ if (this._mode === MODE_LOW_POWER) {
415
+ table = [16, 32, 64, 192];
416
+ } else if (this._mode === MODE_NORMAL) {
417
+ table = [4, 8, 16, 48];
418
+ } else {
419
+ table = [1, 2, 4, 12];
420
+ }
421
+ const index = { 2: 0, 4: 1, 8: 2, 16: 3 }[this._fsG];
422
+ return table[index];
423
+ }
424
+
425
+ /**
426
+ * Convert a six-byte accelerometer burst into (x, y, z) in *g*.
427
+ *
428
+ * @param {Buffer} raw - Six raw bytes, little-endian per axis.
429
+ * @returns {{x:number, y:number, z:number}} Acceleration in *g*.
430
+ */
431
+ _convertAccel(raw) {
432
+ const shift = this._shift();
433
+ const sens = this._sensitivityMg() / 1000.0;
434
+ const x = _toSigned16(raw[0] | (raw[1] << 8)) >> shift;
435
+ const y = _toSigned16(raw[2] | (raw[3] << 8)) >> shift;
436
+ const z = _toSigned16(raw[4] | (raw[5] << 8)) >> shift;
437
+ return { x: x * sens, y: y * sens, z: z * sens };
438
+ }
439
+
440
+ /**
441
+ * Convert a six-byte accelerometer burst into raw counts.
442
+ *
443
+ * @param {Buffer} raw - Six raw bytes, little-endian per axis.
444
+ * @returns {{x:number, y:number, z:number}} Signed counts.
445
+ */
446
+ _rawAccel(raw) {
447
+ const shift = this._shift();
448
+ return {
449
+ x: _toSigned16(raw[0] | (raw[1] << 8)) >> shift,
450
+ y: _toSigned16(raw[2] | (raw[3] << 8)) >> shift,
451
+ z: _toSigned16(raw[4] | (raw[5] << 8)) >> shift,
452
+ };
453
+ }
454
+
455
+ /**
456
+ * Reorder a six-byte magnetometer burst (X, Z, Y) into (X, Y, Z).
457
+ *
458
+ * @param {Buffer} raw - Six raw bytes, high byte first.
459
+ * @returns {{x:number, y:number, z:number}} Signed counts.
460
+ */
461
+ _reorderMag(raw) {
462
+ const x = _toSigned16((raw[0] << 8) | raw[1]);
463
+ const z = _toSigned16((raw[2] << 8) | raw[3]);
464
+ const y = _toSigned16((raw[4] << 8) | raw[5]);
465
+ return { x, y, z };
466
+ }
467
+
468
+ /**
469
+ * Return the (X/Y, Z) gains in LSB per gauss for the current range.
470
+ *
471
+ * @returns {[number, number]} The X/Y gain and the Z gain.
472
+ */
473
+ _magGains() {
474
+ const table = {
475
+ 130: [1100, 980],
476
+ 190: [855, 760],
477
+ 250: [670, 600],
478
+ 400: [450, 400],
479
+ 470: [400, 355],
480
+ 560: [330, 295],
481
+ 810: [230, 205],
482
+ };
483
+ return table[this._magRangeUt];
484
+ }
485
+ }
486
+
487
+ /**
488
+ * Mixin that adds Full-stage functionality on top of `LSM303Minimal`.
489
+ *
490
+ * Usage: `class LSM303Full extends _LSM303FullMixin(LSM303Minimal) {}`.
491
+ */
492
+ const _LSM303FullMixin = (Base) => class extends Base {
493
+ /**
494
+ * @param {object} accelConnection - Register connection (I²C) bound to 0x19.
495
+ * @param {object} magConnection - Register connection (I²C) bound to 0x1E.
496
+ *
497
+ * @throws {Error} If the magnetometer identity does not match.
498
+ */
499
+ constructor(accelConnection, magConnection) {
500
+ super(accelConnection, magConnection);
501
+ this._tempEnabled = false;
502
+ this._callback = null;
503
+ this._intPinUsed = null;
504
+ this._pollTimer = null;
505
+ this._lastStatus = 0;
506
+ this._enabledSources = 0;
507
+ }
508
+
509
+ // --- Accelerometer configuration ----------------------------------------
510
+
511
+ /**
512
+ * Set the accelerometer full-scale range.
513
+ *
514
+ * @param {number} rangeG - 2, 4, 8 or 16 (*g*).
515
+ * @returns {Promise<boolean>} True on success.
516
+ * @throws {Error} If rangeG is not one of the supported ranges.
517
+ */
518
+ async setRange(rangeG) {
519
+ const codes = { 2: 0x00, 4: 0x10, 8: 0x20, 16: 0x30 };
520
+ if (!(rangeG in codes)) {
521
+ throw new Error('rangeG must be one of 2, 4, 8, 16');
522
+ }
523
+ const ctrl4 = await this._accelRead(REG_CTRL4_A, 1);
524
+ const value = (ctrl4[0] & ~CTRL4_FS_MASK) | codes[rangeG];
525
+ await this._accelWrite(REG_CTRL4_A, value);
526
+ this._fsG = rangeG;
527
+ return true;
528
+ }
529
+
530
+ /**
531
+ * Set the accelerometer output data rate.
532
+ *
533
+ * @param {number} rateHz - 0 (power-down), 1, 10, 25, 50, 100, 200, 400,
534
+ * 1344, 1620 or 5376. The rates 1620 and 5376 Hz exist in low-power mode
535
+ * only, 1344 Hz in normal and high-resolution mode only.
536
+ * @returns {Promise<boolean>} True on success.
537
+ * @throws {Error} If the rate is not supported or not allowed in the
538
+ * current operating mode.
539
+ */
540
+ async setDataRate(rateHz) {
541
+ if (!(rateHz in ODR_CODES)) {
542
+ throw new Error('rateHz must be one of 0, 1, 10, 25, 50, 100, 200, 400, 1344, 1620, 5376');
543
+ }
544
+ const lowPower = this._mode === MODE_LOW_POWER;
545
+ if ((rateHz === 1620 || rateHz === 5376) && !lowPower) {
546
+ throw new Error(`${rateHz} Hz is only available in low-power mode`);
547
+ }
548
+ if (rateHz === 1344 && lowPower) {
549
+ throw new Error('1344 Hz is not available in low-power mode (use 5376 Hz)');
550
+ }
551
+ const ctrl1 = await this._accelRead(REG_CTRL1_A, 1);
552
+ const value = (ctrl1[0] & ~CTRL1_ODR_MASK) | (ODR_CODES[rateHz] << 4);
553
+ await this._accelWrite(REG_CTRL1_A, value);
554
+ this._odrHz = Number(rateHz);
555
+ return true;
556
+ }
557
+
558
+ /**
559
+ * Set the accelerometer operating mode.
560
+ *
561
+ * Low-power mode uses 8-bit output, normal mode 10-bit and
562
+ * high-resolution mode 12-bit; the sensitivity changes accordingly.
563
+ *
564
+ * @param {number} mode - MODE_LOW_POWER (0), MODE_NORMAL (1) or
565
+ * MODE_HIGH_RESOLUTION (2).
566
+ * @returns {Promise<boolean>} True on success.
567
+ * @throws {Error} If mode is not one of the three operating modes.
568
+ */
569
+ async setMode(mode) {
570
+ let lpen, hr;
571
+ if (mode === MODE_LOW_POWER) {
572
+ lpen = true; hr = false;
573
+ } else if (mode === MODE_NORMAL) {
574
+ lpen = false; hr = false;
575
+ } else if (mode === MODE_HIGH_RESOLUTION) {
576
+ lpen = false; hr = true;
577
+ } else {
578
+ throw new Error('mode must be MODE_LOW_POWER, MODE_NORMAL or MODE_HIGH_RESOLUTION');
579
+ }
580
+ const ctrl1 = await this._accelRead(REG_CTRL1_A, 1);
581
+ const ctrl4 = await this._accelRead(REG_CTRL4_A, 1);
582
+ const v1 = lpen ? (ctrl1[0] | CTRL1_LPEN) : (ctrl1[0] & ~CTRL1_LPEN);
583
+ const v4 = hr ? (ctrl4[0] | CTRL4_HR) : (ctrl4[0] & ~CTRL4_HR);
584
+ await this._accelWrite(REG_CTRL1_A, v1);
585
+ await this._accelWrite(REG_CTRL4_A, v4);
586
+ this._mode = mode;
587
+ return true;
588
+ }
589
+
590
+ /**
591
+ * Enable or disable the individual accelerometer axes.
592
+ *
593
+ * @param {boolean} [x=true] - True to enable the X axis.
594
+ * @param {boolean} [y=true] - True to enable the Y axis.
595
+ * @param {boolean} [z=true] - True to enable the Z axis.
596
+ * @returns {Promise<void>}
597
+ */
598
+ async setAxes(x = true, y = true, z = true) {
599
+ const ctrl1 = await this._accelRead(REG_CTRL1_A, 1);
600
+ let value = ctrl1[0] & ~CTRL1_AXES_MASK;
601
+ if (x) value |= 0x01;
602
+ if (y) value |= 0x02;
603
+ if (z) value |= 0x04;
604
+ await this._accelWrite(REG_CTRL1_A, value);
605
+ }
606
+
607
+ /**
608
+ * Put the accelerometer into power-down mode.
609
+ *
610
+ * The output data rate is set to 0000; the register contents are
611
+ * retained, so `setDataRate()` resumes the conversions.
612
+ *
613
+ * @returns {Promise<void>}
614
+ */
615
+ async powerDown() {
616
+ const ctrl1 = await this._accelRead(REG_CTRL1_A, 1);
617
+ await this._accelWrite(REG_CTRL1_A, ctrl1[0] & ~CTRL1_ODR_MASK);
618
+ this._odrHz = 0.0;
619
+ }
620
+
621
+ // --- Accelerometer readings ---------------------------------------------
622
+
623
+ /**
624
+ * Read the acceleration as raw counts.
625
+ *
626
+ * @returns {Promise<{x:number, y:number, z:number}>} Signed counts at the
627
+ * current resolution.
628
+ */
629
+ async readAccelerationRaw() {
630
+ const raw = await this._accelRead(REG_OUT_X_L_A, 6);
631
+ return this._rawAccel(raw);
632
+ }
633
+
634
+ /**
635
+ * Report whether a new accelerometer sample is available.
636
+ *
637
+ * @returns {Promise<boolean>} True if `STATUS_REG_A.ZYXDA` is set.
638
+ */
639
+ async newDataAvailable() {
640
+ const status = await this._accelRead(REG_STATUS_A, 1);
641
+ return (status[0] & STATUS_ZYXDA) !== 0;
642
+ }
643
+
644
+ /**
645
+ * Read the raw accelerometer status register.
646
+ *
647
+ * @returns {Promise<number>} The `STATUS_REG_A` byte (overrun and
648
+ * data-available flags).
649
+ */
650
+ async readStatus() {
651
+ const status = await this._accelRead(REG_STATUS_A, 1);
652
+ return status[0];
653
+ }
654
+
655
+ // --- High-pass filter ----------------------------------------------------
656
+
657
+ /**
658
+ * Configure the accelerometer high-pass filter.
659
+ *
660
+ * @param {number} [mode=0] - HPM code 0–3 (0 = normal, 1 = reference
661
+ * signal, 2 = normal, 3 = autoreset on interrupt event).
662
+ * @param {number} [cutoff=0] - HPCF code 0–3 (high-pass cutoff selection).
663
+ * @param {boolean} [filterOutput=false] - True to route the filtered data
664
+ * to the output registers and the FIFO (`FDS`).
665
+ * @param {boolean} [onIa1=false] - True to high-pass the input of
666
+ * interrupt generator 1.
667
+ * @param {boolean} [onIa2=false] - True to high-pass the input of
668
+ * interrupt generator 2.
669
+ * @param {boolean} [onClick=false] - True to high-pass the click detector
670
+ * input.
671
+ * @returns {Promise<boolean>} True on success.
672
+ * @throws {Error} If mode or cutoff is out of range.
673
+ */
674
+ async setHighPass(mode = 0, cutoff = 0, filterOutput = false,
675
+ onIa1 = false, onIa2 = false, onClick = false) {
676
+ if (mode < 0 || mode > 3) {
677
+ throw new Error('mode must be 0..3');
678
+ }
679
+ if (cutoff < 0 || cutoff > 3) {
680
+ throw new Error('cutoff must be 0..3');
681
+ }
682
+ let ctrl2 = (mode << 6) | (cutoff << 4);
683
+ if (filterOutput) ctrl2 |= CTRL2_FDS;
684
+ if (onClick) ctrl2 |= CTRL2_HPCLICK;
685
+ if (onIa2) ctrl2 |= CTRL2_HPIS2;
686
+ if (onIa1) ctrl2 |= CTRL2_HPIS1;
687
+ await this._accelWrite(REG_CTRL2_A, ctrl2);
688
+ return true;
689
+ }
690
+
691
+ /**
692
+ * Reset the high-pass filter and return the reference value.
693
+ *
694
+ * Reading `REFERENCE_A` resets the filter in HPM = 00 mode.
695
+ *
696
+ * @returns {Promise<number>} The reference value (0–255).
697
+ */
698
+ async resetHighPass() {
699
+ const ref = await this._accelRead(REG_REFERENCE_A, 1);
700
+ return ref[0];
701
+ }
702
+
703
+ /**
704
+ * Write the high-pass reference value.
705
+ *
706
+ * @param {number} value - Reference value 0–255.
707
+ * @returns {Promise<void>}
708
+ */
709
+ async setReference(value) {
710
+ await this._accelWrite(REG_REFERENCE_A, value & 0xFF);
711
+ }
712
+
713
+ // --- FIFO ----------------------------------------------------------------
714
+
715
+ /**
716
+ * Configure the accelerometer FIFO.
717
+ *
718
+ * @param {number} mode - FIFO_BYPASS (0), FIFO_MODE (1), FIFO_STREAM (2)
719
+ * or FIFO_TRIGGER (3).
720
+ * @param {number} [watermark=0] - Watermark level 0–31.
721
+ * @param {boolean} [triggerInt2=false] - True to use INT2 as the trigger
722
+ * source in trigger mode, False for INT1.
723
+ * @returns {Promise<boolean>} True on success.
724
+ * @throws {Error} If mode or watermark is out of range.
725
+ */
726
+ async setFifo(mode, watermark = 0, triggerInt2 = false) {
727
+ if (mode < 0 || mode > 3) {
728
+ throw new Error('mode must be 0..3');
729
+ }
730
+ if (watermark < 0 || watermark > 31) {
731
+ throw new Error('watermark must be 0..31');
732
+ }
733
+ let fifo = (mode << 6) | (watermark & 0x1F);
734
+ if (triggerInt2) fifo |= 0x20;
735
+ await this._accelWrite(REG_FIFO_CTRL_A, fifo);
736
+ const ctrl5 = await this._accelRead(REG_CTRL5_A, 1);
737
+ const value = mode === FIFO_BYPASS
738
+ ? (ctrl5[0] & ~CTRL5_FIFO_EN)
739
+ : (ctrl5[0] | CTRL5_FIFO_EN);
740
+ await this._accelWrite(REG_CTRL5_A, value);
741
+ return true;
742
+ }
743
+
744
+ /**
745
+ * Return the number of unread sample sets in the FIFO.
746
+ *
747
+ * @returns {Promise<number>} The number of unread sample sets (0–32).
748
+ */
749
+ async fifoLevel() {
750
+ const src = await this._accelRead(REG_FIFO_SRC_A, 1);
751
+ return src[0] & FIFO_SRC_LEVEL_MASK;
752
+ }
753
+
754
+ /**
755
+ * Read sample sets from the FIFO.
756
+ *
757
+ * Each sample set is popped with one six-byte burst read.
758
+ *
759
+ * @param {number} [count] - Number of sample sets to read; undefined
760
+ * reads all of them.
761
+ * @returns {Promise<Array<{x:number, y:number, z:number}>>} The sample
762
+ * sets in *g*.
763
+ */
764
+ async readFifo(count) {
765
+ let n = count;
766
+ if (n === undefined || n === null) {
767
+ n = await this.fifoLevel();
768
+ }
769
+ const result = [];
770
+ for (let i = 0; i < n; i++) {
771
+ const raw = await this._accelRead(REG_OUT_X_L_A, 6);
772
+ result.push(this._convertAccel(raw));
773
+ }
774
+ return result;
775
+ }
776
+
777
+ /**
778
+ * Read the decoded FIFO status.
779
+ *
780
+ * @returns {Promise<{watermark:boolean, overrun:boolean, empty:boolean, level:number}>}
781
+ * The decoded FIFO status.
782
+ */
783
+ async readFifoStatus() {
784
+ const src = await this._accelRead(REG_FIFO_SRC_A, 1);
785
+ return {
786
+ watermark: (src[0] & FIFO_SRC_WTM) !== 0,
787
+ overrun: (src[0] & FIFO_SRC_OVERRUN) !== 0,
788
+ empty: (src[0] & FIFO_SRC_EMPTY) !== 0,
789
+ level: src[0] & FIFO_SRC_LEVEL_MASK,
790
+ };
791
+ }
792
+
793
+ // --- Inertial interrupt generators ---------------------------------------
794
+
795
+ /**
796
+ * Configure one inertial interrupt generator.
797
+ *
798
+ * @param {number} generator - 1 or 2.
799
+ * @param {number} events - Bit mask of the enabled events — `ZHIE` (0x20),
800
+ * `ZLIE` (0x10), `YHIE` (0x08), `YLIE` (0x04), `XHIE` (0x02) or
801
+ * `XLIE` (0x01).
802
+ * @param {number} thresholdG - Threshold in *g*, relative to the current
803
+ * range.
804
+ * @param {number} [durationMs=0] - Minimum duration in milliseconds; 0
805
+ * disables the duration filter.
806
+ * @param {boolean} [andMode=false] - True to combine the events with AND
807
+ * instead of OR.
808
+ * @returns {Promise<boolean>} True on success.
809
+ * @throws {Error} If the generator or the event mask is invalid.
810
+ */
811
+ async setMotionInterrupt(generator, events, thresholdG, durationMs = 0, andMode = false) {
812
+ const [cfg, ths, dur] = this._intRegs(generator);
813
+ if (events < 0 || events > 0x3F) {
814
+ throw new Error('events must be a 6-bit mask (0..63)');
815
+ }
816
+ let value = events & 0x3F;
817
+ if (andMode) value |= 0x80;
818
+ await this._accelWrite(cfg, value);
819
+ await this._accelWrite(ths, this._encodeThreshold(thresholdG));
820
+ await this._accelWrite(dur, this._encodeDuration(durationMs));
821
+ return true;
822
+ }
823
+
824
+ /**
825
+ * Arm the free-fall detector of one generator.
826
+ *
827
+ * All three axes are watched for a low event and combined with AND.
828
+ *
829
+ * @param {number} [generator=1] - 1 or 2.
830
+ * @param {number} [thresholdG=0.35] - Threshold in *g*.
831
+ * @param {number} [durationMs=30] - Minimum duration in milliseconds.
832
+ * @returns {Promise<boolean>} True on success.
833
+ */
834
+ async setFreeFall(generator = 1, thresholdG = 0.35, durationMs = 30) {
835
+ return this.setMotionInterrupt(generator, 0x15, thresholdG, durationMs, true);
836
+ }
837
+
838
+ /**
839
+ * Arm the 6D orientation detector of one generator.
840
+ *
841
+ * @param {number} [generator=1] - 1 or 2.
842
+ * @param {boolean} [position=false] - False for movement recognition (a
843
+ * one-ODR pulse), True for position recognition (a level).
844
+ * @param {boolean} [fourD=false] - True for 4D detection, which drops the
845
+ * Z axis from the zone detection.
846
+ * @returns {Promise<boolean>} True on success.
847
+ */
848
+ async setOrientation(generator = 1, position = false, fourD = false) {
849
+ const [cfg] = this._intRegs(generator);
850
+ let events = 0x3F;
851
+ if (fourD) events &= ~0x30;
852
+ let value = events | 0x40;
853
+ if (position) value |= 0x80;
854
+ await this._accelWrite(cfg, value);
855
+ const ctrl5 = await this._accelRead(REG_CTRL5_A, 1);
856
+ const bit = generator === 1 ? CTRL5_D4D_INT1 : CTRL5_D4D_INT2;
857
+ const updated = fourD ? (ctrl5[0] | bit) : (ctrl5[0] & ~bit);
858
+ await this._accelWrite(REG_CTRL5_A, updated);
859
+ return true;
860
+ }
861
+
862
+ /**
863
+ * Latch or release the interrupt of one generator.
864
+ *
865
+ * A latched interrupt stays asserted until `INTx_SOURCE_A` is read.
866
+ *
867
+ * @param {number} generator - 1 or 2.
868
+ * @param {boolean} enabled - True to latch, False to release.
869
+ * @returns {Promise<boolean>} True on success.
870
+ * @throws {Error} If the generator is not 1 or 2.
871
+ */
872
+ async setLatch(generator, enabled) {
873
+ if (generator !== 1 && generator !== 2) {
874
+ throw new Error('generator must be 1 or 2');
875
+ }
876
+ const ctrl5 = await this._accelRead(REG_CTRL5_A, 1);
877
+ const bit = generator === 1 ? CTRL5_LIR_INT1 : CTRL5_LIR_INT2;
878
+ const value = enabled ? (ctrl5[0] | bit) : (ctrl5[0] & ~bit);
879
+ await this._accelWrite(REG_CTRL5_A, value);
880
+ return true;
881
+ }
882
+
883
+ /**
884
+ * Read the interrupt source register of one generator.
885
+ *
886
+ * Reading the register clears the latched interrupt.
887
+ *
888
+ * @param {number} generator - 1 or 2.
889
+ * @returns {Promise<number>} The raw `INTx_SOURCE_A` byte.
890
+ * @throws {Error} If the generator is not 1 or 2.
891
+ */
892
+ async readInterruptSource(generator) {
893
+ if (generator !== 1 && generator !== 2) {
894
+ throw new Error('generator must be 1 or 2');
895
+ }
896
+ const reg = generator === 1 ? REG_INT1_SOURCE_A : REG_INT2_SOURCE_A;
897
+ const src = await this._accelRead(reg, 1);
898
+ return src[0];
899
+ }
900
+
901
+ /**
902
+ * Return the (config, threshold, duration) register triple of a generator.
903
+ *
904
+ * @param {number} generator - 1 or 2.
905
+ * @returns {[number, number, number]} The three register addresses.
906
+ * @throws {Error} If the generator is not 1 or 2.
907
+ */
908
+ _intRegs(generator) {
909
+ if (generator === 1) {
910
+ return [REG_INT1_CFG_A, REG_INT1_THS_A, REG_INT1_DURATION_A];
911
+ }
912
+ if (generator === 2) {
913
+ return [REG_INT2_CFG_A, REG_INT2_THS_A, REG_INT2_DURATION_A];
914
+ }
915
+ throw new Error('generator must be 1 or 2');
916
+ }
917
+
918
+ /**
919
+ * Convert a threshold in *g* into a 7-bit register code.
920
+ *
921
+ * @param {number} thresholdG - Threshold in *g*.
922
+ * @returns {number} The register code (0–127).
923
+ */
924
+ _encodeThreshold(thresholdG) {
925
+ let code = Math.round(thresholdG * 128.0 / this._fsG);
926
+ if (code < 0) code = 0;
927
+ if (code > 127) code = 127;
928
+ return code;
929
+ }
930
+
931
+ /**
932
+ * Convert a duration in milliseconds into a register code.
933
+ *
934
+ * One LSB is 1/ODR; the code is clamped to the register width.
935
+ *
936
+ * @param {number} durationMs - Duration in milliseconds.
937
+ * @param {number} [bits=7] - Register width in bits.
938
+ * @returns {number} The register code.
939
+ */
940
+ _encodeDuration(durationMs, bits = 7) {
941
+ let code;
942
+ if (this._odrHz <= 0) {
943
+ code = 0;
944
+ } else {
945
+ code = Math.round(durationMs * this._odrHz / 1000.0);
946
+ }
947
+ if (code < 0) code = 0;
948
+ if (code > (1 << bits) - 1) code = (1 << bits) - 1;
949
+ return code;
950
+ }
951
+
952
+ // --- Click detection -----------------------------------------------------
953
+
954
+ /**
955
+ * Configure the click detector.
956
+ *
957
+ * @param {number} [single=0] - Axis mask of the enabled single-click
958
+ * detectors (X = 1, Y = 2, Z = 4).
959
+ * @param {number} [double=0] - Axis mask of the enabled double-click
960
+ * detectors (X = 1, Y = 2, Z = 4).
961
+ * @param {number} [thresholdG=0.5] - Click threshold in *g*.
962
+ * @param {number} [timeLimitMs=10] - Maximum time between the two clicks
963
+ * of a double click, in milliseconds.
964
+ * @param {number} [latencyMs=20] - Minimum time between two consecutive
965
+ * clicks, in milliseconds.
966
+ * @param {number} [windowMs=50] - Time the click detector observes, in
967
+ * milliseconds.
968
+ * @returns {Promise<boolean>} True on success.
969
+ * @throws {Error} If an axis mask is out of range.
970
+ */
971
+ async setClick(single = 0, double = 0, thresholdG = 0.5,
972
+ timeLimitMs = 10, latencyMs = 20, windowMs = 50) {
973
+ if (single < 0 || single > 0x07) {
974
+ throw new Error('single must be an axis mask 0..7');
975
+ }
976
+ if (double < 0 || double > 0x07) {
977
+ throw new Error('double must be an axis mask 0..7');
978
+ }
979
+ let cfg = 0;
980
+ if (single & CLICK_AXIS_X) cfg |= 0x01;
981
+ if (single & CLICK_AXIS_Y) cfg |= 0x04;
982
+ if (single & CLICK_AXIS_Z) cfg |= 0x10;
983
+ if (double & CLICK_AXIS_X) cfg |= 0x02;
984
+ if (double & CLICK_AXIS_Y) cfg |= 0x08;
985
+ if (double & CLICK_AXIS_Z) cfg |= 0x20;
986
+ await this._accelWrite(REG_CLICK_CFG_A, cfg);
987
+ await this._accelWrite(REG_CLICK_THS_A, this._encodeThreshold(thresholdG));
988
+ await this._accelWrite(REG_TIME_LIMIT_A, this._encodeDuration(timeLimitMs, 7));
989
+ await this._accelWrite(REG_TIME_LATENCY_A, this._encodeDuration(latencyMs, 8));
990
+ await this._accelWrite(REG_TIME_WINDOW_A, this._encodeDuration(windowMs, 8));
991
+ return true;
992
+ }
993
+
994
+ /**
995
+ * Read the click source register.
996
+ *
997
+ * Reading the register clears the latched click interrupt.
998
+ *
999
+ * @returns {Promise<number>} The raw `CLICK_SRC_A` byte.
1000
+ */
1001
+ async readClickSource() {
1002
+ const src = await this._accelRead(REG_CLICK_SRC_A, 1);
1003
+ return src[0];
1004
+ }
1005
+
1006
+ /**
1007
+ * Set the polarity of the INT1 / INT2 outputs.
1008
+ *
1009
+ * @param {boolean} activeLow - True for active-low, False for
1010
+ * active-high (default).
1011
+ * @returns {Promise<void>}
1012
+ */
1013
+ async setInterruptPolarity(activeLow) {
1014
+ const ctrl6 = await this._accelRead(REG_CTRL6_A, 1);
1015
+ const value = activeLow
1016
+ ? (ctrl6[0] | CTRL6_H_LACTIVE)
1017
+ : (ctrl6[0] & ~CTRL6_H_LACTIVE);
1018
+ await this._accelWrite(REG_CTRL6_A, value);
1019
+ }
1020
+
1021
+ // --- Magnetometer --------------------------------------------------------
1022
+
1023
+ /**
1024
+ * Set the magnetometer full-scale range.
1025
+ *
1026
+ * @param {number} rangeUt - 130, 190, 250, 400, 470, 560 or 810 (µT).
1027
+ * @returns {Promise<boolean>} True on success.
1028
+ * @throws {Error} If the range is not supported.
1029
+ */
1030
+ async setMagRange(rangeUt) {
1031
+ const codes = { 130: 0x20, 190: 0x40, 250: 0x60, 400: 0x80,
1032
+ 470: 0xA0, 560: 0xC0, 810: 0xE0 };
1033
+ if (!(rangeUt in codes)) {
1034
+ throw new Error('rangeUt must be one of 130, 190, 250, 400, 470, 560, 810');
1035
+ }
1036
+ const crb = await this._magRead(REG_CRB_M, 1);
1037
+ const value = (crb[0] & ~CRB_GN_MASK) | codes[rangeUt];
1038
+ await this._magWrite(REG_CRB_M, value);
1039
+ this._magRangeUt = rangeUt;
1040
+ return true;
1041
+ }
1042
+
1043
+ /**
1044
+ * Set the magnetometer output data rate.
1045
+ *
1046
+ * @param {number} rateHz - 0.75, 1.5, 3, 7.5, 15, 30, 75 or 220.
1047
+ * @returns {Promise<boolean>} True on success.
1048
+ * @throws {Error} If the rate is not supported.
1049
+ */
1050
+ async setMagDataRate(rateHz) {
1051
+ for (let code = 0; code < MAG_ODR_RATES.length; code++) {
1052
+ if (Math.abs(MAG_ODR_RATES[code] - rateHz) < 1e-6) {
1053
+ const cra = await this._magRead(REG_CRA_M, 1);
1054
+ const value = (cra[0] & ~CRA_DO_MASK) | (code << 2);
1055
+ await this._magWrite(REG_CRA_M, value);
1056
+ return true;
1057
+ }
1058
+ }
1059
+ throw new Error('rateHz must be one of 0.75, 1.5, 3, 7.5, 15, 30, 75, 220');
1060
+ }
1061
+
1062
+ /**
1063
+ * Set the magnetometer operating mode.
1064
+ *
1065
+ * @param {number} mode - MAG_MODE_CONTINUOUS (0), MAG_MODE_SINGLE (1) or
1066
+ * MAG_MODE_SLEEP (2).
1067
+ * @returns {Promise<boolean>} True on success.
1068
+ * @throws {Error} If the mode is not supported.
1069
+ */
1070
+ async setMagMode(mode) {
1071
+ if (mode !== MAG_MODE_CONTINUOUS && mode !== MAG_MODE_SINGLE && mode !== MAG_MODE_SLEEP) {
1072
+ throw new Error('mode must be MAG_MODE_CONTINUOUS, MAG_MODE_SINGLE or MAG_MODE_SLEEP');
1073
+ }
1074
+ const mr = await this._magRead(REG_MR_M, 1);
1075
+ const value = (mr[0] & ~0x03) | mode;
1076
+ await this._magWrite(REG_MR_M, value);
1077
+ return true;
1078
+ }
1079
+
1080
+ /**
1081
+ * Read the magnetic field as raw counts.
1082
+ *
1083
+ * @returns {Promise<{x:number, y:number, z:number}>} Signed counts in
1084
+ * X, Y, Z order.
1085
+ */
1086
+ async readMagneticRaw() {
1087
+ const raw = await this._magRead(REG_OUT_X_H_M, 6);
1088
+ return this._reorderMag(raw);
1089
+ }
1090
+
1091
+ /**
1092
+ * Report whether a new magnetometer sample is available.
1093
+ *
1094
+ * @returns {Promise<boolean>} True if `SR_REG_M.DRDY` is set.
1095
+ */
1096
+ async magDataReady() {
1097
+ const sr = await this._magRead(REG_SR_M, 1);
1098
+ return (sr[0] & SR_DRDY) !== 0;
1099
+ }
1100
+
1101
+ /**
1102
+ * Read the decoded magnetometer status register.
1103
+ *
1104
+ * @returns {Promise<{drdy:boolean, lock:boolean}>} The decoded status.
1105
+ */
1106
+ async readMagStatus() {
1107
+ const sr = await this._magRead(REG_SR_M, 1);
1108
+ return { drdy: (sr[0] & SR_DRDY) !== 0, lock: (sr[0] & SR_LOCK) !== 0 };
1109
+ }
1110
+
1111
+ /**
1112
+ * Read the magnetometer identity bytes.
1113
+ *
1114
+ * @returns {Promise<Buffer>} The three identity bytes (0x48 0x34 0x33 on a
1115
+ * real part).
1116
+ */
1117
+ async readMagIdentity() {
1118
+ return this._magRead(REG_IRA_M, 3);
1119
+ }
1120
+
1121
+ /**
1122
+ * Enable or disable the magnetometer temperature sensor.
1123
+ *
1124
+ * @param {boolean} enabled - True to enable the sensor, False to disable
1125
+ * it.
1126
+ * @returns {Promise<void>}
1127
+ */
1128
+ async enableTemperature(enabled) {
1129
+ const cra = await this._magRead(REG_CRA_M, 1);
1130
+ const value = enabled ? (cra[0] | CRA_TEMP_EN) : (cra[0] & ~CRA_TEMP_EN);
1131
+ await this._magWrite(REG_CRA_M, value);
1132
+ this._tempEnabled = Boolean(enabled);
1133
+ }
1134
+
1135
+ /**
1136
+ * Read the relative temperature of the magnetometer.
1137
+ *
1138
+ * The sensor is enabled on first use. The datasheet gives no zero
1139
+ * reference, so the value is a *delta* relative to an unspecified
1140
+ * temperature and not an absolute °C reading.
1141
+ *
1142
+ * @returns {Promise<number>} Temperature difference in °C (8 LSB/°C).
1143
+ */
1144
+ async readTemperatureDelta() {
1145
+ if (!this._tempEnabled) {
1146
+ await this.enableTemperature(true);
1147
+ }
1148
+ const raw = await this._magRead(REG_TEMP_OUT_H_M, 2);
1149
+ const value = _toSigned16((raw[0] << 8) | raw[1]) >> 4;
1150
+ return value / 8.0;
1151
+ }
1152
+
1153
+ // --- Compass --------------------------------------------------------------
1154
+
1155
+ /**
1156
+ * Compute the compass heading.
1157
+ *
1158
+ * The heading is measured in the sensor frame: the datasheet does not
1159
+ * document the axis alignment between the two dies, so the tilt
1160
+ * compensation assumes both report in the same body frame.
1161
+ *
1162
+ * @param {boolean} [tiltCompensate=true] - True to compensate for the tilt
1163
+ * reported by the accelerometer, False for the flat (magnetic-only)
1164
+ * heading.
1165
+ * @returns {Promise<number>} The heading in degrees, 0–360.
1166
+ */
1167
+ async heading(tiltCompensate = true) {
1168
+ const m = await this.readMagnetic();
1169
+ const mx = m.x, my = m.y, mz = m.z;
1170
+ if (!tiltCompensate) {
1171
+ return this._wrapDegrees(Math.atan2(my, mx) * 180.0 / Math.PI);
1172
+ }
1173
+ const a = await this.readAcceleration();
1174
+ const ax = a.x, ay = a.y, az = a.z;
1175
+ const roll = Math.atan2(ay, az);
1176
+ const pitch = Math.atan2(-ax, ay * Math.sin(roll) + az * Math.cos(roll));
1177
+ const xh = mx * Math.cos(pitch) + mz * Math.sin(pitch);
1178
+ const yh = (mx * Math.sin(roll) * Math.sin(pitch) + my * Math.cos(roll)
1179
+ - mz * Math.sin(roll) * Math.cos(pitch));
1180
+ return this._wrapDegrees(Math.atan2(yh, xh) * 180.0 / Math.PI);
1181
+ }
1182
+
1183
+ /**
1184
+ * Wrap an angle in degrees into the 0–360 range.
1185
+ *
1186
+ * @param {number} degrees - The angle in degrees.
1187
+ * @returns {number} The wrapped angle.
1188
+ */
1189
+ _wrapDegrees(degrees) {
1190
+ let value = degrees % 360.0;
1191
+ if (value < 0) value += 360.0;
1192
+ return value;
1193
+ }
1194
+
1195
+ // --- Interrupt API ---------------------------------------------------------
1196
+
1197
+ /**
1198
+ * Route one interrupt source to INT1 or INT2.
1199
+ *
1200
+ * Only the routing is configured; the generator or function behind the
1201
+ * source is set up with the `set*` methods.
1202
+ *
1203
+ * @param {number} source - One of the `LSM303Source.SOURCE_*` constants.
1204
+ * @param {number} [pin=1] - 1 for INT1, 2 for INT2. The argument is
1205
+ * ignored for sources that can only appear on one of the two lines.
1206
+ * @returns {Promise<boolean>} True on success.
1207
+ * @throws {Error} If the source cannot be routed or the pin is invalid.
1208
+ */
1209
+ async enableInterrupt(source, pin = 1) {
1210
+ if (pin !== 1 && pin !== 2) {
1211
+ throw new Error('pin must be 1 or 2');
1212
+ }
1213
+ if (source === LSM303Source.SOURCE_MAG_DATA_READY) {
1214
+ throw new Error('SOURCE_MAG_DATA_READY is reported on the DRDY pin and cannot be routed');
1215
+ }
1216
+ const [ctrl3Bit, ctrl6Bit] = this._sourceBits(source);
1217
+ if (ctrl3Bit === null && ctrl6Bit === null) {
1218
+ throw new Error('unknown interrupt source');
1219
+ }
1220
+ if (ctrl3Bit !== null && (pin === 1 || ctrl6Bit === null)) {
1221
+ const ctrl3 = await this._accelRead(REG_CTRL3_A, 1);
1222
+ await this._accelWrite(REG_CTRL3_A, ctrl3[0] | ctrl3Bit);
1223
+ }
1224
+ if (ctrl6Bit !== null && (pin === 2 || ctrl3Bit === null)) {
1225
+ const ctrl6 = await this._accelRead(REG_CTRL6_A, 1);
1226
+ await this._accelWrite(REG_CTRL6_A, ctrl6[0] | ctrl6Bit);
1227
+ }
1228
+ this._enabledSources |= source;
1229
+ return true;
1230
+ }
1231
+
1232
+ /**
1233
+ * Remove one interrupt source from INT1 or INT2.
1234
+ *
1235
+ * @param {number} source - One of the `LSM303Source.SOURCE_*` constants.
1236
+ * @param {number} [pin=1] - 1 for INT1, 2 for INT2.
1237
+ * @returns {Promise<boolean>} True on success.
1238
+ * @throws {Error} If the source cannot be routed or the pin is invalid.
1239
+ */
1240
+ async disableInterrupt(source, pin = 1) {
1241
+ if (pin !== 1 && pin !== 2) {
1242
+ throw new Error('pin must be 1 or 2');
1243
+ }
1244
+ if (source === LSM303Source.SOURCE_MAG_DATA_READY) {
1245
+ throw new Error('SOURCE_MAG_DATA_READY is reported on the DRDY pin and cannot be routed');
1246
+ }
1247
+ const [ctrl3Bit, ctrl6Bit] = this._sourceBits(source);
1248
+ if (ctrl3Bit === null && ctrl6Bit === null) {
1249
+ throw new Error('unknown interrupt source');
1250
+ }
1251
+ if (ctrl3Bit !== null && (pin === 1 || ctrl6Bit === null)) {
1252
+ const ctrl3 = await this._accelRead(REG_CTRL3_A, 1);
1253
+ await this._accelWrite(REG_CTRL3_A, ctrl3[0] & ~ctrl3Bit);
1254
+ }
1255
+ if (ctrl6Bit !== null && (pin === 2 || ctrl3Bit === null)) {
1256
+ const ctrl6 = await this._accelRead(REG_CTRL6_A, 1);
1257
+ await this._accelWrite(REG_CTRL6_A, ctrl6[0] & ~ctrl6Bit);
1258
+ }
1259
+ this._enabledSources &= ~source;
1260
+ return true;
1261
+ }
1262
+
1263
+ /**
1264
+ * Return the (CTRL_REG3_A, CTRL_REG6_A) routing bits of a source.
1265
+ *
1266
+ * @param {number} source - One of the `LSM303Source.SOURCE_*` constants.
1267
+ * @returns {[number|null, number|null]} The two routing bits.
1268
+ */
1269
+ _sourceBits(source) {
1270
+ switch (source) {
1271
+ case LSM303Source.SOURCE_DATA_READY: return [CTRL3_I1_DRDY1, null];
1272
+ case LSM303Source.SOURCE_WATERMARK: return [CTRL3_I1_WTM, null];
1273
+ case LSM303Source.SOURCE_OVERRUN: return [CTRL3_I1_OVERRUN, null];
1274
+ case LSM303Source.SOURCE_IA1: return [CTRL3_I1_AOI1, CTRL6_I2_INT1];
1275
+ case LSM303Source.SOURCE_IA2: return [CTRL3_I1_AOI2, CTRL6_I2_INT2];
1276
+ case LSM303Source.SOURCE_CLICK: return [CTRL3_I1_CLICK, CTRL6_I2_CLICK];
1277
+ case LSM303Source.SOURCE_BOOT: return [null, CTRL6_BOOT_I1];
1278
+ default: return [null, null];
1279
+ }
1280
+ }
1281
+
1282
+ /**
1283
+ * Read the interrupt status of every enabled source.
1284
+ *
1285
+ * Reading `INTx_SOURCE_A` and `CLICK_SRC_A` clears their latched flags.
1286
+ *
1287
+ * @returns {Promise<number>} A mask of the `LSM303Source.SOURCE_*`
1288
+ * constants.
1289
+ */
1290
+ async pollInterrupt() {
1291
+ let status = 0;
1292
+ const regStatus = await this._accelRead(REG_STATUS_A, 1);
1293
+ if (regStatus[0] & STATUS_ZYXDA) {
1294
+ status |= LSM303Source.SOURCE_DATA_READY;
1295
+ }
1296
+ const fifo = await this._accelRead(REG_FIFO_SRC_A, 1);
1297
+ if (fifo[0] & FIFO_SRC_WTM) {
1298
+ status |= LSM303Source.SOURCE_WATERMARK;
1299
+ }
1300
+ if (fifo[0] & FIFO_SRC_OVERRUN) {
1301
+ status |= LSM303Source.SOURCE_OVERRUN;
1302
+ }
1303
+ if (this._enabledSources & LSM303Source.SOURCE_IA1) {
1304
+ const src = await this._accelRead(REG_INT1_SOURCE_A, 1);
1305
+ if (src[0] & SOURCE_IA) status |= LSM303Source.SOURCE_IA1;
1306
+ }
1307
+ if (this._enabledSources & LSM303Source.SOURCE_IA2) {
1308
+ const src = await this._accelRead(REG_INT2_SOURCE_A, 1);
1309
+ if (src[0] & SOURCE_IA) status |= LSM303Source.SOURCE_IA2;
1310
+ }
1311
+ if (this._enabledSources & LSM303Source.SOURCE_CLICK) {
1312
+ const src = await this._accelRead(REG_CLICK_SRC_A, 1);
1313
+ if (src[0] & SOURCE_IA) status |= LSM303Source.SOURCE_CLICK;
1314
+ }
1315
+ const sr = await this._magRead(REG_SR_M, 1);
1316
+ if (sr[0] & SR_DRDY) {
1317
+ status |= LSM303Source.SOURCE_MAG_DATA_READY;
1318
+ }
1319
+ return status;
1320
+ }
1321
+
1322
+ /**
1323
+ * Subscribe to the INT1 or INT2 line.
1324
+ *
1325
+ * The edge direction follows the polarity set with
1326
+ * `setInterruptPolarity`. Without an INT pin — for example on a Linux host
1327
+ * without a GPIO wired up — a 5 ms polling timer takes over.
1328
+ *
1329
+ * @param {function(number):void} callback - Receives the
1330
+ * `pollInterrupt()` mask.
1331
+ * @param {number} [pin=1] - 1 for INT1, 2 for INT2. The line must be
1332
+ * wired to the INT pin of the accelerometer connection.
1333
+ * @returns {Promise<void>}
1334
+ * @throws {Error} If the pin is not 1 or 2.
1335
+ */
1336
+ async onInterrupt(callback, pin = 1) {
1337
+ if (pin !== 1 && pin !== 2) {
1338
+ throw new Error('pin must be 1 or 2');
1339
+ }
1340
+ this._callback = callback;
1341
+ const intPin = this._accel.intPin || null;
1342
+ this._intPinUsed = intPin;
1343
+ if (intPin) {
1344
+ const ctrl6 = await this._accelRead(REG_CTRL6_A, 1);
1345
+ const activeLow = (ctrl6[0] & CTRL6_H_LACTIVE) !== 0;
1346
+ intPin.onEdge(this._intHandler, activeLow ? 'falling' : 'rising');
1347
+ } else {
1348
+ this._lastStatus = await this.pollInterrupt();
1349
+ this._pollTimer = setInterval(() => {
1350
+ this._pollLoop();
1351
+ }, 5);
1352
+ }
1353
+ }
1354
+
1355
+ /**
1356
+ * Unsubscribe from the INT1 or INT2 line.
1357
+ *
1358
+ * @param {number} [pin=1] - 1 for INT1, 2 for INT2.
1359
+ * @returns {Promise<void>}
1360
+ */
1361
+ async offInterrupt(pin = 1) {
1362
+ if (this._intPinUsed) {
1363
+ this._intPinUsed.offEdge(this._intHandler);
1364
+ this._intPinUsed = null;
1365
+ } else if (this._pollTimer) {
1366
+ clearInterval(this._pollTimer);
1367
+ this._pollTimer = null;
1368
+ }
1369
+ this._callback = null;
1370
+ }
1371
+
1372
+ /**
1373
+ * Handle an INT edge: read the status and dispatch it.
1374
+ *
1375
+ * @returns {Promise<void>}
1376
+ */
1377
+ async _intHandler() {
1378
+ const status = await this.pollInterrupt();
1379
+ if (this._callback) {
1380
+ this._callback(status);
1381
+ }
1382
+ }
1383
+
1384
+ /**
1385
+ * Poll the interrupt status every 5 ms and dispatch on change.
1386
+ *
1387
+ * @returns {Promise<void>}
1388
+ */
1389
+ async _pollLoop() {
1390
+ const status = await this.pollInterrupt();
1391
+ if (status !== this._lastStatus) {
1392
+ this._lastStatus = status;
1393
+ if (this._callback) {
1394
+ this._callback(status);
1395
+ }
1396
+ }
1397
+ }
1398
+
1399
+ // --- Miscellaneous ---------------------------------------------------------
1400
+
1401
+ /**
1402
+ * Reboot the accelerometer's memory content.
1403
+ *
1404
+ * The `BOOT` bit reloads the factory trim values. The datasheet does
1405
+ * not say whether the control registers survive, so the initialization
1406
+ * sequence is run again afterwards and any configuration applied after
1407
+ * `init()` has to be repeated.
1408
+ *
1409
+ * @returns {Promise<void>}
1410
+ */
1411
+ async reboot() {
1412
+ const ctrl5 = await this._accelRead(REG_CTRL5_A, 1);
1413
+ await this._accelWrite(REG_CTRL5_A, ctrl5[0] | CTRL5_BOOT);
1414
+ await _delayMs(5);
1415
+ await this._init();
1416
+ this._tempEnabled = false;
1417
+ }
1418
+ };
1419
+
1420
+ /**
1421
+ * LSM303 full interface — extends `LSM303Minimal` with the complete chip API.
1422
+ *
1423
+ * Adds:
1424
+ * - Accelerometer range, output data rate, operating mode and per-axis
1425
+ * enable, plus power-down.
1426
+ * - Raw acceleration and raw magnetic readings.
1427
+ * - High-pass filter configuration and the reference value.
1428
+ * - The 32-level FIFO in bypass / FIFO / stream / trigger mode.
1429
+ * - Two inertial interrupt generators with thresholds, durations,
1430
+ * free-fall and 6D/4D orientation recognition, plus latching.
1431
+ * - Single and double click detection.
1432
+ * - Magnetometer gain, data rate, operating mode, identity, status and
1433
+ * the relative temperature sensor.
1434
+ * - A software compass heading, optionally tilt-compensated.
1435
+ * - The Level-3 interrupt API (INT1 / INT2).
1436
+ */
1437
+ class LSM303Full extends _LSM303FullMixin(LSM303Minimal) {}
1438
+
1439
+ LSM303Full.MODE_LOW_POWER = MODE_LOW_POWER;
1440
+ LSM303Full.MODE_NORMAL = MODE_NORMAL;
1441
+ LSM303Full.MODE_HIGH_RESOLUTION = MODE_HIGH_RESOLUTION;
1442
+ LSM303Full.FIFO_BYPASS = FIFO_BYPASS;
1443
+ LSM303Full.FIFO_MODE = FIFO_MODE;
1444
+ LSM303Full.FIFO_STREAM = FIFO_STREAM;
1445
+ LSM303Full.FIFO_TRIGGER = FIFO_TRIGGER;
1446
+ LSM303Full.MAG_MODE_CONTINUOUS = MAG_MODE_CONTINUOUS;
1447
+ LSM303Full.MAG_MODE_SINGLE = MAG_MODE_SINGLE;
1448
+ LSM303Full.MAG_MODE_SLEEP = MAG_MODE_SLEEP;
1449
+ LSM303Full.CLICK_AXIS_X = CLICK_AXIS_X;
1450
+ LSM303Full.CLICK_AXIS_Y = CLICK_AXIS_Y;
1451
+ LSM303Full.CLICK_AXIS_Z = CLICK_AXIS_Z;
1452
+ LSM303Full.I2C_ADDRESS_ACCEL = I2C_ADDRESS_ACCEL;
1453
+ LSM303Full.I2C_ADDRESS_MAG = I2C_ADDRESS_MAG;
1454
+
1455
+ // The interrupt source constants are also exposed directly on the class, so
1456
+ // `LSM303Full.SOURCE_CLICK` works next to the standalone `LSM303Source`.
1457
+ Object.assign(LSM303Full, LSM303Source);
1458
+
1459
+ LSM303Minimal.MODE_LOW_POWER = MODE_LOW_POWER;
1460
+ LSM303Minimal.MODE_NORMAL = MODE_NORMAL;
1461
+ LSM303Minimal.MODE_HIGH_RESOLUTION = MODE_HIGH_RESOLUTION;
1462
+ LSM303Minimal.I2C_ADDRESS_ACCEL = I2C_ADDRESS_ACCEL;
1463
+ LSM303Minimal.I2C_ADDRESS_MAG = I2C_ADDRESS_MAG;
1464
+
1465
+ module.exports = {
1466
+ LSM303Minimal,
1467
+ LSM303Full,
1468
+ LSM303Source,
1469
+ I2C_ADDRESS_ACCEL,
1470
+ I2C_ADDRESS_MAG,
1471
+ };