node-red-contrib-periph-motor 1.5.1 → 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,32 +1,30 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-drv8830', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('drv8830-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: '0x60', required: true }
9
9
  },
10
- inputs: 1,
11
- outputs: 1,
12
- icon: 'font-awesome/fa-cog',
13
- label: function() { return this.name || 'DRV8830'; },
14
- paletteLabel: 'DRV8830'
10
+ label: function() {
11
+ return this.name || ('DRV8830 @ ' + this.address + ' (bus ' + this.bus + ')');
12
+ }
15
13
  });
16
14
  </script>
17
15
 
18
- <script type="text/html" data-template-name="periph-drv8830">
16
+ <script type="text/html" data-template-name="drv8830-device">
19
17
  <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="DRV8830">
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="DRV8830">
22
20
  </div>
23
21
  <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)
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)
26
24
  </div>
27
25
  <div class="form-row">
28
- <label for="node-input-address"><i class="fa fa-at"></i> I²C address</label>
29
- <select id="node-input-address" style="width:120px">
26
+ <label for="node-config-input-address"><i class="fa fa-at"></i> I²C address</label>
27
+ <select id="node-config-input-address" style="width:120px">
30
28
  <option value="0x60">0x60</option>
31
29
  <option value="0x61">0x61</option>
32
30
  <option value="0x62">0x62</option>
@@ -41,7 +39,35 @@
41
39
  </div>
42
40
  </script>
43
41
 
44
- <script type="text/html" data-help-name="periph-drv8830">
42
+ <!-- Read node -->
43
+ <script type="text/javascript">
44
+ RED.nodes.registerType('drv8830', {
45
+ category: 'Peripheral',
46
+ color: '#C084FC',
47
+ defaults: {
48
+ name: { value: '' },
49
+ device: { value: '', type: 'drv8830-device', required: true }
50
+ },
51
+ inputs: 1,
52
+ outputs: 1,
53
+ icon: 'font-awesome/fa-cog',
54
+ label: function() { return this.name || 'DRV8830'; },
55
+ paletteLabel: 'DRV8830'
56
+ });
57
+ </script>
58
+
59
+ <script type="text/html" data-template-name="drv8830">
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="DRV8830">
63
+ </div>
64
+ <div class="form-row">
65
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
66
+ <input type="text" id="node-input-device">
67
+ </div>
68
+ </script>
69
+
70
+ <script type="text/html" data-help-name="drv8830">
45
71
  <p>Drives a brushed DC motor through a DRV8830 low-voltage H-bridge over I²C, and reports its fault status. The chip regulates the commanded <em>voltage</em> across the motor, so speed stays constant as the supply sags.</p>
46
72
 
47
73
  <h3>Inputs</h3>
