-
Notifications
You must be signed in to change notification settings - Fork 3
/
flasher.js
60 lines (44 loc) · 1.18 KB
/
flasher.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
'use strict';
let fs = require('fs');
let Avrgirl = require('avrgirl-arduino');
function findArduboy() {
return new Promise(function(resolve, reject) {
Avrgirl.list(function(err, ports) {
if (err) return reject(err);
// Find (first) Arduboy device
for (let p of ports) {
// Bootloader mode
if (p.vendorId === '0x2341' && p.productId === '0x8036') {
return resolve(p.comName);
}
// Recovery mode
if (p.vendorId === '0x2341' && p.productId === '0x0036') {
return resolve(p.comName);
}
}
reject(new Error("Can't find any connected Arduboy device!"));
});
});
}
function flashArduboy(hex, port) {
return require('./flash.js')({
PORT: port,
DATA: hex
});
}
// Find & flash Arduboy with the provided .hex
let abPort, abHex;
function main(hex) {
return findArduboy()
.then(function(port) {
abPort = port;
abHex = hex;
return flashArduboy(abHex, abPort);
}).catch(function(e) {
console.log("Flashing failed: ", e.stack||e);
throw e;
});
}
module.exports = main;
module.exports.findArduboy = findArduboy;
module.exports.flashArduboy = flashArduboy;