encoder.c 5.3 KB

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