File Coverage

blib/lib/Device/USB/MissileLauncher.pm
Criterion Covered Total %
statement 7 9 77.7
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 10 12 83.3


line stmt bran cond sub pod time code
1              
2             package Device::USB::MissileLauncher;
3              
4 1     1   58548 use strict;
  1         2  
  1         276  
5 1     1   8 use warnings;
  1         2  
  1         33  
6              
7 1     1   1869 use Device::USB;
  0            
  0            
8             #use Data::Dumper;
9              
10             our $VERSION = '0.03';
11             our $timeout = 1000;
12              
13             sub new {
14             my $class = shift;
15             my $usb = Device::USB->new();
16             my $dev = $usb->find_device(0x1130,0x0202);
17             #my $inita = join ('', map { chr $_ } (85, 83, 66, 67, 0, 0, 4, 0)); # 8 bytes
18             #my $initb = join ('', map { chr $_ } (85, 83, 66, 67, 0, 64, 2, 0));
19             $dev->open() || die "$!";
20             $dev->set_configuration(1);
21             $dev->claim_interface(0);
22             $dev->claim_interface(1);
23             my $self = {};
24             $self->{dev} = $dev;
25             #$self->{dev}->control_msg(0x21,9,0x2,0x01,$inita,8,$timeout);
26             #sleep 1;
27             #$self->{dev}->control_msg(0x21,9,0x2,0x01,$initb,8,$timeout);
28             #sleep 1;
29             #print STDERR "ok\n";
30             return bless $self, $class;
31             }
32              
33             sub do {
34             my $self = shift;
35             my $command = shift;
36              
37             my $command_string = {};
38              
39             my $inita = join ('', map { chr $_ } (85, 83, 66, 67, 0, 0, 4, 0)); # 8 bytes
40             my $initb = join ('', map { chr $_ } (85, 83, 66, 67, 0, 64, 2, 0));
41              
42              
43             my $command_fill = join ('', map { chr $_ } ( 8, 8,
44             0, 0, 0, 0, 0, 0, 0, 0,
45             0, 0, 0, 0, 0, 0, 0, 0,
46             0, 0, 0, 0, 0, 0, 0, 0,
47             0, 0, 0, 0, 0, 0, 0, 0,
48             0, 0, 0, 0, 0, 0, 0, 0,
49             0, 0, 0, 0, 0, 0, 0, 0,
50             0, 0, 0, 0, 0, 0, 0, 0)); # 58 bytes
51              
52             # command_string entries are 6 + 58 (64) bytes long.
53             $command_string->{stop} = join('', map { chr $_ } ( 0, 0, 0, 0, 0, 0)).$command_fill;
54             $command_string->{left} = join('', map { chr $_ } ( 0, 1, 0, 0, 0, 0)).$command_fill;
55             $command_string->{right} = join('', map { chr $_ } ( 0, 0, 1, 0, 0, 0)).$command_fill;
56             $command_string->{up} = join('', map { chr $_ } ( 0, 0, 0, 1, 0, 0)).$command_fill;
57             $command_string->{down} = join('', map { chr $_ } ( 0, 0, 0, 0, 1, 0)).$command_fill;
58             $command_string->{leftup} = join('', map { chr $_ } ( 0, 1, 0, 1, 0, 0)).$command_fill;
59             $command_string->{rightup} = join('', map { chr $_ } ( 0, 0, 1, 1, 0, 0)).$command_fill;
60             $command_string->{leftdown} = join('', map { chr $_ } ( 0, 1, 0, 0, 1, 0)).$command_fill;
61             $command_string->{rightdown} = join('', map { chr $_ } ( 0, 0, 1, 0, 1, 0)).$command_fill;
62             $command_string->{fire} = join('', map { chr $_ } ( 0, 0, 0, 0, 0, 1)).$command_fill;
63              
64             return -1 unless exists $command_string->{$command};
65             #print STDERR $command,"\n";
66              
67             $self->{dev}->control_msg(0x21,9,0x2,0x01,$inita,8,$timeout);
68             sleep 1;
69             $self->{dev}->control_msg(0x21,9,0x2,0x01,$initb,8,$timeout);
70             sleep 1;
71              
72             $self->{dev}->control_msg(0x21,9,0x2,0x0,$command_string->{$command},64,$timeout);
73             }
74              
75             1;
76              
77             =head1 NAME
78              
79             Device::USB::MissileLauncher - interface to toy USB missile launchers
80              
81             =head1 SYNOPSIS
82              
83             use Device::USB::MissileLauncher;
84             my $ml = Device::USB::MissileLauncher->new();
85             $ml->do('left');
86             $ml->do('up');
87             $ml->do('fire');
88              
89             =head1 DESCRIPTION
90              
91             This implements a basic interface to the toy USB missile launchers that were on sale
92             in Marks and Spencers Christmas 2005 and later at 'I want one of those'.
93              
94             It has two methods - new() and do(). do() takes a string out of the following list.
95              
96             stop
97             left
98             right
99             up
100             down
101             leftup
102             rightup
103             leftdown
104             rightdown
105             fire
106              
107             =head1 THANKS
108              
109             Ian Jeffray published some C code to work with the Missile Launcher at
110             http://ian.jeffray.co.uk/linux/ and Scott Weston published some Python
111             code at http://scott.weston.id.au/software/pymissile-20060126/ , both
112             sets of code helped a lot when I was working out how to control the toy.
113              
114             Jonathan Stowe also helped with the debugging to get v0.03 out, he spotted
115             the C code sending the init strings everytime.
116              
117             =head1 AUTHOR
118              
119             Greg McCarroll
120              
121             =head1 COPYRIGHT
122              
123             Copyright 2006 Greg McCarroll. All Rights Reserved.
124              
125             This program is free software; you can redistribute it and/or modify it under the
126             same terms as Perl itself.
127              
128             =head1 SEE ALSO
129              
130             Device::USB
131              
132             =cut