Add Hummingboard GPIO node

This commit is contained in:
Dave C-J 2014-12-10 09:29:18 +00:00
parent 139dcd67bc
commit dbff1dc28c
7 changed files with 688 additions and 0 deletions

View File

@ -0,0 +1,177 @@
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.
"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.
2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.
3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.
4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:
(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and
(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and
(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.
You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.
5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.
6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.
8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS

View File

@ -0,0 +1,43 @@
node-red-node-hbgpio
====================
A <a href="http://nodered.org" target="_new">Node-RED</a> node to read and write the digital IO of a SolidRun HummingBoard.
Install
-------
Run the following command in the root directory of your Node-RED install:
npm install node-red-node-hpgpio
**Note :** This **should** be run as root in order to move and set SUID permissions on a script to talk to the gpio pins. Alternatively you can run as a normal user and then move the file and change permissions manually.
sudo cp node_modules/node-red-node-hpgpio/gpiohb /usr/local/bin/
sudo chmod 4755 /usr/lcoal/bin/gpiohb
Usage
-----
Hummingboard GPIO input and output nodes.
This requires a small script (gpiohb) to run as root in order to work. It should be placed in /usr/local/bin/ and have SUID permissions 4755.
**Note:** We are using the actual physical pin numbers as they are easier to locate.
###Output node
Expects a <b>msg.payload</b> with either a 0 or 1 (or true or false).
Will set the selected physical pin high or low depending on the value passed in.
The initial value of the pin at deploy time can also be set to 0 or 1.
###Input node
Generates a **msg.payload** with either a 0 or 1 depending on the state of the input pin.
The **msg.topic** is set to **pin/{the pin number}**
**Note:** The input node waits for a change on the level of the pin before reading the value - and then resets to wait for the next interrupt - so it is possible to miss very fast changes as we may still be reading the value when the second edge occurs.

View File

@ -0,0 +1,18 @@
#!/bin/bash
echo "*********************************************"
echo "Moving gpiohb to /usr/local/bin/"
if [[ $EUID -ne 0 ]]; then
echo "Not running as user root" 2>&1
echo "please run the following commands manually"
echo " sudo cp $PWD/gpiohb /usr/local/bin/gpiohb"
echo " sudo chmod 4755 /usr/local/bin/gpiohb"
echo "or re-run npm install as root / sudo"
echo "*********************************************"
exit 1
else
cp gpiohb /usr/local/bin/
chmod 4755 /usr/local/bin/gpiohb
echo "OK - gpiohb moved to /usr/local/bin"
echo "*********************************************"
fi

View File

@ -0,0 +1,74 @@
#!/bin/bash
# There is NO error checking in this program - you can mess up pins big time if you want.
# gpio mode pin inputtype
# gpio read pin
# gpio write pin level
# gpio pwm pin level
# gpio close pin
declare -A pins
# Map the GPIO names to actual underlying IO numbers
pins=(["0"]="73" ["1"]="72" ["2"]="71" ["3"]="70" ["4"]="194" ["5"]="195" ["6"]="67" ["7"]="1")
usage() {
echo ""
echo " gpiohb mode {pin} {in|out}"
echo " gpiohb read {pin}"
echo " gpiohb write {pin} {0|1}"
echo " gpiohb wfi {pin} {rising|falling|both|none}"
#echo " gpiohb pwm {pin} {0-1023}"
echo " gpiohb close {pin}"
echo ""
}
while [ "$1" != "" ]; do
case $1 in
-h | --help | -?)
usage
exit
;;
mode)
echo $1 $2 $3
if [ "$3" != "out" ] ; then
$3="in"
fi
pin=${pins[$2]}
echo $pin > /sys/class/gpio/export
echo $3 > /sys/class/gpio/gpio$pin/direction
exit
;;
read)
pin=${pins[$2]}
cat /sys/class/gpio/gpio$pin/value
exit
;;
write)
pin=${pins[$2]}
echo $3 > /sys/class/gpio/gpio$pin/value
exit
;;
wfi)
pin=${pins[$2]}
echo $3 > /sys/class/gpio/gpio$pin/edge
exit
;;
pwm)
# Not implemented yet....
pin=${pins[$2]}
exit
;;
close)
pin=${pins[$2]}
echo $pin > /sys/class/gpio/unexport
exit
;;
*)
echo "ERROR: unknown parameter \"$PARAM\""
usage
exit 1
;;
esac
shift
done

View File

