Exemple #1
0
def run():
    sdl2.ext.init()
    sdl2.SDL_Init(sdl2.SDL_INIT_JOYSTICK)
    window = sdl2.ext.Window("Controller test", size=(640, 480))
    window.show()
    running = True

    if sdl2.joystick.SDL_NumJoysticks() < 1:
        print("No joysticks plugged in")
        return 0

    joystick = sdl2.SDL_JoystickOpen(0)
    print("Name:", sdl2.SDL_JoystickName(joystick))
    print("NumAxes", sdl2.SDL_JoystickNumAxes(joystick))
    print("Trackballs:", sdl2.SDL_JoystickNumBalls(joystick))
    print("Buttons:", sdl2.SDL_JoystickNumButtons(joystick))
    print("Hats:", sdl2.SDL_JoystickNumHats(joystick))
    print("Haptic?:", sdl2.SDL_JoystickIsHaptic(joystick))

    #sdl2.SDL_JoystickClose(joystick)
    #return 0

    while (running):
        events = sdl2.ext.get_events()
        for event in events:
            if event.type == sdl2.SDL_JOYAXISMOTION:
                print("=======================")
                for axis in range(sdl2.SDL_JoystickNumAxes(joystick)):
                    print("Axis: %i, value: %i" %
                          (axis, sdl2.SDL_JoystickGetAxis(joystick, axis)))
            if event.type == sdl2.SDL_JOYBUTTONDOWN:
                print("=======================")
                for button in range(sdl2.SDL_JoystickNumButtons(joystick)):
                    print(
                        "Button: %i, value: %i" %
                        (button, sdl2.SDL_JoystickGetButton(joystick, button)))
            if event.type == sdl2.SDL_JOYHATMOTION:
                print("=======================")
                for hat in range(sdl2.SDL_JoystickNumHats(joystick)):
                    print("Hat: %i, value: %i" %
                          (hat, sdl2.SDL_JoystickGetHat(joystick, hat)))
            if event.type == sdl2.SDL_QUIT:
                running = False
                break
        window.refresh()
    return 0
Exemple #2
0
    def open(self, device_index):
        self._sdl_controller = sdl2.SDL_GameControllerOpen(device_index)
        self._sdl_joystick = sdl2.SDL_GameControllerGetJoystick(self._sdl_controller)
        self._sdl_joystick_id = sdl2.SDL_JoystickInstanceID(self._sdl_joystick)

        self._controller_name = sdl2.SDL_JoystickName(self._sdl_joystick)
        self._num_axis = sdl2.SDL_JoystickNumAxes(self._sdl_joystick),
        self._num_buttons = sdl2.SDL_JoystickNumButtons(self._sdl_joystick)
        self._num_balls = sdl2.SDL_JoystickNumBalls(self._sdl_joystick)

        for btn_index in range(0, self.MAX_BUTTONS):
            self._button_down[btn_index] = 0
            self._button_pressed[btn_index] = 0
            self._button_released[btn_index] = 0

        if self._sdl_joystick_id != -1:
            self._connected = True
Exemple #3
0
    def __new__(cls, identifier=None, instance_id=None, *args, **kwargs):
        # Check init
        if not get_init():
            init()

        # Create the object
        joy = super().__new__(cls)

        if instance_id is not None:
            # Create the underlying joystick from the instance id.
            # SDL_JOYDEVICEREMOVED and all other SDL_JOY#### events give the instance id
            joy.joystick = sdl2.SDL_JoystickFromInstanceID(instance_id)
            # print('Instance ID:', raw_joystick, SDL_JoystickGetAttached(raw_joystick))
        else:
            # Create the underlying joystick from the enumerated identifier
            # SDL_JOYDEVICEADDED and SDL_NumJoysticks use open
            if identifier is None:
                identifier = 0
            if isinstance(identifier, str):
                # Get the joystick from the name or None if not found!
                for i in range(sdl2.SDL_NumJoysticks()):
                    raw_joystick = sdl2.SDL_JoystickOpen(i)
                    try:
                        if sdl2.SDL_JoystickName(raw_joystick).decode(
                                'utf-8') == identifier:
                            joy.joystick = raw_joystick
                            break
                    except:
                        pass
            else:
                joy.joystick = sdl2.SDL_JoystickOpen(identifier)
            # print('ID:', raw_joystick, SDL_JoystickGetAttached(raw_joystick))

        try:
            joy.identifier = sdl2.SDL_JoystickID(
                sdl2.SDL_JoystickInstanceID(joy.joystick)).value
            # joy.identifier = SDL_JoystickInstanceID(raw_joystick)
            joy.name = sdl2.SDL_JoystickName(joy.joystick).decode('utf-8')
            joy.numaxes = sdl2.SDL_JoystickNumAxes(joy.joystick)
            joy.numbuttons = sdl2.SDL_JoystickNumButtons(joy.joystick)
            joy.numhats = sdl2.SDL_JoystickNumHats(joy.joystick)
            joy.numballs = sdl2.SDL_JoystickNumBalls(joy.joystick)
            joy.init_keys()
        except:
            pass

        # Try to get the gamepad object
        try:
            joy.gamecontroller = sdl2.SDL_GameControllerOpen(joy.identifier)
            # FromInstanceId does not Attach!
            # joy.gamecontroller = SDL_GameControllerFromInstanceID(SDL_JoystickInstanceID(joy.joystick)
            # print('ID:', SDL_GameControllerGetAttached(joy.gamecontroller))
        except:
            joy.gamecontroller = None

        try:
            joy.guid = get_guid(
                joy.joystick
            )  # Using this is more reliable for the GameController stuff
        except:
            pass

        return joy
Exemple #4
0
#
joystick = sdl2.SDL_JoystickOpen(0)
name = sdl2.SDL_JoystickName(joystick)
print(name)

num_axes = sdl2.SDL_JoystickNumAxes(joystick)
print('num axes; ', num_axes)

num_buttons = sdl2.SDL_JoystickNumButtons(joystick)
print('num buttons: ', num_buttons)

num_hats = sdl2.SDL_JoystickNumHats(joystick)
print('num hats: ', num_hats)

num_balls = sdl2.SDL_JoystickNumBalls(joystick)
print('num balls: ', num_balls)

#
import ctypes
import time

event = sdl2.SDL_Event()
while True:
    while sdl2.SDL_PollEvent(ctypes.byref(event)) != 0:
        if event.type == sdl2.SDL_JOYAXISMOTION:
            print('axis[{}] => {}'.format(event.jaxis.axis, event.jaxis.value))
        elif event.type == sdl2.SDL_JOYBUTTONDOWN:
            print('button[{}] => down'.format(event.jbutton.button))
        elif event.type == sdl2.SDL_JOYBUTTONUP:
            print('button[{}] => up'.format(event.jbutton.button))