#pragma once #include "esp_log.h" #include "driver/gpio.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_event.h" #define ZH_ENCODER_INIT_CONFIG_DEFAULT() \ { \ .task_priority = 10, \ .stack_size = 2048, \ .queue_size = 10, \ .a_gpio_number = 0, \ .b_gpio_number = 0, \ .encoder_min_value = -100, \ .encoder_max_value = 100, \ .encoder_step = 1, \ .encoder_number = 0} #ifdef __cplusplus extern "C" { #endif typedef struct // Structure for initial initialization of encoder. { uint8_t task_priority; // Task priority for the encoder isr processing. @note It is not recommended to set a value less than 10. uint16_t stack_size; // Stack size for task for the encoder isr processing processing. @note The minimum size is 2048 bytes. uint8_t queue_size; // Queue size for task for the encoder processing. @note It is not recommended to set a value less than 10. uint8_t a_gpio_number; // Encoder A GPIO number. uint8_t b_gpio_number; // Encoder B GPIO number. int32_t encoder_min_value; // Encoder min value. @note Must be less than encoder_max_value. int32_t encoder_max_value; // Encoder max value. @note Must be greater than encoder_min_value. float encoder_step; // Encoder step. @note Must be greater than 0. uint8_t encoder_number; // Unique encoder number. } zh_encoder_init_config_t; typedef struct // Encoder handle. { uint8_t a_gpio_number; // Encoder A GPIO number. uint8_t b_gpio_number; // Encoder B GPIO number. int32_t encoder_min_value; // Encoder min value. @note Must be less than encoder_max_value. int32_t encoder_max_value; // Encoder max value. @note Must be greater than encoder_min_value. float encoder_step; // Encoder step. @note Must be greater than 0. float encoder_position; // Encoder position. uint8_t encoder_number; // Encoder unique number. bool is_initialized; // Encoder initialization flag. } zh_encoder_handle_t; ESP_EVENT_DECLARE_BASE(ZH_ENCODER); typedef struct // Structure for sending data to the event handler when cause an interrupt. @note Should be used with ZH_ENCODER event base. { uint8_t encoder_number; // Encoder unique number. float encoder_position; // Encoder current position. } zh_encoder_event_on_isr_t; /** * @brief Initialize encoder. * * @note The encoder will be set to the position (encoder_min_value + encoder_max_value)/2. * * @param[in] config Pointer to encoder initialized configuration structure. Can point to a temporary variable. * @param[out] handle Pointer to unique encoder handle. * * @note Before initialize the expander recommend initialize zh_encoder_init_config_t structure with default values. * * @code zh_encoder_init_config_t config = ZH_ENCODER_INIT_CONFIG_DEFAULT() @endcode * * @return ESP_OK if success or an error code otherwise. */ esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle); /** * @brief Set encoder position. * * @param[in, out] handle Pointer to unique encoder handle. * @param[in] position Encoder position (must be between encoder_min_value and encoder_max_value). * * @return ESP_OK if success or an error code otherwise. */ esp_err_t zh_encoder_set(zh_encoder_handle_t *handle, float position); /** * @brief Reset encoder position. * * @note The encoder will be set to the position (encoder_min_value + encoder_max_value)/2. * * @param[in, out] handle Pointer to unique encoder handle. * * @return ESP_OK if success or an error code otherwise. */ esp_err_t zh_encoder_reset(zh_encoder_handle_t *handle); #ifdef __cplusplus } #endif // #ifndef ROTARY_ENCODER_H // #define ROTARY_ENCODER_H // #include // #include // #include "freertos/FreeRTOS.h" // #include "freertos/queue.h" // #include "esp_err.h" // #include "driver/gpio.h" // #ifdef __cplusplus // extern "C" { // #endif // typedef int32_t rotary_encoder_position_t; // /** // * @brief Enum representing the direction of rotation. // */ // typedef enum // { // ROTARY_ENCODER_DIRECTION_NOT_SET = 0, ///< Direction not yet known (stationary since reset) // ROTARY_ENCODER_DIRECTION_CLOCKWISE, // ROTARY_ENCODER_DIRECTION_COUNTER_CLOCKWISE, // } rotary_encoder_direction_t; // // Used internally // ///@cond INTERNAL // #define TABLE_COLS 4 // typedef uint8_t table_row_t[TABLE_COLS]; // ///@endcond // /** // * @brief Struct represents the current state of the device in terms of incremental position and direction of last movement // */ // typedef struct // { // 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. // rotary_encoder_direction_t direction; ///< Direction of last movement. Set to NOT_SET on reset. // } rotary_encoder_state_t; // /** // * @brief Struct carries all the information needed by this driver to manage the rotary encoder device. // * The fields of this structure should not be accessed directly. // */ // typedef struct // { // gpio_num_t pin_a; ///< GPIO for Signal A from the rotary encoder device // gpio_num_t pin_b; ///< GPIO for Signal B from the rotary encoder device // QueueHandle_t queue; ///< Handle for event queue, created by ::rotary_encoder_create_queue // const table_row_t * table; ///< Pointer to active state transition table // uint8_t table_state; ///< Internal state // volatile rotary_encoder_state_t state; ///< Device state // } rotary_encoder_info_t; // /** // * @brief Struct represents a queued event, used to communicate current position to a waiting task // */ // typedef struct // { // rotary_encoder_state_t state; ///< The device state corresponding to this event // } rotary_encoder_event_t; // /** // * @brief 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. // * @param[in, out] info Pointer to allocated rotary encoder info structure. // * @param[in] pin_a GPIO number for rotary encoder output A. // * @param[in] pin_b GPIO number for rotary encoder output B. // * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred. // */ // esp_err_t rotary_encoder_init(rotary_encoder_info_t * info, gpio_num_t pin_a, gpio_num_t pin_b); // /** // * @brief Enable half-stepping mode. This generates twice as many counted steps per rotation. // * @param[in] info Pointer to initialised rotary encoder info structure. // * @param[in] enable If true, count half steps. If false, only count full steps. // * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred. // */ // esp_err_t rotary_encoder_enable_half_steps(rotary_encoder_info_t * info, bool enable); // /** // * @brief Reverse (flip) the sense of the direction. // * Use this if clockwise/counterclockwise are not what you expect. // * @param[in] info Pointer to initialised rotary encoder info structure. // * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred. // */ // esp_err_t rotary_encoder_flip_direction(rotary_encoder_info_t * info); // /** // * @brief Remove the interrupt handlers installed by ::rotary_encoder_init. // * Note: GPIOs will be left in the state they were configured by ::rotary_encoder_init. // * @param[in] info Pointer to initialised rotary encoder info structure. // * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred. // */ // esp_err_t rotary_encoder_uninit(rotary_encoder_info_t * info); // /** // * @brief Create a queue handle suitable for use as an event queue. // * @return A handle to a new queue suitable for use as an event queue. // */ // QueueHandle_t rotary_encoder_create_queue(void); // /** // * @brief Set the driver to use the specified queue as an event queue. // * It is recommended that a queue constructed by ::rotary_encoder_create_queue is used. // * @param[in] info Pointer to initialised rotary encoder info structure. // * @param[in] queue Handle to queue suitable for use as an event queue. See ::rotary_encoder_create_queue. // * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred. // */ // esp_err_t rotary_encoder_set_queue(rotary_encoder_info_t * info, QueueHandle_t queue); // /** // * @brief Get the current position of the rotary encoder. // * @param[in] info Pointer to initialised rotary encoder info structure. // * @param[in, out] state Pointer to an allocated rotary_encoder_state_t struct that will // * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred. // */ // esp_err_t rotary_encoder_get_state(const rotary_encoder_info_t * info, rotary_encoder_state_t * state); // /** // * @brief Reset the current position of the rotary encoder to zero. // * @param[in] info Pointer to initialised rotary encoder info structure. // * @return ESP_OK if successful, ESP_FAIL or ESP_ERR_* if an error occurred. // */ // esp_err_t rotary_encoder_reset(rotary_encoder_info_t * info); // #ifdef __cplusplus // } // #endif // #endif // ROTARY_ENCODER_H