1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-25 06:32:22 +01:00

🏗 Merge C++ into python codebase (#504)

## Description:

Move esphome-core codebase into esphome (and a bunch of other refactors). See https://github.com/esphome/feature-requests/issues/97

Yes this is a shit ton of work and no there's no way to automate it :( But it will be worth it 👍

Progress:
- Core support (file copy etc): 80%
- Base Abstractions (light, switch): ~50%
- Integrations: ~10%
- Working? Yes, (but only with ported components).

Other refactors:
- Moves all codegen related stuff into a single class: `esphome.codegen` (imported as `cg`)
- Rework coroutine syntax
- Move from `component/platform.py` to `domain/component.py` structure as with HA
- Move all defaults out of C++ and into config validation.
- Remove `make_...` helpers from Application class. Reason: Merge conflicts with every single new integration.
- Pointer Variables are stored globally instead of locally in setup(). Reason: stack size limit.

Future work:
- Rework const.py - Move all `CONF_...` into a conf class (usage `conf.UPDATE_INTERVAL` vs `CONF_UPDATE_INTERVAL`). Reason: Less convoluted import block
- Enable loading from `custom_components` folder.

**Related issue (if applicable):** https://github.com/esphome/feature-requests/issues/97

**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#<esphome-docs PR number goes here>

## Checklist:
  - [ ] The code change is tested and works locally.
  - [ ] Tests have been added to verify that the new code works (under `tests/` folder).

If user exposed functionality or configuration variables are added/changed:
  - [ ] Documentation added/updated in [esphomedocs](https://github.com/OttoWinter/esphomedocs).
This commit is contained in:
Otto Winter
2019-04-17 12:06:00 +02:00
committed by GitHub
parent 049807e3ab
commit 6682c43dfa
817 changed files with 54156 additions and 10830 deletions

View File

@@ -1,23 +1,18 @@
import voluptuous as vol
from esphome.automation import ACTION_REGISTRY
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.automation import ACTION_REGISTRY
from esphome.const import CONF_ACCELERATION, CONF_DECELERATION, CONF_ID, CONF_MAX_SPEED, \
CONF_POSITION, CONF_TARGET
from esphome.core import CORE
from esphome.cpp_generator import Pvariable, add, get_variable, templatable
from esphome.cpp_types import Action, esphome_ns, int32
from esphome.core import CORE, coroutine
PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA.extend({
})
IS_PLATFORM_COMPONENT = True
# pylint: disable=invalid-name
stepper_ns = esphome_ns.namespace('stepper')
stepper_ns = cg.esphome_ns.namespace('stepper')
Stepper = stepper_ns.class_('Stepper')
SetTargetAction = stepper_ns.class_('SetTargetAction', Action)
ReportPositionAction = stepper_ns.class_('ReportPositionAction', Action)
SetTargetAction = stepper_ns.class_('SetTargetAction', cg.Action)
ReportPositionAction = stepper_ns.class_('ReportPositionAction', cg.Action)
def validate_acceleration(value):
@@ -32,10 +27,10 @@ def validate_acceleration(value):
try:
value = float(value)
except ValueError:
raise vol.Invalid("Expected acceleration as floating point number, got {}".format(value))
raise cv.Invalid("Expected acceleration as floating point number, got {}".format(value))
if value <= 0:
raise vol.Invalid("Acceleration must be larger than 0 steps/s^2!")
raise cv.Invalid("Acceleration must be larger than 0 steps/s^2!")
return value
@@ -52,69 +47,65 @@ def validate_speed(value):
try:
value = float(value)
except ValueError:
raise vol.Invalid("Expected speed as floating point number, got {}".format(value))
raise cv.Invalid("Expected speed as floating point number, got {}".format(value))
if value <= 0:
raise vol.Invalid("Speed must be larger than 0 steps/s!")
raise cv.Invalid("Speed must be larger than 0 steps/s!")
return value
STEPPER_SCHEMA = cv.Schema({
vol.Required(CONF_MAX_SPEED): validate_speed,
vol.Optional(CONF_ACCELERATION): validate_acceleration,
vol.Optional(CONF_DECELERATION): validate_acceleration,
cv.Required(CONF_MAX_SPEED): validate_speed,
cv.Optional(CONF_ACCELERATION, default='inf'): validate_acceleration,
cv.Optional(CONF_DECELERATION, default='inf'): validate_acceleration,
})
STEPPER_PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(STEPPER_SCHEMA.schema)
@coroutine
def setup_stepper_core_(stepper_var, config):
if CONF_ACCELERATION in config:
add(stepper_var.set_acceleration(config[CONF_ACCELERATION]))
cg.add(stepper_var.set_acceleration(config[CONF_ACCELERATION]))
if CONF_DECELERATION in config:
add(stepper_var.set_deceleration(config[CONF_DECELERATION]))
cg.add(stepper_var.set_deceleration(config[CONF_DECELERATION]))
if CONF_MAX_SPEED in config:
add(stepper_var.set_max_speed(config[CONF_MAX_SPEED]))
cg.add(stepper_var.set_max_speed(config[CONF_MAX_SPEED]))
def setup_stepper(stepper_var, config):
CORE.add_job(setup_stepper_core_, stepper_var, config)
@coroutine
def register_stepper(var, config):
if not CORE.has_id(config[CONF_ID]):
var = cg.Pvariable(config[CONF_ID], var)
yield setup_stepper_core_(var, config)
BUILD_FLAGS = '-DUSE_STEPPER'
CONF_STEPPER_SET_TARGET = 'stepper.set_target'
STEPPER_SET_TARGET_ACTION_SCHEMA = cv.Schema({
vol.Required(CONF_ID): cv.use_variable_id(Stepper),
vol.Required(CONF_TARGET): cv.templatable(cv.int_),
})
@ACTION_REGISTRY.register(CONF_STEPPER_SET_TARGET, STEPPER_SET_TARGET_ACTION_SCHEMA)
@ACTION_REGISTRY.register('stepper.set_target', cv.Schema({
cv.Required(CONF_ID): cv.use_variable_id(Stepper),
cv.Required(CONF_TARGET): cv.templatable(cv.int_),
}))
def stepper_set_target_to_code(config, action_id, template_arg, args):
var = yield get_variable(config[CONF_ID])
rhs = var.make_set_target_action(template_arg)
var = yield cg.get_variable(config[CONF_ID])
type = SetTargetAction.template(template_arg)
action = Pvariable(action_id, rhs, type=type)
template_ = yield templatable(config[CONF_TARGET], args, int32)
add(action.set_target(template_))
rhs = type.new(var)
action = cg.Pvariable(action_id, rhs, type=type)
template_ = yield cg.templatable(config[CONF_TARGET], args, cg.int32)
cg.add(action.set_target(template_))
yield action
CONF_STEPPER_REPORT_POSITION = 'stepper.report_position'
STEPPER_REPORT_POSITION_ACTION_SCHEMA = cv.Schema({
vol.Required(CONF_ID): cv.use_variable_id(Stepper),
vol.Required(CONF_POSITION): cv.templatable(cv.int_),
})
@ACTION_REGISTRY.register(CONF_STEPPER_REPORT_POSITION, STEPPER_REPORT_POSITION_ACTION_SCHEMA)
@ACTION_REGISTRY.register('stepper.report_position', cv.Schema({
cv.Required(CONF_ID): cv.use_variable_id(Stepper),
cv.Required(CONF_POSITION): cv.templatable(cv.int_),
}))
def stepper_report_position_to_code(config, action_id, template_arg, args):
var = yield get_variable(config[CONF_ID])
rhs = var.make_report_position_action(template_arg)
var = yield cg.get_variable(config[CONF_ID])
type = ReportPositionAction.template(template_arg)
action = Pvariable(action_id, rhs, type=type)
template_ = yield templatable(config[CONF_POSITION], args, int32)
add(action.set_position(template_))
rhs = type.new(var)
action = cg.Pvariable(action_id, rhs, type=type)
template_ = yield cg.templatable(config[CONF_POSITION], args, cg.int32)
cg.add(action.set_position(template_))
yield action
def to_code(config):
cg.add_global(stepper_ns.using)

View File

@@ -1,35 +0,0 @@
import voluptuous as vol
from esphome import pins
from esphome.components import stepper
import esphome.config_validation as cv
from esphome.const import CONF_DIR_PIN, CONF_ID, CONF_SLEEP_PIN, CONF_STEP_PIN
from esphome.cpp_generator import Pvariable, add
from esphome.cpp_helpers import gpio_output_pin_expression, setup_component
from esphome.cpp_types import App, Component
A4988 = stepper.stepper_ns.class_('A4988', stepper.Stepper, Component)
PLATFORM_SCHEMA = stepper.STEPPER_PLATFORM_SCHEMA.extend({
vol.Required(CONF_ID): cv.declare_variable_id(A4988),
vol.Required(CONF_STEP_PIN): pins.gpio_output_pin_schema,
vol.Required(CONF_DIR_PIN): pins.gpio_output_pin_schema,
vol.Optional(CONF_SLEEP_PIN): pins.gpio_output_pin_schema,
}).extend(cv.COMPONENT_SCHEMA.schema)
def to_code(config):
step_pin = yield gpio_output_pin_expression(config[CONF_STEP_PIN])
dir_pin = yield gpio_output_pin_expression(config[CONF_DIR_PIN])
rhs = App.make_a4988(step_pin, dir_pin)
a4988 = Pvariable(config[CONF_ID], rhs)
if CONF_SLEEP_PIN in config:
sleep_pin = yield gpio_output_pin_expression(config[CONF_SLEEP_PIN])
add(a4988.set_sleep_pin(sleep_pin))
stepper.setup_stepper(a4988, config)
setup_component(a4988, config)
BUILD_FLAGS = '-DUSE_A4988'

View File

@@ -0,0 +1,50 @@
#include "stepper.h"
#include "esphome/core/log.h"
namespace esphome {
namespace stepper {
static const char *TAG = "stepper";
void Stepper::calculate_speed_(uint32_t now) {
// delta t since last calculation in seconds
float dt = (now - this->last_calculation_) * 1e-6f;
this->last_calculation_ = now;
if (this->has_reached_target()) {
this->current_speed_ = 0.0f;
return;
}
int32_t num_steps = abs(int32_t(this->target_position) - int32_t(this->current_position));
// (v_0)^2 / 2*a
float v_squared = this->current_speed_ * this->current_speed_;
auto steps_to_decelerate = static_cast<int32_t>(v_squared / (2 * this->deceleration_));
if (num_steps <= steps_to_decelerate) {
// need to start decelerating
this->current_speed_ -= this->deceleration_ * dt;
} else {
// we can still accelerate
this->current_speed_ += this->acceleration_ * dt;
}
this->current_speed_ = clamp(this->current_speed_, 0.0f, this->max_speed_);
}
int32_t Stepper::should_step_() {
uint32_t now = micros();
this->calculate_speed_(now);
if (this->current_speed_ == 0.0f)
return 0;
// assumes this method is called in a constant interval
uint32_t dt = now - this->last_step_;
if (dt >= (1 / this->current_speed_) * 1e6f) {
int32_t mag = this->target_position > this->current_position ? 1 : -1;
this->last_step_ = now;
this->current_position += mag;
return mag;
}
return 0;
}
} // namespace stepper
} // namespace esphome

View File

@@ -0,0 +1,70 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/automation.h"
#include "esphome/components/stepper/stepper.h"
namespace esphome {
namespace stepper {
#define LOG_STEPPER(this) \
ESP_LOGCONFIG(TAG, " Acceleration: %.0f steps/s^2", this->acceleration_); \
ESP_LOGCONFIG(TAG, " Deceleration: %.0f steps/s^2", this->deceleration_); \
ESP_LOGCONFIG(TAG, " Max Speed: %.0f steps/s", this->max_speed_);
class Stepper {
public:
void set_target(int32_t steps) { this->target_position = steps; }
void report_position(int32_t steps) { this->current_position = steps; }
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; }
bool has_reached_target() { return this->current_position == this->target_position; }
int32_t current_position{0};
int32_t target_position{0};
protected:
void calculate_speed_(uint32_t now);
int32_t should_step_();
float acceleration_{1e6f};
float deceleration_{1e6f};
float current_speed_{0.0f};
float max_speed_{1e6f};
uint32_t last_calculation_{0};
uint32_t last_step_{0};
};
template<typename... Ts> class SetTargetAction : public Action<Ts...> {
public:
explicit SetTargetAction(Stepper *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(int32_t, target)
void play(Ts... x) override {
this->parent_->set_target(this->target_.value(x...));
this->play_next(x...);
}
protected:
Stepper *parent_;
};
template<typename... Ts> class ReportPositionAction : public Action<Ts...> {
public:
explicit ReportPositionAction(Stepper *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(int32_t, position)
void play(Ts... x) override {
this->parent_->report_position(this->position_.value(x...));
this->play_next(x...);
}
protected:
Stepper *parent_;
};
} // namespace stepper
} // namespace esphome

View File

@@ -1,51 +0,0 @@
import voluptuous as vol
from esphome import pins
from esphome.components import stepper
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_PIN_A, CONF_PIN_B, CONF_PIN_C, CONF_PIN_D, \
CONF_SLEEP_WHEN_DONE, CONF_STEP_MODE
from esphome.cpp_generator import Pvariable, add
from esphome.cpp_helpers import gpio_output_pin_expression, setup_component
from esphome.cpp_types import App, Component
ULN2003StepMode = stepper.stepper_ns.enum('ULN2003StepMode')
STEP_MODES = {
'FULL_STEP': ULN2003StepMode.ULN2003_STEP_MODE_FULL_STEP,
'HALF_STEP': ULN2003StepMode.ULN2003_STEP_MODE_HALF_STEP,
'WAVE_DRIVE': ULN2003StepMode.ULN2003_STEP_MODE_WAVE_DRIVE,
}
ULN2003 = stepper.stepper_ns.class_('ULN2003', stepper.Stepper, Component)
PLATFORM_SCHEMA = stepper.STEPPER_PLATFORM_SCHEMA.extend({
vol.Required(CONF_ID): cv.declare_variable_id(ULN2003),
vol.Required(CONF_PIN_A): pins.gpio_output_pin_schema,
vol.Required(CONF_PIN_B): pins.gpio_output_pin_schema,
vol.Required(CONF_PIN_C): pins.gpio_output_pin_schema,
vol.Required(CONF_PIN_D): pins.gpio_output_pin_schema,
vol.Optional(CONF_SLEEP_WHEN_DONE): cv.boolean,
vol.Optional(CONF_STEP_MODE): cv.one_of(*STEP_MODES, upper=True, space='_')
}).extend(cv.COMPONENT_SCHEMA.schema)
def to_code(config):
pin_a = yield gpio_output_pin_expression(config[CONF_PIN_A])
pin_b = yield gpio_output_pin_expression(config[CONF_PIN_B])
pin_c = yield gpio_output_pin_expression(config[CONF_PIN_C])
pin_d = yield gpio_output_pin_expression(config[CONF_PIN_D])
rhs = App.make_uln2003(pin_a, pin_b, pin_c, pin_d)
uln = Pvariable(config[CONF_ID], rhs)
if CONF_SLEEP_WHEN_DONE in config:
add(uln.set_sleep_when_done(config[CONF_SLEEP_WHEN_DONE]))
if CONF_STEP_MODE in config:
add(uln.set_step_mode(STEP_MODES[config[CONF_STEP_MODE]]))
stepper.setup_stepper(uln, config)
setup_component(uln, config)
BUILD_FLAGS = '-DUSE_ULN2003'