From 2060a56b557c4fdf02c412632e1e04473edf59a4 Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Wed, 24 Dec 2025 12:30:14 +0300 Subject: [PATCH] feat: added statistics --- README.md | 8 ++++++++ include/zh_encoder.h | 22 ++++++++++++++++++++++ version.txt | 2 +- zh_encoder.c | 27 +++++++++++++++++++++++++-- 4 files changed, 56 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 8547a54..92abfe9 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,14 @@ void app_main(void) printf("Encoder position %0.2f.\n", position); // For ESP8266 first disable "Component config -> Newlib -> Enable ‘nano’ formatting options for printf/scanf family" via menuconfig. // zh_encoder_set(&encoder_handle, 5); // Just for example. // zh_encoder_reset(&encoder_handle); // Just for example. + for (;;) + { + const zh_encoder_stats_t *stats = zh_encoder_get_stats(); + printf("Number of event post error: %ld.\n", stats->event_post_error); + printf("Number of queue overflow error: %ld.\n", stats->queue_overflow_error); + printf("Minimum free stack size: %ld.\n", stats->min_stack_size); + vTaskDelay(10000 / portTICK_PERIOD_MS); + } } void zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data) diff --git a/include/zh_encoder.h b/include/zh_encoder.h index a1289bd..e65b596 100644 --- a/include/zh_encoder.h +++ b/include/zh_encoder.h @@ -64,6 +64,16 @@ extern "C" bool is_initialized; /*!< Encoder initialization flag. */ } zh_encoder_handle_t; + /** + * @brief Structure for error statistics storage. + */ + typedef struct + { + uint32_t event_post_error; /*!< Number of event post error. */ + uint32_t queue_overflow_error; /*!< Number of queue overflow error. */ + uint32_t min_stack_size; /*!< Minimum free stack size. */ + } zh_encoder_stats_t; + ESP_EVENT_DECLARE_BASE(ZH_ENCODER); /** @@ -124,6 +134,18 @@ extern "C" */ esp_err_t zh_encoder_reset(zh_encoder_handle_t *handle); + /** + * @brief Get error statistics. + * + * @return Pointer to the statistics structure. + */ + const zh_encoder_stats_t *zh_encoder_get_stats(void); + + /** + * @brief Reset error statistics. + */ + void zh_encoder_reset_stats(void); + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/version.txt b/version.txt index afaf360..1cc5f65 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.0.0 \ No newline at end of file +1.1.0 \ No newline at end of file diff --git a/zh_encoder.c b/zh_encoder.c index f0b1346..e20d4d8 100644 --- a/zh_encoder.c +++ b/zh_encoder.c @@ -29,6 +29,7 @@ TaskHandle_t zh_encoder = NULL; static QueueHandle_t _queue_handle = NULL; static bool _is_initialized = false; static bool _is_prev_gpio_isr_handler = false; +static zh_encoder_stats_t _stats = {0}; static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config); static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle); @@ -109,6 +110,20 @@ esp_err_t zh_encoder_reset(zh_encoder_handle_t *handle) return ESP_OK; } +const zh_encoder_stats_t *zh_encoder_get_stats(void) +{ + return &_stats; +} + +void zh_encoder_reset_stats(void) +{ + ZH_LOGI("Error statistic reset started."); + _stats.event_post_error = 0; + _stats.queue_overflow_error = 0; + _stats.min_stack_size = 0; + ZH_LOGI("Error statistic reset successfully."); +} + static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config) { ZH_ERROR_CHECK(config != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid configuration."); @@ -188,7 +203,10 @@ static void IRAM_ATTR _zh_encoder_isr_handler(void *arg) { encoder_handle->encoder_position = encoder_handle->encoder_max_value; } - xQueueSendFromISR(_queue_handle, encoder_handle, &xHigherPriorityTaskWoken); + if (xQueueSendFromISR(_queue_handle, encoder_handle, &xHigherPriorityTaskWoken) != pdTRUE) + { + ++_stats.queue_overflow_error; + } } break; case ZH_ENCODER_DIRECTION_CCW: @@ -199,7 +217,10 @@ static void IRAM_ATTR _zh_encoder_isr_handler(void *arg) { encoder_handle->encoder_position = encoder_handle->encoder_min_value; } - xQueueSendFromISR(_queue_handle, encoder_handle, &xHigherPriorityTaskWoken); + if (xQueueSendFromISR(_queue_handle, encoder_handle, &xHigherPriorityTaskWoken) != pdTRUE) + { + ++_stats.queue_overflow_error; + } } break; default: @@ -222,8 +243,10 @@ static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter) esp_err_t err = esp_event_post(ZH_ENCODER, 0, &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); } vTaskDelete(NULL); } \ No newline at end of file