Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 361d2f5fe9 | |||
| 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:
|
||||
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;
|
||||
case ZH_ENCODER_EVENT:
|
||||
zh_encoder_event_on_isr_t *encoder_event = event_data;
|
||||
|
||||
@@ -59,21 +59,22 @@ extern "C"
|
||||
/**
|
||||
* @brief Encoder handle.
|
||||
*/
|
||||
typedef struct // -V802
|
||||
typedef struct
|
||||
{
|
||||
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. */
|
||||
float encoder_step; /*!< Encoder step. */
|
||||
float encoder_position; /*!< Encoder position. */
|
||||
float encoder_min_value; /*!< Encoder min value. */
|
||||
float encoder_max_value; /*!< Encoder max value. */
|
||||
bool s_gpio_status; /*!< Encoder button status. */
|
||||
bool is_initialized; /*!< Encoder initialization flag. */
|
||||
uint8_t encoder_number; /*!< Encoder unique number. */
|
||||
uint8_t s_gpio_number; /*!< Encoder button GPIO number. */
|
||||
uint16_t s_gpio_debounce_time; /*!< Encoder button debounce_time. */
|
||||
uint64_t s_gpio_prev_time; /*!< Encoder button prev interrupt time. */
|
||||
bool s_gpio_status; /*!< Encoder button status. */
|
||||
bool is_initialized; /*!< Encoder initialization flag. */
|
||||
float encoder_step; /*!< Encoder step. */
|
||||
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;
|
||||
|
||||
/**
|
||||
@@ -138,6 +139,8 @@ extern "C"
|
||||
/**
|
||||
* @brief Deinitialize encoder.
|
||||
*
|
||||
* @param[in, out] handle Pointer to unique encoder handle.
|
||||
*
|
||||
* @return ESP_OK if success or an error code otherwise.
|
||||
*/
|
||||
esp_err_t zh_encoder_deinit(zh_encoder_handle_t *handle);
|
||||
|
||||
@@ -1 +1 @@
|
||||
2.2.0
|
||||
2.3.1
|
||||
21
zh_encoder.c
21
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_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.");
|
||||
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.");
|
||||
@@ -53,6 +54,10 @@ esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_han
|
||||
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_unit_stop(handle->pcnt_unit_handle); pcnt_unit_disable(handle->pcnt_unit_handle); pcnt_del_channel(handle->pcnt_channel_a_handle);
|
||||
pcnt_del_channel(handle->pcnt_channel_b_handle); pcnt_del_unit(handle->pcnt_unit_handle); vQueueDelete(_queue_handle); _queue_handle = NULL;
|
||||
vTaskDelete(zh_encoder); zh_encoder = NULL, "Encoder initialization failed. GPIO initialization failed.");
|
||||
if (_stats.min_stack_size == 0)
|
||||
{
|
||||
_stats.min_stack_size = config->stack_size;
|
||||
}
|
||||
handle->is_initialized = true;
|
||||
++_encoder_counter;
|
||||
for (uint8_t i = 0; i < sizeof(_encoder_number_matrix); ++i)
|
||||
@@ -171,7 +176,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
|
||||
{
|
||||
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->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.");
|
||||
@@ -191,7 +195,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
|
||||
{
|
||||
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 != config->b_gpio_number, ESP_ERR_INVALID_ARG, NULL, "Encoder A and B GPIO is same.")
|
||||
pcnt_unit_config_t pcnt_unit_config = {
|
||||
@@ -260,7 +263,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
|
||||
{
|
||||
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->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)
|
||||
@@ -322,10 +324,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)
|
||||
{
|
||||
zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)pvParameter;
|
||||
int encoder_data = {0};
|
||||
while (xQueueReceive(_queue_handle, &encoder_data, portMAX_DELAY) == pdTRUE)
|
||||
int pcnt_count = {0};
|
||||
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:
|
||||
if (encoder_handle->encoder_position < encoder_handle->encoder_max_value)
|
||||
@@ -335,6 +338,7 @@ static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
|
||||
{
|
||||
encoder_handle->encoder_position = encoder_handle->encoder_max_value;
|
||||
}
|
||||
is_value_changed = true;
|
||||
}
|
||||
break;
|
||||
case ZH_ENCODER_DIRECTION_CCW:
|
||||
@@ -345,11 +349,15 @@ static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
|
||||
{
|
||||
encoder_handle->encoder_position = encoder_handle->encoder_min_value;
|
||||
}
|
||||
is_value_changed = true;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
if (is_value_changed == true)
|
||||
{
|
||||
is_value_changed = false;
|
||||
zh_encoder_event_on_isr_t encoder_data = {0};
|
||||
encoder_data.encoder_number = encoder_handle->encoder_number;
|
||||
encoder_data.encoder_position = encoder_handle->encoder_position;
|
||||
@@ -359,6 +367,7 @@ static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
|
||||
++_stats.event_post_error;
|
||||
ZH_LOGE("Encoder isr processing failed. Failed to post interrupt event.", err);
|
||||
}
|
||||
}
|
||||
_stats.min_stack_size = (uint32_t)uxTaskGetStackHighWaterMark(NULL);
|
||||
}
|
||||
vTaskDelete(NULL);
|
||||
|
||||
Reference in New Issue
Block a user