node-red-contrib-periph-power 1.3.2 → 1.4.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
@@ -17,6 +17,8 @@ npm install node-red-contrib-periph-power
17
17
  | Node | Kind | Description |
18
18
  |------|------|-------------|
19
19
  | `periph-ade7953` | input | Reads bus voltage, load current, active power, and accumulated active energy from an ADE7953 single-phase multifunction metering IC. |
20
+ | `axp2101-device` | config | I²C bus and address for an AXP2101 |
21
+ | `axp2101` | input | Reads battery, charger and power-input state from an AXP2101 single-cell power-management IC over I²C. |
20
22
  | `ina219-device` | config | I²C bus and address for an INA219 |
21
23
  | `ina219` | input | Reads voltage, current, power, and shunt voltage from an INA219 power monitor over I²C. |
22
24
  | `ina226-device` | config | I²C bus and address for an INA226 |
@@ -0,0 +1,86 @@
1
+ [
2
+ {
3
+ "id": "tab-axp2101-demo",
4
+ "type": "tab",
5
+ "label": "AXP2101 Battery Dashboard",
6
+ "disabled": false,
7
+ "info": "Reads battery voltage, state of charge, charge state, VBUS and die temperature from an AXP2101 every 5 s. A function node keeps the last VBUS state and adds a vbus_event field (inserted / removed) whenever it changes, and a debug node prints the result. Demonstrates a battery and charger dashboard. Adjust the I2C bus and address in the device config node; set a charge voltage / current there only if you know your cell's limits (the 4.35 V and 4.4 V options are for high-voltage Li-ion cells only)."
8
+ },
9
+ {
10
+ "id": "cfg-axp2101",
11
+ "type": "axp2101-device",
12
+ "name": "AXP2101",
13
+ "bus": "1",
14
+ "address": "34",
15
+ "chargeVoltage": "",
16
+ "chargeCurrent": ""
17
+ },
18
+ {
19
+ "id": "inj-tick",
20
+ "type": "inject",
21
+ "name": "every 5 s",
22
+ "repeat": "5",
23
+ "crontab": "",
24
+ "once": true,
25
+ "onceDelay": 0.1,
26
+ "topic": "",
27
+ "payload": "",
28
+ "payloadType": "date",
29
+ "z": "tab-axp2101-demo",
30
+ "x": 120,
31
+ "y": 100,
32
+ "wires": [
33
+ [
34
+ "read-axp2101"
35
+ ]
36
+ ]
37
+ },
38
+ {
39
+ "id": "read-axp2101",
40
+ "type": "axp2101",
41
+ "name": "AXP2101",
42
+ "device": "cfg-axp2101",
43
+ "z": "tab-axp2101-demo",
44
+ "x": 300,
45
+ "y": 100,
46
+ "wires": [
47
+ [
48
+ "fn-vbus"
49
+ ]
50
+ ]
51
+ },
52
+ {
53
+ "id": "fn-vbus",
54
+ "type": "function",
55
+ "name": "flag VBUS change",
56
+ "func": "const last = context.get('vbus');\nconst now = msg.payload.vbus_present;\nif (last !== undefined && last !== now) {\n msg.payload.vbus_event = now ? 'inserted' : 'removed';\n}\ncontext.set('vbus', now);\nreturn msg;",
57
+ "outputs": 1,
58
+ "noerr": 0,
59
+ "initialize": "",
60
+ "finalize": "",
61
+ "libs": [],
62
+ "z": "tab-axp2101-demo",
63
+ "x": 520,
64
+ "y": 100,
65
+ "wires": [
66
+ [
67
+ "dbg-axp2101"
68
+ ]
69
+ ]
70
+ },
71
+ {
72
+ "id": "dbg-axp2101",
73
+ "type": "debug",
74
+ "name": "battery state",
75
+ "active": true,
76
+ "tosidebar": true,
77
+ "console": false,
78
+ "tostatus": false,
79
+ "complete": "payload",
80
+ "targetType": "msg",
81
+ "z": "tab-axp2101-demo",
82
+ "x": 740,
83
+ "y": 100,
84
+ "wires": []
85
+ }
86
+ ]
@@ -0,0 +1,103 @@
1
+ <!-- Config node: one instance per physical device -->
2
+ <script type="text/javascript">
3
+ RED.nodes.registerType('axp2101-device', {
4
+ category: 'config',
5
+ defaults: {
6
+ name: { value: '' },
7
+ bus: { value: '1', required: true, validate: RED.validators.number() },
8
+ address: { value: '34', required: true },
9
+ chargeVoltage: { value: '' },
10
+ chargeCurrent: { value: '', validate: function(v) { return v === '' || (!isNaN(parseFloat(v)) && parseFloat(v) >= 0 && parseFloat(v) <= 1.5); } }
11
+ },
12
+ label: function() {
13
+ return this.name || ('AXP2101 @ 0x' + this.address + ' (bus ' + this.bus + ')');
14
+ }
15
+ });
16
+ </script>
17
+
18
+ <script type="text/html" data-template-name="axp2101-device">
19
+ <div class="form-row">
20
+ <label for="node-config-input-name"><i class="fa fa-tag"></i> Name</label>
21
+ <input type="text" id="node-config-input-name" placeholder="AXP2101">
22
+ </div>
23
+ <div class="form-row">
24
+ <label for="node-config-input-bus"><i class="fa fa-microchip"></i> I²C bus</label>
25
+ <input type="number" id="node-config-input-bus" min="0" style="width:80px"> &nbsp;(Linux /dev/i2c-N)
26
+ </div>
27
+ <div class="form-row">
28
+ <label for="node-config-input-address">Address (hex)</label>
29
+ <input type="text" id="node-config-input-address" placeholder="34" style="width:80px"> &nbsp;default: 34 (0x34)
30
+ </div>
31
+ <div class="form-row">
32
+ <label for="node-config-input-chargeVoltage">Charge voltage</label>
33
+ <select id="node-config-input-chargeVoltage" style="width:150px">
34
+ <option value="">keep chip default</option>
35
+ <option value="4.0">4.0 V</option>
36
+ <option value="4.1">4.1 V</option>
37
+ <option value="4.2">4.2 V</option>
38
+ <option value="4.35">4.35 V (HV cells)</option>
39
+ <option value="4.4">4.4 V (HV cells)</option>
40
+ </select>
41
+ </div>
42
+ <div class="form-row">
43
+ <label for="node-config-input-chargeCurrent">Charge current (A)</label>
44
+ <input type="number" id="node-config-input-chargeCurrent" min="0" max="1.5" step="0.025" style="width:100px"> &nbsp;empty = keep chip default
45
+ </div>
46
+ </script>
47
+
48
+ <!-- Read node: trigger → battery and power state -->
49
+ <script type="text/javascript">
50
+ RED.nodes.registerType('axp2101', {
51
+ category: 'Peripheral',
52
+ color: '#C084FC',
53
+ defaults: {
54
+ name: { value: '' },
55
+ device: { value: '', type: 'axp2101-device', required: true }
56
+ },
57
+ inputs: 1,
58
+ outputs: 1,
59
+ icon: 'font-awesome/fa-battery-half',
60
+ label: function() { return this.name || 'AXP2101'; },
61
+ paletteLabel: 'AXP2101'
62
+ });
63
+ </script>
64
+
65
+ <script type="text/html" data-template-name="axp2101">
66
+ <div class="form-row">
67
+ <label for="node-input-device"><i class="fa fa-microchip"></i> Device</label>
68
+ <input type="text" id="node-input-device">
69
+ </div>
70
+ <div class="form-row">
71
+ <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
72
+ <input type="text" id="node-input-name" placeholder="AXP2101">
73
+ </div>
74
+ </script>
75
+
76
+ <script type="text/html" data-help-name="axp2101">
77
+ <p>Reads battery, charger and power-input state from an AXP2101 single-cell power-management IC over I²C.</p>
78
+ <p>Any input message triggers a measurement. The device is configured via an <b>axp2101-device</b> config node.</p>
79
+
80
+ <h3>Output</h3>
81
+ <dl class="message-properties">
82
+ <dt>payload.battery_voltage <span class="property-type">number</span></dt>
83
+ <dd>Battery voltage in V.</dd>
84
+ <dt>payload.battery_percent <span class="property-type">number</span></dt>
85
+ <dd>State of charge from the fuel gauge, 0–100 %.</dd>
86
+ <dt>payload.charge_state <span class="property-type">string</span></dt>
87
+ <dd><code>trickle</code>, <code>pre</code>, <code>cc</code>, <code>cv</code>, <code>done</code> or <code>none</code>.</dd>
88
+ <dt>payload.vbus_present <span class="property-type">boolean</span></dt>
89
+ <dd>True when a good input source is connected.</dd>
90
+ <dt>payload.vbus_voltage <span class="property-type">number</span></dt>
91
+ <dd>VBUS voltage in V.</dd>
92
+ <dt>payload.system_voltage <span class="property-type">number</span></dt>
93
+ <dd>System (VSYS) voltage in V.</dd>
94
+ <dt>payload.die_temperature <span class="property-type">number</span></dt>
95
+ <dd>Die temperature in °C (vendor formula, not from the datasheet).</dd>
96
+ </dl>
97
+
98
+ <h3>Configuration</h3>
99
+ <p>Set the I²C bus number and the chip's 7-bit address (default <code>0x34</code>) in the device config
100
+ node. The optional charge voltage (4.0 / 4.1 / 4.2 / 4.35 / 4.4 V) and charge current (0–1.5 A) are written
101
+ once on deploy; leave them empty to keep the board's eFuse defaults. The 4.35 and 4.4 V settings are for
102
+ high-voltage Li-ion cells only.</p>
103
+ </script>
@@ -0,0 +1,63 @@
1
+ 'use strict';
2
+
3
+ module.exports = function(RED) {
4
+ const { I2CConnection } = require('periph/src/connection/i2c');
5
+ const { AXP2101Full } = require('periph/src/chips/power/axp2101');
6
+
7
+ const CHARGE_STATES = ['trickle', 'pre', 'cc', 'cv', 'done', 'none'];
8
+
9
+ function AXP2101DeviceNode(config) {
10
+ RED.nodes.createNode(this, config);
11
+ const node = this;
12
+ try {
13
+ const connection = new I2CConnection(parseInt(config.bus), parseInt(config.address, 16));
14
+ node.connection = connection;
15
+ node.driver = new AXP2101Full(connection);
16
+ // Optional charge profile, written once on deploy (after the init sequence has finished).
17
+ node.driver.ready.then(async function() {
18
+ if (config.chargeVoltage) await node.driver.setChargeVoltage(parseFloat(config.chargeVoltage));
19
+ if (config.chargeCurrent) await node.driver.setChargeCurrent(parseFloat(config.chargeCurrent));
20
+ }).catch(function(e) {
21
+ node.error('AXP2101 init failed: ' + e.message);
22
+ });
23
+ } catch (e) {
24
+ node.error('AXP2101 init failed: ' + e.message);
25
+ }
26
+ node.on('close', function() {
27
+ if (node.connection) node.connection.close();
28
+ });
29
+ }
30
+ RED.nodes.registerType('axp2101-device', AXP2101DeviceNode);
31
+
32
+ function AXP2101ReadNode(config) {
33
+ RED.nodes.createNode(this, config);
34
+ const node = this;
35
+ node.device = RED.nodes.getNode(config.device);
36
+
37
+ node.on('input', async function(msg, send, done) {
38
+ if (!node.device || !node.device.driver) {
39
+ node.error('No AXP2101 device configured', msg);
40
+ done();
41
+ return;
42
+ }
43
+ try {
44
+ const d = node.device.driver;
45
+ const r = await d.readAll();
46
+ msg.payload = {
47
+ battery_voltage: r.batteryVoltage,
48
+ battery_percent: r.batteryPercent,
49
+ charge_state: CHARGE_STATES[r.chargeState] || 'none',
50
+ vbus_present: r.vbusPresent,
51
+ vbus_voltage: r.vbusVoltage,
52
+ system_voltage: r.systemVoltage,
53
+ die_temperature: r.dieTemperature
54
+ };
55
+ send(msg);
56
+ done();
57
+ } catch (e) {
58
+ done(e);
59
+ }
60
+ });
61
+ }
62
+ RED.nodes.registerType('axp2101', AXP2101ReadNode);
63
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "node-red-contrib-periph-power",
3
- "version": "1.3.2",
3
+ "version": "1.4.0",
4
4
  "description": "Node-RED nodes for power monitor chips",
5
5
  "keywords": [
6
6
  "node-red",
@@ -16,7 +16,8 @@
16
16
  "ina226": "nodes/ina226/ina226.js",
17
17
  "ina219": "nodes/ina219/ina219.js",
18
18
  "ina3221": "nodes/ina3221/ina3221.js",
19
- "ade7953": "nodes/ade7953/ade7953.js"
19
+ "ade7953": "nodes/ade7953/ade7953.js",
20
+ "axp2101": "nodes/axp2101/axp2101.js"
20
21
  }
21
22
  },
22
23
  "dependencies": {