encoder.c 9.1 KB

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