periph 1.3.3 → 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 (150) hide show
  1. package/README.md +36 -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/es8311/complete.js +80 -0
  24. package/examples/adc_dac/es8311/demo.js +59 -0
  25. package/examples/adc_dac/es8311/minimal.js +18 -0
  26. package/examples/adc_dac/mcp3422/complete.js +48 -0
  27. package/examples/adc_dac/mcp3422/demo.js +49 -0
  28. package/examples/adc_dac/mcp3422/minimal.js +25 -0
  29. package/examples/adc_dac/mcp4xxx/complete.js +97 -0
  30. package/examples/adc_dac/mcp4xxx/demo.js +76 -0
  31. package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
  32. package/examples/display/sh1106/complete.js +101 -0
  33. package/examples/display/sh1106/demo.js +68 -0
  34. package/examples/display/sh1106/minimal.js +25 -0
  35. package/examples/display/ssd1305/complete.js +129 -0
  36. package/examples/display/ssd1305/demo.js +71 -0
  37. package/examples/display/ssd1305/minimal.js +25 -0
  38. package/examples/display/ssd1306/complete.js +115 -0
  39. package/examples/display/ssd1306/demo.js +70 -0
  40. package/examples/display/ssd1306/minimal.js +25 -0
  41. package/examples/environmental/am2315/complete.js +34 -0
  42. package/examples/environmental/am2315/demo.js +46 -0
  43. package/examples/environmental/am2315/minimal.js +21 -0
  44. package/examples/humidity/htu21df/complete.js +49 -0
  45. package/examples/humidity/htu21df/demo.js +50 -0
  46. package/examples/humidity/htu21df/minimal.js +22 -0
  47. package/examples/humidity/sht31/complete.js +53 -0
  48. package/examples/humidity/sht31/demo.js +53 -0
  49. package/examples/humidity/sht31/minimal.js +21 -0
  50. package/examples/humidity/si7021/complete.js +55 -0
  51. package/examples/humidity/si7021/demo.js +58 -0
  52. package/examples/humidity/si7021/minimal.js +21 -0
  53. package/examples/imu/bno055/complete.js +85 -0
  54. package/examples/imu/bno055/demo.js +70 -0
  55. package/examples/imu/bno055/minimal.js +20 -0
  56. package/examples/imu/qmi8658a/complete.js +134 -0
  57. package/examples/imu/qmi8658a/demo.js +81 -0
  58. package/examples/imu/qmi8658a/minimal.js +22 -0
  59. package/examples/io_expander/pca6408a/complete.js +63 -0
  60. package/examples/io_expander/pca6408a/demo.js +58 -0
  61. package/examples/io_expander/pca6408a/minimal.js +20 -0
  62. package/examples/io_expander/pca6416a/complete.js +68 -0
  63. package/examples/io_expander/pca6416a/demo.js +60 -0
  64. package/examples/io_expander/pca6416a/minimal.js +20 -0
  65. package/examples/io_expander/pca9536/complete.js +63 -0
  66. package/examples/io_expander/pca9536/demo.js +57 -0
  67. package/examples/io_expander/pca9536/minimal.js +20 -0
  68. package/examples/io_expander/pca9539/complete.js +68 -0
  69. package/examples/io_expander/pca9539/demo.js +60 -0
  70. package/examples/io_expander/pca9539/minimal.js +20 -0
  71. package/examples/light/max44009/complete.js +56 -0
  72. package/examples/light/max44009/demo.js +49 -0
  73. package/examples/light/max44009/minimal.js +22 -0
  74. package/examples/memory/at24c02n/complete.js +39 -0
  75. package/examples/memory/at24c02n/demo.js +39 -0
  76. package/examples/memory/at24c02n/minimal.js +15 -0
  77. package/examples/memory/at24c64/complete.js +39 -0
  78. package/examples/memory/at24c64/demo.js +39 -0
  79. package/examples/memory/at24c64/minimal.js +15 -0
  80. package/examples/memory/cat24c512/complete.js +39 -0
  81. package/examples/memory/cat24c512/demo.js +39 -0
  82. package/examples/memory/cat24c512/minimal.js +15 -0
  83. package/examples/motor/tmc2130/complete.js +138 -0
  84. package/examples/motor/tmc2130/demo.js +96 -0
  85. package/examples/motor/tmc2130/minimal.js +30 -0
  86. package/examples/motor/tmc2208/complete.js +92 -0
  87. package/examples/motor/tmc2208/demo.js +85 -0
  88. package/examples/motor/tmc2208/minimal.js +25 -0
  89. package/examples/motor/tmc2209/complete.js +106 -0
  90. package/examples/motor/tmc2209/demo.js +97 -0
  91. package/examples/motor/tmc2209/minimal.js +25 -0
  92. package/examples/motor/tmc5160/complete.js +205 -0
  93. package/examples/motor/tmc5160/demo.js +104 -0
  94. package/examples/motor/tmc5160/minimal.js +35 -0
  95. package/examples/other/pca9541/complete.js +52 -0
  96. package/examples/other/pca9541/demo.js +61 -0
  97. package/examples/other/pca9541/minimal.js +23 -0
  98. package/examples/power/axp2101/complete.js +135 -0
  99. package/examples/power/axp2101/demo.js +68 -0
  100. package/examples/power/axp2101/minimal.js +24 -0
  101. package/examples/rtc/pcf85063a/complete.js +68 -0
  102. package/examples/rtc/pcf85063a/demo.js +76 -0
  103. package/examples/rtc/pcf85063a/minimal.js +20 -0
  104. package/examples/temperature/tmp102/complete.js +74 -0
  105. package/examples/temperature/tmp102/demo.js +71 -0
  106. package/examples/temperature/tmp102/minimal.js +20 -0
  107. package/examples/tof/vl6180x/complete.js +62 -0
  108. package/examples/tof/vl6180x/demo.js +77 -0
  109. package/examples/tof/vl6180x/minimal.js +23 -0
  110. package/package.json +1 -1
  111. package/src/chips/adc_dac/ad5251.js +345 -0
  112. package/src/chips/adc_dac/ad5252.js +345 -0
  113. package/src/chips/adc_dac/ads7828.js +225 -0
  114. package/src/chips/adc_dac/cat5171.js +213 -0
  115. package/src/chips/adc_dac/cs43l22.js +645 -0
  116. package/src/chips/adc_dac/ds1881.js +357 -0
  117. package/src/chips/adc_dac/ds3502.js +241 -0
  118. package/src/chips/adc_dac/es8311.js +621 -0
  119. package/src/chips/adc_dac/mcp3422.js +312 -0
  120. package/src/chips/adc_dac/mcp4xxx.js +982 -0
  121. package/src/chips/display/mono_oled.js +182 -0
  122. package/src/chips/display/sh1106.js +496 -0
  123. package/src/chips/display/ssd1305.js +655 -0
  124. package/src/chips/display/ssd1306.js +458 -0
  125. package/src/chips/environmental/am2315.js +237 -0
  126. package/src/chips/humidity/htu21df.js +222 -0
  127. package/src/chips/humidity/sht31.js +284 -0
  128. package/src/chips/humidity/si7021.js +282 -0
  129. package/src/chips/imu/bno055.js +496 -0
  130. package/src/chips/imu/qmi8658a.js +1011 -0
  131. package/src/chips/io_expander/pca6408a.js +407 -0
  132. package/src/chips/io_expander/pca6416a.js +455 -0
  133. package/src/chips/io_expander/pca9536.js +398 -0
  134. package/src/chips/io_expander/pca9539.js +457 -0
  135. package/src/chips/light/max44009.js +336 -0
  136. package/src/chips/memory/at24c02n.js +220 -0
  137. package/src/chips/memory/at24c64.js +220 -0
  138. package/src/chips/memory/cat24c512.js +221 -0
  139. package/src/chips/motor/tmc2130.js +537 -0
  140. package/src/chips/motor/tmc2208.js +490 -0
  141. package/src/chips/motor/tmc2209.js +565 -0
  142. package/src/chips/motor/tmc5160.js +900 -0
  143. package/src/chips/other/pca9541.js +315 -0
  144. package/src/chips/power/axp2101.js +968 -0
  145. package/src/chips/rtc/pcf85063a.js +482 -0
  146. package/src/chips/temperature/tmp102.js +361 -0
  147. package/src/chips/tof/vl6180x.js +580 -0
  148. package/src/connection/uart.js +9 -0
  149. package/src/connection/uart_mock.js +65 -0
  150. package/src/discovery/registry.js +85 -8