@@ -4,10 +4,9 @@ module.exports = function(RED) {
4
4
  const { I2CConnection } = require('periph/src/connection/i2c');
5
5
  const { DRV8830Full } = require('periph/src/chips/motor/drv8830');
6
6
 
7
- function DRV8830Node(config) {
7
+ function DRV8830DeviceNode(config) {
8
8
  RED.nodes.createNode(this, config);
9
9
  const node = this;
10
-
11
10
  try {
12
11
  const address = parseInt(config.address, 16) || DRV8830Full.I2C_ADDRESS;
13
12
  const connection = new I2CConnection(parseInt(config.bus, 10), address);
@@ -18,34 +17,41 @@ module.exports = function(RED) {
18
17
  node.error('DRV8830 init failed: ' + e.message);
19
18
  }
20
19
 
20
+ node.on('close', async function(removed, done) {
21
+ try {
22
+ if (node.driver) await node.driver.stop();
23
+ if (node.connection) await node.connection.close();
24
+ } catch (e) {
25
+ // bus may already be gone; nothing more to do on close
26
+ } finally {
27
+ done();
28
+ }
29
+ });
30
+ }
31
+ RED.nodes.registerType('drv8830-device', DRV8830DeviceNode);
32
+
33
+ function DRV8830ReadNode(config) {
34
+ RED.nodes.createNode(this, config);
35
+ const node = this;
36
+ const deviceNode = RED.nodes.getNode(config.device) || {};
37
+
21
38
  node.on('input', async function(msg, send, done) {
22
- if (!node.driver) { done(); return; }
39
+ if (!deviceNode.driver) { done(); return; }
23
40
  try {
24
41
  const p = msg.payload;
25
42
  if (p && typeof p === 'object') {
26
- if (p.clear_fault === true) { await node.driver.clearFault(); done(); return; }
27
- if (p.brake === true) { await node.driver.brake(); done(); return; }
28
- if (p.stop === true) { await node.driver.stop(); done(); return; }
29
- if (typeof p.voltage === 'number') { await node.driver.drive(p.voltage); done(); return; }
43
+ if (p.clear_fault === true) { await deviceNode.driver.clearFault(); done(); return; }
44
+ if (p.brake === true) { await deviceNode.driver.brake(); done(); return; }
45
+ if (p.stop === true) { await deviceNode.driver.stop(); done(); return; }
46
+ if (typeof p.voltage === 'number') { await deviceNode.driver.drive(p.voltage); done(); return; }
30
47
  }
31
- msg.payload = await node.driver.readFault();
48
+ msg.payload = await deviceNode.driver.readFault();
32
49
  send(msg);
33
50
  done();
34
51
  } catch (e) {
35
52
  done(e);
36
53
  }
37
54
  });
38
-
39
- node.on('close', async function(removed, done) {
40
- try {
41
- if (node.driver) await node.driver.stop();
42
- if (node.connection) await node.connection.close();
43
- } catch (e) {
44
- // bus may already be gone; nothing more to do on close
45
- } finally {
46
- done();
47
- }
48
- });
49
55
  }
50
- RED.nodes.registerType('periph-drv8830', DRV8830Node);
56
+ RED.nodes.registerType('drv8830', DRV8830ReadNode);
51
57
  };
@@ -1,7 +1,7 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-tmc2130', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('tmc2130-device', {
4
+ category: 'config',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  bus: { value: '0', required: true, validate: RED.validators.number() },
@@ -15,66 +15,92 @@
15
15
  microsteps: { value: '16' },
16
16
  speed: { value: '200', validate: RED.validators.number() }
17
17
  },
18
- inputs: 1,
19
- outputs: 1,
20
- icon: 'font-awesome/fa-cog',
21
- label: function() { return this.name || 'TMC2130'; },
22
- paletteLabel: 'TMC2130'
18
+ label: function() {
19
+ return this.name || 'TMC2130';
20
+ }
23
21
  });
24
22
  </script>
25
23
 
26
- <script type="text/html" data-template-name="periph-tmc2130">
24
+ <script type="text/html" data-template-name="tmc2130-device">
27
25
  <div class="form-row">
28
- <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
29
- <input type="text" id="node-input-name" placeholder="TMC2130">
26
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
27
+ <input type="text" id="node-config-input-name" placeholder="TMC2130">
30
28
  </div>
31
29
  <div class="form-row">
32
- <label for="node-input-bus"><i class="fa fa-plug"></i> SPI bus</label>
33
- <input type="number" id="node-input-bus" min="0" style="width:100px">
30
+ <label for="node-config-input-bus"><i class="fa fa-plug"></i> SPI bus</label>
31
+ <input type="number" id="node-config-input-bus" min="0" style="width:100px">
34
32
  </div>
35
33
  <div class="form-row">
36
- <label for="node-input-device"><i class="fa fa-plug"></i> SPI device (CS)</label>
37
- <input type="number" id="node-input-device" min="0" style="width:100px">
34
+ <label for="node-config-input-device"><i class="fa fa-plug"></i> SPI device (CS)</label>
35
+ <input type="number" id="node-config-input-device" min="0" style="width:100px">
38
36
  </div>
39
37
  <div class="form-row">
