2014-07-25 11:07:23 +02:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
2015-01-06 15:54:45 +01:00
|
|
|
var util = require("util");
|
|
|
|
var spawn = require('child_process').spawn;
|
2015-05-11 20:25:39 +02:00
|
|
|
var fs = require('fs');
|
2014-07-25 11:07:23 +02:00
|
|
|
|
2021-06-18 09:56:15 +02:00
|
|
|
var gpioCommand = __dirname+'/nrgpio';
|
2018-04-15 12:24:56 +02:00
|
|
|
var allOK = true;
|
2015-01-06 15:54:45 +01:00
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
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;
|
|
|
|
}
|
2014-11-07 09:25:53 +01:00
|
|
|
}
|
2018-04-15 12:24:56 +02:00
|
|
|
catch(err) {
|
|
|
|
RED.log.warn("rpi-liter : "+RED._("node-red:rpi-gpio.errors.ignorenode"));
|
|
|
|
allOK = false;
|
2014-07-25 11:07:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function PiLiter(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.pinv = n.pin;
|
2015-01-06 15:54:45 +01:00
|
|
|
this.dir = (n.dir ? 1 : 0) || 0;
|
2014-07-25 11:07:23 +02:00
|
|
|
var node = this;
|
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
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."); }
|
2014-07-25 11:07:23 +02:00
|
|
|
}
|
|
|
|
else { node.warn("Not a valid object - see Info panel."); }
|
2018-04-15 12:24:56 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
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');
|
2014-07-25 11:07:23 +02:00
|
|
|
}
|
2018-04-15 12:24:56 +02:00
|
|
|
if (RED.settings.verbose) { node.log("end"); }
|
2014-07-25 11:07:23 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2018-04-15 12:24:56 +02:00
|
|
|
node.status({fill:"grey",shape:"dot",text:"node-red:rpi-gpio.status.not-available"});
|
2014-07-25 11:07:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("rpi-liter",PiLiter);
|
|
|
|
}
|