|
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 /** |
|
22 * @file rotary_encoder.h |
|
23 * @brief Interface definitions for the ESP32-compatible Incremental Rotary Encoder component. |
|
24 * |
|
25 * This component provides a means to interface with a typical rotary encoder such as the EC11 or LPD3806. |
|
26 * These encoders produce a quadrature signal on two outputs, which can be used to track the position and |
|
27 * direction as movement occurs. |
|
28 * |
|
29 * This component provides functions to initialise the GPIOs and install appropriate interrupt handlers to |
|
30 * track a single device's position. An event queue is used to provide a way for a user task to obtain |
|
31 * position information from the component as it is generated. |
|
32 * |
|
33 * Note that the queue is of length 1, and old values will be overwritten. Using a longer queue is |
|
34 * possible with some minor modifications however newer values are lost if the queue overruns. A circular |
|
35 * buffer where old values are lost would be better (maybe StreamBuffer in FreeRTOS 10.0.0?). |
|
36 */ |
|
37 |
|
38 #ifndef ROTARY_ENCODER_H |
|
39 #define ROTARY_ENCODER_H |
|
40 |
|
41 #include <stdbool.h> |
|
42 #include <stdint.h> |
|
43 |
|
44 #include "freertos/FreeRTOS.h" |
|
45 #include "freertos/queue.h" |
|
46 #include "esp_err.h" |
|
47 #include "driver/gpio.h" |
|
48 |
|
49 #ifdef __cplusplus |
|
50 extern "C" { |
|
51 #endif |
|
52 |
|
53 typedef int32_t rotary_encoder_position_t; |
|
54 |
|
55 /** |
|
56 * @brief Enum representing the direction of rotation. |
|
57 */ |
|
58 typedef enum |
|
59 { |
|
60 ROTARY_ENCODER_DIRECTION_NOT_SET = 0, ///< Direction not yet known (stationary since reset) |
|
61 ROTARY_ENCODER_DIRECTION_CLOCKWISE, |
|
62 ROTARY_ENCODER_DIRECTION_COUNTER_CLOCKWISE, |
|
63 } rotary_encoder_direction_t; |
|
64 |
|
65 // Used internally |
|
66 ///@cond INTERNAL |
|
67 #define TABLE_COLS 4 |
|
68 typedef uint8_t table_row_t[TABLE_COLS]; |
|
69 ///@endcond |
|
70 |
|
71 /** |
|
72 * @brief Struct represents the current state of the device in terms of incremental position and direction of last movement |
|
73 */ |
|
74 typedef struct |
|
75 { |
|
76 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. |
|
77 rotary_encoder_direction_t direction; ///< Direction of last movement. Set to NOT_SET on reset. |
|
78 } rotary_encoder_state_t; |
|
79 |
|
80 /** |
|
81 * @brief Struct carries all the information needed by this driver to manage the rotary encoder device. |
|
82 * The fields of this structure should not be accessed directly. |
|
83 */ |
|
84 typedef struct |
|
85 { |
|
86 gpio_num_t pin_a; ///< GPIO for Signal A from the rotary encoder device |
|
87 gpio_num_t pin_b; ///< GPIO for Signal B from the rotary encoder device |
|
88 QueueHandle_t queue; ///< Handle for event queue, created by ::rotary_encoder_create_queue |
|
89 const table_row_t * table; ///< Pointer to active state transition table |
|
90 uint8_t table_state; ///< Internal state |
|
91 volatile rotary_encoder_state_t state; ///< Device state |
|
92 } rotary_encoder_info_t; |
|
93 |
|
94 /** |
|
95 * @brief Struct represents a queued event, used to communicate current position to a waiting task |
|
96 */ |
|
97 typedef struct |
|
98 { |
|
99 rotary_encoder_state_t state; ///< The device state corresponding to this event |
|
100 } rotary_encoder_event_t; |
|
101 |
|
102 /** |
|
103 * @brief Initialise the rotary encoder device with the specified GPIO pins and full step increments. |
|
104 * This function will set up the GPIOs as needed, |
|
105 * Note: this function assumes that gpio_install_isr_service(0) has already been called. |
|
106 * @param[in, out] info Pointer to allocated rotary encoder info structure. |
|
107 * @param[in] pin_a GPIO number for rotary encoder output A. |
|
108 * @param[in] pin_b GPIO number for rotary encoder output B. |
|
109 * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred. |
|
110 */ |
|
111 esp_err_t rotary_encoder_init(rotary_encoder_info_t * info, gpio_num_t pin_a, gpio_num_t pin_b); |
|
112 |
|
113 /** |
|
114 * @brief Enable half-stepping mode. This generates twice as many counted steps per rotation. |
|
115 * @param[in] info Pointer to initialised rotary encoder info structure. |
|
116 * @param[in] enable If true, count half steps. If false, only count full steps. |
|
117 * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred. |
|
118 */ |
|
119 esp_err_t rotary_encoder_enable_half_steps(rotary_encoder_info_t * info, bool enable); |
|
120 |
|
121 /** |
|
122 * @brief Reverse (flip) the sense of the direction. |
|
123 * Use this if clockwise/counterclockwise are not what you expect. |
|
124 * @param[in] info Pointer to initialised rotary encoder info structure. |
|
125 * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred. |
|
126 */ |
|
127 esp_err_t rotary_encoder_flip_direction(rotary_encoder_info_t * info); |
|
128 |
|
129 /** |
|
130 * @brief Remove the interrupt handlers installed by ::rotary_encoder_init. |
|
131 * Note: GPIOs will be left in the state they were configured by ::rotary_encoder_init. |
|
132 * @param[in] info Pointer to initialised rotary encoder info structure. |
|
133 * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred. |
|
134 */ |
|
135 esp_err_t rotary_encoder_uninit(rotary_encoder_info_t * info); |
|
136 |
|
137 /** |
|
138 * @brief Create a queue handle suitable for use as an event queue. |
|
139 * @return A handle to a new queue suitable for use as an event queue. |
|
140 */ |
|
141 QueueHandle_t rotary_encoder_create_queue(void); |
|
142 |
|
143 /** |
|
144 * @brief Set the driver to use the specified queue as an event queue. |
|
145 * It is recommended that a queue constructed by ::rotary_encoder_create_queue is used. |
|
146 * @param[in] info Pointer to initialised rotary encoder info structure. |
|
147 * @param[in] queue Handle to queue suitable for use as an event queue. See ::rotary_encoder_create_queue. |
|
148 * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred. |
|
149 */ |
|
150 esp_err_t rotary_encoder_set_queue(rotary_encoder_info_t * info, QueueHandle_t queue); |
|
151 |
|
152 /** |
|
153 * @brief Get the current position of the rotary encoder. |
|
154 * @param[in] info Pointer to initialised rotary encoder info structure. |
|
155 * @param[in, out] state Pointer to an allocated rotary_encoder_state_t struct that will |
|
156 * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred. |
|
157 */ |
|
158 esp_err_t rotary_encoder_get_state(const rotary_encoder_info_t * info, rotary_encoder_state_t * state); |
|
159 |
|
160 /** |
|
161 * @brief Reset the current position of the rotary encoder to zero. |
|
162 * @param[in] info Pointer to initialised rotary encoder info structure. |
|
163 * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred. |
|
164 */ |
|
165 esp_err_t rotary_encoder_reset(rotary_encoder_info_t * info); |
|
166 |
|
167 |
|
168 #ifdef __cplusplus |
|
169 } |
|
170 #endif |
|
171 |
|
172 #endif // ROTARY_ENCODER_H |