feat: initial
This commit is contained in:
42
README.md
42
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"
|
#include "zh_tachometer.h"
|
||||||
|
|
||||||
zh_tachometer_handle_t tachometer_handle = {0};
|
zh_tachometer_handle_t tachometer_handle = {0};
|
||||||
@@ -16,9 +49,10 @@ void app_main(void)
|
|||||||
zh_tachometer_init(&config, &tachometer_handle);
|
zh_tachometer_init(&config, &tachometer_handle);
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
int16_t value = 0;
|
uint16_t value = 0;
|
||||||
zh_tachometer_get(&tachometer_handle, &value);
|
zh_tachometer_get(&tachometer_handle, &value);
|
||||||
printf("Tachometer value is %d rpm.\n", value);
|
printf("Tachometer value is %d rpm.\n", value);
|
||||||
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
vTaskDelay(100 / portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
```
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ extern "C"
|
|||||||
pcnt_channel_handle_t pcnt_channel_b_handle; /*!< Tachometer unique pcnt channel handle. */
|
pcnt_channel_handle_t pcnt_channel_b_handle; /*!< Tachometer unique pcnt channel handle. */
|
||||||
esp_timer_handle_t esp_timer_handle; /*!< Tachometer unique timer handle. */
|
esp_timer_handle_t esp_timer_handle; /*!< Tachometer unique timer handle. */
|
||||||
uint16_t value; /*!< Tachometer value. */
|
uint16_t value; /*!< Tachometer value. */
|
||||||
|
uint16_t encoder_pulses; /*!< Number of pulses per one rotation. */
|
||||||
bool is_initialized; /*!< Tachometer initialization flag. */
|
bool is_initialized; /*!< Tachometer initialization flag. */
|
||||||
} zh_tachometer_handle_t;
|
} zh_tachometer_handle_t;
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1 @@
|
|||||||
|
1.0.0
|
||||||
@@ -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 esp_err_t _zh_tachometer_timer_init(zh_tachometer_handle_t *handle);
|
||||||
static void _zh_tachometer_timer_on_alarm_cb(void *arg);
|
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_LOGI("Tachometer initialization started.");
|
||||||
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Tachometer initialization failed. Invalid argument.");
|
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.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Tachometer initialization failed. Timer initialization failed.");
|
||||||
err = _zh_tachometer_pcnt_init(config, handle);
|
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.");
|
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;
|
handle->is_initialized = true;
|
||||||
ZH_LOGI("Tachometer initialization completed successfully.");
|
ZH_LOGI("Tachometer initialization completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -69,7 +70,7 @@ static esp_err_t _zh_tachometer_validate_config(const zh_tachometer_init_config_
|
|||||||
return ESP_OK;
|
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 < 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.")
|
||||||
@@ -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);
|
esp_err_t err = esp_timer_create(&timer_args, &handle->esp_timer_handle);
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Timer initialization failed.");
|
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.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, esp_timer_delete(handle->esp_timer_handle), "Timer initialization failed.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@@ -152,5 +153,6 @@ static void IRAM_ATTR _zh_tachometer_timer_on_alarm_cb(void *arg)
|
|||||||
int pcnt_count = 0;
|
int pcnt_count = 0;
|
||||||
pcnt_unit_get_count(handle->pcnt_unit_handle, &pcnt_count);
|
pcnt_unit_get_count(handle->pcnt_unit_handle, &pcnt_count);
|
||||||
pcnt_unit_clear_count(handle->pcnt_unit_handle);
|
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
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user