From e38b321c336ea0ecb4b6f7030bbf3435dca310c6 Mon Sep 17 00:00:00 2001 From: Dave C-J Date: Tue, 8 Apr 2014 16:04:13 +0100 Subject: [PATCH] Tweak to Pi GPIO node to throw exceptions more in line with latest thinking --- nodes/core/hardware/36-rpi-gpio.js | 39 ++++++++++++++---------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/nodes/core/hardware/36-rpi-gpio.js b/nodes/core/hardware/36-rpi-gpio.js index 4c6bae7aa..05195fc02 100644 --- a/nodes/core/hardware/36-rpi-gpio.js +++ b/nodes/core/hardware/36-rpi-gpio.js @@ -19,14 +19,12 @@ var util = require("util"); var exec = require('child_process').exec; var fs = require('fs'); -if (!fs.existsSync("/usr/local/bin/gpio")) { - exec("cat /proc/cpuinfo | grep BCM27",function(err,stdout,stderr) { - if (stdout.indexOf('BCM27') > -1) { - util.log('[36-rpi-gpio.js] Error: Cannot find Wiring-Pi "gpio" command. http://wiringpi.com/download-and-install/'); - } - // else not on a Pi so don't worry anyone with needless messages. - }); - return; +if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi + 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."; } // Map physical P1 pins to Gordon's Wiring-Pi Pins (as they should be V1/V2 tolerant) @@ -103,6 +101,10 @@ function GPIOInNode(n) { else { this.error("Invalid GPIO pin: "+this.pin); } + + this.on("close", function() { + clearInterval(this._interval); + }); } function GPIOOutNode(n) { @@ -133,6 +135,10 @@ function GPIOOutNode(n) { else { this.error("Invalid GPIO pin: "+this.pin); } + + this.on("close", function() { + exec("gpio mode "+this.pin+" in"); + }); } exec("gpio mode 0 in",function(err,stdout,stderr) { @@ -145,17 +151,8 @@ exec("gpio mode 0 in",function(err,stdout,stderr) { exec("gpio mode 4 in"); exec("gpio mode 5 in"); exec("gpio mode 6 in"); - exec("gpio mode 7 in",function(err,stdout,stderr) { - RED.nodes.registerType("rpi-gpio in",GPIOInNode); - RED.nodes.registerType("rpi-gpio out",GPIOOutNode); - - GPIOInNode.prototype.close = function() { - clearInterval(this._interval); - } - - GPIOOutNode.prototype.close = function() { - exec("gpio mode "+this.pin+" in"); - } - - }); + exec("gpio mode 7 in"); }); + +RED.nodes.registerType("rpi-gpio in",GPIOInNode); +RED.nodes.registerType("rpi-gpio out",GPIOOutNode);