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;
|
2019-04-24 15:07:01 +02:00
|
|
|
this.running = false;
|
|
|
|
this.reported = false;
|
2015-06-06 19:13:53 +02:00
|
|
|
var node = this;
|
2016-07-13 15:19:09 +02:00
|
|
|
|
2019-04-10 10:57:16 +02:00
|
|
|
var startup = function() {
|
|
|
|
node.board = new Board(node.device, function(e) {
|
|
|
|
if ((e !== undefined) && (e.toString().indexOf("cannot open") !== -1) ) {
|
2019-04-24 15:07:01 +02:00
|
|
|
if (node.reported === false) {
|
2019-04-11 17:35:02 +02:00
|
|
|
node.error(RED._("arduino.errors.portnotfound",{device:node.device}));
|
2019-04-24 15:07:01 +02:00
|
|
|
node.reported = true;
|
2019-04-11 17:35:02 +02:00
|
|
|
}
|
2019-04-10 10:57:16 +02:00
|
|
|
}
|
|
|
|
else if (e === undefined) {
|
2019-04-24 15:07:01 +02:00
|
|
|
node.running = true;
|
|
|
|
node.reported = false;
|
2019-04-10 10:57:16 +02:00
|
|
|
node.board.once('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}));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
node.board.once('close', function() {
|
|
|
|
node.error(RED._("arduino.status.portclosed"));
|
|
|
|
});
|
|
|
|
node.board.once('disconnect', function() {
|
2019-04-24 15:07:01 +02:00
|
|
|
if (node.running === true) { setTimeout(function() { node.running = false; startup(); }, 5000); }
|
2019-04-10 10:57:16 +02:00
|
|
|
});
|
|
|
|
}
|
2015-06-06 19:13:53 +02:00
|
|
|
});
|
2019-04-24 15:07:01 +02:00
|
|
|
setTimeout(function() { if (node.running === false) { startup(); } }, 5000);
|
2019-04-10 10:57:16 +02:00
|
|
|
};
|
|
|
|
startup();
|
2015-06-06 19:13:53 +02:00
|
|
|
|
|
|
|
node.on('close', function(done) {
|
2019-04-24 15:07:01 +02:00
|
|
|
node.running = false;
|
2015-06-06 19:13:53 +02:00
|
|
|
if (node.board) {
|
|
|
|
try {
|
2019-02-12 23:44:21 +01:00
|
|
|
node.board.transport.close(function() {
|
2015-06-16 11:36:19 +02:00
|
|
|
if (RED.settings.verbose) { node.log(RED._("arduino.status.portclosed")); }
|
2018-05-13 18:26:32 +02:00
|
|
|
done();
|
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);
|
2019-04-10 10:57:16 +02:00
|
|
|
this.running = false;
|
2019-04-15 18:28:55 +02:00
|
|
|
var node = this;
|
2015-06-06 19:13:53 +02:00
|
|
|
if (typeof this.serverConfig === "object") {
|
2019-04-10 10:57:16 +02:00
|
|
|
var startup = function() {
|
2019-04-15 18:28:55 +02:00
|
|
|
node.board = node.serverConfig.board;
|
2019-12-03 22:38:33 +01:00
|
|
|
node.board.setMaxListeners(0);
|
2019-04-10 10:57:16 +02:00
|
|
|
node.oldval = "";
|
|
|
|
node.status({fill:"grey",shape:"ring",text:"node-red:common.status.connecting"});
|
|
|
|
var doit = function() {
|
|
|
|
node.running = true;
|
2019-10-22 23:05:49 +02:00
|
|
|
if (node.state === "ANALOG") { node.board.pinMode(node.pin, 0x02); }
|
|
|
|
if (node.state === "INPUT") { node.board.pinMode(node.pin, 0x00); }
|
|
|
|
if (node.state === "PULLUP") { node.board.pinMode(node.pin, 0x0B); }
|
2019-04-10 10:57:16 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
|
|
|
if (node.state === "ANALOG") {
|
|
|
|
node.board.analogRead(node.pin, function(v) {
|
|
|
|
if (v !== node.oldval) {
|
|
|
|
node.oldval = v;
|
|
|
|
node.send({payload:v, topic:"A"+node.pin});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (node.state === "INPUT") {
|
|
|
|
node.board.digitalRead(node.pin, function(v) {
|
|
|
|
if (v !== node.oldval) {
|
|
|
|
node.oldval = v;
|
|
|
|
node.send({payload:v, topic:node.pin});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (node.state === "PULLUP") {
|
|
|
|
node.board.digitalRead(node.pin, function(v) {
|
|
|
|
if (v !== node.oldval) {
|
|
|
|
node.oldval = v;
|
|
|
|
node.send({payload:v, topic:node.pin});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (node.state == "STRING") {
|
|
|
|
node.board.on('string', function(v) {
|
|
|
|
if (v !== node.oldval) {
|
|
|
|
node.oldval = v;
|
|
|
|
node.send({payload:v, topic:"string"});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
node.board.once('disconnect', function() {
|
|
|
|
node.status({fill:"red",shape:"ring",text:"node-red:common.status.not-connected"});
|
|
|
|
if (node.running) { setTimeout(function() { node.running = false; startup(); }, 5500); }
|
2016-03-02 14:44:33 +01:00
|
|
|
});
|
|
|
|
}
|
2019-04-10 10:57:16 +02:00
|
|
|
if (node.board.isReady) { doit(); }
|
|
|
|
else { node.board.once("ready", function() { doit(); }); }
|
2019-04-15 18:28:55 +02:00
|
|
|
setTimeout(function() { if (node.running === false) { startup(); } }, 4500);
|
2016-07-15 15:45:32 +02:00
|
|
|
}
|
2019-04-10 10:57:16 +02:00
|
|
|
startup();
|
2015-06-06 19:13:53 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-04-15 18:28:55 +02:00
|
|
|
node.warn(RED._("arduino.errors.portnotconf"));
|
2015-06-06 19:13:53 +02:00
|
|
|
}
|
2019-04-10 10:57:16 +02:00
|
|
|
node.on('close', function() {
|
|
|
|
node.running = false;
|
|
|
|
});
|
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);
|
2019-04-10 10:57:16 +02:00
|
|
|
this.running = false;
|
|
|
|
var node = this;
|
|
|
|
if (typeof node.serverConfig === "object") {
|
|
|
|
var startup = function() {
|
|
|
|
node.board = node.serverConfig.board;
|
2019-12-03 22:38:33 +01:00
|
|
|
node.board.setMaxListeners(0);
|
2019-04-10 10:57:16 +02:00
|
|
|
node.status({fill:"grey",shape:"ring",text:"node-red:common.status.connecting"});
|
|
|
|
var doit = function() {
|
|
|
|
node.running = true;
|
2019-10-22 23:05:49 +02:00
|
|
|
if (node.state === "OUTPUT") { node.board.pinMode(node.pin, 0x01); }
|
|
|
|
if (node.state === "PWM") { node.board.pinMode(node.pin, 0x03); }
|
|
|
|
if (node.state === "SERVO") { node.board.pinMode(node.pin, 0x04); }
|
2019-04-10 10:57:16 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
|
|
|
node.on("input", function(msg) {
|
|
|
|
if (node.board.isReady) {
|
|
|
|
if (node.state === "OUTPUT") {
|
|
|
|
if ((msg.payload === true)||(msg.payload.toString() == "1")||(msg.payload.toString().toLowerCase() == "on")) {
|
|
|
|
node.board.digitalWrite(node.pin, node.board.HIGH);
|
|
|
|
}
|
|
|
|
if ((msg.payload === false)||(msg.payload.toString() == "0")||(msg.payload.toString().toLowerCase() == "off")) {
|
|
|
|
node.board.digitalWrite(node.pin, node.board.LOW);
|
|
|
|
}
|
|
|
|
}
|
2019-10-22 23:05:49 +02:00
|
|
|
if (node.state === "PWM") {
|
2019-04-10 10:57:16 +02:00
|
|
|
msg.payload = parseInt((msg.payload * 1) + 0.5);
|
|
|
|
if ((msg.payload >= 0) && (msg.payload <= 255)) {
|
|
|
|
node.board.analogWrite(node.pin, msg.payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node.state === "SERVO") {
|
|
|
|
msg.payload = parseInt((msg.payload * 1) + 0.5);
|
|
|
|
if ((msg.payload >= 0) && (msg.payload <= 180)) {
|
|
|
|
node.board.servoWrite(node.pin, msg.payload);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node.state === "SYSEX") {
|
|
|
|
node.board.sysexCommand(msg.payload);
|
|
|
|
}
|
|
|
|
if (node.state === "STRING") {
|
2019-08-11 14:37:15 +02:00
|
|
|
node.board.sendString(msg.payload.toString());
|
|
|
|
}
|
2015-06-06 19:13:53 +02:00
|
|
|
}
|
2019-04-10 10:57:16 +02:00
|
|
|
});
|
|
|
|
node.board.once('disconnect', function() {
|
|
|
|
node.status({fill:"red",shape:"ring",text:"node-red:common.status.not-connected"});
|
2019-04-24 15:07:01 +02:00
|
|
|
if (node.running === true) { setTimeout(function() { node.running = false; startup(); }, 5500); }
|
2019-04-10 10:57:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
if (node.board.isReady) { doit(); }
|
|
|
|
else { node.board.once("ready", function() { doit(); }); }
|
2019-04-24 15:07:01 +02:00
|
|
|
setTimeout(function() { if (node.running === false) { startup(); } }, 4500);
|
2016-07-15 15:45:32 +02:00
|
|
|
}
|
2019-04-10 10:57:16 +02:00
|
|
|
startup();
|
2015-06-06 19:13:53 +02:00
|
|
|
}
|
|
|
|
else {
|
2019-04-10 10:57:16 +02:00
|
|
|
node.warn(RED._("arduino.errors.portnotconf"));
|
2015-06-06 19:13:53 +02:00
|
|
|
}
|
2019-04-10 10:57:16 +02:00
|
|
|
node.on('close', function() {
|
|
|
|
node.running = false;
|
|
|
|
});
|
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) {
|
2019-11-13 22:52:01 +01:00
|
|
|
SP.list().then(
|
|
|
|
ports => {
|
|
|
|
const a = ports.map(p => p.comName);
|
|
|
|
res.json(a);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
this.log('Error listing serial ports', err)
|
|
|
|
}
|
|
|
|
)
|
2015-06-06 19:13:53 +02:00
|
|
|
});
|
|
|
|
}
|