/
firmata-party.js
executable file
·131 lines (108 loc) · 3.41 KB
/
firmata-party.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
#!/usr/bin/env node
var Avrgirl = require('avrgirl-arduino');
var path = require('path');
var keypress = require('keypress');
var argv = require('minimist')(process.argv.slice(2), opts = {
boolean: ['party', 'debug', 'help']
});
var fs = require('fs');
var debugMode = argv.debug;
var partyMode = argv.party;
var helpMsg = `usage: firmata-party [<arduino name> | <command>] [--party] [--debug]
firmata-party list # list all supported boards
firmata-party uno # flash Standard Firmata to an Arduino Uno
firmata-party uno --debug # show debug info
firmata-party uno --party # keep flashing firmata on new arduinos until you quit the program with ctrl+c!
firmata-party help # show usage info
`;
var supportedBoards = Avrgirl.listKnownBoards();
var supportedBoardsString = supportedBoards.join(', ');
function showHelp() {
console.log(helpMsg);
}
function showSupported() {
console.log('supported board flags: \n' + supportedBoardsString);
}
handleArgs(argv);
function handleArgs(argv) {
var board = argv._[0];
var args = argv._;
if (!argv || args.length == 0 || args.indexOf('help') > -1 || args.indexOf('man') > -1) {
var status = board ? 0 : 1;
showHelp();
return process.exit(status);
} else if (args.indexOf('list') > -1) {
showSupported();
return process.exit(0);
} else {
var options = {board: board, debug: debugMode};
if (supportedBoards.indexOf(options.board) < 0) {
var error = new Error("oops! Sorry, the board '" + options.board + "'' is not currently supported");
return console.error(error);
}
if (args.length > 1) {
options["port"] = args[1]
}
if (partyMode) {
party(options);
} else {
flashAndQuit(options);
}
}
}
function flash(options, callback) {
var avrgirl = new Avrgirl(options);
var avrgirlDir = path.dirname(require.resolve('avrgirl-arduino'));
var firmataDir = path.resolve(avrgirlDir, 'junk', 'hex', options.board);
var firmataPath;
fs.readdir(firmataDir, function(err, files) {
if (err) { return console.error(err); }
for (var i = 0, len = files.length; i < len; i++) {
var filename = files[i];
if (filename.indexOf('StandardFirmata') > -1) {
firmataPath = path.join(firmataDir, filename);
break;
}
};
if (typeof firmataPath === 'undefined') {
var error = new Error("oops! Couldn't find Standard Firmata file for " + options.board + " board.");
return console.error(error);
}
avrgirl.flash(firmataPath, callback);
});
}
function flashAndQuit(options) {
flash(options, function(error) {
if (error) {
console.error(error);
return process.exit(1);
} else {
return process.exit();
}
});
}
function party(options) {
var boardsFlashed = 0;
keypress(process.stdin);
process.stdin.setRawMode(true);
process.stdin.resume();
var flashing = false;
console.log("Press any key to start the party! or q to go home!");
process.stdin.on('keypress', function (ch, key) {
if (!key ) { return; }
if ((key.ctrl && key.name == 'c') || key.name == 'q') {
return process.exit();
}
if (flashing) { return; }
flashing = true;
console.log('Starting the Party!');
flash(options, function(error) {
if (error) {
console.error("Oh no! Party foul!", error);
} else{
console.error("Success! Party legend! Press any key to keep partying!");
}
flashing = false;
});
});
}