This repository has been archived by the owner on Aug 27, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathuploader.js
123 lines (114 loc) · 4.1 KB
/
uploader.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
//var sp = require('serialport');
var child_process = require('child_process');
var os = require('os');
/*
sp.list(function(err,list) {
console.log(list);
});
*/
function runAVRDude(hexfile, portpath, options, debug, cb) {
debug("running AVR dude");
if(options.platform.useSerial()) {
var uploadcmd = [
options.platform.getAvrDudeBinary(),
'-C'+options.platform.getAvrDudeConf(),
'-v','-v','-v', //super verbose
'-p'+options.device.build.mcu,
'-c'+options.device.upload.protocol,
'-P'+portpath,
'-b'+options.device.upload.speed,
'-D', //don't erase
'-Uflash:w:'+os.tmpdir() +'/'+hexfile+':i',
];
} else {
var uploadcmd = [
options.platform.getAvrDudeBinary(),
'-C'+options.platform.getAvrDudeConf(),
'-c',options.platform.getProgrammerId(),//'usbtiny',
'-p',options.device.build.mcu,//'attiny85',
'-Uflash:w:'+hexfile,
];
}
console.log("running", uploadcmd.join(' '));
var result = child_process.execFile(
uploadcmd[0],
uploadcmd.slice(1),
function(error, stdout, stderr) {
console.log(error,stdout,stderr);
if(error) {
console.log("error. code = ",error.code);
console.log(error);
var err = new Error("there was a problem running " + uploadcmd.join(" "));
err.cmd = uploadcmd;
err.output = stdout + stderr;
console.log(stdout);
console.log(stderr)
debug(err);
if(cb) cb(err);
} else {
debug("uploaded");
if(cb) cb();
}
}
);
}
function scanForPortReturn(list1,cb) {
sp.list(function(err, list2) {
console.log("list 2 is ",list2);
console.log("lengths = ",list1.length,list2.length);
if(list2.length < list1.length) {
console.log("we need to rescan");
setTimeout(function() {
scanForPortReturn(list1, cb);
},700);
} else {
console.log('we are back to normal!');
setTimeout(function() {
cb(list1[list1.length-1].comName);
},500);
}
});
}
exports.upload = function(hexfile,portpath,options, publish, callback) {
function debug(message) {
var args = Array.prototype.slice.call(arguments);
if(message instanceof Error) {
publish({type:'error', message: args.join(" ") + message.output});
} else {
publish({type:"upload", message:args.join(" ")});
}
}
console.log("uploading to device using ",options.device);
//var serialpath = "/tty/foobar";
//var serialpath = '/dev/cu.usbserial-AH019ZWX';
if(options.device.bootloader.path == 'caterina') {
console.log("need to do the leonardo dance");
//scan for ports
sp.list(function(err,list1) {
console.log("list 1 is ",list1);
//open port at 1200 baud
var port = new sp.SerialPort(portpath, { baudrate: 1200 });
port.on('open',function() {
console.log("opened at 1200bd");
//close port
port.flush(function() {
port.close(function() {
console.log("did a successful close");
console.log("closed at 1200bd");
//wait 300ms
setTimeout(function() {
console.log("doing a second list");
//scan for ports again
scanForPortReturn(list1,function(ppath) {
console.log("got new path",ppath);
runAVRDude(hexfile,ppath,options, debug, callback);
})
},500);
})
});
});
});
} else {
runAVRDude(hexfile,portpath,options, debug, callback);
}
}