node-red-node-pi-gpio 2.0.4 → 2.0.5
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/36-rpi-gpio.js +45 -37
- package/package.json +1 -1
package/36-rpi-gpio.js
CHANGED
|
@@ -45,46 +45,53 @@ module.exports = function(RED) {
|
|
|
45
45
|
}
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
node.status({fill:"yellow",shape:"dot",text:"rpi-gpio.status.ok"});
|
|
48
|
+
var startPin = function() {
|
|
49
|
+
node.child = spawn(gpioCommand, ["in",node.pin,node.intype,node.debounce]);
|
|
50
|
+
node.running = true;
|
|
51
|
+
node.status({fill:"yellow",shape:"dot",text:"rpi-gpio.status.ok"});
|
|
53
52
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
node.buttonState = d[i];
|
|
62
|
-
node.status({fill:"green",shape:"dot",text:d[i]});
|
|
63
|
-
if (RED.settings.verbose) { node.log("out: "+d[i]+" :"); }
|
|
53
|
+
node.child.stdout.on('data', function (data) {
|
|
54
|
+
var d = data.toString().trim().split("\n");
|
|
55
|
+
for (var i = 0; i < d.length; i++) {
|
|
56
|
+
if (d[i] === '') { return; }
|
|
57
|
+
if (node.running && node.buttonState !== -1 && !isNaN(Number(d[i])) && node.buttonState !== d[i]) {
|
|
58
|
+
node.send({ topic:"gpio/"+node.pin, payload:Number(d[i]) });
|
|
64
59
|
}
|
|
65
|
-
|
|
60
|
+
node.buttonState = d[i];
|
|
61
|
+
node.status({fill:"green",shape:"dot",text:d[i]});
|
|
62
|
+
if (RED.settings.verbose) { node.log("out: "+d[i]+" :"); }
|
|
63
|
+
}
|
|
64
|
+
});
|
|
66
65
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
66
|
+
node.child.stderr.on('data', function (data) {
|
|
67
|
+
if (RED.settings.verbose) { node.log("err: "+data+" :"); }
|
|
68
|
+
});
|
|
70
69
|
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
70
|
+
node.child.on('close', function (code) {
|
|
71
|
+
node.running = false;
|
|
72
|
+
node.child.removeAllListeners();
|
|
73
|
+
delete node.child;
|
|
74
|
+
if (RED.settings.verbose) { node.log(RED._("rpi-gpio.status.closed")); }
|
|
75
|
+
if (!node.finished && code === 1) {
|
|
76
|
+
setTimeout(function() {startPin()}, 250);
|
|
77
|
+
}
|
|
78
|
+
else if (node.finished) {
|
|
79
|
+
node.status({fill:"grey",shape:"ring",text:"rpi-gpio.status.closed"});
|
|
80
|
+
node.finished();
|
|
81
|
+
}
|
|
82
|
+
else { node.status({fill:"red",shape:"ring",text:"rpi-gpio.status.stopped"}); }
|
|
83
|
+
});
|
|
81
84
|
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
85
|
+
node.child.on('error', function (err) {
|
|
86
|
+
if (err.errno === "ENOENT") { node.error(RED._("rpi-gpio.errors.commandnotfound")); }
|
|
87
|
+
else if (err.errno === "EACCES") { node.error(RED._("rpi-gpio.errors.commandnotexecutable")); }
|
|
88
|
+
else { node.error(RED._("rpi-gpio.errors.error",{error:err.errno})) }
|
|
89
|
+
});
|
|
90
|
+
}
|
|
87
91
|
|
|
92
|
+
if (allOK === true) {
|
|
93
|
+
if (node.pin !== undefined) {
|
|
94
|
+
startPin();
|
|
88
95
|
}
|
|
89
96
|
else {
|
|
90
97
|
node.warn(RED._("rpi-gpio.errors.invalidpin")+": "+node.pin);
|
|
@@ -109,8 +116,9 @@ module.exports = function(RED) {
|
|
|
109
116
|
if (node.child != null) {
|
|
110
117
|
node.finished = done;
|
|
111
118
|
node.child.stdin.write("close "+node.pin, () => {
|
|
112
|
-
node.child
|
|
113
|
-
|
|
119
|
+
if (node.child) {
|
|
120
|
+
node.child.kill('SIGKILL');
|
|
121
|
+
}
|
|
114
122
|
});
|
|
115
123
|
}
|
|
116
124
|
else { if (done) { done(); } }
|
|
@@ -214,7 +222,7 @@ module.exports = function(RED) {
|
|
|
214
222
|
node.finished = done;
|
|
215
223
|
node.child.stdin.write("close "+node.pin, () => {
|
|
216
224
|
node.child.kill('SIGKILL');
|
|
217
|
-
setTimeout(function() { if (done) { done(); } },
|
|
225
|
+
setTimeout(function() { if (done) { done(); } }, 50);
|
|
218
226
|
});
|
|
219
227
|
}
|
|
220
228
|
else { if (done) { done(); } }
|