node-red-contrib-periph-other 1.5.1 → 1.5.3

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
@@ -1,6 +1,10 @@
1
1
  # node-red-contrib-periph-other
2
2
 
3
- Node-RED nodes for miscellaneous peripheral chips — part of the [Periph](https://github.com/tuhde/Periph) library.
3
+ Node-RED nodes for miscellaneous peripheral chips: MPR121, PCA9541 — part of the [Periph](https://github.com/tuhde/Periph) library.
4
+
5
+ **Supported chips (2):** MPR121, PCA9541.
6
+
7
+ Each chip has a sensor/actuator node plus a shared `-device` config node that holds the bus settings. Importable example flows are in [`examples/`](https://github.com/tuhde/Periph/tree/main/nodejs/packages/node-red-contrib-periph-other/examples).
4
8
 
5
9
  ## Install
6
10
 
@@ -18,7 +22,8 @@ npm install node-red-contrib-periph-other
18
22
  |------|------|-------------|
19
23
  | `mpr121-device` | config | I²C bus and address for a MPR121 |
20
24
  | `mpr121` | input | Reads the 12-electrode touch bitmask from an MPR121 capacitive touch sensor over I²C. |
21
- | `periph-pca9541` | input | Controls the 2-to-1 I²C-bus master selector PCA9541 from one of two redundant masters: take, give up and check ownership of the shared downstream bus. There is no arbitration: whichever master writes last before a STOP wins. |
25
+ | `pca9541-device` | config | I²C bus and address for a PCA9541 |
26
+ | `pca9541` | input | Controls the 2-to-1 I²C-bus master selector PCA9541 from one of two redundant masters: take, give up and check ownership of the shared downstream bus. There is no arbitration: whichever master writes last before a STOP wins. |
22
27
 
23
28
  ## Links
24
29
 
@@ -67,13 +67,18 @@
67
67
  ]
68
68
  },
69
69
  {
70
- "id": "pca9541-node",
71
- "type": "periph-pca9541",
70
+ "id": "pca9541-device-1",
71
+ "type": "pca9541-device",
72
72
  "name": "PCA9541",
73
- "bus": "1",
74
73
  "address": "0x70",
75
- "gpioChip": "0",
74
+ "bus": "1",
76
75
  "intPin": "",
76
+ "gpioChip": "0"
77
+ },
78
+ {
79
+ "id": "pca9541-node",
80
+ "type": "pca9541",
81
+ "name": "PCA9541",
77
82
  "z": "tab-pca9541-demo",
78
83
  "x": 400,
79
84
  "y": 140,
@@ -81,7 +86,8 @@
81
86
  [
82
87
  "dbg-pca9541"
83
88
  ]
84
- ]
89
+ ],
90
+ "device": "pca9541-device-1"
85
91
  },
86
92
  {
87
93
  "id": "dbg-pca9541",
@@ -31,7 +31,7 @@
31
31
  <!-- Read node: trigger -> touch bitmask -->
32
32
  <script type="text/javascript">
33
33
  RED.nodes.registerType('mpr121', {
34
- category: 'Periph',
34
+ category: 'Peripheral',
35
35
  color: '#C084FC',
36
36
  defaults: {
37
37
  name: { value: '' },
@@ -1,7 +1,7 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-pca9541', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('pca9541-device', {
4
+ category: 'config',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  bus: { value: '1', required: true, validate: RED.validators.number() },
@@ -9,26 +9,24 @@
9
9
  gpioChip: { value: '0' },
10
10
  intPin: { value: '' }
11
11
  },
12
- inputs: 1,
13
- outputs: 1,
14
- icon: 'font-awesome/fa-exchange',
15
- label: function() { return this.name || 'PCA9541'; },
16
- paletteLabel: 'PCA9541'
12
+ label: function() {
13
+ return this.name || ('PCA9541 @ ' + this.address + ' (bus ' + this.bus + ')');
14
+ }
17
15
  });
18
16
  </script>
19
17
 
20
- <script type="text/html" data-template-name="periph-pca9541">
18
+ <script type="text/html" data-template-name="pca9541-device">
21
19
  <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="PCA9541">
20
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
21
+ <input type="text" id="node-config-input-name" placeholder="PCA9541">
24
22
  </div>
25
23
  <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)
