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

Catch board not configured error without crashing

This commit is contained in:
Dave Conway-Jones 2013-09-06 13:57:43 +01:00
parent 6922a71995
commit 28b7009f33

View File

@ -74,39 +74,44 @@ function DuinoNodeIn(n) {
this.state = n.state; this.state = n.state;
this.arduino = n.arduino; this.arduino = n.arduino;
this.serverConfig = RED.nodes.getNode(this.arduino); this.serverConfig = RED.nodes.getNode(this.arduino);
if (typeof this.serverConfig === "object") {
this.board = this.serverConfig.board; this.board = this.serverConfig.board;
this.repeat = this.serverConfig.repeat; this.repeat = this.serverConfig.repeat;
var node = this; var node = this;
var tout = setInterval(function() { var tout = setInterval(function() {
if (arduinoReady) { if (arduinoReady) {
clearInterval(tout); clearInterval(tout);
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);
var oldrdg = ""; var oldrdg = "";
if (node.state == "ANALOG") { if (node.state == "ANALOG") {
node.board.analogRead(node.pin, function(data) { node.board.analogRead(node.pin, function(data) {
var msg = {payload:data, topic:"A"+node.pin}; var msg = {payload:data, topic:"A"+node.pin};
if (data != oldrdg) { if (data != oldrdg) {
node.send(msg);
oldrdg = data;
}
});
}
else {
node.board.digitalRead(node.pin, function(data) {
var msg = {payload:data, topic:node.pin};
node.send(msg); node.send(msg);
oldrdg = data; });
} }
});
}
else {
node.board.digitalRead(node.pin, function(data) {
var msg = {payload:data, topic:node.pin};
node.send(msg);
});
} }
else { node.log("Waiting for Arduino"); }
}, 2000); // loop to wait for firmata to connect to arduino
this._close = function() {
clearInterval(this._interval);
util.log("[arduino] input eventlistener stopped");
} }
else { node.log("Waiting for Arduino"); } }
}, 2000); // loop to wait for firmata to connect to arduino else {
util.log("[arduino] Serial Port not Configured");
this._close = function() {
clearInterval(this._interval);
util.log("[firmata] input eventlistener stopped");
} }
} }
RED.nodes.registerType("arduino in",DuinoNodeIn); RED.nodes.registerType("arduino in",DuinoNodeIn);
@ -124,44 +129,49 @@ function DuinoNodeOut(n) {
this.state = n.state; this.state = n.state;
this.arduino = n.arduino; this.arduino = n.arduino;
this.serverConfig = RED.nodes.getNode(this.arduino); this.serverConfig = RED.nodes.getNode(this.arduino);
this.board = this.serverConfig.board; if (typeof this.serverConfig === "object") {
var node = this; this.board = this.serverConfig.board;
var node = this;
this.on("input", function(msg) {
//console.log(msg); this.on("input", function(msg) {
if (arduinoReady) { //console.log(msg);
if (node.state == "OUTPUT") { if (arduinoReady) {
if ((msg.payload == true)||(msg.payload == 1)||(msg.payload.toString().toLowerCase() == "on")) { if (node.state == "OUTPUT") {
node.board.digitalWrite(node.pin, node.board.HIGH); if ((msg.payload == true)||(msg.payload == 1)||(msg.payload.toString().toLowerCase() == "on")) {
node.board.digitalWrite(node.pin, node.board.HIGH);
}
if ((msg.payload == false)||(msg.payload == 0)||(msg.payload.toString().toLowerCase() == "off")) {
node.board.digitalWrite(node.pin, node.board.LOW);
}
} }
if ((msg.payload == false)||(msg.payload == 0)||(msg.payload.toString().toLowerCase() == "off")) { if (node.state == "PWM") {
node.board.digitalWrite(node.pin, node.board.LOW); msg.payload = msg.payload * 1;
if ((msg.payload >= 0) && (msg.payload <= 255)) {
//console.log(msg.payload, node.pin);
node.board.servoWrite(node.pin, msg.payload);
}
}
if (node.state == "SERVO") {
msg.payload = msg.payload * 1;
if ((msg.payload >= 0) && (msg.payload <= 180)) {
//console.log(msg.payload, node.pin);
node.board.servoWrite(node.pin, msg.payload);
}
} }
} }
if (node.state == "PWM") { //else { console.log("Arduino not ready"); }
msg.payload = msg.payload * 1; });
if ((msg.payload >= 0) && (msg.payload <= 255)) {
//console.log(msg.payload, node.pin); var touo = setInterval(function() {
node.board.servoWrite(node.pin, msg.payload); if (arduinoReady) {
} clearInterval(touo);
//console.log(node.state,node.pin,node.board.MODES[node.state]);
node.board.pinMode(node.pin, node.board.MODES[node.state]);
} }
if (node.state == "SERVO") { }, 5000); // loop to wait for firmata to connect to arduino
msg.payload = msg.payload * 1; }
if ((msg.payload >= 0) && (msg.payload <= 180)) { else {
//console.log(msg.payload, node.pin); util.log("[arduino] Serial Port not Configured");
node.board.servoWrite(node.pin, msg.payload); }
}
}
}
//else { console.log("Arduino not ready"); }
});
var touo = setInterval(function() {
if (arduinoReady) {
clearInterval(touo);
//console.log(node.state,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
} }
RED.nodes.registerType("arduino out",DuinoNodeOut); RED.nodes.registerType("arduino out",DuinoNodeOut);