periph 1.2.0 → 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 (153) hide show
  1. package/README.md +18 -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/motor/drv8830/complete.js +50 -0
  48. package/examples/motor/drv8830/demo.js +57 -0
  49. package/examples/motor/drv8830/minimal.js +23 -0
  50. package/examples/pressure/bmp280/complete.js +1 -1
  51. package/examples/pressure/bmp280/demo.js +1 -1
  52. package/examples/pressure/bmp280/minimal.js +1 -1
  53. package/examples/pressure/bmp581/complete.js +1 -1
  54. package/examples/pressure/bmp581/demo.js +1 -1
  55. package/examples/pressure/bmp581/minimal.js +1 -1
  56. package/examples/pressure/lps22df/complete.js +1 -1
  57. package/examples/pressure/lps22df/demo.js +1 -1
  58. package/examples/pressure/lps22df/minimal.js +1 -1
  59. package/examples/rtc/ds3231/complete.js +77 -0
  60. package/examples/rtc/ds3231/demo.js +60 -0
  61. package/examples/rtc/ds3231/minimal.js +23 -0
  62. package/examples/rtc/pcf8523/complete.js +66 -0
  63. package/examples/rtc/pcf8523/demo.js +65 -0
  64. package/examples/rtc/pcf8523/minimal.js +20 -0
  65. package/examples/temperature/mcp9808/complete.js +89 -0
  66. package/examples/temperature/mcp9808/demo.js +62 -0
  67. package/examples/temperature/mcp9808/minimal.js +21 -0
  68. package/examples/temperature/tmp117/complete.js +90 -0
  69. package/examples/temperature/tmp117/demo.js +73 -0
  70. package/examples/temperature/tmp117/minimal.js +21 -0
  71. package/examples/tof/vl53l0x/complete.js +118 -0
  72. package/examples/tof/vl53l0x/demo.js +83 -0
  73. package/examples/tof/vl53l0x/minimal.js +25 -0
  74. package/examples/tof/vl53l1x/complete.js +151 -0
  75. package/examples/tof/vl53l1x/demo.js +100 -0
  76. package/examples/tof/vl53l1x/minimal.js +25 -0
  77. package/package.json +1 -1
  78. package/src/chips/accelerometer/adxl345.js +10 -31
  79. package/src/chips/accelerometer/adxl362.js +7 -7
  80. package/src/chips/accelerometer/bma150.js +419 -0
  81. package/src/chips/adc_dac/ad7705.js +36 -9
  82. package/src/chips/adc_dac/ad7706.js +39 -9
  83. package/src/chips/adc_dac/hy3131.js +311 -0
  84. package/src/chips/display/il0373.js +607 -0
  85. package/src/chips/display/ssd1331.js +412 -0
  86. package/src/chips/display/st7036.js +401 -0
  87. package/src/chips/display/uc1611s.js +648 -0
  88. package/src/chips/display/uc8159.js +394 -0
  89. package/src/chips/display/uc8276c.js +658 -0
  90. package/src/chips/environmental/bme280.js +31 -46
  91. package/src/chips/environmental/bme680.js +38 -43
  92. package/src/chips/gas/ens160.js +18 -26
  93. package/src/chips/gyroscope/l3g4200d.js +34 -50
  94. package/src/chips/gyroscope/l3gd20h.js +24 -40
  95. package/src/chips/imu/mpu6050.js +29 -41
  96. package/src/chips/imu/mpu9250.js +45 -69
  97. package/src/chips/imu/mpu9255.js +54 -78
  98. package/src/chips/io_expander/mcp23017.js +24 -33
  99. package/src/chips/light/apds-9930.js +52 -60
  100. package/src/chips/light/apds9960.js +69 -77
  101. package/src/chips/magnetometer/as5600.js +13 -27
  102. package/src/chips/magnetometer/hmc5883l.js +32 -29
  103. package/src/chips/memory/_23x1024.js +228 -0
  104. package/src/chips/memory/_24aa02uid.js +8 -11
  105. package/src/chips/memory/mx25l12845e.js +467 -0
  106. package/src/chips/memory/sd_spi.js +486 -0
  107. package/src/chips/motor/drv8830.js +239 -0
  108. package/src/chips/other/mpr121.js +7 -7
  109. package/src/chips/power/ina219.js +5 -8
  110. package/src/chips/power/ina226.js +5 -8
  111. package/src/chips/power/ina3221.js +5 -8
  112. package/src/chips/pressure/bmp085.js +17 -11
  113. package/src/chips/pressure/bmp180.js +7 -7
  114. package/src/chips/pressure/bmp280.js +21 -33
  115. package/src/chips/pressure/bmp384.js +28 -39
  116. package/src/chips/pressure/bmp581.js +59 -70
  117. package/src/chips/pressure/lps22df.js +50 -52
  118. package/src/chips/pressure/lps28dfw.js +15 -13
  119. package/src/chips/pressure/lps33hw.js +7 -5
  120. package/src/chips/rtc/ds3231.js +504 -0
  121. package/src/chips/rtc/pcf8523.js +533 -0
  122. package/src/chips/temperature/mcp9808.js +490 -0
  123. package/src/chips/temperature/tmp117.js +471 -0
  124. package/src/chips/tof/_vl53_base.js +160 -0
  125. package/src/chips/tof/vl53l0x.js +816 -0
  126. package/src/chips/tof/vl53l1x.js +816 -0
  127. package/src/connection/connection.js +6 -4
  128. package/src/connection/dhtxx.js +5 -3
  129. package/src/connection/display.js +278 -0
  130. package/src/connection/display_mock.js +77 -0
  131. package/src/connection/hx711.js +5 -3
  132. package/src/connection/i2c.js +6 -4
  133. package/src/connection/i2c_mock.js +47 -0
  134. package/src/connection/input_pin.js +36 -5
  135. package/src/connection/register.js +14 -0
  136. package/src/connection/register_connection.js +65 -0
  137. package/src/connection/sipo.js +4 -2
  138. package/src/connection/sipo_mock.js +49 -0
  139. package/src/connection/smbus.js +6 -4
  140. package/src/connection/softspi.js +73 -0
  141. package/src/connection/spi.js +110 -23
  142. package/src/connection/spi_mock.js +50 -0
  143. package/src/connection/uart.js +2 -1
  144. package/src/discovery/discovery.js +218 -0
  145. package/src/discovery/registry.js +85 -0
  146. package/src/storage/block_device.js +95 -0
  147. package/src/storage/ram_block_device.js +72 -0
  148. package/examples/motor/.gitkeep +0 -0
  149. package/examples/temperature/.gitkeep +0 -0
  150. package/examples/tof/.gitkeep +0 -0
  151. package/src/chips/motor/.gitkeep +0 -0
  152. package/src/chips/temperature/.gitkeep +0 -0
  153. package/src/chips/tof/.gitkeep +0 -0
