File Coverage

blib/lib/UAV/Pilot.pm
Criterion Covered Total %
statement 29 69 42.0
branch 0 6 0.0
condition n/a
subroutine 10 19 52.6
pod 0 8 0.0
total 39 102 38.2


line stmt bran cond sub pod time code
1             # Copyright (c) 2015 Timm Murray
2             # All rights reserved.
3             #
4             # Redistribution and use in source and binary forms, with or without
5             # modification, are permitted provided that the following conditions are met:
6             #
7             # * Redistributions of source code must retain the above copyright notice,
8             # this list of conditions and the following disclaimer.
9             # * Redistributions in binary form must reproduce the above copyright
10             # notice, this list of conditions and the following disclaimer in the
11             # documentation and/or other materials provided with the distribution.
12             #
13             # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14             # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15             # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16             # ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
17             # LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18             # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19             # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20             # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21             # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22             # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
23             # POSSIBILITY OF SUCH DAMAGE.
24             package UAV::Pilot;
25             $UAV::Pilot::VERSION = '1.3';
26 2     2   21841 use v5.14;
  2         8  
27 2     2   1724 use Moose;
  2         954527  
  2         13  
28 2     2   15776 use namespace::autoclean;
  2         16499  
  2         8  
29 2     2   116 use File::Spec;
  2         4  
  2         48  
30 2     2   1609 use File::ShareDir;
  2         13803  
  2         110  
31 2     2   1642 use File::HomeDir;
  2         11956  
  2         112  
32 2     2   2504 use Log::Log4perl;
  2         106391  
  2         12  
33              
34 2     2   104 use constant DIST_NAME => 'UAV-Pilot';
  2         6  
  2         107  
35 2     2   11 use constant LOG_CONF_FILE => 'log4perl.conf';
  2         6  
  2         1224  
36              
37             our $LOG_WAS_INITD = 0;
38              
39             # ABSTRACT: Base library for controlling UAVs
40              
41              
42             sub default_module_dir
43             {
44 0     0 0   my ($class) = @_;
45 0           my $dir = File::ShareDir::dist_dir( $class->DIST_NAME );
46 0           return $dir;
47             }
48              
49             sub default_config_dir
50             {
51 0     0 0   my ($class) = @_;
52 0           my $dir = File::HomeDir->my_dist_config( $class->DIST_NAME, {
53             create => 1,
54             });
55 0           return $dir,
56             }
57              
58             sub init_log
59             {
60 0     0 0   my ($class) = @_;
61 0 0         return if $LOG_WAS_INITD;
62 0           my $conf_dir = $class->default_config_dir;
63 0           my $log_conf = File::Spec->catfile( $conf_dir, $class->LOG_CONF_FILE );
64              
65 0 0         $class->_make_default_log_conf( $log_conf ) if ! -e $log_conf;
66              
67 0           Log::Log4perl::init( $log_conf );
68 0           return 1;
69             }
70              
71             sub checksum_fletcher8
72             {
73 0     0 0   my ($class, @bytes) = @_;
74 0           my $ck_a = 0;
75 0           my $ck_b = 0;
76              
77 0           foreach (@bytes) {
78 0           $ck_a = ($ck_a + $_) & 0xFF;
79 0           $ck_b = ($ck_b + $ck_a) & 0xFF;
80             }
81              
82 0           return ($ck_a, $ck_b);
83             }
84              
85             sub convert_32bit_LE
86             {
87 0     0 0   my ($class, @bytes) = @_;
88 0           my $val = $bytes[0]
89             | ($bytes[1] << 8)
90             | ($bytes[2] << 16)
91             | ($bytes[3] << 24);
92 0           return $val;
93             }
94              
95             sub convert_16bit_LE
96             {
97 0     0 0   my ($class, @bytes) = @_;
98 0           my $val = $bytes[0] | ($bytes[1] << 8);
99 0           return $val;
100             }
101              
102             sub convert_32bit_BE
103             {
104 0     0 0   my ($class, @bytes) = @_;
105 0           my $val = ($bytes[0] << 24)
106             | ($bytes[1] << 16)
107             | ($bytes[2] << 8)
108             | $bytes[3];
109 0           return $val;
110             }
111              
112             sub convert_16bit_BE
113             {
114 0     0 0   my ($class, @bytes) = @_;
115 0           my $val = ($bytes[0] << 8) | $bytes[1];
116 0           return $val;
117             }
118              
119             sub _make_default_log_conf
120             {
121 0     0     my ($class, $out_file) = @_;
122              
123 0 0         open( my $out, '>', $out_file )
124             or die "Can't open [$out_file] for writing: $!\n";
125              
126 0           print $out "log4j.rootLogger=WARN, A1\n";
127 0           print $out "log4j.appender.A1=Log::Log4perl::Appender::Screen\n";
128 0           print $out "log4j.appender.A1.layout=org.apache.log4j.PatternLayout\n";
129 0           print $out "log4j.appender.A1.layout.ConversionPattern="
130             . '%-4r [%t] %-5p %c %t - %m%n' . "\n";
131              
132 0           close $out;
133 0           return 1;
134             }
135              
136              
137 2     2   21 no Moose;
  2         4  
  2         19  
138             __PACKAGE__->meta->make_immutable;
139             1;
140             __END__
141              
142              
143             =head1 NAME
144              
145             UAV::Pilot - Base library for controlling UAVs
146              
147             =head1 DESCRIPTION
148              
149             This library does not support controlling any UAVs on its own. Rather, it
150             provides the basic support for implementing other UAV libraries, much the same
151             way DBI provides support for implementing different database drivers.
152              
153             If you would like to control the Parrot AR.Drone, you should also install
154             C<UAV::Pilot::ARDrone>, and probably C<UAV::Pilot::SDL> and
155             C<UAV::Pilot::Video::Ffmpeg> as well.
156              
157             =head1 LICENSE
158              
159             Copyright (c) 2014 Timm Murray
160             All rights reserved.
161              
162             Redistribution and use in source and binary forms, with or without modification, are
163             permitted provided that the following conditions are met:
164              
165             * Redistributions of source code must retain the above copyright notice, this list of
166             conditions and the following disclaimer.
167             * Redistributions in binary form must reproduce the above copyright notice, this list of
168             conditions and the following disclaimer in the documentation and/or other materials
169             provided with the distribution.
170              
171             THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
172             OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
173             MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
174             COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
175             EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
176             SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
177             HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
178             TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
179             EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
180              
181              
182             =cut