~ubuntu-branches/debian/jessie/python-expyriment/jessie

« back to all changes in this revision

Viewing changes to expyriment/io/_gamepad.py

  • Committer: Package Import Robot
  • Author(s): Oliver Lindemann
  • Date: 2014-03-26 14:35:33 UTC
  • Revision ID: package-import@ubuntu.com-20140326143533-w5emkoy0zq64ybfn
Tags: upstream-0.7.0+git34-g55a4e7e
ImportĀ upstreamĀ versionĀ 0.7.0+git34-g55a4e7e

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
"""A gamepad.
 
2
 
 
3
This module contains a class implementing a pygame gamepad.
 
4
 
 
5
"""
 
6
 
 
7
__author__ = 'Florian Krause <florian@expyriment.org>, \
 
8
Oliver Lindemann <oliver@expyriment.org>'
 
9
__version__ = '0.7.0'
 
10
__revision__ = '55a4e7e'
 
11
__date__ = 'Wed Mar 26 14:33:37 2014 +0100'
 
12
 
 
13
 
 
14
import pygame
 
15
import time
 
16
import expyriment
 
17
from expyriment.misc._timer import get_time
 
18
from _keyboard import Keyboard
 
19
from  _input_output import Input, Output
 
20
 
 
21
 
 
22
pygame.joystick.init()
 
23
 
 
24
class GamePad(Input, Output):
 
25
    """A class for creating gamepad/joystick input."""
 
26
 
 
27
    @staticmethod
 
28
    def get_gampad_count():
 
29
        """Get the number of gamepads/joysticks connected to the system."""
 
30
 
 
31
        return pygame.joystick.get_count()
 
32
 
 
33
    def __init__(self, gamepad_id, track_button_events=True,
 
34
                 track_motion_events=False):
 
35
        """Create a gamepad/joystick input.
 
36
 
 
37
        Parameters
 
38
        ----------
 
39
        gamepad_id : int
 
40
            id of the gamepad
 
41
        track_button_events : bool, optional
 
42
            Track button events (default=True)
 
43
        track_motion_events : bool, optional
 
44
            Track motion events (default=False)
 
45
 
 
46
        """
 
47
 
 
48
        if not expyriment._active_exp.is_initialized:
 
49
            raise RuntimeError(
 
50
                "Cannot create GamePad before expyriment.initialize()!")
 
51
        Input.__init__(self)
 
52
        Output.__init__(self)
 
53
        self.track_button_events = track_button_events
 
54
        self.track_motion_events = track_motion_events
 
55
        self._joystick = pygame.joystick.Joystick(gamepad_id)
 
56
        self._joystick.init()
 
57
 
 
58
    @property
 
59
    def track_button_events(self):
 
60
        """Getter for track_button_events."""
 
61
 
 
62
        return self._track_button_events
 
63
 
 
64
    @track_button_events.setter
 
65
    def track_button_events(self, value):
 
66
        """Setter for track_button_events.
 
67
 
 
68
        Switch on/off the processing of button events.
 
69
 
 
70
        """
 
71
 
 
72
        self._track_button_events = value
 
73
        if value:
 
74
            pygame.event.set_allowed(pygame.JOYBUTTONDOWN)
 
75
            pygame.event.set_allowed(pygame.JOYBUTTONUP)
 
76
        else:
 
77
            pygame.event.set_blocked(pygame.JOYBUTTONDOWN)
 
78
            pygame.event.set_blocked(pygame.JOYBUTTONUP)
 
79
 
 
80
    @property
 
81
    def track_motion_events(self):
 
82
        """Getter for track_motion_events."""
 
83
 
 
84
        return self._track_motion_events
 
85
 
 
86
    @track_motion_events.setter
 
87
    def track_motion_events(self, value):
 
88
        """Setter for track_motion_events.
 
89
 
 
90
        Switch on/off the processing of motion events.
 
91
 
 
92
        """
 
93
 
 
94
        self._track_motion_events = value
 
95
        if value:
 
96
            pygame.event.set_allowed(pygame.JOYAXISMOTION)
 
97
            pygame.event.set_allowed(pygame.JOYBALLMOTION)
 
98
            pygame.event.set_allowed(pygame.JOYHATMOTION)
 
99
        else:
 
100
            pygame.event.set_blocked(pygame.JOYAXISMOTION)
 
101
            pygame.event.set_blocked(pygame.JOYBALLMOTION)
 
102
            pygame.event.set_blocked(pygame.JOYHATMOTION)
 
103
 
 
104
    @property
 
105
    def id(self):
 
106
        """Getter for id."""
 
107
 
 
108
        return self._joystick.get_id()
 
109
 
 
110
    @property
 
