encoder.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 void encoder_update(int8_t index, uint8_t state) {
  83. uint8_t i = index;
  84. #ifdef ENCODER_RESOLUTIONS
  85. int8_t resolution = encoder_resolutions[i];
  86. #else
  87. int8_t resolution = ENCODER_RESOLUTION;
  88. #endif
  89. #ifdef SPLIT_KEYBOARD
  90. index += thisHand;
  91. #endif
  92. encoder_pulses[i] += encoder_LUT[state & 0xF];
  93. if (encoder_pulses[i] >= resolution) {
  94. encoder_value[index]++;
  95. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  96. }
  97. if (encoder_pulses[i] <= -resolution) { // direction is arbitrary here, but this clockwise
  98. encoder_value[index]--;
  99. encoder_update_kb(index, ENCODER_CLOCKWISE);
  100. }
  101. encoder_pulses[i] %= resolution;
  102. }
  103. void encoder_read(void) {
  104. for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
  105. encoder_state[i] <<= 2;
  106. encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  107. encoder_update(i, encoder_state[i]);
  108. }
  109. }
  110. #ifdef SPLIT_KEYBOARD
  111. void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, &encoder_value[thisHand], sizeof(uint8_t) * NUMBER_OF_ENCODERS); }
  112. void encoder_update_raw(uint8_t* slave_state) {
  113. for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
  114. uint8_t index = i + thatHand;
  115. int8_t delta = slave_state[i] - encoder_value[index];
  116. while (delta > 0) {
  117. delta--;
  118. encoder_value[index]++;
  119. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  120. }
  121. while (delta < 0) {
  122. delta++;
  123. encoder_value[index]--;
  124. encoder_update_kb(index, ENCODER_CLOCKWISE);
  125. }
  126. }
  127. }
  128. #endif