thumbstick.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. #pragma once
  2. typedef enum {
  3. THUMBSTICK_MODE_MOUSE = 0,
  4. THUMBSTICK_MODE_ARROWS,
  5. THUMBSTICK_MODE_SCROLL,
  6. _THUMBSTICK_MODE_LAST // Do not use, except for looping through enum values
  7. } thumbstick_mode_t;
  8. // Parameters
  9. #define THUMBSTICK_DEAD_ZONE 90 // Values below this are ignored (deadzone)
  10. #define THUMBSTICK_FINE_ZONE 180 // Values below this enable fine movement
  11. #define THUMBSTICK_MODE THUMBSTICK_MODE_MOUSE
  12. #define THUMBSTICK_SPEED 256
  13. #define THUMBSTICK_FINE_SPEED 96
  14. #define THUMBSTICK_SCROLL_SPEED 1
  15. #define THUMBSTICK_EIGHT_AXIS true
  16. #define THUMBSTICK_AXIS_SEPARATION 0.5f
  17. // Implicit and derived constants
  18. #define THUMBSTICK_TIMEOUT 10 // Mouse report throttling time in ms
  19. #define THUMBSTICK_SCROLL_TIMEOUT 200 // Mouse scroll throttling time in ms
  20. #define THUMBSTICK_RANGE_START 0
  21. #define THUMBSTICK_RANGE_STOP 1023
  22. #define THUMBSTICK_RANGE_CENTER (THUMBSTICK_RANGE_STOP - THUMBSTICK_RANGE_START + 1) / 2
  23. #define THUMBSTICK_RANGE_MOVEMENT (THUMBSTICK_RANGE_CENTER - THUMBSTICK_DEAD_ZONE)
  24. #include "timer.h"
  25. #include "analog.h"
  26. #include "split_util.h"
  27. #include "pointing_device.h"
  28. #if defined THUMBSTICK_DEBUG
  29. # include "print.h"
  30. uint16_t rawX;
  31. uint16_t rawY;
  32. uint16_t distX;
  33. uint16_t distY;
  34. uint16_t thumbstickLogTimer;
  35. #endif
  36. typedef struct {
  37. thumbstick_mode_t mode;
  38. uint16_t deadZone;
  39. uint16_t fineZone;
  40. uint16_t speed;
  41. uint16_t fineSpeed;
  42. float axisSeparation;
  43. bool eightAxis;
  44. } thumbstick_config_t;
  45. typedef struct {
  46. int16_t x;
  47. int16_t y;
  48. } thumbstick_vector_t;
  49. typedef struct {
  50. bool up;
  51. bool right;
  52. bool down;
  53. bool left;
  54. } thumbstick_direction_t;
  55. typedef struct {
  56. thumbstick_config_t config;
  57. thumbstick_vector_t vector;
  58. thumbstick_direction_t direction;
  59. thumbstick_direction_t lastDirection;
  60. report_mouse_t report;
  61. } thumbstick_state_t;
  62. uint16_t thumbstickTimer;
  63. uint16_t thumbstickScrollTimer;
  64. thumbstick_state_t thumbstick_state;
  65. void thumbstick_mode_set(thumbstick_mode_t mode);
  66. thumbstick_mode_t thumbstick_mode_get(void);
  67. void thumbstick_mode_cycle(bool reverse);
  68. void thumbstick_init(void);
  69. // Axis-level wrapper to read raw value, do logging and calculate speed
  70. int16_t thumbstick_get_component(uint8_t pin);
  71. // Get mouse speed
  72. int16_t thumbstick_get_mouse_speed(int16_t component);
  73. // Fix direction within one of 8 axes (or 4 if 8-axis is disabled)
  74. thumbstick_direction_t thumbstick_get_discretized_direction(thumbstick_vector_t vector, float axisSeparation, bool eightAxis);
  75. void thumbstick_process(void);
  76. void update_keycode_status(uint16_t keycode, bool last, bool current);