40
- <label for="node-input-speedHz"><i class="fa fa-tachometer"></i> SPI clock (Hz)</label>
41
- <input type="number" id="node-input-speedHz" min="100000" max="4000000" style="width:120px">
38
+ <label for="node-config-input-speedHz"><i class="fa fa-tachometer"></i> SPI clock (Hz)</label>
39
+ <input type="number" id="node-config-input-speedHz" min="100000" max="4000000" style="width:120px">
42
40
  </div>
43
41
  <div class="form-row">
44
- <label for="node-input-stepGpio"><i class="fa fa-microchip"></i> STEP GPIO</label>
45
- <input type="text" id="node-input-stepGpio" placeholder="e.g. 0:27" style="width:140px">
42
+ <label for="node-config-input-stepGpio"><i class="fa fa-microchip"></i> STEP GPIO</label>
43
+ <input type="text" id="node-config-input-stepGpio" placeholder="e.g. 0:27" style="width:140px">
46
44
  </div>
47
45
  <div class="form-row">
48
- <label for="node-input-dirGpio"><i class="fa fa-microchip"></i> DIR GPIO</label>
49
- <input type="text" id="node-input-dirGpio" placeholder="e.g. 0:22" style="width:140px">
46
+ <label for="node-config-input-dirGpio"><i class="fa fa-microchip"></i> DIR GPIO</label>
47
+ <input type="text" id="node-config-input-dirGpio" placeholder="e.g. 0:22" style="width:140px">
50
48
  </div>
51
49
  <div class="form-row">
52
- <label for="node-input-enGpio"><i class="fa fa-microchip"></i> ENN GPIO (optional)</label>
53
- <input type="text" id="node-input-enGpio" placeholder="not wired, e.g. 0:17" style="width:140px">
50
+ <label for="node-config-input-enGpio"><i class="fa fa-microchip"></i> ENN GPIO (optional)</label>
51
+ <input type="text" id="node-config-input-enGpio" placeholder="not wired, e.g. 0:17" style="width:140px">
54
52
  </div>
55
53
  <div class="form-row">
56
- <label for="node-input-rsense"><i class="fa fa-bolt"></i> Sense resistor (Ω)</label>
57
- <input type="number" id="node-input-rsense" min="0.01" step="0.01" style="width:100px">
54
+ <label for="node-config-input-rsense"><i class="fa fa-bolt"></i> Sense resistor (Ω)</label>
55
+ <input type="number" id="node-config-input-rsense" min="0.01" step="0.01" style="width:100px">
58
56
  </div>
59
57
  <div class="form-row">
60
- <label for="node-input-current"><i class="fa fa-bolt"></i> Default current (A RMS)</label>
61
- <input type="number" id="node-input-current" min="0" max="1.4" step="0.05" style="width:100px">
58
+ <label for="node-config-input-current"><i class="fa fa-bolt"></i> Default current (A RMS)</label>
59
+ <input type="number" id="node-config-input-current" min="0" max="1.4" step="0.05" style="width:100px">
62
60
  </div>
63
61
  <div class="form-row">
64
- <label for="node-input-microsteps"><i class="fa fa-th"></i> Default microsteps</label>
65
- <select id="node-input-microsteps" style="width:100px">
62
+ <label for="node-config-input-microsteps"><i class="fa fa-th"></i> Default microsteps</label>
63
+ <select id="node-config-input-microsteps" style="width:100px">
66
64
  <option value="1">1</option><option value="2">2</option><option value="4">4</option>
67
65
  <option value="8">8</option><option value="16">16</option><option value="32">32</option>
68
66
  <option value="64">64</option><option value="128">128</option><option value="256">256</option>
69
67
  </select>
70
68
  </div>
71
69
  <div class="form-row">
