forked from samyk/samytools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflash
executable file
·102 lines (86 loc) · 1.8 KB
/
flash
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
#!/usr/bin/perl
#
# execute or compile or flash code, locally, using particle, or platformio
#
# used in vim with ctrl+k/ctrl+l:
# nnoremap <C-k> :w \| !flash -c %p<CR>
# nnoremap <C-l> :w \| !flash %p<CR>
#
# -samy kamkar 2020/06/20
#use Data::Dumper;
#open F,">/tmp/dump";print F Dumper(\@ARGV) .$/;print F Dumper(\%ENV); close(F);
use y;;;
use strict;
my $PKG = "package.json";
my $compile = $ARGV[0] eq "-c" && shift;
my $pio = -e "platformio.ini"; # true if we are working with pio
my $npm = -e $PKG && json($PKG)->{scripts}{start};
my $name = shift || "desk" || "p"; # particle board name or file name
my ($ext) = ($name =~ /\.(\w+)$/);
my $board = shift || "photon";
my $PARTICLE = "particle";
my $PLATFORMIO = "platformio";
my $CC = "gcc";
# platformio
if ($pio)
{
system($compile ? ($PLATFORMIO, qw/run -v/) : ($PLATFORMIO, qw/run -v --target upload/));
}
# npm start
elsif ($npm)
{
system(qw/npm start/);
}
# pl file
elsif ($ext eq "pl")
{
system("perl", $name);
}
# js file
elsif ($ext eq "js")
{
system("node", $name);
}
# c/cpp file
elsif ($ext eq "c" || $ext eq "cpp")
{
my $code = code($name);
# particle/arduino code
$code =~ /void\s+setup\s*\(/ ? particle() : c();
}
# particle
else
{
particle();
}
sub c
{
system($CC, $name, "-o", "$name.bin");
system($name =~ m|^/| ? "$name.bin" : "./$name.bin");
}
sub particle
{
# dfu over usb
if ($name eq "usb")
{
my $out = `$PARTICLE compile $board`;
print $out;
if ($out =~ /saved firmware to: (\S+)/i)
{
system($PARTICLE, qw/flash --usb/, $1);
}
}
# flash over network
else
{
system($compile ? ($PARTICLE, qw/-v compile/, $board) : ($PARTICLE, qw/-v flash/, $name));
}
}
sub code
{
my $code = cat($name);
# remove comments
$code =~ s~/\*.*?\*\/~~g;
$code =~ s~//.*?\n~~g;
return $code;
}