node-red-contrib-periph-adc-dac 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # node-red-contrib-periph-adc-dac
2
+
3
+ Node-RED nodes for ADC and DAC chips — part of the [Periph](https://github.com/tuhde/Periph) library.
4
+
5
+ ## Install
6
+
7
+ Open Node-RED, go to **Manage Palette → Install** and search for `node-red-contrib-periph-adc-dac`.
8
+
9
+ Or from the command line in your Node-RED user directory:
10
+
11
+ ```sh
12
+ npm install node-red-contrib-periph-adc-dac
13
+ ```
14
+
15
+ ## Nodes
16
+
17
+ | Node | Kind | Description |
18
+ |------|------|-------------|
19
+ | `mcp4725-device` | config | I²C bus and address for an MCP4725 |
20
+ | `periph-mcp4725` | output | Write a voltage to the MCP4725 12-bit DAC |
21
+
22
+ ## Links
23
+
24
+ - [GitHub](https://github.com/tuhde/Periph)
25
+ - [periph JS driver](https://www.npmjs.com/package/periph)
@@ -0,0 +1,95 @@
1
+ [
2
+ {
3
+ "id": "tab-hx711-demo",
4
+ "type": "tab",
5
+ "label": "HX711 Scale Demo",
6
+ "disabled": false,
7
+ "info": "Kitchen scale demo using the HX711 24-bit ADC and a load cell. On deploy, an inject node sends 'tare' to zero the scale. Every 2 seconds a second inject node triggers a weight reading. The HX711 node outputs { raw, weight } — weight in grams using the configured scale factor. The debug node shows msg.payload.weight; the gauge displays it as a live reading. Requires node-red-dashboard for the gauge. Set DOUT and PD_SCK to your GPIO pin numbers and calibrate the scale factor before use."
8
+ },
9
+ {
10
+ "id": "inj-tare",
11
+ "type": "inject",
12
+ "name": "Tare on deploy",
13
+ "topic": "",
14
+ "payload": "tare",
15
+ "payloadType": "str",
16
+ "repeat": "",
17
+ "crontab": "",
18
+ "once": true,
19
+ "onceDelay": 0.5,
20
+ "z": "tab-hx711-demo",
21
+ "x": 140,
22
+ "y": 100,
23
+ "wires": [["hx711"]]
24
+ },
25
+ {
26
+ "id": "inj-read",
27
+ "type": "inject",
28
+ "name": "Read every 2 s",
29
+ "topic": "",
30
+ "payload": "",
31
+ "payloadType": "str",
32
+ "repeat": "2",
33
+ "crontab": "",
34
+ "once": false,
35
+ "onceDelay": 0.1,
36
+ "z": "tab-hx711-demo",
37
+ "x": 140,
38
+ "y": 200,
39
+ "wires": [["hx711"]]
40
+ },
41
+ {
42
+ "id": "hx711",
43
+ "type": "periph-hx711",
44
+ "name": "HX711",
45
+ "doutPin": "5",
46
+ "pdSckPin": "6",
47
+ "gain": "128",
48
+ "scaleFactor": "420.0",
49
+ "tareOffset": "0",
50
+ "z": "tab-hx711-demo",
51
+ "x": 360,
52
+ "y": 200,
53
+ "wires": [["dbg-weight", "gauge-weight"]]
54
+ },
55
+ {
56
+ "id": "dbg-weight",
57
+ "type": "debug",
58
+ "name": "weight (g)",
59
+ "active": true,
60
+ "tosidebar": true,
61
+ "console": false,
62
+ "tostatus": false,
63
+ "complete": "payload.weight",
64
+ "targetType": "msg",
65
+ "statusVal": "",
66
+ "statusType": "auto",
67
+ "z": "tab-hx711-demo",
68
+ "x": 580,
69
+ "y": 160,
70
+ "wires": []
71
+ },
72
+ {
73
+ "id": "gauge-weight",
74
+ "type": "ui_gauge",
75
+ "name": "Scale (g)",
76
+ "group": "",
77
+ "order": 0,
78
+ "width": 0,
79
+ "height": 0,
80
+ "gtype": "gage",
81
+ "title": "Weight",
82
+ "label": "g",
83
+ "format": "{{payload.weight | number:1}}",
84
+ "min": "0",
85
+ "max": "1000",
86
+ "colors": ["#00b500", "#e6e600", "#ca3838"],
87
+ "seg1": "500",
88
+ "seg2": "800",
89
+ "className": "",
90
+ "z": "tab-hx711-demo",
91
+ "x": 580,
92
+ "y": 240,
93
+ "wires": []
94
+ }
95
+ ]
@@ -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
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-hx711', {
3
+ category: 'Peripheral',
4
+ color: '#a6bbcf',
5
+ defaults: {
6
+ name: { value: '' },
7
+ doutPin: { value: '5', required: true, validate: RED.validators.number() },
8
+ pdSckPin: { value: '6', required: true, validate: RED.validators.number() },
9
+ gain: { value: '128' },
10
+ scaleFactor: { value: '1.0' },
11
+ tareOffset: { value: '0' }
12
+ },
13
+ inputs: 1,
14
+ outputs: 1,
15
+ icon: 'font-awesome/fa-weight-hanging',
16
+ label: function() { return this.name || 'HX711'; },
17
+ paletteLabel: 'HX711'
18
+ });
19
+ </script>
20
+
21
+ <script type="text/html" data-template-name="periph-hx711">
22
+ <div class="form-row">
23
+ <label for="node-input-doutPin"><i class="fa fa-microchip"></i> DOUT pin</label>
24
+ <input type="number" id="node-input-doutPin" min="0" style="width:80px"> &nbsp;GPIO number (BCM)
25
+ </div>
26
+ <div class="form-row">
27
+ <label for="node-input-pdSckPin"><i class="fa fa-microchip"></i> PD_SCK pin</label>
28
+ <input type="number" id="node-input-pdSckPin" min="0" style="width:80px"> &nbsp;GPIO number (BCM)
29
+ </div>
30
+ <div class="form-row">
31
+ <label for="node-input-gain"><i class="fa fa-sliders-h"></i> Gain</label>
32
+ <select id="node-input-gain" style="width:200px">
33
+ <option value="128">128 — Channel A (default)</option>
34
+ <option value="64">64 — Channel A</option>
35
+ <option value="32">32 — Channel B</option>
36
+ </select>
37
+ </div>
38
+ <div class="form-row">
39
+ <label for="node-input-scaleFactor"><i class="fa fa-balance-scale"></i> Scale factor</label>
40
+ <input type="text" id="node-input-scaleFactor" style="width:120px"> &nbsp;counts per unit weight
41
+ </div>
42
+ <div class="form-row">
43
+ <label for="node-input-tareOffset"><i class="fa fa-crosshairs"></i> Tare offset</label>
44
+ <input type="text" id="node-input-tareOffset" style="width:120px"> &nbsp;or send <code>msg.payload = "tare"</code>
45
+ </div>
46
+ <div class="form-row">
47
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
48
+ <input type="text" id="node-input-name" placeholder="HX711">
49
+ </div>
50
+ </script>
51
+
52
+ <script type="text/html" data-help-name="periph-hx711">
53
+ <p>Reads weight from an HX711 24-bit ADC connected to a load cell via two GPIO pins.</p>
54
+ <p>Any input message triggers a raw ADC read. Sending <code>msg.payload = "tare"</code> captures
55
+ the current reading as the zero offset instead of producing output.</p>
56
+
57
+ <h3>Inputs</h3>
58
+ <dl class="message-properties">
59
+ <dt>payload <span class="property-type">any</span></dt>
60
+ <dd>Any message triggers a weight reading. If <code>payload</code> is the string
61
+ <code>"tare"</code>, the node captures the current average (10 readings) as the
62
+ zero offset and produces no output message.</dd>
63
+ </dl>
64
+
65
+ <h3>Outputs</h3>
66
+ <dl class="message-properties">
67
+ <dt>payload.raw <span class="property-type">number</span></dt>
68
+ <dd>Signed 24-bit ADC value (-8 388 608 to +8 388 607).</dd>
69
+ <dt>payload.weight <span class="property-type">number</span></dt>
70
+ <dd>Calibrated weight: <code>(raw - tareOffset) / scaleFactor</code>.
71
+ Returns <code>0.0</code> when scale factor is 1.0 and offset is 0.</dd>
72
+ </dl>
73
+
74
+ <h3>Details</h3>
75
+ <p><b>DOUT pin</b> and <b>PD_SCK pin</b> are BCM GPIO numbers (Raspberry Pi / Linux GPIO).</p>
76
+ <p><b>Gain</b> selects the input channel and amplifier gain: 128 and 64 use Channel A;
77
+ 32 uses Channel B (fixed gain).</p>
78
+ <p><b>Scale factor</b> is the number of ADC counts per unit weight. Calibrate by placing a
79
+ known weight on the scale: <code>factor = (readAverage() - tareOffset) / knownWeight</code>.</p>
80
+ <p><b>Tare offset</b> sets the initial zero point. You can also zero the scale at any time
81
+ by injecting a message with <code>payload = "tare"</code>.</p>
82
+ </script>
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const Gpio = require('onoff').Gpio;
5
+ const { HX711Transport } = require('periph/src/transport/hx711');
6
+ const { HX711Full } = require('periph/src/chips/adc_dac/hx711');
7
+
8
+ function HX711Node(config) {
9
+ RED.nodes.createNode(this, config);
10
+ const node = this;
11
+
12
+ try {
13
+ const dout = new Gpio(parseInt(config.doutPin), 'in');
14
+ const pd_sck = new Gpio(parseInt(config.pdSckPin), 'out');
15
+ node.transport = new HX711Transport(dout, pd_sck);
16
+ node.driver = new HX711Full(node.transport);
17
+
18
+ const gain = parseInt(config.gain || 128);
19
+ if (gain !== 128) node.driver.setGain(gain);
20
+ node.driver._offset = parseInt(config.tareOffset || 0);
21
+ node.driver.setScale(parseFloat(config.scaleFactor || 1.0));
22
+ } catch (e) {
23
+ node.error('HX711 init failed: ' + e.message);
24
+ }
25
+
26
+ node.on('input', function(msg, send, done) {
27
+ if (!node.driver) { done(); return; }
28
+ try {
29
+ if (msg.payload === 'tare') {
30
+ node.driver.tare(10);
31
+ done();
32
+ } else {
33
+ const raw = node.driver.readRaw();
34
+ const weight = (raw - node.driver.getOffset()) / node.driver.getScale();
35
+ msg.payload = { raw, weight };
36
+ send(msg);
37
+ done();
38
+ }
39
+ } catch (e) {
40
+ done(e);
41
+ }
42
+ });
43
+
44
+ node.on('close', function() {
45
+ if (node.transport) node.transport.close();
46
+ });
47
+ }
48
+
49
+ RED.nodes.registerType('periph-hx711', HX711Node);
50
+ };
@@ -0,0 +1,108 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('mcp4728-device', {
3
+ category: 'config',
4
+ defaults: {
5
+ name: { value: '' },
6
+ bus: { value: '1', required: true, validate: RED.validators.number() },
7
+ address: { value: '60', required: true }
8
+ },
9
+ label: function() {
10
+ return this.name || ('MCP4728 @ 0x' + this.address + ' (bus ' + this.bus + ')');
11
+ }
12
+ });
13
+ </script>
14
+
15
+ <script type="text/html" data-template-name="mcp4728-device">
16
+ <div class="form-row">
17
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
18
+ <input type="text" id="node-config-input-name" placeholder="MCP4728">
19
+ </div>
20
+ <div class="form-row">
21
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
22
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
23
+ </div>
24
+ <div class="form-row">
25
+ <label for="node-config-input-address">Address (hex)</label>
26
+ <input type="text" id="node-config-input-address" placeholder="60" style="width:80px"> &nbsp;default: 60 (0x60)
27
+ </div>
28
+ </script>
29
+
30
+ <script type="text/javascript">
31
+ RED.nodes.registerType('periph-mcp4728', {
32
+ category: 'Peripheral',
33
+ color: '#a6bbcf',
34
+ defaults: {
35
+ name: { value: '' },
36
+ device: { value: '', type: 'mcp4728-device', required: true },
37
+ channel: { value: 'A' },
38
+ inputMode: { value: 'fraction' },
39
+ vref: { value: 'external' },
40
+ gain: { value: 'x1' },
41
+ persist: { value: false }
42
+ },
43
+ inputs: 1,
44
+ outputs: 0,
45
+ icon: 'font-awesome/fa-wave-square',
46
+ label: function() { return this.name || 'MCP4728'; },
47
+ paletteLabel: 'MCP4728'
48
+ });
49
+ </script>
50
+
51
+ <script type="text/html" data-template-name="periph-mcp4728">
52
+ <div class="form-row">
53
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
54
+ <input type="text" id="node-input-device">
55
+ </div>
56
+ <div class="form-row">
57
+ <label for="node-input-channel"><i class="fa fa-sitemap"></i> Channel</label>
58
+ <select id="node-input-channel" style="width:100px">
59
+ <option value="A">A</option>
60
+ <option value="B">B</option>
61
+ <option value="C">C</option>
62
+ <option value="D">D</option>
63
+ </select>
64
+ </div>
65
+ <div class="form-row">
66
+ <label for="node-input-inputMode"><i class="fa fa-cog"></i> Input mode</label>
67
+ <select id="node-input-inputMode" style="width:150px">
68
+ <option value="fraction">fraction (0.0–1.0)</option>
69
+ <option value="raw">raw (0–4095)</option>
70
+ </select>
71
+ </div>
72
+ <div class="form-row">
73
+ <label for="node-input-vref"><i class="fa fa-bolt"></i> V_REF</label>
74
+ <select id="node-input-vref" style="width:150px">
75
+ <option value="external">External (V_DD)</option>
76
+ <option value="internal">Internal (2.048 V)</option>
77
+ </select>
78
+ </div>
79
+ <div class="form-row">
80
+ <label for="node-input-gain"><i class="fa fa-arrows-h"></i> Gain</label>
81
+ <select id="node-input-gain" style="width:100px">
82
+ <option value="x1">×1</option>
83
+ <option value="x2">×2</option>
84
+ </select>
85
+ </div>
86
+ <div class="form-row">
87
+ <label for="node-input-persist"><i class="fa fa-save"></i> Persist to EEPROM</label>
88
+ <input type="checkbox" id="node-input-persist" style="width:auto;margin-top:0">
89
+ </div>
90
+ <div class="form-row">
91
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
92
+ <input type="text" id="node-input-name" placeholder="MCP4728">
93
+ </div>
94
+ </script>
95
+
96
+ <script type="text/html" data-help-name="periph-mcp4728">
97
+ <p>Sets the output voltage of one channel (A–D) of an MCP4728 quad-channel 12-bit DAC over I²C.</p>
98
+ <p>Any input message triggers a DAC write. The device is configured via an <b>mcp4728-device</b> config node. Pass a 4-element array as <code>msg.payload</code> to update all four channels simultaneously via Fast Write.</p>
99
+
100
+ <h3>Inputs</h3>
101
+ <dl class="message-properties">
102
+ <dt>payload <span class="property-type">number | number[4]</span></dt>
103
+ <dd>When input mode is <code>fraction</code>: value between 0.0 and 1.0 representing the output as a fraction of the configured full-scale. When input mode is <code>raw</code>: raw 12-bit DAC code (0–4095). When <code>payload</code> is an array of 4 numbers, the array is applied to channels A–D simultaneously.</dd>
104
+ </dl>
105
+
106
+ <h3>Details</h3>
107
+ <p>Configure bus number and I²C address (default <code>0x60</code>) in the device config node. The input mode determines whether incoming values are interpreted as normalized fractions or raw 12-bit codes. Selecting <i>Persist to EEPROM</i> uses Single Write instead of Multi-Write and updates the nonvolatile EEPROM; the value then survives a power cycle (takes up to 50 ms).</p>
108
+ </script>
@@ -0,0 +1,71 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CTransport } = require('periph/src/transport/i2c');
5
+ const { MCP4728Full } = require('periph/src/chips/adc_dac/mcp4728');
6
+
7
+ function MCP4728DeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const transport = new I2CTransport(parseInt(config.bus), parseInt(config.address, 16));
12
+ node.driver = new MCP4728Full(transport);
13
+ node.transport = transport;
14
+ } catch (e) {
15
+ node.error('MCP4728 init failed: ' + e.message);
16
+ }
17
+ node.on('close', function() {
18
+ if (node.transport) node.transport.close();
19
+ });
20
+ }
21
+ RED.nodes.registerType('mcp4728-device', MCP4728DeviceNode);
22
+
23
+ function MCP4728WriteNode(config) {
24
+ RED.nodes.createNode(this, config);
25
+ const node = this;
26
+ node.device = RED.nodes.getNode(config.device);
27
+ node.channel = (config.channel === undefined ? 'A' : config.channel);
28
+ node.inputMode = config.inputMode || 'fraction';
29
+ node.vref = (config.vref === 'internal') ? 1 : 0;
30
+ node.gain = (config.gain === 'x2') ? 2 : 1;
31
+ node.persist = !!config.persist;
32
+
33
+ node.on('input', function(msg, send, done) {
34
+ if (!node.device || !node.device.driver) {
35
+ node.error('No MCP4728 device configured', msg);
36
+ done();
37
+ return;
38
+ }
39
+ try {
40
+ const dac = node.device.driver;
41
+ const chIdx = { 'A': 0, 'B': 1, 'C': 2, 'D': 3 }[node.channel];
42
+ if (chIdx === undefined) {
43
+ node.error('Invalid channel: ' + node.channel, msg);
44
+ done();
45
+ return;
46
+ }
47
+ if (Array.isArray(msg.payload) && msg.payload.length === 4) {
48
+ dac.set_all(msg.payload);
49
+ } else {
50
+ const v = parseFloat(msg.payload);
51
+ if (Number.isNaN(v)) {
52
+ node.error('Invalid payload: ' + msg.payload, msg);
53
+ done();
54
+ return;
55
+ }
56
+ if (node.inputMode === 'raw') {
57
+ if (node.persist) dac.set_raw_eeprom(chIdx, v, node.vref, node.gain);
58
+ else dac.set_raw(chIdx, v);
59
+ } else {
60
+ if (node.persist) dac.set_voltage_eeprom(chIdx, v, node.vref, node.gain);
61
+ else dac.set_voltage(chIdx, v);
62
+ }
63
+ }
64
+ done();
65
+ } catch (e) {
66
+ done(e);
67
+ }
68
+ });
69
+ }
70
+ RED.nodes.registerType('periph-mcp4728', MCP4728WriteNode);
71
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-adc-dac",
3
- "version": "1.0.0",
3
+ "version": "1.1.0",
4
4
  "description": "Node-RED nodes for ADC and DAC chips",
5
5
  "keywords": [
6
6
  "node-red",
@@ -12,7 +12,8 @@
12
12
  ],
13
13
  "node-red": {
14
14
  "nodes": {
15
- "mcp4725": "nodes/mcp4725/mcp4725.js"
15
+ "mcp4725": "nodes/mcp4725/mcp4725.js",
16
+ "mcp4728": "nodes/mcp4728/mcp4728.js"
16
17
  }
17
18
  },
18
19
  "dependencies": {