def find_joystick(self, printName=True): if not glfw.joystick_present(glfw.JOYSTICK_1): raise LookupError( 'Cannot find controller! Please connect one and ensure it is recognised in slot 0' ) if printName: print('Controller:', self.get_joystick_name())
def find_joysticks(): joylist = [] for joy in range(15): if glfw.joystick_present(joy): axes = glfw.get_joystick_axes(joy) sum = 2. for i in range(axes[1]): sum += axes[0][i] if abs(sum) > .000001: print('Found Joy %d' % (joy + 1)) joylist.append(Joystick(joy)) return joylist
def getNumJoysticks(): """Return a count of the number of joysticks available.""" if backend == 'pyglet': return len(pyglet_input.get_joysticks()) elif backend == 'glfw': n_joys = 0 for joy in range(glfw.JOYSTICK_1, glfw.JOYSTICK_LAST): if glfw.joystick_present(joy): n_joys += 1 return n_joys else: pygame.joystick.init() return pygame.joystick.get_count()
def joystick_callback(self, jid): present = glfw.joystick_present(jid) if present == 1: ax, n_ax = glfw.get_joystick_axes(jid) # TODO: get better threshold for stick drift if abs(ax[0]) > 0.09: # right stick left/right self.action[1] = ax[0] else: self.action[0] = 0 if abs(ax[1]) > 0.09: # right stick up/down self.action[0] = ax[1] else: self.action[1] = 0 if abs(ax[4]) > 0.09: # left stick up/down self.action[2] = -ax[4] else: self.action[2] = 0 butt, n_butt = glfw.get_joystick_buttons(jid) if butt[0] == 1: # X button self.action[3] = 1. if butt[1] == 1: # O button self.action[3] = -1.
[-5.0, -5.0, 0.0], [5.0, -5.0, 0.0], [5.0, 5.0, 0.0], [5.0, 5.0, 0.0], [-5.0, 5.0, 0.0], [-5.0, -5.0, 0.0], ], dtype=np.float32, ) square = Square(r=0.0, g=0.0, b=1.0, position=[0.0, 0.0, 0.0]) square.prepare_to_render() number_of_controllers = glfw.joystick_present(glfw.JOYSTICK_1) @dataclass class Camera: x: float = 0.0 y: float = 0.0 z: float = 0.0 rot_y: float = 0.0 rot_x: float = 0.0 camera = Camera(x=0.0, y=0.0, z=400.0, rot_y=0.0, rot_x=0.0) def handle_inputs():
def __init__(self, id): """An object to control a multi-axis joystick or gamepad. .. note: You do need to be flipping frames (or dispatching events manually) in order for the values of the joystick to be updated. :Known issues: Currently under pyglet backends the axis values initialise to zero rather than reading the current true value. This gets fixed on the first change to each axis. """ self.id = id if backend == 'pyglet': joys = pyglet_input.get_joysticks() if id >= len(joys): logging.error("You don't have that many joysticks attached " "(remember that the first joystick has id=0 " "etc...)") else: self._device = joys[id] self._device.open() self.name = self._device.device.name if len(visual.openWindows) == 0: logging.error( "You need to open a window before creating your joystick") else: for win in visual.openWindows: win()._eventDispatchers.append(self._device.device) elif backend == 'glfw': # We can create a joystick anytime after glfwInit() is called, but # there should be a window open first. # Joystick events are processed when flipping the associated window. if not glfw.init(): logging.error("GLFW could not be initialized. Exiting.") # get all available joysticks, GLFW supports up to 16. joys = [] for joy in range(glfw.JOYSTICK_1, glfw.JOYSTICK_LAST): if glfw.joystick_present(joy): joys.append(joy) # error checks if not joys: # if the list is empty, no joysticks were found error_msg = ("No joysticks were found by the GLFW runtime. " "Check connections and try again.") logging.error(error_msg) raise RuntimeError(error_msg) elif id not in joys: error_msg = ("You don't have that many joysticks attached " "(remember that the first joystick has id=0 " "etc...)") logging.error(error_msg) raise RuntimeError(error_msg) self._device = id # just need the ID for GLFW self.name = glfw.get_joystick_name(self._device).decode("utf-8") if len(visual.openWindows) == 0: logging.error( "You need to open a window before creating your joystick") else: for win in visual.openWindows: # sending the raw ID to the window. win()._eventDispatchers.append(self._device) else: pygame.joystick.init() self._device = pygame.joystick.Joystick(id) self._device.init() self.name = self._device.get_name()
def __init__(self, id): """An object to control a multi-axis joystick or gamepad. .. note: You do need to be flipping frames (or dispatching events manually) in order for the values of the joystick to be updated. :Known issues: Currently under pyglet backends the axis values initialise to zero rather than reading the current true value. This gets fixed on the first change to each axis. """ self.id = id if backend == 'pyglet': joys = pyglet_input.get_joysticks() if id >= len(joys): logging.error("You don't have that many joysticks attached " "(remember that the first joystick has id=0 " "etc...)") else: self._device = joys[id] self._device.open() self.name = self._device.device.name if len(visual.openWindows) == 0: logging.error( "You need to open a window before creating your joystick") else: for win in visual.openWindows: win()._eventDispatchers.append(pyglet_dispatcher) elif backend == 'glfw': # We can create a joystick anytime after glfwInit() is called, but # there should be a window open first. # Joystick events are processed when flipping the associated window. if not glfw.init(): logging.error("GLFW could not be initialized. Exiting.") # get all available joysticks, GLFW supports up to 16. joys = [] for joy in range(glfw.JOYSTICK_1, glfw.JOYSTICK_LAST): if glfw.joystick_present(joy): joys.append(joy) # error checks if not joys: # if the list is empty, no joysticks were found error_msg = ("No joysticks were found by the GLFW runtime. " "Check connections and try again.") logging.error(error_msg) raise RuntimeError(error_msg) elif id not in joys: error_msg = ("You don't have that many joysticks attached " "(remember that the first joystick has id=0 " "etc...)") logging.error(error_msg) raise RuntimeError(error_msg) self._device = id # just need the ID for GLFW self.name = glfw.get_joystick_name(self._device).decode("utf-8") if len(visual.openWindows) == 0: logging.error( "You need to open a window before creating your joystick") else: for win in visual.openWindows: # sending the raw ID to the window. win()._eventDispatchers.append(self._device) else: pygame.joystick.init() self._device = pygame.joystick.Joystick(id) self._device.init() self.name = self._device.get_name()
def present(self): return bool(glfw.joystick_present(self.gamepad_id))
def gamepad_available(i): return glfw.joystick_present(i) == glfw.TRUE and glfw.joystick_is_gamepad( i)
def get_joysticks_impl() -> List[Tuple[int, str]]: joysticks = [glfw.JOYSTICK_1 + i for i in range(16)] joysticks = [j for j in joysticks if glfw.joystick_present(j)] return [(j, glfw.get_joystick_guid(j).decode('utf-8')) for j in joysticks]