This commit is contained in:
2025-12-24 19:54:21 +03:00
parent 5ef3913eb9
commit 4368ac9845
2 changed files with 24 additions and 3 deletions

View File

@@ -20,6 +20,7 @@
.queue_size = 1, \ .queue_size = 1, \
.a_gpio_number = GPIO_NUM_MAX, \ .a_gpio_number = GPIO_NUM_MAX, \
.b_gpio_number = GPIO_NUM_MAX, \ .b_gpio_number = GPIO_NUM_MAX, \
.s_gpio_number = GPIO_NUM_MAX, \
.encoder_min_value = -100, \ .encoder_min_value = -100, \
.encoder_max_value = 100, \ .encoder_max_value = 100, \
.encoder_step = 1, \ .encoder_step = 1, \
@@ -42,6 +43,7 @@ extern "C"
uint8_t queue_size; /*!< Queue size for task for the encoder processing. @note Minimum value is 1. */ uint8_t queue_size; /*!< Queue size for task for the encoder processing. @note Minimum value is 1. */
uint8_t a_gpio_number; /*!< Encoder A GPIO number. */ uint8_t a_gpio_number; /*!< Encoder A GPIO number. */
uint8_t b_gpio_number; /*!< Encoder B GPIO number. */ uint8_t b_gpio_number; /*!< Encoder B GPIO number. */
uint8_t s_gpio_number; /*!< Encoder button GPIO number. */
int32_t encoder_min_value; /*!< Encoder min value. @note Must be less than encoder_max_value. */ 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. */ int32_t encoder_max_value; /*!< Encoder max value. @note Must be greater than encoder_min_value. */
double encoder_step; /*!< Encoder step. @note Must be greater than 0. */ double encoder_step; /*!< Encoder step. @note Must be greater than 0. */
@@ -55,10 +57,12 @@ extern "C"
{ {
uint8_t a_gpio_number; /*!< Encoder A GPIO number. */ uint8_t a_gpio_number; /*!< Encoder A GPIO number. */
uint8_t b_gpio_number; /*!< Encoder B GPIO number. */ uint8_t b_gpio_number; /*!< Encoder B GPIO number. */
uint8_t s_gpio_number; /*!< Encoder button GPIO number. */
int32_t encoder_min_value; /*!< Encoder min value. */ int32_t encoder_min_value; /*!< Encoder min value. */
int32_t encoder_max_value; /*!< Encoder max value. */ int32_t encoder_max_value; /*!< Encoder max value. */
double encoder_step; /*!< Encoder step. */ double encoder_step; /*!< Encoder step. */
double encoder_position; /*!< Encoder position. */ double encoder_position; /*!< Encoder position. */
bool button_status; /*!< Encoder button status. */
uint8_t encoder_number; /*!< Encoder unique number. */ uint8_t encoder_number; /*!< Encoder unique number. */
uint8_t encoder_state; /*!< Encoder internal state. */ uint8_t encoder_state; /*!< Encoder internal state. */
bool is_initialized; /*!< Encoder initialization flag. */ bool is_initialized; /*!< Encoder initialization flag. */
@@ -85,6 +89,7 @@ extern "C"
{ {
uint8_t encoder_number; /*!< Encoder unique number. */ uint8_t encoder_number; /*!< Encoder unique number. */
double encoder_position; /*!< Encoder current position. */ double encoder_position; /*!< Encoder current position. */
bool button_status; /*!< Encoder button status. */
} zh_encoder_event_on_isr_t; } zh_encoder_event_on_isr_t;
/** /**

View File

@@ -164,11 +164,13 @@ static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *con
static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle)
{ {
ZH_ERROR_CHECK(config->a_gpio_number < GPIO_NUM_MAX && config->b_gpio_number < GPIO_NUM_MAX, ESP_ERR_INVALID_ARG, NULL, "Invalid GPIO number.") ZH_ERROR_CHECK(config->a_gpio_number < GPIO_NUM_MAX && config->b_gpio_number < GPIO_NUM_MAX && config->s_gpio_number <= GPIO_NUM_MAX, ESP_ERR_INVALID_ARG, NULL, "Invalid GPIO number.")
ZH_ERROR_CHECK(config->a_gpio_number != config->b_gpio_number, ESP_ERR_INVALID_ARG, NULL, "Both GPIO is same.") ZH_ERROR_CHECK(config->a_gpio_number != config->b_gpio_number, ESP_ERR_INVALID_ARG, NULL, "Encoder A and B GPIO is same.")
ZH_ERROR_CHECK(config->a_gpio_number != config->s_gpio_number && config->b_gpio_number != config->s_gpio_number, ESP_ERR_INVALID_ARG, NULL, "Encoder GPIO and button GPIO is same.")
uint64_t bit_mask = (config->s_gpio_number == GPIO_NUM_MAX) ? ((1ULL << config->a_gpio_number) | (1ULL << config->b_gpio_number)) : ((1ULL << config->a_gpio_number) | (1ULL << config->b_gpio_number) | (1ULL << config->s_gpio_number));
gpio_config_t pin_config = { gpio_config_t pin_config = {
.mode = GPIO_MODE_INPUT, .mode = GPIO_MODE_INPUT,
.pin_bit_mask = (1ULL << config->a_gpio_number) | (1ULL << config->b_gpio_number), .pin_bit_mask = bit_mask,
.pull_up_en = GPIO_PULLUP_ENABLE, .pull_up_en = GPIO_PULLUP_ENABLE,
.intr_type = GPIO_INTR_ANYEDGE}; .intr_type = GPIO_INTR_ANYEDGE};
esp_err_t err = gpio_config(&pin_config); esp_err_t err = gpio_config(&pin_config);
@@ -193,6 +195,20 @@ static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config, z
{ {
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_uninstall_isr_service(); gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Interrupt initialization failed."); ZH_ERROR_CHECK(err == ESP_OK, err, gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_uninstall_isr_service(); gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Interrupt initialization failed.");
} }
if (config->s_gpio_number != GPIO_NUM_MAX)
{
err = gpio_isr_handler_add(config->s_gpio_number, _zh_encoder_isr_handler, handle);
if (_is_prev_gpio_isr_handler == true)
{
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_isr_handler_remove((gpio_num_t)config->b_gpio_number);
gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Interrupt initialization failed.");
}
else
{
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_isr_handler_remove((gpio_num_t)config->b_gpio_number); gpio_uninstall_isr_service();
gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Interrupt initialization failed.");
}
}
return ESP_OK; return ESP_OK;
} }