encoder_quadrature.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // Copyright 2018 Jack Humbert <jack.humb@gmail.com>
  2. // Copyright 2018-2023 Nick Brassel (@tzarc)
  3. // SPDX-License-Identifier: GPL-2.0-or-later
  4. #include <stdint.h>
  5. #include "encoder.h"
  6. #include "gpio.h"
  7. #include "keyboard.h"
  8. #include "action.h"
  9. #include "keycodes.h"
  10. #include "wait.h"
  11. #ifdef SPLIT_KEYBOARD
  12. # include "split_util.h"
  13. #endif
  14. // for memcpy
  15. #include <string.h>
  16. #if !defined(ENCODER_RESOLUTIONS) && !defined(ENCODER_RESOLUTION)
  17. # define ENCODER_RESOLUTION 4
  18. #endif
  19. #undef ENCODER_DEFAULT_PIN_API_IMPL
  20. #if defined(ENCODER_A_PINS) && defined(ENCODER_B_PINS)
  21. // Inform the quadrature driver that it needs to implement pin init/read functions
  22. # define ENCODER_DEFAULT_PIN_API_IMPL
  23. #endif
  24. __attribute__((weak)) void encoder_quadrature_init_pin(uint8_t index, bool pad_b);
  25. __attribute__((weak)) uint8_t encoder_quadrature_read_pin(uint8_t index, bool pad_b);
  26. #ifdef ENCODER_DEFAULT_PIN_API_IMPL
  27. static pin_t encoders_pad_a[NUM_ENCODERS_MAX_PER_SIDE] = ENCODER_A_PINS;
  28. static pin_t encoders_pad_b[NUM_ENCODERS_MAX_PER_SIDE] = ENCODER_B_PINS;
  29. __attribute__((weak)) void encoder_wait_pullup_charge(void) {
  30. wait_us(100);
  31. }
  32. __attribute__((weak)) void encoder_quadrature_init_pin(uint8_t index, bool pad_b) {
  33. pin_t pin = pad_b ? encoders_pad_b[index] : encoders_pad_a[index];
  34. if (pin != NO_PIN) {
  35. gpio_set_pin_input_high(pin);
  36. }
  37. }
  38. __attribute__((weak)) uint8_t encoder_quadrature_read_pin(uint8_t index, bool pad_b) {
  39. pin_t pin = pad_b ? encoders_pad_b[index] : encoders_pad_a[index];
  40. if (pin != NO_PIN) {
  41. return gpio_read_pin(pin) ? 1 : 0;
  42. }
  43. return 0;
  44. }
  45. #endif // ENCODER_DEFAULT_PIN_API_IMPL
  46. #ifdef ENCODER_RESOLUTIONS
  47. static uint8_t encoder_resolutions[NUM_ENCODERS] = ENCODER_RESOLUTIONS;
  48. #endif
  49. #ifndef ENCODER_DIRECTION_FLIP
  50. # define ENCODER_CLOCKWISE true
  51. # define ENCODER_COUNTER_CLOCKWISE false
  52. #else
  53. # define ENCODER_CLOCKWISE false
  54. # define ENCODER_COUNTER_CLOCKWISE true
  55. #endif
  56. static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
  57. static uint8_t encoder_state[NUM_ENCODERS] = {0};
  58. static int8_t encoder_pulses[NUM_ENCODERS] = {0};
  59. // encoder counts
  60. static uint8_t thisCount;
  61. #ifdef SPLIT_KEYBOARD
  62. // encoder offsets for each hand
  63. static uint8_t thisHand, thatHand;
  64. // encoder counts for each hand
  65. static uint8_t thatCount;
  66. #endif
  67. __attribute__((weak)) void encoder_quadrature_post_init_kb(void) {
  68. extern void encoder_quadrature_handle_read(uint8_t index, uint8_t pin_a_state, uint8_t pin_b_state);
  69. // Unused normally, but can be used for things like setting up pin-change interrupts in keyboard code.
  70. // During the interrupt, read the pins then call `encoder_handle_read()` with the pin states and it'll queue up an encoder event if needed.
  71. }
  72. void encoder_quadrature_post_init(void) {
  73. #ifdef ENCODER_DEFAULT_PIN_API_IMPL
  74. for (uint8_t i = 0; i < thisCount; i++) {
  75. encoder_quadrature_init_pin(i, false);
  76. encoder_quadrature_init_pin(i, true);
  77. }
  78. encoder_wait_pullup_charge();
  79. for (uint8_t i = 0; i < thisCount; i++) {
  80. encoder_state[i] = (encoder_quadrature_read_pin(i, false) << 0) | (encoder_quadrature_read_pin(i, true) << 1);
  81. }
  82. #else
  83. memset(encoder_state, 0, sizeof(encoder_state));
  84. #endif
  85. encoder_quadrature_post_init_kb();
  86. }
  87. void encoder_driver_init(void) {
  88. #ifdef SPLIT_KEYBOARD
  89. thisHand = is_keyboard_left() ? 0 : NUM_ENCODERS_LEFT;
  90. thatHand = NUM_ENCODERS_LEFT - thisHand;
  91. thisCount = is_keyboard_left() ? NUM_ENCODERS_LEFT : NUM_ENCODERS_RIGHT;
  92. thatCount = is_keyboard_left() ? NUM_ENCODERS_RIGHT : NUM_ENCODERS_LEFT;
  93. #else // SPLIT_KEYBOARD
  94. thisCount = NUM_ENCODERS;
  95. #endif
  96. #ifdef ENCODER_TESTS
  97. // Annoying that we have to clear out values during initialisation here, but
  98. // because all the arrays are static locals, rerunning tests in the same
  99. // executable doesn't reset any of these. Kinda crappy having test-only code
  100. // here, but it's the simplest solution.
  101. memset(encoder_state, 0, sizeof(encoder_state));
  102. memset(encoder_pulses, 0, sizeof(encoder_pulses));
  103. const pin_t encoders_pad_a_left[] = ENCODER_A_PINS;
  104. const pin_t encoders_pad_b_left[] = ENCODER_B_PINS;
  105. for (uint8_t i = 0; i < thisCount; i++) {
  106. encoders_pad_a[i] = encoders_pad_a_left[i];
  107. encoders_pad_b[i] = encoders_pad_b_left[i];
  108. }
  109. #endif
  110. #if defined(SPLIT_KEYBOARD) && defined(ENCODER_A_PINS_RIGHT) && defined(ENCODER_B_PINS_RIGHT)
  111. // Re-initialise the pads if it's the right-hand side
  112. if (!is_keyboard_left()) {
  113. const pin_t encoders_pad_a_right[] = ENCODER_A_PINS_RIGHT;
  114. const pin_t encoders_pad_b_right[] = ENCODER_B_PINS_RIGHT;
  115. for (uint8_t i = 0; i < thisCount; i++) {
  116. encoders_pad_a[i] = encoders_pad_a_right[i];
  117. encoders_pad_b[i] = encoders_pad_b_right[i];
  118. }
  119. }
  120. #endif // defined(SPLIT_KEYBOARD) && defined(ENCODER_A_PINS_RIGHT) && defined(ENCODER_B_PINS_RIGHT)
  121. // Encoder resolutions is defined differently in config.h, so concatenate
  122. #if defined(SPLIT_KEYBOARD) && defined(ENCODER_RESOLUTIONS)
  123. # if defined(ENCODER_RESOLUTIONS_RIGHT)
  124. static const uint8_t encoder_resolutions_right[NUM_ENCODERS_RIGHT] = ENCODER_RESOLUTIONS_RIGHT;
  125. # else // defined(ENCODER_RESOLUTIONS_RIGHT)
  126. static const uint8_t encoder_resolutions_right[NUM_ENCODERS_RIGHT] = ENCODER_RESOLUTIONS;
  127. # endif // defined(ENCODER_RESOLUTIONS_RIGHT)
  128. for (uint8_t i = 0; i < NUM_ENCODERS_RIGHT; i++) {
  129. encoder_resolutions[NUM_ENCODERS_LEFT + i] = encoder_resolutions_right[i];
  130. }
  131. #endif // defined(SPLIT_KEYBOARD) && defined(ENCODER_RESOLUTIONS)
  132. encoder_quadrature_post_init();
  133. }
  134. static void encoder_handle_state_change(uint8_t index, uint8_t state) {
  135. uint8_t i = index;
  136. #ifdef SPLIT_KEYBOARD
  137. index += thisHand;
  138. #endif
  139. #ifdef ENCODER_RESOLUTIONS
  140. const uint8_t resolution = encoder_resolutions[index];
  141. #else
  142. const uint8_t resolution = ENCODER_RESOLUTION;
  143. #endif
  144. encoder_pulses[i] += encoder_LUT[state & 0xF];
  145. #ifdef ENCODER_DEFAULT_POS
  146. if ((encoder_pulses[i] >= resolution) || (encoder_pulses[i] <= -resolution) || ((state & 0x3) == ENCODER_DEFAULT_POS)) {
  147. if (encoder_pulses[i] >= 1) {
  148. #else
  149. if (encoder_pulses[i] >= resolution) {
  150. #endif
  151. encoder_queue_event(index, ENCODER_COUNTER_CLOCKWISE);
  152. }
  153. #ifdef ENCODER_DEFAULT_POS
  154. if (encoder_pulses[i] <= -1) {
  155. #else
  156. if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
  157. #endif
  158. encoder_queue_event(index, ENCODER_CLOCKWISE);
  159. }
  160. encoder_pulses[i] %= resolution;
  161. #ifdef ENCODER_DEFAULT_POS
  162. encoder_pulses[i] = 0;
  163. }
  164. #endif
  165. }
  166. void encoder_quadrature_handle_read(uint8_t index, uint8_t pin_a_state, uint8_t pin_b_state) {
  167. uint8_t state = pin_a_state | (pin_b_state << 1);
  168. if ((encoder_state[index] & 0x3) != state) {
  169. encoder_state[index] <<= 2;
  170. encoder_state[index] |= state;
  171. encoder_handle_state_change(index, encoder_state[index]);
  172. }
  173. }
  174. __attribute__((weak)) void encoder_driver_task(void) {
  175. for (uint8_t i = 0; i < thisCount; i++) {
  176. encoder_quadrature_handle_read(i, encoder_quadrature_read_pin(i, false), encoder_quadrature_read_pin(i, true));
  177. }
  178. }