joystick.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 <stdint.h>
  18. #include "gpio.h"
  19. #ifndef JOYSTICK_BUTTON_COUNT
  20. # define JOYSTICK_BUTTON_COUNT 8
  21. #elif JOYSTICK_BUTTON_COUNT > 32
  22. # error Joystick feature only supports up to 32 buttons
  23. #endif
  24. #ifndef JOYSTICK_AXES_COUNT
  25. # define JOYSTICK_AXES_COUNT 4
  26. #elif JOYSTICK_AXES_COUNT > 6
  27. # error Joystick feature only supports up to 6 axes
  28. #endif
  29. #if JOYSTICK_AXES_COUNT == 0 && JOYSTICK_BUTTON_COUNT == 0
  30. # error Joystick feature requires at least one axis or button
  31. #endif
  32. #ifndef JOYSTICK_AXES_RESOLUTION
  33. # define JOYSTICK_AXES_RESOLUTION 8
  34. #elif JOYSTICK_AXES_RESOLUTION < 8 || JOYSTICK_AXES_RESOLUTION > 16
  35. # error JOYSTICK_AXES_RESOLUTION must be between 8 and 16
  36. #endif
  37. #define JOYSTICK_RESOLUTION ((1L << (JOYSTICK_AXES_RESOLUTION - 1)) - 1)
  38. // configure on input_pin of the joystick_axes array entry to JS_VIRTUAL_AXIS
  39. // to prevent it from being read from the ADC. This allows outputing forged axis value.
  40. //
  41. #define JS_VIRTUAL_AXIS 0xFF
  42. #define JOYSTICK_AXIS_VIRTUAL \
  43. { JS_VIRTUAL_AXIS, JS_VIRTUAL_AXIS, JS_VIRTUAL_AXIS, 0, 1023 }
  44. #define JOYSTICK_AXIS_IN(INPUT_PIN, LOW, REST, HIGH) \
  45. { JS_VIRTUAL_AXIS, INPUT_PIN, JS_VIRTUAL_AXIS, LOW, REST, HIGH }
  46. #define JOYSTICK_AXIS_IN_OUT(INPUT_PIN, OUTPUT_PIN, LOW, REST, HIGH) \
  47. { OUTPUT_PIN, INPUT_PIN, JS_VIRTUAL_AXIS, LOW, REST, HIGH }
  48. #define JOYSTICK_AXIS_IN_OUT_GROUND(INPUT_PIN, OUTPUT_PIN, GROUND_PIN, LOW, REST, HIGH) \
  49. { OUTPUT_PIN, INPUT_PIN, GROUND_PIN, LOW, REST, HIGH }
  50. typedef struct {
  51. pin_t output_pin;
  52. pin_t input_pin;
  53. pin_t ground_pin;
  54. // the AVR ADC offers 10 bit precision, with significant bits on the higher part
  55. uint16_t min_digit;
  56. uint16_t mid_digit;
  57. uint16_t max_digit;
  58. } joystick_config_t;
  59. extern joystick_config_t joystick_axes[JOYSTICK_AXES_COUNT];
  60. enum joystick_status {
  61. JS_INITIALIZED = 1,
  62. JS_UPDATED,
  63. };
  64. typedef struct {
  65. uint8_t buttons[(JOYSTICK_BUTTON_COUNT - 1) / 8 + 1];
  66. int16_t axes[JOYSTICK_AXES_COUNT];
  67. uint8_t status : 2;
  68. } joystick_t;
  69. extern joystick_t joystick_status;
  70. void joystick_task(void);
  71. void joystick_flush(void);
  72. void register_joystick_button(uint8_t button);
  73. void unregister_joystick_button(uint8_t button);
  74. int16_t joystick_read_axis(uint8_t axis);
  75. void joystick_read_axes(void);
  76. void joystick_set_axis(uint8_t axis, int16_t value);
  77. void host_joystick_send(joystick_t *joystick);