@ -0,0 +1,162 @@
<!--
Copyright 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.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<script type="text/x-red" data-template-name="hb-gpio in">
<div class="form-row">
<label for="node-input-pin"><i class="fa fa-circle"></i> GPIO Pin</label>
<select type="text" id="node-input-pin" style="width: 250px;">
<option value='' disabled selected style='display:none;'>select pin</option>
<option value="7">7 - GPIO 1</option>
<option value="11">11 - GPIO 73</option>
<option value="12">12 - GPIO 72</option>
<option value="13">13 - GPIO 71</option>
<option value="15">15 - GPIO 10</option>
<option value="16">16 - GPIO 194</option>
<option value="18">18 - GPIO 195</option>
<option value="22">22 - GPIO 87</option>
</select>
&nbsp;<span id="pitype"></span>
</div>
<!-- <div class="form-row">
<label for="node-input-intype"><i class="fa fa-level-up"></i> Resistor ?</label>
<select type="text" id="node-input-intype" style="width: 150px;">
<option value="tri">none</option>
<option value="up">pullup</option>
<option value="down">pulldown</option>
</select>
</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/restart ?</label>
</div>
<br/>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-tips">Tip: Only Digital Input is supported - input must be 0 or 1.</div>
</script>
<script type="text/x-red" data-help-name="hb-gpio in">
<p>Hummingboard GPIO input pin node. Generates a <b>msg.payload</b> with either a 0 or 1 depending on the state of the input pin.</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><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> requires the gpiohb command to be executeable as root in order to work.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('hb-gpio in',{
category: 'Hummingboard',
color:"#c6dbef",
defaults: {
name: { value:"" },
pin: { value:"",required:true,validate:RED.validators.number() },
intype: { value: "in" },
read: { value:false }
},
inputs:0,
outputs:1,
icon: "arrow.png",
label: function() {
return this.name||"Pin: "+this.pin ;
},
labelStyle: function() {
return this.name?"node_label_italic":"";
}
});
</script>
<script type="text/x-red" data-template-name="hb-gpio out">
<div class="form-row">
<label for="node-input-pin"><i class="fa fa-circle"></i> GPIO Pin</label>
<select type="text" id="node-input-pin" style="width: 250px;">
<option value='' disabled selected style='display:none;'>select pin</option>
<option value="7">7 - GPIO 1</option>
<option value="11">11 - GPIO 73</option>
<option value="12">12 - GPIO 72</option>
<option value="13">13 - GPIO 71</option>
<option value="15">15 - GPIO 10</option>
<option value="16">16 - GPIO 194</option>
<option value="18">18 - GPIO 195</option>
<option value="22">22 - GPIO 87</option>
</select>
&nbsp;<span id="pitype"></span>
</div>
<div class="form-row" id="node-set-tick">
<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: 250px;">
<option value="0">initial level of pin - low (0)</option>
<option value="1">initial level of pin - high (1)</option>
</select>
</div>
<br/>
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name">
</div>
<div class="form-tips" id="dig-tip"><b>Tip</b>: For digital output - input must be 0 or 1.</div>
<!-- <div class="form-tips" id="pwm-tip"><b>Tip</b>: For PWM output - input must be between 0 to 1023.</div> -->
</script>
<script type="text/x-red" data-help-name="hb-gpio out">
<p>Hummingboard GPIO output pin node. Expects a <b>msg.payload</b> with either a 0 or 1 (or true or false).</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 as they are easier to locate.</p>
<p><b>Note:</b> requires the gpiohb command to be executeable as root in order to work.</p>
</script>
<script type="text/javascript">
RED.nodes.registerType('hb-gpio out',{
category: 'Hummingboard',
color:"#c6dbef",
defaults: {
name: { value:"" },
pin: { value:"",required:true,validate:RED.validators.number() },
set: { value:"" },
level: { value:"0" }
},
inputs:1,
outputs:0,
icon: "arrow.png",
align: "right",
label: function() {
return this.name||"Pin: "+this.pin ;
},
labelStyle: function() {
return this.name?"node_label_italic":"";
},
oneditprepare: function() {
var setstate = function () {
if ($('#node-input-set').is(":checked")) {
$("#node-set-state").show();
} else {
$("#node-set-state").hide();
}
};
$("#node-input-set").change(function () { setstate(); });
setstate();
}
});
</script>

View File

