node-red-contrib-periph-io-expander 1.4.0 → 1.5.1

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,167 @@
1
+ [
2
+ {
3
+ "id": "tab_tca9554_demo",
4
+ "type": "tab",
5
+ "label": "TCA9554 Demo",
6
+ "info": "Output control and input polling on the TCA9554 8-bit I²C / SMBus IO expander.\n\nHardware:\n- P0–P3: four LEDs (anode through 220 Ω, cathode to GND; active-high)\n- P4–P7: push buttons to GND (the internal 100 kΩ pull-ups hold the inputs high)\n\nHow it works:\n1. An Inject node every 200 ms feeds a Function node that writes a rotating one-hot pattern to the LEDs (P0→P3), one pin at a time.\n2. A second Inject node reads the full 8-bit port every second; a Function node formats the hex value for a Debug node.\n3. A third Inject node reads P4 (a button input); a Debug node shows the 0/1 level.\n\nWith the INT line wired (set the INT GPIO line in the node's config panel) the node also emits { port: 0, changed, value } whenever a button on P4–P7 changes.\n\nTo observe: watch the LEDs chase and press the buttons; the port value changes in the debug sidebar."
7
+ },
8
+ {
9
+ "id": "inj_rotate",
10
+ "type": "inject",
11
+ "z": "tab_tca9554_demo",
12
+ "name": "Rotate 200ms",
13
+ "repeat": "0.2",
14
+ "payload": "",
15
+ "payloadType": "date",
16
+ "topic": "",
17
+ "once": false,
18
+ "x": 120,
19
+ "y": 60,
20
+ "wires": [
21
+ [
22
+ "fn_rotate"
23
+ ]
24
+ ]
25
+ },
26
+ {
27
+ "id": "fn_rotate",
28
+ "type": "function",
29
+ "z": "tab_tca9554_demo",
30
+ "name": "One-hot pattern",
31
+ "func": "const i = Math.floor(Date.now() / 200) % 4;\nconst prev = (i + 3) % 4;\nreturn [[{ payload: { pin: prev, value: 0 } }, { payload: { pin: i, value: 1 } }]];",
32
+ "outputs": 1,
33
+ "noerr": 0,
34
+ "x": 330,
35
+ "y": 60,
36
+ "wires": [
37
+ [
38
+ "mcp_write"
39
+ ]
40
+ ]
41
+ },
42
+ {
43
+ "id": "mcp_write",
44
+ "type": "periph-tca9554",
45
+ "z": "tab_tca9554_demo",
46
+ "name": "Write LEDs",
47
+ "bus": "1",
48
+ "address": "20",
49
+ "gpioChip": "0",
50
+ "intPin": "",
51
+ "x": 560,
52
+ "y": 60,
53
+ "wires": [
54
+ []
55
+ ]
56
+ },
57
+ {
58
+ "id": "inj_port",
59
+ "type": "inject",
60
+ "z": "tab_tca9554_demo",
61
+ "name": "Read port 1s",
62
+ "repeat": "1",
63
+ "payload": "{\"port\":0}",
64
+ "payloadType": "json",
65
+ "topic": "",
66
+ "once": false,
67
+ "x": 120,
68
+ "y": 160,
69
+ "wires": [
70
+ [
71
+ "mcp_port"
72
+ ]
73
+ ]
74
+ },
75
+ {
76
+ "id": "mcp_port",
77
+ "type": "periph-tca9554",
78
+ "z": "tab_tca9554_demo",
79
+ "name": "Read port",
80
+ "bus": "1",
81
+ "address": "20",
82
+ "gpioChip": "0",
83
+ "intPin": "",
84
+ "x": 330,
85
+ "y": 160,
86
+ "wires": [
87
+ [
88
+ "fn_hex"
89
+ ]
90
+ ]
91
+ },
92
+ {
93
+ "id": "fn_hex",
94
+ "type": "function",
95
+ "z": "tab_tca9554_demo",
96
+ "name": "Format hex",
97
+ "func": "msg.payload = 'PIO=0x' + msg.payload.value.toString(16).padStart(2, '0');\nreturn msg;",
98
+ "outputs": 1,
99
+ "noerr": 0,
100
+ "x": 540,
101
+ "y": 160,
102
+ "wires": [
103
+ [
104
+ "dbg_port"
105
+ ]
106
+ ]
107
+ },
108
+ {
109
+ "id": "dbg_port",
110
+ "type": "debug",
111
+ "z": "tab_tca9554_demo",
112
+ "name": "Port value",
113
+ "active": true,
114
+ "tosidebar": true,
115
+ "complete": "payload",
116
+ "x": 740,
117
+ "y": 160,
118
+ "wires": []
119
+ },
120
+ {
121
+ "id": "inj_btn",
122
+ "type": "inject",
123
+ "z": "tab_tca9554_demo",
124
+ "name": "Read P4 500ms",
125
+ "repeat": "0.5",
126
+ "payload": "{\"pin\":4}",
127
+ "payloadType": "json",
128
+ "topic": "",
129
+ "once": false,
130
+ "x": 120,
131
+ "y": 260,
132
+ "wires": [
133
+ [
134
+ "mcp_btn"
135
+ ]
136
+ ]
137
+ },
138
+ {
139
+ "id": "mcp_btn",
140
+ "type": "periph-tca9554",
141
+ "z": "tab_tca9554_demo",
142
+ "name": "Read P4",
143
+ "bus": "1",
144
+ "address": "20",
145
+ "gpioChip": "0",
146
+ "intPin": "",
147
+ "x": 330,
148
+ "y": 260,
149
+ "wires": [
150
+ [
151
+ "dbg_btn"
152
+ ]
153
+ ]
154
+ },
155
+ {
156
+ "id": "dbg_btn",
157
+ "type": "debug",
158
+ "z": "tab_tca9554_demo",
159
+ "name": "P4 level",
160
+ "active": true,
161
+ "tosidebar": true,
162
+ "complete": "payload",
163
+ "x": 540,
164
+ "y": 260,
165
+ "wires": []
166
+ }
167
+ ]
@@ -0,0 +1,77 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-pca6408a', {
3
+ category: 'Peripheral',
4
+ color: '#C084FC',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '20' },
9
+ gpioChip: { value: '0' },
10
+ intPin: { value: '' }
11
+ },
12
+ inputs: 1,
13
+ outputs: 1,
14
+ icon: 'font-awesome/fa-microchip',
15
+ label: function() {
16
+ return this.name || ('PCA6408A @ 0x' + this.address + ' (bus ' + this.bus + ')');
17
+ },
18
+ paletteLabel: 'PCA6408A'
19
+ });
20
+ </script>
21
+
22
+ <script type="text/html" data-template-name="periph-pca6408a">
23
+ <div class="form-row">
24
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
25
+ <input type="text" id="node-input-name" placeholder="PCA6408A">
26
+ </div>
27
+ <div class="form-row">
28
+ <label for="node-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
29
+ <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(/dev/i2c-N)
30
+ </div>
31
+ <div class="form-row">
32
+ <label for="node-input-address">Address (hex)</label>
33
+ <input type="text" id="node-input-address" placeholder="20" style="width:80px">
34
+ &nbsp;20 (ADDR = VSS) or 21 (ADDR = VDD(P))
35
+ </div>
36
+ <div class="form-row">
37
+ <label for="node-input-gpioChip">GPIO chip</label>
38
+ <input type="number" id="node-input-gpioChip" min="0" style="width:80px"> &nbsp;(for the optional INT line)
39
+ </div>
40
+ <div class="form-row">
41
+ <label for="node-input-intPin">INT GPIO line</label>
42
+ <input type="number" id="node-input-intPin" min="0" style="width:80px" placeholder="none">
43
+ </div>
44
+ </script>
45
+
46
+ <script type="text/html" data-help-name="periph-pca6408a">
47
+ <p>Controls and reads an 8-bit PCA6408A I/O expander (I²C / SMBus, NXP). The chip has no internal pull-ups: inputs need external pull-up resistors.</p>
48
+
49
+ <h3>Inputs</h3>
50
+ <dl class="message-properties">
51
+ <dt>payload <span class="property-type">object</span></dt>
52
+ <dd>
53
+ <code>{ pin: N, value: 0|1 }</code> — set output pin N (0–7); the pin is switched to output first.<br>
54
+ <code>{ pin: N }</code> — read pin N; reply sent on output.<br>
55
+ <code>{ port: 0 }</code> — read all 8 pins; reply sent on output.
56
+ </dd>
57
+ </dl>
58
+
59
+ <h3>Outputs</h3>
60
+ <dl class="message-properties">
61
+ <dt>payload.pin <span class="property-type">number</span></dt>
62
+ <dd>Pin index (0–7), present when reading a single pin.</dd>
63
+ <dt>payload.value <span class="property-type">number</span></dt>
64
+ <dd>Logic level (0 or 1) when reading a pin; 8-bit bitmask when reading the port.</dd>
65
+ <dt>payload.port <span class="property-type">number</span></dt>
66
+ <dd>Always 0 (the chip has a single port), present when reading the full port or on an interrupt message.</dd>
67
+ <dt>payload.changed <span class="property-type">number</span></dt>
68
+ <dd>Only when an INT GPIO line is configured: mask of the input pins whose level changed since the last INPUT read (sent together with <code>port</code> and <code>value</code>).</dd>
69
+ </dl>
70
+
71
+ <h3>Configuration</h3>
72
+ <p>I²C bus number (Linux <code>/dev/i2c-N</code>) and 7-bit address in hex (<code>20</code> or <code>21</code>).
73
+ The optional <b>INT GPIO line</b> (with GPIO chip number) emits a message whenever an input changes; the line is sampled every 5 ms.</p>
74
+
75
+ <h3>Notes</h3>
76
+ <p>All eight pins (P0–P7) are bidirectional. Pins start as inputs; writing a value switches the pin to output. The chip has two supply rails: VDD(I²C-bus) for the bus and VDD(P) for the port.</p>
77
+ </script>
@@ -0,0 +1,68 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { Pca6408aFull } = require('periph/src/chips/io_expander/pca6408a');
6
+
7
+ function Pca6408aNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ node.connection = new I2CConnection(parseInt(config.bus), parseInt(config.address, 16));
12
+ node.chip = new Pca6408aFull(node.connection);
13
+ node.chip.ready.catch((e) => node.error('PCA6408A init failed: ' + e.message));
14
+ node.status({ fill: 'green', shape: 'dot', text: 'I2C' });
15
+ } catch (e) {
16
+ node.error('PCA6408A init failed: ' + e.message);
17
+ }
18
+
19
+ // Optional INT line (otherwise nothing is emitted unless polled): emit { port, changed, value }.
20
+ if (node.chip && config.intPin !== '' && config.intPin !== undefined) {
21
+ (async () => {
22
+ try {
23
+ const { Default } = require('opengpio');
24
+ const { PollingInputPin } = require('periph/src/connection/input_pin');
25
+ const line = Default.input({ chip: parseInt(config.gpioChip || 0, 10), line: parseInt(config.intPin, 10) });
26
+ const intPin = new PollingInputPin(() => (line.value ? 1 : 0), 5);
27
+ await node.chip.ready;
28
+ await node.chip.onInterrupt(async (changed) => {
29
+ const value = await node.chip.readPort();
30
+ node.send({ payload: { port: 0, changed, value } });
31
+ }, { intPin });
32
+ } catch (e) {
33
+ node.error('PCA6408A INT setup failed: ' + e.message);
34
+ }
35
+ })();
36
+ }
37
+
38
+ node.on('input', async function(msg, send, done) {
39
+ if (!node.chip) { done(new Error('PCA6408A not initialised')); return; }
40
+ try {
41
+ const p = msg.payload;
42
+ if (p && typeof p.pin === 'number' && typeof p.value === 'number') {
43
+ const pin = node.chip.pin(p.pin);
44
+ if ((node.chip._direction >> p.pin) & 1) await pin.setDirection('out');
45
+ await pin.write(p.value ? 1 : 0);
46
+ done();
47
+ } else if (p && typeof p.pin === 'number') {
48
+ msg.payload = { pin: p.pin, value: ((await node.chip.readPort()) >> p.pin) & 1 };
49
+ send(msg);
50
+ done();
51
+ } else if (p && typeof p.port === 'number') {
52
+ msg.payload = { port: p.port, value: await node.chip.readPort() };
53
+ send(msg);
54
+ done();
55
+ } else {
56
+ done(new Error('msg.payload must be { pin, value }, { pin }, or { port }'));
57
+ }
58
+ } catch (e) { done(e); }
59
+ });
60
+
61
+ node.on('close', async function() {
62
+ if (node.chip) await node.chip.offInterrupt();
63
+ if (node.connection) await node.connection.close();
64
+ });
65
+ }
66
+
67
+ RED.nodes.registerType('periph-pca6408a', Pca6408aNode);
68
+ };
@@ -0,0 +1,78 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-pca6416a', {
3
+ category: 'Peripheral',
4
+ color: '#C084FC',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '20' },
9
+ gpioChip: { value: '0' },
10
+ intPin: { value: '' }
11
+ },
12
+ inputs: 1,
13
+ outputs: 1,
14
+ icon: 'font-awesome/fa-microchip',
15
+ label: function() {
16
+ return this.name || ('PCA6416A @ 0x' + this.address + ' (bus ' + this.bus + ')');
17
+ },
18
+ paletteLabel: 'PCA6416A'
19
+ });
20
+ </script>
21
+
22
+ <script type="text/html" data-template-name="periph-pca6416a">
23
+ <div class="form-row">
24
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
25
+ <input type="text" id="node-input-name" placeholder="PCA6416A">
26
+ </div>
27
+ <div class="form-row">
28
+ <label for="node-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
29
+ <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(/dev/i2c-N)
30
+ </div>
31
+ <div class="form-row">
32
+ <label for="node-input-address">Address (hex)</label>
33
+ <input type="text" id="node-input-address" placeholder="20" style="width:80px">
34
+ &nbsp;20 (ADDR = VSS) or 21 (ADDR = VDD(P))
35
+ </div>
36
+ <div class="form-row">
37
+ <label for="node-input-gpioChip">GPIO chip</label>
38
+ <input type="number" id="node-input-gpioChip" min="0" style="width:80px"> &nbsp;(for the optional INT line)
39
+ </div>
40
+ <div class="form-row">
41
+ <label for="node-input-intPin">INT GPIO line</label>
42
+ <input type="number" id="node-input-intPin" min="0" style="width:80px" placeholder="none">
43
+ </div>
44
+ </script>
45
+
46
+ <script type="text/html" data-help-name="periph-pca6416a">
47
+ <p>Controls and reads a 16-bit PCA6416A I/O expander (I²C / SMBus, NXP). The chip has no internal pull-ups: inputs need external pull-up resistors.</p>
48
+
49
+ <h3>Inputs</h3>
50
+ <dl class="message-properties">
51
+ <dt>payload <span class="property-type">object</span></dt>
52
+ <dd>
53
+ <code>{ pin: N, value: 0|1 }</code> — set output pin N (0–15); the pin is switched to output first.<br>
54
+ <code>{ pin: N }</code> — read pin N; reply sent on output.<br>
55
+ <code>{ port: N }</code> — read the 8 pins of port N (0 or 1); reply sent on output.<br>
56
+ <code>{}</code> (no <code>pin</code> / <code>port</code>) — read all 16 pins; reply sent on output.
57
+ </dd>
58
+ </dl>
59
+
60
+ <h3>Outputs</h3>
61
+ <dl class="message-properties">
62
+ <dt>payload.pin <span class="property-type">number</span></dt>
63
+ <dd>Pin index (0–15), present when reading a single pin (0–7 = P0_0–P0_7, 8–15 = P1_0–P1_7).</dd>
64
+ <dt>payload.value <span class="property-type">number</span></dt>
65
+ <dd>Logic level (0 or 1) when reading a pin; 8-bit bitmask when reading a port; 16-bit value when reading all pins.</dd>
66
+ <dt>payload.port <span class="property-type">number</span></dt>
67
+ <dd>Port number (0 or 1), present when reading a port.</dd>
68
+ <dt>payload.changed <span class="property-type">number</span></dt>
69
+ <dd>Only when an INT GPIO line is configured: 16-bit mask of the input pins whose level changed since the last INPUT read (sent together with the 16-bit <code>value</code>).</dd>
70
+ </dl>
71
+
72
+ <h3>Configuration</h3>
73
+ <p>I²C bus number (Linux <code>/dev/i2c-N</code>) and 7-bit address in hex (<code>20</code> or <code>21</code>).
74
+ The optional <b>INT GPIO line</b> (with GPIO chip number) emits a message whenever an input changes; the line is sampled every 5 ms.</p>
75
+
76
+ <h3>Notes</h3>
77
+ <p>All sixteen pins (P0_0–P0_7, P1_0–P1_7) are bidirectional. Pins start as inputs; writing a value switches the pin to output. The chip has two supply rails: VDD(I²C-bus) for the bus and VDD(P) for the port.</p>
78
+ </script>
@@ -0,0 +1,72 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { Pca6416aFull } = require('periph/src/chips/io_expander/pca6416a');
6
+
7
+ function Pca6416aNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ node.connection = new I2CConnection(parseInt(config.bus), parseInt(config.address, 16));
12
+ node.chip = new Pca6416aFull(node.connection);
13
+ node.chip.ready.catch((e) => node.error('PCA6416A init failed: ' + e.message));
14
+ node.status({ fill: 'green', shape: 'dot', text: 'I2C' });
15
+ } catch (e) {
16
+ node.error('PCA6416A init failed: ' + e.message);
17
+ }
18
+
19
+ // Optional INT line (otherwise nothing is emitted unless polled): emit { changed, value } (16-bit masks).
20
+ if (node.chip && config.intPin !== '' && config.intPin !== undefined) {
21
+ (async () => {
22
+ try {
23
+ const { Default } = require('opengpio');
24
+ const { PollingInputPin } = require('periph/src/connection/input_pin');
25
+ const line = Default.input({ chip: parseInt(config.gpioChip || 0, 10), line: parseInt(config.intPin, 10) });
26
+ const intPin = new PollingInputPin(() => (line.value ? 1 : 0), 5);
27
+ await node.chip.ready;
28
+ await node.chip.onInterrupt(async (changed) => {
29
+ const value = await node.chip.readAll();
30
+ node.send({ payload: { changed, value } });
31
+ }, { intPin });
32
+ } catch (e) {
33
+ node.error('PCA6416A INT setup failed: ' + e.message);
34
+ }
35
+ })();
36
+ }
37
+
38
+ node.on('input', async function(msg, send, done) {
39
+ if (!node.chip) { done(new Error('PCA6416A not initialised')); return; }
40
+ try {
41
+ const p = msg.payload;
42
+ if (p && typeof p.pin === 'number' && typeof p.value === 'number') {
43
+ const pin = node.chip.pin(p.pin);
44
+ if ((node.chip._direction >> p.pin) & 1) await pin.setDirection('out');
45
+ await pin.write(p.value ? 1 : 0);
46
+ done();
47
+ } else if (p && typeof p.pin === 'number') {
48
+ msg.payload = { pin: p.pin, value: ((await node.chip.readAll()) >> p.pin) & 1 };
49
+ send(msg);
50
+ done();
51
+ } else if (p && typeof p.port === 'number') {
52
+ msg.payload = { port: p.port, value: await node.chip.readPort(p.port) };
53
+ send(msg);
54
+ done();
55
+ } else if (p && typeof p === 'object') {
56
+ msg.payload = { value: await node.chip.readAll() };
57
+ send(msg);
58
+ done();
59
+ } else {
60
+ done(new Error('msg.payload must be { pin, value }, { pin }, { port }, or {} (read all pins)'));
61
+ }
62
+ } catch (e) { done(e); }
63
+ });
64
+
65
+ node.on('close', async function() {
66
+ if (node.chip) await node.chip.offInterrupt();
67
+ if (node.connection) await node.connection.close();
68
+ });
69
+ }
70
+
71
+ RED.nodes.registerType('periph-pca6416a', Pca6416aNode);
72
+ };
@@ -0,0 +1,65 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-pca9536', {
3
+ category: 'Peripheral',
4
+ color: '#C084FC',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ pollInterval: { value: '' }
9
+ },
10
+ inputs: 1,
11
+ outputs: 1,
12
+ icon: 'font-awesome/fa-microchip',
13
+ label: function() {
14
+ return this.name || ('PCA9536 (bus ' + this.bus + ')');
15
+ },
16
+ paletteLabel: 'PCA9536'
17
+ });
18
+ </script>
19
+
20
+ <script type="text/html" data-template-name="periph-pca9536">
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="PCA9536">
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"> &nbsp;(/dev/i2c-N)
28
+ </div>
29
+ <div class="form-row">
30
+ <label for="node-input-pollInterval">Poll interval</label>
31
+ <input type="number" id="node-input-pollInterval" min="0" style="width:80px" placeholder="off"> &nbsp;ms (the chip has no INT pin; address is fixed at 0x41)
32
+ </div>
33
+ </script>
34
+
35
+ <script type="text/html" data-help-name="periph-pca9536">
36
+ <p>Controls and reads a 4-bit PCA9536 I/O expander (I²C / SMBus, TI). The chip has the fixed address <code>0x41</code>, no interrupt pin, and a weak (about 100 kΩ) pull-up that is always on each pin, so an open input reads 1.</p>
37
+
38
+ <h3>Inputs</h3>
39
+ <dl class="message-properties">
40
+ <dt>payload <span class="property-type">object</span></dt>
41
+ <dd>
42
+ <code>{ pin: N, value: 0|1 }</code> — set output pin N (0–3); the pin is switched to output first.<br>
43
+ <code>{ pin: N }</code> — read pin N; reply sent on output.<br>
44
+ <code>{ port: 0 }</code> — read the 4-bit port; reply sent on output.
45
+ </dd>
46
+ </dl>
47
+
48
+ <h3>Outputs</h3>
49
+ <dl class="message-properties">
50
+ <dt>payload.pin <span class="property-type">number</span></dt>
51
+ <dd>Pin index (0–3), present when reading a single pin.</dd>
52
+ <dt>payload.value <span class="property-type">number</span></dt>
53
+ <dd>Logic level (0 or 1) when reading a pin; 4-bit bitmask when reading the port.</dd>
54
+ <dt>payload.port <span class="property-type">number</span></dt>
55
+ <dd>Always 0 (the chip has a single port), present when reading the full port or on a change message.</dd>
56
+ <dt>payload.changed <span class="property-type">number</span></dt>
57
+ <dd>Only when a poll interval is configured: mask of the input pins whose level changed since the last poll (sent together with <code>port</code> and <code>value</code>).</dd>
58
+ </dl>
59
+
60
+ <h3>Configuration</h3>
61
+ <p>I²C bus number (Linux <code>/dev/i2c-N</code>). The optional <b>poll interval</b> (ms) emits a message whenever an input changes.</p>
62
+
63
+ <h3>Notes</h3>
64
+ <p>All four pins (P0–P3) are bidirectional. Pins start as inputs; writing a value switches the pin to output.</p>
65
+ </script>
@@ -0,0 +1,62 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { Pca9536Full } = require('periph/src/chips/io_expander/pca9536');
6
+
7
+ function Pca9536Node(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ node.connection = new I2CConnection(parseInt(config.bus), 0x41);
12
+ node.chip = new Pca9536Full(node.connection);
13
+ node.chip.ready.catch((e) => node.error('PCA9536 init failed: ' + e.message));
14
+ node.status({ fill: 'green', shape: 'dot', text: 'I2C' });
15
+ } catch (e) {
16
+ node.error('PCA9536 init failed: ' + e.message);
17
+ }
18
+
19
+ // Optional change polling (the chip has no interrupt pin): emit { port, changed, value }.
20
+ const interval = parseFloat(config.pollInterval);
21
+ if (node.chip && interval > 0) {
22
+ node.chip.ready.then(() => {
23
+ node.chip.watchChanges(async (changed) => {
24
+ const value = await node.chip.readPort();
25
+ node.send({ payload: { port: 0, changed, value } });
26
+ }, interval / 1000);
27
+ }).catch((e) => node.error('PCA9536 polling setup failed: ' + e.message));
28
+ }
29
+
30
+ node.on('input', async function(msg, send, done) {
31
+ if (!node.chip) { done(new Error('PCA9536 not initialised')); return; }
32
+ try {
33
+ const p = msg.payload;
34
+ if (p && typeof p.pin === 'number' && (p.pin < 0 || p.pin > 3)) {
35
+ done(new Error('pin must be 0-3'));
36
+ } else if (p && typeof p.pin === 'number' && typeof p.value === 'number') {
37
+ const pin = node.chip.pin(p.pin);
38
+ if ((node.chip._direction >> p.pin) & 1) await pin.setDirection('out');
39
+ await pin.write(p.value ? 1 : 0);
40
+ done();
41
+ } else if (p && typeof p.pin === 'number') {
42
+ msg.payload = { pin: p.pin, value: ((await node.chip.readPort()) >> p.pin) & 1 };
43
+ send(msg);
44
+ done();
45
+ } else if (p && typeof p.port === 'number') {
46
+ msg.payload = { port: p.port, value: await node.chip.readPort() };
47
+ send(msg);
48
+ done();
49
+ } else {
50
+ done(new Error('msg.payload must be { pin, value }, { pin }, or { port }'));
51
+ }
52
+ } catch (e) { done(e); }
53
+ });
54
+
55
+ node.on('close', async function() {
56
+ if (node.chip) node.chip.unwatchChanges();
57
+ if (node.connection) await node.connection.close();
58
+ });
59
+ }
60
+
61
+ RED.nodes.registerType('periph-pca9536', Pca9536Node);
62
+ };