24
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
25
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
28
26
  </div>
29
27
  <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:200px">
28
+ <label for="node-config-input-address"><i class="fa fa-at"></i> I²C address</label>
29
+ <select id="node-config-input-address" style="width:200px">
32
30
  <option value="0x70">0x70 (A3..A0 = 0000)</option><option value="0x71">0x71</option>
33
31
  <option value="0x72">0x72</option><option value="0x73">0x73</option>
34
32
  <option value="0x74">0x74</option><option value="0x75">0x75</option>
@@ -36,17 +34,45 @@
36
34
  </select>
37
35
  </div>
38
36
  <div class="form-row">
39
- <label for="node-input-gpioChip">GPIO chip</label>
40
- <input type="number" id="node-input-gpioChip" min="0" style="width:80px"> &nbsp;(for the optional INT line)
37
+ <label for="node-config-input-gpioChip">GPIO chip</label>
38
+ <input type="number" id="node-config-input-gpioChip" min="0" style="width:80px"> &nbsp;(for the optional INT line)
41
39
  </div>
42
40
  <div class="form-row">
43
- <label for="node-input-intPin">INT GPIO line</label>
44
- <input type="number" id="node-input-intPin" min="0" style="width:80px" placeholder="none">
41
+ <label for="node-config-input-intPin">INT GPIO line</label>
42
+ <input type="number" id="node-config-input-intPin" min="0" style="width:80px" placeholder="none">
45
43
  &nbsp;this master's INT0 / INT1
46
44
  </div>
47
45
  </script>
48
46
 
49
- <script type="text/html" data-help-name="periph-pca9541">
47
+ <!-- Read node -->
48
+ <script type="text/javascript">
49
+ RED.nodes.registerType('pca9541', {
50
+ category: 'Peripheral',
51
+ color: '#C084FC',
52
+ defaults: {
53
+ name: { value: '' },
54
+ device: { value: '', type: 'pca9541-device', required: true }
55
+ },
56
+ inputs: 1,
57
+ outputs: 1,
58
+ icon: 'font-awesome/fa-exchange',
59
+ label: function() { return this.name || 'PCA9541'; },
60
+ paletteLabel: 'PCA9541'
61
+ });
62
+ </script>
63
+
64
+ <script type="text/html" data-template-name="pca9541">
65
+ <div class="form-row">
66
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
67
+ <input type="text" id="node-input-name" placeholder="PCA9541">
68
+ </div>
69
+ <div class="form-row">
70
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
71
+ <input type="text" id="node-input-device">
72
+ </div>
73
+ </script>
74
+
75
+ <script type="text/html" data-help-name="pca9541">
50
76
  <p>Controls the 2-to-1 I²C-bus master selector PCA9541 from one of two redundant masters: take, give up and check ownership of the shared downstream bus. There is no arbitration: whichever master writes last before a STOP wins.</p>
51
77
 
52
78
  <h3>Inputs</h3>
