You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

111 lines
3.6 KiB

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