encoder.c 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // for memcpy
  19. #include <string.h>
  20. #ifndef ENCODER_RESOLUTION
  21. # define ENCODER_RESOLUTION 4
  22. #endif
  23. #ifndef NUMBER_OF_ENCODERS
  24. # error "Number of encoders not defined by NUMBER_OF_ENCODERS"
  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. static pin_t encoders_pad_a[NUMBER_OF_ENCODERS] = ENCODERS_PAD_A;
  30. static pin_t encoders_pad_b[NUMBER_OF_ENCODERS] = ENCODERS_PAD_B;
  31. static int8_t encoder_LUT[] = {0, -1, 1, 0, 1, 0, 0, -1, -1, 0, 0, 1, 0, 1, -1, 0};
  32. static uint8_t encoder_state[NUMBER_OF_ENCODERS] = {0};
  33. #ifdef SPLIT_KEYBOARD
  34. // slave half encoders come over as second set of encoders
  35. static int8_t encoder_value[NUMBER_OF_ENCODERS * 2] = {0};
  36. #else
  37. static int8_t encoder_value[NUMBER_OF_ENCODERS] = {0};
  38. #endif
  39. __attribute__((weak)) void encoder_update_user(int8_t index, bool clockwise) {}
  40. __attribute__((weak)) void encoder_update_kb(int8_t index, bool clockwise) { encoder_update_user(index, clockwise); }
  41. void encoder_init(void) {
  42. for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
  43. setPinInputHigh(encoders_pad_a[i]);
  44. setPinInputHigh(encoders_pad_b[i]);
  45. encoder_state[i] = (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  46. }
  47. }
  48. void encoder_read(void) {
  49. for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
  50. encoder_state[i] <<= 2;
  51. encoder_state[i] |= (readPin(encoders_pad_a[i]) << 0) | (readPin(encoders_pad_b[i]) << 1);
  52. encoder_value[i] += encoder_LUT[encoder_state[i] & 0xF];
  53. if (encoder_value[i] >= ENCODER_RESOLUTION) {
  54. encoder_update_kb(i, false);
  55. }
  56. if (encoder_value[i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
  57. encoder_update_kb(i, true);
  58. }
  59. encoder_value[i] %= ENCODER_RESOLUTION;
  60. }
  61. }
  62. #ifdef SPLIT_KEYBOARD
  63. void encoder_state_raw(uint8_t* slave_state) { memcpy(slave_state, encoder_state, sizeof(encoder_state)); }
  64. void encoder_update_raw(uint8_t* slave_state) {
  65. for (int i = 0; i < NUMBER_OF_ENCODERS; i++) {
  66. encoder_value[NUMBER_OF_ENCODERS + i] += encoder_LUT[slave_state[i] & 0xF];
  67. if (encoder_value[NUMBER_OF_ENCODERS + i] >= ENCODER_RESOLUTION) {
  68. encoder_update_kb(NUMBER_OF_ENCODERS + i, false);
  69. }
  70. if (encoder_value[NUMBER_OF_ENCODERS + i] <= -ENCODER_RESOLUTION) { // direction is arbitrary here, but this clockwise
  71. encoder_update_kb(NUMBER_OF_ENCODERS + i, true);
  72. }
  73. encoder_value[NUMBER_OF_ENCODERS + i] %= ENCODER_RESOLUTION;
  74. }
  75. }
  76. #endif