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.
- package/README.md +27 -0
- package/examples/adc_dac/ad5251/complete.js +66 -0
- package/examples/adc_dac/ad5251/demo.js +69 -0
- package/examples/adc_dac/ad5251/minimal.js +23 -0
- package/examples/adc_dac/ad5252/complete.js +66 -0
- package/examples/adc_dac/ad5252/demo.js +69 -0
- package/examples/adc_dac/ad5252/minimal.js +23 -0
- package/examples/adc_dac/ads7828/complete.js +46 -0
- package/examples/adc_dac/ads7828/demo.js +45 -0
- package/examples/adc_dac/ads7828/minimal.js +17 -0
- package/examples/adc_dac/cat5171/complete.js +52 -0
- package/examples/adc_dac/cat5171/demo.js +59 -0
- package/examples/adc_dac/cat5171/minimal.js +23 -0
- package/examples/adc_dac/cs43l22/complete.js +88 -0
- package/examples/adc_dac/cs43l22/demo.js +56 -0
- package/examples/adc_dac/cs43l22/minimal.js +20 -0
- package/examples/adc_dac/ds1881/complete.js +62 -0
- package/examples/adc_dac/ds1881/demo.js +61 -0
- package/examples/adc_dac/ds1881/minimal.js +25 -0
- package/examples/adc_dac/ds3502/complete.js +46 -0
- package/examples/adc_dac/ds3502/demo.js +54 -0
- package/examples/adc_dac/ds3502/minimal.js +28 -0
- package/examples/adc_dac/mcp3422/complete.js +48 -0
- package/examples/adc_dac/mcp3422/demo.js +49 -0
- package/examples/adc_dac/mcp3422/minimal.js +25 -0
- package/examples/display/sh1106/complete.js +101 -0
- package/examples/display/sh1106/demo.js +68 -0
- package/examples/display/sh1106/minimal.js +25 -0
- package/examples/display/ssd1305/complete.js +129 -0
- package/examples/display/ssd1305/demo.js +71 -0
- package/examples/display/ssd1305/minimal.js +25 -0
- package/examples/display/ssd1306/complete.js +115 -0
- package/examples/display/ssd1306/demo.js +70 -0
- package/examples/display/ssd1306/minimal.js +25 -0
- package/examples/environmental/am2315/complete.js +34 -0
- package/examples/environmental/am2315/demo.js +46 -0
- package/examples/environmental/am2315/minimal.js +21 -0
- package/examples/humidity/htu21df/complete.js +49 -0
- package/examples/humidity/htu21df/demo.js +50 -0
- package/examples/humidity/htu21df/minimal.js +22 -0
- package/examples/humidity/sht31/complete.js +53 -0
- package/examples/humidity/sht31/demo.js +53 -0
- package/examples/humidity/sht31/minimal.js +21 -0
- package/examples/humidity/si7021/complete.js +55 -0
- package/examples/humidity/si7021/demo.js +58 -0
- package/examples/humidity/si7021/minimal.js +21 -0
- package/examples/imu/bno055/complete.js +85 -0
- package/examples/imu/bno055/demo.js +70 -0
- package/examples/imu/bno055/minimal.js +20 -0
- package/examples/io_expander/pca6408a/complete.js +63 -0
- package/examples/io_expander/pca6408a/demo.js +58 -0
- package/examples/io_expander/pca6408a/minimal.js +20 -0
- package/examples/io_expander/pca6416a/complete.js +68 -0
- package/examples/io_expander/pca6416a/demo.js +60 -0
- package/examples/io_expander/pca6416a/minimal.js +20 -0
- package/examples/io_expander/pca9536/complete.js +63 -0
- package/examples/io_expander/pca9536/demo.js +57 -0
- package/examples/io_expander/pca9536/minimal.js +20 -0
- package/examples/io_expander/pca9539/complete.js +68 -0
- package/examples/io_expander/pca9539/demo.js +60 -0
- package/examples/io_expander/pca9539/minimal.js +20 -0
- package/examples/light/max44009/complete.js +56 -0
- package/examples/light/max44009/demo.js +49 -0
- package/examples/light/max44009/minimal.js +22 -0
- package/examples/memory/at24c02n/complete.js +39 -0
- package/examples/memory/at24c02n/demo.js +39 -0
- package/examples/memory/at24c02n/minimal.js +15 -0
- package/examples/memory/at24c64/complete.js +39 -0
- package/examples/memory/at24c64/demo.js +39 -0
- package/examples/memory/at24c64/minimal.js +15 -0
- package/examples/memory/cat24c512/complete.js +39 -0
- package/examples/memory/cat24c512/demo.js +39 -0
- package/examples/memory/cat24c512/minimal.js +15 -0
- package/examples/other/pca9541/complete.js +52 -0
- package/examples/other/pca9541/demo.js +61 -0
- package/examples/other/pca9541/minimal.js +23 -0
- package/examples/temperature/tmp102/complete.js +74 -0
- package/examples/temperature/tmp102/demo.js +71 -0
- package/examples/temperature/tmp102/minimal.js +20 -0
- package/examples/tof/vl6180x/complete.js +62 -0
- package/examples/tof/vl6180x/demo.js +77 -0
- package/examples/tof/vl6180x/minimal.js +23 -0
- package/package.json +1 -1
- package/src/chips/adc_dac/ad5251.js +345 -0
- package/src/chips/adc_dac/ad5252.js +345 -0
- package/src/chips/adc_dac/ads7828.js +225 -0
- package/src/chips/adc_dac/cat5171.js +213 -0
- package/src/chips/adc_dac/cs43l22.js +645 -0
- package/src/chips/adc_dac/ds1881.js +357 -0
- package/src/chips/adc_dac/ds3502.js +241 -0
- package/src/chips/adc_dac/mcp3422.js +312 -0
- package/src/chips/display/mono_oled.js +182 -0
- package/src/chips/display/sh1106.js +496 -0
- package/src/chips/display/ssd1305.js +655 -0
- package/src/chips/display/ssd1306.js +458 -0
- package/src/chips/environmental/am2315.js +237 -0
- package/src/chips/humidity/htu21df.js +222 -0
- package/src/chips/humidity/sht31.js +284 -0
- package/src/chips/humidity/si7021.js +282 -0
- package/src/chips/imu/bno055.js +496 -0
- package/src/chips/io_expander/pca6408a.js +407 -0
- package/src/chips/io_expander/pca6416a.js +455 -0
- package/src/chips/io_expander/pca9536.js +398 -0
- package/src/chips/io_expander/pca9539.js +457 -0
- package/src/chips/light/max44009.js +336 -0
- package/src/chips/memory/at24c02n.js +220 -0
- package/src/chips/memory/at24c64.js +220 -0
- package/src/chips/memory/cat24c512.js +221 -0
- package/src/chips/other/pca9541.js +315 -0
- package/src/chips/temperature/tmp102.js +361 -0
- package/src/chips/tof/vl6180x.js +580 -0
- package/src/discovery/registry.js +65 -13
|
@@ -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 > 63) return 63;
|
|
33
|
+
return code;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* AD5251 dual 64-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 32 until
|
|
45
|
+
* a value is stored).
|
|
46
|
+
*/
|
|
47
|
+
class AD5251Minimal {
|
|
48
|
+
/**
|
|
49
|
+
* @param {import('../../connection/register_connection').RegisterConnection} connection - I²C or SMBus register connection (0x2C–0x2F).
|
|
50
|
+
* @param {number} [rAb=50000] - End-to-end resistance RAB in ohms.
|
|
51
|
+
* @param {number} [rWiper=75] - Wiper resistance RW in ohms.
|
|
52
|
+
*/
|
|
53
|
+
constructor(connection, rAb = 50000.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 AD5251 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–63 (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–63.
|
|
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 * 64 + 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 / 64 * RAB + RW`.
|
|
111
|
+
*/
|
|
112
|
+
async getResistance(channel) {
|
|
113
|
+
return (await this._readCode(channel)) / 64 * this._rAb + this._rW;
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* AD5251 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 AD5251Full extends AD5251Minimal {
|
|
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('AD5251 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–63.
|
|
155
|
+
* @param {number} code1 - Channel 1 wiper position 0–63.
|
|
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 * 64) clamped to 63.
|
|
166
|
+
* @returns {Promise<void>}
|
|
167
|
+
*/
|
|
168
|
+
async setRatio(channel, ratio) {
|
|
169
|
+
await this._writeCode(channel, Math.floor(ratio * 64 + 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 / 64`.
|
|
176
|
+
*/
|
|
177
|
+
async getRatio(channel) {
|
|
178
|
+
return (await this._readCode(channel)) / 64;
|
|
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 * 64 + 0.5);
|
|
189
|
+
await this._writeCode(channel, 64 - 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, `(64 - D) / 64 * RAB + RW`.
|
|
196
|
+
*/
|
|
197
|
+
async getResistanceWa(channel) {
|
|
198
|
+
return (64 - (await this._readCode(channel))) / 64 * 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–63.
|
|
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
|
+
AD5251Minimal.I2C_ADDRESS = 0x2C;
|
|
344
|
+
|
|
345
|
+
module.exports = { AD5251Minimal, AD5251Full };
|
|
@@ -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 };
|