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
|
|
|
|
2015-12-12 17:33:39 +01:00
|
|
|
var gpioCommand = __dirname+'/nrgpio.py';
|
2015-01-06 15:54:45 +01:00
|
|
|
|
2014-11-07 09:25:53 +01:00
|
|
|
if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi
|
2015-01-06 15:54:45 +01:00
|
|
|
//util.log("Info : Ignoring Raspberry Pi specific node.");
|
2014-11-07 09:25:53 +01:00
|
|
|
throw "Info : Ignoring Raspberry Pi specific node.";
|
|
|
|
}
|
2014-07-25 11:07:23 +02:00
|
|
|
|
2015-01-06 15:54:45 +01:00
|
|
|
if (!fs.existsSync("/usr/share/doc/python-rpi.gpio")) {
|
|
|
|
util.log("[rpi-gpio] Info : Can't find Pi RPi.GPIO python library.");
|
|
|
|
throw "Warning : Can't find Pi RPi.GPIO python library.";
|
2014-07-25 11:07:23 +02:00
|
|
|
}
|
|
|
|
|
2015-01-06 15:54:45 +01:00
|
|
|
if ( !(1 & parseInt ((fs.statSync(gpioCommand).mode & parseInt ("777", 8)).toString (8)[0]) )) {
|
|
|
|
util.log("[rpi-gpio] Error : "+gpioCommand+" needs to be executable.");
|
2015-12-12 17:33:39 +01:00
|
|
|
throw "Error : " + gpioCommand + " must to be executable.";
|
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;
|
|
|
|
|
|
|
|
if (this.pinv === "bar") {
|
2015-01-06 15:54:45 +01:00
|
|
|
node.child = spawn(gpioCommand, ["byte",node.dir]);
|
2014-07-25 11:07:23 +02:00
|
|
|
node.on("input", function(msg) {
|
|
|
|
var out = Number(msg.payload);
|
2015-01-06 15:54:45 +01:00
|
|
|
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"); }
|
2014-07-25 11:07:23 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (this.pinv === "meter") {
|
2015-01-06 15:54:45 +01:00
|
|
|
node.child = spawn(gpioCommand, ["byte",node.dir]);
|
2014-07-25 11:07:23 +02:00
|
|
|
node.on("input", function(msg) {
|
|
|
|
var out = Number(msg.payload);
|
2015-01-06 15:54:45 +01:00
|
|
|
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"); }
|
2014-07-25 11:07:23 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (this.pinv === "all") {
|
2015-01-06 15:54:45 +01:00
|
|
|
node.child = spawn(gpioCommand, ["byte",node.dir]);
|
2014-07-25 11:07:23 +02:00
|
|
|
node.on("input", function(msg) {
|
|
|
|
var out = msg.payload;
|
|
|
|
if ((out === 1)|(out === true)|(out === "1")|(out === "on")) {
|
2015-01-06 15:54:45 +01:00
|
|
|
out = 255;
|
2014-07-25 11:07:23 +02:00
|
|
|
}
|
2015-01-06 15:54:45 +01:00
|
|
|
else { out = 0; }
|
|
|
|
if (node.child !== null) { node.child.stdin.write(out+"\n"); }
|
|
|
|
else { node.warn("Command not running"); }
|
2014-07-25 11:07:23 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else if (this.pinv === "pin") {
|
2015-01-06 15:54:45 +01:00
|
|
|
node.child = spawn(gpioCommand, ["byte",node.dir]);
|
|
|
|
var byte = 0;
|
2014-07-25 11:07:23 +02:00
|
|
|
node.on("input", function(msg) {
|
|
|
|
if (typeof msg.payload === "object") {
|
|
|
|
var out = Number(msg.payload.led);
|
2015-01-06 15:54:45 +01:00
|
|
|
var l = Number(msg.payload.state);
|
|
|
|
if ((out >= 1) && (out <= 8)) {
|
|
|
|
out = (Math.pow(2, (out-1)));
|
2017-09-14 13:13:28 +02:00
|
|
|
if (l === 0) { byte = (byte & (~out) & 255); }
|
|
|
|
else { byte = (byte | out) & 255; }
|
2015-01-06 15:54:45 +01:00
|
|
|
if (node.child !== null) { node.child.stdin.write(byte+"\n"); }
|
|
|
|
else { node.warn("Command not running"); }
|
2014-07-25 11:07:23 +02:00
|
|
|
}
|
|
|
|
else { node.warn("Not a valid object - see Info panel."); }
|
|
|
|
}
|
|
|
|
else { node.warn("Not a valid object - see Info panel."); }
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2015-01-06 15:54:45 +01:00
|
|
|
node.child = spawn(gpioCommand, ["byte",node.dir]);
|
2014-07-25 11:07:23 +02:00
|
|
|
node.on("input", function(msg) {
|
|
|
|
var out = Number(msg.payload);
|
2015-01-06 15:54:45 +01:00
|
|
|
if ((out >= 0) && (out <= 255)) {
|
|
|
|
if (node.child !== null) { node.child.stdin.write(out+"\n"); }
|
|
|
|
else { node.warn("Command not running"); }
|
2014-07-25 11:07:23 +02:00
|
|
|
}
|
|
|
|
else { node.warn("Invalid input - not between 0 and 255"); }
|
|
|
|
});
|
|
|
|
}
|
2015-01-06 15:54:45 +01:00
|
|
|
|
2014-07-25 11:07:23 +02:00
|
|
|
node.on("close", function() {
|
2015-01-06 15:54:45 +01:00
|
|
|
if (node.child != null) {
|
|
|
|
node.child.kill('SIGKILL');
|
|
|
|
}
|
|
|
|
if (RED.settings.verbose) { node.log("end"); }
|
2014-07-25 11:07:23 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("rpi-liter",PiLiter);
|
|
|
|
}
|