encoder.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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) {
  54. wait_us(100);
  55. }
  56. __attribute__((weak)) bool encoder_update_user(uint8_t index, bool clockwise) {
  57. return true;
  58. }
  59. __attribute__((weak)) bool encoder_update_kb(uint8_t index, bool clockwise) {
  60. return encoder_update_user(index, clockwise);
  61. }
  62. void encoder_init(void) {
  63. #if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
  64. if (!isLeftHand) {
  65. const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT;
  66. const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT;
  67. # if defined(ENCODER_RESOLUTIONS_RIGHT)
  68. const uint8_t encoder_resolutions_right[] = ENCODER_RESOLUTIONS_RIGHT;
  69. # endif
  70. for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
  71. encoders_pad_a[i] = encoders_pad_a_right[i];
  72. encoders_pad_b[i] = encoders_pad_b_right[i];
  73. # if defined(ENCODER_RESOLUTIONS_RIGHT)
  74. encoder_resolutions[i] = encoder_resolutions_right[i];
  75. # endif
  76. }
  77. }
  78. #endif
  79. for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
  80. setPinInputHigh(encoders_pad_a[i]);
  81. setPinInputHigh(encoders_pad_b[i]);
  82. }
  83. encoder_wait_pullup_charge();
  84. for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
  85. encoder_state[i] = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  86. }
  87. #ifdef SPLIT_KEYBOARD
  88. thisHand = isLeftHand ? 0 : NUMBER_OF_ENCODERS;
  89. thatHand = NUMBER_OF_ENCODERS - thisHand;
  90. #endif
  91. }
  92. static bool encoder_update(uint8_t index, uint8_t state) {
  93. bool changed = false;
  94. uint8_t i = index;
  95. #ifdef ENCODER_RESOLUTIONS
  96. uint8_t resolution = encoder_resolutions[i];
  97. #else
  98. uint8_t resolution = ENCODER_RESOLUTION;
  99. #endif
  100. #ifdef SPLIT_KEYBOARD
  101. index += thisHand;
  102. #endif
  103. encoder_pulses[i] += encoder_LUT[state & 0xF];
  104. if (encoder_pulses[i] >= resolution) {
  105. encoder_value[index]++;
  106. changed = true;
  107. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  108. }
  109. if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
  110. encoder_value[index]--;
  111. changed = true;
  112. encoder_update_kb(index, ENCODER_CLOCKWISE);
  113. }
  114. encoder_pulses[i] %= resolution;
  115. #ifdef ENCODER_DEFAULT_POS
  116. if ((state & 0x3) == ENCODER_DEFAULT_POS) {
  117. encoder_pulses[i] = 0;
  118. }
  119. #endif
  120. return changed;
  121. }
  122. bool encoder_read(void) {
  123. bool changed = false;
  124. for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
  125. encoder_state[i] <<= 2;
  126. encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  127. changed |= encoder_update(i, encoder_state[i]);
  128. }
  129. return changed;
  130. }
  131. #ifdef SPLIT_KEYBOARD
  132. void last_encoder_activity_trigger(void);
  133. void encoder_state_raw(uint8_t* slave_state) {
  134. memcpy(slave_state, &encoder_value[thisHand], sizeof(uint8_t) * NUMBER_OF_ENCODERS);
  135. }
  136. void encoder_update_raw(uint8_t* slave_state) {
  137. bool changed = false;
  138. for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
  139. uint8_t index = i + thatHand;
  140. int8_t delta = slave_state[i] - encoder_value[index];
  141. while (delta > 0) {
  142. delta--;
  143. encoder_value[index]++;
  144. changed = true;
  145. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  146. }
  147. while (delta < 0) {
  148. delta++;
  149. encoder_value[index]--;
  150. changed = true;
  151. encoder_update_kb(index, ENCODER_CLOCKWISE);
  152. }
  153. }
  154. // Update the last encoder input time -- handled external to encoder_read() when we're running a split
  155. if (changed) last_encoder_activity_trigger();
  156. }
  157. #endif