diff --git a/include/zh_inclinometer.h b/include/zh_inclinometer.h index 3348475..82af823 100644 --- a/include/zh_inclinometer.h +++ b/include/zh_inclinometer.h @@ -18,6 +18,7 @@ .a_gpio_number = GPIO_NUM_MAX, \ .b_gpio_number = GPIO_NUM_MAX, \ .pullup = true, \ + .rotation = true, \ .encoder_pulses = 0} #ifdef __cplusplus @@ -33,6 +34,7 @@ extern "C" uint8_t a_gpio_number; /*!< Encoder A GPIO number. */ uint8_t b_gpio_number; /*!< Encoder B GPIO number. */ 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. */ } 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_b_handle; /*!< Inclinometer unique pcnt channel handle. */ float degrees_per_pulse; /*!< Number of degrees per pulse. */ + bool rotation; /*!< Encoder rotation. */ bool is_initialized; /*!< Inclinometer initialization flag. */ } zh_inclinometer_handle_t; diff --git a/version.txt b/version.txt index 359a5b9..50aea0e 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -2.0.0 \ No newline at end of file +2.1.0 \ No newline at end of file diff --git a/zh_inclinometer.c b/zh_inclinometer.c index ba2c354..1fa65f1 100644 --- a/zh_inclinometer.c +++ b/zh_inclinometer.c @@ -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); ZH_ERROR_CHECK(err == ESP_OK, err, NULL, "Inclinometer initialization failed. PCNT initialization failed."); handle->degrees_per_pulse = 360.0 / config->encoder_pulses; + handle->rotation = config->rotation; handle->is_initialized = true; ZH_LOGI("Inclinometer initialization completed successfully."); return ESP_OK; @@ -54,7 +55,7 @@ esp_err_t zh_inclinometer_get(zh_inclinometer_handle_t *handle, float *angle) int pcnt_count = 0; pcnt_unit_get_count(handle->pcnt_unit_handle, &pcnt_count); 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."); return ESP_OK; }