periph 1.3.2 → 1.4.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 (114) hide show
  1. package/README.md +27 -0
  2. package/examples/accelerometer/bma180/complete.js +83 -0
  3. package/examples/accelerometer/bma180/demo.js +47 -0
  4. package/examples/accelerometer/bma180/minimal.js +21 -0
  5. package/examples/accelerometer/fxos8700/complete.js +111 -0
  6. package/examples/accelerometer/fxos8700/demo.js +80 -0
  7. package/examples/accelerometer/fxos8700/minimal.js +20 -0
  8. package/examples/accelerometer/lis3dh/complete.js +123 -0
  9. package/examples/accelerometer/lis3dh/demo.js +99 -0
  10. package/examples/accelerometer/lis3dh/minimal.js +19 -0
  11. package/examples/accelerometer/lsm303/complete.js +82 -0
  12. package/examples/accelerometer/lsm303/demo.js +96 -0
  13. package/examples/accelerometer/lsm303/minimal.js +23 -0
  14. package/examples/accelerometer/mma8451q/complete.js +96 -0
  15. package/examples/accelerometer/mma8451q/demo.js +120 -0
  16. package/examples/accelerometer/mma8451q/minimal.js +20 -0
  17. package/examples/adc_dac/ad5243/complete.js +52 -0
  18. package/examples/adc_dac/ad5243/demo.js +50 -0
  19. package/examples/adc_dac/ad5243/minimal.js +23 -0
  20. package/examples/adc_dac/ad5248/complete.js +49 -0
  21. package/examples/adc_dac/ad5248/demo.js +42 -0
  22. package/examples/adc_dac/ad5248/minimal.js +21 -0
  23. package/examples/adc_dac/ads1013/complete.js +51 -0
  24. package/examples/adc_dac/ads1013/demo.js +51 -0
  25. package/examples/adc_dac/ads1013/minimal.js +21 -0
  26. package/examples/adc_dac/ads1014/complete.js +88 -0
  27. package/examples/adc_dac/ads1014/demo.js +52 -0
  28. package/examples/adc_dac/ads1014/minimal.js +21 -0
  29. package/examples/adc_dac/ads1113/complete.js +47 -0
  30. package/examples/adc_dac/ads1113/demo.js +39 -0
  31. package/examples/adc_dac/ads1113/minimal.js +20 -0
  32. package/examples/adc_dac/ads1114/complete.js +77 -0
  33. package/examples/adc_dac/ads1114/demo.js +47 -0
  34. package/examples/adc_dac/ads1114/minimal.js +20 -0
  35. package/examples/adc_dac/ads1115/complete.js +82 -0
  36. package/examples/adc_dac/ads1115/demo.js +55 -0
  37. package/examples/adc_dac/ads1115/minimal.js +20 -0
  38. package/examples/adc_dac/es8311/complete.js +80 -0
  39. package/examples/adc_dac/es8311/demo.js +59 -0
  40. package/examples/adc_dac/es8311/minimal.js +18 -0
  41. package/examples/adc_dac/mcp4xxx/complete.js +97 -0
  42. package/examples/adc_dac/mcp4xxx/demo.js +76 -0
  43. package/examples/adc_dac/mcp4xxx/minimal.js +22 -0
  44. package/examples/display/pcf8578/complete.js +58 -0
  45. package/examples/display/pcf8578/demo.js +62 -0
  46. package/examples/display/pcf8578/minimal.js +19 -0
  47. package/examples/display/pcf8579/complete.js +58 -0
  48. package/examples/display/pcf8579/demo.js +61 -0
  49. package/examples/display/pcf8579/minimal.js +19 -0
  50. package/examples/imu/icm20948/complete.js +114 -0
  51. package/examples/imu/icm20948/demo.js +79 -0
  52. package/examples/imu/icm20948/minimal.js +22 -0
  53. package/examples/imu/qmi8658a/complete.js +134 -0
  54. package/examples/imu/qmi8658a/demo.js +81 -0
  55. package/examples/imu/qmi8658a/minimal.js +22 -0
  56. package/examples/io_expander/mcp23008/complete.js +70 -0
  57. package/examples/io_expander/mcp23008/demo.js +58 -0
  58. package/examples/io_expander/mcp23008/minimal.js +20 -0
  59. package/examples/memory/mb85rc/complete.js +40 -0
  60. package/examples/memory/mb85rc/demo.js +58 -0
  61. package/examples/memory/mb85rc/minimal.js +15 -0
  62. package/examples/motor/drv2605/complete.js +100 -0
  63. package/examples/motor/drv2605/demo.js +55 -0
  64. package/examples/motor/drv2605/minimal.js +24 -0
  65. package/examples/motor/tmc2130/complete.js +138 -0
  66. package/examples/motor/tmc2130/demo.js +96 -0
  67. package/examples/motor/tmc2130/minimal.js +30 -0
  68. package/examples/motor/tmc2208/complete.js +92 -0
  69. package/examples/motor/tmc2208/demo.js +85 -0
  70. package/examples/motor/tmc2208/minimal.js +25 -0
  71. package/examples/motor/tmc2209/complete.js +106 -0
  72. package/examples/motor/tmc2209/demo.js +97 -0
  73. package/examples/motor/tmc2209/minimal.js +25 -0
  74. package/examples/motor/tmc5160/complete.js +205 -0
  75. package/examples/motor/tmc5160/demo.js +104 -0
  76. package/examples/motor/tmc5160/minimal.js +35 -0
  77. package/examples/power/axp2101/complete.js +135 -0
  78. package/examples/power/axp2101/demo.js +68 -0
  79. package/examples/power/axp2101/minimal.js +24 -0
  80. package/examples/rtc/pcf85063a/complete.js +68 -0
  81. package/examples/rtc/pcf85063a/demo.js +76 -0
  82. package/examples/rtc/pcf85063a/minimal.js +20 -0
  83. package/package.json +1 -1
  84. package/src/chips/accelerometer/bma180.js +573 -0
  85. package/src/chips/accelerometer/fxos8700.js +1600 -0
  86. package/src/chips/accelerometer/lis3dh.js +1341 -0
  87. package/src/chips/accelerometer/lsm303.js +1471 -0
  88. package/src/chips/accelerometer/mma8451q.js +1406 -0
  89. package/src/chips/adc_dac/_ads111x_base.js +272 -0
  90. package/src/chips/adc_dac/ad5243.js +231 -0
  91. package/src/chips/adc_dac/ad5248.js +194 -0
  92. package/src/chips/adc_dac/ads1013.js +219 -0
  93. package/src/chips/adc_dac/ads1014.js +509 -0
  94. package/src/chips/adc_dac/ads1113.js +112 -0
  95. package/src/chips/adc_dac/ads1114.js +201 -0
  96. package/src/chips/adc_dac/ads1115.js +232 -0
  97. package/src/chips/adc_dac/es8311.js +621 -0
  98. package/src/chips/adc_dac/mcp4xxx.js +982 -0
  99. package/src/chips/display/pcf8578.js +452 -0
  100. package/src/chips/display/pcf8579.js +451 -0
  101. package/src/chips/imu/icm20948.js +849 -0
  102. package/src/chips/imu/qmi8658a.js +1011 -0
  103. package/src/chips/io_expander/mcp23008.js +558 -0
  104. package/src/chips/memory/mb85rc.js +166 -0
  105. package/src/chips/motor/drv2605.js +592 -0
  106. package/src/chips/motor/tmc2130.js +537 -0
  107. package/src/chips/motor/tmc2208.js +490 -0
  108. package/src/chips/motor/tmc2209.js +565 -0
  109. package/src/chips/motor/tmc5160.js +900 -0
  110. package/src/chips/power/axp2101.js +968 -0
  111. package/src/chips/rtc/pcf85063a.js +482 -0
  112. package/src/connection/uart.js +9 -0
  113. package/src/connection/uart_mock.js +65 -0
  114. package/src/discovery/registry.js +45 -19
