periph 1.2.1 → 1.3.1

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 (123) hide show
  1. package/README.md +11 -0
  2. package/examples/accelerometer/bma150/complete.js +62 -0
  3. package/examples/accelerometer/bma150/demo.js +65 -0
  4. package/examples/accelerometer/bma150/minimal.js +18 -0
  5. package/examples/adc_dac/hy3131/complete.js +66 -0
  6. package/examples/adc_dac/hy3131/demo.js +37 -0
  7. package/examples/adc_dac/hy3131/minimal.js +18 -0
  8. package/examples/discovery/complete.js +25 -0
  9. package/examples/discovery/demo.js +30 -0
  10. package/examples/discovery/minimal.js +12 -0
  11. package/examples/display/il0373/complete.js +71 -0
  12. package/examples/display/il0373/demo.js +49 -0
  13. package/examples/display/il0373/minimal.js +26 -0
  14. package/examples/display/ssd1331/complete.js +83 -0
  15. package/examples/display/ssd1331/demo.js +49 -0
  16. package/examples/display/ssd1331/minimal.js +27 -0
  17. package/examples/display/st7036/complete.js +71 -0
  18. package/examples/display/st7036/demo.js +56 -0
  19. package/examples/display/st7036/minimal.js +21 -0
  20. package/examples/display/uc1611s/complete.js +135 -0
  21. package/examples/display/uc1611s/complete_spi.js +128 -0
  22. package/examples/display/uc1611s/demo.js +90 -0
  23. package/examples/display/uc1611s/demo_spi.js +91 -0
  24. package/examples/display/uc1611s/minimal.js +28 -0
  25. package/examples/display/uc1611s/minimal_spi.js +30 -0
  26. package/examples/display/uc8159/complete.js +64 -0
  27. package/examples/display/uc8159/demo.js +41 -0
  28. package/examples/display/uc8159/minimal.js +28 -0
  29. package/examples/display/uc8276c/complete.js +73 -0
  30. package/examples/display/uc8276c/demo.js +49 -0
  31. package/examples/display/uc8276c/minimal.js +26 -0
  32. package/examples/environmental/bme280/complete.js +1 -1
  33. package/examples/environmental/bme280/demo.js +1 -1
  34. package/examples/environmental/bme280/minimal.js +1 -1
  35. package/examples/gyroscope/l3g4200d/complete.js +1 -1
  36. package/examples/gyroscope/l3g4200d/demo.js +1 -1
  37. package/examples/gyroscope/l3g4200d/minimal.js +1 -1
  38. package/examples/memory/23x1024/complete.js +54 -0
  39. package/examples/memory/23x1024/demo.js +51 -0
  40. package/examples/memory/23x1024/minimal.js +22 -0
  41. package/examples/memory/mx25l12845e/complete.js +66 -0
  42. package/examples/memory/mx25l12845e/demo.js +66 -0
  43. package/examples/memory/mx25l12845e/minimal.js +27 -0
  44. package/examples/memory/sd_spi/complete.js +55 -0
  45. package/examples/memory/sd_spi/demo.js +67 -0
  46. package/examples/memory/sd_spi/minimal.js +32 -0
  47. package/examples/pressure/bmp280/complete.js +1 -1
  48. package/examples/pressure/bmp280/demo.js +1 -1
  49. package/examples/pressure/bmp280/minimal.js +1 -1
  50. package/examples/pressure/bmp581/complete.js +1 -1
  51. package/examples/pressure/bmp581/demo.js +1 -1
  52. package/examples/pressure/bmp581/minimal.js +1 -1
  53. package/examples/pressure/lps22df/complete.js +1 -1
  54. package/examples/pressure/lps22df/demo.js +1 -1
  55. package/examples/pressure/lps22df/minimal.js +1 -1
  56. package/package.json +1 -1
  57. package/src/chips/accelerometer/adxl345.js +10 -31
  58. package/src/chips/accelerometer/adxl362.js +7 -7
  59. package/src/chips/accelerometer/bma150.js +419 -0
  60. package/src/chips/adc_dac/ad7705.js +36 -9
  61. package/src/chips/adc_dac/ad7706.js +39 -9
  62. package/src/chips/adc_dac/hy3131.js +311 -0
  63. package/src/chips/display/il0373.js +607 -0
  64. package/src/chips/display/ssd1331.js +412 -0
  65. package/src/chips/display/st7036.js +401 -0
  66. package/src/chips/display/uc1611s.js +648 -0
  67. package/src/chips/display/uc8159.js +394 -0
  68. package/src/chips/display/uc8276c.js +658 -0
  69. package/src/chips/environmental/bme280.js +31 -46
  70. package/src/chips/environmental/bme680.js +38 -43
  71. package/src/chips/gas/ens160.js +18 -26
  72. package/src/chips/gyroscope/l3g4200d.js +34 -50
  73. package/src/chips/gyroscope/l3gd20h.js +24 -40
  74. package/src/chips/imu/mpu6050.js +29 -41
  75. package/src/chips/imu/mpu9250.js +42 -66
  76. package/src/chips/imu/mpu9255.js +51 -75
  77. package/src/chips/io_expander/mcp23017.js +24 -33
  78. package/src/chips/light/apds-9930.js +52 -60
  79. package/src/chips/light/apds9960.js +69 -77
  80. package/src/chips/magnetometer/as5600.js +13 -27
  81. package/src/chips/magnetometer/hmc5883l.js +32 -29
  82. package/src/chips/memory/_23x1024.js +228 -0
  83. package/src/chips/memory/_24aa02uid.js +8 -11
  84. package/src/chips/memory/mx25l12845e.js +467 -0
  85. package/src/chips/memory/sd_spi.js +486 -0
  86. package/src/chips/motor/drv8830.js +4 -4
  87. package/src/chips/other/mpr121.js +7 -7
  88. package/src/chips/power/ina219.js +5 -8
  89. package/src/chips/power/ina226.js +5 -8
  90. package/src/chips/power/ina3221.js +5 -8
  91. package/src/chips/pressure/bmp085.js +17 -11
  92. package/src/chips/pressure/bmp180.js +7 -7
  93. package/src/chips/pressure/bmp280.js +21 -33
  94. package/src/chips/pressure/bmp384.js +28 -39
  95. package/src/chips/pressure/bmp581.js +59 -70
  96. package/src/chips/pressure/lps22df.js +50 -52
  97. package/src/chips/pressure/lps28dfw.js +15 -13
  98. package/src/chips/pressure/lps33hw.js +7 -5
  99. package/src/chips/rtc/ds3231.js +8 -8
  100. package/src/chips/rtc/pcf8523.js +8 -14
  101. package/src/chips/temperature/mcp9808.js +9 -7
  102. package/src/chips/temperature/tmp117.js +4 -4
  103. package/src/connection/connection.js +6 -4
  104. package/src/connection/dhtxx.js +5 -3
  105. package/src/connection/display.js +278 -0
  106. package/src/connection/display_mock.js +77 -0
  107. package/src/connection/hx711.js +5 -3
  108. package/src/connection/i2c.js +6 -4
  109. package/src/connection/i2c_mock.js +47 -0
  110. package/src/connection/input_pin.js +36 -5
  111. package/src/connection/register.js +14 -0
  112. package/src/connection/register_connection.js +65 -0
  113. package/src/connection/sipo.js +4 -2
  114. package/src/connection/sipo_mock.js +49 -0
  115. package/src/connection/smbus.js +6 -4
  116. package/src/connection/softspi.js +73 -0
  117. package/src/connection/spi.js +110 -23
  118. package/src/connection/spi_mock.js +50 -0
  119. package/src/connection/uart.js +2 -1
  120. package/src/discovery/discovery.js +218 -0
  121. package/src/discovery/registry.js +85 -0
  122. package/src/storage/block_device.js +95 -0
  123. package/src/storage/ram_block_device.js +72 -0
