node-red-contrib-periph-io-expander 1.5.0 → 1.5.2

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.
@@ -1,7 +1,7 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-mcp23008', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('mcp23008-device', {
4
+ category: 'config',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  transport: { value: 'i2c' },
@@ -13,71 +13,93 @@
13
13
  intPin: { value: '' },
14
14
  intMask: { value: 'F0' }
15
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 + ')'));
16
+ label: function() {
17
+ return this.name || ('MCP23008 @ ' + this.address + ' (bus ' + this.bus + ')');
23
18
  },
24
- paletteLabel: 'MCP23008',
25
19
  oneditprepare: function() {
26
20
  function sync() {
27
- const spi = $('#node-input-transport').val() === 'spi';
21
+ const spi = $('#node-config-input-transport').val() === 'spi';
28
22
  $('.mcp-i2c').toggle(!spi);
29
23
  $('.mcp-spi').toggle(spi);
30
24
  }
31
- $('#node-input-transport').on('change', sync);
25
+ $('#node-config-input-transport').on('change', sync);
32
26
  sync();
33
27
  }
34
28
  });
35
29
  </script>
36
30
 
37
- <script type="text/html" data-template-name="periph-mcp23008">
31
+ <script type="text/html" data-template-name="mcp23008-device">
38
32
  <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">
33
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
34
+ <input type="text" id="node-config-input-name" placeholder="MCP23008">
41
35
  </div>
42
36
  <div class="form-row">
43
- <label for="node-input-transport">Transport</label>
44
- <select id="node-input-transport">
37
+ <label for="node-config-input-transport">Transport</label>
38
+ <select id="node-config-input-transport">
45
39
  <option value="i2c">I²C (MCP23008)</option>
46
40
  <option value="spi">SPI (MCP23S08)</option>
47
41
  </select>
48
42
  </div>
49
43
  <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>)
44
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> Bus</label>
45
+ <input type="number" id="node-config-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
46
  </div>
53
47
  <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">
48
+ <label for="node-config-input-address">Address (hex)</label>
49
+ <input type="text" id="node-config-input-address" placeholder="20" style="width:80px">
56
50
  &nbsp;0x20–0x27
57
51
  </div>
58
52
  <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>)
53
+ <label for="node-config-input-spiDevice">Chip select</label>
54
+ <input type="number" id="node-config-input-spiDevice" min="0" style="width:80px"> &nbsp;(spidevN.<i>M</i>)
61
55
  </div>
62
56
  <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
57
+ <label for="node-config-input-spiAddr">Device address</label>
58
+ <input type="number" id="node-config-input-spiAddr" min="0" max="3" style="width:80px"> &nbsp;A1:A0 pins, 0–3
65
59
  </div>
66
60
  <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)
61
+ <label for="node-config-input-gpioChip">GPIO chip</label>
62
+ <input type="number" id="node-config-input-gpioChip" min="0" style="width:80px"> &nbsp;(for the optional INT line)
69
63
  </div>
70
64
  <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">
65
+ <label for="node-config-input-intPin">INT GPIO line</label>
66
+ <input type="number" id="node-config-input-intPin" min="0" style="width:80px" placeholder="none">
67
+ </div>
68
+ <div class="form-row">
69
+ <label for="node-config-input-intMask">INT pin mask (hex)</label>
70
+ <input type="text" id="node-config-input-intMask" placeholder="F0" style="width:80px"> &nbsp;pins armed for interrupt-on-change (pull-ups enabled)
71
+ </div>
72
+ </script>
73
+
74
+ <!-- Read node -->
75
+ <script type="text/javascript">
76
+ RED.nodes.registerType('mcp23008', {
77
+ category: 'Peripheral',
78
+ color: '#C084FC',
79
+ defaults: {
80
+ name: { value: '' },
81
+ device: { value: '', type: 'mcp23008-device', required: true }
82
+ },
83
+ inputs: 1,
84
+ outputs: 1,
85
+ icon: 'font-awesome/fa-microchip',
86
+ label: function() { return this.name || 'MCP23008'; },
87
+ paletteLabel: 'MCP23008'
88
+ });
89
+ </script>
90
+
91
+ <script type="text/html" data-template-name="mcp23008">
92
+ <div class="form-row">
93
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
94
+ <input type="text" id="node-input-name" placeholder="MCP23008">
73
95
  </div>
74
96
  <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)
97
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
98
+ <input type="text" id="node-input-device">
77
99
  </div>
78
100
  </script>
79
101
 
