Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
727 rain-er 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
#
43
###############################################################################
44
 
45
$Version{'libjoystick.pl'} = "0.0.1 - 2009-12-06";
46
 
47
# Packages
48
use threads;                # http://search.cpan.org/~jdhedden/threads-1.72/threads.pm
49
                            # http://perldoc.perl.org/threads.html
50
use threads::shared;        # http://search.cpan.org/~jdhedden/threads-shared-1.28/shared.pm
51
use Time::HiRes qw(usleep); # http://search.cpan.org/~jhi/Time-HiRes-1.9719/HiRes.pm
52
use Win32::MultiMedia::Joystick;  # http://aspn.activestate.com/ASPN/CodeDoc/Win32-MultiMedia/Joystick/Joystick.html
53
 
54
# Hashes exported to other threads and main-program
55
share (%Stick);
56
 
57
my $StickNum = "JOY1";
58
my $StickRange = 1024;      # global stick range
59
 
60
$Stick{'StickRange'} = $StickRange;
61
$Stick{'JoystickX'} = 512;
62
$Stick{'JoystickY'} = 512;
63
$Stick{'JoystickZ'} = 512;
64
$Stick{'JoystickR'} = 512;
65
$Stick{'JoystickU'} = 512;
66
$Stick{'JoystickV'} = 512;
67
$Stick{'JoystickButton'} = 0;
68
$Stick{'JoystickPov'} = 0xffff;
69
$Stick{'_JoystickTimestamp'} = time;
70
 
71
my $Joystick = Win32::MultiMedia::Joystick->new($StickNum);
72
if ( defined $Joystick )
73
   {
74
    $StickNumAxes = $Joystick->NumAxes;
75
    $StickNumButtons = $Joystick->NumButtons;
76
 
77
    $StickXmin = $Joystick->Xmin;
78
    $StickXmax = $Joystick->Xmax;
79
    $StickYmin = $Joystick->Ymin;
80
    $StickYmax = $Joystick->Ymax;
81
    $StickZmin = $Joystick->Zmin;
82
    $StickZmax = $Joystick->Zmax;
83
 
84
    $StickRmin = $Joystick->Rmin;
85
    $StickRmax = $Joystick->Rmax;
86
    $StickUmin = $Joystick->Umin;
87
    $StickUmax = $Joystick->Umax;
88
    $StickVmin = $Joystick->Vmin;
89
    $StickVmax = $Joystick->Vmax;
90
 
91
    $Stick{'JoystickAxes'}       = $StickNumAxes;
92
    $Stick{'JoystickNumButtons'} = $StickNumButtons;
93
    $Stick{'_JoystickTimestamp'} = time;
94
    }
95
 
96
 
97
sub Joystick()
98
    {
99
    while ( usleep (10000) )   # 10ms loop
100
        {
101
        if ( defined $Joystick  and  $Stick{'JoystickAxes'} > 0)
102
            {
103
            $Joystick->update;
104
 
105
            my $x = $Joystick->X;
106
            my $y = $Joystick->Y;
107
            my $z = $Joystick->Z;
108
            my $r = $Joystick->R;
109
            my $u = $Joystick->U;
110
            my $v = $Joystick->V;    
111
            my $Button = $Joystick->Buttons;
112
            my $Pov = $Joystick->POV;
113
 
114
            lock (%Stick);   # until end of block
115
 
116
            $Stick{'JoystickX'} = int ($x / ($StickXmax - $StickXmin) * $StickRange + 0.5);
117
            $Stick{'JoystickY'} = $StickRange - int ($y / ($StickYmax - $StickYmin) * $StickRange + 0.5);
118
            $Stick{'JoystickZ'} = $StickRange - int ($z / ($StickYmax - $StickYmin) * $StickRange + 0.5);
119
            $Stick{'JoystickR'} = int ($r / ($StickRmax - $StickRmin) * $StickRange + 0.5);
120
            $Stick{'JoystickU'} = int ($u / ($StickUmax - $StickUmin) * $StickRange + 0.5);
121
            $Stick{'JoystickV'} = int ($v / ($StickVmax - $StickVmin) * $StickRange + 0.5);
122
            $Stick{'JoystickButton'} = $Button;
123
            $Stick{'JoystickPov'} = $Pov;
124
            $Stick{'_JoystickTimestamp'} = time;
125
            }
126
        }
127
    }
128
 
129
 
130
sub JoystickStop ()
131
    {
132
    # Nothing to do
133
    }
134
 
135
 
136
# check, if button "Num" pressed, Num = 0 .. n
137
sub JoystickButton()
138
    {
139
    my ($Num) = @_;
140
 
141
    return (($Stick{'JoystickButton'} >> $Num) & 1) == 1;
142
    }
143
 
144
1;
145
 
146
__END__