node-red-contrib-periph-adc-dac 1.0.1 → 1.2.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,188 @@
1
+ [
2
+ {
3
+ "id": "tab-mcp4728-demo",
4
+ "type": "tab",
5
+ "label": "MCP4728 Quad DAC Demo",
6
+ "disabled": false,
7
+ "info": "Drives the four channels of an MCP4728 quad 12-bit DAC. Four inject nodes target channels A–D individually with fractions 0.0, 0.25, 0.5, 0.75. A fifth inject node sends the array [0.1, 0.4, 0.7, 1.0] to update all four channels simultaneously via Fast Write. Each applied value is printed to the debug panel showing the channel, the fraction, and the approximate voltage assuming a 3.3 V supply."
8
+ },
9
+ {
10
+ "id": "cfg-mcp4728",
11
+ "type": "mcp4728-device",
12
+ "name": "MCP4728",
13
+ "bus": "1",
14
+ "address": "60"
15
+ },
16
+ {
17
+ "id": "inj-A",
18
+ "type": "inject",
19
+ "name": "A = 0.0",
20
+ "topic": "",
21
+ "payload": "0.0",
22
+ "payloadType": "num",
23
+ "repeat": "",
24
+ "crontab": "",
25
+ "once": false,
26
+ "onceDelay": 0.1,
27
+ "z": "tab-mcp4728-demo",
28
+ "x": 120,
29
+ "y": 80,
30
+ "wires": [["mcp4728-A"]]
31
+ },
32
+ {
33
+ "id": "inj-B",
34
+ "type": "inject",
35
+ "name": "B = 0.25",
36
+ "topic": "",
37
+ "payload": "0.25",
38
+ "payloadType": "num",
39
+ "repeat": "",
40
+ "crontab": "",
41
+ "once": false,
42
+ "onceDelay": 0.1,
43
+ "z": "tab-mcp4728-demo",
44
+ "x": 120,
45
+ "y": 160,
46
+ "wires": [["mcp4728-B"]]
47
+ },
48
+ {
49
+ "id": "inj-C",
50
+ "type": "inject",
51
+ "name": "C = 0.5",
52
+ "topic": "",
53
+ "payload": "0.5",
54
+ "payloadType": "num",
55
+ "repeat": "",
56
+ "crontab": "",
57
+ "once": false,
58
+ "onceDelay": 0.1,
59
+ "z": "tab-mcp4728-demo",
60
+ "x": 120,
61
+ "y": 240,
62
+ "wires": [["mcp4728-C"]]
63
+ },
64
+ {
65
+ "id": "inj-D",
66
+ "type": "inject",
67
+ "name": "D = 0.75",
68
+ "topic": "",
69
+ "payload": "0.75",
70
+ "payloadType": "num",
71
+ "repeat": "",
72
+ "crontab": "",
73
+ "once": false,
74
+ "onceDelay": 0.1,
75
+ "z": "tab-mcp4728-demo",
76
+ "x": 120,
77
+ "y": 320,
78
+ "wires": [["mcp4728-D"]]
79
+ },
80
+ {
81
+ "id": "inj-all",
82
+ "type": "inject",
83
+ "name": "All = [0.1, 0.4, 0.7, 1.0]",
84
+ "topic": "",
85
+ "payload": "[0.1, 0.4, 0.7, 1.0]",
86
+ "payloadType": "json",
87
+ "repeat": "",
88
+ "crontab": "",
89
+ "once": false,
90
+ "onceDelay": 0.1,
91
+ "z": "tab-mcp4728-demo",
92
+ "x": 180,
93
+ "y": 420,
94
+ "wires": [["mcp4728-all"]]
95
+ },
96
+ {
97
+ "id": "mcp4728-A",
98
+ "type": "periph-mcp4728",
99
+ "name": "MCP4728 A",
100
+ "device": "cfg-mcp4728",
101
+ "channel": "A",
102
+ "inputMode": "fraction",
103
+ "vref": "external",
104
+ "gain": "x1",
105
+ "persist": false,
106
+ "z": "tab-mcp4728-demo",
107
+ "x": 360,
108
+ "y": 80,
109
+ "wires": [["dbg"]]
110
+ },
111
+ {
112
+ "id": "mcp4728-B",
113
+ "type": "periph-mcp4728",
114
+ "name": "MCP4728 B",
115
+ "device": "cfg-mcp4728",
116
+ "channel": "B",
117
+ "inputMode": "fraction",
118
+ "vref": "external",
119
+ "gain": "x1",
120
+ "persist": false,
121
+ "z": "tab-mcp4728-demo",
122
+ "x": 360,
123
+ "y": 160,
124
+ "wires": [["dbg"]]
125
+ },
126
+ {
127
+ "id": "mcp4728-C",
128
+ "type": "periph-mcp4728",
129
+ "name": "MCP4728 C",
130
+ "device": "cfg-mcp4728",
131
+ "channel": "C",
132
+ "inputMode": "fraction",
133
+ "vref": "external",
134
+ "gain": "x1",
135
+ "persist": false,
136
+ "z": "tab-mcp4728-demo",
137
+ "x": 360,
138
+ "y": 240,
139
+ "wires": [["dbg"]]
140
+ },
141
+ {
142
+ "id": "mcp4728-D",
143
+ "type": "periph-mcp4728",
144
+ "name": "MCP4728 D",
145
+ "device": "cfg-mcp4728",
146
+ "channel": "D",
147
+ "inputMode": "fraction",
148
+ "vref": "external",
149
+ "gain": "x1",
150
+ "persist": false,
151
+ "z": "tab-mcp4728-demo",
152
+ "x": 360,
153
+ "y": 320,
154
+ "wires": [["dbg"]]
155
+ },
156
+ {
157
+ "id": "mcp4728-all",
158
+ "type": "periph-mcp4728",
159
+ "name": "MCP4728 All",
160
+ "device": "cfg-mcp4728",
161
+ "channel": "A",
162
+ "inputMode": "fraction",
163
+ "vref": "external",
164
+ "gain": "x1",
165
+ "persist": false,
166
+ "z": "tab-mcp4728-demo",
167
+ "x": 420,
168
+ "y": 420,
169
+ "wires": [["dbg"]]
170
+ },
171
+ {
172
+ "id": "dbg",
173
+ "type": "debug",
174
+ "name": "",
175
+ "active": true,
176
+ "tosidebar": true,
177
+ "console": false,
178
+ "tostatus": false,
179
+ "complete": "false",
180
+ "targetType": "msg",
181
+ "statusVal": "",
182
+ "statusType": "auto",
183
+ "z": "tab-mcp4728-demo",
184
+ "x": 660,
185
+ "y": 240,
186
+ "wires": []
187
+ }
188
+ ]
@@ -0,0 +1,82 @@
1
+ [
2
+ {
3
+ "id": "tab-pcf8591-demo",
4
+ "type": "tab",
5
+ "label": "PCF8591 ADC + DAC Demo",
6
+ "disabled": false,
7
+ "info": "An Inject node fires every second, triggering the periph-pcf8591 node to read all four analog channels. A Function node maps the four raw 0–255 values to approximate voltages (assuming 3.3 V reference). A Debug node prints the four channel voltages. A second Inject node sends a numeric payload to write a value to the on-chip DAC. The flow shows simultaneous ADC reading and DAC output from a single PCF8591 device."
8
+ },
9
+ {
10
+ "id": "inj-read",
11
+ "type": "inject",
12
+ "name": "Read ADC (1s)",
13
+ "topic": "",
14
+ "payload": "",
15
+ "payloadType": "date",
16
+ "repeat": "1",
17
+ "crontab": "",
18
+ "once": false,
19
+ "onceDelay": 0.1,
20
+ "z": "tab-pcf8591-demo",
21
+ "x": 140,
22
+ "y": 100,
23
+ "wires": [["pcf8591-node"]]
24
+ },
25
+ {
26
+ "id": "pcf8591-node",
27
+ "type": "periph-pcf8591",
28
+ "name": "PCF8591",
29
+ "bus": "1",
30
+ "address": "48",
31
+ "inputMode": "4 single-ended",
32
+ "z": "tab-pcf8591-demo",
33
+ "x": 360,
34
+ "y": 100,
35
+ "wires": [["fn-volts"]]
36
+ },
37
+ {
38
+ "id": "fn-volts",
39
+ "type": "function",
40
+ "name": "raw → voltage",
41
+ "func": "const vref = 3.3;\nmsg.payload = {\n ch0_v: msg.payload.ch0 * vref / 256.0,\n ch1_v: msg.payload.ch1 * vref / 256.0,\n ch2_v: msg.payload.ch2 * vref / 256.0,\n ch3_v: msg.payload.ch3 * vref / 256.0,\n};\nreturn msg;",
42
+ "outputs": 1,
43
+ "noerr": 0,
44
+ "z": "tab-pcf8591-demo",
45
+ "x": 560,
46
+ "y": 100,
47
+ "wires": [["dbg-voltages"]]
48
+ },
49
+ {
50
+ "id": "dbg-voltages",
51
+ "type": "debug",
52
+ "name": "channel voltages",
53
+ "active": true,
54
+ "tosidebar": true,
55
+ "console": false,
56
+ "tostatus": false,
57
+ "complete": "false",
58
+ "targetType": "msg",
59
+ "statusVal": "",
60
+ "statusType": "auto",
61
+ "z": "tab-pcf8591-demo",
62
+ "x": 760,
63
+ "y": 100,
64
+ "wires": []
65
+ },
66
+ {
67
+ "id": "inj-dac",
68
+ "type": "inject",
69
+ "name": "DAC = 128",
70
+ "topic": "",
71
+ "payload": "{\"dac\":128}",
72
+ "payloadType": "json",
73
+ "repeat": "",
74
+ "crontab": "",
75
+ "once": false,
76
+ "onceDelay": 0.1,
77
+ "z": "tab-pcf8591-demo",
78
+ "x": 140,
79
+ "y": 200,
80
+ "wires": [["pcf8591-node"]]
81
+ }
82
+ ]
@@ -0,0 +1,93 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('ad7705-device', {
3
+ category: 'config',
4
+ defaults: {
5
+ name: { value: '' },
6
+ bus: { value: '0', required: true, validate: RED.validators.number() },
7
+ device: { value: '0', required: true, validate: RED.validators.number() },
8
+ vref: { value: '2.5', required: true },
9
+ mclkHz: { value: '2457600', required: true }
10
+ },
11
+ label: function() {
12
+ return this.name || ('AD7705 @ SPI ' + this.bus + '.' + this.device + ' (Vref=' + this.vref + 'V)');
13
+ }
14
+ });
15
+ </script>
16
+
17
+ <script type="text/html" data-template-name="ad7705-device">
18
+ <div class="form-row">
19
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
20
+ <input type="text" id="node-config-input-name" placeholder="AD7705">
21
+ </div>
22
+ <div class="form-row">
23
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> SPI bus</label>
24
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px">
25
+ </div>
26
+ <div class="form-row">
27
+ <label for="node-config-input-device"><i class="fa fa-microchip"></i> SPI device (CS)</label>
28
+ <input type="number" id="node-config-input-device" min="0" style="width:80px">
29
+ </div>
30
+ <div class="form-row">
31
+ <label for="node-config-input-vref">V_REF (V)</label>
32
+ <input type="number" id="node-config-input-vref" step="0.01" value="2.5" style="width:80px">
33
+ </div>
34
+ <div class="form-row">
35
+ <label for="node-config-input-mclkHz">Master clock (Hz)</label>
36
+ <select id="node-config-input-mclkHz" style="width:160px">
37
+ <option value="1000000">1 MHz</option>
38
+ <option value="2000000">2 MHz</option>
39
+ <option value="2457600">2.4576 MHz</option>
40
+ <option value="4915200">4.9152 MHz</option>
41
+ </select>
42
+ </div>
43
+ </script>
44
+
45
+ <script type="text/javascript">
46
+ RED.nodes.registerType('periph-ad7705', {
47
+ category: 'Peripheral',
48
+ color: '#C084FC',
49
+ defaults: {
50
+ name: { value: '' },
51
+ device: { value: '', type: 'ad7705-device', required: true }
52
+ },
53
+ inputs: 1,
54
+ outputs: 1,
55
+ icon: 'font-awesome/fa-wave-square',
56
+ label: function() { return this.name || 'AD7705'; },
57
+ paletteLabel: 'AD7705'
58
+ });
59
+ </script>
60
+
61
+ <script type="text/html" data-template-name="periph-ad7705">
62
+ <div class="form-row">
63
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
64
+ <input type="text" id="node-input-device">
65
+ </div>
66
+ <div class="form-row">
67
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
68
+ <input type="text" id="node-input-name" placeholder="AD7705">
69
+ </div>
70
+ </script>
71
+
72
+ <script type="text/html" data-help-name="periph-ad7705">
73
+ <p>Reads voltage from a 2-channel, 16-bit sigma-delta AD7705 ADC over SPI. The driver uses the chip's two-phase register-access protocol and polls DRDY over SPI, so only SCLK/DIN/DOUT/CS are needed (no dedicated DRDY GPIO).</p>
74
+
75
+ <h3>Inputs</h3>
76
+ <dl class="message-properties">
77
+ <dt>payload <span class="property-type">any</span></dt>
78
+ <dd>Any incoming message triggers a dual-channel read. If the device config has Channel 2 disabled, the output omits <code>channel2</code>. The literal string <code>"calibrate"</code> triggers a self-calibration of both configured channels.</dd>
79
+ </dl>
80
+
81
+ <h3>Outputs</h3>
82
+ <dl class="message-properties">
83
+ <dt>payload.channel1 <span class="property-type">number</span></dt>
84
+ <dd>Channel 1 input voltage in V.</dd>
85
+ <dt>payload.channel2 <span class="property-type">number</span></dt>
86
+ <dd>Channel 2 input voltage in V (omitted if Channel 2 is disabled).</dd>
87
+ <dt>payload.calibrated <span class="property-type">boolean</span></dt>
88
+ <dd>True after a <code>"calibrate"</code> command.</dd>
89
+ </dl>
90
+
91
+ <h3>Configuration</h3>
92
+ <p>Set the SPI bus and CS line (Linux <code>/dev/spidevBUS.DEVICE</code>), the reference voltage V_REF in V (default 2.5), and the master clock frequency wired to MCLK IN (1/2/2.4576/4.9152 MHz). SPI Mode 3 (CPOL=1 CPHA=1) is used; the maximum clock is 5 MHz.</p>
93
+ </script>
@@ -0,0 +1,78 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const SPIConnection = require('periph/src/connection/spi').SPIConnection;
5
+ const AD7705Full = require('periph/src/chips/adc_dac/ad7705').AD7705Full;
6
+
7
+ function AD7705DeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const connection = new SPIConnection(
12
+ parseInt(config.bus),
13
+ parseInt(config.device),
14
+ { mode: 3, maxSpeedHz: 5_000_000 }
15
+ );
16
+ const vref = parseFloat(config.vref) || 2.5;
17
+ const mclkHz = parseInt(config.mclkHz) || 2457600;
18
+ const chip = new AD7705Full(connection, vref, mclkHz);
19
+
20
+ const channel1 = config.channel1 || { enabled: true, gain: '1', bipolar: true, buffered: false, rate: '50' };
21
+ const channel2 = config.channel2 || { enabled: false, gain: '1', bipolar: true, buffered: false, rate: '50' };
22
+
23
+ if (channel2.enabled) {
24
+ chip.configure(2, parseInt(channel2.gain), channel2.bipolar, channel2.buffered, parseInt(channel2.rate))
25
+ .then(() => chip.selfCalibrate(2))
26
+ .catch((err) => node.error('AD7705 channel 2 init failed: ' + err.message));
27
+ }
28
+
29
+ chip.selfCalibrate(1).then(() => {
30
+ node.chip = chip;
31
+ node.connection = connection;
32
+ node.channel2Enabled = channel2.enabled;
33
+ }).catch((err) => {
34
+ node.error('AD7705 channel 1 init failed: ' + err.message);
35
+ });
36
+ } catch (e) {
37
+ node.error('AD7705 init failed: ' + e.message);
38
+ }
39
+ node.on('close', function() {
40
+ if (node.connection) node.connection.close();
41
+ });
42
+ }
43
+ RED.nodes.registerType('ad7705-device', AD7705DeviceNode);
44
+
45
+ function AD7705Node(config) {
46
+ RED.nodes.createNode(this, config);
47
+ const node = this;
48
+ node.device = RED.nodes.getNode(config.device);
49
+
50
+ node.on('input', async function(msg, send, done) {
51
+ if (!node.device || !node.device.chip) {
52
+ done(new Error('No AD7705 device configured'));
53
+ return;
54
+ }
55
+ try {
56
+ const chip = node.device.chip;
57
+ if (msg.payload === 'calibrate') {
58
+ await chip.selfCalibrate(1);
59
+ if (node.device.channel2Enabled) {
60
+ await chip.selfCalibrate(2);
61
+ }
62
+ msg.payload = { calibrated: true };
63
+ send(msg); done();
64
+ return;
65
+ }
66
+ const v1 = await chip.readVoltageChannel(1);
67
+ const result = { channel1: v1 };
68
+ if (node.device.channel2Enabled) {
69
+ const v2 = await chip.readVoltageChannel(2);
70
+ result.channel2 = v2;
71
+ }
72
+ msg.payload = result;
73
+ send(msg); done();
74
+ } catch (e) { done(e); }
75
+ });
76
+ }
77
+ RED.nodes.registerType('periph-ad7705', AD7705Node);
78
+ };
@@ -0,0 +1,95 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('ad7706-device', {
3
+ category: 'config',
4
+ defaults: {
5
+ name: { value: '' },
6
+ bus: { value: '0', required: true, validate: RED.validators.number() },
7
+ device: { value: '0', required: true, validate: RED.validators.number() },
8
+ vref: { value: '2.5', required: true },
9
+ mclkHz: { value: '2457600', required: true }
10
+ },
11
+ label: function() {
12
+ return this.name || ('AD7706 @ SPI ' + this.bus + '.' + this.device + ' (Vref=' + this.vref + 'V)');
13
+ }
14
+ });
15
+ </script>
16
+
17
+ <script type="text/html" data-template-name="ad7706-device">
18
+ <div class="form-row">
19
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
20
+ <input type="text" id="node-config-input-name" placeholder="AD7706">
21
+ </div>
22
+ <div class="form-row">
23
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> SPI bus</label>
24
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px">
25
+ </div>
26
+ <div class="form-row">
27
+ <label for="node-config-input-device"><i class="fa fa-microchip"></i> SPI device (CS)</label>
28
+ <input type="number" id="node-config-input-device" min="0" style="width:80px">
29
+ </div>
30
+ <div class="form-row">
31
+ <label for="node-config-input-vref">V_REF (V)</label>
32
+ <input type="number" id="node-config-input-vref" step="0.01" value="2.5" style="width:80px">
33
+ </div>
34
+ <div class="form-row">
35
+ <label for="node-config-input-mclkHz">Master clock (Hz)</label>
36
+ <select id="node-config-input-mclkHz" style="width:160px">
37
+ <option value="1000000">1 MHz</option>
38
+ <option value="2000000">2 MHz</option>
39
+ <option value="2457600">2.4576 MHz</option>
40
+ <option value="4915200">4.9152 MHz</option>
41
+ </select>
42
+ </div>
43
+ </script>
44
+
45
+ <script type="text/javascript">
46
+ RED.nodes.registerType('periph-ad7706', {
47
+ category: 'Peripheral',
48
+ color: '#C084FC',
49
+ defaults: {
50
+ name: { value: '' },
51
+ device: { value: '', type: 'ad7706-device', required: true }
52
+ },
53
+ inputs: 1,
54
+ outputs: 1,
55
+ icon: 'font-awesome/fa-wave-square',
56
+ label: function() { return this.name || 'AD7706'; },
57
+ paletteLabel: 'AD7706'
58
+ });
59
+ </script>
60
+
61
+ <script type="text/html" data-template-name="periph-ad7706">
62
+ <div class="form-row">
63
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
64
+ <input type="text" id="node-input-device">
65
+ </div>
66
+ <div class="form-row">
67
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
68
+ <input type="text" id="node-input-name" placeholder="AD7706">
69
+ </div>
70
+ </script>
71
+
72
+ <script type="text/html" data-help-name="periph-ad7706">
73
+ <p>Reads voltage from a 3-channel, 16-bit sigma-delta AD7706 ADC over SPI. The driver uses the chip's two-phase register-access protocol and polls DRDY over SPI, so only SCLK/DIN/DOUT/CS are needed (no dedicated DRDY GPIO). All three channels share a single COMMON reference pin.</p>
74
+
75
+ <h3>Inputs</h3>
76
+ <dl class="message-properties">
77
+ <dt>payload <span class="property-type">any</span></dt>
78
+ <dd>Any incoming message triggers a multi-channel read. Channels 2 and 3 are omitted from the output if their device config has them disabled. The literal string <code>"calibrate"</code> triggers a self-calibration of all configured channels.</dd>
79
+ </dl>
80
+
81
+ <h3>Outputs</h3>
82
+ <dl class="message-properties">
83
+ <dt>payload.channel1 <span class="property-type">number</span></dt>
84
+ <dd>Channel 1 (AIN1 vs COMMON) input voltage in V.</dd>
85
+ <dt>payload.channel2 <span class="property-type">number</span></dt>
86
+ <dd>Channel 2 (AIN2 vs COMMON) input voltage in V (omitted if Channel 2 is disabled).</dd>
87
+ <dt>payload.channel3 <span class="property-type">number</span></dt>
88
+ <dd>Channel 3 (AIN3 vs COMMON) input voltage in V (omitted if Channel 3 is disabled).</dd>
89
+ <dt>payload.calibrated <span class="property-type">boolean</span></dt>
90
+ <dd>True after a <code>"calibrate"</code> command.</dd>
91
+ </dl>
92
+
93
+ <h3>Configuration</h3>
94
+ <p>Set the SPI bus and CS line (Linux <code>/dev/spidevBUS.DEVICE</code>), the reference voltage V_REF in V (default 2.5), and the master clock frequency wired to MCLK IN (1/2/2.4576/4.9152 MHz). SPI Mode 3 (CPOL=1 CPHA=1) is used; the maximum clock is 5 MHz.</p>
95
+ </script>
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const SPIConnection = require('periph/src/connection/spi').SPIConnection;
5
+ const AD7706Full = require('periph/src/chips/adc_dac/ad7706').AD7706Full;
6
+
7
+ function AD7706DeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const connection = new SPIConnection(
12
+ parseInt(config.bus),
13
+ parseInt(config.device),
14
+ { mode: 3, maxSpeedHz: 5_000_000 }
15
+ );
16
+ const vref = parseFloat(config.vref) || 2.5;
17
+ const mclkHz = parseInt(config.mclkHz) || 2457600;
18
+ const chip = new AD7706Full(connection, vref, mclkHz);
19
+
20
+ const channel1 = config.channel1 || { enabled: true, gain: '1', bipolar: true, buffered: false, rate: '50' };
21
+ const channel2 = config.channel2 || { enabled: false, gain: '1', bipolar: true, buffered: false, rate: '50' };
22
+ const channel3 = config.channel3 || { enabled: false, gain: '1', bipolar: true, buffered: false, rate: '50' };
23
+
24
+ const initChannel = (chNum, ch) => {
25
+ if (ch.enabled) {
26
+ chip.configure(chNum, parseInt(ch.gain), ch.bipolar, ch.buffered, parseInt(ch.rate))
27
+ .then(() => chip.selfCalibrate(chNum))
28
+ .catch((err) => node.error(`AD7706 channel ${chNum} init failed: ` + err.message));
29
+ }
30
+ };
31
+
32
+ initChannel(2, channel2);
33
+ initChannel(3, channel3);
34
+
35
+ chip.selfCalibrate(1).then(() => {
36
+ node.chip = chip;
37
+ node.connection = connection;
38
+ node.channel2Enabled = channel2.enabled;
39
+ node.channel3Enabled = channel3.enabled;
40
+ }).catch((err) => {
41
+ node.error('AD7706 channel 1 init failed: ' + err.message);
42
+ });
43
+ } catch (e) {
44
+ node.error('AD7706 init failed: ' + e.message);
45
+ }
46
+ node.on('close', function() {
47
+ if (node.connection) node.connection.close();
48
+ });
49
+ }
50
+ RED.nodes.registerType('ad7706-device', AD7706DeviceNode);
51
+
52
+ function AD7706Node(config) {
53
+ RED.nodes.createNode(this, config);
54
+ const node = this;
55
+ node.device = RED.nodes.getNode(config.device);
56
+
57
+ node.on('input', async function(msg, send, done) {
58
+ if (!node.device || !node.device.chip) {
59
+ done(new Error('No AD7706 device configured'));
60
+ return;
61
+ }
62
+ try {
63
+ const chip = node.device.chip;
64
+ if (msg.payload === 'calibrate') {
65
+ await chip.selfCalibrate(1);
66
+ if (node.device.channel2Enabled) {
67
+ await chip.selfCalibrate(2);
68
+ }
69
+ if (node.device.channel3Enabled) {
70
+ await chip.selfCalibrate(3);
71
+ }
72
+ msg.payload = { calibrated: true };
73
+ send(msg); done();
74
+ return;
75
+ }
76
+ const v1 = await chip.readVoltageChannel(1);
77
+ const result = { channel1: v1 };
78
+ if (node.device.channel2Enabled) {
79
+ const v2 = await chip.readVoltageChannel(2);
80
+ result.channel2 = v2;
81
+ }
82
+ if (node.device.channel3Enabled) {
83
+ const v3 = await chip.readVoltageChannel(3);
84
+ result.channel3 = v3;
85
+ }
86
+ msg.payload = result;
87
+ send(msg); done();
88
+ } catch (e) { done(e); }
89
+ });
90
+ }
91
+ RED.nodes.registerType('periph-ad7706', AD7706Node);
92
+ };