node-red-contrib-periph-imu 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,18 +1,40 @@
1
- <script type="text/html" data-template-name="periph-mpu9250">
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('mpu9250-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ addr: { value: '0x68', required: true },
9
+ gyroFs: { value: '0' },
10
+ accelFs: { value: '0' },
11
+ enableMag: { value: false }
12
+ },
13
+ label: function() {
14
+ return this.name || ('MPU9250 @ ' + this.addr + ' (bus ' + this.bus + ')');
15
+ }
16
+ });
17
+ </script>
18
+
19
+ <script type="text/html" data-template-name="mpu9250-device">
20
+ <div class="form-row">
21
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
22
+ <input type="text" id="node-config-input-name" placeholder="MPU9250">
23
+ </div>
2
24
  <div class="form-row">
3
- <label for="node-input-connection"><i class="fa fa-link"></i> I2C Connection</label>
4
- <input type="text" id="node-input-connection" placeholder="Select I2C connection node">
25
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
26
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
5
27
  </div>
6
28
  <div class="form-row">
7
- <label for="node-input-addr"><i class="fa fa-tag"></i> I2C Address</label>
8
- <select id="node-input-addr">
29
+ <label for="node-config-input-addr">I²C address</label>
30
+ <select id="node-config-input-addr" style="width:100px">
9
31
  <option value="0x68">0x68 (AD0 = GND)</option>
10
32
  <option value="0x69">0x69 (AD0 = VCC)</option>
11
33
  </select>
12
34
  </div>
13
35
  <div class="form-row">
14
- <label for="node-input-gyroFs"><i class="fa fa-cog"></i> Gyro Full-Scale</label>
15
- <select id="node-input-gyroFs">
36
+ <label for="node-config-input-gyroFs"><i class="fa fa-cog"></i> Gyro full scale</label>
37
+ <select id="node-config-input-gyroFs">
16
38
  <option value="0">±250 dps</option>
17
39
  <option value="1">±500 dps</option>
18
40
  <option value="2">±1000 dps</option>
@@ -20,8 +42,8 @@
20
42
  </select>
21
43
  </div>
22
44
  <div class="form-row">
23
- <label for="node-input-accelFs"><i class="fa fa-cog"></i> Accel Full-Scale</label>
24
- <select id="node-input-accelFs">
45
+ <label for="node-config-input-accelFs"><i class="fa fa-cog"></i> Accel full scale</label>
46
+ <select id="node-config-input-accelFs">
25
47
  <option value="0">±2 g</option>
26
48
  <option value="1">±4 g</option>
27
49
  <option value="2">±8 g</option>
@@ -29,12 +51,40 @@
29
51
  </select>
30
52
  </div>
31
53
  <div class="form-row">
32
- <label for="node-input-enableMag"><i class="fa fa-magnet"></i> Enable Magnetometer</label>
33
- <input type="checkbox" id="node-input-enableMag">
54
+ <label for="node-config-input-enableMag"><i class="fa fa-magnet"></i> Magnetometer</label>
55
+ <input type="checkbox" id="node-config-input-enableMag" style="width:auto">
56
+ </div>
57
+ </script>
58
+
59
+ <!-- Read node -->
60
+ <script type="text/javascript">
61
+ RED.nodes.registerType('mpu9250', {
62
+ category: 'Peripheral',
63
+ color: '#C084FC',
64
+ defaults: {
65
+ name: { value: '' },
66
+ device: { value: '', type: 'mpu9250-device', required: true }
67
+ },
68
+ inputs: 1,
69
+ outputs: 1,
70
+ icon: 'font-awesome/fa-compass',
71
+ label: function() { return this.name || 'MPU9250'; },
72
+ paletteLabel: 'MPU9250'
73
+ });
74
+ </script>
75
+
76
+ <script type="text/html" data-template-name="mpu9250">
77
+ <div class="form-row">
78
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
79
+ <input type="text" id="node-input-name" placeholder="MPU9250">
80
+ </div>
81
+ <div class="form-row">
82
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
83
+ <input type="text" id="node-input-device">
34
84
  </div>
35
85
  </script>
36
86
 
37
- <script type="text/html" data-help-name="periph-mpu9250">
87
+ <script type="text/html" data-help-name="mpu9250">
38
88
  <p>Reads bus voltage, load current, and power from an MPU-9250 9-axis MotionTracking device.</p>
