Update stepper.h to use enums

This commit is contained in:
cvwillegen 2024-05-28 11:40:16 +02:00 committed by GitHub
parent 5040a9146d
commit eccf7860b5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -7,6 +7,13 @@
namespace esphome { namespace esphome {
namespace stepper { namespace stepper {
// Possible rotations
enum Rotations {
ROTATION_CCW = -1, /// Only rotate counter-clockwise
ROTATION_BOTH = 0, /// Rotation possible in 2 directions
ROTATION_CW = 1, /// Only rotate clockwise
};
#define LOG_STEPPER(this) \ #define LOG_STEPPER(this) \
ESP_LOGCONFIG(TAG, " Acceleration: %.0f steps/s^2", this->acceleration_); \ ESP_LOGCONFIG(TAG, " Acceleration: %.0f steps/s^2", this->acceleration_); \
ESP_LOGCONFIG(TAG, " Deceleration: %.0f steps/s^2", this->deceleration_); \ ESP_LOGCONFIG(TAG, " Deceleration: %.0f steps/s^2", this->deceleration_); \
@ -20,7 +27,7 @@ class Stepper {
void set_acceleration(float acceleration) { this->acceleration_ = acceleration; } void set_acceleration(float acceleration) { this->acceleration_ = acceleration; }
void set_deceleration(float deceleration) { this->deceleration_ = deceleration; } void set_deceleration(float deceleration) { this->deceleration_ = deceleration; }
void set_max_speed(float max_speed) { this->max_speed_ = max_speed; } void set_max_speed(float max_speed) { this->max_speed_ = max_speed; }
void set_rotation(int rotation) { this->rotation_ = rotation; } void set_rotation(Rotations rotation) { this->rotation_ = rotation; }
virtual void on_update_speed() {} virtual void on_update_speed() {}
bool has_reached_target() { return this->current_position == this->target_position; } bool has_reached_target() { return this->current_position == this->target_position; }
@ -35,7 +42,7 @@ class Stepper {
float deceleration_{1e6f}; float deceleration_{1e6f};
float current_speed_{0.0f}; float current_speed_{0.0f};
float max_speed_{1e6f}; float max_speed_{1e6f};
int rotation_{0}; Rotations rotation_{ROTATION_BOTH};
uint32_t last_calculation_{0}; uint32_t last_calculation_{0};
uint32_t last_step_{0}; uint32_t last_step_{0};
}; };
@ -114,10 +121,10 @@ template<typename... Ts> class SetRotationAction : public Action<Ts...> {
public: public:
explicit SetRotationAction(Stepper *parent) : parent_(parent) {} explicit SetRotationAction(Stepper *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(int, rotation); TEMPLATABLE_VALUE(Rotations, rotation);
void play(Ts... x) override { void play(Ts... x) override {
int rotation = this->rotation_.value(x...); Rotations rotation = this->rotation_.value(x...);
this->parent_->set_rotation(rotation); this->parent_->set_rotation(rotation);
} }