encoder.c 4.1 KB

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