72
- <label for="node-input-speed"><i class="fa fa-forward"></i> Default speed (full steps/s)</label>
73
- <input type="number" id="node-input-speed" min="1" style="width:100px">
70
+ <label for="node-config-input-speed"><i class="fa fa-forward"></i> Default speed (full steps/s)</label>
71
+ <input type="number" id="node-config-input-speed" min="1" style="width:100px">
72
+ </div>
73
+ </script>
74
+
75
+ <!-- Read node -->
76
+ <script type="text/javascript">
77
+ RED.nodes.registerType('tmc2130', {
78
+ category: 'Peripheral',
79
+ color: '#C084FC',
80
+ defaults: {
81
+ name: { value: '' },
82
+ device: { value: '', type: 'tmc2130-device', required: true }
83
+ },
84
+ inputs: 1,
85
+ outputs: 1,
86
+ icon: 'font-awesome/fa-cog',
87
+ label: function() { return this.name || 'TMC2130'; },
88
+ paletteLabel: 'TMC2130'
89
+ });
90
+ </script>
91
+
92
+ <script type="text/html" data-template-name="tmc2130">
93
+ <div class="form-row">
94
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
95
+ <input type="text" id="node-input-name" placeholder="TMC2130">
96
+ </div>
97
+ <div class="form-row">
98
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
99
+ <input type="text" id="node-input-device">
74
100
  </div>
75
101
  </script>
76
102
 
77
- <script type="text/html" data-help-name="periph-tmc2130">
103
+ <script type="text/html" data-help-name="tmc2130">
78
104
  <p>Drives a two-phase bipolar stepper motor through a Trinamic TMC2130 over SPI (Mode 3). The chip has no internal step generator, so the node pulses the STEP and DIR GPIOs; moves are blocking.</p>
79
105
 
80
106
  <h3>Inputs</h3>
@@ -12,7 +12,7 @@ module.exports = function(RED) {
12
12
  return new GpioOutputPin(Default.output({ chip, line }));
13
13
  }
14
14
 
