node-red-contrib-periph-adc-dac 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.
@@ -0,0 +1,73 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-ds3502', {
3
+ category: 'Peripheral',
4
+ color: '#C084FC',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '0x28' },
9
+ vhigh: { value: '15', required: true, validate: RED.validators.number() },
10
+ vlow: { value: '0', required: true, validate: RED.validators.number() }
11
+ },
12
+ inputs: 1,
13
+ outputs: 1,
14
+ icon: 'font-awesome/fa-sliders',
15
+ label: function() { return this.name || 'DS3502'; },
16
+ paletteLabel: 'DS3502'
17
+ });
18
+ </script>
19
+
20
+ <script type="text/html" data-template-name="periph-ds3502">
21
+ <div class="form-row">
22
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
23
+ <input type="text" id="node-input-name" placeholder="DS3502">
24
+ </div>
25
+ <div class="form-row">
26
+ <label for="node-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
27
+ <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
28
+ </div>
29
+ <div class="form-row">
30
+ <label for="node-input-address"><i class="fa fa-at"></i> I²C address</label>
31
+ <select id="node-input-address" style="width:200px">
32
+ <option value="0x28">0x28 (A1 = A0 = GND)</option>
33
+ <option value="0x29">0x29 (A1 = GND, A0 = VCC)</option>
34
+ <option value="0x2A">0x2A (A1 = VCC, A0 = GND)</option>
35
+ <option value="0x2B">0x2B (A1 = A0 = VCC)</option>
36
+ </select>
37
+ </div>
38
+ <div class="form-row">
39
+ <label for="node-input-vhigh"><i class="fa fa-bolt"></i> RH voltage</label>
40
+ <input type="number" id="node-input-vhigh" step="any" style="width:80px"> &nbsp;V
41
+ </div>
42
+ <div class="form-row">
43
+ <label for="node-input-vlow"><i class="fa fa-bolt"></i> RL voltage</label>
44
+ <input type="number" id="node-input-vlow" step="any" style="width:80px"> &nbsp;V
45
+ </div>
46
+ </script>
47
+
48
+ <script type="text/html" data-help-name="periph-ds3502">
49
+ <p>Sets and reads the 128-position wiper of a DS3502 high-voltage I²C digital potentiometer. Wiper writes go to SRAM only (MODE = 1) and never wear the EEPROM; the wiper powers up at the stored Initial Value Register position (factory default 64, midscale).</p>
50
+
51
+ <h3>Inputs</h3>
52
+ <dl class="message-properties">
53
+ <dt>payload.code <span class="property-type">number</span></dt>
54
+ <dd>Wiper position 0–127 (clamped).</dd>
55
+ <dt>payload.ratio <span class="property-type">number</span></dt>
56
+ <dd>Without <code>code</code>: divider ratio 0.0–1.0, set to the nearest code.</dd>
57
+ <dt>payload.voltage <span class="property-type">number</span></dt>
58
+ <dd>Without <code>code</code> / <code>ratio</code>: unloaded RW voltage in volts, set to the nearest code between the configured RL and RH voltages.</dd>
59
+ <dt>payload.store <span class="property-type">boolean</span></dt>
60
+ <dd><code>true</code> writes the current wiper position into the Initial Value Register (EEPROM, power-up value). This consumes one of the limited EEPROM write cycles (50 000 at 70 °C).</dd>
61
+ <dt>payload <span class="property-type">any</span></dt>
62
+ <dd>Any other message reads back the wiper.</dd>
63
+ </dl>
64
+
65
+ <h3>Outputs</h3>
66
+ <dl class="message-properties">
67
+ <dt>payload.code, payload.ratio, payload.voltage <span class="property-type">number</span></dt>
68
+ <dd>The wiper code read back, the divider ratio (code / 127) and the unloaded RW voltage in volts. After a store, <code>payload.stored</code> is <code>true</code>.</dd>
69
+ </dl>
70
+
71
+ <h3>Configuration</h3>
72
+ <p>Set the Linux I²C bus number, the address (0x28–0x2B via A1, A0) and the voltages applied to RH and RL (up to 15.5 V; V+ must exceed VCC and the RH voltage). The wiper carries at most 1 mA, so buffer the output for heavier loads. Absolute resistance is only accurate to ±20 %; ratios are exact.</p>
73
+ </script>
@@ -0,0 +1,65 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { DS3502Full } = require('periph/src/chips/adc_dac/ds3502');
6
+
7
+ function DS3502Node(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+
11
+ const vHigh = config.vhigh === '' || config.vhigh === undefined ? 1.0 : parseFloat(config.vhigh);
12
+ const vLow = config.vlow === '' || config.vlow === undefined ? 0.0 : parseFloat(config.vlow);
13
+ const address = parseInt(config.address || '0x28', 16);
14
+ try {
15
+ const connection = new I2CConnection(parseInt(config.bus, 10), address);
16
+ node.driver = new DS3502Full(connection, vHigh, vLow);
17
+ node.connection = connection;
18
+ node.ready = node.driver.init();
19
+ node.ready.catch((e) => node.error('DS3502 init failed: ' + e.message));
20
+ } catch (e) {
21
+ node.error('DS3502 init failed: ' + e.message);
22
+ }
23
+
24
+ async function readback(extra) {
25
+ const code = await node.driver.getWiper();
26
+ const ratio = await node.driver.getRatio();
27
+ const voltage = await node.driver.getVoltage();
28
+ return Object.assign({ code, ratio, voltage }, extra);
29
+ }
30
+
31
+ node.on('input', async function(msg, send, done) {
32
+ if (!node.driver) { done(); return; }
33
+ try {
34
+ await node.ready;
35
+ const p = msg.payload;
36
+ let extra;
37
+ if (p && typeof p === 'object') {
38
+ if (typeof p.code === 'number') await node.driver.setWiper(p.code);
39
+ else if (typeof p.ratio === 'number') await node.driver.setRatio(p.ratio);
40
+ else if (typeof p.voltage === 'number') await node.driver.setVoltage(p.voltage);
41
+ if (p.store === true) {
42
+ await node.driver.storeWiper();
43
+ extra = { stored: true };
44
+ }
45
+ }
46
+ msg.payload = await readback(extra);
47
+ send(msg);
48
+ done();
49
+ } catch (e) {
50
+ done(e);
51
+ }
52
+ });
53
+
54
+ node.on('close', async function(removed, done) {
55
+ try {
56
+ if (node.connection) await node.connection.close();
57
+ } catch (e) {
58
+ // bus may already be gone; nothing more to do on close
59
+ } finally {
60
+ done();
61
+ }
62
+ });
63
+ }
64
+ RED.nodes.registerType('periph-ds3502', DS3502Node);
65
+ };
@@ -0,0 +1,111 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('es8311-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '18', required: true },
9
+ mclk: { value: '12288000', required: true, validate: RED.validators.number() },
10
+ sampleRate: { value: '48000', required: true, validate: RED.validators.number() },
11
+ volume: { value: '-40', validate: RED.validators.number(true) },
12
+ micGain: { value: '24', validate: RED.validators.number(true) }
13
+ },
14
+ label: function() {
15
+ return this.name || ('ES8311 @ 0x' + this.address + ' (bus ' + this.bus + ')');
16
+ }
17
+ });
18
+ </script>
19
+
20
+ <script type="text/html" data-template-name="es8311-device">
21
+ <div class="form-row">
22
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
23
+ <input type="text" id="node-config-input-name" placeholder="ES8311">
24
+ </div>
25
+ <div class="form-row">
26
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
27
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
28
+ </div>
29
+ <div class="form-row">
30
+ <label for="node-config-input-address">Address (hex)</label>
31
+ <select id="node-config-input-address" style="width:150px">
32
+ <option value="18">0x18 (CE low)</option>
33
+ <option value="19">0x19 (CE high)</option>
34
+ </select>
35
+ </div>
36
+ <div class="form-row">
37
+ <label for="node-config-input-mclk">MCLK (Hz)</label>
38
+ <input type="number" id="node-config-input-mclk" min="1" style="width:120px">
39
+ </div>
40
+ <div class="form-row">
41
+ <label for="node-config-input-sampleRate">Sample rate (Hz)</label>
42
+ <input type="number" id="node-config-input-sampleRate" min="1" style="width:120px">
43
+ </div>
44
+ <div class="form-row">
45
+ <label for="node-config-input-volume">Initial volume (dB)</label>
46
+ <input type="number" id="node-config-input-volume" min="-95.5" max="32" step="0.5" style="width:120px">
47
+ </div>
48
+ <div class="form-row">
49
+ <label for="node-config-input-micGain">Microphone gain (dB)</label>
50
+ <input type="number" id="node-config-input-micGain" min="0" max="30" step="3" style="width:120px"> &nbsp;0–30 dB
51
+ </div>
52
+ </script>
53
+
54
+ <!-- Control node: volume / mute / mic gain / readback -->
55
+ <script type="text/javascript">
56
+ RED.nodes.registerType('periph-es8311', {
57
+ category: 'Peripheral',
58
+ color: '#C084FC',
59
+ defaults: {
60
+ name: { value: '' },
61
+ device: { value: '', type: 'es8311-device', required: true }
62
+ },
63
+ inputs: 1,
64
+ outputs: 1,
65
+ icon: 'font-awesome/fa-volume-up',
66
+ label: function() { return this.name || 'ES8311'; },
67
+ paletteLabel: 'ES8311'
68
+ });
69
+ </script>
70
+
71
+ <script type="text/html" data-template-name="periph-es8311">
72
+ <div class="form-row">
73
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
74
+ <input type="text" id="node-input-device">
75
+ </div>
76
+ <div class="form-row">
77
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
78
+ <input type="text" id="node-input-name" placeholder="ES8311">
79
+ </div>
80
+ </script>
81
+
82
+ <script type="text/html" data-help-name="periph-es8311">
83
+ <p>Controls an ES8311 low-power mono audio codec over its I²C control port. The audio stream
84
+ itself (I²S) is not handled by this node.</p>
85
+
86
+ <h3>Inputs</h3>
87
+ <dl class="message-properties">
88
+ <dt class="optional">payload.volume <span class="property-type">number</span></dt>
89
+ <dd>DAC volume in dB (−95.5 … +32, 0.5 dB steps).</dd>
90
+ <dt class="optional">payload.mute <span class="property-type">boolean</span></dt>
91
+ <dd>Mute (true) or unmute (false) the DAC.</dd>
92
+ <dt class="optional">payload.mic_gain <span class="property-type">number</span></dt>
93
+ <dd>Analog microphone PGA gain in dB (0 … 30, 3 dB steps).</dd>
94
+ </dl>
95
+ <p>Several fields may be combined in one message. Any message without one of these fields
96
+ reads the current settings back.</p>
97
+
98
+ <h3>Output</h3>
99
+ <dl class="message-properties">
100
+ <dt>payload.volume <span class="property-type">number</span></dt>
101
+ <dd>DAC volume read back from the chip, in dB.</dd>
102
+ <dt>payload.mic_gain <span class="property-type">number</span></dt>
103
+ <dd>PGA gain read back from the chip, in dB.</dd>
104
+ </dl>
105
+ <p>Command messages produce no output.</p>
106
+
107
+ <h3>Configuration</h3>
108
+ <p>Set the I²C bus number, the 7-bit address (<code>0x18</code> with CE low, <code>0x19</code> with
109
+ CE high), the MCLK frequency and sample rate (must give a valid divider set, see the datasheet clock
110
+ table), the initial DAC volume and the microphone gain in the device config node.</p>
111
+ </script>
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { ES8311Full } = require('periph/src/chips/adc_dac/es8311');
6
+
7
+ function ES8311DeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const connection = new I2CConnection(parseInt(config.bus), parseInt(config.address, 16));
12
+ node.connection = connection;
13
+ node.driver = new ES8311Full(connection, parseInt(config.mclk) || 12288000, parseInt(config.sampleRate) || 48000);
14
+ node.ready = (async () => {
15
+ if (config.volume !== undefined && config.volume !== '') await node.driver.setVolume(parseFloat(config.volume));
16
+ if (config.micGain !== undefined && config.micGain !== '') await node.driver.setMicGain(parseFloat(config.micGain));
17
+ })();
18
+ node.ready.catch((e) => node.error('ES8311 init failed: ' + e.message));
19
+ } catch (e) {
20
+ node.error('ES8311 init failed: ' + e.message);
21
+ }
22
+ node.on('close', function() {
23
+ if (node.connection) node.connection.close();
24
+ });
25
+ }
26
+ RED.nodes.registerType('es8311-device', ES8311DeviceNode);
27
+
28
+ function ES8311Node(config) {
29
+ RED.nodes.createNode(this, config);
30
+ const node = this;
31
+ node.device = RED.nodes.getNode(config.device);
32
+
33
+ node.on('input', async function(msg, send, done) {
34
+ if (!node.device || !node.device.driver) {
35
+ node.error('No ES8311 device configured', msg);
36
+ done();
37
+ return;
38
+ }
39
+ try {
40
+ const d = node.device.driver;
41
+ const p = (msg.payload !== null && typeof msg.payload === 'object') ? msg.payload : {};
42
+ let command = false;
43
+ if (p.volume !== undefined) { await d.setVolume(Number(p.volume)); command = true; }
44
+ if (p.mute !== undefined) { await d.setMute(!!p.mute); command = true; }
45
+ if (p.mic_gain !== undefined) { await d.setMicGain(Number(p.mic_gain)); command = true; }
46
+ if (!command) {
47
+ msg.payload = { volume: await d.getVolume(), mic_gain: await d.getMicGain() };
48
+ send(msg);
49
+ }
50
+ done();
51
+ } catch (e) {
52
+ done(e);
53
+ }
54
+ });
55
+ }
56
+ RED.nodes.registerType('periph-es8311', ES8311Node);
57
+ };
@@ -0,0 +1,79 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-mcp3422', {
3
+ category: 'Peripheral',
4
+ color: '#C084FC',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '0x68' },
9
+ resolution: { value: '16' },
10
+ gain: { value: '1' }
11
+ },
12
+ inputs: 1,
13
+ outputs: 1,
14
+ icon: 'font-awesome/fa-area-chart',
15
+ label: function() { return this.name || 'MCP3422'; },
16
+ paletteLabel: 'MCP3422'
17
+ });
18
+ </script>
19
+
20
+ <script type="text/html" data-template-name="periph-mcp3422">
21
+ <div class="form-row">
22
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
23
+ <input type="text" id="node-input-name" placeholder="MCP3422">
24
+ </div>
25
+ <div class="form-row">
26
+ <label for="node-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
27
+ <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
28
+ </div>
29
+ <div class="form-row">
30
+ <label for="node-input-address"><i class="fa fa-at"></i> I²C address</label>
31
+ <select id="node-input-address" style="width:200px">
32
+ <option value="0x68">0x68 (order code A0)</option><option value="0x69">0x69 (A1)</option>
33
+ <option value="0x6A">0x6A (A2)</option><option value="0x6B">0x6B (A3)</option>
34
+ <option value="0x6C">0x6C (A4)</option><option value="0x6D">0x6D (A5)</option>
35
+ <option value="0x6E">0x6E (A6)</option><option value="0x6F">0x6F (A7)</option>
36
+ </select>
37
+ </div>
38
+ <div class="form-row">
39
+ <label for="node-input-resolution"><i class="fa fa-sliders"></i> Resolution</label>
40
+ <select id="node-input-resolution" style="width:200px">
41
+ <option value="12">12 bit / 240 SPS</option><option value="14">14 bit / 60 SPS</option>
42
+ <option value="16">16 bit / 15 SPS</option><option value="18">18 bit / 3.75 SPS</option>
43
+ </select>
44
+ </div>
45
+ <div class="form-row">
46
+ <label for="node-input-gain"><i class="fa fa-expand"></i> Gain</label>
47
+ <select id="node-input-gain" style="width:120px">
48
+ <option value="1">x1</option><option value="2">x2</option>
49
+ <option value="4">x4</option><option value="8">x8</option>
50
+ </select>
51
+ </div>
52
+ </script>
53
+
54
+ <script type="text/html" data-help-name="periph-mcp3422">
55
+ <p>Reads the two differential inputs of a MCP3422 18-bit delta-sigma ADC (I²C) with a one-shot conversion per channel. Full scale is ±2.048 V / gain; the LSB is 2·2.048 V / 2<sup>bits</sup> / gain.</p>
56
+
57
+ <h3>Inputs</h3>
58
+ <dl class="message-properties">
59
+ <dt>channel <span class="property-type">number</span></dt>
60
+ <dd>Optional: 0 (CH1) or 1 (CH2). With it only that channel is converted.</dd>
61
+ <dt>gain <span class="property-type">number</span></dt>
62
+ <dd>Optional: PGA gain 1, 2, 4 or 8; applied before the conversion and kept.</dd>
63
+ <dt>resolution <span class="property-type">number</span></dt>
64
+ <dd>Optional: 12, 14, 16 or 18 bit; applied before the conversion and kept (the 18 bit conversion takes about 270 ms).</dd>
65
+ <dt>payload <span class="property-type">any</span></dt>
66
+ <dd>Any message triggers a conversion.</dd>
67
+ </dl>
68
+
69
+ <h3>Outputs</h3>
70
+ <dl class="message-properties">
71
+ <dt>payload.channels, payload.raw <span class="property-type">array</span></dt>
72
+ <dd>Without <code>msg.channel</code>: the CH1 and CH2 voltages in volts and the signed codes.</dd>
73
+ <dt>payload.channel, payload.raw, payload.voltage <span class="property-type">number</span></dt>
74
+ <dd>With <code>msg.channel</code>: the channel, its signed code and its voltage in volts.</dd>
75
+ </dl>
76
+
77
+ <h3>Configuration</h3>
78
+ <p>Set the Linux I²C bus number, the factory-programmed address (0x68–0x6F, fixed by the order code) and the initial resolution and gain. Inputs must stay within VSS − 0.3 V … VDD + 0.3 V.</p>
79
+ </script>
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { MCP3422Full } = require('periph/src/chips/adc_dac/mcp3422');
6
+
7
+ function MCP3422Node(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+
11
+ try {
12
+ const address = parseInt(config.address || '0x68', 16);
13
+ node.connection = new I2CConnection(parseInt(config.bus, 10), address);
14
+ node.driver = new MCP3422Full(node.connection);
15
+ node.ready = (async () => {
16
+ await node.driver.init();
17
+ await node.driver.setResolution(parseInt(config.resolution || '16', 10));
18
+ await node.driver.setGain(parseInt(config.gain || '1', 10));
19
+ })();
20
+ node.ready.then(() => node.status({ fill: 'green', shape: 'dot', text: 'I2C' }))
21
+ .catch((e) => node.error('MCP3422 init failed: ' + e.message));
22
+ } catch (e) {
23
+ node.error('MCP3422 init failed: ' + e.message);
24
+ }
25
+
26
+ node.on('input', async function(msg, send, done) {
27
+ if (!node.driver) { done(new Error('MCP3422 not initialised')); return; }
28
+ try {
29
+ await node.ready;
30
+ // msg.gain / msg.resolution apply before the conversion and persist.
31
+ if (msg.resolution !== undefined) await node.driver.setResolution(Number(msg.resolution));
32
+ if (msg.gain !== undefined) await node.driver.setGain(Number(msg.gain));
33
+ const lsb = node.driver.lsb();
34
+ const gain = node.driver.getGain();
35
+ if (msg.channel !== undefined) {
36
+ const channel = Number(msg.channel) < 1 ? 0 : 1;
37
+ const raw = await node.driver.readRaw(channel);
38
+ msg.payload = { channel, raw, voltage: raw * lsb / gain };
39
+ } else {
40
+ const raw0 = await node.driver.readRaw(0);
41
+ const raw1 = await node.driver.readRaw(1);
42
+ msg.payload = { channels: [raw0 * lsb / gain, raw1 * lsb / gain], raw: [raw0, raw1] };
43
+ }
44
+ send(msg);
45
+ done();
46
+ } catch (e) {
47
+ done(e);
48
+ }
49
+ });
50
+
51
+ node.on('close', async function(removed, done) {
52
+ try {
53
+ if (node.connection) await node.connection.close();
54
+ } catch (e) {
55
+ // bus may already be gone; nothing more to do on close
56
+ } finally {
57
+ done();
58
+ }
59
+ });
60
+ }
61
+ RED.nodes.registerType('periph-mcp3422', MCP3422Node);
62
+ };
@@ -0,0 +1,167 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('mcp4xxx-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ part: { value: 'MCP4532', required: true },
8
+ bus: { value: '1', required: true, validate: RED.validators.number() },
9
+ address: { value: '58', required: true, validate: RED.validators.regex(/^[0-9a-fA-F]{1,2}$/) },
10
+ rab: { value: '10000', required: true, validate: RED.validators.number() },
11
+ rwiper: { value: '75', required: true, validate: RED.validators.number() },
12
+ gpioChip: { value: '0' },
13
+ hvcPin: { value: '' }
14
+ },
15
+ label: function() {
16
+ return this.name || (this.part + ' @ 0x' + this.address + ' (bus ' + this.bus + ')');
17
+ },
18
+ oneditprepare: function() {
19
+ // Part digits "4 S M P": S = 5 single / 6 dual, M = 3/5 volatile, 4/6 non-volatile, P = 1 pot / 2 rheostat.
20
+ // The address range follows the part: 45X1 0x2E-0x2F, 45X2 / 46X2 0x58-0x5B, 46X1 0x28-0x2F.
21
+ function fill() {
22
+ const d = $('#node-config-input-part').val().slice(3);
23
+ const dual = d[1] === '6';
24
+ const nv = d[2] === '4' || d[2] === '6';
25
+ const pot = d[3] === '1';
26
+ const lo = pot ? (dual ? 0x28 : 0x2E) : 0x58;
27
+ const hi = pot ? 0x2F : 0x5B;
28
+ const sel = $('#node-config-input-address');
29
+ const cur = parseInt(sel.val() || '0', 16);
30
+ sel.empty();
31
+ for (let a = lo; a <= hi; a++) sel.append($('<option>').val(a.toString(16).toUpperCase()).text('0x' + a.toString(16).toUpperCase()));
32
+ sel.val((cur >= lo && cur <= hi ? cur : lo).toString(16).toUpperCase());
33
+ $('#mcp4xxx-hvc-row').toggle(nv);
34
+ }
35
+ $('#node-config-input-part').on('change', fill);
36
+ fill();
37
+ }
38
+ });
39
+ </script>
40
+
41
+ <script type="text/html" data-template-name="mcp4xxx-device">
42
+ <div class="form-row">
43
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
44
+ <input type="text" id="node-config-input-name" placeholder="MCP4XXX">
45
+ </div>
46
+ <div class="form-row">
47
+ <label for="node-config-input-part">Part</label>
48
+ <select id="node-config-input-part" style="width:120px">
49
+ <option value="MCP4531">MCP4531</option>
50
+ <option value="MCP4532">MCP4532</option>
51
+ <option value="MCP4541">MCP4541</option>
52
+ <option value="MCP4542">MCP4542</option>
53
+ <option value="MCP4551">MCP4551</option>
54
+ <option value="MCP4552">MCP4552</option>
55
+ <option value="MCP4561">MCP4561</option>
56
+ <option value="MCP4562">MCP4562</option>
57
+ <option value="MCP4631">MCP4631</option>
58
+ <option value="MCP4632">MCP4632</option>
59
+ <option value="MCP4641">MCP4641</option>
60
+ <option value="MCP4642">MCP4642</option>
61
+ <option value="MCP4651">MCP4651</option>
62
+ <option value="MCP4652">MCP4652</option>
63
+ <option value="MCP4661">MCP4661</option>
64
+ <option value="MCP4662">MCP4662</option>
65
+ </select>
66
+ </div>
67
+ <div class="form-row">
68
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
69
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
70
+ </div>
71
+ <div class="form-row">
72
+ <label for="node-config-input-address">Address (hex)</label>
73
+ <select id="node-config-input-address" style="width:100px"></select>
74
+ </div>
75
+ <div class="form-row">
76
+ <label for="node-config-input-rab">RAB (Ω)</label>
77
+ <select id="node-config-input-rab" style="width:100px">
78
+ <option value="5000">5 kΩ</option>
79
+ <option value="10000">10 kΩ</option>
80
+ <option value="50000">50 kΩ</option>
81
+ <option value="100000">100 kΩ</option>
82
+ </select>
83
+ </div>
84
+ <div class="form-row">
85
+ <label for="node-config-input-rwiper">Wiper R (Ω)</label>
86
+ <input type="number" id="node-config-input-rwiper" min="0" style="width:100px"> &nbsp;typical 75 Ω
87
+ </div>
88
+ <div class="form-row" id="mcp4xxx-hvc-row">
89
+ <label for="node-config-input-hvcPin">HVC GPIO</label>
90
+ <input type="number" id="node-config-input-hvcPin" min="0" style="width:80px" placeholder="none"> &nbsp;chip
91
+ <input type="number" id="node-config-input-gpioChip" min="0" style="width:60px">
92
+ <div class="form-tips">Non-volatile parts only: GPIO line that switches the external 8.5-12.5 V circuit on HVC/A0 (enables WiperLock / write-protect commands). Leave empty if unused.</div>
93
+ </div>
94
+ </script>
95
+
96
+ <!-- Node: set or read the wipers -->
97
+ <script type="text/javascript">
98
+ RED.nodes.registerType('periph-mcp4xxx', {
99
+ category: 'Peripheral',
100
+ color: '#C084FC',
101
+ defaults: {
102
+ name: { value: '' },
103
+ device: { value: '', type: 'mcp4xxx-device', required: true }
104
+ },
105
+ inputs: 1,
106
+ outputs: 1,
107
+ icon: 'font-awesome/fa-sliders',
108
+ label: function() { return this.name || 'MCP4XXX'; },
109
+ paletteLabel: 'MCP4XXX'
110
+ });
111
+ </script>
112
+
113
+ <script type="text/html" data-template-name="periph-mcp4xxx">
114
+ <div class="form-row">
115
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
116
+ <input type="text" id="node-input-device">
117
+ </div>
118
+ <div class="form-row">
119
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
120
+ <input type="text" id="node-input-name" placeholder="MCP4XXX">
121
+ </div>
122
+ </script>
123
+
124
+ <script type="text/html" data-help-name="periph-mcp4xxx">
125
+ <p>Sets or reads the volatile wiper(s) of an MCP4XXX I²C digital potentiometer / rheostat
126
+ (MCP4531 … MCP4662: 7 / 8-bit, one or two networks, volatile or non-volatile). The device is
127
+ configured via an <b>mcp4xxx-device</b> config node.</p>
128
+
129
+ <h3>Inputs</h3>
130
+ <dl class="message-properties">
131
+ <dt>payload.channel <span class="property-type">number</span></dt>
132
+ <dd>Network 0 (default) or 1 (dual parts).</dd>
133
+ <dt>payload.code <span class="property-type">number</span></dt>
134
+ <dd>Wiper position 0–128 (7-bit parts) / 0–256 (8-bit parts). Takes precedence over the other set fields.</dd>
135
+ <dt>payload.ohms <span class="property-type">number</span></dt>
136
+ <dd>Target wiper-to-B resistance in ohms; the nearest reachable code is used.</dd>
137
+ <dt>payload.ratio <span class="property-type">number</span></dt>
138
+ <dd>Divider ratio 0–1.</dd>
139
+ <dt>payload.step <span class="property-type">number</span></dt>
140
+ <dd>Increment (positive) or decrement (negative) the wiper by that many steps.</dd>
141
+ <dt>payload.shutdown <span class="property-type">boolean</span></dt>
142
+ <dd><code>true</code> shuts the network down (A open, W tied to B), <code>false</code> resumes.</dd>
143
+ <dt>payload.store <span class="property-type">boolean</span></dt>
144
+ <dd>Non-volatile parts: store the current wiper in the EEPROM wiper.</dd>
145
+ <dt>payload.data <span class="property-type">object</span></dt>
146
+ <dd>Non-volatile parts: <code>{ index: 0-9, value?: 0-511 }</code> reads (and, with <code>value</code>, writes) a general-purpose EEPROM register.</dd>
147
+ </dl>
148
+ <p>Any other message reads back all networks.</p>
149
+
150
+ <h3>Outputs</h3>
151
+ <dl class="message-properties">
152
+ <dt>payload.channel, payload.code, payload.resistance</dt>
153
+ <dd>After a write: the network, the resulting wiper code and its W–B resistance (Ω); plus
154
+ <code>shutdown</code> / <code>stored</code> where requested.</dd>
155
+ <dt>payload.index, payload.value</dt>
156
+ <dd>After an EEPROM data access.</dd>
157
+ <dt>payload.wiper, payload.resistance <span class="property-type">array</span></dt>
158
+ <dd>After a read: one entry per network; non-volatile parts also return <code>payload.status</code>
159
+ (<code>eepromBusy</code>, <code>wiperLock</code>, <code>writeProtect</code>).</dd>
160
+ </dl>
161
+
162
+ <h3>Configuration</h3>
163
+ <p>Choose the part, I²C bus and address (limited to the part's range), the end-to-end resistance RAB
164
+ (5 / 10 / 50 / 100 kΩ) and the wiper resistance RW. Volatile wipers return to mid-scale at power-up;
165
+ non-volatile parts restore the stored EEPROM wiper. The optional HVC GPIO (non-volatile parts) drives the
166
+ external high-voltage circuit needed for WiperLock / write-protect changes.</p>
167
+ </script>