diff --git a/include/zh_encoder.h b/include/zh_encoder.h index f36e912..618f03f 100644 --- a/include/zh_encoder.h +++ b/include/zh_encoder.h @@ -39,16 +39,16 @@ extern "C" */ typedef struct { - double encoder_step; /*!< Encoder step. @note Must be greater than 0. */ + 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 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 b_gpio_number; /*!< Encoder B GPIO number. */ - uint8_t s_gpio_number; /*!< Encoder button GPIO 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. */ + uint8_t task_priority; /*!< Task priority for the encoder isr 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 b_gpio_number; /*!< Encoder B GPIO number. */ + uint8_t s_gpio_number; /*!< Encoder button GPIO 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; /** @@ -56,17 +56,17 @@ extern "C" */ typedef struct // -V802 { - double encoder_step; /*!< Encoder step. */ - double encoder_position; /*!< Encoder position. */ + 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 b_gpio_number; /*!< Encoder B GPIO number. */ - uint8_t s_gpio_number; /*!< Encoder button GPIO number. */ - uint8_t encoder_number; /*!< Encoder unique number. */ - uint8_t encoder_state; /*!< Encoder internal state. */ - bool button_status; /*!< Encoder button status. */ - bool is_initialized; /*!< Encoder initialization flag. */ + uint8_t a_gpio_number; /*!< Encoder A GPIO number. */ + uint8_t b_gpio_number; /*!< Encoder B GPIO number. */ + uint8_t s_gpio_number; /*!< Encoder button GPIO number. */ + uint8_t encoder_number; /*!< Encoder unique number. */ + uint8_t encoder_state; /*!< Encoder internal state. */ + bool button_status; /*!< Encoder button status. */ + bool is_initialized; /*!< Encoder initialization flag. */ } zh_encoder_handle_t; /** @@ -116,6 +116,20 @@ extern "C" */ esp_err_t zh_encoder_deinit(zh_encoder_handle_t *handle); + /** + * @brief Reinitialize encoder (change min, max and step values). + * + * @note The encoder will be set to the position (encoder_min_value + encoder_max_value)/2. + * + * @param[in, out] handle Pointer to unique encoder handle. + * @param[in] min Encoder min value. @note Must be less than encoder_max_value. + * @param[in] max Encoder max value. @note Must be greater than encoder_min_value. + * @param[in] step Encoder step. @note Must be greater than 0. + * + * @return ESP_OK if success or an error code otherwise. + */ + esp_err_t zh_encoder_reinit(zh_encoder_handle_t *handle, double min, double max, double step); + /** * @brief Set encoder position. * diff --git a/version.txt b/version.txt index 589268e..e21e727 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.3.0 \ No newline at end of file +1.4.0 \ No newline at end of file diff --git a/zh_encoder.c b/zh_encoder.c index 09d4fbe..d06a685 100644 --- a/zh_encoder.c +++ b/zh_encoder.c @@ -29,6 +29,7 @@ static const uint8_t _encoder_matrix[7][4] = { TaskHandle_t zh_encoder = NULL; static QueueHandle_t _queue_handle = NULL; +static portMUX_TYPE _spinlock = portMUX_INITIALIZER_UNLOCKED; static volatile uint64_t _prev_us = 0; static uint8_t _encoder_counter = 0; @@ -131,6 +132,23 @@ esp_err_t zh_encoder_deinit(zh_encoder_handle_t *handle) return ESP_OK; } +esp_err_t zh_encoder_reinit(zh_encoder_handle_t *handle, double min, double max, double step) +{ + ZH_LOGI("Encoder reinitialization started."); + ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder reinitialization failed. Invalid argument."); + ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder reinitialization failed. Encoder not initialized."); + ZH_ERROR_CHECK(max > min, ESP_ERR_INVALID_ARG, NULL, "Encoder reinitialization failed. Invalid encoder min/max value."); + ZH_ERROR_CHECK(step > 0, ESP_ERR_INVALID_ARG, NULL, "Encoder reinitialization failed. Invalid encoder step."); + taskENTER_CRITICAL(&_spinlock); + handle->encoder_min_value = min; + handle->encoder_max_value = max; + handle->encoder_step = step; + handle->encoder_position = (handle->encoder_min_value + handle->encoder_max_value) / 2; + taskEXIT_CRITICAL(&_spinlock); + ZH_LOGI("Encoder reinitialization completed successfully."); + return ESP_OK; +} + esp_err_t zh_encoder_set(zh_encoder_handle_t *handle, double position) { ZH_LOGI("Encoder set position started.");