node-red-contrib-periph-other 1.1.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -2,15 +2,24 @@
2
2
 
3
3
  Node-RED nodes for miscellaneous peripheral chips — part of the [Periph](https://github.com/tuhde/Periph) library.
4
4
 
5
- > **Coming soon.** Nodes will be added as chips in this category are implemented.
6
-
7
5
  ## Install
8
6
 
7
+ Open Node-RED, go to **Manage Palette → Install** and search for `node-red-contrib-periph-other`.
8
+
9
+ Or from the command line in your Node-RED user directory:
10
+
9
11
  ```sh
10
12
  npm install node-red-contrib-periph-other
11
13
  ```
12
14
 
15
+ ## Nodes
16
+
17
+ | Node | Kind | Description |
18
+ |------|------|-------------|
19
+ | `mpr121-device` | config | I²C bus and address for a MPR121 |
20
+ | `mpr121` | input | Reads the 12-electrode touch bitmask from an MPR121 capacitive touch sensor over I²C. |
21
+
13
22
  ## Links
14
23
 
15
24
  - [GitHub](https://github.com/tuhde/Periph)
16
- - [All supported chips](https://github.com/tuhde/Periph#supported-chips)
25
+ - [periph JS driver](https://www.npmjs.com/package/periph)
@@ -0,0 +1,73 @@
1
+ [
2
+ {
3
+ "id": "tab-mpr121-demo",
4
+ "type": "tab",
5
+ "label": "MPR121 Capacitive Touch",
6
+ "disabled": false,
7
+ "info": "Polls an MPR121 capacitive touch controller every 100 ms and converts the 12-bit touched bitmask into a human-readable string like 'ELE0 ELE3 ELE7 touched'. A Debug node displays the string. When no electrodes are touched, the function outputs 'none'. Touching multiple electrodes simultaneously is supported (multi-touch bitmask reads). This demonstrates polling-based multi-electrode touch detection on the MPR121."
8
+ },
9
+ {
10
+ "id": "cfg-mpr121",
11
+ "type": "mpr121-device",
12
+ "name": "MPR121",
13
+ "bus": "1",
14
+ "address": "5A"
15
+ },
16
+ {
17
+ "id": "inj-tick",
18
+ "type": "inject",
19
+ "name": "every 100 ms",
20
+ "repeat": "0.1",
21
+ "crontab": "",
22
+ "once": true,
23
+ "onceDelay": 0.1,
24
+ "topic": "",
25
+ "payload": "",
26
+ "payloadType": "date",
27
+ "z": "tab-mpr121-demo",
28
+ "x": 120,
29
+ "y": 160,
30
+ "wires": [["read-mpr121"]]
31
+ },
32
+ {
33
+ "id": "read-mpr121",
34
+ "type": "mpr121",
35
+ "name": "MPR121",
36
+ "device": "cfg-mpr121",
37
+ "z": "tab-mpr121-demo",
38
+ "x": 300,
39
+ "y": 160,
40
+ "wires": [["fmt-touched"]]
41
+ },
42
+ {
43
+ "id": "fmt-touched",
44
+ "type": "function",
45
+ "name": "format touched",
46
+ "func": "const electrodes = msg.payload.electrodes;\nconst names = electrodes\n .map((on, i) => on ? 'ELE' + i : null)\n .filter(s => s);\nmsg.payload = names.length ? names.join(' ') + ' touched' : 'none';\nreturn msg;",
47
+ "outputs": 1,
48
+ "noerr": 0,
49
+ "initialize": "",
50
+ "finalize": "",
51
+ "libs": [],
52
+ "x": 480,
53
+ "y": 160,
54
+ "wires": [["dbg-touched"]]
55
+ },
56
+ {
57
+ "id": "dbg-touched",
58
+ "type": "debug",
59
+ "name": "touched",
60
+ "active": true,
61
+ "tosidebar": true,
62
+ "console": false,
63
+ "tostatus": false,
64
+ "complete": "payload",
65
+ "targetType": "msg",
66
+ "statusVal": "",
67
+ "statusType": "auto",
68
+ "z": "tab-mpr121-demo",
69
+ "x": 660,
70
+ "y": 160,
71
+ "wires": []
72
+ }
73
+ ]
@@ -0,0 +1,74 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('mpr121-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '5A', required: true }
9
+ },
10
+ label: function() {
11
+ return this.name || ('MPR121 @ 0x' + this.address + ' (bus ' + this.bus + ')');
12
+ }
13
+ });
14
+ </script>
15
+
16
+ <script type="text/html" data-template-name="mpr121-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="MPR121">
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="5A" style="width:80px"> &nbsp;default: 5A (0x5A)
28
+ </div>
29
+ </script>
30
+
31
+ <!-- Read node: trigger -> touch bitmask -->
32
+ <script type="text/javascript">
33
+ RED.nodes.registerType('mpr121', {
34
+ category: 'Periph',
35
+ color: '#C084FC',
36
+ defaults: {
37
+ name: { value: '' },
38
+ device: { value: '', type: 'mpr121-device', required: true }
39
+ },
40
+ inputs: 1,
41
+ outputs: 1,
42
+ icon: 'font-awesome/fa-hand-pointer-o',
43
+ label: function() { return this.name || 'MPR121'; },
44
+ paletteLabel: 'MPR121'
45
+ });
46
+ </script>
47
+
48
+ <script type="text/html" data-template-name="mpr121">
49
+ <div class="form-row">
50
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
51
+ <input type="text" id="node-input-device">
52
+ </div>
53
+ <div class="form-row">
54
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
55
+ <input type="text" id="node-input-name" placeholder="MPR121">
56
+ </div>
57
+ </script>
58
+
59
+ <script type="text/html" data-help-name="mpr121">
60
+ <p>Reads the 12-electrode touch bitmask from an MPR121 capacitive touch sensor over I²C.</p>
61
+ <p>Any input message triggers a measurement. The device is configured via an <b>mpr121-device</b> config node.</p>
62
+
63
+ <h3>Output</h3>
64
+ <dl class="message-properties">
65
+ <dt>payload.touched <span class="property-type">number</span></dt>
66
+ <dd>12-bit bitmask; bit n = 1 if ELEn is currently touched (ELE0 at LSB).</dd>
67
+ <dt>payload.electrodes <span class="property-type">boolean[12]</span></dt>
68
+ <dd>12-element array of booleans, one per electrode (ELE0..ELE11).</dd>
69
+ </dl>
70
+
71
+ <h3>Configuration</h3>
72
+ <p>Set the I²C bus number and the chip's 7-bit address (default <code>0x5A</code>) in the device config node.
73
+ The driver uses default touch/release thresholds (12 / 6) and runs autoconfiguration on the chip's first measurement.</p>
74
+ </script>
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { MPR121Minimal } = require('periph/src/chips/other/mpr121');
6
+
7
+ function MPR121DeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const bus = parseInt(config.bus);
12
+ const addr = parseInt(config.address, 16);
13
+ const connection = new I2CConnection(bus, addr);
14
+ node.driver = new MPR121Minimal(connection);
15
+ node.connection = connection;
16
+ } catch (e) {
17
+ node.error('MPR121 init failed: ' + e.message);
18
+ }
19
+ node.on('close', function() {
20
+ if (node.connection) node.connection.close();
21
+ });
22
+ }
23
+ RED.nodes.registerType('mpr121-device', MPR121DeviceNode);
24
+
25
+ function MPR121ReadNode(config) {
26
+ RED.nodes.createNode(this, config);
27
+ const node = this;
28
+ node.device = RED.nodes.getNode(config.device);
29
+
30
+ node.on('input', async function(msg, send, done) {
31
+ if (!node.device || !node.device.driver) {
32
+ node.error('No MPR121 device configured', msg);
33
+ done();
34
+ return;
35
+ }
36
+ try {
37
+ const d = node.device.driver;
38
+ const touched = await d.touched();
39
+ const electrodes = [];
40
+ for (let i = 0; i < 12; i++) electrodes.push((touched & (1 << i)) !== 0);
41
+ msg.payload = { touched, electrodes };
42
+ send(msg);
43
+ done();
44
+ } catch (e) {
45
+ done(e);
46
+ }
47
+ });
48
+ }
49
+ RED.nodes.registerType('mpr121', MPR121ReadNode);
50
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-other",
3
- "version": "1.1.0",
3
+ "version": "1.2.1",
4
4
  "description": "Node-RED nodes for miscellaneous peripheral chips",
5
5
  "keywords": [
6
6
  "node-red",
@@ -11,7 +11,9 @@
11
11
  "peripheral"
12
12
  ],
13
13
  "node-red": {
14
- "nodes": {}
14
+ "nodes": {
15
+ "mpr121": "nodes/mpr121/mpr121.js"
16
+ }
15
17
  },
16
18
  "dependencies": {
17
19
  "periph": "*"