node-red-contrib-periph-other 1.4.0 → 1.5.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 +1 -0
- package/examples/pca9541/demo.json +101 -0
- package/nodes/pca9541/pca9541.html +72 -0
- package/nodes/pca9541/pca9541.js +79 -0
- package/package.json +3 -2
package/README.md
CHANGED
|
@@ -18,6 +18,7 @@ npm install node-red-contrib-periph-other
|
|
|
18
18
|
|------|------|-------------|
|
|
19
19
|
| `mpr121-device` | config | I²C bus and address for a MPR121 |
|
|
20
20
|
| `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. |
|
|
21
22
|
|
|
22
23
|
## Links
|
|
23
24
|
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"id": "tab-pca9541-demo",
|
|
4
|
+
"type": "tab",
|
|
5
|
+
"label": "PCA9541 Failover Demo",
|
|
6
|
+
"disabled": false,
|
|
7
|
+
"info": "Bus ownership on one of two redundant masters. An inject node sends {command: 'acquire'} to take the downstream bus, a second inject reads the status every 5 s into a debug node, and a third sends {command: 'release'}. With this master's INT line wired, a message with bus_lost = true is emitted when the other master takes the bus."
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"id": "inj-acquire",
|
|
11
|
+
"type": "inject",
|
|
12
|
+
"name": "Acquire",
|
|
13
|
+
"topic": "",
|
|
14
|
+
"payload": "{\"command\":\"acquire\"}",
|
|
15
|
+
"payloadType": "json",
|
|
16
|
+
"repeat": "",
|
|
17
|
+
"crontab": "",
|
|
18
|
+
"once": true,
|
|
19
|
+
"onceDelay": 1,
|
|
20
|
+
"z": "tab-pca9541-demo",
|
|
21
|
+
"x": 130,
|
|
22
|
+
"y": 80,
|
|
23
|
+
"wires": [
|
|
24
|
+
[
|
|
25
|
+
"pca9541-node"
|
|
26
|
+
]
|
|
27
|
+
]
|
|
28
|
+
},
|
|
29
|
+
{
|
|
30
|
+
"id": "inj-status",
|
|
31
|
+
"type": "inject",
|
|
32
|
+
"name": "Status every 5 s",
|
|
33
|
+
"topic": "",
|
|
34
|
+
"payload": "",
|
|
35
|
+
"payloadType": "date",
|
|
36
|
+
"repeat": "5",
|
|
37
|
+
"crontab": "",
|
|
38
|
+
"once": true,
|
|
39
|
+
"onceDelay": 2,
|
|
40
|
+
"z": "tab-pca9541-demo",
|
|
41
|
+
"x": 150,
|
|
42
|
+
"y": 140,
|
|
43
|
+
"wires": [
|
|
44
|
+
[
|
|
45
|
+
"pca9541-node"
|
|
46
|
+
]
|
|
47
|
+
]
|
|
48
|
+
},
|
|
49
|
+
{
|
|
50
|
+
"id": "inj-release",
|
|
51
|
+
"type": "inject",
|
|
52
|
+
"name": "Release",
|
|
53
|
+
"topic": "",
|
|
54
|
+
"payload": "{\"command\":\"release\"}",
|
|
55
|
+
"payloadType": "json",
|
|
56
|
+
"repeat": "",
|
|
57
|
+
"crontab": "",
|
|
58
|
+
"once": false,
|
|
59
|
+
"onceDelay": 0.1,
|
|
60
|
+
"z": "tab-pca9541-demo",
|
|
61
|
+
"x": 130,
|
|
62
|
+
"y": 200,
|
|
63
|
+
"wires": [
|
|
64
|
+
[
|
|
65
|
+
"pca9541-node"
|
|
66
|
+
]
|
|
67
|
+
]
|
|
68
|
+
},
|
|
69
|
+
{
|
|
70
|
+
"id": "pca9541-node",
|
|
71
|
+
"type": "periph-pca9541",
|
|
72
|
+
"name": "PCA9541",
|
|
73
|
+
"bus": "1",
|
|
74
|
+
"address": "0x70",
|
|
75
|
+
"gpioChip": "0",
|
|
76
|
+
"intPin": "",
|
|
77
|
+
"z": "tab-pca9541-demo",
|
|
78
|
+
"x": 400,
|
|
79
|
+
"y": 140,
|
|
80
|
+
"wires": [
|
|
81
|
+
[
|
|
82
|
+
"dbg-pca9541"
|
|
83
|
+
]
|
|
84
|
+
]
|
|
85
|
+
},
|
|
86
|
+
{
|
|
87
|
+
"id": "dbg-pca9541",
|
|
88
|
+
"type": "debug",
|
|
89
|
+
"name": "control / connected",
|
|
90
|
+
"active": true,
|
|
91
|
+
"tosidebar": true,
|
|
92
|
+
"console": false,
|
|
93
|
+
"tostatus": false,
|
|
94
|
+
"complete": "payload",
|
|
95
|
+
"targetType": "msg",
|
|
96
|
+
"z": "tab-pca9541-demo",
|
|
97
|
+
"x": 640,
|
|
98
|
+
"y": 140,
|
|
99
|
+
"wires": []
|
|
100
|
+
}
|
|
101
|
+
]
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
<script type="text/javascript">
|
|
2
|
+
RED.nodes.registerType('periph-pca9541', {
|
|
3
|
+
category: 'Peripheral',
|
|
4
|
+
color: '#C084FC',
|
|
5
|
+
defaults: {
|
|
6
|
+
name: { value: '' },
|
|
7
|
+
bus: { value: '1', required: true, validate: RED.validators.number() },
|
|
8
|
+
address: { value: '0x70' },
|
|
9
|
+
gpioChip: { value: '0' },
|
|
10
|
+
intPin: { value: '' }
|
|
11
|
+
},
|
|
12
|
+
inputs: 1,
|
|
13
|
+
outputs: 1,
|
|
14
|
+
icon: 'font-awesome/fa-exchange',
|
|
15
|
+
label: function() { return this.name || 'PCA9541'; },
|
|
16
|
+
paletteLabel: 'PCA9541'
|
|
17
|
+
});
|
|
18
|
+
</script>
|
|
19
|
+
|
|
20
|
+
<script type="text/html" data-template-name="periph-pca9541">
|
|
21
|
+
<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">
|
|
24
|
+
</div>
|
|
25
|
+
<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"> (Linux /dev/i2c-N)
|
|
28
|
+
</div>
|
|
29
|
+
<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">
|
|
32
|
+
<option value="0x70">0x70 (A3..A0 = 0000)</option><option value="0x71">0x71</option>
|
|
33
|
+
<option value="0x72">0x72</option><option value="0x73">0x73</option>
|
|
34
|
+
<option value="0x74">0x74</option><option value="0x75">0x75</option>
|
|
35
|
+
<option value="0x76">0x76</option><option value="0x77">0x77</option>
|
|
36
|
+
</select>
|
|
37
|
+
</div>
|
|
38
|
+
<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"> (for the optional INT line)
|
|
41
|
+
</div>
|
|
42
|
+
<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">
|
|
45
|
+
this master's INT0 / INT1
|
|
46
|
+
</div>
|
|
47
|
+
</script>
|
|
48
|
+
|
|
49
|
+
<script type="text/html" data-help-name="periph-pca9541">
|
|
50
|
+
<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
|
+
|
|
52
|
+
<h3>Inputs</h3>
|
|
53
|
+
<dl class="message-properties">
|
|
54
|
+
<dt>payload.command <span class="property-type">string</span></dt>
|
|
55
|
+
<dd><code>"acquire"</code> takes the downstream bus (<code>payload.busInit</code> = true also requests bus initialization), <code>"release"</code> gives it to the other master, <code>"recover"</code> acquires with bus initialization (nine clocks, NACK, STOP on the downstream bus), <code>"disconnect"</code> isolates the downstream bus.</dd>
|
|
56
|
+
<dt>payload <span class="property-type">any</span></dt>
|
|
57
|
+
<dd>Any other message reads the status.</dd>
|
|
58
|
+
</dl>
|
|
59
|
+
|
|
60
|
+
<h3>Outputs</h3>
|
|
61
|
+
<dl class="message-properties">
|
|
62
|
+
<dt>payload.control <span class="property-type">boolean</span></dt>
|
|
63
|
+
<dd>True if this master's channel is connected to the downstream bus.</dd>
|
|
64
|
+
<dt>payload.connected <span class="property-type">boolean</span></dt>
|
|
65
|
+
<dd>True if the downstream bus is connected to some master.</dd>
|
|
66
|
+
<dt>payload.interrupt <span class="property-type">number</span></dt>
|
|
67
|
+
<dd>Only when an INT GPIO line is configured: the ISTAT byte, with <code>int_in</code>, <code>bus_init</code>, <code>bus_not_idle</code>, <code>bus_lost</code>, <code>test</code> and <code>other_test</code> flags.</dd>
|
|
68
|
+
</dl>
|
|
69
|
+
|
|
70
|
+
<h3>Configuration</h3>
|
|
71
|
+
<p>Set the Linux I²C bus number and the address (0x70–0x77; 0x78–0x7F fall into reserved I²C ranges). The optional <b>INT GPIO line</b> is this master's own INT pin (INT0 or INT1; sampled every 20 ms). The chip powers up with master 0 connected (/01) or with no channel connected (/03).</p>
|
|
72
|
+
</script>
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
module.exports = function(RED) {
|
|
4
|
+
const { I2CConnection } = require('periph/src/connection/i2c');
|
|
5
|
+
const { PCA9541Full } = require('periph/src/chips/other/pca9541');
|
|
6
|
+
|
|
7
|
+
function PCA9541Node(config) {
|
|
8
|
+
RED.nodes.createNode(this, config);
|
|
9
|
+
const node = this;
|
|
10
|
+
|
|
11
|
+
try {
|
|
12
|
+
const address = parseInt(config.address || '0x70', 16);
|
|
13
|
+
node.connection = new I2CConnection(parseInt(config.bus, 10), address);
|
|
14
|
+
node.driver = new PCA9541Full(node.connection);
|
|
15
|
+
node.ready = node.driver.init();
|
|
16
|
+
node.ready.then(() => node.status({ fill: 'green', shape: 'dot', text: 'I2C' }))
|
|
17
|
+
.catch((e) => node.error('PCA9541 init failed: ' + e.message));
|
|
18
|
+
} catch (e) {
|
|
19
|
+
node.error('PCA9541 init failed: ' + e.message);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Optional INT line of THIS master: emit { interrupt, bus_lost, ... } from the callback.
|
|
23
|
+
if (node.driver && config.intPin !== '' && config.intPin !== undefined) {
|
|
24
|
+
(async () => {
|
|
25
|
+
try {
|
|
26
|
+
const { Default } = require('opengpio');
|
|
27
|
+
const { PollingInputPin } = require('periph/src/connection/input_pin');
|
|
28
|
+
const line = Default.input({ chip: parseInt(config.gpioChip || 0, 10), line: parseInt(config.intPin, 10) });
|
|
29
|
+
const intPin = new PollingInputPin(() => (line.value ? 1 : 0), 20);
|
|
30
|
+
await node.ready;
|
|
31
|
+
await node.driver.onInterrupt((status) => {
|
|
32
|
+
node.send({ payload: {
|
|
33
|
+
interrupt: status,
|
|
34
|
+
int_in: Boolean(status & 0x01),
|
|
35
|
+
bus_init: Boolean(status & 0x02),
|
|
36
|
+
bus_not_idle: Boolean(status & 0x04),
|
|
37
|
+
bus_lost: Boolean(status & 0x08),
|
|
38
|
+
test: Boolean(status & 0x40),
|
|
39
|
+
other_test: Boolean(status & 0x80),
|
|
40
|
+
} });
|
|
41
|
+
}, { intPin });
|
|
42
|
+
} catch (e) {
|
|
43
|
+
node.error('PCA9541 INT setup failed: ' + e.message);
|
|
44
|
+
}
|
|
45
|
+
})();
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
node.on('input', async function(msg, send, done) {
|
|
49
|
+
if (!node.driver) { done(new Error('PCA9541 not initialised')); return; }
|
|
50
|
+
try {
|
|
51
|
+
await node.ready;
|
|
52
|
+
const p = msg.payload;
|
|
53
|
+
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();
|
|
59
|
+
msg.payload = { control: st.hasControl && st.connected, connected: st.connected };
|
|
60
|
+
send(msg);
|
|
61
|
+
done();
|
|
62
|
+
} catch (e) {
|
|
63
|
+
done(e);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
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
|
+
}
|
|
78
|
+
RED.nodes.registerType('periph-pca9541', PCA9541Node);
|
|
79
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "node-red-contrib-periph-other",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.5.0",
|
|
4
4
|
"description": "Node-RED nodes for miscellaneous peripheral chips",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"node-red",
|
|
@@ -12,7 +12,8 @@
|
|
|
12
12
|
],
|
|
13
13
|
"node-red": {
|
|
14
14
|
"nodes": {
|
|
15
|
-
"mpr121": "nodes/mpr121/mpr121.js"
|
|
15
|
+
"mpr121": "nodes/mpr121/mpr121.js",
|
|
16
|
+
"periph-pca9541": "nodes/pca9541/pca9541.js"
|
|
16
17
|
}
|
|
17
18
|
},
|
|
18
19
|
"dependencies": {
|