# __DRIVER_CODE__ imports

import pygame
from time import sleep
{%- if config["config"]["sdl_driver"] %}
import os
os.environ["SDL_VIDEODRIVER"] = "{{config["config"]["sdl_driver"]}}"
{%- endif %}

# __DRIVER_CODE__ variables


# __DRIVER_CODE__ setup
{% for sig,args in (out_signals.items()) %}
{{sig}}BufVars[6] = {{args['schema']['max_packets_per_tick'] * args['packet_size']}}
{%- endfor %}

pygame.display.init()
pygame.joystick.init()

if pygame.joystick.get_count() < 1:
    die("No joystick found!\n")

pygame_joystick = pygame.joystick.Joystick(0)
pygame_joystick.init()
num_buttons = pygame_joystick.get_numbuttons()
pygame.event.pump()
sleep_duration = {{config["config"]["tick_len"]}} / (2. * 1e6)

# __DRIVER_CODE__ read  
  pygame.event.pump()
  (<double*>inBuf)[0] = pygame_joystick.get_axis(0)
  (<double*>inBuf)[1] = pygame_joystick.get_axis(1)
  for i in range(num_buttons): 
    (<uint8_t*>inBuf)[128 + i] = pygame_joystick.get_button(i)

{%- if async %}

  sleep(sleep_duration)
{%- endif %}


# __DRIVER_CODE__ exit_handler
