encoder.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. #if !defined(ENCODER_RESOLUTIONS) && !defined(ENCODER_RESOLUTION)
  24. # define ENCODER_RESOLUTION 4
  25. #endif
  26. #if !defined(ENCODERS_PAD_A) || !defined(ENCODERS_PAD_B)
  27. # error "No encoder pads defined by ENCODERS_PAD_A and ENCODERS_PAD_B"
  28. #endif
  29. #define NUMBER_OF_ENCODERS (sizeof(encoders_pad_a) / sizeof(pin_t))
  30. static pin_t encoders_pad_a[] = ENCODERS_PAD_A;
  31. static pin_t encoders_pad_b[] = ENCODERS_PAD_B;
  32. #ifdef ENCODER_RESOLUTIONS
  33. static uint8_t encoder_resolutions[] = ENCODER_RESOLUTIONS;
  34. #endif
  35. #ifndef ENCODER_DIRECTION_FLIP
  36. # define ENCODER_CLOCKWISE true
  37. # define ENCODER_COUNTER_CLOCKWISE false
  38. #else
  39. # define ENCODER_CLOCKWISE false
  40. # define ENCODER_COUNTER_CLOCKWISE true
  41. #endif
  42. static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
  43. static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0};
  44. static int8_t encoder_pulses[NUMBER_OF_ENCODERS] = {0};
  45. #ifdef SPLIT_KEYBOARD
  46. // right half encoders come over as second set of encoders
  47. static uint8_t encoder_value[NUMBER_OF_ENCODERS * 2] = {0};
  48. // row offsets for each hand
  49. static uint8_t thisHand, thatHand;
  50. #else
  51. static uint8_t encoder_value[NUMBER_OF_ENCODERS] = {0};
  52. #endif
  53. __attribute__((weak)) void encoder_wait_pullup_charge(void) { wait_us(100); }
  54. __attribute__((weak)) bool encoder_update_user(uint8_t index, bool clockwise) { return true; }
  55. __attribute__((weak)) bool encoder_update_kb(uint8_t index, bool clockwise) { return encoder_update_user(index, clockwise); }
  56. void encoder_init(void) {
  57. #if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
  58. if (!isLeftHand) {
  59. const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT;
  60. const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT;
  61. # if defined(ENCODER_RESOLUTIONS_RIGHT)
  62. const uint8_t encoder_resolutions_right[] = ENCODER_RESOLUTIONS_RIGHT;
  63. # endif
  64. for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
  65. encoders_pad_a[i] = encoders_pad_a_right[i];
  66. encoders_pad_b[i] = encoders_pad_b_right[i];
  67. # if defined(ENCODER_RESOLUTIONS_RIGHT)
  68. encoder_resolutions[i] = encoder_resolutions_right[i];
  69. # endif
  70. }
  71. }
  72. #endif
  73. for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
  74. setPinInputHigh(encoders_pad_a[i]);
  75. setPinInputHigh(encoders_pad_b[i]);
  76. }
  77. encoder_wait_pullup_charge();
  78. for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
  79. encoder_state[i] = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  80. }
  81. #ifdef SPLIT_KEYBOARD
  82. thisHand = isLeftHand ? 0 : NUMBER_OF_ENCODERS;
  83. thatHand = NUMBER_OF_ENCODERS - thisHand;
  84. #endif
  85. }
  86. static bool encoder_update(uint8_t index, uint8_t state) {
  87. bool changed = false;
  88. uint8_t i = index;
  89. #ifdef ENCODER_RESOLUTIONS
  90. uint8_t resolution = encoder_resolutions[i];
  91. #else
  92. uint8_t resolution = ENCODER_RESOLUTION;
  93. #endif
  94. #ifdef SPLIT_KEYBOARD
  95. index += thisHand;
  96. #endif
  97. encoder_pulses[i] += encoder_LUT[state & 0xF];
  98. if (encoder_pulses[i] >= resolution) {
  99. encoder_value[index]++;
  100. changed = true;
  101. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  102. }
  103. if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
  104. encoder_value[index]--;
  105. changed = true;
  106. encoder_update_kb(index, ENCODER_CLOCKWISE);
  107. }
  108. encoder_pulses[i] %= resolution;
  109. #ifdef ENCODER_DEFAULT_POS
  110. if ((state & 0x3) == ENCODER_DEFAULT_POS) {
  111. encoder_pulses[i] = 0;
  112. }
  113. #endif
  114. return changed;
  115. }
  116. bool encoder_read(void) {
  117. bool changed = false;
  118. for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
  119. encoder_state[i] <<= 2;
  120. encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  121. changed |= encoder_update(i, encoder_state[i]);
  122. }
  123. return changed;
  124. }
  125. #ifdef SPLIT_KEYBOARD
  126. void last_encoder_activity_trigger(void);
  127. void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, &encoder_value[thisHand], sizeof(uint8_t) * NUMBER_OF_ENCODERS); }
  128. void encoder_update_raw(uint8_t* slave_state) {
  129. bool changed = false;
  130. for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
  131. uint8_t index = i + thatHand;
  132. int8_t delta = slave_state[i] - encoder_value[index];
  133. while (delta > 0) {
  134. delta--;
  135. encoder_value[index]++;
  136. changed = true;
  137. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  138. }
  139. while (delta < 0) {
  140. delta++;
  141. encoder_value[index]--;
  142. changed = true;
  143. encoder_update_kb(index, ENCODER_CLOCKWISE);
  144. }
  145. }
  146. // Update the last encoder input time -- handled external to encoder_read() when we're running a split
  147. if (changed) last_encoder_activity_trigger();
  148. }
  149. #endif