BBB nodes will now use octalbonescript if it is available

This commit is contained in:
Maxwell Hadley 2014-12-29 18:15:57 +00:00
parent 7d541c38a7
commit ee341f7056
1 changed files with 287 additions and 240 deletions

223
hardware/BBB/145-BBB-hardware.js Normal file → Executable file
View File

@ -16,16 +16,48 @@
module.exports = function (RED) {
"use strict";
var bonescript = require("bonescript");
var bonescript, adjustName, setPinMode;
var analogInputPins = ["P9_39", "P9_40", "P9_37", "P9_38", "P9_33", "P9_36", "P9_35"];
var gpioPins = ["P8_7", "P8_8", "P8_9", "P8_10", "P8_11", "P8_12", "P8_13", "P8_14", "P8_15",
"P8_16", "P8_17", "P8_18", "P8_19", "P8_26", "P9_11", "P9_12", "P9_13", "P9_14",
"P9_15", "P9_16", "P9_17", "P9_18", "P9_21", "P9_22", "P9_23", "P9_24", "P9_26",
"P9_27", "P9_30", "P9_41", "P9_42"];
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) {
bonescript = require("bonescript");
adjustName = function (pin) {
return pin;
};
setPinMode = function (pin, direction, callback) {
bonescript.pinMode(pin, direction, undefined, undefined, undefined, callback);
}
}
// Node constructor for bbb-analogue-in
function AnalogueInputNode(n) {
// Create a RED node
RED.nodes.createNode(this, n);
var node = this;
// Store local copies of the node configuration (as defined in the .html)
this.topic = n.topic;
this.pin = n.pin;
this.pin = n.pin; // The Beaglebone Black pin identifying string
this.breakpoints = n.breakpoints;
this.averaging = n.averaging;
if (this.averaging) {
@ -34,9 +66,6 @@ module.exports = function(RED) {
this.averages = 1;
}
// Define 'node' to allow us to access 'this' from within callbacks
var node = this;
// Variables used for input averaging
var sum; // accumulates the input readings to be averaged
var count; // keep track of the number of measurements made
@ -48,14 +77,16 @@ module.exports = function(RED) {
sum = sum + x.value;
count = count - 1;
if (count > 0) {
bonescript.analogRead(node.pin, analogReadCallback);
bonescript.analogRead(node._pin, analogReadCallback);
} else {
var msg = {};
msg.topic = node.topic;
sum = sum/node.averages;
// i is the index of the first breakpoint where the 'input' value is strictly
// greater than the measurement (note: a measurement can never be == 1)
var i = node.breakpoints.map(function (breakpoint) { return sum >= breakpoint.input; }).indexOf(false);
var i = node.breakpoints.map(function (breakpoint) {
return sum >= breakpoint.input;
}).indexOf(false);
msg.payload = node.breakpoints[i - 1].output + (node.breakpoints[i].output - node.breakpoints[i - 1].output)*
(sum - node.breakpoints[i - 1].input)/(node.breakpoints[i].input - node.breakpoints[i - 1].input);
node.send(msg);
@ -63,11 +94,11 @@ module.exports = function(RED) {
};
// If we have a valid pin, set the input event handler to Bonescript's analogRead
if (["P9_39", "P9_40", "P9_37", "P9_38", "P9_33", "P9_36", "P9_35"].indexOf(node.pin) >= 0) {
node.on("input", function (msg) {
if (analogInputPins.indexOf(node.pin) >= 0) {
node.on("input", function () {
sum = 0;
count = node.averages;
bonescript.analogRead(node.pin, analogReadCallback);
bonescript.analogRead(node._pin, analogReadCallback);
});
} else {
node.error("Unconfigured input pin");
@ -77,16 +108,16 @@ module.exports = function(RED) {
// Node constructor for bbb-discrete-in
function DiscreteInputNode(n) {
RED.nodes.createNode(this, n);
var node = this;
// Store local copies of the node configuration (as defined in the .html)
this.topic = n.topic; // the topic is not currently used
this.pin = n.pin; // The Beaglebone Black pin identifying string
if (n.activeLow) { // Set the 'active' state 0 or 1 as appropriate
this._pin = adjustName(this.pin); // Adjusted for Octal if necessary
if (n.activeLow) // Set the 'active' state 0 or 1 as appropriate
this.activeState = 0;
}
else {
else
this.activeState = 1;
}
this.updateInterval = n.updateInterval*1000; // How often to send totalActiveTime messages
this.debounce = n.debounce; // Enable switch contact debouncing algorithm
if (n.outputOn === "rising") {
@ -110,21 +141,28 @@ module.exports = function(RED) {
this.debouncing = false; // True after a change of state while waiting for the 7ms debounce time to elapse
this.debounceTimer = null;
// Define 'node' to allow us to access 'this' from within callbacks
var node = this;
// This function is called by the input pin change-of-state interrupt. If
// debounce is disabled, send the output message. Otherwise, if we are
// currently debouncing, ignore this interrupt. If we are not debouncing,
// schedule a re-read of the input pin in 7ms time, and set the debouncing flag
// Note: this function gets called spuriously when the interrupt is first enabled:
// in this case x.value is undefined - we must test for this
// 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) {
if (x.value !== undefined && node.currentState !== Number(x.value)) {
if (x.value === undefined) {
if (x.attached === true) {
node.interruptAttached = true;
node.on("input", inputCallback);
node.intervalId = setInterval(timerCallback, node.updateInterval);
} else {
node.error("Failed to attach interrupt");
}
} else if (node.currentState !== Number(x.value)) {
if (node.debounce) {
if (node.debouncing === false) {
node.debouncing = true;
node.debounceTimer = setTimeout(function () { bonescript.digitalRead(node.pin, debounceCallback); }, 7);
node.debounceTimer = setTimeout(function () {
bonescript.digitalRead(node._pin, debounceCallback);
}, 7);
}
} else {
sendStateMessage(x);
@ -177,7 +215,7 @@ module.exports = function(RED) {
// Re-synchronise the pin state if we have missed a state change interrupt for some
// reason, and we are not in the process of debouncing one
if (node.debouncing === false) {
bonescript.digitalRead(node.pin, interruptCallback);
bonescript.digitalRead(node._pin, interruptCallback);
}
};
@ -186,7 +224,7 @@ module.exports = function(RED) {
// payload, if possible. Otherwise clear the totalActiveTime (so we start counting
// from zero again)
var inputCallback = function (ipMsg) {
if (String(ipMsg.topic).search(/load/i) < 0 || isFinite(ipMsg.payload) === false) {
if (String(ipMsg.topic).search(/load/i) < 0 || isFinite(ipMsg.payload) == false) {
node.totalActiveTime = 0;
} else {
node.totalActiveTime = Number(ipMsg.payload);
@ -211,32 +249,29 @@ module.exports = function(RED) {
};
// If we have a valid pin, set it as an input and read the (digital) state
if (["P8_7", "P8_8", "P8_9", "P8_10", "P8_11", "P8_12", "P8_13", "P8_14", "P8_15",
"P8_16", "P8_17", "P8_18", "P8_19", "P8_26", "P9_11", "P9_12", "P9_13", "P9_14",
"P9_15", "P9_16", "P9_17", "P9_18", "P9_21", "P9_22", "P9_23", "P9_24", "P9_26",
"P9_27", "P9_30", "P9_41", "P9_42"].indexOf(node.pin) >= 0) {
if (gpioPins.indexOf(node.pin) >= 0) {
// Don't set up interrupts & intervals until after the close event handler has been installed
bonescript.detachInterrupt(node.pin);
bonescript.detachInterrupt(node._pin);
process.nextTick(function () {
bonescript.pinMode(node.pin, bonescript.INPUT);
bonescript.digitalRead(node.pin, function (x) {
// Initialise the currentState and lastActveTime variables based on the value read
setPinMode(node._pin, bonescript.INPUT, function (response, pin) {
if (response.value === true) {
bonescript.digitalRead(node._pin, function (x) {
// Initialise the currentState and lastActiveTime variables based on the value read
node.currentState = Number(x.value);
if (node.currentState === node.activeState) {
node.lastActiveTime = Date.now();
// switch to process.hrtime()
}
// Attempt to attach a change-of-state interrupt handler to the pin. If we succeed,
// set the input event and interval handlers, then send an initial message with the
// pin state on the first output
if (bonescript.attachInterrupt(node.pin, true, bonescript.CHANGE, interruptCallback)) {
node.interruptAttached = true;
node.on("input", inputCallback);
node.intervalId = setInterval(timerCallback, node.updateInterval);
// the input event and interval handlers will be installed by interruptCallback
bonescript.attachInterrupt(node._pin, true, bonescript.CHANGE, interruptCallback);
// Send an initial message with the pin state on the first output
setTimeout(function () {
node.emit("input", {});
}, 50);
});
} else {
node.error("Failed to attach interrupt");
node.error("Unable to set " + pin + " as input: " + response.err);
}
setTimeout(function () { node.emit("input", {}); }, 50);
});
});
} else {
@ -247,13 +282,15 @@ module.exports = function(RED) {
// Node constructor for bbb-pulse-in
function PulseInputNode(n) {
RED.nodes.createNode(this, n);
var node = this;
// Store local copies of the node configuration (as defined in the .html)
this.topic = n.topic; // the topic is not currently used
this.pin = n.pin; // The Beaglebone Black pin identifying string
this._pin = adjustName(this.pin); // Adjusted for Octal if necessary
this.updateInterval = n.updateInterval*1000; // How often to send output messages
this.countType = n.countType; // Sets either 'edge' or 'pulse' counting
this.countUnit = n.countUnit; // Scaling appling to count output
this.countUnit = n.countUnit; // Scaling applied to count output
this.countRate = n.countRate; // Scaling applied to rate output
// Working variables
@ -263,13 +300,19 @@ module.exports = function(RED) {
// Hold the hrtime of the last two pulses (with ns resolution)
this.pulseTime = [[NaN, NaN], [NaN, NaN]];
// Define 'node' to allow us to access 'this' from within callbacks
var node = this;
// Called by the edge or pulse interrupt. If this is a valid interrupt, record the
// pulse time and count the pulse
// Called by the edge or pulse interrupt. Record the pulse time and count the pulse
// 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) {
if (x.value !== undefined) {
if (x.value === undefined) {
if (x.attached === true) {
node.interruptAttached = true;
node.on("input", inputCallback);
node.intervalId = setInterval(timerCallback, node.updateInterval);
} else {
node.error("Failed to attach interrupt");
}
} else {
node.pulseTime = [node.pulseTime[1], process.hrtime()];
node.pulseCount = node.pulseCount + 1;
}
@ -279,7 +322,7 @@ module.exports = function(RED) {
// insensitive) and the payload is a valid number, set the count to that
// number, otherwise set it to zero
var inputCallback = function (msg) {
if (String(msg.topic).search(/load/i) < 0 || isFinite(msg.payload) === false) {
if (String(msg.topic).search(/load/i) < 0 || isFinite(msg.payload) == false) {
node.pulseCount = 0;
} else {
node.pulseCount = Number(msg.payload);
@ -302,15 +345,13 @@ module.exports = function(RED) {
};
// If we have a valid pin, set it as an input and read the (digital) state
if (["P8_7", "P8_8", "P8_9", "P8_10", "P8_11", "P8_12", "P8_13", "P8_14", "P8_15",
"P8_16", "P8_17", "P8_18", "P8_19", "P8_26", "P9_11", "P9_12", "P9_13", "P9_14",
"P9_15", "P9_16", "P9_17", "P9_18", "P9_21", "P9_22", "P9_23", "P9_24", "P9_26",
"P9_27", "P9_30", "P9_41", "P9_42"].indexOf(node.pin) >= 0) {
if (gpioPins.indexOf(node.pin) >= 0) {
// Don't set up interrupts & intervals until after the close event handler has been installed
bonescript.detachInterrupt(node.pin);
bonescript.detachInterrupt(node._pin);
process.nextTick(function () {
bonescript.pinMode(node.pin, bonescript.INPUT);
bonescript.digitalRead(node.pin, function (x) {
setPinMode(node._pin, bonescript.INPUT, function (response, pin) {
if (response.value === true) {
bonescript.digitalRead(node._pin, function (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,
@ -322,12 +363,12 @@ module.exports = function(RED) {
} else {
interruptType = bonescript.CHANGE;
}
if (bonescript.attachInterrupt(node.pin, true, interruptType, interruptCallback)) {
node.interruptAttached = true;
node.on("input", inputCallback);
node.intervalId = setInterval(timerCallback, node.updateInterval);
// Attempt to attach the required interrupt handler to the pin. If we succeed,
// the input event and interval handlers will be installed by interruptCallback
bonescript.attachInterrupt(node._pin, true, interruptType, interruptCallback)
});
} else {
node.error("Failed to attach interrupt");
node.error("Unable to set " + pin + " as input: " + response.err);
}
});
});
@ -339,10 +380,12 @@ module.exports = function(RED) {
// Node constructor for bbb-discrete-out
function DiscreteOutputNode(n) {
RED.nodes.createNode(this, n);
var node = this;
// Store local copies of the node configuration (as defined in the .html)
this.topic = n.topic; // the topic is not currently used
this.pin = n.pin; // The Beaglebone Black pin identifying string
this._pin = adjustName(this.pin); // Adjusted for Octal if necessary
this.defaultState = Number(n.defaultState); // What state to set up as
this.inverting = n.inverting;
this.toggle = n.toggle;
@ -350,9 +393,7 @@ module.exports = function(RED) {
// Working variables
this.currentState = this.defaultState;
var node = this;
// If the input message paylod is numeric, values > 0.5 are 'true', otherwise use
// If the input message payload is numeric, values > 0.5 are 'true', otherwise use
// the truthiness of the payload. Apply the inversion flag before setting the output
var inputCallback = function (msg) {
var newState;
@ -360,7 +401,7 @@ module.exports = function(RED) {
newState = node.currentState === 0 ? 1 : 0;
} else {
if (isFinite(Number(msg.payload))) {
newState = Number(msg.payload) > 0.5 ? true : false;
newState = Number(msg.payload) > 0.5;
} else if (msg.payload) {
newState = true;
} else {
@ -370,22 +411,26 @@ module.exports = function(RED) {
newState = !newState;
}
}
bonescript.digitalWrite(node.pin, newState ? 1 : 0);
bonescript.digitalWrite(node._pin, newState ? 1 : 0);
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 (["P8_7", "P8_8", "P8_9", "P8_10", "P8_11", "P8_12", "P8_13", "P8_14", "P8_15",
"P8_16", "P8_17", "P8_18", "P8_19", "P8_26", "P9_11", "P9_12", "P9_13", "P9_14",
"P9_15", "P9_16", "P9_17", "P9_18", "P9_21", "P9_22", "P9_23", "P9_24", "P9_26",
"P9_27", "P9_30", "P9_41", "P9_42", "USR0", "USR1", "USR2", "USR3"].indexOf(node.pin) >= 0) {
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);
bonescript.detachInterrupt(node._pin);
process.nextTick(function () {
bonescript.pinMode(node.pin, bonescript.OUTPUT);
setPinMode(node._pin, bonescript.OUTPUT, function (response, pin) {
if (response.value === true) {
node.on("input", inputCallback);
setTimeout(function () { bonescript.digitalWrite(node.pin, node.defaultState); }, 50);
setTimeout(function () {
bonescript.digitalWrite(node._pin, node.defaultState);
}, 50);
} else {
node.error("Unable to set " + pin + " as output: " + response.err);
}
});
});
} else {
node.error("Unconfigured output pin");
@ -395,10 +440,12 @@ module.exports = function(RED) {
// Node constructor for bbb-pulse-out
function PulseOutputNode(n) {
RED.nodes.createNode(this, n);
var node = this;
// Store local copies of the node configuration (as defined in the .html)
this.topic = n.topic; // the topic is not currently used
this.pin = n.pin; // The Beaglebone Black pin identifying string
this._pin = adjustName(this.pin); // Adjusted for Octal if necessary
this.pulseState = Number(n.pulseState); // What state the pulse will be..
this.defaultState = this.pulseState === 1 ? 0 : 1;
this.retriggerable = n.retriggerable;
@ -407,8 +454,6 @@ module.exports = function(RED) {
// Working variables
this.pulseTimer = null; // Non-null while a pulse is being generated
var node = this;
// Generate a pulse in response to an input message. If the topic includes the text
// 'time' (case insensitive) and the payload is numeric, use this value as the
// pulse time. Otherwise use the value from the properties dialog.
@ -426,14 +471,14 @@ module.exports = function(RED) {
if (node.retriggerable === false) {
if (node.pulseTimer === null) {
node.pulseTimer = setTimeout(endPulseCallback, time);
bonescript.digitalWrite(node.pin, node.pulseState);
bonescript.digitalWrite(node._pin, node.pulseState);
node.send({topic: node.topic, payload: node.pulseState});
}
} else {
if (node.pulseTimer !== null) {
clearTimeout(node.pulseTimer);
} else {
bonescript.digitalWrite(node.pin, node.pulseState);
bonescript.digitalWrite(node._pin, node.pulseState);
node.send({topic: node.topic, payload: node.pulseState});
}
node.pulseTimer = setTimeout(endPulseCallback, time);
@ -444,22 +489,24 @@ 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);
bonescript.digitalWrite(node._pin, node.defaultState);
node.send({topic: node.topic, payload: node.defaultState});
};
// If we have a valid pin, set it as an output and set the default state
if (["P8_7", "P8_8", "P8_9", "P8_10", "P8_11", "P8_12", "P8_13", "P8_14", "P8_15",
"P8_16", "P8_17", "P8_18", "P8_19", "P8_26", "P9_11", "P9_12", "P9_13", "P9_14",
"P9_15", "P9_16", "P9_17", "P9_18", "P9_21", "P9_22", "P9_23", "P9_24", "P9_26",
"P9_27", "P9_30", "P9_41", "P9_42", "USR0", "USR1", "USR2", "USR3"].indexOf(node.pin) >= 0) {
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);
bonescript.detachInterrupt(node._pin);
process.nextTick(function () {
bonescript.pinMode(node.pin, bonescript.OUTPUT);
setPinMode(node._pin, bonescript.OUTPUT, function (response, pin) {
if (response.value === true) {
node.on("input", inputCallback);
// Set the pin to the default stte once the dust settles
// Set the pin to the default state once the dust settles
setTimeout(endPulseCallback, 50);
} else {
node.error("Unable to set " + pin + " as output: " + response.err);
}
});
});
} else {
node.error("Unconfigured output pin");
@ -476,7 +523,7 @@ module.exports = function(RED) {
// On close, detach the interrupt (if we attached one) and clear any active timers
DiscreteInputNode.prototype.close = function () {
if (this.interruptAttached) {
bonescript.detachInterrupt(this.pin);
bonescript.detachInterrupt(this._pin);
}
if (this.intervalId !== null) {
clearInterval(this.intervalId);
@ -489,7 +536,7 @@ module.exports = function(RED) {
// On close, detach the interrupt (if we attached one) and clear the interval (if we set one)
PulseInputNode.prototype.close = function () {
if (this.interruptAttached) {
bonescript.detachInterrupt(this.pin);
bonescript.detachInterrupt(this._pin);
}
if (this.intervalId !== null) {
clearInterval(this.intervalId);
@ -502,4 +549,4 @@ module.exports = function(RED) {
clearTimeout(this.pulseTimer);
}
};
}
};