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,13 +61,11 @@ function _u24(data, offset) {
61
61
  * `await sensor.temperature()` (or any other async method) once before
62
62
  * relying on calibrated readings if this matters.
63
63
  *
64
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
65
- * @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
64
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C, SMBus, or SPI register connection (SPI: default Bosch convention, readBit 0x80, no multi-byte bit).
66
65
  */
67
66
  class BMP581Minimal {
68
- constructor(connection, busType = 'i2c') {
67
+ constructor(connection) {
69
68
  this._conn = connection;
70
- this._busType = busType;
71
69
  this._odr = 0x1C;
72
70
  this._pwrMode = 0x01;
73
71
  this._osrP = 0;
@@ -77,51 +75,43 @@ class BMP581Minimal {
77
75
  }
78
76
 
79
77
  async _init() {
78
+ await this._dummyRead();
80
79
  try {
81
- const cid = (await this._readReg(_REG_CHIP_ID, 1))[0];
80
+ const cid = (await this._conn.readReg(_REG_CHIP_ID, 1))[0];
82
81
  if (cid !== _CHIP_ID_EXPECTED) return;
83
82
  for (let i = 0; i < 50; i++) {
84
- const st = (await this._readReg(_REG_STATUS, 1))[0];
83
+ const st = (await this._conn.readReg(_REG_STATUS, 1))[0];
85
84
  if ((st & _STATUS_NVM_RDY) && !(st & _STATUS_NVM_ERR)) break;
86
85
  _delay(2);
87
86
  }
88
- await this._readReg(_REG_INT_STATUS, 1);
87
+ await this._conn.readReg(_REG_INT_STATUS, 1);
89
88
  try {
90
- await this._writeReg(_REG_CMD, _SOFT_RESET_CMD);
89
+ await this._conn.writeReg(_REG_CMD, _SOFT_RESET_CMD);
91
90
  } catch (e) { /* expected NACK */ }
92
91
  _delay(2);
93
92
  for (let i = 0; i < 50; i++) {
94
- const st = (await this._readReg(_REG_STATUS, 1))[0];
93
+ const st = (await this._conn.readReg(_REG_STATUS, 1))[0];
95
94
  if ((st & _STATUS_NVM_RDY) && !(st & _STATUS_NVM_ERR)) break;
96
95
  _delay(2);
97
96
  }
98
- await this._readReg(_REG_INT_STATUS, 1);
99
- await this._writeReg(_REG_OSR_CONFIG, 0x40);
100
- await this._writeReg(_REG_ODR_CONFIG, 0x71);
97
+ await this._conn.readReg(_REG_INT_STATUS, 1);
98
+ await this._conn.writeReg(_REG_OSR_CONFIG, 0x40);
99
+ await this._conn.writeReg(_REG_ODR_CONFIG, 0x71);
101
100
  } catch (e) { /* bus may be idle */ }
102
101
  }
103
102
 
104
- async _writeReg(reg, value) {
105
- const addr = this._busType === 'spi' ? (reg & 0x7F) : reg;
106
- await this._conn.write(Buffer.from([addr, value & 0xFF]));
107
- }
108
-
109
- async _readReg(reg, n) {
110
- return this._conn.writeRead(Buffer.from([reg]), n);
111
- }
112
-
113
- async _spiDummyRead() {
114
- if (this._busType !== 'spi') return;
103
+ // The first read after CSB falls on SPI returns invalid data (datasheet);
104
+ // one throwaway CHIP_ID read is harmless on I²C, so it is unconditional.
105
+ async _dummyRead() {
115
106
  try {
116
- await this._conn.write(Buffer.from([_REG_CHIP_ID | 0x80]));
117
- await this._conn.read(1);
107
+ await this._conn.readReg(_REG_CHIP_ID, 1);
118
108
  } catch (e) { /* ignore */ }
119
109
  }
120
110
 
121
111
  async _waitForced() {
122
112
  if (this._pwrMode !== 2) return;
123
113
  for (let i = 0; i < 200; i++) {
124
- const st = (await this._readReg(_REG_INT_STATUS, 1))[0];
114
+ const st = (await this._conn.readReg(_REG_INT_STATUS, 1))[0];
125
115
  if (st & _INT_STATUS_DRDY) return;
126
116
  _delay(5);
127
117
  }
@@ -133,7 +123,7 @@ class BMP581Minimal {
133
123
  */
134
124
  async pressure() {
135
125
  await this._waitForced();
136
- const buf = await this._readReg(_REG_PRESS_XLSB, 3);
126
+ const buf = await this._conn.readReg(_REG_PRESS_XLSB, 3);
137
127
  return _u24(buf, 0) / 64.0;
138
128
  }
139
129
 
@@ -143,7 +133,7 @@ class BMP581Minimal {
143
133
  */
144
134
  async temperature() {
145
135
  await this._waitForced();
146
- const buf = await this._readReg(_REG_TEMP_XLSB, 3);
136
+ const buf = await this._conn.readReg(_REG_TEMP_XLSB, 3);
147
137
  return _u24(buf, 0) / 65536.0;
148
138
  }
149
139
 
@@ -153,7 +143,7 @@ class BMP581Minimal {
153
143
  */
154
144
  async both() {
155
145
  await this._waitForced();
156
- const buf = await this._readReg(_REG_TEMP_XLSB, 6);
146
+ const buf = await this._conn.readReg(_REG_TEMP_XLSB, 6);
157
147
  return {
158
148
  pressure: _u24(buf, 3) / 64.0,
159
149
  temperature: _u24(buf, 0) / 65536.0,
@@ -165,8 +155,7 @@ class BMP581Minimal {
165
155
  * BMP581 full interface — extends BMP581Minimal with configuration, FIFO,
166
156
  * interrupts, OOR detection, and NVM access.
167
157
  *
168
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
169
- * @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
158
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C, SMBus, or SPI register connection (SPI: default Bosch convention, readBit 0x80, no multi-byte bit).
170
159
  */
171
160
  class BMP581Full extends BMP581Minimal {
172
161
  static OSR_1X = 0;
@@ -205,8 +194,8 @@ class BMP581Full extends BMP581Minimal {
205
194
  static INT_SOURCE_FIFO_THS = 0x04;
206
195
  static INT_SOURCE_OOR_P = 0x08;
207
196
 
208
- constructor(connection, busType = 'i2c') {
209
- super(connection, busType);
197
+ constructor(connection) {
198
+ super(connection);
210
199
  }
211
200
 
212
201
  /**
@@ -223,9 +212,9 @@ class BMP581Full extends BMP581Minimal {
223
212
  this._osrT = osrT;
224
213
  this._pressEn = pressEn;
225
214
  const osr = (pressEn ? 0x40 : 0) | ((osrP & 0x7) << 3) | (osrT & 0x7);
226
- await this._writeReg(_REG_OSR_CONFIG, osr);
215
+ await this._conn.writeReg(_REG_OSR_CONFIG, osr);
227
216
  const odrByte = ((odr & 0x1F) << 2) | (this._pwrMode & 0x3);
228
- await this._writeReg(_REG_ODR_CONFIG, odrByte);
217
+ await this._conn.writeReg(_REG_ODR_CONFIG, odrByte);
229
218
  }
230
219
 
231
220
  /**
@@ -236,7 +225,7 @@ class BMP581Full extends BMP581Minimal {
236
225
  async setMode(mode) {
237
226
  this._pwrMode = mode;
238
227
  const odrByte = ((this._odr & 0x1F) << 2) | (mode & 0x3);
239
- await this._writeReg(_REG_ODR_CONFIG, odrByte);
228
+ await this._conn.writeReg(_REG_ODR_CONFIG, odrByte);
240
229
  }
241
230
 
242
231
  /**
@@ -247,11 +236,11 @@ class BMP581Full extends BMP581Minimal {
247
236
  const prev = this._pwrMode;
248
237
  if (prev !== 2) await this.setMode(2);
249
238
  for (let i = 0; i < 400; i++) {
250
- const st = (await this._readReg(_REG_INT_STATUS, 1))[0];
239
+ const st = (await this._conn.readReg(_REG_INT_STATUS, 1))[0];
251
240
  if (st & _INT_STATUS_DRDY) break;
252
241
  _delay(5);
253
242
  }
254
- const buf = await this._readReg(_REG_TEMP_XLSB, 6);
243
+ const buf = await this._conn.readReg(_REG_TEMP_XLSB, 6);
255
244
  return {
256
245
  pressure: _u24(buf, 3) / 64.0,
257
246
  temperature: _u24(buf, 0) / 65536.0,
@@ -272,7 +261,7 @@ class BMP581Full extends BMP581Minimal {
272
261
  /** Issue a soft reset and re-initialise the chip. @returns {Promise<void>} */
273
262
  async softwareReset() {
274
263
  try {
275
- await this._writeReg(_REG_CMD, _SOFT_RESET_CMD);
264
+ await this._conn.writeReg(_REG_CMD, _SOFT_RESET_CMD);
276
265
  } catch (e) { /* expected NACK */ }
277
266
  _delay(2);
278
267
  await this._init();
@@ -283,7 +272,7 @@ class BMP581Full extends BMP581Minimal {
283
272
  * @returns {Promise<number>} 0x50 for a genuine BMP581.
284
273
  */
285
274
  async chipId() {
286
- const buf = await this._readReg(_REG_CHIP_ID, 1);
275
+ const buf = await this._conn.readReg(_REG_CHIP_ID, 1);
287
276
  return buf[0];
288
277
  }
289
278
 
@@ -292,7 +281,7 @@ class BMP581Full extends BMP581Minimal {
292
281
  * @returns {Promise<number>} ASIC revision identifier.
293
282
  */
294
283
  async revId() {
295
- const buf = await this._readReg(_REG_REV_ID, 1);
284
+ const buf = await this._conn.readReg(_REG_REV_ID, 1);
296
285
  return buf[0];
297
286
  }
298
287
 
@@ -301,7 +290,7 @@ class BMP581Full extends BMP581Minimal {
301
290
  * @returns {Promise<number>} Raw status byte.
302
291
  */
303
292
  async status() {
304
- const buf = await this._readReg(_REG_STATUS, 1);
293
+ const buf = await this._conn.readReg(_REG_STATUS, 1);
305
294
  return buf[0];
306
295
  }
307
296
 
@@ -310,7 +299,7 @@ class BMP581Full extends BMP581Minimal {
310
299
  * @returns {Promise<number>} Raw interrupt status byte.
311
300
  */
312
301
  async interruptStatus() {
313
- const buf = await this._readReg(_REG_INT_STATUS, 1);
302
+ const buf = await this._conn.readReg(_REG_INT_STATUS, 1);
314
303
  return buf[0];
315
304
  }
316
305
 
@@ -335,15 +324,15 @@ class BMP581Full extends BMP581Minimal {
335
324
  if (openDrain) val |= 0x04;
336
325
  if (polarity) val |= 0x02;
337
326
  if (mode) val |= 0x01;
338
- await this._writeReg(_REG_INT_CONFIG, val);
327
+ await this._conn.writeReg(_REG_INT_CONFIG, val);
339
328
  }
340
329
 
341
330
  async _setIntSource(source, enable) {
342
- const buf = await this._readReg(_REG_INT_SOURCE, 1);
331
+ const buf = await this._conn.readReg(_REG_INT_SOURCE, 1);
343
332
  let cur = buf[0];
344
333
  if (enable) cur |= source;
345
334
  else cur &= ~source;
346
- await this._writeReg(_REG_INT_SOURCE, cur);
335
+ await this._conn.writeReg(_REG_INT_SOURCE, cur);
347
336
  }
348
337
 
349
338
  /** Enable/disable the data-ready interrupt source. @returns {Promise<void>} */
@@ -353,11 +342,11 @@ class BMP581Full extends BMP581Minimal {
353
342
 
354
343
  /** Enable/disable FIFO threshold and FIFO-full interrupt sources. @returns {Promise<void>} */
355
344
  async enableFifoInterrupt(threshold, full) {
356
- const buf = await this._readReg(_REG_INT_SOURCE, 1);
345
+ const buf = await this._conn.readReg(_REG_INT_SOURCE, 1);
357
346
  let cur = buf[0] & ~(BMP581Full.INT_SOURCE_FIFO_FULL | BMP581Full.INT_SOURCE_FIFO_THS);
358
347
  if (threshold) cur |= BMP581Full.INT_SOURCE_FIFO_THS;
359
348
  if (full) cur |= BMP581Full.INT_SOURCE_FIFO_FULL;
360
- await this._writeReg(_REG_INT_SOURCE, cur);
349
+ await this._conn.writeReg(_REG_INT_SOURCE, cur);
361
350
  }
362
351
 
363
352
  /** Enable/disable the pressure out-of-range interrupt source. @returns {Promise<void>} */
@@ -373,11 +362,11 @@ class BMP581Full extends BMP581Minimal {
373
362
  * @returns {Promise<void>}
374
363
  */
375
364
  async setIirFilter(coeffP, coeffT) {
376
- const buf = await this._readReg(_REG_DSP_CONFIG, 1);
365
+ const buf = await this._conn.readReg(_REG_DSP_CONFIG, 1);
377
366
  let dsp = buf[0] | 0x28;
378
- await this._writeReg(_REG_DSP_CONFIG, dsp);
367
+ await this._conn.writeReg(_REG_DSP_CONFIG, dsp);
379
368
  const iirVal = ((coeffP & 0x7) << 3) | (coeffT & 0x7);
380
- await this._writeReg(_REG_DSP_IIR, iirVal);
369
+ await this._conn.writeReg(_REG_DSP_IIR, iirVal);
381
370
  }
382
371
 
383
372
  /**
@@ -390,8 +379,8 @@ class BMP581Full extends BMP581Minimal {
390
379
  async configureFifo(frameSel, mode, threshold) {
391
380
  const prev = this._pwrMode;
392
381
  if (prev !== 0) await this.setMode(0);
393
- await this._writeReg(_REG_FIFO_SEL, frameSel & 0x3);
394
- await this._writeReg(_REG_FIFO_CONFIG, ((mode & 0x1) << 5) | (threshold & 0x1F));
382
+ await this._conn.writeReg(_REG_FIFO_SEL, frameSel & 0x3);
383
+ await this._conn.writeReg(_REG_FIFO_CONFIG, ((mode & 0x1) << 5) | (threshold & 0x1F));
395
384
  if (prev !== 0) await this.setMode(prev);
396
385
  }
397
386
 
@@ -400,7 +389,7 @@ class BMP581Full extends BMP581Minimal {
400
389
  * @returns {Promise<number>} Frame count 0-32.
401
390
  */
402
391
  async fifoCount() {
403
- const buf = await this._readReg(_REG_FIFO_COUNT, 1);
392
+ const buf = await this._conn.readReg(_REG_FIFO_COUNT, 1);
404
393
  return buf[0] & 0x3F;
405
394
  }
406
395
 
@@ -409,7 +398,7 @@ class BMP581Full extends BMP581Minimal {
409
398
  * @returns {Promise<{osrP: number, osrT: number}>}
410
399
  */
411
400
  async effectiveOsr() {
412
- const buf = await this._readReg(_REG_OSR_EFF, 1);
401
+ const buf = await this._conn.readReg(_REG_OSR_EFF, 1);
413
402
  return {
414
403
  osrP: (buf[0] >> 3) & 0x7,
415
404
  osrT: buf[0] & 0x7,
@@ -421,7 +410,7 @@ class BMP581Full extends BMP581Minimal {
421
410
  * @returns {Promise<boolean>}
422
411
  */
423
412
  async odrIsValid() {
424
- const buf = await this._readReg(_REG_OSR_EFF, 1);
413
+ const buf = await this._conn.readReg(_REG_OSR_EFF, 1);
425
414
  return (buf[0] & 0x80) !== 0;
426
415
  }
427
416
 
@@ -434,12 +423,12 @@ class BMP581Full extends BMP581Minimal {
434
423
  */
435
424
  async setOorThreshold(thresholdPa, rangePa, countLimit) {
436
425
  let thr17 = Math.floor(thresholdPa * 64.0) >> 7;
437
- await this._writeReg(_REG_OOR_THR_P_LSB, thr17 & 0xFF);
438
- await this._writeReg(_REG_OOR_THR_P_MSB, (thr17 >> 8) & 0xFF);
426
+ await this._conn.writeReg(_REG_OOR_THR_P_LSB, thr17 & 0xFF);
427
+ await this._conn.writeReg(_REG_OOR_THR_P_MSB, (thr17 >> 8) & 0xFF);
439
428
  const range8 = (Math.floor(rangePa * 64.0) >> 7) & 0xFF;
440
- await this._writeReg(_REG_OOR_RANGE, range8);
429
+ await this._conn.writeReg(_REG_OOR_RANGE, range8);
441
430
  const oorCfg = ((countLimit & 0x3) << 6) | ((thr17 >> 16) & 0x01);
442
- await this._writeReg(_REG_OOR_CONFIG, oorCfg);
431
+ await this._conn.writeReg(_REG_OOR_CONFIG, oorCfg);
443
432
  }
444
433
 
445
434
  /**
@@ -451,13 +440,13 @@ class BMP581Full extends BMP581Minimal {
451
440
  const prev = this._pwrMode;
452
441
  if (prev !== 0) await this.setMode(0);
453
442
  try {
454
- await this._writeReg(_REG_NVM_ADDR, 0x5D);
455
- await this._writeReg(_REG_CMD, 0xA5);
443
+ await this._conn.writeReg(_REG_NVM_ADDR, 0x5D);
444
+ await this._conn.writeReg(_REG_CMD, 0xA5);
456
445
  _delay(2);
457
- await this._writeReg(_REG_NVM_ADDR, 0x40 | (row & 0x3F));
458
- await this._writeReg(_REG_CMD, 0xA5);
446
+ await this._conn.writeReg(_REG_NVM_ADDR, 0x40 | (row & 0x3F));
447
+ await this._conn.writeReg(_REG_CMD, 0xA5);
459
448
  _delay(2);
460
- const buf = await this._readReg(_REG_NVM_DATA_LSB, 2);
449
+ const buf = await this._conn.readReg(_REG_NVM_DATA_LSB, 2);
461
450
  return (buf[1] << 8) | buf[0];
462
451
  } finally {
463
452
  if (prev !== 0) await this.setMode(prev);
@@ -474,11 +463,11 @@ class BMP581Full extends BMP581Minimal {
474
463
  const prev = this._pwrMode;
475
464
  if (prev !== 0) await this.setMode(0);
476
465
  try {
477
- await this._writeReg(_REG_NVM_ADDR, 0x40 | (row & 0x3F));
478
- await this._writeReg(_REG_NVM_DATA_LSB, value & 0xFF);
479
- await this._writeReg(_REG_NVM_DATA_MSB, (value >> 8) & 0xFF);
480
- await this._writeReg(_REG_NVM_ADDR, 0x5D);
481
- await this._writeReg(_REG_CMD, 0xA0);
466
+ await this._conn.writeReg(_REG_NVM_ADDR, 0x40 | (row & 0x3F));
467
+ await this._conn.writeReg(_REG_NVM_DATA_LSB, value & 0xFF);
468
+ await this._conn.writeReg(_REG_NVM_DATA_MSB, (value >> 8) & 0xFF);
469
+ await this._conn.writeReg(_REG_NVM_ADDR, 0x5D);
470
+ await this._conn.writeReg(_REG_CMD, 0xA0);
482
471
  _delay(5);
483
472
  } finally {
484
473
  if (prev !== 0) await this.setMode(prev);
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const { toSigned } = require('../../connection/register');
4
+
3
5
  const _REG_INTERRUPT_CFG = 0x0B;
4
6
  const _REG_THS_P_L = 0x0C;
5
7
  const _REG_THS_P_H = 0x0D;
@@ -38,45 +40,41 @@ function _delay(ms) {
38
40
  * Default: ODR=10 Hz, AVG=4 samples, BDU enabled, low-pass filter off,
39
41
  * FIFO bypass mode.
40
42
  *
41
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
42
- * @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
43
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C, SMBus, or SPI register connection (SPI: readBit 0x80, no multi-byte bit).
43
44
  */
44
45
  class LPS22DFMinimal {
45
- constructor(connection, busType = 'i2c') {
46
+ constructor(connection) {
46
47
  this._conn = connection;
47
- this._busType = busType;
48
48
  this._init();
49
49
  }
50
50
 
51
51
  async _init() {
52
- const who = await this._readReg(_REG_WHO_AM_I, 1);
52
+ const who = await this._conn.readReg(_REG_WHO_AM_I, 1);
53
53
  if (who[0] !== _CHIP_ID) {
54
54
  throw new Error(`LPS22DF not found: WHO_AM_I expected 0x${_CHIP_ID.toString(16)}, got 0x${who[0].toString(16)}`);
55
55
  }
56
- await this._writeReg(_REG_CTRL_REG2, 0x04); // SWRESET=1
56
+ await this._conn.writeReg(_REG_CTRL_REG2, 0x04); // SWRESET=1
57
57
  _delay(1);
58
- await this._writeReg(_REG_CTRL_REG1, (3 << 3) | 0); // ODR=10 Hz, AVG=4
59
- await this._writeReg(_REG_CTRL_REG2, 0x08); // BDU=1
60
- }
61
-
62
- async _writeReg(reg, value) {
63
- const addr = this._busType === 'spi' ? (reg & 0x7F) : reg;
64
- await this._conn.write(Buffer.from([addr, value & 0xFF]));
65
- }
66
-
67
- async _readReg(reg, n) {
68
- const addr = this._busType === 'spi' ? (reg | 0x80) : reg;
69
- return this._conn.writeRead(Buffer.from([addr]), n);
58
+ await this._conn.writeReg(_REG_CTRL_REG1, (3 << 3) | 0); // ODR=10 Hz, AVG=4
59
+ await this._conn.writeReg(_REG_CTRL_REG2, 0x08); // BDU=1
70
60
  }
71
61
 
72
62
  async _waitPDa() {
73
63
  while (true) {
74
- const status = await this._readReg(_REG_STATUS, 1);
64
+ const status = await this._conn.readReg(_REG_STATUS, 1);
75
65
  if (status[0] & 0x01) return;
76
66
  _delay(1);
77
67
  }
78
68
  }
79
69
 
70
+ async _waitTDa() {
71
+ while (true) {
72
+ const status = await this._conn.readReg(_REG_STATUS, 1);
73
+ if (status[0] & 0x02) return;
74
+ _delay(1);
75
+ }
76
+ }
77
+
80
78
  /**
81
79
  * Read absolute pressure.
82
80
  *
@@ -87,24 +85,25 @@ class LPS22DFMinimal {
87
85
  */
88
86
  async pressure() {
89
87
  await this._waitPDa();
90
- const raw = await this._readReg(_REG_PRESS_OUT_XL, 3);
88
+ const raw = await this._conn.readReg(_REG_PRESS_OUT_XL, 3);
91
89
  let value = raw[0] | (raw[1] << 8) | (raw[2] << 16);
92
- if (value & 0x800000) value -= 0x1000000;
90
+ value = toSigned(value, 24);
93
91
  return (value / 4096.0) * 100.0;
94
92
  }
95
93
 
96
94
  /**
97
95
  * Read temperature.
98
96
  *
99
- * Reads TEMP_OUT_L..H. Sign-extends the 16-bit two's complement value
100
- * and converts to °C (100 LSB/°C).
97
+ * Polls STATUS.T_DA then reads TEMP_OUT_L..H. Sign-extends the 16-bit
98
+ * two's complement value and converts to °C (100 LSB/°C).
101
99
  *
102
100
  * @returns {Promise<number>} Temperature in degrees Celsius.
103
101
  */
104
102
  async temperature() {
105
- const raw = await this._readReg(_REG_TEMP_OUT_L, 2);
103
+ await this._waitTDa();
104
+ const raw = await this._conn.readReg(_REG_TEMP_OUT_L, 2);
106
105
  let value = raw[0] | (raw[1] << 8);
107
- if (value & 0x8000) value -= 0x10000;
106
+ value = toSigned(value, 16);
108
107
  return value / 100.0;
109
108
  }
110
109
  }
@@ -113,8 +112,7 @@ class LPS22DFMinimal {
113
112
  * LPS22DF full interface — extends LPS22DFMinimal with configuration,
114
113
  * threshold/offset calibration, FIFO, interrupts, and AUTOZERO/AUTOREFP.
115
114
  *
116
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
117
- * @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
115
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C, SMBus, or SPI register connection (SPI: readBit 0x80, no multi-byte bit).
118
116
  */
119
117
  class LPS22DFFull extends LPS22DFMinimal {
120
118
  static ODR_POWER_DOWN = 0;
@@ -142,8 +140,8 @@ class LPS22DFFull extends LPS22DFMinimal {
142
140
  static FIFO_BYPASS_TO_CONT = 4;
143
141
  static FIFO_CONT_TO_FIFO = 5;
144
142
 
145
- constructor(connection, busType = 'i2c') {
146
- super(connection, busType);
143
+ constructor(connection) {
144
+ super(connection);
147
145
  }
148
146
 
149
147
  /**
@@ -161,14 +159,14 @@ class LPS22DFFull extends LPS22DFMinimal {
161
159
  if (enLpfp) ctrl2 |= 0x10;
162
160
  if (lfpfCfg) ctrl2 |= 0x20;
163
161
  if (bdu) ctrl2 |= 0x08;
164
- await this._writeReg(_REG_CTRL_REG1, ctrl1);
165
- await this._writeReg(_REG_CTRL_REG2, ctrl2);
162
+ await this._conn.writeReg(_REG_CTRL_REG1, ctrl1);
163
+ await this._conn.writeReg(_REG_CTRL_REG2, ctrl2);
166
164
  }
167
165
 
168
166
  /** @returns {Promise<void>} */
169
167
  async oneshot() {
170
- await this._writeReg(_REG_CTRL_REG1, 0x00);
171
- await this._writeReg(_REG_CTRL_REG2, 0x08 | 0x01);
168
+ await this._conn.writeReg(_REG_CTRL_REG1, 0x00);
169
+ await this._conn.writeReg(_REG_CTRL_REG2, 0x08 | 0x01);
172
170
  await this._waitPDa();
173
171
  }
174
172
 
@@ -184,7 +182,7 @@ class LPS22DFFull extends LPS22DFMinimal {
184
182
 
185
183
  /** @returns {Promise<void>} */
186
184
  async softwareReset() {
187
- await this._writeReg(_REG_CTRL_REG2, 0x04);
185
+ await this._conn.writeReg(_REG_CTRL_REG2, 0x04);
188
186
  _delay(1);
189
187
  }
190
188
 
@@ -197,8 +195,8 @@ class LPS22DFFull extends LPS22DFMinimal {
197
195
  const offsetHpa = offsetPa / 100.0;
198
196
  let raw = Math.round(offsetHpa * 4096.0);
199
197
  if (raw < 0) raw += 0x10000;
200
- await this._writeReg(_REG_RPDS_L, raw & 0xFF);
201
- await this._writeReg(_REG_RPDS_H, (raw >> 8) & 0xFF);
198
+ await this._conn.writeReg(_REG_RPDS_L, raw & 0xFF);
199
+ await this._conn.writeReg(_REG_RPDS_H, (raw >> 8) & 0xFF);
202
200
  }
203
201
 
204
202
  /**
@@ -209,8 +207,8 @@ class LPS22DFFull extends LPS22DFMinimal {
209
207
  async setPressureThreshold(thresholdPa) {
210
208
  const thresholdHpa = thresholdPa / 100.0;
211
209
  const raw = (Math.round(thresholdHpa * 16.0)) & 0x7FFF;
212
- await this._writeReg(_REG_THS_P_L, raw & 0xFF);
213
- await this._writeReg(_REG_THS_P_H, (raw >> 8) & 0xFF);
210
+ await this._conn.writeReg(_REG_THS_P_L, raw & 0xFF);
211
+ await this._conn.writeReg(_REG_THS_P_H, (raw >> 8) & 0xFF);
214
212
  }
215
213
 
216
214
  /**
@@ -228,8 +226,8 @@ class LPS22DFFull extends LPS22DFMinimal {
228
226
  if (intFFull) ctrl4 |= 0x04;
229
227
  if (intFWtm) ctrl4 |= 0x02;
230
228
  if (intFOvr) ctrl4 |= 0x01;
231
- await this._writeReg(_REG_CTRL_REG3, ctrl3);
232
- await this._writeReg(_REG_CTRL_REG4, ctrl4);
229
+ await this._conn.writeReg(_REG_CTRL_REG3, ctrl3);
230
+ await this._conn.writeReg(_REG_CTRL_REG4, ctrl4);
233
231
  }
234
232
 
235
233
  /**
@@ -241,22 +239,22 @@ class LPS22DFFull extends LPS22DFMinimal {
241
239
  if (phe) cfg |= 0x01;
242
240
  if (ple) cfg |= 0x02;
243
241
  if (lir) cfg |= 0x04;
244
- await this._writeReg(_REG_INTERRUPT_CFG, cfg);
242
+ await this._conn.writeReg(_REG_INTERRUPT_CFG, cfg);
245
243
  }
246
244
 
247
245
  /** @returns {Promise<void>} */
248
246
  async autozero() {
249
- await this._writeReg(_REG_INTERRUPT_CFG, 0x20);
247
+ await this._conn.writeReg(_REG_INTERRUPT_CFG, 0x20);
250
248
  }
251
249
 
252
250
  /** @returns {Promise<void>} */
253
251
  async autorefp() {
254
- await this._writeReg(_REG_INTERRUPT_CFG, 0x80);
252
+ await this._conn.writeReg(_REG_INTERRUPT_CFG, 0x80);
255
253
  }
256
254
 
257
255
  /** @returns {Promise<void>} */
258
256
  async resetReference() {
259
- await this._writeReg(_REG_INTERRUPT_CFG, 0x50);
257
+ await this._conn.writeReg(_REG_INTERRUPT_CFG, 0x50);
260
258
  }
261
259
 
262
260
  /**
@@ -264,9 +262,9 @@ class LPS22DFFull extends LPS22DFMinimal {
264
262
  * @returns {Promise<number>} Reference pressure in pascals.
265
263
  */
266
264
  async referencePressure() {
267
- const raw = await this._readReg(_REG_REF_P_L, 2);
265
+ const raw = await this._conn.readReg(_REG_REF_P_L, 2);
268
266
  let value = raw[0] | (raw[1] << 8);
269
- if (value & 0x8000) value -= 0x10000;
267
+ value = toSigned(value, 16);
270
268
  return (value / 4096.0) * 100.0;
271
269
  }
272
270
 
@@ -283,7 +281,7 @@ class LPS22DFFull extends LPS22DFMinimal {
283
281
  else if (mode === 3) { trig = 1; fm = 1; }
284
282
  else if (mode === 4) { trig = 1; fm = 2; }
285
283
  else { trig = 1; fm = 3; }
286
- await this._writeReg(_REG_FIFO_CTRL, (trig << 2) | (fm & 0x03));
284
+ await this._conn.writeReg(_REG_FIFO_CTRL, (trig << 2) | (fm & 0x03));
287
285
  }
288
286
 
289
287
  /**
@@ -292,7 +290,7 @@ class LPS22DFFull extends LPS22DFMinimal {
292
290
  * @returns {Promise<void>}
293
291
  */
294
292
  async setFifoWatermark(level) {
295
- await this._writeReg(_REG_FIFO_WTM, level & 0x7F);
293
+ await this._conn.writeReg(_REG_FIFO_WTM, level & 0x7F);
296
294
  }
297
295
 
298
296
  /**
@@ -300,7 +298,7 @@ class LPS22DFFull extends LPS22DFMinimal {
300
298
  * @returns {Promise<number>} Stored FIFO samples (0..127).
301
299
  */
302
300
  async fifoSampleCount() {
303
- const v = await this._readReg(_REG_FIFO_STATUS1, 1);
301
+ const v = await this._conn.readReg(_REG_FIFO_STATUS1, 1);
304
302
  return v[0];
305
303
  }
306
304
 
@@ -311,12 +309,12 @@ class LPS22DFFull extends LPS22DFMinimal {
311
309
  async readFifo() {
312
310
  const count = await this.fifoSampleCount();
313
311
  if (count === 0) return [];
314
- const raw = await this._readReg(_REG_FIFO_PRESS_XL, count * 3);
312
+ const raw = await this._conn.readReg(_REG_FIFO_PRESS_XL, count * 3);
315
313
  const out = new Array(count);
316
314
  for (let i = 0; i < count; i++) {
317
315
  const base = i * 3;
318
316
  let value = raw[base] | (raw[base + 1] << 8) | (raw[base + 2] << 16);
319
- if (value & 0x800000) value -= 0x1000000;
317
+ value = toSigned(value, 24);
320
318
  out[i] = (value / 4096.0) * 100.0;
321
319
  }
322
320
  return out;
@@ -327,7 +325,7 @@ class LPS22DFFull extends LPS22DFMinimal {
327
325
  * @returns {Promise<{boot_on:boolean,ia:boolean,ph:boolean,pl:boolean}>}
328
326
  */
329
327
  async interruptSource() {
330
- const raw = await this._readReg(_REG_INT_SOURCE, 1);
328
+ const raw = await this._conn.readReg(_REG_INT_SOURCE, 1);
331
329
  const v = raw[0];
332
330
  return {
333
331
  boot_on: !!(v & 0x80),