Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| a0ecc5f3b8 | |||
| 36e200b972 | |||
| 579af4f6fc | |||
| d6812c3fdb | |||
| 9a716209b2 | |||
| 8b946d7576 | |||
| f2d1822ab4 | |||
| 8e77720d7f |
28
README.md
28
README.md
@@ -55,19 +55,19 @@ void app_main(void)
|
|||||||
esp_log_level_set("zh_encoder", ESP_LOG_ERROR);
|
esp_log_level_set("zh_encoder", ESP_LOG_ERROR);
|
||||||
esp_event_loop_create_default();
|
esp_event_loop_create_default();
|
||||||
esp_event_handler_instance_register(ZH_ENCODER, ESP_EVENT_ANY_ID, &zh_encoder_event_handler, NULL, NULL);
|
esp_event_handler_instance_register(ZH_ENCODER, ESP_EVENT_ANY_ID, &zh_encoder_event_handler, NULL, NULL);
|
||||||
zh_encoder_init_config_t encoder_init_config = ZH_ENCODER_INIT_CONFIG_DEFAULT();
|
zh_encoder_init_config_t config = ZH_ENCODER_INIT_CONFIG_DEFAULT();
|
||||||
encoder_init_config.task_priority = 5;
|
config.task_priority = 5;
|
||||||
encoder_init_config.stack_size = configMINIMAL_STACK_SIZE;
|
config.stack_size = configMINIMAL_STACK_SIZE;
|
||||||
encoder_init_config.queue_size = 5;
|
config.queue_size = 5;
|
||||||
encoder_init_config.a_gpio_number = GPIO_NUM_4;
|
config.a_gpio_number = GPIO_NUM_4;
|
||||||
encoder_init_config.b_gpio_number = GPIO_NUM_16;
|
config.b_gpio_number = GPIO_NUM_16;
|
||||||
encoder_init_config.s_gpio_number = GPIO_NUM_15;
|
config.s_gpio_number = GPIO_NUM_15;
|
||||||
encoder_init_config.encoder_min_value = -10;
|
config.encoder_min_value = -10;
|
||||||
encoder_init_config.encoder_max_value = 10;
|
config.encoder_max_value = 10;
|
||||||
encoder_init_config.encoder_step = 0.001;
|
config.encoder_step = 0.001;
|
||||||
encoder_init_config.encoder_number = ENCODER_NUMBER;
|
config.encoder_number = ENCODER_NUMBER;
|
||||||
zh_encoder_init(&encoder_init_config, &encoder_handle);
|
zh_encoder_init(&config, &encoder_handle);
|
||||||
double encoder_position = 0;
|
float encoder_position = 0;
|
||||||
zh_encoder_get(&encoder_handle, &encoder_position);
|
zh_encoder_get(&encoder_handle, &encoder_position);
|
||||||
printf("Encoder position %0.3f.\n", encoder_position);
|
printf("Encoder position %0.3f.\n", encoder_position);
|
||||||
for (;;)
|
for (;;)
|
||||||
@@ -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;
|
||||||
|
|||||||
@@ -24,6 +24,7 @@
|
|||||||
.b_gpio_number = GPIO_NUM_MAX, \
|
.b_gpio_number = GPIO_NUM_MAX, \
|
||||||
.s_gpio_number = GPIO_NUM_MAX, \
|
.s_gpio_number = GPIO_NUM_MAX, \
|
||||||
.s_gpio_debounce_time = 10, \
|
.s_gpio_debounce_time = 10, \
|
||||||
|
.pullup = true, \
|
||||||
.encoder_min_value = -10, \
|
.encoder_min_value = -10, \
|
||||||
.encoder_max_value = 10, \
|
.encoder_max_value = 10, \
|
||||||
.encoder_step = 1, \
|
.encoder_step = 1, \
|
||||||
@@ -41,14 +42,15 @@ extern "C"
|
|||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
double encoder_step; /*!< Encoder step. @note Must be greater than 0. */
|
float 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. */
|
float 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. */
|
float 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. */
|
||||||
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. */
|
||||||
|
bool pullup; /*!< Pullup GPIO enable/disable. */
|
||||||
uint16_t s_gpio_debounce_time; /*!< Encoder button debounce_time. @note In microseconds. */
|
uint16_t s_gpio_debounce_time; /*!< Encoder button debounce_time. @note In microseconds. */
|
||||||
uint8_t encoder_number; /*!< Unique encoder number. @note Must be greater than 0. */
|
uint8_t encoder_number; /*!< Unique encoder number. @note Must be greater than 0. */
|
||||||
uint16_t stack_size; /*!< Stack size for task for the encoder isr processing processing. @note The minimum size is configMINIMAL_STACK_SIZE. */
|
uint16_t stack_size; /*!< Stack size for task for the encoder isr processing processing. @note The minimum size is configMINIMAL_STACK_SIZE. */
|
||||||
@@ -57,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. */
|
|
||||||
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 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -102,8 +105,8 @@ extern "C"
|
|||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
double encoder_position; /*!< Encoder current position. */
|
float encoder_position; /*!< Encoder current position. */
|
||||||
uint8_t encoder_number; /*!< Encoder unique number. */
|
uint8_t encoder_number; /*!< Encoder unique number. */
|
||||||
} zh_encoder_event_on_isr_t;
|
} zh_encoder_event_on_isr_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -136,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);
|
||||||
@@ -152,7 +157,7 @@ extern "C"
|
|||||||
*
|
*
|
||||||
* @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_reinit(zh_encoder_handle_t *handle, double min, double max, double step);
|
esp_err_t zh_encoder_reinit(zh_encoder_handle_t *handle, float min, float max, float step);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set encoder position.
|
* @brief Set encoder position.
|
||||||
@@ -162,7 +167,7 @@ extern "C"
|
|||||||
*
|
*
|
||||||
* @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_set(zh_encoder_handle_t *handle, double position);
|
esp_err_t zh_encoder_set(zh_encoder_handle_t *handle, float position);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get encoder position.
|
* @brief Get encoder position.
|
||||||
@@ -172,7 +177,7 @@ extern "C"
|
|||||||
*
|
*
|
||||||
* @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_get(const zh_encoder_handle_t *handle, double *position);
|
esp_err_t zh_encoder_get(const zh_encoder_handle_t *handle, float *position);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Reset encoder position.
|
* @brief Reset encoder position.
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
2.0.0
|
2.3.0
|
||||||
108
zh_encoder.c
108
zh_encoder.c
@@ -28,7 +28,7 @@ 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);
|
static esp_err_t _zh_encoder_pcnt_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);
|
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_resources_init(const zh_encoder_init_config_t *config);
|
static esp_err_t _zh_encoder_resources_init(const zh_encoder_init_config_t *config);
|
||||||
static esp_err_t _zh_encoder_task_init(const zh_encoder_init_config_t *config);
|
static esp_err_t _zh_encoder_task_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle);
|
||||||
static bool _zh_encoder_isr_handler(pcnt_unit_handle_t unit, const pcnt_watch_event_data_t *edata, void *user_ctx);
|
static bool _zh_encoder_isr_handler(pcnt_unit_handle_t unit, const pcnt_watch_event_data_t *edata, void *user_ctx);
|
||||||
static void _zh_encoder_isr_processing_task(void *pvParameter);
|
static void _zh_encoder_isr_processing_task(void *pvParameter);
|
||||||
static void _zh_encoder_button_isr_handler(void *arg);
|
static void _zh_encoder_button_isr_handler(void *arg);
|
||||||
@@ -39,12 +39,13 @@ 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.");
|
||||||
err = _zh_encoder_resources_init(config);
|
err = _zh_encoder_resources_init(config);
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. Resources initialization failed.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. Resources initialization failed.");
|
||||||
err = _zh_encoder_task_init(config);
|
err = _zh_encoder_task_init(config, handle);
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, vQueueDelete(_queue_handle); _queue_handle = NULL, "Encoder initialization failed. Processing task initialization failed.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, vQueueDelete(_queue_handle); _queue_handle = NULL, "Encoder initialization failed. Processing task initialization failed.");
|
||||||
err = _zh_encoder_pcnt_init(config, handle);
|
err = _zh_encoder_pcnt_init(config, handle);
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, vQueueDelete(_queue_handle); _queue_handle = NULL; vTaskDelete(zh_encoder); zh_encoder = NULL,
|
ZH_ERROR_CHECK(err == ESP_OK, err, vQueueDelete(_queue_handle); _queue_handle = NULL; vTaskDelete(zh_encoder); zh_encoder = NULL,
|
||||||
@@ -103,7 +104,7 @@ esp_err_t zh_encoder_deinit(zh_encoder_handle_t *handle)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t zh_encoder_reinit(zh_encoder_handle_t *handle, double min, double max, double step) // -V2008
|
esp_err_t zh_encoder_reinit(zh_encoder_handle_t *handle, float min, float max, float step) // -V2008
|
||||||
{
|
{
|
||||||
ZH_LOGI("Encoder reinitialization started.");
|
ZH_LOGI("Encoder reinitialization started.");
|
||||||
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder reinitialization failed. Invalid argument.");
|
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder reinitialization failed. Invalid argument.");
|
||||||
@@ -120,7 +121,7 @@ esp_err_t zh_encoder_reinit(zh_encoder_handle_t *handle, double min, double max,
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t zh_encoder_set(zh_encoder_handle_t *handle, double position)
|
esp_err_t zh_encoder_set(zh_encoder_handle_t *handle, float position)
|
||||||
{
|
{
|
||||||
ZH_LOGI("Encoder set position started.");
|
ZH_LOGI("Encoder set position started.");
|
||||||
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder set position failed. Invalid argument.");
|
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder set position failed. Invalid argument.");
|
||||||
@@ -133,7 +134,7 @@ esp_err_t zh_encoder_set(zh_encoder_handle_t *handle, double position)
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t zh_encoder_get(const zh_encoder_handle_t *handle, double *position)
|
esp_err_t zh_encoder_get(const zh_encoder_handle_t *handle, float *position)
|
||||||
{
|
{
|
||||||
ZH_LOGI("Encoder get position started.");
|
ZH_LOGI("Encoder get position started.");
|
||||||
ZH_ERROR_CHECK(handle != NULL && position != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder get position failed. Invalid argument.");
|
ZH_ERROR_CHECK(handle != NULL && position != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder get position failed. Invalid argument.");
|
||||||
@@ -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 = {
|
||||||
@@ -235,7 +234,7 @@ static esp_err_t _zh_encoder_pcnt_init(const zh_encoder_init_config_t *config, z
|
|||||||
pcnt_event_callbacks_t cbs = {
|
pcnt_event_callbacks_t cbs = {
|
||||||
.on_reach = _zh_encoder_isr_handler,
|
.on_reach = _zh_encoder_isr_handler,
|
||||||
};
|
};
|
||||||
err = pcnt_unit_register_event_callbacks(pcnt_unit_handle, &cbs, handle);
|
err = pcnt_unit_register_event_callbacks(pcnt_unit_handle, &cbs, NULL);
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_channel(pcnt_channel_b_handle); pcnt_del_unit(pcnt_unit_handle),
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_channel(pcnt_channel_b_handle); pcnt_del_unit(pcnt_unit_handle),
|
||||||
"PCNT initialization failed.");
|
"PCNT initialization failed.");
|
||||||
err = pcnt_unit_enable(pcnt_unit_handle);
|
err = pcnt_unit_enable(pcnt_unit_handle);
|
||||||
@@ -247,6 +246,11 @@ static esp_err_t _zh_encoder_pcnt_init(const zh_encoder_init_config_t *config, z
|
|||||||
err = pcnt_unit_start(pcnt_unit_handle);
|
err = pcnt_unit_start(pcnt_unit_handle);
|
||||||
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_unit_disable(pcnt_unit_handle); pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_channel(pcnt_channel_b_handle);
|
ZH_ERROR_CHECK(err == ESP_OK, err, pcnt_unit_disable(pcnt_unit_handle); pcnt_del_channel(pcnt_channel_a_handle); pcnt_del_channel(pcnt_channel_b_handle);
|
||||||
pcnt_del_unit(pcnt_unit_handle), "PCNT initialization failed.");
|
pcnt_del_unit(pcnt_unit_handle), "PCNT initialization failed.");
|
||||||
|
if (config->pullup == false)
|
||||||
|
{
|
||||||
|
gpio_pullup_dis((gpio_num_t)config->a_gpio_number);
|
||||||
|
gpio_pullup_dis((gpio_num_t)config->b_gpio_number);
|
||||||
|
}
|
||||||
handle->pcnt_unit_handle = pcnt_unit_handle;
|
handle->pcnt_unit_handle = pcnt_unit_handle;
|
||||||
handle->pcnt_channel_a_handle = pcnt_channel_a_handle;
|
handle->pcnt_channel_a_handle = pcnt_channel_a_handle;
|
||||||
handle->pcnt_channel_b_handle = pcnt_channel_b_handle;
|
handle->pcnt_channel_b_handle = pcnt_channel_b_handle;
|
||||||
@@ -255,13 +259,13 @@ 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)
|
||||||
{
|
{
|
||||||
gpio_config_t pin_config = {
|
gpio_config_t pin_config = {
|
||||||
.mode = GPIO_MODE_INPUT,
|
.mode = GPIO_MODE_INPUT,
|
||||||
|
.pull_up_en = (config->pullup == true) ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE,
|
||||||
.pin_bit_mask = (1ULL << config->s_gpio_number),
|
.pin_bit_mask = (1ULL << config->s_gpio_number),
|
||||||
.intr_type = GPIO_INTR_ANYEDGE};
|
.intr_type = GPIO_INTR_ANYEDGE};
|
||||||
esp_err_t err = gpio_config(&pin_config);
|
esp_err_t err = gpio_config(&pin_config);
|
||||||
@@ -281,17 +285,17 @@ static esp_err_t _zh_encoder_resources_init(const zh_encoder_init_config_t *conf
|
|||||||
{
|
{
|
||||||
if (_encoder_counter == 0)
|
if (_encoder_counter == 0)
|
||||||
{
|
{
|
||||||
_queue_handle = xQueueCreate(config->queue_size, sizeof(zh_encoder_event_on_isr_t));
|
_queue_handle = xQueueCreate(config->queue_size, sizeof(int));
|
||||||
ZH_ERROR_CHECK(_queue_handle != NULL, ESP_FAIL, NULL, "Failed to create queue.");
|
ZH_ERROR_CHECK(_queue_handle != NULL, ESP_FAIL, NULL, "Failed to create queue.");
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t _zh_encoder_task_init(const zh_encoder_init_config_t *config)
|
static esp_err_t _zh_encoder_task_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle)
|
||||||
{
|
{
|
||||||
if (_encoder_counter == 0)
|
if (_encoder_counter == 0)
|
||||||
{
|
{
|
||||||
BaseType_t err = xTaskCreatePinnedToCore(&_zh_encoder_isr_processing_task, "zh_encoder_isr_processing", config->stack_size, NULL, config->task_priority,
|
BaseType_t err = xTaskCreatePinnedToCore(&_zh_encoder_isr_processing_task, "zh_encoder_isr_processing", config->stack_size, handle, config->task_priority,
|
||||||
&zh_encoder, tskNO_AFFINITY);
|
&zh_encoder, tskNO_AFFINITY);
|
||||||
ZH_ERROR_CHECK(err == pdPASS, ESP_FAIL, NULL, "Failed to create isr processing task.");
|
ZH_ERROR_CHECK(err == pdPASS, ESP_FAIL, NULL, "Failed to create isr processing task.");
|
||||||
}
|
}
|
||||||
@@ -301,38 +305,8 @@ static esp_err_t _zh_encoder_task_init(const zh_encoder_init_config_t *config)
|
|||||||
static bool IRAM_ATTR _zh_encoder_isr_handler(pcnt_unit_handle_t unit, const pcnt_watch_event_data_t *edata, void *user_ctx)
|
static bool IRAM_ATTR _zh_encoder_isr_handler(pcnt_unit_handle_t unit, const pcnt_watch_event_data_t *edata, void *user_ctx)
|
||||||
{
|
{
|
||||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||||
zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)user_ctx;
|
|
||||||
pcnt_unit_clear_count(unit);
|
pcnt_unit_clear_count(unit);
|
||||||
switch (edata->watch_point_value)
|
if (xQueueSendFromISR(_queue_handle, &edata->watch_point_value, &xHigherPriorityTaskWoken) != pdTRUE)
|
||||||
{
|
|
||||||
case ZH_ENCODER_DIRECTION_CW:
|
|
||||||
if (encoder_handle->encoder_position < encoder_handle->encoder_max_value)
|
|
||||||
{
|
|
||||||
encoder_handle->encoder_position = encoder_handle->encoder_position + encoder_handle->encoder_step;
|
|
||||||
if (encoder_handle->encoder_position > encoder_handle->encoder_max_value)
|
|
||||||
{
|
|
||||||
encoder_handle->encoder_position = encoder_handle->encoder_max_value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case ZH_ENCODER_DIRECTION_CCW:
|
|
||||||
if (encoder_handle->encoder_position > encoder_handle->encoder_min_value)
|
|
||||||
{
|
|
||||||
encoder_handle->encoder_position = encoder_handle->encoder_position - encoder_handle->encoder_step;
|
|
||||||
if (encoder_handle->encoder_position < encoder_handle->encoder_min_value)
|
|
||||||
{
|
|
||||||
encoder_handle->encoder_position = encoder_handle->encoder_min_value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return false;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
if (xQueueSendFromISR(_queue_handle, &encoder_data, &xHigherPriorityTaskWoken) != pdTRUE)
|
|
||||||
{
|
{
|
||||||
++_stats.queue_overflow_error;
|
++_stats.queue_overflow_error;
|
||||||
}
|
}
|
||||||
@@ -345,14 +319,50 @@ 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_event_on_isr_t encoder_data = {0};
|
zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)pvParameter;
|
||||||
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)
|
||||||
{
|
{
|
||||||
esp_err_t err = esp_event_post(ZH_ENCODER, ZH_ENCODER_EVENT, &encoder_data, sizeof(zh_encoder_event_on_isr_t), 1000 / portTICK_PERIOD_MS);
|
switch (pcnt_count)
|
||||||
if (err != ESP_OK)
|
|
||||||
{
|
{
|
||||||
++_stats.event_post_error;
|
case ZH_ENCODER_DIRECTION_CW:
|
||||||
ZH_LOGE("Encoder isr processing failed. Failed to post interrupt event.", err);
|
if (encoder_handle->encoder_position < encoder_handle->encoder_max_value)
|
||||||
|
{
|
||||||
|
encoder_handle->encoder_position = encoder_handle->encoder_position + encoder_handle->encoder_step;
|
||||||
|
if (encoder_handle->encoder_position > encoder_handle->encoder_max_value)
|
||||||
|
{
|
||||||
|
encoder_handle->encoder_position = encoder_handle->encoder_max_value;
|
||||||
|
}
|
||||||
|
is_value_changed = true;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
case ZH_ENCODER_DIRECTION_CCW:
|
||||||
|
if (encoder_handle->encoder_position > encoder_handle->encoder_min_value)
|
||||||
|
{
|
||||||
|
encoder_handle->encoder_position = encoder_handle->encoder_position - encoder_handle->encoder_step;
|
||||||
|
if (encoder_handle->encoder_position < encoder_handle->encoder_min_value)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
esp_err_t err = esp_event_post(ZH_ENCODER, ZH_ENCODER_EVENT, &encoder_data, sizeof(zh_encoder_event_on_isr_t), 1000 / portTICK_PERIOD_MS);
|
||||||
|
if (err != ESP_OK)
|
||||||
|
{
|
||||||
|
++_stats.event_post_error;
|
||||||
|
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);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user