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,71 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/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 connection = new I2CConnection(parseInt(config.bus), parseInt(config.address, 16));
12
+ node.driver = new MCP4728Full(connection);
13
+ node.connection = connection;
14
+ } catch (e) {
15
+ node.error('MCP4728 init failed: ' + e.message);
16
+ }
17
+ node.on('close', function() {
18
+ if (node.connection) node.connection.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', async 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
+ await 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) await dac.set_raw_eeprom(chIdx, v, node.vref, node.gain);
58
+ else await dac.set_raw(chIdx, v);
59
+ } else {
60
+ if (node.persist) await dac.set_voltage_eeprom(chIdx, v, node.vref, node.gain);
61
+ else await 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
+ };
@@ -0,0 +1,68 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-pcf8591', {
3
+ category: 'Peripheral',
4
+ color: '#C084FC',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '48', required: true },
9
+ inputMode: { value: '4 single-ended' }
10
+ },
11
+ inputs: 1,
12
+ outputs: 1,
13
+ icon: 'font-awesome/fa-microchip',
14
+ label: function() { return this.name || 'PCF8591'; },
15
+ paletteLabel: 'PCF8591'
16
+ });
17
+ </script>
18
+
19
+ <script type="text/html" data-template-name="periph-pcf8591">
20
+ <div class="form-row">
21
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
22
+ <input type="text" id="node-input-name" placeholder="PCF8591">
23
+ </div>
24
+ <div class="form-row">
25
+ <label for="node-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
26
+ <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
27
+ </div>
28
+ <div class="form-row">
29
+ <label for="node-input-address">Address (hex)</label>
30
+ <input type="text" id="node-input-address" placeholder="48" style="width:80px"> &nbsp;default: 48 (0x48)
31
+ </div>
32
+ <div class="form-row">
33
+ <label for="node-input-inputMode"><i class="fa fa-cog"></i> Input mode</label>
34
+ <select id="node-input-inputMode" style="width:200px">
35
+ <option value="4 single-ended">4 single-ended</option>
36
+ <option value="3 differential">3 differential</option>
37
+ <option value="mixed">mixed</option>
38
+ <option value="2 differential">2 differential</option>
39
+ </select>
40
+ </div>
41
+ </script>
42
+
43
+ <script type="text/html" data-help-name="periph-pcf8591">
44
+ <p>Reads the four analog inputs of a PCF8591 8-bit ADC/DAC over I²C. Default address is <code>0x48</code>; any address in the range <code>0x48</code>–<code>0x4F</code> is valid depending on the A0/A1/A2 pin states.</p>
45
+
46
+ <h3>Inputs</h3>
47
+ <dl class="message-properties">
48
+ <dt>payload <span class="property-type">any</span></dt>
49
+ <dd>Any incoming message triggers a read of all four channels.</dd>
50
+ <dt>payload.dac <span class="property-type">number</span> (optional)</dt>
51
+ <dd>If set, the value (0–255) is written to the on-chip DAC before the ADC read.</dd>
52
+ </dl>
53
+
54
+ <h3>Outputs</h3>
55
+ <dl class="message-properties">
56
+ <dt>payload.ch0 <span class="property-type">number</span></dt>
57
+ <dd>Raw 8-bit ADC value of channel 0 (0–255).</dd>
58
+ <dt>payload.ch1 <span class="property-type">number</span></dt>
59
+ <dd>Raw 8-bit ADC value of channel 1 (0–255).</dd>
60
+ <dt>payload.ch2 <span class="property-type">number</span></dt>
61
+ <dd>Raw 8-bit ADC value of channel 2 (0–255).</dd>
62
+ <dt>payload.ch3 <span class="property-type">number</span></dt>
63
+ <dd>Raw 8-bit ADC value of channel 3 (0–255).</dd>
64
+ </dl>
65
+
66
+ <h3>Configuration</h3>
67
+ <p>Set the I²C bus number, the chip's 7-bit address (default <code>0x48</code>), and the analog input mode (default: 4 single-ended inputs).</p>
68
+ </script>
@@ -0,0 +1,37 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { PCF8591Full } = require('periph/src/chips/adc_dac/pcf8591');
6
+
7
+ function PCF8591Node(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.driver = new PCF8591Full(connection);
13
+ node.connection = connection;
14
+ } catch (e) {
15
+ node.error('PCF8591 init failed: ' + e.message);
16
+ }
17
+ node.on('input', async function(msg, send, done) {
18
+ if (!node.driver) { done(); return; }
19
+ try {
20
+ const driver = node.driver;
21
+ if (msg.payload && msg.payload.dac !== undefined) {
22
+ await driver.set_dac(msg.payload.dac);
23
+ }
24
+ const all = await driver.read_all();
25
+ msg.payload = { ch0: all[0], ch1: all[1], ch2: all[2], ch3: all[3] };
26
+ send(msg);
27
+ done();
28
+ } catch (e) {
29
+ done(e);
30
+ }
31
+ });
32
+ node.on('close', function() {
33
+ if (node.connection) node.connection.close();
34
+ });
35
+ }
36
+ RED.nodes.registerType('periph-pcf8591', PCF8591Node);
37
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-adc-dac",
3
- "version": "1.0.1",
3
+ "version": "1.2.0",
4
4
  "description": "Node-RED nodes for ADC and DAC chips",
5
5
  "keywords": [
6
6
  "node-red",
@@ -12,11 +12,21 @@
12
12
  ],
13
13
  "node-red": {
14
14
  "nodes": {
15
- "mcp4725": "nodes/mcp4725/mcp4725.js"
15
+ "hx710a": "nodes/hx710a/hx710a.js",
16
+ "hx710b": "nodes/hx710b/hx710b.js",
17
+ "hx711": "nodes/hx711/hx711.js",
18
+ "mcp4725": "nodes/mcp4725/mcp4725.js",
19
+ "mcp4728": "nodes/mcp4728/mcp4728.js",
20
+ "pcf8591": "nodes/pcf8591/pcf8591.js",
21
+ "ad7705": "nodes/ad7705/ad7705.js",
22
+ "ad7706": "nodes/ad7706/ad7706.js"
16
23
  }
17
24
  },
18
25
  "dependencies": {
19
26
  "periph": "*"
20
27
  },
28
+ "optionalDependencies": {
29
+ "opengpio": "^2.0.2"
30
+ },
21
31
  "license": "MIT"
22
32
  }