node-red-contrib-periph-light 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-light
2
+
3
+ Node-RED nodes for light sensor 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-light
11
+ ```
12
+
13
+ ## Links
14
+
15
+ - [GitHub](https://github.com/tuhde/Periph)
16
+ - [All supported chips](https://github.com/tuhde/Periph#supported-chips)
@@ -0,0 +1,74 @@
1
+ [
2
+ {
3
+ "id": "tab-apds9960-demo",
4
+ "type": "tab",
5
+ "label": "APDS-9960 Light Sensor",
6
+ "disabled": false,
7
+ "info": "Reads ambient light and color (RGBC) from an APDS-9960 every second. A function node computes an approximate lux value and normalizes red/green/blue as percentages of the clear channel. The debug panel shows formatted values. Cover the sensor with a hand to see values drop; expose to bright light to see them rise. Adjust the inject interval or add a chart node for live history."
8
+ },
9
+ {
10
+ "id": "cfg-apds9960",
11
+ "type": "apds9960-device",
12
+ "name": "APDS-9960",
13
+ "bus": "1",
14
+ "address": "39"
15
+ },
16
+ {
17
+ "id": "inj-tick",
18
+ "type": "inject",
19
+ "name": "every 1 s",
20
+ "repeat": "1",
21
+ "crontab": "",
22
+ "once": true,
23
+ "onceDelay": 0.1,
24
+ "topic": "",
25
+ "payload": "",
26
+ "payloadType": "date",
27
+ "z": "tab-apds9960-demo",
28
+ "x": 120,
29
+ "y": 160,
30
+ "wires": [["read-apds9960"]]
31
+ },
32
+ {
33
+ "id": "read-apds9960",
34
+ "type": "apds9960",
35
+ "name": "APDS-9960",
36
+ "device": "cfg-apds9960",
37
+ "z": "tab-apds9960-demo",
38
+ "x": 300,
39
+ "y": 160,
40
+ "wires": [["fmt-readings"]]
41
+ },
42
+ {
43
+ "id": "fmt-readings",
44
+ "type": "function",
45
+ "name": "compute lux",
46
+ "func": "const p = msg.payload;\nconst lux = (-0.32466 * p.red + 1.57837 * p.green + -0.73191 * p.blue).toFixed(0);\nconst rPct = (p.clear > 0 ? (p.red / p.clear * 100).toFixed(1) : '0.0');\nconst gPct = (p.clear > 0 ? (p.green / p.clear * 100).toFixed(1) : '0.0');\nconst bPct = (p.clear > 0 ? (p.blue / p.clear * 100).toFixed(1) : '0.0');\nmsg.payload = 'C=' + p.clear + ' R=' + p.red + '(' + rPct + '%) G=' + p.green + '(' + gPct + '%) B=' + p.blue + '(' + bPct + '%) lux~' + lux;\nreturn msg;",
47
+ "outputs": 1,
48
+ "noerr": 0,
49
+ "initialize": "",
50
+ "finalize": "",
51
+ "libs": [],
52
+ "z": "tab-apds9960-demo",
53
+ "x": 480,
54
+ "y": 160,
55
+ "wires": [["dbg-readings"]]
56
+ },
57
+ {
58
+ "id": "dbg-readings",
59
+ "type": "debug",
60
+ "name": "",
61
+ "active": true,
62
+ "tosidebar": true,
63
+ "console": false,
64
+ "tostatus": true,
65
+ "complete": "payload",
66
+ "targetType": "msg",
67
+ "statusVal": "payload",
68
+ "statusType": "auto",
69
+ "z": "tab-apds9960-demo",
70
+ "x": 650,
71
+ "y": 160,
72
+ "wires": []
73
+ }
74
+ ]
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
+ };
@@ -0,0 +1,78 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('apds9960-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '39', required: true }
9
+ },
10
+ label: function() {
11
+ return this.name || ('APDS-9960 @ 0x' + this.address + ' (bus ' + this.bus + ')');
12
+ }
13
+ });
14
+ </script>
15
+
16
+ <script type="text/html" data-template-name="apds9960-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="APDS-9960">
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="39" style="width:80px"> &nbsp;default: 39 (0x39)
28
+ </div>
29
+ </script>
30
+
31
+ <!-- Read node: trigger → measurements -->
32
+ <script type="text/javascript">
33
+ RED.nodes.registerType('apds9960', {
34
+ category: 'Peripheral',
35
+ color: '#c0d8a0',
36
+ defaults: {
37
+ name: { value: '' },
38
+ device: { value: '', type: 'apds9960-device', required: true }
39
+ },
40
+ inputs: 1,
41
+ outputs: 1,
42
+ icon: 'font-awesome/fa-sun-o',
43
+ label: function() { return this.name || 'APDS-9960'; },
44
+ paletteLabel: 'APDS-9960'
45
+ });
46
+ </script>
47
+
48
+ <script type="text/html" data-template-name="apds9960">
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="APDS-9960">
56
+ </div>
57
+ </script>
58
+
59
+ <script type="text/html" data-help-name="apds9960">
60
+ <p>Reads ambient light and color (RGBC) from an APDS-9960 sensor over I²C.</p>
61
+ <p>Any input message triggers a measurement. The device is configured via an <b>apds9960-device</b> config node.</p>
62
+
63
+ <h3>Output</h3>
64
+ <dl class="message-properties">
65
+ <dt>payload.clear <span class="property-type">number</span></dt>
66
+ <dd>Clear (unfiltered) channel count, 0-65535.</dd>
67
+ <dt>payload.red <span class="property-type">number</span></dt>
68
+ <dd>Red channel count, 0-65535.</dd>
69
+ <dt>payload.green <span class="property-type">number</span></dt>
70
+ <dd>Green channel count, 0-65535.</dd>
71
+ <dt>payload.blue <span class="property-type">number</span></dt>
72
+ <dd>Blue channel count, 0-65535.</dd>
73
+ </dl>
74
+
75
+ <h3>Configuration</h3>
76
+ <p>Set the I²C bus number and the chip's 7-bit address (default <code>0x39</code>) in the device config node.
77
+ The driver uses ATIME=0xB6 (~200 ms integration) and AGAIN=4x by default.</p>
78
+ </script>
@@ -0,0 +1,48 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CTransport } = require('periph/src/transport/i2c');
5
+ const { APDS9960Minimal } = require('periph/src/chips/light/apds9960');
6
+
7
+ function APDS9960DeviceNode(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 transport = new I2CTransport(bus, addr);
14
+ node.driver = new APDS9960Minimal(transport);
15
+ node.transport = transport;
16
+ } catch (e) {
17
+ node.error('APDS-9960 init failed: ' + e.message);
18
+ }
19
+ node.on('close', function() {
20
+ if (node.transport) node.transport.close();
21
+ });
22
+ }
23
+ RED.nodes.registerType('apds9960-device', APDS9960DeviceNode);
24
+
25
+ function APDS9960ReadNode(config) {
26
+ RED.nodes.createNode(this, config);
27
+ const node = this;
28
+ node.device = RED.nodes.getNode(config.device);
29
+
30
+ node.on('input', function(msg, send, done) {
31
+ if (!node.device || !node.device.driver) {
32
+ node.error('No APDS-9960 device configured', msg);
33
+ done();
34
+ return;
35
+ }
36
+ try {
37
+ const d = node.device.driver;
38
+ const { clear, red, green, blue } = d.color();
39
+ msg.payload = { clear, red, green, blue };
40
+ send(msg);
41
+ done();
42
+ } catch (e) {
43
+ done(e);
44
+ }
45
+ });
46
+ }
47
+ RED.nodes.registerType('apds9960', APDS9960ReadNode);
48
+ };
package/package.json ADDED
@@ -0,0 +1,22 @@
1
+ {
2
+ "name": "node-red-contrib-periph-light",
3
+ "version": "1.1.0",
4
+ "description": "Node-RED nodes for light sensor chips",
5
+ "keywords": [
6
+ "node-red",
7
+ "light",
8
+ "i2c",
9
+ "spi",
10
+ "sensor",
11
+ "peripheral"
12
+ ],
13
+ "node-red": {
14
+ "nodes": {
15
+ "apds9960": "nodes/apds9960/apds9960.js"
16
+ }
17
+ },
18
+ "dependencies": {
19
+ "periph": "*"
20
+ },
21
+ "license": "MIT"
22
+ }