From aa0883dd8baf0aa5690a0a0c0be9eb95c1dd0822 Mon Sep 17 00:00:00 2001 From: Alexey Zholtikov Date: Sun, 1 Feb 2026 09:34:54 +0300 Subject: [PATCH] feat: initial --- README.md | 44 ++++++++++++++++++++++++++++++++++++----- include/zh_tachometer.h | 1 + version.txt | 1 + zh_tachometer.c | 10 ++++++---- 4 files changed, 47 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index ee36ec1..de10e2d 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,40 @@ -# esp_component_template +# ESP32 ESP-IDF component for tachometer (via rotary encoder) -esp_component_template +## Tested on +1. [ESP32 ESP-IDF v5.5.2](https://docs.espressif.com/projects/esp-idf/en/v5.5.2/esp32/index.html) + +## SAST Tools + +[PVS-Studio](https://pvs-studio.com/pvs-studio/?utm_source=website&utm_medium=github&utm_campaign=open_source) - static analyzer for C, C++, C#, and Java code. + +## Attention + +1. For correct operation, please enable the following settings in the menuconfig: + +```text +PCNT_CTRL_FUNC_IN_IRAM +PCNT_ISR_IRAM_SAF +``` + +## Using + +In an existing project, run the following command to install the components: + +```text +cd ../your_project/components +git clone http://git.zh.com.ru/esp_components/zh_tachometer +``` + +In the application, add the component: + +```c +#include "zh_tachometer.h" +``` + +## Examples + +```c #include "zh_tachometer.h" zh_tachometer_handle_t tachometer_handle = {0}; @@ -16,9 +49,10 @@ void app_main(void) zh_tachometer_init(&config, &tachometer_handle); for (;;) { - int16_t value = 0; + uint16_t value = 0; zh_tachometer_get(&tachometer_handle, &value); printf("Tachometer value is %d rpm.\n", value); - vTaskDelay(1000 / portTICK_PERIOD_MS); + vTaskDelay(100 / portTICK_PERIOD_MS); } -} \ No newline at end of file +} +``` diff --git a/include/zh_tachometer.h b/include/zh_tachometer.h index 855fd29..d68b0b2 100644 --- a/include/zh_tachometer.h +++ b/include/zh_tachometer.h @@ -47,6 +47,7 @@ extern "C" pcnt_channel_handle_t pcnt_channel_b_handle; /*!< Tachometer unique pcnt channel handle. */ esp_timer_handle_t esp_timer_handle; /*!< Tachometer unique timer handle. */ uint16_t value; /*!< Tachometer value. */ + uint16_t encoder_pulses; /*!< Number of pulses per one rotation. */ bool is_initialized; /*!< Tachometer initialization flag. */ } zh_tachometer_handle_t; diff --git a/version.txt b/version.txt index e69de29..afaf360 100644 --- a/version.txt +++ b/version.txt @@ -0,0 +1 @@ +1.0.0 \ No newline at end of file diff --git a/zh_tachometer.c b/zh_tachometer.c index aecd9d7..3dc3cf6 100644 --- a/zh_tachometer.c +++ b/zh_tachometer.c @@ -18,7 +18,7 @@ static esp_err_t _zh_tachometer_pcnt_init(const zh_tachometer_init_config_t *con static esp_err_t _zh_tachometer_timer_init(zh_tachometer_handle_t *handle); static void _zh_tachometer_timer_on_alarm_cb(void *arg); -esp_err_t zh_tachometer_init(const zh_tachometer_init_config_t *config, zh_tachometer_handle_t *handle) +esp_err_t zh_tachometer_init(const zh_tachometer_init_config_t *config, zh_tachometer_handle_t *handle) // -V2008 { ZH_LOGI("Tachometer initialization started."); ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Tachometer initialization failed. Invalid argument."); @@ -29,6 +29,7 @@ esp_err_t zh_tachometer_init(const zh_tachometer_init_config_t *config, zh_tacho ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Tachometer initialization failed. Timer initialization failed."); err = _zh_tachometer_pcnt_init(config, handle); ZH_ERROR_CHECK(err == ESP_OK, err, esp_timer_stop(handle->esp_timer_handle); esp_timer_delete(handle->esp_timer_handle), "Tachometer initialization failed. PCNT initialization failed."); + handle->encoder_pulses = config->encoder_pulses; handle->is_initialized = true; ZH_LOGI("Tachometer initialization completed successfully."); return ESP_OK; @@ -69,7 +70,7 @@ static esp_err_t _zh_tachometer_validate_config(const zh_tachometer_init_config_ return ESP_OK; } -static esp_err_t _zh_tachometer_pcnt_init(const zh_tachometer_init_config_t *config, zh_tachometer_handle_t *handle) +static esp_err_t _zh_tachometer_pcnt_init(const zh_tachometer_init_config_t *config, zh_tachometer_handle_t *handle) // -V2008 { 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.") @@ -141,7 +142,7 @@ static esp_err_t _zh_tachometer_timer_init(zh_tachometer_handle_t *handle) }; esp_err_t err = esp_timer_create(&timer_args, &handle->esp_timer_handle); ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Timer initialization failed."); - err = esp_timer_start_periodic(handle->esp_timer_handle, 10000); // 0.01 sec. + err = esp_timer_start_periodic(handle->esp_timer_handle, 10000); ZH_ERROR_CHECK(err == ESP_OK, err, esp_timer_delete(handle->esp_timer_handle), "Timer initialization failed."); return ESP_OK; } @@ -152,5 +153,6 @@ static void IRAM_ATTR _zh_tachometer_timer_on_alarm_cb(void *arg) int pcnt_count = 0; pcnt_unit_get_count(handle->pcnt_unit_handle, &pcnt_count); pcnt_unit_clear_count(handle->pcnt_unit_handle); - handle->value = (uint16_t)pcnt_count; + float value_temp = ((pcnt_count * 100.0) / handle->encoder_pulses) * 60; + handle->value = (value_temp < 0) ? -(uint16_t)value_temp : (uint16_t)value_temp; // -V2004 } \ No newline at end of file