encoder.c 9.8 KB

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