@ -0,0 +1,188 @@
/**
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
module.exports = function(RED) {
"use strict";
var exec = require('child_process').exec;
var fs = require('fs');
var gpioCommand = '/usr/local/bin/gpiohb';
//if (!fs.existsSync("/dev/ttyAMA0")) { // unlikely if not on a Pi
//throw "Info : Ignoring Raspberry Pi specific node.";
//}
if (!fs.existsSync(gpioCommand)) { // gpio command not installed
throw "Info : Can't find "+gpioCommand+" command.";
}
// Map physical P1 pins to Gordon's Wiring-Pi Pins (as they should be V1/V2 tolerant)
var pintable = {
// Physical : WiringPi
"11":"0",
"12":"1",
"13":"2",
"15":"3",
"16":"4",
"18":"5",
"22":"6",
"7":"7",
"3":"8",
"5":"9",
"24":"10",
"26":"11",
"19":"12",
"21":"13",
"23":"14",
"8":"15",
"10":"16",
"27":"30",
"28":"31",
"29":"21",
"31":"22",
"32":"26",
"33":"23",
"35":"24",
"36":"27",
"37":"25",
"38":"28",
"40":"29"
}
var tablepin = {
// WiringPi : Physical
"0":"11",
"1":"12",
"2":"13",
"3":"15",
"4":"16",
"5":"18",
"6":"22",
"7":"7",
"8":"3",
"9":"5",
"10":"24",
"11":"26",
"12":"19",
"13":"21",
"14":"23",
"15":"8",
"16":"10",
"30":"27",
"31":"28",
"21":"29",
"22":"31",
"26":"32",
"23":"33",
"24":"35",
"27":"36",
"25":"37",
"28":"38",
"29":"40"
}
function GPIOHBInNode(n) {
RED.nodes.createNode(this,n);
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;
var readit = function() {
exec(gpioCommand+" read "+node.pin, function(err,stdout,stderr) {
if (err) { node.error(err); }
else {
exec(gpioCommand+" wfi "+node.pin + " both", function(err,stdo,stde) {
if (err) { node.error(err); }
else { readit(); }
});
if (node.buttonState !== Number(stdout)) {
var previousState = node.buttonState;
node.buttonState = Number(stdout);
if (previousState !== -1) {
var msg = {topic:"pi/"+tablepin[node.pin], payload:(node.buttonState === 0 ? 0 : 1)};
node.send(msg);
}
}
}
});
}
if (node.pin !== undefined) {
exec(gpioCommand+" mode "+node.pin+" "+node.intype, function(err,stdout,stderr) {
if (err) { node.error(err); }
else {
readit();
}
});
}
else {
node.error("Invalid GPIO pin: "+node.pin);
}
node.on("close", function() {
clearInterval(node._interval);
exec(gpioCommand+" close "+node.pin);
});
}
function GPIOHBOutNode(n) {
RED.nodes.createNode(this,n);
this.pin = pintable[n.pin];
this.set = n.set || false;
this.level = n.level || 0;
this.out = n.out || "out";
var node = this;
(node.out === "pwm") ? (node.op = "pwm") : (node.op = "write");
if (node.pin !== undefined) {
exec(gpioCommand+" mode "+node.pin+" "+node.out, function(err,stdout,stderr) {
if (err) { node.error(err); }
else {
if (node.set && (node.out === "out")) {
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);
var limit = 1;
if (node.out === "pwm") { limit = 1023; }
if ((out >= 0) && (out <= limit)) {
exec(gpioCommand+" "+node.op+" "+node.pin+" "+out, function(err,stdout,stderr) {
if (err) { node.error(err); }
});
}
else { node.warn("Invalid input: "+out); }
});
}
});
}
else {
node.error("Invalid GPIO pin: "+node.pin);
}
node.on("close", function() {
exec(gpioCommand+" close "+node.pin);
});
}
RED.nodes.registerType("hb-gpio in",GPIOHBInNode);
RED.nodes.registerType("hb-gpio out",GPIOHBOutNode);
}

View File

@ -0,0 +1,26 @@
{
"name" : "node-red-node-hbgpio",
"version" : "0.0.1",
"description" : "A Node-RED node for the GPIO of the Hummingboard",
"dependencies": {
},
"repository" : {
"type":"tgz",
"url":"http://dcj.dtdns.net/npms/hbgpio.tgz"
},
"license": "Apache-2.0",
"keywords": [ "node-red", "hummingboard", "gpio" ],
"node-red" : {
"nodes": {
"hb-gpio": "hb-gpio.js"
}
},
"scripts": {
"postinstall": "./fixup.sh"
},
"author": {
"name": "Dave Conway-Jones",
"email": "ceejay@vnet.ibm.com",
"url": "http://nodered.org"
}
}