logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

encoder.h (3875B)


  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. // ======== DEPRECATED DEFINES - DO NOT USE ========
  23. #ifdef ENCODERS_PAD_A
  24. # define ENCODER_A_PINS ENCODERS_PAD_A
  25. #endif
  26. #ifdef ENCODERS_PAD_B
  27. # define ENCODER_B_PINS ENCODERS_PAD_B
  28. #endif
  29. #ifdef ENCODERS_PAD_A_RIGHT
  30. # define ENCODER_A_PINS_RIGHT ENCODERS_PAD_A_RIGHT
  31. #endif
  32. #ifdef ENCODERS_PAD_B_RIGHT
  33. # define ENCODER_B_PINS_RIGHT ENCODERS_PAD_B_RIGHT
  34. #endif
  35. // ========
  36. #ifdef ENCODER_ENABLE
  37. __attribute__((weak)) bool should_process_encoder(void);
  38. void encoder_init(void);
  39. bool encoder_task(void);
  40. bool encoder_queue_event(uint8_t index, bool clockwise);
  41. bool encoder_dequeue_event(uint8_t *index, bool *clockwise);
  42. bool encoder_update_kb(uint8_t index, bool clockwise);
  43. bool encoder_update_user(uint8_t index, bool clockwise);
  44. # ifdef SPLIT_KEYBOARD
  45. # if defined(ENCODER_A_PINS_RIGHT)
  46. # ifndef NUM_ENCODERS_LEFT
  47. # define NUM_ENCODERS_LEFT ARRAY_SIZE(((pin_t[])ENCODER_A_PINS))
  48. # endif
  49. # ifndef NUM_ENCODERS_RIGHT
  50. # define NUM_ENCODERS_RIGHT ARRAY_SIZE(((pin_t[])ENCODER_A_PINS_RIGHT))
  51. # endif
  52. # else
  53. # ifndef NUM_ENCODERS_LEFT
  54. # define NUM_ENCODERS_LEFT ARRAY_SIZE(((pin_t[])ENCODER_A_PINS))
  55. # endif
  56. # ifndef NUM_ENCODERS_RIGHT
  57. # define NUM_ENCODERS_RIGHT NUM_ENCODERS_LEFT
  58. # endif
  59. # endif
  60. # ifndef NUM_ENCODERS
  61. # define NUM_ENCODERS (NUM_ENCODERS_LEFT + NUM_ENCODERS_RIGHT)
  62. # endif
  63. # else // SPLIT_KEYBOARD
  64. # ifndef NUM_ENCODERS
  65. # define NUM_ENCODERS ARRAY_SIZE(((pin_t[])ENCODER_A_PINS))
  66. # endif
  67. # define NUM_ENCODERS_LEFT NUM_ENCODERS
  68. # define NUM_ENCODERS_RIGHT 0
  69. # endif // SPLIT_KEYBOARD
  70. # define NUM_ENCODERS_MAX_PER_SIDE MAX(NUM_ENCODERS_LEFT, NUM_ENCODERS_RIGHT)
  71. # ifndef MAX_QUEUED_ENCODER_EVENTS
  72. # define MAX_QUEUED_ENCODER_EVENTS MAX(4, ((NUM_ENCODERS_MAX_PER_SIDE) + 1))
  73. # endif // MAX_QUEUED_ENCODER_EVENTS
  74. typedef struct encoder_event_t {
  75. uint8_t index : 7;
  76. uint8_t clockwise : 1;
  77. } encoder_event_t;
  78. typedef struct encoder_events_t {
  79. uint8_t enqueued;
  80. uint8_t dequeued;
  81. uint8_t head;
  82. uint8_t tail;
  83. encoder_event_t queue[MAX_QUEUED_ENCODER_EVENTS];
  84. } encoder_events_t;
  85. // Get the current queued events
  86. void encoder_retrieve_events(encoder_events_t *events);
  87. // Encoder event queue management
  88. bool encoder_queue_event_advanced(encoder_events_t *events, uint8_t index, bool clockwise);
  89. bool encoder_dequeue_event_advanced(encoder_events_t *events, uint8_t *index, bool *clockwise);
  90. // Reset the queue to be empty
  91. void encoder_signal_queue_drain(void);
  92. # ifdef ENCODER_MAP_ENABLE
  93. # define NUM_DIRECTIONS 2
  94. # define ENCODER_CCW_CW(ccw, cw) \
  95. { (cw), (ccw) }
  96. extern const uint16_t encoder_map[][NUM_ENCODERS][NUM_DIRECTIONS];
  97. # endif // ENCODER_MAP_ENABLE
  98. // "Custom encoder lite" support
  99. void encoder_driver_init(void);
  100. void encoder_driver_task(void);
  101. #endif // ENCODER_ENABLE