Update BBB nodes to support Jessie

Fix to close #156
Now mandates octalbonescript so a minor version bump.
This commit is contained in:
Dave Conway-Jones
2016-01-01 14:38:39 +00:00
parent c2dc764965
commit 681bdae576
3 changed files with 55 additions and 61 deletions

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

@@ -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