@@ -0,0 +1,345 @@
1
+ 'use strict';
2
+
3
+ const _RDAC = [0x01, 0x03];
4
+ const _EEMEM_FLAG = 0x20;
5
+ const _TOL_INT = [0x3A, 0x3E];
6
+
7
+ const _Q_NOP = 0x80;
8
+ const _Q_RESTORE = 0x88;
9
+ const _Q_STORE = 0x90;
10
+ const _Q_HALVE = 0x98;
11
+ const _Q_DEC_STEP = 0xA8;
12
+ const _Q_INC_6DB = 0xC0;
13
+ const _Q_INC_STEP = 0xD0;
14
+ const _Q_DEC_6DB_ALL = 0xA0;
15
+ const _Q_DEC_STEP_ALL = 0xB0;
16
+ const _Q_RESET = 0xB8;
17
+ const _Q_INC_6DB_ALL = 0xC8;
18
+ const _Q_INC_STEP_ALL = 0xD8;
19
+
20
+ const _POLL_MS = 2;
21
+ const _POLL_TIMEOUT_MS = 100;
22
+
23
+ const _sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
24
+
25
+ function _checkChannel(channel) {
26
+ if (channel !== 0 && channel !== 1) throw new RangeError('channel must be 0 or 1');
27
+ }
28
+
29
+ function _clampCode(code) {
30
+ code = Math.trunc(code);
31
+ if (code < 0) return 0;
32
+ if (code > 255) return 255;
33
+ return code;
34
+ }
35
+
36
+ /**
37
+ * AD5252 dual 256-position I²C digital potentiometer with EEMEM (Analog
38
+ * Devices) — minimal interface.
39
+ *
40
+ * Two independent 3-terminal potentiometers (A, W, B). The chip has no
41
+ * register map: the instruction byte works as a one-byte register pointer
42
+ * (RDAC1 = 0x01 for channel 0, RDAC3 = 0x03 for channel 1), so the driver
43
+ * uses a RegisterConnection with a 1-byte register width. The I²C address is
44
+ * 0x2C–0x2F (AD1, AD0 pins). Wipers power up from EEMEM (midscale 128 until
45
+ * a value is stored).
46
+ */
47
+ class AD5252Minimal {
48
+ /**
49
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x2C–0x2F).
50
+ * @param {number} [rAb=10000] - End-to-end resistance RAB in ohms.
51
+ * @param {number} [rWiper=75] - Wiper resistance RW in ohms.
52
+ */
53
+ constructor(connection, rAb = 10000.0, rWiper = 75.0) {
54
+ this._conn = connection;
55
+ this._rAb = rAb;
56
+ this._rW = rWiper;
57
+ }
58
+
59
+ async _writeCode(channel, code) {
60
+ _checkChannel(channel);
61
+ await this._conn.writeReg(_RDAC[channel], _clampCode(code));
62
+ }
63
+
64
+ async _readCode(channel) {
65
+ _checkChannel(channel);
66
+ const buf = await this._conn.readReg(_RDAC[channel], 1);
67
+ return buf[0];
68
+ }
69
+
70
+ /**
71
+ * Prepare the driver. The AD5252 has no identity register and needs no
72
+ * initialisation writes, so this performs no I²C traffic.
73
+ * @returns {Promise<void>}
74
+ */
75
+ async init() {}
76
+
77
+ /**
78
+ * Set the wiper position of a channel.
79
+ * @param {number} channel - 0 or 1.
80
+ * @param {number} code - Wiper position 0–255 (clamped).
81
+ * @returns {Promise<void>}
82
+ */
83
+ async setWiper(channel, code) {
84
+ await this._writeCode(channel, code);
85
+ }
86
+
87
+ /**
88
+ * Read back the wiper position of a channel (instruction byte alone,
89
+ * then one data byte).
90
+ * @param {number} channel - 0 or 1.
91
+ * @returns {Promise<number>} Wiper position 0–255.
92
+ */
93
+ async getWiper(channel) {
94
+ return this._readCode(channel);
95
+ }
96
+
97
+ /**
98
+ * Set the W–B resistance to the nearest reachable code.
99
+ * @param {number} channel - 0 or 1.
100
+ * @param {number} ohms - Target W–B resistance in ohms (clamped to the reachable range).
101
+ * @returns {Promise<void>}
102
+ */
103
+ async setResistance(channel, ohms) {
104
+ await this._writeCode(channel, Math.floor((ohms - this._rW) / this._rAb * 256 + 0.5));
105
+ }
106
+
107
+ /**
108
+ * Read the W–B resistance for the current wiper code.
109
+ * @param {number} channel - 0 or 1.
110
+ * @returns {Promise<number>} Resistance in ohms, `D / 256 * RAB + RW`.
111
+ */
112
+ async getResistance(channel) {
113
+ return (await this._readCode(channel)) / 256 * this._rAb + this._rW;
114
+ }
115
+ }
116
+
117
+ /**
118
+ * AD5252 full interface — extends Minimal with ratio and W–A helpers, quick
119
+ * commands (±1 step, ±6 dB), EEMEM store/restore/reset, user EEMEM and the
120
+ * factory RAB tolerance. EEMEM writes poll with the NOP quick command until
121
+ * the chip acknowledges again (timeout 100 ms).
122
+ */
123
+ class AD5252Full extends AD5252Minimal {
124
+ async _quickByte(byte) {
125
+ await this._conn.write(Buffer.from([byte]));
126
+ }
127
+
128
+ async _quick(base, baseAll, channel) {
129
+ if (channel === undefined || channel === null) {
130
+ await this._quickByte(baseAll);
131
+ return;
132
+ }
133
+ _checkChannel(channel);
134
+ await this._quickByte(base | _RDAC[channel]);
135
+ }
136
+
137
+ async _waitReady() {
138
+ const start = Date.now();
139
+ for (;;) {
140
+ try {
141
+ await this._quickByte(_Q_NOP);
142
+ return;
143
+ } catch (e) {
144
+ if (Date.now() - start >= _POLL_TIMEOUT_MS) {
145
+ throw new Error('AD5252 busy: EEMEM write did not finish');
146
+ }
147
+ await _sleep(_POLL_MS);
148
+ }
149
+ }
150
+ }
151
+
152
+ /**
153
+ * Write both wipers in one consecutive-write transaction.
154
+ * @param {number} code0 - Channel 0 wiper position 0–255.
155
+ * @param {number} code1 - Channel 1 wiper position 0–255.
156
+ * @returns {Promise<void>}
157
+ */
158
+ async setBoth(code0, code1) {
159
+ await this._conn.writeReg(_RDAC[0], Buffer.from([_clampCode(code0), _clampCode(code1)]));
160
+ }
161
+
162
+ /**
163
+ * Set the wiper by divider ratio V(W−B)/V(A−B).
164
+ * @param {number} channel - 0 or 1.
165
+ * @param {number} ratio - 0.0–1.0; the code is round(ratio * 256) clamped to 255.
166
+ * @returns {Promise<void>}
167
+ */
168
+ async setRatio(channel, ratio) {
169
+ await this._writeCode(channel, Math.floor(ratio * 256 + 0.5));
170
+ }
171
+
172
+ /**
173
+ * Read the divider ratio of a channel.
174
+ * @param {number} channel - 0 or 1.
175
+ * @returns {Promise<number>} `code / 256`.
176
+ */
177
+ async getRatio(channel) {
178
+ return (await this._readCode(channel)) / 256;
179
+ }
180
+
181
+ /**
182
+ * Set the W–A resistance (rheostat mode, B open) to the nearest code.
183
+ * @param {number} channel - 0 or 1.
184
+ * @param {number} ohms - Target W–A resistance in ohms.
185
+ * @returns {Promise<void>}
186
+ */
187
+ async setResistanceWa(channel, ohms) {
188
+ const steps = Math.floor((ohms - this._rW) / this._rAb * 256 + 0.5);
189
+ await this._writeCode(channel, 256 - steps);
190
+ }
191
+
192
+ /**
193
+ * Read the W–A resistance for the current wiper code.
194
+ * @param {number} channel - 0 or 1.
195
+ * @returns {Promise<number>} Resistance in ohms, `(256 - D) / 256 * RAB + RW`.
196
+ */
197
+ async getResistanceWa(channel) {
198
+ return (256 - (await this._readCode(channel))) / 256 * this._rAb + this._rW;
199
+ }
200
+
201
+ /**
202
+ * Increment the wiper by one step (quick command).
203
+ * @param {number} [channel] - 0 or 1; omit for both channels.
204
+ * @returns {Promise<void>}
205
+ */
206
+ async increment(channel) {
207
+ await this._quick(_Q_INC_STEP, _Q_INC_STEP_ALL, channel);
208
+ }
209
+
210
+ /**
211
+ * Decrement the wiper by one step (quick command).
212
+ * @param {number} [channel] - 0 or 1; omit for both channels.
213
+ * @returns {Promise<void>}
214
+ */
215
+ async decrement(channel) {
216
+ await this._quick(_Q_DEC_STEP, _Q_DEC_STEP_ALL, channel);
217
+ }
218
+
219
+ /**
220
+ * Increase by 6 dB — the code doubles (quick command).
221
+ * @param {number} [channel] - 0 or 1; omit for both channels.
222
+ * @returns {Promise<void>}
223
+ */
224
+ async double(channel) {
225
+ await this._quick(_Q_INC_6DB, _Q_INC_6DB_ALL, channel);
226
+ }
227
+
228
+ /**
229
+ * Decrease by 6 dB — the code halves (quick command).
230
+ * @param {number} [channel] - 0 or 1; omit for both channels.
231
+ * @returns {Promise<void>}
232
+ */
233
+ async halve(channel) {
234
+ await this._quick(_Q_HALVE, _Q_DEC_6DB_ALL, channel);
235
+ }
236
+
237
+ /**
238
+ * Store the wiper in EEMEM, then wait for the ≈ 26 ms write cycle.
239
+ * @param {number} [channel] - 0 or 1; omit for both channels.
240
+ * @returns {Promise<void>}
241
+ * @throws {Error} If the chip does not acknowledge again within 100 ms.
242
+ */
243
+ async store(channel) {
244
+ if (channel === undefined || channel === null) {
245
+ await this._quick(_Q_STORE, 0, 0);
246
+ await this._waitReady();
247
+ await this._quick(_Q_STORE, 0, 1);
248
+ } else {
249
+ await this._quick(_Q_STORE, 0, channel);
250
+ }
251
+ await this._waitReady();
252
+ }
253
+
254
+ /**
255
+ * Restore the wiper from EEMEM, then send a NOP to return to idle.
256
+ * @param {number} [channel] - 0 or 1; omit for both channels.
257
+ * @returns {Promise<void>}
258
+ */
259
+ async restore(channel) {
260
+ if (channel === undefined || channel === null) {
261
+ await this._quick(_Q_RESTORE, 0, 0);
262
+ await this._quick(_Q_RESTORE, 0, 1);
263
+ } else {
264
+ await this._quick(_Q_RESTORE, 0, channel);
265
+ }
266
+ await this.nop();
267
+ }
268
+
269
+ /**
270
+ * Quick-command reset: restore both wipers from EEMEM and wait.
271
+ * @returns {Promise<void>}
272
+ * @throws {Error} If the chip does not acknowledge again within 100 ms.
273
+ */
274
+ async reset() {
275
+ await this._quickByte(_Q_RESET);
276
+ await this._waitReady();
277
+ }
278
+
279
+ /**
280
+ * Read a user EEMEM byte.
281
+ * @param {number} index - 4–15.
282
+ * @returns {Promise<number>} The stored byte.
283
+ */
284
+ async readEemem(index) {
285
+ if (index < 4 || index > 15) throw new RangeError('index must be 4..15');
286
+ return (await this._conn.readReg(_EEMEM_FLAG | index, 1))[0];
287
+ }
288
+
289
+ /**
290
+ * Write a user EEMEM byte and wait for the ≈ 26 ms write cycle.
291
+ * @param {number} index - 4–15.
292
+ * @param {number} value - Byte 0–255.
293
+ * @returns {Promise<void>}
294
+ * @throws {Error} If the chip does not acknowledge again within 100 ms.
295
+ */
296
+ async writeEemem(index, value) {
297
+ if (index < 4 || index > 15) throw new RangeError('index must be 4..15');
298
+ await this._conn.writeReg(_EEMEM_FLAG | index, value & 0xFF);
299
+ await this._waitReady();
300
+ }
301
+
302
+ /**
303
+ * Read the factory-stored RAB tolerance of a channel.
304
+ * @param {number} channel - 0 or 1.
305
+ * @returns {Promise<number>} Tolerance in percent (sign-magnitude integer byte plus decimal byte / 256).
306
+ */
307
+ async readTolerance(channel) {
308
+ _checkChannel(channel);
309
+ const raw = await this._conn.readReg(_TOL_INT[channel], 2);
310
+ const value = (raw[0] & 0x7F) + raw[1] / 256;
311
+ return (raw[0] & 0x80) ? -value : value;
312
+ }
313
+
314
+ /**
315
+ * Actual RAB of a channel from its stored tolerance.
316
+ * @param {number} channel - 0 or 1.
317
+ * @returns {Promise<number>} `rAb * (1 + tolerance / 100)` in ohms.
318
+ */
319
+ async rabActual(channel) {
320
+ return this._rAb * (1 + (await this.readTolerance(channel)) / 100);
321
+ }
322
+
323
+ /**
324
+ * Send the NOP quick command (0x80), returning the chip to idle.
325
+ * @returns {Promise<void>}
326
+ */
327
+ async nop() {
328
+ await this._quickByte(_Q_NOP);
329
+ }
330
+
331
+ /**
332
+ * Change the resistance parameters used by conversions (no I²C traffic).
333
+ * @param {number} rAb - End-to-end resistance RAB in ohms.
334
+ * @param {number} [rWiper] - Wiper resistance RW in ohms; unchanged if omitted.
335
+ */
336
+ setRab(rAb, rWiper) {
337
+ this._rAb = rAb;
338
+ if (rWiper !== undefined && rWiper !== null) this._rW = rWiper;
339
+ }
340
+ }
341
+
342
+ /** Default 7-bit I²C address (AD1 = AD0 = GND); 0x2C–0x2F are possible. */
343
+ AD5252Minimal.I2C_ADDRESS = 0x2C;
344
+
345
+ module.exports = { AD5252Minimal, AD5252Full };
@@ -0,0 +1,225 @@
1
+ 'use strict';
2
+
3
+ const _INTERNAL_VREF = 2.5;
4
+ const _PD_ALL_DOWN = 0;
5
+ const _PD_REF_OFF_ADC_ON = 1;
6
+ const _PD_REF_ON_ADC_OFF = 2;
7
+ const _PD_REF_ON_ADC_ON = 3;
8
+ const _CMD_REF_ON = 0x08; // lone command: SD=0, C=0, PD=10
9
+ const _CMD_POWER_DOWN = 0x00; // lone command: SD=0, C=0, PD=00
10
+ const _REF_SETTLE_MS = 1;
11
+
12
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
13
+
14
+ /** C2:C0 for single-ended channel n — the encoding is interleaved. */
15
+ function _singleEndedCode(channel) {
16
+ return ((channel & 1) << 2) | (channel >> 1);
17
+ }
18
+
19
+ /**
20
+ * ADS7828 12-bit, 8-channel sampling ADC — minimal interface.
21
+ *
22
+ * The chip has no registers: one command byte selects the input mode, the
23
+ * channel and the power-down state and starts a conversion; the 12-bit
24
+ * result is read back as two bytes (`readReg(command, 2)` is the datasheet's
25
+ * write-command / repeated-START / read sequence). The I²C address is
26
+ * 0x48–0x4B. Reads the eight single-ended inputs (CHn against COM) as raw
27
+ * codes or voltages. Defaults to the internal 2.5 V reference, which is kept
28
+ * on between conversions.
29
+ */
30
+ class ADS7828Minimal {
31
+ /**
32
+ * @param {import('../../connection/connection').RegisterConnection} connection - I²C connection pointing at the device (0x48–0x4B).
33
+ * @param {number|null} [vref=null] - null for the internal 2.5 V reference, or the external reference voltage in volts (0.05 V … VDD).
34
+ */
35
+ constructor(connection, vref = null) {
36
+ this._conn = connection;
37
+ this._refOn = false;
38
+ this._selectReference(vref);
39
+ }
40
+
41
+ _selectReference(vref) {
42
+ if (vref === null || vref === undefined) {
43
+ this._internal = true;
44
+ this._vref = _INTERNAL_VREF;
45
+ this._pd = _PD_REF_ON_ADC_ON;
46
+ } else {
47
+ this._internal = false;
48
+ this._vref = Number(vref);
49
+ this._pd = _PD_ALL_DOWN;
50
+ }
51
+ this._refOn = false;
52
+ }
53
+
54
+ async _enableReference() {
55
+ await this._conn.write(Buffer.from([_CMD_REF_ON]));
56
+ await sleep(_REF_SETTLE_MS);
57
+ this._refOn = true;
58
+ }
59
+
60
+ /**
61
+ * Prepare the driver. With the internal reference this sends the
62
+ * reference-on command and waits 1 ms for it to settle; with an external
63
+ * reference there is no I²C traffic. Reads call this lazily if it was skipped.
64
+ * @returns {Promise<void>}
65
+ */
66
+ async init() {
67
+ if (this._internal) await this._enableReference();
68
+ }
69
+
70
+ async _convert(singleEnded, code) {
71
+ if (this._internal && !this._refOn) await this._enableReference();
72
+ const command = ((singleEnded ? 1 : 0) << 7) | (code << 4) | (this._pd << 2);
73
+ const data = await this._conn.readReg(command, 2);
74
+ return ((data[0] & 0x0F) << 8) | data[1];
75
+ }
76
+
77
+ /**
78
+ * Read a single-ended channel (CHn against COM) as a 12-bit code.
79
+ * @param {number} channel - Channel number 0–7. Clamped to the valid range.
80
+ * @returns {Promise<number>} Raw 12-bit code (0–4095).
81
+ */
82
+ async readRaw(channel) {
83
+ const ch = Math.max(0, Math.min(7, Math.trunc(channel)));
84
+ return this._convert(true, _singleEndedCode(ch));
85
+ }
86
+
87
+ /**
88
+ * Read a single-ended channel as a voltage.
89
+ * @param {number} channel - Channel number 0–7. Clamped to the valid range.
90
+ * @returns {Promise<number>} Input voltage in volts (raw / 4096 × vref).
91
+ */
92
+ async readVoltage(channel) {
93
+ return (await this.readRaw(channel)) / 4096 * this._vref;
94
+ }
95
+
96
+ /**
97
+ * Read all eight single-ended channels, one conversion each.
98
+ * @returns {Promise<number[]>} Voltages of CH0…CH7 in volts.
99
+ */
100
+ async readAll() {
101
+ const out = [];
102
+ for (let ch = 0; ch < 8; ch++) out.push(await this.readVoltage(ch));
103
+ return out;
104
+ }
105
+ }
106
+
107
+ /**
108
+ * ADS7828 full interface — extends ADS7828Minimal.
109
+ *
110
+ * Adds differential reads (both polarities), a signed differential read,
111
+ * run-time reference switching, power-down control and multi-channel scans.
112
+ */
113
+ class ADS7828Full extends ADS7828Minimal {
114
+ /** @type {number} PD=00: power down between conversions. */
115
+ static POWER_DOWN_BETWEEN = _PD_ALL_DOWN;
116
+ /** @type {number} PD=01: internal reference off, ADC on. */
117
+ static REF_OFF_ADC_ON = _PD_REF_OFF_ADC_ON;
118
+ /** @type {number} PD=10: internal reference on, ADC off. */
119
+ static REF_ON_ADC_OFF = _PD_REF_ON_ADC_OFF;
120
+ /** @type {number} PD=11: internal reference on, ADC on. */
121
+ static REF_ON_ADC_ON = _PD_REF_ON_ADC_ON;
122
+
123
+ /**
124
+ * Read a differential pair as a 12-bit code.
125
+ * @param {number} pair - Pair index 0–3 (CH0/CH1 … CH6/CH7). Clamped.
126
+ * @param {boolean} [inverted=false] - If true the positive input is CH(2·pair+1).
127
+ * @returns {Promise<number>} Raw 12-bit code (0–4095); a negative differential voltage reads 0.
128
+ */
129
+ async readDifferentialRaw(pair, inverted = false) {
130
+ const p = Math.max(0, Math.min(3, Math.trunc(pair)));
131
+ return this._convert(false, inverted ? (4 | p) : p);
132
+ }
133
+
134
+ /**
135
+ * Read a differential pair as a voltage.
136
+ * @param {number} pair - Pair index 0–3. Clamped.
137
+ * @param {boolean} [inverted=false] - If true the positive input is CH(2·pair+1).
138
+ * @returns {Promise<number>} Differential voltage in volts (≥ 0).
139
+ */
140
+ async readDifferential(pair, inverted = false) {
141
+ return (await this.readDifferentialRaw(pair, inverted)) / 4096 * this._vref;
142
+ }
143
+
144
+ /**
145
+ * Read V(CH2p) − V(CH2p+1) with sign, using two conversions.
146
+ * @param {number} pair - Pair index 0–3. Clamped.
147
+ * @returns {Promise<number>} Signed differential voltage in volts (−vref … +vref).
148
+ */
149
+ async readSignedDifferential(pair) {
150
+ const normal = await this.readDifferentialRaw(pair, false);
151
+ const inverted = await this.readDifferentialRaw(pair, true);
152
+ return (normal - inverted) / 4096 * this._vref;
153
+ }
154
+
155
+ /**
156
+ * Switch between the internal and an external reference.
157
+ * @param {number|null} vref - null for the internal 2.5 V reference (re-runs the turn-on and settle wait), or an external reference voltage in volts (PD1 = 0 from then on).
158
+ * @returns {Promise<void>}
159
+ */
160
+ async setReference(vref) {
161
+ this._selectReference(vref);
162
+ if (this._internal) await this._enableReference();
163
+ }
164
+
165
+ /**
166
+ * Reference voltage used for scaling.
167
+ * @returns {number} Volts (2.5 for the internal reference).
168
+ */
169
+ getReference() {
170
+ return this._vref;
171
+ }
172
+
173
+ /**
174
+ * Whether the internal reference is selected.
175
+ * @returns {boolean}
176
+ */
177
+ usesInternalReference() {
178
+ return this._internal;
179
+ }
180
+
181
+ /**
182
+ * Set the PD bits used in all following conversion commands.
183
+ * @param {number} pd - 0 = POWER_DOWN_BETWEEN, 1 = REF_OFF_ADC_ON, 2 = REF_ON_ADC_OFF, 3 = REF_ON_ADC_ON.
184
+ * @throws {RangeError} If pd is outside 0–3, or if the internal reference is selected and pd has PD1 = 0.
185
+ */
186
+ setPowerMode(pd) {
187
+ if (!Number.isInteger(pd) || pd < 0 || pd > 3) throw new RangeError('pd must be 0-3');
188
+ if (this._internal && !(pd & 2)) throw new RangeError('internal reference requires PD1 = 1');
189
+ this._pd = pd;
190
+ }
191
+
192
+ /**
193
+ * Power the chip down completely (≈ 400 nA) with a lone command (PD = 00).
194
+ * With the internal reference this turns it off; the next read re-runs the turn-on and settle wait.
195
+ * @returns {Promise<void>}
196
+ */
197
+ async powerDown() {
198
+ await this._conn.write(Buffer.from([_CMD_POWER_DOWN]));
199
+ this._refOn = false;
200
+ }
201
+
202
+ /**
203
+ * Read several single-ended channels in order as raw codes.
204
+ * @param {number[]} channels - Channel numbers 0–7.
205
+ * @returns {Promise<number[]>} Raw 12-bit codes, one conversion per channel.
206
+ */
207
+ async readScan(channels) {
208
+ const out = [];
209
+ for (const ch of channels) out.push(await this.readRaw(ch));
210
+ return out;
211
+ }
212
+
213
+ /**
214
+ * Size of one code step.
215
+ * @returns {number} vref / 4096 in volts (610 µV at 2.5 V).
216
+ */
217
+ lsb() {
218
+ return this._vref / 4096;
219
+ }
220
+ }
221
+
222
+ ADS7828Minimal.I2C_ADDRESS = 0x48;
223
+ ADS7828Full.I2C_ADDRESS = 0x48;
224
+
225
+ module.exports = { ADS7828Minimal, ADS7828Full };