node-red-contrib-periph-io-expander 1.1.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 ADDED
@@ -0,0 +1,26 @@
1
+ # node-red-contrib-periph-io-expander
2
+
3
+ Node-RED nodes for IO expander chips — part of the [Periph](https://github.com/tuhde/Periph) library.
4
+
5
+ ## Install
6
+
7
+ Open Node-RED, go to **Manage Palette → Install** and search for `node-red-contrib-periph-io-expander`.
8
+
9
+ Or from the command line in your Node-RED user directory:
10
+
11
+ ```sh
12
+ npm install node-red-contrib-periph-io-expander
13
+ ```
14
+
15
+ ## Nodes
16
+
17
+ | Node | Description |
18
+ |------|-------------|
19
+ | `periph-mcp23017` | Read/write MCP23017 16-bit GPIO expander |
20
+ | `periph-pcf8574` | Read/write PCF8574 8-bit GPIO expander |
21
+ | `periph-pcf8575` | Read/write PCF8575 16-bit GPIO expander |
22
+
23
+ ## Links
24
+
25
+ - [GitHub](https://github.com/tuhde/Periph)
26
+ - [periph JS driver](https://www.npmjs.com/package/periph)
File without changes
@@ -0,0 +1,154 @@
1
+ [
2
+ {
3
+ "id": "tab_mcp23017_demo",
4
+ "type": "tab",
5
+ "label": "MCP23017 Demo",
6
+ "info": "Bidirectional port control using the MCP23017 16-bit IO expander.\n\nHardware:\n- GPA0–GPA6: seven LEDs (anode → VCC via 220Ω, cathode → pin; active-high)\n- GPB0–GPB6: seven push buttons (pin → GND when pressed; internal pull-ups enabled)\n\nHow it works:\n1. Three Inject nodes send periodic pattern writes to PORTA (GPA0–GPA6): a Knight Rider\n scanner (sequence 0→6→0), an alternating bit pattern, and a rotating bit pattern.\n2. A fourth Inject node reads both ports every 500 ms and sends the hex values to a\n Debug node showing PORTA and PORTB as a pair (e.g. \"A=0x01 B=0x80\").\n3. A Function node between the poll inject and the MCP23017 node splits the combined\n request into separate reads: { port: 0 } for PORTA and { port: 1 } for PORTB.\n4. A second MCP23017 node reads PORTB (GPB0–GPB6) and a third writes the poll result\n back as output.\n\nTo observe: watch the LEDs cycle through patterns; press buttons on GPB0–GPB6 and\nsee PORTB values change. GPA7 and GPB7 are output-only (always driven high)."
7
+ },
8
+ {
9
+ "id": "inject_poll",
10
+ "type": "inject",
11
+ "z": "tab_mcp23017_demo",
12
+ "name": "Poll 500ms",
13
+ "repeat": "0.5",
14
+ "payload": "{\"port\":0}",
15
+ "payloadType": "json",
16
+ "x": 120,
17
+ "y": 160,
18
+ "wires": [["mcp23017_read_a"]]
19
+ },
20
+ {
21
+ "id": "mcp23017_read_a",
22
+ "type": "periph-mcp23017",
23
+ "z": "tab_mcp23017_demo",
24
+ "name": "Read PORTA",
25
+ "bus": "1",
26
+ "address": "20",
27
+ "x": 340,
28
+ "y": 160,
29
+ "wires": [["fn_combine"]]
30
+ },
31
+ {
32
+ "id": "fn_combine",
33
+ "type": "function",
34
+ "z": "tab_mcp23017_demo",
35
+ "name": "Read PORTB too",
36
+ "func": "const porta = msg.payload.value;\nreturn [\n { payload: { port: 1 } }, // read PORTB\n { payload: { porta: porta } } // pass PORTA through\n];",
37
+ "outputs": 2,
38
+ "x": 540,
39
+ "y": 160,
40
+ "wires": [["mcp23017_read_b"], ["fn_display"]]
41
+ },
42
+ {
43
+ "id": "mcp23017_read_b",
44
+ "type": "periph-mcp23017",
45
+ "z": "tab_mcp23017_demo",
46
+ "name": "Read PORTB",
47
+ "bus": "1",
48
+ "address": "20",
49
+ "x": 760,
50
+ "y": 160,
51
+ "wires": [["fn_display"]]
52
+ },
53
+ {
54
+ "id": "fn_display",
55
+ "type": "function",
56
+ "z": "tab_mcp23017_demo",
57
+ "name": "Format hex pair",
58
+ "func": "if (msg.payload.porta !== undefined) {\n node.porta = msg.payload.porta;\n return null;\n}\nif (msg.payload.port !== undefined) {\n const portb = msg.payload.value;\n const porta = node.porta;\n msg.payload = 'A=0x' + (porta !== undefined ? porta.toString(16).padStart(2,'0') : '??') + ' B=0x' + portb.toString(16).padStart(2,'0');\n node.porta = undefined;\n return msg;\n}\nreturn null;",
59
+ "outputs": 1,
60
+ "x": 980,
61
+ "y": 160,
62
+ "wires": [["debug_ports"]]
63
+ },
64
+ {
65
+ "id": "debug_ports",
66
+ "type": "debug",
67
+ "z": "tab_mcp23017_demo",
68
+ "name": "Port values",
69
+ "active": true,
70
+ "x": 1200,
71
+ "y": 160,
72
+ "wires": []
73
+ },
74
+ {
75
+ "id": "inject_knight",
76
+ "type": "inject",
77
+ "z": "tab_mcp23017_demo",
78
+ "name": "Knight Rider",
79
+ "repeat": "0.1",
80
+ "payload": "{\"port\":0,\"values\":[1,2,4,8,16,32,64,32,16,8,4,2]}",
81
+ "payloadType": "json",
82
+ "x": 120,
83
+ "y": 60,
84
+ "wires": [["fn_knight"]]
85
+ },
86
+ {
87
+ "id": "fn_knight",
88
+ "type": "function",
89
+ "z": "tab_mcp23017_demo",
90
+ "name": "Knight pattern",
91
+ "func": "const values = msg.payload.values;\nconst i = Math.floor(Date.now() / 100) % values.length;\nconst mask = values[i] | (1 << 7); // GPA7 output-only high\nreturn { payload: { port: 0, value: mask } };",
92
+ "outputs": 1,
93
+ "x": 340,
94
+ "y": 60,
95
+ "wires": [["mcp23017_write"]]
96
+ },
97
+ {
98
+ "id": "inject_alt",
99
+ "type": "inject",
100
+ "z": "tab_mcp23017_demo",
101
+ "name": "Alternating",
102
+ "repeat": "0.4",
103
+ "payload": "{\"port\":0,\"values\":[0x55,0xAA]}",
104
+ "payloadType": "json",
105
+ "x": 120,
106
+ "y": 100,
107
+ "wires": [["fn_alt"]]
108
+ },
109
+ {
110
+ "id": "fn_alt",
111
+ "type": "function",
112
+ "z": "tab_mcp23017_demo",
113
+ "name": "Alt pattern",
114
+ "func": "const values = msg.payload.values;\nconst i = Math.floor(Date.now() / 400) % values.length;\nconst mask = values[i] | (1 << 7); // GPA7 output-only high\nreturn { payload: { port: 0, value: mask } };",
115
+ "outputs": 1,
116
+ "x": 340,
117
+ "y": 100,
118
+ "wires": [["mcp23017_write"]]
119
+ },
120
+ {
121
+ "id": "inject_rotate",
122
+ "type": "inject",
123
+ "z": "tab_mcp23017_demo",
124
+ "name": "Rotating bit",
125
+ "repeat": "0.15",
126
+ "payload": "{\"port\":0,\"values\":[1,2,4,8,16,32]}",
127
+ "payloadType": "json",
128
+ "x": 120,
129
+ "y": 140,
130
+ "wires": [["fn_rotate"]]
131
+ },
132
+ {
133
+ "id": "fn_rotate",
134
+ "type": "function",
135
+ "z": "tab_mcp23017_demo",
136
+ "name": "Rotate pattern",
137
+ "func": "const values = msg.payload.values;\nconst i = Math.floor(Date.now() / 150) % values.length;\nconst mask = values[i] | (1 << 7); // GPA7 output-only high\nreturn { payload: { port: 0, value: mask } };",
138
+ "outputs": 1,
139
+ "x": 340,
140
+ "y": 140,
141
+ "wires": [["mcp23017_write"]]
142
+ },
143
+ {
144
+ "id": "mcp23017_write",
145
+ "type": "periph-mcp23017",
146
+ "z": "tab_mcp23017_demo",
147
+ "name": "Write PORTA",
148
+ "bus": "1",
149
+ "address": "20",
150
+ "x": 540,
151
+ "y": 100,
152
+ "wires": []
153
+ }
154
+ ]
@@ -0,0 +1,68 @@
1
+ [
2
+ {
3
+ "id": "tab_pcf8574_demo",
4
+ "type": "tab",
5
+ "label": "PCF8574 Demo",
6
+ "info": "Button-controlled LED mirror using the PCF8574 IO expander.\n\nHardware:\n- P0–P3: LEDs (anode → VCC, cathode → pin; active-low)\n- P4–P7: push buttons (pin → GND when pressed)\n\nHow it works:\n1. An Inject node fires every 200 ms.\n2. The PCF8574 node receives { port: 0 } and returns the full 8-bit port byte.\n3. A Function node extracts bits 4–7 (buttons), inverts them, and sends { pin: N, value: V } for each LED (P0–P3).\n4. Four separate PCF8574 nodes (one per LED) write the individual pin values.\n5. A Debug node shows the raw port byte.\n\nTo observe: press any button and watch the corresponding LED toggle."
7
+ },
8
+ {
9
+ "id": "inject_poll",
10
+ "type": "inject",
11
+ "z": "tab_pcf8574_demo",
12
+ "name": "Poll 200ms",
13
+ "repeat": "0.2",
14
+ "payload": "{\"port\":0}",
15
+ "payloadType": "json",
16
+ "x": 120,
17
+ "y": 100,
18
+ "wires": [["pcf8574_read"]]
19
+ },
20
+ {
21
+ "id": "pcf8574_read",
22
+ "type": "periph-pcf8574",
23
+ "z": "tab_pcf8574_demo",
24
+ "name": "PCF8574 read",
25
+ "bus": "1",
26
+ "address": "20",
27
+ "x": 320,
28
+ "y": 100,
29
+ "wires": [["fn_mirror", "debug_port"]]
30
+ },
31
+ {
32
+ "id": "fn_mirror",
33
+ "type": "function",
34
+ "z": "tab_pcf8574_demo",
35
+ "name": "Mirror buttons→LEDs",
36
+ "func": "const port = msg.payload.value;\nconst buttons = (port >> 4) & 0x0F;\nconst leds = (~buttons) & 0x0F;\nconst msgs = [];\nfor (let i = 0; i < 4; i++) {\n msgs.push({ payload: { pin: i, value: (leds >> i) & 1 } });\n}\nreturn [msgs[0], msgs[1], msgs[2], msgs[3]];",
37
+ "outputs": 4,
38
+ "x": 540,
39
+ "y": 100,
40
+ "wires": [["pcf8574_p0"], ["pcf8574_p1"], ["pcf8574_p2"], ["pcf8574_p3"]]
41
+ },
42
+ {
43
+ "id": "pcf8574_p0", "type": "periph-pcf8574", "z": "tab_pcf8574_demo",
44
+ "name": "PCF8574 P0", "bus": "1", "address": "20", "x": 760, "y": 60, "wires": [[]]
45
+ },
46
+ {
47
+ "id": "pcf8574_p1", "type": "periph-pcf8574", "z": "tab_pcf8574_demo",
48
+ "name": "PCF8574 P1", "bus": "1", "address": "20", "x": 760, "y": 100, "wires": [[]]
49
+ },
50
+ {
51
+ "id": "pcf8574_p2", "type": "periph-pcf8574", "z": "tab_pcf8574_demo",
52
+ "name": "PCF8574 P2", "bus": "1", "address": "20", "x": 760, "y": 140, "wires": [[]]
53
+ },
54
+ {
55
+ "id": "pcf8574_p3", "type": "periph-pcf8574", "z": "tab_pcf8574_demo",
56
+ "name": "PCF8574 P3", "bus": "1", "address": "20", "x": 760, "y": 180, "wires": [[]]
57
+ },
58
+ {
59
+ "id": "debug_port",
60
+ "type": "debug",
61
+ "z": "tab_pcf8574_demo",
62
+ "name": "Port byte",
63
+ "active": true,
64
+ "x": 540,
65
+ "y": 160,
66
+ "wires": []
67
+ }
68
+ ]
@@ -0,0 +1,70 @@
1
+ {
2
+ "id": "pcf8575-demo",
3
+ "type": "tab",
4
+ "label": "PCF8575 Demo",
5
+ "info": "16-bit IO expander demo. Port 0 (P00–P07) drives active-low LEDs; Port 1 (P10–P17) reads push buttons with internal pull-up. Every 200ms the buttons are mirrored to the LEDs. Use inject nodes to read individual pins or ports manually.",
6
+ "nodes": [
7
+ {
8
+ "id": "pcf8575-1",
9
+ "type": "periph-pcf8575",
10
+ "z": "pcf8575-demo",
11
+ "bus": "1",
12
+ "address": "20",
13
+ "name": "PCF8575",
14
+ "wires": [[]]
15
+ },
16
+ {
17
+ "id": "inject-periodic",
18
+ "type": "inject",
19
+ "z": "pcf8575-demo",
20
+ "name": "Tick (200ms)",
21
+ "topic": "",
22
+ "payload": "",
23
+ "payloadType": "date",
24
+ "repeat": "0.2",
25
+ "crontab": "",
26
+ "once": true,
27
+ "onceDelay": "",
28
+ "wires": [["pcf8575-1"]]
29
+ },
30
+ {
31
+ "id": "read-port0",
32
+ "type": "inject",
33
+ "z": "pcf8575-demo",
34
+ "name": "Read Port 0",
35
+ "topic": "",
36
+ "payload": "{\"port\": 0}",
37
+ "payloadType": "json",
38
+ "repeat": "",
39
+ "crontab": "",
40
+ "once": false,
41
+ "onceDelay": "",
42
+ "wires": [["pcf8575-1"]]
43
+ },
44
+ {
45
+ "id": "read-port1",
46
+ "type": "inject",
47
+ "z": "pcf8575-demo",
48
+ "name": "Read Port 1",
49
+ "topic": "",
50
+ "payload": "{\"port\": 1}",
51
+ "payloadType": "json",
52
+ "repeat": "",
53
+ "crontab": "",
54
+ "once": false,
55
+ "onceDelay": "",
56
+ "wires": [["pcf8575-1"]]
57
+ },
58
+ {
59
+ "id": "debug-out",
60
+ "type": "debug",
61
+ "z": "pcf8575-demo",
62
+ "name": "",
63
+ "active": true,
64
+ "console": "true",
65
+ "complete": "false",
66
+ "statusVal": "",
67
+ "wires": []
68
+ }
69
+ ]
70
+ }
package/index.js ADDED
@@ -0,0 +1,11 @@
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
+ };
package/nodes/.gitkeep ADDED
File without changes
@@ -0,0 +1,65 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-mcp23017', {
3
+ category: 'Peripheral',
4
+ color: '#a6bbcf',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '20', required: true }
9
+ },
10
+ inputs: 1,
11
+ outputs: 1,
12
+ icon: 'font-awesome/fa-microchip',
13
+ label: function() { return this.name || ('MCP23017 @ 0x' + this.address + ' (bus ' + this.bus + ')'); },
14
+ paletteLabel: 'MCP23017'
15
+ });
16
+ </script>
17
+
18
+ <script type="text/html" data-template-name="periph-mcp23017">
19
+ <div class="form-row">
20
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
21
+ <input type="text" id="node-input-name" placeholder="MCP23017">
22
+ </div>
23
+ <div class="form-row">
24
+ <label for="node-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
25
+ <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
26
+ </div>
27
+ <div class="form-row">
28
+ <label for="node-input-address">Address (hex)</label>
29
+ <input type="text" id="node-input-address" placeholder="20" style="width:80px">
30
+ &nbsp;0x20–0x27
31
+ </div>
32
+ </script>
33
+
34
+ <script type="text/html" data-help-name="periph-mcp23017">
35
+ <p>Controls and reads a 16-bit MCP23017 I/O expander over I²C.</p>
36
+
37
+ <h3>Inputs</h3>
38
+ <dl class="message-properties">
39
+ <dt>payload <span class="property-type">object</span></dt>
40
+ <dd>
41
+ <code>{ pin: N, value: 0|1 }</code> — set output pin N (0=low, 1=high).<br>
42
+ <code>{ pin: N }</code> — read pin N; reply sent on output.<br>
43
+ <code>{ port: 0|1 }</code> — read all 8 pins of port 0 (PORTA) or port 1 (PORTB); reply sent on output.
44
+ </dd>
45
+ </dl>
46
+
47
+ <h3>Outputs</h3>
48
+ <dl class="message-properties">
49
+ <dt>payload.pin <span class="property-type">number</span></dt>
50
+ <dd>Pin index (0–15), present when reading a single pin.</dd>
51
+ <dt>payload.value <span class="property-type">number</span></dt>
52
+ <dd>Logic level (0 or 1) when reading a pin; 8-bit bitmask when reading a port.</dd>
53
+ <dt>payload.port <span class="property-type">number</span></dt>
54
+ <dd>Port index (0=PORTA, 1=PORTB), present when reading a full port.</dd>
55
+ </dl>
56
+
57
+ <h3>Configuration</h3>
58
+ <p>Set the I²C bus number (Linux <code>/dev/i2c-N</code>) and the 7-bit device address in hex
59
+ (default <code>20</code>, range <code>20</code>–<code>27</code>).</p>
60
+
61
+ <h3>Notes</h3>
62
+ <p>PORTA = pins 0–7 (GPA0–GPA7), PORTB = pins 8–15 (GPB0–GPB7). GPA7 and GPB7 are
63
+ output-only on the hardware. Internal 100 kΩ pull-ups can be enabled on input pins
64
+ via the driver API; the node exposes per-pin set and read operations.</p>
65
+ </script>
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CTransport } = require('periph/src/transport/i2c');
5
+ const { Mcp23017Full } = require('periph/src/chips/io_expander/mcp23017');
6
+
7
+ function Mcp23017Node(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const transport = new I2CTransport(
12
+ parseInt(config.bus),
13
+ parseInt(config.address, 16)
14
+ );
15
+ node.chip = new Mcp23017Full(transport);
16
+ node.transport = transport;
17
+ } catch (e) {
18
+ node.error('MCP23017 init failed: ' + e.message);
19
+ }
20
+
21
+ node.on('input', function(msg, send, done) {
22
+ if (!node.chip) { done(new Error('MCP23017 not initialised')); return; }
23
+ try {
24
+ const p = msg.payload;
25
+ if (p && typeof p.pin === 'number' && typeof p.value === 'number') {
26
+ node.chip._setPin(p.pin, p.value ? 1 : 0);
27
+ done();
28
+ } else if (p && typeof p.pin === 'number') {
29
+ const port = Math.floor(p.pin / 8);
30
+ const bit = p.pin % 8;
31
+ msg.payload = { pin: p.pin, value: (node.chip.readPort(port) >> bit) & 1 };
32
+ send(msg);
33
+ done();
34
+ } else if (p && typeof p.port === 'number') {
35
+ msg.payload = { port: p.port, value: node.chip.readPort(p.port) };
36
+ send(msg);
37
+ done();
38
+ } else {
39
+ done(new Error('msg.payload must be { pin, value }, { pin }, or { port }'));
40
+ }
41
+ } catch (e) { done(e); }
42
+ });
43
+
44
+ node.on('close', function() {
45
+ if (node.transport) node.transport.close && node.transport.close();
46
+ });
47
+ }
48
+
49
+ RED.nodes.registerType('periph-mcp23017', Mcp23017Node);
50
+ };
@@ -0,0 +1,66 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-pcf8574', {
3
+ category: 'Peripheral',
4
+ color: '#a6bbcf',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '20', required: true }
9
+ },
10
+ inputs: 1,
11
+ outputs: 1,
12
+ icon: 'font-awesome/fa-microchip',
13
+ label: function() { return this.name || ('PCF8574 @ 0x' + this.address + ' (bus ' + this.bus + ')'); },
14
+ paletteLabel: 'PCF8574'
15
+ });
16
+ </script>
17
+
18
+ <script type="text/html" data-template-name="periph-pcf8574">
19
+ <div class="form-row">
20
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
21
+ <input type="text" id="node-input-name" placeholder="PCF8574">
22
+ </div>
23
+ <div class="form-row">
24
+ <label for="node-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
25
+ <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
26
+ </div>
27
+ <div class="form-row">
28
+ <label for="node-input-address">Address (hex)</label>
29
+ <input type="text" id="node-input-address" placeholder="20" style="width:80px">
30
+ &nbsp;PCF8574: 20–27 &nbsp;PCF8574A: 38–3F
31
+ </div>
32
+ </script>
33
+
34
+ <script type="text/html" data-help-name="periph-pcf8574">
35
+ <p>Controls and reads an 8-bit PCF8574 I/O expander over I²C.</p>
36
+
37
+ <h3>Inputs</h3>
38
+ <dl class="message-properties">
39
+ <dt>payload <span class="property-type">object</span></dt>
40
+ <dd>
41
+ <code>{ pin: N, value: 0|1 }</code> — set output pin N (0 drives low; 1 releases to quasi-high/input).<br>
42
+ <code>{ pin: N }</code> — read pin N; reply sent on output.<br>
43
+ <code>{ port: 0 }</code> — read all 8 pins; reply sent on output.
44
+ </dd>
45
+ </dl>
46
+
47
+ <h3>Outputs</h3>
48
+ <dl class="message-properties">
49
+ <dt>payload.pin <span class="property-type">number</span></dt>
50
+ <dd>Pin index (0–7), present when reading a single pin.</dd>
51
+ <dt>payload.value <span class="property-type">number</span></dt>
52
+ <dd>Logic level (0 or 1) when reading a pin; 8-bit bitmask when reading a port.</dd>
53
+ <dt>payload.port <span class="property-type">number</span></dt>
54
+ <dd>Port index (always 0 for PCF8574), present when reading a full port.</dd>
55
+ </dl>
56
+
57
+ <h3>Configuration</h3>
58
+ <p>Set the I²C bus number (Linux <code>/dev/i2c-N</code>) and the 7-bit device address in hex
59
+ (PCF8574: <code>20</code>–<code>27</code>; PCF8574A: <code>38</code>–<code>3F</code>; default <code>20</code>).</p>
60
+
61
+ <h3>Notes</h3>
62
+ <p>The PCF8574 has no push-pull high drive. Writing 1 to a pin enables the internal
63
+ ~100 µA pull-up; writing 0 sinks up to 25 mA. LEDs must be wired active-low
64
+ (cathode to pin, anode to VCC). The PCF8574A address range (38–3F) overlaps
65
+ with many OLED display modules — check for conflicts.</p>
66
+ </script>
@@ -0,0 +1,51 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CTransport } = require('periph/src/transport/i2c');
5
+ const { Pcf8574Full } = require('periph/src/chips/io_expander/pcf8574');
6
+
7
+ function Pcf8574Node(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const transport = new I2CTransport(
12
+ parseInt(config.bus),
13
+ parseInt(config.address, 16)
14
+ );
15
+ node.chip = new Pcf8574Full(transport);
16
+ node.transport = transport;
17
+ } catch (e) {
18
+ node.error('PCF8574 init failed: ' + e.message);
19
+ }
20
+
21
+ node.on('input', function(msg, send, done) {
22
+ if (!node.chip) { done(new Error('PCF8574 not initialised')); return; }
23
+ try {
24
+ const p = msg.payload;
25
+ if (p && typeof p.pin === 'number' && typeof p.value === 'number') {
26
+ // Set output pin
27
+ node.chip._setPin(p.pin, p.value ? 1 : 0);
28
+ done();
29
+ } else if (p && typeof p.pin === 'number') {
30
+ // Read input pin
31
+ msg.payload = { pin: p.pin, value: node.chip.readPort() >> p.pin & 1 };
32
+ send(msg);
33
+ done();
34
+ } else if (p && typeof p.port === 'number') {
35
+ // Read full port
36
+ msg.payload = { port: p.port, value: node.chip.readPort(p.port) };
37
+ send(msg);
38
+ done();
39
+ } else {
40
+ done(new Error('msg.payload must be { pin, value }, { pin }, or { port }'));
41
+ }
42
+ } catch (e) { done(e); }
43
+ });
44
+
45
+ node.on('close', function() {
46
+ if (node.transport) node.transport.close && node.transport.close();
47
+ });
48
+ }
49
+
50
+ RED.nodes.registerType('periph-pcf8574', Pcf8574Node);
51
+ };
@@ -0,0 +1,66 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-pcf8575', {
3
+ category: 'Peripheral',
4
+ color: '#a6bbcf',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '20', required: true }
9
+ },
10
+ inputs: 1,
11
+ outputs: 1,
12
+ icon: 'font-awesome/fa-microchip',
13
+ label: function() { return this.name || ('PCF8575 @ 0x' + this.address + ' (bus ' + this.bus + ')'); },
14
+ paletteLabel: 'PCF8575'
15
+ });
16
+ </script>
17
+
18
+ <script type="text/html" data-template-name="periph-pcf8575">
19
+ <div class="form-row">
20
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
21
+ <input type="text" id="node-input-name" placeholder="PCF8575">
22
+ </div>
23
+ <div class="form-row">
24
+ <label for="node-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
25
+ <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
26
+ </div>
27
+ <div class="form-row">
28
+ <label for="node-input-address">Address (hex)</label>
29
+ <input type="text" id="node-input-address" placeholder="20" style="width:80px">
30
+ &nbsp;PCF8575: 20–27
31
+ </div>
32
+ </script>
33
+
34
+ <script type="text/html" data-help-name="periph-pcf8575">
35
+ <p>Controls and reads a 16-bit PCF8575 I/O expander over I²C.</p>
36
+
37
+ <h3>Inputs</h3>
38
+ <dl class="message-properties">
39
+ <dt>payload <span class="property-type">object</span></dt>
40
+ <dd>
41
+ <code>{ pin: N, value: 0|1 }</code> — set output pin N (0 drives low; 1 releases to quasi-high/input).<br>
42
+ <code>{ pin: N }</code> — read pin N (0–15); reply sent on output.<br>
43
+ <code>{ port: 0|1 }</code> — read all 8 pins of port 0 or 1; reply sent on output.
44
+ </dd>
45
+ </dl>
46
+
47
+ <h3>Outputs</h3>
48
+ <dl class="message-properties">
49
+ <dt>payload.pin <span class="property-type">number</span></dt>
50
+ <dd>Pin index (0–15), present when reading a single pin.</dd>
51
+ <dt>payload.value <span class="property-type">number</span></dt>
52
+ <dd>Logic level (0 or 1) when reading a pin; 8-bit bitmask when reading a port.</dd>
53
+ <dt>payload.port <span class="property-type">number</span></dt>
54
+ <dd>Port index (0 or 1), present when reading a full port.</dd>
55
+ </dl>
56
+
57
+ <h3>Configuration</h3>
58
+ <p>Set the I²C bus number (Linux <code>/dev/i2c-N</code>) and the 7-bit device address in hex
59
+ (PCF8575: <code>20</code>–<code>27</code>; default <code>20</code>).</p>
60
+
61
+ <h3>Notes</h3>
62
+ <p>The PCF8575 has no push-pull high drive. Writing 1 to a pin enables the internal
63
+ ~100 µA pull-up; writing 0 sinks up to 25 mA. LEDs must be wired active-low
64
+ (cathode to pin, anode to VCC). Port 0 is P00–P07; Port 1 is P10–P17.
65
+ The 2-byte I²C protocol always transfers both ports.</p>
66
+ </script>
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CTransport } = require('periph/src/transport/i2c');
5
+ const { Pcf8575Full } = require('periph/src/chips/io_expander/pcf8575');
6
+
7
+ function Pcf8575Node(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const transport = new I2CTransport(
12
+ parseInt(config.bus),
13
+ parseInt(config.address, 16)
14
+ );
15
+ node.chip = new Pcf8575Full(transport);
16
+ node.transport = transport;
17
+ } catch (e) {
18
+ node.error('PCF8575 init failed: ' + e.message);
19
+ }
20
+
21
+ node.on('input', function(msg, send, done) {
22
+ if (!node.chip) { done(new Error('PCF8575 not initialised')); return; }
23
+ try {
24
+ const p = msg.payload;
25
+ if (p && typeof p.pin === 'number' && typeof p.value === 'number') {
26
+ node.chip._setPin(p.pin, p.value ? 1 : 0);
27
+ done();
28
+ } else if (p && typeof p.pin === 'number') {
29
+ const port = Math.floor(p.pin / 8);
30
+ const bit = p.pin % 8;
31
+ msg.payload = { pin: p.pin, value: (node.chip.readPort(port) >> bit) & 1 };
32
+ send(msg);
33
+ done();
34
+ } else if (p && typeof p.port === 'number') {
35
+ msg.payload = { port: p.port, value: node.chip.readPort(p.port) };
36
+ send(msg);
37
+ done();
38
+ } else {
39
+ done(new Error('msg.payload must be { pin, value }, { pin }, or { port }'));
40
+ }
41
+ } catch (e) { done(e); }
42
+ });
43
+
44
+ node.on('close', function() {
45
+ if (node.transport) node.transport.close && node.transport.close();
46
+ });
47
+ }
48
+
49
+ RED.nodes.registerType('periph-pcf8575', Pcf8575Node);
50
+ };
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "node-red-contrib-periph-io-expander",
3
+ "version": "1.1.0",
4
+ "description": "Node-RED nodes for IO expander chips",
5
+ "keywords": [
6
+ "node-red",
7
+ "io-expander",
8
+ "i2c",
9
+ "spi",
10
+ "peripheral"
11
+ ],
12
+ "main": "index.js",
13
+ "node-red": {
14
+ "nodes": {
15
+ "periph-pcf8574": "nodes/pcf8574/pcf8574.js",
16
+ "periph-mcp23017": "nodes/mcp23017/mcp23017.js",
17
+ "periph-pcf8575": "nodes/pcf8575/pcf8575.js"
18
+ }
19
+ },
20
+ "dependencies": {
21
+ "periph": "*"
22
+ },
23
+ "license": "MIT"
24
+ }