periph 1.4.0 → 1.5.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 (112) hide show
  1. package/README.md +27 -0
  2. package/examples/adc_dac/ad5251/complete.js +66 -0
  3. package/examples/adc_dac/ad5251/demo.js +69 -0
  4. package/examples/adc_dac/ad5251/minimal.js +23 -0
  5. package/examples/adc_dac/ad5252/complete.js +66 -0
  6. package/examples/adc_dac/ad5252/demo.js +69 -0
  7. package/examples/adc_dac/ad5252/minimal.js +23 -0
  8. package/examples/adc_dac/ads7828/complete.js +46 -0
  9. package/examples/adc_dac/ads7828/demo.js +45 -0
  10. package/examples/adc_dac/ads7828/minimal.js +17 -0
  11. package/examples/adc_dac/cat5171/complete.js +52 -0
  12. package/examples/adc_dac/cat5171/demo.js +59 -0
  13. package/examples/adc_dac/cat5171/minimal.js +23 -0
  14. package/examples/adc_dac/cs43l22/complete.js +88 -0
  15. package/examples/adc_dac/cs43l22/demo.js +56 -0
  16. package/examples/adc_dac/cs43l22/minimal.js +20 -0
  17. package/examples/adc_dac/ds1881/complete.js +62 -0
  18. package/examples/adc_dac/ds1881/demo.js +61 -0
  19. package/examples/adc_dac/ds1881/minimal.js +25 -0
  20. package/examples/adc_dac/ds3502/complete.js +46 -0
  21. package/examples/adc_dac/ds3502/demo.js +54 -0
  22. package/examples/adc_dac/ds3502/minimal.js +28 -0
  23. package/examples/adc_dac/mcp3422/complete.js +48 -0
  24. package/examples/adc_dac/mcp3422/demo.js +49 -0
  25. package/examples/adc_dac/mcp3422/minimal.js +25 -0
  26. package/examples/display/sh1106/complete.js +101 -0
  27. package/examples/display/sh1106/demo.js +68 -0
  28. package/examples/display/sh1106/minimal.js +25 -0
  29. package/examples/display/ssd1305/complete.js +129 -0
  30. package/examples/display/ssd1305/demo.js +71 -0
  31. package/examples/display/ssd1305/minimal.js +25 -0
  32. package/examples/display/ssd1306/complete.js +115 -0
  33. package/examples/display/ssd1306/demo.js +70 -0
  34. package/examples/display/ssd1306/minimal.js +25 -0
  35. package/examples/environmental/am2315/complete.js +34 -0
  36. package/examples/environmental/am2315/demo.js +46 -0
  37. package/examples/environmental/am2315/minimal.js +21 -0
  38. package/examples/humidity/htu21df/complete.js +49 -0
  39. package/examples/humidity/htu21df/demo.js +50 -0
  40. package/examples/humidity/htu21df/minimal.js +22 -0
  41. package/examples/humidity/sht31/complete.js +53 -0
  42. package/examples/humidity/sht31/demo.js +53 -0
  43. package/examples/humidity/sht31/minimal.js +21 -0
  44. package/examples/humidity/si7021/complete.js +55 -0
  45. package/examples/humidity/si7021/demo.js +58 -0
  46. package/examples/humidity/si7021/minimal.js +21 -0
  47. package/examples/imu/bno055/complete.js +85 -0
  48. package/examples/imu/bno055/demo.js +70 -0
  49. package/examples/imu/bno055/minimal.js +20 -0
  50. package/examples/io_expander/pca6408a/complete.js +63 -0
  51. package/examples/io_expander/pca6408a/demo.js +58 -0
  52. package/examples/io_expander/pca6408a/minimal.js +20 -0
  53. package/examples/io_expander/pca6416a/complete.js +68 -0
  54. package/examples/io_expander/pca6416a/demo.js +60 -0
  55. package/examples/io_expander/pca6416a/minimal.js +20 -0
  56. package/examples/io_expander/pca9536/complete.js +63 -0
  57. package/examples/io_expander/pca9536/demo.js +57 -0
  58. package/examples/io_expander/pca9536/minimal.js +20 -0
  59. package/examples/io_expander/pca9539/complete.js +68 -0
  60. package/examples/io_expander/pca9539/demo.js +60 -0
  61. package/examples/io_expander/pca9539/minimal.js +20 -0
  62. package/examples/light/max44009/complete.js +56 -0
  63. package/examples/light/max44009/demo.js +49 -0
  64. package/examples/light/max44009/minimal.js +22 -0
  65. package/examples/memory/at24c02n/complete.js +39 -0
  66. package/examples/memory/at24c02n/demo.js +39 -0
  67. package/examples/memory/at24c02n/minimal.js +15 -0
  68. package/examples/memory/at24c64/complete.js +39 -0
  69. package/examples/memory/at24c64/demo.js +39 -0
  70. package/examples/memory/at24c64/minimal.js +15 -0
  71. package/examples/memory/cat24c512/complete.js +39 -0
  72. package/examples/memory/cat24c512/demo.js +39 -0
  73. package/examples/memory/cat24c512/minimal.js +15 -0
  74. package/examples/other/pca9541/complete.js +52 -0
  75. package/examples/other/pca9541/demo.js +61 -0
  76. package/examples/other/pca9541/minimal.js +23 -0
  77. package/examples/temperature/tmp102/complete.js +74 -0
  78. package/examples/temperature/tmp102/demo.js +71 -0
  79. package/examples/temperature/tmp102/minimal.js +20 -0
  80. package/examples/tof/vl6180x/complete.js +62 -0
  81. package/examples/tof/vl6180x/demo.js +77 -0
  82. package/examples/tof/vl6180x/minimal.js +23 -0
  83. package/package.json +1 -1
  84. package/src/chips/adc_dac/ad5251.js +345 -0
  85. package/src/chips/adc_dac/ad5252.js +345 -0
  86. package/src/chips/adc_dac/ads7828.js +225 -0
  87. package/src/chips/adc_dac/cat5171.js +213 -0
  88. package/src/chips/adc_dac/cs43l22.js +645 -0
  89. package/src/chips/adc_dac/ds1881.js +357 -0
  90. package/src/chips/adc_dac/ds3502.js +241 -0
  91. package/src/chips/adc_dac/mcp3422.js +312 -0
  92. package/src/chips/display/mono_oled.js +182 -0
  93. package/src/chips/display/sh1106.js +496 -0
  94. package/src/chips/display/ssd1305.js +655 -0
  95. package/src/chips/display/ssd1306.js +458 -0
  96. package/src/chips/environmental/am2315.js +237 -0
  97. package/src/chips/humidity/htu21df.js +222 -0
  98. package/src/chips/humidity/sht31.js +284 -0
  99. package/src/chips/humidity/si7021.js +282 -0
  100. package/src/chips/imu/bno055.js +496 -0
  101. package/src/chips/io_expander/pca6408a.js +407 -0
  102. package/src/chips/io_expander/pca6416a.js +455 -0
  103. package/src/chips/io_expander/pca9536.js +398 -0
  104. package/src/chips/io_expander/pca9539.js +457 -0
  105. package/src/chips/light/max44009.js +336 -0
  106. package/src/chips/memory/at24c02n.js +220 -0
  107. package/src/chips/memory/at24c64.js +220 -0
  108. package/src/chips/memory/cat24c512.js +221 -0
  109. package/src/chips/other/pca9541.js +315 -0
  110. package/src/chips/temperature/tmp102.js +361 -0
  111. package/src/chips/tof/vl6180x.js +580 -0
  112. package/src/discovery/registry.js +65 -13
