node-red-contrib-periph-light 1.4.0 → 1.5.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
@@ -20,6 +20,7 @@ npm install node-red-contrib-periph-light
20
20
  | `apds9930` | input | Reads ambient illuminance and proximity from an APDS-9930 sensor over I²C. |
21
21
  | `apds9960-device` | config | I²C bus and address for an APDS9960 |
22
22
  | `apds9960` | input | Reads ambient light and color (RGBC) from an APDS-9960 sensor over I²C. |
23
+ | `periph-max44009` | input | Reads the ambient illuminance of a MAX44009 ultra-low-power light sensor (I²C, 0.045 lx to 188 006.4 lx) and arms its threshold interrupt. |
23
24
 
24
25
  ## Links
25
26
 
@@ -0,0 +1,101 @@
1
+ [
2
+ {
3
+ "id": "tab-max44009-demo",
4
+ "type": "tab",
5
+ "label": "MAX44009 Backlight Demo",
6
+ "disabled": false,
7
+ "info": "Adaptive backlight on a MAX44009 ambient light sensor. An inject node fires every 2 s into periph-max44009; a function node maps the lux to a backlight percentage (log scale) and a debug node shows lux and percentage. A second inject sends {low: 50, high: 500, delay: 2} to arm the interrupt window, and with the INT line wired the node emits {lux, interrupt: true} messages when the light leaves the window."
8
+ },
9
+ {
10
+ "id": "inj-read",
11
+ "type": "inject",
12
+ "name": "Read every 2 s",
13
+ "topic": "",
14
+ "payload": "",
15
+ "payloadType": "date",
16
+ "repeat": "2",
17
+ "crontab": "",
18
+ "once": true,
19
+ "onceDelay": 1,
20
+ "z": "tab-max44009-demo",
21
+ "x": 150,
22
+ "y": 100,
23
+ "wires": [
24
+ [
25
+ "max44009-node"
26
+ ]
27
+ ]
28
+ },
29
+ {
30
+ "id": "inj-arm",
31
+ "type": "inject",
32
+ "name": "Arm window 50-500 lx",
33
+ "topic": "",
34
+ "payload": "{\"low\":50,\"high\":500,\"delay\":2}",
35
+ "payloadType": "json",
36
+ "repeat": "",
37
+ "crontab": "",
38
+ "once": false,
39
+ "onceDelay": 0.1,
40
+ "z": "tab-max44009-demo",
41
+ "x": 170,
42
+ "y": 180,
43
+ "wires": [
44
+ [
45
+ "max44009-node"
46
+ ]
47
+ ]
48
+ },
49
+ {
50
+ "id": "max44009-node",
51
+ "type": "periph-max44009",
52
+ "name": "MAX44009",
53
+ "bus": "1",
54
+ "address": "0x4A",
55
+ "mode": "lowpower",
56
+ "range": "auto",
57
+ "integration": "100",
58
+ "divide8": false,
59
+ "gpioChip": "0",
60
+ "intPin": "",
61
+ "z": "tab-max44009-demo",
62
+ "x": 400,
63
+ "y": 140,
64
+ "wires": [
65
+ [
66
+ "fn-backlight"
67
+ ]
68
+ ]
69
+ },
70
+ {
71
+ "id": "fn-backlight",
72
+ "type": "function",
73
+ "name": "lux to backlight %",
74
+ "func": "// Logarithmic map of 0.1 - 10000 lx to a 10 - 100 % backlight level.\nconst lx = Math.min(10000, Math.max(0.1, msg.payload.lux));\nmsg.payload.backlight = Math.round(10 + 90 * (Math.log10(lx) + 1) / 5);\nreturn msg;",
75
+ "outputs": 1,
76
+ "noerr": 0,
77
+ "z": "tab-max44009-demo",
78
+ "x": 640,
79
+ "y": 140,
80
+ "wires": [
81
+ [
82
+ "dbg-max44009"
83
+ ]
84
+ ]
85
+ },
86
+ {
87
+ "id": "dbg-max44009",
88
+ "type": "debug",
89
+ "name": "lux / backlight",
90
+ "active": true,
91
+ "tosidebar": true,
92
+ "console": false,
93
+ "tostatus": false,
94
+ "complete": "payload",
95
+ "targetType": "msg",
96
+ "z": "tab-max44009-demo",
97
+ "x": 860,
98
+ "y": 140,
99
+ "wires": []
100
+ }
101
+ ]
@@ -0,0 +1,99 @@
1
+ <script type="text/javascript">
2
+ RED.nodes.registerType('periph-max44009', {
3
+ category: 'Peripheral',
4
+ color: '#C084FC',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '0x4A' },
9
+ mode: { value: 'lowpower' },
10
+ range: { value: 'auto' },
11
+ integration: { value: '100' },
12
+ divide8: { value: false },
13
+ gpioChip: { value: '0' },
14
+ intPin: { value: '' }
15
+ },
16
+ inputs: 1,
17
+ outputs: 1,
18
+ icon: 'font-awesome/fa-sun-o',
19
+ label: function() { return this.name || 'MAX44009'; },
20
+ paletteLabel: 'MAX44009'
21
+ });
22
+ </script>
23
+
24
+ <script type="text/html" data-template-name="periph-max44009">
25
+ <div class="form-row">
26
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
27
+ <input type="text" id="node-input-name" placeholder="MAX44009">
28
+ </div>
29
+ <div class="form-row">
30
+ <label for="node-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
31
+ <input type="number" id="node-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
32
+ </div>
33
+ <div class="form-row">
34
+ <label for="node-input-address"><i class="fa fa-at"></i> I²C address</label>
35
+ <select id="node-input-address" style="width:200px">
36
+ <option value="0x4A">0x4A (A0 = GND)</option>
37
+ <option value="0x4B">0x4B (A0 = VCC)</option>
38
+ </select>
39
+ </div>
40
+ <div class="form-row">
41
+ <label for="node-input-mode"><i class="fa fa-clock-o"></i> Mode</label>
42
+ <select id="node-input-mode" style="width:200px">
43
+ <option value="lowpower">Low power (800 ms)</option>
44
+ <option value="continuous">Continuous</option>
45
+ </select>
46
+ </div>
47
+ <div class="form-row">
48
+ <label for="node-input-range"><i class="fa fa-sliders"></i> Range</label>
49
+ <select id="node-input-range" style="width:200px">
50
+ <option value="auto">Automatic</option>
51
+ <option value="manual">Manual</option>
52
+ </select>
53
+ </div>
54
+ <div class="form-row">
55
+ <label for="node-input-integration">Integration</label>
56
+ <select id="node-input-integration" style="width:120px">
57
+ <option value="800">800 ms</option><option value="400">400 ms</option>
58
+ <option value="200">200 ms</option><option value="100">100 ms</option>
59
+ <option value="50">50 ms</option><option value="25">25 ms</option>
60
+ <option value="12.5">12.5 ms</option><option value="6.25">6.25 ms</option>
61
+ </select>
62
+ &nbsp;<input type="checkbox" id="node-input-divide8" style="width:auto; vertical-align:top"> divide by 8
63
+ </div>
64
+ <div class="form-row">
65
+ <label for="node-input-gpioChip">GPIO chip</label>
66
+ <input type="number" id="node-input-gpioChip" min="0" style="width:80px"> &nbsp;(for the optional INT line)
67
+ </div>
68
+ <div class="form-row">
69
+ <label for="node-input-intPin">INT GPIO line</label>
70
+ <input type="number" id="node-input-intPin" min="0" style="width:80px" placeholder="none">
71
+ </div>
72
+ </script>
73
+
74
+ <script type="text/html" data-help-name="periph-max44009">
75
+ <p>Reads the ambient illuminance of a MAX44009 ultra-low-power light sensor (I²C, 0.045 lx to 188 006.4 lx) and arms its threshold interrupt.</p>
76
+
77
+ <h3>Inputs</h3>
78
+ <dl class="message-properties">
79
+ <dt>payload.low, payload.high <span class="property-type">number</span></dt>
80
+ <dd>Interrupt window in lux. Programs the lower and upper thresholds and enables the interrupt; no output message.</dd>
81
+ <dt>payload.delay <span class="property-type">number</span></dt>
82
+ <dd>Optional with <code>low</code> / <code>high</code>: seconds (0–25.5) the light must stay outside the window before the interrupt.</dd>
83
+ <dt>payload <span class="property-type">any</span></dt>
84
+ <dd>Any other message reads the illuminance.</dd>
85
+ </dl>
86
+
87
+ <h3>Outputs</h3>
88
+ <dl class="message-properties">
89
+ <dt>payload.lux <span class="property-type">number</span></dt>
90
+ <dd>Illuminance in lux (the maximum 188006.4 on overrange).</dd>
91
+ <dt>payload.overrange <span class="property-type">boolean</span></dt>
92
+ <dd>True if the last reading was overrange (exponent field 15).</dd>
93
+ <dt>payload.interrupt <span class="property-type">boolean</span></dt>
94
+ <dd>Only when an INT GPIO line is configured: <code>{ lux, interrupt: true }</code> is emitted when the light leaves the window for longer than the delay.</dd>
95
+ </dl>
96
+
97
+ <h3>Configuration</h3>
98
+ <p>Set the Linux I²C bus number and the address (0x4A with A0 at GND, 0x4B with A0 at VCC). <b>Mode</b> selects low power (one measurement per 800 ms) or continuous; <b>Range</b> selects automatic or manual ranging with an integration time of 6.25–800 ms and an optional current divider of 8. The optional <b>INT GPIO line</b> (with GPIO chip number) is sampled every 20 ms. The chip is not 5 V tolerant (VCC 1.7–3.6 V).</p>
99
+ </script>
@@ -0,0 +1,82 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { MAX44009Full } = require('periph/src/chips/light/max44009');
6
+
7
+ function MAX44009Node(config) {
8
+ RED.nodes.createNode(this, config);
9
+ const node = this;
10
+
11
+ try {
12
+ const address = parseInt(config.address || '0x4A', 16);
13
+ node.connection = new I2CConnection(parseInt(config.bus, 10), address);
14
+ node.driver = new MAX44009Full(node.connection);
15
+ node.ready = (async () => {
16
+ await node.driver.init();
17
+ await node.driver.setContinuous(config.mode === 'continuous');
18
+ if (config.range === 'manual') {
19
+ await node.driver.setManual(parseFloat(config.integration) || 100, config.divide8 === true || config.divide8 === 'true');
20
+ } else {
21
+ await node.driver.setAutomatic();
22
+ }
23
+ })();
24
+ node.ready.then(() => node.status({ fill: 'green', shape: 'dot', text: 'I2C' }))
25
+ .catch((e) => node.error('MAX44009 init failed: ' + e.message));
26
+ } catch (e) {
27
+ node.error('MAX44009 init failed: ' + e.message);
28
+ }
29
+
30
+ // Optional INT line: emit { lux, interrupt: true } when the light leaves the window.
31
+ if (node.driver && config.intPin !== '' && config.intPin !== undefined) {
32
+ (async () => {
33
+ try {
34
+ const { Default } = require('opengpio');
35
+ const { PollingInputPin } = require('periph/src/connection/input_pin');
36
+ const line = Default.input({ chip: parseInt(config.gpioChip || 0, 10), line: parseInt(config.intPin, 10) });
37
+ const intPin = new PollingInputPin(() => (line.value ? 1 : 0), 20);
38
+ await node.ready;
39
+ await node.driver.onInterrupt(async () => {
40
+ const lux = await node.driver.lux();
41
+ node.send({ payload: { lux, interrupt: true } });
42
+ }, { intPin });
43
+ } catch (e) {
44
+ node.error('MAX44009 INT setup failed: ' + e.message);
45
+ }
46
+ })();
47
+ }
48
+
49
+ node.on('input', async function(msg, send, done) {
50
+ if (!node.driver) { done(new Error('MAX44009 not initialised')); return; }
51
+ try {
52
+ await node.ready;
53
+ const p = msg.payload;
54
+ if (p && typeof p === 'object' && typeof p.low === 'number' && typeof p.high === 'number') {
55
+ await node.driver.setThresholds(p.low, p.high);
56
+ if (typeof p.delay === 'number') await node.driver.setThresholdDelay(p.delay);
57
+ await node.driver.enableInterrupt();
58
+ done();
59
+ return;
60
+ }
61
+ const lux = await node.driver.lux();
62
+ msg.payload = { lux, overrange: node.driver.isOverrange() };
63
+ send(msg);
64
+ done();
65
+ } catch (e) {
66
+ done(e);
67
+ }
68
+ });
69
+
70
+ node.on('close', async function(removed, done) {
71
+ try {
72
+ if (node.driver) await node.driver.offInterrupt();
73
+ if (node.connection) await node.connection.close();
74
+ } catch (e) {
75
+ // bus may already be gone; nothing more to do on close
76
+ } finally {
77
+ done();
78
+ }
79
+ });
80
+ }
81
+ RED.nodes.registerType('periph-max44009', MAX44009Node);
82
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-light",
3
- "version": "1.4.0",
3
+ "version": "1.5.1",
4
4
  "description": "Node-RED nodes for light sensor chips",
5
5
  "keywords": [
6
6
  "node-red",
@@ -12,7 +12,8 @@
12
12
  ],
13
13
  "node-red": {
14
14
  "nodes": {
15
- "apds9960": "nodes/apds9960/apds9960.js"
15
+ "apds9960": "nodes/apds9960/apds9960.js",
16
+ "periph-max44009": "nodes/max44009/max44009.js"
16
17
  }
17
18
  },
18
19
  "dependencies": {