39
89
  <h3>Inputs</h3>
40
90
  <dl class="message-properties">
@@ -55,9 +105,9 @@
55
105
  <dd>Y angular rate in rad/s.</dd>
56
106
  <dt>payload.gz <span class="property-type">number</span></dt>
57
107
  <dd>Z angular rate in rad/s.</dd>
108
+ <dt>payload.mx / my / mz <span class="property-type">number</span></dt>
109
+ <dd>Magnetic field in µT (only when the magnetometer is enabled).</dd>
58
110
  </dl>
59
111
  <h3>Configuration</h3>
60
- <p>Select the I²C connection node, the chip's 7-bit address (default <code>0x68</code>),
61
- the gyroscope full-scale range (±250/±500/±1000/±2000 dps), the accelerometer full-scale range (±2/±4/±8/±16 g),
62
- and whether to enable the magnetometer.</p>
112
+ <p>Bus, address, ranges and magnetometer are set in the <b>mpu9250-device</b> config node. Default address is <code>0x68</code>.</p>
63
113
  </script>
@@ -1,30 +1,63 @@
1
1
  'use strict';
2
2
 
3
3
  module.exports = function(RED) {
4
- function MPU9250Node(config) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { MPU9250Full } = require('periph/src/chips/imu/mpu9250');
6
+
7
+ function MPU9250DeviceNode(config) {
5
8
  RED.nodes.createNode(this, config);
6
9
  const node = this;
10
+ node.enableMag = !!config.enableMag;
11
+ try {
12
+ const bus = parseInt(config.bus, 10);
13
+ const connection = new I2CConnection(bus, parseInt(config.addr, 16) || 0x68);
14
+ // The AK8963 magnetometer sits behind the bypass mux at 0x0C and needs its own connection.
15
+ const magConnection = node.enableMag ? new I2CConnection(bus, 0x0C) : null;
16
+ node.connection = connection;
17
+ node.magConnection = magConnection;
18
+ node.driver = new MPU9250Full(connection, magConnection);
19
+ node.ready = (async () => {
20
+ await node.driver.configureGyro(parseInt(config.gyroFs, 10) || 0);
21
+ await node.driver.configureAccel(parseInt(config.accelFs, 10) || 0);
22
+ if (node.enableMag) await node.driver.enableMag();
7
23
 
8
- const connection = RED.nodes.getNode(config.connection);
9
- if (!connection) {
10
- node.error('No I2C connection configured');
11
- return;
24
+ })();
25
+ node.ready.catch((e) => node.error('MPU9250 init failed: ' + e.message));
26
+ } catch (e) {
27
+ node.error('MPU9250 init failed: ' + e.message);
12
28
  }
29
+ node.on('close', function() {
30
+ if (node.connection) node.connection.close();
31
+ if (node.magConnection) node.magConnection.close();
32
+ });
33
+ }
34
+ RED.nodes.registerType('mpu9250-device', MPU9250DeviceNode);
13
35
 
14
- const { MPU9250Minimal } = require('periph/src/chips/imu/mpu9250');
15
- const sensor = new MPU9250Minimal(connection);
36
+ function MPU9250ReadNode(config) {
37
+ RED.nodes.createNode(this, config);
38
+ const node = this;
39
+ const deviceNode = RED.nodes.getNode(config.device) || {};
16
40
 
17
- this.on('input', async function(msg) {
41
+ node.on('input', async function(msg, send, done) {
42
+ if (!deviceNode.driver) { done(new Error('No MPU9250 device configured')); return; }
18
43
  try {
19
- const [ax, ay, az] = await sensor.accel();
20
- const [gx, gy, gz] = await sensor.gyro();
21
- msg.payload = { ax, ay, az, gx, gy, gz };
22
- node.send(msg);
23
- } catch (err) {
24
- node.error('MPU9250 read failed: ' + err.message, msg);
44
+ await deviceNode.ready;
45
+ const d = deviceNode.driver;
46
+ const [ax, ay, az] = await d.accel();
47
+ const [gx, gy, gz] = await d.gyro();
48
+ const out = { ax, ay, az, gx, gy, gz };
49
+ if (deviceNode.enableMag) {
50
+ const [mx, my, mz] = await d.mag();
51
+ Object.assign(out, { mx, my, mz });
52
+ }
53
+
54
+ msg.payload = out;
55
+ send(msg);
56
+ done();
57
+ } catch (e) {
58
+ done(e);
25
59
  }
26
60
  });
27
61
  }
28
-
29
- RED.nodes.registerType('periph-mpu9250', MPU9250Node);
30
- };
62
+ RED.nodes.registerType('mpu9250', MPU9250ReadNode);
63
+ };
@@ -1,18 +1,42 @@
1
- <script type="text/html" data-template-name="periph-mpu9255">
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('mpu9255-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ addr: { value: '0x68', required: true },
9
+ gyroFs: { value: '0' },
10
+ accelFs: { value: '0' },
11
+ enableMag: { value: false },
12
+ enableWom: { value: false },
13
+ womThreshold: { value: '64', validate: RED.validators.number() }
14
+ },
15
+ label: function() {
16
+ return this.name || ('MPU9255 @ ' + this.addr + ' (bus ' + this.bus + ')');
17
+ }
18
+ });
19
+ </script>
20
+
21
+ <script type="text/html" data-template-name="mpu9255-device">
22
+ <div class="form-row">
23
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
24
+ <input type="text" id="node-config-input-name" placeholder="MPU9255">
25
+ </div>
2
26
  <div class="form-row">
