esp32-rotary-encoder
ESP32-compatible C library for Incremental Rotary Encoders.
 All Data Structures Files Functions Variables Enumerations Enumerator
rotary_encoder.h
Go to the documentation of this file.
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 
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 
58 typedef enum
59 {
61  ROTARY_ENCODER_DIRECTION_CLOCKWISE,
62  ROTARY_ENCODER_DIRECTION_COUNTER_CLOCKWISE,
64 
65 // Used internally
67 #define TABLE_COLS 4
68 typedef uint8_t table_row_t[TABLE_COLS];
70 
74 typedef struct
75 {
76  rotary_encoder_position_t position;
79 
84 typedef struct
85 {
86  gpio_num_t pin_a;
87  gpio_num_t pin_b;
88  QueueHandle_t queue;
89  const table_row_t * table;
90  uint8_t table_state;
93 
97 typedef struct
98 {
101 
111 esp_err_t rotary_encoder_init(rotary_encoder_info_t * info, gpio_num_t pin_a, gpio_num_t pin_b);
112 
119 esp_err_t rotary_encoder_enable_half_steps(rotary_encoder_info_t * info, bool enable);
120 
128 
136 
141 QueueHandle_t rotary_encoder_create_queue(void);
142 
150 esp_err_t rotary_encoder_set_queue(rotary_encoder_info_t * info, QueueHandle_t queue);
151 
159 
166 
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif // ROTARY_ENCODER_H
volatile rotary_encoder_state_t state
Device state.
Definition: rotary_encoder.h:91
esp_err_t rotary_encoder_enable_half_steps(rotary_encoder_info_t *info, bool enable)
Enable half-stepping mode. This generates twice as many counted steps per rotation.
Definition: rotary_encoder.c:245
uint8_t table_state
Internal state.
Definition: rotary_encoder.h:90
Struct represents the current state of the device in terms of incremental position and direction of l...
Definition: rotary_encoder.h:74
const table_row_t * table
Pointer to active state transition table.
Definition: rotary_encoder.h:89
esp_err_t rotary_encoder_init(rotary_encoder_info_t *info, gpio_num_t pin_a, gpio_num_t pin_b)
Initialise the rotary encoder device with the specified GPIO pins and full step increments. This function will set up the GPIOs as needed, Note: this function assumes that gpio_install_isr_service(0) has already been called.
Definition: rotary_encoder.c:210
rotary_encoder_direction_t
Enum representing the direction of rotation.
Definition: rotary_encoder.h:58
esp_err_t rotary_encoder_reset(rotary_encoder_info_t *info)
Reset the current position of the rotary encoder to zero.
Definition: rotary_encoder.c:331
rotary_encoder_direction_t direction
Direction of last movement. Set to NOT_SET on reset.
Definition: rotary_encoder.h:77
esp_err_t rotary_encoder_set_queue(rotary_encoder_info_t *info, QueueHandle_t queue)
Set the driver to use the specified queue as an event queue. It is recommended that a queue construct...
Definition: rotary_encoder.c:299
rotary_encoder_position_t position
Numerical position since reset. This value increments on clockwise rotation, and decrements on counte...
Definition: rotary_encoder.h:76
gpio_num_t pin_b
GPIO for Signal B from the rotary encoder device.
Definition: rotary_encoder.h:87
esp_err_t rotary_encoder_flip_direction(rotary_encoder_info_t *info)
Reverse (flip) the sense of the direction. Use this if clockwise/counterclockwise are not what you ex...
Definition: rotary_encoder.c:261
esp_err_t rotary_encoder_uninit(rotary_encoder_info_t *info)
Remove the interrupt handlers installed by rotary_encoder_init. Note: GPIOs will be left in the state...
Definition: rotary_encoder.c:278
Direction not yet known (stationary since reset)
Definition: rotary_encoder.h:60
rotary_encoder_state_t state
The device state corresponding to this event.
Definition: rotary_encoder.h:99
Struct carries all the information needed by this driver to manage the rotary encoder device...
Definition: rotary_encoder.h:84
QueueHandle_t rotary_encoder_create_queue(void)
Create a queue handle suitable for use as an event queue.
Definition: rotary_encoder.c:294
gpio_num_t pin_a
GPIO for Signal A from the rotary encoder device.
Definition: rotary_encoder.h:86
QueueHandle_t queue
Handle for event queue, created by rotary_encoder_create_queue.
Definition: rotary_encoder.h:88
esp_err_t rotary_encoder_get_state(const rotary_encoder_info_t *info, rotary_encoder_state_t *state)
Get the current position of the rotary encoder.
Definition: rotary_encoder.c:314
Struct represents a queued event, used to communicate current position to a waiting task...
Definition: rotary_encoder.h:97