Skip to content

Commit

Permalink
Board: emit a "connect" event when transport opens; set isReady = true
Browse files Browse the repository at this point in the history
  • Loading branch information
rwaldron committed Apr 27, 2015
1 parent 6c8458b commit 70dcea3
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 6 deletions.
28 changes: 28 additions & 0 deletions examples/johnny-five-io-plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
var SerialPort = require("serialport");
var five = require("johnny-five");
var Firmata = require("../");

SerialPort.list(function(error, list) {
var device = list.reduce(function(accum, item) {
if (item.manufacturer.indexOf("Arduino") === 0) {
return item;
}
return accum;
}, null);


/*
The following demonstrates using Firmata
as an IO Plugin for Johnny-Five
*/

var board = new five.Board({
io: new Firmata(device.comName)
});

board.on("ready", function() {
var led = new five.Led(13);
led.blink(500);
});
});

19 changes: 13 additions & 6 deletions lib/firmata.js
Original file line number Diff line number Diff line change
Expand Up @@ -383,12 +383,7 @@ var Board = function(port, options, callback) {

var settings = assign({}, defaults, options);

var ready = function() {
this.emit("ready");
if (typeof callback === "function") {
callback();
}
}.bind(this);
this.isReady = false;

this.MODES = {
INPUT: 0x00,
Expand Down Expand Up @@ -448,6 +443,10 @@ var Board = function(port, options, callback) {
// For backward compat
this.sp = this.transport;

this.transport.on("open", function() {
this.emit("connect");
}.bind(this));

this.transport.on("error", function(string) {
if (typeof callback === "function") {
callback(string);
Expand Down Expand Up @@ -525,6 +524,14 @@ var Board = function(port, options, callback) {
}
}.bind(this), settings.reportVersionTimeout);

function ready() {
board.isReady = true;
board.emit("ready");
if (typeof callback === "function") {
callback();
}
}

// Await the reported version.
this.once("reportversion", function() {
clearTimeout(this.reportVersionTimeoutId);
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"grunt-jsbeautifier": "~0.2.7",
"grunt-jscs": "~0.8.1",
"grunt-mocha-test": "~0.2.2",
"johnny-five": "^0.8.62",
"mocha": ">=0.13.x",
"rewire": "~2.1.2",
"should": ">=0.5.x",
Expand Down
53 changes: 53 additions & 0 deletions test/firmata.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,59 @@ describe("board", function() {
done();
});

it("emits 'connect' when the transport is opened.", function(done) {
var serialPort = new SerialPort("/path/to/fake/usb");
var board = new Board(serialPort, function(err) {});

board.on("connect", function() {
done();
});

serialPort.emit("open");
});

it("emits 'ready' after handshakes complete (skipCapabilities)", function(done) {
var serialPort = new SerialPort("/path/to/fake/usb");
var board = new Board(serialPort, {skipCapabilities: true}, function(err) {});
var connected = false;

board.on("connect", function() {
connected = true;
});

board.on("ready", function() {
should.equal(connected, true);
should.equal(this.isReady, true);
done();
});

serialPort.emit("open");
board.emit("reportversion");
board.emit("queryfirmware");
});

it("emits 'ready' after handshakes complete", function(done) {
var serialPort = new SerialPort("/path/to/fake/usb");
var board = new Board(serialPort, function(err) {});
var connected = false;

board.on("connect", function() {
connected = true;
});

board.on("ready", function() {
should.equal(connected, true);
should.equal(this.isReady, true);
done();
});

serialPort.emit("open");
board.emit("reportversion");
board.emit("queryfirmware");
board.emit("capability-query");
board.emit("analog-mapping-query");
});

it("reports errors", function(done) {
var serialPort = new SerialPort("/path/to/fake/usb");
var board = new Board(serialPort, function(err) {
Expand Down

0 comments on commit 70dcea3

Please sign in to comment.