3
- <label for="node-input-connection"><i class="fa fa-link"></i> I2C Connection</label>
4
- <input type="text" id="node-input-connection" placeholder="Select I2C connection node">
27
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
28
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
5
29
  </div>
6
30
  <div class="form-row">
7
- <label for="node-input-addr"><i class="fa fa-tag"></i> I2C Address</label>
8
- <select id="node-input-addr">
31
+ <label for="node-config-input-addr">I²C address</label>
32
+ <select id="node-config-input-addr" style="width:100px">
9
33
  <option value="0x68">0x68 (AD0 = GND)</option>
10
34
  <option value="0x69">0x69 (AD0 = VCC)</option>
11
35
  </select>
12
36
  </div>
13
37
  <div class="form-row">
14
- <label for="node-input-gyroFs"><i class="fa fa-cog"></i> Gyro Full-Scale</label>
15
- <select id="node-input-gyroFs">
38
+ <label for="node-config-input-gyroFs"><i class="fa fa-cog"></i> Gyro full scale</label>
39
+ <select id="node-config-input-gyroFs">
16
40
  <option value="0">±250 dps</option>
17
41
  <option value="1">±500 dps</option>
18
42
  <option value="2">±1000 dps</option>
@@ -20,8 +44,8 @@
20
44
  </select>
21
45
  </div>
22
46
  <div class="form-row">
23
- <label for="node-input-accelFs"><i class="fa fa-cog"></i> Accel Full-Scale</label>
24
- <select id="node-input-accelFs">
47
+ <label for="node-config-input-accelFs"><i class="fa fa-cog"></i> Accel full scale</label>
48
+ <select id="node-config-input-accelFs">
25
49
  <option value="0">±2 g</option>
26
50
  <option value="1">±4 g</option>
27
51
  <option value="2">±8 g</option>
@@ -29,20 +53,48 @@
29
53
  </select>
30
54
  </div>
31
55
  <div class="form-row">
32
- <label for="node-input-enableMag"><i class="fa fa-magnet"></i> Enable Magnetometer</label>
33
- <input type="checkbox" id="node-input-enableMag">
56
+ <label for="node-config-input-enableMag"><i class="fa fa-magnet"></i> Magnetometer</label>
57
+ <input type="checkbox" id="node-config-input-enableMag" style="width:auto">
58
+ </div>
59
+ <div class="form-row">
60
+ <label for="node-config-input-enableWom"><i class="fa fa-bell"></i> Wake-on-Motion</label>
61
+ <input type="checkbox" id="node-config-input-enableWom" style="width:auto">
34
62
  </div>
35
63
  <div class="form-row">
