-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
95 lines (90 loc) · 2.24 KB
/
index.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
const inquirer = require("inquirer");
const argv = require("minimist")(process.argv.slice(2));
const { AVR, listSerialPorts } = require("./lib/AVR");
(async () => {
const { file } = argv;
let port = argv.port;
if (!file) {
throw new Error("No hex file specified");
}
if (!port) {
const ports = await listSerialPorts();
if (ports.length === 0) {
throw new Error("No serial ports found");
}
if (ports.length === 1) {
port = ports[0];
} else {
const answers = await inquirer.prompt([
{
type: "list",
name: "port",
message: "Select a port",
choices: ports,
},
]);
port = answers.port;
}
}
const { baudrate, device, programmer } = argv;
const avr = new AVR({
port,
baudrate,
device,
programmer,
});
flashLoop(port, file, avr);
})().catch((error) => {
console.error(`Error: ${error.message}`);
process.exit(1);
});
async function flashLoop(port, file, avr) {
while (true) {
await inquirer.prompt([
{
type: "confirm",
name: "confirm",
message: "Press enter to flash new device",
},
]);
console.log(`Flashing ${file} on ${port}`);
try {
const signature = await avr.readSignature();
console.log(
`Found a signature ${signature}, waiting a second to write fuses`
);
await sleep(100);
await avr.writeFuses();
await sleep(100);
await avr.flash(file);
console.log("Done");
// console.log("Waiting for device to reset");
// while (true) {
// await sleep(100);
// try {
// await avr.readSignature();
// console.log("Device has not reset");
// } catch (error) {
// console.log("Device reset");
// break;
// }
// }
} catch (error) {
console.error(error.message);
}
// waiting for device to reset
// while (true) {
// await sleep(100);
// try {
// await avr.readSignature();
// console.log("Device ready");
// } catch (error) {
// console.log("Device not ready");
// break;
// }
// }
}
}
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}