@@ -0,0 +1,2391 @@
+import pygame, json, math, socket%0A%0A# Axes%0AROLL_AXIS         = 0 # Xbox: left stick H%0APITCH_AXIS        = 1 # Xbox: left stick V%0AYAW_AXIS          = 3 # Xbox: right stick H%0AGAS_AXIS          = 4 # Xbox: right stick V%0A%0A# Buttons%0ARESET_BUTTON      = 7 # Xbox: 'Start'%0AQUIT_BUTTON       = 6 # Xbox: 'Back'%0ATAKE_OFF_BUTTON   = 8 # Xbox: 'Xbox logo'%0A%0Asock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)%0A%0Aseq = 0%0Adef send_state(state):%0A  global seq, sock%0A  seq += 1%0A  HOST, PORT = ('127.0.0.1', 5560)%0A  sock.sendto(json.dumps(%7B'seq': seq, 'state': state%7D), (HOST, PORT))%0A%0Adef main():%0A  pygame.init()%0A%0A  n_joysticks = pygame.joystick.get_count()%0A  print('Joysticks: %25i' %25 (n_joysticks,))%0A%0A  if n_joysticks %3C 1:%0A    print('No joysticks found :(')%0A    return%0A%0A  joystick = pygame.joystick.Joystick(0)%0A  joystick.init()%0A%0A  print('Found: %25s' %25 (joystick.get_name(),))%0A%0A  print('Assuming an xbox controller. See top of file for details of change.')%0A  print('Use Back button to quit')%0A%0A  state = %7B%0A      'roll': 0.0,%0A      'pitch': 0.0,%0A      'yaw': 0.0,%0A      'gas': 0.0,%0A      'take_off': False,%0A      'reset': False,%0A      'hover': False,%0A      %7D;%0A%0A  # Create a state timer%0A  STATE_TICK_EVENT = pygame.USEREVENT%0A  pygame.time.set_timer(STATE_TICK_EVENT, 50)%0A%0A  while True:%0A    e = pygame.event.wait()%0A    if e.type == pygame.JOYBUTTONDOWN:%0A      if e.button == TAKE_OFF_BUTTON:%0A        state%5B'take_off'%5D = True%0A      elif e.button == RESET_BUTTON:%0A        state%5B'reset'%5D = True%0A      elif e.button == QUIT_BUTTON:%0A        print('Quitting')%0A        return%0A    elif e.type == pygame.JOYBUTTONUP:%0A      if e.button == TAKE_OFF_BUTTON:%0A        state%5B'take_off'%5D = False%0A      elif e.button == RESET_BUTTON:%0A        state%5B'reset'%5D = False%0A    elif e.type == pygame.JOYAXISMOTION:%0A      surface = None%0A      sign = 1.0%0A%0A      if e.axis == ROLL_AXIS:%0A        surface = 'roll'%0A      elif e.axis == PITCH_AXIS:%0A        surface = 'pitch'%0A      elif e.axis == YAW_AXIS:%0A        surface = 'yaw'%0A      elif e.axis == GAS_AXIS:%0A        surface = 'gas'%0A        sign = -1.0%0A%0A      if surface is not None:%0A        state%5Bsurface%5D = sign * e.value%0A%0A    max_shift = 0.0%0A    for x in ('roll', 'pitch', 'yaw', 'gas'):%0A      max_shift = max(max_shift, abs(state%5Bx%5D))%0A    %0A    state%5B'hover'%5D = max_shift %3C 0.3%0A    if state%5B'hover'%5D:%0A      state%5B'gas'%5D = 0.0%0A    %0A    send_state(state)%0A%0Aif __name__ == '__main__':%0A  main()%0A

add proof of concept xbox controller