feat!: main structure of handle changed to double pointer
This commit is contained in:
1 parent
911d402e47
commit
e88225d4c6
5 files changed
+410
-239
No files matched your search
@@ -1,20 +1,21 @@
|
|||||||
# ESP32 ESP-IDF component for rotary encoder
|
# ESP32 ESP-IDF component for rotary encoder using PCNT peripheral
|
||||||
|
|
||||||
## Tested on
|
|
||||||
|
|
||||||
1. [ESP32 ESP-IDF v6.0.0](https://docs.espressif.com/projects/esp-idf/en/v6.0/esp32/index.html)
|
|
||||||
|
|
||||||
## SAST Tools
|
## 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.
|
[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 up to 8 encoders on one device.
|
1. Quadrature decoding via PCNT peripheral.
|
||||||
|
2. Configurable min/max/step values with float precision.
|
||||||
|
3. Push-button support with debouncing.
|
||||||
|
4. Multiple simultaneous encoders (up to 8).
|
||||||
|
5. Event-based notification via esp_event (ISR context).
|
||||||
|
6. Error statistics tracking for diagnostics.
|
||||||
|
|
||||||
## Attention
|
## Note
|
||||||
|
|
||||||
1. For correct operation, please enable the following settings in the menuconfig:
|
Enable the following settings in menuconfig:
|
||||||
|
|
||||||
```text
|
```text
|
||||||
GPIO_CTRL_FUNC_IN_IRAM
|
GPIO_CTRL_FUNC_IN_IRAM
|
||||||
@@ -24,9 +25,9 @@ PCNT_ISR_IRAM_SAFE
|
|||||||
|
|
||||||
## 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 component:
|
||||||
|
|
||||||
```text
|
```bash
|
||||||
cd ../your_project/components
|
cd ../your_project/components
|
||||||
git clone https://github.com/aZholtikov/zh_encoder
|
git clone https://github.com/aZholtikov/zh_encoder
|
||||||
```
|
```
|
||||||
@@ -37,16 +38,14 @@ In the application, add the component:
|
|||||||
#include "zh_encoder.h"
|
#include "zh_encoder.h"
|
||||||
```
|
```
|
||||||
|
|
||||||
## Examples
|
## Example
|
||||||
|
|
||||||
One encoder with button on device:
|
|
||||||
|
|
||||||
```c
|
```c
|
||||||
#include "zh_encoder.h"
|
#include "zh_encoder.h"
|
||||||
|
|
||||||
#define ENCODER_NUMBER 0x01
|
#define ENCODER_NUMBER 0x01
|
||||||
|
|
||||||
zh_encoder_handle_t encoder_handle = {0};
|
zh_encoder_handle_t *encoder_handle = NULL;
|
||||||
|
|
||||||
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);
|
||||||
|
|
||||||
@@ -82,6 +81,7 @@ 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)
|
||||||
{
|
{
|
||||||
|
(void)arg;
|
||||||
switch (event_id)
|
switch (event_id)
|
||||||
{
|
{
|
||||||
case ZH_BUTTON_EVENT:
|
case ZH_BUTTON_EVENT:
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
description: ESP32 ESP-IDF component for rotary encoder
|
||||||
|
url: https://github.com/aZholtikov/zh_encoder
|
||||||
|
version: "3.0.0"
|
||||||
|
license: "Apache-2.0"
|
||||||
|
maintainers:
|
||||||
|
- "Alexey Zholtikov <alexey.zholtikov@yandex.ru>"
|
||||||
|
dependencies:
|
||||||
|
idf: ">=5.0"
|
||||||
+131
-97
@@ -1,5 +1,19 @@
|
|||||||
/**
|
/**
|
||||||
* @file zh_encoder.h
|
* @file zh_encoder.h
|
||||||
|
*
|
||||||
|
* @brief Rotary encoder driver for ESP-IDF using PCNT peripheral.
|
||||||
|
*
|
||||||
|
* Provides a FreeRTOS-based driver for rotary encoders with optional
|
||||||
|
* push-button support. Uses ESP-IDF PCNT peripheral for quadrature
|
||||||
|
* decoding and esp_event for asynchronous event notification.
|
||||||
|
*
|
||||||
|
* Key features:
|
||||||
|
* - Quadrature decoding via PCNT peripheral
|
||||||
|
* - Configurable min/max/step values with float precision
|
||||||
|
* - Push-button support with debouncing
|
||||||
|
* - Multiple simultaneous encoders (up to 8)
|
||||||
|
* - Event-based notification (ISR context)
|
||||||
|
* - Error statistics tracking
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
@@ -12,9 +26,6 @@
|
|||||||
#include "freertos/task.h"
|
#include "freertos/task.h"
|
||||||
#include "esp_event.h"
|
#include "esp_event.h"
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Encoder initial default values.
|
|
||||||
*/
|
|
||||||
#define ZH_ENCODER_INIT_CONFIG_DEFAULT() \
|
#define ZH_ENCODER_INIT_CONFIG_DEFAULT() \
|
||||||
{ \
|
{ \
|
||||||
.task_priority = 1, \
|
.task_priority = 1, \
|
||||||
@@ -35,169 +46,192 @@ extern "C"
|
|||||||
{
|
{
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
extern TaskHandle_t zh_encoder; /*!< Encoder Task Handle. */
|
/**
|
||||||
|
* @brief Handle to the shared encoder ISR processing task.
|
||||||
|
*/
|
||||||
|
extern TaskHandle_t zh_encoder;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Structure for initial initialization of encoder.
|
* @brief Opaque encoder handle.
|
||||||
|
*
|
||||||
|
* Opaque pointer to the internal encoder structure.
|
||||||
|
* Used to reference a specific encoder instance across all API functions.
|
||||||
|
*/
|
||||||
|
typedef struct _zh_encoder_handle_t zh_encoder_handle_t;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Configuration structure for encoder initialization.
|
||||||
|
*
|
||||||
|
* Contains all parameters required to configure a rotary encoder,
|
||||||
|
* including GPIO pins, value range, step size, and task settings.
|
||||||
|
*
|
||||||
|
* @note All GPIO numbers must be valid ESP-IDF GPIO numbers or GPIO_NUM_MAX (disabled).
|
||||||
|
* @note encoder_number must be unique across all initialized encoders.
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
float encoder_step; /*!< Encoder step. @note Must be greater than 0. */
|
float encoder_step; /*!< Encoder step. @note Must be greater than 0 */
|
||||||
float 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 */
|
||||||
float 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. */
|
gpio_num_t a_gpio_number; /*!< Encoder A GPIO number */
|
||||||
uint8_t queue_size; /*!< Queue size for task for the encoder processing. @note Minimum value is 1. */
|
gpio_num_t b_gpio_number; /*!< Encoder B GPIO number */
|
||||||
uint8_t a_gpio_number; /*!< Encoder A GPIO number. */
|
gpio_num_t s_gpio_number; /*!< Encoder button GPIO number */
|
||||||
uint8_t b_gpio_number; /*!< Encoder B GPIO number. */
|
uint16_t s_gpio_debounce_time; /*!< Encoder button debounce_time. @note In microseconds */
|
||||||
uint8_t s_gpio_number; /*!< Encoder button GPIO number. */
|
uint16_t stack_size; /*!< Stack size for task for the encoder isr processing processing. @note The minimum size is configMINIMAL_STACK_SIZE */
|
||||||
bool pullup; /*!< Pullup GPIO enable/disable. */
|
uint8_t task_priority; /*!< Task priority for the encoder isr processing. @note Minimum value is 1 */
|
||||||
uint16_t s_gpio_debounce_time; /*!< Encoder button debounce_time. @note In microseconds. */
|
uint8_t queue_size; /*!< Queue size for task for the encoder processing. @note Minimum value is 1 */
|
||||||
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. */
|
bool pullup; /*!< Pullup GPIO enable/disable */
|
||||||
} zh_encoder_init_config_t;
|
} zh_encoder_init_config_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Encoder handle.
|
* @brief Structure for encoder error statistics.
|
||||||
|
*
|
||||||
|
* Tracks runtime errors and task health metrics for diagnostic purposes.
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
bool s_gpio_status; /*!< Encoder button status. */
|
uint32_t event_post_error; /*!< Number of event post error */
|
||||||
bool is_initialized; /*!< Encoder initialization flag. */
|
uint32_t queue_overflow_error; /*!< Number of queue overflow error */
|
||||||
uint8_t encoder_number; /*!< Encoder unique number. */
|
uint32_t min_stack_size; /*!< Minimum free stack size */
|
||||||
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. */
|
|
||||||
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;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Structure for error statistics storage.
|
|
||||||
*/
|
|
||||||
typedef struct
|
|
||||||
{
|
|
||||||
uint32_t event_post_error; /*!< Number of event post error. */
|
|
||||||
uint32_t queue_overflow_error; /*!< Number of queue overflow error. */
|
|
||||||
uint32_t min_stack_size; /*!< Minimum free stack size. */
|
|
||||||
} zh_encoder_stats_t;
|
} zh_encoder_stats_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Enumeration of encoder event ID.
|
* @brief Encoder event identifiers for esp_event base.
|
||||||
*/
|
*/
|
||||||
typedef enum
|
typedef enum
|
||||||
{
|
{
|
||||||
ZH_BUTTON_EVENT, /*!< Button event. */
|
ZH_BUTTON_EVENT, /*!< Encoder push-button interrupt event */
|
||||||
ZH_ENCODER_EVENT /*!< Encoder event. */
|
ZH_ENCODER_EVENT /*!< Encoder rotation interrupt event */
|
||||||
} zh_encoder_event_id_t;
|
} zh_encoder_event_id_t;
|
||||||
|
|
||||||
ESP_EVENT_DECLARE_BASE(ZH_ENCODER);
|
ESP_EVENT_DECLARE_BASE(ZH_ENCODER);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Structure for sending data to the event handler when cause encoder interrupt.
|
* @brief Event data structure for encoder rotation events.
|
||||||
|
*
|
||||||
|
* Posted from ISR context via esp_event_isr_post when encoder position changes.
|
||||||
*
|
*
|
||||||
* @note Should be used with ZH_ENCODER event base and ZH_ENCODER_EVENT event ID.
|
* @note Should be used with ZH_ENCODER event base and ZH_ENCODER_EVENT event ID.
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
float 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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Structure for sending data to the event handler when cause encoder button interrupt.
|
* @brief Event data structure for encoder button events.
|
||||||
|
*
|
||||||
|
* Posted from ISR context via esp_event_isr_post when encoder button state changes.
|
||||||
*
|
*
|
||||||
* @note Should be used with ZH_ENCODER event base and ZH_BUTTON_EVENT event ID.
|
* @note Should be used with ZH_ENCODER event base and ZH_BUTTON_EVENT event ID.
|
||||||
*/
|
*/
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
uint8_t encoder_number; /*!< Encoder unique number. */
|
uint8_t encoder_number; /*!< Encoder unique number */
|
||||||
bool button_status; /*!< Encoder button status. */
|
bool button_status; /*!< Encoder button status */
|
||||||
} zh_encoder_button_event_on_isr_t;
|
} zh_encoder_button_event_on_isr_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Initialize encoder.
|
* @brief Initialize encoder with the provided configuration.
|
||||||
|
*
|
||||||
|
* Creates and configures a rotary encoder using the ESP-IDF PCNT peripheral.
|
||||||
|
* The encoder is initialized at the midpoint of the configured range.
|
||||||
|
*
|
||||||
|
* @note The encoder will be set to the position (encoder_min_value + encoder_max_value)/2.
|
||||||
|
* @note Before initialization, configure zh_encoder_init_config_t with default values.
|
||||||
|
*
|
||||||
|
* @param[in] config Pointer to encoder configuration structure. Can point to a temporary variable (must not be NULL)
|
||||||
|
* @param[out] handle Pointer to store the unique encoder handle (must be NULL)
|
||||||
|
*
|
||||||
|
* @return ESP_OK on success
|
||||||
|
* @return ESP_ERR_INVALID_ARG if config or handle is NULL, or parameters are invalid
|
||||||
|
* @return ESP_ERR_INVALID_STATE if the encoder is already initialized
|
||||||
|
* @return ESP_FAIL if resource allocation or peripheral initialization fails
|
||||||
|
*/
|
||||||
|
esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t **handle);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Deinitialize encoder and release all resources.
|
||||||
|
*
|
||||||
|
* Stops the PCNT peripheral, removes GPIO ISR handlers, and frees allocated memory.
|
||||||
|
* If this is the last encoder, the shared processing task and queue are also deleted.
|
||||||
|
*
|
||||||
|
* @param[in,out] handle Pointer to unique encoder handle (must not be NULL)
|
||||||
|
*
|
||||||
|
* @return ESP_OK on success
|
||||||
|
* @return ESP_ERR_INVALID_ARG if handle is NULL or points to an uninitialized encoder
|
||||||
|
* @return ESP_FAIL if PCNT or GPIO cleanup fails
|
||||||
|
*/
|
||||||
|
esp_err_t zh_encoder_deinit(zh_encoder_handle_t **handle);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Reinitialize encoder by updating min, max, and step values.
|
||||||
|
*
|
||||||
|
* Updates the encoder's value range and step size without reallocating resources.
|
||||||
|
* The encoder position is reset to the midpoint of the new range.
|
||||||
*
|
*
|
||||||
* @note The encoder will be set to the position (encoder_min_value + encoder_max_value)/2.
|
* @note The encoder will be set to the position (encoder_min_value + encoder_max_value)/2.
|
||||||
*
|
*
|
||||||
* @param[in] config Pointer to encoder initialized configuration structure. Can point to a temporary variable.
|
* @param[in,out] handle Pointer to unique encoder handle (must not be NULL)
|
||||||
* @param[out] handle Pointer to unique encoder handle.
|
* @param[in] min Encoder minimum value. Must be less than max
|
||||||
|
* @param[in] max Encoder maximum value. Must be greater than min
|
||||||
|
* @param[in] step Encoder step size. Must be greater than 0
|
||||||
*
|
*
|
||||||
* @note Before initialize the encoder recommend initialize zh_encoder_init_config_t structure with default values.
|
* @return ESP_OK on success
|
||||||
*
|
* @return ESP_ERR_INVALID_ARG if handle is NULL, or parameters are invalid
|
||||||
* @code zh_encoder_init_config_t config = ZH_ENCODER_INIT_CONFIG_DEFAULT() @endcode
|
|
||||||
*
|
|
||||||
* @return ESP_OK if success or an error code otherwise.
|
|
||||||
*/
|
*/
|
||||||
esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle);
|
esp_err_t zh_encoder_reinit(zh_encoder_handle_t **handle, float min, float max, float step);
|
||||||
|
|
||||||
/**
|
|
||||||
* @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);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @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, float min, float max, float step);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set encoder position.
|
* @brief Set encoder position.
|
||||||
*
|
*
|
||||||
* @param[in, out] handle Pointer to unique encoder handle.
|
* @param[in,out] handle Pointer to unique encoder handle (must not be NULL)
|
||||||
* @param[in] position Encoder position (must be between encoder_min_value and encoder_max_value).
|
* @param[in] position Encoder position (must be between encoder_min_value and encoder_max_value)
|
||||||
*
|
*
|
||||||
* @return ESP_OK if success or an error code otherwise.
|
* @return ESP_OK on success
|
||||||
|
* @return ESP_ERR_INVALID_ARG if handle is NULL, or parameters are invalid
|
||||||
*/
|
*/
|
||||||
esp_err_t zh_encoder_set(zh_encoder_handle_t *handle, float position);
|
esp_err_t zh_encoder_set(zh_encoder_handle_t **handle, float position);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get encoder position.
|
* @brief Get encoder position.
|
||||||
*
|
*
|
||||||
* @param[in] handle Pointer to unique encoder handle.
|
* @param[in] handle Pointer to unique encoder handle (must not be NULL)
|
||||||
* @param[out] position Encoder position.
|
* @param[out] position Pointer to store the current encoder position
|
||||||
*
|
*
|
||||||
* @return ESP_OK if success or an error code otherwise.
|
* @return ESP_OK on success
|
||||||
|
* @return ESP_ERR_INVALID_ARG if handle or position is NULL
|
||||||
*/
|
*/
|
||||||
esp_err_t zh_encoder_get(const zh_encoder_handle_t *handle, float *position);
|
esp_err_t zh_encoder_get(zh_encoder_handle_t **handle, float *position);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Reset encoder position.
|
* @brief Reset encoder position to the midpoint of the configured range.
|
||||||
*
|
*
|
||||||
* @note The encoder will be set to the position (encoder_min_value + encoder_max_value)/2.
|
* @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,out] handle Pointer to unique encoder handle (must not be NULL)
|
||||||
*
|
*
|
||||||
* @return ESP_OK if success or an error code otherwise.
|
* @return ESP_OK on success
|
||||||
|
* @return ESP_ERR_INVALID_ARG if handle is NULL
|
||||||
*/
|
*/
|
||||||
esp_err_t zh_encoder_reset(zh_encoder_handle_t *handle);
|
esp_err_t zh_encoder_reset(zh_encoder_handle_t **handle);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Get error statistics.
|
* @brief Get a pointer to the encoder error statistics.
|
||||||
*
|
*
|
||||||
* @return Pointer to the statistics structure.
|
* Returns a read-only pointer to the global statistics structure
|
||||||
|
* containing error counts and task health metrics.
|
||||||
|
*
|
||||||
|
* @return Pointer to the statistics structure. Valid until zh_encoder_reset_stats() is called
|
||||||
*/
|
*/
|
||||||
const zh_encoder_stats_t *zh_encoder_get_stats(void);
|
const zh_encoder_stats_t *zh_encoder_get_stats(void);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Reset error statistics.
|
* @brief Reset all error statistics to zero.
|
||||||
|
*
|
||||||
|
* Clears error counters and stack watermarks.
|
||||||
*/
|
*/
|
||||||
void zh_encoder_reset_stats(void);
|
void zh_encoder_reset_stats(void);
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -1 +1 @@
|
|||||||
2.4.1
|
3.0.0
|
||||||
+255
-126
@@ -13,66 +13,199 @@
|
|||||||
return err; \
|
return err; \
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#define ZH_ERROR_CHECK_CONT(cond, cleanup, msg, ...) \
|
||||||
|
if (!(cond)) \
|
||||||
|
{ \
|
||||||
|
ZH_LOGE(msg, ESP_FAIL, ##__VA_ARGS__); \
|
||||||
|
cleanup; \
|
||||||
|
continue; \
|
||||||
|
}
|
||||||
|
|
||||||
#define ZH_ENCODER_DIRECTION_CW 1
|
#define ZH_ENCODER_DIRECTION_CW 1
|
||||||
#define ZH_ENCODER_DIRECTION_CCW -1
|
#define ZH_ENCODER_DIRECTION_CCW -1
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Internal encoder handle structure.
|
||||||
|
*
|
||||||
|
* Stores all runtime state for a single encoder instance,
|
||||||
|
* including GPIO configuration, PCNT handles, and position tracking.
|
||||||
|
*/
|
||||||
|
struct _zh_encoder_handle_t
|
||||||
|
{
|
||||||
|
bool s_gpio_status; /*!< Current encoder button status */
|
||||||
|
bool is_initialized; /*!< Encoder initialization flag */
|
||||||
|
uint8_t encoder_number; /*!< Unique encoder number */
|
||||||
|
gpio_num_t s_gpio_number; /*!< Encoder button GPIO number */
|
||||||
|
uint16_t s_gpio_debounce_time; /*!< Encoder button debounce time in microseconds */
|
||||||
|
uint64_t s_gpio_prev_time; /*!< Timestamp of the previous button interrupt */
|
||||||
|
float encoder_step; /*!< Encoder step size */
|
||||||
|
float encoder_position; /*!< Current encoder position */
|
||||||
|
float encoder_min_value; /*!< Encoder minimum value */
|
||||||
|
float encoder_max_value; /*!< Encoder maximum value */
|
||||||
|
pcnt_unit_handle_t pcnt_unit_handle; /*!< PCNT unit handle for quadrature decoding */
|
||||||
|
pcnt_channel_handle_t pcnt_channel_a_handle; /*!< PCNT channel A handle (A phase) */
|
||||||
|
pcnt_channel_handle_t pcnt_channel_b_handle; /*!< PCNT channel B handle (B phase) */
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Queue message structure for encoder ISR processing.
|
||||||
|
*
|
||||||
|
* Sent from ISR context to the processing task when encoder position changes.
|
||||||
|
*/
|
||||||
typedef struct
|
typedef struct
|
||||||
{
|
{
|
||||||
int watch_point_value;
|
int watch_point_value; /*!< PCNT watch point value (CW or CCW direction) */
|
||||||
zh_encoder_handle_t *handle;
|
zh_encoder_handle_t *handle; /*!< Pointer to the encoder handle */
|
||||||
} zh_encoder_queue_t;
|
} zh_encoder_queue_t;
|
||||||
|
|
||||||
TaskHandle_t zh_encoder = NULL;
|
TaskHandle_t zh_encoder = NULL; /*!< Handle to the shared encoder ISR processing task */
|
||||||
static QueueHandle_t _queue_handle = NULL;
|
static QueueHandle_t _queue_handle = NULL; /*!< Queue for passing encoder events from ISR to processing task */
|
||||||
static portMUX_TYPE _spinlock = portMUX_INITIALIZER_UNLOCKED;
|
static portMUX_TYPE _spinlock = portMUX_INITIALIZER_UNLOCKED; /*!< Spinlock for protecting encoder position access */
|
||||||
|
|
||||||
volatile static uint8_t _encoder_counter = 0;
|
volatile static uint8_t _encoder_counter = 0; /*!< Total number of initialized encoders */
|
||||||
static zh_encoder_stats_t _stats = {0};
|
static zh_encoder_stats_t _stats = {0}; /*!< Global error statistics */
|
||||||
volatile static uint8_t _encoder_number_matrix[8] = {0};
|
volatile static uint8_t _encoder_number_matrix[8] = {0}; /*!< Matrix tracking used encoder numbers for uniqueness validation */
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Validate encoder initialization configuration.
|
||||||
|
*
|
||||||
|
* Checks all configuration parameters for validity and initializes
|
||||||
|
* the encoder handle with configuration values.
|
||||||
|
*
|
||||||
|
* @param config Pointer to encoder configuration structure
|
||||||
|
* @param handle Pointer to encoder handle to initialize
|
||||||
|
*
|
||||||
|
* @return ESP_OK on success
|
||||||
|
* @return ESP_ERR_INVALID_ARG if any configuration parameter is invalid
|
||||||
|
*/
|
||||||
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle);
|
static esp_err_t _zh_encoder_validate_config(const zh_encoder_init_config_t *config, zh_encoder_handle_t *handle);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initialize PCNT peripheral for quadrature decoding.
|
||||||
|
*
|
||||||
|
* Configures PCNT unit with two channels (A and B phases),
|
||||||
|
* sets up glitch filter, edge/level actions for CW/CCW counting,
|
||||||
|
* registers watch points and event callbacks.
|
||||||
|
*
|
||||||
|
* @param config Pointer to encoder configuration structure
|
||||||
|
* @param handle Pointer to encoder handle to populate with PCNT handles
|
||||||
|
*
|
||||||
|
* @return ESP_OK on success
|
||||||
|
* @return ESP_ERR_INVALID_ARG if GPIO numbers are invalid or identical
|
||||||
|
* @return ESP_FAIL if PCNT peripheral initialization fails
|
||||||
|
*/
|
||||||
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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initialize GPIO pin for encoder button (if configured).
|
||||||
|
*
|
||||||
|
* Configures the button GPIO as input with interrupt on any edge.
|
||||||
|
* Installs ISR service if not already installed and registers the button ISR handler.
|
||||||
|
*
|
||||||
|
* @param config Pointer to encoder configuration structure
|
||||||
|
* @param handle Pointer to encoder handle to populate with GPIO settings
|
||||||
|
*
|
||||||
|
* @return ESP_OK on success
|
||||||
|
* @return ESP_ERR_INVALID_ARG if GPIO numbers conflict with encoder pins
|
||||||
|
* @return ESP_FAIL if GPIO or ISR service initialization fails
|
||||||
|
*/
|
||||||
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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initialize shared resources (queue) for the first encoder.
|
||||||
|
*
|
||||||
|
* Creates the FreeRTOS queue used for passing encoder events
|
||||||
|
* from ISR context to the processing task. Only executed once
|
||||||
|
* when the first encoder is initialized.
|
||||||
|
*
|
||||||
|
* @param config Pointer to encoder configuration structure
|
||||||
|
*
|
||||||
|
* @return ESP_OK on success
|
||||||
|
* @return ESP_FAIL if queue creation fails
|
||||||
|
*/
|
||||||
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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Initialize the shared ISR processing task for the first encoder.
|
||||||
|
*
|
||||||
|
* Creates the FreeRTOS task that processes encoder events from the queue.
|
||||||
|
* Only executed once when the first encoder is initialized.
|
||||||
|
*
|
||||||
|
* @param config Pointer to encoder configuration structure
|
||||||
|
*
|
||||||
|
* @return ESP_OK on success
|
||||||
|
* @return ESP_FAIL if task creation fails
|
||||||
|
*/
|
||||||
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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief PCNT event callback invoked when watch point is reached.
|
||||||
|
*
|
||||||
|
* Clears the PCNT counter and sends the watch point value
|
||||||
|
* to the processing task queue. Executed in ISR context.
|
||||||
|
*
|
||||||
|
* @param unit PCNT unit handle that triggered the event
|
||||||
|
* @param edata Pointer to PCNT watch event data
|
||||||
|
* @param user_ctx Pointer to encoder 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);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief FreeRTOS task for processing encoder events from the queue.
|
||||||
|
*
|
||||||
|
* Receives queue messages from the ISR handler, updates encoder position
|
||||||
|
* based on CW/CCW direction, and posts esp_event notifications
|
||||||
|
* when position changes. Runs indefinitely until the queue is deleted.
|
||||||
|
*
|
||||||
|
* @param pvParameter Task parameter (unused)
|
||||||
|
*/
|
||||||
static void _zh_encoder_isr_processing_task(void *pvParameter);
|
static void _zh_encoder_isr_processing_task(void *pvParameter);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief GPIO ISR handler for encoder button press/release.
|
||||||
|
*
|
||||||
|
* Implements debouncing by checking the time since the last interrupt.
|
||||||
|
* Posts a button event via esp_event_isr_post when button state changes.
|
||||||
|
* Executed in ISR context.
|
||||||
|
*
|
||||||
|
* @param arg Pointer to encoder handle
|
||||||
|
*/
|
||||||
static void _zh_encoder_button_isr_handler(void *arg);
|
static void _zh_encoder_button_isr_handler(void *arg);
|
||||||
|
|
||||||
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) // -V2008
|
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(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(*handle == NULL, 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.");
|
*handle = heap_caps_calloc(1, sizeof(zh_encoder_handle_t), MALLOC_CAP_8BIT);
|
||||||
ZH_ERROR_CHECK(_zh_encoder_validate_config(config, handle) == ESP_OK, ESP_FAIL, NULL, "Encoder initialization failed. Initial configuration check failed.");
|
ZH_ERROR_CHECK(_encoder_counter < sizeof(_encoder_number_matrix), ESP_ERR_INVALID_ARG, heap_caps_free(*handle); *handle = NULL, "Encoder initialization failed. Maximum quantity reached.");
|
||||||
ZH_ERROR_CHECK(_zh_encoder_resources_init(config) == ESP_OK, ESP_FAIL, NULL, "Encoder initialization failed. Resources initialization failed.");
|
ZH_ERROR_CHECK(_zh_encoder_validate_config(config, *handle) == ESP_OK, ESP_FAIL, heap_caps_free(*handle); *handle = NULL, "Encoder initialization failed. Initial configuration check failed.");
|
||||||
|
ZH_ERROR_CHECK(_zh_encoder_resources_init(config) == ESP_OK, ESP_FAIL, heap_caps_free(*handle); *handle = NULL, "Encoder initialization failed. Resources initialization failed.");
|
||||||
|
// clang-format off
|
||||||
ZH_ERROR_CHECK(_zh_encoder_task_init(config) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(_zh_encoder_task_init(config) == ESP_OK, ESP_FAIL,
|
||||||
vQueueDelete(_queue_handle), "Encoder initialization failed. Processing task initialization failed.");
|
vQueueDelete(_queue_handle); _queue_handle = NULL; heap_caps_free(*handle); *handle = NULL, "Encoder initialization failed. Processing task initialization failed.");
|
||||||
ZH_ERROR_CHECK(_zh_encoder_pcnt_init(config, handle) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(_zh_encoder_pcnt_init(config, *handle) == ESP_OK, ESP_FAIL,
|
||||||
vQueueDelete(_queue_handle);
|
vQueueDelete(_queue_handle); _queue_handle = NULL; heap_caps_free(*handle); *handle = NULL; vTaskDelete(zh_encoder); zh_encoder = NULL, "Encoder initialization failed. PCNT initialization failed.");
|
||||||
vTaskDelete(zh_encoder), "Encoder initialization failed. PCNT initialization failed.");
|
ZH_ERROR_CHECK(_zh_encoder_gpio_init(config, *handle) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(_zh_encoder_gpio_init(config, handle) == ESP_OK, ESP_FAIL,
|
{ZH_ERROR_CHECK(pcnt_unit_stop((*handle)->pcnt_unit_handle) == ESP_OK, ESP_FAIL, heap_caps_free(*handle); *handle = NULL, "PCNT unit stop fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_unit_stop(handle->pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT unit stop fail.");
|
{ZH_ERROR_CHECK(pcnt_unit_disable((*handle)->pcnt_unit_handle) == ESP_OK, ESP_FAIL, heap_caps_free(*handle); *handle = NULL, "PCNT unit disable fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_unit_disable(handle->pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT unit disable fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel((*handle)->pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, heap_caps_free(*handle); *handle = NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(handle->pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel((*handle)->pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, heap_caps_free(*handle); *handle = NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(handle->pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_unit((*handle)->pcnt_unit_handle) == ESP_OK, ESP_FAIL, heap_caps_free(*handle); *handle = NULL, "PCNT delete unit fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(handle->pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail.");
|
vQueueDelete(_queue_handle); _queue_handle = NULL; heap_caps_free(*handle); *handle = NULL; vTaskDelete(zh_encoder); zh_encoder = NULL, "Encoder initialization failed. GPIO initialization failed.");
|
||||||
vQueueDelete(_queue_handle);
|
// clang-format on
|
||||||
vTaskDelete(zh_encoder), "Encoder initialization failed. GPIO initialization failed.");
|
|
||||||
if (_stats.min_stack_size == 0)
|
if (_stats.min_stack_size == 0)
|
||||||
{
|
{
|
||||||
_stats.min_stack_size = config->stack_size;
|
_stats.min_stack_size = config->stack_size;
|
||||||
}
|
}
|
||||||
handle->is_initialized = true;
|
|
||||||
++_encoder_counter;
|
++_encoder_counter;
|
||||||
for (uint8_t i = 0; i < sizeof(_encoder_number_matrix); ++i)
|
for (uint8_t i = 0; i < sizeof(_encoder_number_matrix); ++i)
|
||||||
{
|
{
|
||||||
if (_encoder_number_matrix[i] == 0)
|
if (_encoder_number_matrix[i] == 0)
|
||||||
{
|
{
|
||||||
_encoder_number_matrix[i] = handle->encoder_number;
|
_encoder_number_matrix[i] = (*handle)->encoder_number;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -80,89 +213,87 @@ esp_err_t zh_encoder_init(const zh_encoder_init_config_t *config, zh_encoder_han
|
|||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t zh_encoder_deinit(zh_encoder_handle_t *handle) // -V2008
|
esp_err_t zh_encoder_deinit(zh_encoder_handle_t **handle) // -V2008
|
||||||
{
|
{
|
||||||
ZH_LOGI("Encoder deinitialization started.");
|
ZH_LOGI("Encoder deinitialization started.");
|
||||||
ZH_ERROR_CHECK(handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder deinitialization failed. Invalid argument.");
|
ZH_ERROR_CHECK(handle != NULL && *handle != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder deinitialization failed. Invalid argument.");
|
||||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder deinitialization failed. Encoder not initialized.");
|
ZH_ERROR_CHECK(pcnt_unit_stop((*handle)->pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. PCNT unit stop fail.");
|
||||||
ZH_ERROR_CHECK(pcnt_unit_stop(handle->pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. PCNT unit stop fail.");
|
ZH_ERROR_CHECK(pcnt_unit_disable((*handle)->pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. PCNT unit disable fail.");
|
||||||
ZH_ERROR_CHECK(pcnt_unit_disable(handle->pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. PCNT unit disable fail.");
|
ZH_ERROR_CHECK(pcnt_unit_remove_watch_point((*handle)->pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. PCNT unit remove watch point fail.");
|
||||||
ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(handle->pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. PCNT unit remove watch point fail.");
|
ZH_ERROR_CHECK(pcnt_unit_remove_watch_point((*handle)->pcnt_unit_handle, ZH_ENCODER_DIRECTION_CCW) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. PCNT unit remove watch point fail.");
|
||||||
ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(handle->pcnt_unit_handle, ZH_ENCODER_DIRECTION_CCW) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. PCNT unit remove watch point fail.");
|
ZH_ERROR_CHECK(pcnt_del_channel((*handle)->pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. PCNT delete channel fail.");
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(handle->pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. PCNT delete channel fail.");
|
ZH_ERROR_CHECK(pcnt_del_channel((*handle)->pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. PCNT delete channel fail.");
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(handle->pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. PCNT delete channel fail.");
|
ZH_ERROR_CHECK(pcnt_del_unit((*handle)->pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. PCNT delete unit fail.");
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(handle->pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. PCNT delete unit fail.");
|
if ((*handle)->s_gpio_number != GPIO_NUM_MAX)
|
||||||
if (handle->s_gpio_number != GPIO_NUM_MAX)
|
|
||||||
{
|
{
|
||||||
ZH_ERROR_CHECK(gpio_isr_handler_remove((gpio_num_t)handle->s_gpio_number) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. Remove GPIO isr handler failed.");
|
ZH_ERROR_CHECK(gpio_isr_handler_remove((*handle)->s_gpio_number) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. Remove GPIO isr handler failed.");
|
||||||
ZH_ERROR_CHECK(gpio_reset_pin((gpio_num_t)handle->s_gpio_number) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. Reset GPIO failed.");
|
ZH_ERROR_CHECK(gpio_reset_pin((*handle)->s_gpio_number) == ESP_OK, ESP_FAIL, NULL, "Encoder deinitialization failed. Reset GPIO failed.");
|
||||||
}
|
}
|
||||||
if (_encoder_counter == 1)
|
if (_encoder_counter == 1)
|
||||||
{
|
{
|
||||||
vQueueDelete(_queue_handle);
|
vQueueDelete(_queue_handle);
|
||||||
|
_queue_handle = NULL;
|
||||||
vTaskDelete(zh_encoder);
|
vTaskDelete(zh_encoder);
|
||||||
|
zh_encoder = NULL;
|
||||||
}
|
}
|
||||||
handle->is_initialized = false;
|
|
||||||
--_encoder_counter;
|
--_encoder_counter;
|
||||||
for (uint8_t i = 0; i < sizeof(_encoder_number_matrix); ++i)
|
for (uint8_t i = 0; i < sizeof(_encoder_number_matrix); ++i)
|
||||||
{
|
{
|
||||||
if (_encoder_number_matrix[i] == handle->encoder_number)
|
if (_encoder_number_matrix[i] == (*handle)->encoder_number)
|
||||||
{
|
{
|
||||||
_encoder_number_matrix[i] = 0;
|
_encoder_number_matrix[i] = 0;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
heap_caps_free(*handle);
|
||||||
|
*handle = NULL;
|
||||||
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, float min, float max, float 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 && *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(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.");
|
ZH_ERROR_CHECK(step > 0, ESP_ERR_INVALID_ARG, NULL, "Encoder reinitialization failed. Invalid encoder step.");
|
||||||
taskENTER_CRITICAL(&_spinlock);
|
taskENTER_CRITICAL(&_spinlock);
|
||||||
handle->encoder_min_value = min;
|
(*handle)->encoder_min_value = min;
|
||||||
handle->encoder_max_value = max;
|
(*handle)->encoder_max_value = max;
|
||||||
handle->encoder_step = step;
|
(*handle)->encoder_step = step;
|
||||||
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);
|
taskEXIT_CRITICAL(&_spinlock);
|
||||||
ZH_LOGI("Encoder reinitialization completed successfully.");
|
ZH_LOGI("Encoder reinitialization completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t zh_encoder_set(zh_encoder_handle_t *handle, float 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 && *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(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);
|
taskENTER_CRITICAL(&_spinlock);
|
||||||
handle->encoder_position = position;
|
(*handle)->encoder_position = position;
|
||||||
taskEXIT_CRITICAL(&_spinlock);
|
taskEXIT_CRITICAL(&_spinlock);
|
||||||
ZH_LOGI("Encoder set position completed successfully.");
|
ZH_LOGI("Encoder set position completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t zh_encoder_get(const zh_encoder_handle_t *handle, float *position)
|
esp_err_t zh_encoder_get(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 && *handle != NULL && position != NULL, ESP_ERR_INVALID_ARG, NULL, "Encoder get position failed. Invalid argument.");
|
||||||
ZH_ERROR_CHECK(handle->is_initialized == true, ESP_FAIL, NULL, "Encoder get position failed. Encoder not initialized.");
|
*position = (*handle)->encoder_position;
|
||||||
*position = handle->encoder_position;
|
|
||||||
ZH_LOGI("Encoder get position completed successfully.");
|
ZH_LOGI("Encoder get position completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
|
|
||||||
esp_err_t zh_encoder_reset(zh_encoder_handle_t *handle)
|
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 && *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.");
|
|
||||||
taskENTER_CRITICAL(&_spinlock);
|
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);
|
taskEXIT_CRITICAL(&_spinlock);
|
||||||
ZH_LOGI("Encoder reset completed successfully.");
|
ZH_LOGI("Encoder reset completed successfully.");
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
@@ -215,80 +346,80 @@ static esp_err_t _zh_encoder_pcnt_init(const zh_encoder_init_config_t *config, z
|
|||||||
.max_glitch_ns = 1000,
|
.max_glitch_ns = 1000,
|
||||||
};
|
};
|
||||||
ZH_ERROR_CHECK(pcnt_unit_set_glitch_filter(pcnt_unit_handle, &pcnt_glitch_filter_config) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(pcnt_unit_set_glitch_filter(pcnt_unit_handle, &pcnt_glitch_filter_config) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail."), "PCNT initialization failed.");
|
{ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail.")}, "PCNT initialization failed.");
|
||||||
pcnt_chan_config_t pcnt_chan_a_config = {
|
pcnt_chan_config_t pcnt_chan_a_config = {
|
||||||
.edge_gpio_num = config->a_gpio_number,
|
.edge_gpio_num = config->a_gpio_number,
|
||||||
.level_gpio_num = config->b_gpio_number,
|
.level_gpio_num = config->b_gpio_number,
|
||||||
};
|
};
|
||||||
pcnt_channel_handle_t pcnt_channel_a_handle = NULL;
|
pcnt_channel_handle_t pcnt_channel_a_handle = NULL;
|
||||||
ZH_ERROR_CHECK(pcnt_new_channel(pcnt_unit_handle, &pcnt_chan_a_config, &pcnt_channel_a_handle) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(pcnt_new_channel(pcnt_unit_handle, &pcnt_chan_a_config, &pcnt_channel_a_handle) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail."), "PCNT initialization failed.");
|
{ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail.")}, "PCNT initialization failed.");
|
||||||
pcnt_chan_config_t pcnt_chan_b_config = {
|
pcnt_chan_config_t pcnt_chan_b_config = {
|
||||||
.edge_gpio_num = config->b_gpio_number,
|
.edge_gpio_num = config->b_gpio_number,
|
||||||
.level_gpio_num = config->a_gpio_number,
|
.level_gpio_num = config->a_gpio_number,
|
||||||
};
|
};
|
||||||
pcnt_channel_handle_t pcnt_channel_b_handle = NULL;
|
pcnt_channel_handle_t pcnt_channel_b_handle = NULL;
|
||||||
ZH_ERROR_CHECK(pcnt_new_channel(pcnt_unit_handle, &pcnt_chan_b_config, &pcnt_channel_b_handle) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(pcnt_new_channel(pcnt_unit_handle, &pcnt_chan_b_config, &pcnt_channel_b_handle) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail."), "PCNT initialization failed.");
|
{ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail.")}, "PCNT initialization failed.");
|
||||||
ZH_ERROR_CHECK(pcnt_channel_set_edge_action(pcnt_channel_a_handle, PCNT_CHANNEL_EDGE_ACTION_DECREASE, PCNT_CHANNEL_EDGE_ACTION_HOLD) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(pcnt_channel_set_edge_action(pcnt_channel_a_handle, PCNT_CHANNEL_EDGE_ACTION_DECREASE, PCNT_CHANNEL_EDGE_ACTION_HOLD) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail."), "PCNT initialization failed.");
|
{ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail.")}, "PCNT initialization failed.");
|
||||||
ZH_ERROR_CHECK(pcnt_channel_set_level_action(pcnt_channel_a_handle, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_HOLD) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(pcnt_channel_set_level_action(pcnt_channel_a_handle, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_HOLD) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail."), "PCNT initialization failed.");
|
{ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail.")}, "PCNT initialization failed.");
|
||||||
ZH_ERROR_CHECK(pcnt_channel_set_edge_action(pcnt_channel_b_handle, PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_HOLD) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(pcnt_channel_set_edge_action(pcnt_channel_b_handle, PCNT_CHANNEL_EDGE_ACTION_INCREASE, PCNT_CHANNEL_EDGE_ACTION_HOLD) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail."), "PCNT initialization failed.");
|
{ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail.")}, "PCNT initialization failed.");
|
||||||
ZH_ERROR_CHECK(pcnt_channel_set_level_action(pcnt_channel_b_handle, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_HOLD) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(pcnt_channel_set_level_action(pcnt_channel_b_handle, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_HOLD) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail."), "PCNT initialization failed.");
|
{ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail.")}, "PCNT initialization failed.");
|
||||||
ZH_ERROR_CHECK(pcnt_unit_add_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(pcnt_unit_add_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail."), "PCNT initialization failed.");
|
{ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail.")}, "PCNT initialization failed.");
|
||||||
ZH_ERROR_CHECK(pcnt_unit_add_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CCW) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(pcnt_unit_add_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CCW) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.");
|
{ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail."), "PCNT initialization failed.");
|
{ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail.")}, "PCNT initialization failed.");
|
||||||
pcnt_event_callbacks_t cbs = {
|
pcnt_event_callbacks_t cbs = {
|
||||||
.on_reach = _zh_encoder_isr_handler,
|
.on_reach = _zh_encoder_isr_handler,
|
||||||
};
|
};
|
||||||
ZH_ERROR_CHECK(pcnt_unit_register_event_callbacks(pcnt_unit_handle, &cbs, handle) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(pcnt_unit_register_event_callbacks(pcnt_unit_handle, &cbs, handle) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.");
|
{ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CCW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.");
|
{ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CCW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail."), "PCNT initialization failed.");
|
{ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail.")}, "PCNT initialization failed.");
|
||||||
ZH_ERROR_CHECK(pcnt_unit_enable(pcnt_unit_handle) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(pcnt_unit_enable(pcnt_unit_handle) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.");
|
{ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CCW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.");
|
{ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CCW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail."), "PCNT initialization failed.");
|
{ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail.")}, "PCNT initialization failed.");
|
||||||
ZH_ERROR_CHECK(pcnt_unit_clear_count(pcnt_unit_handle) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(pcnt_unit_clear_count(pcnt_unit_handle) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(pcnt_unit_disable(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT unit disable fail.");
|
{ZH_ERROR_CHECK(pcnt_unit_disable(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT unit disable fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.");
|
{ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CCW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.");
|
{ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CCW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail."), "PCNT initialization failed.");
|
{ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail.")}, "PCNT initialization failed.");
|
||||||
ZH_ERROR_CHECK(pcnt_unit_start(pcnt_unit_handle) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(pcnt_unit_start(pcnt_unit_handle) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(pcnt_unit_disable(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT unit disable fail.");
|
{ZH_ERROR_CHECK(pcnt_unit_disable(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT unit disable fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.");
|
{ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CCW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.");
|
{ZH_ERROR_CHECK(pcnt_unit_remove_watch_point(pcnt_unit_handle, ZH_ENCODER_DIRECTION_CCW) == ESP_OK, ESP_FAIL, NULL, "PCNT unit remove watch point fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_a_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.");
|
{ZH_ERROR_CHECK(pcnt_del_channel(pcnt_channel_b_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete channel fail.")};
|
||||||
ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail."), "PCNT initialization failed.");
|
{ZH_ERROR_CHECK(pcnt_del_unit(pcnt_unit_handle) == ESP_OK, ESP_FAIL, NULL, "PCNT delete unit fail.")}, "PCNT initialization failed.");
|
||||||
if (config->pullup == false)
|
if (config->pullup == false)
|
||||||
{
|
{
|
||||||
ZH_ERROR_CHECK(gpio_pullup_dis((gpio_num_t)config->a_gpio_number) == ESP_OK, ESP_FAIL, NULL, "GPIO pullup disable fail.");
|
ZH_ERROR_CHECK(gpio_pullup_dis(config->a_gpio_number) == ESP_OK, ESP_FAIL, NULL, "GPIO pullup disable fail.");
|
||||||
ZH_ERROR_CHECK(gpio_pullup_dis((gpio_num_t)config->b_gpio_number) == ESP_OK, ESP_FAIL, NULL, "GPIO pullup disable fail.");
|
ZH_ERROR_CHECK(gpio_pullup_dis(config->b_gpio_number) == ESP_OK, ESP_FAIL, NULL, "GPIO pullup disable fail.");
|
||||||
}
|
}
|
||||||
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;
|
||||||
@@ -310,12 +441,12 @@ static esp_err_t _zh_encoder_gpio_init(const zh_encoder_init_config_t *config, z
|
|||||||
ZH_ERROR_CHECK(gpio_config(&pin_config) == ESP_OK, ESP_FAIL, NULL, "GPIO initialization failed.");
|
ZH_ERROR_CHECK(gpio_config(&pin_config) == ESP_OK, ESP_FAIL, NULL, "GPIO initialization failed.");
|
||||||
esp_err_t err = gpio_install_isr_service(ESP_INTR_FLAG_LOWMED);
|
esp_err_t err = gpio_install_isr_service(ESP_INTR_FLAG_LOWMED);
|
||||||
ZH_ERROR_CHECK(err == ESP_OK || err == ESP_ERR_INVALID_STATE, ESP_FAIL,
|
ZH_ERROR_CHECK(err == ESP_OK || err == ESP_ERR_INVALID_STATE, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(gpio_reset_pin((gpio_num_t)config->s_gpio_number) == ESP_OK, ESP_FAIL, NULL, "Reset GPIO failed."), "Failed install isr service.");
|
{ZH_ERROR_CHECK(gpio_reset_pin(config->s_gpio_number) == ESP_OK, ESP_FAIL, NULL, "Reset GPIO failed.")}, "Failed install isr service.");
|
||||||
ZH_ERROR_CHECK(gpio_isr_handler_add((gpio_num_t)config->s_gpio_number, _zh_encoder_button_isr_handler, handle) == ESP_OK, ESP_FAIL,
|
ZH_ERROR_CHECK(gpio_isr_handler_add(config->s_gpio_number, _zh_encoder_button_isr_handler, handle) == ESP_OK, ESP_FAIL,
|
||||||
ZH_ERROR_CHECK(gpio_reset_pin((gpio_num_t)config->s_gpio_number) == ESP_OK, ESP_FAIL, NULL, "Reset GPIO failed."), "Interrupt initialization failed.");
|
{ZH_ERROR_CHECK(gpio_reset_pin(config->s_gpio_number) == ESP_OK, ESP_FAIL, NULL, "Reset GPIO failed.")}, "Interrupt initialization failed.");
|
||||||
handle->s_gpio_number = config->s_gpio_number;
|
handle->s_gpio_number = config->s_gpio_number;
|
||||||
handle->s_gpio_debounce_time = config->s_gpio_debounce_time;
|
handle->s_gpio_debounce_time = config->s_gpio_debounce_time;
|
||||||
handle->s_gpio_status = gpio_get_level((gpio_num_t)config->s_gpio_number);
|
handle->s_gpio_status = gpio_get_level(config->s_gpio_number);
|
||||||
}
|
}
|
||||||
return ESP_OK;
|
return ESP_OK;
|
||||||
}
|
}
|
||||||
@@ -345,13 +476,12 @@ static bool IRAM_ATTR _zh_encoder_isr_handler(pcnt_unit_handle_t unit, const pcn
|
|||||||
zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)user_ctx;
|
zh_encoder_handle_t *encoder_handle = (zh_encoder_handle_t *)user_ctx;
|
||||||
zh_encoder_queue_t encoder_queue = {0};
|
zh_encoder_queue_t encoder_queue = {0};
|
||||||
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
|
||||||
esp_err_t err = pcnt_unit_clear_count(unit);
|
if (pcnt_unit_clear_count(unit) != ESP_OK)
|
||||||
encoder_queue.watch_point_value = edata->watch_point_value;
|
|
||||||
encoder_queue.handle = encoder_handle;
|
|
||||||
if (err != ESP_OK)
|
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
encoder_queue.watch_point_value = edata->watch_point_value;
|
||||||
|
encoder_queue.handle = encoder_handle;
|
||||||
if (xQueueSendFromISR(_queue_handle, &encoder_queue, &xHigherPriorityTaskWoken) != pdTRUE)
|
if (xQueueSendFromISR(_queue_handle, &encoder_queue, &xHigherPriorityTaskWoken) != pdTRUE)
|
||||||
{
|
{
|
||||||
++_stats.queue_overflow_error;
|
++_stats.queue_overflow_error;
|
||||||
@@ -365,10 +495,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)
|
||||||
{
|
{
|
||||||
|
(void)pvParameter;
|
||||||
zh_encoder_queue_t encoder_queue = {0};
|
zh_encoder_queue_t encoder_queue = {0};
|
||||||
bool is_value_changed = false;
|
|
||||||
while (xQueueReceive(_queue_handle, &encoder_queue, portMAX_DELAY) == pdTRUE)
|
while (xQueueReceive(_queue_handle, &encoder_queue, portMAX_DELAY) == pdTRUE)
|
||||||
{
|
{
|
||||||
|
bool is_value_changed = false;
|
||||||
zh_encoder_handle_t *encoder_handle = encoder_queue.handle;
|
zh_encoder_handle_t *encoder_handle = encoder_queue.handle;
|
||||||
switch (encoder_queue.watch_point_value)
|
switch (encoder_queue.watch_point_value)
|
||||||
{
|
{
|
||||||
@@ -402,13 +533,12 @@ static void IRAM_ATTR _zh_encoder_isr_processing_task(void *pvParameter)
|
|||||||
is_value_changed = false;
|
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;
|
if (encoder_handle->encoder_position == -0)
|
||||||
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;
|
encoder_handle->encoder_position = 0;
|
||||||
ZH_LOGE("Encoder isr processing failed. Failed to post interrupt event.", err);
|
|
||||||
}
|
}
|
||||||
|
encoder_data.encoder_position = encoder_handle->encoder_position;
|
||||||
|
ZH_ERROR_CHECK_CONT(esp_event_post(ZH_ENCODER, ZH_ENCODER_EVENT, &encoder_data, sizeof(zh_encoder_event_on_isr_t), 1000 / portTICK_PERIOD_MS) == ESP_OK, ++_stats.event_post_error, "Encoder isr processing failed. Failed to post interrupt event.");
|
||||||
}
|
}
|
||||||
_stats.min_stack_size = (uint32_t)uxTaskGetStackHighWaterMark(NULL);
|
_stats.min_stack_size = (uint32_t)uxTaskGetStackHighWaterMark(NULL);
|
||||||
}
|
}
|
||||||
@@ -422,15 +552,14 @@ static void IRAM_ATTR _zh_encoder_button_isr_handler(void *arg)
|
|||||||
uint64_t _current_us = esp_timer_get_time();
|
uint64_t _current_us = esp_timer_get_time();
|
||||||
if (_current_us - encoder_handle->s_gpio_prev_time >= encoder_handle->s_gpio_debounce_time)
|
if (_current_us - encoder_handle->s_gpio_prev_time >= encoder_handle->s_gpio_debounce_time)
|
||||||
{
|
{
|
||||||
bool s_gpio_status = gpio_get_level((gpio_num_t)encoder_handle->s_gpio_number);
|
bool s_gpio_status = gpio_get_level(encoder_handle->s_gpio_number);
|
||||||
if (encoder_handle->s_gpio_status != s_gpio_status)
|
if (encoder_handle->s_gpio_status != s_gpio_status)
|
||||||
{
|
{
|
||||||
encoder_handle->s_gpio_status = s_gpio_status;
|
encoder_handle->s_gpio_status = s_gpio_status;
|
||||||
zh_encoder_button_event_on_isr_t encoder_data = {0};
|
zh_encoder_button_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.button_status = encoder_handle->s_gpio_status;
|
encoder_data.button_status = encoder_handle->s_gpio_status;
|
||||||
esp_err_t err = esp_event_isr_post(ZH_ENCODER, ZH_BUTTON_EVENT, &encoder_data, sizeof(zh_encoder_button_event_on_isr_t), &xHigherPriorityTaskWoken);
|
if (esp_event_isr_post(ZH_ENCODER, ZH_BUTTON_EVENT, &encoder_data, sizeof(zh_encoder_button_event_on_isr_t), &xHigherPriorityTaskWoken) != ESP_OK)
|
||||||
if (err != ESP_OK)
|
|
||||||
{
|
{
|
||||||
++_stats.event_post_error;
|
++_stats.event_post_error;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in new issue
Block a user