node-red-contrib-periph-io-expander 1.3.2 → 1.4.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 CHANGED
@@ -16,6 +16,7 @@ npm install node-red-contrib-periph-io-expander
16
16
 
17
17
  | Node | Kind | Description |
18
18
  |------|------|-------------|
19
+ | `periph-mcp23008` | input | Controls and reads an 8-bit MCP23008 (I²C) or MCP23S08 (SPI) I/O expander. Both chips share one register map and one node. |
19
20
  | `periph-mcp23017` | input | Controls and reads a 16-bit MCP23017 I/O expander over I²C. |
20
21
  | `periph-pcf8574` | input | Controls and reads an 8-bit PCF8574 I/O expander over I²C. |
21
22
  | `periph-pcf8575` | input | Controls and reads a 16-bit PCF8575 I/O expander over I²C. |
@@ -0,0 +1,179 @@
1
+ [
2
+ {
3
+ "id": "tab_mcp23008_demo",
4
+ "type": "tab",
5
+ "label": "MCP23008 Demo",
6
+ "info": "Output control and input polling on the MCP23008 (I²C) / MCP23S08 (SPI) 8-bit IO expander.\n\nHardware:\n- GP0–GP3: four LEDs (anode through 220 Ω, cathode to GND; active-high)\n- GP4–GP7: push buttons to GND (internal pull-ups enabled in the driver's Full configuration)\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 (GP0→GP3), 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 GP4 (a button input); a Debug node shows the 0/1 level.\n\nSwitch the node's transport between I²C and SPI in its config panel; the flow is otherwise identical.\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_mcp23008_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_mcp23008_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-mcp23008",
45
+ "z": "tab_mcp23008_demo",
46
+ "name": "Write LEDs",
47
+ "transport": "i2c",
48
+ "bus": "1",
49
+ "address": "20",
50
+ "spiDevice": "0",
51
+ "spiAddr": "0",
52
+ "gpioChip": "0",
53
+ "intPin": "",
54
+ "intMask": "F0",
55
+ "x": 560,
56
+ "y": 60,
57
+ "wires": [
58
+ []
59
+ ]
60
+ },
61
+ {
62
+ "id": "inj_port",
63
+ "type": "inject",
64
+ "z": "tab_mcp23008_demo",
65
+ "name": "Read port 1s",
66
+ "repeat": "1",
67
+ "payload": "{\"port\":0}",
68
+ "payloadType": "json",
69
+ "topic": "",
70
+ "once": false,
71
+ "x": 120,
72
+ "y": 160,
73
+ "wires": [
74
+ [
75
+ "mcp_port"
76
+ ]
77
+ ]
78
+ },
79
+ {
80
+ "id": "mcp_port",
81
+ "type": "periph-mcp23008",
82
+ "z": "tab_mcp23008_demo",
83
+ "name": "Read port",
84
+ "transport": "i2c",
85
+ "bus": "1",
86
+ "address": "20",
87
+ "spiDevice": "0",
88
+ "spiAddr": "0",
89
+ "gpioChip": "0",
90
+ "intPin": "",
91
+ "intMask": "F0",
92
+ "x": 330,
93
+ "y": 160,
94
+ "wires": [
95
+ [
96
+ "fn_hex"
97
+ ]
98
+ ]
99
+ },
100
+ {
101
+ "id": "fn_hex",
102
+ "type": "function",
103
+ "z": "tab_mcp23008_demo",
104
+ "name": "Format hex",
105
+ "func": "msg.payload = 'GPIO=0x' + msg.payload.value.toString(16).padStart(2, '0');\nreturn msg;",
106
+ "outputs": 1,
107
+ "noerr": 0,
108
+ "x": 540,
109
+ "y": 160,
110
+ "wires": [
111
+ [
112
+ "dbg_port"
113
+ ]
114
+ ]
115
+ },
116
+ {
117
+ "id": "dbg_port",
118
+ "type": "debug",
119
+ "z": "tab_mcp23008_demo",
120
+ "name": "Port value",
121
+ "active": true,
122
+ "tosidebar": true,
123
+ "complete": "payload",
124
+ "x": 740,
125
+ "y": 160,
126
+ "wires": []
127
+ },
128
+ {
129
+ "id": "inj_btn",
130
+ "type": "inject",
131
+ "z": "tab_mcp23008_demo",
132
+ "name": "Read GP4 500ms",
133
+ "repeat": "0.5",
134
+ "payload": "{\"pin\":4}",
135
+ "payloadType": "json",
136
+ "topic": "",
137
+ "once": false,
138
+ "x": 120,
139
+ "y": 260,
140
+ "wires": [
141
+ [
142
+ "mcp_btn"
143
+ ]
144
+ ]
145
+ },
146
+ {
147
+ "id": "mcp_btn",
148
+ "type": "periph-mcp23008",
149
+ "z": "tab_mcp23008_demo",
150
+ "name": "Read GP4",
151
+ "transport": "i2c",
152
+ "bus": "1",
153
+ "address": "20",
154
+ "spiDevice": "0",
155
+ "spiAddr": "0",
156
+ "gpioChip": "0",
157
+ "intPin": "",
158
+ "intMask": "F0",
159
+ "x": 330,
160
+ "y": 260,
161
+ "wires": [
162
+ [
163
+ "dbg_btn"
164
+ ]
165
+ ]
166
+ },
167
+ {
168
+ "id": "dbg_btn",
169
+ "type": "debug",
170
+ "z": "tab_mcp23008_demo",
171
+ "name": "GP4 level",
172
+ "active": true,
173
+ "tosidebar": true,
174
+ "complete": "payload",
175
+ "x": 540,
176
+ "y": 260,
177
+ "wires": []
178
+ }
179
+ ]
@@ -0,0 +1,112 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-mcp23008', {
3
+ category: 'Peripheral',
4
+ color: '#C084FC',
5
+ defaults: {
6
+ name: { value: '' },
7
+ transport: { value: 'i2c' },
8
+ bus: { value: '1', required: true, validate: RED.validators.number() },
9
+ address: { value: '20' },
10
+ spiDevice: { value: '0' },
11
+ spiAddr: { value: '0' },
12
+ gpioChip: { value: '0' },
13
+ intPin: { value: '' },
14
+ intMask: { value: 'F0' }
15
+ },
16
+ inputs: 1,
17
+ outputs: 1,
18
+ icon: 'font-awesome/fa-microchip',
19
+ label: function() {
20
+ return this.name || (this.transport === 'spi'
21
+ ? ('MCP23S08 @ ' + this.spiAddr + ' (spi ' + this.bus + '.' + this.spiDevice + ')')
22
+ : ('MCP23008 @ 0x' + this.address + ' (bus ' + this.bus + ')'));
23
+ },
24
+ paletteLabel: 'MCP23008',
25
+ oneditprepare: function() {
26
+ function sync() {
27
+ const spi = $('#node-input-transport').val() === 'spi';
28
+ $('.mcp-i2c').toggle(!spi);
29
+ $('.mcp-spi').toggle(spi);
30
+ }
31
+ $('#node-input-transport').on('change', sync);
32
+ sync();
33
+ }
34
+ });
35
+ </script>
36
+
37
+ <script type="text/html" data-template-name="periph-mcp23008">
38
+ <div class="form-row">
39
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
40
+ <input type="text" id="node-input-name" placeholder="MCP23008">
41
+ </div>
42
+ <div class="form-row">
43
+ <label for="node-input-transport">Transport</label>
44
+ <select id="node-input-transport">
45
+ <option value="i2c">I²C (MCP23008)</option>
46
+ <option value="spi">SPI (MCP23S08)</option>
47
+ </select>
48
+ </div>
49
+ <div class="form-row">
50
+ <label for="node-input-bus"><i class="fa fa-microchip"></i> Bus</label>
51
+ <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(<span class="mcp-i2c">/dev/i2c-N</span><span class="mcp-spi">/dev/spidevN.M</span>)
52
+ </div>
53
+ <div class="form-row mcp-i2c">
54
+ <label for="node-input-address">Address (hex)</label>
55
+ <input type="text" id="node-input-address" placeholder="20" style="width:80px">
56
+ &nbsp;0x20–0x27
57
+ </div>
58
+ <div class="form-row mcp-spi">
59
+ <label for="node-input-spiDevice">Chip select</label>
60
+ <input type="number" id="node-input-spiDevice" min="0" style="width:80px"> &nbsp;(spidevN.<i>M</i>)
61
+ </div>
62
+ <div class="form-row mcp-spi">
63
+ <label for="node-input-spiAddr">Device address</label>
64
+ <input type="number" id="node-input-spiAddr" min="0" max="3" style="width:80px"> &nbsp;A1:A0 pins, 0–3
65
+ </div>
66
+ <div class="form-row">
67
+ <label for="node-input-gpioChip">GPIO chip</label>
68
+ <input type="number" id="node-input-gpioChip" min="0" style="width:80px"> &nbsp;(for the optional INT line)
69
+ </div>
70
+ <div class="form-row">
71
+ <label for="node-input-intPin">INT GPIO line</label>
72
+ <input type="number" id="node-input-intPin" min="0" style="width:80px" placeholder="none">
73
+ </div>
74
+ <div class="form-row">
75
+ <label for="node-input-intMask">INT pin mask (hex)</label>
76
+ <input type="text" id="node-input-intMask" placeholder="F0" style="width:80px"> &nbsp;pins armed for interrupt-on-change (pull-ups enabled)
77
+ </div>
78
+ </script>
79
+
80
+ <script type="text/html" data-help-name="periph-mcp23008">
81
+ <p>Controls and reads an 8-bit MCP23008 (I²C) or MCP23S08 (SPI) I/O expander. Both chips share one register map and one node.</p>
82
+
83
+ <h3>Inputs</h3>
84
+ <dl class="message-properties">
85
+ <dt>payload <span class="property-type">object</span></dt>
86
+ <dd>
87
+ <code>{ pin: N, value: 0|1 }</code> — set output pin N (0–7); the pin is switched to output first.<br>
88
+ <code>{ pin: N }</code> — read pin N; reply sent on output.<br>
89
+ <code>{ port: 0 }</code> — read all 8 pins; reply sent on output.
90
+ </dd>
91
+ </dl>
92
+
93
+ <h3>Outputs</h3>
94
+ <dl class="message-properties">
95
+ <dt>payload.pin <span class="property-type">number</span></dt>
96
+ <dd>Pin index (0–7), present when reading a single pin.</dd>
97
+ <dt>payload.value <span class="property-type">number</span></dt>
98
+ <dd>Logic level (0 or 1) when reading a pin; 8-bit bitmask when reading the port.</dd>
99
+ <dt>payload.port <span class="property-type">number</span></dt>
100
+ <dd>Always 0 (the chip has a single port), present when reading the full port.</dd>
101
+ <dt>payload.interrupt / payload.capture <span class="property-type">number</span></dt>
102
+ <dd>Only when an INT GPIO line is configured: the INTF mask of the pins that changed and the INTCAP port snapshot.</dd>
103
+ </dl>
104
+
105
+ <h3>Configuration</h3>
106
+ <p><b>I²C:</b> bus number (Linux <code>/dev/i2c-N</code>) and 7-bit address in hex (default <code>20</code>, range <code>20</code>–<code>27</code>).
107
+ <b>SPI:</b> bus number, chip-select (<code>/dev/spidevN.M</code>) and the hardware device address 0–3 (A1:A0 pins; up to four devices share one chip select).
108
+ The optional <b>INT GPIO line</b> (with GPIO chip number) enables interrupt-on-change for the pins in the mask, with pull-ups; the line is sampled every 5 ms.</p>
109
+
110
+ <h3>Notes</h3>
111
+ <p>All eight pins (GP0–GP7) are bidirectional. Pins start as inputs; writing a value switches the pin to output.</p>
112
+ </script>
@@ -0,0 +1,77 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { SPIConnection } = require('periph/src/connection/spi');
6
+ const { Mcp23008Full, Mcp23S08Full } = require('periph/src/chips/io_expander/mcp23008');
7
+
8
+ function Mcp23008Node(config) {
9
+ RED.nodes.createNode(this, config);
10
+ const node = this;
11
+ try {
12
+ if (config.transport === 'spi') {
13
+ node.connection = new SPIConnection(parseInt(config.bus), parseInt(config.spiDevice || 0),
14
+ { maxSpeedHz: 5000000 });
15
+ node.chip = new Mcp23S08Full(node.connection, parseInt(config.spiAddr || 0));
16
+ } else {
17
+ node.connection = new I2CConnection(parseInt(config.bus), parseInt(config.address, 16));
18
+ node.chip = new Mcp23008Full(node.connection);
19
+ }
20
+ node.chip.ready.catch((e) => node.error('MCP23008 init failed: ' + e.message));
21
+ node.status({ fill: 'green', shape: 'dot', text: config.transport === 'spi' ? 'SPI' : 'I2C' });
22
+ } catch (e) {
23
+ node.error('MCP23008 init failed: ' + e.message);
24
+ }
25
+
26
+ // Optional INT line: arm the pins in the mask and emit { interrupt, capture } messages.
27
+ if (node.chip && config.intPin !== '' && config.intPin !== undefined) {
28
+ (async () => {
29
+ try {
30
+ const { Default } = require('opengpio');
31
+ const { PollingInputPin } = require('periph/src/connection/input_pin');
32
+ const line = Default.input({ chip: parseInt(config.gpioChip || 0, 10), line: parseInt(config.intPin, 10) });
33
+ const intPin = new PollingInputPin(() => (line.value ? 1 : 0), 5);
34
+ await node.chip.ready;
35
+ await node.chip.configurePullup(parseInt(config.intMask || 'F0', 16));
36
+ await node.chip.enablePinInterrupt(parseInt(config.intMask || 'F0', 16));
37
+ await node.chip.onInterrupt(async (status) => {
38
+ const capture = await node.chip.readCapture();
39
+ node.send({ payload: { interrupt: status, capture } });
40
+ }, { intPin });
41
+ } catch (e) {
42
+ node.error('MCP23008 INT setup failed: ' + e.message);
43
+ }
44
+ })();
45
+ }
46
+
47
+ node.on('input', async function(msg, send, done) {
48
+ if (!node.chip) { done(new Error('MCP23008 not initialised')); return; }
49
+ try {
50
+ const p = msg.payload;
51
+ if (p && typeof p.pin === 'number' && typeof p.value === 'number') {
52
+ const pin = node.chip.pin(p.pin);
53
+ if ((node.chip._direction >> p.pin) & 1) await pin.setDirection('out');
54
+ await pin.write(p.value ? 1 : 0);
55
+ done();
56
+ } else if (p && typeof p.pin === 'number') {
57
+ msg.payload = { pin: p.pin, value: ((await node.chip.readPort()) >> p.pin) & 1 };
58
+ send(msg);
59
+ done();
60
+ } else if (p && typeof p.port === 'number') {
61
+ msg.payload = { port: p.port, value: await node.chip.readPort() };
62
+ send(msg);
63
+ done();
64
+ } else {
65
+ done(new Error('msg.payload must be { pin, value }, { pin }, or { port }'));
66
+ }
67
+ } catch (e) { done(e); }
68
+ });
69
+
70
+ node.on('close', async function() {
71
+ if (node.chip) await node.chip.offInterrupt();
72
+ if (node.connection) await node.connection.close();
73
+ });
74
+ }
75
+
76
+ RED.nodes.registerType('periph-mcp23008', Mcp23008Node);
77
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-io-expander",
3
- "version": "1.3.2",
3
+ "version": "1.4.0",
4
4
  "description": "Node-RED nodes for IO expander chips",
5
5
  "keywords": [
6
6
  "node-red",
@@ -12,8 +12,10 @@
12
12
  "main": "index.js",
13
13
  "node-red": {
14
14
  "nodes": {
15
- "periph-pcf8574": "nodes/pcf8574/pcf8574.js",
15
+ "mcp23008": "nodes/mcp23008/mcp23008.js",
16
+ "periph-mcp23008": "nodes/mcp23008/mcp23008.js",
16
17
  "periph-mcp23017": "nodes/mcp23017/mcp23017.js",
18
+ "periph-pcf8574": "nodes/pcf8574/pcf8574.js",
17
19
  "periph-pcf8575": "nodes/pcf8575/pcf8575.js",
18
20
  "periph-tpic6b595": "nodes/tpic6b595/tpic6b595.js"
19
21
  }