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,357 @@
1
+ 'use strict';
2
+
3
+ const _CMD_CONFIG = 0x80;
4
+ const _CFG_VOLATILE = 0x04;
5
+ const _CFG_ZC = 0x02;
6
+ const _CFG_OPTION2 = 0x01;
7
+ const _CFG_MASK = 0x07;
8
+ const _CFG_DEFAULT = _CFG_VOLATILE | _CFG_ZC; // volatile, zero-crossing, Option 1
9
+ const _MUTE_DB = 80.0;
10
+
11
+ const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
12
+
13
+ function _checkChannel(channel) {
14
+ if (channel !== 0 && channel !== 1) throw new RangeError('channel must be 0 or 1');
15
+ return channel;
16
+ }
17
+
18
+ function _mutePosition(option) {
19
+ return option === 1 ? 63 : 33;
20
+ }
21
+
22
+ function _positionToDb(position, option) {
23
+ if (position >= _mutePosition(option)) return _MUTE_DB;
24
+ if (option === 1 || position <= 12) return position;
25
+ if (position <= 24) return 12 + 2 * (position - 12);
26
+ return 36 + 3 * (position - 24);
27
+ }
28
+
29
+ function _nearestPosition(db, option) {
30
+ if (db <= 0) return 0;
31
+ if (option === 1) return Math.min(Math.floor(db + 0.5), 62);
32
+ let best = 0;
33
+ let bestErr = db;
34
+ for (let p = 1; p <= 32; p++) {
35
+ const err = Math.abs(_positionToDb(p, 2) - db);
36
+ if (err < bestErr) { best = p; bestErr = err; }
37
+ }
38
+ return best;
39
+ }
40
+
41
+ /**
42
+ * DS1881 dual nonvolatile audio log-taper digital potentiometer (Maxim) —
43
+ * minimal interface.
44
+ *
45
+ * Two 45 kΩ potentiometers whose wiper positions are steps in an attenuation
46
+ * table. Option 1 gives 64 positions (1 dB per step, 0 to −62 dB, position 63
47
+ * = mute); Option 2 (DS1808 compatible) gives 34 positions (12 × 1 dB,
48
+ * 12 × 2 dB, 8 × 3 dB, position 33 = mute). The I²C address is 0x28
49
+ * (A2 = A1 = A0 = 0) to 0x2F.
50
+ *
51
+ * The chip has no register map: a write is one or more command bytes
52
+ * (`00pppppp` wiper 0, `01pppppp` wiper 1, `10xxxVZC` configuration) and a
53
+ * read returns wiper 0, wiper 1 and the configuration register round-robin.
54
+ * The driver therefore uses the plain Connection `write()` / `read()` calls.
55
+ * Wipers and configuration live in EEPROM when V/NV = 0 (50 000 writes); the
56
+ * drivers default to volatile operation. The CE pin is active low and is left
57
+ * to the Connection `enPin` (`enActiveHigh = false`).
58
+ */
59
+ class DS1881Minimal {
60
+ /**
61
+ * The constructor makes no bus access; call {@link DS1881Minimal#init} once after power-up.
62
+ * @param {import('../../connection/connection').Connection} connection - I²C connection (0x28–0x2F).
63
+ */
64
+ constructor(connection) {
65
+ this._conn = connection;
66
+ this._config = _CFG_DEFAULT;
67
+ this._option = 1;
68
+ }
69
+
70
+ /**
71
+ * Run the initialization sequence: wait the 1 ms power-up time, read the
72
+ * three bytes and rewrite the configuration (volatile, zero-crossing on,
73
+ * Option 1) only if it differs, waiting for the EEPROM write that this
74
+ * triggers. The wipers are not touched.
75
+ * @returns {Promise<void>}
76
+ */
77
+ async init() {
78
+ await _sleep(1);
79
+ const raw = await this._conn.read(3);
80
+ if ((raw[2] & _CFG_MASK) !== _CFG_DEFAULT) {
81
+ await this._conn.write(Buffer.from([_CMD_CONFIG | _CFG_DEFAULT]));
82
+ await this._waitReady(0.05);
83
+ }
84
+ this._config = _CFG_DEFAULT;
85
+ this._option = 1;
86
+ }
87
+
88
+ async _waitReady(timeoutS) {
89
+ const attempts = Math.floor(timeoutS / 0.001) + 1;
90
+ for (let i = 0; i < attempts; i++) {
91
+ try {
92
+ await this._conn.read(1);
93
+ return;
94
+ } catch (e) {
95
+ await _sleep(1);
96
+ }
97
+ }
98
+ throw new Error('DS1881 busy: EEPROM write did not finish');
99
+ }
100
+
101
+ async _writeCommands(bytes) {
102
+ await this._conn.write(Buffer.from(bytes));
103
+ if (!(this._config & _CFG_VOLATILE)) await this._waitReady(0.05);
104
+ }
105
+
106
+ async _writePosition(channel, position) {
107
+ _checkChannel(channel);
108
+ const mute = _mutePosition(this._option);
109
+ position = Math.trunc(position);
110
+ if (position < 0) position = 0;
111
+ else if (position > mute) position = mute;
112
+ await this._writeCommands([(channel << 6) | position]);
113
+ }
114
+
115
+ async _readAll() {
116
+ const raw = await this._conn.read(3);
117
+ return [raw[0] & 0x3F, raw[1] & 0x3F, raw[2]];
118
+ }
119
+
120
+ _dbToPos(db) {
121
+ return _nearestPosition(db, this._option);
122
+ }
123
+
124
+ /**
125
+ * Set a channel's attenuation to the nearest position.
126
+ * @param {number} channel - 0 or 1.
127
+ * @param {number} db - Attenuation in dB (positive = quieter); rounded to the nearest step, clamped to 0…62 dB (Option 1).
128
+ * @returns {Promise<void>}
129
+ */
130
+ async setAttenuation(channel, db) {
131
+ _checkChannel(channel);
132
+ await this._writePosition(channel, this._dbToPos(db));
133
+ }
134
+
135
+ /**
136
+ * Read a channel's attenuation.
137
+ * @param {number} channel - 0 or 1.
138
+ * @returns {Promise<number>} Attenuation in dB (80 for mute).
139
+ */
140
+ async getAttenuation(channel) {
141
+ return _positionToDb(await this.getPosition(channel), this._option);
142
+ }
143
+
144
+ /**
145
+ * Mute a channel (position 63 in Option 1).
146
+ * @param {number} channel - 0 or 1.
147
+ * @returns {Promise<void>}
148
+ */
149
+ async mute(channel) {
150
+ await this._writePosition(channel, _mutePosition(this._option));
151
+ }
152
+
153
+ /**
154
+ * Write a raw wiper position.
155
+ * @param {number} channel - 0 or 1.
156
+ * @param {number} position - 0–63 (63 = mute); clamped.
157
+ * @returns {Promise<void>}
158
+ */
159
+ async setPosition(channel, position) {
160
+ await this._writePosition(channel, position);
161
+ }
162
+
163
+ /**
164
+ * Read a wiper position with the three-byte round-robin read.
165
+ * @param {number} channel - 0 or 1.
166
+ * @returns {Promise<number>} Position (low 6 bits of the read-back byte).
167
+ */
168
+ async getPosition(channel) {
169
+ _checkChannel(channel);
170
+ return (await this._readAll())[channel];
171
+ }
172
+ }
173
+
174
+ DS1881Minimal.I2C_ADDRESS = 0x28;
175
+
176
+ /**
177
+ * DS1881 full interface — extends Minimal with stereo writes, Option 2,
178
+ * zero-crossing and EEPROM control, and table helpers.
179
+ *
180
+ * The driver caches the configuration (volatile, zero-crossing, option);
181
+ * `init()` and `getConfig()` refresh it from the chip.
182
+ */
183
+ class DS1881Full extends DS1881Minimal {
184
+ _dbToPos(db) {
185
+ if (db >= 70) return _mutePosition(this._option);
186
+ return _nearestPosition(db, this._option);
187
+ }
188
+
189
+ /**
190
+ * Write both wipers in one transaction (no STOP between them).
191
+ * @param {number} position0 - Position for channel 0 (clamped).
192
+ * @param {number} position1 - Position for channel 1 (clamped).
193
+ * @returns {Promise<void>}
194
+ */
195
+ async setBoth(position0, position1) {
196
+ const mute = _mutePosition(this._option);
197
+ const data = [position0, position1].map((p, channel) => {
198
+ p = Math.max(0, Math.min(Math.trunc(p), mute));
199
+ return (channel << 6) | p;
200
+ });
201
+ await this._writeCommands(data);
202
+ }
203
+
204
+ /**
205
+ * Set both channels' attenuation (stereo volume).
206
+ * @param {number} db0 - Attenuation of channel 0 in dB.
207
+ * @param {number} [db1] - Attenuation of channel 1 in dB; defaults to `db0`.
208
+ * @returns {Promise<void>}
209
+ */
210
+ async setAttenuationBoth(db0, db1) {
211
+ if (db1 === undefined || db1 === null) db1 = db0;
212
+ await this.setBoth(this._dbToPos(db0), this._dbToPos(db1));
213
+ }
214
+
215
+ /**
216
+ * Write the configuration byte if it differs from the cached value. The
217
+ * configuration is stored in EEPROM, so a write waits for acknowledge
218
+ * polling. The wipers are not rewritten — positions keep their numeric
219
+ * value when the option changes.
220
+ * @param {number} [option=1] - 1 (63 positions + mute, 1 dB steps) or 2 (33 positions + mute, DS1808 compatible).
221
+ * @param {boolean} [zeroCrossing=true] - Enable zero-crossing detection.
222
+ * @param {boolean} [nonvolatile=false] - Store wiper writes in EEPROM and restore them at power-up (50 000 writes).
223
+ * @returns {Promise<void>}
224
+ */
225
+ async configure(option = 1, zeroCrossing = true, nonvolatile = false) {
226
+ if (option !== 1 && option !== 2) throw new RangeError('option must be 1 or 2');
227
+ let config = 0;
228
+ if (!nonvolatile) config |= _CFG_VOLATILE;
229
+ if (zeroCrossing) config |= _CFG_ZC;
230
+ if (option === 2) config |= _CFG_OPTION2;
231
+ if (config !== this._config) {
232
+ await this._conn.write(Buffer.from([_CMD_CONFIG | config]));
233
+ await this._waitReady(0.05);
234
+ }
235
+ this._config = config;
236
+ this._option = option;
237
+ }
238
+
239
+ /**
240
+ * Read the configuration register (fresh read; refreshes the cache).
241
+ * @returns {Promise<{option: number, zeroCrossing: boolean, nonvolatile: boolean, raw: number}>}
242
+ */
243
+ async getConfig() {
244
+ const raw = (await this._readAll())[2];
245
+ this._config = raw & _CFG_MASK;
246
+ this._option = (raw & _CFG_OPTION2) ? 2 : 1;
247
+ return {
248
+ option: this._option,
249
+ zeroCrossing: Boolean(raw & _CFG_ZC),
250
+ nonvolatile: !(raw & _CFG_VOLATILE),
251
+ raw,
252
+ };
253
+ }
254
+
255
+ /**
256
+ * Switch between Option 1 and 2 (positions are not converted).
257
+ * @param {number} option - 1 or 2.
258
+ * @returns {Promise<void>}
259
+ */
260
+ async setOption(option) {
261
+ await this.configure(option, Boolean(this._config & _CFG_ZC), !(this._config & _CFG_VOLATILE));
262
+ }
263
+
264
+ /**
265
+ * Enable or disable zero-crossing detection.
266
+ * @param {boolean} enabled - true to enable.
267
+ * @returns {Promise<void>}
268
+ */
269
+ async setZeroCrossing(enabled) {
270
+ await this.configure(this._option, enabled, !(this._config & _CFG_VOLATILE));
271
+ }
272
+
273
+ /**
274
+ * Select where wiper writes go (V/NV bit).
275
+ * @param {boolean} enabled - true: subsequent wiper writes are stored in EEPROM and restored at power-up (50 000 writes).
276
+ * @returns {Promise<void>}
277
+ */
278
+ async setNonvolatile(enabled) {
279
+ await this.configure(this._option, Boolean(this._config & _CFG_ZC), enabled);
280
+ }
281
+
282
+ /**
283
+ * Persist the current wiper positions to EEPROM: read both positions,
284
+ * switch to nonvolatile mode if needed, rewrite both wiper bytes in one
285
+ * transaction, wait by acknowledge polling and restore volatile mode if
286
+ * it was active. The EEPROM is rated for only 50 000 writes.
287
+ * @returns {Promise<void>}
288
+ */
289
+ async store() {
290
+ const [position0, position1] = await this._readAll();
291
+ const wasNv = !(this._config & _CFG_VOLATILE);
292
+ const zc = Boolean(this._config & _CFG_ZC);
293
+ const option = this._option;
294
+ if (!wasNv) await this.configure(option, zc, true);
295
+ await this._writeCommands([position0, 0x40 | position1]);
296
+ if (!wasNv) await this.configure(option, zc, false);
297
+ }
298
+
299
+ /**
300
+ * Acknowledge-poll until the device answers (it NACKs while an EEPROM
301
+ * write is in progress).
302
+ * @param {number} [timeoutS=0.05] - Maximum time to wait in seconds.
303
+ * @returns {Promise<void>} Rejects if the device is still busy after the timeout.
304
+ */
305
+ async waitReady(timeoutS = 0.05) {
306
+ await this._waitReady(timeoutS);
307
+ }
308
+
309
+ /**
310
+ * Read wiper 0, wiper 1 and the configuration byte in one transaction.
311
+ * @returns {Promise<number[]>} `[position0, position1, configByte]`.
312
+ */
313
+ async readAll() {
314
+ return this._readAll();
315
+ }
316
+
317
+ /**
318
+ * Attenuation of a position in the current option.
319
+ * @param {number} position - Wiper position.
320
+ * @returns {number} dB (80 for mute and above).
321
+ */
322
+ positionToDb(position) {
323
+ return _positionToDb(Math.trunc(position), this._option);
324
+ }
325
+
326
+ /**
327
+ * Nearest position for an attenuation in the current option.
328
+ * @param {number} db - Attenuation in dB; ≥ 70 dB maps to mute, negative to 0.
329
+ * @returns {number} Wiper position.
330
+ */
331
+ dbToPosition(db) {
332
+ return this._dbToPos(db);
333
+ }
334
+
335
+ /**
336
+ * Last non-mute position of the current option.
337
+ * @returns {number} 62 (Option 1) or 32 (Option 2).
338
+ */
339
+ maxPosition() {
340
+ return _mutePosition(this._option) - 1;
341
+ }
342
+
343
+ /**
344
+ * Volatile, zero-crossing on, Option 1 and both wipers to mute, sent as
345
+ * one transaction (configuration byte, then both wiper bytes); waits for
346
+ * the configuration EEPROM write.
347
+ * @returns {Promise<void>}
348
+ */
349
+ async reset() {
350
+ await this._conn.write(Buffer.from([_CMD_CONFIG | _CFG_DEFAULT, 0x3F, 0x7F]));
351
+ this._config = _CFG_DEFAULT;
352
+ this._option = 1;
353
+ await this._waitReady(0.05);
354
+ }
355
+ }
356
+
357
+ module.exports = { DS1881Minimal, DS1881Full };
@@ -0,0 +1,241 @@
1
+ 'use strict';
2
+
3
+ const REG_WR = 0x00;
4
+ const REG_CR = 0x02;
5
+ const CR_MODE_SRAM = 0x80; // MODE = 1: writes to 0x00 update WR only
6
+ const CR_MODE_EEPROM = 0x00; // MODE = 0: writes to 0x00 update WR and IVR
7
+ const MAX_CODE = 127;
8
+ const POWER_UP_MS = 3; // tD: power-up recall of the IVR into WR
9
+ const POLL_MS = 1;
10
+ const R_TOTAL = 10000.0; // nominal end-to-end resistance, ±20 %
11
+
12
+ const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
13
+
14
+ function _clampCode(code) {
15
+ code = Math.trunc(code);
16
+ if (code < 0) return 0;
17
+ if (code > MAX_CODE) return MAX_CODE;
18
+ return code;
19
+ }
20
+
21
+ /**
22
+ * DS3502 7-bit nonvolatile high-voltage I²C digital potentiometer (Maxim) —
23
+ * minimal interface.
24
+ *
25
+ * A single 128-position potentiometer (10 kΩ end to end, ±20 %) whose
26
+ * terminals RH / RL / RW accept up to 15.5 V (V+ = 4.5–15.5 V) while the logic
27
+ * runs from VCC = 2.5–5.5 V. The wiper position lives in the Wiper Register
28
+ * (WR, SRAM); the Initial Value Register (IVR, EEPROM) holds the power-up
29
+ * position (factory default 0x40). The I²C address is 0x28 (A1 = A0 = GND) to
30
+ * 0x2B (A1 = A0 = VCC).
31
+ *
32
+ * Two registers exist: 0x00 is WR (and, depending on the MODE bit of CR, also
33
+ * IVR; reads always return WR) and 0x02 is the Control Register whose bit 7
34
+ * selects MODE (1 = writes to 0x00 only change WR). The driver runs with
35
+ * MODE = 1 so routine wiper writes never wear the EEPROM.
36
+ */
37
+ class DS3502Minimal {
38
+ /**
39
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection (1-byte register address).
40
+ * @param {number} [vHigh=1.0] - Voltage applied to RH, in volts.
41
+ * @param {number} [vLow=0.0] - Voltage applied to RL, in volts.
42
+ */
43
+ constructor(connection, vHigh = 1.0, vLow = 0.0) {
44
+ this._conn = connection;
45
+ this._vHigh = vHigh;
46
+ this._vLow = vLow;
47
+ this._code = 0;
48
+ }
49
+
50
+ /** 7-bit I²C address with A1 = A0 = GND. */
51
+ static get I2C_ADDRESS() { return 0x28; }
52
+
53
+ async _readReg(reg) {
54
+ return (await this._conn.readReg(reg, 1))[0];
55
+ }
56
+
57
+ async _writeReg(reg, value) {
58
+ await this._conn.writeReg(reg, value & 0xFF);
59
+ }
60
+
61
+ async _writeCode(code) {
62
+ code = _clampCode(code);
63
+ await this._writeReg(REG_WR, code);
64
+ this._code = code;
65
+ }
66
+
67
+ async _readCode() {
68
+ this._code = (await this._readReg(REG_WR)) & MAX_CODE;
69
+ return this._code;
70
+ }
71
+
72
+ _codeForVoltage(volts) {
73
+ const span = this._vHigh - this._vLow;
74
+ if (span === 0) return 0;
75
+ return _clampCode(Math.round((volts - this._vLow) / span * MAX_CODE));
76
+ }
77
+
78
+ /**
79
+ * Run the initialization sequence: write CR ← 0x80 (MODE = 1), retrying once
80
+ * after 3 ms if the chip NACKs because it is still in its power-up recall,
81
+ * then read WR. The wiper is left where the IVR put it.
82
+ * @returns {Promise<void>}
83
+ */
84
+ async init() {
85
+ try {
86
+ await this._writeReg(REG_CR, CR_MODE_SRAM);
87
+ } catch (e) {
88
+ await sleep(POWER_UP_MS);
89
+ await this._writeReg(REG_CR, CR_MODE_SRAM);
90
+ }
91
+ await this._readCode();
92
+ }
93
+
94
+ /**
95
+ * Set the wiper position (volatile, SRAM only).
96
+ * @param {number} code - Wiper position 0–127 (clamped); 0 = at RL, 127 = at RH.
97
+ * @returns {Promise<void>}
98
+ */
99
+ async setWiper(code) {
100
+ await this._writeCode(code);
101
+ }
102
+
103
+ /**
104
+ * Read back the wiper position (WR).
105
+ * @returns {Promise<number>} Wiper position 0–127.
106
+ */
107
+ async getWiper() {
108
+ return this._readCode();
109
+ }
110
+
111
+ /**
112
+ * Set the unloaded RW output voltage to the nearest reachable code.
113
+ * @param {number} volts - Target voltage in volts (clamped between vLow and vHigh).
114
+ * @returns {Promise<void>}
115
+ */
116
+ async setVoltage(volts) {
117
+ await this._writeCode(this._codeForVoltage(volts));
118
+ }
119
+
120
+ /**
121
+ * Read the unloaded RW voltage for the current wiper code.
122
+ * @returns {Promise<number>} Voltage in volts, `vLow + (vHigh - vLow) * code / 127`.
123
+ */
124
+ async getVoltage() {
125
+ return this._vLow + (this._vHigh - this._vLow) * (await this._readCode()) / MAX_CODE;
126
+ }
127
+ }
128
+
129
+ /**
130
+ * DS3502 full interface — extends Minimal with ratio / resistance helpers,
131
+ * increment, EEPROM storage of the power-up value, MODE control and
132
+ * acknowledge polling.
133
+ */
134
+ class DS3502Full extends DS3502Minimal {
135
+ /**
136
+ * Set the wiper by divider ratio V(RW−RL) / V(RH−RL).
137
+ * @param {number} ratio - 0.0–1.0; the code is `round(ratio * 127)`.
138
+ * @returns {Promise<void>}
139
+ */
140
+ async setRatio(ratio) {
141
+ await this._writeCode(Math.round(ratio * MAX_CODE));
142
+ }
143
+
144
+ /**
145
+ * Read the divider ratio.
146
+ * @returns {Promise<number>} `code / 127`.
147
+ */
148
+ async getRatio() {
149
+ return (await this._readCode()) / MAX_CODE;
150
+ }
151
+
152
+ /**
153
+ * Nominal wiper resistance for the current code (RTOTAL = 10 kΩ ±20 %).
154
+ * @param {string} [terminal='L'] - 'L' for wiper-to-RL, 'H' for wiper-to-RH.
155
+ * @returns {Promise<number>} Resistance in ohms (approximate; excludes the wiper resistance).
156
+ */
157
+ async getResistance(terminal = 'L') {
158
+ if (terminal !== 'L' && terminal !== 'H') throw new RangeError("terminal must be 'L' or 'H'");
159
+ const code = await this._readCode();
160
+ return terminal === 'L' ? code / MAX_CODE * R_TOTAL : (MAX_CODE - code) / MAX_CODE * R_TOTAL;
161
+ }
162
+
163
+ /**
164
+ * Move the wiper relative to its current position (SRAM only).
165
+ * @param {number} [steps=1] - Signed step count (negative to decrement).
166
+ * @returns {Promise<number>} The new wiper code, clamped to 0–127.
167
+ */
168
+ async increment(steps = 1) {
169
+ const code = _clampCode((await this._readCode()) + Math.trunc(steps));
170
+ await this._writeCode(code);
171
+ return code;
172
+ }
173
+
174
+ /**
175
+ * Write the Initial Value Register (EEPROM power-up position). Sets MODE = 0,
176
+ * writes WR / IVR, waits for the EEPROM write (acknowledge polling, ≤ 20 ms)
177
+ * and restores MODE = 1, also on error. The wiper moves to `code`. Each call
178
+ * counts toward the EEPROM endurance (50 000 writes at 70 °C, 200 000 at 25 °C).
179
+ * @param {number} [code] - Position 0–127 to store; default the current wiper position.
180
+ * @returns {Promise<void>}
181
+ */
182
+ async storeWiper(code) {
183
+ code = code === undefined || code === null ? await this._readCode() : _clampCode(code);
184
+ await this._writeReg(REG_CR, CR_MODE_EEPROM);
185
+ try {
186
+ await this._writeReg(REG_WR, code);
187
+ this._code = code;
188
+ await this.waitReady();
189
+ } finally {
190
+ await this._writeReg(REG_CR, CR_MODE_SRAM);
191
+ }
192
+ }
193
+
194
+ /**
195
+ * Select what a write to the wiper register updates (advanced; storeWiper handles it).
196
+ * @param {boolean} eeprom - true for MODE = 0 (writes also program the IVR), false for MODE = 1 (SRAM only).
197
+ * @returns {Promise<void>}
198
+ */
199
+ async setMode(eeprom) {
200
+ await this._writeReg(REG_CR, eeprom ? CR_MODE_EEPROM : CR_MODE_SRAM);
201
+ }
202
+
203
+ /**
204
+ * Read the MODE bit of the Control Register.
205
+ * @returns {Promise<boolean>} true if MODE = 0 (writes reach the EEPROM).
206
+ */
207
+ async getMode() {
208
+ return ((await this._readReg(REG_CR)) & CR_MODE_SRAM) === 0;
209
+ }
210
+
211
+ /**
212
+ * Acknowledge polling: repeat a read of CR until the chip ACKs.
213
+ * @param {number} [timeoutMs=30] - Maximum time to wait, in milliseconds.
214
+ * @returns {Promise<void>}
215
+ * @throws {Error} if the chip still NACKs after `timeoutMs`.
216
+ */
217
+ async waitReady(timeoutMs = 30) {
218
+ const attempts = Math.floor(timeoutMs / POLL_MS) + 1;
219
+ for (let i = 0; i < attempts; i++) {
220
+ try {
221
+ await this._readReg(REG_CR);
222
+ return;
223
+ } catch (e) {
224
+ await sleep(POLL_MS);
225
+ }
226
+ }
227
+ throw new Error('DS3502 busy: EEPROM write did not finish');
228
+ }
229
+
230
+ /**
231
+ * Change the RH / RL voltages used by the conversions (no I²C traffic).
232
+ * @param {number} vHigh - Voltage applied to RH, in volts.
233
+ * @param {number} [vLow=0.0] - Voltage applied to RL, in volts.
234
+ */
235
+ setEndpoints(vHigh, vLow = 0.0) {
236
+ this._vHigh = vHigh;
237
+ this._vLow = vLow;
238
+ }
239
+ }
240
+
241
+ module.exports = { DS3502Minimal, DS3502Full };