Compare commits
5 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a0ecc5f3b8 | |||
| 36e200b972 | |||
| 579af4f6fc | |||
| d6812c3fdb | |||
| 9a716209b2 |
@@ -86,7 +86,7 @@ void zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t ev
|
|||||||
{
|
{
|
||||||
case ZH_BUTTON_EVENT:
|
case ZH_BUTTON_EVENT:
|
||||||
zh_encoder_button_event_on_isr_t *button_event = event_data;
|
zh_encoder_button_event_on_isr_t *button_event = event_data;
|
||||||
printf("Encoder number %d button %s.\n", button_event->encoder_number, button_event->button_status == 1 ? "released" : "pressed");
|
printf("Encoder number %d button %s.\n", button_event->encoder_number, (button_event->button_status == 1) ? "released" : "pressed");
|
||||||
break;
|
break;
|
||||||
case ZH_ENCODER_EVENT:
|
case ZH_ENCODER_EVENT:
|
||||||
zh_encoder_event_on_isr_t *encoder_event = event_data;
|
zh_encoder_event_on_isr_t *encoder_event = event_data;
|
||||||
|
|||||||
@@ -59,21 +59,22 @@ extern "C"
|
|||||||
/**
|
/**
|
||||||
* @brief Encoder handle.
|
* @brief Encoder handle.
|
||||||
*/
|
*/
|
||||||
typedef struct // -V802
|
typedef struct
|
||||||
{
|
{
|
||||||
pcnt_unit_handle_t pcnt_unit_handle; /*!< Encoder unique pcnt unit handle. */
|
bool s_gpio_status; /*!< Encoder button status. */
|
||||||
pcnt_channel_handle_t pcnt_channel_a_handle; /*!< Encoder unique pcnt channel handle. */
|
bool is_initialized; /*!< Encoder initialization flag. */
|
||||||
pcnt_channel_handle_t pcnt_channel_b_handle; /*!< Encoder unique pcnt channel handle. */
|
|
||||||
float encoder_step; /*!< Encoder step. */
|
|
||||||
float encoder_position; /*!< Encoder position. */
|
|
||||||
float encoder_min_value; /*!< Encoder min value. */
|
|
||||||
float encoder_max_value; /*!< Encoder max value. */
|
|
||||||
uint8_t encoder_number; /*!< Encoder unique number. */
|
uint8_t encoder_number; /*!< Encoder unique number. */
|
||||||
uint8_t s_gpio_number; /*!< Encoder button GPIO number. */
|
uint8_t s_gpio_number; /*!< Encoder button GPIO number. */
|
||||||
uint16_t s_gpio_debounce_time; /*!< Encoder button debounce_time. */
|
uint16_t s_gpio_debounce_time; /*!< Encoder button debounce_time. */
|
||||||
uint64_t s_gpio_prev_time; /*!< Encoder button prev interrupt time. */
|
uint64_t s_gpio_prev_time; /*!< Encoder button prev interrupt time. */
|
||||||
bool s_gpio_status; /*!< Encoder button status. */
|
float encoder_step; /*!< Encoder step. */
|
||||||
bool is_initialized; /*!< Encoder initialization flag. */
|
float encoder_position; /*!< Encoder position. */
|
||||||
|
float encoder_min_value; /*!< Encoder min value. */
|
||||||
|
float encoder_max_value; /*!< Encoder max value. */
|
||||||
|
pcnt_unit_handle_t pcnt_unit_handle; /*!< Encoder unique pcnt unit handle. */
|
||||||
|
pcnt_channel_handle_t pcnt_channel_a_handle; /*!< Encoder unique pcnt channel handle. */
|
||||||
|
pcnt_channel_handle_t pcnt_channel_b_handle; /*!< Encoder unique pcnt channel handle. */
|
||||||
|
|
||||||
} zh_encoder_handle_t;
|
} zh_encoder_handle_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -138,6 +139,8 @@ extern "C"
|
|||||||
/**
|
/**
|
||||||
* @brief Deinitialize encoder.
|
* @brief Deinitialize encoder.
|
||||||
*
|
*
|
||||||
|
* @param[in, out] handle Pointer to unique encoder handle.
|
||||||
|
*
|
||||||
* @return ESP_OK if success or an error code otherwise.
|
* @return ESP_OK if success or an error code otherwise.
|
||||||
*/
|
*/
|
||||||
esp_err_t zh_encoder_deinit(zh_encoder_handle_t *handle);
|
esp_err_t zh_encoder_deinit(zh_encoder_handle_t *handle);
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
2.2.0
|
2.3.0
|
||||||
17
zh_encoder.c
17
zh_encoder.c
@@ -39,6 +39,7 @@ esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_han
|
|||||||
{
|
{
|
||||||
ZH_LOGI("Encoder initialization started.");
|
ZH_LOGI("Encoder initialization started.");
|
||||||
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder initialization failed. Invalid argument.");
|
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder initialization failed. Invalid argument.");
|
||||||
|
ZH_ERROR_CHECK(handle->is_initialized == false, ESP_ERR_INVALID_STATE, NULL, "Encoder initialization failed. Encoder is already initialized.");
|
||||||
ZH_ERROR_CHECK(_encoder_counter < sizeof(_encoder_number_matrix), ESP_ERR_INVALID_ARG, NULL, "Encoder initialization failed. Maximum quantity reached.");
|
ZH_ERROR_CHECK(_encoder_counter < sizeof(_encoder_number_matrix), ESP_ERR_INVALID_ARG, NULL, "Encoder initialization failed. Maximum quantity reached.");
|
||||||
esp_err_t err = _zh_encoder_validate_config(config, handle);
|
esp_err_t err = _zh_encoder_validate_config(config, handle);
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. Initial configuration check failed.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. Initial configuration check failed.");
|
||||||
@@ -171,7 +172,6 @@ void zh_encoder_reset_stats(void)
|
|||||||
|
|
||||||
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008
|
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008
|
||||||
{
|
{
|
||||||
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid argument.");
|
|
||||||
ZH_ERROR_CHECK(config->task_priority >= 1 && config->stack_size >= configMINIMAL_STACK_SIZE, ESP_ERR_INVALID_ARG, NULL, "Invalid task settings.");
|
ZH_ERROR_CHECK(config->task_priority >= 1 && config->stack_size >= configMINIMAL_STACK_SIZE, ESP_ERR_INVALID_ARG, NULL, "Invalid task settings.");
|
||||||
ZH_ERROR_CHECK(config->queue_size >= 1, ESP_ERR_INVALID_ARG, NULL, "Invalid queue size.");
|
ZH_ERROR_CHECK(config->queue_size >= 1, ESP_ERR_INVALID_ARG, NULL, "Invalid queue size.");
|
||||||
ZH_ERROR_CHECK(config->encoder_max_value > config->encoder_min_value, ESP_ERR_INVALID_ARG, NULL, "Invalid encoder min/max value.");
|
ZH_ERROR_CHECK(config->encoder_max_value > config->encoder_min_value, ESP_ERR_INVALID_ARG, NULL, "Invalid encoder min/max value.");
|
||||||
@@ -191,7 +191,6 @@ static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *con
|
|||||||
|
|
||||||
static esp_err_t _zh_encoder_pcnt_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008
|
static esp_err_t _zh_encoder_pcnt_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008
|
||||||
{
|
{
|
||||||
ZH_ERROR_CHECK(config != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid argument.");
|
|
||||||
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.")
|
||||||
pcnt_unit_config_t pcnt_unit_config = {
|
pcnt_unit_config_t pcnt_unit_config = {
|
||||||
@@ -260,7 +259,6 @@ static esp_err_t _zh_encoder_pcnt_init(const zh_encoder_init_config_t *config, z
|
|||||||
|
|
||||||
static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle) // -V2008
|
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 != NULL && handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid argument.");
|
|
||||||
ZH_ERROR_CHECK(config->s_gpio_number <= GPIO_NUM_MAX, ESP_ERR_INVALID_ARG, NULL, "Invalid GPIO number.")
|
ZH_ERROR_CHECK(config->s_gpio_number <= GPIO_NUM_MAX, ESP_ERR_INVALID_ARG, NULL, "Invalid GPIO number.")
|
||||||
ZH_ERROR_CHECK(config->a_gpio_number != config->s_gpio_number && config->b_gpio_number != config->s_gpio_number, ESP_ERR_INVALID_ARG, NULL, "Encoder GPIO and button GPIO is same.")
|
ZH_ERROR_CHECK(config->a_gpio_number != config->s_gpio_number && config->b_gpio_number != config->s_gpio_number, ESP_ERR_INVALID_ARG, NULL, "Encoder GPIO and button GPIO is same.")
|
||||||
if (config->s_gpio_number != GPIO_NUM_MAX)
|
if (config->s_gpio_number != GPIO_NUM_MAX)
|
||||||
@@ -322,10 +320,11 @@ static bool IRAM_ATTR _zh_encoder_isr_handler(pcnt_unit_handle_t unit, const pcn
|
|||||||
static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
|
static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
|
||||||
{
|
{
|
||||||
zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)pvParameter;
|
zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)pvParameter;
|
||||||
int encoder_data = {0};
|
int pcnt_count = {0};
|
||||||
while (xQueueReceive(_queue_handle, &encoder_data, portMAX_DELAY) == pdTRUE)
|
bool is_value_changed = false;
|
||||||
|
while (xQueueReceive(_queue_handle, &pcnt_count, portMAX_DELAY) == pdTRUE)
|
||||||
{
|
{
|
||||||
switch (encoder_data)
|
switch (pcnt_count)
|
||||||
{
|
{
|
||||||
case ZH_ENCODER_DIRECTION_CW:
|
case ZH_ENCODER_DIRECTION_CW:
|
||||||
if (encoder_handle->encoder_position < encoder_handle->encoder_max_value)
|
if (encoder_handle->encoder_position < encoder_handle->encoder_max_value)
|
||||||
@@ -335,6 +334,7 @@ static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
|
|||||||
{
|
{
|
||||||
encoder_handle->encoder_position = encoder_handle->encoder_max_value;
|
encoder_handle->encoder_position = encoder_handle->encoder_max_value;
|
||||||
}
|
}
|
||||||
|
is_value_changed = true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case ZH_ENCODER_DIRECTION_CCW:
|
case ZH_ENCODER_DIRECTION_CCW:
|
||||||
@@ -345,11 +345,15 @@ static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
|
|||||||
{
|
{
|
||||||
encoder_handle->encoder_position = encoder_handle->encoder_min_value;
|
encoder_handle->encoder_position = encoder_handle->encoder_min_value;
|
||||||
}
|
}
|
||||||
|
is_value_changed = true;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
if (is_value_changed == true)
|
||||||
|
{
|
||||||
|
is_value_changed = false;
|
||||||
zh_encoder_event_on_isr_t encoder_data = {0};
|
zh_encoder_event_on_isr_t encoder_data = {0};
|
||||||
encoder_data.encoder_number = encoder_handle->encoder_number;
|
encoder_data.encoder_number = encoder_handle->encoder_number;
|
||||||
encoder_data.encoder_position = encoder_handle->encoder_position;
|
encoder_data.encoder_position = encoder_handle->encoder_position;
|
||||||
@@ -359,6 +363,7 @@ static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
|
|||||||
++_stats.event_post_error;
|
++_stats.event_post_error;
|
||||||
ZH_LOGE("Encoder isr processing failed. Failed to post interrupt event.", err);
|
ZH_LOGE("Encoder isr processing failed. Failed to post interrupt event.", err);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
_stats.min_stack_size = (uint32_t)uxTaskGetStackHighWaterMark(NULL);
|
_stats.min_stack_size = (uint32_t)uxTaskGetStackHighWaterMark(NULL);
|
||||||
}
|
}
|
||||||
vTaskDelete(NULL);
|
vTaskDelete(NULL);
|
||||||
|
|||||||
Reference in New Issue
Block a user