Permalink
Browse files

Merge pull request #9 from lwille/master

throw is evil
  • Loading branch information...
ecto committed Apr 24, 2012
2 parents e19c968 + d44af87 commit 299f1b40425daa4fa4deb929a696122a4658c595
Showing with 39 additions and 33 deletions.
  1. +39 −33 lib/board.js
View
@@ -16,39 +16,45 @@ var Board = function (options) {
var self = this;
this.detect(function (err, serial) {
- if (err) throw err;
- self.serial = serial;
- self.emit('connected');
-
- self.log('info', 'binding serial events');
- self.serial.on('data', function(data){
- self.log('receive', data.toString().red);
- self.emit('data', data);
- });
-
- setTimeout(function(){
- self.log('info', 'board ready');
- self.sendClearingBytes();
-
- if (self.debug) {
- self.log('info', 'sending debug mode toggle on to board');
- self.write('99' + self.normalizePin(0) + self.normalizeVal(1));
- process.on('SIGINT', function(){
- self.log('info', 'sending debug mode toggle off to board');
- self.write('99' + self.normalizePin(0) + self.normalizeVal(0));
- delete self.serial;
- setTimeout(function(){
- process.exit();
- }, 100);
- });
- }
-
- if (self.writeBuffer.length > 0) {
- self.processWriteBuffer();
- }
-
- self.emit('ready');
- }, 500);
+ if (err) {
+ if(self.listeners('error').length)
+ self.emit('error', err);
+ else
+ throw new Error(err);
+ }else{
+ self.serial = serial;
+ self.emit('connected');
+
+ self.log('info', 'binding serial events');
+ self.serial.on('data', function(data){
+ self.log('receive', data.toString().red);
+ self.emit('data', data);
+ });
+
+ setTimeout(function(){
+ self.log('info', 'board ready');
+ self.sendClearingBytes();
+
+ if (self.debug) {
+ self.log('info', 'sending debug mode toggle on to board');
+ self.write('99' + self.normalizePin(0) + self.normalizeVal(1));
+ process.on('SIGINT', function(){
+ self.log('info', 'sending debug mode toggle off to board');
+ self.write('99' + self.normalizePin(0) + self.normalizeVal(0));
+ delete self.serial;
+ setTimeout(function(){
+ process.exit();
+ }, 100);
+ });
+ }
+
+ if (self.writeBuffer.length > 0) {
+ self.processWriteBuffer();
+ }
+
+ self.emit('ready');
+ }, 500);
+ }
});
}

0 comments on commit 299f1b4

Please sign in to comment.