Ejemplo n.º 1
0
    def controller_states(self):
        cls = self.__class__
        controller = self.controller
        while True:
            for _ in sdl2.ext.get_events():
                pass
            elaped_time = dt.datetime.now().timestamp(
            ) - self.start_dttm.timestamp()
            buttons = sum((sdl2.SDL_GameControllerGetButton(controller, b) << n
                           for n, b in enumerate(cls.buttonmapping)))
            buttons |= (abs(
                sdl2.SDL_GameControllerGetAxis(
                    controller, sdl2.SDL_CONTROLLER_AXIS_TRIGGERLEFT)) >
                        cls.trigger_deadzone) << 6
            buttons |= (abs(
                sdl2.SDL_GameControllerGetAxis(
                    controller, sdl2.SDL_CONTROLLER_AXIS_TRIGGERRIGHT)) >
                        cls.trigger_deadzone) << 7

            hat = cls.hatcodes[sum([
                sdl2.SDL_GameControllerGetButton(controller, b) << n
                for n, b in enumerate(cls.hatmapping)
            ])]

            rawaxis = [
                sdl2.SDL_GameControllerGetAxis(controller, n)
                for n in cls.axismapping
            ]
            axis = [((0 if abs(x) < cls.axis_deadzone else x) >> 8) + 128
                    for x in rawaxis]

            rawbytes = struct.pack('>BHBBBB', hat, buttons, *axis)
            message_stamp = ControllerStateTime(rawbytes, elaped_time)
            yield message_stamp
Ejemplo n.º 2
0
def controller_states(controller_id):

    sdl2.SDL_Init(sdl2.SDL_INIT_GAMECONTROLLER)

    controller = get_controller(controller_id)

    try:
        print('Using "{:s}" for input.'.format(
            sdl2.SDL_JoystickName(sdl2.SDL_GameControllerGetJoystick(controller)).decode('utf8')))
    except AttributeError:
        print('Using controller {:s} for input.'.format(controller_id))

    while True:
        elaped_time = dt.datetime.now().timestamp() - start_dttm
        buttons = sum([sdl2.SDL_GameControllerGetButton(controller, b) << n for n, b in enumerate(buttonmapping)])
        buttons |= (abs(sdl2.SDL_GameControllerGetAxis(controller, sdl2.SDL_CONTROLLER_AXIS_TRIGGERLEFT)) > trigger_deadzone) << 6
        buttons |= (abs(sdl2.SDL_GameControllerGetAxis(controller, sdl2.SDL_CONTROLLER_AXIS_TRIGGERRIGHT)) > trigger_deadzone) << 7

        hat = hatcodes[sum([sdl2.SDL_GameControllerGetButton(controller, b) << n for n, b in enumerate(hatmapping)])]

        rawaxis = [sdl2.SDL_GameControllerGetAxis(controller, n) for n in axismapping]
        axis = [((0 if abs(x) < axis_deadzone else x) >> 8) + 128 for x in rawaxis]

        rawbytes = struct.pack('>BHBBBB', hat, buttons, *axis)
        message_stamp = ControllerStateTime(rawbytes, elaped_time)
        yield message_stamp
Ejemplo n.º 3
0
    def __next__(self):
        buttons = sum([
            sdl2.SDL_GameControllerGetButton(self.controller, b) << n
            for n, b in enumerate(Controller.buttonmapping)
        ])
        buttons |= (abs(
            sdl2.SDL_GameControllerGetAxis(
                self.controller, sdl2.SDL_CONTROLLER_AXIS_TRIGGERLEFT)) >
                    self.trigger_deadzone) << 6
        buttons |= (abs(
            sdl2.SDL_GameControllerGetAxis(
                self.controller, sdl2.SDL_CONTROLLER_AXIS_TRIGGERRIGHT)) >
                    self.trigger_deadzone) << 7

        hat = Controller.hatcodes[sum([
            sdl2.SDL_GameControllerGetButton(self.controller, b) << n
            for n, b in enumerate(Controller.hatmapping)
        ])]

        rawaxis = [
            sdl2.SDL_GameControllerGetAxis(self.controller, n)
            for n in Controller.axismapping
        ]
        axis = [((0 if abs(x) < self.axis_deadzone else x) >> 8) + 128
                for x in rawaxis]

        state = State(buttons, hat, *axis)
        # TODO: quantize
        self.previous_state = state
        return state
Ejemplo n.º 4
0
    def update(self):
        if not self._connected:
            return

        for btn_index in range(0, self._num_buttons):
            self._button_pressed[btn_index] = 0

            is_down = sdl2.SDL_GameControllerGetButton(self._sdl_controller, btn_index)
            if is_down and not self._button_down[btn_index]:
                self._button_pressed[btn_index] = True

            self._button_down[btn_index] = is_down
Ejemplo n.º 5
0
right_buttons = 0
right_touched = 0

while True:
    sdl2.SDL_PumpEvents()

    # Controller assignments:
    # Controller1 (right)
    # SDL_CONTROLLER_AXIS_RIGHTX
    # SDL_CONTROLLER_AXIS_RIGHTY
    # SDL_CONTROLLER_BUTTON_RIGHTSTICK
    # SDL_CONTROLLER_AXIS_TRIGGERRIGHT
    # SDL_CONTROLLER_BUTTON_RIGHTSHOULDER
    # SDL_CONTROLLER_BUTTON_GUIDE

    if sdl2.SDL_GameControllerGetButton(gamecontroller,
                                        sdl2.SDL_CONTROLLER_BUTTON_RIGHTSTICK):
        right_buttons |= (1 << 0)  # System
    else:
        right_buttons &= ~(1 << 0)
    if sdl2.SDL_GameControllerGetButton(gamecontroller,
                                        sdl2.SDL_CONTROLLER_BUTTON_GUIDE):
        right_buttons |= (1 << 1)  # Menu
    else:
        right_buttons &= ~(1 << 1)
    if sdl2.SDL_GameControllerGetButton(
            gamecontroller, sdl2.SDL_CONTROLLER_BUTTON_RIGHTSHOULDER):
        right_buttons |= (1 << 2)  # Grip
    else:
        right_buttons &= ~(1 << 2)

    right_pitch = sdl2.SDL_GameControllerGetAxis(