forked from node-red/node-red-nodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
39-rpi-piliter.js
113 lines (107 loc) · 4.71 KB
/
39-rpi-piliter.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
module.exports = function(RED) {
"use strict";
var util = require("util");
var spawn = require('child_process').spawn;
var fs = require('fs');
var gpioCommand = __dirname+'/nrgpio.py';
var allOK = true;
try {
var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString();
if (cpuinfo.indexOf(": BCM") === -1) {
RED.log.warn("rpi-liter : "+RED._("node-red:rpi-gpio.errors.ignorenode"));
allOK = false;
}
else if (!fs.existsSync("/usr/share/doc/python-rpi.gpio")) {
RED.log.warn("rpi-liter : "+RED._("node-red:rpi-gpio.errors.libnotfound"));
allOK = false;
}
else if (!(1 & parseInt ((fs.statSync(gpioCommand).mode & parseInt ("777", 8)).toString (8)[0]))) {
RED.log.warn("rpi-liter : "+RED._("node-red:rpi-gpio.errors.needtobeexecutable",{command:gpioCommand}));
allOK = false;
}
}
catch(err) {
RED.log.warn("rpi-liter : "+RED._("node-red:rpi-gpio.errors.ignorenode"));
allOK = false;
}
function PiLiter(n) {
RED.nodes.createNode(this,n);
this.pinv = n.pin;
this.dir = (n.dir ? 1 : 0) || 0;
var node = this;
if (allOK === true) {
if (this.pinv === "bar") {
node.child = spawn(gpioCommand, ["byte",node.dir]);
node.on("input", function(msg) {
var out = Number(msg.payload);
if ((out >= 1) && (out <= 8)) { out = Math.pow(2, out) - 1; }
else { out = 0; }
if (node.child !== null) { node.child.stdin.write(out+"\n"); }
else { node.warn("Command not running"); }
});
}
else if (this.pinv === "meter") {
node.child = spawn(gpioCommand, ["byte",node.dir]);
node.on("input", function(msg) {
var out = Number(msg.payload);
if ((out >= 1) && (out <= 8)) { out = Math.pow(2, (out-1)); }
else { out = 0; }
if (node.child !== null) { node.child.stdin.write(out+"\n"); }
else { node.warn("Command not running"); }
});
}
else if (this.pinv === "all") {
node.child = spawn(gpioCommand, ["byte",node.dir]);
node.on("input", function(msg) {
var out = msg.payload;
if ((out === 1)|(out === true)|(out === "1")|(out === "on")) {
out = 255;
}
else { out = 0; }
if (node.child !== null) { node.child.stdin.write(out+"\n"); }
else { node.warn("Command not running"); }
});
}
else if (this.pinv === "pin") {
node.child = spawn(gpioCommand, ["byte",node.dir]);
var byte = 0;
node.on("input", function(msg) {
if (typeof msg.payload === "object") {
var out = Number(msg.payload.led);
var l = Number(msg.payload.state);
if ((out >= 1) && (out <= 8)) {
out = (Math.pow(2, (out-1)));
if (l === 0) { byte = (byte & (~out) & 255); }
else { byte = (byte | out) & 255; }
if (node.child !== null) { node.child.stdin.write(byte+"\n"); }
else { node.warn("Command not running"); }
}
else { node.warn("Not a valid object - see Info panel."); }
}
else { node.warn("Not a valid object - see Info panel."); }
});
}
else {
node.child = spawn(gpioCommand, ["byte",node.dir]);
node.on("input", function(msg) {
var out = Number(msg.payload);
if ((out >= 0) && (out <= 255)) {
if (node.child !== null) { node.child.stdin.write(out+"\n"); }
else { node.warn("Command not running"); }
}
else { node.warn("Invalid input - not between 0 and 255"); }
});
}
node.on("close", function() {
if (node.child != null) {
node.child.kill('SIGKILL');
}
if (RED.settings.verbose) { node.log("end"); }
});
}
else {
node.status({fill:"grey",shape:"dot",text:"node-red:rpi-gpio.status.not-available"});
}
}
RED.nodes.registerType("rpi-liter",PiLiter);
}