Skip to content

Commit

Permalink
Merge pull request #2 from caleboller/master
Browse files Browse the repository at this point in the history
SerialPort's open is asynchronous,treat it as such
  • Loading branch information
edgarsilva committed Dec 11, 2013
2 parents b1d7c28 + 7c39247 commit cda7067
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions lib/sphero.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,15 @@ module.exports = function() {
}
};

_sphero.open = function(device) {
_sphero.open = function(device, callback) {
_serialPort = new serialPort.SerialPort(device, {
parser:responseParser.spheroResponseParser()
});
}, true, function(err){
if (err){
if (callback && typeof(callback) == 'function'){
callback(err);
}
}});

_serialPort.on('open', function() {
_serialPort.on('data', function(packet) {
Expand All @@ -56,19 +61,23 @@ module.exports = function() {
}
});

_serialPort.on('close', function() {
_sphero.emit('close');
});
_serialPort.on('end', function() {
_sphero.emit('end');
});
_serialPort.on('error', function(error) {
_sphero.emit('error', error);
});
_serialPort.on('oob', function(packet) {
_sphero.emit('oob', packet);
});

_sphero.emit('open');
});
_serialPort.on('close', function() {
_sphero.emit('close');
});
_serialPort.on('end', function() {
_sphero.emit('end');
});
_serialPort.on('error', function(error) {
_sphero.emit('error', error);
});
_serialPort.on('oob', function(packet) {
_sphero.emit('oob', packet);
if (callback && typeof(callback) == 'function'){
callback(null);
}
});
return _sphero;
};
Expand Down

0 comments on commit cda7067

Please sign in to comment.