node-red-contrib-periph-memory 1.1.0

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 ADDED
@@ -0,0 +1,16 @@
1
+ # node-red-contrib-periph-memory
2
+
3
+ Node-RED nodes for memory chips — part of the [Periph](https://github.com/tuhde/Periph) library.
4
+
5
+ > **Coming soon.** Nodes will be added as chips in this category are implemented.
6
+
7
+ ## Install
8
+
9
+ ```sh
10
+ npm install node-red-contrib-periph-memory
11
+ ```
12
+
13
+ ## Links
14
+
15
+ - [GitHub](https://github.com/tuhde/Periph)
16
+ - [All supported chips](https://github.com/tuhde/Periph#supported-chips)
File without changes
@@ -0,0 +1,116 @@
1
+ [
2
+ {
3
+ "id": "tab-24aa02uid-demo",
4
+ "type": "tab",
5
+ "label": "24AA02UID UID + Counter",
6
+ "disabled": false,
7
+ "info": "Demonstrates the 24AA02UID's two distinct memory regions. The first branch reads the factory-programmed 32-bit unique serial number every 5 seconds and prints it as a hex string and integer. The second branch writes a 2-byte incrementing counter to user EEPROM address 0x00 on every tick and reads it back, showing the read/write round-trip on the rewritable user EEPROM while the UID never changes."
8
+ },
9
+ {
10
+ "id": "cfg-24aa02uid",
11
+ "type": "24aa02uid-device",
12
+ "name": "24AA02UID",
13
+ "bus": "1",
14
+ "address": "50"
15
+ },
16
+ {
17
+ "id": "inj-tick",
18
+ "type": "inject",
19
+ "name": "every 5 s",
20
+ "repeat": "5",
21
+ "crontab": "",
22
+ "once": true,
23
+ "onceDelay": 0.1,
24
+ "topic": "",
25
+ "payload": "",
26
+ "payloadType": "date",
27
+ "z": "tab-24aa02uid-demo",
28
+ "x": 130,
29
+ "y": 120,
30
+ "wires": [["read-uid", "counter"]]
31
+ },
32
+ {
33
+ "id": "read-uid",
34
+ "type": "24aa02uid",
35
+ "name": "Read UID",
36
+ "device": "cfg-24aa02uid",
37
+ "z": "tab-24aa02uid-demo",
38
+ "x": 320,
39
+ "y": 120,
40
+ "wires": [["dbg-uid"]]
41
+ },
42
+ {
43
+ "id": "dbg-uid",
44
+ "type": "debug",
45
+ "name": "UID",
46
+ "active": true,
47
+ "tosidebar": true,
48
+ "console": false,
49
+ "tostatus": true,
50
+ "complete": "payload",
51
+ "targetType": "msg",
52
+ "statusVal": "payload.uid",
53
+ "statusType": "auto",
54
+ "z": "tab-24aa02uid-demo",
55
+ "x": 490,
56
+ "y": 120,
57
+ "wires": []
58
+ },
59
+ {
60
+ "id": "counter",
61
+ "type": "function",
62
+ "name": "Bump counter",
63
+ "func": "// Read the current 2-byte counter at 0x00, increment, write back.\nconst cur = (flow.get('counter') || 0) + 1;\nflow.set('counter', cur);\nmsg.op = 'write';\nmsg.address = 0;\nmsg.data = [ (cur >> 8) & 0xFF, cur & 0xFF ];\nreturn msg;",
64
+ "outputs": 1,
65
+ "noerr": 0,
66
+ "initialize": "",
67
+ "finalize": "",
68
+ "libs": [],
69
+ "z": "tab-24aa02uid-demo",
70
+ "x": 320,
71
+ "y": 200,
72
+ "wires": [["write-counter"]]
73
+ },
74
+ {
75
+ "id": "write-counter",
76
+ "type": "24aa02uid",
77
+ "name": "Write counter",
78
+ "device": "cfg-24aa02uid",
79
+ "z": "tab-24aa02uid-demo",
80
+ "x": 490,
81
+ "y": 200,
82
+ "wires": [["read-counter"]]
83
+ },
84
+ {
85
+ "id": "read-counter",
86
+ "type": "function",
87
+ "name": "Read counter",
88
+ "func": "msg.op = 'read';\nmsg.address = 0;\nmsg.length = 2;\nreturn msg;",
89
+ "outputs": 1,
90
+ "noerr": 0,
91
+ "initialize": "",
92
+ "finalize": "",
93
+ "libs": [],
94
+ "z": "tab-24aa02uid-demo",
95
+ "x": 660,
96
+ "y": 200,
97
+ "wires": [["dbg-counter"]]
98
+ },
99
+ {
100
+ "id": "dbg-counter",
101
+ "type": "debug",
102
+ "name": "Counter",
103
+ "active": true,
104
+ "tosidebar": true,
105
+ "console": false,
106
+ "tostatus": true,
107
+ "complete": "payload",
108
+ "targetType": "msg",
109
+ "statusVal": "payload.data",
110
+ "statusType": "auto",
111
+ "z": "tab-24aa02uid-demo",
112
+ "x": 830,
113
+ "y": 200,
114
+ "wires": []
115
+ }
116
+ ]
package/index.js ADDED
@@ -0,0 +1,11 @@
1
+ const fs = require('fs');
2
+ const path = require('path');
3
+
4
+ module.exports = function(RED) {
5
+ const nodesDir = path.join(__dirname, 'nodes');
6
+ if (!fs.existsSync(nodesDir)) return;
7
+ fs.readdirSync(nodesDir).forEach(function(chip) {
8
+ const nodeFile = path.join(nodesDir, chip, chip + '.js');
9
+ if (fs.existsSync(nodeFile)) require(nodeFile)(RED);
10
+ });
11
+ };
package/nodes/.gitkeep ADDED
File without changes
@@ -0,0 +1,94 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('24aa02uid-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '50', required: true }
9
+ },
10
+ label: function() {
11
+ return this.name || ('24AA02UID @ 0x' + this.address + ' (bus ' + this.bus + ')');
12
+ }
13
+ });
14
+ </script>
15
+
16
+ <script type="text/html" data-template-name="24aa02uid-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="24AA02UID">
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="50" style="width:80px"> &nbsp;default: 50 (0x50, fixed)
28
+ </div>
29
+ </script>
30
+
31
+ <!-- Read node: trigger → uid / read / write -->
32
+ <script type="text/javascript">
33
+ RED.nodes.registerType('24aa02uid', {
34
+ category: 'Peripheral',
35
+ color: '#a6bbcf',
36
+ defaults: {
37
+ name: { value: '' },
38
+ device: { value: '', type: '24aa02uid-device', required: true }
39
+ },
40
+ inputs: 1,
41
+ outputs: 1,
42
+ icon: 'font-awesome/fa-id-card',
43
+ label: function() { return this.name || '24AA02UID'; },
44
+ paletteLabel: '24AA02UID'
45
+ });
46
+ </script>
47
+
48
+ <script type="text/html" data-template-name="24aa02uid">
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="24AA02UID">
56
+ </div>
57
+ </script>
58
+
59
+ <script type="text/html" data-help-name="24aa02uid">
60
+ <p>Reads the factory-programmed 32-bit unique serial number from a 24AA02UID
61
+ EEPROM, or reads / writes user EEPROM bytes, over I²C.</p>
62
+ <p>Any input message triggers an operation. The device is configured via a
63
+ <b>24aa02uid-device</b> config node.</p>
64
+
65
+ <h3>Inputs</h3>
66
+ <dl class="message-properties">
67
+ <dt>op <span class="property-type">string</span></dt>
68
+ <dd>Operation: <code>read_uid</code> (default), <code>read</code>, or
69
+ <code>write</code>. May be passed in <code>msg.op</code>.</dd>
70
+ <dt>address <span class="property-type">number</span></dt>
71
+ <dd>Memory address for read/write. Ignored for <code>read_uid</code>.</dd>
72
+ <dt>length <span class="property-type">number</span></dt>
73
+ <dd>Number of bytes to read (only for <code>read</code>).</dd>
74
+ <dt>data <span class="property-type">array of numbers</span></dt>
75
+ <dd>Bytes to write (only for <code>write</code>).</dd>
76
+ </dl>
77
+
78
+ <h3>Outputs</h3>
79
+ <dl class="message-properties">
80
+ <dt>payload.uid <span class="property-type">string</span></dt>
81
+ <dd>UID as an 8-character hex string (uppercase). <code>read_uid</code> only.</dd>
82
+ <dt>payload.uid_int <span class="property-type">number</span></dt>
83
+ <dd>UID as a 32-bit unsigned integer (big-endian). <code>read_uid</code> only.</dd>
84
+ <dt>payload.data <span class="property-type">array of numbers</span></dt>
85
+ <dd>Bytes read from the chip. <code>read</code> only.</dd>
86
+ <dt>payload.ok <span class="property-type">boolean</span></dt>
87
+ <dd>True on a successful write. <code>write</code> only.</dd>
88
+ </dl>
89
+
90
+ <h3>Configuration</h3>
91
+ <p>Set the I²C bus number and the chip's fixed address (default
92
+ <code>0x50</code>) in the device config node. The 24AA02UID's address
93
+ pins are not connected — only one device per bus is supported.</p>
94
+ </script>
@@ -0,0 +1,66 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CTransport } = require('periph/src/transport/i2c');
5
+ const { EEPROM24AA02UIDFull } = require('periph/src/chips/memory/_24aa02uid');
6
+
7
+ function Eeprom24AA02UIDDeviceNode(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+ try {
11
+ const transport = new I2CTransport(parseInt(config.bus), parseInt(config.address, 16));
12
+ node.driver = new EEPROM24AA02UIDFull(transport);
13
+ node.transport = transport;
14
+ } catch (e) {
15
+ node.error('24AA02UID init failed: ' + e.message);
16
+ }
17
+ node.on('close', function() {
18
+ if (node.transport) node.transport.close();
19
+ });
20
+ }
21
+ RED.nodes.registerType('24aa02uid-device', Eeprom24AA02UIDDeviceNode);
22
+
23
+ function Eeprom24AA02UIDReadNode(config) {
24
+ RED.nodes.createNode(this, config);
25
+ const node = this;
26
+ node.device = RED.nodes.getNode(config.device);
27
+
28
+ node.on('input', function(msg, send, done) {
29
+ if (!node.device || !node.device.driver) {
30
+ node.error('No 24AA02UID device configured', msg);
31
+ done();
32
+ return;
33
+ }
34
+ try {
35
+ const d = node.device.driver;
36
+ const op = (msg.op || config.op || 'read_uid');
37
+ if (op === 'read_uid') {
38
+ const uid = d.readUid();
39
+ msg.payload = {
40
+ uid: uid.toString('hex').toUpperCase(),
41
+ uid_int: uid.readUInt32BE(0)
42
+ };
43
+ } else if (op === 'read') {
44
+ const address = (msg.address != null) ? msg.address : 0;
45
+ const length = (msg.length != null) ? msg.length : 4;
46
+ const data = d.read(address, length);
47
+ msg.payload = { data: Array.from(data) };
48
+ } else if (op === 'write') {
49
+ const address = (msg.address != null) ? msg.address : 0;
50
+ const data = (msg.data != null) ? msg.data : [];
51
+ d.write(address, Buffer.from(data));
52
+ msg.payload = { ok: true };
53
+ } else {
54
+ node.error('Unknown op: ' + op, msg);
55
+ done();
56
+ return;
57
+ }
58
+ send(msg);
59
+ done();
60
+ } catch (e) {
61
+ done(e);
62
+ }
63
+ });
64
+ }
65
+ RED.nodes.registerType('24aa02uid', Eeprom24AA02UIDReadNode);
66
+ };
package/package.json ADDED
@@ -0,0 +1,23 @@
1
+ {
2
+ "name": "node-red-contrib-periph-memory",
3
+ "version": "1.1.0",
4
+ "description": "Node-RED nodes for memory chips",
5
+ "keywords": [
6
+ "node-red",
7
+ "memory",
8
+ "eeprom",
9
+ "flash",
10
+ "i2c",
11
+ "spi",
12
+ "peripheral"
13
+ ],
14
+ "node-red": {
15
+ "nodes": {
16
+ "24aa02uid": "nodes/24aa02uid/24aa02uid.js"
17
+ }
18
+ },
19
+ "dependencies": {
20
+ "periph": "*"
21
+ },
22
+ "license": "MIT"
23
+ }