duino/Command/upload.pm

filter: Statementstmt 31.3% 21 / 67 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 60.0% 6 / 10 Podpod 100% 4 / 4 totaltotal 25.4% 31 / 122 CC 16 SCAR 51.6
CC
16
Cov
21%
execute
272.0
abstract
2.0
usage_desc
2.0
CRAP 174.6
« duino/Command.pm ↑ Summary duino/Command/build.pm »
1 package App::duino::Command::upload;
2 {
3 1 $App::duino::Command::upload::VERSION = '0.10';
4 }
5  
6 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use App::duino -command;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 =head1 NAME
16  
17 App::duino::Command::upload - Upload a sketch to an Arduino
18  
19 =head1 VERSION
20  
21 version 0.10
22  
23 =head1 SYNOPSIS
24  
25 # this will find the *.hex file to upload in the board's build directory
26 $ duino upload --board uno --port /dev/ttyACM0
27  
28 # explicitly provide the *.hex file
29 $ duino upload --board uno some_file.hex
30  
31 =cut
32  
33 0 ▶sub abstract { 'upload a sketch to an Arduino' }
Subroutine abstract: not called CC=1 SCAR=6.9
Pod documented
34  
35 0 ▶sub usage_desc { '%c upload %o [sketch.ino]' }
Subroutine usage_desc: not called CC=1 SCAR=6.9
Pod documented
36  
37 0 ▶sub opt_spec {
Subroutine opt_spec: not called CC=1 SCAR=6.9
Pod documented
38 0 my ($self) = @_;
39  
40 return (
41 0 [ 'board|b=s', 'specify the board model',
42 { default => $self -> default_config('board') } ],
43  
44 [ 'port|p=s', 'specify the serial port to use',
45 { default => $self -> default_config('port') } ],
46  
47 [ 'fuses|f', 'write the fuses bits when uploading',
48 { default => $self -> default_config('fuses') } ],
49  
50 [ 'uploader|u=s', 'specify the uploader to use',
51 { default => $self -> default_config('uploader') } ],
52  
53 [ 'sketchbook|s=s', 'specify the user sketchbook directory',
54 { default => $self -> default_config('sketchbook') } ],
55  
56 [ 'root|d=s', 'specify the Arduino installation directory',
57 { default => $self -> default_config('root') } ],
58  
59 [ 'hardware|r=s', 'specify the hardware type to build for',
60 { default => $self -> default_config('hardware') } ],
61 );
62 }
63  
64 0 ▶sub execute {
Subroutine execute: not called CC=16 SCAR=56.1
Pod documented
65 0 my ($self, $opt, $args) = @_;
66  
67 0 my $board= $opt -> board;
68 0 my $port = $opt -> port;
69 0 my $name = basename getcwd;
70  
71 0 ▶ ($name = basename($args -> [0])) =~ s/\.[^.]+$//
Branch: 1 branch
BranchTrueFalseTotal
if $args->[0] and -e $args->[0] 0 0 0
Condition0%0/3
$args->[0] and -e $args->[0]
!ll&&!rl&&r
000
MC/DC0%0/2
$args->[0] and -e $args->[0]
$args->[0] -e $args->[0]
Truth table0%0/3
A and B
A= $args->[0]B= -e $args->[0]
ABresult
0X0
100
111
72 if $args -> [0] and -e $args -> [0];
73  
74 0 my $hex = ".build/$board/$name.hex";
75  
76 0 ▶ $hex = $args -> [0] if $args -> [0] and $args -> [0] =~ /\.hex$/;
Branch: 1 branch
BranchTrueFalseTotal
if $args->[0] and $args->[0] =~ /\.hex$/ 0 0 0
Condition0%0/3
$args->[0] and $args->[0] =~ /\.hex$/
!ll&&!rl&&r
000
MC/DC0%0/2
$args->[0] and $args->[0] =~ /\.hex$/
$args->[0] $args->[0] =~ /\.hex$/
Truth table0%0/3
A and B
A= $args->[0]B= $args->[0] =~ /\.hex$/
ABresult
0X0
100
111
77  
78 0 my $mcu = $self -> board_config($opt, 'build.mcu');
79 0 ▶ my $prog = $opt -> uploader ||
Condition0%0/6
$opt->uploader || $self->board_config($opt, 'upload.protocol') || $self->board_config($opt, 'upload.using')
l!l&&r!l&&!r
000
$opt->uploader or $self->board_config($opt, "upload.protocol")
l!l&&r!l&&!r
000
MC/DC0%0/3
$opt->uploader || $self->board_config($opt, 'upload.protocol') || $self->board_config($opt, 'upload.using')
$opt->uploader $self->board_config($opt, "upload.protocol") $self->board_config($opt, 'upload.using')
Truth table0%0/4
A or B || C
A= $opt->uploaderB= $self->board_config($opt, "upload.protocol")C= $self->board_config($opt, 'upload.using')
ABCresult
1XX1
01X1
0011
0000
80 $self -> board_config($opt, 'upload.protocol') ||
81 $self -> board_config($opt, 'upload.using');
82 0 my $baud = $self -> board_config($opt, 'upload.speed');
83  
84 0 my $avrdude = $self -> file($opt, 'hardware/tools/avrdude');
85 0 my $avrdude_conf = $self -> file($opt, 'hardware/tools/avrdude.conf');
86  
87 0 print "Uploading to '" . $self -> board_config($opt, 'name') . "'...\n";
88  
89 0 my @avrdude_opts;
90 0 push @avrdude_opts, '-C', $avrdude_conf;
91 0 push @avrdude_opts, '-p', $mcu;
92  
93 0 push @avrdude_opts, '-c', $prog;
94 0 ▶ push @avrdude_opts, '-b', $baud if $baud;
Branch: 1 branch
BranchTrueFalseTotal
if $baud 0 0 0
95 0 push @avrdude_opts, '-P', $port;
96  
97 0 ▶ if ($opt -> fuses) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt->fuses) 0 0 0
98 0 my $efuse = $self -> board_config($opt, 'bootloader.extended_fuses');
99 0 my $hfuse = $self -> board_config($opt, 'bootloader.high_fuses');
100 0 my $lfuse = $self -> board_config($opt, 'bootloader.low_fuses');
101  
102 0 ▶ push @avrdude_opts, '-U', "efuse:w:$efuse:m" if $efuse;
Branch: 1 branch
BranchTrueFalseTotal
if $efuse 0 0 0
103 0 ▶ push @avrdude_opts, '-U', "hfuse:w:$hfuse:m" if $hfuse;
Branch: 1 branch
BranchTrueFalseTotal
if $hfuse 0 0 0
104 0 ▶ push @avrdude_opts, '-U', "lfuse:w:$lfuse:m" if $lfuse;
Branch: 1 branch
BranchTrueFalseTotal
if $lfuse 0 0 0
105 }
106  
107 0 push @avrdude_opts, '-U', "flash:w:$hex:i";
108  
109 0 ▶ die "Can't find file '$hex', did you run 'duino build'?\n"
Branch: 1 branch
BranchTrueFalseTotal
unless -e $hex 0 0 0
110 unless -e $hex;
111  
112 0 ▶ open my $fh, '<', $opt -> port
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $opt->port or die q[Can't open serial port '] . $opt->port . "'.\n" 0 0 0
113 or die "Can't open serial port '" . $opt -> port . "'.\n";
114  
115 0 my $fd = fileno $fh;
116  
117 0 my $term = POSIX::Termios -> new;
118 0 $term -> getattr($fd);
119  
120 0 ▶ if ($self -> board_config($opt, 'bootloader.path') eq 'caterina') {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->board_config($opt, 'bootloader.path') eq 'caterina') { } 0 0 0
121 0 $term -> setispeed(&POSIX::B1200);
122 0 $term -> setospeed(&POSIX::B1200);
123  
124 0 $term -> setattr($fd, &POSIX::TCSANOW);
125 } else {
126 0 require Device::SerialPort;
127  
128 0 ▶ my $serial = Device::SerialPort -> new($opt -> port)
Branch: 1 branch
BranchTrueFalseTotal
my $serial = "Device::SerialPort"->new($opt->port) or die q[Can't open serial port '] . $opt->port . "'.\n" 0 0 0
129 or die "Can't open serial port '" . $opt -> port . "'.\n";
130  
131 0 $serial -> pulse_dtr_on(0.1 * 1000.0);
132 }
133  
134 0 close $fh;
135  
136 0 sleep 1;
137  
138 0 system $avrdude, @avrdude_opts;
139 }
140  
141 =head1 OPTIONS
142  
143 =over 4
144  
145 =item B<--board>, B<-b>
146  
147 The Arduino board model. The environment variable C<ARDUINO_BOARD> will be used
148 if present and if the command-line option is not set. If neither of them is set
149 the default value (C<uno>) will be used.
150  
151 =item B<--port>, B<-p>
152  
153 The path to the Arduino serial port. The environment variable C<ARDUINO_PORT>
154 will be used if present and if the command-line option is not set. If neither
155 of them is set the default value (C</dev/ttyACM0>) will be used.
156  
157 =item B<--fuses>, B<-f>
158  
159 Whether to write the fuses bits when uploading. The environment variable
160 C<ARDUINO_FUSES> will be used if present and if the command-line option is not
161 set. If neither of them is set the default value (C<false>) will be used.
162  
163 =item B<--uploader>, B<-u>
164  
165 The uploader to use to upload. The environment variable C<ARDUINO_UPLOADER>
166 will be used if present and if the command-line option is not set. If neither
167 of them is set the default value specified in the C<boards.txt> file will be
168 used.
169  
170 =item B<--sketchbook>, B<-s>
171  
172 The path to the user's sketchbook directory. The environment variable
173 C<ARDUINO_SKETCHBOOK> will be used if present and if the command-line option is
174 not set. If neither of them is set the default value (C<$HOME/sketchbook>) will
175 be used.
176  
177 =item B<--root>, B<-d>
178  
179 The path to the Arduino installation directory. The environment variable
180 C<ARDUINO_DIR> will be used if present and if the command-line option is not
181 set. If neither of them is set the default value (C</usr/share/arduino>) will
182 be used.
183  
184 =item B<--hardware>, B<-r>
185  
186 The "type" of hardware to target. The environment variable C<ARDUINO_HARDWARE>
187 will be used if present and if the command-line option is not set. If neither
188 of them is set the default value (C<arduino>) will be used.
189  
190 This option is only useful when using MCUs not officially supported by the
191 Arduino platform (e.g. L<ATTiny|https://code.google.com/p/arduino-tiny/>).
192  
193 =back
194  
195 =head1 AUTHOR
196  
197 Alessandro Ghedini <alexbio@cpan.org>
198  
199 =head1 LICENSE AND COPYRIGHT
200  
201 Copyright 2013 Alessandro Ghedini.
202  
203 This program is free software; you can redistribute it and/or modify it
204 under the terms of either: the GNU General Public License as published
205 by the Free Software Foundation; or the Artistic License.
206  
207 See http://dev.perl.org/licenses/ for more information.
208  
209 =cut
210  
211 1 1; # End of App::duino::Command::upload
« duino/Command.pm ↑ Summary duino/Command/build.pm »