2013-09-19 11:08:00 +02:00
|
|
|
/**
|
2014-10-19 14:54:21 +02:00
|
|
|
* Copyright 2013,2014 IBM Corp.
|
2013-09-19 11:08:00 +02:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
module.exports = function(RED) {
|
2014-05-29 23:13:21 +02:00
|
|
|
"use strict";
|
2014-05-04 00:32:04 +02:00
|
|
|
var exec = require('child_process').exec;
|
2014-12-27 14:11:44 +01:00
|
|
|
var spawn = require('child_process').spawn;
|
2014-05-04 00:32:04 +02:00
|
|
|
var fs = require('fs');
|
2014-05-06 21:58:36 +02:00
|
|
|
|
2014-12-27 14:11:44 +01:00
|
|
|
var gpioCommand = __dirname+'/nrgpio';
|
2014-09-03 21:03:52 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi
|
2015-05-10 22:47:22 +02:00
|
|
|
//RED.log.info(RED._("rpi-gpio.errors.ignorenode"));
|
|
|
|
throw "Info : "+RED._("rpi-gpio.errors.ignorenode");
|
2013-11-03 20:09:45 +01:00
|
|
|
}
|
2014-05-06 21:58:36 +02:00
|
|
|
|
2014-12-27 14:11:44 +01:00
|
|
|
if (!fs.existsSync("/usr/share/doc/python-rpi.gpio")) {
|
2015-05-10 22:47:22 +02:00
|
|
|
RED.log.warn(RED._("rpi-gpio.errors.libnotfound"));
|
|
|
|
throw "Warning : "+RED._("rpi-gpio.errors.libnotfound");
|
2013-11-03 20:09:45 +01:00
|
|
|
}
|
2014-05-06 21:58:36 +02:00
|
|
|
|
2014-12-27 14:11:44 +01:00
|
|
|
if ( !(1 & parseInt ((fs.statSync(gpioCommand).mode & parseInt ("777", 8)).toString (8)[0]) )) {
|
2015-05-28 16:29:01 +02:00
|
|
|
RED.log.error(RED._("rpi-gpio.errors.needtobeexecutable",{command:gpioCommand}));
|
2015-05-10 22:47:22 +02:00
|
|
|
throw "Error : "+RED._("rpi-gpio.errors.mustbeexecutable");
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2014-05-06 21:58:36 +02:00
|
|
|
|
2015-01-06 23:06:28 +01:00
|
|
|
// the magic to make python print stuff immediately
|
|
|
|
process.env.PYTHONUNBUFFERED = 1;
|
|
|
|
|
2014-12-27 14:11:44 +01:00
|
|
|
var pinsInUse = {};
|
2015-05-28 16:29:01 +02:00
|
|
|
var pinTypes = {"out":RED._("rpi-gpio.types.digout"), "tri":RED._("rpi-gpio.types.input"), "up":RED._("rpi-gpio.types.pullup"), "down":RED._("rpi-gpio.types.pulldown"), "pwm":RED._("rpi-gpio.types.pwmout")};
|
2014-12-27 14:11:44 +01:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function GPIOInNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.buttonState = -1;
|
2014-12-27 14:11:44 +01:00
|
|
|
this.pin = n.pin;
|
2014-05-04 00:32:04 +02:00
|
|
|
this.intype = n.intype;
|
2014-10-19 14:54:21 +02:00
|
|
|
this.read = n.read || false;
|
|
|
|
if (this.read) { this.buttonState = -2; }
|
2014-05-04 00:32:04 +02:00
|
|
|
var node = this;
|
2014-12-27 14:11:44 +01:00
|
|
|
if (!pinsInUse.hasOwnProperty(this.pin)) {
|
|
|
|
pinsInUse[this.pin] = this.intype;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((pinsInUse[this.pin] !== this.intype)||(pinsInUse[this.pin] === "pwm")) {
|
2015-05-28 16:29:01 +02:00
|
|
|
node.warn(RED._("rpi-gpio.errors.alreadyset",{pin:this.pin,type:pinTypes[pinsInUse[this.pin]]}));
|
2014-12-27 14:11:44 +01:00
|
|
|
}
|
|
|
|
}
|
2014-05-06 21:58:36 +02:00
|
|
|
|
2014-07-18 16:08:16 +02:00
|
|
|
if (node.pin !== undefined) {
|
2014-12-27 14:11:44 +01:00
|
|
|
if (node.intype === "tri") {
|
|
|
|
node.child = spawn(gpioCommand, ["in",node.pin]);
|
|
|
|
} else {
|
|
|
|
node.child = spawn(gpioCommand, ["in",node.pin,node.intype]);
|
|
|
|
}
|
|
|
|
node.running = true;
|
2015-05-10 22:47:22 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:RED._("common.status.ok")});
|
2014-12-27 14:11:44 +01:00
|
|
|
|
|
|
|
node.child.stdout.on('data', function (data) {
|
|
|
|
data = data.toString().trim();
|
|
|
|
if (data.length > 0) {
|
|
|
|
if (node.buttonState !== -1) {
|
|
|
|
node.send({ topic:"pi/"+node.pin, payload:Number(data) });
|
|
|
|
}
|
|
|
|
node.buttonState = data;
|
|
|
|
node.status({fill:"green",shape:"dot",text:data});
|
|
|
|
if (RED.settings.verbose) { node.log("out: "+data+" :"); }
|
2013-11-03 20:09:45 +01:00
|
|
|
}
|
|
|
|
});
|
2014-12-27 14:11:44 +01:00
|
|
|
|
|
|
|
node.child.stderr.on('data', function (data) {
|
|
|
|
if (RED.settings.verbose) { node.log("err: "+data+" :"); }
|
|
|
|
});
|
|
|
|
|
|
|
|
node.child.on('close', function (code) {
|
|
|
|
node.child = null;
|
|
|
|
node.running = false;
|
2015-05-28 22:55:22 +02:00
|
|
|
if (RED.settings.verbose) { node.log(RED._("rpi-gpio.status.closed")); }
|
2015-02-03 18:22:32 +01:00
|
|
|
if (node.done) {
|
2015-05-28 22:55:22 +02:00
|
|
|
node.status({fill:"grey",shape:"ring",text:RED._("rpi-gpio.status.closed")});
|
2015-02-03 18:22:32 +01:00
|
|
|
node.done();
|
|
|
|
}
|
2015-05-28 22:55:22 +02:00
|
|
|
else { node.status({fill:"red",shape:"ring",text:RED._("rpi-gpio.status.stopped")}); }
|
2014-12-27 14:11:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
node.child.on('error', function (err) {
|
2015-05-10 22:47:22 +02:00
|
|
|
if (err.errno === "ENOENT") { node.error(RED._("rpi-gpio.errors.commandnotfound")); }
|
|
|
|
else if (err.errno === "EACCES") { node.error(RED._("rpi-gpio.errors.commandnotexecutable")); }
|
2015-05-28 16:29:01 +02:00
|
|
|
else { node.error(RED._("rpi-gpio.errors.error",{error:err.errnp})) }
|
2014-12-27 14:11:44 +01:00
|
|
|
});
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-05-10 22:47:22 +02:00
|
|
|
node.warn(RED._("rpi-gpio.errors.invalidpin")+": "+node.pin);
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2014-05-06 21:58:36 +02:00
|
|
|
|
2015-01-14 10:55:30 +01:00
|
|
|
node.on("close", function(done) {
|
2015-05-28 22:55:22 +02:00
|
|
|
node.status({fill:"grey",shape:"ring",text:RED._("rpi-gpio.status.closed")});
|
2015-01-14 10:55:30 +01:00
|
|
|
delete pinsInUse[node.pin];
|
2014-12-27 14:11:44 +01:00
|
|
|
if (node.child != null) {
|
2015-01-29 22:42:33 +01:00
|
|
|
node.done = done;
|
2015-02-25 20:10:44 +01:00
|
|
|
node.child.stdin.write("close "+node.pin);
|
2014-12-27 14:11:44 +01:00
|
|
|
node.child.kill('SIGKILL');
|
|
|
|
}
|
2015-01-29 22:42:33 +01:00
|
|
|
else { done(); }
|
2013-11-03 20:09:45 +01:00
|
|
|
});
|
|
|
|
}
|
2014-12-27 14:11:44 +01:00
|
|
|
RED.nodes.registerType("rpi-gpio in",GPIOInNode);
|
|
|
|
|
2014-05-06 21:58:36 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
function GPIOOutNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2014-12-27 14:11:44 +01:00
|
|
|
this.pin = n.pin;
|
2014-10-19 14:54:21 +02:00
|
|
|
this.set = n.set || false;
|
|
|
|
this.level = n.level || 0;
|
2014-11-10 21:03:51 +01:00
|
|
|
this.out = n.out || "out";
|
2014-05-04 00:32:04 +02:00
|
|
|
var node = this;
|
2014-12-27 14:11:44 +01:00
|
|
|
if (!pinsInUse.hasOwnProperty(this.pin)) {
|
|
|
|
pinsInUse[this.pin] = this.out;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if ((pinsInUse[this.pin] !== this.out)||(pinsInUse[this.pin] === "pwm")) {
|
2015-05-28 16:29:01 +02:00
|
|
|
node.warn(RED._("rpi-gpio.errors.alreadyset",{pin:this.pin,type:pinTypes[pinsInUse[this.pin]]}));
|
2014-12-27 14:11:44 +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 ((out >= 0) && (out <= limit)) {
|
2015-01-14 10:55:30 +01:00
|
|
|
if (RED.settings.verbose) { node.log("out: "+msg.payload); }
|
2015-02-03 18:22:32 +01:00
|
|
|
if (node.child !== null) {
|
|
|
|
node.child.stdin.write(msg.payload+"\n");
|
2015-02-04 20:02:32 +01:00
|
|
|
node.status({fill:"green",shape:"dot",text:msg.payload.toString()});
|
2015-02-03 18:22:32 +01:00
|
|
|
}
|
|
|
|
else {
|
2015-05-10 22:47:22 +02:00
|
|
|
node.error(RED._("rpi-gpio.errors.pythoncommandnotfound"),msg);
|
2015-05-28 22:55:22 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:RED._("rpi-gpio.status.not-running")});
|
2015-02-03 18:22:32 +01:00
|
|
|
}
|
2014-12-27 14:11:44 +01:00
|
|
|
}
|
2015-05-10 22:47:22 +02:00
|
|
|
else { node.warn(RED._("rpi-gpio.errors.invalidinput")+": "+out); }
|
2014-12-27 14:11:44 +01:00
|
|
|
}
|
2014-05-06 21:58:36 +02:00
|
|
|
|
2014-07-18 16:08:16 +02:00
|
|
|
if (node.pin !== undefined) {
|
2014-12-27 14:11:44 +01:00
|
|
|
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]);
|
|
|
|
}
|
|
|
|
node.running = true;
|
2015-05-10 22:47:22 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:RED._("common.status.ok")});
|
2014-12-27 14:11:44 +01:00
|
|
|
|
|
|
|
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+" :"); }
|
|
|
|
});
|
|
|
|
|
|
|
|
node.child.on('close', function (code) {
|
|
|
|
node.child = null;
|
|
|
|
node.running = false;
|
2015-05-28 22:55:22 +02:00
|
|
|
if (RED.settings.verbose) { node.log(RED._("rpi-gpio.status.closed")); }
|
2015-02-03 18:22:32 +01:00
|
|
|
if (node.done) {
|
2015-05-28 22:55:22 +02:00
|
|
|
node.status({fill:"grey",shape:"ring",text:RED._("rpi-gpio.status.closed")});
|
2015-02-03 18:22:32 +01:00
|
|
|
node.done();
|
|
|
|
}
|
2015-05-28 22:55:22 +02:00
|
|
|
else { node.status({fill:"red",shape:"ring",text:RED._("rpi-gpio.status.stopped")}); }
|
2014-12-27 14:11:44 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
node.child.on('error', function (err) {
|
2015-05-10 22:47:22 +02:00
|
|
|
if (err.errno === "ENOENT") { node.error(RED._("rpi-gpio.errors.commandnotfound")); }
|
|
|
|
else if (err.errno === "EACCES") { node.error(RED._("rpi-gpio.errors.commandnotexecutable")); }
|
|
|
|
else { node.error(RED._("rpi-gpio.errors.error")+': ' + err.errno); }
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-12-27 14:11:44 +01:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-05-10 22:47:22 +02:00
|
|
|
node.warn(RED._("rpi-gpio.errors.invalidpin")+": "+node.pin);
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2014-05-06 21:58:36 +02:00
|
|
|
|
2015-01-14 10:55:30 +01:00
|
|
|
node.on("close", function(done) {
|
2015-05-28 22:55:22 +02:00
|
|
|
node.status({fill:"grey",shape:"ring",text:RED._("rpi-gpio.status.closed")});
|
2015-01-14 10:55:30 +01:00
|
|
|
delete pinsInUse[node.pin];
|
2014-12-27 14:11:44 +01:00
|
|
|
if (node.child != null) {
|
2015-01-29 22:42:33 +01:00
|
|
|
node.done = done;
|
2015-02-25 20:10:44 +01:00
|
|
|
node.child.stdin.write("close "+node.pin);
|
2014-12-27 14:11:44 +01:00
|
|
|
node.child.kill('SIGKILL');
|
|
|
|
}
|
2015-01-29 22:42:33 +01:00
|
|
|
else { done(); }
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
2014-12-27 14:11:44 +01:00
|
|
|
|
2013-11-03 20:09:45 +01:00
|
|
|
}
|
2014-05-06 21:58:36 +02:00
|
|
|
|
2014-07-18 16:08:16 +02:00
|
|
|
var pitype = { type:"" };
|
2014-12-27 14:11:44 +01:00
|
|
|
exec(gpioCommand+" rev 0", function(err,stdout,stderr) {
|
2014-07-18 16:08:16 +02:00
|
|
|
if (err) {
|
2015-05-10 22:47:22 +02:00
|
|
|
RED.log.info(RED._("rpi-gpio.errors.version"));
|
2014-07-18 16:08:16 +02:00
|
|
|
}
|
|
|
|
else {
|
2014-12-27 14:11:44 +01:00
|
|
|
if (stdout.trim() == "0") { pitype = { type:"Compute" }; }
|
|
|
|
else if (stdout.trim() == "1") { pitype = { type:"A/B v1" }; }
|
|
|
|
else if (stdout.trim() == "2") { pitype = { type:"A/B v2" }; }
|
|
|
|
else if (stdout.trim() == "3") { pitype = { type:"Model B+" }; }
|
2015-05-10 22:47:22 +02:00
|
|
|
else { RED.log.info(RED._("rpi-gpio.errors.sawpitype"),stdout.trim()); }
|
2014-07-18 16:08:16 +02:00
|
|
|
}
|
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.nodes.registerType("rpi-gpio out",GPIOOutNode);
|
2014-07-18 16:08:16 +02:00
|
|
|
|
2015-01-06 23:06:28 +01:00
|
|
|
function PiMouseNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.butt = n.butt || 7;
|
|
|
|
var node = this;
|
|
|
|
|
|
|
|
node.child = spawn(gpioCommand+".py", ["mouse",node.butt]);
|
2015-05-10 22:47:22 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:RED._("common.status.ok")});
|
2015-01-06 23:06:28 +01:00
|
|
|
|
|
|
|
node.child.stdout.on('data', function (data) {
|
|
|
|
data = Number(data);
|
|
|
|
if (data === 0) { node.send({ topic:"pi/mouse", button:data, payload:0 }); }
|
|
|
|
else { node.send({ topic:"pi/mouse", button:data, payload:1 }); }
|
|
|
|
});
|
|
|
|
|
|
|
|
node.child.stderr.on('data', function (data) {
|
|
|
|
if (RED.settings.verbose) { node.log("err: "+data+" :"); }
|
|
|
|
});
|
|
|
|
|
|
|
|
node.child.on('close', function (code) {
|
|
|
|
node.child = null;
|
|
|
|
node.running = false;
|
2015-05-28 22:55:22 +02:00
|
|
|
if (RED.settings.verbose) { node.log(RED._("rpi-gpio.status.closed")); }
|
2015-02-03 18:22:32 +01:00
|
|
|
if (node.done) {
|
2015-05-28 22:55:22 +02:00
|
|
|
node.status({fill:"grey",shape:"ring",text:RED._("rpi-gpio.status.closed")});
|
2015-02-03 18:22:32 +01:00
|
|
|
node.done();
|
|
|
|
}
|
2015-05-28 22:55:22 +02:00
|
|
|
else { node.status({fill:"red",shape:"ring",text:RED._("rpi-gpio.status.stopped")}); }
|
2015-01-06 23:06:28 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
node.child.on('error', function (err) {
|
2015-05-10 22:47:22 +02:00
|
|
|
if (err.errno === "ENOENT") { node.error(RED._("rpi-gpio.errors.commandnotfound")); }
|
|
|
|
else if (err.errno === "EACCES") { node.error(RED._("rpi-gpio.errors.commandnotexecutable")); }
|
|
|
|
else { node.error(RED._("rpi-gpio.errors.error")+': ' + err.errno); }
|
2015-01-06 23:06:28 +01:00
|
|
|
});
|
|
|
|
|
2015-01-14 10:55:30 +01:00
|
|
|
node.on("close", function(done) {
|
2015-05-28 22:55:22 +02:00
|
|
|
node.status({fill:"grey",shape:"ring",text:RED._("rpi-gpio.status.closed")});
|
2015-01-06 23:06:28 +01:00
|
|
|
if (node.child != null) {
|
2015-01-29 22:42:33 +01:00
|
|
|
node.done = done;
|
2015-01-06 23:06:28 +01:00
|
|
|
node.child.kill('SIGINT');
|
|
|
|
node.child = null;
|
|
|
|
}
|
2015-01-29 22:42:33 +01:00
|
|
|
else { done(); }
|
2015-01-06 23:06:28 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("rpi-mouse",PiMouseNode);
|
|
|
|
|
2015-02-06 14:57:15 +01:00
|
|
|
RED.httpAdmin.get('/rpi-gpio/:id', RED.auth.needsPermission('rpi-gpio.read'), function(req,res) {
|
|
|
|
res.json(pitype);
|
2014-07-18 16:08:16 +02:00
|
|
|
});
|
2014-12-27 14:11:44 +01:00
|
|
|
|
2015-02-06 14:57:15 +01:00
|
|
|
RED.httpAdmin.get('/rpi-pins/:id', RED.auth.needsPermission('rpi-gpio.read'), function(req,res) {
|
|
|
|
res.json(pinsInUse);
|
2014-12-27 14:11:44 +01:00
|
|
|
});
|
2013-09-19 11:08:00 +02:00
|
|
|
}
|