node-red-contrib-periph-adc-dac 1.4.0 → 1.5.1

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,78 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { CS43L22Full, BEEP, FMT } = require('periph/src/chips/adc_dac/cs43l22');
6
+
7
+ function CS43L22Node(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ node.ready = null;
11
+
12
+ try {
13
+ const address = parseInt(config.address, 16) || 0x4A;
14
+ const connection = new I2CConnection(parseInt(config.bus, 10), address);
15
+ let resPin = null;
16
+ if (config.resGpio !== undefined && config.resGpio !== '') {
17
+ // opengpio is optional: only required when a RESET GPIO is configured.
18
+ const { Default } = require('opengpio');
19
+ const { GpioOutputPin } = require('periph/src/connection/output_pin');
20
+ resPin = new GpioOutputPin(Default.output({ chip: parseInt(config.gpioChip || 0, 10), line: parseInt(config.resGpio, 10) }));
21
+ }
22
+ node.driver = new CS43L22Full(connection, resPin);
23
+ node.connection = connection;
24
+ const fmt = { i2s: FMT.I2S, lj: FMT.LJ, rj: FMT.RJ }[config.format] ?? FMT.I2S;
25
+ node.ready = (async () => {
26
+ await node.driver.init();
27
+ if (fmt !== FMT.I2S) await node.driver.setInterface({ fmt });
28
+ })();
29
+ node.ready.catch((e) => node.error('CS43L22 init failed: ' + e.message));
30
+ } catch (e) {
31
+ node.error('CS43L22 init failed: ' + e.message);
32
+ }
33
+
34
+ node.on('input', async function(msg, send, done) {
35
+ if (!node.driver) { done(); return; }
36
+ try {
37
+ await node.ready;
38
+ const p = msg.payload;
39
+ const isObj = p && typeof p === 'object';
40
+ let handled = false;
41
+ if (isObj && p.start === true) { await node.driver.start(); handled = true; }
42
+ if (isObj && p.stop === true) { await node.driver.stop(); handled = true; }
43
+ if (isObj && typeof p.volume === 'number') { await node.driver.setVolume(p.volume); handled = true; }
44
+ if (isObj && typeof p.mute === 'boolean') { await node.driver.mute(p.mute); handled = true; }
45
+ if (isObj && p.beep && typeof p.beep === 'object') {
46
+ const b = p.beep;
47
+ await node.driver.setBeep(b.frequency ?? 12, b.onTime ?? 2, b.offTime ?? 0, b.volume ?? -6,
48
+ b.mode ?? BEEP.SINGLE);
49
+ handled = true;
50
+ }
51
+ if (isObj && p.tone && typeof p.tone === 'object') {
52
+ await node.driver.setTone(p.tone.treble ?? 0, p.tone.bass ?? 0);
53
+ handled = true;
54
+ }
55
+ if (handled && !(isObj && typeof p.volume === 'number')) { done(); return; }
56
+ msg.payload = {
57
+ volume: await node.driver.getVolume(),
58
+ status: await node.driver.readStatus(),
59
+ };
60
+ send(msg);
61
+ done();
62
+ } catch (e) {
63
+ done(e);
64
+ }
65
+ });
66
+
67
+ node.on('close', async function(removed, done) {
68
+ try {
69
+ if (node.connection) await node.connection.close();
70
+ } catch (e) {
71
+ // bus may already be gone; nothing more to do on close
72
+ } finally {
73
+ done();
74
+ }
75
+ });
76
+ }
77
+ RED.nodes.registerType('periph-cs43l22', CS43L22Node);
78
+ };
@@ -0,0 +1,87 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-ds1881', {
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
+ option: { value: '1' },
10
+ zerocrossing: { value: true },
11
+ nonvolatile: { value: false }
12
+ },
13
+ inputs: 1,
14
+ outputs: 1,
15
+ icon: 'font-awesome/fa-volume-up',
16
+ label: function() { return this.name || 'DS1881'; },
17
+ paletteLabel: 'DS1881'
18
+ });
19
+ </script>
20
+
21
+ <script type="text/html" data-template-name="periph-ds1881">
22
+ <div class="form-row">
23
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
24
+ <input type="text" id="node-input-name" placeholder="DS1881">
25
+ </div>
26
+ <div class="form-row">
27
+ <label for="node-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
28
+ <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
29
+ </div>
30
+ <div class="form-row">
31
+ <label for="node-input-address"><i class="fa fa-at"></i> I²C address</label>
32
+ <select id="node-input-address" style="width:200px">
33
+ <option value="0x28">0x28 (A2 A1 A0 = 000)</option>
34
+ <option value="0x29">0x29 (A2 A1 A0 = 001)</option>
35
+ <option value="0x2A">0x2A (A2 A1 A0 = 010)</option>
36
+ <option value="0x2B">0x2B (A2 A1 A0 = 011)</option>
37
+ <option value="0x2C">0x2C (A2 A1 A0 = 100)</option>
38
+ <option value="0x2D">0x2D (A2 A1 A0 = 101)</option>
39
+ <option value="0x2E">0x2E (A2 A1 A0 = 110)</option>
40
+ <option value="0x2F">0x2F (A2 A1 A0 = 111)</option>
41
+ </select>
42
+ </div>
43
+ <div class="form-row">
44
+ <label for="node-input-option"><i class="fa fa-sliders"></i> Option</label>
45
+ <select id="node-input-option" style="width:260px">
46
+ <option value="1">1 — 1 dB steps, 0 to −62 dB + mute</option>
47
+ <option value="2">2 — 1/2/3 dB steps (DS1808 compatible)</option>
48
+ </select>
49
+ </div>
50
+ <div class="form-row">
51
+ <label for="node-input-zerocrossing"><i class="fa fa-bolt"></i> Zero-crossing</label>
52
+ <input type="checkbox" id="node-input-zerocrossing" style="width:auto"> &nbsp;clickless wiper changes (up to 50 ms)
53
+ </div>
54
+ <div class="form-row">
55
+ <label for="node-input-nonvolatile"><i class="fa fa-database"></i> Nonvolatile</label>
56
+ <input type="checkbox" id="node-input-nonvolatile" style="width:auto"> &nbsp;store every wiper write in EEPROM (50 000 writes)
57
+ </div>
58
+ </script>
59
+
60
+ <script type="text/html" data-help-name="periph-ds1881">
61
+ <p>Controls the two audio log-taper channels of a DS1881 dual digital potentiometer. Attenuation is in dB (0 dB = wiper at H); position 63 (Option 1) or 33 (Option 2) is mute. By default the wipers are volatile and power up at mute.</p>
62
+
63
+ <h3>Inputs</h3>
64
+ <dl class="message-properties">
65
+ <dt>payload.channel, payload.db <span class="property-type">number</span></dt>
66
+ <dd>Set one channel (0 or 1) to the nearest attenuation step.</dd>
67
+ <dt>payload.db <span class="property-type">number</span></dt>
68
+ <dd>Without <code>channel</code>: set both channels (stereo volume) in one transaction.</dd>
69
+ <dt>payload.mute <span class="property-type">boolean</span></dt>
70
+ <dd><code>true</code> mutes, <code>false</code> restores the last volume set through this node. Optional <code>payload.channel</code> limits it to one channel. Produces no output message.</dd>
71
+ <dt>payload.store <span class="property-type">boolean</span></dt>
72
+ <dd><code>true</code> persists the current positions to EEPROM (rated for only 50 000 writes). Produces no output message.</dd>
73
+ <dt>payload <span class="property-type">any</span></dt>
74
+ <dd>Any other message reads back both channels.</dd>
75
+ </dl>
76
+
77
+ <h3>Outputs</h3>
78
+ <dl class="message-properties">
79
+ <dt>payload.channel, payload.position, payload.db <span class="property-type">number</span></dt>
80
+ <dd>After a single-channel set: the position read back and its attenuation in dB.</dd>
81
+ <dt>payload.position0, position1, db0, db1 <span class="property-type">number</span></dt>
82
+ <dd>After a stereo set or any other message: both positions and their attenuation in dB (80 = mute). Read-back messages also carry <code>payload.config</code> (<code>option</code>, <code>zeroCrossing</code>, <code>nonvolatile</code>, <code>raw</code>).</dd>
83
+ </dl>
84
+
85
+ <h3>Configuration</h3>
86
+ <p>Set the Linux I²C bus number and the address (0x28–0x2F from A2, A1, A0). The CE pin must be low for the chip to listen on the bus. Option 1 has 63 one-dB steps plus mute; Option 2 has 1, 2 and 3 dB steps. Zero-crossing detection makes changes clickless; nonvolatile storage writes every change to EEPROM and should stay off for continuous volume control.</p>
87
+ </script>
@@ -0,0 +1,93 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { DS1881Full } = require('periph/src/chips/adc_dac/ds1881');
6
+
7
+ function DS1881Node(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ node.lastDb = [0, 0];
11
+ node.ready = null;
12
+
13
+ try {
14
+ const address = parseInt(config.address, 16) || DS1881Full.I2C_ADDRESS;
15
+ const connection = new I2CConnection(parseInt(config.bus, 10), address);
16
+ node.driver = new DS1881Full(connection);
17
+ node.connection = connection;
18
+ node.ready = (async () => {
19
+ await node.driver.init();
20
+ await node.driver.configure(
21
+ config.option === '2' ? 2 : 1,
22
+ config.zerocrossing !== false && config.zerocrossing !== 'false',
23
+ config.nonvolatile === true || config.nonvolatile === 'true'
24
+ );
25
+ })();
26
+ node.ready.catch((e) => node.error('DS1881 init failed: ' + e.message));
27
+ } catch (e) {
28
+ node.error('DS1881 init failed: ' + e.message);
29
+ }
30
+
31
+ async function readback() {
32
+ const [position0, position1, raw] = await node.driver.readAll();
33
+ const cfg = await node.driver.getConfig();
34
+ return {
35
+ position0, position1,
36
+ db0: node.driver.positionToDb(position0),
37
+ db1: node.driver.positionToDb(position1),
38
+ config: { option: cfg.option, zeroCrossing: cfg.zeroCrossing, nonvolatile: cfg.nonvolatile, raw },
39
+ };
40
+ }
41
+
42
+ node.on('input', async function(msg, send, done) {
43
+ if (!node.driver) { done(); return; }
44
+ try {
45
+ await node.ready;
46
+ const p = msg.payload;
47
+ const isObj = p && typeof p === 'object';
48
+ if (isObj && p.store === true) {
49
+ await node.driver.store();
50
+ done();
51
+ return;
52
+ }
53
+ if (isObj && typeof p.mute === 'boolean') {
54
+ const channels = typeof p.channel === 'number' ? [p.channel] : [0, 1];
55
+ for (const ch of channels) {
56
+ if (p.mute) await node.driver.mute(ch);
57
+ else await node.driver.setAttenuation(ch, node.lastDb[ch]);
58
+ }
59
+ done();
60
+ return;
61
+ }
62
+ if (isObj && typeof p.db === 'number' && typeof p.channel === 'number') {
63
+ await node.driver.setAttenuation(p.channel, p.db);
64
+ node.lastDb[p.channel] = p.db;
65
+ const position = await node.driver.getPosition(p.channel);
66
+ msg.payload = { channel: p.channel, position, db: node.driver.positionToDb(position) };
67
+ } else if (isObj && typeof p.db === 'number') {
68
+ await node.driver.setAttenuationBoth(p.db);
69
+ node.lastDb = [p.db, p.db];
70
+ const r = await readback();
71
+ msg.payload = { position0: r.position0, position1: r.position1, db0: r.db0, db1: r.db1 };
72
+ } else {
73
+ msg.payload = await readback();
74
+ }
75
+ send(msg);
76
+ done();
77
+ } catch (e) {
78
+ done(e);
79
+ }
80
+ });
81
+
82
+ node.on('close', async function(removed, done) {
83
+ try {
84
+ if (node.connection) await node.connection.close();
85
+ } catch (e) {
86
+ // bus may already be gone; nothing more to do on close
87
+ } finally {
88
+ done();
89
+ }
90
+ });
91
+ }
92
+ RED.nodes.registerType('periph-ds1881', DS1881Node);
93
+ };
@@ -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,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
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-adc-dac",
3
- "version": "1.4.0",
3
+ "version": "1.5.1",
4
4
  "description": "Node-RED nodes for ADC and DAC chips",
