encoder.c 3.6 KB

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