rotary_encoder.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (c) 2019 David Antliff
  3. * Copyright 2011 Ben Buxton
  4. *
  5. * This file is part of the esp32-rotary-encoder component.
  6. *
  7. * esp32-rotary-encoder is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * esp32-rotary-encoder is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with esp32-rotary-encoder. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * @file rotary_encoder.h
  22. * @brief Interface definitions for the ESP32-compatible Incremental Rotary Encoder component.
  23. *
  24. * This component provides a means to interface with a typical rotary encoder such as the EC11 or LPD3806.
  25. * These encoders produce a quadrature signal on two outputs, which can be used to track the position and
  26. * direction as movement occurs.
  27. *
  28. * This component provides functions to initialise the GPIOs and install appropriate interrupt handlers to
  29. * track a single device's position. An event queue is used to provide a way for a user task to obtain
  30. * position information from the component as it is generated.
  31. *
  32. * Note that the queue is of length 1, and old values will be overwritten. Using a longer queue is
  33. * possible with some minor modifications however newer values are lost if the queue overruns. A circular
  34. * buffer where old values are lost would be better (maybe StreamBuffer in FreeRTOS 10.0.0?).
  35. */
  36. #ifndef ROTARY_ENCODER_H
  37. #define ROTARY_ENCODER_H
  38. #include <stdbool.h>
  39. #include <stdint.h>
  40. #include "freertos/FreeRTOS.h"
  41. #include "freertos/queue.h"
  42. #include "esp_err.h"
  43. #include "driver/gpio.h"
  44. #ifdef __cplusplus
  45. extern "C" {
  46. #endif
  47. typedef int32_t rotary_encoder_position_t;
  48. /**
  49. * @brief Enum representing the direction of rotation.
  50. */
  51. typedef enum
  52. {
  53. ROTARY_ENCODER_DIRECTION_NOT_SET = 0, ///< Direction not yet known (stationary since reset)
  54. ROTARY_ENCODER_DIRECTION_CLOCKWISE,
  55. ROTARY_ENCODER_DIRECTION_COUNTER_CLOCKWISE,
  56. } rotary_encoder_direction_t;
  57. // Used internally
  58. ///@cond INTERNAL
  59. #define TABLE_COLS 4
  60. typedef uint8_t table_row_t[TABLE_COLS];
  61. ///@endcond
  62. /**
  63. * @brief Struct represents the current state of the device in terms of incremental position and direction of last movement
  64. */
  65. typedef struct
  66. {
  67. rotary_encoder_position_t position; ///< Numerical position since reset. This value increments on clockwise rotation, and decrements on counter-clockewise rotation. Counts full or half steps depending on mode. Set to zero on reset.
  68. rotary_encoder_direction_t direction; ///< Direction of last movement. Set to NOT_SET on reset.
  69. } rotary_encoder_state_t;
  70. /**
  71. * @brief Struct carries all the information needed by this driver to manage the rotary encoder device.
  72. * The fields of this structure should not be accessed directly.
  73. */
  74. typedef struct
  75. {
  76. gpio_num_t pin_a; ///< GPIO for Signal A from the rotary encoder device
  77. gpio_num_t pin_b; ///< GPIO for Signal B from the rotary encoder device
  78. QueueHandle_t queue; ///< Handle for event queue, created by ::rotary_encoder_create_queue
  79. const table_row_t * table; ///< Pointer to active state transition table
  80. uint8_t table_state; ///< Internal state
  81. volatile rotary_encoder_state_t state; ///< Device state
  82. } rotary_encoder_info_t;
  83. /**
  84. * @brief Struct represents a queued event, used to communicate current position to a waiting task
  85. */
  86. typedef struct
  87. {
  88. rotary_encoder_state_t state; ///< The device state corresponding to this event
  89. } rotary_encoder_event_t;
  90. /**
  91. * @brief Initialise the rotary encoder device with the specified GPIO pins and full step increments.
  92. * This function will set up the GPIOs as needed,
  93. * Note: this function assumes that gpio_install_isr_service(0) has already been called.
  94. * @param[in, out] info Pointer to allocated rotary encoder info structure.
  95. * @param[in] pin_a GPIO number for rotary encoder output A.
  96. * @param[in] pin_b GPIO number for rotary encoder output B.
  97. * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
  98. */
  99. esp_err_t rotary_encoder_init(rotary_encoder_info_t * info, gpio_num_t pin_a, gpio_num_t pin_b);
  100. /**
  101. * @brief Enable half-stepping mode. This generates twice as many counted steps per rotation.
  102. * @param[in] info Pointer to initialised rotary encoder info structure.
  103. * @param[in] enable If true, count half steps. If false, only count full steps.
  104. * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
  105. */
  106. esp_err_t rotary_encoder_enable_half_steps(rotary_encoder_info_t * info, bool enable);
  107. /**
  108. * @brief Reverse (flip) the sense of the direction.
  109. * Use this if clockwise/counterclockwise are not what you expect.
  110. * @param[in] info Pointer to initialised rotary encoder info structure.
  111. * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
  112. */
  113. esp_err_t rotary_encoder_flip_direction(rotary_encoder_info_t * info);
  114. /**
  115. * @brief Remove the interrupt handlers installed by ::rotary_encoder_init.
  116. * Note: GPIOs will be left in the state they were configured by ::rotary_encoder_init.
  117. * @param[in] info Pointer to initialised rotary encoder info structure.
  118. * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
  119. */
  120. esp_err_t rotary_encoder_uninit(rotary_encoder_info_t * info);
  121. /**
  122. * @brief Create a queue handle suitable for use as an event queue.
  123. * @return A handle to a new queue suitable for use as an event queue.
  124. */
  125. QueueHandle_t rotary_encoder_create_queue(void);
  126. /**
  127. * @brief Set the driver to use the specified queue as an event queue.
  128. * It is recommended that a queue constructed by ::rotary_encoder_create_queue is used.
  129. * @param[in] info Pointer to initialised rotary encoder info structure.
  130. * @param[in] queue Handle to queue suitable for use as an event queue. See ::rotary_encoder_create_queue.
  131. * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
  132. */
  133. esp_err_t rotary_encoder_set_queue(rotary_encoder_info_t * info, QueueHandle_t queue);
  134. /**
  135. * @brief Get the current position of the rotary encoder.
  136. * @param[in] info Pointer to initialised rotary encoder info structure.
  137. * @param[in, out] state Pointer to an allocated rotary_encoder_state_t struct that will
  138. * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
  139. */
  140. esp_err_t rotary_encoder_get_state(const rotary_encoder_info_t * info, rotary_encoder_state_t * state);
  141. /**
  142. * @brief Reset the current position of the rotary encoder to zero.
  143. * @param[in] info Pointer to initialised rotary encoder info structure.
  144. * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred.
  145. */
  146. esp_err_t rotary_encoder_reset(rotary_encoder_info_t * info);
  147. #ifdef __cplusplus
  148. }
  149. #endif
  150. #endif // ROTARY_ENCODER_H