@@ -61,11 +61,11 @@ const _MAG_SENSITIVITY_16BIT = 0.15;
61
61
  * - All six axes enabled
62
62
  * - SPI only: I2C_IF_DIS set to prevent accidental I²C re-enable
63
63
  *
64
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
64
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
65
65
  */
66
66
  class MPU9250Minimal {
67
67
  /**
68
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
68
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
69
69
  */
70
70
  constructor(connection) {
71
71
  this._conn = connection;
@@ -75,37 +75,25 @@ class MPU9250Minimal {
75
75
  }
76
76
 
77
77
  async _init() {
78
- await this._writeReg(_REG_PWR_MGMT_1, 0x80);
78
+ await this._conn.writeReg(_REG_PWR_MGMT_1, 0x80);
79
79
  const end1 = Date.now() + 100;
80
80
  while (Date.now() < end1) {}
81
- await this._writeReg(_REG_PWR_MGMT_1, 0x01);
82
- const who = await this._readReg(_REG_WHO_AM_I);
81
+ await this._conn.writeReg(_REG_PWR_MGMT_1, 0x01);
82
+ const who = (await this._conn.readReg(_REG_WHO_AM_I, 1))[0];
83
83
  if (who !== _WHO_AM_I_VALUE) {
84
84
  throw new Error('MPU9250 WHO_AM_I: expected 0x' + _WHO_AM_I_VALUE.toString(16) + ', got 0x' + who.toString(16));
85
85
  }
86
- await this._writeReg(_REG_GYRO_CONFIG, 0x00);
87
- await this._writeReg(_REG_ACCEL_CONFIG, 0x00);
88
- await this._writeReg(_REG_ACCEL_CONFIG2, 0x03);
89
- await this._writeReg(_REG_CONFIG, 0x03);
90
- await this._writeReg(_REG_SMPLRT_DIV, 0x04);
86
+ await this._conn.writeReg(_REG_GYRO_CONFIG, 0x00);
87
+ await this._conn.writeReg(_REG_ACCEL_CONFIG, 0x00);
88
+ await this._conn.writeReg(_REG_ACCEL_CONFIG2, 0x03);
89
+ await this._conn.writeReg(_REG_CONFIG, 0x03);
90
+ await this._conn.writeReg(_REG_SMPLRT_DIV, 0x04);
91
91
  const end2 = Date.now() + 35;
92
92
  while (Date.now() < end2) {}
93
93
  }
94
94
 
95
- async _writeReg(reg, value) {
96
- await this._conn.write(Buffer.from([reg, value]));
97
- }
98
-
99
- async _readReg(reg) {
100
- return (await this._conn.writeRead(Buffer.from([reg]), 1))[0];
101
- }
102
-
103
95
  async _readReg16Signed(reg) {
104
- return (await this._conn.writeRead(Buffer.from([reg]), 2)).readInt16BE(0);
105
- }
106
-
107
- async _readBurst(reg, len) {
108
- return this._conn.writeRead(Buffer.from([reg]), len);
96
+ return (await this._conn.readReg(reg, 2)).readInt16BE(0);
109
97
  }
110
98
 
111
99
  /**
@@ -113,7 +101,7 @@ class MPU9250Minimal {
113
101
  * @returns {Promise<number[]>} [x, y, z] acceleration in m/s².
114
102
  */
115
103
  async accel() {
116
- const buf = await this._readBurst(_REG_ACCEL_XOUT_H, 6);
104
+ const buf = await this._conn.readReg(_REG_ACCEL_XOUT_H, 6);
117
105
  const ax = buf.readInt16BE(0);
118
106
  const ay = buf.readInt16BE(2);
119
107
  const az = buf.readInt16BE(4);
@@ -126,7 +114,7 @@ class MPU9250Minimal {
126
114
  * @returns {Promise<number[]>} [x, y, z] angular rate in rad/s.
127
115
  */
128
116
  async gyro() {
129
- const buf = await this._readBurst(_REG_GYRO_XOUT_H, 6);
117
+ const buf = await this._conn.readReg(_REG_GYRO_XOUT_H, 6);
130
118
  const gx = buf.readInt16BE(0);
131
119
  const gy = buf.readInt16BE(2);
132
120
  const gz = buf.readInt16BE(4);
@@ -154,8 +142,8 @@ class MPU9250Minimal {
154
142
  */
155
143
  class MPU9250Full extends MPU9250Minimal {
156
144
  /**
157
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection pointing at the MPU-9250.
158
- * @param {import('../../connection/connection').Connection} magConnection - Configured I²C connection bound to the AK8963's address (0x0C), on the same bus as connection.
145
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection pointing at the MPU-9250.
146
+ * @param {import('../../connection/register_connection').RegisterConnection} magConnection - I²C or SMBus register connection bound to the AK8963's address (0x0C), on the same bus as connection.
159
147
  */
160
148
  constructor(connection, magConnection) {
161
149
  super(connection);
@@ -167,18 +155,6 @@ class MPU9250Full extends MPU9250Minimal {
167
155
  this._magScaleZ = 1.0;
168
156
  }
169
157
 
170
- async _ak8963Write(reg, value) {
171
- await this._magConn.write(Buffer.from([reg, value]));
172
- }
173
-
174
- async _ak8963Read(reg) {
175
- return (await this._magConn.writeRead(Buffer.from([reg]), 1))[0];
176
- }
177
-
178
- async _ak8963ReadBurst(reg, len) {
179
- return this._magConn.writeRead(Buffer.from([reg]), len);
180
- }
181
-
182
158
  /**
183
159
  * Set gyroscope full-scale range.
184
160
  * @param {number} [fullScale=0] - Range selector 0–3 (0=±250, 1=±500, 2=±1000, 3=±2000 dps).
@@ -186,7 +162,7 @@ class MPU9250Full extends MPU9250Minimal {
186
162
  */
187
163
  async configureGyro(fullScale = 0) {
188
164
  this._gyroFs = fullScale & 0x03;
189
- await this._writeReg(_REG_GYRO_CONFIG, (fullScale & 0x03) << 3);
165
+ await this._conn.writeReg(_REG_GYRO_CONFIG, (fullScale & 0x03) << 3);
190
166
  }
191
167
 
192
168
  /**
@@ -196,7 +172,7 @@ class MPU9250Full extends MPU9250Minimal {
196
172
  */
197
173
  async configureAccel(fullScale = 0) {
198
174
  this._accelFs = fullScale & 0x03;
199
- await this._writeReg(_REG_ACCEL_CONFIG, (fullScale & 0x03) << 3);
175
+ await this._conn.writeReg(_REG_ACCEL_CONFIG, (fullScale & 0x03) << 3);
200
176
  }
201
177
 
202
178
  /**
@@ -206,8 +182,8 @@ class MPU9250Full extends MPU9250Minimal {
206
182
  * @returns {Promise<void>}
207
183
  */
208
184
  async configureDlpf(gyroDlpf = 3, accelDlpf = 3) {
209
- await this._writeReg(_REG_CONFIG, gyroDlpf & 0x07);
210
- await this._writeReg(_REG_ACCEL_CONFIG2, accelDlpf & 0x07);
185
+ await this._conn.writeReg(_REG_CONFIG, gyroDlpf & 0x07);
186
+ await this._conn.writeReg(_REG_ACCEL_CONFIG2, accelDlpf & 0x07);
211
187
  }
212
188
 
213
189
  /**
@@ -216,7 +192,7 @@ class MPU9250Full extends MPU9250Minimal {
216
192
  * @returns {Promise<void>}
217
193
  */
218
194
  async configureSampleRate(divider = 4) {
219
- await this._writeReg(_REG_SMPLRT_DIV, divider & 0xFF);
195
+ await this._conn.writeReg(_REG_SMPLRT_DIV, divider & 0xFF);
220
196
  }
221
197
 
222
198
  /**
@@ -235,27 +211,27 @@ class MPU9250Full extends MPU9250Minimal {
235
211
  * @returns {Promise<void>}
236
212
  */
237
213
  async enableMag(bits = 16, mode = 6) {
238
- await this._writeReg(_REG_INT_PIN_CFG, 0x22);
214
+ await this._conn.writeReg(_REG_INT_PIN_CFG, 0x22);
239
215
  const end1 = Date.now() + 10;
240
216
  while (Date.now() < end1) {}
241
217
 
242
- await this._ak8963Write(_AK8963_REG_CNTL1, 0x00);
218
+ await this._magConn.writeReg(_AK8963_REG_CNTL1, 0x00);
243
219
  const end2 = Date.now() + 10;
244
220
  while (Date.now() < end2) {}
245
221
 
246
- await this._ak8963Write(_AK8963_REG_CNTL1, 0x0F);
222
+ await this._magConn.writeReg(_AK8963_REG_CNTL1, 0x0F);
247
223
  const end3 = Date.now() + 10;
248
224
  while (Date.now() < end3) {}
249
225
 
250
- const asax = await this._ak8963Read(_AK8963_REG_ASAX);
251
- const asay = await this._ak8963Read(_AK8963_REG_ASAY);
252
- const asaz = await this._ak8963Read(_AK8963_REG_ASAZ);
226
+ const asax = (await this._magConn.readReg(_AK8963_REG_ASAX, 1))[0];
227
+ const asay = (await this._magConn.readReg(_AK8963_REG_ASAY, 1))[0];
228
+ const asaz = (await this._magConn.readReg(_AK8963_REG_ASAZ, 1))[0];
253
229
 
254
230
  this._magScaleX = (asax - 128) / 256.0 + 1.0;
255
231
  this._magScaleY = (asay - 128) / 256.0 + 1.0;
256
232
  this._magScaleZ = (asaz - 128) / 256.0 + 1.0;
257
233
 
258
- await this._ak8963Write(_AK8963_REG_CNTL1, 0x00);
234
+ await this._magConn.writeReg(_AK8963_REG_CNTL1, 0x00);
259
235
  const end4 = Date.now() + 10;
260
236
  while (Date.now() < end4) {}
261
237
 
@@ -264,7 +240,7 @@ class MPU9250Full extends MPU9250Minimal {
264
240
  cntl1Val |= 0x10;
265
241
  }
266
242
  cntl1Val |= (mode & 0x0F);
267
- await this._ak8963Write(_AK8963_REG_CNTL1, cntl1Val);
243
+ await this._magConn.writeReg(_AK8963_REG_CNTL1, cntl1Val);
268
244
  const end5 = Date.now() + 10;
269
245
  while (Date.now() < end5) {}
270
246
 
@@ -281,7 +257,7 @@ class MPU9250Full extends MPU9250Minimal {
281
257
  if (!this._magEnabled) {
282
258
  throw new Error('Magnetometer not enabled. Call enableMag() first.');
283
259
  }
284
- const buf = await this._ak8963ReadBurst(_AK8963_REG_HXL, 7);
260
+ const buf = await this._magConn.readReg(_AK8963_REG_HXL, 7);
285
261
  const mx = buf.readInt16LE(0);
286
262
  const my = buf.readInt16LE(2);
287
263
  const mz = buf.readInt16LE(4);
@@ -298,7 +274,7 @@ class MPU9250Full extends MPU9250Minimal {
298
274
  * @returns {Promise<number[]>} [x, y, z] raw 16-bit signed values.
299
275
  */
300
276
  async accelRaw() {
301
- const buf = await this._readBurst(_REG_ACCEL_XOUT_H, 6);
277
+ const buf = await this._conn.readReg(_REG_ACCEL_XOUT_H, 6);
302
278
  return [buf.readInt16BE(0), buf.readInt16BE(2), buf.readInt16BE(4)];
303
279
  }
304
280
 
@@ -307,7 +283,7 @@ class MPU9250Full extends MPU9250Minimal {
307
283
  * @returns {Promise<number[]>} [x, y, z] raw 16-bit signed values.
308
284
  */
309
285
  async gyroRaw() {
310
- const buf = await this._readBurst(_REG_GYRO_XOUT_H, 6);
286
+ const buf = await this._conn.readReg(_REG_GYRO_XOUT_H, 6);
311
287
  return [buf.readInt16BE(0), buf.readInt16BE(2), buf.readInt16BE(4)];
312
288
  }
313
289
 
@@ -321,7 +297,7 @@ class MPU9250Full extends MPU9250Minimal {
321
297
  throw new Error('Magnetometer not enabled. Call enableMag() first.');
322
298
  }
323
299
  // ST2 (buf[6]) is not used but must be read to unlock the next measurement.
324
- const buf = await this._ak8963ReadBurst(_AK8963_REG_HXL, 7);
300
+ const buf = await this._magConn.readReg(_AK8963_REG_HXL, 7);
325
301
  return [buf.readInt16LE(0), buf.readInt16LE(2), buf.readInt16LE(4)];
326
302
  }
327
303
 
@@ -330,7 +306,7 @@ class MPU9250Full extends MPU9250Minimal {
330
306
  * @returns {Promise<boolean>} True when RAW_DATA_RDY_INT is set in INT_STATUS.
331
307
  */
332
308
  async dataReady() {
333
- return !!((await this._readReg(_REG_INT_STATUS)) & 0x01);
309
+ return !!((await this._conn.readReg(_REG_INT_STATUS, 1))[0] & 0x01);
334
310
  }
335
311
 
336
312
  /**
@@ -339,13 +315,13 @@ class MPU9250Full extends MPU9250Minimal {
339
315
  * @returns {Promise<void>}
340
316
  */
341
317
  async setSleep(sleep = true) {
342
- let val = await this._readReg(_REG_PWR_MGMT_1);
318
+ let val = (await this._conn.readReg(_REG_PWR_MGMT_1, 1))[0];
343
319
  if (sleep) {
344
320
  val |= 0x40;
345
321
  } else {
346
322
  val &= ~0x40;
347
323
  }
348
- await this._writeReg(_REG_PWR_MGMT_1, val);
324
+ await this._conn.writeReg(_REG_PWR_MGMT_1, val);
349
325
  }
350
326
 
351
327
  /**
@@ -353,7 +329,7 @@ class MPU9250Full extends MPU9250Minimal {
353
329
  * @returns {Promise<number>} FIFO byte count (0–512).
354
330
  */
355
331
  async fifoCount() {
356
- const buf = await this._readBurst(_REG_FIFO_COUNTH, 2);
332
+ const buf = await this._conn.readReg(_REG_FIFO_COUNTH, 2);
357
333
  return ((buf[0] & 0x1F) << 8) | buf[1];
358
334
  }
359
335
 
@@ -364,7 +340,7 @@ class MPU9250Full extends MPU9250Minimal {
364
340
  async readFifo() {
365
341
  const count = await this.fifoCount();
366
342
  if (count === 0) return Buffer.alloc(0);
367
- return this._readBurst(_REG_FIFO_R_W, count);
343
+ return this._conn.readReg(_REG_FIFO_R_W, count);
368
344
  }
369
345
 
370
346
  /**
@@ -376,9 +352,9 @@ class MPU9250Full extends MPU9250Minimal {
376
352
  */
377
353
  async enableFifo(gyro = true, accel = true, temp = false) {
378
354
  const fifoEn = ((accel ? 1 : 0) << 3) | ((temp ? 1 : 0) << 2) | ((gyro ? 1 : 0) << 4);
379
- await this._writeReg(_REG_FIFO_EN, fifoEn);
380
- const userCtrl = await this._readReg(_REG_USER_CTRL);
381
- await this._writeReg(_REG_USER_CTRL, userCtrl | 0x40);
355
+ await this._conn.writeReg(_REG_FIFO_EN, fifoEn);
356
+ const userCtrl = (await this._conn.readReg(_REG_USER_CTRL, 1))[0];
357
+ await this._conn.writeReg(_REG_USER_CTRL, userCtrl | 0x40);
382
358
  }
383
359
 
384
360
  /**
@@ -386,8 +362,8 @@ class MPU9250Full extends MPU9250Minimal {
386
362
  * @returns {Promise<void>}
387
363
  */
388
364
  async resetFifo() {
389
- const userCtrl = await this._readReg(_REG_USER_CTRL);
390
- await this._writeReg(_REG_USER_CTRL, userCtrl | 0x04);
365
+ const userCtrl = (await this._conn.readReg(_REG_USER_CTRL, 1))[0];
366
+ await this._conn.writeReg(_REG_USER_CTRL, userCtrl | 0x04);
391
367
  }
392
368
  }
393
369
 
@@ -63,11 +63,11 @@ const _MAG_SENSITIVITY_16BIT = 0.15;
63
63
  * - All six axes enabled
64
64
  * - SPI only: I2C_IF_DIS set to prevent accidental I²C re-enable
65
65
  *
66
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
66
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
67
67
  */
68
68
  class MPU9255Minimal {
69
69
  /**
70
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
70
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
71
71
  */
72
72
  constructor(connection) {
73
73
  this._conn = connection;
@@ -77,37 +77,25 @@ class MPU9255Minimal {
77
77
  }
78
78
 
79
79
  async _init() {
80
- await this._writeReg(_REG_PWR_MGMT_1, 0x80);
80
+ await this._conn.writeReg(_REG_PWR_MGMT_1, 0x80);
81
81
  const end1 = Date.now() + 100;
82
82
  while (Date.now() < end1) {}
83
- await this._writeReg(_REG_PWR_MGMT_1, 0x01);
84
- const who = await this._readReg(_REG_WHO_AM_I);
83
+ await this._conn.writeReg(_REG_PWR_MGMT_1, 0x01);
84
+ const who = (await this._conn.readReg(_REG_WHO_AM_I, 1))[0];
85
85
  if (who !== _WHO_AM_I_VALUE) {
86
86
  throw new Error('MPU9255 WHO_AM_I: expected 0x' + _WHO_AM_I_VALUE.toString(16) + ', got 0x' + who.toString(16));
87
87
  }
88
- await this._writeReg(_REG_GYRO_CONFIG, 0x00);
89
- await this._writeReg(_REG_ACCEL_CONFIG, 0x00);
90
- await this._writeReg(_REG_ACCEL_CONFIG2, 0x03);
91
- await this._writeReg(_REG_CONFIG, 0x03);
92
- await this._writeReg(_REG_SMPLRT_DIV, 0x04);
88
+ await this._conn.writeReg(_REG_GYRO_CONFIG, 0x00);
89
+ await this._conn.writeReg(_REG_ACCEL_CONFIG, 0x00);
90
+ await this._conn.writeReg(_REG_ACCEL_CONFIG2, 0x03);
91
+ await this._conn.writeReg(_REG_CONFIG, 0x03);
92
+ await this._conn.writeReg(_REG_SMPLRT_DIV, 0x04);
93
93
  const end2 = Date.now() + 35;
94
94
  while (Date.now() < end2) {}
95
95
  }
96
96
 
97
- async _writeReg(reg, value) {
98
- await this._conn.write(Buffer.from([reg, value]));
99
- }
100
-
101
- async _readReg(reg) {
102
- return (await this._conn.writeRead(Buffer.from([reg]), 1))[0];
103
- }
104
-
105
97
  async _readReg16Signed(reg) {
106
- return (await this._conn.writeRead(Buffer.from([reg]), 2)).readInt16BE(0);
107
- }
108
-
109
- async _readBurst(reg, len) {
110
- return this._conn.writeRead(Buffer.from([reg]), len);
98
+ return (await this._conn.readReg(reg, 2)).readInt16BE(0);
111
99
  }
112
100
 
113
101
  /**
@@ -115,7 +103,7 @@ class MPU9255Minimal {
115
103
  * @returns {Promise<number[]>} [x, y, z] acceleration in m/s².
116
104
  */
117
105
  async accel() {
118
- const buf = await this._readBurst(_REG_ACCEL_XOUT_H, 6);
106
+ const buf = await this._conn.readReg(_REG_ACCEL_XOUT_H, 6);
119
107
  const ax = buf.readInt16BE(0);
120
108
  const ay = buf.readInt16BE(2);
121
109
  const az = buf.readInt16BE(4);
@@ -128,7 +116,7 @@ class MPU9255Minimal {
128
116
  * @returns {Promise<number[]>} [x, y, z] angular rate in rad/s.
129
117
  */
130
118
  async gyro() {
131
- const buf = await this._readBurst(_REG_GYRO_XOUT_H, 6);
119
+ const buf = await this._conn.readReg(_REG_GYRO_XOUT_H, 6);
132
120
  const gx = buf.readInt16BE(0);
133
121
  const gy = buf.readInt16BE(2);
134
122
  const gz = buf.readInt16BE(4);
@@ -157,8 +145,8 @@ class MPU9255Minimal {
157
145
  */
158
146
  class MPU9255Full extends MPU9255Minimal {
159
147
  /**
160
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection pointing at the MPU-9255.
161
- * @param {import('../../connection/connection').Connection} magConnection - Configured I²C connection bound to the AK8963's address (0x0C), on the same bus as connection.
148
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection pointing at the MPU-9255.
149
+ * @param {import('../../connection/register_connection').RegisterConnection} magConnection - I²C or SMBus register connection bound to the AK8963's address (0x0C), on the same bus as connection.
162
150
  */
163
151
  constructor(connection, magConnection) {
164
152
  super(connection);
@@ -170,18 +158,6 @@ class MPU9255Full extends MPU9255Minimal {
170
158
  this._magScaleZ = 1.0;
171
159
  }
172
160
 
173
- async _ak8963Write(reg, value) {
174
- await this._magConn.write(Buffer.from([reg, value]));
175
- }
176
-
177
- async _ak8963Read(reg) {
178
- return (await this._magConn.writeRead(Buffer.from([reg]), 1))[0];
179
- }
180
-
181
- async _ak8963ReadBurst(reg, len) {
182
- return this._magConn.writeRead(Buffer.from([reg]), len);
183
- }
184
-
185
161
  /**
186
162
  * Set gyroscope full-scale range.
187
163
  * @param {number} [fullScale=0] - Range selector 0–3 (0=±250, 1=±500, 2=±1000, 3=±2000 dps).
@@ -189,7 +165,7 @@ class MPU9255Full extends MPU9255Minimal {
189
165
  */
190
166
  async configureGyro(fullScale = 0) {
191
167
  this._gyroFs = fullScale & 0x03;
192
- await this._writeReg(_REG_GYRO_CONFIG, (fullScale & 0x03) << 3);
168
+ await this._conn.writeReg(_REG_GYRO_CONFIG, (fullScale & 0x03) << 3);
193
169
  }
194
170
 
195
171
  /**
@@ -199,7 +175,7 @@ class MPU9255Full extends MPU9255Minimal {
199
175
  */
200
176
  async configureAccel(fullScale = 0) {
201
177
  this._accelFs = fullScale & 0x03;
202
- await this._writeReg(_REG_ACCEL_CONFIG, (fullScale & 0x03) << 3);
178
+ await this._conn.writeReg(_REG_ACCEL_CONFIG, (fullScale & 0x03) << 3);
203
179
  }
204
180
 
205
181
  /**
@@ -209,8 +185,8 @@ class MPU9255Full extends MPU9255Minimal {
209
185
  * @returns {Promise<void>}
210
186
  */
211
187
  async configureDlpf(gyroDlpf = 3, accelDlpf = 3) {
212
- await this._writeReg(_REG_CONFIG, gyroDlpf & 0x07);
213
- await this._writeReg(_REG_ACCEL_CONFIG2, accelDlpf & 0x07);
188
+ await this._conn.writeReg(_REG_CONFIG, gyroDlpf & 0x07);
189
+ await this._conn.writeReg(_REG_ACCEL_CONFIG2, accelDlpf & 0x07);
214
190
  }
215
191
 
216
192
  /**
@@ -219,7 +195,7 @@ class MPU9255Full extends MPU9255Minimal {
219
195
  * @returns {Promise<void>}
220
196
  */
221
197
  async configureSampleRate(divider = 4) {
222
- await this._writeReg(_REG_SMPLRT_DIV, divider & 0xFF);
198
+ await this._conn.writeReg(_REG_SMPLRT_DIV, divider & 0xFF);
223
199
  }
224
200
 
225
201
  /**
@@ -238,27 +214,27 @@ class MPU9255Full extends MPU9255Minimal {
238
214
  * @returns {Promise<void>}
239
215
  */
240
216
  async enableMag(bits = 16, mode = 6) {
241
- await this._writeReg(_REG_INT_PIN_CFG, 0x22);
217
+ await this._conn.writeReg(_REG_INT_PIN_CFG, 0x22);
242
218
  const end1 = Date.now() + 10;
243
219
  while (Date.now() < end1) {}
244
220
 
245
- await this._ak8963Write(_AK8963_REG_CNTL1, 0x00);
221
+ await this._magConn.writeReg(_AK8963_REG_CNTL1, 0x00);
246
222
  const end2 = Date.now() + 10;
247
223
  while (Date.now() < end2) {}
248
224
 
249
- await this._ak8963Write(_AK8963_REG_CNTL1, 0x0F);
225
+ await this._magConn.writeReg(_AK8963_REG_CNTL1, 0x0F);
250
226
  const end3 = Date.now() + 10;
251
227
  while (Date.now() < end3) {}
252
228
 
253
- const asax = await this._ak8963Read(_AK8963_REG_ASAX);
254
- const asay = await this._ak8963Read(_AK8963_REG_ASAY);
255
- const asaz = await this._ak8963Read(_AK8963_REG_ASAZ);
229
+ const asax = (await this._magConn.readReg(_AK8963_REG_ASAX, 1))[0];
230
+ const asay = (await this._magConn.readReg(_AK8963_REG_ASAY, 1))[0];
231
+ const asaz = (await this._magConn.readReg(_AK8963_REG_ASAZ, 1))[0];
256
232
 
257
233
  this._magScaleX = (asax - 128) / 256.0 + 1.0;
258
234
  this._magScaleY = (asay - 128) / 256.0 + 1.0;
259
235
  this._magScaleZ = (asaz - 128) / 256.0 + 1.0;
260
236
 
261
- await this._ak8963Write(_AK8963_REG_CNTL1, 0x00);
237
+ await this._magConn.writeReg(_AK8963_REG_CNTL1, 0x00);
262
238
  const end4 = Date.now() + 10;
263
239
  while (Date.now() < end4) {}
264
240
 
@@ -267,7 +243,7 @@ class MPU9255Full extends MPU9255Minimal {
267
243
  cntl1Val |= 0x10;
268
244
  }
269
245
  cntl1Val |= (mode & 0x0F);
270
- await this._ak8963Write(_AK8963_REG_CNTL1, cntl1Val);
246
+ await this._magConn.writeReg(_AK8963_REG_CNTL1, cntl1Val);
271
247
  const end5 = Date.now() + 10;
272
248
  while (Date.now() < end5) {}
273
249
 
@@ -284,7 +260,7 @@ class MPU9255Full extends MPU9255Minimal {
284
260
  if (!this._magEnabled) {
285
261
  throw new Error('Magnetometer not enabled. Call enableMag() first.');
286
262
  }
287
- const buf = await this._ak8963ReadBurst(_AK8963_REG_HXL, 7);
263
+ const buf = await this._magConn.readReg(_AK8963_REG_HXL, 7);
288
264
  const mx = buf.readInt16LE(0);
289
265
  const my = buf.readInt16LE(2);
290
266
  const mz = buf.readInt16LE(4);
@@ -301,7 +277,7 @@ class MPU9255Full extends MPU9255Minimal {
301
277
  * @returns {Promise<number[]>} [x, y, z] raw 16-bit signed values.
302
278
  */
303
279
  async accelRaw() {
304
- const buf = await this._readBurst(_REG_ACCEL_XOUT_H, 6);
280
+ const buf = await this._conn.readReg(_REG_ACCEL_XOUT_H, 6);
305
281
  return [buf.readInt16BE(0), buf.readInt16BE(2), buf.readInt16BE(4)];
306
282
  }
307
283
 
@@ -310,7 +286,7 @@ class MPU9255Full extends MPU9255Minimal {
310
286
  * @returns {Promise<number[]>} [x, y, z] raw 16-bit signed values.
311
287
  */
312
288
  async gyroRaw() {
313
- const buf = await this._readBurst(_REG_GYRO_XOUT_H, 6);
289
+ const buf = await this._conn.readReg(_REG_GYRO_XOUT_H, 6);
314
290
  return [buf.readInt16BE(0), buf.readInt16BE(2), buf.readInt16BE(4)];
315
291
  }
316
292
 
@@ -324,7 +300,7 @@ class MPU9255Full extends MPU9255Minimal {
324
300
  throw new Error('Magnetometer not enabled. Call enableMag() first.');
325
301
  }
326
302
  // ST2 (buf[6]) is not used but must be read to unlock the next measurement.
327
- const buf = await this._ak8963ReadBurst(_AK8963_REG_HXL, 7);
303
+ const buf = await this._magConn.readReg(_AK8963_REG_HXL, 7);
328
304
  return [buf.readInt16LE(0), buf.readInt16LE(2), buf.readInt16LE(4)];
329
305
  }
330
306
 
@@ -333,7 +309,7 @@ class MPU9255Full extends MPU9255Minimal {
333
309
  * @returns {Promise<boolean>} True when RAW_DATA_RDY_INT is set in INT_STATUS.
334
310
  */
335
311
  async dataReady() {
336
- return !!((await this._readReg(_REG_INT_STATUS)) & 0x01);
312
+ return !!((await this._conn.readReg(_REG_INT_STATUS, 1))[0] & 0x01);
337
313
  }
338
314
 
339
315
  /**
@@ -342,13 +318,13 @@ class MPU9255Full extends MPU9255Minimal {
342
318
  * @returns {Promise<void>}
343
319
  */
344
320
  async setSleep(sleep = true) {
345
- let val = await this._readReg(_REG_PWR_MGMT_1);
321
+ let val = (await this._conn.readReg(_REG_PWR_MGMT_1, 1))[0];
346
322
  if (sleep) {
347
323
  val |= 0x40;
348
324
  } else {
349
325
  val &= ~0x40;
350
326
  }
351
- await this._writeReg(_REG_PWR_MGMT_1, val);
327
+ await this._conn.writeReg(_REG_PWR_MGMT_1, val);
352
328
  }
353
329
 
354
330
  /**
@@ -356,7 +332,7 @@ class MPU9255Full extends MPU9255Minimal {
356
332
  * @returns {Promise<number>} FIFO byte count (0–512).
357
333
  */
358
334
  async fifoCount() {
359
- const buf = await this._readBurst(_REG_FIFO_COUNTH, 2);
335
+ const buf = await this._conn.readReg(_REG_FIFO_COUNTH, 2);
360
336
  return ((buf[0] & 0x1F) << 8) | buf[1];
361
337
  }
362
338
 
@@ -367,7 +343,7 @@ class MPU9255Full extends MPU9255Minimal {
367
343
  async readFifo() {
368
344
  const count = await this.fifoCount();
369
345
  if (count === 0) return Buffer.alloc(0);
370
- return this._readBurst(_REG_FIFO_R_W, count);
346
+ return this._conn.readReg(_REG_FIFO_R_W, count);
371
347
  }
372
348
 
373
349
  /**
@@ -379,9 +355,9 @@ class MPU9255Full extends MPU9255Minimal {
379
355
  */
380
356
  async enableFifo(gyro = true, accel = true, temp = false) {
381
357
  const fifoEn = ((accel ? 1 : 0) << 3) | ((temp ? 1 : 0) << 2) | ((gyro ? 1 : 0) << 4);
382
- await this._writeReg(_REG_FIFO_EN, fifoEn);
383
- const userCtrl = await this._readReg(_REG_USER_CTRL);
384
- await this._writeReg(_REG_USER_CTRL, userCtrl | 0x40);
358
+ await this._conn.writeReg(_REG_FIFO_EN, fifoEn);
359
+ const userCtrl = (await this._conn.readReg(_REG_USER_CTRL, 1))[0];
360
+ await this._conn.writeReg(_REG_USER_CTRL, userCtrl | 0x40);
385
361
  }
386
362
 
387
363
  /**
@@ -389,8 +365,8 @@ class MPU9255Full extends MPU9255Minimal {
389
365
  * @returns {Promise<void>}
390
366
  */
391
367
  async resetFifo() {
392
- const userCtrl = await this._readReg(_REG_USER_CTRL);
393
- await this._writeReg(_REG_USER_CTRL, userCtrl | 0x04);
368
+ const userCtrl = (await this._conn.readReg(_REG_USER_CTRL, 1))[0];
369
+ await this._conn.writeReg(_REG_USER_CTRL, userCtrl | 0x04);
394
370
  }
395
371
 
396
372
  /**
@@ -425,14 +401,14 @@ class MPU9255Full extends MPU9255Minimal {
425
401
  }
426
402
  }
427
403
 
428
- await this._writeReg(_REG_PWR_MGMT_1, 0x01);
429
- await this._writeReg(_REG_PWR_MGMT_2, 0x07);
430
- await this._writeReg(_REG_ACCEL_CONFIG2, 0x01);
431
- await this._writeReg(_REG_INT_ENABLE, 0x40);
432
- await this._writeReg(_REG_MOT_DETECT_CTRL, 0xC0);
433
- await this._writeReg(_REG_WOM_THR, thresholdLsb & 0xFF);
434
- await this._writeReg(_REG_LP_ACCEL_ODR, bestSel & 0x0F);
435
- await this._writeReg(_REG_PWR_MGMT_1, 0x21);
404
+ await this._conn.writeReg(_REG_PWR_MGMT_1, 0x01);
405
+ await this._conn.writeReg(_REG_PWR_MGMT_2, 0x07);
406
+ await this._conn.writeReg(_REG_ACCEL_CONFIG2, 0x01);
407
+ await this._conn.writeReg(_REG_INT_ENABLE, 0x40);
408
+ await this._conn.writeReg(_REG_MOT_DETECT_CTRL, 0xC0);
409
+ await this._conn.writeReg(_REG_WOM_THR, thresholdLsb & 0xFF);
410
+ await this._conn.writeReg(_REG_LP_ACCEL_ODR, bestSel & 0x0F);
411
+ await this._conn.writeReg(_REG_PWR_MGMT_1, 0x21);
436
412
  }
437
413
 
438
414
  /**
@@ -440,7 +416,7 @@ class MPU9255Full extends MPU9255Minimal {
440
416
  * @returns {Promise<boolean>} True when WOM_INT (bit 6) is set in INT_STATUS.
441
417
  */
442
418
  async motionDetected() {
443
- return !!((await this._readReg(_REG_INT_STATUS)) & 0x40);
419
+ return !!((await this._conn.readReg(_REG_INT_STATUS, 1))[0] & 0x40);
444
420
  }
445
421
  }
446
422