Permalink
Browse files

Board module improvements, use while() loop in place of for-in for mu…

…table array iteration

Signed-off-by: Rick Waldron waldron.rick@gmail.com <waldron.rick@gmail.com>
  • Loading branch information...
1 parent 5ec3afe commit e5f4149276149f50b0c37fe7528e48432a98883c @rwaldron committed Mar 19, 2012
Showing with 27 additions and 19 deletions.
  1. +27 −19 lib/board.js
View
@@ -62,30 +62,38 @@ util.inherits(Board, events.EventEmitter);
* Loop through all USB devices and try to connect
* This should really message the device and wait for a correct response
*/
-Board.prototype.detect = function (cb) {
+Board.prototype.detect = function (callback) {
this.log('info', 'attempting to find Arduino board');
var self = this;
child.exec('ls /dev | grep usb', function(err, stdout, stderr){
- var possible = stdout.slice(0, -1).split('\n'),
- found = false;
- for (var i in possible) {
- var tempSerial, err;
- try {
- tempSerial = new serial.SerialPort('/dev/' + possible[i], {
- baudrate: 115200,
- parser: serial.parsers.readline('\n')
- });
- } catch (e) {
- err = e;
- }
- if (!err) {
- found = tempSerial;
- self.log('info', 'found board at ' + tempSerial.port);
- break;
+ var usb = stdout.slice(0, -1).split('\n'),
+ found = false,
+ err = null,
+ possible, temp;
+
+ while ( usb.length ) {
+ possible = usb.pop();
+
+ if (possible.slice(0, 2) !== 'cu') {
+ try {
+ temp = new serial.SerialPort('/dev/' + possible, {
+ baudrate: 115200,
+ parser: serial.parsers.readline('\n')
+ });
+ } catch (e) {
+ err = e;
+ }
+ if (!err) {
+ found = temp;
+ self.log('info', 'found board at ' + temp.port);
+ break;
+ } else {
+ err = new Error('Could not find Arduino');
+ }
}
}
- if (found) cb(null, found);
- else cb(new Error('Could not find Arduino'));
+
+ callback(err, found);
});
}

0 comments on commit e5f4149

Please sign in to comment.