encoder.c 3.1 KB

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