AccessoryMenu.pm

filter: Statementstmt 53.4% 54 / 101 Branchbran 25.0% 10 / 40 Conditioncond 30.0% 6 / 20 MC/DCmcdc 9.0% 1 / 11 Subroutinesub 50.0% 7 / 14 Podpod 63.6% 7 / 11 totaltotal 43.1% 85 / 197 CC 29 SCAR 27.7
CC
29
Cov
43%
control
30.0
expect
22.1
percent_slider
20.0
CRAP 15.9
« AccessoryMenu/Slider.pm ↑ Summary AccessoryMenu/State.pm »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package Device::Ericsson::AccessoryMenu;
3 1 ▶use base 'Class::Accessor::Fast';
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 __PACKAGE__->mk_accessors( qw( states menu port debug callback ) );
5 1 ▶use vars qw( $VERSION );
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $VERSION = '0.8';
7  
8 =head1 NAME
9  
10 Device::Ericsson::AccessoryMenu - allows use of a T68i as a remote control
11  
12 =head1 SYNOPSIS
13  
14 my $remote = Device::Ericsson::AccessoryMenu->new;
15 $remote->menu( [ 'Remote' => [ pause => sub { ... },
16 Volume => [ up => sub { ... },
17 down => sub { ... },
18 ],
19 ],
20 ] );
21  
22 # on Win32, Win32::SerialPort should be equivalent
23 my $port = Device::SerialPort->new('/dev/rfcomm0')
24 or die "couldn't connect to T68i";
25 $remote->port( $port );
26  
27 $remote->register_menu;
28  
29 while (1) {
30 $remote->control;
31 }
32  
33 =head1 DESCRIPTION
34  
35 Device::Ericsson::AccessoryMenu provides a framework for adding an
36 accessory menu to devices that obey the EAM set of AT commands.
37  
38 This allows you to write programs with similar function to the Romeo
39 and Clicker applications for OSX, only instead of applescript your
40 actions invoke perl subroutines (which of course may invoke
41 applescript events, if that's your desire).
42  
43 =head1 METHODS
44  
45 =head2 new
46  
47 =cut
48  
49 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
50 1 my $class = shift;
51 1 $class->SUPER::new({ menu => [], @_ });
52 }
53  
54 =head2 menu
55  
56 your menus and actions.
57  
58 If your action is a subroutine, it will be invoked with the
59 Device::Ericsson::AccesoryMenu object as its first parameter.
60  
61 If the action returns a scalar, this is sent on to the phone via
62 C<send_text>
63  
64 If your action is, or returns an array reference, then it's taken as a
65 sub menu.
66  
67 =head2 port
68  
69 The serial port to communicate over.
70  
71 This may be real serial port, or a bluetooth RFCOMM device, just so
72 long as it looks like a Device::SerialPort or Win32::SerialPort.
73  
74 =head2 send( $what )
75  
76 send bytes over the serial port to the phone
77  
78 =cut
79  
80 4 ▶sub send {
Subroutine send: called CC=2 SCAR=7.0
Pod documented
81 4 my $self = shift;
82 4 my $what = shift;
83 4 my $count = $self->port->write( "$what\r" );
84 4 $self->port->write_drain;
85 4 ▶ print "# send '$what'\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 4 4
86 4 return $count == length $what;
87 }
88  
89  
90 # Lifted from Device::Modem
91 4 ▶sub expect {
Subroutine expect: called CC=15 SCAR=30.9
Pod undocumented
92 4 my $self = shift;
93 4 my ($expect, $timeout) = @_;
94  
95 4 ▶ $timeout ||= 2000;
Condition100%2/2
$timeout ||= 2000
l!l
13
MC/DC100%1/1
$timeout ||= 2000
$timeout
Truth table100%2/2
A
A= $timeout
Aresult
11
00
96  
97 4 my $time_slice = 100; # single cycle wait time
98 4 ▶ $time_slice = 20 if $timeout < 200;
Branch: 1 branch
BranchTrueFalseTotal
if $timeout < 200 0 4 4
99 4 my $max_cycles = $timeout / $time_slice;
100 4 my $max_idle_cycles = $max_cycles;
101  
102 # If we expect something, we must first match against serial input
103 4 my $done;# = (defined $expect and $expect eq '');
104  
105 # Main read cycle
106 4 my ($answer, $cycles, $idle_cycles);
107 4 ▶ do {
Branch: 1 branch
BranchTrueFalseTotal
$done or do { do { () = $self->port->read($time_slice); if (defined $what and $howmany > 0) { $answer .= $what; $idle_cycles = 1; } else { ++$idle_cycles; } ; ++$done if $expect and $answer and $answer =~ /$expect/; ++$done if $idle_cycles >= $max_idle_cycles; ++$done if ++$cycles >= $max_cycles; select undef, undef, undef, $time_slice / 1000 unless $done } } 4 0 4
108 4 my ($howmany, $what) = $self->port->read($time_slice);
109  
110 # Timeout count incremented only on empty readings
111 4 ▶ if ( defined $what && $howmany > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $what and $howmany > 0) { } 4 0 4
Condition33%1/3
defined $what and $howmany > 0
!ll&&!rl&&r
004
MC/DC0%0/2
defined $what and $howmany > 0
defined $what $howmany > 0
Truth table33%1/3
A and B
A= defined $whatB= $howmany > 0
ABresult
0X0
100
111
112 4 $answer .= $what;
113 4 $idle_cycles = 1;
114 #$max_idle_cycles = $max_cycles;
115 }
116 else {
117 0 ++$idle_cycles;
118 }
119  
120 4 ▶ ++$done if $expect && $answer && $answer =~ $expect;
Branch: 1 branch
BranchTrueFalseTotal
if $expect and $answer and $answer =~ /$expect/ 4 0 4
Condition33%2/6
$expect and $answer and $answer =~ /$expect/
!ll&&!rl&&r
004
$expect and $answer
!ll&&!rl&&r
004
MC/DC0%0/3
$expect and $answer and $answer =~ /$expect/
$expect $answer $answer =~ /$expect/
Truth table25%1/4
A and B and C
A= $expectB= $answerC= $answer =~ /$expect/
ABCresult
0XX0
10X0
1100
1111
121 4 ▶ ++$done if $idle_cycles >= $max_idle_cycles;
Branch: 1 branch
BranchTrueFalseTotal
if $idle_cycles >= $max_idle_cycles 0 4 4
122 4 ▶ ++$done if ++$cycles >= $max_cycles;
Branch: 1 branch
BranchTrueFalseTotal
if ++$cycles >= $max_cycles 0 4 4
123 4 ▶ select(undef, undef, undef, $time_slice/1000) unless $done;
Branch: 1 branch
BranchTrueFalseTotal
unless $done 0 4 4
124 } while ( not $done );
125  
126 # Flush receive and trasmit buffers
127 4 $self->port->purge_all;
128  
129 # Trim result of beginning and ending CR+LF (XXX)
130 4 ▶ if( defined $answer ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $answer) 4 0 4
131 4 $answer =~ s/^[\r\n]+//;
132 4 $answer =~ s/[\r\n]+$//;
133 }
134  
135 4 ▶ print "# got '$answer'\n" if $self->debug && defined $answer;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug and defined $answer 0 4 4
Condition33%1/3
$self->debug and defined $answer
!ll&&!rl&&r
400
MC/DC0%0/2
$self->debug and defined $answer
$self->debug defined $answer
Truth table33%1/3
A and B
A= $self->debugB= defined $answer
ABresult
0X0
100
111
136 4 return $answer;
137 }
138  
139  
140 =head2 register_menu
141  
142 Notify the phone that there's an accessory connected
143  
144 =cut
145  
146 1 ▶sub register_menu {
Subroutine register_menu: called CC=1 SCAR=0.0
Pod documented
147 1 my $self = shift;
148  
149 1 $self->states( [] );
150  
151 # Phone, Kree!
152 1 $self->send( "ATZ" );
153 1 $self->expect( "OK", 5000 );
154 # turn off echo
155 1 $self->send( "ATE=0" );
156 1 $self->expect( "OK" );
157 1 $self->send( 'AT*EAM="'. $self->menu->[0] . '"' );
158 1 $self->expect( "OK" );
159 1 $self->send( 'AT+CSCS="8859-1"' );
160 1 $self->expect( "OK" );
161  
162 }
163  
164 0 ▶sub enter_state {
Subroutine enter_state: not called CC=3 SCAR=24.8
Pod undocumented
165 0 my $self = shift;
166 0 my $class = shift;
167  
168 0 $class = __PACKAGE__."::$class";
169 0 ▶ eval "require $class" or die $@;
Branch: 1 branch
BranchTrueFalseTotal
eval "require $class" or die $@ 0 0 0
170  
171 0 my $entering = $class->new( parent => $self, @_ );
172 0 unshift @{ $self->states }, $entering;
173  
174 0 ▶ print "entering $entering\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
175 0 $entering->on_enter;
176 0 return;
177 }
178  
179 0 ▶sub exit_state {
Subroutine exit_state: not called CC=3 SCAR=24.8
Pod undocumented
180 0 my $self = shift;
181  
182 0 my $leaving = shift @{ $self->states };
183 0 ▶ print "leaving $leaving\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
184 0 $leaving->on_exit;
185 0 my ($current) = @{ $self->states };
186 0 ▶ $current->on_enter if $current;
Branch: 1 branch
BranchTrueFalseTotal
if $current 0 0 0
187 0 return;
188 }
189  
190 0 ▶sub current_state {
Subroutine current_state: not called CC=1 SCAR=6.9
Pod undocumented
191 0 my $self = shift;
192 0 my ($state) = @{ $self->states };
193 0 return $state;
194 }
195  
196  
197 =head2 send_text( $title, @lines )
198  
199 Send the text as a message dialog and wait for user input.
200  
201 =cut
202  
203 0 ▶sub send_text {
Subroutine send_text: not called CC=2 SCAR=17.9
Pod documented
204 0 my $self = shift;
205 0 my $title = shift;
206 0 ▶ @_ = ($title) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 0 0
207  
208 0 $self->enter_state( 'Text', title => $title, lines => \@_ );
209 }
210  
211  
212 =head2 percent_slider( %args )
213  
214 %args = (
215 title => 'Slider',
216 steps => 10, # 1..10
217 value => 50,
218 callback => undef, # a subroutine ref, will be called with the new value
219 );
220  
221 =cut
222  
223 0 ▶sub percent_slider {
Subroutine percent_slider: not called CC=4 SCAR=30.0
Pod documented
224 0 my $self = shift;
225 0 my %args = @_;
226  
227 0 ▶ my $value = defined $args{value} ? $args{value}: 50;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'value'} ? : 0 0 0
228 $self->enter_state( 'Slider', ( title => $args{title} || 'Slider',
229 steps => $args{steps} || 10,
230 value => $value,
231 0 ▶ callback => $args{callback} ) );
Condition0%0/4
$args{'title'} || 'Slider'
l!l
00
$args{'steps'} || 10
l!l
00
MC/DC0%0/1
$args{'title'} || 'Slider'
$args{'title'}
MC/DC0%0/1
$args{'steps'} || 10
$args{'steps'}
Truth table0%0/2
A
A= $args{'title'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'steps'}
Aresult
11
00
232 }
233  
234 =head2 mouse_mode( %args )
235  
236 Put the T68i into a fullscan mode. Returns keyboard events for every
237 key pressed and released.
238  
239 %args = (
240 title => 'Mouse',
241 callback => sub ( $key, $updown ) {}, # will be called with the key and
242 # the updown event (1 = key
243 # down, 0 = key up)
244  
245 );
246  
247 =cut
248  
249 0 ▶sub mouse_mode {
Subroutine mouse_mode: not called CC=2 SCAR=17.9
Pod documented
250 0 my $self = shift;
251 0 my %args = @_;
252  
253 $self->enter_state( 'Mouse', ( title => $args{title} || 'Mouse',
254 0 ▶ callback => $args{callback} ) );
Condition0%0/2
$args{'title'} || 'Mouse'
l!l
00
MC/DC0%0/1
$args{'title'} || 'Mouse'
$args{'title'}
Truth table0%0/2
A
A= $args{'title'}
Aresult
11
00
255 }
256  
257  
258 =head2 control
259  
260 Respond to what the phone is sending back over the port, invoking
261 callbacks and all that jazz.
262  
263 =cut
264  
265 0 ▶sub control {
Subroutine control: not called CC=5 SCAR=34.0
Pod documented
266 0 my $self = shift;
267 0 my ($timeout) = @_;
268  
269 # $self->port->modemlines; may be the key to 'it's attached, it's
270 # not attached' stuff
271  
272 0 my $line = $self->expect("\r", $timeout);
273 0 ▶ return unless $line;
Branch: 1 branch
BranchTrueFalseTotal
unless $line 0 0 0
274  
275 0 ▶ print "# control '$line'\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 0 0
276  
277 0 ▶ if ( my $state = $self->current_state ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $state = $self->current_state) 0 0 0
278 0 $state->handle( $line );
279 0 return;
280 }
281  
282 0 ▶ if ($line =~ /EAAI/) { # top level menu
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ /EAAI/) 0 0 0
283 0 $self->enter_state( 'Menu', data => $self->menu );
284 0 return;
285 }
286  
287 0 warn "control got unexpected '$line'\n";
288 }
289  
290 1 1;
291 __END__
« AccessoryMenu/Slider.pm ↑ Summary AccessoryMenu/State.pm »