node-red-contrib-periph-adc-dac 1.3.3 → 1.4.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 CHANGED
@@ -28,6 +28,8 @@ npm install node-red-contrib-periph-adc-dac
28
28
  | `periph-ads1113` | input | Reads the ADS1113 16-bit ΔΣ ADC (one differential input (AIN0 − AIN1), fixed ±2.048 V) over I²C. Default address is 0x48 ; 0x48 – 0x4B are valid depending on the ADDR strap. |
29
29
  | `periph-ads1114` | input | Reads the ADS1114 16-bit ΔΣ ADC (one differential input (AIN0 − AIN1) with programmable gain) over I²C. Default address is 0x48 ; 0x48 – 0x4B are valid depending on the ADDR strap. |
30
30
  | `periph-ads1115` | input | Reads the ADS1115 16-bit ΔΣ ADC (four single-ended or two differential inputs with programmable gain) over I²C. Default address is 0x48 ; 0x48 – 0x4B are valid depending on the ADDR strap. |
31
+ | `es8311-device` | config | I²C bus and address for an ES8311 |
32
+ | `periph-es8311` | input | Controls an ES8311 low-power mono audio codec over its I²C control port. The audio stream itself (I²S) is not handled by this node. |
31
33
  | `periph-hx710a` | input | Reads weight from an HX710A 24-bit ADC connected to a load cell via two GPIO pins. Also exposes the chip's on-chip temperature sensor as a raw ADC code. |
32
34
  | `periph-hx710b` | input | Reads weight from an HX710B 24-bit ADC connected to a load cell via two GPIO pins. Also exposes the chip's DVDD-AVDD supply-voltage-difference channel as a raw ADC code for battery-monitoring applications. |
33
35
  | `periph-hx711` | input | Reads weight from an HX711 24-bit ADC connected to a load cell via two GPIO pins. |
@@ -37,6 +39,8 @@ npm install node-red-contrib-periph-adc-dac
37
39
  | `periph-mcp4725` | output | Sets the output voltage of an MCP4725 12-bit DAC over I²C. |
38
40
  | `mcp4728-device` | config | I²C bus and address for a MCP4728 |
39
41
  | `periph-mcp4728` | output | Sets the output voltage of one channel (A–D) of an MCP4728 quad-channel 12-bit DAC over I²C. |
42
+ | `mcp4xxx-device` | config | I²C bus and address for a MCP4XXX |
43
+ | `periph-mcp4xxx` | input | Sets or reads the volatile wiper(s) of an MCP4XXX I²C digital potentiometer / rheostat (MCP4531 … MCP4662: 7 / 8-bit, one or two networks, volatile or non-volatile). The device is configured via an mcp4xxx-device config node. |
40
44
  | `periph-pcf8591` | input | Reads the four analog inputs of a PCF8591 8-bit ADC/DAC over I²C. Default address is 0x48 ; any address in the range 0x48 – 0x4F is valid depending on the A0/A1/A2 pin states. |
41
45
 
42
46
  ## Links
