From 2afcddfa0e51add0c9f927172c9b301c39f20e32 Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Sun, 5 Apr 2026 10:53:04 +0300 Subject: [PATCH] feat: added statistics --- README.md | 2 ++ include/zh_aht.h | 20 ++++++++++++++++++++ version.txt | 2 +- zh_aht.c | 22 +++++++++++++++++++--- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 61ac7a0..7f451c2 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,8 @@ void app_main(void) zh_aht_read(&aht_handle, &humidity, &temperature); printf("Humidity %0.2f\n", humidity); printf("Temperature %0.2f\n", temperature); + const zh_aht_stats_t *stats = zh_aht_get_stats(); + printf("Number of i2c driver error: %ld.\n", stats->i2c_driver_error); vTaskDelay(5000 / portTICK_PERIOD_MS); } } diff --git a/include/zh_aht.h b/include/zh_aht.h index 3c3872b..651f257 100644 --- a/include/zh_aht.h +++ b/include/zh_aht.h @@ -41,6 +41,14 @@ extern "C" i2c_master_dev_handle_t dev_handle; /*!< Unique I2C device handle. */ } zh_aht_handle_t; + /** + * @brief Structure for error statistics storage. + */ + typedef struct + { + uint32_t i2c_driver_error; /*!< Number of i2c driver error. */ + } zh_aht_stats_t; + /** * @brief Initialize AHT sensor. * @@ -77,6 +85,18 @@ extern "C" */ esp_err_t zh_aht_reset(zh_aht_handle_t *handle); + /** + * @brief Get error statistics. + * + * @return Pointer to the statistics structure. + */ + const zh_aht_stats_t *zh_aht_get_stats(void); + + /** + * @brief Reset error statistics. + */ + void zh_aht_reset_stats(void); + #ifdef __cplusplus } #endif \ No newline at end of file diff --git a/version.txt b/version.txt index a0cd9f0..a4f52a5 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -3.1.0 \ No newline at end of file +3.2.0 \ No newline at end of file diff --git a/zh_aht.c b/zh_aht.c index 2b4fa8c..69ccd39 100644 --- a/zh_aht.c +++ b/zh_aht.c @@ -16,6 +16,7 @@ static const char *TAG = "zh_aht"; static const uint8_t _aht_reset_command = 0xBA; static const uint8_t _aht_read_command[] = {0xAC, 0x33, 0x00}; static const uint8_t _aht_init_command[] = {0xBE, 0x08, 0x00}; +static zh_aht_stats_t _stats = {0}; static esp_err_t _zh_aht_validate_config(const zh_aht_init_config_t *config); static esp_err_t _zh_aht_i2c_init(const zh_aht_init_config_t *config, zh_aht_handle_t *handle); @@ -38,9 +39,11 @@ esp_err_t zh_aht_read(zh_aht_handle_t *handle, float *humidity, float *temperatu ZH_ERROR_CHECK(humidity != NULL && temperature != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "AHT read fail. Invalid argument."); ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "AHT read fail. AHT not initialized."); uint8_t sensor_data[7] = {0}; - ZH_ERROR_CHECK(i2c_master_transmit(handle->dev_handle, _aht_read_command, sizeof(_aht_read_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, NULL, "AHT read fail. I2C driver error."); + ZH_ERROR_CHECK(i2c_master_transmit(handle->dev_handle, _aht_read_command, sizeof(_aht_read_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, + ++_stats.i2c_driver_error, "AHT read fail. I2C driver error."); vTaskDelay(80 / portTICK_PERIOD_MS); - ZH_ERROR_CHECK(i2c_master_receive(handle->dev_handle, sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, NULL, "AHT read fail. I2C driver error."); + ZH_ERROR_CHECK(i2c_master_receive(handle->dev_handle, sensor_data, sizeof(sensor_data), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, + ++_stats.i2c_driver_error, "AHT read fail. I2C driver error."); ZH_ERROR_CHECK((sensor_data[0] & 0x80) == 0, ESP_ERR_TIMEOUT, NULL, "AHT read fail. Timeout exceeded."); ZH_ERROR_CHECK(_zh_aht_calc_crc(sensor_data, 6) == sensor_data[6], ESP_ERR_INVALID_CRC, NULL, "AHT read fail. Invalid CRC."); *humidity = (((((uint32_t)sensor_data[1]) << 16) | (((uint32_t)sensor_data[2]) << 8) | (((uint32_t)sensor_data[3]) << 0)) >> 4) / 1048576.0 * 100.0; @@ -54,12 +57,25 @@ esp_err_t zh_aht_reset(zh_aht_handle_t *handle) ZH_LOGI("AHT reset begin."); ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "AHT reset fail. Invalid argument."); ZH_ERROR_CHECK(handle->is_initialized == true, ESP_ERR_NOT_FOUND, NULL, "AHT reset fail. AHT not initialized."); - ZH_ERROR_CHECK(i2c_master_transmit(handle->dev_handle, &_aht_reset_command, sizeof(_aht_reset_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, NULL, "AHT reset fail. I2C driver error."); + ZH_ERROR_CHECK(i2c_master_transmit(handle->dev_handle, &_aht_reset_command, sizeof(_aht_reset_command), 1000 / portTICK_PERIOD_MS) == ESP_OK, ESP_FAIL, + ++_stats.i2c_driver_error, "AHT reset fail. I2C driver error."); vTaskDelay(20 / portTICK_PERIOD_MS); ZH_LOGI("AHT reset completed successfully."); return ESP_OK; } +const zh_aht_stats_t *zh_aht_get_stats(void) +{ + return &_stats; +} + +void zh_aht_reset_stats(void) +{ + ZH_LOGI("Error statistic reset started."); + _stats.i2c_driver_error = 0; + ZH_LOGI("Error statistic reset successfully."); +} + static esp_err_t _zh_aht_validate_config(const zh_aht_init_config_t *config) { ZH_ERROR_CHECK(config->i2c_address == 0x38 || config->i2c_address == 0x39, ESP_ERR_INVALID_ARG, NULL, "Invalid I2C address.");