2013-09-05 16:02:48 +02:00
|
|
|
/**
|
|
|
|
* Copyright 2013 IBM Corp.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
**/
|
|
|
|
|
|
|
|
var RED = require("../../red/red");
|
|
|
|
var util = require("util");
|
|
|
|
var firmata = require("firmata");
|
|
|
|
var arduinoReady = false;
|
|
|
|
var thisboard = null;
|
|
|
|
|
|
|
|
// The Board Definition - this opens (and closes) the connection
|
|
|
|
function ArduinoNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.device = n.device;
|
|
|
|
this.repeat = n.repeat||25;
|
2013-10-10 16:35:26 +02:00
|
|
|
util.log("[firmata] Opening "+this.device);
|
2013-10-08 00:36:44 +02:00
|
|
|
var node = this;
|
2013-09-05 16:02:48 +02:00
|
|
|
|
2013-10-08 00:36:44 +02:00
|
|
|
node.toun = setInterval(function() {
|
|
|
|
if (!arduinoReady) {
|
|
|
|
if (thisboard == null) {
|
|
|
|
node.board = new firmata.Board(node.device, function(err) {
|
|
|
|
if (err) {
|
|
|
|
console.log("[firmata] error: ",err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
arduinoReady = true;
|
|
|
|
thisboard = node.board;
|
|
|
|
clearInterval(node.toun);
|
|
|
|
util.log('[firmata] Arduino connected');
|
|
|
|
});
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2013-10-08 00:36:44 +02:00
|
|
|
else {
|
|
|
|
node.board = thisboard;
|
|
|
|
node.board.removeAllListeners();
|
|
|
|
arduinoReady = true;
|
|
|
|
clearInterval(node.toun);
|
2013-10-10 16:35:26 +02:00
|
|
|
node.toun = false;
|
|
|
|
util.log("[firmata] Arduino already connected");
|
2013-10-08 00:36:44 +02:00
|
|
|
}
|
|
|
|
} else { util.log("[firmata] Waiting for Firmata"); }
|
|
|
|
}, 10000); // wait for firmata to connect to arduino
|
2013-09-05 16:02:48 +02:00
|
|
|
|
2013-10-08 00:36:44 +02:00
|
|
|
this.on('close', function() {
|
2013-09-05 16:02:48 +02:00
|
|
|
//this.board.sp.close(function() { console.log("[firmata] Serial port closed"); arduinoReady = false; });
|
2013-10-10 16:35:26 +02:00
|
|
|
arduinoReady = false;
|
2013-10-08 00:36:44 +02:00
|
|
|
if (node.toun) {
|
|
|
|
clearInterval(node.toun);
|
2013-10-10 16:35:26 +02:00
|
|
|
util.log("[firmata] arduino wait loop stopped");
|
2013-10-08 00:36:44 +02:00
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
util.log("[firmata] Stopped");
|
2013-10-08 00:36:44 +02:00
|
|
|
});
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("arduino-board",ArduinoNode);
|
|
|
|
|
|
|
|
|
|
|
|
// The Input Node
|
|
|
|
function DuinoNodeIn(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.buttonState = -1;
|
|
|
|
this.pin = n.pin;
|
|
|
|
this.state = n.state;
|
|
|
|
this.arduino = n.arduino;
|
|
|
|
this.serverConfig = RED.nodes.getNode(this.arduino);
|
2013-09-06 14:57:43 +02:00
|
|
|
if (typeof this.serverConfig === "object") {
|
2013-09-05 16:02:48 +02:00
|
|
|
this.board = this.serverConfig.board;
|
2013-09-06 14:57:43 +02:00
|
|
|
this.repeat = this.serverConfig.repeat;
|
|
|
|
var node = this;
|
2013-10-08 00:36:44 +02:00
|
|
|
|
|
|
|
node.toui = setInterval(function() {
|
2013-10-10 16:35:26 +02:00
|
|
|
if (thisboard != null) {
|
|
|
|
node.board = thisboard;
|
2013-10-08 00:36:44 +02:00
|
|
|
clearInterval(node.toui);
|
2013-10-10 16:35:26 +02:00
|
|
|
node.toui = false;
|
|
|
|
//console.log(node.state,node.pin,node.board.MODES[node.state]);
|
2013-09-06 14:57:43 +02:00
|
|
|
node.board.pinMode(node.pin, node.board.MODES[node.state]);
|
|
|
|
node.board.setSamplingInterval(node.repeat);
|
|
|
|
var oldrdg = "";
|
|
|
|
if (node.state == "ANALOG") {
|
|
|
|
node.board.analogRead(node.pin, function(data) {
|
|
|
|
var msg = {payload:data, topic:"A"+node.pin};
|
|
|
|
if (data != oldrdg) {
|
|
|
|
node.send(msg);
|
|
|
|
oldrdg = data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node.board.digitalRead(node.pin, function(data) {
|
|
|
|
var msg = {payload:data, topic:node.pin};
|
2013-09-05 16:02:48 +02:00
|
|
|
node.send(msg);
|
2013-09-06 14:57:43 +02:00
|
|
|
});
|
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2013-09-06 14:57:43 +02:00
|
|
|
else { node.log("Waiting for Arduino"); }
|
2013-10-08 00:36:44 +02:00
|
|
|
}, 5000); // loop to wait for firmata to connect to arduino
|
|
|
|
|
|
|
|
this.on('close', function() {
|
|
|
|
if (node.toui) {
|
|
|
|
clearInterval(node.toui);
|
2013-10-10 16:35:26 +02:00
|
|
|
util.log("[firmata] input wait loop stopped");
|
2013-10-08 00:36:44 +02:00
|
|
|
}
|
|
|
|
});
|
2013-09-06 14:57:43 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-10-10 16:35:26 +02:00
|
|
|
util.log("[firmata] Serial Port not Configured");
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("arduino in",DuinoNodeIn);
|
|
|
|
|
|
|
|
|
|
|
|
// The Output Node
|
|
|
|
function DuinoNodeOut(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.buttonState = -1;
|
|
|
|
this.pin = n.pin;
|
|
|
|
this.state = n.state;
|
|
|
|
this.arduino = n.arduino;
|
|
|
|
this.serverConfig = RED.nodes.getNode(this.arduino);
|
2013-09-06 14:57:43 +02:00
|
|
|
if (typeof this.serverConfig === "object") {
|
|
|
|
this.board = this.serverConfig.board;
|
|
|
|
var node = this;
|
2013-10-08 00:36:44 +02:00
|
|
|
|
2013-09-06 14:57:43 +02:00
|
|
|
this.on("input", function(msg) {
|
|
|
|
//console.log(msg);
|
2013-10-10 16:35:26 +02:00
|
|
|
if (thisboard != null) {
|
2013-09-06 14:57:43 +02:00
|
|
|
if (node.state == "OUTPUT") {
|
|
|
|
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);
|
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2013-09-06 14:57:43 +02:00
|
|
|
if (node.state == "PWM") {
|
|
|
|
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);
|
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2013-09-06 14:57:43 +02:00
|
|
|
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);
|
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
|
|
|
}
|
2013-09-06 14:57:43 +02:00
|
|
|
//else { console.log("Arduino not ready"); }
|
|
|
|
});
|
2013-10-08 00:36:44 +02:00
|
|
|
|
|
|
|
node.touo = setInterval(function() {
|
2013-10-10 16:35:26 +02:00
|
|
|
if (thisboard != null) {
|
2013-10-08 00:36:44 +02:00
|
|
|
clearInterval(node.touo);
|
2013-10-10 16:35:26 +02:00
|
|
|
node.touo = false;
|
|
|
|
node.board = thisboard;
|
2013-09-06 14:57:43 +02:00
|
|
|
node.board.pinMode(node.pin, node.board.MODES[node.state]);
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
2013-10-10 16:35:26 +02:00
|
|
|
else { util.log("[firmata] waiting for arduino to connect"); }
|
2013-09-06 14:57:43 +02:00
|
|
|
}, 5000); // loop to wait for firmata to connect to arduino
|
2013-10-08 00:36:44 +02:00
|
|
|
|
|
|
|
this.on('close', function() {
|
|
|
|
if (node.touo) {
|
|
|
|
clearInterval(node.touo);
|
2013-10-10 16:35:26 +02:00
|
|
|
util.log("[firmata] output wait loop stopped");
|
2013-10-08 00:36:44 +02:00
|
|
|
}
|
|
|
|
});
|
2013-09-06 14:57:43 +02:00
|
|
|
}
|
|
|
|
else {
|
2013-10-10 16:35:26 +02:00
|
|
|
util.log("[firmata] Serial Port not Configured");
|
2013-09-06 14:57:43 +02:00
|
|
|
}
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|
|
|
|
RED.nodes.registerType("arduino out",DuinoNodeOut);
|