@@ -0,0 +1,173 @@
1
+ [
2
+ {
3
+ "id": "tab-es8311-demo",
4
+ "type": "tab",
5
+ "label": "ES8311 Volume Control",
6
+ "disabled": false,
7
+ "info": "Runtime volume control of an ES8311 audio codec whose I2S stream is produced elsewhere. An Inject node sets -40 dB on deploy; a second Inject node fires every 10 s and a Function node alternates the volume between -20 dB and -60 dB. The Debug node prints the value read back from the chip, which shows the 0.5 dB quantisation. Adjust MCLK and sample rate in the device config node to match the I2S source."
8
+ },
9
+ {
10
+ "id": "cfg-es8311",
11
+ "type": "es8311-device",
12
+ "name": "ES8311",
13
+ "bus": "1",
14
+ "address": "18",
15
+ "mclk": "12288000",
16
+ "sampleRate": "48000",
17
+ "volume": "-60",
18
+ "micGain": "24"
19
+ },
20
+ {
21
+ "id": "inj-start",
22
+ "type": "inject",
23
+ "name": "-40 dB on deploy",
24
+ "props": [
25
+ {
26
+ "p": "payload"
27
+ }
28
+ ],
29
+ "repeat": "",
30
+ "crontab": "",
31
+ "once": true,
32
+ "onceDelay": 1,
33
+ "topic": "",
34
+ "payload": "{\"volume\":-40,\"mute\":false}",
35
+ "payloadType": "json",
36
+ "z": "tab-es8311-demo",
37
+ "x": 150,
38
+ "y": 100,
39
+ "wires": [
40
+ [
41
+ "es8311"
42
+ ]
43
+ ]
44
+ },
45
+ {
46
+ "id": "inj-tick",
47
+ "type": "inject",
48
+ "name": "every 10 s",
49
+ "props": [
50
+ {
51
+ "p": "payload"
52
+ }
53
+ ],
54
+ "repeat": "10",
55
+ "crontab": "",
56
+ "once": false,
57
+ "onceDelay": 0.1,
58
+ "topic": "",
59
+ "payload": "",
60
+ "payloadType": "date",
61
+ "z": "tab-es8311-demo",
62
+ "x": 130,
63
+ "y": 180,
64
+ "wires": [
65
+ [
66
+ "fn-alternate"
67
+ ]
68
+ ]
69
+ },
70
+ {
71
+ "id": "fn-alternate",
72
+ "type": "function",
73
+ "name": "alternate -20 / -60 dB",
74
+ "func": "const loud = context.get('loud') || false;\ncontext.set('loud', !loud);\nmsg.payload = { volume: loud ? -60 : -20 };\nreturn msg;",
75
+ "outputs": 1,
76
+ "noerr": 0,
77
+ "initialize": "",
78
+ "finalize": "",
79
+ "libs": [],
80
+ "z": "tab-es8311-demo",
81
+ "x": 330,
82
+ "y": 180,
83
+ "wires": [
84
+ [
85
+ "es8311",
86
+ "delay-1s"
87
+ ]
88
+ ]
89
+ },
90
+ {
91
+ "id": "fn-readback",
92
+ "type": "change",
93
+ "name": "read back",
94
+ "rules": [
95
+ {
96
+ "t": "set",
97
+ "p": "payload",
98
+ "pt": "msg",
99
+ "to": "{}",
100
+ "tot": "json"
101
+ }
102
+ ],
103
+ "action": "",
104
+ "property": "",
105
+ "from": "",
106
+ "to": "",
107
+ "reg": false,
108
+ "z": "tab-es8311-demo",
109
+ "x": 360,
110
+ "y": 260,
111
+ "wires": [
112
+ [
113
+ "es8311"
114
+ ]
115
+ ]
116
+ },
117
+ {
118
+ "id": "delay-1s",
119
+ "type": "delay",
120
+ "name": "",
121
+ "pauseType": "delay",
122
+ "timeout": "1",
123
+ "timeoutUnits": "seconds",
124
+ "rate": "1",
125
+ "nbRateUnits": "1",
126
+ "rateUnits": "second",
127
+ "randomFirst": "1",
128
+ "randomLast": "5",
129
+ "randomUnits": "seconds",
130
+ "drop": false,
131
+ "allowrate": false,
132
+ "outputs": 1,
133
+ "z": "tab-es8311-demo",
134
+ "x": 350,
135
+ "y": 220,
136
+ "wires": [
137
+ [
138
+ "fn-readback"
139
+ ]
140
+ ]
141
+ },
142
+ {
143
+ "id": "es8311",
144
+ "type": "periph-es8311",
145
+ "name": "ES8311",
146
+ "device": "cfg-es8311",
147
+ "z": "tab-es8311-demo",
148
+ "x": 560,
149
+ "y": 180,
150
+ "wires": [
151
+ [
152
+ "dbg-output"
153
+ ]
154
+ ]
155
+ },
156
+ {
157
+ "id": "dbg-output",
158
+ "type": "debug",
159
+ "name": "",
160
+ "active": true,
161
+ "tosidebar": true,
162
+ "console": false,
163
+ "tostatus": false,
164
+ "complete": "payload",
165
+ "targetType": "msg",
166
+ "statusVal": "",
167
+ "statusType": "auto",
168
+ "z": "tab-es8311-demo",
169
+ "x": 760,
170
+ "y": 180,
171
+ "wires": []
172
+ }
173
+ ]
@@ -0,0 +1,206 @@
1
+ [
2
+ {
3
+ "id": "tab-mcp4xxx-demo",
4
+ "type": "tab",
5
+ "label": "MCP4XXX Digital Potentiometer Demo",
6
+ "disabled": false,
7
+ "info": "Sets network 0 of an MCP4652 (dual 8-bit rheostat; change the part in the config node for any of the sixteen MCP4531...MCP4662) to a code from a slider-like inject, sweeps the wiper in 16 steps every 500 ms with { step: 16 } to show the resistance staircase (wrapping back to zero), and mirrors the code to network 1 with a third inject. For a non-volatile part (MCP454X/456X/464X/466X) the fourth inject sends { store: true } to keep the current wiper over power cycles. All paths print the resulting code and W-B resistance in ohms to the debug panel."
8
+ },
9
+ {
10
+ "id": "cfg-mcp4xxx",
11
+ "type": "mcp4xxx-device",
12
+ "name": "MCP4652",
13
+ "part": "MCP4652",
14
+ "bus": "1",
15
+ "address": "58",
16
+ "rab": "10000",
17
+ "rwiper": "75",
18
+ "gpioChip": "0",
19
+ "hvcPin": ""
20
+ },
21
+ {
22
+ "id": "inj-set",
23
+ "type": "inject",
24
+ "name": "set code 128",
25
+ "topic": "",
26
+ "payload": "128",
27
+ "payloadType": "num",
28
+ "repeat": "",
29
+ "crontab": "",
30
+ "once": false,
31
+ "onceDelay": 0.1,
32
+ "z": "tab-mcp4xxx-demo",
33
+ "x": 150,
34
+ "y": 80,
35
+ "wires": [
36
+ [
37
+ "fn-set"
38
+ ]
39
+ ]
40
+ },
41
+ {
42
+ "id": "fn-set",
43
+ "type": "function",
44
+ "name": "network 0 code",
45
+ "func": "msg.payload = { channel: 0, code: msg.payload };\nreturn msg;",
46
+ "outputs": 1,
47
+ "noerr": 0,
48
+ "initialize": "",
49
+ "finalize": "",
50
+ "libs": [],
51
+ "z": "tab-mcp4xxx-demo",
52
+ "x": 380,
53
+ "y": 80,
54
+ "wires": [
55
+ [
56
+ "mcp4xxx"
57
+ ]
58
+ ]
59
+ },
60
+ {
61
+ "id": "inj-sweep",
62
+ "type": "inject",
63
+ "name": "step +16 every 500 ms",
64
+ "topic": "",
65
+ "payload": "",
66
+ "payloadType": "date",
67
+ "repeat": "0.5",
68
+ "crontab": "",
69
+ "once": false,
70
+ "onceDelay": 0.1,
71
+ "z": "tab-mcp4xxx-demo",
72
+ "x": 150,
73
+ "y": 140,
74
+ "wires": [
75
+ [
76
+ "fn-sweep"
77
+ ]
78
+ ]
79
+ },
80
+ {
81
+ "id": "fn-sweep",
82
+ "type": "function",
83
+ "name": "staircase step",
84
+ "func": "let n = context.get('n') || 0;\nmsg.payload = n % 16 === 0 ? { channel: 0, code: 0 } : { channel: 0, step: 16 };\ncontext.set('n', n + 1);\nreturn msg;",
85
+ "outputs": 1,
86
+ "noerr": 0,
87
+ "initialize": "",
88
+ "finalize": "",
89
+ "libs": [],
90
+ "z": "tab-mcp4xxx-demo",
91
+ "x": 380,
92
+ "y": 140,
93
+ "wires": [
94
+ [
95
+ "mcp4xxx"
96
+ ]
97
+ ]
98
+ },
99
+ {
100
+ "id": "inj-mirror",
101
+ "type": "inject",
102
+ "name": "mirror to network 1",
103
+ "topic": "",
104
+ "payload": "64",
105
+ "payloadType": "num",
106
+ "repeat": "",
107
+ "crontab": "",
108
+ "once": false,
109
+ "onceDelay": 0.1,
110
+ "z": "tab-mcp4xxx-demo",
111
+ "x": 150,
112
+ "y": 200,
113
+ "wires": [
114
+ [
115
+ "fn-mirror"
116
+ ]
117
+ ]
118
+ },
119
+ {
120
+ "id": "fn-mirror",
121
+ "type": "function",
122
+ "name": "network 1 code",
123
+ "func": "msg.payload = { channel: 1, code: msg.payload };\nreturn msg;",
124
+ "outputs": 1,
125
+ "noerr": 0,
126
+ "initialize": "",
127
+ "finalize": "",
128
+ "libs": [],
129
+ "z": "tab-mcp4xxx-demo",
130
+ "x": 380,
131
+ "y": 200,
132
+ "wires": [
133
+ [
134
+ "mcp4xxx"
135
+ ]
136
+ ]
137
+ },
138
+ {
139
+ "id": "inj-store",
140
+ "type": "inject",
141
+ "name": "store wiper (NV parts)",
142
+ "topic": "",
143
+ "payload": "",
144
+ "payloadType": "date",
145
+ "repeat": "",
146
+ "crontab": "",
147
+ "once": false,
148
+ "onceDelay": 0.1,
149
+ "z": "tab-mcp4xxx-demo",
150
+ "x": 150,
151
+ "y": 260,
152
+ "wires": [
153
+ [
154
+ "fn-store"
155
+ ]
156
+ ]
157
+ },
158
+ {
159
+ "id": "fn-store",
160
+ "type": "function",
161
+ "name": "store",
162
+ "func": "msg.payload = { channel: 0, store: true };\nreturn msg;",
163
+ "outputs": 1,
164
+ "noerr": 0,
165
+ "initialize": "",
166
+ "finalize": "",
167
+ "libs": [],
168
+ "z": "tab-mcp4xxx-demo",
169
+ "x": 380,
170
+ "y": 260,
171
+ "wires": [
172
+ [
173
+ "mcp4xxx"
174
+ ]
175
+ ]
176
+ },
177
+ {
178
+ "id": "mcp4xxx",
179
+ "type": "periph-mcp4xxx",
180
+ "name": "MCP4XXX",
181
+ "device": "cfg-mcp4xxx",
182
+ "z": "tab-mcp4xxx-demo",
183
+ "x": 600,
184
+ "y": 140,
185
+ "wires": [
186
+ [
187
+ "dbg"
188
+ ]
189
+ ]
190
+ },
191
+ {
192
+ "id": "dbg",
193
+ "type": "debug",
194
+ "name": "code and resistance",
195
+ "active": true,
196
+ "tosidebar": true,
197
+ "console": false,
198
+ "tostatus": false,
199
+ "complete": "payload",
200
+ "targetType": "msg",
201
+ "z": "tab-mcp4xxx-demo",
202
+ "x": 820,
203
+ "y": 140,
204
+ "wires": []
205
+ }
206
+ ]
@@ -0,0 +1,111 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('es8311-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '18', required: true },
9
+ mclk: { value: '12288000', required: true, validate: RED.validators.number() },
10
+ sampleRate: { value: '48000', required: true, validate: RED.validators.number() },
11
+ volume: { value: '-40', validate: RED.validators.number(true) },
12
+ micGain: { value: '24', validate: RED.validators.number(true) }
13
+ },
14
+ label: function() {
15
+ return this.name || ('ES8311 @ 0x' + this.address + ' (bus ' + this.bus + ')');
16
+ }
17
+ });
18
+ </script>
19
+
20
+ <script type="text/html" data-template-name="es8311-device">
21
+ <div class="form-row">
22
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
23
+ <input type="text" id="node-config-input-name" placeholder="ES8311">
24
+ </div>
25
+ <div class="form-row">
26
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
27
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
28
+ </div>
29
+ <div class="form-row">
30
+ <label for="node-config-input-address">Address (hex)</label>
31
+ <select id="node-config-input-address" style="width:150px">
32
+ <option value="18">0x18 (CE low)</option>
33
+ <option value="19">0x19 (CE high)</option>
34
+ </select>
35
+ </div>
36
+ <div class="form-row">
37
+ <label for="node-config-input-mclk">MCLK (Hz)</label>
38
+ <input type="number" id="node-config-input-mclk" min="1" style="width:120px">
39
+ </div>
40
+ <div class="form-row">
41
+ <label for="node-config-input-sampleRate">Sample rate (Hz)</label>
42
+ <input type="number" id="node-config-input-sampleRate" min="1" style="width:120px">
43
+ </div>
44
+ <div class="form-row">
45
+ <label for="node-config-input-volume">Initial volume (dB)</label>
46
+ <input type="number" id="node-config-input-volume" min="-95.5" max="32" step="0.5" style="width:120px">
47
+ </div>
48
+ <div class="form-row">
49
+ <label for="node-config-input-micGain">Microphone gain (dB)</label>
50
+ <input type="number" id="node-config-input-micGain" min="0" max="30" step="3" style="width:120px"> &nbsp;0–30 dB
51
+ </div>
52
+ </script>
53
+
54
+ <!-- Control node: volume / mute / mic gain / readback -->
55
+ <script type="text/javascript">
56
+ RED.nodes.registerType('periph-es8311', {
57
+ category: 'Peripheral',
58
+ color: '#C084FC',
59
+ defaults: {
60
+ name: { value: '' },
61
+ device: { value: '', type: 'es8311-device', required: true }
62
+ },
63
+ inputs: 1,
64
+ outputs: 1,
65
+ icon: 'font-awesome/fa-volume-up',
66
+ label: function() { return this.name || 'ES8311'; },
67
+ paletteLabel: 'ES8311'
68
+ });
69
+ </script>
70
+
71
+ <script type="text/html" data-template-name="periph-es8311">
72
+ <div class="form-row">
73
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
74
+ <input type="text" id="node-input-device">
75
+ </div>
76
+ <div class="form-row">
77
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
78
+ <input type="text" id="node-input-name" placeholder="ES8311">
79
+ </div>
80
+ </script>
81
+
82
+ <script type="text/html" data-help-name="periph-es8311">
83
+ <p>Controls an ES8311 low-power mono audio codec over its I²C control port. The audio stream
84
+ itself (I²S) is not handled by this node.</p>
85
+
86
+ <h3>Inputs</h3>
87
+ <dl class="message-properties">
88
+ <dt class="optional">payload.volume <span class="property-type">number</span></dt>
89
+ <dd>DAC volume in dB (−95.5 … +32, 0.5 dB steps).</dd>
90
+ <dt class="optional">payload.mute <span class="property-type">boolean</span></dt>
91
+ <dd>Mute (true) or unmute (false) the DAC.</dd>
92
+ <dt class="optional">payload.mic_gain <span class="property-type">number</span></dt>
93
+ <dd>Analog microphone PGA gain in dB (0 … 30, 3 dB steps).</dd>
94
+ </dl>
95
+ <p>Several fields may be combined in one message. Any message without one of these fields
96
+ reads the current settings back.</p>
97
+
98
+ <h3>Output</h3>
99
+ <dl class="message-properties">
100
+ <dt>payload.volume <span class="property-type">number</span></dt>
101
+ <dd>DAC volume read back from the chip, in dB.</dd>
102
+ <dt>payload.mic_gain <span class="property-type">number</span></dt>
103
+ <dd>PGA gain read back from the chip, in dB.</dd>
104
+ </dl>
105
+ <p>Command messages produce no output.</p>
106
+
107
+ <h3>Configuration</h3>
108
+ <p>Set the I²C bus number, the 7-bit address (<code>0x18</code> with CE low, <code>0x19</code> with
109
+ CE high), the MCLK frequency and sample rate (must give a valid divider set, see the datasheet clock
110
+ table), the initial DAC volume and the microphone gain in the device config node.</p>
111
+ </script>
@@ -0,0 +1,57 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { ES8311Full } = require('periph/src/chips/adc_dac/es8311');
6
+
7
+ function ES8311DeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const connection = new I2CConnection(parseInt(config.bus), parseInt(config.address, 16));
12
+ node.connection = connection;
13
+ node.driver = new ES8311Full(connection, parseInt(config.mclk) || 12288000, parseInt(config.sampleRate) || 48000);
14
+ node.ready = (async () => {
15
+ if (config.volume !== undefined && config.volume !== '') await node.driver.setVolume(parseFloat(config.volume));
16
+ if (config.micGain !== undefined && config.micGain !== '') await node.driver.setMicGain(parseFloat(config.micGain));
17
+ })();
18
+ node.ready.catch((e) => node.error('ES8311 init failed: ' + e.message));
19
+ } catch (e) {
20
+ node.error('ES8311 init failed: ' + e.message);
21
+ }
22
+ node.on('close', function() {
23
+ if (node.connection) node.connection.close();
24
+ });
25
+ }
26
+ RED.nodes.registerType('es8311-device', ES8311DeviceNode);
27
+
28
+ function ES8311Node(config) {
29
+ RED.nodes.createNode(this, config);
30
+ const node = this;
31
+ node.device = RED.nodes.getNode(config.device);
32
+
33
+ node.on('input', async function(msg, send, done) {
34
+ if (!node.device || !node.device.driver) {
35
+ node.error('No ES8311 device configured', msg);
36
+ done();
37
+ return;
38
+ }
39
+ try {
40
+ const d = node.device.driver;
41
+ const p = (msg.payload !== null && typeof msg.payload === 'object') ? msg.payload : {};
42
+ let command = false;
43
+ if (p.volume !== undefined) { await d.setVolume(Number(p.volume)); command = true; }
44
+ if (p.mute !== undefined) { await d.setMute(!!p.mute); command = true; }
45
+ if (p.mic_gain !== undefined) { await d.setMicGain(Number(p.mic_gain)); command = true; }
46
+ if (!command) {
47
+ msg.payload = { volume: await d.getVolume(), mic_gain: await d.getMicGain() };
48
+ send(msg);
49
+ }
50
+ done();
51
+ } catch (e) {
52
+ done(e);
53
+ }
54
+ });
55
+ }
56
+ RED.nodes.registerType('periph-es8311', ES8311Node);
57
+ };
@@ -0,0 +1,167 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('mcp4xxx-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ part: { value: 'MCP4532', required: true },
8
+ bus: { value: '1', required: true, validate: RED.validators.number() },
9
+ address: { value: '58', required: true, validate: RED.validators.regex(/^[0-9a-fA-F]{1,2}$/) },
10
+ rab: { value: '10000', required: true, validate: RED.validators.number() },
11
+ rwiper: { value: '75', required: true, validate: RED.validators.number() },
12
+ gpioChip: { value: '0' },
13
+ hvcPin: { value: '' }
14
+ },
15
+ label: function() {
16
+ return this.name || (this.part + ' @ 0x' + this.address + ' (bus ' + this.bus + ')');
17
+ },
18
+ oneditprepare: function() {
19
+ // Part digits "4 S M P": S = 5 single / 6 dual, M = 3/5 volatile, 4/6 non-volatile, P = 1 pot / 2 rheostat.
20
+ // The address range follows the part: 45X1 0x2E-0x2F, 45X2 / 46X2 0x58-0x5B, 46X1 0x28-0x2F.
21
+ function fill() {
22
+ const d = $('#node-config-input-part').val().slice(3);
23
+ const dual = d[1] === '6';
24
+ const nv = d[2] === '4' || d[2] === '6';
25
+ const pot = d[3] === '1';
26
+ const lo = pot ? (dual ? 0x28 : 0x2E) : 0x58;
27
+ const hi = pot ? 0x2F : 0x5B;
28
+ const sel = $('#node-config-input-address');
29
+ const cur = parseInt(sel.val() || '0', 16);
30
+ sel.empty();
31
+ for (let a = lo; a <= hi; a++) sel.append($('<option>').val(a.toString(16).toUpperCase()).text('0x' + a.toString(16).toUpperCase()));
32
+ sel.val((cur >= lo && cur <= hi ? cur : lo).toString(16).toUpperCase());
33
+ $('#mcp4xxx-hvc-row').toggle(nv);
34
+ }
35
+ $('#node-config-input-part').on('change', fill);
36
+ fill();
37
+ }
38
+ });
39
+ </script>
40
+
41
+ <script type="text/html" data-template-name="mcp4xxx-device">
42
+ <div class="form-row">
43
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
44
+ <input type="text" id="node-config-input-name" placeholder="MCP4XXX">
45
+ </div>
46
+ <div class="form-row">
47
+ <label for="node-config-input-part">Part</label>
48
+ <select id="node-config-input-part" style="width:120px">
49
+ <option value="MCP4531">MCP4531</option>
50
+ <option value="MCP4532">MCP4532</option>
51
+ <option value="MCP4541">MCP4541</option>
52
+ <option value="MCP4542">MCP4542</option>
53
+ <option value="MCP4551">MCP4551</option>
54
+ <option value="MCP4552">MCP4552</option>
55
+ <option value="MCP4561">MCP4561</option>
56
+ <option value="MCP4562">MCP4562</option>
57
+ <option value="MCP4631">MCP4631</option>
58
+ <option value="MCP4632">MCP4632</option>
59
+ <option value="MCP4641">MCP4641</option>
60
+ <option value="MCP4642">MCP4642</option>
61
+ <option value="MCP4651">MCP4651</option>
62
+ <option value="MCP4652">MCP4652</option>
63
+ <option value="MCP4661">MCP4661</option>
64
+ <option value="MCP4662">MCP4662</option>
65
+ </select>
66
+ </div>
67
+ <div class="form-row">
68
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
69
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
70
+ </div>
71
+ <div class="form-row">
72
+ <label for="node-config-input-address">Address (hex)</label>
73
+ <select id="node-config-input-address" style="width:100px"></select>
74
+ </div>
75
+ <div class="form-row">
76
+ <label for="node-config-input-rab">RAB (Ω)</label>
77
+ <select id="node-config-input-rab" style="width:100px">
78
+ <option value="5000">5 kΩ</option>
79
+ <option value="10000">10 kΩ</option>
80
+ <option value="50000">50 kΩ</option>
81
+ <option value="100000">100 kΩ</option>
82
+ </select>
83
+ </div>
84
+ <div class="form-row">
85
+ <label for="node-config-input-rwiper">Wiper R (Ω)</label>
86
+ <input type="number" id="node-config-input-rwiper" min="0" style="width:100px"> &nbsp;typical 75 Ω
87
+ </div>
88
+ <div class="form-row" id="mcp4xxx-hvc-row">
89
+ <label for="node-config-input-hvcPin">HVC GPIO</label>
90
+ <input type="number" id="node-config-input-hvcPin" min="0" style="width:80px" placeholder="none"> &nbsp;chip
91
+ <input type="number" id="node-config-input-gpioChip" min="0" style="width:60px">
92
+ <div class="form-tips">Non-volatile parts only: GPIO line that switches the external 8.5-12.5 V circuit on HVC/A0 (enables WiperLock / write-protect commands). Leave empty if unused.</div>
93
+ </div>
94
+ </script>
95
+
96
+ <!-- Node: set or read the wipers -->
97
+ <script type="text/javascript">
98
+ RED.nodes.registerType('periph-mcp4xxx', {
99
+ category: 'Peripheral',
100
+ color: '#C084FC',
101
+ defaults: {
102
+ name: { value: '' },
103
+ device: { value: '', type: 'mcp4xxx-device', required: true }
104
+ },
105
+ inputs: 1,
106
+ outputs: 1,
107
+ icon: 'font-awesome/fa-sliders',
108
+ label: function() { return this.name || 'MCP4XXX'; },
109
+ paletteLabel: 'MCP4XXX'
110
+ });
111
+ </script>
112
+
113
+ <script type="text/html" data-template-name="periph-mcp4xxx">
114
+ <div class="form-row">
115
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
116
+ <input type="text" id="node-input-device">
117
+ </div>
118
+ <div class="form-row">
119
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
120
+ <input type="text" id="node-input-name" placeholder="MCP4XXX">
121
+ </div>
122
+ </script>
123
+
124
+ <script type="text/html" data-help-name="periph-mcp4xxx">
125
+ <p>Sets or reads the volatile wiper(s) of an MCP4XXX I²C digital potentiometer / rheostat
126
+ (MCP4531 … MCP4662: 7 / 8-bit, one or two networks, volatile or non-volatile). The device is
127
+ configured via an <b>mcp4xxx-device</b> config node.</p>
128
+
129
+ <h3>Inputs</h3>
130
+ <dl class="message-properties">
131
+ <dt>payload.channel <span class="property-type">number</span></dt>
132
+ <dd>Network 0 (default) or 1 (dual parts).</dd>
133
+ <dt>payload.code <span class="property-type">number</span></dt>
134
+ <dd>Wiper position 0–128 (7-bit parts) / 0–256 (8-bit parts). Takes precedence over the other set fields.</dd>
135
+ <dt>payload.ohms <span class="property-type">number</span></dt>
136
+ <dd>Target wiper-to-B resistance in ohms; the nearest reachable code is used.</dd>
137
+ <dt>payload.ratio <span class="property-type">number</span></dt>
138
+ <dd>Divider ratio 0–1.</dd>
139
+ <dt>payload.step <span class="property-type">number</span></dt>
140
+ <dd>Increment (positive) or decrement (negative) the wiper by that many steps.</dd>
141
+ <dt>payload.shutdown <span class="property-type">boolean</span></dt>
142
+ <dd><code>true</code> shuts the network down (A open, W tied to B), <code>false</code> resumes.</dd>
143
+ <dt>payload.store <span class="property-type">boolean</span></dt>
144
+ <dd>Non-volatile parts: store the current wiper in the EEPROM wiper.</dd>
145
+ <dt>payload.data <span class="property-type">object</span></dt>
146
+ <dd>Non-volatile parts: <code>{ index: 0-9, value?: 0-511 }</code> reads (and, with <code>value</code>, writes) a general-purpose EEPROM register.</dd>
147
+ </dl>
148
+ <p>Any other message reads back all networks.</p>
149
+
150
+ <h3>Outputs</h3>
151
+ <dl class="message-properties">
152
+ <dt>payload.channel, payload.code, payload.resistance</dt>
153
+ <dd>After a write: the network, the resulting wiper code and its W–B resistance (Ω); plus
154
+ <code>shutdown</code> / <code>stored</code> where requested.</dd>
155
+ <dt>payload.index, payload.value</dt>
156
+ <dd>After an EEPROM data access.</dd>
157
+ <dt>payload.wiper, payload.resistance <span class="property-type">array</span></dt>
158
+ <dd>After a read: one entry per network; non-volatile parts also return <code>payload.status</code>
159
+ (<code>eepromBusy</code>, <code>wiperLock</code>, <code>writeProtect</code>).</dd>
160
+ </dl>
161
+
162
+ <h3>Configuration</h3>
163
+ <p>Choose the part, I²C bus and address (limited to the part's range), the end-to-end resistance RAB
164
+ (5 / 10 / 50 / 100 kΩ) and the wiper resistance RW. Volatile wipers return to mid-scale at power-up;
165
+ non-volatile parts restore the stored EEPROM wiper. The optional HVC GPIO (non-volatile parts) drives the
166
+ external high-voltage circuit needed for WiperLock / write-protect changes.</p>
167
+ </script>
@@ -0,0 +1,96 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { Mcp4xxxFull } = require('periph/src/chips/adc_dac/mcp4xxx');
6
+ const { GpioOutputPin } = require('periph/src/connection/output_pin');
7
+
8
+ function Mcp4xxxDeviceNode(config) {
9
+ RED.nodes.createNode(this, config);
10
+ const node = this;
11
+ try {
12
+ const connection = new I2CConnection(parseInt(config.bus), parseInt(config.address, 16));
13
+ let hvc = null;
14
+ if (config.hvcPin !== undefined && config.hvcPin !== '') {
15
+ // opengpio is only needed when a high-voltage control pin is configured.
16
+ const { Default } = require('opengpio');
17
+ hvc = new GpioOutputPin(Default.output({ chip: parseInt(config.gpioChip || 0), line: parseInt(config.hvcPin) }));
18
+ }
19
+ node.driver = new Mcp4xxxFull(connection, config.part || 'MCP4532',
20
+ parseFloat(config.rab || '10000'), parseFloat(config.rwiper || '75'), hvc);
21
+ node.connection = connection;
22
+ node.driver.init().catch(function(e) { node.error('MCP4XXX init failed: ' + e.message); });
23
+ } catch (e) {
24
+ node.error('MCP4XXX init failed: ' + e.message);
25
+ }
26
+ node.on('close', function() {
27
+ if (node.connection) node.connection.close();
28
+ });
29
+ }
30
+ RED.nodes.registerType('mcp4xxx-device', Mcp4xxxDeviceNode);
31
+
32
+ async function snapshot(d, channel) {
33
+ return {
34
+ channel: channel,
35
+ code: await d.getWiper(channel),
36
+ resistance: await d.getResistance(channel)
37
+ };
38
+ }
39
+
40
+ function Mcp4xxxNode(config) {
41
+ RED.nodes.createNode(this, config);
42
+ const node = this;
43
+ node.device = RED.nodes.getNode(config.device);
44
+
45
+ node.on('input', async function(msg, send, done) {
46
+ if (!node.device || !node.device.driver) {
47
+ node.error('No MCP4XXX device configured', msg);
48
+ done();
49
+ return;
50
+ }
51
+ try {
52
+ const d = node.device.driver;
53
+ const p = msg.payload;
54
+ if (p && typeof p === 'object' && p.data !== undefined) {
55
+ const index = parseInt(p.data.index);
56
+ if (p.data.value !== undefined) await d.writeData(index, parseInt(p.data.value));
57
+ msg.payload = { index: index, value: await d.readData(index) };
58
+ } else if (p && typeof p === 'object' && (p.code !== undefined || p.ohms !== undefined ||
59
+ p.ratio !== undefined || p.step !== undefined || p.shutdown !== undefined ||
60
+ p.store !== undefined)) {
61
+ const channel = p.channel !== undefined ? parseInt(p.channel) : 0;
62
+ if (p.code !== undefined) {
63
+ await d.setWiper(parseInt(p.code), channel);
64
+ } else if (p.ohms !== undefined) {
65
+ await d.setResistance(parseFloat(p.ohms), channel);
66
+ } else if (p.ratio !== undefined) {
67
+ await d.setRatio(parseFloat(p.ratio), channel);
68
+ } else if (p.step !== undefined) {
69
+ const n = parseInt(p.step);
70
+ if (n >= 0) await d.increment(n, channel); else await d.decrement(-n, channel);
71
+ } else if (p.shutdown !== undefined) {
72
+ if (p.shutdown) await d.shutdown(channel); else await d.resume(channel);
73
+ }
74
+ const out = await snapshot(d, channel);
75
+ if (p.shutdown !== undefined) out.shutdown = await d.isShutdown(channel);
76
+ if (p.store) out.stored = await d.storeWiper(channel);
77
+ msg.payload = out;
78
+ } else {
79
+ const wiper = [];
80
+ const resistance = [];
81
+ for (let c = 0; c < d.channels(); c++) {
82
+ wiper.push(await d.getWiper(c));
83
+ resistance.push(await d.getResistance(c));
84
+ }
85
+ msg.payload = { wiper: wiper, resistance: resistance };
86
+ if (d.isNonvolatile()) msg.payload.status = await d.readStatus();
87
+ }
88
+ send(msg);
89
+ done();
90
+ } catch (e) {
91
+ done(e);
92
+ }
93
+ });
94
+ }
95
+ RED.nodes.registerType('periph-mcp4xxx', Mcp4xxxNode);
96
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-adc-dac",
3
- "version": "1.3.3",
3
+ "version": "1.4.0",
4
4
  "description": "Node-RED nodes for ADC and DAC chips",
5
5
  "keywords": [
6
6
  "node-red",
@@ -21,12 +21,14 @@
21
21
  "ads1113": "nodes/ads1113/ads1113.js",
22
22
  "ads1114": "nodes/ads1114/ads1114.js",
23
23
  "ads1115": "nodes/ads1115/ads1115.js",
24
+ "es8311": "nodes/es8311/es8311.js",
24
25
  "hx710a": "nodes/hx710a/hx710a.js",
25
26
  "hx710b": "nodes/hx710b/hx710b.js",
26
27
  "hx711": "nodes/hx711/hx711.js",
27
28
  "hy3131": "nodes/hy3131/hy3131.js",
28
29
  "mcp4725": "nodes/mcp4725/mcp4725.js",
29
30
  "mcp4728": "nodes/mcp4728/mcp4728.js",
31
+ "mcp4xxx": "nodes/mcp4xxx/mcp4xxx.js",
30
32
  "pcf8591": "nodes/pcf8591/pcf8591.js"
31
33
  }
32
34
  },