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.
@@ -4,7 +4,7 @@ module.exports = function(RED) {
4
4
  const { I2CConnection } = require('periph/src/connection/i2c');
5
5
  const { Pca6416aFull } = require('periph/src/chips/io_expander/pca6416a');
6
6
 
7
- function Pca6416aNode(config) {
7
+ function Pca6416aDeviceNode(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.readAll();
30
- node.send({ payload: { changed, value } });
30
+ node.emit('periph-data', { payload: { changed, value } });
31
31
  }, { intPin });
32
32
  } catch (e) {
33
33
  node.error('PCA6416A INT setup failed: ' + e.message);
@@ -35,25 +35,42 @@ 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('pca6416a-device', Pca6416aDeviceNode);
44
+
45
+ function Pca6416aReadNode(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('PCA6416A not initialised')); return; }
56
+ if (!deviceNode.chip) { done(new Error('PCA6416A 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.readAll()) >> p.pin) & 1 };
65
+ msg.payload = { pin: p.pin, value: ((await deviceNode.chip.readAll()) >> 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(p.port) };
69
+ msg.payload = { port: p.port, value: await deviceNode.chip.readPort(p.port) };
53
70
  send(msg);
54
71
  done();
55
72
  } else if (p && typeof p === 'object') {
56
- msg.payload = { value: await node.chip.readAll() };
73
+ msg.payload = { value: await deviceNode.chip.readAll() };
57
74
  send(msg);
58
75
  done();
59
76
  } else {
@@ -61,12 +78,6 @@ module.exports = function(RED) {
61
78
  }
62
79
  } catch (e) { done(e); }
63
80
  });
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
81
  }
70
-
71
- RED.nodes.registerType('periph-pca6416a', Pca6416aNode);
82
+ RED.nodes.registerType('pca6416a', Pca6416aReadNode);
72
83
  };
@@ -1,38 +1,62 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-pca9536', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('pca9536-device', {
4
+ category: 'config',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  bus: { value: '1', required: true, validate: RED.validators.number() },
8
8
  pollInterval: { value: '' }
9
9
  },
10
+ label: function() {
11
+ return this.name || 'PCA9536';
12
+ }
13
+ });
14
+ </script>
15
+
16
+ <script type="text/html" data-template-name="pca9536-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="PCA9536">
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;(/dev/i2c-N)
24
+ </div>
25
+ <div class="form-row">
26
+ <label for="node-config-input-pollInterval">Poll interval</label>
27
+ <input type="number" id="node-config-input-pollInterval" min="0" style="width:80px" placeholder="off"> &nbsp;ms (the chip has no INT pin; address is fixed at 0x41)
28
+ </div>
29
+ </script>
30
+
31
+ <!-- Read node -->
32
+ <script type="text/javascript">
33
+ RED.nodes.registerType('pca9536', {
34
+ category: 'Peripheral',
35
+ color: '#C084FC',
36
+ defaults: {
37
+ name: { value: '' },
38
+ device: { value: '', type: 'pca9536-device', required: true }
39
+ },
10
40
  inputs: 1,
11
41
  outputs: 1,
12
42
  icon: 'font-awesome/fa-microchip',
13
- label: function() {
14
- return this.name || ('PCA9536 (bus ' + this.bus + ')');
15
- },
43
+ label: function() { return this.name || 'PCA9536'; },
16
44
  paletteLabel: 'PCA9536'
17
45
  });
18
46
  </script>
19
47
 
20
- <script type="text/html" data-template-name="periph-pca9536">
48
+ <script type="text/html" data-template-name="pca9536">
21
49
  <div class="form-row">
22
50
  <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
23
51
  <input type="text" id="node-input-name" placeholder="PCA9536">
24
52
  </div>
25
53
  <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)
54
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
55
+ <input type="text" id="node-input-device">
32
56
  </div>
33
57
  </script>
34
58
 
35
- <script type="text/html" data-help-name="periph-pca9536">
59
+ <script type="text/html" data-help-name="pca9536">
36
60
  <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
