-
Notifications
You must be signed in to change notification settings - Fork 129
/
avrgirl-arduino.js
151 lines (126 loc) · 4.89 KB
/
avrgirl-arduino.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
var injectDependencies = function(boards, Connection, protocols) {
var EventEmitter = require('events');
var util = require('util');
var tools = require('./lib/tools');
/**
* Constructor
*
* @param {object} opts - options for consumer to pass in
*/
var AvrgirlArduino = function(opts) {
opts = opts || {};
this.options = {
debug: opts.debug || false,
megaDebug: opts.megaDebug || false,
board: opts.board || 'uno',
port: opts.port || '',
manualReset: opts.manualReset || false,
disableVerify: opts.disableVerify || false
};
// this here checks for 3 conditions:
// if debug option is simply true, we want to fall back to default debug function
// if a custom debug function is passed in, we want to assign debug to be that
// if debug option is false, then run debug as a no-op
if (this.options.debug === true) {
this.debug = this.options.debug = console.log.bind(console);
} else if (typeof this.options.debug === 'function') {
this.debug = this.options.debug = this.options.debug;
} else {
this.debug = this.options.debug = function debugNoop() {};
}
// handle 'sparse' boards, ie. boards with only the 'name' property defined
if (typeof this.options.board === 'object') {
const properties = Object.getOwnPropertyNames(this.options.board);
if ((properties.length === 1) && (properties[0] === 'name')) {
this.options.board = this.options.board.name;
}
}
if (typeof this.options.board === 'string') {
this.options.board = boards[this.options.board];
}
if (this.options.board && !this.options.board.manualReset) {
this.options.board.manualReset = this.options.manualReset;
}
if (this.options.board && !this.options.board.disableVerify) {
this.options.board.disableVerify = this.options.disableVerify;
}
this.connection = new Connection(this.options);
if (this.options.board) {
var Protocol = protocols[this.options.board.protocol] || function() {};
this.protocol = new Protocol({
board: this.options.board,
connection: this.connection,
debug: this.debug,
megaDebug: this.options.megaDebug
});
}
EventEmitter.call(this);
};
util.inherits(AvrgirlArduino, EventEmitter);
/**
* Validates the board properties
*
* @param {function} callback - function to run upon completion/error
*/
AvrgirlArduino.prototype._validateBoard = function(callback) {
if (typeof this.options.board !== 'object') {
// cannot find a matching board in supported list
return callback(new Error('"' + this.options.board + '" is not a supported board type.'));
} else if (!this.protocol.chip) {
// something went wrong trying to set up the protocol
var errorMsg = 'not a supported programming protocol: ' + this.options.board.protocol;
return callback(new Error(errorMsg));
} else if (!this.options.port && this.options.board.name === 'pro-mini') {
// when using a pro mini, a port is required in the options
return callback(new Error('using a pro-mini, please specify the port in your options.'));
} else {
// all good
return callback(null);
}
};
/**
* Public method for flashing a hex file to the main program allocation of the Arduino
*
* @param {string} file - path to hex file for uploading
* @param {function} callback - function to run upon completion/error
*/
AvrgirlArduino.prototype.flash = function(file, callback) {
var _this = this;
// validate board properties first
_this._validateBoard(function(error) {
if (error) { return callback(error); }
// set up serialport connection
_this.connection._init(function(error) {
if (error) { return callback(error); }
// upload file to board
_this.protocol._upload(file, callback);
});
});
};
/**
* Return a list of devices on serial ports. In addition to the output provided
* by SerialPort.list, it adds a platform independent PID in _pid
*
* @param {function} callback - function to run upon completion/error
*/
AvrgirlArduino.prototype.listPorts = AvrgirlArduino.listPorts =
AvrgirlArduino.prototype.list = AvrgirlArduino.list = function(callback) {
return Connection.prototype._listPorts(callback);
};
/**
* Static method to return the names of all known boards.
*/
AvrgirlArduino.listKnownBoards = function() {
// filter the boards to find all non-aliases
return Object.keys(boards).filter(function(name) {
// fetch the current board aliases
var aliases = boards[name].aliases;
// only allow the name if it's not an alias
return !aliases || !~aliases.indexOf(name);
});
};
// shift public static exposure for demo purposes
AvrgirlArduino.prototype.tools = tools;
return AvrgirlArduino;
};
module.exports = injectDependencies;