node-red-contrib-periph-light 1.1.0 → 1.2.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 CHANGED
@@ -2,15 +2,26 @@
2
2
 
3
3
  Node-RED nodes for light sensor 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-light`.
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-light
11
13
  ```
12
14
 
15
+ ## Nodes
16
+
17
+ | Node | Kind | Description |
18
+ |------|------|-------------|
19
+ | `apds9930-device` | config | I²C bus and address for an APDS9930 |
20
+ | `apds9930` | input | Reads ambient illuminance and proximity from an APDS-9930 sensor over I²C. |
21
+ | `apds9960-device` | config | I²C bus and address for an APDS9960 |
22
+ | `apds9960` | input | Reads ambient light and color (RGBC) from an APDS-9960 sensor over I²C. |
23
+
13
24
  ## Links
14
25
 
15
26
  - [GitHub](https://github.com/tuhde/Periph)
16
- - [All supported chips](https://github.com/tuhde/Periph#supported-chips)
27
+ - [periph JS driver](https://www.npmjs.com/package/periph)
@@ -0,0 +1,112 @@
1
+ [
2
+ {
3
+ "id": "tab-apds9930-demo",
4
+ "type": "tab",
5
+ "label": "APDS-9930 Light + Proximity",
6
+ "disabled": false,
7
+ "info": "Reads ambient illuminance (lux) and raw proximity from an APDS-9930 every 2 seconds. A Switch node routes the flow when proximity exceeds 500 counts (phone-screen-disable trigger) or when lux drops below 10 lx (dim backlight trigger). Cover the sensor with a hand to see the proximity branch fire; dim the room light to see the dim-backlight branch. Adjust the inject interval or change the threshold values to taste."
8
+ },
9
+ {
10
+ "id": "cfg-apds9930",
11
+ "type": "apds9930-device",
12
+ "name": "APDS-9930",
13
+ "bus": "1",
14
+ "address": "39"
15
+ },
16
+ {
17
+ "id": "inj-tick",
18
+ "type": "inject",
19
+ "name": "every 2 s",
20
+ "repeat": "2",
21
+ "crontab": "",
22
+ "once": true,
23
+ "onceDelay": 0.1,
24
+ "topic": "",
25
+ "payload": "",
26
+ "payloadType": "date",
27
+ "z": "tab-apds9930-demo",
28
+ "x": 120,
29
+ "y": 160,
30
+ "wires": [["read-apds9930"]]
31
+ },
32
+ {
33
+ "id": "read-apds9930",
34
+ "type": "apds9930",
35
+ "name": "APDS-9930",
36
+ "device": "cfg-apds9930",
37
+ "z": "tab-apds9930-demo",
38
+ "x": 300,
39
+ "y": 160,
40
+ "wires": [["fmt-readings", "sw-prox"]]
41
+ },
42
+ {
43
+ "id": "fmt-readings",
44
+ "type": "function",
45
+ "name": "format readings",
46
+ "func": "const p = msg.payload;\nmsg.payload = 'lux=' + p.lux.toFixed(1) + ' lx proximity=' + p.proximity;\nreturn msg;",
47
+ "outputs": 1,
48
+ "noerr": 0,
49
+ "initialize": "",
50
+ "finalize": "",
51
+ "libs": [],
52
+ "z": "tab-apds9930-demo",
53
+ "x": 480,
54
+ "y": 120,
55
+ "wires": [["dbg-readings"]]
56
+ },
57
+ {
58
+ "id": "sw-prox",
59
+ "type": "switch",
60
+ "name": "proximity > 500",
61
+ "property": "payload.proximity",
62
+ "propertyType": "msg",
63
+ "rules": [
64
+ {
65
+ "t": "gt",
66
+ "v": "500",
67
+ "vt": "num"
68
+ }
69
+ ],
70
+ "checkall": "true",
71
+ "repair": false,
72
+ "outputs": 1,
73
+ "z": "tab-apds9930-demo",
74
+ "x": 480,
75
+ "y": 200,
76
+ "wires": [["dbg-screen-off"]]
77
+ },
78
+ {
79
+ "id": "dbg-readings",
80
+ "type": "debug",
81
+ "name": "readings",
82
+ "active": true,
83
+ "tosidebar": true,
84
+ "console": false,
85
+ "tostatus": true,
86
+ "complete": "payload",
87
+ "targetType": "msg",
88
+ "statusVal": "payload",
89
+ "statusType": "auto",
90
+ "z": "tab-apds9930-demo",
91
+ "x": 650,
92
+ "y": 120,
93
+ "wires": []
94
+ },
95
+ {
96
+ "id": "dbg-screen-off",
97
+ "type": "debug",
98
+ "name": "disable screen",
99
+ "active": true,
100
+ "tosidebar": true,
101
+ "console": false,
102
+ "tostatus": true,
103
+ "complete": "payload",
104
+ "targetType": "msg",
105
+ "statusVal": "payload",
106
+ "statusType": "auto",
107
+ "z": "tab-apds9930-demo",
108
+ "x": 650,
109
+ "y": 200,
110
+ "wires": []
111
+ }
112
+ ]
@@ -0,0 +1,74 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('apds9930-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-9930 @ 0x' + this.address + ' (bus ' + this.bus + ')');
12
+ }
13
+ });
14
+ </script>
15
+
16
+ <script type="text/html" data-template-name="apds9930-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-9930">
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('apds9930', {
34
+ category: 'Periph',
35
+ color: '#C084FC',
36
+ defaults: {
37
+ name: { value: '' },
38
+ device: { value: '', type: 'apds9930-device', required: true }
39
+ },
40
+ inputs: 1,
41
+ outputs: 1,
42
+ icon: 'font-awesome/fa-sun-o',
43
+ label: function() { return this.name || 'APDS-9930'; },
44
+ paletteLabel: 'APDS-9930'
45
+ });
46
+ </script>
47
+
48
+ <script type="text/html" data-template-name="apds9930">
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-9930">
56
+ </div>
57
+ </script>
58
+
59
+ <script type="text/html" data-help-name="apds9930">
60
+ <p>Reads ambient illuminance and proximity from an APDS-9930 sensor over I²C.</p>
61
+ <p>Any input message triggers a measurement. The device is configured via an <b>apds9930-device</b> config node.</p>
62
+
63
+ <h3>Output</h3>
64
+ <dl class="message-properties">
65
+ <dt>payload.lux <span class="property-type">number</span></dt>
66
+ <dd>Illuminance in lux (IR-compensated via Ch0/Ch1 difference).</dd>
67
+ <dt>payload.proximity <span class="property-type">number</span></dt>
68
+ <dd>Raw 16-bit proximity ADC count (higher = closer).</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>0x39</code>) in the device config node.
73
+ The driver uses ATIME=0xDB (101 ms integration), PTIME=0xFF, PPULSE=8 pulses, and 100 mA LED drive by default.</p>
74
+ </script>
@@ -0,0 +1,49 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { APDS9930Minimal } = require('periph/src/chips/light/apds-9930');
6
+
7
+ function APDS9930DeviceNode(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 APDS9930Minimal(connection);
15
+ node.connection = connection;
16
+ } catch (e) {
17
+ node.error('APDS-9930 init failed: ' + e.message);
18
+ }
19
+ node.on('close', function() {
20
+ if (node.connection) node.connection.close();
21
+ });
22
+ }
23
+ RED.nodes.registerType('apds9930-device', APDS9930DeviceNode);
24
+
25
+ function APDS9930ReadNode(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 APDS-9930 device configured', msg);
33
+ done();
34
+ return;
35
+ }
36
+ try {
37
+ const d = node.device.driver;
38
+ const lux = await d.lux();
39
+ const proximity = await d.proximity();
40
+ msg.payload = { lux, proximity };
41
+ send(msg);
42
+ done();
43
+ } catch (e) {
44
+ done(e);
45
+ }
46
+ });
47
+ }
48
+ RED.nodes.registerType('apds9930', APDS9930ReadNode);
49
+ };
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
3
  module.exports = function(RED) {
4
- const { I2CTransport } = require('periph/src/transport/i2c');
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
5
  const { APDS9960Minimal } = require('periph/src/chips/light/apds9960');
6
6
 
7
7
  function APDS9960DeviceNode(config) {
@@ -10,14 +10,14 @@ module.exports = function(RED) {
10
10
  try {
11
11
  const bus = parseInt(config.bus);
12
12
  const addr = parseInt(config.address, 16);
13
- const transport = new I2CTransport(bus, addr);
14
- node.driver = new APDS9960Minimal(transport);
15
- node.transport = transport;
13
+ const connection = new I2CConnection(bus, addr);
14
+ node.driver = new APDS9960Minimal(connection);
15
+ node.connection = connection;
16
16
  } catch (e) {
17
17
  node.error('APDS-9960 init failed: ' + e.message);
18
18
  }
19
19
  node.on('close', function() {
20
- if (node.transport) node.transport.close();
20
+ if (node.connection) node.connection.close();
21
21
  });
22
22
  }
23
23
  RED.nodes.registerType('apds9960-device', APDS9960DeviceNode);
@@ -27,7 +27,7 @@ module.exports = function(RED) {
27
27
  const node = this;
28
28
  node.device = RED.nodes.getNode(config.device);
29
29
 
30
- node.on('input', function(msg, send, done) {
30
+ node.on('input', async function(msg, send, done) {
31
31
  if (!node.device || !node.device.driver) {
32
32
  node.error('No APDS-9960 device configured', msg);
33
33
  done();
@@ -35,7 +35,7 @@ module.exports = function(RED) {
35
35
  }
36
36
  try {
37
37
  const d = node.device.driver;
38
- const { clear, red, green, blue } = d.color();
38
+ const { clear, red, green, blue } = await d.color();
39
39
  msg.payload = { clear, red, green, blue };
40
40
  send(msg);
41
41
  done();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-light",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "description": "Node-RED nodes for light sensor chips",
5
5
  "keywords": [
6
6
  "node-red",