forked from revspace/operame
-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathmulti-upload.pl
87 lines (68 loc) · 2.12 KB
/
multi-upload.pl
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
#!/usr/bin/perl
use strict;
use warnings;
use autodie;
use Time::HiRes qw(sleep);
use POSIX qw(strftime);
if (!@ARGV or $ARGV[0] ne "--please-destroy-my-data") {
print "DANGER! Please read and understand the source code before using.\n";
print "Regular users do not need to use this program. Use 'pio run' instead.\n";
exit 1;
}
my %pane;
my $command = "";
sub printlog {
print strftime("%Y-%m-%d %H:%M:%S", localtime), " @_\n";
}
sub spawn {
my ($dev) = @_;
printlog "Spawning $dev";
my $c = $command;
$c =~ s[ttyUSB\w+][$dev];
$c =~ s/^/time /;
$c =~ s[$][| perl -pe"BEGIN { \$/ = \\1 } s/\\r/\\n/"; echo "-- $dev done --"; read a];
$pane{$dev} = readpipe qq{tmux split-window -d -h -P -F '#{pane_id}' '$c'};
chomp $pane{$dev};
system "tmux select-layout even-horizontal";
}
if (1 < (() = glob("/dev/ttyUSB*"))) {
die "Disconnect USB serial devices except one target device.\n";
}
if ($ARGV[-1] ne "it's-a-me") {
exec qw[tmux new perl], $0, @ARGV, "it's-a-me";
}
my $pio_pid = open my $pio, "-|", "pio run -v -t upload";
while (defined(my $line = readline $pio)) {
print $line;
if ($line =~ /esptool/ && $line =~ /write_flash/) {
$command = $line;
system "pkill -9 -P $pio_pid"; # kill upload process.
kill 9, $pio_pid;
last;
}
}
if ($command =~ /(ttyUSB\d+)/) {
spawn $1;
} else {
warn "Could not snatch upload command.\nMake sure a single target device is already connected.\nPress enter to exit.\n";
scalar <STDIN>;
die;
}
open my $monitor, "-|", "udevadm monitor --kernel --subsystem-match=usb-serial";
printlog "Started monitoring for new usb-serial devices.";
while (defined(my $line = readline $monitor)) {
my ($event) = $line =~ /\b(remove|add)\b/ or next;
my ($dev) = $line =~ /(ttyUSB\d+)/ or next;
if ($event eq 'add') {
while (!-w "/dev/$dev") {
sleep .1;
# wait for permissions to settle;
}
spawn($dev);
} else {
if (exists $pane{$dev}) {
printlog "Killing $dev";
system "tmux kill-pane -t $pane{$dev}";
}
}
}