encoder.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. #include "encoder.h"
  18. #ifdef SPLIT_KEYBOARD
  19. # include "split_util.h"
  20. #endif
  21. // for memcpy
  22. #include <string.h>
  23. #ifndef ENCODER_MAP_KEY_DELAY
  24. # include "action.h"
  25. # define ENCODER_MAP_KEY_DELAY TAP_CODE_DELAY
  26. #endif
  27. #if !defined(ENCODER_RESOLUTIONS) && !defined(ENCODER_RESOLUTION)
  28. # define ENCODER_RESOLUTION 4
  29. #endif
  30. #if !defined(ENCODERS_PAD_A) || !defined(ENCODERS_PAD_B)
  31. # error "No encoder pads defined by ENCODERS_PAD_A and ENCODERS_PAD_B"
  32. #endif
  33. extern volatile bool isLeftHand;
  34. static pin_t encoders_pad_a[NUM_ENCODERS_MAX_PER_SIDE] = ENCODERS_PAD_A;
  35. static pin_t encoders_pad_b[NUM_ENCODERS_MAX_PER_SIDE] = ENCODERS_PAD_B;
  36. #ifdef ENCODER_RESOLUTIONS
  37. static uint8_t encoder_resolutions[NUM_ENCODERS] = ENCODER_RESOLUTIONS;
  38. #endif
  39. #ifndef ENCODER_DIRECTION_FLIP
  40. # define ENCODER_CLOCKWISE true
  41. # define ENCODER_COUNTER_CLOCKWISE false
  42. #else
  43. # define ENCODER_CLOCKWISE false
  44. # define ENCODER_COUNTER_CLOCKWISE true
  45. #endif
  46. static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
  47. static uint8_t encoder_state[NUM_ENCODERS] = {0};
  48. static int8_t encoder_pulses[NUM_ENCODERS] = {0};
  49. // encoder counts
  50. static uint8_t thisCount;
  51. #ifdef SPLIT_KEYBOARD
  52. // encoder offsets for each hand
  53. static uint8_t thisHand, thatHand;
  54. // encoder counts for each hand
  55. static uint8_t thatCount;
  56. #endif
  57. static uint8_t encoder_value[NUM_ENCODERS] = {0};
  58. __attribute__((weak)) void encoder_wait_pullup_charge(void) {
  59. wait_us(100);
  60. }
  61. __attribute__((weak)) bool encoder_update_user(uint8_t index, bool clockwise) {
  62. return true;
  63. }
  64. __attribute__((weak)) bool encoder_update_kb(uint8_t index, bool clockwise) {
  65. return encoder_update_user(index, clockwise);
  66. }
  67. void encoder_init(void) {
  68. #ifdef SPLIT_KEYBOARD
  69. thisHand = isLeftHand ? 0 : NUM_ENCODERS_LEFT;
  70. thatHand = NUM_ENCODERS_LEFT - thisHand;
  71. thisCount = isLeftHand ? NUM_ENCODERS_LEFT : NUM_ENCODERS_RIGHT;
  72. thatCount = isLeftHand ? NUM_ENCODERS_RIGHT : NUM_ENCODERS_LEFT;
  73. #else // SPLIT_KEYBOARD
  74. thisCount = NUM_ENCODERS;
  75. #endif
  76. #ifdef ENCODER_TESTS
  77. // Annoying that we have to clear out values during initialisation here, but
  78. // because all the arrays are static locals, rerunning tests in the same
  79. // executable doesn't reset any of these. Kinda crappy having test-only code
  80. // here, but it's the simplest solution.
  81. memset(encoder_value, 0, sizeof(encoder_value));
  82. memset(encoder_state, 0, sizeof(encoder_state));
  83. memset(encoder_pulses, 0, sizeof(encoder_pulses));
  84. static const pin_t encoders_pad_a_left[] = ENCODERS_PAD_A;
  85. static const pin_t encoders_pad_b_left[] = ENCODERS_PAD_B;
  86. for (uint8_t i = 0; i < thisCount; i++) {
  87. encoders_pad_a[i] = encoders_pad_a_left[i];
  88. encoders_pad_b[i] = encoders_pad_b_left[i];
  89. }
  90. #endif
  91. #if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
  92. // Re-initialise the pads if it's the right-hand side
  93. if (!isLeftHand) {
  94. static const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT;
  95. static const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT;
  96. for (uint8_t i = 0; i < thisCount; i++) {
  97. encoders_pad_a[i] = encoders_pad_a_right[i];
  98. encoders_pad_b[i] = encoders_pad_b_right[i];
  99. }
  100. }
  101. #endif // defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
  102. // Encoder resolutions is handled purely master-side, so concatenate the two arrays
  103. #if defined(SPLIT_KEYBOARD) && defined(ENCODER_RESOLUTIONS)
  104. # if defined(ENCODER_RESOLUTIONS_RIGHT)
  105. static const uint8_t encoder_resolutions_right[NUM_ENCODERS_RIGHT] = ENCODER_RESOLUTIONS_RIGHT;
  106. # else // defined(ENCODER_RESOLUTIONS_RIGHT)
  107. static const uint8_t encoder_resolutions_right[NUM_ENCODERS_RIGHT] = ENCODER_RESOLUTIONS;
  108. # endif // defined(ENCODER_RESOLUTIONS_RIGHT)
  109. for (uint8_t i = 0; i < NUM_ENCODERS_RIGHT; i++) {
  110. encoder_resolutions[NUM_ENCODERS_LEFT + i] = encoder_resolutions_right[i];
  111. }
  112. #endif // defined(SPLIT_KEYBOARD) && defined(ENCODER_RESOLUTIONS)
  113. for (uint8_t i = 0; i < thisCount; i++) {
  114. setPinInputHigh(encoders_pad_a[i]);
  115. setPinInputHigh(encoders_pad_b[i]);
  116. }
  117. encoder_wait_pullup_charge();
  118. for (uint8_t i = 0; i < thisCount; i++) {
  119. encoder_state[i] = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  120. }
  121. }
  122. #ifdef ENCODER_MAP_ENABLE
  123. static void encoder_exec_mapping(uint8_t index, bool clockwise) {
  124. // The delays below cater for Windows and its wonderful requirements.
  125. action_exec(clockwise ? ENCODER_CW_EVENT(index, true) : ENCODER_CCW_EVENT(index, true));
  126. # if ENCODER_MAP_KEY_DELAY > 0
  127. wait_ms(ENCODER_MAP_KEY_DELAY);
  128. # endif // ENCODER_MAP_KEY_DELAY > 0
  129. action_exec(clockwise ? ENCODER_CW_EVENT(index, false) : ENCODER_CCW_EVENT(index, false));
  130. # if ENCODER_MAP_KEY_DELAY > 0
  131. wait_ms(ENCODER_MAP_KEY_DELAY);
  132. # endif // ENCODER_MAP_KEY_DELAY > 0
  133. }
  134. #endif // ENCODER_MAP_ENABLE
  135. static bool encoder_update(uint8_t index, uint8_t state) {
  136. bool changed = false;
  137. uint8_t i = index;
  138. #ifdef ENCODER_RESOLUTIONS
  139. const uint8_t resolution = encoder_resolutions[i];
  140. #else
  141. const uint8_t resolution = ENCODER_RESOLUTION;
  142. #endif
  143. #ifdef SPLIT_KEYBOARD
  144. index += thisHand;
  145. #endif
  146. encoder_pulses[i] += encoder_LUT[state & 0xF];
  147. #ifdef ENCODER_DEFAULT_POS
  148. if ((encoder_pulses[i] >= resolution) || (encoder_pulses[i] <= -resolution) || ((state & 0x3) == ENCODER_DEFAULT_POS)) {
  149. if (encoder_pulses[i] >= 1) {
  150. #else
  151. if (encoder_pulses[i] >= resolution) {
  152. #endif
  153. encoder_value[index]++;
  154. changed = true;
  155. #ifdef ENCODER_MAP_ENABLE
  156. encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE);
  157. #else // ENCODER_MAP_ENABLE
  158. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  159. #endif // ENCODER_MAP_ENABLE
  160. }
  161. #ifdef ENCODER_DEFAULT_POS
  162. if (encoder_pulses[i] <= -1) {
  163. #else
  164. if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
  165. #endif
  166. encoder_value[index]--;
  167. changed = true;
  168. #ifdef ENCODER_MAP_ENABLE
  169. encoder_exec_mapping(index, ENCODER_CLOCKWISE);
  170. #else // ENCODER_MAP_ENABLE
  171. encoder_update_kb(index, ENCODER_CLOCKWISE);
  172. #endif // ENCODER_MAP_ENABLE
  173. }
  174. encoder_pulses[i] %= resolution;
  175. #ifdef ENCODER_DEFAULT_POS
  176. encoder_pulses[i] = 0;
  177. }
  178. #endif
  179. return changed;
  180. }
  181. bool encoder_read(void) {
  182. bool changed = false;
  183. for (uint8_t i = 0; i < thisCount; i++) {
  184. uint8_t new_status = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  185. if ((encoder_state[i] & 0x3) != new_status) {
  186. encoder_state[i] <<= 2;
  187. encoder_state[i] |= new_status;
  188. changed |= encoder_update(i, encoder_state[i]);
  189. }
  190. }
  191. return changed;
  192. }
  193. #ifdef SPLIT_KEYBOARD
  194. void last_encoder_activity_trigger(void);
  195. void encoder_state_raw(uint8_t *slave_state) {
  196. memcpy(slave_state, &encoder_value[thisHand], sizeof(uint8_t) * thisCount);
  197. }
  198. void encoder_update_raw(uint8_t *slave_state) {
  199. bool changed = false;
  200. for (uint8_t i = 0; i < thatCount; i++) { // Note inverted logic -- we want the opposite side
  201. const uint8_t index = i + thatHand;
  202. int8_t delta = slave_state[i] - encoder_value[index];
  203. while (delta > 0) {
  204. delta--;
  205. encoder_value[index]++;
  206. changed = true;
  207. # ifdef ENCODER_MAP_ENABLE
  208. encoder_exec_mapping(index, ENCODER_COUNTER_CLOCKWISE);
  209. # else // ENCODER_MAP_ENABLE
  210. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  211. # endif // ENCODER_MAP_ENABLE
  212. }
  213. while (delta < 0) {
  214. delta++;
  215. encoder_value[index]--;
  216. changed = true;
  217. # ifdef ENCODER_MAP_ENABLE
  218. encoder_exec_mapping(index, ENCODER_CLOCKWISE);
  219. # else // ENCODER_MAP_ENABLE
  220. encoder_update_kb(index, ENCODER_CLOCKWISE);
  221. # endif // ENCODER_MAP_ENABLE
  222. }
  223. }
  224. // Update the last encoder input time -- handled external to encoder_read() when we're running a split
  225. if (changed) last_encoder_activity_trigger();
  226. }
  227. #endif