15
- function TMC2130Node(config) {
15
+ function TMC2130DeviceNode(config) {
16
16
  RED.nodes.createNode(this, config);
17
17
  const node = this;
18
18
  node.ready = false;
@@ -50,35 +50,53 @@ module.exports = function(RED) {
50
50
  };
51
51
  }
52
52
 
53
+ node.on('close', async function(removed, done) {
54
+ try {
55
+ if (node.driver && node.ready) await node.driver.disable();
56
+ if (node.connection) await node.connection.close();
57
+ } catch (e) {
58
+ // bus may already be gone; nothing more to do on close
59
+ } finally {
60
+ done();
61
+ }
62
+ });
63
+ }
64
+ RED.nodes.registerType('tmc2130-device', TMC2130DeviceNode);
65
+
66
+ function TMC2130ReadNode(config) {
67
+ RED.nodes.createNode(this, config);
68
+ const node = this;
69
+ const deviceNode = RED.nodes.getNode(config.device) || {};
70
+
53
71
  node.on('input', async function(msg, send, done) {
54
- if (!node.driver || !node.ready) { done(); return; }
72
+ if (!deviceNode.driver || !deviceNode.ready) { done(); return; }
55
73
  try {
56
74
  const p = msg.payload;
57
75
  let handled = false;
58
76
  if (p && typeof p === 'object') {
59
- if (typeof p.microsteps === 'number') { await node.driver.setMicrosteps(p.microsteps); handled = true; }
60
- if (typeof p.current === 'number') { await node.driver.setCurrent(p.current); handled = true; }
61
- if (typeof p.stall_threshold === 'number') { await node.driver.setStallThreshold(p.stall_threshold); handled = true; }
62
- if (typeof p.spreadcycle === 'boolean') { await node.driver.setChopperMode(p.spreadcycle); handled = true; }
77
+ if (typeof p.microsteps === 'number') { await deviceNode.driver.setMicrosteps(p.microsteps); handled = true; }
78
+ if (typeof p.current === 'number') { await deviceNode.driver.setCurrent(p.current); handled = true; }
79
+ if (typeof p.stall_threshold === 'number') { await deviceNode.driver.setStallThreshold(p.stall_threshold); handled = true; }
80
+ if (typeof p.spreadcycle === 'boolean') { await deviceNode.driver.setChopperMode(p.spreadcycle); handled = true; }
63
81
  if (typeof p.enable === 'boolean') {
64
- if (p.enable) await node.driver.enable(); else await node.driver.disable();
82
+ if (p.enable) await deviceNode.driver.enable(); else await deviceNode.driver.disable();
65
83
  handled = true;
66
84
  }
67
85
  if (typeof p.move === 'number') {
68
- await node.driver.move(p.move, typeof p.speed === 'number' ? p.speed : node.speed);
86
+ await deviceNode.driver.move(p.move, typeof p.speed === 'number' ? p.speed : deviceNode.speed);
69
87
  handled = true;
70
88
  }
71
89
  }
72
90
  if (handled) {
73
91
  msg.payload = await snapshot();
74
92
  } else {
75
- const status = await node.driver.readStatus();
93
+ const status = await deviceNode.driver.readStatus();
76
94
  msg.payload = {
77
95
  standstill: status.stst,
78
- fault: await node.driver.readFault(),
96
+ fault: await deviceNode.driver.readFault(),
79
97
  cs_actual: status.cs_actual,
80
98
  sg_result: status.sg_result,
81
- tstep: await node.driver.readTstep(),
99
+ tstep: await deviceNode.driver.readTstep(),
82
100
  stallguard: status.stallguard,
83
101
  otpw: status.otpw,
84
102
  ot: status.ot,
@@ -90,17 +108,6 @@ module.exports = function(RED) {
90
108
  done(e);
91
109
  }
92
110
  });
93
-
94
- node.on('close', async function(removed, done) {
95
- try {
96
- if (node.driver && node.ready) await node.driver.disable();
97
- if (node.connection) await node.connection.close();
98
- } catch (e) {
99
- // bus may already be gone; nothing more to do on close
100
- } finally {
101
- done();
102
- }
103
- });
104
111
  }
105
- RED.nodes.registerType('periph-tmc2130', TMC2130Node);
112
+ RED.nodes.registerType('tmc2130', TMC2130ReadNode);
106
113
  };
@@ -1,7 +1,7 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-tmc2208', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('tmc2208-device', {
4
+ category: 'config',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  port: { value: '/dev/ttyS0', required: true },
@@ -11,50 +11,76 @@
11
11
  microsteps: { value: '16' },
12
12
  enGpio: { value: '' }
13
13
  },
14
- inputs: 1,
15
- outputs: 1,
16
- icon: 'font-awesome/fa-cog',
17
- label: function() { return this.name || 'TMC2208'; },
18
- paletteLabel: 'TMC2208'
14
+ label: function() {
15
+ return this.name || 'TMC2208';
16
+ }
19
17
  });
20
18
  </script>
21
19
 
22
- <script type="text/html" data-template-name="periph-tmc2208">
20
+ <script type="text/html" data-template-name="tmc2208-device">
23
21
  <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="TMC2208">
22
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
23
+ <input type="text" id="node-config-input-name" placeholder="TMC2208">
26
24
  </div>
27
25
  <div class="form-row">
28
- <label for="node-input-port"><i class="fa fa-plug"></i> Serial port</label>
29
- <input type="text" id="node-input-port" placeholder="/dev/ttyS0">
26
+ <label for="node-config-input-port"><i class="fa fa-plug"></i> Serial port</label>
27
+ <input type="text" id="node-config-input-port" placeholder="/dev/ttyS0">
30
28
  </div>
31
29
  <div class="form-row">
32
- <label for="node-input-baudrate"><i class="fa fa-tachometer"></i> Baud rate</label>
33
- <input type="number" id="node-input-baudrate" min="9000" style="width:100px">
30
+ <label for="node-config-input-baudrate"><i class="fa fa-tachometer"></i> Baud rate</label>
31
+ <input type="number" id="node-config-input-baudrate" min="9000" style="width:100px">
34
32
  </div>
35
33
  <div class="form-row">
