2014-03-31 15:27:21 +02:00
|
|
|
|
2014-06-29 00:35:21 +02:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
|
|
|
var util = require("util");
|
2014-12-26 16:24:21 +01:00
|
|
|
var spawn = require('child_process').spawn;
|
2015-05-11 20:25:39 +02:00
|
|
|
var fs = require('fs');
|
2014-03-31 15:27:21 +02:00
|
|
|
|
2016-02-12 22:27:02 +01:00
|
|
|
var gpioCommand = __dirname+'/nrgpio';
|
2014-12-26 16:24:21 +01:00
|
|
|
|
2014-06-29 00:35:21 +02:00
|
|
|
if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi
|
2014-12-26 16:24:21 +01:00
|
|
|
//util.log("Info : Ignoring Raspberry Pibrella specific node.");
|
|
|
|
throw "Info : Ignoring Raspberry Pibrella specific node.";
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.existsSync("/usr/share/doc/python-rpi.gpio")) {
|
|
|
|
util.log("[rpi-pibrella] Info : Can't find RPi.GPIO python library.");
|
|
|
|
throw "Warning : Can't find RPi.GPIO python library.";
|
2014-06-29 00:35:21 +02:00
|
|
|
}
|
2014-04-08 16:54:08 +02:00
|
|
|
|
2014-12-26 16:24:21 +01:00
|
|
|
if ( !(1 & parseInt ((fs.statSync(gpioCommand).mode & parseInt ("777", 8)).toString (8)[0]) )) {
|
|
|
|
util.log("[rpi-pibrella] Error : "+gpioCommand+" needs to be executable.");
|
2015-12-12 17:33:39 +01:00
|
|
|
throw "Error : " + gpioCommand + " must to be executable.";
|
2014-06-29 00:35:21 +02:00
|
|
|
}
|
2014-03-31 15:27:21 +02:00
|
|
|
|
2014-12-26 16:24:21 +01:00
|
|
|
var pinsInUse = {};
|
|
|
|
var pinTypes = {"out":"digital output", "tri":"input", "up":"input with pull up", "down":"input with pull down", "pwm":"PWM output"};
|
|
|
|
|
2014-06-29 00:35:21 +02:00
|
|
|
var pintable = {
|
2014-12-26 16:24:21 +01:00
|
|
|
// Name : Pin
|
|
|
|
"Amber LED":"11",
|
|
|
|
"Buzzer ":"12",
|
|
|
|
"Red LED":"13",
|
|
|
|
"Out E":"15",
|
|
|
|
"Out F":"16",
|
|
|
|
"Out G":"18",
|
|
|
|
"Out H":"22",
|
|
|
|
"Green LED":"7",
|
|
|
|
"In D":"19",
|
|
|
|
"In A":"21",
|
|
|
|
"In B":"26",
|
|
|
|
"In C":"24",
|
|
|
|
"Red Button":"23"
|
2014-06-29 00:35:21 +02:00
|
|
|
}
|
|
|
|
var tablepin = {
|
2014-12-26 16:24:21 +01:00
|
|
|
// Pin : Name
|
|
|
|
"11":"Amber",
|
|
|
|
"12":"Buzzer",
|
|
|
|
"13":"Red",
|
|
|
|
"15":"E",
|
|
|
|
"16":"F",
|
|
|
|
"18":"G",
|
|
|
|
"22":"H",
|
|
|
|
"7":"Green",
|
|
|
|
"19":"D",
|
|
|
|
"21":"A",
|
|
|
|
"26":"B",
|
|
|
|
"24":"C",
|
|
|
|
"23":"R"
|
2014-06-29 00:35:21 +02:00
|
|
|
}
|
2014-03-31 15:27:21 +02:00
|
|
|
|
2014-06-29 00:35:21 +02:00
|
|
|
function PibrellaIn(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.buttonState = -1;
|
|
|
|
this.pin = pintable[n.pin];
|
2014-10-27 21:28:05 +01:00
|
|
|
this.read = n.read || false;
|
|
|
|
if (this.read) { this.buttonState = -2; }
|
2014-06-29 00:35:21 +02:00
|
|
|
var node = this;
|
2014-12-26 16:24:21 +01:00
|
|
|
if (!pinsInUse.hasOwnProperty(this.pin)) {
|
|
|
|
pinsInUse[this.pin] = "tri";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((pinsInUse[this.pin] !== "tri")||(pinsInUse[this.pin] === "pwm")) {
|
|
|
|
node.error("GPIO pin "+this.pin+" already set as "+pinTypes[pinsInUse[this.pin]]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node.pin !== undefined) {
|
2016-02-12 22:27:02 +01:00
|
|
|
node.child = spawn(gpioCommand, ["in",node.pin,"down",35]);
|
2014-12-26 16:24:21 +01:00
|
|
|
node.running = true;
|
|
|
|
node.status({fill:"green",shape:"dot",text:"OK"});
|
2014-03-31 15:27:21 +02:00
|
|
|
|
2014-12-26 16:24:21 +01:00
|
|
|
node.child.stdout.on('data', function (data) {
|
|
|
|
data = data.toString().trim();
|
|
|
|
if (data.length > 0) {
|
|
|
|
if (node.buttonState !== -1) {
|
|
|
|
node.send({ topic:"pibrella/"+tablepin[node.pin], payload:Number(data) });
|
|
|
|
}
|
|
|
|
node.buttonState = data;
|
|
|
|
node.status({fill:"green",shape:"dot",text:data});
|
|
|
|
if (RED.settings.verbose) { node.log("out: "+data+" :"); }
|
2014-06-29 00:35:21 +02:00
|
|
|
}
|
|
|
|
});
|
2014-12-26 16:24:21 +01:00
|
|
|
|
|
|
|
node.child.stderr.on('data', function (data) {
|
|
|
|
if (RED.settings.verbose) { node.log("err: "+data+" :"); }
|
|
|
|
});
|
|
|
|
|
|
|
|
node.child.on('close', function (code) {
|
|
|
|
if (RED.settings.verbose) { node.log("ret: "+code+" :"); }
|
|
|
|
node.child = null;
|
|
|
|
node.running = false;
|
|
|
|
node.status({fill:"red",shape:"circle",text:""});
|
|
|
|
});
|
|
|
|
|
|
|
|
node.child.on('error', function (err) {
|
|
|
|
if (err.errno === "ENOENT") { node.warn('Command not found'); }
|
|
|
|
else if (err.errno === "EACCES") { node.warn('Command not executable'); }
|
|
|
|
else { node.log('error: ' + err); }
|
|
|
|
});
|
|
|
|
|
2014-06-29 00:35:21 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.error("Invalid GPIO pin: "+node.pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
node.on("close", function() {
|
2014-12-26 16:24:21 +01:00
|
|
|
if (node.child != null) {
|
|
|
|
node.child.stdin.write(" close "+node.pin);
|
|
|
|
node.child.kill('SIGKILL');
|
|
|
|
}
|
|
|
|
node.status({fill:"red",shape:"circle",text:""});
|
|
|
|
delete pinsInUse[node.pin];
|
|
|
|
if (RED.settings.verbose) { node.log("end"); }
|
2014-03-31 15:27:21 +02:00
|
|
|
});
|
|
|
|
}
|
2014-12-26 16:24:21 +01:00
|
|
|
RED.nodes.registerType("rpi-pibrella in",PibrellaIn);
|
|
|
|
|
2014-04-08 16:54:08 +02:00
|
|
|
|
2014-06-29 00:35:21 +02:00
|
|
|
function PibrellaOut(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.pin = pintable[n.pin];
|
2014-10-27 21:28:05 +01:00
|
|
|
this.set = n.set || false;
|
|
|
|
this.level = n.level || 0;
|
2014-12-26 16:24:21 +01:00
|
|
|
this.out = n.out || "out";
|
2014-06-29 00:35:21 +02:00
|
|
|
var node = this;
|
2014-12-26 16:24:21 +01:00
|
|
|
if (!pinsInUse.hasOwnProperty(this.pin)) {
|
|
|
|
pinsInUse[this.pin] = this.out;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((pinsInUse[this.pin] !== this.out)||(pinsInUse[this.pin] === "pwm")) {
|
|
|
|
node.error("GPIO pin "+this.pin+" already set as "+pinTypes[pinsInUse[this.pin]]);
|
|
|
|
}
|
|
|
|
}
|
2014-03-31 15:27:21 +02:00
|
|
|
|
2014-12-26 16:24:21 +01:00
|
|
|
function inputlistener(msg) {
|
|
|
|
if (msg.payload === "true") { msg.payload = true; }
|
|
|
|
if (msg.payload === "false") { msg.payload = false; }
|
|
|
|
var out = Number(msg.payload);
|
|
|
|
var limit = 1;
|
|
|
|
if (node.out === "pwm") { limit = 100; }
|
|
|
|
if (node.pin === "12") {
|
|
|
|
limit = 4096;
|
|
|
|
if (out === 1) { out = 262; }
|
|
|
|
}
|
|
|
|
if ((out >= 0) && (out <= limit)) {
|
|
|
|
if (RED.settings.verbose) { node.log("inp: "+msg.payload); }
|
|
|
|
if (node.child !== null) { node.child.stdin.write(out+"\n"); }
|
|
|
|
else { node.warn("Command not running"); }
|
|
|
|
node.status({fill:"green",shape:"dot",text:msg.payload});
|
|
|
|
}
|
|
|
|
else { node.warn("Invalid input: "+out); }
|
2014-06-29 00:35:21 +02:00
|
|
|
}
|
2014-12-26 16:24:21 +01:00
|
|
|
|
|
|
|
if (node.pin !== undefined) {
|
|
|
|
if (node.pin === "12") {
|
|
|
|
node.child = spawn(gpioCommand, ["buzz",node.pin]);
|
|
|
|
} else {
|
|
|
|
if (node.set && (node.out === "out")) {
|
|
|
|
node.child = spawn(gpioCommand, [node.out,node.pin,node.level]);
|
|
|
|
} else {
|
|
|
|
node.child = spawn(gpioCommand, [node.out,node.pin]);
|
2014-10-27 21:28:05 +01:00
|
|
|
}
|
2014-12-26 16:24:21 +01:00
|
|
|
}
|
|
|
|
node.running = true;
|
|
|
|
node.status({fill:"green",shape:"dot",text:"OK"});
|
|
|
|
|
|
|
|
node.on("input", inputlistener);
|
|
|
|
|
|
|
|
node.child.stdout.on('data', function (data) {
|
|
|
|
if (RED.settings.verbose) { node.log("out: "+data+" :"); }
|
|
|
|
});
|
|
|
|
|
|
|
|
node.child.stderr.on('data', function (data) {
|
|
|
|
if (RED.settings.verbose) { node.log("err: "+data+" :"); }
|
2014-03-31 15:27:21 +02:00
|
|
|
});
|
2014-12-26 16:24:21 +01:00
|
|
|
|
|
|
|
node.child.on('close', function (code) {
|
|
|
|
if (RED.settings.verbose) { node.log("ret: "+code+" :"); }
|
|
|
|
node.child = null;
|
|
|
|
node.running = false;
|
|
|
|
node.status({fill:"red",shape:"circle",text:""});
|
|
|
|
});
|
|
|
|
|
|
|
|
node.child.on('error', function (err) {
|
|
|
|
if (err.errno === "ENOENT") { node.warn('Command not found'); }
|
|
|
|
else if (err.errno === "EACCES") { node.warn('Command not executable'); }
|
|
|
|
else { node.log('error: ' + err); }
|
|
|
|
});
|
|
|
|
|
2014-06-29 00:35:21 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.error("Invalid GPIO pin: "+node.pin);
|
|
|
|
}
|
|
|
|
|
|
|
|
node.on("close", function() {
|
2014-12-26 16:24:21 +01:00
|
|
|
if (node.child != null) {
|
|
|
|
node.child.stdin.write(" close "+node.pin);
|
|
|
|
node.child.kill('SIGKILL');
|
|
|
|
}
|
|
|
|
node.status({fill:"red",shape:"circle",text:""});
|
|
|
|
delete pinsInUse[node.pin];
|
|
|
|
if (RED.settings.verbose) { node.log("end"); }
|
2014-03-31 15:27:21 +02:00
|
|
|
});
|
2014-04-08 16:54:08 +02:00
|
|
|
|
2014-12-26 16:24:21 +01:00
|
|
|
}
|
2014-06-29 00:35:21 +02:00
|
|
|
RED.nodes.registerType("rpi-pibrella out",PibrellaOut);
|
2014-12-26 16:24:21 +01:00
|
|
|
|
2015-02-06 22:10:14 +01:00
|
|
|
RED.httpAdmin.get('/rpi-pibpins/:id',RED.auth.needsPermission('rpi-pibrella.read'),function(req,res) {
|
|
|
|
res.json(pinsInUse);
|
2014-12-26 16:24:21 +01:00
|
|
|
});
|
2014-06-29 00:35:21 +02:00
|
|
|
}
|