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
@@ -84,12 +84,12 @@ async function readRegChannel(conn, reg, channel, nBytes) {
84
84
  return value;
85
85
  }
86
86
 
87
- async function configureClock(conn, mclkHz, outputRateHz) {
87
+ async function configureClock(conn, mclkHz, outputRateHz, channel) {
88
88
  const clkBit = (mclkHz >= MCLK_2_4576MHZ) ? 0x04 : 0x00;
89
89
  const clkDivBit = (mclkHz === MCLK_2MHZ || mclkHz === MCLK_4_9152MHZ) ? 0x08 : 0x00;
90
90
  const rates = (mclkHz >= MCLK_2_4576MHZ) ? FS_RATES_2_4MHZ : FS_RATES_1MHZ;
91
91
  const fsIdx = rates.indexOf(outputRateHz);
92
- await writeRegChannel(conn, REG_CLOCK, clkDivBit | clkBit | fsIdx, CH1, 1);
92
+ await writeRegChannel(conn, REG_CLOCK, clkDivBit | clkBit | fsIdx, channel, 1);
93
93
  }
94
94
 
95
95
  function codeToVoltage(code, gain, bipolar, vref) {
@@ -120,6 +120,9 @@ class AD7705Minimal {
120
120
  this._gain = 1;
121
121
  this._bipolar = true;
122
122
  this._buffered = false;
123
+ this._gain2 = 1;
124
+ this._bipolar2 = true;
125
+ this._buffered2 = false;
123
126
 
124
127
  if (resetPin) {
125
128
  resetPin.set(false);
@@ -127,11 +130,17 @@ class AD7705Minimal {
127
130
  }
128
131
 
129
132
  const defaultRate = (mclkHz >= MCLK_2_4576MHZ) ? FS_RATES_2_4MHZ[0] : FS_RATES_1MHZ[0];
130
- configureClock(connection, mclkHz, defaultRate);
133
+ configureClock(connection, mclkHz, defaultRate, CH1);
131
134
 
132
135
  const setup = MODE_SELF_CAL | GAIN_BITS[0] | BIPOLAR | UNBUFFERED | FSYNC_RUN;
133
136
  writeRegChannel(connection, REG_SETUP, setup, CH1, 1);
134
- return waitDrdy(connection);
137
+ // JS constructors can't be async, so this fires unawaited -- same
138
+ // documented fire-and-forget convention as MFRC522Minimal. A stray
139
+ // `return waitDrdy(connection)` here previously made `new
140
+ // AD7705Minimal(...)` evaluate to a Promise instead of an instance
141
+ // (constructors that explicitly return an object short-circuit
142
+ // `new`'s result) -- no other driver in this codebase does that.
143
+ waitDrdy(connection);
135
144
  }
136
145
 
137
146
  /**
@@ -157,6 +166,16 @@ class AD7705Minimal {
157
166
  * AD7705 full driver — adds per-channel configuration, calibration, and power control.
158
167
  */
159
168
  class AD7705Full extends AD7705Minimal {
169
+ /**
170
+ * @param {number} channel - 1 or 2.
171
+ * @returns {{gain: number, bipolar: boolean, buffered: boolean}} The gain/bipolar/buffered state last set by configure() for that channel.
172
+ */
173
+ _channelState(channel) {
174
+ return channel === 1
175
+ ? { gain: this._gain, bipolar: this._bipolar, buffered: this._buffered }
176
+ : { gain: this._gain2, bipolar: this._bipolar2, buffered: this._buffered2 };
177
+ }
178
+
160
179
  /**
161
180
  * Write the Setup and Clock Registers for the given channel.
162
181
  * Does not calibrate — call selfCalibrate() (or one of the
@@ -181,7 +200,7 @@ class AD7705Full extends AD7705Minimal {
181
200
  }
182
201
  const ch = (channel === 1) ? CH1 : CH2;
183
202
 
184
- await configureClock(this._conn, this._mclkHz, outputRateHz);
203
+ await configureClock(this._conn, this._mclkHz, outputRateHz, ch);
185
204
 
186
205
  const buBit = bipolar ? BIPOLAR : UNIPOLAR;
187
206
  const bufBit = buffered ? BUFFERED : UNBUFFERED;
@@ -192,6 +211,10 @@ class AD7705Full extends AD7705Minimal {
192
211
  this._gain = gain;
193
212
  this._bipolar = bipolar;
194
213
  this._buffered = buffered;
214
+ } else {
215
+ this._gain2 = gain;
216
+ this._bipolar2 = bipolar;
217
+ this._buffered2 = buffered;
195
218
  }
196
219
  }
197
220
 
@@ -216,7 +239,8 @@ class AD7705Full extends AD7705Minimal {
216
239
  */
217
240
  async readVoltageChannel(channel) {
218
241
  const code = await this.readRawChannel(channel);
219
- return codeToVoltage(code, this._gain, this._bipolar, this._vref);
242
+ const { gain, bipolar } = this._channelState(channel);
243
+ return codeToVoltage(code, gain, bipolar, this._vref);
220
244
  }
221
245
 
222
246
  /**
@@ -228,7 +252,8 @@ class AD7705Full extends AD7705Minimal {
228
252
  throw new Error('channel must be 1 or 2');
229
253
  }
230
254
  const ch = (channel === 1) ? CH1 : CH2;
231
- const setup = MODE_SELF_CAL | GAIN_BITS[GAIN_TO_IDX[this._gain]] | (this._bipolar ? BIPOLAR : UNIPOLAR) | (this._buffered ? BUFFERED : UNBUFFERED) | FSYNC_RUN;
255
+ const { gain, bipolar, buffered } = this._channelState(channel);
256
+ const setup = MODE_SELF_CAL | GAIN_BITS[GAIN_TO_IDX[gain]] | (bipolar ? BIPOLAR : UNIPOLAR) | (buffered ? BUFFERED : UNBUFFERED) | FSYNC_RUN;
232
257
  await writeRegChannel(this._conn, REG_SETUP, setup, ch, 1);
233
258
  await waitDrdy(this._conn);
234
259
  }
@@ -242,7 +267,8 @@ class AD7705Full extends AD7705Minimal {
242
267
  throw new Error('channel must be 1 or 2');
243
268
  }
244
269
  const ch = (channel === 1) ? CH1 : CH2;
245
- const setup = MODE_ZERO_SYS | GAIN_BITS[GAIN_TO_IDX[this._gain]] | (this._bipolar ? BIPOLAR : UNIPOLAR) | (this._buffered ? BUFFERED : UNBUFFERED) | FSYNC_RUN;
270
+ const { gain, bipolar, buffered } = this._channelState(channel);
271
+ const setup = MODE_ZERO_SYS | GAIN_BITS[GAIN_TO_IDX[gain]] | (bipolar ? BIPOLAR : UNIPOLAR) | (buffered ? BUFFERED : UNBUFFERED) | FSYNC_RUN;
246
272
  await writeRegChannel(this._conn, REG_SETUP, setup, ch, 1);
247
273
  await waitDrdy(this._conn);
248
274
  }
@@ -256,7 +282,8 @@ class AD7705Full extends AD7705Minimal {
256
282
  throw new Error('channel must be 1 or 2');
257
283
  }
258
284
  const ch = (channel === 1) ? CH1 : CH2;
259
- const setup = MODE_FULL_SYS | GAIN_BITS[GAIN_TO_IDX[this._gain]] | (this._bipolar ? BIPOLAR : UNIPOLAR) | (this._buffered ? BUFFERED : UNBUFFERED) | FSYNC_RUN;
285
+ const { gain, bipolar, buffered } = this._channelState(channel);
286
+ const setup = MODE_FULL_SYS | GAIN_BITS[GAIN_TO_IDX[gain]] | (bipolar ? BIPOLAR : UNIPOLAR) | (buffered ? BUFFERED : UNBUFFERED) | FSYNC_RUN;
260
287
  await writeRegChannel(this._conn, REG_SETUP, setup, ch, 1);
261
288
  await waitDrdy(this._conn);
262
289
  }
@@ -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 };