mirror of
https://github.com/node-red/node-red-nodes.git
synced 2023-10-10 13:36:58 +02:00
Move Arduino nodes to use firmata nom
rather than arduino-firmata
This commit is contained in:
parent
0b094cbfc7
commit
0bfeb13abc
@ -167,7 +167,7 @@
|
|||||||
$("#node-config-lookup-serial").removeClass('disabled');
|
$("#node-config-lookup-serial").removeClass('disabled');
|
||||||
var ports = [];
|
var ports = [];
|
||||||
$.each(data, function(i, port) {
|
$.each(data, function(i, port) {
|
||||||
ports.push(port);
|
ports.push(port.comName);
|
||||||
});
|
});
|
||||||
$("#node-config-input-device").autocomplete({
|
$("#node-config-input-device").autocomplete({
|
||||||
source:ports,
|
source:ports,
|
||||||
|
@ -16,44 +16,38 @@
|
|||||||
|
|
||||||
module.exports = function(RED) {
|
module.exports = function(RED) {
|
||||||
"use strict";
|
"use strict";
|
||||||
var ArduinoFirmata = require('arduino-firmata');
|
|
||||||
|
var Board = require('firmata');
|
||||||
|
var SP = require('firmata/node_modules/serialport');
|
||||||
|
|
||||||
// The Board Definition - this opens (and closes) the connection
|
// The Board Definition - this opens (and closes) the connection
|
||||||
function ArduinoNode(n) {
|
function ArduinoNode(n) {
|
||||||
RED.nodes.createNode(this,n);
|
RED.nodes.createNode(this,n);
|
||||||
this.device = n.device || null;
|
this.device = n.device || null;
|
||||||
this.repeat = n.repeat||25;
|
|
||||||
var node = this;
|
var node = this;
|
||||||
node.board = new ArduinoFirmata();
|
|
||||||
// TODO: nls
|
|
||||||
ArduinoFirmata.list(function (err, ports) {
|
|
||||||
if (!node.device) {
|
|
||||||
node.log(RED._("arduino.status.connectfirst"));
|
|
||||||
node.board.connect();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (ports.indexOf(node.device) === -1) {
|
|
||||||
node.warn(RED._("arduino.errors.devnotfound",{dev:node.device}));
|
|
||||||
node.board.connect();
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
node.log(RED._("arduino.status.connect",{device:node.device}));
|
|
||||||
node.board.connect(node.device);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
node.board.on('boardReady', function() {
|
node.board = Board(node.device, function(e) {
|
||||||
node.log(RED._("arduino.status.connected",{device:node.board.serialport_name}));
|
//console.log("ERR",e);
|
||||||
if (RED.settings.verbose) {
|
if ((e !== undefined) && (e.toString().indexOf("cannot open") !== -1) ) {
|
||||||
node.log(RED._("arduino.status.version",{version:node.board.boardVersion}));
|
node.error(RED._("arduino.errors.portnotfound",{device:node.device}));
|
||||||
}
|
}
|
||||||
|
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}));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
node.board.on('close', function() {
|
||||||
|
node.error(RED._("arduino.status.portclosed"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
node.on('close', function(done) {
|
node.on('close', function(done) {
|
||||||
if (node.board) {
|
if (node.board) {
|
||||||
try {
|
try {
|
||||||
node.board.close(function() {
|
node.board.sp.close(function() {
|
||||||
done();
|
done();
|
||||||
if (RED.settings.verbose) { node.log(RED._("arduino.status.portclosed")); }
|
if (RED.settings.verbose) { node.log(RED._("arduino.status.portclosed")); }
|
||||||
});
|
});
|
||||||
@ -76,39 +70,28 @@ module.exports = function(RED) {
|
|||||||
this.board = this.serverConfig.board;
|
this.board = this.serverConfig.board;
|
||||||
var node = this;
|
var node = this;
|
||||||
node.status({fill:"red",shape:"ring",text:"node-red:common.status.connecting"});
|
node.status({fill:"red",shape:"ring",text:"node-red:common.status.connecting"});
|
||||||
node.board.on('connect', function() {
|
node.board.on('ready', function() {
|
||||||
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
||||||
//console.log("i",node.state,node.pin);
|
if (node.state === "ANALOG") {
|
||||||
if (node.state == "ANALOG") {
|
node.board.pinMode(node.pin, 0x02);
|
||||||
node.board.on('analogChange', function(e) {
|
node.board.analogRead(node.pin, function(v) {
|
||||||
if (e.pin == node.pin) {
|
node.send({payload:v, topic:"A"+node.pin});
|
||||||
var msg = {payload:e.value, topic:"A"+e.pin};
|
|
||||||
node.send(msg);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (node.state == "INPUT") {
|
if (node.state === "INPUT") {
|
||||||
node.board.pinMode(node.pin, ArduinoFirmata.INPUT);
|
node.board.pinMode(node.pin, 0x00);
|
||||||
node.board.on('digitalChange', function(e) {
|
node.board.digitalRead(node.pin, function(v) {
|
||||||
if (e.pin == node.pin) {
|
node.send({payload:v, topic:node.pin});
|
||||||
var msg = {payload:e.value, topic:e.pin};
|
|
||||||
node.send(msg);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
if (node.state == "SYSEX") {
|
|
||||||
node.board.on('sysex', function(e) {
|
|
||||||
var msg = {payload:e, topic:"sysex"};
|
|
||||||
node.send(msg);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (node.state == "STRING") {
|
if (node.state == "STRING") {
|
||||||
node.board.on('sysex', function(e) {
|
node.board.on('string', function(v) {
|
||||||
var string = new Buffer(e.data.slice(0, -1)).toString("utf8").replace(/\0/g, "");
|
node.send({payload:v, topic:"string"});
|
||||||
var msg = {payload:string, topic:"string"};
|
|
||||||
node.send(msg);
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
node.board.on('close', function() {
|
||||||
|
node.status({fill:"grey",shape:"ring",text:"node-red:common.status.not-connected"});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -131,48 +114,42 @@ module.exports = function(RED) {
|
|||||||
var node = this;
|
var node = this;
|
||||||
node.status({fill:"red",shape:"ring",text:"node-red:common.status.connecting"});
|
node.status({fill:"red",shape:"ring",text:"node-red:common.status.connecting"});
|
||||||
|
|
||||||
node.board.on('connect', function() {
|
node.board.on('ready', function() {
|
||||||
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
node.status({fill:"green",shape:"dot",text:"node-red:common.status.connected"});
|
||||||
//console.log("o",node.state,node.pin);
|
|
||||||
|
|
||||||
node.on("input", function(msg) {
|
node.on("input", function(msg) {
|
||||||
if (node.state === "OUTPUT") {
|
if (node.state === "OUTPUT") {
|
||||||
node.board.pinMode(node.pin, node.state);
|
node.board.pinMode(node.pin, 0x01);
|
||||||
if ((msg.payload === true)||(msg.payload.toString() == "1")||(msg.payload.toString().toLowerCase() == "on")) {
|
if ((msg.payload === true)||(msg.payload.toString() == "1")||(msg.payload.toString().toLowerCase() == "on")) {
|
||||||
node.board.digitalWrite(node.pin, true);
|
node.board.digitalWrite(node.pin, node.board.HIGH);
|
||||||
}
|
}
|
||||||
if ((msg.payload === false)||(msg.payload.toString() == "0")||(msg.payload.toString().toLowerCase() == "off")) {
|
if ((msg.payload === false)||(msg.payload.toString() == "0")||(msg.payload.toString().toLowerCase() == "off")) {
|
||||||
node.board.digitalWrite(node.pin, false);
|
node.board.digitalWrite(node.pin, node.board.LOW);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (node.state === "PWM") {
|
if (node.state === "PWM") {
|
||||||
node.board.pinMode(node.pin, node.state);
|
node.board.pinMode(node.pin, 0x03);
|
||||||
msg.payload = parseInt((msg.payload * 1) + 0.5);
|
msg.payload = parseInt((msg.payload * 1) + 0.5);
|
||||||
if ((msg.payload >= 0) && (msg.payload <= 255)) {
|
if ((msg.payload >= 0) && (msg.payload <= 255)) {
|
||||||
node.board.analogWrite(node.pin, msg.payload);
|
node.board.analogWrite(node.pin, msg.payload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (node.state === "SERVO") {
|
if (node.state === "SERVO") {
|
||||||
node.board.pinMode(node.pin, node.state);
|
node.board.pinMode(node.pin, 0x04);
|
||||||
msg.payload = parseInt((msg.payload * 1) + 0.5);
|
msg.payload = parseInt((msg.payload * 1) + 0.5);
|
||||||
if ((msg.payload >= 0) && (msg.payload <= 180)) {
|
if ((msg.payload >= 0) && (msg.payload <= 180)) {
|
||||||
node.board.servoWrite(node.pin, msg.payload);
|
node.board.servoWrite(node.pin, msg.payload);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (node.state === "SYSEX") {
|
if (node.state === "SYSEX") {
|
||||||
node.board.sysex(msg.payload);
|
node.board.sysexCommand(msg.payload);
|
||||||
}
|
}
|
||||||
if (node.state === "STRING") {
|
if (node.state === "STRING") {
|
||||||
var bytes = new Buffer(msg.payload.toString(), "utf8");
|
node.board.sendString(msg.payload.toString());
|
||||||
var data = [];
|
|
||||||
for (var i = 0, length = bytes.length; i < length; i++) {
|
|
||||||
data.push(bytes[i] & 0x7F);
|
|
||||||
data.push((bytes[i] >> 7) & 0x7F);
|
|
||||||
}
|
|
||||||
data.push(0);
|
|
||||||
node.board.sysex(0x71, data);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
node.board.on('close', function() {
|
||||||
|
node.status({fill:"grey",shape:"ring",text:"node-red:common.status.not-connected"});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
@ -182,8 +159,9 @@ module.exports = function(RED) {
|
|||||||
RED.nodes.registerType("arduino out",DuinoNodeOut);
|
RED.nodes.registerType("arduino out",DuinoNodeOut);
|
||||||
|
|
||||||
RED.httpAdmin.get("/arduinoports", RED.auth.needsPermission("arduino.read"), function(req,res) {
|
RED.httpAdmin.get("/arduinoports", RED.auth.needsPermission("arduino.read"), function(req,res) {
|
||||||
ArduinoFirmata.list(function (err, ports) {
|
SP.list(function(error, ports) {
|
||||||
res.json(ports);
|
res.json(ports);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@
|
|||||||
"connect": "connecting to __device__",
|
"connect": "connecting to __device__",
|
||||||
"connected": "connected to __device__",
|
"connected": "connected to __device__",
|
||||||
"version": "version: __version__",
|
"version": "version: __version__",
|
||||||
"portclosed": "port closed"
|
"portclosed": "Arduino serial port closed"
|
||||||
},
|
},
|
||||||
"state": {
|
"state": {
|
||||||
"in": {
|
"in": {
|
||||||
@ -34,6 +34,7 @@
|
|||||||
},
|
},
|
||||||
"errors": {
|
"errors": {
|
||||||
"portnotconf": "port not configured",
|
"portnotconf": "port not configured",
|
||||||
|
"portnotfound": "port not found : __device__",
|
||||||
"devnotfound": "device __dev__ not found. Trying to find board."
|
"devnotfound": "device __dev__ not found. Trying to find board."
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name" : "node-red-node-arduino",
|
"name" : "node-red-node-arduino",
|
||||||
"version" : "0.0.7",
|
"version" : "0.0.8",
|
||||||
"description" : "A Node-RED node to talk to an Arduino running firmata",
|
"description" : "A Node-RED node to talk to an Arduino running firmata",
|
||||||
"dependencies" : {
|
"dependencies" : {
|
||||||
"arduino-firmata" : "0.3.4"
|
"firmata" : "~0.13.0"
|
||||||
},
|
},
|
||||||
"repository" : {
|
"repository" : {
|
||||||
"type":"git",
|
"type":"git",
|
||||||
|
Loading…
Reference in New Issue
Block a user