1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-25 06:32:22 +01:00
This commit is contained in:
Otto Winter
2019-04-24 23:49:02 +02:00
parent 0a0713f0e2
commit 766f6c045d
44 changed files with 1202 additions and 592 deletions

View File

@@ -2,8 +2,8 @@ import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import automation
from esphome.const import CONF_ACCELERATION, CONF_DECELERATION, CONF_ID, CONF_MAX_SPEED, \
CONF_POSITION, CONF_TARGET
from esphome.core import CORE, coroutine
CONF_POSITION, CONF_TARGET, CONF_SPEED
from esphome.core import CORE, coroutine, coroutine_with_priority
IS_PLATFORM_COMPONENT = True
@@ -13,6 +13,7 @@ Stepper = stepper_ns.class_('Stepper')
SetTargetAction = stepper_ns.class_('SetTargetAction', automation.Action)
ReportPositionAction = stepper_ns.class_('ReportPositionAction', automation.Action)
SetSpeedAction = stepper_ns.class_('SetSpeedAction', automation.Action)
def validate_acceleration(value):
@@ -103,5 +104,18 @@ def stepper_report_position_to_code(config, action_id, template_arg, args):
yield var
@automation.register_action('stepper.set_speed', SetSpeedAction, cv.Schema({
cv.Required(CONF_ID): cv.use_id(Stepper),
cv.Required(CONF_SPEED): cv.templatable(validate_speed),
}))
def stepper_set_speed_to_code(config, action_id, template_arg, args):
paren = yield cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
template_ = yield cg.templatable(config[CONF_SPEED], args, cg.int32)
cg.add(var.set_speed(template_))
yield var
@coroutine_with_priority(100.0)
def to_code(config):
cg.add_global(stepper_ns.using)

View File

@@ -19,6 +19,7 @@ class Stepper {
void set_acceleration(float acceleration) { this->acceleration_ = acceleration; }
void set_deceleration(float deceleration) { this->deceleration_ = deceleration; }
void set_max_speed(float max_speed) { this->max_speed_ = max_speed; }
virtual void on_update_speed() {}
bool has_reached_target() { return this->current_position == this->target_position; }
int32_t current_position{0};
@@ -60,5 +61,21 @@ template<typename... Ts> class ReportPositionAction : public Action<Ts...> {
Stepper *parent_;
};
template<typename... Ts> class SetSpeedAction : public Action<Ts...> {
public:
explicit SetSpeedAction(Stepper *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(float, speed);
void play(Ts... x) override {
float speed = this->speed_.value(x...);
this->parent_->set_max_speed(speed);
this->parent_->on_update_speed();
}
protected:
Stepper *parent_;
};
} // namespace stepper
} // namespace esphome