@@ -0,0 +1,272 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Shared implementation for the ADS1113 / ADS1114 / ADS1115 16-bit ΔΣ ADCs.
5
+ *
6
+ * The three chips share one register map and one Config-register layout; they
7
+ * differ only in which Config fields the silicon honours (ADS1113: fixed
8
+ * ±2.048 V, no comparator, no mux; ADS1114: PGA + comparator; ADS1115: PGA +
9
+ * comparator + 4-input mux). This module holds the whole-word Config builder,
10
+ * the single-shot sequence and the feature implementations as underscore
11
+ * methods on `ADS111xBase`; the per-chip modules publish the subset each chip
12
+ * supports.
13
+ *
14
+ * Registers are 16-bit big-endian behind a write-only Address Pointer. Every
15
+ * Config write is a whole word — never read-modify-write.
16
+ */
17
+
18
+ const REG_CONVERSION = 0x00;
19
+ const REG_CONFIG = 0x01;
20
+ const REG_LO_THRESH = 0x02;
21
+ const REG_HI_THRESH = 0x03;
22
+
23
+ const CONFIG_RESET = 0x8583;
24
+ const LO_RESET = 0x8000;
25
+ const HI_RESET = 0x7FFF;
26
+
27
+ const OS = 0x8000;
28
+ const MODE_SHOT = 0x0100;
29
+ const QUE_DISABLED = 3;
30
+
31
+ const SOURCE_CONVERSION_READY = 0x01;
32
+ const SOURCE_COMPARATOR = 0x02;
33
+
34
+ // Full-scale range in volts, indexed by PGA[2:0] (codes 6 and 7 alias ±0.256 V).
35
+ const FSR = [6.144, 4.096, 2.048, 1.024, 0.512, 0.256];
36
+ // Samples per second, indexed by DR[2:0].
37
+ const RATES = [8, 16, 32, 64, 128, 250, 475, 860];
38
+
39
+ const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
40
+
41
+ function toSigned16(v) {
42
+ return v & 0x8000 ? v - 0x10000 : v;
43
+ }
44
+
45
+ class ADS111xBase {
46
+ /**
47
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x48–0x4B). Its optional `intPin` receives ALERT/RDY.
48
+ */
49
+ constructor(connection) {
50
+ this._conn = connection;
51
+ this._resetState();
52
+ }
53
+
54
+ _resetState() {
55
+ this._mux = 0;
56
+ this._pga = 2;
57
+ this._dr = 4;
58
+ this._compMode = 0;
59
+ this._compPol = 0;
60
+ this._compLat = 0;
61
+ this._compQue = QUE_DISABLED;
62
+ this._queue = 0;
63
+ this._continuous = false;
64
+ this._loRaw = -32768;
65
+ this._hiRaw = 32767;
66
+ this._source = 0;
67
+ }
68
+
69
+ async _readReg(reg) {
70
+ const raw = await this._conn.readReg(reg, 2);
71
+ return (raw[0] << 8) | raw[1];
72
+ }
73
+
74
+ async _writeReg(reg, value) {
75
+ await this._conn.writeReg(reg, Buffer.from([(value >> 8) & 0xFF, value & 0xFF]));
76
+ }
77
+
78
+ _configWord(osBit, mux) {
79
+ return ((osBit ? OS : 0) | (mux << 12) | (this._pga << 9) |
80
+ (this._continuous ? 0 : MODE_SHOT) | (this._dr << 5) |
81
+ (this._compMode << 4) | (this._compPol << 3) | (this._compLat << 2) | this._compQue);
82
+ }
83
+
84
+ async _writeConfig(osBit = false) {
85
+ await this._writeReg(REG_CONFIG, this._configWord(osBit, this._mux));
86
+ }
87
+
88
+ /** Single-shot conversion on `mux`; resolves to the signed 16-bit code. */
89
+ async _convert(mux) {
90
+ this._continuous = false;
91
+ await this._writeReg(REG_CONFIG, this._configWord(true, mux));
92
+ const wait = 1000 / RATES[this._dr];
93
+ const deadline = Date.now() + 2 * wait + 50;
94
+ await sleep(wait);
95
+ while (!((await this._readReg(REG_CONFIG)) & OS)) {
96
+ if (Date.now() > deadline) throw new Error('ADS111x conversion timeout');
97
+ await sleep(0.5);
98
+ }
99
+ return toSigned16(await this._readReg(REG_CONVERSION));
100
+ }
101
+
102
+ async _setDataRate(dr) {
103
+ if (!Number.isInteger(dr) || dr < 0 || dr > 7) throw new RangeError('dr must be 0-7');
104
+ this._dr = dr;
105
+ if (this._continuous) await this._writeConfig();
106
+ }
107
+
108
+ async _setGain(pga) {
109
+ if (!Number.isInteger(pga) || pga < 0 || pga > 5) throw new RangeError('pga must be 0-5');
110
+ this._pga = pga;
111
+ if (this._continuous) await this._writeConfig();
112
+ }
113
+
114
+ async _startContinuous(mux) {
115
+ if (!Number.isInteger(mux) || mux < 0 || mux > 7) throw new RangeError('mux must be 0-7');
116
+ this._mux = mux;
117
+ this._continuous = true;
118
+ await this._writeConfig();
119
+ }
120
+
121
+ async _readContinuousRaw() {
122
+ return toSigned16(await this._readReg(REG_CONVERSION));
123
+ }
124
+
125
+ async _stopContinuous() {
126
+ this._continuous = false;
127
+ await this._writeConfig();
128
+ }
129
+
130
+ async _isConverting() {
131
+ return !((await this._readReg(REG_CONFIG)) & OS);
132
+ }
133
+
134
+ _fullScale() { return FSR[this._pga]; }
135
+
136
+ _lsb() { return FSR[this._pga] / 32768; }
137
+
138
+ async _reset(withThresholds) {
139
+ await this._writeReg(REG_CONFIG, CONFIG_RESET);
140
+ if (withThresholds) {
141
+ await this._writeReg(REG_LO_THRESH, LO_RESET);
142
+ await this._writeReg(REG_HI_THRESH, HI_RESET);
143
+ }
144
+ this._resetState();
145
+ }
146
+
147
+ // -- Comparator and thresholds ---------------------------------------
148
+
149
+ async _configureComparator(mode, polarity, latch, queue) {
150
+ if (![0, 1].includes(mode) || ![0, 1].includes(polarity) || ![0, 1, 2].includes(queue)) {
151
+ throw new RangeError('invalid comparator argument');
152
+ }
153
+ if (this._hiRaw <= this._loRaw) throw new RangeError('high threshold must exceed low threshold');
154
+ this._compMode = mode;
155
+ this._compPol = polarity;
156
+ this._compLat = latch ? 1 : 0;
157
+ this._queue = queue;
158
+ this._compQue = queue;
159
+ this._source = SOURCE_COMPARATOR;
160
+ await this._writeConfig();
161
+ }
162
+
163
+ async _disableComparator() {
164
+ this._compQue = QUE_DISABLED;
165
+ this._source = 0;
166
+ await this._writeConfig();
167
+ }
168
+
169
+ async _setLowThresholdRaw(raw) {
170
+ this._loRaw = raw;
171
+ await this._writeReg(REG_LO_THRESH, raw & 0xFFFF);
172
+ }
173
+
174
+ async _setHighThresholdRaw(raw) {
175
+ this._hiRaw = raw;
176
+ await this._writeReg(REG_HI_THRESH, raw & 0xFFFF);
177
+ }
178
+
179
+ _voltsToCode(volts) {
180
+ const steps = volts / this._lsb();
181
+ const code = steps >= 0 ? Math.floor(steps + 0.5) : -Math.floor(-steps + 0.5);
182
+ return Math.max(-32768, Math.min(32767, code));
183
+ }
184
+
185
+ async _setThresholds(low, high) {
186
+ if (high <= low) throw new RangeError('high must exceed low');
187
+ await this._setLowThresholdRaw(this._voltsToCode(low));
188
+ await this._setHighThresholdRaw(this._voltsToCode(high));
189
+ }
190
+
191
+ async _getThresholdsRaw() {
192
+ return [toSigned16(await this._readReg(REG_LO_THRESH)), toSigned16(await this._readReg(REG_HI_THRESH))];
193
+ }
194
+
195
+ async _clearAlert() {
196
+ await this._readReg(REG_CONVERSION);
197
+ }
198
+
199
+ // -- Interrupt API -----------------------------------------------------
200
+
201
+ async _enableInterrupt(source) {
202
+ if (source === SOURCE_CONVERSION_READY) {
203
+ await this._writeReg(REG_HI_THRESH, 0x8000);
204
+ await this._writeReg(REG_LO_THRESH, 0x0000);
205
+ this._compQue = 0;
206
+ this._source = SOURCE_CONVERSION_READY;
207
+ await this._writeConfig();
208
+ } else if (source === SOURCE_COMPARATOR) {
209
+ await this._setLowThresholdRaw(this._loRaw);
210
+ await this._setHighThresholdRaw(this._hiRaw);
211
+ await this._configureComparator(this._compMode, this._compPol, this._compLat === 1, this._queue);
212
+ } else {
213
+ throw new RangeError('unknown interrupt source');
214
+ }
215
+ }
216
+
217
+ async _pollInterrupt() {
218
+ if (this._source === 0) return 0;
219
+ const pin = this._conn.intPin;
220
+ let asserted;
221
+ if (pin) asserted = (await pin.isHigh()) === (this._compPol === 1);
222
+ else if (this._source === SOURCE_CONVERSION_READY) asserted = ((await this._readReg(REG_CONFIG)) & OS) !== 0;
223
+ else asserted = false;
224
+ if (!asserted) return 0;
225
+ if (this._source === SOURCE_COMPARATOR && this._compLat) await this._clearAlert();
226
+ return this._source;
227
+ }
228
+
229
+ async _onInterrupt(callback) {
230
+ this._callback = callback;
231
+ if (this._conn.intPin) {
232
+ this._edgeHandler = async () => {
233
+ const status = await this._pollInterrupt();
234
+ if (this._callback) this._callback(status);
235
+ };
236
+ await this._conn.intPin.onEdge(this._edgeHandler, this._compPol ? 'rising' : 'falling');
237
+ } else {
238
+ let last = await this._pollInterrupt();
239
+ let busy = false;
240
+ this._pollTimer = setInterval(async () => {
241
+ if (busy) return;
242
+ busy = true;
243
+ try {
244
+ const status = await this._pollInterrupt();
245
+ if (status !== last) {
246
+ last = status;
247
+ if (this._callback) this._callback(status);
248
+ }
249
+ } finally {
250
+ busy = false;
251
+ }
252
+ }, 5);
253
+ }
254
+ }
255
+
256
+ async _offInterrupt() {
257
+ if (this._conn.intPin && this._edgeHandler) {
258
+ await this._conn.intPin.offEdge(this._edgeHandler);
259
+ this._edgeHandler = null;
260
+ }
261
+ if (this._pollTimer) {
262
+ clearInterval(this._pollTimer);
263
+ this._pollTimer = null;
264
+ }
265
+ this._callback = null;
266
+ }
267
+ }
268
+
269
+ module.exports = {
270
+ ADS111xBase, RATES, FSR, REG_CONFIG, REG_CONVERSION, REG_LO_THRESH, REG_HI_THRESH,
271
+ SOURCE_CONVERSION_READY, SOURCE_COMPARATOR,
272
+ };
@@ -0,0 +1,231 @@
1
+ 'use strict';
2
+
3
+ const _INSTR_A0 = 0x80;
4
+ const _INSTR_SD = 0x40;
5
+ const _MIDSCALE = 0x80;
6
+
7
+ function _checkChannel(channel) {
8
+ if (channel !== 0 && channel !== 1) throw new RangeError('channel must be 0 or 1');
9
+ }
10
+
11
+ function _clampCode(code) {
12
+ code = Math.trunc(code);
13
+ if (code < 0) return 0;
14
+ if (code > 255) return 255;
15
+ return code;
16
+ }
17
+
18
+ /**
19
+ * AD5243 dual 256-position I²C digital potentiometer (Analog Devices) —
20
+ * minimal interface.
21
+ *
22
+ * Two independent 3-terminal potentiometers (A, W, B). The chip has no
23
+ * register map: a write is `[instruction, data]` where the instruction byte
24
+ * carries the channel select (A0, bit 7) and the shutdown flag (SD, bit 6);
25
+ * it is passed as the "register" of the RegisterConnection. The fixed I²C
26
+ * address is 0x2F. Wiper values are volatile (power-up midscale 0x80).
27
+ */
28
+ class AD5243Minimal {
29
+ /**
30
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x2F).
31
+ * @param {number} [rAb=10000] - End-to-end resistance RAB in ohms.
32
+ * @param {number} [rWiper=160] - Wiper resistance RW in ohms.
33
+ */
34
+ constructor(connection, rAb = 10000.0, rWiper = 160.0) {
35
+ this._conn = connection;
36
+ this._rAb = rAb;
37
+ this._rW = rWiper;
38
+ this._code = [_MIDSCALE, _MIDSCALE];
39
+ this._sd = [false, false];
40
+ }
41
+
42
+ _instr(channel) {
43
+ _checkChannel(channel);
44
+ return (channel ? _INSTR_A0 : 0) | (this._sd[channel] ? _INSTR_SD : 0);
45
+ }
46
+
47
+ async _writeCode(channel, code) {
48
+ code = _clampCode(code);
49
+ await this._conn.writeReg(this._instr(channel), code);
50
+ this._code[channel] = code;
51
+ }
52
+
53
+ async _readCode(channel) {
54
+ const buf = await this._conn.readReg(this._instr(channel), 1);
55
+ this._code[channel] = buf[0];
56
+ return buf[0];
57
+ }
58
+
59
+ /**
60
+ * Prepare the driver. The AD5243 has no identity register and needs no
61
+ * initialisation writes, so this performs no I²C traffic.
62
+ * @returns {Promise<void>}
63
+ */
64
+ async init() {}
65
+
66
+ /**
67
+ * Set the wiper position of a channel.
68
+ * @param {number} channel - 0 or 1.
69
+ * @param {number} code - Wiper position 0–255 (clamped).
70
+ * @returns {Promise<void>}
71
+ */
72
+ async setWiper(channel, code) {
73
+ await this._writeCode(channel, code);
74
+ }
75
+
76
+ /**
77
+ * Read back the wiper position of a channel (instruction byte alone,
78
+ * then one data byte).
79
+ * @param {number} channel - 0 or 1.
80
+ * @returns {Promise<number>} Wiper position 0–255.
81
+ */
82
+ async getWiper(channel) {
83
+ return this._readCode(channel);
84
+ }
85
+
86
+ /**
87
+ * Set the W–B resistance to the nearest reachable code.
88
+ * @param {number} channel - 0 or 1.
89
+ * @param {number} ohms - Target W–B resistance in ohms (clamped to the reachable range).
90
+ * @returns {Promise<void>}
91
+ */
92
+ async setResistance(channel, ohms) {
93
+ await this._writeCode(channel, Math.floor((ohms - 2 * this._rW) / this._rAb * 256 + 0.5));
94
+ }
95
+
96
+ /**
97
+ * Read the W–B resistance for the current wiper code.
98
+ * @param {number} channel - 0 or 1.
99
+ * @returns {Promise<number>} Resistance in ohms, `D / 256 * RAB + 2 * RW`.
100
+ */
101
+ async getResistance(channel) {
102
+ return (await this._readCode(channel)) / 256 * this._rAb + 2 * this._rW;
103
+ }
104
+ }
105
+
106
+ /** Fixed 7-bit I²C address (no address pins). */
107
+ AD5243Minimal.I2C_ADDRESS = 0x2F;
108
+
109
+ /**
110
+ * AD5243 full interface — extends Minimal with ratio and W–A helpers,
111
+ * increment/decrement, per-channel shutdown and reset. The shutdown state is
112
+ * write-only on the chip, so the driver tracks it.
113
+ */
114
+ class AD5243Full extends AD5243Minimal {
115
+ /**
116
+ * Write both wipers (two transactions).
117
+ * @param {number} code1 - Channel 0 wiper position 0–255.
118
+ * @param {number} code2 - Channel 1 wiper position 0–255.
119
+ * @returns {Promise<void>}
120
+ */
121
+ async setBoth(code1, code2) {
122
+ await this._writeCode(0, code1);
123
+ await this._writeCode(1, code2);
124
+ }
125
+
126
+ /**
127
+ * Set the wiper by divider ratio V(W−B)/V(A−B).
128
+ * @param {number} channel - 0 or 1.
129
+ * @param {number} ratio - 0.0–1.0; code is round(ratio × 256) clamped to 255.
130
+ * @returns {Promise<void>}
131
+ */
132
+ async setRatio(channel, ratio) {
133
+ await this._writeCode(channel, Math.floor(ratio * 256 + 0.5));
134
+ }
135
+
136
+ /**
137
+ * Read the divider ratio of a channel.
138
+ * @param {number} channel - 0 or 1.
139
+ * @returns {Promise<number>} `code / 256`.
140
+ */
141
+ async getRatio(channel) {
142
+ return (await this._readCode(channel)) / 256;
143
+ }
144
+
145
+ /**
146
+ * Set the W–A resistance (rheostat mode, B open) to the nearest code.
147
+ * @param {number} channel - 0 or 1.
148
+ * @param {number} ohms - Target W–A resistance in ohms.
149
+ * @returns {Promise<void>}
150
+ */
151
+ async setResistanceWa(channel, ohms) {
152
+ const steps = Math.floor((ohms - 2 * this._rW) / this._rAb * 256 + 0.5);
153
+ await this._writeCode(channel, 256 - steps);
154
+ }
155
+
156
+ /**
157
+ * Read the W–A resistance for the current wiper code.
158
+ * @param {number} channel - 0 or 1.
159
+ * @returns {Promise<number>} Resistance in ohms, `(256 − D) / 256 * RAB + 2 * RW`.
160
+ */
161
+ async getResistanceWa(channel) {
162
+ return (256 - (await this._readCode(channel))) / 256 * this._rAb + 2 * this._rW;
163
+ }
164
+
165
+ /**
166
+ * Move the wiper relative to its current position.
167
+ * @param {number} channel - 0 or 1.
168
+ * @param {number} [steps=1] - Signed step count (negative to decrement).
169
+ * @returns {Promise<number>} The new wiper code, clamped to 0–255.
170
+ */
171
+ async increment(channel, steps = 1) {
172
+ const code = _clampCode((await this._readCode(channel)) + Math.trunc(steps));
173
+ await this._writeCode(channel, code);
174
+ return code;
175
+ }
176
+
177
+ /**
178
+ * Shut a channel down: terminal A opens and the wiper shorts to B. The
179
+ * RDAC contents are kept.
180
+ * @param {number} channel - 0 or 1.
181
+ * @returns {Promise<void>}
182
+ */
183
+ async shutdown(channel) {
184
+ _checkChannel(channel);
185
+ this._sd[channel] = true;
186
+ await this._writeCode(channel, this._code[channel]);
187
+ }
188
+
189
+ /**
190
+ * Leave shutdown (SD = 0), rewriting the tracked current code.
191
+ * @param {number} channel - 0 or 1.
192
+ * @returns {Promise<void>}
193
+ */
194
+ async resume(channel) {
195
+ _checkChannel(channel);
196
+ this._sd[channel] = false;
197
+ await this._writeCode(channel, this._code[channel]);
198
+ }
199
+
200
+ /**
201
+ * Driver-tracked shutdown state (the chip cannot report it).
202
+ * @param {number} channel - 0 or 1.
203
+ * @returns {boolean} True if the channel was shut down by this driver.
204
+ */
205
+ isShutdown(channel) {
206
+ _checkChannel(channel);
207
+ return this._sd[channel];
208
+ }
209
+
210
+ /**
211
+ * Restore the power-up preset: both wipers 0x80, SD = 0.
212
+ * @returns {Promise<void>}
213
+ */
214
+ async reset() {
215
+ this._sd = [false, false];
216
+ await this._writeCode(0, _MIDSCALE);
217
+ await this._writeCode(1, _MIDSCALE);
218
+ }
219
+
220
+ /**
221
+ * Change the resistance parameters used by conversions (no I²C traffic).
222
+ * @param {number} rAb - End-to-end resistance RAB in ohms.
223
+ * @param {number} [rWiper] - Wiper resistance RW in ohms; unchanged if omitted.
224
+ */
225
+ setRab(rAb, rWiper) {
226
+ this._rAb = rAb;
227
+ if (rWiper !== undefined && rWiper !== null) this._rW = rWiper;
228
+ }
229
+ }
230
+
231
+ module.exports = { AD5243Minimal, AD5243Full };
@@ -0,0 +1,194 @@
1
+ 'use strict';
2
+
3
+ const _SD = 0x40;
4
+
5
+ /**
6
+ * AD5248 dual 256-position I²C digital rheostat — minimal interface.
7
+ *
8
+ * Sets and reads back the wiper position (0–255) or the wiper-to-B resistance of
9
+ * either channel. The chip has no register map: the instruction byte (channel select
10
+ * bit A0 in bit 7, shutdown bit SD in bit 6) plays the role of the register address,
11
+ * so the driver uses readReg/writeReg with the instruction byte.
12
+ *
13
+ * Default behaviour: RAB = 10 kΩ, RW = 160 Ω, both wipers at the power-up midscale
14
+ * (0x80), both channels assumed active; the constructor performs no I²C traffic.
15
+ */
16
+ class AD5248Minimal {
17
+ /**
18
+ * @param {import('../../connection/register_connection').RegisterConnection} connection - I²C register connection pointing at the device (0x2C–0x2F).
19
+ * @param {number} [rAb=10000] - End-to-end resistance RAB in ohms (2500, 10000, 50000 or 100000).
20
+ * @param {number} [rWiper=160] - Wiper resistance RW in ohms (typically 160 Ω at 5 V).
21
+ */
22
+ constructor(connection, rAb = 10000.0, rWiper = 160.0) {
23
+ this._conn = connection;
24
+ this._rAb = rAb;
25
+ this._rWiper = rWiper;
26
+ this._shutdown = [false, false];
27
+ }
28
+
29
+ _checkChannel(channel) {
30
+ if (channel !== 0 && channel !== 1) {
31
+ throw new RangeError('AD5248: channel must be 0 or 1');
32
+ }
33
+ }
34
+
35
+ _instruction(channel) {
36
+ return (channel << 7) | (this._shutdown[channel] ? _SD : 0);
37
+ }
38
+
39
+ /**
40
+ * Set the wiper position of a channel.
41
+ *
42
+ * Clamps the code to [0, 255]. On a shut-down channel the RDAC is updated and the
43
+ * channel stays in shutdown.
44
+ * @param {number} channel - Channel index, 0 or 1.
45
+ * @param {number} code - Wiper position 0–255 (0 = at B, 255 = at A).
46
+ * @returns {Promise<void>}
47
+ */
48
+ async setWiper(channel, code) {
49
+ this._checkChannel(channel);
50
+ const c = Math.max(0, Math.min(255, Math.trunc(code)));
51
+ await this._conn.writeReg(this._instruction(channel), [c]);
52
+ }
53
+
54
+ /**
55
+ * Read the wiper position of a channel.
56
+ *
57
+ * Sends the channel's instruction byte alone, then reads one byte.
58
+ * @param {number} channel - Channel index, 0 or 1.
59
+ * @returns {Promise<number>} Wiper position 0–255.
60
+ */
61
+ async getWiper(channel) {
62
+ this._checkChannel(channel);
63
+ return (await this._conn.readReg(this._instruction(channel), 1))[0];
64
+ }
65
+
66
+ /**
67
+ * Set the W–B resistance to the nearest reachable value.
68
+ * @param {number} channel - Channel index, 0 or 1.
69
+ * @param {number} ohms - Target wiper-to-B resistance in ohms (clamped to the reachable range).
70
+ * @returns {Promise<void>}
71
+ */
72
+ async setResistance(channel, ohms) {
73
+ this._checkChannel(channel);
74
+ const code = Math.round((ohms - 2 * this._rWiper) / this._rAb * 256);
75
+ await this.setWiper(channel, code);
76
+ }
77
+
78
+ /**
79
+ * Read the W–B resistance for the current wiper position.
80
+ * @param {number} channel - Channel index, 0 or 1.
81
+ * @returns {Promise<number>} Resistance in ohms, D / 256 * RAB + 2 * RW.
82
+ */
83
+ async getResistance(channel) {
84
+ const code = await this.getWiper(channel);
85
+ return code / 256 * this._rAb + 2 * this._rWiper;
86
+ }
87
+ }
88
+
89
+ /**
90
+ * AD5248 full interface — extends AD5248Minimal with ratio, increment, shutdown and reset.
91
+ */
92
+ class AD5248Full extends AD5248Minimal {
93
+ /**
94
+ * Write both wipers (two transactions).
95
+ * @param {number} code1 - Channel 0 wiper position 0–255.
96
+ * @param {number} code2 - Channel 1 wiper position 0–255.
97
+ * @returns {Promise<void>}
98
+ */
99
+ async setBoth(code1, code2) {
100
+ await this.setWiper(0, code1);
101
+ await this.setWiper(1, code2);
102
+ }
103
+
104
+ /**
105
+ * Set the wiper to a divider ratio of the full scale.
106
+ *
107
+ * Wiper code = round(ratio × 256) clamped to 255. Only meaningful if terminal A is
108
+ * wired externally; the AD5248 does not pin out A, so prefer setResistance.
109
+ * @param {number} channel - Channel index, 0 or 1.
110
+ * @param {number} ratio - Ratio 0.0–1.0.
111
+ * @returns {Promise<void>}
112
+ */
113
+ async setRatio(channel, ratio) {
114
+ const r = Math.max(0, Math.min(1, ratio));
115
+ await this.setWiper(channel, Math.min(255, Math.round(r * 256)));
116
+ }
117
+
118
+ /**
119
+ * Read the wiper position as a ratio.
120
+ * @param {number} channel - Channel index, 0 or 1.
121
+ * @returns {Promise<number>} code / 256.
122
+ */
123
+ async getRatio(channel) {
124
+ return (await this.getWiper(channel)) / 256;
125
+ }
126
+
127
+ /**
128
+ * Move the wiper by a number of steps.
129
+ * @param {number} channel - Channel index, 0 or 1.
130
+ * @param {number} [steps=1] - Steps to add (negative to decrement).
131
+ * @returns {Promise<number>} New wiper code, clamped to 0–255.
132
+ */
133
+ async increment(channel, steps = 1) {
134
+ const code = Math.max(0, Math.min(255, (await this.getWiper(channel)) + steps));
135
+ await this.setWiper(channel, code);
136
+ return code;
137
+ }
138
+
139
+ /**
140
+ * Put a channel in shutdown (terminal A open, wiper shorted to B); RDAC is kept.
141
+ * @param {number} channel - Channel index, 0 or 1.
142
+ * @returns {Promise<void>}
143
+ */
144
+ async shutdown(channel) {
145
+ this._checkChannel(channel);
146
+ const code = await this.getWiper(channel);
147
+ this._shutdown[channel] = true;
148
+ await this._conn.writeReg(this._instruction(channel), [code]);
149
+ }
150
+
151
+ /**
152
+ * Leave shutdown; the RDAC contents are restored to the wiper.
153
+ * @param {number} channel - Channel index, 0 or 1.
154
+ * @returns {Promise<void>}
155
+ */
156
+ async resume(channel) {
157
+ this._checkChannel(channel);
158
+ const code = await this.getWiper(channel);
159
+ this._shutdown[channel] = false;
160
+ await this._conn.writeReg(this._instruction(channel), [code]);
161
+ }
162
+
163
+ /**
164
+ * Return the driver-tracked shutdown state (the chip cannot be read back).
165
+ * @param {number} channel - Channel index, 0 or 1.
166
+ * @returns {boolean} True if the channel was shut down by this driver.
167
+ */
168
+ isShutdown(channel) {
169
+ this._checkChannel(channel);
170
+ return this._shutdown[channel];
171
+ }
172
+
173
+ /**
174
+ * Write midscale (0x80) to both channels with SD = 0 (power-up preset).
175
+ * @returns {Promise<void>}
176
+ */
177
+ async reset() {
178
+ this._shutdown = [false, false];
179
+ await this.setWiper(0, 0x80);
180
+ await this.setWiper(1, 0x80);
181
+ }
182
+
183
+ /**
184
+ * Change the resistance model used by the conversions (no I²C traffic).
185
+ * @param {number} rAb - End-to-end resistance RAB in ohms.
186
+ * @param {number} [rWiper] - Wiper resistance RW in ohms; unchanged if omitted.
187
+ */
188
+ setRab(rAb, rWiper) {
189
+ this._rAb = rAb;
190
+ if (rWiper !== undefined && rWiper !== null) this._rWiper = rWiper;
191
+ }
192
+ }
193
+
194
+ module.exports = { AD5248Minimal, AD5248Full };