joystick.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #pragma once
  2. #include "quantum.h"
  3. #include <stdint.h>
  4. #ifndef JOYSTICK_BUTTON_COUNT
  5. # define JOYSTICK_BUTTON_COUNT 8
  6. #endif
  7. #ifndef JOYSTICK_AXES_COUNT
  8. # define JOYSTICK_AXES_COUNT 4
  9. #endif
  10. #ifndef JOYSTICK_AXES_RESOLUTION
  11. # define JOYSTICK_AXES_RESOLUTION 8
  12. #elif JOYSTICK_AXES_RESOLUTION < 8 || JOYSTICK_AXES_RESOLUTION > 16
  13. # error JOYSTICK_AXES_RESOLUTION must be between 8 and 16
  14. #endif
  15. #define JOYSTICK_RESOLUTION ((1L << (JOYSTICK_AXES_RESOLUTION - 1)) - 1)
  16. // configure on input_pin of the joystick_axes array entry to JS_VIRTUAL_AXIS
  17. // to prevent it from being read from the ADC. This allows outputing forged axis value.
  18. //
  19. #define JS_VIRTUAL_AXIS 0xFF
  20. #define JOYSTICK_AXIS_VIRTUAL \
  21. { JS_VIRTUAL_AXIS, JS_VIRTUAL_AXIS, JS_VIRTUAL_AXIS, 0, 1023 }
  22. #define JOYSTICK_AXIS_IN(INPUT_PIN, LOW, REST, HIGH) \
  23. { JS_VIRTUAL_AXIS, INPUT_PIN, JS_VIRTUAL_AXIS, LOW, REST, HIGH }
  24. #define JOYSTICK_AXIS_IN_OUT(INPUT_PIN, OUTPUT_PIN, LOW, REST, HIGH) \
  25. { OUTPUT_PIN, INPUT_PIN, JS_VIRTUAL_AXIS, LOW, REST, HIGH }
  26. #define JOYSTICK_AXIS_IN_OUT_GROUND(INPUT_PIN, OUTPUT_PIN, GROUND_PIN, LOW, REST, HIGH) \
  27. { OUTPUT_PIN, INPUT_PIN, GROUND_PIN, LOW, REST, HIGH }
  28. typedef struct {
  29. pin_t output_pin;
  30. pin_t input_pin;
  31. pin_t ground_pin;
  32. // the AVR ADC offers 10 bit precision, with significant bits on the higher part
  33. uint16_t min_digit;
  34. uint16_t mid_digit;
  35. uint16_t max_digit;
  36. } joystick_config_t;
  37. extern joystick_config_t joystick_axes[JOYSTICK_AXES_COUNT];
  38. enum joystick_status { JS_INITIALIZED = 1, JS_UPDATED = 2 };
  39. typedef struct {
  40. uint8_t buttons[JOYSTICK_BUTTON_COUNT / 8 + 1];
  41. int16_t axes[JOYSTICK_AXES_COUNT];
  42. uint8_t status : 2;
  43. } joystick_t;
  44. extern joystick_t joystick_status;
  45. // to be implemented in the hid protocol library
  46. void send_joystick_packet(joystick_t *joystick);