80
- <script type="text/html" data-help-name="periph-mcp23008">
102
+ <script type="text/html" data-help-name="mcp23008">
81
103
  <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
104
 
83
105
  <h3>Inputs</h3>
@@ -5,7 +5,7 @@ module.exports = function(RED) {
5
5
  const { SPIConnection } = require('periph/src/connection/spi');
6
6
  const { Mcp23008Full, Mcp23S08Full } = require('periph/src/chips/io_expander/mcp23008');
7
7
 
8
- function Mcp23008Node(config) {
8
+ function Mcp23008DeviceNode(config) {
9
9
  RED.nodes.createNode(this, config);
10
10
  const node = this;
11
11
  try {
@@ -36,7 +36,7 @@ module.exports = function(RED) {
36
36
  await node.chip.enablePinInterrupt(parseInt(config.intMask || 'F0', 16));
37
37
  await node.chip.onInterrupt(async (status) => {
38
38
  const capture = await node.chip.readCapture();
39
- node.send({ payload: { interrupt: status, capture } });
39
+ node.emit('periph-data', { payload: { interrupt: status, capture } });
40
40
  }, { intPin });
41
41
  } catch (e) {
42
42
  node.error('MCP23008 INT setup failed: ' + e.message);
@@ -44,21 +44,38 @@ module.exports = function(RED) {
44
44
  })();
45
45
  }
46
46
 
47
+ node.on('close', async function() {
48
+ if (node.chip) await node.chip.offInterrupt();
49
+ if (node.connection) await node.connection.close();
50
+ });
51
+ }
52
+ RED.nodes.registerType('mcp23008-device', Mcp23008DeviceNode);
53
+
54
+ function Mcp23008ReadNode(config) {
55
+ RED.nodes.createNode(this, config);
56
+ const node = this;
57
+ const deviceNode = RED.nodes.getNode(config.device) || {};
58
+ if (deviceNode.on) {
59
+ const onData = (m) => node.send(m);
60
+ deviceNode.on('periph-data', onData);
61
+ node.on('close', () => deviceNode.removeListener('periph-data', onData));
62
+ }
63
+
47
64
  node.on('input', async function(msg, send, done) {
48
- if (!node.chip) { done(new Error('MCP23008 not initialised')); return; }
65
+ if (!deviceNode.chip) { done(new Error('MCP23008 not initialised')); return; }
49
66
  try {
50
67
  const p = msg.payload;
51
68
  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');
69
+ const pin = deviceNode.chip.pin(p.pin);
70
+ if ((deviceNode.chip._direction >> p.pin) & 1) await pin.setDirection('out');
54
71
  await pin.write(p.value ? 1 : 0);
55
72
  done();
56
73
  } else if (p && typeof p.pin === 'number') {
57
- msg.payload = { pin: p.pin, value: ((await node.chip.readPort()) >> p.pin) & 1 };
74
+ msg.payload = { pin: p.pin, value: ((await deviceNode.chip.readPort()) >> p.pin) & 1 };
58
75
  send(msg);
59
76
  done();
60
77
  } else if (p && typeof p.port === 'number') {
61
- msg.payload = { port: p.port, value: await node.chip.readPort() };
78
+ msg.payload = { port: p.port, value: await deviceNode.chip.readPort() };
62
79
  send(msg);
63
80
  done();
64
81
  } else {
@@ -66,12 +83,6 @@ module.exports = function(RED) {
66
83
  }
67
84
  } catch (e) { done(e); }
68
85
  });
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
86
  }
75
-
76
- RED.nodes.registerType('periph-mcp23008', Mcp23008Node);
87
+ RED.nodes.registerType('mcp23008', Mcp23008ReadNode);
77
88
  };
@@ -1,37 +1,63 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-mcp23017', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('mcp23017-device', {
4
+ category: 'config',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  bus: { value: '1', required: true, validate: RED.validators.number() },
8
8
  address: { value: '20', required: true }
9
9
  },
