Compare commits
10 Commits
v1.2.0
...
fca6da29cc
| Author | SHA1 | Date | |
|---|---|---|---|
| fca6da29cc | |||
| 624a6b4661 | |||
| 020194431b | |||
| 4efbe96de1 | |||
| 7d2bd7fd2e | |||
| b70d625a05 | |||
| 28d067eb98 | |||
| be29ff31f7 | |||
| 5ef3913eb9 | |||
| c4d6bf7dec |
@@ -1,6 +1 @@
|
|||||||
if(${IDF_TARGET} STREQUAL esp8266)
|
idf_component_register(SRCS "zh_encoder.c" INCLUDE_DIRS "include" REQUIRES esp_event driver esp_timer)
|
||||||
set(requires driver)
|
|
||||||
else()
|
|
||||||
set(requires driver esp_event)
|
|
||||||
endif()
|
|
||||||
idf_component_register(SRCS "zh_encoder.c" INCLUDE_DIRS "include" REQUIRES ${requires})
|
|
||||||
61
README.md
61
README.md
@@ -4,10 +4,23 @@
|
|||||||
|
|
||||||
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.
|
||||||
|
|
||||||
|
## Attention
|
||||||
|
|
||||||
|
1. If the button is not used, specify any free GPIO in the initial configuration.
|
||||||
|
2. For correct operation, please enable the following settings in the menuconfig:
|
||||||
|
|
||||||
|
```text
|
||||||
|
GPIO_CTRL_FUNC_IN_IRAM
|
||||||
|
```
|
||||||
|
|
||||||
## Using
|
## Using
|
||||||
|
|
||||||
In an existing project, run the following command to install the components:
|
In an existing project, run the following command to install the components:
|
||||||
@@ -30,8 +43,12 @@ One encoder on device:
|
|||||||
```c
|
```c
|
||||||
#include "zh_encoder.h"
|
#include "zh_encoder.h"
|
||||||
|
|
||||||
|
#define ENCODER_NUMBER 0x01
|
||||||
|
|
||||||
zh_encoder_handle_t encoder_handle = {0};
|
zh_encoder_handle_t encoder_handle = {0};
|
||||||
|
|
||||||
|
double encoder_position = 0;
|
||||||
|
|
||||||
void zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
void zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data);
|
||||||
|
|
||||||
void app_main(void)
|
void app_main(void)
|
||||||
@@ -39,19 +56,20 @@ 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.a_gpio_number = GPIO_NUM_26;
|
config.task_priority = 5;
|
||||||
encoder_init_config.b_gpio_number = GPIO_NUM_27;
|
config.stack_size = configMINIMAL_STACK_SIZE;
|
||||||
encoder_init_config.encoder_min_value = -10;
|
config.queue_size = 5;
|
||||||
encoder_init_config.encoder_max_value = 20;
|
config.a_gpio_number = GPIO_NUM_27;
|
||||||
encoder_init_config.encoder_step = 0.1;
|
config.b_gpio_number = GPIO_NUM_26;
|
||||||
encoder_init_config.encoder_number = 1;
|
config.s_gpio_number = GPIO_NUM_17;
|
||||||
zh_encoder_init(&encoder_init_config, &encoder_handle);
|
config.encoder_min_value = 0;
|
||||||
double position = 0;
|
config.encoder_max_value = 100;
|
||||||
zh_encoder_get(&encoder_handle, &position);
|
config.encoder_step = 0.1;
|
||||||
printf("Encoder position %0.2f.\n", position);
|
config.encoder_number = ENCODER_NUMBER;
|
||||||
zh_encoder_set(&encoder_handle, 5);
|
zh_encoder_init(&config, &encoder_handle);
|
||||||
zh_encoder_reset(&encoder_handle);
|
zh_encoder_get(&encoder_handle, &encoder_position);
|
||||||
|
printf("Encoder position %0.2f.\n", encoder_position);
|
||||||
for (;;)
|
for (;;)
|
||||||
{
|
{
|
||||||
const zh_encoder_stats_t *stats = zh_encoder_get_stats();
|
const zh_encoder_stats_t *stats = zh_encoder_get_stats();
|
||||||
@@ -65,6 +83,21 @@ void app_main(void)
|
|||||||
void zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
void zh_encoder_event_handler(void *arg, esp_event_base_t event_base, int32_t event_id, void *event_data)
|
||||||
{
|
{
|
||||||
zh_encoder_event_on_isr_t *event = event_data;
|
zh_encoder_event_on_isr_t *event = event_data;
|
||||||
printf("Encoder number %d position %0.2f.\n", event->encoder_number, event->encoder_position);
|
switch (event->encoder_number)
|
||||||
|
{
|
||||||
|
case ENCODER_NUMBER:
|
||||||
|
if (encoder_position == event->encoder_position)
|
||||||
|
{
|
||||||
|
printf("Encoder number %d button %s.\n", event->encoder_number, event->button_status == 1 ? "released" : "pressed");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
encoder_position = event->encoder_position;
|
||||||
|
printf("Encoder number %d position %0.2f with button %s.\n", event->encoder_number, event->encoder_position, event->button_status == 1 ? "released" : "pressed");
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|||||||
2981
Schematic.pdf
Normal file
2981
Schematic.pdf
Normal file
File diff suppressed because it is too large
Load Diff
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include "esp_log.h"
|
#include "esp_log.h"
|
||||||
#include "driver/gpio.h"
|
#include "driver/gpio.h"
|
||||||
|
#include "esp_timer.h"
|
||||||
#include "freertos/FreeRTOS.h"
|
#include "freertos/FreeRTOS.h"
|
||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
@@ -20,6 +21,7 @@
|
|||||||
.queue_size = 1, \
|
.queue_size = 1, \
|
||||||
.a_gpio_number = GPIO_NUM_MAX, \
|
.a_gpio_number = GPIO_NUM_MAX, \
|
||||||
.b_gpio_number = GPIO_NUM_MAX, \
|
.b_gpio_number = GPIO_NUM_MAX, \
|
||||||
|
.s_gpio_number = GPIO_NUM_MAX, \
|
||||||
.encoder_min_value = -100, \
|
.encoder_min_value = -100, \
|
||||||
.encoder_max_value = 100, \
|
.encoder_max_value = 100, \
|
||||||
.encoder_step = 1, \
|
.encoder_step = 1, \
|
||||||
@@ -37,30 +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. */
|
||||||
int32_t encoder_min_value; /*!< Encoder min value. @note Must be less than encoder_max_value. */
|
uint8_t s_gpio_number; /*!< Encoder button GPIO number. */
|
||||||
int32_t encoder_max_value; /*!< Encoder max value. @note Must be greater than encoder_min_value. */
|
uint8_t encoder_number; /*!< Unique encoder number. @note Must be greater than 0. */
|
||||||
double encoder_step; /*!< Encoder step. @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. */
|
||||||
uint8_t encoder_number; /*!< Unique encoder number. */
|
|
||||||
} zh_encoder_init_config_t;
|
} zh_encoder_init_config_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Encoder handle.
|
* @brief Encoder handle.
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct // -V802
|
||||||
{
|
{
|
||||||
uint8_t a_gpio_number; /*!< Encoder A GPIO number. */
|
|
||||||
uint8_t b_gpio_number; /*!< Encoder B 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_step; /*!< Encoder step. */
|
||||||
double encoder_position; /*!< Encoder position. */
|
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_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;
|
||||||
|
|
||||||
@@ -83,8 +88,9 @@ 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. */
|
||||||
} zh_encoder_event_on_isr_t;
|
} zh_encoder_event_on_isr_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -110,6 +116,20 @@ extern "C"
|
|||||||
*/
|
*/
|
||||||
esp_err_t zh_encoder_deinit(zh_encoder_handle_t *handle);
|
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.
|
* @brief Set encoder position.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
1.2.0
|
1.5.0
|
||||||
118
zh_encoder.c
118
zh_encoder.c
@@ -16,6 +16,8 @@
|
|||||||
#define ZH_ENCODER_DIRECTION_CW 0x10
|
#define ZH_ENCODER_DIRECTION_CW 0x10
|
||||||
#define ZH_ENCODER_DIRECTION_CCW 0x20
|
#define ZH_ENCODER_DIRECTION_CCW 0x20
|
||||||
|
|
||||||
|
#define BUTTON_DEBOUNCE_TIME 20
|
||||||
|
|
||||||
static const uint8_t _encoder_matrix[7][4] = {
|
static const uint8_t _encoder_matrix[7][4] = {
|
||||||
{0x03, 0x02, 0x01, 0x00},
|
{0x03, 0x02, 0x01, 0x00},
|
||||||
{0x23, 0x00, 0x01, 0x00},
|
{0x23, 0x00, 0x01, 0x00},
|
||||||
@@ -27,9 +29,13 @@ static const uint8_t _encoder_matrix[7][4] = {
|
|||||||
|
|
||||||
TaskHandle_t zh_encoder = NULL;
|
TaskHandle_t zh_encoder = NULL;
|
||||||
static QueueHandle_t _queue_handle = 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;
|
static uint8_t _encoder_counter = 0;
|
||||||
static bool _is_prev_gpio_isr_handler = false;
|
static bool _is_prev_gpio_isr_handler = false;
|
||||||
static zh_encoder_stats_t _stats = {0};
|
static zh_encoder_stats_t _stats = {0};
|
||||||
|
static uint8_t _encoder_number_matrix[10] = {0};
|
||||||
|
|
||||||
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config);
|
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config);
|
||||||
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);
|
||||||
@@ -40,7 +46,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.");
|
||||||
@@ -50,15 +56,31 @@ esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_han
|
|||||||
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. GPIO initialization failed.");
|
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Encoder initialization failed. GPIO initialization failed.");
|
||||||
err = _zh_encoder_resources_init(config);
|
err = _zh_encoder_resources_init(config);
|
||||||
if (_is_prev_gpio_isr_handler == true)
|
if (_is_prev_gpio_isr_handler == true)
|
||||||
|
{
|
||||||
|
if (config->s_gpio_number != GPIO_NUM_MAX)
|
||||||
|
{
|
||||||
|
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); gpio_isr_handler_remove((gpio_num_t)config->s_gpio_number);
|
||||||
|
gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number); gpio_reset_pin((gpio_num_t)config->s_gpio_number), "Encoder initialization failed. Resources initialization failed.");
|
||||||
|
}
|
||||||
|
else
|
||||||
{
|
{
|
||||||
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); gpio_reset_pin((gpio_num_t)config->a_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); gpio_reset_pin((gpio_num_t)config->a_gpio_number);
|
||||||
gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Encoder initialization failed. Resources initialization failed.");
|
gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Encoder initialization failed. Resources initialization failed.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (config->s_gpio_number != GPIO_NUM_MAX)
|
||||||
|
{
|
||||||
|
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); gpio_isr_handler_remove((gpio_num_t)config->s_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); gpio_reset_pin((gpio_num_t)config->s_gpio_number), "Encoder initialization failed. Resources initialization failed.");
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
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); gpio_uninstall_isr_service();
|
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); gpio_uninstall_isr_service();
|
||||||
gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Encoder initialization failed. Resources initialization failed.");
|
gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Encoder initialization failed. Resources initialization failed.");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
err = _zh_encoder_task_init(config);
|
err = _zh_encoder_task_init(config);
|
||||||
if (_is_prev_gpio_isr_handler == true)
|
if (_is_prev_gpio_isr_handler == true)
|
||||||
{
|
{
|
||||||
@@ -72,6 +94,8 @@ esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_han
|
|||||||
}
|
}
|
||||||
handle->a_gpio_number = config->a_gpio_number;
|
handle->a_gpio_number = config->a_gpio_number;
|
||||||
handle->b_gpio_number = config->b_gpio_number;
|
handle->b_gpio_number = config->b_gpio_number;
|
||||||
|
handle->s_gpio_number = config->s_gpio_number;
|
||||||
|
handle->button_status = gpio_get_level((gpio_num_t)config->s_gpio_number);
|
||||||
handle->encoder_number = config->encoder_number;
|
handle->encoder_number = config->encoder_number;
|
||||||
handle->encoder_min_value = config->encoder_min_value;
|
handle->encoder_min_value = config->encoder_min_value;
|
||||||
handle->encoder_max_value = config->encoder_max_value;
|
handle->encoder_max_value = config->encoder_max_value;
|
||||||
@@ -79,6 +103,14 @@ esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_han
|
|||||||
handle->encoder_position = (handle->encoder_min_value + handle->encoder_max_value) / 2;
|
handle->encoder_position = (handle->encoder_min_value + handle->encoder_max_value) / 2;
|
||||||
handle->is_initialized = true;
|
handle->is_initialized = true;
|
||||||
++_encoder_counter;
|
++_encoder_counter;
|
||||||
|
for (uint8_t i = 0; i < sizeof(_encoder_number_matrix); ++i)
|
||||||
|
{
|
||||||
|
if (_encoder_number_matrix[i] == 0)
|
||||||
|
{
|
||||||
|
_encoder_number_matrix[i] = handle->encoder_number;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
ZH_LOGI("Encoder initialization completed successfully.");
|
ZH_LOGI("Encoder initialization completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@@ -90,8 +122,10 @@ esp_err_t zh_encoder_deinit(zh_encoder_handle_t *handle)
|
|||||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder deinitialization failed. Encoder not initialized.");
|
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder deinitialization failed. Encoder not initialized.");
|
||||||
gpio_isr_handler_remove((gpio_num_t)handle->a_gpio_number);
|
gpio_isr_handler_remove((gpio_num_t)handle->a_gpio_number);
|
||||||
gpio_isr_handler_remove((gpio_num_t)handle->b_gpio_number);
|
gpio_isr_handler_remove((gpio_num_t)handle->b_gpio_number);
|
||||||
|
gpio_isr_handler_remove((gpio_num_t)handle->s_gpio_number);
|
||||||
gpio_reset_pin((gpio_num_t)handle->a_gpio_number);
|
gpio_reset_pin((gpio_num_t)handle->a_gpio_number);
|
||||||
gpio_reset_pin((gpio_num_t)handle->b_gpio_number);
|
gpio_reset_pin((gpio_num_t)handle->b_gpio_number);
|
||||||
|
gpio_reset_pin((gpio_num_t)handle->s_gpio_number);
|
||||||
if (_encoder_counter == 1)
|
if (_encoder_counter == 1)
|
||||||
{
|
{
|
||||||
vQueueDelete(_queue_handle);
|
vQueueDelete(_queue_handle);
|
||||||
@@ -103,17 +137,44 @@ esp_err_t zh_encoder_deinit(zh_encoder_handle_t *handle)
|
|||||||
}
|
}
|
||||||
handle->is_initialized = false;
|
handle->is_initialized = false;
|
||||||
--_encoder_counter;
|
--_encoder_counter;
|
||||||
|
for (uint8_t i = 0; i < sizeof(_encoder_number_matrix); ++i)
|
||||||
|
{
|
||||||
|
if (_encoder_number_matrix[i] == handle->encoder_number)
|
||||||
|
{
|
||||||
|
_encoder_number_matrix[i] = 0;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
ZH_LOGI("Encoder deinitialization completed successfully.");
|
ZH_LOGI("Encoder deinitialization completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
esp_err_t zh_encoder_reinit(zh_encoder_handle_t *handle, double min, double max, double step) // -V2008
|
||||||
|
{
|
||||||
|
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)
|
esp_err_t zh_encoder_set(zh_encoder_handle_t *handle, double 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.");
|
||||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder set position failed. Encoder not initialized.");
|
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder set position failed. Encoder not initialized.");
|
||||||
ZH_ERROR_CHECK(position <= handle->encoder_max_value && position >= handle->encoder_min_value, ESP_ERR_INVALID_ARG, NULL, "Encoder set position failed. Invalid argument.");
|
ZH_ERROR_CHECK(position <= handle->encoder_max_value && position >= handle->encoder_min_value, ESP_ERR_INVALID_ARG, NULL, "Encoder set position failed. Invalid argument.");
|
||||||
|
taskENTER_CRITICAL(&_spinlock);
|
||||||
handle->encoder_position = position;
|
handle->encoder_position = position;
|
||||||
|
taskEXIT_CRITICAL(&_spinlock);
|
||||||
ZH_LOGI("Encoder set position completed successfully.");
|
ZH_LOGI("Encoder set position completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@@ -133,7 +194,9 @@ esp_err_t zh_encoder_reset(zh_encoder_handle_t *handle)
|
|||||||
ZH_LOGI("Encoder reset started.");
|
ZH_LOGI("Encoder reset started.");
|
||||||
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder reset failed. Invalid argument.");
|
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder reset failed. Invalid argument.");
|
||||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder reset failed. Encoder not initialized.");
|
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder reset failed. Encoder not initialized.");
|
||||||
|
taskENTER_CRITICAL(&_spinlock);
|
||||||
handle->encoder_position = (handle->encoder_min_value + handle->encoder_max_value) / 2;
|
handle->encoder_position = (handle->encoder_min_value + handle->encoder_max_value) / 2;
|
||||||
|
taskEXIT_CRITICAL(&_spinlock);
|
||||||
ZH_LOGI("Encoder reset completed successfully.");
|
ZH_LOGI("Encoder reset completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@@ -152,23 +215,29 @@ void zh_encoder_reset_stats(void)
|
|||||||
ZH_LOGI("Error statistic reset successfully.");
|
ZH_LOGI("Error statistic reset successfully.");
|
||||||
}
|
}
|
||||||
|
|
||||||
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config)
|
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config) // -V2008
|
||||||
{
|
{
|
||||||
ZH_ERROR_CHECK(config != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid configuration.");
|
ZH_ERROR_CHECK(config != NULL, ESP_ERR_INVALID_ARG, NULL, "Invalid configuration.");
|
||||||
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.");
|
||||||
ZH_ERROR_CHECK(config->encoder_step > 0, ESP_ERR_INVALID_ARG, NULL, "Invalid encoder step.");
|
ZH_ERROR_CHECK(config->encoder_step > 0, ESP_ERR_INVALID_ARG, NULL, "Invalid encoder step.");
|
||||||
|
ZH_ERROR_CHECK(config->encoder_number > 0, ESP_ERR_INVALID_ARG, NULL, "Invalid encoder number.");
|
||||||
|
for (uint8_t i = 0; i < sizeof(_encoder_number_matrix); ++i)
|
||||||
|
{
|
||||||
|
ZH_ERROR_CHECK(config->encoder_number != _encoder_number_matrix[i], ESP_ERR_INVALID_ARG, NULL, "Encoder number already present.");
|
||||||
|
}
|
||||||
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, 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, "Both 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.")
|
||||||
|
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.")
|
||||||
gpio_config_t pin_config = {
|
gpio_config_t pin_config = {
|
||||||
.mode = GPIO_MODE_INPUT,
|
.mode = GPIO_MODE_INPUT,
|
||||||
.pin_bit_mask = (1ULL << config->a_gpio_number) | (1ULL << config->b_gpio_number),
|
.pin_bit_mask = (1ULL << config->a_gpio_number) | (1ULL << config->b_gpio_number) | (1ULL << config->s_gpio_number),
|
||||||
.pull_up_en = GPIO_PULLUP_ENABLE,
|
.pull_up_en = GPIO_PULLUP_ENABLE,
|
||||||
.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);
|
||||||
@@ -182,9 +251,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.");
|
||||||
@@ -193,6 +262,17 @@ 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((gpio_num_t)config->s_gpio_number, _zh_encoder_isr_handler, handle);
|
||||||
|
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);
|
||||||
|
gpio_reset_pin((gpio_num_t)config->a_gpio_number); gpio_reset_pin((gpio_num_t)config->b_gpio_number), "Interrupt initialization failed.");
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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); 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.");
|
||||||
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -220,7 +300,19 @@ static void IRAM_ATTR _zh_encoder_isr_handler(void *arg)
|
|||||||
{
|
{
|
||||||
zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)arg;
|
zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)arg;
|
||||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||||
encoder_handle->encoder_state = _encoder_matrix[encoder_handle->encoder_state & 0x0F][(gpio_get_level(encoder_handle->b_gpio_number) << 1) | gpio_get_level(encoder_handle->a_gpio_number)];
|
uint64_t _current_us = esp_timer_get_time();
|
||||||
|
bool button_flag = false;
|
||||||
|
if (_current_us - _prev_us >= BUTTON_DEBOUNCE_TIME)
|
||||||
|
{
|
||||||
|
bool button_status = gpio_get_level((gpio_num_t)encoder_handle->s_gpio_number);
|
||||||
|
if (encoder_handle->button_status != button_status)
|
||||||
|
{
|
||||||
|
button_flag = true;
|
||||||
|
encoder_handle->button_status = button_status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
_prev_us = _current_us;
|
||||||
|
encoder_handle->encoder_state = _encoder_matrix[encoder_handle->encoder_state & 0x0F][(gpio_get_level((gpio_num_t)encoder_handle->b_gpio_number) << 1) | gpio_get_level((gpio_num_t)encoder_handle->a_gpio_number)];
|
||||||
switch (encoder_handle->encoder_state & 0x30)
|
switch (encoder_handle->encoder_state & 0x30)
|
||||||
{
|
{
|
||||||
case ZH_ENCODER_DIRECTION_CW:
|
case ZH_ENCODER_DIRECTION_CW:
|
||||||
@@ -252,6 +344,13 @@ static void IRAM_ATTR _zh_encoder_isr_handler(void *arg)
|
|||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
|
if (button_flag == true)
|
||||||
|
{
|
||||||
|
if (xQueueSendFromISR(_queue_handle, encoder_handle, &xHigherPriorityTaskWoken) != pdTRUE)
|
||||||
|
{
|
||||||
|
++_stats.queue_overflow_error;
|
||||||
|
}
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
if (xHigherPriorityTaskWoken == pdTRUE)
|
if (xHigherPriorityTaskWoken == pdTRUE)
|
||||||
@@ -268,6 +367,7 @@ static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
|
|||||||
{
|
{
|
||||||
encoder_data.encoder_number = queue.encoder_number;
|
encoder_data.encoder_number = queue.encoder_number;
|
||||||
encoder_data.encoder_position = queue.encoder_position;
|
encoder_data.encoder_position = queue.encoder_position;
|
||||||
|
encoder_data.button_status = queue.button_status;
|
||||||
esp_err_t err = esp_event_post(ZH_ENCODER, 0, &encoder_data, sizeof(zh_encoder_event_on_isr_t), 1000 / portTICK_PERIOD_MS);
|
esp_err_t err = esp_event_post(ZH_ENCODER, 0, &encoder_data, sizeof(zh_encoder_event_on_isr_t), 1000 / portTICK_PERIOD_MS);
|
||||||
if (err != ESP_OK)
|
if (err != ESP_OK)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user