2013-10-04 09:22:35 +02:00
|
|
|
|
2014-06-29 00:35:21 +02:00
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
2015-01-30 09:43:55 +01:00
|
|
|
var util = require("util");
|
2015-04-16 11:58:34 +02:00
|
|
|
//var exec = require('child_process').exec;
|
2015-01-30 09:43:55 +01:00
|
|
|
var spawn = require('child_process').spawn;
|
2015-05-11 20:25:39 +02:00
|
|
|
var fs = require('fs');
|
2015-11-21 17:36:27 +01:00
|
|
|
var LedBorgInUse = false;
|
2013-10-04 09:22:35 +02:00
|
|
|
|
2015-01-30 09:43:55 +01:00
|
|
|
var gpioCommand = __dirname+'/nrgpio';
|
|
|
|
|
2016-11-02 11:02:07 +01:00
|
|
|
try {
|
|
|
|
var cpuinfo = fs.readFileSync("/proc/cpuinfo").toString();
|
|
|
|
if (cpuinfo.indexOf(": BCM") === -1) { throw "Info : "+RED._("rpi-gpio.errors.ignorenode"); }
|
2017-01-29 18:45:44 +01:00
|
|
|
}
|
|
|
|
catch(err) {
|
2016-11-02 11:02:07 +01:00
|
|
|
throw "Info : "+RED._("rpi-gpio.errors.ignorenode");
|
2015-01-30 09:43:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!fs.existsSync("/usr/share/doc/python-rpi.gpio")) {
|
2015-03-12 14:54:20 +01:00
|
|
|
util.log("[rpi-ledborg] Info : Can't find RPi.GPIO python library.");
|
2015-01-30 09:43:55 +01:00
|
|
|
throw "Warning : Can't find RPi.GPIO python library.";
|
2014-06-29 00:35:21 +02:00
|
|
|
}
|
2013-10-04 09:22:35 +02:00
|
|
|
|
2015-01-30 09:43:55 +01:00
|
|
|
if ( !(1 & parseInt ((fs.statSync(gpioCommand).mode & parseInt ("777", 8)).toString (8)[0]) )) {
|
2015-03-12 14:54:20 +01:00
|
|
|
util.log("[rpi-ledborg] Error : "+gpioCommand+" needs to be executable.");
|
2015-01-30 09:43:55 +01:00
|
|
|
throw "Error : nrgpio must to be executable.";
|
|
|
|
}
|
|
|
|
|
|
|
|
// GPIO pins 11 (R), 13 (G), 15 (B).
|
|
|
|
|
2014-06-29 00:35:21 +02:00
|
|
|
function LedBorgNode(n) {
|
2015-01-30 09:43:55 +01:00
|
|
|
RED.nodes.createNode(this,n);
|
2015-11-21 17:36:27 +01:00
|
|
|
if (LedBorgInUse) { this.error("LEDborg node already in use - you may only have one."); }
|
|
|
|
else { LedBorgInUse = true; }
|
2015-01-30 09:43:55 +01:00
|
|
|
this.pin = n.pin;
|
|
|
|
this.set = n.set || false;
|
|
|
|
this.level = n.level || 0;
|
|
|
|
this.out = n.out || "out";
|
|
|
|
var node = this;
|
2014-06-29 00:35:21 +02:00
|
|
|
var p1 = /^[0-2][0-2][0-2]$/
|
|
|
|
var p2 = /^\#[A-Fa-f0-9]{6}$/
|
2015-01-30 09:43:55 +01:00
|
|
|
var p3 = /^\d+,\d+,\d+$/
|
|
|
|
|
|
|
|
function inputlistener(msg) {
|
|
|
|
var rgb = "000";
|
2013-10-04 09:22:35 +02:00
|
|
|
|
2015-12-24 15:29:37 +01:00
|
|
|
if (typeof msg.payload === "number") {
|
|
|
|
msg.payload = ("000"+msg.payload.toString()).substr(-3);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof msg.payload === "boolean") {
|
|
|
|
msg.payload = msg.payload ? "222" : "000";
|
|
|
|
}
|
|
|
|
|
2014-06-29 00:35:21 +02:00
|
|
|
if (p1.test(msg.payload)) {
|
2015-01-30 09:43:55 +01:00
|
|
|
rgb = msg.payload;
|
|
|
|
rgb = Number(rgb[0])*50+","+Number(rgb[1])*50+","+Number(rgb[2])*50;
|
2014-06-29 00:35:21 +02:00
|
|
|
}
|
|
|
|
else if (p2.test(msg.payload)) {
|
2015-01-30 09:43:55 +01:00
|
|
|
var r = Math.floor(parseInt(msg.payload.slice(1,3),16)*100/256).toString();
|
|
|
|
var g = Math.floor(parseInt(msg.payload.slice(3,5),16)*100/256).toString();
|
|
|
|
var b = Math.floor(parseInt(msg.payload.slice(5),16)*100/256).toString();
|
|
|
|
rgb = r+","+g+","+b;
|
|
|
|
}
|
|
|
|
else if (p3.test(msg.payload)) {
|
|
|
|
var c = msg.payload.split(",");
|
2015-02-05 14:52:20 +01:00
|
|
|
var r1 = Math.floor(parseInt(c[0])*100/256).toString();
|
|
|
|
var g1 = Math.floor(parseInt(c[1])*100/256).toString();
|
|
|
|
var b1 = Math.floor(parseInt(c[2])*100/256).toString();
|
|
|
|
rgb = r1+","+g1+","+b1;
|
2014-01-07 17:00:55 +01:00
|
|
|
}
|
|
|
|
else {
|
2014-06-29 00:35:21 +02:00
|
|
|
// you can add fancy colours by name here if you want...
|
|
|
|
// these are the @cheerlight ones.
|
2015-03-12 12:12:44 +01:00
|
|
|
var colors = {"red":"200","green":"020","blue":"002","cyan":"022","white":"222","pink":"201","oldlace":"221",
|
2015-11-30 17:58:02 +01:00
|
|
|
"warmwhite":"221","purple":"101","magenta":"202","yellow":"220","amber":"220","orange":"210","black":"000","off":"000"}
|
2014-06-29 00:35:21 +02:00
|
|
|
if (msg.payload.toLowerCase() in colors) {
|
2015-01-30 09:43:55 +01:00
|
|
|
rgb = colors[msg.payload.toLowerCase()];
|
|
|
|
rgb = Number(rgb[0])*50+","+Number(rgb[1])*50+","+Number(rgb[2])*50;
|
2014-06-29 00:35:21 +02:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.warn("Invalid LedBorg colour code");
|
|
|
|
}
|
2014-01-07 17:00:55 +01:00
|
|
|
}
|
2015-01-30 09:43:55 +01:00
|
|
|
|
|
|
|
if (RED.settings.verbose) { node.log("out: "+msg.payload); }
|
2016-01-01 15:35:54 +01:00
|
|
|
if (node.child !== null) {
|
|
|
|
node.child.stdin.write(rgb+"\n");
|
|
|
|
node.status({fill:"green",shape:"dot",text:msg.payload});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.warn("Command not running");
|
|
|
|
node.status({fill:"red",shape:"ring",text:"Command not running"});
|
|
|
|
}
|
2015-01-30 09:43:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
node.child = spawn(gpioCommand, ["borg","11"]);
|
|
|
|
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+" :"); }
|
|
|
|
});
|
|
|
|
|
2015-04-16 11:58:34 +02:00
|
|
|
node.child.on('close', function () {
|
2015-01-30 09:43:55 +01:00
|
|
|
node.child = null;
|
|
|
|
node.running = false;
|
|
|
|
node.status({fill:"red",shape:"circle",text:""});
|
|
|
|
if (RED.settings.verbose) { node.log("closed"); }
|
|
|
|
if (node.done) { node.done(); }
|
|
|
|
});
|
|
|
|
|
|
|
|
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); }
|
|
|
|
});
|
|
|
|
|
|
|
|
node.on("close", function(done) {
|
2015-11-21 17:36:27 +01:00
|
|
|
LedBorgInUse = false;
|
2015-01-30 09:43:55 +01:00
|
|
|
node.status({fill:"red",shape:"circle",text:""});
|
|
|
|
if (node.child != null) {
|
|
|
|
node.done = done;
|
|
|
|
node.child.stdin.write(" close 11");
|
|
|
|
node.child.kill('SIGKILL');
|
|
|
|
}
|
|
|
|
else { done(); }
|
|
|
|
|
2014-06-29 00:35:21 +02:00
|
|
|
});
|
2015-01-30 09:43:55 +01:00
|
|
|
|
2014-06-29 00:35:21 +02:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("ledborg",LedBorgNode);
|
2013-10-04 09:22:35 +02:00
|
|
|
}
|