logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

joystick.h (3988B)


  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 \
  57. { NO_PIN, 0, JOYSTICK_MAX_VALUE / 2, JOYSTICK_MAX_VALUE }
  58. #define JOYSTICK_AXIS_IN(INPUT_PIN, LOW, REST, HIGH) \
  59. { INPUT_PIN, LOW, REST, HIGH }
  60. typedef struct {
  61. pin_t input_pin;
  62. // the AVR ADC offers 10 bit precision, with significant bits on the higher part
  63. uint16_t min_digit;
  64. uint16_t mid_digit;
  65. uint16_t max_digit;
  66. } joystick_config_t;
  67. extern joystick_config_t joystick_axes[JOYSTICK_AXIS_COUNT];
  68. typedef struct {
  69. uint8_t buttons[(JOYSTICK_BUTTON_COUNT - 1) / 8 + 1];
  70. int16_t axes[JOYSTICK_AXIS_COUNT];
  71. #ifdef JOYSTICK_HAS_HAT
  72. int8_t hat;
  73. #endif
  74. bool dirty;
  75. } joystick_t;
  76. extern joystick_t joystick_state;
  77. /**
  78. * \brief Handle the initialization of the subsystem.
  79. */
  80. void joystick_init(void);
  81. /**
  82. * \brief Handle various subsystem background tasks.
  83. */
  84. void joystick_task(void);
  85. /**
  86. * \brief Send the joystick report to the host, if it has been marked as dirty.
  87. */
  88. void joystick_flush(void);
  89. /**
  90. * \brief Set the state of a button, and flush the report.
  91. *
  92. * \param button The index of the button to press, from 0 to 31.
  93. */
  94. void register_joystick_button(uint8_t button);
  95. /**
  96. * \brief Reset the state of a button, and flush the report.
  97. *
  98. * \param button The index of the button to release, from 0 to 31.
  99. */
  100. void unregister_joystick_button(uint8_t button);
  101. /**
  102. * \brief Sample and process the analog value of the given axis.
  103. *
  104. * \param axis The axis to read.
  105. *
  106. * \return A signed 16-bit integer, where 0 is the resting or mid point.
  107. */
  108. int16_t joystick_read_axis(uint8_t axis);
  109. /**
  110. * \brief Sample and process the all axis.
  111. */
  112. void joystick_read_axes(void);
  113. /**
  114. * \brief Set the value of the given axis.
  115. *
  116. * \param axis The axis to set the value of.
  117. * \param value The value to set.
  118. */
  119. void joystick_set_axis(uint8_t axis, int16_t value);
  120. /**
  121. * \brief Set the position of the hat switch.
  122. *
  123. * \param value The hat switch position to set.
  124. */
  125. void joystick_set_hat(int8_t value);
  126. /** \} */