36
- <label for="node-input-enableWom"><i class="fa fa-bell"></i> Enable Wake-on-Motion</label>
37
- <input type="checkbox" id="node-input-enableWom">
64
+ <label for="node-config-input-womThreshold"><i class="fa fa-cog"></i> WoM threshold (mg)</label>
65
+ <input type="number" id="node-config-input-womThreshold" min="4" max="1020" step="4" style="width:80px">
66
+ </div>
67
+ </script>
68
+
69
+ <!-- Read node -->
70
+ <script type="text/javascript">
71
+ RED.nodes.registerType('mpu9255', {
72
+ category: 'Peripheral',
73
+ color: '#C084FC',
74
+ defaults: {
75
+ name: { value: '' },
76
+ device: { value: '', type: 'mpu9255-device', required: true }
77
+ },
78
+ inputs: 1,
79
+ outputs: 1,
80
+ icon: 'font-awesome/fa-compass',
81
+ label: function() { return this.name || 'MPU9255'; },
82
+ paletteLabel: 'MPU9255'
83
+ });
84
+ </script>
85
+
86
+ <script type="text/html" data-template-name="mpu9255">
87
+ <div class="form-row">
88
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
89
+ <input type="text" id="node-input-name" placeholder="MPU9255">
38
90
  </div>
39
91
  <div class="form-row">
40
- <label for="node-input-womThreshold"><i class="fa fa-cog"></i> WoM Threshold (mg)</label>
41
- <input type="number" id="node-input-womThreshold" min="4" max="1020" step="4" value="64">
92
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
93
+ <input type="text" id="node-input-device">
42
94
  </div>
43
95
  </script>
44
96
 
45
- <script type="text/html" data-help-name="periph-mpu9255">
97
+ <script type="text/html" data-help-name="mpu9255">
46
98
  <p>Reads acceleration and angular rate from an MPU-9255 9-axis MotionTracking device.</p>
47
99
  <h3>Inputs</h3>
48
100
  <dl class="message-properties">
@@ -63,12 +115,11 @@
63
115
  <dd>Y angular rate in rad/s.</dd>
64
116
  <dt>payload.gz <span class="property-type">number</span></dt>
65
117
  <dd>Z angular rate in rad/s.</dd>
118
+ <dt>payload.mx / my / mz <span class="property-type">number</span></dt>
119
+ <dd>Magnetic field in µT (only when the magnetometer is enabled).</dd>
66
120
  <dt>payload.motion <span class="property-type">boolean</span></dt>
67
121
  <dd>True when wake-on-motion fires (only set when WoM is enabled).</dd>
68
122
  </dl>
69
123
  <h3>Configuration</h3>
70
- <p>Select the I²C connection node, the chip's 7-bit address (default <code>0x68</code>),
71
- the gyroscope full-scale range (±250/±500/±1000/±2000 dps), the accelerometer full-scale range (±2/±4/±8/±16 g),
72
- whether to enable the magnetometer (requires the AK8963 connection wired separately),
73
- and whether to enable wake-on-motion with a configurable threshold in mg.</p>
124
+ <p>Bus, address, ranges, magnetometer and wake-on-motion are set in the <b>mpu9255-device</b> config node. Default address is <code>0x68</code>.</p>
74
125
  </script>
@@ -1,58 +1,64 @@
1
1
  'use strict';
2
2
 
