2016-01-11 22:21:54 +01:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
2016-02-26 23:01:00 +01:00
|
|
|
var fs = require('fs');
|
2016-01-11 22:21:54 +01:00
|
|
|
var PNG = require('pngjs').PNG;
|
2016-02-26 23:01:00 +01:00
|
|
|
var spawn = require('child_process').spawn;
|
2017-09-01 16:22:35 +02:00
|
|
|
var execSync = require('child_process').execSync;
|
2016-01-11 22:21:54 +01:00
|
|
|
|
|
|
|
var hatCommand = __dirname+'/unihat';
|
2018-04-18 18:44:47 +02:00
|
|
|
var allOK = true;
|
2016-01-11 22:21:54 +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-unicorn : "+RED._("node-red:rpi-gpio.errors.ignorenode"));
|
|
|
|
allOK = false;
|
|
|
|
}
|
2021-12-08 10:38:50 +01:00
|
|
|
else if (!fs.existsSync("/usr/share/doc/python-rpi.gpio") && !fs.existsSync("/usr/share/doc/python3-rpi.gpio")) {
|
2018-04-15 12:24:56 +02:00
|
|
|
RED.log.warn("rpi-unicorn : "+RED._("node-red:rpi-gpio.errors.libnotfound"));
|
|
|
|
allOK = false;
|
|
|
|
}
|
2018-04-18 18:44:47 +02:00
|
|
|
else if (!(1 & parseInt ((fs.statSync(hatCommand).mode & parseInt ("777", 8)).toString (8)[0]))) {
|
2018-04-15 12:24:56 +02:00
|
|
|
RED.log.warn("rpi-unicorn : "+RED._("node-red:rpi-gpio.errors.needtobeexecutable",{command:hatCommand}));
|
|
|
|
allOK = false;
|
|
|
|
}
|
2016-01-11 22:21:54 +01:00
|
|
|
}
|
2018-04-15 12:24:56 +02:00
|
|
|
catch(err) {
|
|
|
|
RED.log.warn("rpi-unicorn : "+RED._("node-red:rpi-gpio.errors.ignorenode"));
|
|
|
|
allOK = false;
|
2016-01-11 22:21:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// the magic to make python print stuff immediately
|
|
|
|
process.env.PYTHONUNBUFFERED = 1;
|
|
|
|
|
|
|
|
function UnicornHatNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.png = n.png;
|
|
|
|
this.bright = n.bright || 20;
|
|
|
|
this.items = {};
|
|
|
|
var node = this;
|
|
|
|
|
|
|
|
var pic = new Buffer(192);
|
|
|
|
pic.fill(0);
|
|
|
|
var ready = false;
|
|
|
|
if (node.png) {
|
|
|
|
if (node.png.split(",").length === 3) {
|
|
|
|
var c = node.png.split(",");
|
|
|
|
for (var i=0; i<192; i++) {
|
|
|
|
pic[i] = c[0];
|
|
|
|
pic[i+1] = c[1];
|
|
|
|
pic[i+2] = c[2];
|
|
|
|
i += 2;
|
|
|
|
}
|
|
|
|
ready = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
try {
|
|
|
|
var file = fs.readFileSync(node.png);
|
|
|
|
new PNG().parse( file, function(error, data) {
|
|
|
|
if (error) {
|
|
|
|
node.warn("error reading : "+node.png);
|
|
|
|
ready = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var pix = data.data;
|
|
|
|
var j=0;
|
|
|
|
for (var i=0; i<192; i++) {
|
|
|
|
pic[i] = pix[j];
|
|
|
|
pic[i+1] = pix[j+1];
|
|
|
|
pic[i+2] = pix[j+2];
|
|
|
|
i += 2;
|
|
|
|
j += 4;
|
|
|
|
}
|
|
|
|
ready = true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
node.warn("error loading : "+node.png);
|
|
|
|
ready = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { ready = true; }
|
|
|
|
|
|
|
|
function inputlistener(msg) {
|
2016-03-22 23:42:31 +01:00
|
|
|
if (typeof msg.payload === "string") {
|
2016-04-09 19:00:14 +02:00
|
|
|
var a,b,i,j,x,y;
|
2016-03-22 23:42:31 +01:00
|
|
|
msg.payload = msg.payload.replace('"','');
|
|
|
|
var s = msg.payload.toUpperCase().split(",");
|
|
|
|
var doDraw = true;
|
|
|
|
if (s.length === 1) {
|
2016-03-26 18:34:07 +01:00
|
|
|
if ((s[0] == "CLS") || (s[0] == "CLR") || (s[0] == "CLEAR")) {
|
2016-03-22 23:42:31 +01:00
|
|
|
//console.log("CLEAR")
|
|
|
|
pic.fill(0);
|
|
|
|
node.items = {};
|
|
|
|
}
|
2016-03-26 18:34:07 +01:00
|
|
|
if ((s[0] == "DEL") || (s[0] == "DELETE")) {
|
2016-03-22 23:42:31 +01:00
|
|
|
//console.log("DELETE")
|
|
|
|
node.items = {};
|
|
|
|
}
|
2016-01-11 22:21:54 +01:00
|
|
|
}
|
2016-03-22 23:42:31 +01:00
|
|
|
else if (s.length === 2) {
|
|
|
|
if (s[0] === "BRIGHTNESS") {
|
|
|
|
//console.log("BRIGHTNESS",s[1])
|
|
|
|
node.child.stdin.write("B"+s[1]+"\n");
|
|
|
|
}
|
|
|
|
if (s[0] === "ROTATE") {
|
|
|
|
//console.log("ROTATE",s[1])
|
|
|
|
node.child.stdin.write("R"+s[1]+"\n");
|
|
|
|
}
|
2016-01-11 22:21:54 +01:00
|
|
|
}
|
2016-03-22 23:42:31 +01:00
|
|
|
else if (s.length === 3) {
|
|
|
|
//console.log("BACKGROUND",s)
|
2016-03-26 19:15:19 +01:00
|
|
|
for (i=0; i<192; i++) {
|
2016-03-22 23:42:31 +01:00
|
|
|
pic[i] = s[0];
|
|
|
|
pic[i+1] = s[1];
|
|
|
|
pic[i+2] = s[2];
|
|
|
|
i += 2;
|
|
|
|
}
|
2016-01-11 22:21:54 +01:00
|
|
|
}
|
2016-03-22 23:42:31 +01:00
|
|
|
else if (s.length % 5 === 0) { // handles pixel updates
|
|
|
|
if (msg.topic) {
|
|
|
|
node.items[msg.topic] = msg.payload;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.child.stdin.write('P'+msg.payload+'\n');
|
|
|
|
doDraw = false;
|
2016-03-26 19:15:19 +01:00
|
|
|
x = [];
|
|
|
|
y = [];
|
2016-03-26 18:34:07 +01:00
|
|
|
for (a = 0; a < s.length; a++) {
|
2016-03-22 23:42:31 +01:00
|
|
|
//console.log("PIXELS",a);
|
2016-03-26 18:34:07 +01:00
|
|
|
if (s[a] === "*") {
|
|
|
|
x[0] = 0;
|
|
|
|
x[1] = 7;
|
2016-02-26 23:01:00 +01:00
|
|
|
}
|
2016-03-26 18:34:07 +01:00
|
|
|
else if (s[a].indexOf("-") !== -1) {
|
|
|
|
x = s[a].split("-").sort();
|
2016-02-26 23:01:00 +01:00
|
|
|
}
|
2016-03-26 18:34:07 +01:00
|
|
|
else { x[0] = x[1] = s[a]; }
|
|
|
|
if (s[a+1] === "*") {
|
|
|
|
y[0] = 0;
|
|
|
|
y[1] = 7;
|
2016-02-26 23:01:00 +01:00
|
|
|
}
|
2016-03-26 18:34:07 +01:00
|
|
|
else if (s[a+1].indexOf("-") !== -1) {
|
|
|
|
y = s[a+1].split("-").sort();
|
|
|
|
}
|
|
|
|
else { y[0] = y[1] = s[a+1]; }
|
|
|
|
|
2016-03-26 19:15:19 +01:00
|
|
|
for (j = y[0]; j <= y[1]; j++) {
|
|
|
|
for (i = x[0]; i <= x[1]; i++) {
|
2016-03-26 18:34:07 +01:00
|
|
|
pic[i*3+j*24] = s[a+2];
|
|
|
|
pic[i*3+j*24+1] = s[a+3];
|
|
|
|
pic[i*3+j*24+2] = s[a+4];
|
|
|
|
}
|
2016-03-22 23:42:31 +01:00
|
|
|
}
|
2016-03-26 18:34:07 +01:00
|
|
|
|
2016-03-22 23:42:31 +01:00
|
|
|
a += 4;
|
2016-01-11 22:21:54 +01:00
|
|
|
}
|
|
|
|
}
|
2016-02-26 23:01:00 +01:00
|
|
|
}
|
2016-03-22 23:42:31 +01:00
|
|
|
else if (s.length === 192) { // handle complete buffer refresh.
|
|
|
|
for (var h=0; h<192; h++) {
|
|
|
|
pic[h] = s[h];
|
|
|
|
pic[h+1] = s[h+1];
|
|
|
|
pic[h+2] = s[h+2];
|
|
|
|
h += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (node.items.hasOwnProperty(msg.topic)) { delete node.items[msg.topic]; }
|
2016-01-11 22:21:54 +01:00
|
|
|
}
|
|
|
|
|
2016-03-22 23:42:31 +01:00
|
|
|
if (doDraw) {
|
|
|
|
var pixels = new Buffer(192);
|
|
|
|
pic.copy(pixels);
|
|
|
|
for (var p in node.items) {
|
|
|
|
if (node.items.hasOwnProperty(p)) {
|
|
|
|
b = node.items[p].split(",");
|
2016-03-26 19:15:19 +01:00
|
|
|
x = [];
|
|
|
|
y = [];
|
2016-03-26 18:34:07 +01:00
|
|
|
for (a = 0; a < b.length; a++) {
|
2016-03-22 23:42:31 +01:00
|
|
|
if (b[a] === "*") {
|
2016-03-26 18:34:07 +01:00
|
|
|
x[0] = 0;
|
|
|
|
x[1] = 7;
|
2016-03-01 22:10:58 +01:00
|
|
|
}
|
2016-03-26 18:34:07 +01:00
|
|
|
else if (b[a].indexOf("-") !== -1) {
|
|
|
|
x = b[a].split("-").sort();
|
2016-03-01 22:10:58 +01:00
|
|
|
}
|
2016-03-26 18:34:07 +01:00
|
|
|
else { x[0] = x[1] = b[a]; }
|
|
|
|
if (b[a+1] === "*") {
|
|
|
|
y[0] = 0;
|
|
|
|
y[1] = 7;
|
|
|
|
}
|
|
|
|
else if (b[a+1].indexOf("-") !== -1) {
|
|
|
|
y = b[a+1].split("-").sort();
|
|
|
|
}
|
|
|
|
else { y[0] = y[1] = b[a+1]; }
|
2016-03-26 19:15:19 +01:00
|
|
|
for (j = y[0]; j <= y[1]; j++) {
|
|
|
|
for (i = x[0]; i <= x[1]; i++) {
|
|
|
|
pixels[i*3+j*24] = b[a+2];
|
2016-03-26 18:34:07 +01:00
|
|
|
pixels[i*3+j*24+1] = b[a+3];
|
|
|
|
pixels[i*3+j*24+2] = b[a+4];
|
|
|
|
}
|
2016-03-22 23:42:31 +01:00
|
|
|
}
|
|
|
|
a += 4;
|
2016-02-26 23:01:00 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-11 22:21:54 +01:00
|
|
|
}
|
2016-03-22 23:42:31 +01:00
|
|
|
node.child.stdin.write(pixels);
|
|
|
|
node.child.stdin.write("\n");
|
2016-01-11 22:21:54 +01:00
|
|
|
}
|
|
|
|
}
|
2016-03-22 23:42:31 +01:00
|
|
|
else { node.warn("Input not a string"); }
|
2016-01-11 22:21:54 +01:00
|
|
|
}
|
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
if (allOK === true) {
|
|
|
|
node.child = spawn(hatCommand, [node.bright]);
|
|
|
|
node.status({fill:"green",shape:"dot",text:"ok"});
|
2016-01-11 22:21:54 +01:00
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
node.on("input", inputlistener);
|
2016-01-11 22:21:54 +01:00
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
node.child.stdout.on('data', function (data) {
|
|
|
|
if (RED.settings.verbose) { node.log("out: "+data+" :"); }
|
|
|
|
});
|
2016-01-11 22:21:54 +01:00
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
node.child.stderr.on('data', function (data) {
|
|
|
|
if (RED.settings.verbose) { node.log("err: "+data+" :"); }
|
|
|
|
});
|
2016-01-11 22:21:54 +01:00
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
node.child.on('close', function () {
|
|
|
|
node.child = null;
|
|
|
|
if (RED.settings.verbose) { node.log(RED._("rpi-gpio.status.closed")); }
|
2019-06-27 23:26:50 +02:00
|
|
|
if (node.finished) {
|
2018-04-15 12:24:56 +02:00
|
|
|
node.status({fill:"grey",shape:"ring",text:"closed"});
|
2019-06-27 23:26:50 +02:00
|
|
|
node.finished();
|
2018-04-15 12:24:56 +02:00
|
|
|
}
|
|
|
|
else { node.status({fill:"red",shape:"ring",text:"stopped"}); }
|
|
|
|
});
|
2016-01-11 22:21:54 +01:00
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
node.child.on('error', function (err) {
|
|
|
|
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); }
|
|
|
|
});
|
2016-01-11 22:21:54 +01:00
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
node.on("close", function(done) {
|
|
|
|
node.status({fill:"grey",shape:"ring",text:"closed"});
|
|
|
|
if (node.tout) { clearTimeout(node.tout); }
|
|
|
|
if (node.child != null) {
|
2019-06-27 23:26:50 +02:00
|
|
|
node.finished = done;
|
2018-04-15 12:24:56 +02:00
|
|
|
node.child.kill('SIGKILL');
|
|
|
|
}
|
|
|
|
else { done(); }
|
|
|
|
});
|
2016-01-11 22:21:54 +01:00
|
|
|
|
2018-04-15 12:24:56 +02:00
|
|
|
var nowready = function() {
|
|
|
|
node.tout = setTimeout( function() {
|
|
|
|
if (ready) { inputlistener({payload:"0"}); }
|
|
|
|
else { nowready(); }
|
|
|
|
}, 100);
|
|
|
|
}
|
|
|
|
nowready();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.status({fill:"grey",shape:"dot",text:"node-red:rpi-gpio.status.not-available"});
|
2016-01-11 22:21:54 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("rpi-unicorn",UnicornHatNode);
|
|
|
|
}
|