joystick.c 984 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "joystick.h"
  2. // clang-format off
  3. joystick_t joystick_status = {
  4. .buttons = {0},
  5. .axes = {
  6. #if JOYSTICK_AXES_COUNT > 0
  7. 0
  8. #endif
  9. },
  10. .status = 0
  11. };
  12. // clang-format on
  13. // array defining the reading of analog values for each axis
  14. __attribute__((weak)) joystick_config_t joystick_axes[JOYSTICK_AXES_COUNT] = {};
  15. // to be implemented in the hid protocol library
  16. void send_joystick_packet(joystick_t *joystick);
  17. void joystick_flush(void) {
  18. if ((joystick_status.status & JS_UPDATED) > 0) {
  19. send_joystick_packet(&joystick_status);
  20. joystick_status.status &= ~JS_UPDATED;
  21. }
  22. }
  23. void register_joystick_button(uint8_t button) {
  24. joystick_status.buttons[button / 8] |= 1 << (button % 8);
  25. joystick_status.status |= JS_UPDATED;
  26. joystick_flush();
  27. }
  28. void unregister_joystick_button(uint8_t button) {
  29. joystick_status.buttons[button / 8] &= ~(1 << (button % 8));
  30. joystick_status.status |= JS_UPDATED;
  31. joystick_flush();
  32. }