periph 1.2.1 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (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
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const { toSigned } = require('../../connection/register');
4
+
3
5
  const _REG_WHO_AM_I = 0x0F;
4
6
  const _REG_CTRL_REG1 = 0x20;
5
7
  const _REG_CTRL_REG2 = 0x21;
@@ -39,9 +41,7 @@ const _SENSITIVITY = {
39
41
  const _DPS_TO_RAD = Math.PI / 180.0;
40
42
 
41
43
  function _int16Le(data, offset) {
42
- let v = data[offset] | (data[offset + 1] << 8);
43
- if (v & 0x8000) v -= 0x10000;
44
- return v;
44
+ return toSigned(data[offset] | (data[offset + 1] << 8), 16);
45
45
  }
46
46
 
47
47
  class L3G4200DMinimal {
@@ -60,12 +60,10 @@ class L3G4200DMinimal {
60
60
  * - FIFO disabled
61
61
  * - HPF disabled
62
62
  *
63
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
64
- * @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
63
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C, SMBus, or SPI register connection (SPI: readBit=0xC0, no multi-byte bit).
65
64
  */
66
- constructor(connection, busType = 'i2c') {
65
+ constructor(connection) {
67
66
  this._conn = connection;
68
- this._busType = busType;
69
67
  this._fullScale = 250;
70
68
  this._init();
71
69
  }
@@ -74,26 +72,15 @@ class L3G4200DMinimal {
74
72
  try {
75
73
  const who = (await this._readReg(_REG_WHO_AM_I, 1))[0];
76
74
  if (who !== _WHO_AM_I_EXPECTED) return;
77
- await this._writeReg(_REG_CTRL_REG4, _CTRL_REG4_DEFAULT);
78
- await this._writeReg(_REG_CTRL_REG1, _CTRL_REG1_DEFAULT);
75
+ await this._conn.writeReg(_REG_CTRL_REG4, _CTRL_REG4_DEFAULT);
76
+ await this._conn.writeReg(_REG_CTRL_REG1, _CTRL_REG1_DEFAULT);
79
77
  } catch (e) { /* bus may be idle */ }
80
78
  }
81
79
 
82
- async _writeReg(reg, value) {
83
- const addr = this._busType === 'spi' ? (reg & 0x3F) : reg;
84
- await this._conn.write(Buffer.from([addr, value & 0xFF]));
85
- }
86
-
87
80
  async _readReg(reg, n) {
88
- let addr;
89
- if (this._busType === 'spi') {
90
- addr = reg | 0xC0; // READ=1, MS=1 (auto-increment)
91
- } else if (n > 1) {
92
- addr = reg | 0x80; // MSB set = I²C multi-byte auto-increment
93
- } else {
94
- addr = reg;
95
- }
96
- return this._conn.writeRead(Buffer.from([addr & 0xFF]), n);
81
+ // I²C needs bit 7 of the sub-address set for multi-byte auto-increment;
82
+ // on SPI the connection's readBit=0xC0 already ORs it in (idempotent).
83
+ return this._conn.readReg(n > 1 ? reg | 0x80 : reg, n);
97
84
  }
98
85
 
99
86
  _sensitivity() {
@@ -119,11 +106,10 @@ class L3G4200DFull extends L3G4200DMinimal {
119
106
  * L3G4200D full interface — extends L3G4200DMinimal with full configuration,
120
107
  * FIFO, high-pass filter, interrupts, axis-enable, and power-mode control.
121
108
  *
122
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
123
- * @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
109
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C, SMBus, or SPI register connection (SPI: readBit=0xC0, no multi-byte bit).
124
110
  */
125
- constructor(connection, busType = 'i2c') {
126
- super(connection, busType);
111
+ constructor(connection) {
112
+ super(connection);
127
113
  this._odr = 0;
128
114
  this._bw = 0;
129
115
  }
@@ -160,9 +146,9 @@ class L3G4200DFull extends L3G4200DMinimal {
160
146
  this._bw = bandwidth & 0x3;
161
147
  this._fullScale = fullScale;
162
148
  const ctrl1 = _CTRL_REG1_DEFAULT | ((this._odr & 0x3) << 6) | ((this._bw & 0x3) << 4);
163
- await this._writeReg(_REG_CTRL_REG1, ctrl1);
149
+ await this._conn.writeReg(_REG_CTRL_REG1, ctrl1);
164
150
  const fsBits = fullScale === 250 ? 0 : (fullScale === 500 ? 1 : 2);
165
- await this._writeReg(_REG_CTRL_REG4, _CTRL_REG4_DEFAULT | ((fsBits & 0x3) << 4));
151
+ await this._conn.writeReg(_REG_CTRL_REG4, _CTRL_REG4_DEFAULT | ((fsBits & 0x3) << 4));
166
152
  }
167
153
 
168
154
  /**
@@ -176,7 +162,7 @@ class L3G4200DFull extends L3G4200DMinimal {
176
162
  this._fullScale = fullScale;
177
163
  const fsBits = fullScale === 250 ? 0 : (fullScale === 500 ? 1 : 2);
178
164
  const ctrl4 = (await this._readReg(_REG_CTRL_REG4, 1))[0];
179
- await this._writeReg(_REG_CTRL_REG4, (ctrl4 & 0xCF) | ((fsBits & 0x3) << 4));
165
+ await this._conn.writeReg(_REG_CTRL_REG4, (ctrl4 & 0xCF) | ((fsBits & 0x3) << 4));
180
166
  }
181
167
 
182
168
  /** @returns {Promise<number>} WHO_AM_I (0xD3 for genuine L3G4200D). */
@@ -196,26 +182,24 @@ class L3G4200DFull extends L3G4200DMinimal {
196
182
 
197
183
  /** @returns {Promise<number>} Signed 8-bit temperature count (-1 °C/digit). */
198
184
  async temperature() {
199
- let raw = (await this._readReg(_REG_OUT_TEMP, 1))[0];
200
- if (raw & 0x80) raw -= 0x100;
201
- return raw;
185
+ return toSigned((await this._readReg(_REG_OUT_TEMP, 1))[0], 8);
202
186
  }
203
187
 
204
188
  /** Enter power-down mode (PD=0 in CTRL_REG1). */
205
189
  async powerDown() {
206
190
  const ctrl1 = (await this._readReg(_REG_CTRL_REG1, 1))[0] & 0xF7;
207
- await this._writeReg(_REG_CTRL_REG1, ctrl1);
191
+ await this._conn.writeReg(_REG_CTRL_REG1, ctrl1);
208
192
  }
209
193
 
210
194
  /** Wake from power-down (PD=1); previously enabled axes restored. */
211
195
  async wakeUp() {
212
196
  const ctrl1 = (await this._readReg(_REG_CTRL_REG1, 1))[0] | 0x08;
213
- await this._writeReg(_REG_CTRL_REG1, ctrl1);
197
+ await this._conn.writeReg(_REG_CTRL_REG1, ctrl1);
214
198
  }
215
199
 
216
200
  /** Enter sleep mode (PD=1, all axes disabled). */
217
201
  async sleep() {
218
- await this._writeReg(_REG_CTRL_REG1, 0x08);
202
+ await this._conn.writeReg(_REG_CTRL_REG1, 0x08);
219
203
  }
220
204
 
221
205
  /**
@@ -227,7 +211,7 @@ class L3G4200DFull extends L3G4200DMinimal {
227
211
  async enableAxes(x, y, z) {
228
212
  const ctrl1 = (await this._readReg(_REG_CTRL_REG1, 1))[0] & 0xF8;
229
213
  const val = ctrl1 | (z ? 0x04 : 0) | (y ? 0x02 : 0) | (x ? 0x01 : 0);
230
- await this._writeReg(_REG_CTRL_REG1, val);
214
+ await this._conn.writeReg(_REG_CTRL_REG1, val);
231
215
  }
232
216
 
233
217
  /**
@@ -239,15 +223,15 @@ class L3G4200DFull extends L3G4200DMinimal {
239
223
  if (mode < 0 || mode > 4) throw new Error('mode must be 0..4');
240
224
  if (watermark < 0 || watermark > 31) throw new Error('watermark must be 0..31');
241
225
  const ctrl5 = (await this._readReg(_REG_CTRL_REG5, 1))[0] | 0x40;
242
- await this._writeReg(_REG_CTRL_REG5, ctrl5);
243
- await this._writeReg(_REG_FIFO_CTRL, ((mode & 0x7) << 5) | (watermark & 0x1F));
226
+ await this._conn.writeReg(_REG_CTRL_REG5, ctrl5);
227
+ await this._conn.writeReg(_REG_FIFO_CTRL, ((mode & 0x7) << 5) | (watermark & 0x1F));
244
228
  }
245
229
 
246
230
  /** Disable the FIFO. */
247
231
  async disableFifo() {
248
232
  const ctrl5 = (await this._readReg(_REG_CTRL_REG5, 1))[0] & ~0x40;
249
- await this._writeReg(_REG_CTRL_REG5, ctrl5);
250
- await this._writeReg(_REG_FIFO_CTRL, 0x00);
233
+ await this._conn.writeReg(_REG_CTRL_REG5, ctrl5);
234
+ await this._conn.writeReg(_REG_FIFO_CTRL, 0x00);
251
235
  }
252
236
 
253
237
  /** @returns {Promise<number>} FSS[4:0] from FIFO_SRC_REG. */
@@ -283,15 +267,15 @@ class L3G4200DFull extends L3G4200DMinimal {
283
267
  async enableHighpass(mode, cutoff) {
284
268
  if (mode < 0 || mode > 3) throw new Error('mode must be 0..3');
285
269
  if (cutoff < 0 || cutoff > 9) throw new Error('cutoff must be 0..9');
286
- await this._writeReg(_REG_CTRL_REG2, ((mode & 0x3) << 4) | (cutoff & 0x0F));
270
+ await this._conn.writeReg(_REG_CTRL_REG2, ((mode & 0x3) << 4) | (cutoff & 0x0F));
287
271
  const ctrl5 = (await this._readReg(_REG_CTRL_REG5, 1))[0] | 0x10;
288
- await this._writeReg(_REG_CTRL_REG5, ctrl5);
272
+ await this._conn.writeReg(_REG_CTRL_REG5, ctrl5);
289
273
  }
290
274
 
291
275
  /** Disable the high-pass filter. */
292
276
  async disableHighpass() {
293
277
  const ctrl5 = (await this._readReg(_REG_CTRL_REG5, 1))[0] & ~0x10;
294
- await this._writeReg(_REG_CTRL_REG5, ctrl5);
278
+ await this._conn.writeReg(_REG_CTRL_REG5, ctrl5);
295
279
  }
296
280
 
297
281
  /**
@@ -315,10 +299,10 @@ class L3G4200DFull extends L3G4200DMinimal {
315
299
  if (yLow) cfg |= 0x04;
316
300
  if (xHigh) cfg |= 0x02;
317
301
  if (xLow) cfg |= 0x01;
318
- await this._writeReg(_REG_INT1_CFG, cfg);
302
+ await this._conn.writeReg(_REG_INT1_CFG, cfg);
319
303
  if (cfg & 0x3F) {
320
304
  const ctrl3 = (await this._readReg(_REG_CTRL_REG3, 1))[0] | 0x80;
321
- await this._writeReg(_REG_CTRL_REG3, ctrl3);
305
+ await this._conn.writeReg(_REG_CTRL_REG3, ctrl3);
322
306
  }
323
307
  }
324
308
 
@@ -334,8 +318,8 @@ class L3G4200DFull extends L3G4200DMinimal {
334
318
  else if (axis === 'y') { hi = _REG_INT1_THS_YH; lo = _REG_INT1_THS_YL; }
335
319
  else if (axis === 'z') { hi = _REG_INT1_THS_ZH; lo = _REG_INT1_THS_ZL; }
336
320
  else throw new Error("axis must be 'x', 'y' or 'z'");
337
- await this._writeReg(hi, (raw >> 8) & 0x7F);
338
- await this._writeReg(lo, raw & 0xFF);
321
+ await this._conn.writeReg(hi, (raw >> 8) & 0x7F);
322
+ await this._conn.writeReg(lo, raw & 0xFF);
339
323
  }
340
324
 
341
325
  /**
@@ -345,7 +329,7 @@ class L3G4200DFull extends L3G4200DMinimal {
345
329
  */
346
330
  async setDuration(samples, wait) {
347
331
  if (samples < 0 || samples > 127) throw new Error('samples must be 0..127');
348
- await this._writeReg(_REG_INT1_DURATION, ((wait ? 1 : 0) << 7) | (samples & 0x7F));
332
+ await this._conn.writeReg(_REG_INT1_DURATION, ((wait ? 1 : 0) << 7) | (samples & 0x7F));
349
333
  }
350
334
 
351
335
  /** @returns {Promise<number>} Raw INT1_SRC byte; reading clears the interrupt-active bit. */
@@ -359,7 +343,7 @@ class L3G4200DFull extends L3G4200DMinimal {
359
343
  */
360
344
  async setDataReadyPin(enable) {
361
345
  const ctrl3 = (await this._readReg(_REG_CTRL_REG3, 1))[0];
362
- await this._writeReg(_REG_CTRL_REG3, enable ? (ctrl3 | 0x08) : (ctrl3 & ~0x08));
346
+ await this._conn.writeReg(_REG_CTRL_REG3, enable ? (ctrl3 | 0x08) : (ctrl3 & ~0x08));
363
347
  }
364
348
  }
365
349
 
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const { toSigned } = require('../../connection/register');
4
+
3
5
  const _REG_WHO_AM_I = 0x0F;
4
6
  const _REG_CTRL_REG1 = 0x20;
5
7
  const _REG_CTRL_REG2 = 0x21;
@@ -40,9 +42,7 @@ const _SENSITIVITY = {
40
42
  const _DPS_TO_RAD = Math.PI / 180.0;
41
43
 
42
44
  function _int16Le(data, offset) {
43
- let v = data[offset] | (data[offset + 1] << 8);
44
- if (v & 0x8000) v -= 0x10000;
45
- return v;
45
+ return toSigned(data[offset] | (data[offset + 1] << 8), 16);
46
46
  }
47
47
 
48
48
  class L3GD20HMinimal {
@@ -60,12 +60,10 @@ class L3GD20HMinimal {
60
60
  * - All axes enabled, normal power mode
61
61
  * - 250 ms startup delay for gyroscope stabilization
62
62
  *
63
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
64
- * @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
63
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C, SMBus, or SPI register connection (SPI: readBit=0xC0, no multi-byte bit).
65
64
  */
66
- constructor(connection, busType = 'i2c') {
65
+ constructor(connection) {
67
66
  this._conn = connection;
68
- this._busType = busType;
69
67
  this._fullScale = 250;
70
68
  this._init();
71
69
  }
@@ -74,28 +72,17 @@ class L3GD20HMinimal {
74
72
  try {
75
73
  const who = (await this._readReg(_REG_WHO_AM_I, 1))[0];
76
74
  if (who !== _WHO_AM_I_L3GD20 && who !== _WHO_AM_I_L3GD20H) return;
77
- await this._writeReg(_REG_CTRL_REG4, _CTRL_REG4_DEFAULT);
78
- await this._writeReg(_REG_CTRL_REG1, _CTRL_REG1_DEFAULT);
75
+ await this._conn.writeReg(_REG_CTRL_REG4, _CTRL_REG4_DEFAULT);
76
+ await this._conn.writeReg(_REG_CTRL_REG1, _CTRL_REG1_DEFAULT);
79
77
  // 250 ms startup delay
80
78
  await new Promise(resolve => setTimeout(resolve, 250));
81
79
  } catch (e) { /* bus may be idle */ }
82
80
  }
83
81
 
84
- async _writeReg(reg, value) {
85
- const addr = this._busType === 'spi' ? (reg & 0x3F) : reg;
86
- await this._conn.write(Buffer.from([addr, value & 0xFF]));
87
- }
88
-
89
82
  async _readReg(reg, n) {
90
- let addr;
91
- if (this._busType === 'spi') {
92
- addr = reg | 0xC0; // READ=1, MS=1 (auto-increment)
93
- } else if (n > 1) {
94
- addr = reg | 0x80; // MSB set = I²C multi-byte auto-increment
95
- } else {
96
- addr = reg;
97
- }
98
- return this._conn.writeRead(Buffer.from([addr & 0xFF]), n);
83
+ // I²C needs bit 7 of the sub-address set for multi-byte auto-increment;
84
+ // on SPI the connection's readBit=0xC0 already ORs it in (idempotent).
85
+ return this._conn.readReg(n > 1 ? reg | 0x80 : reg, n);
99
86
  }
100
87
 
101
88
  _sensitivity() {
@@ -126,11 +113,10 @@ class L3GD20HFull extends L3GD20HMinimal {
126
113
  * interrupt generation with threshold and duration, INT1 pin routing,
127
114
  * and access to temperature and status registers.
128
115
  *
129
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
130
- * @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
116
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C, SMBus, or SPI register connection (SPI: readBit=0xC0, no multi-byte bit).
131
117
  */
132
- constructor(connection, busType = 'i2c') {
133
- super(connection, busType);
118
+ constructor(connection) {
119
+ super(connection);
134
120
  this._odr = 0;
135
121
  this._bw = 0;
136
122
  this._thresholdRaw = 0;
@@ -180,8 +166,8 @@ class L3GD20HFull extends L3GD20HMinimal {
180
166
  const fsMap = [250, 500, 2000];
181
167
  this._fullScale = fsMap[fullScale];
182
168
  const ctrl1 = _CTRL_REG1_DEFAULT | ((this._odr & 0x3) << 6) | ((this._bw & 0x3) << 4);
183
- await this._writeReg(_REG_CTRL_REG1, ctrl1);
184
- await this._writeReg(_REG_CTRL_REG4, _CTRL_REG4_DEFAULT | ((fullScale & 0x3) << 4));
169
+ await this._conn.writeReg(_REG_CTRL_REG1, ctrl1);
170
+ await this._conn.writeReg(_REG_CTRL_REG4, _CTRL_REG4_DEFAULT | ((fullScale & 0x3) << 4));
185
171
  }
186
172
 
187
173
  /**
@@ -201,9 +187,7 @@ class L3GD20HFull extends L3GD20HMinimal {
201
187
  * @returns {Promise<number>} Signed 8-bit temperature count.
202
188
  */
203
189
  async temperature() {
204
- let raw = (await this._readReg(_REG_OUT_TEMP, 1))[0];
205
- if (raw & 0x80) raw -= 0x100;
206
- return raw;
190
+ return toSigned((await this._readReg(_REG_OUT_TEMP, 1))[0], 8);
207
191
  }
208
192
 
209
193
  /**
@@ -223,7 +207,7 @@ class L3GD20HFull extends L3GD20HMinimal {
223
207
  async configureHpFilter(mode, cutoff) {
224
208
  if (mode < 0 || mode > 3) throw new Error('mode must be 0..3');
225
209
  if (cutoff < 0 || cutoff > 15) throw new Error('cutoff must be 0..15');
226
- await this._writeReg(_REG_CTRL_REG2, ((mode & 0x3) << 4) | (cutoff & 0x0F));
210
+ await this._conn.writeReg(_REG_CTRL_REG2, ((mode & 0x3) << 4) | (cutoff & 0x0F));
227
211
  }
228
212
 
229
213
  /**
@@ -232,7 +216,7 @@ class L3GD20HFull extends L3GD20HMinimal {
232
216
  */
233
217
  async enableHpFilter(enable) {
234
218
  const ctrl5 = (await this._readReg(_REG_CTRL_REG5, 1))[0];
235
- await this._writeReg(_REG_CTRL_REG5, enable ? (ctrl5 | 0x10) : (ctrl5 & ~0x10));
219
+ await this._conn.writeReg(_REG_CTRL_REG5, enable ? (ctrl5 | 0x10) : (ctrl5 & ~0x10));
236
220
  }
237
221
 
238
222
  /**
@@ -245,8 +229,8 @@ class L3GD20HFull extends L3GD20HMinimal {
245
229
  if (!validModes.includes(mode)) throw new Error('mode must be 0, 1, 2, 3, or 7');
246
230
  if (watermark < 0 || watermark > 31) throw new Error('watermark must be 0..31');
247
231
  const ctrl5 = (await this._readReg(_REG_CTRL_REG5, 1))[0] | 0x40;
248
- await this._writeReg(_REG_CTRL_REG5, ctrl5);
249
- await this._writeReg(_REG_FIFO_CTRL, ((mode & 0x7) << 5) | (watermark & 0x1F));
232
+ await this._conn.writeReg(_REG_CTRL_REG5, ctrl5);
233
+ await this._conn.writeReg(_REG_FIFO_CTRL, ((mode & 0x7) << 5) | (watermark & 0x1F));
250
234
  }
251
235
 
252
236
  /**
@@ -256,10 +240,10 @@ class L3GD20HFull extends L3GD20HMinimal {
256
240
  async enableFifo(enable) {
257
241
  const ctrl5 = (await this._readReg(_REG_CTRL_REG5, 1))[0];
258
242
  if (enable) {
259
- await this._writeReg(_REG_CTRL_REG5, ctrl5 | 0x40);
243
+ await this._conn.writeReg(_REG_CTRL_REG5, ctrl5 | 0x40);
260
244
  } else {
261
- await this._writeReg(_REG_CTRL_REG5, ctrl5 & ~0x40);
262
- await this._writeReg(_REG_FIFO_CTRL, 0x00);
245
+ await this._conn.writeReg(_REG_CTRL_REG5, ctrl5 & ~0x40);
246
+ await this._conn.writeReg(_REG_FIFO_CTRL, 0x00);
263
247
  }
264
248
  }
265
249
 
@@ -307,7 +291,7 @@ class L3GD20HFull extends L3GD20HMinimal {
307
291
  } else {
308
292
  throw new Error("mode must be 'normal', 'sleep', or 'power_down'");
309
293
  }
310
- await this._writeReg(_REG_CTRL_REG1, ctrl1);
294
+ await this._conn.writeReg(_REG_CTRL_REG1, ctrl1);
311
295
  }
312
296
  }
313
297
 
@@ -51,7 +51,7 @@ const _GYRO_SENSITIVITY = [131.0, 65.5, 32.8, 16.4];
51
51
  */
52
52
  class MPU6050Minimal {
53
53
  /**
54
- * @param {import('../../connection/connection').Connection} connection - Configured I²C connection (writeRead, write).
54
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
55
55
  */
56
56
  constructor(connection) {
57
57
  this._conn = connection;
@@ -61,36 +61,24 @@ class MPU6050Minimal {
61
61
  }
62
62
 
63
63
  async _init() {
64
- await this._writeReg(_REG_PWR_MGMT_1, 0x80);
64
+ await this._conn.writeReg(_REG_PWR_MGMT_1, 0x80);
65
65
  const end1 = Date.now() + 100;
66
66
  while (Date.now() < end1) {}
67
- await this._writeReg(_REG_PWR_MGMT_1, 0x01);
68
- const who = await this._readReg(_REG_WHO_AM_I);
67
+ await this._conn.writeReg(_REG_PWR_MGMT_1, 0x01);
68
+ const who = (await this._conn.readReg(_REG_WHO_AM_I, 1))[0];
69
69
  if (who !== _WHO_AM_I_VALUE) {
70
70
  throw new Error('MPU6050 WHO_AM_I: expected 0x' + _WHO_AM_I_VALUE.toString(16) + ', got 0x' + who.toString(16));
71
71
  }
72
- await this._writeReg(_REG_GYRO_CONFIG, 0x00);
73
- await this._writeReg(_REG_ACCEL_CONFIG, 0x00);
74
- await this._writeReg(_REG_CONFIG, 0x03);
75
- await this._writeReg(_REG_SMPLRT_DIV, 0x04);
72
+ await this._conn.writeReg(_REG_GYRO_CONFIG, 0x00);
73
+ await this._conn.writeReg(_REG_ACCEL_CONFIG, 0x00);
74
+ await this._conn.writeReg(_REG_CONFIG, 0x03);
75
+ await this._conn.writeReg(_REG_SMPLRT_DIV, 0x04);
76
76
  const end2 = Date.now() + 35;
77
77
  while (Date.now() < end2) {}
78
78
  }
79
79
 
80
- async _writeReg(reg, value) {
81
- await this._conn.write(Buffer.from([reg, value]));
82
- }
83
-
84
- async _readReg(reg) {
85
- return (await this._conn.writeRead(Buffer.from([reg]), 1))[0];
86
- }
87
-
88
80
  async _readReg16Signed(reg) {
89
- return (await this._conn.writeRead(Buffer.from([reg]), 2)).readInt16BE(0);
90
- }
91
-
92
- async _readBurst(reg, len) {
93
- return this._conn.writeRead(Buffer.from([reg]), len);
81
+ return (await this._conn.readReg(reg, 2)).readInt16BE(0);
94
82
  }
95
83
 
96
84
  /**
@@ -98,7 +86,7 @@ class MPU6050Minimal {
98
86
  * @returns {Promise<number[]>} [x, y, z] acceleration in m/s².
99
87
  */
100
88
  async accel() {
101
- const buf = await this._readBurst(_REG_ACCEL_XOUT_H, 6);
89
+ const buf = await this._conn.readReg(_REG_ACCEL_XOUT_H, 6);
102
90
  const ax = buf.readInt16BE(0);
103
91
  const ay = buf.readInt16BE(2);
104
92
  const az = buf.readInt16BE(4);
@@ -111,7 +99,7 @@ class MPU6050Minimal {
111
99
  * @returns {Promise<number[]>} [x, y, z] angular rate in rad/s.
112
100
  */
113
101
  async gyro() {
114
- const buf = await this._readBurst(_REG_GYRO_XOUT_H, 6);
102
+ const buf = await this._conn.readReg(_REG_GYRO_XOUT_H, 6);
115
103
  const gx = buf.readInt16BE(0);
116
104
  const gy = buf.readInt16BE(2);
117
105
  const gz = buf.readInt16BE(4);
@@ -131,7 +119,7 @@ class MPU6050Minimal {
131
119
  */
132
120
  class MPU6050Full extends MPU6050Minimal {
133
121
  /**
134
- * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
122
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
135
123
  */
136
124
  constructor(connection) {
137
125
  super(connection);
@@ -144,7 +132,7 @@ class MPU6050Full extends MPU6050Minimal {
144
132
  */
145
133
  async configureGyro(fullScale = 0) {
146
134
  this._gyroFs = fullScale & 0x03;
147
- await this._writeReg(_REG_GYRO_CONFIG, (fullScale & 0x03) << 3);
135
+ await this._conn.writeReg(_REG_GYRO_CONFIG, (fullScale & 0x03) << 3);
148
136
  }
149
137
 
150
138
  /**
@@ -154,7 +142,7 @@ class MPU6050Full extends MPU6050Minimal {
154
142
  */
155
143
  async configureAccel(fullScale = 0) {
156
144
  this._accelFs = fullScale & 0x03;
157
- await this._writeReg(_REG_ACCEL_CONFIG, (fullScale & 0x03) << 3);
145
+ await this._conn.writeReg(_REG_ACCEL_CONFIG, (fullScale & 0x03) << 3);
158
146
  }
159
147
 
160
148
  /**
@@ -164,7 +152,7 @@ class MPU6050Full extends MPU6050Minimal {
164
152
  * @returns {Promise<void>}
165
153
  */
166
154
  async configureDlpf(dlpf = 3) {
167
- await this._writeReg(_REG_CONFIG, dlpf & 0x07);
155
+ await this._conn.writeReg(_REG_CONFIG, dlpf & 0x07);
168
156
  }
169
157
 
170
158
  /**
@@ -174,7 +162,7 @@ class MPU6050Full extends MPU6050Minimal {
174
162
  * @returns {Promise<void>}
175
163
  */
176
164
  async configureSampleRate(divider = 4) {
177
- await this._writeReg(_REG_SMPLRT_DIV, divider & 0xFF);
165
+ await this._conn.writeReg(_REG_SMPLRT_DIV, divider & 0xFF);
178
166
  }
179
167
 
180
168
  /**
@@ -191,7 +179,7 @@ class MPU6050Full extends MPU6050Minimal {
191
179
  * @returns {Promise<number[]>} [x, y, z] raw 16-bit signed values.
192
180
  */
193
181
  async accelRaw() {
194
- const buf = await this._readBurst(_REG_ACCEL_XOUT_H, 6);
182
+ const buf = await this._conn.readReg(_REG_ACCEL_XOUT_H, 6);
195
183
  return [buf.readInt16BE(0), buf.readInt16BE(2), buf.readInt16BE(4)];
196
184
  }
197
185
 
@@ -200,7 +188,7 @@ class MPU6050Full extends MPU6050Minimal {
200
188
  * @returns {Promise<number[]>} [x, y, z] raw 16-bit signed values.
201
189
  */
202
190
  async gyroRaw() {
203
- const buf = await this._readBurst(_REG_GYRO_XOUT_H, 6);
191
+ const buf = await this._conn.readReg(_REG_GYRO_XOUT_H, 6);
204
192
  return [buf.readInt16BE(0), buf.readInt16BE(2), buf.readInt16BE(4)];
205
193
  }
206
194
 
@@ -209,7 +197,7 @@ class MPU6050Full extends MPU6050Minimal {
209
197
  * @returns {Promise<boolean>} True when DATA_RDY_INT is set in INT_STATUS.
210
198
  */
211
199
  async dataReady() {
212
- return !!((await this._readReg(_REG_INT_STATUS)) & 0x01);
200
+ return !!((await this._conn.readReg(_REG_INT_STATUS, 1))[0] & 0x01);
213
201
  }
214
202
 
215
203
  /**
@@ -218,13 +206,13 @@ class MPU6050Full extends MPU6050Minimal {
218
206
  * @returns {Promise<void>}
219
207
  */
220
208
  async setSleep(sleep = true) {
221
- let val = await this._readReg(_REG_PWR_MGMT_1);
209
+ let val = (await this._conn.readReg(_REG_PWR_MGMT_1, 1))[0];
222
210
  if (sleep) {
223
211
  val |= 0x40;
224
212
  } else {
225
213
  val &= ~0x40;
226
214
  }
227
- await this._writeReg(_REG_PWR_MGMT_1, val);
215
+ await this._conn.writeReg(_REG_PWR_MGMT_1, val);
228
216
  }
229
217
 
230
218
  /**
@@ -240,7 +228,7 @@ class MPU6050Full extends MPU6050Minimal {
240
228
  async setStandby(xa = false, ya = false, za = false, xg = false, yg = false, zg = false) {
241
229
  const val = ((xa ? 1 : 0) << 5) | ((ya ? 1 : 0) << 4) | ((za ? 1 : 0) << 3) |
242
230
  ((xg ? 1 : 0) << 2) | ((yg ? 1 : 0) << 1) | (zg ? 1 : 0);
243
- await this._writeReg(_REG_PWR_MGMT_2, val);
231
+ await this._conn.writeReg(_REG_PWR_MGMT_2, val);
244
232
  }
245
233
 
246
234
  /**
@@ -248,7 +236,7 @@ class MPU6050Full extends MPU6050Minimal {
248
236
  * @returns {Promise<number>} FIFO byte count (0–1024).
249
237
  */
250
238
  async fifoCount() {
251
- const buf = await this._readBurst(_REG_FIFO_COUNTH, 2);
239
+ const buf = await this._conn.readReg(_REG_FIFO_COUNTH, 2);
252
240
  return ((buf[0] & 0x1F) << 8) | buf[1];
253
241
  }
254
242
 
@@ -259,7 +247,7 @@ class MPU6050Full extends MPU6050Minimal {
259
247
  async readFifo() {
260
248
  const count = await this.fifoCount();
261
249
  if (count === 0) return Buffer.alloc(0);
262
- return this._readBurst(_REG_FIFO_R_W, count);
250
+ return this._conn.readReg(_REG_FIFO_R_W, count);
263
251
  }
264
252
 
265
253
  /**
@@ -271,9 +259,9 @@ class MPU6050Full extends MPU6050Minimal {
271
259
  */
272
260
  async enableFifo(gyro = true, accel = true, temp = false) {
273
261
  const fifoEn = ((accel ? 1 : 0) << 3) | ((temp ? 1 : 0) << 2) | ((gyro ? 1 : 0) << 4);
274
- await this._writeReg(_REG_FIFO_EN, fifoEn);
275
- const userCtrl = await this._readReg(_REG_USER_CTRL);
276
- await this._writeReg(_REG_USER_CTRL, userCtrl | 0x40);
262
+ await this._conn.writeReg(_REG_FIFO_EN, fifoEn);
263
+ const userCtrl = (await this._conn.readReg(_REG_USER_CTRL, 1))[0];
264
+ await this._conn.writeReg(_REG_USER_CTRL, userCtrl | 0x40);
277
265
  }
278
266
 
279
267
  /**
@@ -281,8 +269,8 @@ class MPU6050Full extends MPU6050Minimal {
281
269
  * @returns {Promise<void>}
282
270
  */
283
271
  async resetFifo() {
284
- const userCtrl = await this._readReg(_REG_USER_CTRL);
285
- await this._writeReg(_REG_USER_CTRL, userCtrl | 0x04);
272
+ const userCtrl = (await this._conn.readReg(_REG_USER_CTRL, 1))[0];
273
+ await this._conn.writeReg(_REG_USER_CTRL, userCtrl | 0x04);
286
274
  }
287
275
  }
288
276