feat: added encoder positive rotation select

This commit is contained in:
2026-02-09 11:36:49 +03:00
parent d73d399255
commit 2c51e244a7
3 changed files with 6 additions and 2 deletions

View File

@@ -18,6 +18,7 @@
.a_gpio_number = GPIO_NUM_MAX, \ .a_gpio_number = GPIO_NUM_MAX, \
.b_gpio_number = GPIO_NUM_MAX, \ .b_gpio_number = GPIO_NUM_MAX, \
.pullup = true, \ .pullup = true, \
.rotation = true, \
.encoder_pulses = 0} .encoder_pulses = 0}
#ifdef __cplusplus #ifdef __cplusplus
@@ -33,6 +34,7 @@ extern "C"
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. */
bool pullup; /*!< Pullup GPIO enable/disable. */ bool pullup; /*!< Pullup GPIO enable/disable. */
bool rotation; /*!< Encoder rotation (true - positive for CW rotation, false - positive for CCW rotation). */
uint16_t encoder_pulses; /*!< Number of pulses per one rotation. */ uint16_t encoder_pulses; /*!< Number of pulses per one rotation. */
} zh_inclinometer_init_config_t; } zh_inclinometer_init_config_t;
@@ -45,6 +47,7 @@ extern "C"
pcnt_channel_handle_t pcnt_channel_a_handle; /*!< Inclinometer unique pcnt channel handle. */ pcnt_channel_handle_t pcnt_channel_a_handle; /*!< Inclinometer unique pcnt channel handle. */
pcnt_channel_handle_t pcnt_channel_b_handle; /*!< Inclinometer unique pcnt channel handle. */ pcnt_channel_handle_t pcnt_channel_b_handle; /*!< Inclinometer unique pcnt channel handle. */
float degrees_per_pulse; /*!< Number of degrees per pulse. */ float degrees_per_pulse; /*!< Number of degrees per pulse. */
bool rotation; /*!< Encoder rotation. */
bool is_initialized; /*!< Inclinometer initialization flag. */ bool is_initialized; /*!< Inclinometer initialization flag. */
} zh_inclinometer_handle_t; } zh_inclinometer_handle_t;

View File

@@ -1 +1 @@
2.0.0 2.1.0

View File

@@ -26,6 +26,7 @@ esp_err_t zh_inclinometer_init(const zh_inclinometer_init_config_t *config, zh_i
err = _zh_inclinometer_pcnt_init(config, handle); err = _zh_inclinometer_pcnt_init(config, handle);
ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Inclinometer initialization failed. PCNT initialization failed."); ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Inclinometer initialization failed. PCNT initialization failed.");
handle->degrees_per_pulse = 360.0 / config->encoder_pulses; handle->degrees_per_pulse = 360.0 / config->encoder_pulses;
handle->rotation = config->rotation;
handle->is_initialized = true; handle->is_initialized = true;
ZH_LOGI("Inclinometer initialization completed successfully."); ZH_LOGI("Inclinometer initialization completed successfully.");
return ESP_OK; return ESP_OK;
@@ -54,7 +55,7 @@ esp_err_t zh_inclinometer_get(zh_inclinometer_handle_t *handle, float *angle)
int pcnt_count = 0; int pcnt_count = 0;
pcnt_unit_get_count(handle->pcnt_unit_handle, &pcnt_count); pcnt_unit_get_count(handle->pcnt_unit_handle, &pcnt_count);
float angle_temp = pcnt_count * handle->degrees_per_pulse; float angle_temp = pcnt_count * handle->degrees_per_pulse;
*angle = (angle_temp < 0) ? -angle_temp : angle_temp; *angle = (handle->rotation == true) ? angle_temp : -angle_temp;
ZH_LOGI("Inclinometer get position completed successfully."); ZH_LOGI("Inclinometer get position completed successfully.");
return ESP_OK; return ESP_OK;
} }