Subject: | No support for other missile launchers, specificly the Rocket Baby |
I have a Rocket Baby from Dream Cheeky and it is not supported in the
current version.
Attached is a new version of MissileLauncher.pm that supports my
launcher in addition to the other. I don't have one of the other
launchers, so I can't tell if I broke anything, but I don't think I did.
I got most of the information about it from here, and playing with it.
http://dgwilson.wordpress.com/2007/12/31/usb-missile-launcger-1410-release/
I am also attaching the script I use to aim and fire it.
Subject: | missile_command.pl |
#!/usr/bin/perl
use strict;
use warnings;
use Term::ReadKey;
ReadMode 3;
use lib './lib';
use Device::USB::MissileLauncher;
my %commands = (
s => 'stop',
j => 'left',
l => 'right',
i => 'up',
k => 'down',
q{ } => 'fire',
);
my $ml = Device::USB::MissileLauncher->new();
sub END {
$ml->do('stop');
ReadMode 0;
}
$SIG{ALRM} = sub { $ml->do('stop'); };
KEY: while ( my $key = ReadKey ) {
exit if $key eq 'q';
if ( $commands{$key} ) {
alarm 1;
$ml->do( $commands{$key} );
}
}
Subject: | MissileLauncher.pm |
package Device::USB::MissileLauncher;
use strict;
use warnings;
use Carp qw/ croak /;
use Device::USB;
#use Data::Dumper;
our $VERSION = '0.04';
our $timeout = 1000;
my %devices = (
0x1130 => {
0x0202 => {
init => [
join( '', map { chr $_ } ( 85, 83, 66, 67, 0, 0, 4, 0 ) ),
join( '', map { chr $_ } ( 85, 83, 66, 67, 0, 64, 2, 0 ) ),
],
commands => {
stop => join( '', map { chr $_ } ( 0, 0, 0, 0, 0, 0 ) ),
left => join( '', map { chr $_ } ( 0, 1, 0, 0, 0, 0 ) ),
right => join( '', map { chr $_ } ( 0, 0, 1, 0, 0, 0 ) ),
up => join( '', map { chr $_ } ( 0, 0, 0, 1, 0, 0 ) ),
down => join( '', map { chr $_ } ( 0, 0, 0, 0, 1, 0 ) ),
leftup => join( '', map { chr $_ } ( 0, 1, 0, 1, 0, 0 ) ),
rightup => join( '', map { chr $_ } ( 0, 0, 1, 1, 0, 0 ) ),
leftdown => join( '', map { chr $_ } ( 0, 1, 0, 0, 1, 0 ) ),
rightdown => join( '', map { chr $_ } ( 0, 0, 1, 0, 1, 0 ) ),
fire => join( '', map { chr $_ } ( 0, 0, 0, 0, 0, 1 ) ),
},
command_fill => join(
'',
map { chr $_ } (
8, 8,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0
)
),
},
},
# Rocket Baby
0x0a81 => {
# Rocket Baby
0x0701 => {
commands => {
down => chr(0x01),
up => chr(0x02),
left => chr(0x04),
right => chr(0x08),
fire => chr(0x10),
stop => chr(0x20),
status => chr(0x40),
},
},
},
);
sub new {
my $class = shift;
my $usb = Device::USB->new();
my $self = {};
DEV: foreach my $vendor ( keys %devices ) {
foreach my $device ( keys %{ $devices{$vendor} } ) {
my $dev = $usb->find_device( $vendor, $device );
if ($dev) {
$dev->open() || die "$!";
$dev->set_configuration(1);
$dev->claim_interface(0);
$dev->claim_interface(1);
$self->{dev} = $dev;
$self->{vendor} = $vendor;
$self->{device} = $device;
last DEV;
}
}
}
if ( !$self->{dev} ) {
croak "Couldn't find any supported devices!";
}
return bless $self, $class;
}
sub do {
my $self = shift;
my $command = shift;
my $c = $devices{ $self->{vendor} }->{ $self->{device} };
croak "Invalid command [$command]" unless exists $c->{commands}->{$command};
if ( exists $c->{init} ) {
foreach my $i ( @{ $c->{init} } ) {
$self->{dev}
->control_msg( 0x21, 9, 0x2, 0x01, $i, length($i), $timeout );
sleep 1;
}
}
my $cs = $c->{commands}->{$command};
if ( $c->{command_fill} ) {
$cs .= $c->{command_fill};
}
$self->{dev}->control_msg( 0x21, 9, 0x2, 0x0, $cs, length($cs), $timeout );
}
1;
=head1 NAME
Device::USB::MissileLauncher - interface to toy USB missile launchers
=head1 SYNOPSIS
use Device::USB::MissileLauncher;
my $ml = Device::USB::MissileLauncher->new();
$ml->do('left');
sleep 1;
$ml->do('up');
sleep 1;
$ml->do('fire');
sleep 5;
$ml->do('stop');
=head1 DESCRIPTION
This implements a basic interface to the toy USB missile launchers that were on sale
in Marks and Spencers Christmas 2005 and later at 'I want one of those'.
It also supports the Rocket Baby Missile Launcher from Dream Cheeky
(http://www.dreamcheeky.com)
It has two methods - new() and do(). do() takes a string out of the following list.
(Not all devices support all actions)
stop
left
right
up
down
leftup
rightup
leftdown
rightdown
fire
=head1 THANKS
Ian Jeffray published some C code to work with the Missile Launcher at
http://ian.jeffray.co.uk/linux/ and Scott Weston published some Python
code at http://scott.weston.id.au/software/pymissile-20060126/ , both
sets of code helped a lot when I was working out how to control the toy.
Jonathan Stowe also helped with the debugging to get v0.03 out, he spotted
the C code sending the init strings everytime.
Andrew Fresh <andrew@cpan.org> for support for the Rocket Baby green and
black missiles. This was possible because of David Wilson's USB Missile
Launcher NZ.
http://dgwilson.wordpress.com/2007/12/31/usb-missile-launcger-1410-release/
=head1 AUTHOR
Greg McCarroll <greg@mccarroll.org.uk>
=head1 COPYRIGHT
Copyright 2006 Greg McCarroll. All Rights Reserved.
This program is free software; you can redistribute it and/or modify it under the
same terms as Perl itself.
=head1 SEE ALSO
Device::USB
=head1 BUGS
Only supports the first device found on the USB chain. It should support
multiple rockets somehow.
=cut