From 681bdae5765b1c85ee2642753069ca8622a8071c Mon Sep 17 00:00:00 2001 From: Dave Conway-Jones Date: Fri, 1 Jan 2016 14:38:39 +0000 Subject: [PATCH] Update BBB nodes to support Jessie Fix to close #156 Now mandates octalbonescript so a minor version bump. --- hardware/BBB/145-BBB-hardware.js | 84 ++++++++++++++------------------ hardware/BBB/README.md | 27 +++++----- hardware/BBB/package.json | 5 +- 3 files changed, 55 insertions(+), 61 deletions(-) mode change 100755 => 100644 hardware/BBB/145-BBB-hardware.js diff --git a/hardware/BBB/145-BBB-hardware.js b/hardware/BBB/145-BBB-hardware.js old mode 100755 new mode 100644 index 8372245a..ef725e11 --- a/hardware/BBB/145-BBB-hardware.js +++ b/hardware/BBB/145-BBB-hardware.js @@ -25,34 +25,20 @@ module.exports = function (RED) { var usrLEDs = ["USR0", "USR1", "USR2", "USR3"]; // Load the hardware library and set up polymorphic functions to suit it. Prefer // octalbonescript (faster & less buggy) but drop back to bonescript if not available - try { - bonescript = require("octalbonescript"); - adjustName = function (pin) { - if (pin === "P8_7") { - pin = "P8_07"; - } else if (pin === "P8_8") { - pin = "P8_08"; - } else if (pin === "P8_9") { - pin = "P8_09"; - } - return pin; - }; - setPinMode = function (pin, direction, callback) { - bonescript.pinMode(pin, direction, callback); - } - } catch (e) { - try { - bonescript = require("bonescript"); - adjustName = function (pin) { - return pin; - }; - setPinMode = function (pin, direction, callback) { - bonescript.pinMode(pin, direction, undefined, undefined, undefined, callback); - } - } catch (er) { - throw "Info : Ignoring Beaglebone specific node."; - } - } + bonescript = require("octalbonescript"); + adjustName = function (pin) { + if (pin === "P8_7") { + pin = "P8_07"; + } else if (pin === "P8_8") { + pin = "P8_08"; + } else if (pin === "P8_9") { + pin = "P8_09"; + } + return pin; + }; + setPinMode = function (pin, direction, callback) { + bonescript.pinMode(pin, direction, callback); + } // Node constructor for bbb-analogue-in function AnalogueInputNode(n) { @@ -78,7 +64,7 @@ module.exports = function (RED) { // The callback function for analogRead. Accumulates the required number of // measurements, then divides the total number, applies output scaling and // sends the result - var analogReadCallback = function (x) { + var analogReadCallback = function (err, x) { sum = sum + x.value; count = count - 1; if (count > 0) { @@ -153,7 +139,7 @@ module.exports = function (RED) { // schedule a re-read of the input pin in 7ms time, and set the debouncing flag // Note: if x has an 'attached' field and no 'value' field, the callback is reporting // the success or failure of attaching the interrupt - we must handle this - var interruptCallback = function (x) { + var interruptCallback = function (err, x) { if (x.value === undefined) { if (x.attached === true) { node.interruptAttached = true; @@ -179,7 +165,7 @@ module.exports = function (RED) { // This function is called approx 7ms after a potential change-of-state which is // being debounced. Terminate the debounce, and send a message if the state has // actually changed - var debounceCallback = function (x) { + var debounceCallback = function (err, x) { node.debounceTimer = null; node.debouncing = false; if (x.value !== undefined && node.currentState !== Number(x.value)) { @@ -261,7 +247,7 @@ module.exports = function (RED) { process.nextTick(function () { setPinMode(node._pin, bonescript.INPUT, function (response, pin) { if (response.value === true) { - bonescript.digitalRead(node._pin, function (x) { + bonescript.digitalRead(node._pin, function (err, x) { // Initialise the currentState and lastActiveTime variables based on the value read node.currentState = Number(x.value); if (node.currentState === node.activeState) { @@ -357,7 +343,7 @@ module.exports = function (RED) { process.nextTick(function () { setPinMode(node._pin, bonescript.INPUT, function (response, pin) { if (response.value === true) { - bonescript.digitalRead(node._pin, function (x) { + bonescript.digitalRead(node._pin, function (err, x) { // Initialise the currentState based on the value read node.currentState = Number(x.value); // Attempt to attach an interrupt handler to the pin. If we succeed, @@ -417,24 +403,25 @@ module.exports = function (RED) { newState = !newState; } } - bonescript.digitalWrite(node._pin, newState ? 1 : 0); - node.send({topic: node.topic, payload: newState}); - node.currentState = newState; + bonescript.digitalWrite(node._pin, newState ? 1 : 0, function() { + node.send({topic: node.topic, payload: newState}); + node.currentState = newState; + }); }; // If we have a valid pin, set it as an output and set the default state if (gpioPins.concat(usrLEDs).indexOf(node.pin) >= 0) { // Don't set up interrupts & intervals until after the close event handler has been installed - bonescript.detachInterrupt(node._pin); + if (node._pin) { bonescript.detachInterrupt(node._pin); } process.nextTick(function () { setPinMode(node._pin, bonescript.OUTPUT, function (response, pin) { - if (response.value === true) { + if (response) { + node.error("Unable to set " + pin + " as output: " + response.err); + } else { node.on("input", inputCallback); setTimeout(function () { - bonescript.digitalWrite(node._pin, node.defaultState); + bonescript.digitalWrite(node._pin, node.defaultState, function() {}); }, 50); - } else { - node.error("Unable to set " + pin + " as output: " + response.err); } }); }); @@ -477,15 +464,17 @@ module.exports = function (RED) { if (node.retriggerable === false) { if (node.pulseTimer === null) { node.pulseTimer = setTimeout(endPulseCallback, time); - bonescript.digitalWrite(node._pin, node.pulseState); - node.send({topic: node.topic, payload: node.pulseState}); + bonescript.digitalWrite(node._pin, node.pulseState, function() { + node.send({topic: node.topic, payload: node.pulseState}); + }); } } else { if (node.pulseTimer !== null) { clearTimeout(node.pulseTimer); } else { - bonescript.digitalWrite(node._pin, node.pulseState); - node.send({topic: node.topic, payload: node.pulseState}); + bonescript.digitalWrite(node._pin, node.pulseState, function() { + node.send({topic: node.topic, payload: node.pulseState}); + }); } node.pulseTimer = setTimeout(endPulseCallback, time); } @@ -495,8 +484,9 @@ module.exports = function (RED) { // At the end of the pulse, restore the default state and set the timer to null var endPulseCallback = function () { node.pulseTimer = null; - bonescript.digitalWrite(node._pin, node.defaultState); - node.send({topic: node.topic, payload: node.defaultState}); + bonescript.digitalWrite(node._pin, node.defaultState, function() { + node.send({topic: node.topic, payload: node.defaultState}); + }); }; // If we have a valid pin, set it as an output and set the default state diff --git a/hardware/BBB/README.md b/hardware/BBB/README.md index fea49ac5..ff43e246 100644 --- a/hardware/BBB/README.md +++ b/hardware/BBB/README.md @@ -1,29 +1,34 @@ node-red-node-beaglebone ======================== -A set of Node-RED nodes to interface with the GPIO pins of a Beaglebone Black. +A set of Node-RED nodes to +interface with the GPIO pins of a Beaglebone Black. Pre-requisites -------------- -Only of use on a BeagleboneBlack board. Should ideally be running the latest Debian image - as that has node.js v0.10.x and the bonescript npm preinstalled. -it does also need bonescript - but this is also pre-installed so no need to mark as a dependency... +Only of use on a BeagleboneBlack board. Should ideally be running the + latest Debian +images - as they have node.js v0.10.x preinstalled. Install ------- -Run the following command in the root directory of your Node-RED install +For Debian **Jessie** with kernel 4.1 run the following command in the root +directory of your Node-RED install. This is usually `~/.node-red` npm install node-red-node-beaglebone -v0.4 now also supports OctaBoneScript if installed. +For previous versions of Debian, for example **Wheezy** - use the older version +of this node. + npm install node-red-node-beaglebone@0.0.8 Usage ----- This package provides 5 nodes for use with the BeagleboneBlack board. -###Analogue Input +### Analogue Input Reads an analogue pin when triggered by a message. @@ -41,7 +46,7 @@ each. Values between breakpoints are linearly interpolated. To reduce the effect of noise, enable averaging. This will read the input pin voltage ten times in rapid succession for each input message and output the mean value. -###Digital Input +### Digital Input Sends a message with payload 0 or 1 on the first output when the pin changes state, and logs the total time in the active state. @@ -57,7 +62,7 @@ The pin state messages may be generated for both directions of change, or for ju or just 1 to 0 changes. This is useful to generate a single message from a button press. When using buttons or switches, enable debouncing to improve reliability. -###Pulse Input +### Pulse Input Pulse input for the Beaglebone Black. Counts input pulses or pulse edges: outputs total counts and the rate of counts/sec, with scaling. @@ -67,22 +72,20 @@ rate message on the second output, at the chosen interval. An input message with and a numeric payload will set the total count to that value (no scaling is applied): any other input message will reset it to zero. -###Digital Output +### Digital Output Sets the output pin high or low depending on the payload of the input message. Numeric payloads > 0.5 are 'high' (1), payloads <= 0.5 are 'low' (0). Other payloads which evaluate to true are 'high', if not then 'low'. Selecting the Inverting checkbox will switch the sense of the pin output. - If the Toggle state checkbox is checked, the message content is ignored: successive messages cause the pin to toggle between 0 and 1. - The pin will be initially set to the given Startup state until the first message arrives: the Inverting property is not applied to this value. -###Pulse Output +### Pulse Output Pulses the output pin for the set time after receiving an input message, unless the message has a topic including the text 'time' and a numeric payload. In this case, the diff --git a/hardware/BBB/package.json b/hardware/BBB/package.json index 19511080..b689136f 100644 --- a/hardware/BBB/package.json +++ b/hardware/BBB/package.json @@ -1,15 +1,16 @@ { "name" : "node-red-node-beaglebone", - "version" : "0.0.8", + "version" : "0.1.0", "description" : "A set of Node-RED nodes to interface to the GPIO pins of a Beaglebone Black board", "dependencies" : { + "octalbonescript":"^1.1.*" }, "repository" : { "type":"git", "url":"https://github.com/node-red/node-red-nodes/tree/master/hardware/BBB" }, "license": "Apache-2.0", - "keywords": [ "node-red", "beagleboneblack", "beaglebone", "bbb" ], + "keywords": [ "node-red", "beagleboneblack", "beaglebone", "bbb", "octalbonescript" ], "node-red" : { "nodes" : { "bbb": "145-BBB-hardware.js"