10
+ label: function() {
11
+ return this.name || ('MCP23017 @ ' + this.address + ' (bus ' + this.bus + ')');
12
+ }
13
+ });
14
+ </script>
15
+
16
+ <script type="text/html" data-template-name="mcp23017-device">
17
+ <div class="form-row">
18
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
19
+ <input type="text" id="node-config-input-name" placeholder="MCP23017">
20
+ </div>
21
+ <div class="form-row">
22
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
23
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
24
+ </div>
25
+ <div class="form-row">
26
+ <label for="node-config-input-address">Address (hex)</label>
27
+ <input type="text" id="node-config-input-address" placeholder="20" style="width:80px">
28
+ &nbsp;0x20–0x27
29
+ </div>
30
+ </script>
31
+
32
+ <!-- Read node -->
33
+ <script type="text/javascript">
34
+ RED.nodes.registerType('mcp23017', {
35
+ category: 'Peripheral',
36
+ color: '#C084FC',
37
+ defaults: {
38
+ name: { value: '' },
39
+ device: { value: '', type: 'mcp23017-device', required: true }
40
+ },
10
41
  inputs: 1,
11
42
  outputs: 1,
12
43
  icon: 'font-awesome/fa-microchip',
13
- label: function() { return this.name || ('MCP23017 @ 0x' + this.address + ' (bus ' + this.bus + ')'); },
44
+ label: function() { return this.name || 'MCP23017'; },
14
45
  paletteLabel: 'MCP23017'
15
46
  });
16
47
  </script>
17
48
 
18
- <script type="text/html" data-template-name="periph-mcp23017">
49
+ <script type="text/html" data-template-name="mcp23017">
19
50
  <div class="form-row">
20
51
  <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
21
52
  <input type="text" id="node-input-name" placeholder="MCP23017">
22
53
  </div>
23
54
  <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
55
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
56
+ <input type="text" id="node-input-device">
31
57
  </div>
32
58
  </script>
33
59
 
34
- <script type="text/html" data-help-name="periph-mcp23017">
60
+ <script type="text/html" data-help-name="mcp23017">
35
61
  <p>Controls and reads a 16-bit MCP23017 I/O expander over I²C.</p>
36
62
 
37
63
  <h3>Inputs</h3>
@@ -4,7 +4,7 @@ module.exports = function(RED) {
4
4
  const { I2CConnection } = require('periph/src/connection/i2c');
5
5
  const { Mcp23017Full } = require('periph/src/chips/io_expander/mcp23017');
6
6
 
7
- function Mcp23017Node(config) {
7
+ function Mcp23017DeviceNode(config) {
8
8
  RED.nodes.createNode(this, config);
9
9
  const node = this;
10
10
  try {
@@ -18,21 +18,32 @@ module.exports = function(RED) {
18
18
  node.error('MCP23017 init failed: ' + e.message);
19
19
  }
20
20
 
21
+ node.on('close', async function() {
22
+ if (node.connection) await node.connection.close();
23
+ });
24
+ }
25
+ RED.nodes.registerType('mcp23017-device', Mcp23017DeviceNode);
26
+
27
+ function Mcp23017ReadNode(config) {
28
+ RED.nodes.createNode(this, config);
29
+ const node = this;
30
+ const deviceNode = RED.nodes.getNode(config.device) || {};
31
+
21
32
  node.on('input', async function(msg, send, done) {
22
- if (!node.chip) { done(new Error('MCP23017 not initialised')); return; }
33
+ if (!deviceNode.chip) { done(new Error('MCP23017 not initialised')); return; }
23
34
  try {
24
35
  const p = msg.payload;
25
36
  if (p && typeof p.pin === 'number' && typeof p.value === 'number') {
26
- await node.chip._setPin(p.pin, p.value ? 1 : 0);
37
+ await deviceNode.chip._setPin(p.pin, p.value ? 1 : 0);
27
38
  done();
28
39
  } else if (p && typeof p.pin === 'number') {
29
40
  const port = Math.floor(p.pin / 8);
30
41
  const bit = p.pin % 8;
31
- msg.payload = { pin: p.pin, value: ((await node.chip.readPort(port)) >> bit) & 1 };
42
+ msg.payload = { pin: p.pin, value: ((await deviceNode.chip.readPort(port)) >> bit) & 1 };
32
43
  send(msg);
33
44
  done();
34
45
  } else if (p && typeof p.port === 'number') {
35
- msg.payload = { port: p.port, value: await node.chip.readPort(p.port) };
46
+ msg.payload = { port: p.port, value: await deviceNode.chip.readPort(p.port) };
36
47
  send(msg);
37
48
  done();
38
49
  } else {
@@ -40,11 +51,6 @@ module.exports = function(RED) {
40
51
  }
41
52
  } catch (e) { done(e); }
42
53
  });
43
-
44
- node.on('close', async function() {
45
- if (node.connection) await node.connection.close();
46
- });
47
54
  }
48
-
49
- RED.nodes.registerType('periph-mcp23017', Mcp23017Node);
55
+ RED.nodes.registerType('mcp23017', Mcp23017ReadNode);
50
56
  };
@@ -1,7 +1,7 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-pca6408a', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('pca6408a-device', {
4
+ category: 'config',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  bus: { value: '1', required: true, validate: RED.validators.number() },
@@ -9,41 +9,65 @@
9
9
  gpioChip: { value: '0' },
10
10
  intPin: { value: '' }
11
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'
12
+ label: function() {
13
+ return this.name || ('PCA6408A @ ' + this.address + ' (bus ' + this.bus + ')');
14
+ }
19
15
  });
20
16
  </script>
21
17
 
22
- <script type="text/html" data-template-name="periph-pca6408a">
18
+ <script type="text/html" data-template-name="pca6408a-device">
23
19
  <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">
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="PCA6408A">
26
22
  </div>
27
23
  <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)
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;(/dev/i2c-N)
30
26
  </div>
