1
0
mirror of https://github.com/node-red/node-red.git synced 2023-10-10 13:36:53 +02:00

added more debug / state checking to Arduino node

This commit is contained in:
Dave C-J 2013-10-07 23:36:44 +01:00
parent f1bcf130aa
commit a1de6d2d0b

View File

@ -26,45 +26,45 @@ function ArduinoNode(n) {
this.device = n.device; this.device = n.device;
this.repeat = n.repeat||25; this.repeat = n.repeat||25;
util.log("[firmata] Opening"+this.device); util.log("[firmata] Opening"+this.device);
var node = this;
// var tou = setInterval(function() { node.toun = setInterval(function() {
// if (!arduinoReady) { if (!arduinoReady) {
// clearInterval(tou);
arduinoReady = false; arduinoReady = false;
if (thisboard == null) { if (thisboard == null) {
this.board = new firmata.Board(this.device, function(err) { node.board = new firmata.Board(node.device, function(err) {
if (err) { if (err) {
util.log("[firmata] "+err); console.log("[firmata] error: ",err);
return; return;
} }
arduinoReady = true; arduinoReady = true;
thisboard = node.board;
clearInterval(node.toun);
util.log('[firmata] Arduino connected'); util.log('[firmata] Arduino connected');
}); });
thisboard = this.board;
} }
else { else {
util.log("[firmata] Arduino already connected"); util.log("[firmata] Arduino already connected");
this.board = thisboard; node.board = thisboard;
console.log(this.board._events); console.log(node.board.sp);
this.board.removeAllListeners(); node.board.removeAllListeners();
arduinoReady = true; arduinoReady = true;
clearInterval(node.toun);
} }
} else { util.log("[firmata] Waiting for Firmata"); }
}, 10000); // wait for firmata to connect to arduino
// } else { util.log("[firmata] Waiting for Firmata"); } this.on('close', function() {
// }, 1000); // wait for firmata to disconnect from arduino
this._close = function() {
//this.board.sp.close(function() { console.log("[firmata] Serial port closed"); arduinoReady = false; }); //this.board.sp.close(function() { console.log("[firmata] Serial port closed"); arduinoReady = false; });
util.log("[firmata] Stopped"); if (node.toun) {
clearInterval(node.toun);
util.log("[arduino] arduino wait loop stopped");
} }
util.log("[firmata] Stopped");
});
} }
RED.nodes.registerType("arduino-board",ArduinoNode); RED.nodes.registerType("arduino-board",ArduinoNode);
ArduinoNode.prototype.close = function() {
this._close();
}
// The Input Node // The Input Node
function DuinoNodeIn(n) { function DuinoNodeIn(n) {
@ -79,9 +79,9 @@ function DuinoNodeIn(n) {
this.repeat = this.serverConfig.repeat; this.repeat = this.serverConfig.repeat;
var node = this; var node = this;
var tout = setInterval(function() { node.toui = setInterval(function() {
if (arduinoReady) { if (arduinoReady) {
clearInterval(tout); clearInterval(node.toui);
console.log(node.state,node.pin,node.board.MODES[node.state]); console.log(node.state,node.pin,node.board.MODES[node.state]);
node.board.pinMode(node.pin, node.board.MODES[node.state]); node.board.pinMode(node.pin, node.board.MODES[node.state]);
node.board.setSamplingInterval(node.repeat); node.board.setSamplingInterval(node.repeat);
@ -103,12 +103,14 @@ function DuinoNodeIn(n) {
} }
} }
else { node.log("Waiting for Arduino"); } else { node.log("Waiting for Arduino"); }
}, 2000); // loop to wait for firmata to connect to arduino }, 5000); // loop to wait for firmata to connect to arduino
this._close = function() { this.on('close', function() {
clearInterval(this._interval); if (node.toui) {
util.log("[arduino] input eventlistener stopped"); clearInterval(node.toui);
util.log("[arduino] input wait loop stopped");
} }
});
} }
else { else {
util.log("[arduino] Serial Port not Configured"); util.log("[arduino] Serial Port not Configured");
@ -116,10 +118,6 @@ function DuinoNodeIn(n) {
} }
RED.nodes.registerType("arduino in",DuinoNodeIn); RED.nodes.registerType("arduino in",DuinoNodeIn);
DuinoNodeIn.prototype.close = function() {
this._close();
}
// The Output Node // The Output Node
function DuinoNodeOut(n) { function DuinoNodeOut(n) {
@ -162,13 +160,20 @@ function DuinoNodeOut(n) {
//else { console.log("Arduino not ready"); } //else { console.log("Arduino not ready"); }
}); });
var touo = setInterval(function() { node.touo = setInterval(function() {
if (arduinoReady) { if (arduinoReady) {
clearInterval(touo); clearInterval(node.touo);
//console.log(node.state,node.pin,node.board.MODES[node.state]); //console.log(node.state,node.pin,node.board.MODES[node.state]);
node.board.pinMode(node.pin, node.board.MODES[node.state]); node.board.pinMode(node.pin, node.board.MODES[node.state]);
} }
}, 5000); // loop to wait for firmata to connect to arduino }, 5000); // loop to wait for firmata to connect to arduino
this.on('close', function() {
if (node.touo) {
clearInterval(node.touo);
util.log("[arduino] output wait loop stopped");
}
});
} }
else { else {
util.log("[arduino] Serial Port not Configured"); util.log("[arduino] Serial Port not Configured");