From 472fdc65a94f922b267d50a377dec130df2b9a1f Mon Sep 17 00:00:00 2001 From: Dave C-J Date: Sun, 19 Oct 2014 13:54:21 +0100 Subject: [PATCH] Allow Raspberry Pi node to set initial output level. Fix for #443 Also allow (optional) initial read of input pins on deploy. Moved to Category Raspberry Pi (other Pi related nodes will be updated to match soon). --- nodes/core/hardware/36-rpi-gpio.html | 40 +++++++++++++++++++++++----- nodes/core/hardware/36-rpi-gpio.js | 39 ++++++++++++++++----------- 2 files changed, 57 insertions(+), 22 deletions(-) diff --git a/nodes/core/hardware/36-rpi-gpio.html b/nodes/core/hardware/36-rpi-gpio.html index 9e705c2c9..9d9faf279 100644 --- a/nodes/core/hardware/36-rpi-gpio.html +++ b/nodes/core/hardware/36-rpi-gpio.html @@ -40,7 +40,7 @@  
- +
+
+ + + +
@@ -60,18 +65,18 @@

You may also enable the input pullup resitor or the pulldown resistor.

The msg.topic is set to pi/{the pin number}

Note: we are using the actual physical pin numbers on connector P1 as they are easier to locate.

-

Note: This node currently polls the pin every 250mS. This is not ideal as it loads the cpu, and will be rewritten shortly to try to use interrupts.

- +

Note: This node currently polls the pin every 250mS. This is not ideal as it loads the cpu, and should be rewritten to try to use interrupts.

diff --git a/nodes/core/hardware/36-rpi-gpio.js b/nodes/core/hardware/36-rpi-gpio.js index 93cbc4e00..b59cbca78 100644 --- a/nodes/core/hardware/36-rpi-gpio.js +++ b/nodes/core/hardware/36-rpi-gpio.js @@ -1,5 +1,5 @@ /** - * Copyright 2013 IBM Corp. + * Copyright 2013,2014 IBM Corp. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -99,6 +99,8 @@ module.exports = function(RED) { this.buttonState = -1; this.pin = pintable[n.pin]; this.intype = n.intype; + this.read = n.read || false; + if (this.read) { this.buttonState = -2; } var node = this; if (node.pin !== undefined) { @@ -135,26 +137,31 @@ module.exports = function(RED) { function GPIOOutNode(n) { RED.nodes.createNode(this,n); this.pin = pintable[n.pin]; + this.set = n.set || false; + this.level = n.level || 0; var node = this; if (node.pin !== undefined) { - process.nextTick(function() { - exec(gpioCommand+" mode "+node.pin+" out", function(err,stdout,stderr) { - if (err) { node.error(err); } - else { - node.on("input", function(msg) { - if (msg.payload === "true") { msg.payload = true; } - if (msg.payload === "false") { msg.payload = false; } - var out = Number(msg.payload); - if ((out === 0)|(out === 1)) { - exec(gpioCommand+" write "+node.pin+" "+out, function(err,stdout,stderr) { - if (err) { node.error(err); } - }); - } - else { node.warn("Invalid input - not 0 or 1"); } + exec(gpioCommand+" mode "+node.pin+" out", function(err,stdout,stderr) { + if (err) { node.error(err); } + else { + if (node.set) { + exec(gpioCommand+" write "+node.pin+" "+node.level, function(err,stdout,stderr) { + if (err) { node.error(err); } }); } - }); + node.on("input", function(msg) { + if (msg.payload === "true") { msg.payload = true; } + if (msg.payload === "false") { msg.payload = false; } + var out = Number(msg.payload); + if ((out === 0)|(out === 1)) { + exec(gpioCommand+" write "+node.pin+" "+out, function(err,stdout,stderr) { + if (err) { node.error(err); } + }); + } + else { node.warn("Invalid input - not 0 or 1"); } + }); + } }); } else {