Subversion Repositories Projects

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
823 - 1
#!/usr/bin/perl
2
#!/usr/bin/perl -d:ptkdb
3
 
4
###############################################################################
5
#
6
# libjoystick.pl -  Joystick controls
7
#
8
# Copyright (C) 2009  Rainer Walther  (rainerwalther-mail@web.de)
9
#
10
# Creative Commons Lizenz mit den Zusaetzen (by, nc, sa)
11
#
12
# Es ist Ihnen gestattet: 
13
#     * das Werk vervielfältigen, verbreiten und öffentlich zugänglich machen
14
#     * Abwandlungen bzw. Bearbeitungen des Inhaltes anfertigen
15
# 
16
# Zu den folgenden Bedingungen:
17
#     * Namensnennung.
18
#       Sie müssen den Namen des Autors/Rechteinhabers in der von ihm festgelegten Weise nennen.
19
#     * Keine kommerzielle Nutzung.
20
#       Dieses Werk darf nicht für kommerzielle Zwecke verwendet werden.
21
#     * Weitergabe unter gleichen Bedingungen.
22
#       Wenn Sie den lizenzierten Inhalt bearbeiten oder in anderer Weise umgestalten,
23
#       verändern oder als Grundlage für einen anderen Inhalt verwenden,
24
#       dürfen Sie den neu entstandenen Inhalt nur unter Verwendung von Lizenzbedingungen
25
#       weitergeben, die mit denen dieses Lizenzvertrages identisch oder vergleichbar sind.
26
# 
27
# Im Falle einer Verbreitung müssen Sie anderen die Lizenzbedingungen, unter welche dieses
28
# Werk fällt, mitteilen. Am Einfachsten ist es, einen Link auf diese Seite einzubinden.
29
# 
30
# Jede der vorgenannten Bedingungen kann aufgehoben werden, sofern Sie die Einwilligung
31
# des Rechteinhabers dazu erhalten.
32
# 
33
# Diese Lizenz lässt die Urheberpersönlichkeitsrechte unberührt.
34
# 
35
# Weitere Details zur Lizenzbestimmung gibt es hier:
36
#   Kurzform: http://creativecommons.org/licenses/by-nc-sa/3.0/de/
37
#   Komplett: http://creativecommons.org/licenses/by-nc-sa/3.0/de/legalcode
38
#
39
###############################################################################
40
#
41
# 2009-12-06 0.0.1 rw created
42
# 2010-06-20 0.0.2 rw check hasPOV
43
#
44
###############################################################################
45
 
46
$Version{'libjoystick.pl'} = "0.0.2 - 2010-06-20";
47
 
48
# Packages
49
use threads;                # http://search.cpan.org/~jdhedden/threads-1.72/threads.pm
50
                            # http://perldoc.perl.org/threads.html
51
use threads::shared;        # http://search.cpan.org/~jdhedden/threads-shared-1.28/shared.pm
52
use Time::HiRes qw(usleep); # http://search.cpan.org/~jhi/Time-HiRes-1.9719/HiRes.pm
53
use Win32::MultiMedia::Joystick;  # http://aspn.activestate.com/ASPN/CodeDoc/Win32-MultiMedia/Joystick/Joystick.html
54
 
55
# Hashes exported to other threads and main-program
56
share (%Stick);
57
 
58
my $StickNum = "JOY1";
59
my $StickRange = 1024;      # global stick range
60
 
61
$Stick{'StickRange'} = $StickRange;
62
$Stick{'JoystickX'} = 512;
63
$Stick{'JoystickY'} = 512;
64
$Stick{'JoystickZ'} = 512;
65
$Stick{'JoystickR'} = 512;
66
$Stick{'JoystickU'} = 512;
67
$Stick{'JoystickV'} = 512;
68
$Stick{'JoystickButton'} = 0;
69
$Stick{'JoystickPov'} = 0xffff;
70
$Stick{'_JoystickTimestamp'} = time;
71
 
72
my $Joystick = Win32::MultiMedia::Joystick->new($StickNum);
73
if ( defined $Joystick )
74
   {
75
    $StickNumAxes = $Joystick->NumAxes;
76
    $StickNumButtons = $Joystick->NumButtons;
77
 
78
    $StickXmin = $Joystick->Xmin;
79
    $StickXmax = $Joystick->Xmax;
80
    $StickYmin = $Joystick->Ymin;
81
    $StickYmax = $Joystick->Ymax;
82
    $StickZmin = $Joystick->Zmin;
83
    $StickZmax = $Joystick->Zmax;
84
 
85
    $StickRmin = $Joystick->Rmin;
86
    $StickRmax = $Joystick->Rmax;
87
    $StickUmin = $Joystick->Umin;
88
    $StickUmax = $Joystick->Umax;
89
    $StickVmin = $Joystick->Vmin;
90
    $StickVmax = $Joystick->Vmax;
91
 
92
    $Stick{'JoystickAxes'}       = $StickNumAxes;
93
    $Stick{'JoystickNumButtons'} = $StickNumButtons;
94
    $Stick{'_JoystickTimestamp'} = time;
95
    }
96
 
97
 
98
sub Joystick()
99
    {
100
    while ( usleep (10000) )   # 10ms loop
101
        {
102
        if ( defined $Joystick  and  $Stick{'JoystickAxes'} > 0)
103
            {
104
            $Joystick->update;
105
 
106
            my $x = $Joystick->X;
107
            my $y = $Joystick->Y;
108
            my $z = $Joystick->Z;
109
            my $r = $Joystick->R;
110
            my $u = $Joystick->U;
111
            my $v = $Joystick->V;    
112
            my $Button = $Joystick->Buttons;
113
            my $Pov = $Joystick->POV;
114
 
115
            lock (%Stick);   # until end of block
116
 
117
            $Stick{'JoystickX'} = int ($x / ($StickXmax - $StickXmin) * $StickRange + 0.5);
118
            $Stick{'JoystickY'} = $StickRange - int ($y / ($StickYmax - $StickYmin) * $StickRange + 0.5);
119
            $Stick{'JoystickZ'} = $StickRange - int ($z / ($StickYmax - $StickYmin) * $StickRange + 0.5);
120
            $Stick{'JoystickR'} = int ($r / ($StickRmax - $StickRmin) * $StickRange + 0.5);
121
            $Stick{'JoystickU'} = int ($u / ($StickUmax - $StickUmin) * $StickRange + 0.5);
122
            $Stick{'JoystickV'} = int ($v / ($StickVmax - $StickVmin) * $StickRange + 0.5);
123
            $Stick{'JoystickButton'} = $Button;
124
            if ( $Joystick->hasPOV )
125
                {
126
                $Stick{'JoystickPov'} = $Pov;
127
                }
128
            $Stick{'_JoystickTimestamp'} = time;
129
            }
130
        }
131
    }
132
 
133
 
134
sub JoystickStop ()
135
    {
136
    # Nothing to do
137
    }
138
 
139
 
140
# check, if button "Num" pressed, Num = 0 .. n
141
sub JoystickButton()
142
    {
143
    my ($Num) = @_;
144
 
145
    return (($Stick{'JoystickButton'} >> $Num) & 1) == 1;
146
    }
147
 
148
1;
149
 
150
__END__