You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
1.0 KiB
Python
32 lines
1.0 KiB
Python
import sys
|
|
import sdl2
|
|
import sdl2.ext
|
|
import time
|
|
|
|
def enable_motion(controller):
|
|
if sdl2.SDL_GameControllerSetSensorEnabled(controller, sdl2.SDL_SENSOR_ACCEL, 1) != 0:
|
|
print(f"Failed to enable accelerometer: {sdl2.SDL_GetError().decode()}")
|
|
#sdl2.SDL_GameControllerClose(controller)
|
|
#sdl2.SDL_Quit()
|
|
|
|
def main():
|
|
if sdl2.SDL_Init(sdl2.SDL_INIT_GAMECONTROLLER | sdl2.SDL_INIT_EVENTS) != 0:
|
|
print(f"SDL_Init error: {sdl2.SDL_GetError().decode()}")
|
|
return 1
|
|
num_joysticks = sdl2.SDL_NumJoysticks()
|
|
print("Joystick count: ", num_joysticks)
|
|
for i in range(num_joysticks):
|
|
print(i)
|
|
name = sdl2.SDL_GameControllerNameForIndex(i)
|
|
if name:
|
|
print(f"Controller {i}: {name.decode()}")
|
|
else:
|
|
print(f"Controller {i}: (unknown name)")
|
|
if sdl2.SDL_IsGameController(i):
|
|
controller = sdl2.SDL_GameControllerOpen(i)
|
|
if controller:
|
|
enable_motion(controller)
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|