31
27
  <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">
28
+ <label for="node-config-input-address">Address (hex)</label>
29
+ <input type="text" id="node-config-input-address" placeholder="20" style="width:80px">
34
30
  &nbsp;20 (ADDR = VSS) or 21 (ADDR = VDD(P))
35
31
  </div>
36
32
  <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)
33
+ <label for="node-config-input-gpioChip">GPIO chip</label>
34
+ <input type="number" id="node-config-input-gpioChip" min="0" style="width:80px"> &nbsp;(for the optional INT line)
35
+ </div>
36
+ <div class="form-row">
37
+ <label for="node-config-input-intPin">INT GPIO line</label>
38
+ <input type="number" id="node-config-input-intPin" min="0" style="width:80px" placeholder="none">
39
+ </div>
40
+ </script>
41
+
42
+ <!-- Read node -->
43
+ <script type="text/javascript">
44
+ RED.nodes.registerType('pca6408a', {
45
+ category: 'Peripheral',
46
+ color: '#C084FC',
47
+ defaults: {
48
+ name: { value: '' },
49
+ device: { value: '', type: 'pca6408a-device', required: true }
50
+ },
51
+ inputs: 1,
52
+ outputs: 1,
53
+ icon: 'font-awesome/fa-microchip',
54
+ label: function() { return this.name || 'PCA6408A'; },
55
+ paletteLabel: 'PCA6408A'
56
+ });
57
+ </script>
58
+
59
+ <script type="text/html" data-template-name="pca6408a">
60
+ <div class="form-row">
61
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
62
+ <input type="text" id="node-input-name" placeholder="PCA6408A">
39
63
  </div>
40
64
  <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">
65
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
66
+ <input type="text" id="node-input-device">
43
67
  </div>
44
68
  </script>
45
69
 
46
- <script type="text/html" data-help-name="periph-pca6408a">
70
+ <script type="text/html" data-help-name="pca6408a">
47
71
  <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
72
 
49
73
  <h3>Inputs</h3>
