Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions avrgirl-arduino.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var protocols = require('./lib/protocols');
*
* @param {object} opts - options for consumer to pass in
*/
var AvrgirlArduino = function(opts) {
var AvrgirlArduino = function (opts) {
opts = opts || {};

this.options = {
Expand All @@ -16,14 +16,14 @@ var AvrgirlArduino = function(opts) {
port: opts.port || ''
};

this.debug = this.options.debug ? console.log.bind(console) : function() {};
this.debug = this.options.debug ? console.log.bind(console) : function () {};

this.connection = new Connection(this.options);

this.board = boards.byName[this.options.board];

if (this.board) {
var Protocol = protocols[this.board.protocol] || function() {};
var Protocol = protocols[this.board.protocol] || function () {};

this.protocol = new Protocol({
board: this.board,
Expand All @@ -38,7 +38,7 @@ var AvrgirlArduino = function(opts) {
*
* @param {function} callback - function to run upon completion/error
*/
AvrgirlArduino.prototype._validateBoard = function(callback) {
AvrgirlArduino.prototype._validateBoard = function (callback) {
if (!this.board) {
// cannot find a matching board in supported list
return callback(new Error('"' + this.options.board + '" is not a supported board type.'));
Expand All @@ -63,15 +63,15 @@ AvrgirlArduino.prototype._validateBoard = function(callback) {
* @param {string} file - path to hex file for uploading
* @param {function} callback - function to run upon completion/error
*/
AvrgirlArduino.prototype.flash = function(file, callback) {
AvrgirlArduino.prototype.flash = function (file, callback) {
var _this = this;

// validate board properties first
_this._validateBoard(function(error) {
_this._validateBoard(function (error) {
if (error) { return callback(error); }

// set up serialport connection
_this.connection._init(function(error) {
_this.connection._init(function (error) {
if (error) { return callback(error); }

// upload file to board
Expand All @@ -87,7 +87,7 @@ AvrgirlArduino.prototype.flash = function(file, callback) {
* @param {function} callback - function to run upon completion/error
*/
AvrgirlArduino.prototype.listPorts = AvrgirlArduino.listPorts =
AvrgirlArduino.prototype.list = AvrgirlArduino.list = function(callback) {
AvrgirlArduino.prototype.list = AvrgirlArduino.list = function (callback) {
return Connection.prototype._listPorts(callback);
};

Expand Down
24 changes: 12 additions & 12 deletions lib/avr109.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ var Protocol = require('./protocol');
var util = require('util');
var os = require('os');

var Avr109 = function(options) {
options.protocol = function() { return AVR109; };
var Avr109 = function (options) {
options.protocol = function () { return AVR109; };

Protocol.call(this, options);
};
Expand All @@ -21,7 +21,7 @@ util.inherits(Avr109, Protocol);
* @param {string} hex - path of hex file for uploading
* @param {function} callback - function to run upon completion/error
*/
Avr109.prototype._upload = function(file, callback) {
Avr109.prototype._upload = function (file, callback) {
var _this = this;
var data;

Expand All @@ -33,17 +33,17 @@ Avr109.prototype._upload = function(file, callback) {
return callback(error);
}

_this._reset(function(error) {
_this._reset(function (error) {
if (error) { return callback(error); }

_this.debug('reset complete.');

_this.connection.serialPort.open(function(error) {
_this.connection.serialPort.open(function (error) {
if (error) { return callback(error); }

_this.debug('connected');

_this._write(data, function(error) {
_this._write(data, function (error) {
var color = (error ? colors.red : colors.green);
_this.debug(color('flash complete.'));

Expand All @@ -62,15 +62,15 @@ Avr109.prototype._upload = function(file, callback) {
* @param {buffer} data - hex buffer to write to the chip
* @param {function} callback - function to run upon completion/error
*/
Avr109.prototype._write = function(data, callback) {
Avr109.prototype._write = function (data, callback) {
var _this = this;

var options = {
signature: _this.board.signature.toString(),
debug: false
};

_this.chip.init(_this.connection.serialPort, options, function(error, flasher) {
_this.chip.init(_this.connection.serialPort, options, function (error, flasher) {
if (error) { return callback(error); }

_this.debug('flashing, please wait...');
Expand All @@ -89,7 +89,7 @@ Avr109.prototype._write = function(data, callback) {

flasher.fuseCheck.bind(flasher)
],
function(error) {
function (error) {
return callback(error);
});
});
Expand All @@ -100,7 +100,7 @@ Avr109.prototype._write = function(data, callback) {
*
* @param {function} callback - function to run upon completion/error
*/
Avr109.prototype._reset = function(callback) {
Avr109.prototype._reset = function (callback) {
var _this = this;
var delay = 500;
var conn;
Expand All @@ -121,9 +121,9 @@ Avr109.prototype._reset = function(callback) {
conn._setUpSerial.bind(conn),
conn._pollForPort.bind(conn)
],
function(error) {
function (error) {
// some leos are just plain tardy
setTimeout(function() {
setTimeout(function () {
return callback(error);
}, delay);
});
Expand Down
42 changes: 21 additions & 21 deletions lib/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,29 @@ var Serialport = require('serialport');
var async = require('async');
var boards = require('../boards.js');

var Connection = function(options) {
var Connection = function (options) {
this.options = options;
this.debug = this.options.debug ? console.log.bind(console) : function() {};
this.debug = this.options.debug ? console.log.bind(console) : function () {};

// get board properties
this.board = boards.byName[this.options.board];
};

Connection.prototype._init = function(callback) {
Connection.prototype._init = function (callback) {
var _this = this;

// check for port
if (!_this.options.port) {
// no port, auto sniff for the correct one
_this._sniffPort(function(error, port) {
_this._sniffPort(function (error, port) {
if (port.length) {
// found a port, save it
_this.options.port = port[0].comName;

_this.debug('found ' + _this.options.board + ' on port ' + _this.options.port);

// set up serialport for it
_this._setUpSerial(function(error) {
_this._setUpSerial(function (error) {
return callback(error);
});
} else {
Expand All @@ -35,7 +35,7 @@ Connection.prototype._init = function(callback) {

} else {
// when a port is manually specified
_this._setUpSerial(function(error) {
_this._setUpSerial(function (error) {
return callback(error);
});
}
Expand All @@ -44,7 +44,7 @@ Connection.prototype._init = function(callback) {
/**
* Create new serialport instance for the Arduino board, but do not immediately connect.
*/
Connection.prototype._setUpSerial = function(callback) {
Connection.prototype._setUpSerial = function (callback) {
this.serialPort = new Serialport.SerialPort(this.options.port, {
baudRate: this.board.baud,
}, false);
Expand All @@ -57,13 +57,13 @@ Connection.prototype._setUpSerial = function(callback) {
*
* @param {function} callback - function to run upon completion/error
*/
Connection.prototype._sniffPort = function(callback) {
Connection.prototype._sniffPort = function (callback) {
var _this = this;
var pidList = _this.board.productId;

_this._listPorts(function(error, ports) {
_this._listPorts(function (error, ports) {
// filter for a match by product id
var portMatch = ports.filter(function(p) {
var portMatch = ports.filter(function (p) {
return pidList.indexOf(p._standardPid) !== -1;
});

Expand All @@ -78,17 +78,17 @@ Connection.prototype._sniffPort = function(callback) {
* @param {number} timeout - number in milliseconds to delay after
* @param {function} callback - function to run upon completion/error
*/
Connection.prototype._setDTR = function(bool, timeout, callback) {
Connection.prototype._setDTR = function (bool, timeout, callback) {
var _this = this;
var props = {
rts: bool,
dtr: bool
};

_this.serialPort.set(props, function(error) {
_this.serialPort.set(props, function (error) {
if (error) { return callback(error); }

setTimeout(function() {
setTimeout(function () {
callback(error);
}, timeout);
});
Expand All @@ -99,13 +99,13 @@ Connection.prototype._setDTR = function(bool, timeout, callback) {
*
* @param {function} callback - function to run upon completion/error
*/
Connection.prototype._pollForPort = function(callback) {
Connection.prototype._pollForPort = function (callback) {
var _this = this;
var tries = 0;
var delay = 300;

function checkList() {
Serialport.list(function(error, ports) {
Serialport.list(function (error, ports) {
// iterate through ports looking for the one port to rule them all
for (var i = 0; i < ports.length; i++) {
if (ports[i].comName === _this.options.port) {
Expand All @@ -118,13 +118,13 @@ Connection.prototype._pollForPort = function(callback) {
setTimeout(checkList, delay);
} else {
// try to sniff port instead (for port hopping devices) {
_this._sniffPort(function(error, port) {
_this._sniffPort(function (error, port) {
if (port.length) {
// found a port, save it
_this.options.port = port[0].comName;

// set up serialport for it
_this._setUpSerial(function(error) {
_this._setUpSerial(function (error) {
return callback(error);
});
} else {
Expand All @@ -144,14 +144,14 @@ Connection.prototype._pollForPort = function(callback) {
*
* @param {function} callback - function to run upon completion/error
*/
Connection.prototype._cycleDTR = function(callback) {
Connection.prototype._cycleDTR = function (callback) {
var _this = this;

async.series([
_this._setDTR.bind(_this, true, 250),
_this._setDTR.bind(_this, false, 50)
],
function(error) {
function (error) {
return callback(error);
});
};
Expand All @@ -162,11 +162,11 @@ Connection.prototype._cycleDTR = function(callback) {
*
* @param {function} callback - function to run upon completion/error
*/
Connection.prototype._listPorts = function(callback) {
Connection.prototype._listPorts = function (callback) {
var foundPorts = [];

// list all available ports
Serialport.list(function(err, ports) {
Serialport.list(function (err, ports) {
if (err) { return callback(err); }

// iterate through ports
Expand Down
8 changes: 4 additions & 4 deletions lib/protocol.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* Generic Protocol for other protocols to inherit from
*
*/
var Protocol = function(options) {
this.debug = options.debug ? console.log.bind(console) : function() {};
var Protocol = function (options) {
this.debug = options.debug ? console.log.bind(console) : function () {};

this.board = options.board;
this.connection = options.connection;
Expand All @@ -19,11 +19,11 @@ var Protocol = function(options) {
*
* @param {function} callback - function to run upon completion/error
*/
Protocol.prototype._reset = function(callback) {
Protocol.prototype._reset = function (callback) {
var _this = this;

// cycle DTR/RTS from low to high
_this.connection._cycleDTR(function(error) {
_this.connection._cycleDTR(function (error) {
if (!error) {
_this.debug('reset complete.');
}
Expand Down
10 changes: 5 additions & 5 deletions lib/stk500v1.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ var tools = require('./tools');
var Protocol = require('./protocol');
var util = require('util');

var Stk500v1 = function(options) {
var Stk500v1 = function (options) {
options.protocol = STK;
Protocol.call(this, options);
};
Expand All @@ -17,7 +17,7 @@ util.inherits(Stk500v1, Protocol);
* @param {string} file - path to hex file for uploading
* @param {function} callback - function to run upon completion/error
*/
Stk500v1.prototype._upload = function(file, callback) {
Stk500v1.prototype._upload = function (file, callback) {
var _this = this;

this.serialPort = this.connection.serialPort;
Expand All @@ -29,19 +29,19 @@ Stk500v1.prototype._upload = function(file, callback) {
}

// open connection
_this.serialPort.open(function(error) {
_this.serialPort.open(function (error) {
if (error) { return callback(error); }

_this.debug('connected');

// reset
_this._reset(function(error) {
_this._reset(function (error) {
if (error) { return callback(error); }

_this.debug('flashing, please wait...');

// flash
_this.chip.bootload(_this.serialPort, hex, _this.board, function(error) {
_this.chip.bootload(_this.serialPort, hex, _this.board, function (error) {
var color = (error ? colors.red : colors.green);

_this.debug(color('flash complete.'));
Expand Down
Loading