2013-09-05 16:02:48 +02:00
|
|
|
/**
|
2014-05-23 16:35:55 +02:00
|
|
|
* Copyright 2013,2014 IBM Corp.
|
2013-09-05 16:02:48 +02:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
**/
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
module.exports = function(RED) {
|
2014-05-23 16:35:55 +02:00
|
|
|
"use strict";
|
|
|
|
var ArduinoFirmata = require('arduino-firmata');
|
2014-09-05 16:10:40 +02:00
|
|
|
var fs = require('fs');
|
|
|
|
var plat = require('os').platform();
|
2014-10-04 23:56:52 +02:00
|
|
|
var portlist = ArduinoFirmata.list(function (err, ports) {
|
|
|
|
portlist = ports;
|
|
|
|
});
|
2014-05-23 16:35:55 +02:00
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
// The Board Definition - this opens (and closes) the connection
|
|
|
|
function ArduinoNode(n) {
|
|
|
|
RED.nodes.createNode(this,n);
|
2014-05-23 16:35:55 +02:00
|
|
|
this.device = n.device || null;
|
2014-05-04 00:32:04 +02:00
|
|
|
this.repeat = n.repeat||25;
|
2014-05-23 16:35:55 +02:00
|
|
|
//node.log("opening connection "+this.device);
|
2013-10-16 20:04:32 +02:00
|
|
|
var node = this;
|
2014-05-23 16:35:55 +02:00
|
|
|
node.board = new ArduinoFirmata();
|
2014-10-04 23:56:52 +02:00
|
|
|
if (portlist.indexOf(node.device) === -1) {
|
2015-02-25 20:10:44 +01:00
|
|
|
node.warn("device "+node.device+" not found");
|
2014-09-05 16:10:40 +02:00
|
|
|
}
|
2014-10-04 23:56:52 +02:00
|
|
|
else {
|
2014-09-05 16:10:40 +02:00
|
|
|
node.board.connect(node.device);
|
|
|
|
}
|
2014-05-23 16:35:55 +02:00
|
|
|
|
2014-10-04 23:26:02 +02:00
|
|
|
node.board.on('boardReady', function(){
|
2015-02-25 20:10:44 +01:00
|
|
|
if (RED.settings.verbose) { node.log("version "+node.board.boardVersion); }
|
2014-10-04 23:26:02 +02:00
|
|
|
});
|
|
|
|
|
2015-01-29 22:42:33 +01:00
|
|
|
node.on('close', function(done) {
|
2014-05-23 16:35:55 +02:00
|
|
|
if (node.board) {
|
|
|
|
try {
|
|
|
|
node.board.close(function() {
|
2015-01-29 22:42:33 +01:00
|
|
|
done();
|
2015-02-25 20:10:44 +01:00
|
|
|
if (RED.settings.verbose) { node.log("port closed"); }
|
2013-10-16 20:04:32 +02:00
|
|
|
});
|
2015-01-29 22:42:33 +01:00
|
|
|
} catch(e) { done(); }
|
|
|
|
} else { done(); }
|
2013-10-16 20:04:32 +02:00
|
|
|
});
|
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.nodes.registerType("arduino-board",ArduinoNode);
|
2014-05-23 16:35:55 +02:00
|
|
|
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
// 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;
|
2014-05-23 16:35:55 +02:00
|
|
|
//this.repeat = this.serverConfig.repeat;
|
2014-05-04 00:32:04 +02:00
|
|
|
var node = this;
|
2014-05-30 21:30:26 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:"connecting"});
|
2014-05-23 16:35:55 +02:00
|
|
|
|
|
|
|
node.board.on('connect', function() {
|
2014-05-30 21:30:26 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
2014-05-23 16:35:55 +02:00
|
|
|
//console.log("i",node.state,node.pin);
|
|
|
|
if (node.state == "ANALOG") {
|
|
|
|
node.board.on('analogChange', function(e) {
|
|
|
|
if (e.pin == node.pin) {
|
|
|
|
var msg = {payload:e.value, topic:"A"+e.pin};
|
2014-05-04 00:32:04 +02:00
|
|
|
node.send(msg);
|
2014-05-23 16:35:55 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2013-10-16 20:04:32 +02:00
|
|
|
}
|
2014-05-23 16:35:55 +02:00
|
|
|
else {
|
|
|
|
node.board.pinMode(node.pin, ArduinoFirmata.INPUT);
|
|
|
|
node.board.on('digitalChange', function(e) {
|
|
|
|
if (e.pin == node.pin) {
|
|
|
|
var msg = {payload:e.value, topic:e.pin};
|
|
|
|
node.send(msg);
|
|
|
|
}
|
|
|
|
});
|
2013-10-16 20:04:32 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2015-02-25 20:10:44 +01:00
|
|
|
this.warn("port not configured");
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
RED.nodes.registerType("arduino in",DuinoNodeIn);
|
2014-05-23 16:35:55 +02:00
|
|
|
|
|
|
|
|
2014-05-04 00:32:04 +02:00
|
|
|
// 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;
|
2014-05-30 21:30:26 +02:00
|
|
|
node.status({fill:"red",shape:"ring",text:"connecting"});
|
2014-05-23 16:35:55 +02:00
|
|
|
|
|
|
|
node.board.on('connect', function() {
|
2014-05-30 21:30:26 +02:00
|
|
|
node.status({fill:"green",shape:"dot",text:"connected"});
|
2014-05-23 16:35:55 +02:00
|
|
|
//console.log("o",node.state,node.pin);
|
|
|
|
node.board.pinMode(node.pin, node.state);
|
|
|
|
node.on("input", function(msg) {
|
2014-05-04 00:32:04 +02:00
|
|
|
if (node.state == "OUTPUT") {
|
|
|
|
if ((msg.payload == true)||(msg.payload == 1)||(msg.payload.toString().toLowerCase() == "on")) {
|
2014-05-23 16:35:55 +02:00
|
|
|
node.board.digitalWrite(node.pin, true);
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
if ((msg.payload == false)||(msg.payload == 0)||(msg.payload.toString().toLowerCase() == "off")) {
|
2014-05-23 16:35:55 +02:00
|
|
|
node.board.digitalWrite(node.pin, false);
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (node.state == "PWM") {
|
|
|
|
msg.payload = msg.payload * 1;
|
|
|
|
if ((msg.payload >= 0) && (msg.payload <= 255)) {
|
|
|
|
//console.log(msg.payload, node.pin);
|
2014-11-25 20:42:17 +01:00
|
|
|
node.board.analogWrite(node.pin, msg.payload);
|
2014-05-04 00:32:04 +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-10-16 20:04:32 +02:00
|
|
|
}
|
2014-05-23 16:35:55 +02:00
|
|
|
});
|
2014-05-04 00:32:04 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
else {
|
2015-02-25 20:10:44 +01:00
|
|
|
this.warn("port not configured");
|
2014-05-04 00:32:04 +02:00
|
|
|
}
|
2013-10-16 20:04:32 +02:00
|
|
|
}
|
2014-05-04 00:32:04 +02:00
|
|
|
RED.nodes.registerType("arduino out",DuinoNodeOut);
|
2014-05-23 16:35:55 +02:00
|
|
|
|
2015-02-06 14:57:15 +01:00
|
|
|
RED.httpAdmin.get("/arduinoports", RED.auth.needsPermission("arduino.read"), function(req,res) {
|
2014-05-23 16:35:55 +02:00
|
|
|
ArduinoFirmata.list(function (err, ports) {
|
2015-02-06 14:57:15 +01:00
|
|
|
res.json(ports);
|
2014-05-23 16:35:55 +02:00
|
|
|
});
|
|
|
|
});
|
2013-09-05 16:02:48 +02:00
|
|
|
}
|