61
 
38
62
  <h3>Inputs</h3>
@@ -4,7 +4,7 @@ module.exports = function(RED) {
4
4
  const { I2CConnection } = require('periph/src/connection/i2c');
5
5
  const { Pca9536Full } = require('periph/src/chips/io_expander/pca9536');
6
6
 
7
- function Pca9536Node(config) {
7
+ function Pca9536DeviceNode(config) {
8
8
  RED.nodes.createNode(this, config);
9
9
  const node = this;
10
10
  try {
@@ -22,28 +22,45 @@ module.exports = function(RED) {
22
22
  node.chip.ready.then(() => {
23
23
  node.chip.watchChanges(async (changed) => {
24
24
  const value = await node.chip.readPort();
25
- node.send({ payload: { port: 0, changed, value } });
25
+ node.emit('periph-data', { payload: { port: 0, changed, value } });
26
26
  }, interval / 1000);
27
27
  }).catch((e) => node.error('PCA9536 polling setup failed: ' + e.message));
28
28
  }
29
29
 
30
+ node.on('close', async function() {
31
+ if (node.chip) node.chip.unwatchChanges();
32
+ if (node.connection) await node.connection.close();
33
+ });
34
+ }
35
+ RED.nodes.registerType('pca9536-device', Pca9536DeviceNode);
36
+
37
+ function Pca9536ReadNode(config) {
38
+ RED.nodes.createNode(this, config);
39
+ const node = this;
40
+ const deviceNode = RED.nodes.getNode(config.device) || {};
41
+ if (deviceNode.on) {
42
+ const onData = (m) => node.send(m);
43
+ deviceNode.on('periph-data', onData);
44
+ node.on('close', () => deviceNode.removeListener('periph-data', onData));
45
+ }
46
+
30
47
  node.on('input', async function(msg, send, done) {
31
- if (!node.chip) { done(new Error('PCA9536 not initialised')); return; }
48
+ if (!deviceNode.chip) { done(new Error('PCA9536 not initialised')); return; }
32
49
  try {
33
50
  const p = msg.payload;
34
51
  if (p && typeof p.pin === 'number' && (p.pin < 0 || p.pin > 3)) {
35
52
  done(new Error('pin must be 0-3'));
36
53
  } 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');
54
+ const pin = deviceNode.chip.pin(p.pin);
55
+ if ((deviceNode.chip._direction >> p.pin) & 1) await pin.setDirection('out');
39
56
  await pin.write(p.value ? 1 : 0);
40
57
  done();
41
58
  } else if (p && typeof p.pin === 'number') {
42
- msg.payload = { pin: p.pin, value: ((await node.chip.readPort()) >> p.pin) & 1 };
59
+ msg.payload = { pin: p.pin, value: ((await deviceNode.chip.readPort()) >> p.pin) & 1 };
43
60
  send(msg);
44
61
  done();
45
62
  } else if (p && typeof p.port === 'number') {
46
- msg.payload = { port: p.port, value: await node.chip.readPort() };
63
+ msg.payload = { port: p.port, value: await deviceNode.chip.readPort() };
47
64
  send(msg);
48
65
  done();
49
66
  } else {
@@ -51,12 +68,6 @@ module.exports = function(RED) {
51
68
  }
52
69
  } catch (e) { done(e); }
53
70
  });
54
-
55
- node.on('close', async function() {
56
- if (node.chip) node.chip.unwatchChanges();
57
- if (node.connection) await node.connection.close();
58
- });
59
71
  }
60
-
61
- RED.nodes.registerType('periph-pca9536', Pca9536Node);
72
+ RED.nodes.registerType('pca9536', Pca9536ReadNode);
62
73
  };
@@ -1,7 +1,7 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-pca9539', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('pca9539-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 || ('PCA9539 @ 0x' + this.address + ' (bus ' + this.bus + ')');
17
- },
18
- paletteLabel: 'PCA9539'
12
+ label: function() {
13
+ return this.name || ('PCA9539 @ ' + this.address + ' (bus ' + this.bus + ')');
14
+ }
19
15
  });
