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
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const { toSigned } = require('../../connection/register');
4
+
3
5
  const _REG_CAL_START = 0x88;
4
6
  const _REG_H1 = 0xA1;
5
7
  const _REG_ID = 0xD0;
@@ -21,11 +23,6 @@ function _delay(ms) {
21
23
  while (Date.now() - start < ms) { /* spin */ }
22
24
  }
23
25
 
24
- function _signExtend12(raw) {
25
- if (raw & 0x800) return raw - 0x1000;
26
- return raw;
27
- }
28
-
29
26
  /**
30
27
  * BME280 combined humidity + pressure + temperature sensor — minimal interface.
31
28
  *
@@ -44,13 +41,11 @@ function _signExtend12(raw) {
44
41
  * `await sensor.temperature()` (or any other async method) once before
45
42
  * relying on calibrated readings if this matters for your use case.
46
43
  *
47
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
48
- * @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
44
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C, SMBus, or SPI register connection (SPI: default Bosch convention, readBit 0x80, no multi-byte bit).
49
45
  */
50
46
  class BME280Minimal {
51
- constructor(connection, busType = 'i2c') {
47
+ constructor(connection) {
52
48
  this._conn = connection;
53
- this._busType = busType;
54
49
  this._mode = 0;
55
50
  this._osrsT = 1;
56
51
  this._osrsP = 1;
@@ -63,13 +58,13 @@ class BME280Minimal {
63
58
 
64
59
  async _init() {
65
60
  await this._readCalibration();
66
- await this._writeReg(_REG_CTRL_HUM, this._osrsH);
67
- await this._writeReg(_REG_CTRL_MEAS, (1 << 5) | (1 << 2) | 0);
68
- await this._writeReg(_REG_CONFIG, 0);
61
+ await this._conn.writeReg(_REG_CTRL_HUM, this._osrsH);
62
+ await this._conn.writeReg(_REG_CTRL_MEAS, (1 << 5) | (1 << 2) | 0);
63
+ await this._conn.writeReg(_REG_CONFIG, 0);
69
64
  }
70
65
 
71
66
  async _readCalibration() {
72
- const data = await this._conn.writeRead(Buffer.from([_REG_CAL_START]), 26);
67
+ const data = await this._conn.readReg(_REG_CAL_START, 26);
73
68
  this._digT1 = data.readUInt16LE(0);
74
69
  this._digT2 = data.readInt16LE(2);
75
70
  this._digT3 = data.readInt16LE(4);
@@ -84,31 +79,22 @@ class BME280Minimal {
84
79
  this._digP9 = data.readInt16LE(22);
85
80
  this._digH1 = data[25];
86
81
 
87
- const h = await this._conn.writeRead(Buffer.from([_REG_CAL_H2]), 7);
82
+ const h = await this._conn.readReg(_REG_CAL_H2, 7);
88
83
  this._digH2 = h.readInt16LE(0);
89
84
  this._digH3 = h[2];
90
- this._digH4 = _signExtend12(((h[3] << 4) | (h[4] & 0x0F)) & 0x0FFF);
91
- this._digH5 = _signExtend12(((h[5] << 4) | ((h[4] >> 4) & 0x0F)) & 0x0FFF);
85
+ this._digH4 = toSigned((h[3] << 4) | (h[4] & 0x0F), 12);
86
+ this._digH5 = toSigned((h[5] << 4) | ((h[4] >> 4) & 0x0F), 12);
92
87
  this._digH6 = h.readInt8(6);
93
88
  }
94
89
 
95
- async _writeReg(reg, value) {
96
- const addr = this._busType === 'spi' ? (reg & 0x7F) : reg;
97
- await this._conn.write(Buffer.from([addr, value]));
98
- }
99
-
100
- async _readReg(reg, n) {
101
- return this._conn.writeRead(Buffer.from([reg]), n);
102
- }
103
-
104
90
  async _triggerAndRead() {
105
91
  if (this._mode !== 3) {
106
- await this._writeReg(_REG_CTRL_HUM, this._osrsH);
92
+ await this._conn.writeReg(_REG_CTRL_HUM, this._osrsH);
107
93
  const ctrl = (this._osrsT << 5) | (this._osrsP << 2) | 1;
108
- await this._writeReg(_REG_CTRL_MEAS, ctrl);
94
+ await this._conn.writeReg(_REG_CTRL_MEAS, ctrl);
109
95
  _delay(_MEAS_TIME_MS);
110
96
  }
111
- const raw = await this._readReg(_REG_DATA_START, 8);
97
+ const raw = await this._conn.readReg(_REG_DATA_START, 8);
112
98
  const adcP = (raw[0] << 12) | (raw[1] << 4) | (raw[2] >> 4);
113
99
  const adcT = (raw[3] << 12) | (raw[4] << 4) | (raw[5] >> 4);
114
100
  const adcH = (raw[6] << 8) | raw[7];
@@ -210,8 +196,7 @@ class BME280Minimal {
210
196
  /**
211
197
  * BME280 full interface — extends BME280Minimal with configuration, dew point, and altitude helpers.
212
198
  *
213
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
214
- * @param {string} [busType='i2c'] - Bus type: 'i2c' or 'spi'.
199
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C, SMBus, or SPI register connection (SPI: default Bosch convention, readBit 0x80, no multi-byte bit).
215
200
  */
216
201
  class BME280Full extends BME280Minimal {
217
202
  static OSRS_SKIP = 0;
@@ -243,8 +228,8 @@ class BME280Full extends BME280Minimal {
243
228
  static STATUS_MEASURING = 0x08;
244
229
  static STATUS_IM_UPDATE = 0x01;
245
230
 
246
- constructor(connection, busType = 'i2c') {
247
- super(connection, busType);
231
+ constructor(connection) {
232
+ super(connection);
248
233
  }
249
234
 
250
235
  /**
@@ -265,9 +250,9 @@ class BME280Full extends BME280Minimal {
265
250
  this._mode = mode;
266
251
  this._filter = filter;
267
252
  this._tSb = tSb;
268
- await this._writeReg(_REG_CTRL_HUM, osrsH);
269
- await this._writeReg(_REG_CONFIG, (tSb << 5) | (filter << 2));
270
- await this._writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | mode);
253
+ await this._conn.writeReg(_REG_CTRL_HUM, osrsH);
254
+ await this._conn.writeReg(_REG_CONFIG, (tSb << 5) | (filter << 2));
255
+ await this._conn.writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | mode);
271
256
  }
272
257
 
273
258
  /**
@@ -281,8 +266,8 @@ class BME280Full extends BME280Minimal {
281
266
  this._osrsT = osrsT;
282
267
  this._osrsP = osrsP;
283
268
  this._osrsH = osrsH;
284
- await this._writeReg(_REG_CTRL_HUM, osrsH);
285
- await this._writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | this._mode);
269
+ await this._conn.writeReg(_REG_CTRL_HUM, osrsH);
270
+ await this._conn.writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | this._mode);
286
271
  }
287
272
 
288
273
  /**
@@ -292,7 +277,7 @@ class BME280Full extends BME280Minimal {
292
277
  */
293
278
  async setMode(mode) {
294
279
  this._mode = mode;
295
- await this._writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | mode);
280
+ await this._conn.writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | mode);
296
281
  }
297
282
 
298
283
  /**
@@ -302,7 +287,7 @@ class BME280Full extends BME280Minimal {
302
287
  */
303
288
  async setFilter(coeff) {
304
289
  this._filter = coeff;
305
- await this._writeReg(_REG_CONFIG, (this._tSb << 5) | (coeff << 2));
290
+ await this._conn.writeReg(_REG_CONFIG, (this._tSb << 5) | (coeff << 2));
306
291
  }
307
292
 
308
293
  /**
@@ -313,7 +298,7 @@ class BME280Full extends BME280Minimal {
313
298
  */
314
299
  async setStandby(tSb) {
315
300
  this._tSb = tSb;
316
- await this._writeReg(_REG_CONFIG, (tSb << 5) | (this._filter << 2));
301
+ await this._conn.writeReg(_REG_CONFIG, (tSb << 5) | (this._filter << 2));
317
302
  }
318
303
 
319
304
  /**
@@ -321,7 +306,7 @@ class BME280Full extends BME280Minimal {
321
306
  * @returns {Promise<number>} Status byte; bit 3 = measuring, bit 0 = im_update.
322
307
  */
323
308
  async status() {
324
- const data = await this._readReg(_REG_STATUS, 1);
309
+ const data = await this._conn.readReg(_REG_STATUS, 1);
325
310
  return data[0];
326
311
  }
327
312
 
@@ -364,7 +349,7 @@ class BME280Full extends BME280Minimal {
364
349
  * @returns {Promise<number>} Chip ID; expect 0x60.
365
350
  */
366
351
  async chipId() {
367
- const data = await this._readReg(_REG_ID, 1);
352
+ const data = await this._conn.readReg(_REG_ID, 1);
368
353
  return data[0];
369
354
  }
370
355
 
@@ -373,12 +358,12 @@ class BME280Full extends BME280Minimal {
373
358
  * @returns {Promise<void>}
374
359
  */
375
360
  async reset() {
376
- await this._writeReg(_REG_RESET, _RESET_CMD);
361
+ await this._conn.writeReg(_REG_RESET, _RESET_CMD);
377
362
  _delay(2);
378
363
  await this._readCalibration();
379
- await this._writeReg(_REG_CTRL_HUM, this._osrsH);
380
- await this._writeReg(_REG_CONFIG, (this._tSb << 5) | (this._filter << 2));
381
- await this._writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | this._mode);
364
+ await this._conn.writeReg(_REG_CTRL_HUM, this._osrsH);
365
+ await this._conn.writeReg(_REG_CONFIG, (this._tSb << 5) | (this._filter << 2));
366
+ await this._conn.writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | this._mode);
382
367
  }
383
368
  }
384
369
 
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ const { toSigned } = require('../../connection/register');
4
+
3
5
  const CONST_ARRAY1 = [
4
6
  2147483647, 2147483647, 2147483647, 2147483647, 2147483647,
5
7
  2126008810, 2147483647, 2130303777, 2147483647, 2147483647,
@@ -57,7 +59,7 @@ function _delay(ms) {
57
59
  * `await sensor.temperature()` (or any other async method) once before
58
60
  * relying on calibrated readings if this matters for your use case.
59
61
  *
60
- * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
62
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
61
63
  */
62
64
  class BME680Minimal {
63
65
  constructor(connection) {
@@ -77,19 +79,19 @@ class BME680Minimal {
77
79
 
78
80
  async _init() {
79
81
  await this._readCalibration();
80
- await this._writeReg(_REG_CTRL_HUM, this._osrsH);
81
- await this._writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | 0);
82
- await this._writeReg(_REG_CONFIG, 0);
82
+ await this._conn.writeReg(_REG_CTRL_HUM, this._osrsH);
83
+ await this._conn.writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | 0);
84
+ await this._conn.writeReg(_REG_CONFIG, 0);
83
85
  await this._setupHeater(0, this._heatTemp, this._heatDur);
84
- await this._writeReg(_REG_CTRL_GAS_1, (1 << 4) | 0);
86
+ await this._conn.writeReg(_REG_CTRL_GAS_1, (1 << 4) | 0);
85
87
  }
86
88
 
87
89
  async _readCalibration() {
88
- const b1 = await this._conn.writeRead(Buffer.from([_REG_CAL_BLOCK1]), 23);
89
- const b2 = await this._conn.writeRead(Buffer.from([_REG_CAL_BLOCK2]), 14);
90
- const s1 = await this._conn.writeRead(Buffer.from([_REG_RES_HEAT_VAL]), 1);
91
- const s2 = await this._conn.writeRead(Buffer.from([_REG_RES_HEAT_RANGE]), 1);
92
- const s3 = await this._conn.writeRead(Buffer.from([_REG_RANGE_SW_ERR]), 1);
90
+ const b1 = await this._conn.readReg(_REG_CAL_BLOCK1, 23);
91
+ const b2 = await this._conn.readReg(_REG_CAL_BLOCK2, 14);
92
+ const s1 = await this._conn.readReg(_REG_RES_HEAT_VAL, 1);
93
+ const s2 = await this._conn.readReg(_REG_RES_HEAT_RANGE, 1);
94
+ const s3 = await this._conn.readReg(_REG_RANGE_SW_ERR, 1);
93
95
 
94
96
  this._parT2 = b1.readInt16LE(0);
95
97
  this._parT3 = b1.readInt8(2);
@@ -119,16 +121,9 @@ class BME680Minimal {
119
121
  this._resHeatVal = s1.readInt8(0);
120
122
  this._resHeatRange = (s2[0] >> 4) & 0x03;
121
123
  const rse = (s3[0] >> 4) & 0x0F;
122
- this._rangeSwitchingError = rse < 8 ? rse : rse - 16;
123
- }
124
-
125
- async _writeReg(reg, value) {
126
- await this._conn.write(Buffer.from([reg, value]));
124
+ this._rangeSwitchingError = toSigned(rse, 4);
127
125
  }
128
126
 
129
- async _readReg(reg, n) {
130
- return this._conn.writeRead(Buffer.from([reg]), n);
131
- }
132
127
 
133
128
  _calcHeaterResistance(targetTemp, ambientTemp) {
134
129
  const parG1 = this._parG1;
@@ -157,16 +152,16 @@ class BME680Minimal {
157
152
  async _setupHeater(index, tempC, durMs) {
158
153
  const res = this._calcHeaterResistance(tempC, this._ambientTemp);
159
154
  const gw = this._calcGasWait(durMs);
160
- await this._writeReg(0x5A + index, res);
161
- await this._writeReg(0x64 + index, gw);
155
+ await this._conn.writeReg(0x5A + index, res);
156
+ await this._conn.writeReg(0x64 + index, gw);
162
157
  }
163
158
 
164
159
  async _triggerAndRead() {
165
- await this._writeReg(_REG_CTRL_HUM, this._osrsH);
160
+ await this._conn.writeReg(_REG_CTRL_HUM, this._osrsH);
166
161
  const ctrl = (this._osrsT << 5) | (this._osrsP << 2) | 1;
167
- await this._writeReg(_REG_CTRL_MEAS, ctrl);
162
+ await this._conn.writeReg(_REG_CTRL_MEAS, ctrl);
168
163
  _delay(_MEAS_TIME_MS);
169
- const raw = await this._readReg(_REG_PRESS_MSB, 13);
164
+ const raw = await this._conn.readReg(_REG_PRESS_MSB, 13);
170
165
  const pressAdc = (raw[0] << 12) | (raw[1] << 4) | (raw[2] >> 4);
171
166
  const tempAdc = (raw[3] << 12) | (raw[4] << 4) | (raw[5] >> 4);
172
167
  const humAdc = (raw[6] << 8) | raw[7];
@@ -301,7 +296,7 @@ class BME680Minimal {
301
296
  /**
302
297
  * BME680 full interface — extends BME680Minimal with configuration, multi-profile heater, and status.
303
298
  *
304
- * @param {import('../../connection/connection').Connection} connection - Configured I²C connection.
299
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
305
300
  */
306
301
  class BME680Full extends BME680Minimal {
307
302
  static OSRS_SKIP = 0;
@@ -347,9 +342,9 @@ class BME680Full extends BME680Minimal {
347
342
  this._osrsP = osrsP;
348
343
  this._osrsH = osrsH;
349
344
  this._filter = filter;
350
- await this._writeReg(_REG_CTRL_HUM, osrsH);
351
- await this._writeReg(_REG_CONFIG, filter << 2);
352
- await this._writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | mode);
345
+ await this._conn.writeReg(_REG_CTRL_HUM, osrsH);
346
+ await this._conn.writeReg(_REG_CONFIG, filter << 2);
347
+ await this._conn.writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | mode);
353
348
  }
354
349
 
355
350
  /**
@@ -363,8 +358,8 @@ class BME680Full extends BME680Minimal {
363
358
  this._osrsT = osrsT;
364
359
  this._osrsP = osrsP;
365
360
  this._osrsH = osrsH;
366
- await this._writeReg(_REG_CTRL_HUM, osrsH);
367
- await this._writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | 0);
361
+ await this._conn.writeReg(_REG_CTRL_HUM, osrsH);
362
+ await this._conn.writeReg(_REG_CTRL_MEAS, (osrsT << 5) | (osrsP << 2) | 0);
368
363
  }
369
364
 
370
365
  /**
@@ -374,7 +369,7 @@ class BME680Full extends BME680Minimal {
374
369
  */
375
370
  async setFilter(coeff) {
376
371
  this._filter = coeff;
377
- await this._writeReg(_REG_CONFIG, coeff << 2);
372
+ await this._conn.writeReg(_REG_CONFIG, coeff << 2);
378
373
  }
379
374
 
380
375
  /**
@@ -387,7 +382,7 @@ class BME680Full extends BME680Minimal {
387
382
  this._heatTemp = tempC;
388
383
  this._heatDur = durationMs;
389
384
  await this._setupHeater(0, tempC, durationMs);
390
- await this._writeReg(_REG_CTRL_GAS_1, (1 << 4) | 0);
385
+ await this._conn.writeReg(_REG_CTRL_GAS_1, (1 << 4) | 0);
391
386
  }
392
387
 
393
388
  /**
@@ -409,7 +404,7 @@ class BME680Full extends BME680Minimal {
409
404
  async selectHeaterProfile(index) {
410
405
  this._nbConv = index;
411
406
  const gas1 = this._gasEnabled ? ((1 << 4) | index) : index;
412
- await this._writeReg(_REG_CTRL_GAS_1, gas1);
407
+ await this._conn.writeReg(_REG_CTRL_GAS_1, gas1);
413
408
  }
414
409
 
415
410
  /**
@@ -420,7 +415,7 @@ class BME680Full extends BME680Minimal {
420
415
  async setGasEnabled(enabled) {
421
416
  this._gasEnabled = enabled;
422
417
  const gas1 = enabled ? ((1 << 4) | this._nbConv) : this._nbConv;
423
- await this._writeReg(_REG_CTRL_GAS_1, gas1);
418
+ await this._conn.writeReg(_REG_CTRL_GAS_1, gas1);
424
419
  }
425
420
 
426
421
  /**
@@ -429,7 +424,7 @@ class BME680Full extends BME680Minimal {
429
424
  * @returns {Promise<void>}
430
425
  */
431
426
  async setHeaterOff(off) {
432
- await this._writeReg(_REG_CTRL_GAS_0, off ? 0x08 : 0x00);
427
+ await this._conn.writeReg(_REG_CTRL_GAS_0, off ? 0x08 : 0x00);
433
428
  }
434
429
 
435
430
  /**
@@ -461,7 +456,7 @@ class BME680Full extends BME680Minimal {
461
456
  * @returns {Promise<boolean>} True if gas_valid_r was set.
462
457
  */
463
458
  async gasValid() {
464
- const raw = await this._readReg(0x2B, 1);
459
+ const raw = await this._conn.readReg(0x2B, 1);
465
460
  return ((raw[0] >> 5) & 1) === 1;
466
461
  }
467
462
 
@@ -470,7 +465,7 @@ class BME680Full extends BME680Minimal {
470
465
  * @returns {Promise<boolean>} True if heat_stab_r was set.
471
466
  */
472
467
  async heaterStable() {
473
- const raw = await this._readReg(0x2B, 1);
468
+ const raw = await this._conn.readReg(0x2B, 1);
474
469
  return ((raw[0] >> 4) & 1) === 1;
475
470
  }
476
471
 
@@ -479,7 +474,7 @@ class BME680Full extends BME680Minimal {
479
474
  * @returns {Promise<number>} Status byte with flags.
480
475
  */
481
476
  async status() {
482
- const raw = await this._readReg(_REG_MEAS_STATUS, 1);
477
+ const raw = await this._conn.readReg(_REG_MEAS_STATUS, 1);
483
478
  return raw[0];
484
479
  }
485
480
 
@@ -488,7 +483,7 @@ class BME680Full extends BME680Minimal {
488
483
  * @returns {Promise<number>} Chip ID; expect 0x61.
489
484
  */
490
485
  async chipId() {
491
- const raw = await this._readReg(_REG_ID, 1);
486
+ const raw = await this._conn.readReg(_REG_ID, 1);
492
487
  return raw[0];
493
488
  }
494
489
 
@@ -497,15 +492,15 @@ class BME680Full extends BME680Minimal {
497
492
  * @returns {Promise<void>}
498
493
  */
499
494
  async reset() {
500
- await this._writeReg(_REG_RESET, _RESET_CMD);
495
+ await this._conn.writeReg(_REG_RESET, _RESET_CMD);
501
496
  _delay(2);
502
497
  await this._readCalibration();
503
- await this._writeReg(_REG_CTRL_HUM, this._osrsH);
504
- await this._writeReg(_REG_CONFIG, this._filter << 2);
505
- await this._writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | 0);
498
+ await this._conn.writeReg(_REG_CTRL_HUM, this._osrsH);
499
+ await this._conn.writeReg(_REG_CONFIG, this._filter << 2);
500
+ await this._conn.writeReg(_REG_CTRL_MEAS, (this._osrsT << 5) | (this._osrsP << 2) | 0);
506
501
  await this._setupHeater(this._nbConv, this._heatTemp, this._heatDur);
507
502
  const gas1 = this._gasEnabled ? ((1 << 4) | this._nbConv) : this._nbConv;
508
- await this._writeReg(_REG_CTRL_GAS_1, gas1);
503
+ await this._conn.writeReg(_REG_CTRL_GAS_1, gas1);
509
504
  }
510
505
  }
511
506
 
@@ -48,7 +48,7 @@ function _delay(ms) {
48
48
  * absent device" deterministically should call an async method (e.g.
49
49
  * `await sensor.status()`) right after construction and handle its rejection.
50
50
  *
51
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
51
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
52
52
  */
53
53
  class ENS160Minimal {
54
54
  constructor(connection) {
@@ -57,34 +57,26 @@ class ENS160Minimal {
57
57
  }
58
58
 
59
59
  async _init() {
60
- await this._writeReg(_REG_OPMODE, _OPMODE_IDLE);
60
+ await this._conn.writeReg(_REG_OPMODE, _OPMODE_IDLE);
61
61
  _delay(1);
62
62
  const partId = await this._readRegLE16(_REG_PART_ID);
63
63
  if (partId !== _PART_ID_EXPECTED) {
64
64
  throw new Error('ENS160 not found: expected PART_ID 0x0160, got 0x' + partId.toString(16).padStart(4, '0'));
65
65
  }
66
- await this._writeReg(_REG_OPMODE, _OPMODE_STANDARD);
67
- }
68
-
69
- async _writeReg(reg, value) {
70
- await this._conn.write(Buffer.from([reg, value]));
66
+ await this._conn.writeReg(_REG_OPMODE, _OPMODE_STANDARD);
71
67
  }
72
68
 
73
69
  async _writeRegLE16(reg, value) {
74
- await this._conn.write(Buffer.from([reg, value & 0xFF, (value >> 8) & 0xFF]));
75
- }
76
-
77
- async _readReg(reg, n) {
78
- return this._conn.writeRead(Buffer.from([reg]), n);
70
+ await this._conn.writeReg(reg, Buffer.from([value & 0xFF, (value >> 8) & 0xFF]));
79
71
  }
80
72
 
81
73
  async _readRegLE16(reg) {
82
- const data = await this._readReg(reg, 2);
74
+ const data = await this._conn.readReg(reg, 2);
83
75
  return data[0] | (data[1] << 8);
84
76
  }
85
77
 
86
78
  async _readDeviceStatus() {
87
- const data = await this._readReg(_REG_DEVICE_STATUS, 1);
79
+ const data = await this._conn.readReg(_REG_DEVICE_STATUS, 1);
88
80
  return data[0];
89
81
  }
90
82
 
@@ -127,7 +119,7 @@ class ENS160Minimal {
127
119
  if (validity !== 0) {
128
120
  throw new Error('ENS160: data not valid (VALIDITY_FLAG=' + validity + ')');
129
121
  }
130
- const data = await this._readReg(_REG_DATA_AQI, 5);
122
+ const data = await this._conn.readReg(_REG_DATA_AQI, 5);
131
123
  const aqi = data[0] & 0x07;
132
124
  const tvocPpb = data[1] | (data[2] << 8);
133
125
  const eco2Ppm = data[3] | (data[4] << 8);
@@ -142,7 +134,7 @@ class ENS160Minimal {
142
134
  * raw sensor resistance, firmware version query, interrupt configuration,
143
135
  * and sleep/wake control.
144
136
  *
145
- * @param {import('../../connection/connection').Connection} connection - Configured I²C or SPI connection.
137
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection.
146
138
  */
147
139
  class ENS160Full extends ENS160Minimal {
148
140
  static VALIDITY_OK = 0;
@@ -191,7 +183,7 @@ class ENS160Full extends ENS160Minimal {
191
183
  */
192
184
  async readAqi() {
193
185
  await this._waitForNewData();
194
- const data = await this._readReg(_REG_DATA_AQI, 1);
186
+ const data = await this._conn.readReg(_REG_DATA_AQI, 1);
195
187
  return data[0] & 0x07;
196
188
  }
197
189
 
@@ -228,7 +220,7 @@ class ENS160Full extends ENS160Minimal {
228
220
  * @returns {Promise<object>} Keys: tempCelsius (number), rhPercent (number).
229
221
  */
230
222
  async readCompensationActuals() {
231
- const data = await this._readReg(_REG_DATA_T, 4);
223
+ const data = await this._conn.readReg(_REG_DATA_T, 4);
232
224
  const tempRaw = data[0] | (data[1] << 8);
233
225
  const rhRaw = data[2] | (data[3] << 8);
234
226
  const tempCelsius = (tempRaw / 64.0) - 273.15;
@@ -245,15 +237,15 @@ class ENS160Full extends ENS160Minimal {
245
237
  * @returns {Promise<object>} Keys: major (number), minor (number), release (number).
246
238
  */
247
239
  async getFirmwareVersion() {
248
- await this._writeReg(_REG_OPMODE, _OPMODE_IDLE);
240
+ await this._conn.writeReg(_REG_OPMODE, _OPMODE_IDLE);
249
241
  _delay(1);
250
- await this._writeReg(_REG_COMMAND, 0x0E);
242
+ await this._conn.writeReg(_REG_COMMAND, 0x0E);
251
243
  _delay(1);
252
- const data = await this._readReg(_REG_GPR_READ + 4, 3);
244
+ const data = await this._conn.readReg(_REG_GPR_READ + 4, 3);
253
245
  const major = data[0];
254
246
  const minor = data[1];
255
247
  const release = data[2];
256
- await this._writeReg(_REG_OPMODE, _OPMODE_STANDARD);
248
+ await this._conn.writeReg(_REG_OPMODE, _OPMODE_STANDARD);
257
249
  return { major, minor, release };
258
250
  }
259
251
 
@@ -273,7 +265,7 @@ class ENS160Full extends ENS160Minimal {
273
265
  if (onGpr) config |= 0x08;
274
266
  if (pushPull) config |= 0x20;
275
267
  if (activeHigh) config |= 0x40;
276
- await this._writeReg(_REG_CONFIG, config);
268
+ await this._conn.writeReg(_REG_CONFIG, config);
277
269
  }
278
270
 
279
271
  /**
@@ -281,7 +273,7 @@ class ENS160Full extends ENS160Minimal {
281
273
  * @returns {Promise<void>}
282
274
  */
283
275
  async sleep() {
284
- await this._writeReg(_REG_OPMODE, _OPMODE_DEEP_SLEEP);
276
+ await this._conn.writeReg(_REG_OPMODE, _OPMODE_DEEP_SLEEP);
285
277
  }
286
278
 
287
279
  /**
@@ -289,9 +281,9 @@ class ENS160Full extends ENS160Minimal {
289
281
  * @returns {Promise<void>}
290
282
  */
291
283
  async wake() {
292
- await this._writeReg(_REG_OPMODE, _OPMODE_IDLE);
284
+ await this._conn.writeReg(_REG_OPMODE, _OPMODE_IDLE);
293
285
  _delay(1);
294
- await this._writeReg(_REG_OPMODE, _OPMODE_STANDARD);
286
+ await this._conn.writeReg(_REG_OPMODE, _OPMODE_STANDARD);
295
287
  }
296
288
  }
297
289