mirror of
https://github.com/node-red/node-red-nodes.git
synced 2025-03-01 10:37:43 +00:00
Update Piliter node to use python library.
This commit is contained in:
@@ -16,104 +16,80 @@
|
||||
|
||||
module.exports = function(RED) {
|
||||
"use strict";
|
||||
var util = require("util");
|
||||
var exec = require('child_process').exec;
|
||||
var spawn = require('child_process').spawn;
|
||||
var fs = require('fs');
|
||||
|
||||
var gpioCommand = __dirname+'/nrgpio';
|
||||
|
||||
if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi
|
||||
//util.log("Info : Ignoring Raspberry Pi specific node.");
|
||||
throw "Info : Ignoring Raspberry Pi specific node.";
|
||||
}
|
||||
|
||||
if (!fs.existsSync("/usr/local/bin/gpio")) { // gpio command not installed
|
||||
throw "Info : Can't find Raspberry Pi wiringPi gpio command.";
|
||||
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.";
|
||||
}
|
||||
|
||||
// Map physical P1 pins to Gordon's Wiring-Pi Pins (as they should be V1/V2 tolerant)
|
||||
var pintable = {
|
||||
// Physical : WiringPi
|
||||
"LED1":"7",
|
||||
"LED2":"0",
|
||||
"LED3":"2",
|
||||
"LED4":"1",
|
||||
"LED5":"3",
|
||||
"LED6":"4",
|
||||
"LED7":"5",
|
||||
"LED8":"6"
|
||||
}
|
||||
|
||||
var tablepin = {
|
||||
// WiringPi : Physical
|
||||
"0":"LED2",
|
||||
"1":"LED4",
|
||||
"2":"LED3",
|
||||
"3":"LED5",
|
||||
"4":"LED6",
|
||||
"5":"LED7",
|
||||
"6":"LED8",
|
||||
"7":"LED1"
|
||||
}
|
||||
|
||||
var barpins = {
|
||||
"1":"128",
|
||||
"2":"129",
|
||||
"3":"133",
|
||||
"4":"135",
|
||||
"5":"143",
|
||||
"6":"159",
|
||||
"7":"191",
|
||||
"8":"255",
|
||||
}
|
||||
|
||||
var meterpins = {
|
||||
"1":"128",
|
||||
"2":"1",
|
||||
"3":"4",
|
||||
"4":"2",
|
||||
"5":"8",
|
||||
"6":"16",
|
||||
"7":"32",
|
||||
"8":"64",
|
||||
if ( !(1 & parseInt ((fs.statSync(gpioCommand).mode & parseInt ("777", 8)).toString (8)[0]) )) {
|
||||
util.log("[rpi-gpio] Error : "+gpioCommand+" needs to be executable.");
|
||||
throw "Error : nrgpio must to be executable.";
|
||||
}
|
||||
|
||||
function PiLiter(n) {
|
||||
RED.nodes.createNode(this,n);
|
||||
this.pin = pintable[n.pin];
|
||||
this.pinv = n.pin;
|
||||
this.dir = (n.dir ? 1 : 0) || 0;
|
||||
var node = this;
|
||||
|
||||
//Set all pins to outputs
|
||||
for (var p = 0; p < 8; p++) {
|
||||
exec("gpio mode "+p+" out");
|
||||
}
|
||||
if (this.pinv === "bar") {
|
||||
node.child = spawn(gpioCommand, ["byte",node.dir]);
|
||||
node.on("input", function(msg) {
|
||||
var out = Number(msg.payload);
|
||||
if ((out >= 1)&&(out <= 8)) { exec("gpio wb "+barpins[out]); }
|
||||
else { exec("gpio wb 0"); }
|
||||
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"); }
|
||||
});
|
||||
}
|
||||
else if (this.pinv === "meter") {
|
||||
node.child = spawn(gpioCommand, ["byte",node.dir]);
|
||||
node.on("input", function(msg) {
|
||||
var out = Number(msg.payload);
|
||||
if ((out >= 1)&&(out <= 8)) { exec("gpio wb "+meterpins[out]); }
|
||||
else { exec("gpio wb 0"); }
|
||||
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"); }
|
||||
});
|
||||
}
|
||||
else if (this.pinv === "all") {
|
||||
node.child = spawn(gpioCommand, ["byte",node.dir]);
|
||||
node.on("input", function(msg) {
|
||||
var out = msg.payload;
|
||||
if ((out === 1)|(out === true)|(out === "1")|(out === "on")) {
|
||||
exec("gpio wb 255");
|
||||
out = 255;
|
||||
}
|
||||
else { exec("gpio wb 0"); }
|
||||
else { out = 0; }
|
||||
if (node.child !== null) { node.child.stdin.write(out+"\n"); }
|
||||
else { node.warn("Command not running"); }
|
||||
});
|
||||
}
|
||||
else if (this.pinv === "pin") {
|
||||
node.child = spawn(gpioCommand, ["byte",node.dir]);
|
||||
var byte = 0;
|
||||
node.on("input", function(msg) {
|
||||
if (typeof msg.payload === "object") {
|
||||
var out = Number(msg.payload.led);
|
||||
var l = msg.payload.state;
|
||||
if ((out >= 1)&&(out <= 8)) {
|
||||
exec("gpio write "+pintable["LED"+out]+" "+l);
|
||||
var l = Number(msg.payload.state);
|
||||
if ((out >= 1) && (out <= 8)) {
|
||||
out = (Math.pow(2, (out-1)));
|
||||
if (l === 0) { out = ~ out; }
|
||||
byte = byte & out & 255;
|
||||
console.log("NOW",byte);
|
||||
if (node.child !== null) { node.child.stdin.write(byte+"\n"); }
|
||||
else { node.warn("Command not running"); }
|
||||
}
|
||||
else { node.warn("Not a valid object - see Info panel."); }
|
||||
}
|
||||
@@ -121,21 +97,22 @@ module.exports = function(RED) {
|
||||
});
|
||||
}
|
||||
else {
|
||||
node.child = spawn(gpioCommand, ["byte",node.dir]);
|
||||
node.on("input", function(msg) {
|
||||
var out = Number(msg.payload);
|
||||
if ((out >= 0)&&(out <= 255)) {
|
||||
var val = 0;
|
||||
for (var i = 0; i < 8; i ++) {
|
||||
val += ((out & 0x01) << pintable["LED"+(i+1)]);
|
||||
out = out >> 1;
|
||||
}
|
||||
exec("gpio wb "+val);
|
||||
if ((out >= 0) && (out <= 255)) {
|
||||
if (node.child !== null) { node.child.stdin.write(out+"\n"); }
|
||||
else { node.warn("Command not running"); }
|
||||
}
|
||||
else { node.warn("Invalid input - not between 0 and 255"); }
|
||||
});
|
||||
}
|
||||
|
||||
node.on("close", function() {
|
||||
exec("gpio wb 0");
|
||||
if (node.child != null) {
|
||||
node.child.kill('SIGKILL');
|
||||
}
|
||||
if (RED.settings.verbose) { node.log("end"); }
|
||||
});
|
||||
}
|
||||
RED.nodes.registerType("rpi-liter",PiLiter);
|
||||
|
Reference in New Issue
Block a user