1
0
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:
Dave Conway-Jones 2016-07-13 14:19:09 +01:00
parent 0b094cbfc7
commit 0bfeb13abc
4 changed files with 58 additions and 79 deletions

View File

@ -107,7 +107,7 @@
</script>
<script type="text/javascript">
RED.nodes.registerType('arduino out',{
RED.nodes.registerType('arduino out', {
category: 'Arduino',
color:"#3fadb5",
defaults: {
@ -167,7 +167,7 @@
$("#node-config-lookup-serial").removeClass('disabled');
var ports = [];
$.each(data, function(i, port) {
ports.push(port);
ports.push(port.comName);
});
$("#node-config-input-device").autocomplete({
source:ports,

View File

@ -16,44 +16,38 @@
module.exports = function(RED) {
"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
function ArduinoNode(n) {
RED.nodes.createNode(this,n);
this.device = n.device || null;
this.repeat = n.repeat||25;
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.log(RED._("arduino.status.connected",{device:node.board.serialport_name}));
if (RED.settings.verbose) {
node.log(RED._("arduino.status.version",{version:node.board.boardVersion}));
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}));
}
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) {
if (node.board) {
try {
node.board.close(function() {
node.board.sp.close(function() {
done();
if (RED.settings.verbose) { node.log(RED._("arduino.status.portclosed")); }
});
@ -76,39 +70,28 @@ module.exports = function(RED) {
this.board = this.serverConfig.board;
var node = this;
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"});
//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};
node.send(msg);
}
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});
});
}
if (node.state == "INPUT") {
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);
}
});
}
if (node.state == "SYSEX") {
node.board.on('sysex', function(e) {
var msg = {payload:e, topic:"sysex"};
node.send(msg);
if (node.state === "INPUT") {
node.board.pinMode(node.pin, 0x00);
node.board.digitalRead(node.pin, function(v) {
node.send({payload:v, topic:node.pin});
});
}
if (node.state == "STRING") {
node.board.on('sysex', function(e) {
var string = new Buffer(e.data.slice(0, -1)).toString("utf8").replace(/\0/g, "");
var msg = {payload:string, topic:"string"};
node.send(msg);
node.board.on('string', function(v) {
node.send({payload:v, topic:"string"});
});
}
node.board.on('close', function() {
node.status({fill:"grey",shape:"ring",text:"node-red:common.status.not-connected"});
});
});
}
else {
@ -131,48 +114,42 @@ module.exports = function(RED) {
var node = this;
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"});
//console.log("o",node.state,node.pin);
node.on("input", function(msg) {
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")) {
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")) {
node.board.digitalWrite(node.pin, false);
node.board.digitalWrite(node.pin, node.board.LOW);
}
}
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);
if ((msg.payload >= 0) && (msg.payload <= 255)) {
node.board.analogWrite(node.pin, msg.payload);
}
}
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);
if ((msg.payload >= 0) && (msg.payload <= 180)) {
node.board.servoWrite(node.pin, msg.payload);
}
}
if (node.state === "SYSEX") {
node.board.sysex(msg.payload);
node.board.sysexCommand(msg.payload);
}
if (node.state === "STRING") {
var bytes = new Buffer(msg.payload.toString(), "utf8");
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.sendString(msg.payload.toString());
}
});
node.board.on('close', function() {
node.status({fill:"grey",shape:"ring",text:"node-red:common.status.not-connected"});
});
});
}
else {
@ -182,8 +159,9 @@ module.exports = function(RED) {
RED.nodes.registerType("arduino out",DuinoNodeOut);
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);
});
});
}

View File

@ -13,7 +13,7 @@
"connect": "connecting to __device__",
"connected": "connected to __device__",
"version": "version: __version__",
"portclosed": "port closed"
"portclosed": "Arduino serial port closed"
},
"state": {
"in": {
@ -34,6 +34,7 @@
},
"errors": {
"portnotconf": "port not configured",
"portnotfound": "port not found : __device__",
"devnotfound": "device __dev__ not found. Trying to find board."
}
}

View File

@ -1,9 +1,9 @@
{
"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",
"dependencies" : {
"arduino-firmata" : "0.3.4"
"firmata" : "~0.13.0"
},
"repository" : {
"type":"git",