@@ -0,0 +1,222 @@
1
+ 'use strict';
2
+
3
+ const _CMD_T_HOLD = 0xE3;
4
+ const _CMD_RH_HOLD = 0xE5;
5
+ const _CMD_T_POLL = 0xF3;
6
+ const _CMD_RH_POLL = 0xF5;
7
+ const _CMD_WRITE_USER = 0xE6;
8
+ const _CMD_READ_USER = 0xE7;
9
+ const _CMD_RESET = 0xFE;
10
+ const _POLL_ATTEMPTS = 100;
11
+
12
+ const _HEATER = 0x04;
13
+ const _VDD_LOW = 0x40;
14
+ const _RES_MASK = 0x81;
15
+
16
+ const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
17
+
18
+ /**
19
+ * CRC-8 used by the HTU21D-F: polynomial 0x131 (x^8 + x^5 + x^4 + 1), init 0x00, no final XOR.
20
+ * @param {Buffer|number[]} data
21
+ * @returns {number}
22
+ */
23
+ function crc8(data) {
24
+ let crc = 0x00;
25
+ for (const byte of data) {
26
+ crc ^= byte;
27
+ for (let i = 0; i < 8; i++) crc = (crc & 0x80) ? ((crc << 1) ^ 0x31) & 0xFF : (crc << 1) & 0xFF;
28
+ }
29
+ return crc;
30
+ }
31
+
32
+ function _toHumidity(code) {
33
+ return -6.0 + 125.0 * code / 65536.0;
34
+ }
35
+
36
+ function _toTemperature(code) {
37
+ return -46.85 + 175.72 * code / 65536.0;
38
+ }
39
+
40
+ /**
41
+ * HTU21D-F humidity and temperature sensor — minimal interface.
42
+ *
43
+ * Each `read()` triggers one temperature and one humidity measurement without hold master
44
+ * (0xF3 / 0xF5), polls the NACKed read header until the chip ACKs (timeout 100 ms) and
45
+ * verifies the CRC of both results (one retry on a mismatch).
46
+ *
47
+ * Default configuration (power-on): 12-bit RH / 14-bit temperature, heater off,
48
+ * no hold master, CRC checked. The constructor does not touch the bus; `init()` performs
49
+ * the soft reset.
50
+ */
51
+ class HTU21DFMinimal {
52
+ /**
53
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (address 0x40).
54
+ */
55
+ constructor(connection) {
56
+ this._conn = connection;
57
+ this._hold = false;
58
+ }
59
+
60
+ /** Wait 15 ms for power-up, soft reset (0xFE) and wait 15 ms. */
61
+ async init() {
62
+ await _sleep(15);
63
+ await this._conn.write(Buffer.from([_CMD_RESET]));
64
+ await _sleep(15);
65
+ }
66
+
67
+ async _measureOnce(holdCmd, pollCmd) {
68
+ if (this._hold) {
69
+ await this._conn.write(Buffer.from([holdCmd]));
70
+ return Buffer.from(await this._conn.read(3));
71
+ }
72
+ await this._conn.write(Buffer.from([pollCmd]));
73
+ for (let i = 0; i < _POLL_ATTEMPTS; i++) {
74
+ try {
75
+ return Buffer.from(await this._conn.read(3));
76
+ } catch (e) {
77
+ await _sleep(1); // read header NACKed: conversion not done yet
78
+ }
79
+ }
80
+ throw new Error('HTU21D-F measurement did not complete');
81
+ }
82
+
83
+ async _measure(holdCmd, pollCmd) {
84
+ for (let attempt = 0; attempt < 2; attempt++) {
85
+ const data = await this._measureOnce(holdCmd, pollCmd);
86
+ if (crc8(data.subarray(0, 2)) === data[2]) return ((data[0] << 8) | data[1]) & 0xFFFC;
87
+ }
88
+ throw new Error('HTU21D-F CRC mismatch');
89
+ }
90
+
91
+ /**
92
+ * Measure temperature and humidity.
93
+ * @returns {Promise<[number, number]>} [temperature °C, relative humidity %RH (not clamped)]
94
+ */
95
+ async read() {
96
+ const t = await this._measure(_CMD_T_HOLD, _CMD_T_POLL);
97
+ const rh = await this._measure(_CMD_RH_HOLD, _CMD_RH_POLL);
98
+ return [_toTemperature(t), _toHumidity(rh)];
99
+ }
100
+ }
101
+
102
+ /**
103
+ * HTU21D-F full interface — resolution, heater, hold master, compensation, dew point.
104
+ *
105
+ * Extends HTU21DFMinimal with single values, raw codes, the four resolution settings,
106
+ * the on-chip heater, the end-of-battery flag, the raw user register, soft reset,
107
+ * hold-master mode, temperature-compensated humidity and the dew point.
108
+ */
109
+ class HTU21DFFull extends HTU21DFMinimal {
110
+ /**
111
+ * Measure temperature and humidity and return the raw codes.
112
+ * @returns {Promise<[number, number]>} [temperatureRaw, humidityRaw], 16-bit with the status bits cleared.
113
+ */
114
+ async readRaw() {
115
+ const t = await this._measure(_CMD_T_HOLD, _CMD_T_POLL);
116
+ const rh = await this._measure(_CMD_RH_HOLD, _CMD_RH_POLL);
117
+ return [t, rh];
118
+ }
119
+
120
+ /**
121
+ * Run one temperature measurement.
122
+ * @returns {Promise<number>} Temperature in °C.
123
+ */
124
+ async readTemperature() {
125
+ return _toTemperature(await this._measure(_CMD_T_HOLD, _CMD_T_POLL));
126
+ }
127
+
128
+ /**
129
+ * Run one humidity measurement (not clamped; the ASIC range is -6 ... 118 %RH).
130
+ * @returns {Promise<number>} Relative humidity in %RH.
131
+ */
132
+ async readHumidity() {
133
+ return _toHumidity(await this._measure(_CMD_RH_HOLD, _CMD_RH_POLL));
134
+ }
135
+
136
+ /**
137
+ * Measure and correct the humidity for temperature (-0.15 %RH/°C relative to 25 °C).
138
+ * @returns {Promise<number>} Compensated relative humidity in %RH.
139
+ */
140
+ async readHumidityCompensated() {
141
+ const [t, rh] = await this.read();
142
+ return rh + (25.0 - t) * (-0.15);
143
+ }
144
+
145
+ /**
146
+ * Read the user register (command 0xE7).
147
+ * @returns {Promise<number>} Raw register value (0x02 after reset).
148
+ */
149
+ async readUserRegister() {
150
+ const data = await this._conn.writeRead(Buffer.from([_CMD_READ_USER]), 1);
151
+ return data[0];
152
+ }
153
+
154
+ async _writeUser(value) {
155
+ await this._conn.write(Buffer.from([_CMD_WRITE_USER, value & 0xFF]));
156
+ }
157
+
158
+ /**
159
+ * Select the resolution (read-modify-write of the RES bits 7 and 0).
160
+ * @param {number} res - 0 = 12-bit RH / 14-bit T, 1 = 8 / 12, 2 = 10 / 13, 3 = 11 / 11.
161
+ */
162
+ async setResolution(res) {
163
+ if (![0, 1, 2, 3].includes(res)) throw new RangeError('res must be 0-3');
164
+ const bits = ((res >> 1) << 7) | (res & 1);
165
+ await this._writeUser(((await this.readUserRegister()) & ~_RES_MASK) | bits);
166
+ }
167
+
168
+ /**
169
+ * @returns {Promise<number>} Resolution setting: 0 = 12/14 bit, 1 = 8/12, 2 = 10/13, 3 = 11/11.
170
+ */
171
+ async getResolution() {
172
+ const reg = await this.readUserRegister();
173
+ return (((reg >> 7) & 1) << 1) | (reg & 1);
174
+ }
175
+
176
+ /**
177
+ * Switch the on-chip heater on or off (read-modify-write).
178
+ * @param {boolean} on
179
+ */
180
+ async setHeater(on) {
181
+ const reg = await this.readUserRegister();
182
+ await this._writeUser(on ? (reg | _HEATER) : (reg & ~_HEATER));
183
+ }
184
+
185
+ /** @returns {Promise<boolean>} True if the on-chip heater is enabled. */
186
+ async heater() {
187
+ return ((await this.readUserRegister()) & _HEATER) !== 0;
188
+ }
189
+
190
+ /** @returns {Promise<boolean>} True if the end-of-battery flag is set (VDD below about 2.25 V). */
191
+ async batteryLow() {
192
+ return ((await this.readUserRegister()) & _VDD_LOW) !== 0;
193
+ }
194
+
195
+ /** Soft reset (0xFE) and wait 15 ms; the heater bit survives. */
196
+ async softReset() {
197
+ await this._conn.write(Buffer.from([_CMD_RESET]));
198
+ await _sleep(15);
199
+ }
200
+
201
+ /**
202
+ * Use the hold-master commands (0xE3 / 0xE5, SCL stretching) instead of polling.
203
+ * The bus layer must support clock stretching.
204
+ * @param {boolean} on
205
+ */
206
+ setHoldMaster(on) {
207
+ this._hold = Boolean(on);
208
+ }
209
+
210
+ /**
211
+ * Run one measurement and compute the dew point.
212
+ * @returns {Promise<number>} Dew point in °C.
213
+ */
214
+ async dewPoint() {
215
+ let [t, rh] = await this.read();
216
+ if (rh <= 0.0) rh = 0.01;
217
+ const pp = Math.pow(10.0, 8.1332 - 1762.39 / (t + 235.66));
218
+ return -1762.39 / (Math.log10(rh * pp / 100.0) - 8.1332) - 235.66;
219
+ }
220
+ }
221
+
222
+ module.exports = { HTU21DFMinimal, HTU21DFFull, crc8 };
@@ -0,0 +1,284 @@
1
+ 'use strict';
2
+
3
+ const _CMD_SOFT_RESET = 0x30A2;
4
+ const _CMD_HEATER_ON = 0x306D;
5
+ const _CMD_HEATER_OFF = 0x3066;
6
+ const _CMD_READ_STATUS = 0xF32D;
7
+ const _CMD_CLEAR_STATUS = 0x3041;
8
+ const _CMD_FETCH = 0xE000;
9
+ const _CMD_BREAK = 0x3093;
10
+ const _CMD_ART = 0x2B32;
11
+
12
+ // single shot: [stretching disabled, stretching enabled, wait in ms]
13
+ const _SINGLE = {
14
+ high: [0x2400, 0x2C06, 15],
15
+ medium: [0x240B, 0x2C0D, 6],
16
+ low: [0x2416, 0x2C10, 4],
17
+ };
18
+
19
+ // periodic: rate -> repeatability -> command
20
+ const _PERIODIC = {
21
+ 0.5: { high: 0x2032, medium: 0x2024, low: 0x202F },
22
+ 1: { high: 0x2130, medium: 0x2126, low: 0x212D },
23
+ 2: { high: 0x2236, medium: 0x2220, low: 0x222B },
24
+ 4: { high: 0x2334, medium: 0x2322, low: 0x2329 },
25
+ 10: { high: 0x2737, medium: 0x2721, low: 0x272A },
26
+ };
27
+
28
+ const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
29
+
30
+ /**
31
+ * CRC-8 used by the SHT3x: polynomial 0x31, init 0xFF, no reflection, no final XOR.
32
+ * @param {Buffer|number[]} data
33
+ * @returns {number}
34
+ */
35
+ function crc8(data) {
36
+ let crc = 0xFF;
37
+ for (const byte of data) {
38
+ crc ^= byte;
39
+ for (let i = 0; i < 8; i++) crc = (crc & 0x80) ? ((crc << 1) ^ 0x31) & 0xFF : (crc << 1) & 0xFF;
40
+ }
41
+ return crc;
42
+ }
43
+
44
+ function _decode(data) {
45
+ if (crc8(data.subarray(0, 2)) !== data[2] || crc8(data.subarray(3, 5)) !== data[5]) {
46
+ throw new Error('SHT31 CRC mismatch');
47
+ }
48
+ return [(data[0] << 8) | data[1], (data[3] << 8) | data[4]];
49
+ }
50
+
51
+ function _convert(rawT, rawRh) {
52
+ return [-45.0 + 175.0 * rawT / 65535.0, 100.0 * rawRh / 65535.0];
53
+ }
54
+
55
+ /**
56
+ * SHT31 temperature and humidity sensor — minimal interface.
57
+ *
58
+ * Each `read()` runs one single-shot measurement at high repeatability with
59
+ * clock stretching disabled and CRC checking on. The heater stays off.
60
+ *
61
+ * Default configuration (baked in at construction):
62
+ * - Soft reset (0x30A2) with 1 ms waits before and after (started asynchronously
63
+ * by the constructor; every public method waits for it; `await sensor.init()` surfaces errors)
64
+ * - Single shot, high repeatability (0x2400), fixed 15 ms wait; if the read header is
65
+ * still NACKed the driver keeps polling for up to 20 ms more
66
+ * - Both CRC-8 bytes verified; one retry of the whole measurement on a mismatch, then an Error
67
+ */
68
+ class SHT31Minimal {
69
+ /**
70
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (address 0x44 with ADDR low, 0x45 with ADDR high).
71
+ */
72
+ constructor(connection) {
73
+ this._conn = connection;
74
+ this._repeatability = 'high';
75
+ this._stretch = false;
76
+ this._ready = this._reset();
77
+ // Re-raised by every public method; mark handled so it never surfaces on its own.
78
+ this._ready.catch(() => {});
79
+ }
80
+
81
+ /**
82
+ * Wait for the soft reset started by the constructor.
83
+ * @returns {Promise<void>}
84
+ */
85
+ async init() {
86
+ await this._ready;
87
+ }
88
+
89
+ async _reset() {
90
+ await _sleep(1);
91
+ await this._command(_CMD_SOFT_RESET);
92
+ await _sleep(1);
93
+ }
94
+
95
+ async _command(code) {
96
+ await this._conn.write(Buffer.from([code >> 8, code & 0xFF]));
97
+ }
98
+
99
+ async _singleShot() {
100
+ await this._ready;
101
+ const [noStretch, stretch, wait] = _SINGLE[this._repeatability];
102
+ for (let attempt = 0; attempt < 2; attempt++) {
103
+ await this._command(this._stretch ? stretch : noStretch);
104
+ if (!this._stretch) await _sleep(wait);
105
+ let data = null;
106
+ for (let i = 0; i < 21 && data === null; i++) {
107
+ try {
108
+ data = Buffer.from(await this._conn.read(6));
109
+ } catch (e) {
110
+ await _sleep(1); // read header NACKed: not ready yet
111
+ }
112
+ }
113
+ if (data === null) throw new Error('SHT31 measurement did not complete');
114
+ try {
115
+ return _decode(data);
116
+ } catch (e) {
117
+ if (attempt === 1) throw e;
118
+ }
119
+ }
120
+ return null;
121
+ }
122
+
123
+ /**
124
+ * Run one single-shot measurement.
125
+ * @returns {Promise<[number, number]>} `[temperature_c, humidity_pct]` — °C and %RH.
126
+ * @throws {Error} If a CRC mismatch persists after one retry.
127
+ */
128
+ async read() {
129
+ const [rawT, rawRh] = await this._singleShot();
130
+ return _convert(rawT, rawRh);
131
+ }
132
+ }
133
+
134
+ /**
135
+ * SHT31 full interface — repeatability, clock stretching, periodic mode, heater and status.
136
+ *
137
+ * Extends SHT31Minimal with single values and raw words, the three repeatability levels, the
138
+ * clock-stretching command set, periodic measurements (0.5–10 mps plus ART), the heater, the
139
+ * status register, soft reset and the dew point.
140
+ */
141
+ class SHT31Full extends SHT31Minimal {
142
+ /**
143
+ * @param {import('../../connection/connection').Connection} connection - Configured I²C connection pointing at the device (address 0x44 or 0x45).
144
+ */
145
+ constructor(connection) {
146
+ super(connection);
147
+ }
148
+
149
+ /**
150
+ * Run one measurement and return the temperature only.
151
+ * @returns {Promise<number>} Temperature in °C.
152
+ */
153
+ async readTemperature() { return (await this.read())[0]; }
154
+
155
+ /**
156
+ * Run one measurement and return the relative humidity only.
157
+ * @returns {Promise<number>} Relative humidity in %RH.
158
+ */
159
+ async readHumidity() { return (await this.read())[1]; }
160
+
161
+ /**
162
+ * Run one measurement and return the raw 16-bit words.
163
+ * @returns {Promise<[number, number]>} `[rawTemperature, rawHumidity]`, each 0–65535.
164
+ */
165
+ async readRaw() { return this._singleShot(); }
166
+
167
+ /**
168
+ * Select the repeatability for later single-shot reads (measurement time 4 / 6 / 15 ms).
169
+ * @param {'low'|'medium'|'high'} level
170
+ * @throws {RangeError} For any other value.
171
+ */
172
+ setRepeatability(level) {
173
+ if (!(level in _SINGLE)) throw new RangeError("level must be 'low', 'medium' or 'high'");
174
+ this._repeatability = level;
175
+ }
176
+
177
+ /**
178
+ * Use the clock-stretching command set for single-shot reads (the bus layer must support it); the fixed wait is skipped.
179
+ * @param {boolean} on
180
+ */
181
+ setClockStretching(on) { this._stretch = !!on; }
182
+
183
+ /**
184
+ * Start periodic measurements; read results with `fetch()`.
185
+ * @param {number} rate - Measurements per second: 0.5, 1, 2, 4 or 10 (ignored with `art`).
186
+ * @param {'low'|'medium'|'high'} [repeatability='high']
187
+ * @param {boolean} [art=false] - Accelerated response time mode (4 mps, command 0x2B32).
188
+ * @returns {Promise<void>}
189
+ * @throws {RangeError} For an unsupported rate or repeatability.
190
+ */
191
+ async startPeriodic(rate, repeatability = 'high', art = false) {
192
+ await this._ready;
193
+ if (art) {
194
+ await this._command(_CMD_ART);
195
+ return;
196
+ }
197
+ if (!(rate in _PERIODIC)) throw new RangeError('rate must be 0.5, 1, 2, 4 or 10');
198
+ if (!(repeatability in _SINGLE)) throw new RangeError("repeatability must be 'low', 'medium' or 'high'");
199
+ await this._command(_PERIODIC[rate][repeatability]);
200
+ }
201
+
202
+ /**
203
+ * Read the latest periodic result (command 0xE000).
204
+ * @returns {Promise<[number, number]|null>} `[temperature_c, humidity_pct]`, or `null` if no new data is available (read header NACKed).
205
+ * @throws {Error} If a CRC check fails.
206
+ */
207
+ async fetch() {
208
+ await this._ready;
209
+ await this._command(_CMD_FETCH);
210
+ let data;
211
+ try {
212
+ data = Buffer.from(await this._conn.read(6));
213
+ } catch (e) {
214
+ return null;
215
+ }
216
+ const [rawT, rawRh] = _decode(data);
217
+ return _convert(rawT, rawRh);
218
+ }
219
+
220
+ /**
221
+ * Stop periodic mode (break command 0x3093) and wait 1 ms.
222
+ * @returns {Promise<void>}
223
+ */
224
+ async stopPeriodic() {
225
+ await this._ready;
226
+ await this._command(_CMD_BREAK);
227
+ await _sleep(1);
228
+ }
229
+
230
+ /**
231
+ * Switch the on-chip heater (about 5–25 mW) on or off.
232
+ * @param {boolean} on
233
+ * @returns {Promise<void>}
234
+ */
235
+ async setHeater(on) {
236
+ await this._ready;
237
+ await this._command(on ? _CMD_HEATER_ON : _CMD_HEATER_OFF);
238
+ }
239
+
240
+ /**
241
+ * Read the status register (command 0xF32D, CRC verified).
242
+ * @returns {Promise<number>} 16-bit status, e.g. 0x8010 after reset; bit 13 = heater on.
243
+ * @throws {Error} If the CRC check fails.
244
+ */
245
+ async readStatus() {
246
+ await this._ready;
247
+ await this._command(_CMD_READ_STATUS);
248
+ const data = Buffer.from(await this._conn.read(3));
249
+ if (crc8(data.subarray(0, 2)) !== data[2]) throw new Error('SHT31 CRC mismatch');
250
+ return (data[0] << 8) | data[1];
251
+ }
252
+
253
+ /**
254
+ * Clear the status register flags (command 0x3041).
255
+ * @returns {Promise<void>}
256
+ */
257
+ async clearStatus() {
258
+ await this._ready;
259
+ await this._command(_CMD_CLEAR_STATUS);
260
+ }
261
+
262
+ /**
263
+ * Soft reset (0x30A2), wait 1 ms; leaves periodic mode and returns to single shot.
264
+ * @returns {Promise<void>}
265
+ */
266
+ async softReset() {
267
+ await this._ready;
268
+ await this._command(_CMD_SOFT_RESET);
269
+ await _sleep(1);
270
+ }
271
+
272
+ /**
273
+ * Run one measurement and compute the dew point (Magnus formula).
274
+ * @returns {Promise<number>} Dew point in °C.
275
+ */
276
+ async dewPoint() {
277
+ let [t, rh] = await this.read();
278
+ if (rh <= 0) rh = 0.01;
279
+ const gamma = Math.log(rh / 100.0) + (17.62 * t) / (243.12 + t);
280
+ return 243.12 * gamma / (17.62 - gamma);
281
+ }
282
+ }
283
+
284
+ module.exports = { SHT31Minimal, SHT31Full, crc8 };