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,82 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-ads7828', {
3
+ category: 'Peripheral',
4
+ color: '#C084FC',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '0x48' },
9
+ reference: { value: 'internal' },
10
+ vref: { value: '5.0' }
11
+ },
12
+ inputs: 1,
13
+ outputs: 1,
14
+ icon: 'font-awesome/fa-microchip',
15
+ label: function() { return this.name || 'ADS7828'; },
16
+ paletteLabel: 'ADS7828',
17
+ oneditprepare: function() {
18
+ $('#node-input-reference').on('change', function() {
19
+ $('#row-vref').toggle($(this).val() === 'external');
20
+ }).trigger('change');
21
+ }
22
+ });
23
+ </script>
24
+
25
+ <script type="text/html" data-template-name="periph-ads7828">
26
+ <div class="form-row">
27
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
28
+ <input type="text" id="node-input-name" placeholder="ADS7828">
29
+ </div>
30
+ <div class="form-row">
31
+ <label for="node-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
32
+ <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
33
+ </div>
34
+ <div class="form-row">
35
+ <label for="node-input-address"><i class="fa fa-at"></i> I²C address</label>
36
+ <select id="node-input-address" style="width:160px">
37
+ <option value="0x48">0x48 (A1 = A0 = GND)</option>
38
+ <option value="0x49">0x49 (A1 = GND, A0 = VDD)</option>
39
+ <option value="0x4A">0x4A (A1 = VDD, A0 = GND)</option>
40
+ <option value="0x4B">0x4B (A1 = A0 = VDD)</option>
41
+ </select>
42
+ </div>
43
+ <div class="form-row">
44
+ <label for="node-input-reference"><i class="fa fa-bolt"></i> Reference</label>
45
+ <select id="node-input-reference" style="width:200px">
46
+ <option value="internal">Internal 2.5 V</option>
47
+ <option value="external">External</option>
48
+ </select>
49
+ </div>
50
+ <div class="form-row" id="row-vref">
51
+ <label for="node-input-vref"><i class="fa fa-bolt"></i> External VREF</label>
52
+ <input type="number" id="node-input-vref" min="0.05" step="0.01" style="width:80px"> &nbsp;V (0.05 V … VDD)
53
+ </div>
54
+ </script>
55
+
56
+ <script type="text/html" data-help-name="periph-ads7828">
57
+ <p>Reads the 12-bit, 8-channel ADS7828 ADC over I²C as eight single-ended inputs or four differential pairs. Default address is <code>0x48</code>; <code>0x48</code>–<code>0x4B</code> are valid depending on the A1/A0 straps. With the internal 2.5 V reference the REFIN/REFOUT pin must be decoupled with 0.1 µF and nothing else may drive it.</p>
58
+
59
+ <h3>Inputs</h3>
60
+ <dl class="message-properties">
61
+ <dt class="optional">channel <span class="property-type">number</span></dt>
62
+ <dd>0–7: read only this single-ended channel (CHn against COM).</dd>
63
+ <dt class="optional">differential <span class="property-type">string</span></dt>
64
+ <dd><code>"0-1"</code>, <code>"1-0"</code>, <code>"2-3"</code>, <code>"3-2"</code>, <code>"4-5"</code>, <code>"5-4"</code>, <code>"6-7"</code> or <code>"7-6"</code>: read that pair; the first number is the positive input.</dd>
65
+ </dl>
66
+ <p>Any other message scans all eight single-ended channels.</p>
67
+
68
+ <h3>Outputs</h3>
69
+ <dl class="message-properties">
70
+ <dt>payload.channels <span class="property-type">number[]</span></dt>
71
+ <dd>Scan only: voltages of CH0…CH7 in volts.</dd>
72
+ <dt>payload.raw <span class="property-type">number | number[]</span></dt>
73
+ <dd>12-bit code(s), 0–4095.</dd>
74
+ <dt>payload.channel / payload.voltage <span class="property-type">number</span></dt>
75
+ <dd>Single-channel and differential reads: the channel and the voltage in volts.</dd>
76
+ <dt>payload.pair / payload.inverted</dt>
77
+ <dd>Differential reads: pair index 0–3 and whether the odd input was positive.</dd>
78
+ </dl>
79
+
80
+ <h3>Configuration</h3>
81
+ <p>I²C bus number, address (<code>0x48</code>–<code>0x4B</code>) and reference: internal 2.5 V, or an external reference voltage (0.05 V … VDD).</p>
82
+ </script>
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { ADS7828Full } = require('periph/src/chips/adc_dac/ads7828');
6
+
7
+ function ADS7828Node(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+
11
+ try {
12
+ const address = parseInt(config.address, 16) || ADS7828Full.I2C_ADDRESS;
13
+ const connection = new I2CConnection(parseInt(config.bus, 10), address);
14
+ const vref = config.reference === 'external' ? parseFloat(config.vref) : null;
15
+ node.driver = new ADS7828Full(connection, vref);
16
+ node.connection = connection;
17
+ node.ready = node.driver.init();
18
+ } catch (e) {
19
+ node.error('ADS7828 init failed: ' + e.message);
20
+ }
21
+
22
+ node.on('input', async function(msg, send, done) {
23
+ if (!node.driver) { done(); return; }
24
+ try {
25
+ await node.ready;
26
+ const d = node.driver;
27
+ const diff = typeof msg.differential === 'string' ? /^([0-7])-([0-7])$/.exec(msg.differential) : null;
28
+ if (diff) {
29
+ const a = parseInt(diff[1], 10);
30
+ const b = parseInt(diff[2], 10);
31
+ if ((a >> 1) !== (b >> 1) || a === b) throw new Error('differential must pair 0-1, 2-3, 4-5 or 6-7');
32
+ const pair = a >> 1;
33
+ const inverted = (a & 1) === 1;
34
+ const raw = await d.readDifferentialRaw(pair, inverted);
35
+ msg.payload = { pair, inverted, raw, voltage: raw / 4096 * d.getReference() };
36
+ } else if (typeof msg.channel === 'number') {
37
+ const raw = await d.readRaw(msg.channel);
38
+ msg.payload = { channel: msg.channel, raw, voltage: raw / 4096 * d.getReference() };
39
+ } else {
40
+ const raw = await d.readScan([0, 1, 2, 3, 4, 5, 6, 7]);
41
+ msg.payload = { channels: raw.map((r) => r / 4096 * d.getReference()), raw };
42
+ }
43
+ send(msg);
44
+ done();
45
+ } catch (e) {
46
+ done(e);
47
+ }
48
+ });
49
+
50
+ node.on('close', async function(removed, done) {
51
+ try {
52
+ if (node.driver) await node.driver.powerDown();
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-ads7828', ADS7828Node);
62
+ };
@@ -0,0 +1,74 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-cat5171', {
3
+ category: 'Peripheral',
4
+ color: '#C084FC',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '0x2C' },
9
+ rab: { value: '100000', required: true },
10
+ rwiper: { value: '50', 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 || 'CAT5171'; },
16
+ paletteLabel: 'CAT5171'
17
+ });
18
+ </script>
19
+
20
+ <script type="text/html" data-template-name="periph-cat5171">
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="CAT5171">
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:160px">
32
+ <option value="0x2C">0x2C (AD0 = GND)</option>
33
+ <option value="0x2D">0x2D (AD0 = VDD)</option>
34
+ </select>
35
+ </div>
36
+ <div class="form-row">
37
+ <label for="node-input-rab"><i class="fa fa-sliders"></i> RAB</label>
38
+ <select id="node-input-rab" style="width:120px">
39
+ <option value="50000">50 kΩ</option>
40
+ <option value="100000">100 kΩ</option>
41
+ </select>
42
+ </div>
43
+ <div class="form-row">
44
+ <label for="node-input-rwiper"><i class="fa fa-sliders"></i> Wiper RW</label>
45
+ <input type="number" id="node-input-rwiper" min="0" style="width:80px"> &nbsp;Ω (typ. 50 Ω at 5 V, 100 Ω at 3 V)
46
+ </div>
47
+ </script>
48
+
49
+ <script type="text/html" data-help-name="periph-cat5171">
50
+ <p>Sets and reads the 256-position wiper of a CAT5171 I²C digital potentiometer. The wiper is volatile: it powers up at midscale (128).</p>
51
+
52
+ <h3>Inputs</h3>
53
+ <dl class="message-properties">
54
+ <dt>payload.code <span class="property-type">number</span></dt>
55
+ <dd>Wiper position 0–255 (clamped).</dd>
56
+ <dt>payload.ohms <span class="property-type">number</span></dt>
57
+ <dd>Without <code>code</code>: target W–B resistance in ohms, set to the nearest code.</dd>
58
+ <dt>payload.shutdown <span class="property-type">boolean</span></dt>
59
+ <dd><code>true</code> opens terminal A and shorts the wiper to B (the wiper position is kept); <code>false</code> restores it. Produces no output message.</dd>
60
+ <dt>payload <span class="property-type">any</span></dt>
61
+ <dd>Any other message reads back the wiper.</dd>
62
+ </dl>
63
+
64
+ <h3>Outputs</h3>
65
+ <dl class="message-properties">
66
+ <dt>payload.code, payload.resistance <span class="property-type">number</span></dt>
67
+ <dd>After a set: the wiper code read back and the resulting W–B resistance in ohms.</dd>
68
+ <dt>payload.wiper, payload.resistance <span class="property-type">number</span></dt>
69
+ <dd>After any other message: the wiper code and the W–B resistance in ohms.</dd>
70
+ </dl>
71
+
72
+ <h3>Configuration</h3>
73
+ <p>Set the Linux I²C bus number, the address (0x2C with AD0 at GND, 0x2D with AD0 at VDD), the part's end-to-end resistance RAB (50 or 100 kΩ, as ordered) and the wiper resistance RW. Absolute resistance is only accurate to the part's tolerance (±20 %); ratios are exact.</p>
74
+ </script>
@@ -0,0 +1,61 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { CAT5171Full } = require('periph/src/chips/adc_dac/cat5171');
6
+
7
+ function CAT5171Node(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+
11
+ try {
12
+ const rAb = parseFloat(config.rab) || 100000.0;
13
+ const rWiper = config.rwiper === '' || config.rwiper === undefined ? 50.0 : parseFloat(config.rwiper);
14
+ const address = config.address === '0x2D' ? CAT5171Full.I2C_ADDRESS_AD0_HIGH : CAT5171Full.I2C_ADDRESS;
15
+ const connection = new I2CConnection(parseInt(config.bus, 10), address);
16
+ node.driver = new CAT5171Full(connection, rAb, rWiper);
17
+ node.connection = connection;
18
+ } catch (e) {
19
+ node.error('CAT5171 init failed: ' + e.message);
20
+ }
21
+
22
+ node.on('input', async function(msg, send, done) {
23
+ if (!node.driver) { done(); return; }
24
+ try {
25
+ const p = msg.payload;
26
+ if (p && typeof p === 'object' && typeof p.shutdown === 'boolean') {
27
+ if (p.shutdown) await node.driver.shutdown();
28
+ else await node.driver.resume();
29
+ done();
30
+ return;
31
+ }
32
+ if (p && typeof p === 'object' && (typeof p.code === 'number' || typeof p.ohms === 'number')) {
33
+ if (typeof p.code === 'number') await node.driver.setWiper(p.code);
34
+ else await node.driver.setResistance(p.ohms);
35
+ const code = await node.driver.getWiper();
36
+ const resistance = await node.driver.getResistance();
37
+ msg.payload = { code, resistance };
38
+ } else {
39
+ const wiper = await node.driver.getWiper();
40
+ const resistance = await node.driver.getResistance();
41
+ msg.payload = { wiper, resistance };
42
+ }
43
+ send(msg);
44
+ done();
45
+ } catch (e) {
46
+ done(e);
47
+ }
48
+ });
49
+
50
+ node.on('close', async function(removed, done) {
51
+ try {
52
+ if (node.connection) await node.connection.close();
53
+ } catch (e) {
54
+ // bus may already be gone; nothing more to do on close
55
+ } finally {
56
+ done();
57
+ }
58
+ });
59
+ }
60
+ RED.nodes.registerType('periph-cat5171', CAT5171Node);
61
+ };
@@ -0,0 +1,84 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-cs43l22', {
3
+ category: 'Peripheral',
4
+ color: '#C084FC',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '0x4A' },
9
+ gpioChip: { value: '0' },
10
+ resGpio: { value: '' },
11
+ format: { value: 'i2s' }
12
+ },
13
+ inputs: 1,
14
+ outputs: 1,
15
+ icon: 'font-awesome/fa-volume-up',
16
+ label: function() { return this.name || 'CS43L22'; },
17
+ paletteLabel: 'CS43L22'
18
+ });
19
+ </script>
20
+
21
+ <script type="text/html" data-template-name="periph-cs43l22">
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="CS43L22">
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="0x4A">0x4A (AD0 = DGND)</option>
34
+ <option value="0x4B">0x4B (AD0 = VL)</option>
35
+ </select>
36
+ </div>
37
+ <div class="form-row">
38
+ <label for="node-input-gpioChip"><i class="fa fa-plug"></i> GPIO chip</label>
39
+ <input type="number" id="node-input-gpioChip" min="0" style="width:80px">
40
+ </div>
41
+ <div class="form-row">
42
+ <label for="node-input-resGpio"><i class="fa fa-power-off"></i> RESET GPIO</label>
43
+ <input type="number" id="node-input-resGpio" min="0" style="width:80px" placeholder="optional"> &nbsp;line of the active-low RESET pin
44
+ </div>
45
+ <div class="form-row">
46
+ <label for="node-input-format"><i class="fa fa-sliders"></i> Audio format</label>
47
+ <select id="node-input-format" style="width:200px">
48
+ <option value="i2s">I²S</option>
49
+ <option value="lj">Left-justified</option>
50
+ <option value="rj">Right-justified</option>
51
+ </select>
52
+ </div>
53
+ </script>
54
+
55
+ <script type="text/html" data-help-name="periph-cs43l22">
56
+ <p>Controls a CS43L22 stereo DAC with headphone and speaker amplifiers over its I²C control port. The audio stream (I²S: MCLK, SCLK, LRCK, SDIN) comes from the board and is not handled by this node. Send <code>{ start: true }</code> only after MCLK is running.</p>
57
+
58
+ <h3>Inputs</h3>
59
+ <dl class="message-properties">
60
+ <dt>payload.start, payload.stop <span class="property-type">boolean</span></dt>
61
+ <dd><code>start: true</code> powers the chip up (MCLK must be running); <code>stop: true</code> runs the power-down sequence (remove MCLK afterwards). No output message.</dd>
62
+ <dt>payload.volume <span class="property-type">number</span></dt>
63
+ <dd>Master volume in dB (−102 … +12). Outputs the read-back.</dd>
64
+ <dt>payload.mute <span class="property-type">boolean</span></dt>
65
+ <dd>Mute or unmute the headphone outputs. No output message.</dd>
66
+ <dt>payload.beep <span class="property-type">object</span></dt>
67
+ <dd><code>{ frequency 0–15, onTime 0–15, offTime 0–7, volume dB, mode 1–3 }</code> triggers the beep generator (mode 0 stops it). No output message.</dd>
68
+ <dt>payload.tone <span class="property-type">object</span></dt>
69
+ <dd><code>{ treble, bass }</code> shelving gains in dB (−10.5 … +12). No output message.</dd>
70
+ <dt>payload <span class="property-type">any</span></dt>
71
+ <dd>Any other message reads back the volume and the status register.</dd>
72
+ </dl>
73
+
74
+ <h3>Outputs</h3>
75
+ <dl class="message-properties">
76
+ <dt>payload.volume <span class="property-type">number</span></dt>
77
+ <dd>Master volume of channel A in dB.</dd>
78
+ <dt>payload.status <span class="property-type">object</span></dt>
79
+ <dd><code>clockError</code>, <code>dspAOverflow</code>, <code>dspBOverflow</code>, <code>pcmAOverflow</code>, <code>pcmBOverflow</code>. Reading clears the flags.</dd>
80
+ </dl>
81
+
82
+ <h3>Configuration</h3>
83
+ <p>Set the Linux I²C bus number and the address (0x4A with AD0 low, 0x4B with AD0 high). The optional RESET GPIO (needs the <code>opengpio</code> package) is pulsed low during initialization. The audio format selects I²S, left-justified or right-justified input.</p>
84
+ </script>
@@ -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
+ };