encoder.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. #pragma once
  18. #include <stdint.h>
  19. #include <stdbool.h>
  20. #include "gpio.h"
  21. #include "util.h"
  22. #ifdef ENCODER_ENABLE
  23. __attribute__((weak)) bool should_process_encoder(void);
  24. void encoder_init(void);
  25. bool encoder_task(void);
  26. bool encoder_queue_event(uint8_t index, bool clockwise);
  27. bool encoder_update_kb(uint8_t index, bool clockwise);
  28. bool encoder_update_user(uint8_t index, bool clockwise);
  29. # ifdef SPLIT_KEYBOARD
  30. # if defined(ENCODERS_PAD_A_RIGHT)
  31. # ifndef NUM_ENCODERS_LEFT
  32. # define NUM_ENCODERS_LEFT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
  33. # endif
  34. # ifndef NUM_ENCODERS_RIGHT
  35. # define NUM_ENCODERS_RIGHT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A_RIGHT))
  36. # endif
  37. # else
  38. # ifndef NUM_ENCODERS_LEFT
  39. # define NUM_ENCODERS_LEFT ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
  40. # endif
  41. # ifndef NUM_ENCODERS_RIGHT
  42. # define NUM_ENCODERS_RIGHT NUM_ENCODERS_LEFT
  43. # endif
  44. # endif
  45. # ifndef NUM_ENCODERS
  46. # define NUM_ENCODERS (NUM_ENCODERS_LEFT + NUM_ENCODERS_RIGHT)
  47. # endif
  48. # else // SPLIT_KEYBOARD
  49. # ifndef NUM_ENCODERS
  50. # define NUM_ENCODERS ARRAY_SIZE(((pin_t[])ENCODERS_PAD_A))
  51. # endif
  52. # define NUM_ENCODERS_LEFT NUM_ENCODERS
  53. # define NUM_ENCODERS_RIGHT 0
  54. # endif // SPLIT_KEYBOARD
  55. # ifndef NUM_ENCODERS
  56. # define NUM_ENCODERS 0
  57. # define NUM_ENCODERS_LEFT 0
  58. # define NUM_ENCODERS_RIGHT 0
  59. # endif // NUM_ENCODERS
  60. # define NUM_ENCODERS_MAX_PER_SIDE MAX(NUM_ENCODERS_LEFT, NUM_ENCODERS_RIGHT)
  61. # ifndef MAX_QUEUED_ENCODER_EVENTS
  62. # define MAX_QUEUED_ENCODER_EVENTS MAX(4, ((NUM_ENCODERS_MAX_PER_SIDE) + 1))
  63. # endif // MAX_QUEUED_ENCODER_EVENTS
  64. typedef struct encoder_event_t {
  65. uint8_t index : 7;
  66. uint8_t clockwise : 1;
  67. } encoder_event_t;
  68. typedef struct encoder_events_t {
  69. uint8_t head;
  70. uint8_t tail;
  71. encoder_event_t queue[MAX_QUEUED_ENCODER_EVENTS];
  72. } encoder_events_t;
  73. // Get the current queued events
  74. void encoder_retrieve_events(encoder_events_t *events);
  75. # ifdef SPLIT_KEYBOARD
  76. void encoder_set_tail_index(uint8_t tail_index);
  77. void encoder_handle_slave_events(encoder_events_t *events);
  78. # endif // SPLIT_KEYBOARD
  79. # ifdef ENCODER_MAP_ENABLE
  80. # define NUM_DIRECTIONS 2
  81. # define ENCODER_CCW_CW(ccw, cw) \
  82. { (cw), (ccw) }
  83. extern const uint16_t encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS];
  84. # endif // ENCODER_MAP_ENABLE
  85. // "Custom encoder lite" support
  86. void encoder_driver_init(void);
  87. void encoder_driver_task(void);
  88. #endif // ENCODER_ENABLE