joystick.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* Copyright 2022
  2. *
  3. * This program is free software: you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation, either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This program is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #pragma once
  17. #include <stdbool.h>
  18. #include <stdint.h>
  19. #include "gpio.h"
  20. /**
  21. * \file
  22. *
  23. * \defgroup joystick HID Joystick
  24. * \{
  25. */
  26. #ifndef JOYSTICK_BUTTON_COUNT
  27. # define JOYSTICK_BUTTON_COUNT 8
  28. #elif JOYSTICK_BUTTON_COUNT > 32
  29. # error Joystick feature only supports up to 32 buttons
  30. #endif
  31. #ifndef JOYSTICK_AXIS_COUNT
  32. # define JOYSTICK_AXIS_COUNT 2
  33. #elif JOYSTICK_AXIS_COUNT > 6
  34. # error Joystick feature only supports up to 6 axes
  35. #endif
  36. #if JOYSTICK_AXIS_COUNT == 0 && JOYSTICK_BUTTON_COUNT == 0
  37. # error Joystick feature requires at least one axis or button
  38. #endif
  39. #ifndef JOYSTICK_AXIS_RESOLUTION
  40. # define JOYSTICK_AXIS_RESOLUTION 8
  41. #elif JOYSTICK_AXIS_RESOLUTION < 8 || JOYSTICK_AXIS_RESOLUTION > 16
  42. # error JOYSTICK_AXIS_RESOLUTION must be between 8 and 16
  43. #endif
  44. #define JOYSTICK_MAX_VALUE ((1L << (JOYSTICK_AXIS_RESOLUTION - 1)) - 1)
  45. #define JOYSTICK_HAT_CENTER -1
  46. #define JOYSTICK_HAT_NORTH 0
  47. #define JOYSTICK_HAT_NORTHEAST 1
  48. #define JOYSTICK_HAT_EAST 2
  49. #define JOYSTICK_HAT_SOUTHEAST 3
  50. #define JOYSTICK_HAT_SOUTH 4
  51. #define JOYSTICK_HAT_SOUTHWEST 5
  52. #define JOYSTICK_HAT_WEST 6
  53. #define JOYSTICK_HAT_NORTHWEST 7
  54. // configure on input_pin of the joystick_axes array entry to NO_PIN
  55. // to prevent it from being read from the ADC. This allows outputting forged axis value.
  56. #define JOYSTICK_AXIS_VIRTUAL {NO_PIN, 0, JOYSTICK_MAX_VALUE / 2, JOYSTICK_MAX_VALUE}
  57. #define JOYSTICK_AXIS_IN(INPUT_PIN, LOW, REST, HIGH) {INPUT_PIN, LOW, REST, HIGH}
  58. typedef struct {
  59. pin_t input_pin;
  60. // the AVR ADC offers 10 bit precision, with significant bits on the higher part
  61. uint16_t min_digit;
  62. uint16_t mid_digit;
  63. uint16_t max_digit;
  64. } joystick_config_t;
  65. extern joystick_config_t joystick_axes[JOYSTICK_AXIS_COUNT];
  66. typedef struct {
  67. uint8_t buttons[(JOYSTICK_BUTTON_COUNT - 1) / 8 + 1];
  68. int16_t axes[JOYSTICK_AXIS_COUNT];
  69. #ifdef JOYSTICK_HAS_HAT
  70. int8_t hat;
  71. #endif
  72. bool dirty;
  73. } joystick_t;
  74. extern joystick_t joystick_state;
  75. /**
  76. * \brief Handle the initialization of the subsystem.
  77. */
  78. void joystick_init(void);
  79. /**
  80. * \brief Handle various subsystem background tasks.
  81. */
  82. void joystick_task(void);
  83. /**
  84. * \brief Send the joystick report to the host, if it has been marked as dirty.
  85. */
  86. void joystick_flush(void);
  87. /**
  88. * \brief Set the state of a button, and flush the report.
  89. *
  90. * \param button The index of the button to press, from 0 to 31.
  91. */
  92. void register_joystick_button(uint8_t button);
  93. /**
  94. * \brief Reset the state of a button, and flush the report.
  95. *
  96. * \param button The index of the button to release, from 0 to 31.
  97. */
  98. void unregister_joystick_button(uint8_t button);
  99. /**
  100. * \brief Sample and process the analog value of the given axis.
  101. *
  102. * \param axis The axis to read.
  103. *
  104. * \return A signed 16-bit integer, where 0 is the resting or mid point.
  105. */
  106. int16_t joystick_read_axis(uint8_t axis);
  107. /**
  108. * \brief Sample and process the all axis.
  109. */
  110. void joystick_read_axes(void);
  111. /**
  112. * \brief Set the value of the given axis.
  113. *
  114. * \param axis The axis to set the value of.
  115. * \param value The value to set.
  116. */
  117. void joystick_set_axis(uint8_t axis, int16_t value);
  118. /**
  119. * \brief Set the position of the hat switch.
  120. *
  121. * \param value The hat switch position to set.
  122. */
  123. void joystick_set_hat(int8_t value);
  124. /** \} */