@@ -4,7 +4,7 @@ module.exports = function(RED) {
4
4
  const { I2CConnection } = require('periph/src/connection/i2c');
5
5
  const { Pca6408aFull } = require('periph/src/chips/io_expander/pca6408a');
6
6
 
7
- function Pca6408aNode(config) {
7
+ function Pca6408aDeviceNode(config) {
8
8
  RED.nodes.createNode(this, config);
9
9
  const node = this;
10
10
  try {
@@ -27,7 +27,7 @@ module.exports = function(RED) {
27
27
  await node.chip.ready;
28
28
  await node.chip.onInterrupt(async (changed) => {
29
29
  const value = await node.chip.readPort();
30
- node.send({ payload: { port: 0, changed, value } });
30
+ node.emit('periph-data', { payload: { port: 0, changed, value } });
31
31
  }, { intPin });
32
32
  } catch (e) {
33
33
  node.error('PCA6408A INT setup failed: ' + e.message);
@@ -35,21 +35,38 @@ module.exports = function(RED) {
35
35
  })();
36
36
  }
37
37
 
38
+ node.on('close', async function() {
39
+ if (node.chip) await node.chip.offInterrupt();
40
+ if (node.connection) await node.connection.close();
41
+ });
42
+ }
43
+ RED.nodes.registerType('pca6408a-device', Pca6408aDeviceNode);
44
+
45
+ function Pca6408aReadNode(config) {
46
+ RED.nodes.createNode(this, config);
47
+ const node = this;
48
+ const deviceNode = RED.nodes.getNode(config.device) || {};
49
+ if (deviceNode.on) {
50
+ const onData = (m) => node.send(m);
51
+ deviceNode.on('periph-data', onData);
52
+ node.on('close', () => deviceNode.removeListener('periph-data', onData));
53
+ }
54
+
38
55
  node.on('input', async function(msg, send, done) {
39
- if (!node.chip) { done(new Error('PCA6408A not initialised')); return; }
56
+ if (!deviceNode.chip) { done(new Error('PCA6408A not initialised')); return; }
40
57
  try {
41
58
  const p = msg.payload;
42
59
  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');
60
+ const pin = deviceNode.chip.pin(p.pin);
61
+ if ((deviceNode.chip._direction >> p.pin) & 1) await pin.setDirection('out');
45
62
  await pin.write(p.value ? 1 : 0);
46
63
  done();
47
64
  } else if (p && typeof p.pin === 'number') {
48
- msg.payload = { pin: p.pin, value: ((await node.chip.readPort()) >> p.pin) & 1 };
65
+ msg.payload = { pin: p.pin, value: ((await deviceNode.chip.readPort()) >> p.pin) & 1 };
49
66
  send(msg);
50
67
  done();
51
68
  } else if (p && typeof p.port === 'number') {
52
- msg.payload = { port: p.port, value: await node.chip.readPort() };
69
+ msg.payload = { port: p.port, value: await deviceNode.chip.readPort() };
53
70
  send(msg);
54
71
  done();
55
72
  } else {
@@ -57,12 +74,6 @@ module.exports = function(RED) {
57
74
  }
58
75
  } catch (e) { done(e); }
59
76
  });
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
77
  }
66
-
67
- RED.nodes.registerType('periph-pca6408a', Pca6408aNode);
78
+ RED.nodes.registerType('pca6408a', Pca6408aReadNode);
68
79
  };
@@ -1,7 +1,7 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-pca6416a', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('pca6416a-device', {
4
+ category: 'config',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  bus: { value: '1', required: true, validate: RED.validators.number() },
@@ -9,41 +9,65 @@
9
9
  gpioChip: { value: '0' },
10
10
  intPin: { value: '' }
11
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'
12
+ label: function() {
13
+ return this.name || ('PCA6416A @ ' + this.address + ' (bus ' + this.bus + ')');
14
+ }
19
15
  });
20
16
  </script>
21
17
 
22
- <script type="text/html" data-template-name="periph-pca6416a">
18
+ <script type="text/html" data-template-name="pca6416a-device">
23
19
  <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">
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="PCA6416A">
26
22
  </div>
27
23
  <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)
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;(/dev/i2c-N)
30
26
  </div>
31
27
  <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">
28
+ <label for="node-config-input-address">Address (hex)</label>
29
+ <input type="text" id="node-config-input-address" placeholder="20" style="width:80px">
34
30
  &nbsp;20 (ADDR = VSS) or 21 (ADDR = VDD(P))
35
31
  </div>
36
32
  <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)
33
+ <label for="node-config-input-gpioChip">GPIO chip</label>
34
+ <input type="number" id="node-config-input-gpioChip" min="0" style="width:80px"> &nbsp;(for the optional INT line)
35
+ </div>
36
+ <div class="form-row">
37
+ <label for="node-config-input-intPin">INT GPIO line</label>
38
+ <input type="number" id="node-config-input-intPin" min="0" style="width:80px" placeholder="none">
39
+ </div>
40
+ </script>
41
+
42
+ <!-- Read node -->
43
+ <script type="text/javascript">
44
+ RED.nodes.registerType('pca6416a', {
45
+ category: 'Peripheral',
46
+ color: '#C084FC',
47
+ defaults: {
48
+ name: { value: '' },
49
+ device: { value: '', type: 'pca6416a-device', required: true }
50
+ },
51
+ inputs: 1,
52
+ outputs: 1,
53
+ icon: 'font-awesome/fa-microchip',
54
+ label: function() { return this.name || 'PCA6416A'; },
55
+ paletteLabel: 'PCA6416A'
56
+ });
57
+ </script>
58
+
59
+ <script type="text/html" data-template-name="pca6416a">
60
+ <div class="form-row">
61
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
62
+ <input type="text" id="node-input-name" placeholder="PCA6416A">
39
63
  </div>
40
64
  <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">
65
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
66
+ <input type="text" id="node-input-device">
43
67
  </div>
44
68
  </script>
45
69
 
46
- <script type="text/html" data-help-name="periph-pca6416a">
70
+ <script type="text/html" data-help-name="pca6416a">
47
71
  <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
72
 
49
73
  <h3>Inputs</h3>