20
16
  </script>
21
17
 
22
- <script type="text/html" data-template-name="periph-pca9539">
18
+ <script type="text/html" data-template-name="pca9539-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="PCA9539">
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="PCA9539">
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="74" style="width:80px">
28
+ <label for="node-config-input-address">Address (hex)</label>
29
+ <input type="text" id="node-config-input-address" placeholder="74" style="width:80px">
34
30
  &nbsp;74–77 (A1, A0)
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('pca9539', {
45
+ category: 'Peripheral',
46
+ color: '#C084FC',
47
+ defaults: {
48
+ name: { value: '' },
49
+ device: { value: '', type: 'pca9539-device', required: true }
50
+ },
51
+ inputs: 1,
52
+ outputs: 1,
53
+ icon: 'font-awesome/fa-microchip',
54
+ label: function() { return this.name || 'PCA9539'; },
55
+ paletteLabel: 'PCA9539'
56
+ });
57
+ </script>
58
+
59
+ <script type="text/html" data-template-name="pca9539">
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="PCA9539">
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-pca9539">
70
+ <script type="text/html" data-help-name="pca9539">
47
71
  <p>Controls and reads a 16-bit PCA9539 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 { Pca9539Full } = require('periph/src/chips/io_expander/pca9539');
6
6
 
7
- function Pca9539Node(config) {
7
+ function Pca9539DeviceNode(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.readAll();
30
- node.send({ payload: { changed, value } });
30
+ node.emit('periph-data', { payload: { changed, value } });
31
31
  }, { intPin });
32
32
  } catch (e) {
33
33
  node.error('PCA9539 INT setup failed: ' + e.message);
@@ -35,25 +35,42 @@ 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('pca9539-device', Pca9539DeviceNode);
44
+
45
+ function Pca9539ReadNode(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('PCA9539 not initialised')); return; }
56
+ if (!deviceNode.chip) { done(new Error('PCA9539 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.readAll()) >> p.pin) & 1 };
65
+ msg.payload = { pin: p.pin, value: ((await deviceNode.chip.readAll()) >> 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(p.port) };
69
+ msg.payload = { port: p.port, value: await deviceNode.chip.readPort(p.port) };
53
70
  send(msg);
54
71
  done();
55
72
  } else if (p && typeof p === 'object') {
56
- msg.payload = { value: await node.chip.readAll() };
73
+ msg.payload = { value: await deviceNode.chip.readAll() };
57
74
  send(msg);
58
75
  done();
59
76
  } else {
@@ -61,12 +78,6 @@ module.exports = function(RED) {
61
78
  }
62
79
  } catch (e) { done(e); }
63
80
  });
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
81
  }
70
-
71
- RED.nodes.registerType('periph-pca9539', Pca9539Node);
82
+ RED.nodes.registerType('pca9539', Pca9539ReadNode);
72
83
  };
