encoder.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. #ifndef 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. #ifndef ENCODER_DIRECTION_FLIP
  33. # define ENCODER_CLOCKWISE true
  34. # define ENCODER_COUNTER_CLOCKWISE false
  35. #else
  36. # define ENCODER_CLOCKWISE false
  37. # define ENCODER_COUNTER_CLOCKWISE true
  38. #endif
  39. static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
  40. static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0};
  41. static int8_t encoder_pulses[NUMBER_OF_ENCODERS] = {0};
  42. #ifdef SPLIT_KEYBOARD
  43. // right half encoders come over as second set of encoders
  44. static uint8_t encoder_value[NUMBER_OF_ENCODERS * 2] = {0};
  45. // row offsets for each hand
  46. static uint8_t thisHand, thatHand;
  47. #else
  48. static uint8_t encoder_value[NUMBER_OF_ENCODERS] = {0};
  49. #endif
  50. __attribute__((weak)) void encoder_update_user(int8_t index, bool clockwise) {}
  51. __attribute__((weak)) void encoder_update_kb(int8_t index, bool clockwise) { encoder_update_user(index, clockwise); }
  52. void encoder_init(void) {
  53. #if defined(SPLIT_KEYBOARD) && defined(ENCODERS_PAD_A_RIGHT) && defined(ENCODERS_PAD_B_RIGHT)
  54. if (!isLeftHand) {
  55. const pin_t encoders_pad_a_right[] = ENCODERS_PAD_A_RIGHT;
  56. const pin_t encoders_pad_b_right[] = ENCODERS_PAD_B_RIGHT;
  57. for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
  58. encoders_pad_a[i] = encoders_pad_a_right[i];
  59. encoders_pad_b[i] = encoders_pad_b_right[i];
  60. }
  61. }
  62. #endif
  63. for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
  64. setPinInputHigh(encoders_pad_a[i]);
  65. setPinInputHigh(encoders_pad_b[i]);
  66. encoder_state[i] = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  67. }
  68. #ifdef SPLIT_KEYBOARD
  69. thisHand = isLeftHand ? 0 : NUMBER_OF_ENCODERS;
  70. thatHand = NUMBER_OF_ENCODERS - thisHand;
  71. #endif
  72. }
  73. static void encoder_update(int8_t index, uint8_t state) {
  74. uint8_t i = index;
  75. #ifdef SPLIT_KEYBOARD
  76. index += thisHand;
  77. #endif
  78. encoder_pulses[i] += encoder_LUT[state & 0xF];
  79. if (encoder_pulses[i] >= ENCODER_RESOLUTION) {
  80. encoder_value[index]++;
  81. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  82. }
  83. if (encoder_pulses[i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
  84. encoder_value[index]--;
  85. encoder_update_kb(index, ENCODER_CLOCKWISE);
  86. }
  87. encoder_pulses[i] %= ENCODER_RESOLUTION;
  88. }
  89. void encoder_read(void) {
  90. for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
  91. encoder_state[i] <<= 2;
  92. encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  93. encoder_update(i, encoder_state[i]);
  94. }
  95. }
  96. #ifdef SPLIT_KEYBOARD
  97. void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, &encoder_value[thisHand], sizeof(uint8_t) * NUMBER_OF_ENCODERS); }
  98. void encoder_update_raw(uint8_t* slave_state) {
  99. for (uint8_t i = 0; i < NUMBER_OF_ENCODERS; i++) {
  100. uint8_t index = i + thatHand;
  101. int8_t delta = slave_state[i] - encoder_value[index];
  102. while (delta > 0) {
  103. delta--;
  104. encoder_value[index]++;
  105. encoder_update_kb(index, ENCODER_COUNTER_CLOCKWISE);
  106. }
  107. while (delta < 0) {
  108. delta++;
  109. encoder_value[index]--;
  110. encoder_update_kb(index, ENCODER_CLOCKWISE);
  111. }
  112. }
  113. }
  114. #endif