refactor: refactored by pvs-studio

This commit is contained in:
2025-12-25 16:44:34 +03:00
parent be29ff31f7
commit 28d067eb98
3 changed files with 20 additions and 16 deletions

View File

@@ -4,6 +4,10 @@
1. [ESP32 ESP-IDF v5.5.1](https://docs.espressif.com/projects/esp-idf/en/v5.5.1/esp32/index.html) 1. [ESP32 ESP-IDF v5.5.1](https://docs.espressif.com/projects/esp-idf/en/v5.5.1/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.
## Features ## Features
1. Support some encoders on one device. 1. Support some encoders on one device.

View File

@@ -39,33 +39,33 @@ extern "C"
*/ */
typedef struct typedef struct
{ {
double encoder_step; /*!< Encoder step. @note Must be greater than 0. */
double encoder_min_value; /*!< Encoder min value. @note Must be less than encoder_max_value. */
double 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. */
uint16_t stack_size; /*!< Stack size for task for the encoder isr processing processing. @note The minimum size is configMINIMAL_STACK_SIZE. */
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. */
uint8_t b_gpio_number; /*!< Encoder B GPIO number. */ uint8_t b_gpio_number; /*!< Encoder B GPIO number. */
uint8_t s_gpio_number; /*!< Encoder button GPIO number. */ uint8_t s_gpio_number; /*!< Encoder button GPIO number. */
int32_t encoder_min_value; /*!< Encoder min value. @note Must be less than encoder_max_value. */
int32_t encoder_max_value; /*!< Encoder max value. @note Must be greater than encoder_min_value. */
double encoder_step; /*!< Encoder step. @note Must be greater than 0. */
uint8_t encoder_number; /*!< Unique encoder number. */ uint8_t encoder_number; /*!< Unique encoder number. */
uint16_t stack_size; /*!< Stack size for task for the encoder isr processing processing. @note The minimum size is configMINIMAL_STACK_SIZE. */
} zh_encoder_init_config_t; } zh_encoder_init_config_t;
/** /**
* @brief Encoder handle. * @brief Encoder handle.
*/ */
typedef struct typedef struct // -V802
{ {
double encoder_step; /*!< Encoder step. */
double encoder_position; /*!< Encoder position. */
double encoder_min_value; /*!< Encoder min value. */
double encoder_max_value; /*!< Encoder max value. */
uint8_t a_gpio_number; /*!< Encoder A GPIO number. */ uint8_t a_gpio_number; /*!< Encoder A GPIO number. */
uint8_t b_gpio_number; /*!< Encoder B GPIO number. */ uint8_t b_gpio_number; /*!< Encoder B GPIO number. */
uint8_t s_gpio_number; /*!< Encoder button GPIO number. */ uint8_t s_gpio_number; /*!< Encoder button GPIO number. */
int32_t encoder_min_value; /*!< Encoder min value. */
int32_t encoder_max_value; /*!< Encoder max value. */
double encoder_step; /*!< Encoder step. */
double encoder_position; /*!< Encoder position. */
bool button_status; /*!< Encoder button status. */
uint8_t encoder_number; /*!< Encoder unique number. */ uint8_t encoder_number; /*!< Encoder unique number. */
uint8_t encoder_state; /*!< Encoder internal state. */ uint8_t encoder_state; /*!< Encoder internal state. */
bool button_status; /*!< Encoder button status. */
bool is_initialized; /*!< Encoder initialization flag. */ bool is_initialized; /*!< Encoder initialization flag. */
} zh_encoder_handle_t; } zh_encoder_handle_t;
@@ -88,8 +88,8 @@ extern "C"
*/ */
typedef struct typedef struct
{ {
uint8_t encoder_number; /*!< Encoder unique number. */
double encoder_position; /*!< Encoder current position. */ double encoder_position; /*!< Encoder current position. */
uint8_t encoder_number; /*!< Encoder unique number. */
bool button_status; /*!< Encoder button status. */ bool button_status; /*!< Encoder button status. */
} zh_encoder_event_on_isr_t; } zh_encoder_event_on_isr_t;

View File

@@ -44,7 +44,7 @@ static void _zh_encoder_isr_processing_task(void *pvParameter);
ESP_EVENT_DEFINE_BASE(ZH_ENCODER); ESP_EVENT_DEFINE_BASE(ZH_ENCODER);
esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008
{ {
ZH_LOGI("Encoder initialization started."); ZH_LOGI("Encoder initialization started.");
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder initialization failed. Invalid argument."); ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder initialization failed. Invalid argument.");
@@ -186,7 +186,7 @@ static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *con
return ESP_OK; return ESP_OK;
} }
static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) 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->a_gpio_number < GPIO_NUM_MAX && config->b_gpio_number < GPIO_NUM_MAX && config->s_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 && config->s_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.")
@@ -207,9 +207,9 @@ static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config, z
_is_prev_gpio_isr_handler = true; _is_prev_gpio_isr_handler = true;
} }
} }
err = gpio_isr_handler_add(config->a_gpio_number, _zh_encoder_isr_handler, handle); err = gpio_isr_handler_add((gpio_num_t)config->a_gpio_number, _zh_encoder_isr_handler, handle);
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Interrupt initialization failed."); ZH_ERROR_CHECK(err == ESP_OK, err, gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Interrupt initialization failed.");
err = gpio_isr_handler_add(config->b_gpio_number, _zh_encoder_isr_handler, handle); err = gpio_isr_handler_add((gpio_num_t)config->b_gpio_number, _zh_encoder_isr_handler, handle);
if (_is_prev_gpio_isr_handler == true) if (_is_prev_gpio_isr_handler == true)
{ {
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Interrupt initialization failed."); ZH_ERROR_CHECK(err == ESP_OK, err, gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Interrupt initialization failed.");
@@ -218,7 +218,7 @@ static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config, z
{ {
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_uninstall_isr_service(); gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Interrupt initialization failed."); ZH_ERROR_CHECK(err == ESP_OK, err, gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_uninstall_isr_service(); gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Interrupt initialization failed.");
} }
err = gpio_isr_handler_add(config->s_gpio_number, _zh_encoder_isr_handler, handle); err = gpio_isr_handler_add((gpio_num_t)config->s_gpio_number, _zh_encoder_isr_handler, handle);
if (_is_prev_gpio_isr_handler == true) if (_is_prev_gpio_isr_handler == true)
{ {
ZH_ERROR_CHECK(err == ESP_OK, err, gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_isr_handler_remove((gpio_num_t)config->b_gpio_number); ZH_ERROR_CHECK(err == ESP_OK, err, gpio_isr_handler_remove((gpio_num_t)config->a_gpio_number); gpio_isr_handler_remove((gpio_num_t)config->b_gpio_number);