#!/usr/bin/perl -w
# 
# This file is licensed under the GPL.  Please see the file "LICENSE" in
# the root directory of this project for terms and conditions.
# (c) 2001 by Trammell Hudson <hudson@swcp.com>
#
use strict;
use FindBin;
use lib "$FindBin::Bin/lib";

use Tk;
use Joystick;

my $version = q$Id: calibrate,v 1.3 2001/10/05 16:21:17 tramm Exp $;


my $mw = MainWindow->new(
	-title		=> 'Calibrate joystick',
);

my $js = new Joystick
	or die "Unable to open joystick: $!\n";

$mw->fileevent( $js->{dev}, readable => sub { $js->update } );

my @joystick;

my $f = $mw->Frame(
)->pack(
	-side		=> 'top',
	-expand		=> 0,
	-fill		=> 'both',
);

for my $axis_num (0..$js->axes-1)
{
	my $axis	= $joystick[ $axis_num ] ||= {
		value		=> 0,
		min		=> 0,
		max		=> 0,
	};

	my $f		= $f->Frame(
	)->pack(
		-side		=> 'left',
		-expand		=> 0,
		-fill		=> 'both',
	);

	$f->Scale(
		-orient		=> 'vertical',
		-showvalue	=> 0,
		-to		=> -32768,
		-from		=> 32768,
		-variable	=> \$axis->{value},
	)->pack(
		-side		=> 'left',
		-expand		=> 1,
		-fill		=> 'y',
	);

	$f->Label(
		-textvariable	=> \$axis->{max},
		-width		=> 10,
		-justify	=> 'right',
	)->pack(
		-anchor		=> 'ne',
		-side		=> 'top',
	);

	$f->Label(
		-textvariable	=> \$axis->{value},
		-width		=> 10,
		-justify	=> 'right',
	)->pack(
		-anchor		=> 'ne',
		-side		=> 'top',
	);


	$f->Label(
		-textvariable	=> \$axis->{min},
		-width		=> 10,
		-justify	=> 'right',
	)->pack(
		-anchor		=> 'ne',
		-side		=> 'top',
	);


	$js->watch_axis( $axis_num, sub {
		my $js		= shift;
		my $new_value	= shift;
		$axis->{value}	= $new_value;
		$axis->{max}	= $new_value if $new_value > $axis->{max};
		$axis->{min}	= $new_value if $new_value < $axis->{min};
	} );
}


$f = $mw->Frame(
)->pack(
	-side		=> 'top',
	-expand		=> 0,
	-fill		=> 'both',
);

for my $button (0..$js->buttons-1)
{
	my $value	= 0;

	$f->Checkbutton(
		-text		=> $button,
		-variable	=> \$value,
	)->pack(
		-side		=> 'left',
	);

	$js->watch_button( $button, sub {
		my $js		= shift;
		my $new_value	= shift;
		$value = $new_value;
	} );
}

$mw->Button(
	-text		=> 'Quit',
	-command	=> sub { $mw->destroy },
)->pack(
	-side		=> 'bottom',
	-expand		=> 1,
	-fill		=> 'x',
);

MainLoop;

# Now to print out the results
print "my %axis = (\n";

for my $axis ( @joystick )
{
	print <<"";
	{
		max	=> $axis->{max},
		min	=> $axis->{min},
		trim	=> $axis->{value},
	},

}

print ");\n";



__END__