@@ -1,37 +1,63 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-pcf8574', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('pcf8574-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 || ('PCF8574 @ ' + this.address + ' (bus ' + this.bus + ')');
12
+ }
13
+ });
14
+ </script>
15
+
16
+ <script type="text/html" data-template-name="pcf8574-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="PCF8574">
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;PCF8574: 20–27 &nbsp;PCF8574A: 38–3F
29
+ </div>
30
+ </script>
31
+
32
+ <!-- Read node -->
33
+ <script type="text/javascript">
34
+ RED.nodes.registerType('pcf8574', {
35
+ category: 'Peripheral',
36
+ color: '#C084FC',
37
+ defaults: {
38
+ name: { value: '' },
39
+ device: { value: '', type: 'pcf8574-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 || ('PCF8574 @ 0x' + this.address + ' (bus ' + this.bus + ')'); },
44
+ label: function() { return this.name || 'PCF8574'; },
14
45
  paletteLabel: 'PCF8574'
15
46
  });
16
47
  </script>
17
48
 
18
- <script type="text/html" data-template-name="periph-pcf8574">
49
+ <script type="text/html" data-template-name="pcf8574">
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="PCF8574">
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;PCF8574: 20–27 &nbsp;PCF8574A: 38–3F
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-pcf8574">
60
+ <script type="text/html" data-help-name="pcf8574">
35
61
  <p>Controls and reads an 8-bit PCF8574 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 { Pcf8574Full } = require('periph/src/chips/io_expander/pcf8574');
6
6
 
7
- function Pcf8574Node(config) {
7
+ function Pcf8574DeviceNode(config) {
8
8
  RED.nodes.createNode(this, config);
9
9
  const node = this;
10
10
  try {
@@ -18,22 +18,33 @@ module.exports = function(RED) {
18
18
  node.error('PCF8574 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('pcf8574-device', Pcf8574DeviceNode);
26
+
27
+ function Pcf8574ReadNode(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('PCF8574 not initialised')); return; }
33
+ if (!deviceNode.chip) { done(new Error('PCF8574 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
37
  // Set output pin
27
- await node.chip._setPin(p.pin, p.value ? 1 : 0);
38
+ await deviceNode.chip._setPin(p.pin, p.value ? 1 : 0);
28
39
  done();
29
40
  } else if (p && typeof p.pin === 'number') {
30
41
  // Read input pin
31
- msg.payload = { pin: p.pin, value: (await node.chip.readPort()) >> p.pin & 1 };
42
+ msg.payload = { pin: p.pin, value: (await deviceNode.chip.readPort()) >> p.pin & 1 };
32
43
  send(msg);
33
44
  done();
34
45
  } else if (p && typeof p.port === 'number') {
35
46
  // Read full port
36
- msg.payload = { port: p.port, value: await node.chip.readPort(p.port) };
47
+ msg.payload = { port: p.port, value: await deviceNode.chip.readPort(p.port) };
37
48
  send(msg);
38
49
  done();
39
50
  } else {
@@ -41,11 +52,6 @@ module.exports = function(RED) {
41
52
  }
42
53
  } catch (e) { done(e); }
43
54
  });
44
-
45
- node.on('close', async function() {
46
- if (node.connection) await node.connection.close();
47
- });
48
55
  }
49
-
50
- RED.nodes.registerType('periph-pcf8574', Pcf8574Node);
56
+ RED.nodes.registerType('pcf8574', Pcf8574ReadNode);
51
57
  };
@@ -1,37 +1,63 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-pcf8575', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('pcf8575-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 || ('PCF8575 @ ' + this.address + ' (bus ' + this.bus + ')');
12
+ }
13
+ });
14
+ </script>
15
+
16
+ <script type="text/html" data-template-name="pcf8575-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="PCF8575">
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;PCF8575: 20–27
29
+ </div>
30
+ </script>
31
+
32
+ <!-- Read node -->
33
+ <script type="text/javascript">
34
+ RED.nodes.registerType('pcf8575', {
35
+ category: 'Peripheral',
36
+ color: '#C084FC',
37
+ defaults: {
38
+ name: { value: '' },
39
+ device: { value: '', type: 'pcf8575-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 || ('PCF8575 @ 0x' + this.address + ' (bus ' + this.bus + ')'); },
44
+ label: function() { return this.name || 'PCF8575'; },
14
45
  paletteLabel: 'PCF8575'
15
46
  });
16
47
  </script>
17
48
 
18
- <script type="text/html" data-template-name="periph-pcf8575">
49
+ <script type="text/html" data-template-name="pcf8575">
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="PCF8575">
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;PCF8575: 20–27
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-pcf8575">
60
+ <script type="text/html" data-help-name="pcf8575">
35
61
  <p>Controls and reads a 16-bit PCF8575 I/O expander over I²C.</p>
36
62
 
37
63
  <h3>Inputs</h3>