3
3
  module.exports = function(RED) {
4
- function MPU9255Node(config) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { MPU9255Full } = require('periph/src/chips/imu/mpu9255');
6
+
7
+ function MPU9255DeviceNode(config) {
5
8
  RED.nodes.createNode(this, config);
6
9
  const node = this;
7
-
8
- const connection = RED.nodes.getNode(config.connection);
9
- if (!connection) {
10
- node.error('No I2C connection configured');
11
- return;
12
- }
13
-
14
- const enableMag = !!config.enableMag;
15
- const enableWom = !!config.enableWom;
16
- const womThreshold = parseInt(config.womThreshold, 10) || 64;
17
-
18
- let sensor;
10
+ node.enableMag = !!config.enableMag;
11
+ node.enableWom = !!config.enableWom;
19
12
  try {
20
- if (enableMag) {
21
- const { MPU9255Full } = require('periph/src/chips/imu/mpu9255');
22
- sensor = new MPU9255Full(connection, null);
23
- // Note: the magnetometer (AK8963 at 0x0C) lives on its own
24
- // connection bound to that address; this Minimal-only node
25
- // does not wire it. For full mag support, use the Full API
26
- // directly via a function node.
27
- } else {
28
- const { MPU9255Minimal } = require('periph/src/chips/imu/mpu9255');
29
- sensor = new MPU9255Minimal(connection);
30
- }
31
- if (enableWom) {
32
- sensor.configure_wake_on_motion && sensor.configure_wake_on_motion(womThreshold, 31.25);
33
- }
34
- } catch (err) {
35
- node.error('MPU9255 init failed: ' + err.message);
36
- return;
13
+ const bus = parseInt(config.bus, 10);
14
+ const connection = new I2CConnection(bus, parseInt(config.addr, 16) || 0x68);
15
+ // The AK8963 magnetometer sits behind the bypass mux at 0x0C and needs its own connection.
16
+ const magConnection = node.enableMag ? new I2CConnection(bus, 0x0C) : null;
17
+ node.connection = connection;
18
+ node.magConnection = magConnection;
19
+ node.driver = new MPU9255Full(connection, magConnection);
20
+ node.ready = (async () => {
21
+ await node.driver.configureGyro(parseInt(config.gyroFs, 10) || 0);
22
+ await node.driver.configureAccel(parseInt(config.accelFs, 10) || 0);
23
+ if (node.enableMag) await node.driver.enableMag();
24
+ if (node.enableWom) await node.driver.configureWakeOnMotion(parseInt(config.womThreshold, 10) || 64);
25
+ })();
26
+ node.ready.catch((e) => node.error('MPU9255 init failed: ' + e.message));
27
+ } catch (e) {
28
+ node.error('MPU9255 init failed: ' + e.message);
37
29
  }
30
+ node.on('close', function() {
31
+ if (node.connection) node.connection.close();
32
+ if (node.magConnection) node.magConnection.close();
33
+ });
34
+ }
35
+ RED.nodes.registerType('mpu9255-device', MPU9255DeviceNode);
36
+
37
+ function MPU9255ReadNode(config) {
38
+ RED.nodes.createNode(this, config);
39
+ const node = this;
40
+ const deviceNode = RED.nodes.getNode(config.device) || {};
38
41
 
39
- this.on('input', async function(msg) {
42
+ node.on('input', async function(msg, send, done) {
43
+ if (!deviceNode.driver) { done(new Error('No MPU9255 device configured')); return; }
40
44
  try {
41
- const [ax, ay, az] = await sensor.accel();
42
- const [gx, gy, gz] = await sensor.gyro();
45
+ await deviceNode.ready;
46
+ const d = deviceNode.driver;
47
+ const [ax, ay, az] = await d.accel();
48
+ const [gx, gy, gz] = await d.gyro();
43
49
  const out = { ax, ay, az, gx, gy, gz };
44
- if (enableWom && typeof sensor.motion_detected === 'function') {
45
- if (await sensor.motion_detected()) {
46
- out.motion = true;
47
- }
50
+ if (deviceNode.enableMag) {
51
+ const [mx, my, mz] = await d.mag();
52
+ Object.assign(out, { mx, my, mz });
48
53
  }
54
+ if (deviceNode.enableWom && await d.motionDetected()) out.motion = true;
49
55
  msg.payload = out;
50
- node.send(msg);
51
- } catch (err) {
52
- node.error('MPU9255 read failed: ' + err.message, msg);
56
+ send(msg);
57
+ done();
58
+ } catch (e) {
59
+ done(e);
53
60
  }
54
61
  });
55
62
  }
56
-
57
- RED.nodes.registerType('periph-mpu9255', MPU9255Node);
58
- };
63
+ RED.nodes.registerType('mpu9255', MPU9255ReadNode);
64
+ };
@@ -1,7 +1,7 @@
1
+ <!-- Config node: one instance per physical device -->
1
2
  <script type="text/javascript">
2
- RED.nodes.registerType('periph-qmi8658a', {
3
- category: 'Peripheral',
4
- color: '#C084FC',
3
+ RED.nodes.registerType('qmi8658a-device', {
4
+ category: 'config',
5
5
  defaults: {
6
6
  name: { value: '' },
7
7
  transport: { value: 'i2c', required: true },
@@ -12,55 +12,51 @@
12
12
  gyroRange: { value: '4', required: true, validate: RED.validators.number() },
13
13
  odr: { value: '5', required: true, validate: RED.validators.number() }
14
14
  },
15
- inputs: 1,
16
- outputs: 1,
17
- icon: 'bridge.svg',
18
15
  label: function() {
19
- return this.name || ('QMI8658A ' + (this.transport === 'spi' ? 'SPI ' + this.bus + '.' + this.device
20
- : '@ ' + this.address + ' (bus ' + this.bus + ')'));
16
+ return this.name || ('QMI8658A @ ' + this.address + ' (bus ' + this.bus + ')');
21
17
  },
22
18
  oneditprepare: function() {
23
19
  function toggle() {
24
- var spi = $('#node-input-transport').val() === 'spi';
20
+ var spi = $('#node-config-input-transport').val() === 'spi';
25
21
  $('.qmi-i2c').toggle(!spi);
26
22
  $('.qmi-spi').toggle(spi);
27
23
  }
28
- $('#node-input-transport').on('change', toggle);
24
+ $('#node-config-input-transport').on('change', toggle);
29
25
  toggle();
30
26
  }
31
27
  });
32
28
  </script>
33
29
 
34
- <script type="text/html" data-template-name="periph-qmi8658a">
30
+ <script type="text/html" data-template-name="qmi8658a-device">
35
31
  <div class="form-row">
36
- <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
37
- <input type="text" id="node-input-name" placeholder="QMI8658A">
32
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
33
+ <input type="text" id="node-config-input-name" placeholder="QMI8658A">
38
34
  </div>
39
35
  <div class="form-row">
40
- <label for="node-input-transport"><i class="fa fa-exchange"></i> Transport</label>
41
- <select id="node-input-transport" style="width:120px">
36
+ <label for="node-config-input-transport"><i class="fa fa-exchange"></i> Transport</label>
37
+ <select id="node-config-input-transport" style="width:120px">
42
38
  <option value="i2c" selected>I²C</option>
43
39
  <option value="spi">SPI</option>
44
40
  </select>
45
41
  </div>
46
42
  <div class="form-row">
47
- <label for="node-input-bus"><i class="fa fa-microchip"></i> Bus</label>
48
- <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N or /dev/spidevN.M)
43
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> Bus</label>
44
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N or /dev/spidevN.M)
49
45
  </div>
50
46
  <div class="form-row qmi-spi">
51
- <label for="node-input-device"><i class="fa fa-microchip"></i> SPI device</label>
52
- <input type="number" id="node-input-device" min="0" style="width:80px"> &nbsp;(chip select N in /dev/spidevB.N)
47
+ <label for="node-config-input-device"><i class="fa fa-microchip"></i> SPI device</label>
48
+ <input type="number" id="node-config-input-device" min="0" style="width:80px"> &nbsp;(chip select N in /dev/spidevB.N)
53
49
  </div>
54
50
  <div class="form-row qmi-i2c">
55
- <label for="node-input-address"><i class="fa fa-microchip"></i> I²C address</label>
56
- <select id="node-input-address" style="width:160px">
51
+ <label for="node-config-input-address"><i class="fa fa-microchip"></i> I²C address</label>
52
+ <select id="node-config-input-address" style="width:160px">
57
53
  <option value="0x6B" selected>0x6B (SA0 = GND)</option>
58
54
  <option value="0x6A">0x6A (SA0 = VDDIO)</option>
59
55
  </select>
60
56
  </div>
61
57
  <div class="form-row">
62
- <label for="node-input-accelRange"><i class="fa fa-cog"></i> Accelerometer range</label>
63
- <select id="node-input-accelRange" style="width:120px">
58
+ <label for="node-config-input-accelRange"><i class="fa fa-cog"></i> Accelerometer range</label>
59
+ <select id="node-config-input-accelRange" style="width:120px">
64
60
  <option value="0" selected>±2 g</option>
65
61
  <option value="1">±4 g</option>
66
62
  <option value="2">±8 g</option>
@@ -68,8 +64,8 @@
68
64
  </select>
69
65
  </div>
70
66
  <div class="form-row">
71
- <label for="node-input-gyroRange"><i class="fa fa-cog"></i> Gyroscope range</label>
72
- <select id="node-input-gyroRange" style="width:120px">
67
+ <label for="node-config-input-gyroRange"><i class="fa fa-cog"></i> Gyroscope range</label>
68
+ <select id="node-config-input-gyroRange" style="width:120px">
73
69
  <option value="0">±16 dps</option>
74
70
  <option value="1">±32 dps</option>
75
71
  <option value="2">±64 dps</option>
@@ -81,8 +77,8 @@
81
77
  </select>
82
78
  </div>
83
79
  <div class="form-row">
84
- <label for="node-input-odr"><i class="fa fa-clock-o"></i> Output data rate</label>
85
- <select id="node-input-odr" style="width:120px">
80
+ <label for="node-config-input-odr"><i class="fa fa-clock-o"></i> Output data rate</label>
81
+ <select id="node-config-input-odr" style="width:120px">
86
82
  <option value="8">28 Hz</option>
87
83
  <option value="7">56 Hz</option>
88
84
  <option value="6">112 Hz</option>
@@ -93,7 +89,35 @@
93
89
  </div>
94
90
  </script>
95
91
 
96
- <script type="text/html" data-help-name="periph-qmi8658a">
92
+ <!-- Read node -->
93
+ <script type="text/javascript">
94
+ RED.nodes.registerType('qmi8658a', {
95
+ category: 'Peripheral',
96
+ color: '#C084FC',
97
+ defaults: {
98
+ name: { value: '' },
99
+ device: { value: '', type: 'qmi8658a-device', required: true }
100
+ },
101
+ inputs: 1,
102
+ outputs: 1,
103
+ icon: 'bridge.svg',
104
+ label: function() { return this.name || 'QMI8658A'; },
105
+ paletteLabel: 'QMI8658A'
106
+ });
107
+ </script>
108
+
109
+ <script type="text/html" data-template-name="qmi8658a">
110
+ <div class="form-row">
111
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
112
+ <input type="text" id="node-input-name" placeholder="QMI8658A">
113
+ </div>
114
+ <div class="form-row">
115
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
116
+ <input type="text" id="node-input-device">
117
+ </div>
118
+ </script>
119
+
120
+ <script type="text/html" data-help-name="qmi8658a">
97
121
  <p>Reads acceleration, angular rate and die temperature from a QST QMI8658A 6-axis IMU over I²C (0x6B with
98
122
  SA0 = GND, 0x6A with SA0 = VDDIO) or 4-wire SPI. The three values come from one 14-byte burst, so they belong
99
123
  to the same sample.</p>
@@ -5,7 +5,7 @@ module.exports = function(RED) {
5
5
  const { SPIConnection } = require('periph/src/connection/spi');
6
6
  const { QMI8658AFull } = require('periph/src/chips/imu/qmi8658a');
7
7
 
8
- function QMI8658ANode(config) {
8
+ function QMI8658ADeviceNode(config) {
9
9
  RED.nodes.createNode(this, config);
10
10
  const node = this;
11
11
  try {
@@ -36,15 +36,26 @@ module.exports = function(RED) {
36
36
  node.error('QMI8658A init failed: ' + e.message);
37
37
  }
38
38
 
39
+ node.on('close', function() {
40
+ if (node.connection) node.connection.close();
41
+ });
42
+ }
43
+ RED.nodes.registerType('qmi8658a-device', QMI8658ADeviceNode);
44
+
45
+ function QMI8658AReadNode(config) {
46
+ RED.nodes.createNode(this, config);
47
+ const node = this;
48
+ const deviceNode = RED.nodes.getNode(config.device) || {};
49
+
39
50
  node.on('input', async function(msg, send, done) {
40
- if (!node.driver) {
51
+ if (!deviceNode.driver) {
41
52
  node.error('QMI8658A not initialised', msg);
42
53
  done();
43
54
  return;
44
55
  }
45
56
  try {
46
- await node.ready;
47
- const [accel, gyro, temperature] = await node.driver.readAll();
57
+ await deviceNode.ready;
58
+ const [accel, gyro, temperature] = await deviceNode.driver.readAll();
48
59
  msg.payload = {
49
60
  accel: { x: accel[0], y: accel[1], z: accel[2] },
50
61
  gyro: { x: gyro[0], y: gyro[1], z: gyro[2] },
@@ -56,10 +67,6 @@ module.exports = function(RED) {
56
67
  done(e);
57
68
  }
58
69
  });
59
-
60
- node.on('close', function() {
61
- if (node.connection) node.connection.close();
62
- });
63
70
  }
64
- RED.nodes.registerType('periph-qmi8658a', QMI8658ANode);
71
+ RED.nodes.registerType('qmi8658a', QMI8658AReadNode);
65
72
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-imu",
3
- "version": "1.5.0",
3
+ "version": "1.5.2",
4
4
  "description": "Node-RED nodes for IMU chips",
5
5
  "keywords": [
6
6
  "node-red",