3
This module contains a class implementing a pygame gamepad.
7
__author__ = 'Florian Krause <florian@expyriment.org>, \
8
Oliver Lindemann <oliver@expyriment.org>'
10
__revision__ = '55a4e7e'
11
__date__ = 'Wed Mar 26 14:33:37 2014 +0100'
17
from expyriment.misc._timer import get_time
18
from _keyboard import Keyboard
19
from _input_output import Input, Output
22
pygame.joystick.init()
24
class GamePad(Input, Output):
25
"""A class for creating gamepad/joystick input."""
28
def get_gampad_count():
29
"""Get the number of gamepads/joysticks connected to the system."""
31
return pygame.joystick.get_count()
33
def __init__(self, gamepad_id, track_button_events=True,
34
track_motion_events=False):
35
"""Create a gamepad/joystick input.
41
track_button_events : bool, optional
42
Track button events (default=True)
43
track_motion_events : bool, optional
44
Track motion events (default=False)
48
if not expyriment._active_exp.is_initialized:
50
"Cannot create GamePad before expyriment.initialize()!")
53
self.track_button_events = track_button_events
54
self.track_motion_events = track_motion_events
55
self._joystick = pygame.joystick.Joystick(gamepad_id)
59
def track_button_events(self):
60
"""Getter for track_button_events."""
62
return self._track_button_events
64
@track_button_events.setter
65
def track_button_events(self, value):
66
"""Setter for track_button_events.
68
Switch on/off the processing of button events.
72
self._track_button_events = value
74
pygame.event.set_allowed(pygame.JOYBUTTONDOWN)
75
pygame.event.set_allowed(pygame.JOYBUTTONUP)
77
pygame.event.set_blocked(pygame.JOYBUTTONDOWN)
78
pygame.event.set_blocked(pygame.JOYBUTTONUP)
81
def track_motion_events(self):
82
"""Getter for track_motion_events."""
84
return self._track_motion_events
86
@track_motion_events.setter
87
def track_motion_events(self, value):
88
"""Setter for track_motion_events.
90
Switch on/off the processing of motion events.
94
self._track_motion_events = value
96
pygame.event.set_allowed(pygame.JOYAXISMOTION)
97
pygame.event.set_allowed(pygame.JOYBALLMOTION)
98
pygame.event.set_allowed(pygame.JOYHATMOTION)
100
pygame.event.set_blocked(pygame.JOYAXISMOTION)
101
pygame.event.set_blocked(pygame.JOYBALLMOTION)
102
pygame.event.set_blocked(pygame.JOYHATMOTION)
108
return self._joystick.get_id()
112
"""Getter for name."""
114
return self._joystick.get_name()
118
"""Getter for joystick."""
120
return self._joystick
122
def get_numaxes(self):
123
"""Get the number of axes."""
125
return self._joystick.get_numaxes()
127
def get_axis(self, axis):
128
"""Get current axis state.
133
axis to get the current state from
138
return self._joystick.get_axis(axis)
140
def get_numballs(self):
141
"""Get the number of balls."""
143
return self._joystick.get_numballs()
145
def get_ball(self, ball):
146
"""Get current ball state.
151
ball to get the current state from
156
return self._joystick.get_ball(ball)
158
def get_numbuttons(self):
159
"""Get the number of buttons."""
161
return self._joystick.get_numbuttons()
163
def get_button(self, button):
164
"""Get current button state.
169
button to get the current state from
174
return self._joystick.get_button(button)
176
def get_numhats(self):
177
"""Get the number of hats."""
179
return self._joystick.get_numhats()
181
def get_hat(self, hat):
182
"""Get current hat state.
187
hat to get the current state from
192
return self._joystick.get_hat(hat)
196
"""Clear gamepad events from cue."""
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)
204
expyriment._active_exp._event_file_log("GamePad,cleared", 2)
206
def wait_press(self, buttons=None, duration=None):
207
"""Wait for gamepad button press.
209
Returns the found button and the reaction time.
213
buttons : int or list, optional
214
specific buttons to wait for
215
duration : int, optional
216
maximal time to wait in ms
221
button _id of the pressed button
232
buttons = range(self.get_numbuttons())
233
if type(buttons) is not list:
237
expyriment._active_exp._execute_wait_callback()
238
for button in buttons:
239
if self.get_button(button):
241
rt = int((get_time() - start) * 1000)
244
if _button is not None or Keyboard.process_control_keys():
248
if int((get_time() - start) * 1000) >= duration:
255
expyriment._active_exp._event_file_log(
256
"Gamepad,received,{0},wait_press".format(_button))