WumpusRover/Commands.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 3 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 99 CC 2 SCAR 10.0
CC
2
Cov
0%
start_joystick
6.0
uav_module_init
2.0
throttle
2.0
CRAP 2.7
1 use v5.14;
2 use UAV::Pilot::Exceptions;
3 use UAV::Pilot::Events;
4 use UAV::Pilot::EasyEvent;
5  
6  
7 {
8 my $dev = undef;
9 my $cv = undef;
10 my $events = undef;
11 my $easy_events = undef;
12  
13 # TODO
14 # Make this into a role so ARDrone and WumpusRover can share the code
15 my $init_events = sub {
16 return 1 if defined $events;
17 die "Can't init UAV::Pilot::Events without a condvar\n" unless defined $cv;
18  
19 $events = UAV::Pilot::Events->new({
20 condvar => $cv,
21 });
22  
23 $events->init_event_loop;
24  
25 # If we can load SDL, then init it here
26 eval "use UAV::Pilot::SDL::Events";
27 if(! $@ ) {
28 my $sdl_events = UAV::Pilot::SDL::Events->new;
29 $events->register( $sdl_events );
30 }
31  
32 $dev->init_event_loop( $cv, $easy_events );
33 return 1;
34 };
35  
36 my $vid_driver = undef;
37 my $init_vid_driver = sub {
38 return 1 if defined $vid_driver;
39  
40 $vid_driver = UAV::Pilot::WumpusRover::Video->new({
41 condvar => $cv,
42 driver => $dev->driver,
43 });
44 $vid_driver->init_event_loop;
45  
46 return 1;
47 };
48  
49 sub uav_module_init
50 {
51 my ($class, $cmd, $args) = @_;
52 $cv = $args->{condvar};
53  
54 $easy_events = UAV::Pilot::EasyEvent->new({
55 condvar => $cv,
56 });
57 $easy_events->init_event_loop;
58  
59 $dev = $cmd->controller_callback_wumpusrover->(
60 $cmd, $cv, $easy_events );
61 return 1;
62 }
63  
64 sub throttle ($)
65 {
66 my ($value) = @_;
67 $dev->throttle( $value );
68 return 1;
69 }
70  
71 sub turn ($)
72 {
73 my ($value) = @_;
74 $dev->turn( $value );
75 return 1;
76 }
77  
78 sub stop ()
79 {
80 $dev->throttle( 0 );
81 $dev->turn( 0 );
82 return 1;
83 }
84  
85 # TODO
86 # Make this into a role so ARDrone and WumpusRover can share the code
87 sub start_joystick ()
88 {
89 $init_events->();
90 eval "use UAV::Pilot::SDL::Joystick";
91 die "Problem loading UAV::Pilot::SDL::Joystick: $@\n" if $@;
92  
93 my $joystick = UAV::Pilot::SDL::Joystick->new({
94 condvar => $cv,
95 events => $easy_events,
96 });
97 $events->register( $joystick );
98  
99 say 'Ready for joystick input on ['
100 . SDL::Joystick::name( $joystick->joystick->index )
101 . ']';
102  
103 return 1;
104 }
105  
106 sub dump_video_to_file ($)
107 {
108 my ($file) = @_;
109 $init_vid_driver->();
110  
111 open( my $fh, '>', $file ) or UAV::Pilot::IOException->throw({
112 error => "Can't open [$file] for reading: $!\n",
113 });
114 my $vid_dump = UAV::Pilot::Video::FileDump->new({
115 fh => $fh,
116 });
117  
118 $vid_driver->add_handler( $vid_dump );
119 say "Dumping video to file '$file'";
120 return 1;
121 }
122 }
123  
124 # TODO
125 # Implement video
126 #sub start_video ()
127 #{
128 #}
129  
130 # TODO
131 # Implement taking picture
132 #sub take_picture ($)
133 #{
134 # my ($file) = @_;
135 #}
136  
137  
138  
139 # TODO
140 # Implement telemetry on server
141 #sub start_nav ()
142 #{
143 #}
144  
145  
146  
147  
148 1;