Add debounce time to the to BBB input node

This commit is contained in:
Dave Conway-Jones 2016-02-02 12:36:27 +00:00
parent 7cb43208e6
commit ed4b6b18ec
3 changed files with 12 additions and 12 deletions

View File

@ -228,7 +228,7 @@ voltage ten times in rapid succession for each input message and output the mean
</div>
<div class="form-row">
<label for="node-input-debounce">Debounce</label>
<input type="checkbox" id="node-input-debounce" style="display:inline-block; width:auto; vertical-align:top;">
<input type="text" id="node-input-debounce" placeholder="debounce time in mS" style="width: 160px"> mS
</div>
<div class="form-row">
<label for="node-input-outputOn">Output on</label>
@ -284,7 +284,7 @@ press. When using buttons or switches, enable debouncing to improve reliability.
defaults: { // defines the editable properties of the node
pin: { value:"", required:true },
activeLow: { value:false, required:true },
debounce: { value:false, required:true },
debounce: { value:0, required:true },
outputOn: { value:"both", required:true },
updateInterval: { value:60, required:true, validate:RED.validators.number() },
topic: { value:"" },

View File

@ -111,7 +111,7 @@ module.exports = function (RED) {
this.activeState = 1;
}
this.updateInterval = n.updateInterval*1000; // How often to send totalActiveTime messages
this.debounce = n.debounce; // Enable switch contact debouncing algorithm
this.debounce = n.debounce || null; // Enable switch contact debouncing algorithm
if (n.outputOn === "rising") {
this.activeEdges = [false, true];
} else if (n.outputOn === "falling") {
@ -140,7 +140,7 @@ module.exports = function (RED) {
// 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 (err, x) {
if (x.value === undefined) {
if (x === undefined) {
if (x.attached === true) {
node.interruptAttached = true;
node.on("input", inputCallback);
@ -148,13 +148,13 @@ module.exports = function (RED) {
} else {
node.error("Failed to attach interrupt");
}
} else if (node.currentState !== Number(x.value)) {
} else if (node.currentState !== Number(x)) {
if (node.debounce) {
if (node.debouncing === false) {
node.debouncing = true;
node.debounceTimer = setTimeout(function () {
bonescript.digitalRead(node._pin, debounceCallback);
}, 7);
}, Number(node.debounce));
}
} else {
sendStateMessage(x);
@ -168,7 +168,7 @@ module.exports = function (RED) {
var debounceCallback = function (err, x) {
node.debounceTimer = null;
node.debouncing = false;
if (x.value !== undefined && node.currentState !== Number(x.value)) {
if (x !== undefined && node.currentState !== Number(x)) {
sendStateMessage(x);
}
};
@ -177,7 +177,7 @@ module.exports = function (RED) {
// have determined we have a 'genuine' change of state. Update the currentState and
// ActiveTime variables, and send a message on the first output with the new state
var sendStateMessage = function (x) {
node.currentState = Number(x.value);
node.currentState = Number(x);
var now = Date.now();
if (node.currentState === node.activeState) {
node.lastActiveTime = now;
@ -249,7 +249,7 @@ module.exports = function (RED) {
if (!response) {
bonescript.digitalRead(node._pin, function (err, x) {
// Initialise the currentState and lastActiveTime variables based on the value read
node.currentState = Number(x.value);
node.currentState = Number(x);
if (node.currentState === node.activeState) {
node.lastActiveTime = Date.now();
}
@ -296,7 +296,7 @@ module.exports = function (RED) {
// 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 === undefined) {
if (x.attached === true) {
node.interruptAttached = true;
node.on("input", inputCallback);
@ -345,7 +345,7 @@ module.exports = function (RED) {
if (!response) {
bonescript.digitalRead(node._pin, function (err, x) {
// Initialise the currentState based on the value read
node.currentState = Number(x.value);
node.currentState = Number(x);
// Attempt to attach an interrupt handler to the pin. If we succeed,
// set the input event and interval handlers
var interruptType;

View File

@ -1,6 +1,6 @@
{
"name" : "node-red-node-beaglebone",
"version" : "0.1.3",
"version" : "0.1.4",
"description" : "A set of Node-RED nodes to interface to the GPIO pins of a Beaglebone Black board",
"dependencies" : {
"octalbonescript":"^1.1.*"