111
    def name(self):
 
112
        """Getter for name."""
 
113
 
 
114
        return self._joystick.get_name()
 
115
 
 
116
    @property
 
117
    def joystick(self):
 
118
        """Getter for joystick."""
 
119
 
 
120
        return self._joystick
 
121
 
 
122
    def get_numaxes(self):
 
123
        """Get the number of axes."""
 
124
 
 
125
        return self._joystick.get_numaxes()
 
126
 
 
127
    def get_axis(self, axis):
 
128
        """Get current axis state.
 
129
 
 
130
        Parameters
 
131
        ----------
 
132
        axis : int
 
133
            axis to get the current state from
 
134
 
 
135
        """
 
136
 
 
137
        pygame.event.pump()
 
138
        return self._joystick.get_axis(axis)
 
139
 
 
140
    def get_numballs(self):
 
141
        """Get the number of balls."""
 
142
 
 
143
        return self._joystick.get_numballs()
 
144
 
 
145
    def get_ball(self, ball):
 
146
        """Get current ball state.
 
147
 
 
148
        Parameters
 
149
        ----------
 
150
        ball : int
 
151
            ball to get the current state from
 
152
 
 
153
        """
 
154
 
 
155
        pygame.event.pump()
 
156
        return self._joystick.get_ball(ball)
 
157
 
 
158
    def get_numbuttons(self):
 
159
        """Get the number of buttons."""
 
160
 
 
161
        return self._joystick.get_numbuttons()
 
162
 
 
163
    def get_button(self, button):
 
164
        """Get current button state.
 
165
 
 
166
        Parameters
 
167
        ----------
 
168
        button : int
 
169
            button to get the current state from
 
170
 
 
171
        """
 
172
 
 
173
        pygame.event.pump()
 
174
        return self._joystick.get_button(button)
 
175
 
 
176
    def get_numhats(self):
 
177
        """Get the number of hats."""
 
178
 
 
179
        return self._joystick.get_numhats()
 
180
 
 
181
    def get_hat(self, hat):
 
182
        """Get current hat state.
 
183
 
 
184
        Parameters
 
185
        ----------
 
186
        hat : int
 
187
            hat to get the current state from
 
188
 
 
189
        """
 
190
 
 
191
        pygame.event.pump()
 
192
        return self._joystick.get_hat(hat)
 
193
 
 
194
 
 
195
    def clear(self):
 
196
        """Clear gamepad events from cue."""
 
197
 
 
198
        pygame.event.clear(pygame.JOYBUTTONDOWN)
 
199
        pygame.event.clear(pygame.JOYBUTTONUP)
 
200
        pygame.event.clear(pygame.JOYAXISMOTION)
 
201
        pygame.event.clear(pygame.JOYBALLMOTION)
 
202
        pygame.event.clear(pygame.JOYHATMOTION)
 
203
        if self._logging:
 
204
            expyriment._active_exp._event_file_log("GamePad,cleared", 2)
 
205
 
 
206
    def wait_press(self, buttons=None, duration=None):
 
207
        """Wait for gamepad button press.
 
208
 
 
209
        Returns the found button and the reaction time.
 
210
 
 
211
        Parameters
 
212
        ----------
 
213
        buttons : int or list, optional
 
214
            specific buttons to wait for
 
215
        duration : int, optional
 
216
            maximal time to wait in ms
 
217
 
 
218
        Returns
 
219
        -------
 
220
        button : int
 
221
            button _id of the pressed button
 
222
        rt : int
 
223
            reaction time in ms
 
224
 
 
225
        """
 
226
 
 
227
        start = get_time()
 
228
        rt = None
 
229
        _button = None
 
230
        self.clear()
 
231
        if buttons is None:
 
232
            buttons = range(self.get_numbuttons())
 
233
        if type(buttons) is not list:
 
234
            buttons = [buttons]
 
235
        done = False
 
236
        while not done:
 
237
            expyriment._active_exp._execute_wait_callback()
 
238
            for button in buttons:
 
239
                if self.get_button(button):
 
240
                    _button = button
 
241
                    rt = int((get_time() - start) * 1000)
 
242
                    done = True
 
243
                    break
 
244
                if _button is not None or Keyboard.process_control_keys():
 
245
                    done = True
 
246
                    break
 
247
                if duration:
 
248
                    if int((get_time() - start) * 1000) >= duration:
 
249
                        done = True
 
250
                        break
 
251
 
 
252
            time.sleep(0.0005)
 
253
 
 
254
        if self._logging:
 
255
            expyriment._active_exp._event_file_log(
 
256
                            "Gamepad,received,{0},wait_press".format(_button))
 
257
        return _button, rt