36
- <label for="node-input-rsense"><i class="fa fa-bolt"></i> Sense resistor (Ω)</label>
37
- <input type="number" id="node-input-rsense" min="0.01" step="0.01" style="width:100px">
34
+ <label for="node-config-input-rsense"><i class="fa fa-bolt"></i> Sense resistor (Ω)</label>
35
+ <input type="number" id="node-config-input-rsense" min="0.01" step="0.01" style="width:100px">
38
36
  </div>
39
37
  <div class="form-row">
40
- <label for="node-input-current"><i class="fa fa-bolt"></i> Default current (A RMS)</label>
41
- <input type="number" id="node-input-current" min="0" max="1.2" step="0.05" style="width:100px">
38
+ <label for="node-config-input-current"><i class="fa fa-bolt"></i> Default current (A RMS)</label>
39
+ <input type="number" id="node-config-input-current" min="0" max="1.2" step="0.05" style="width:100px">
42
40
  </div>
43
41
  <div class="form-row">
44
- <label for="node-input-microsteps"><i class="fa fa-th"></i> Default microsteps</label>
45
- <select id="node-input-microsteps" style="width:100px">
42
+ <label for="node-config-input-microsteps"><i class="fa fa-th"></i> Default microsteps</label>
43
+ <select id="node-config-input-microsteps" style="width:100px">
46
44
  <option value="1">1</option><option value="2">2</option><option value="4">4</option>
47
45
  <option value="8">8</option><option value="16">16</option><option value="32">32</option>
48
46
  <option value="64">64</option><option value="128">128</option><option value="256">256</option>
49
47
  </select>
50
48
  </div>
51
49
  <div class="form-row">
52
- <label for="node-input-enGpio"><i class="fa fa-microchip"></i> ENN GPIO (optional)</label>
53
- <input type="text" id="node-input-enGpio" placeholder="not wired, e.g. 0:17" style="width:140px">
50
+ <label for="node-config-input-enGpio"><i class="fa fa-microchip"></i> ENN GPIO (optional)</label>
51
+ <input type="text" id="node-config-input-enGpio" placeholder="not wired, e.g. 0:17" style="width:140px">
52
+ </div>
53
+ </script>
54
+
55
+ <!-- Read node -->
56
+ <script type="text/javascript">
57
+ RED.nodes.registerType('tmc2208', {
58
+ category: 'Peripheral',
59
+ color: '#C084FC',
60
+ defaults: {
61
+ name: { value: '' },
62
+ device: { value: '', type: 'tmc2208-device', required: true }
63
+ },
64
+ inputs: 1,
65
+ outputs: 1,
66
+ icon: 'font-awesome/fa-cog',
67
+ label: function() { return this.name || 'TMC2208'; },
68
+ paletteLabel: 'TMC2208'
69
+ });
70
+ </script>
71
+
72
+ <script type="text/html" data-template-name="tmc2208">
73
+ <div class="form-row">
74
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
75
+ <input type="text" id="node-input-name" placeholder="TMC2208">
76
+ </div>
77
+ <div class="form-row">
78
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
79
+ <input type="text" id="node-input-device">
54
80
  </div>
55
81
  </script>
56
82
 
57
- <script type="text/html" data-help-name="periph-tmc2208">
83
+ <script type="text/html" data-help-name="tmc2208">
58
84
  <p>Drives a two-phase bipolar stepper motor through a Trinamic TMC2202 / TMC2208 / TMC2224 over its single-wire UART. The chip's internal step generator moves the motor, so no STEP pulses are needed.</p>
59
85
 
60
86
  <h3>Inputs</h3>
