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

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).
This commit is contained in:
Dave C-J 2014-10-19 13:54:21 +01:00
parent 28a4ba1aad
commit 472fdc65a9
2 changed files with 57 additions and 22 deletions

View File

@ -40,7 +40,7 @@
&nbsp;<span id="pitype"></span> &nbsp;<span id="pitype"></span>
</div> </div>
<div class="form-row"> <div class="form-row">
<label for="node-input-intype"><i class="fa fa-long-arrow-up"></i> Resistor?</label> <label for="node-input-intype"><i class="fa fa-long-arrow-up"></i> Resistor ?</label>
<select type="text" id="node-input-intype" style="width: 150px;"> <select type="text" id="node-input-intype" style="width: 150px;">
<option value="tri">none</option> <option value="tri">none</option>
<option value="up">pullup</option> <option value="up">pullup</option>
@ -48,6 +48,11 @@
<!--<option value="tri">tristate</option>--> <!--<option value="tri">tristate</option>-->
</select> </select>
</div> </div>
<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-read" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-read" style="width: 70%;">Read initial state of pin on deploy ?</label>
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
@ -60,18 +65,18 @@
<p>You may also enable the input pullup resitor or the pulldown resistor.</p> <p>You may also enable the input pullup resitor or the pulldown resistor.</p>
<p>The <b>msg.topic</b> is set to <i>pi/{the pin number}</i></p> <p>The <b>msg.topic</b> is set to <i>pi/{the pin number}</i></p>
<p><b>Note:</b> we are using the actual physical pin numbers on connector P1 as they are easier to locate.</p> <p><b>Note:</b> we are using the actual physical pin numbers on connector P1 as they are easier to locate.</p>
<p><b>Note:</b> 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.</p> <p><b>Note:</b> 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.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
RED.nodes.registerType('rpi-gpio in',{ RED.nodes.registerType('rpi-gpio in',{
category: 'advanced-input', category: 'Raspberry Pi',
color:"#c6dbef", color:"#c6dbef",
defaults: { defaults: {
name: { value:"" }, name: { value:"" },
intype: { value: "in" },
pin: { value:"",required:true,validate:RED.validators.number() }, pin: { value:"",required:true,validate:RED.validators.number() },
intype: { value: "in" },
read: { value:false }
}, },
inputs:0, inputs:0,
outputs:1, outputs:1,
@ -129,6 +134,18 @@
</select> </select>
&nbsp;<span id="pitype"></span> &nbsp;<span id="pitype"></span>
</div> </div>
<div class="form-row">
<label>&nbsp;</label>
<input type="checkbox" id="node-input-set" style="display: inline-block; width: auto; vertical-align: top;">
<label for="node-input-set" style="width: 70%;">Initialise pin state ?</label>
</div>
<div class="form-row" id="node-set-state">
<label for="node-input-level">&nbsp;</label>
<select id="node-input-level" style="width: 300px;">
<option value="0">initial level of pin - low (0)</option>
<option value="1">initial level of pin - high (1)</option>
</select>
</div>
<div class="form-row"> <div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label> <label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name"> <input type="text" id="node-input-name" placeholder="Name">
@ -139,16 +156,19 @@
<script type="text/x-red" data-help-name="rpi-gpio out"> <script type="text/x-red" data-help-name="rpi-gpio out">
<p>Raspberry Pi output node. Expects a <b>msg.payload</b> with either a 0 or 1 (or true or false). Requires the gpio command to work.</p> <p>Raspberry Pi output node. Expects a <b>msg.payload</b> with either a 0 or 1 (or true or false). Requires the gpio command to work.</p>
<p>Will set the selected physical pin high or low depending on the value passed in.</p> <p>Will set the selected physical pin high or low depending on the value passed in.</p>
<p>The initial value of the pin at deploy time can also be set to 0 or 1.</p>
<p><b>Note:</b> we are using the actual physical pin numbers on connector P1 as they are easier to locate.</p> <p><b>Note:</b> we are using the actual physical pin numbers on connector P1 as they are easier to locate.</p>
</script> </script>
<script type="text/javascript"> <script type="text/javascript">
RED.nodes.registerType('rpi-gpio out',{ RED.nodes.registerType('rpi-gpio out',{
category: 'advanced-output', category: 'Raspberry Pi',
color:"#c6dbef", color:"#c6dbef",
defaults: { defaults: {
name: { value:"" }, name: { value:"" },
pin: { value:"",required:true,validate:RED.validators.number() }, pin: { value:"",required:true,validate:RED.validators.number() },
set: { value:false },
level: { value:"0" }
}, },
inputs:1, inputs:1,
outputs:0, outputs:0,
@ -177,6 +197,14 @@
$('#node-input-pin').append($("<option></option>").attr("value",40).text("40 - GPIO29")); $('#node-input-pin').append($("<option></option>").attr("value",40).text("40 - GPIO29"));
} }
}); });
$("#node-input-set").change(function() {
if ($('#node-input-set').is(":checked")) {
$("#node-set-state").show();
} else {
$("#node-set-state").hide();
}
});
} }
}); });
</script> </script>

View File

@ -1,5 +1,5 @@
/** /**
* Copyright 2013 IBM Corp. * Copyright 2013,2014 IBM Corp.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.buttonState = -1;
this.pin = pintable[n.pin]; this.pin = pintable[n.pin];
this.intype = n.intype; this.intype = n.intype;
this.read = n.read || false;
if (this.read) { this.buttonState = -2; }
var node = this; var node = this;
if (node.pin !== undefined) { if (node.pin !== undefined) {
@ -135,13 +137,19 @@ module.exports = function(RED) {
function GPIOOutNode(n) { function GPIOOutNode(n) {
RED.nodes.createNode(this,n); RED.nodes.createNode(this,n);
this.pin = pintable[n.pin]; this.pin = pintable[n.pin];
this.set = n.set || false;
this.level = n.level || 0;
var node = this; var node = this;
if (node.pin !== undefined) { if (node.pin !== undefined) {
process.nextTick(function() {
exec(gpioCommand+" mode "+node.pin+" out", function(err,stdout,stderr) { exec(gpioCommand+" mode "+node.pin+" out", function(err,stdout,stderr) {
if (err) { node.error(err); } if (err) { node.error(err); }
else { 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) { node.on("input", function(msg) {
if (msg.payload === "true") { msg.payload = true; } if (msg.payload === "true") { msg.payload = true; }
if (msg.payload === "false") { msg.payload = false; } if (msg.payload === "false") { msg.payload = false; }
@ -155,7 +163,6 @@ module.exports = function(RED) {
}); });
} }
}); });
});
} }
else { else {
node.error("Invalid GPIO pin: "+node.pin); node.error("Invalid GPIO pin: "+node.pin);