/
tpad
executable file
·118 lines (82 loc) · 2.47 KB
/
tpad
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
#!/usr/bin/env node
var
c = require('ansi')(process.stdout),
argv = require('optimist').argv,
version = require(__dirname + '/../package.json').version,
repl = require('../lib/repl'),
serial = require('../lib/serial'),
Tpad = require('../lib/tpad'),
cleanup = c.reset.bind(c),
color = require('color')
flash = require('../firmware'),
command = argv._[0] || null;
c.hex('CCFF00');
console.log([
'_/_/_/_/_/ _/_/_/ _/_/ _/_/_/',
' _/ _/ _/ _/ _/ _/ _/',
' _/ _/_/_/ _/_/_/_/ _/ _/',
' _/ _/ _/ _/ _/ _/',
'_/ _/ _/ _/ _/_/_/'].join('\n'));
c.hex('aaaaaa');
console.log('version:', version);
c.reset();
c.green();
serial.init();
var currentRepl = repl.start(), currentSerialPort;
currentRepl.context.color = color;
var spinner = "|/-\\", spinnerPos = 0, spinnerTimer;
serial.on('searching', function() {
c.grey();
process.stdout.write('searching ');
spinnerTimer = setInterval(function() {
c.back(1);
process.stdout.write(spinner[spinnerPos]);
c.reset();
spinnerPos++;
if (spinnerPos >= spinner.length) {
spinnerPos = 0;
}
}, 50);
});
serial.on('connected', function() {
clearInterval(spinnerTimer);
});
// Normal repl mode
if (!command) {
require('../command')(currentRepl);
serial.on('connected', function(config) {
clearInterval(spinnerTimer);
c.green();
console.log('\n\nconnected to a', config.tpad.name, 'with', config.tpad.pads, 'pads');
c.reset();
currentRepl.updatePrompt(config.tpad);
currentRepl.context.sp = currentSerialPort = config.serialport;
var tpad = currentRepl.context.tpad = new Tpad(config);
c.reset();
});
serial.on('disconnected', function() {
c.red();
currentSerialPort = null;
console.log('disconnected');
});
} else {
switch (command) {
case 'flash':
console.log('Please connect _only_ the tpad you want to flash')
serial.on('connected', function(config) {
serial.removeAllListeners('searching');
// Disable reconnect as it gets in the way of the flashing operation
config.serialport.removeAllListeners('close');
// allow the user to specify a development version of the tpad firmware
config.location = argv._[1] || null;
config.debug = !!argv.debug;
flash(config);
});
break;
}
}
process.on('unhandledException', function(e) {
console.log(e);
cleanup();
});
process.on('exit', cleanup);