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,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 };
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
const _INSTR_RS = 0x40;
|
|
4
|
+
const _INSTR_SD = 0x20;
|
|
5
|
+
const _MIDSCALE = 0x80;
|
|
6
|
+
|
|
7
|
+
function _clampCode(code) {
|
|
8
|
+
code = Math.trunc(code);
|
|
9
|
+
if (code < 0) return 0;
|
|
10
|
+
if (code > 255) return 255;
|
|
11
|
+
return code;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* CAT5171 256-position I²C digital potentiometer (ON Semiconductor) —
|
|
16
|
+
* minimal interface.
|
|
17
|
+
*
|
|
18
|
+
* A single 3-terminal potentiometer (A, W, B). The chip has no register map:
|
|
19
|
+
* a write is `[instruction, data]` where the instruction byte carries the
|
|
20
|
+
* midscale-reset flag (RS, bit 6) and the shutdown flag (SD, bit 5); a read is
|
|
21
|
+
* a bare one-byte read returning the wiper register. The driver therefore
|
|
22
|
+
* uses the plain Connection `write()` / `read()` calls. The I²C address is
|
|
23
|
+
* 0x2C (AD0 = GND) or 0x2D (AD0 = VDD). The wiper is volatile (power-up
|
|
24
|
+
* midscale 0x80).
|
|
25
|
+
*/
|
|
26
|
+
class CAT5171Minimal {
|
|
27
|
+
/**
|
|
28
|
+
* @param {import('../../connection/connection').Connection} connection - I²C connection (0x2C or 0x2D).
|
|
29
|
+
* @param {number} [rAb=100000] - End-to-end resistance RAB in ohms.
|
|
30
|
+
* @param {number} [rWiper=50] - Wiper resistance RW in ohms.
|
|
31
|
+
*/
|
|
32
|
+
constructor(connection, rAb = 100000.0, rWiper = 50.0) {
|
|
33
|
+
this._conn = connection;
|
|
34
|
+
this._rAb = rAb;
|
|
35
|
+
this._rW = rWiper;
|
|
36
|
+
this._code = _MIDSCALE;
|
|
37
|
+
this._sd = false;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async _write(instruction, code) {
|
|
41
|
+
await this._conn.write(Buffer.from([instruction, code]));
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async _writeCode(code) {
|
|
45
|
+
code = _clampCode(code);
|
|
46
|
+
await this._write(this._sd ? _INSTR_SD : 0, code);
|
|
47
|
+
this._code = code;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
async _readCode() {
|
|
51
|
+
const buf = await this._conn.read(1);
|
|
52
|
+
this._code = buf[0];
|
|
53
|
+
return buf[0];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Prepare the driver. The CAT5171 has no identity register and needs no
|
|
58
|
+
* initialisation writes, so this performs no I²C traffic.
|
|
59
|
+
* @returns {Promise<void>}
|
|
60
|
+
*/
|
|
61
|
+
async init() {}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Set the wiper position.
|
|
65
|
+
* @param {number} code - Wiper position 0–255 (clamped).
|
|
66
|
+
* @returns {Promise<void>}
|
|
67
|
+
*/
|
|
68
|
+
async setWiper(code) {
|
|
69
|
+
await this._writeCode(code);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* Read back the wiper position with a single read transaction.
|
|
74
|
+
* @returns {Promise<number>} Wiper position 0–255.
|
|
75
|
+
*/
|
|
76
|
+
async getWiper() {
|
|
77
|
+
return this._readCode();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Set the W–B resistance to the nearest reachable code.
|
|
82
|
+
* @param {number} ohms - Target W–B resistance in ohms (clamped to the reachable range).
|
|
83
|
+
* @returns {Promise<void>}
|
|
84
|
+
*/
|
|
85
|
+
async setResistance(ohms) {
|
|
86
|
+
await this._writeCode(Math.floor((ohms - this._rW) / this._rAb * 256 + 0.5));
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Read the W–B resistance for the current wiper code.
|
|
91
|
+
* @returns {Promise<number>} Resistance in ohms, `D / 256 * RAB + RW`.
|
|
92
|
+
*/
|
|
93
|
+
async getResistance() {
|
|
94
|
+
return (await this._readCode()) / 256 * this._rAb + this._rW;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** 7-bit I²C address with AD0 = GND. */
|
|
99
|
+
CAT5171Minimal.I2C_ADDRESS = 0x2C;
|
|
100
|
+
/** 7-bit I²C address with AD0 = VDD. */
|
|
101
|
+
CAT5171Minimal.I2C_ADDRESS_AD0_HIGH = 0x2D;
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* CAT5171 full interface — extends Minimal with ratio and W–A helpers,
|
|
105
|
+
* increment/decrement, midscale, shutdown and reset. The shutdown state is
|
|
106
|
+
* write-only on the chip, so the driver tracks it.
|
|
107
|
+
*/
|
|
108
|
+
class CAT5171Full extends CAT5171Minimal {
|
|
109
|
+
/**
|
|
110
|
+
* Set the wiper by divider ratio V(W−B)/V(A−B).
|
|
111
|
+
* @param {number} ratio - 0.0–1.0; code is round(ratio × 256) clamped to 255.
|
|
112
|
+
* @returns {Promise<void>}
|
|
113
|
+
*/
|
|
114
|
+
async setRatio(ratio) {
|
|
115
|
+
await this._writeCode(Math.floor(ratio * 256 + 0.5));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Read the divider ratio.
|
|
120
|
+
* @returns {Promise<number>} `code / 256`.
|
|
121
|
+
*/
|
|
122
|
+
async getRatio() {
|
|
123
|
+
return (await this._readCode()) / 256;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Set the W–A resistance (rheostat mode, B open) to the nearest code.
|
|
128
|
+
* @param {number} ohms - Target W–A resistance in ohms.
|
|
129
|
+
* @returns {Promise<void>}
|
|
130
|
+
*/
|
|
131
|
+
async setResistanceWa(ohms) {
|
|
132
|
+
const steps = Math.floor((ohms - this._rW) / this._rAb * 256 + 0.5);
|
|
133
|
+
await this._writeCode(256 - steps);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Read the W–A resistance for the current wiper code.
|
|
138
|
+
* @returns {Promise<number>} Resistance in ohms, `(256 − D) / 256 * RAB + RW`.
|
|
139
|
+
*/
|
|
140
|
+
async getResistanceWa() {
|
|
141
|
+
return (256 - (await this._readCode())) / 256 * this._rAb + this._rW;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* Move the wiper relative to its current position.
|
|
146
|
+
* @param {number} [steps=1] - Signed step count (negative to decrement).
|
|
147
|
+
* @returns {Promise<number>} The new wiper code, clamped to 0–255.
|
|
148
|
+
*/
|
|
149
|
+
async increment(steps = 1) {
|
|
150
|
+
const code = _clampCode((await this._readCode()) + Math.trunc(steps));
|
|
151
|
+
await this._writeCode(code);
|
|
152
|
+
return code;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Move the wiper to midscale (0x80) with the RS instruction bit; SD is
|
|
157
|
+
* kept if the device is shut down.
|
|
158
|
+
* @returns {Promise<void>}
|
|
159
|
+
*/
|
|
160
|
+
async midscale() {
|
|
161
|
+
await this._write(_INSTR_RS | (this._sd ? _INSTR_SD : 0), _MIDSCALE);
|
|
162
|
+
this._code = _MIDSCALE;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/**
|
|
166
|
+
* Shut down: terminal A opens and the wiper shorts to B. The wiper
|
|
167
|
+
* register is kept.
|
|
168
|
+
* @returns {Promise<void>}
|
|
169
|
+
*/
|
|
170
|
+
async shutdown() {
|
|
171
|
+
this._sd = true;
|
|
172
|
+
await this._write(_INSTR_SD, this._code);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Leave shutdown (SD = 0), rewriting the tracked current code.
|
|
177
|
+
* @returns {Promise<void>}
|
|
178
|
+
*/
|
|
179
|
+
async resume() {
|
|
180
|
+
this._sd = false;
|
|
181
|
+
await this._write(0, this._code);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Driver-tracked shutdown state (the chip cannot report it).
|
|
186
|
+
* @returns {boolean} True if the device was shut down by this driver.
|
|
187
|
+
*/
|
|
188
|
+
isShutdown() {
|
|
189
|
+
return this._sd;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Midscale reset (RS = 1, SD = 0): wiper to 0x80, shutdown cleared.
|
|
194
|
+
* @returns {Promise<void>}
|
|
195
|
+
*/
|
|
196
|
+
async reset() {
|
|
197
|
+
this._sd = false;
|
|
198
|
+
await this._write(_INSTR_RS, _MIDSCALE);
|
|
199
|
+
this._code = _MIDSCALE;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Change the resistance parameters used by conversions (no I²C traffic).
|
|
204
|
+
* @param {number} rAb - End-to-end resistance RAB in ohms.
|
|
205
|
+
* @param {number} [rWiper] - Wiper resistance RW in ohms; unchanged if omitted.
|
|
206
|
+
*/
|
|
207
|
+
setRab(rAb, rWiper) {
|
|
208
|
+
this._rAb = rAb;
|
|
209
|
+
if (rWiper !== undefined && rWiper !== null) this._rW = rWiper;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
module.exports = { CAT5171Minimal, CAT5171Full };
|