5
5
  "keywords": [
6
6
  "node-red",
@@ -14,6 +14,8 @@
14
14
  "nodes": {
15
15
  "ad5243": "nodes/ad5243/ad5243.js",
16
16
  "ad5248": "nodes/ad5248/ad5248.js",
17
+ "ad5251": "nodes/ad5251/ad5251.js",
18
+ "ad5252": "nodes/ad5252/ad5252.js",
17
19
  "ad7705": "nodes/ad7705/ad7705.js",
18
20
  "ad7706": "nodes/ad7706/ad7706.js",
19
21
  "ads1013": "nodes/ads1013/ads1013.js",
@@ -21,11 +23,17 @@
21
23
  "ads1113": "nodes/ads1113/ads1113.js",
22
24
  "ads1114": "nodes/ads1114/ads1114.js",
23
25
  "ads1115": "nodes/ads1115/ads1115.js",
26
+ "ads7828": "nodes/ads7828/ads7828.js",
27
+ "cat5171": "nodes/cat5171/cat5171.js",
28
+ "cs43l22": "nodes/cs43l22/cs43l22.js",
29
+ "ds1881": "nodes/ds1881/ds1881.js",
30
+ "ds3502": "nodes/ds3502/ds3502.js",
24
31
  "es8311": "nodes/es8311/es8311.js",
25
32
  "hx710a": "nodes/hx710a/hx710a.js",
26
33
  "hx710b": "nodes/hx710b/hx710b.js",
27
34
  "hx711": "nodes/hx711/hx711.js",
28
35
  "hy3131": "nodes/hy3131/hy3131.js",
36
+ "mcp3422": "nodes/mcp3422/mcp3422.js",
29
37
  "mcp4725": "nodes/mcp4725/mcp4725.js",
30
38
  "mcp4728": "nodes/mcp4728/mcp4728.js",
31
39
  "mcp4xxx": "nodes/mcp4xxx/mcp4xxx.js",