encoder.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*
  2. * Copyright 2018 Jack Humbert <jack.humb@gmail.com>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #pragma once
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #include "gpio.h"
  21. #include "util.h"
  22. #ifdef ENCODER_ENABLE
  23. __attribute__((weak)) bool should_process_encoder(void);
  24. void encoder_init(void);
  25. bool encoder_task(void);
  26. bool encoder_queue_event(uint8_t index, bool clockwise);
  27. bool encoder_dequeue_event(uint8_t *index, bool *clockwise);
  28. bool encoder_update_kb(uint8_t index, bool clockwise);
  29. bool encoder_update_user(uint8_t index, bool clockwise);
  30. # ifdef SPLIT_KEYBOARD
  31. # if defined(ENCODERS_PAD_A_RIGHT)
  32. # ifndef NUM_ENCODERS_LEFT
  33. # define NUM_ENCODERS_LEFT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
  34. # endif
  35. # ifndef NUM_ENCODERS_RIGHT
  36. # define NUM_ENCODERS_RIGHT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A_RIGHT))
  37. # endif
  38. # else
  39. # ifndef NUM_ENCODERS_LEFT
  40. # define NUM_ENCODERS_LEFT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
  41. # endif
  42. # ifndef NUM_ENCODERS_RIGHT
  43. # define NUM_ENCODERS_RIGHT NUM_ENCODERS_LEFT
  44. # endif
  45. # endif
  46. # ifndef NUM_ENCODERS
  47. # define NUM_ENCODERS (NUM_ENCODERS_LEFT + NUM_ENCODERS_RIGHT)
  48. # endif
  49. # else // SPLIT_KEYBOARD
  50. # ifndef NUM_ENCODERS
  51. # define NUM_ENCODERS ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
  52. # endif
  53. # define NUM_ENCODERS_LEFT NUM_ENCODERS
  54. # define NUM_ENCODERS_RIGHT 0
  55. # endif // SPLIT_KEYBOARD
  56. # ifndef NUM_ENCODERS
  57. # define NUM_ENCODERS 0
  58. # define NUM_ENCODERS_LEFT 0
  59. # define NUM_ENCODERS_RIGHT 0
  60. # endif // NUM_ENCODERS
  61. # define NUM_ENCODERS_MAX_PER_SIDE MAX(NUM_ENCODERS_LEFT, NUM_ENCODERS_RIGHT)
  62. # ifndef MAX_QUEUED_ENCODER_EVENTS
  63. # define MAX_QUEUED_ENCODER_EVENTS MAX(4, ((NUM_ENCODERS_MAX_PER_SIDE) + 1))
  64. # endif // MAX_QUEUED_ENCODER_EVENTS
  65. typedef struct encoder_event_t {
  66. uint8_t index : 7;
  67. uint8_t clockwise : 1;
  68. } encoder_event_t;
  69. typedef struct encoder_events_t {
  70. uint8_t enqueued;
  71. uint8_t dequeued;
  72. uint8_t head;
  73. uint8_t tail;
  74. encoder_event_t queue[MAX_QUEUED_ENCODER_EVENTS];
  75. } encoder_events_t;
  76. // Get the current queued events
  77. void encoder_retrieve_events(encoder_events_t *events);
  78. // Encoder event queue management
  79. bool encoder_queue_event_advanced(encoder_events_t *events, uint8_t index, bool clockwise);
  80. bool encoder_dequeue_event_advanced(encoder_events_t *events, uint8_t *index, bool *clockwise);
  81. // Reset the queue to be empty
  82. void encoder_signal_queue_drain(void);
  83. # ifdef ENCODER_MAP_ENABLE
  84. # define NUM_DIRECTIONS 2
  85. # define ENCODER_CCW_CW(ccw, cw) \
  86. { (cw), (ccw) }
  87. extern const uint16_t encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS];
  88. # endif // ENCODER_MAP_ENABLE
  89. // "Custom encoder lite" support
  90. void encoder_driver_init(void);
  91. void encoder_driver_task(void);
  92. #endif // ENCODER_ENABLE