blib/lib/UAV/Pilot/WumpusRover/Server/Backend.pm

filter: Statementstmt 52.0% 26 / 50 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 50.0% 4 / 8 Podpod 100% 1 / 1 totaltotal 46.2% 31 / 67 CC 6 SCAR 19.3
CC
6
Cov
46%
_packet_radio_mins
12.0
_packet_radio_maxes
12.0
process_packet
6.0
CRAP 6.9
↑ Summary Build »
1 package UAV::Pilot::WumpusRover::Server::Backend;
2 1 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 use constant PACKET_METHOD_MAP => {
6 'RequestStartupMessage' => '_packet_request_startup',
7 'RadioTrims' => '_packet_radio_trims',
8 'RadioMins' => '_packet_radio_mins',
9 'RadioMaxes' => '_packet_radio_maxes',
10 'RadioOutputs' => '_packet_radio_out',
11 1 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
12 my @REQUIRED_PACKET_METHODS = grep {
13 5 $_ !~ /\A (?:
14 _packet_radio_mins | _packet_radio_maxes
15 ) \z/x
16 1 } values %{ +PACKET_METHOD_MAP };
17  
18 1 requires @REQUIRED_PACKET_METHODS;
19 1 requires qw{
20 ch1_max_out ch1_min_out
21 ch2_max_out ch2_min_out
22 ch3_max_out ch3_min_out
23 ch4_max_out ch4_min_out
24 ch5_max_out ch5_min_out
25 ch6_max_out ch6_min_out
26 ch7_max_out ch7_min_out
27 ch8_max_out ch8_min_out
28 };
29  
30 1 with 'UAV::Pilot::Logger';
31  
32 1 has 'started' => (
33 is => 'ro',
34 isa => 'Bool',
35 default => 0,
36 writer => '_set_started',
37 );
38  
39  
40 sub process_packet
41 {
42 0 ▶ my ($self, $packet, $server) = @_;
Subroutine process_packet: not called CC=2 SCAR=17.9
Pod documented
43  
44 0 my $packet_class = ref $packet;
45 0 my ($short_class) = $packet_class =~ /:: (\w+) \z/x;
46  
47 0 ▶ if(! exists $self->PACKET_METHOD_MAP->{$short_class}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->PACKET_METHOD_MAP->{$short_class}) 0 0 0
48 0 $self->_logger->warn( "Couldn't find a method to handle packet"
49 . " '$short_class'" );
50 0 return 0;
51 }
52  
53 0 my $method = $self->PACKET_METHOD_MAP->{$short_class};
54 0 return $self->$method( $packet, $server );
55 }
56  
57 sub _packet_radio_mins
58 {
59 0 ▶ my ($self, $packet, $server) = @_;
Subroutine _packet_radio_mins: not called CC=3 SCAR=24.8
60 0 foreach (1..8) {
61 0 my $packet_call = 'ch' . $_ . '_min';
62 0 my $server_call = '_set_ch' . $_ . '_min';
63  
64 0 ▶ my $value = $packet->$packet_call // 0;
Condition0%0/2
$packet->$packet_call // 0
l!l
00
MC/DC0%0/1
$packet->$packet_call // 0
$packet->$packet_call
Truth table0%0/2
A
A= $packet->$packet_call
Aresult
11
00
65 0 $server->$server_call( $value );
66 }
67 0 return 1;
68 }
69  
70 sub _packet_radio_maxes
71 {
72 0 ▶ my ($self, $packet, $server) = @_;
Subroutine _packet_radio_maxes: not called CC=3 SCAR=24.8
73 0 foreach (1..8) {
74 0 my $packet_call = 'ch' . $_ . '_max';
75 0 my $server_call = '_set_ch' . $_ . '_max';
76  
77 0 ▶ my $value = $packet->$packet_call // 0;
Condition0%0/2
$packet->$packet_call // 0
l!l
00
MC/DC0%0/1
$packet->$packet_call // 0
$packet->$packet_call
Truth table0%0/2
A
A= $packet->$packet_call
Aresult
11
00
78 0 $server->$server_call( $value );
79 }
80 0 return 1;
81 }
82  
83 #
84 # Implement _map_ch1_value() through _map_ch8_value() here
85 #
86 1 foreach my $i (1..8) {
87 8 my $sub_name = '_map_ch' . $i . '_value';
88 8 my $min_in = 'ch' . $i . '_min';
89 8 my $max_in = 'ch' . $i . '_max';
90 8 my $min_out = 'ch' . $i . '_min_out';
91 8 my $max_out = 'ch' . $i . '_max_out';
92  
93 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
94 *$sub_name = sub {
95 0 ▶ my ($self, $server, $val) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
96 0 return $server->_map_value(
97 $server->$min_in,
98 $server->$max_in,
99 $self->$min_out,
100 $self->$max_out,
101 $val,
102 );
103 }
104 8 }
105  
106  
107 1 1;
108 __END__
109  
110  
111 =head1 NAME
112  
113 UAV::Pilot::WumpusRover::Server::Backend
114  
115 =head1 DESCRIPTION
116  
117 Role for WumpusRover Backends. A Backend connects directly to the hardware
118 that drives the rover. For instance, the RaspberryPiI2C backend communicates
119 over the Raspberry Pi's I2C interface using a protocol shared by the
120 wumpus_rover Arduino implementation.
121  
122 Does the C<UAV::Pilot::Logger> role.
123  
124 =head1 ATTRIBUTES
125  
126 =head2 started
127  
128 Specifies if this backend has been started yet. Starting it is done by
129 passing a C<RequestStartupMessage> packet to C<process_packet()>.
130  
131  
132 =head1 METHODS
133  
134 =head2 process_packet
135  
136 process_packet( $packet )
137  
138 Takes the packet and does something with it. Usually, this something is a
139 sensible thing to do.
140  
141 =head1 REQUIRED METHODS/ATTRIBUTES
142  
143 =head2 _packet_request_startup
144  
145 _packet_request_startup( $packet, $server )
146  
147 Passed a packet and the server associated with the connection. Handles the
148 initial startup.
149  
150 =head2 _packet_radio_trims
151  
152 _packet_request_trims( $packet, $server )
153  
154 Passed a packet and the server associated with the connection. Handles the
155 radio trims.
156  
157 =head2 _packet_radio_out
158  
159 _packet_request_out( $packet, $server )
160  
161 Passed a packet and the server associated with the connection. Handles the
162 radio outputs, which is the primary way of moving.
163  
164 =head2 _ch*_min_out() and _ch*_max_out()
165  
166 Returns the min/max settings for each channel that will be output by this
167 backend. Channels are numbered 1 through 8.
168  
169 Why do we map the values at this level? For the Arduino output, wouldn't it
170 be better for it to take specified values and convert it to its own output
171 internally? Perhaps. The reason why it was chosen to do the value mapping
172 here is to make the Arduino end as simple-stupid as possible.
173  
174 =cut
↑ Summary Build »