2015-06-06 19:13:53 +02:00
|
|
|
|
|
|
|
module.exports = function(RED) {
|
|
|
|
"use strict";
|
2016-07-13 15:19:09 +02:00
|
|
|
|
|
|
|
var Board = require('firmata');
|
2017-01-29 18:39:44 +01:00
|
|
|
var SP = require('serialport');
|
2015-06-06 19:13:53 +02:00
|
|
|
|
|
|
|
// The Board Definition - this opens (and closes) the connection
|
|
|
|
function ArduinoNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
|
|
|
this.device = n.device || null;
|
|
|
|
var node = this;
|
2016-07-13 15:19:09 +02:00
|
|
|
|
|
|
|
node.board = Board(node.device, function(e) {
|
|
|
|
//console.log("ERR",e);
|
|
|
|
if ((e !== undefined) && (e.toString().indexOf("cannot open") !== -1) ) {
|
|
|
|
node.error(RED._("arduino.errors.portnotfound",{device:node.device}));
|
2015-06-06 19:13:53 +02:00
|
|
|
}
|
2016-07-13 15:19:09 +02:00
|
|
|
else if (e === undefined) {
|
|
|
|
node.board.on('ready', function() {
|
|
|
|
node.log(RED._("arduino.status.connected",{device:node.board.sp.path}));
|
|
|
|
if (RED.settings.verbose) {
|
|
|
|
node.log(RED._("arduino.status.version",{version:node.board.firmware.name+"-"+node.board.version.major+"."+node.board.version.minor}));
|
|
|
|
}
|
|
|
|
});
|
2015-06-06 19:13:53 +02:00
|
|
|
}
|
2016-07-13 15:19:09 +02:00
|
|
|
node.board.on('close', function() {
|
|
|
|
node.error(RED._("arduino.status.portclosed"));
|
2015-06-06 19:13:53 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
node.on('close', function(done) {
|
|
|
|
if (node.board) {
|
|
|
|
try {
|
2016-07-13 15:19:09 +02:00
|
|
|
node.board.sp.close(function() {
|
2015-06-06 19:13:53 +02:00
|
|
|
done();
|
2015-06-16 11:36:19 +02:00
|
|
|
if (RED.settings.verbose) { node.log(RED._("arduino.status.portclosed")); }
|
2015-06-06 19:13:53 +02:00
|
|
|
});
|
2017-01-29 18:43:48 +01:00
|
|
|
}
|
|
|
|
catch(e) { done(); }
|
|
|
|
}
|
|
|
|
else { done(); }
|
2015-06-06 19:13:53 +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);
|
|
|
|
if (typeof this.serverConfig === "object") {
|
|
|
|
this.board = this.serverConfig.board;
|
|
|
|
var node = this;
|
2015-07-07 22:31:28 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:"node-red:common.status.connecting"});
|
2016-07-15 15:45:32 +02:00
|
|
|
var doit = function() {
|
2015-07-07 22:31:28 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
2016-07-13 15:19:09 +02:00
|
|
|
if (node.state === "ANALOG") {
|
|
|
|
node.board.pinMode(node.pin, 0x02);
|
|
|
|
node.board.analogRead(node.pin, function(v) {
|
|
|
|
node.send({payload:v, topic:"A"+node.pin});
|
2015-06-06 19:13:53 +02:00
|
|
|
});
|
|
|
|
}
|
2016-07-13 15:19:09 +02:00
|
|
|
if (node.state === "INPUT") {
|
|
|
|
node.board.pinMode(node.pin, 0x00);
|
|
|
|
node.board.digitalRead(node.pin, function(v) {
|
|
|
|
node.send({payload:v, topic:node.pin});
|
2015-06-06 19:13:53 +02:00
|
|
|
});
|
|
|
|
}
|
2016-03-02 14:44:33 +01:00
|
|
|
if (node.state == "STRING") {
|
2016-07-13 15:19:09 +02:00
|
|
|
node.board.on('string', function(v) {
|
|
|
|
node.send({payload:v, topic:"string"});
|
2016-03-02 14:44:33 +01:00
|
|
|
});
|
|
|
|
}
|
2016-07-15 15:45:32 +02:00
|
|
|
// node.board.on('close', function() {
|
|
|
|
// node.board.removeAllListeners();
|
|
|
|
// node.status({fill:"grey",shape:"ring",text:"node-red:common.status.not-connected"});
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
if (node.board.isReady) { doit(); }
|
|
|
|
else { node.board.on("ready", function() { doit(); }); }
|
|
|
|
node.on("close", function() {
|
|
|
|
if (node.tout) { clearTimeout(node.tout); }
|
|
|
|
})
|
2015-06-06 19:13:53 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-06-16 11:36:19 +02:00
|
|
|
this.warn(RED._("arduino.errors.portnotconf"));
|
2015-06-06 19:13:53 +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);
|
|
|
|
if (typeof this.serverConfig === "object") {
|
|
|
|
this.board = this.serverConfig.board;
|
|
|
|
var node = this;
|
2015-07-07 22:31:28 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:"node-red:common.status.connecting"});
|
2016-07-15 15:45:32 +02:00
|
|
|
var doit = function() {
|
2015-07-07 22:31:28 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
2015-06-06 19:13:53 +02:00
|
|
|
node.on("input", function(msg) {
|
|
|
|
if (node.state === "OUTPUT") {
|
2016-07-13 15:19:09 +02:00
|
|
|
node.board.pinMode(node.pin, 0x01);
|
2015-06-06 19:13:53 +02:00
|
|
|
if ((msg.payload === true)||(msg.payload.toString() == "1")||(msg.payload.toString().toLowerCase() == "on")) {
|
2016-07-13 15:19:09 +02:00
|
|
|
node.board.digitalWrite(node.pin, node.board.HIGH);
|
2015-06-06 19:13:53 +02:00
|
|
|
}
|
|
|
|
if ((msg.payload === false)||(msg.payload.toString() == "0")||(msg.payload.toString().toLowerCase() == "off")) {
|
2016-07-13 15:19:09 +02:00
|
|
|
node.board.digitalWrite(node.pin, node.board.LOW);
|
2015-06-06 19:13:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node.state === "PWM") {
|
2016-07-13 15:19:09 +02:00
|
|
|
node.board.pinMode(node.pin, 0x03);
|
2016-02-01 00:27:10 +01:00
|
|
|
msg.payload = parseInt((msg.payload * 1) + 0.5);
|
2015-06-06 19:13:53 +02:00
|
|
|
if ((msg.payload >= 0) && (msg.payload <= 255)) {
|
|
|
|
node.board.analogWrite(node.pin, msg.payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node.state === "SERVO") {
|
2016-07-13 15:19:09 +02:00
|
|
|
node.board.pinMode(node.pin, 0x04);
|
2016-02-01 00:27:10 +01:00
|
|
|
msg.payload = parseInt((msg.payload * 1) + 0.5);
|
2015-06-06 19:13:53 +02:00
|
|
|
if ((msg.payload >= 0) && (msg.payload <= 180)) {
|
|
|
|
node.board.servoWrite(node.pin, msg.payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node.state === "SYSEX") {
|
2016-07-13 15:19:09 +02:00
|
|
|
node.board.sysexCommand(msg.payload);
|
2015-06-06 19:13:53 +02:00
|
|
|
}
|
2016-03-02 14:44:33 +01:00
|
|
|
if (node.state === "STRING") {
|
2016-07-13 15:19:09 +02:00
|
|
|
node.board.sendString(msg.payload.toString());
|
2016-03-02 14:44:33 +01:00
|
|
|
}
|
2015-06-06 19:13:53 +02:00
|
|
|
});
|
2016-07-15 15:45:32 +02:00
|
|
|
// node.board.on('close', function() {
|
|
|
|
// node.status({fill:"grey",shape:"ring",text:"node-red:common.status.not-connected"});
|
|
|
|
// });
|
|
|
|
}
|
|
|
|
if (node.board.isReady) { doit(); }
|
|
|
|
else { node.board.on("ready", function() { doit(); }); }
|
|
|
|
node.on("close", function() {
|
|
|
|
if (node.tout) { clearTimeout(node.tout); }
|
|
|
|
})
|
2015-06-06 19:13:53 +02:00
|
|
|
}
|
|
|
|
else {
|
2015-06-16 11:36:19 +02:00
|
|
|
this.warn(RED._("arduino.errors.portnotconf"));
|
2015-06-06 19:13:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("arduino out",DuinoNodeOut);
|
|
|
|
|
|
|
|
RED.httpAdmin.get("/arduinoports", RED.auth.needsPermission("arduino.read"), function(req,res) {
|
2016-07-13 15:19:09 +02:00
|
|
|
SP.list(function(error, ports) {
|
2015-06-06 19:13:53 +02:00
|
|
|
res.json(ports);
|
|
|
|
});
|
2016-07-13 15:19:09 +02:00
|
|
|
|
2015-06-06 19:13:53 +02:00
|
|
|
});
|
|
|
|
}
|