joystick.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * \defgroup joystick
  22. *
  23. * 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. // configure on input_pin of the joystick_axes array entry to JS_VIRTUAL_AXIS
  46. // to prevent it from being read from the ADC. This allows outputing forged axis value.
  47. //
  48. #define JS_VIRTUAL_AXIS 0xFF
  49. #define JOYSTICK_AXIS_VIRTUAL \
  50. { JS_VIRTUAL_AXIS, JS_VIRTUAL_AXIS, JS_VIRTUAL_AXIS, 0, 1023 }
  51. #define JOYSTICK_AXIS_IN(INPUT_PIN, LOW, REST, HIGH) \
  52. { JS_VIRTUAL_AXIS, INPUT_PIN, JS_VIRTUAL_AXIS, LOW, REST, HIGH }
  53. #define JOYSTICK_AXIS_IN_OUT(INPUT_PIN, OUTPUT_PIN, LOW, REST, HIGH) \
  54. { OUTPUT_PIN, INPUT_PIN, JS_VIRTUAL_AXIS, LOW, REST, HIGH }
  55. #define JOYSTICK_AXIS_IN_OUT_GROUND(INPUT_PIN, OUTPUT_PIN, GROUND_PIN, LOW, REST, HIGH) \
  56. { OUTPUT_PIN, INPUT_PIN, GROUND_PIN, LOW, REST, HIGH }
  57. typedef struct {
  58. pin_t output_pin;
  59. pin_t input_pin;
  60. pin_t ground_pin;
  61. // the AVR ADC offers 10 bit precision, with significant bits on the higher part
  62. uint16_t min_digit;
  63. uint16_t mid_digit;
  64. uint16_t max_digit;
  65. } joystick_config_t;
  66. extern joystick_config_t joystick_axes[JOYSTICK_AXIS_COUNT];
  67. typedef struct {
  68. uint8_t buttons[(JOYSTICK_BUTTON_COUNT - 1) / 8 + 1];
  69. int16_t axes[JOYSTICK_AXIS_COUNT];
  70. bool dirty;
  71. } joystick_t;
  72. extern joystick_t joystick_state;
  73. void joystick_task(void);
  74. /**
  75. * \brief Send the joystick report to the host, if it has been marked as dirty.
  76. */
  77. void joystick_flush(void);
  78. /**
  79. * \brief Set the state of a button, and flush the report.
  80. *
  81. * \param button The index of the button to press, from 0 to 31.
  82. */
  83. void register_joystick_button(uint8_t button);
  84. /**
  85. * \brief Reset the state of a button, and flush the report.
  86. *
  87. * \param button The index of the button to release, from 0 to 31.
  88. */
  89. void unregister_joystick_button(uint8_t button);
  90. /**
  91. * \brief Sample and process the analog value of the given axis.
  92. *
  93. * \param axis The axis to read.
  94. *
  95. * \return A signed 16-bit integer, where 0 is the resting or mid point.
  96. */
  97. int16_t joystick_read_axis(uint8_t axis);
  98. void joystick_read_axes(void);
  99. /**
  100. * \brief Set the value of the given axis.
  101. *
  102. * \param axis The axis to set the value of.
  103. * \param value The value to set.
  104. */
  105. void joystick_set_axis(uint8_t axis, int16_t value);
  106. void host_joystick_send(joystick_t *joystick);
  107. /** \} */