🏗 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
@@ -0,0 +1,26 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome import pins
from esphome.components import remote_base
from esphome.const import CONF_CARRIER_DUTY_PERCENT, CONF_ID, CONF_PIN
AUTO_LOAD = ['remote_base']
remote_transmitter_ns = cg.esphome_ns.namespace('remote_transmitter')
RemoteTransmitterComponent = remote_transmitter_ns.class_('RemoteTransmitterComponent',
remote_base.RemoteTransmitterBase,
cg.Component)
MULTI_CONF = True
CONFIG_SCHEMA = cv.Schema({
cv.GenerateID(): cv.declare_variable_id(RemoteTransmitterComponent),
cv.Required(CONF_PIN): pins.gpio_output_pin_schema,
cv.Required(CONF_CARRIER_DUTY_PERCENT): cv.All(cv.percentage_int, cv.Range(min=1, max=100)),
}).extend(cv.COMPONENT_SCHEMA)
def to_code(config):
pin = yield cg.gpio_pin_expression(config[CONF_PIN])
var = cg.new_Pvariable(config[CONF_ID], pin)
yield cg.register_component(var, config)
cg.add(var.set_carrier_duty_percent(config[CONF_CARRIER_DUTY_PERCENT]))
@@ -0,0 +1,11 @@
#include "remote_transmitter.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
namespace esphome {
namespace remote_transmitter {
static const char *TAG = "remote_transmitter";
} // namespace remote_transmitter
} // namespace esphome
@@ -0,0 +1,43 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/remote_base/remote_base.h"
namespace esphome {
namespace remote_transmitter {
class RemoteTransmitterComponent : public remote_base::RemoteTransmitterBase, public Component {
public:
explicit RemoteTransmitterComponent(GPIOPin *pin) : remote_base::RemoteTransmitterBase(pin) {}
void setup() override;
void dump_config() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void set_carrier_duty_percent(uint8_t carrier_duty_percent) { this->carrier_duty_percent_ = carrier_duty_percent; }
protected:
void send_internal(uint32_t send_times, uint32_t send_wait) override;
#ifdef ARDUINO_ARCH_ESP8266
void calculate_on_off_time_(uint32_t carrier_frequency, uint32_t *on_time_period, uint32_t *off_time_period);
void mark_(uint32_t on_time, uint32_t off_time, uint32_t usec);
void space_(uint32_t usec);
#endif
#ifdef ARDUINO_ARCH_ESP32
void configure_rmt();
uint32_t current_carrier_frequency_{UINT32_MAX};
bool initialized_{false};
std::vector<rmt_item32_t> rmt_temp_;
#endif
uint8_t carrier_duty_percent_{50};
remote_base::RemoteTransmitData temp_;
};
} // namespace remote_transmitter
} // namespace esphome
@@ -0,0 +1,134 @@
#include "remote_transmitter.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace remote_transmitter {
static const char *TAG = "remote_transmitter";
void RemoteTransmitterComponent::setup() {}
void RemoteTransmitterComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Remote Transmitter...");
ESP_LOGCONFIG(TAG, " Channel: %d", this->channel_);
ESP_LOGCONFIG(TAG, " Clock divider: %u", this->clock_divider_);
LOG_PIN(" Pin: ", this->pin_);
if (this->current_carrier_frequency_ != 0 && this->carrier_duty_percent_ != 100) {
ESP_LOGCONFIG(TAG, " Carrier Duty: %u%%", this->carrier_duty_percent_);
}
if (this->is_failed()) {
ESP_LOGE(TAG, "Configuring RMT driver failed: %s", esp_err_to_name(this->error_code_));
}
}
void RemoteTransmitterComponent::configure_rmt() {
rmt_config_t c{};
c.rmt_mode = RMT_MODE_TX;
c.channel = this->channel_;
c.clk_div = this->clock_divider_;
c.gpio_num = gpio_num_t(this->pin_->get_pin());
c.mem_block_num = 1;
c.tx_config.loop_en = false;
if (this->current_carrier_frequency_ == 0 || this->carrier_duty_percent_ == 100) {
c.tx_config.carrier_en = false;
} else {
c.tx_config.carrier_en = true;
c.tx_config.carrier_freq_hz = this->current_carrier_frequency_;
c.tx_config.carrier_duty_percent = this->carrier_duty_percent_;
}
c.tx_config.idle_output_en = true;
if (!this->pin_->is_inverted()) {
c.tx_config.carrier_level = RMT_CARRIER_LEVEL_HIGH;
c.tx_config.idle_level = RMT_IDLE_LEVEL_LOW;
} else {
c.tx_config.carrier_level = RMT_CARRIER_LEVEL_LOW;
c.tx_config.idle_level = RMT_IDLE_LEVEL_HIGH;
}
esp_err_t error = rmt_config(&c);
if (error != ESP_OK) {
this->error_code_ = error;
this->mark_failed();
return;
}
if (!this->initialized_) {
error = rmt_driver_install(this->channel_, 0, 0);
if (error != ESP_OK) {
this->error_code_ = error;
this->mark_failed();
return;
}
this->initialized_ = true;
}
}
void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) {
if (this->is_failed())
return;
if (this->current_carrier_frequency_ != this->temp_.get_carrier_frequency()) {
this->current_carrier_frequency_ = this->temp_.get_carrier_frequency();
this->configure_rmt();
}
this->rmt_temp_.clear();
this->rmt_temp_.reserve((this->temp_.get_data().size() + 1) / 2);
uint32_t rmt_i = 0;
rmt_item32_t rmt_item;
for (int32_t val : this->temp_.get_data()) {
bool level = val >= 0;
if (!level)
val = -val;
val = this->from_microseconds(static_cast<uint32_t>(val));
do {
int32_t item = std::min(val, 32767);
val -= item;
if (rmt_i % 2 == 0) {
rmt_item.level0 = static_cast<uint32_t>(level);
rmt_item.duration0 = static_cast<uint32_t>(item);
} else {
rmt_item.level1 = static_cast<uint32_t>(level);
rmt_item.duration1 = static_cast<uint32_t>(item);
this->rmt_temp_.push_back(rmt_item);
}
rmt_i++;
} while (val != 0);
}
if (rmt_i % 2 == 1) {
rmt_item.level1 = 0;
rmt_item.duration1 = 0;
this->rmt_temp_.push_back(rmt_item);
}
for (uint16_t i = 0; i < send_times; i++) {
esp_err_t error = rmt_write_items(this->channel_, this->rmt_temp_.data(), this->rmt_temp_.size(), true);
if (error != ESP_OK) {
ESP_LOGW(TAG, "rmt_write_items failed: %s", esp_err_to_name(error));
this->status_set_warning();
} else {
this->status_clear_warning();
}
if (i + 1 < send_times) {
delay(send_wait / 1000UL);
delayMicroseconds(send_wait % 1000UL);
}
}
}
} // namespace remote_transmitter
} // namespace esphome
#endif
@@ -0,0 +1,95 @@
#include "remote_transmitter.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
#ifdef ARDUINO_ARCH_ESP8266
namespace esphome {
namespace remote_transmitter {
static const char *TAG = "remote_transmitter";
void RemoteTransmitterComponent::setup() {
this->pin_->setup();
this->pin_->digital_write(false);
}
void RemoteTransmitterComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Remote Transmitter...");
ESP_LOGCONFIG(TAG, " Carrier Duty: %u%%", this->carrier_duty_percent_);
LOG_PIN(" Pin: ", this->pin_);
}
void RemoteTransmitterComponent::calculate_on_off_time_(uint32_t carrier_frequency, uint32_t *on_time_period,
uint32_t *off_time_period) {
if (carrier_frequency == 0) {
*on_time_period = 0;
*off_time_period = 0;
return;
}
uint32_t period = (1000000UL + carrier_frequency / 2) / carrier_frequency; // round(1000000/freq)
period = std::max(uint32_t(1), period);
*on_time_period = (period * this->carrier_duty_percent_) / 100;
*off_time_period = period - *on_time_period;
}
void RemoteTransmitterComponent::mark_(uint32_t on_time, uint32_t off_time, uint32_t usec) {
if (this->carrier_duty_percent_ == 100 || (on_time == 0 && off_time == 0)) {
this->pin_->digital_write(true);
delay_microseconds_accurate(usec);
this->pin_->digital_write(false);
return;
}
const uint32_t start_time = micros();
uint32_t current_time = start_time;
while (current_time - start_time < usec) {
const uint32_t elapsed = current_time - start_time;
this->pin_->digital_write(true);
delay_microseconds_accurate(std::min(on_time, usec - elapsed));
this->pin_->digital_write(false);
if (elapsed + on_time >= usec)
return;
delay_microseconds_accurate(std::min(usec - elapsed - on_time, off_time));
current_time = micros();
}
}
void RemoteTransmitterComponent::space_(uint32_t usec) {
this->pin_->digital_write(false);
delay_microseconds_accurate(usec);
}
void RemoteTransmitterComponent::send_internal(uint32_t send_times, uint32_t send_wait) {
for (uint32_t i = 0; i < send_times; i++) {
uint32_t on_time, off_time;
this->calculate_on_off_time_(this->temp_.get_carrier_frequency(), &on_time, &off_time);
ESP_LOGD(TAG, "Sending remote code...");
ESP.wdtFeed();
disable_interrupts();
for (int32_t item : this->temp_.get_data()) {
if (item > 0) {
const auto length = uint32_t(item);
this->mark_(on_time, off_time, length);
} else {
const auto length = uint32_t(-item);
this->space_(length);
}
App.feed_wdt();
}
enable_interrupts();
if (i + 1 < send_times) {
delay(send_wait / 1000UL);
delayMicroseconds(send_wait % 1000UL);
}
}
}
} // namespace remote_transmitter
} // namespace esphome
#endif
@@ -0,0 +1,30 @@
import esphome.config_validation as cv
from esphome.components.remote_base import BINARY_SENSOR_REGISTRY
from esphome.util import OrderedDict
def show_new(value):
from esphome import yaml_util
for key in BINARY_SENSOR_REGISTRY:
if key in value:
break
else:
raise cv.Invalid("This platform has been removed in 1.13, please see the docs for updated "
"instructions.")
val = value[key]
args = [('platform', 'template')]
if 'id' in value:
args.append(('id', value['id']))
if 'name' in value:
args.append(('name', value['name']))
args.append(('turn_on_action', {
'remote_transmitter.transmit_{}'.format(key): val
}))
text = yaml_util.dump([OrderedDict(args)])
raise cv.Invalid(u"This platform has been removed in 1.13, please change to:\n\n{}\n\n."
u"".format(text))
CONFIG_SCHEMA = show_new