joystick.h 1.5 KB

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