Add stepper.set_acceleration and stepper.set_deceleration to stepper component (#1977)

This commit is contained in:
WeekendWarrior1 2021-07-05 11:22:43 +10:00 committed by GitHub
parent ab31117bf3
commit 79b9d0579d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 69 additions and 1 deletions

View File

@ -21,6 +21,8 @@ Stepper = stepper_ns.class_("Stepper")
SetTargetAction = stepper_ns.class_("SetTargetAction", automation.Action) SetTargetAction = stepper_ns.class_("SetTargetAction", automation.Action)
ReportPositionAction = stepper_ns.class_("ReportPositionAction", automation.Action) ReportPositionAction = stepper_ns.class_("ReportPositionAction", automation.Action)
SetSpeedAction = stepper_ns.class_("SetSpeedAction", automation.Action) SetSpeedAction = stepper_ns.class_("SetSpeedAction", automation.Action)
SetAccelerationAction = stepper_ns.class_("SetAccelerationAction", automation.Action)
SetDecelerationAction = stepper_ns.class_("SetDecelerationAction", automation.Action)
def validate_acceleration(value): def validate_acceleration(value):
@ -138,11 +140,47 @@ async def stepper_report_position_to_code(config, action_id, template_arg, args)
async def stepper_set_speed_to_code(config, action_id, template_arg, args): async def stepper_set_speed_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID]) paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren) var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_SPEED], args, cg.int32) template_ = await cg.templatable(config[CONF_SPEED], args, cg.float_)
cg.add(var.set_speed(template_)) cg.add(var.set_speed(template_))
return var return var
@automation.register_action(
"stepper.set_acceleration",
SetAccelerationAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(Stepper),
cv.Required(CONF_ACCELERATION): cv.templatable(validate_acceleration),
}
),
)
async def stepper_set_acceleration_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_ACCELERATION], args, cg.float_)
cg.add(var.set_acceleration(template_))
return var
@automation.register_action(
"stepper.set_deceleration",
SetDecelerationAction,
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(Stepper),
cv.Required(CONF_DECELERATION): cv.templatable(validate_acceleration),
}
),
)
async def stepper_set_deceleration_to_code(config, action_id, template_arg, args):
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = await cg.templatable(config[CONF_DECELERATION], args, cg.float_)
cg.add(var.set_deceleration(template_))
return var
@coroutine_with_priority(100.0) @coroutine_with_priority(100.0)
async def to_code(config): async def to_code(config):
cg.add_global(stepper_ns.using) cg.add_global(stepper_ns.using)

View File

@ -77,5 +77,35 @@ template<typename... Ts> class SetSpeedAction : public Action<Ts...> {
Stepper *parent_; Stepper *parent_;
}; };
template<typename... Ts> class SetAccelerationAction : public Action<Ts...> {
public:
explicit SetAccelerationAction(Stepper *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(float, acceleration);
void play(Ts... x) override {
float acceleration = this->acceleration_.value(x...);
this->parent_->set_acceleration(acceleration);
}
protected:
Stepper *parent_;
};
template<typename... Ts> class SetDecelerationAction : public Action<Ts...> {
public:
explicit SetDecelerationAction(Stepper *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(float, deceleration);
void play(Ts... x) override {
float deceleration = this->deceleration_.value(x...);
this->parent_->set_deceleration(deceleration);
}
protected:
Stepper *parent_;
};
} // namespace stepper } // namespace stepper
} // namespace esphome } // namespace esphome