@@ -4,7 +4,7 @@ module.exports = function(RED) {
4
4
  const { UARTConnection } = require('periph/src/connection/uart');
5
5
  const { TMC2208Full } = require('periph/src/chips/motor/tmc2208');
6
6
 
7
- function TMC2208Node(config) {
7
+ function TMC2208DeviceNode(config) {
8
8
  RED.nodes.createNode(this, config);
9
9
  const node = this;
10
10
  node.ready = false;
@@ -52,33 +52,54 @@ module.exports = function(RED) {
52
52
  }, extra);
53
53
  }
54
54
 
55
+ node.on('close', async function(removed, done) {
56
+ try {
57
+ if (node.driver && node.ready) {
58
+ await node.driver.stop();
59
+ await node.driver.disable();
60
+ }
61
+ if (node.connection) await node.connection.close();
62
+ } catch (e) {
63
+ // port may already be gone; nothing more to do on close
64
+ } finally {
65
+ done();
66
+ }
67
+ });
68
+ }
69
+ RED.nodes.registerType('tmc2208-device', TMC2208DeviceNode);
70
+
71
+ function TMC2208ReadNode(config) {
72
+ RED.nodes.createNode(this, config);
73
+ const node = this;
74
+ const deviceNode = RED.nodes.getNode(config.device) || {};
75
+
55
76
  node.on('input', async function(msg, send, done) {
56
- if (!node.driver || !node.ready) { done(); return; }
77
+ if (!deviceNode.driver || !deviceNode.ready) { done(); return; }
57
78
  try {
58
79
  const p = msg.payload;
59
80
  let handled = false;
60
81
  let extra = {};
61
82
  if (p && typeof p === 'object') {
62
- if (typeof p.microsteps === 'number') { await node.driver.setMicrosteps(p.microsteps); handled = true; }
63
- if (typeof p.current === 'number') { await node.driver.setCurrent(p.current); handled = true; }
83
+ if (typeof p.microsteps === 'number') { await deviceNode.driver.setMicrosteps(p.microsteps); handled = true; }
84
+ if (typeof p.current === 'number') { await deviceNode.driver.setCurrent(p.current); handled = true; }
64
85
  if (typeof p.enable === 'boolean') {
65
- if (p.enable) await node.driver.enable(); else await node.driver.disable();
86
+ if (p.enable) await deviceNode.driver.enable(); else await deviceNode.driver.disable();
66
87
  handled = true;
67
88
  }
68
- if (p.stop === true) { await node.driver.stop(); extra.speed = 0; handled = true; }
69
- if (typeof p.speed === 'number') { await node.driver.run(p.speed); extra.speed = p.speed; handled = true; }
89
+ if (p.stop === true) { await deviceNode.driver.stop(); extra.speed = 0; handled = true; }
90
+ if (typeof p.speed === 'number') { await deviceNode.driver.run(p.speed); extra.speed = p.speed; handled = true; }
70
91
  }
71
92
  if (handled) {
72
93
  msg.payload = await snapshot(extra);
73
94
  } else {
74
- const status = await node.driver.readStatus();
95
+ const status = await deviceNode.driver.readStatus();
75
96
  msg.payload = {
76
97
  standstill: status.stst,
77
98
  stealth: status.stealth,
78
- fault: await node.driver.readFault(),
99
+ fault: await deviceNode.driver.readFault(),
79
100
  cs_actual: status.cs_actual,
80
- tstep: await node.driver.readTstep(),
81
- temperature_flags: await node.driver.readTemperatureFlags(),
101
+ tstep: await deviceNode.driver.readTstep(),
102
+ temperature_flags: await deviceNode.driver.readTemperatureFlags(),
82
103
  };
83
104
  }
84
105
  send(msg);
@@ -87,20 +108,6 @@ module.exports = function(RED) {
87
108
  done(e);
88
109
  }
89
110
  });
90
-
91
- node.on('close', async function(removed, done) {
92
- try {
93
- if (node.driver && node.ready) {
94
- await node.driver.stop();
95
- await node.driver.disable();
96
- }
97
- if (node.connection) await node.connection.close();
98
- } catch (e) {
99
- // port may already be gone; nothing more to do on close
100
- } finally {
101
- done();
102
- }
103
- });
104
111
  }
105
- RED.nodes.registerType('periph-tmc2208', TMC2208Node);
112
+ RED.nodes.registerType('tmc2208', TMC2208ReadNode);
106
113
  };