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
@@ -93,12 +93,12 @@ async function readRegChannel(conn, reg, channel, nBytes) {
93
93
  return value;
94
94
  }
95
95
 
96
- async function configureClock(conn, mclkHz, outputRateHz) {
96
+ async function configureClock(conn, mclkHz, outputRateHz, channel) {
97
97
  const clkBit = (mclkHz >= MCLK_2_4576MHZ) ? 0x04 : 0x00;
98
98
  const clkDivBit = (mclkHz === MCLK_2MHZ || mclkHz === MCLK_4_9152MHZ) ? 0x08 : 0x00;
99
99
  const rates = (mclkHz >= MCLK_2_4576MHZ) ? FS_RATES_2_4MHZ : FS_RATES_1MHZ;
100
100
  const fsIdx = rates.indexOf(outputRateHz);
101
- await writeRegChannel(conn, REG_CLOCK, clkDivBit | clkBit | fsIdx, CH1, 1);
101
+ await writeRegChannel(conn, REG_CLOCK, clkDivBit | clkBit | fsIdx, channel, 1);
102
102
  }
103
103
 
104
104
  function codeToVoltage(code, gain, bipolar, vref) {
@@ -133,6 +133,12 @@ class AD7706Minimal {
133
133
  this._gain = 1;
134
134
  this._bipolar = true;
135
135
  this._buffered = false;
136
+ this._gain2 = 1;
137
+ this._bipolar2 = true;
138
+ this._buffered2 = false;
139
+ this._gain3 = 1;
140
+ this._bipolar3 = true;
141
+ this._buffered3 = false;
136
142
 
137
143
  if (resetPin) {
138
144
  resetPin.set(false);
@@ -140,11 +146,13 @@ class AD7706Minimal {
140
146
  }
141
147
 
142
148
  const defaultRate = (mclkHz >= MCLK_2_4576MHZ) ? FS_RATES_2_4MHZ[0] : FS_RATES_1MHZ[0];
143
- configureClock(connection, mclkHz, defaultRate);
149
+ configureClock(connection, mclkHz, defaultRate, CH1);
144
150
 
145
151
  const setup = MODE_SELF_CAL | GAIN_BITS[0] | BIPOLAR | UNBUFFERED | FSYNC_RUN;
146
152
  writeRegChannel(connection, REG_SETUP, setup, CH1, 1);
147
- return waitDrdy(connection);
153
+ // JS constructors can't be async, so this fires unawaited -- same
154
+ // documented fire-and-forget convention as MFRC522Minimal/AD7705Minimal.
155
+ waitDrdy(connection);
148
156
  }
149
157
 
150
158
  /**
@@ -170,6 +178,16 @@ class AD7706Minimal {
170
178
  * AD7706 full driver — adds per-channel configuration, calibration, and power control.
171
179
  */
172
180
  class AD7706Full extends AD7706Minimal {
181
+ /**
182
+ * @param {number} channel - 1, 2, or 3.
183
+ * @returns {{gain: number, bipolar: boolean, buffered: boolean}} The gain/bipolar/buffered state last set by configure() for that channel.
184
+ */
185
+ _channelState(channel) {
186
+ if (channel === 1) return { gain: this._gain, bipolar: this._bipolar, buffered: this._buffered };
187
+ if (channel === 2) return { gain: this._gain2, bipolar: this._bipolar2, buffered: this._buffered2 };
188
+ return { gain: this._gain3, bipolar: this._bipolar3, buffered: this._buffered3 };
189
+ }
190
+
173
191
  /**
174
192
  * Write the Setup and Clock Registers for the given channel.
175
193
  * Does not calibrate — call selfCalibrate() (or one of the
@@ -191,7 +209,7 @@ class AD7706Full extends AD7706Minimal {
191
209
  throw new Error(`outputRateHz must be one of ${rates.join(', ')} Hz`);
192
210
  }
193
211
 
194
- await configureClock(this._conn, this._mclkHz, outputRateHz);
212
+ await configureClock(this._conn, this._mclkHz, outputRateHz, ch);
195
213
 
196
214
  const buBit = bipolar ? BIPOLAR : UNIPOLAR;
197
215
  const bufBit = buffered ? BUFFERED : UNBUFFERED;
@@ -202,6 +220,14 @@ class AD7706Full extends AD7706Minimal {
202
220
  this._gain = gain;
203
221
  this._bipolar = bipolar;
204
222
  this._buffered = buffered;
223
+ } else if (channel === 2) {
224
+ this._gain2 = gain;
225
+ this._bipolar2 = bipolar;
226
+ this._buffered2 = buffered;
227
+ } else {
228
+ this._gain3 = gain;
229
+ this._bipolar3 = bipolar;
230
+ this._buffered3 = buffered;
205
231
  }
206
232
  }
207
233
 
@@ -223,7 +249,8 @@ class AD7706Full extends AD7706Minimal {
223
249
  */
224
250
  async readVoltageChannel(channel) {
225
251
  const code = await this.readRawChannel(channel);
226
- return codeToVoltage(code, this._gain, this._bipolar, this._vref);
252
+ const { gain, bipolar } = this._channelState(channel);
253
+ return codeToVoltage(code, gain, bipolar, this._vref);
227
254
  }
228
255
 
229
256
  /**
@@ -232,7 +259,8 @@ class AD7706Full extends AD7706Minimal {
232
259
  */
233
260
  async selfCalibrate(channel) {
234
261
  const ch = channelConst(channel);
235
- const setup = calibrationSetup(MODE_SELF_CAL, this._gain, this._bipolar, this._buffered);
262
+ const { gain, bipolar, buffered } = this._channelState(channel);
263
+ const setup = calibrationSetup(MODE_SELF_CAL, gain, bipolar, buffered);
236
264
  await writeRegChannel(this._conn, REG_SETUP, setup, ch, 1);
237
265
  await waitDrdy(this._conn);
238
266
  }
@@ -243,7 +271,8 @@ class AD7706Full extends AD7706Minimal {
243
271
  */
244
272
  async systemCalibrateZero(channel) {
245
273
  const ch = channelConst(channel);
246
- const setup = calibrationSetup(MODE_ZERO_SYS, this._gain, this._bipolar, this._buffered);
274
+ const { gain, bipolar, buffered } = this._channelState(channel);
275
+ const setup = calibrationSetup(MODE_ZERO_SYS, gain, bipolar, buffered);
247
276
  await writeRegChannel(this._conn, REG_SETUP, setup, ch, 1);
248
277
  await waitDrdy(this._conn);
249
278
  }
@@ -254,7 +283,8 @@ class AD7706Full extends AD7706Minimal {
254
283
  */
255
284
  async systemCalibrateFull(channel) {
256
285
  const ch = channelConst(channel);
257
- const setup = calibrationSetup(MODE_FULL_SYS, this._gain, this._bipolar, this._buffered);
286
+ const { gain, bipolar, buffered } = this._channelState(channel);
287
+ const setup = calibrationSetup(MODE_FULL_SYS, gain, bipolar, buffered);
258
288
  await writeRegChannel(this._conn, REG_SETUP, setup, ch, 1);
259
289
  await waitDrdy(this._conn);
260
290
  }
@@ -0,0 +1,311 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * HYCON HY3131 50,000-count DMM analog front end driver (SPI).
5
+ *
6
+ * Minimal reads AD1 as a differential voltage; Full exposes AD1/AD2/AD3,
7
+ * low-pass filter, RMS, peak hold, frequency counter, interrupt flags and raw
8
+ * register access. See specs/adc_dac/hy3131.md.
9
+ *
10
+ * The chip's SPI framing is non-standard (7-bit address + R/W bit, one blank
11
+ * clock on reads, auto-increment), so the driver uses the bare connection's
12
+ * `write` / `writeRead`. JS constructors cannot be async: call `await
13
+ * adc.init()` after construction (or use `HY3131Minimal.create`).
14
+ */
15
+
16
+ const REG = {
17
+ AD1: 0x00, AD2: 0x03, LPF: 0x06, RMS: 0x09, PKHMIN: 0x0E, PKHMAX: 0x11,
18
+ CTSTA: 0x14, CTC: 0x15, CTB: 0x18, CTA: 0x1B, INTF: 0x1E, INTE: 0x1F,
19
+ R20: 0x20, R22: 0x22, R23: 0x23, R24: 0x24, R25: 0x25, R26: 0x26, R27: 0x27,
20
+ R28: 0x28, R29: 0x29, R2A: 0x2A, R2F: 0x2F, R30: 0x30, R31: 0x31, R32: 0x32,
21
+ R33: 0x33, R34: 0x34, R35: 0x35, R36: 0x36, R37: 0x37,
22
+ };
23
+ const FLAG = { CTF: 0x01, AD2F: 0x02, AD1F: 0x04, LPFF: 0x08, RMSF: 0x10, BORF: 0x80 };
24
+ const AD1_GAINS = [0.9, 1.8, 2.7, 3.6];
25
+ const DCOS = [0, 0.25, 0.5, 0.75, 0, -0.25, -0.5, -0.75];
26
+ const TIMEOUT_MS = 2000;
27
+
28
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
29
+
30
+ function sext(value, bits) {
31
+ const sign = 2 ** (bits - 1);
32
+ return value >= sign ? value - 2 * sign : value;
33
+ }
34
+
35
+ class HY3131Minimal {
36
+ /**
37
+ * @param {object} connection - SPI connection (mode 0, MSB first, CS active low).
38
+ * @param {number} [vref=1.2] - Voltage on REFO in volts.
39
+ */
40
+ constructor(connection, vref = 1.2) {
41
+ this._conn = connection;
42
+ this._vref = Number(vref);
43
+ this._flags = 0;
44
+ this._ig = 0;
45
+ this._rg = 0;
46
+ this._dcos = 0;
47
+ }
48
+
49
+ /** Create and initialise a driver instance. */
50
+ static async create(connection, vref = 1.2) {
51
+ const adc = new this(connection, vref);
52
+ await adc.init();
53
+ return adc;
54
+ }
55
+
56
+ /** Run the initialisation sequence (reset, oscillator, reference, AD1 defaults). */
57
+ async init() {
58
+ await this.writeRegister(REG.R37, 0x60);
59
+ await sleep(5);
60
+ await this.writeRegister(REG.R33, 0x28);
61
+ await sleep(10);
62
+ await this.writeRegister(REG.R31, 0xC0);
63
+ await this.writeRegister(REG.R30, 0x00);
64
+ await sleep(5);
65
+ await this.writeRegister(REG.R2F, 0x80);
66
+ await this.writeRegister(REG.R2A, Buffer.alloc(5));
67
+ for (const reg of [REG.R32, REG.R20, 0x21, REG.R34, REG.R35, REG.R36,
68
+ REG.R29, REG.R26, REG.R27, REG.INTE]) {
69
+ await this.writeRegister(reg, 0x00);
70
+ }
71
+ await this.writeRegister(REG.R24, 0xA3);
72
+ await this.writeRegister(REG.R28, 0x11);
73
+ this._ig = 0;
74
+ this._rg = 0;
75
+ this._dcos = 0;
76
+ await this.writeRegister(REG.R25, 0x00);
77
+ await this.writeRegister(REG.R22, 0x07);
78
+ await this.writeRegister(REG.R23, 0x80);
79
+ await this.readRegister(REG.INTF);
80
+ this._flags = 0;
81
+ }
82
+
83
+ /**
84
+ * Read `n` consecutive registers starting at `reg` (auto-increment).
85
+ * @param {number} reg - Start address 0x00–0x37.
86
+ * @param {number} [n=1] - Number of bytes.
87
+ * @returns {Promise<Buffer>}
88
+ */
89
+ async readRegister(reg, n = 1) {
90
+ const rx = await this._conn.writeRead(Buffer.from([((reg & 0x7F) << 1) | 1]), n + 1);
91
+ const out = Buffer.alloc(n);
92
+ for (let i = 0; i < n; i++) out[i] = ((rx[i] << 1) | (rx[i + 1] >> 7)) & 0xFF;
93
+ return out;
94
+ }
95
+
96
+ /**
97
+ * Write a byte or Buffer to `reg` (auto-increment).
98
+ * @param {number} reg
99
+ * @param {number|Buffer} data
100
+ */
101
+ async writeRegister(reg, data) {
102
+ const payload = typeof data === 'number' ? Buffer.from([data]) : Buffer.from(data);
103
+ await this._conn.write(Buffer.concat([Buffer.from([(reg & 0x7F) << 1]), payload]));
104
+ }
105
+
106
+ async _waitFlag(mask) {
107
+ const start = Date.now();
108
+ for (;;) {
109
+ this._flags |= (await this.readRegister(REG.INTF))[0];
110
+ if (this._flags & mask) {
111
+ this._flags &= ~mask;
112
+ return true;
113
+ }
114
+ if (Date.now() - start > TIMEOUT_MS) return false;
115
+ await sleep(1);
116
+ }
117
+ }
118
+
119
+ async _read24(reg) {
120
+ const b = await this.readRegister(reg, 3);
121
+ return b[0] | (b[1] << 8) | (b[2] << 16);
122
+ }
123
+
124
+ /** @returns {Promise<boolean>} True if a new AD1 conversion is pending (latches INTF). */
125
+ async dataReady() {
126
+ this._flags |= (await this.readRegister(REG.INTF))[0];
127
+ return Boolean(this._flags & FLAG.AD1F);
128
+ }
129
+
130
+ /** @returns {Promise<number>} Signed 24-bit AD1 code. */
131
+ async readRaw() {
132
+ if (!(await this._waitFlag(FLAG.AD1F))) throw new Error('HY3131 AD1 timeout');
133
+ return sext(await this._read24(REG.AD1), 24);
134
+ }
135
+
136
+ /** @returns {Promise<number>} AD1 differential input voltage in volts. */
137
+ async readVoltage() {
138
+ const code = await this.readRaw();
139
+ return (code / 8388608 * (this._rg === 0 ? 1 : 0.3333333333) - this._dcos)
140
+ * this._vref / AD1_GAINS[this._ig];
141
+ }
142
+ }
143
+
144
+ class HY3131Full extends HY3131Minimal {
145
+ /** Software-reset the chip and re-apply the Minimal defaults. */
146
+ async reset() { await this.init(); }
147
+
148
+ /**
149
+ * Configure AD1.
150
+ * @param {object} [o]
151
+ * @param {number} [o.osr=7] 0–7 → OSR1 256 … 32768
152
+ * @param {number} [o.chop=0] 0–3 (1x enables the chopper, halves the rate)
153
+ * @param {number} [o.inputGain=0] 0–3 → 0.9, 1.8, 2.7, 3.6
154
+ * @param {number} [o.refGain=0] 0 → 1.0, 1 → 0.333
155
+ * @param {number} [o.offset=0] 0–7 DCOS code
156
+ * @param {number} [o.buffers=0] 4-bit mask: bit3 RH, bit2 RL, bit1 IP, bit0 IN
157
+ */
158
+ async configureAd1({ osr = 7, chop = 0, inputGain = 0, refGain = 0, offset = 0, buffers = 0 } = {}) {
159
+ const r25 = (await this.readRegister(REG.R25))[0];
160
+ await this.writeRegister(REG.R25, (r25 & 0xCF) | ((inputGain & 3) << 4));
161
+ await this.writeRegister(REG.R22, ((offset & 7) << 5) | ((chop & 3) << 3) | (osr & 7));
162
+ await this.writeRegister(REG.R23, 0x80 | ((refGain & 1) << 4) | (buffers & 0x0F));
163
+ this._ig = inputGain & 3;
164
+ this._rg = refGain & 1;
165
+ this._dcos = DCOS[offset & 7];
166
+ await this.readRegister(REG.INTF);
167
+ this._flags = 0;
168
+ }
169
+
170
+ /** Select AD1 inputs: pos 0–15 (SAD1FP), neg 0–7 (SAD1FN), filterR 0–3 (SFT1). */
171
+ async setAd1Input(pos, neg, filterR = 2) {
172
+ await this.writeRegister(REG.R24, ((pos & 15) << 4) | (neg & 7));
173
+ const r33 = (await this.readRegister(REG.R33))[0];
174
+ await this.writeRegister(REG.R33, (r33 & 0xF3) | ((filterR & 3) << 2));
175
+ }
176
+
177
+ /** Select AD1 reference: high 0–6 (SAD1RH), low 0–6 (SAD1RL). */
178
+ async setAd1Reference(high, low) {
179
+ await this.writeRegister(REG.R28, ((high & 7) << 4) | (low & 7));
180
+ }
181
+
182
+ /** @returns {Promise<number>} TCode = (AD1[SAD1I=11] − AD1[SAD1I=10]) / 2. */
183
+ async readTemperatureCode() {
184
+ const r33 = (await this.readRegister(REG.R33))[0];
185
+ const codes = [];
186
+ for (const sel of [2, 3]) {
187
+ await this.writeRegister(REG.R33, (r33 & 0xFC) | sel);
188
+ await this.readRegister(REG.INTF);
189
+ this._flags = 0;
190
+ await this._waitFlag(FLAG.AD1F); // discard settling sample
191
+ codes.push(await this.readRaw());
192
+ }
193
+ await this.writeRegister(REG.R33, r33);
194
+ this._flags = 0;
195
+ return Math.trunc((codes[1] - codes[0]) / 2);
196
+ }
197
+
198
+ /**
199
+ * @param {number} tcode25 - readTemperatureCode() measured at 25 °C.
200
+ * @returns {Promise<number>} Die temperature in °C.
201
+ */
202
+ async readTemperature(tcode25) {
203
+ const tOs = -3.15;
204
+ const g = tcode25 / (25 + 273.15 + tOs);
205
+ return (await this.readTemperatureCode()) / g - 273.15 - tOs;
206
+ }
207
+
208
+ /**
209
+ * Enable AD2 (and AD3 when useAd3).
210
+ * @param {object} [o]
211
+ */
212
+ async enableAd2({ osr = 5, inputGain = 1, refGain = 0, chop = false, clkDiv4 = false,
213
+ source = 0, useAd3 = false } = {}) {
214
+ const r25 = (await this.readRegister(REG.R25))[0];
215
+ await this.writeRegister(REG.R25, (r25 & 0x3F) | ((inputGain & 3) << 6));
216
+ await this.writeRegister(REG.R27, ((source & 3) << 6) | 0x15);
217
+ await this.writeRegister(REG.R26, 0x80 | (chop ? 0x20 : 0) | ((refGain & 1) << 4)
218
+ | (clkDiv4 ? 0x08 : 0) | (osr & 7));
219
+ if (useAd3) {
220
+ await this.writeRegister(REG.R35, ((source & 3) << 6) | (1 << 4) | ((inputGain & 3) << 2));
221
+ await this.writeRegister(REG.R34, 0x80 | (chop ? 0x20 : 0) | ((refGain & 1) << 4) | 0x04);
222
+ } else {
223
+ await this.writeRegister(REG.R34, 0x00);
224
+ }
225
+ await this.readRegister(REG.INTF);
226
+ this._flags = 0;
227
+ }
228
+
229
+ /** @returns {Promise<number>} Signed 19-bit AD2 (or AD3) code. */
230
+ async readAd2Raw() {
231
+ if (!(await this._waitFlag(FLAG.AD2F))) throw new Error('HY3131 AD2 timeout');
232
+ return sext(await this._read24(REG.AD2), 24);
233
+ }
234
+
235
+ /** Enable the low-pass filter; bw 0–7 → OSR4 128 … 16384. */
236
+ async enableLpf(bw = 0) {
237
+ const r29 = (await this.readRegister(REG.R29))[0];
238
+ await this.writeRegister(REG.R29, (r29 & 0x87) | 0x40 | ((bw & 7) << 3));
239
+ }
240
+
241
+ /** @returns {Promise<number>} Signed 19-bit LPF code. */
242
+ async readLpfRaw() {
243
+ if (!(await this._waitFlag(FLAG.LPFF))) throw new Error('HY3131 LPF timeout');
244
+ return sext(await this._read24(REG.LPF), 24);
245
+ }
246
+
247
+ /** Enable the RMS converter (requires AD2 and the LPF). */
248
+ async enableRms() {
249
+ const r29 = (await this.readRegister(REG.R29))[0];
250
+ await this.writeRegister(REG.R29, r29 | 0x80);
251
+ }
252
+
253
+ /** @returns {Promise<number>} sqrt(RMS accumulator) in AD2 LSB units. */
254
+ async readRms() {
255
+ if (!(await this._waitFlag(FLAG.RMSF))) throw new Error('HY3131 RMS timeout');
256
+ const b = await this.readRegister(REG.RMS, 5);
257
+ const value = sext(b[0] + b[1] * 2 ** 8 + b[2] * 2 ** 16 + b[3] * 2 ** 24 + b[4] * 2 ** 32, 40);
258
+ return Math.sqrt(Math.max(value, 0));
259
+ }
260
+
261
+ /** Enable peak hold; source 0–3 → AD2, AD1<23:5>, LPF, LPF. Re-enabling resets min/max. */
262
+ async enablePeakHold(source = 0) {
263
+ const r29 = (await this.readRegister(REG.R29))[0];
264
+ await this.writeRegister(REG.R29, r29 & ~0x07);
265
+ await this.writeRegister(REG.R29, (r29 & ~0x07) | 0x04 | (source & 3));
266
+ }
267
+
268
+ /** @returns {Promise<{min:number,max:number}>} Signed 19-bit peak-hold codes. */
269
+ async readPeak() {
270
+ return {
271
+ min: sext(await this._read24(REG.PKHMIN), 24),
272
+ max: sext(await this._read24(REG.PKHMAX), 24),
273
+ };
274
+ }
275
+
276
+ /** Start the frequency counter; gatePreload 0–0xFFFF goes to CTA<23:8>. */
277
+ async startCounter(gatePreload = 0) {
278
+ const r20 = (await this.readRegister(REG.R20))[0];
279
+ await this.writeRegister(REG.R20, r20 & ~0x02);
280
+ await this.writeRegister(REG.CTA + 1, Buffer.from([gatePreload & 0xFF, (gatePreload >> 8) & 0xFF]));
281
+ await this.readRegister(REG.INTF);
282
+ this._flags = 0;
283
+ await this.writeRegister(REG.R20, r20 | 0x0A);
284
+ }
285
+
286
+ /** @returns {Promise<{cta:number,ctb:number,ctc:number,overflow:boolean}>} */
287
+ async readCounter() {
288
+ if (!(await this._waitFlag(FLAG.CTF))) throw new Error('HY3131 counter timeout');
289
+ const overflow = Boolean((await this.readRegister(REG.CTSTA))[0] & 0x01);
290
+ return {
291
+ cta: await this._read24(REG.CTA),
292
+ ctb: await this._read24(REG.CTB),
293
+ ctc: await this._read24(REG.CTC),
294
+ overflow,
295
+ };
296
+ }
297
+
298
+ /** @returns {Promise<number>} INTF (clears it); latched flags included and cleared. */
299
+ async readFlags() {
300
+ const flags = this._flags | (await this.readRegister(REG.INTF))[0];
301
+ this._flags = 0;
302
+ return flags;
303
+ }
304
+
305
+ /** Write INTE (bits: RMSIE 4, LPFIE 3, AD1IE 2, AD2IE 1, CTIE 0). */
306
+ async enableInterrupts(mask) {
307
+ await this.writeRegister(REG.INTE, mask & 0x1F);
308
+ }
309
+ }
310
+
311
+ module.exports = { HY3131Minimal, HY3131Full, FLAG };