@@ -4,10 +4,9 @@ module.exports = function(RED) {
4
4
  const { I2CConnection } = require('periph/src/connection/i2c');
5
5
  const { PCA9541Full } = require('periph/src/chips/other/pca9541');
6
6
 
7
- function PCA9541Node(config) {
7
+ function PCA9541DeviceNode(config) {
8
8
  RED.nodes.createNode(this, config);
9
9
  const node = this;
10
-
11
10
  try {
12
11
  const address = parseInt(config.address || '0x70', 16);
13
12
  node.connection = new I2CConnection(parseInt(config.bus, 10), address);
@@ -29,7 +28,7 @@ module.exports = function(RED) {
29
28
  const intPin = new PollingInputPin(() => (line.value ? 1 : 0), 20);
30
29
  await node.ready;
31
30
  await node.driver.onInterrupt((status) => {
32
- node.send({ payload: {
31
+ node.emit('periph-data', { payload: {
33
32
  interrupt: status,
34
33
  int_in: Boolean(status & 0x01),
35
34
  bus_init: Boolean(status & 0x02),
@@ -45,17 +44,40 @@ module.exports = function(RED) {
45
44
  })();
46
45
  }
47
46
 
47
+ node.on('close', async function(removed, done) {
48
+ try {
49
+ if (node.driver) await node.driver.offInterrupt();
50
+ if (node.connection) await node.connection.close();
51
+ } catch (e) {
52
+ // bus may already be gone; nothing more to do on close
53
+ } finally {
54
+ done();
55
+ }
56
+ });
57
+ }
58
+ RED.nodes.registerType('pca9541-device', PCA9541DeviceNode);
59
+
60
+ function PCA9541ReadNode(config) {
61
+ RED.nodes.createNode(this, config);
62
+ const node = this;
63
+ const deviceNode = RED.nodes.getNode(config.device) || {};
64
+ if (deviceNode.on) {
65
+ const onData = (m) => node.send(m);
66
+ deviceNode.on('periph-data', onData);
67
+ node.on('close', () => deviceNode.removeListener('periph-data', onData));
68
+ }
69
+
48
70
  node.on('input', async function(msg, send, done) {
49
- if (!node.driver) { done(new Error('PCA9541 not initialised')); return; }
71
+ if (!deviceNode.driver) { done(new Error('PCA9541 not initialised')); return; }
50
72
  try {
51
- await node.ready;
73
+ await deviceNode.ready;
52
74
  const p = msg.payload;
53
75
  const command = p && typeof p === 'object' ? p.command : undefined;
54
- if (command === 'acquire') await node.driver.acquire(p.busInit === true);
55
- else if (command === 'release') await node.driver.release();
56
- else if (command === 'recover') await node.driver.recoverBus();
57
- else if (command === 'disconnect') await node.driver.disconnect();
58
- const st = await node.driver.status();
76
+ if (command === 'acquire') await deviceNode.driver.acquire(p.busInit === true);
77
+ else if (command === 'release') await deviceNode.driver.release();
78
+ else if (command === 'recover') await deviceNode.driver.recoverBus();
79
+ else if (command === 'disconnect') await deviceNode.driver.disconnect();
80
+ const st = await deviceNode.driver.status();
59
81
  msg.payload = { control: st.hasControl && st.connected, connected: st.connected };
60
82
  send(msg);
61
83
  done();
@@ -63,17 +85,6 @@ module.exports = function(RED) {
63
85
  done(e);
64
86
  }
65
87
  });
66
-
67
- node.on('close', async function(removed, done) {
68
- try {
69
- if (node.driver) await node.driver.offInterrupt();
70
- if (node.connection) await node.connection.close();
71
- } catch (e) {
72
- // bus may already be gone; nothing more to do on close
73
- } finally {
74
- done();
75
- }
76
- });
77
88
  }
78
- RED.nodes.registerType('periph-pca9541', PCA9541Node);
89
+ RED.nodes.registerType('pca9541', PCA9541ReadNode);
79
90
  };
package/package.json CHANGED
@@ -1,19 +1,23 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-other",
3
- "version": "1.5.1",
4
- "description": "Node-RED nodes for miscellaneous peripheral chips",
3
+ "version": "1.5.3",
4
+ "description": "Node-RED nodes for miscellaneous peripheral chips: MPR121, PCA9541",
5
5
  "keywords": [
6
6
  "node-red",
7
7
  "other",
8
8
  "i2c",
9
9
  "spi",
10
10
  "sensor",
11
- "peripheral"
11
+ "peripheral",
12
+ "node-red-contrib",
13
+ "mpr121",
14
+ "pca9541"
12
15
  ],
13
16
  "node-red": {
17
+ "version": ">=3.0.0",
14
18
  "nodes": {
15
19
  "mpr121": "nodes/mpr121/mpr121.js",
16
- "periph-pca9541": "nodes/pca9541/pca9541.js"
20
+ "pca9541": "nodes/pca9541/pca9541.js"
17
21
  }
18
22
  },
19
23
  "dependencies": {
package/index.js DELETED
@@ -1,11 +0,0 @@
1
- const fs = require('fs');
2
- const path = require('path');
3
-
4
- module.exports = function(RED) {
5
- const nodesDir = path.join(__dirname, 'nodes');
6
- if (!fs.existsSync(nodesDir)) return;
7
- fs.readdirSync(nodesDir).forEach(function(chip) {
8
- const nodeFile = path.join(nodesDir, chip, chip + '.js');
9
- if (fs.existsSync(nodeFile)) require(nodeFile)(RED);
10
- });
11
- };