6 Commits
v2.2.0 ... main

4 changed files with 41 additions and 29 deletions

View File

@@ -86,7 +86,7 @@ void zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t ev
{ {
case ZH_BUTTON_EVENT: case ZH_BUTTON_EVENT:
zh_encoder_button_event_on_isr_t *button_event = event_data; zh_encoder_button_event_on_isr_t *button_event = event_data;
printf("Encoder number %d button %s.\n", button_event->encoder_number, button_event->button_status == 1 ? "released" : "pressed"); printf("Encoder number %d button %s.\n", button_event->encoder_number, (button_event->button_status == 1) ? "released" : "pressed");
break; break;
case ZH_ENCODER_EVENT: case ZH_ENCODER_EVENT:
zh_encoder_event_on_isr_t *encoder_event = event_data; zh_encoder_event_on_isr_t *encoder_event = event_data;

View File

@@ -42,9 +42,9 @@ extern "C"
*/ */
typedef struct typedef struct
{ {
float encoder_step; /*!< Encoder step. @note Must be greater than 0. */ float encoder_step; /*!< Encoder step. @note Must be greater than 0. */
float encoder_min_value; /*!< Encoder min value. @note Must be less than encoder_max_value. */ float encoder_min_value; /*!< Encoder min value. @note Must be less than encoder_max_value. */
float encoder_max_value; /*!< Encoder max value. @note Must be greater than encoder_min_value. */ float encoder_max_value; /*!< Encoder max value. @note Must be greater than encoder_min_value. */
uint8_t task_priority; /*!< Task priority for the encoder isr processing. @note Minimum value is 1. */ uint8_t task_priority; /*!< Task priority for the encoder isr 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 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. */
@@ -59,21 +59,22 @@ extern "C"
/** /**
* @brief Encoder handle. * @brief Encoder handle.
*/ */
typedef struct // -V802 typedef struct
{ {
pcnt_unit_handle_t pcnt_unit_handle; /*!< Encoder unique pcnt unit handle. */ bool s_gpio_status; /*!< Encoder button status. */
pcnt_channel_handle_t pcnt_channel_a_handle; /*!< Encoder unique pcnt channel handle. */ bool is_initialized; /*!< Encoder initialization flag. */
pcnt_channel_handle_t pcnt_channel_b_handle; /*!< Encoder unique pcnt channel handle. */
float encoder_step; /*!< Encoder step. */
float encoder_position; /*!< Encoder position. */
float encoder_min_value; /*!< Encoder min value. */
float encoder_max_value; /*!< Encoder max value. */
uint8_t encoder_number; /*!< Encoder unique number. */ uint8_t encoder_number; /*!< Encoder unique number. */
uint8_t s_gpio_number; /*!< Encoder button GPIO number. */ uint8_t s_gpio_number; /*!< Encoder button GPIO number. */
uint16_t s_gpio_debounce_time; /*!< Encoder button debounce_time. */ uint16_t s_gpio_debounce_time; /*!< Encoder button debounce_time. */
uint64_t s_gpio_prev_time; /*!< Encoder button prev interrupt time. */ uint64_t s_gpio_prev_time; /*!< Encoder button prev interrupt time. */
bool s_gpio_status; /*!< Encoder button status. */ float encoder_step; /*!< Encoder step. */
bool is_initialized; /*!< Encoder initialization flag. */ float encoder_position; /*!< Encoder position. */
float encoder_min_value; /*!< Encoder min value. */
float encoder_max_value; /*!< Encoder max value. */
pcnt_unit_handle_t pcnt_unit_handle; /*!< Encoder unique pcnt unit handle. */
pcnt_channel_handle_t pcnt_channel_a_handle; /*!< Encoder unique pcnt channel handle. */
pcnt_channel_handle_t pcnt_channel_b_handle; /*!< Encoder unique pcnt channel handle. */
} zh_encoder_handle_t; } zh_encoder_handle_t;
/** /**
@@ -105,7 +106,7 @@ extern "C"
typedef struct typedef struct
{ {
float encoder_position; /*!< Encoder current position. */ float encoder_position; /*!< Encoder current position. */
uint8_t encoder_number; /*!< Encoder unique number. */ uint8_t encoder_number; /*!< Encoder unique number. */
} zh_encoder_event_on_isr_t; } zh_encoder_event_on_isr_t;
/** /**
@@ -138,6 +139,8 @@ extern "C"
/** /**
* @brief Deinitialize encoder. * @brief Deinitialize encoder.
* *
* @param[in, out] handle Pointer to unique encoder handle.
*
* @return ESP_OK if success or an error code otherwise. * @return ESP_OK if success or an error code otherwise.
*/ */
esp_err_t zh_encoder_deinit(zh_encoder_handle_t *handle); esp_err_t zh_encoder_deinit(zh_encoder_handle_t *handle);

View File

@@ -1 +1 @@
2.2.0 2.3.1

View File

@@ -39,6 +39,7 @@ esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_han
{ {
ZH_LOGI("Encoder initialization started."); ZH_LOGI("Encoder initialization started.");
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder initialization failed. Invalid argument."); ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder initialization failed. Invalid argument.");
ZH_ERROR_CHECK(handle->is_initialized == false, ESP_ERR_INVALID_STATE, NULL, "Encoder initialization failed. Encoder is already initialized.");
ZH_ERROR_CHECK(_encoder_counter < sizeof(_encoder_number_matrix), ESP_ERR_INVALID_ARG, NULL, "Encoder initialization failed. Maximum quantity reached."); ZH_ERROR_CHECK(_encoder_counter < sizeof(_encoder_number_matrix), ESP_ERR_INVALID_ARG, NULL, "Encoder initialization failed. Maximum quantity reached.");
esp_err_t err = _zh_encoder_validate_config(config, handle); esp_err_t err = _zh_encoder_validate_config(config, handle);
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. Initial configuration check failed."); ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. Initial configuration check failed.");
@@ -53,6 +54,10 @@ esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_han
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_unit_stop(handle->pcnt_unit_handle); pcnt_unit_disable(handle->pcnt_unit_handle); pcnt_del_channel(handle->pcnt_channel_a_handle); ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_unit_stop(handle->pcnt_unit_handle); pcnt_unit_disable(handle->pcnt_unit_handle); pcnt_del_channel(handle->pcnt_channel_a_handle);
pcnt_del_channel(handle->pcnt_channel_b_handle); pcnt_del_unit(handle->pcnt_unit_handle); vQueueDelete(_queue_handle); _queue_handle = NULL; pcnt_del_channel(handle->pcnt_channel_b_handle); pcnt_del_unit(handle->pcnt_unit_handle); vQueueDelete(_queue_handle); _queue_handle = NULL;
vTaskDelete(zh_encoder); zh_encoder = NULL, "Encoder initialization failed. GPIO initialization failed."); vTaskDelete(zh_encoder); zh_encoder = NULL, "Encoder initialization failed. GPIO initialization failed.");
if (_stats.min_stack_size == 0)
{
_stats.min_stack_size = config->stack_size;
}
handle->is_initialized = true; handle->is_initialized = true;
++_encoder_counter; ++_encoder_counter;
for (uint8_t i = 0; i < sizeof(_encoder_number_matrix); ++i) for (uint8_t i = 0; i < sizeof(_encoder_number_matrix); ++i)
@@ -171,7 +176,6 @@ void zh_encoder_reset_stats(void)
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008 static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008
{ {
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid argument.");
ZH_ERROR_CHECK(config->task_priority >= 1 && config->stack_size >= configMINIMAL_STACK_SIZE, ESP_ERR_INVALID_ARG, NULL, "Invalid task settings."); ZH_ERROR_CHECK(config->task_priority >= 1 && config->stack_size >= configMINIMAL_STACK_SIZE, ESP_ERR_INVALID_ARG, NULL, "Invalid task settings.");
ZH_ERROR_CHECK(config->queue_size >= 1, ESP_ERR_INVALID_ARG, NULL, "Invalid queue size."); ZH_ERROR_CHECK(config->queue_size >= 1, ESP_ERR_INVALID_ARG, NULL, "Invalid queue size.");
ZH_ERROR_CHECK(config->encoder_max_value > config->encoder_min_value, ESP_ERR_INVALID_ARG, NULL, "Invalid encoder min/max value."); ZH_ERROR_CHECK(config->encoder_max_value > config->encoder_min_value, ESP_ERR_INVALID_ARG, NULL, "Invalid encoder min/max value.");
@@ -191,7 +195,6 @@ static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *con
static esp_err_t _zh_encoder_pcnt_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008 static esp_err_t _zh_encoder_pcnt_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008
{ {
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid argument.");
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, ESP_ERR_INVALID_ARG, NULL, "Invalid GPIO number.")
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->b_gpio_number, ESP_ERR_INVALID_ARG, NULL, "Encoder A and B GPIO is same.")
pcnt_unit_config_t pcnt_unit_config = { pcnt_unit_config_t pcnt_unit_config = {
@@ -260,7 +263,6 @@ static esp_err_t _zh_encoder_pcnt_init(const zh_encoder_init_config_t *config, z
static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008 static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008
{ {
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid argument.");
ZH_ERROR_CHECK(config->s_gpio_number <= GPIO_NUM_MAX, ESP_ERR_INVALID_ARG, NULL, "Invalid GPIO number.") ZH_ERROR_CHECK(config->s_gpio_number <= GPIO_NUM_MAX, ESP_ERR_INVALID_ARG, NULL, "Invalid GPIO number.")
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.") 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.")
if (config->s_gpio_number != GPIO_NUM_MAX) if (config->s_gpio_number != GPIO_NUM_MAX)
@@ -322,10 +324,11 @@ static bool IRAM_ATTR _zh_encoder_isr_handler(pcnt_unit_handle_t unit, const pcn
static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter) static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
{ {
zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)pvParameter; zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)pvParameter;
int encoder_data = {0}; int pcnt_count = {0};
while (xQueueReceive(_queue_handle, &encoder_data, portMAX_DELAY) == pdTRUE) bool is_value_changed = false;
while (xQueueReceive(_queue_handle, &pcnt_count, portMAX_DELAY) == pdTRUE)
{ {
switch (encoder_data) switch (pcnt_count)
{ {
case ZH_ENCODER_DIRECTION_CW: case ZH_ENCODER_DIRECTION_CW:
if (encoder_handle->encoder_position < encoder_handle->encoder_max_value) if (encoder_handle->encoder_position < encoder_handle->encoder_max_value)
@@ -335,6 +338,7 @@ static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
{ {
encoder_handle->encoder_position = encoder_handle->encoder_max_value; encoder_handle->encoder_position = encoder_handle->encoder_max_value;
} }
is_value_changed = true;
} }
break; break;
case ZH_ENCODER_DIRECTION_CCW: case ZH_ENCODER_DIRECTION_CCW:
@@ -345,19 +349,24 @@ static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
{ {
encoder_handle->encoder_position = encoder_handle->encoder_min_value; encoder_handle->encoder_position = encoder_handle->encoder_min_value;
} }
is_value_changed = true;
} }
break; break;
default: default:
break; break;
} }
zh_encoder_event_on_isr_t encoder_data = {0}; if (is_value_changed == true)
encoder_data.encoder_number = encoder_handle->encoder_number;
encoder_data.encoder_position = encoder_handle->encoder_position;
esp_err_t err = esp_event_post(ZH_ENCODER, ZH_ENCODER_EVENT, &encoder_data, sizeof(zh_encoder_event_on_isr_t), 1000 / portTICK_PERIOD_MS);
if (err != ESP_OK)
{ {
++_stats.event_post_error; is_value_changed = false;
ZH_LOGE("Encoder isr processing failed. Failed to post interrupt event.", err); zh_encoder_event_on_isr_t encoder_data = {0};
encoder_data.encoder_number = encoder_handle->encoder_number;
encoder_data.encoder_position = encoder_handle->encoder_position;
esp_err_t err = esp_event_post(ZH_ENCODER, ZH_ENCODER_EVENT, &encoder_data, sizeof(zh_encoder_event_on_isr_t), 1000 / portTICK_PERIOD_MS);
if (err != ESP_OK)
{
++_stats.event_post_error;
ZH_LOGE("Encoder isr processing failed. Failed to post interrupt event.", err);
}
} }
_stats.min_stack_size = (uint32_t)uxTaskGetStackHighWaterMark(NULL); _stats.min_stack_size = (uint32_t)uxTaskGetStackHighWaterMark(NULL);
} }