1 // Copyright 2020 Espressif Systems (Shanghai) PTE LTD
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #ifdef __cplusplus
17 extern "C" {
18 #endif
19 
20 #include "esp_err.h"
21 
22 /**
23  * @brief Type of Rotary underlying device handle
24  *
25  */
26 typedef void *rotary_encoder_dev_t;
27 
28 /**
29  * @brief Type of rotary encoder configuration
30  *
31  */
32 typedef struct {
33     rotary_encoder_dev_t dev; /*!< Underlying device handle */
34     int phase_a_gpio_num;     /*!< Phase A GPIO number */
35     int phase_b_gpio_num;     /*!< Phase B GPIO number */
36     int flags;                /*!< Extra flags */
37 } rotary_encoder_config_t;
38 
39 /**
40  * @brief Default rotary encoder configuration
41  *
42  */
43 #define ROTARY_ENCODER_DEFAULT_CONFIG(dev_hdl, gpio_a, gpio_b) \
44     {                                                          \
45         .dev = dev_hdl,                                        \
46         .phase_a_gpio_num = gpio_a,                            \
47         .phase_b_gpio_num = gpio_b,                            \
48         .flags = 0,                                            \
49     }
50 
51 /**
52  * @brief Type of rotary encoder handle
53  *
54  */
55 typedef struct rotary_encoder_t rotary_encoder_t;
56 
57 /**
58  * @brief Rotary encoder interface
59  *
60  */
61 struct rotary_encoder_t {
62     /**
63      * @brief Filter out glitch from input signals
64      *
65      * @param encoder Rotary encoder handle
66      * @param max_glitch_us Maximum glitch duration, in us
67      * @return
68      *      - ESP_OK: Set glitch filter successfully
69      *      - ESP_FAIL: Set glitch filter failed because of other error
70      */
71     esp_err_t (*set_glitch_filter)(rotary_encoder_t *encoder, uint32_t max_glitch_us);
72 
73     /**
74      * @brief Start rotary encoder
75      *
76      * @param encoder Rotary encoder handle
77      * @return
78      *      - ESP_OK: Start rotary encoder successfully
79      *      - ESP_FAIL: Start rotary encoder failed because of other error
80      */
81     esp_err_t (*start)(rotary_encoder_t *encoder);
82 
83     /**
84      * @brief Stop rotary encoder
85      *
86      * @param encoder Rotary encoder handle
87      * @return
88      *      - ESP_OK: Stop rotary encoder successfully
89      *      - ESP_FAIL: Stop rotary encoder failed because of other error
90      */
91     esp_err_t (*stop)(rotary_encoder_t *encoder);
92 
93     /**
94      * @brief Recycle rotary encoder memory
95      *
96      * @param encoder Rotary encoder handle
97      * @return
98      *      - ESP_OK: Recycle rotary encoder memory successfully
99      *      - ESP_FAIL: rotary encoder memory failed because of other error
100      */
101     esp_err_t (*del)(rotary_encoder_t *encoder);
102 
103     /**
104      * @brief Get rotary encoder counter value
105      *
106      * @param encoder Rotary encoder handle
107      * @return Current counter value (the sign indicates the direction of rotation)
108      */
109     int (*get_counter_value)(rotary_encoder_t *encoder);
110 };
111 
112 /**
113  * @brief Create rotary encoder instance for EC11
114  *
115  * @param config Rotary encoder configuration
116  * @param ret_encoder Returned rotary encoder handle
117  * @return
118  *      - ESP_OK: Create rotary encoder instance successfully
119  *      - ESP_ERR_INVALID_ARG: Create rotary encoder instance failed because of some invalid argument
120  *      - ESP_ERR_NO_MEM: Create rotary encoder instance failed because there's no enough capable memory
121  *      - ESP_FAIL: Create rotary encoder instance failed because of other error
122  */
123 esp_err_t rotary_encoder_new_ec11(const rotary_encoder_config_t *config, rotary_encoder_t **ret_encoder);
124 
125 #ifdef __cplusplus
126 }
127 #endif
128