1
0
mirror of https://github.com/node-red/node-red-nodes.git synced 2023-10-10 13:36:58 +02:00

Tidied up Pi Peripheral nodes - should now work alongside base GPIO node

This commit is contained in:
Dave C-J 2014-05-06 21:00:09 +01:00
parent 3a4f870257
commit 2b89241331
3 changed files with 40 additions and 50 deletions

View File

@ -19,14 +19,12 @@ var util = require("util");
var exec = require('child_process').exec; var exec = require('child_process').exec;
var fs = require('fs'); var fs = require('fs');
if (!fs.existsSync("/usr/local/bin/gpio")) { if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi
exec("cat /proc/cpuinfo | grep BCM27",function(err,stdout,stderr) { throw "Info : Ignoring Raspberry Pi specific node.";
if (stdout.indexOf('BCM27') > -1) { }
util.log('[37-rpi-piface.js] Error: Cannot find Wiring-Pi "gpio" command');
} if (!fs.existsSync("/usr/local/bin/gpio")) { // gpio command not installed
// else not on a Pi so don't worry anyone with needless messages. throw "Info : Can't find Raspberry Pi wiringPi gpio command.";
});
return;
} }
// Map names of pins to Gordon's gpio PiFace pin numbers // Map names of pins to Gordon's gpio PiFace pin numbers
@ -109,13 +107,16 @@ function PiFACEInNode(n) {
} }
} }
}); });
}, 250); }, 200);
} }
}); });
} }
else { else {
node.error("Invalid PiFACE pin: "+node.pin); node.error("Invalid PiFACE pin: "+node.pin);
} }
node.on("close", function() {
clearInterval(node._interval);
});
} }
function PiFACEOutNode(n) { function PiFACEOutNode(n) {
@ -141,21 +142,10 @@ function PiFACEOutNode(n) {
} }
exec("gpio load spi",function(err,stdout,stderr) { exec("gpio load spi",function(err,stdout,stderr) {
if (err) { if (err) {
util.log('[37-rpi-piface.js] Error: "gpio load spi" command failed for some reason.'); util.log('[37-rpi-piface.js] Error: "gpio load spi" command failed for some reason.');
} }
exec("gpio -p reset",function(err,stdout,stderr) { RED.nodes.registerType("rpi-piface in",PiFACEInNode);
if (err) { RED.nodes.registerType("rpi-piface out",PiFACEOutNode);
util.log('[37-rpi-piface.js] Error: "gpio -p reset" command failed for some reason.');
}
RED.nodes.registerType("rpi-piface in",PiFACEInNode);
RED.nodes.registerType("rpi-piface out",PiFACEOutNode);
PiFACEInNode.prototype.close = function() {
clearInterval(this._interval);
}
PiFACEOutNode.prototype.close = function() {
}
});
}); });

View File

@ -18,7 +18,7 @@
<div class="form-row"> <div class="form-row">
<label for="node-input-pin"><i class="icon-asterisk"></i> Input</label> <label for="node-input-pin"><i class="icon-asterisk"></i> Input</label>
<select type="text" id="node-input-pin" style="width: 150px;"> <select type="text" id="node-input-pin" style="width: 150px;">
<option value="-" disabled>select input</option> <option value="-">select input</option>
<option value="Red Button">Red Button</option> <option value="Red Button">Red Button</option>
<option value="In A">In A</option> <option value="In A">In A</option>
<option value="In B">In B</option> <option value="In B">In B</option>
@ -64,7 +64,7 @@
<div class="form-row"> <div class="form-row">
<label for="node-input-pin"><i class="icon-asterisk"></i> Output</label> <label for="node-input-pin"><i class="icon-asterisk"></i> Output</label>
<select type="text" id="node-input-pin" style="width: 150px;"> <select type="text" id="node-input-pin" style="width: 150px;">
<option value="-" disabled>select output</option> <option value="-">select output</option>
<option value="Red LED">Red LED</option> <option value="Red LED">Red LED</option>
<option value="Amber LED">Amber LED</option> <option value="Amber LED">Amber LED</option>
<option value="Green LED">Green LED</option> <option value="Green LED">Green LED</option>

View File

@ -67,7 +67,7 @@ function PibrellaIn(n) {
this.pin = pintable[n.pin]; this.pin = pintable[n.pin];
var node = this; var node = this;
if (this.pin) { if (node.pin) {
exec("gpio mode "+node.pin+" in", function(err,stdout,stderr) { exec("gpio mode "+node.pin+" in", function(err,stdout,stderr) {
if (err) node.error(err); if (err) node.error(err);
else { else {
@ -90,11 +90,11 @@ function PibrellaIn(n) {
}); });
} }
else { else {
this.error("Invalid GPIO pin: "+this.pin); node.error("Invalid GPIO pin: "+node.pin);
} }
this.on("close", function() { node.on("close", function() {
clearInterval(this._interval); clearInterval(node._interval);
}); });
} }
@ -103,7 +103,7 @@ function PibrellaOut(n) {
this.pin = pintable[n.pin]; this.pin = pintable[n.pin];
var node = this; var node = this;
if (this.pin == "1") { if (node.pin == "1") {
exec("gpio mode 1 pwm"); exec("gpio mode 1 pwm");
process.nextTick(function() { process.nextTick(function() {
exec("gpio pwm-ms"); exec("gpio pwm-ms");
@ -121,7 +121,7 @@ function PibrellaOut(n) {
}); });
}); });
} }
else if (this.pin) { else if (node.pin) {
process.nextTick(function() { process.nextTick(function() {
exec("gpio mode "+node.pin+" out", function(err,stdout,stderr) { exec("gpio mode "+node.pin+" out", function(err,stdout,stderr) {
if (err) node.error(err); if (err) node.error(err);
@ -142,31 +142,31 @@ function PibrellaOut(n) {
}); });
} }
else { else {
this.error("Invalid GPIO pin: "+this.pin); node.error("Invalid GPIO pin: "+node.pin);
} }
this.on("close", function() { node.on("close", function() {
exec("gpio mode "+this.pin+" in"); exec("gpio mode "+node.pin+" in");
}); });
} }
exec("gpio mode 0 out",function(err,stdout,stderr) { //exec("gpio mode 0 out",function(err,stdout,stderr) {
if (err) { //if (err) {
util.log('[36-rpi-gpio.js] Error: "gpio" command failed for some reason.'); //util.log('[36-rpi-gpio.js] Error: "gpio" command failed for some reason.');
} //}
exec("gpio mode 1 out"); //exec("gpio mode 1 out");
exec("gpio mode 2 out"); //exec("gpio mode 2 out");
exec("gpio mode 3 out"); //exec("gpio mode 3 out");
exec("gpio mode 4 out"); //exec("gpio mode 4 out");
exec("gpio mode 5 out"); //exec("gpio mode 5 out");
exec("gpio mode 6 out"); //exec("gpio mode 6 out");
exec("gpio mode 7 out"); //exec("gpio mode 7 out");
exec("gpio mode 10 in"); //exec("gpio mode 10 in");
exec("gpio mode 11 in"); //exec("gpio mode 11 in");
exec("gpio mode 12 in"); //exec("gpio mode 12 in");
exec("gpio mode 13 in"); //exec("gpio mode 13 in");
exec("gpio mode 14 in"); //exec("gpio mode 14 in");
}); //});
RED.nodes.registerType("rpi-pibrella in",PibrellaIn); RED.nodes.registerType("rpi-pibrella in",PibrellaIn);
RED.nodes.registerType("rpi-pibrella out",PibrellaOut); RED.nodes.registerType("rpi-pibrella out",PibrellaOut);