@@ -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),
@@ -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;
@@ -36,7 +38,7 @@ function _delay(ms) {
36
38
  * - ODR = 0100b (25 Hz)
37
39
  * - BDU = 1, EN_LPFP = 1, LFPF_CFG = 0 (ODR/4 bandwidth)
38
40
  *
39
- * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
41
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
40
42
  */
41
43
  class LPS28DFWMinimal {
42
44
  constructor(connection) {
@@ -51,13 +53,13 @@ class LPS28DFWMinimal {
51
53
  }
52
54
 
53
55
  async _init() {
56
+ _delay(_BOOT_WAIT_MS);
54
57
  const who = await this._readReg(_REG_WHO_AM_I, 1);
55
58
  if (who[0] !== _CHIP_ID) {
56
59
  throw new Error('LPS28DFW WHO_AM_I mismatch: expected 0x' +
57
60
  _CHIP_ID.toString(16).toUpperCase().padStart(2, '0') +
58
61
  ', got 0x' + who[0].toString(16).toUpperCase().padStart(2, '0'));
59
62
  }
60
- _delay(_BOOT_WAIT_MS);
61
63
  const ctrl2 = (this._fsMode << 6) | (this._lpfCfg << 5) | (this._lpfEn << 4) | (this._bdu << 3);
62
64
  await this._writeReg(_REG_CTRL_REG2, ctrl2);
63
65
  const ctrl1 = (this._odr << 3) | (this._avg & 0x07);
@@ -65,11 +67,11 @@ class LPS28DFWMinimal {
65
67
  }
66
68
 
67
69
  async _writeReg(reg, value) {
68
- await this._conn.write(Buffer.from([reg, value]));
70
+ await this._conn.writeReg(reg, value);
69
71
  }
70
72
 
71
73
  async _readReg(reg, n) {
72
- return this._conn.writeRead(Buffer.from([reg]), n);
74
+ return this._conn.readReg(reg, n);
73
75
  }
74
76
 
75
77
  /**
@@ -79,7 +81,7 @@ class LPS28DFWMinimal {
79
81
  async readPressure() {
80
82
  const raw = await this._readReg(_REG_PRESS_OUT_XL, 3);
81
83
  let v = (raw[2] << 16) | (raw[1] << 8) | raw[0];
82
- if (v & 0x800000) v |= 0xFF000000;
84
+ v = toSigned(v, 24);
83
85
  const sens = this._fsMode === 0 ? _SENSITIVITY_LSB_PER_HPA_MODE1 : _SENSITIVITY_LSB_PER_HPA_MODE2;
84
86
  return v / sens;
85
87
  }
@@ -90,7 +92,7 @@ class LPS28DFWMinimal {
90
92
  */
91
93
  async readTemperature() {
92
94
  const raw = await this._readReg(_REG_TEMP_OUT_L, 2);
93
- const v = raw.readInt16BE(0);
95
+ const v = raw.readInt16LE(0);
94
96
  return v / 100.0;
95
97
  }
96
98
  }
@@ -98,7 +100,7 @@ class LPS28DFWMinimal {
98
100
  /**
99
101
  * LPS28DFW full interface — extends LPS28DFWMinimal with full configuration.
100
102
  *
101
- * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
103
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
102
104
  */
103
105
  class LPS28DFWFull extends LPS28DFWMinimal {
104
106
  static ODR_POWER_DOWN = 0x00;
@@ -165,8 +167,8 @@ class LPS28DFWFull extends LPS28DFWMinimal {
165
167
  async read() {
166
168
  const raw = await this._readReg(_REG_PRESS_OUT_XL, 5);
167
169
  let p = (raw[2] << 16) | (raw[1] << 8) | raw[0];
168
- if (p & 0x800000) p |= 0xFF000000;
169
- const t = raw.readInt16BE(3);
170
+ p = toSigned(p, 24);
171
+ const t = raw.readInt16LE(3);
170
172
  const sens = this._fsMode === 0 ? _SENSITIVITY_LSB_PER_HPA_MODE1 : _SENSITIVITY_LSB_PER_HPA_MODE2;
171
173
  return { pressure: p / sens, temperature: t / 100.0 };
172
174
  }
@@ -231,9 +233,9 @@ class LPS28DFWFull extends LPS28DFWMinimal {
231
233
  * @returns {Promise<void>}
232
234
  */
233
235
  async fifoConfigure(mode, wtm, stopOnWtm) {
234
- if (mode === LPS28DFWFull.FIFO_BYPASS) {
235
- await this._writeReg(0x14, 0x00);
236
- }
236
+ // Always pass through Bypass first when switching FIFO modes (spec's
237
+ // "FIFO reset" procedure) -- not just when the target mode IS bypass.
238
+ await this._writeReg(0x14, 0x00);
237
239
  const trig = mode >= 4 ? 1 : 0;
238
240
  const fMode = mode & 0x03;
239
241
  const ctrl = (trig << 2) | ((stopOnWtm ? 1 : 0) << 3) | fMode;
@@ -255,7 +257,7 @@ class LPS28DFWFull extends LPS28DFWMinimal {
255
257
  for (let i = 0; i < count; i++) {
256
258
  const b = i * 3;
257
259
  let v = (raw[b + 2] << 16) | (raw[b + 1] << 8) | raw[b];
258
- if (v & 0x800000) v |= 0xFF000000;
260
+ v = toSigned(v, 24);
259
261
  out.push(v / sens);
260
262
  }
261
263
  return out;
@@ -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;
@@ -29,8 +31,8 @@ function _delay(ms) {
29
31
  while (Date.now() - start < ms) {}
30
32
  }
31
33
 
32
- function _signExtend24(v) { return (v & 0x800000) ? (v | 0xFF000000) : v; }
33
- function _signExtend16(v) { return (v & 0x8000) ? (v | 0xFF00) : v; }
34
+ function _signExtend24(v) { return toSigned(v, 24); }
35
+ function _signExtend16(v) { return toSigned(v, 16); }
34
36
 
35
37
  /**
36
38
  * LPS33HW water-resistant MEMS absolute pressure sensor - minimal interface.
@@ -40,7 +42,7 @@ function _signExtend16(v) { return (v & 0x8000) ? (v | 0xFF00) : v; }
40
42
  *
41
43
  * Default: ODR=1 Hz, BDU=1, EN_LPFP=0, IF_ADD_INC=1.
42
44
  *
43
- * @param {import('../../connection/connection').Connection} connection - Configured I2C or SPI connection.
45
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
44
46
  */
45
47
  class LPS33HWMinimal {
46
48
  constructor(connection) {
@@ -64,11 +66,11 @@ class LPS33HWMinimal {
64
66
  }
65
67
 
66
68
  async _writeReg(reg, value) {
67
- await this._conn.write(Buffer.from([reg, value]));
69
+ await this._conn.writeReg(reg, value);
68
70
  }
69
71
 
70
72
  async _readReg(reg, n) {
71
- return this._conn.writeRead(Buffer.from([reg]), n);
73
+ return this._conn.readReg(reg, n);
72
74
  }
73
75
 
74
76
  async _waitStatus(mask) {