1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-26 07:02:21 +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

@@ -0,0 +1,38 @@
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_BUFFER_SIZE, CONF_DUMP, CONF_FILTER, CONF_ID, CONF_IDLE, \
CONF_PIN, CONF_TOLERANCE
AUTO_LOAD = ['remote_base']
remote_receiver_ns = cg.esphome_ns.namespace('remote_receiver')
RemoteReceiverComponent = remote_receiver_ns.class_('RemoteReceiverComponent',
remote_base.RemoteReceiverBase,
cg.Component)
MULTI_CONF = True
CONFIG_SCHEMA = remote_base.validate_triggers(cv.Schema({
cv.GenerateID(): cv.declare_variable_id(RemoteReceiverComponent),
cv.Required(CONF_PIN): cv.All(pins.internal_gpio_input_pin_schema,
pins.validate_has_interrupt),
cv.Optional(CONF_DUMP, default=[]): remote_base.validate_dumpers,
cv.Optional(CONF_TOLERANCE, default=25): cv.All(cv.percentage_int, cv.Range(min=0)),
cv.SplitDefault(CONF_BUFFER_SIZE, esp32='10000b', esp8266='1000b'): cv.validate_bytes,
cv.Optional(CONF_FILTER, default='50us'): cv.positive_time_period_microseconds,
cv.Optional(CONF_IDLE, default='10ms'): cv.positive_time_period_microseconds,
}).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 remote_base.build_dumpers(config[CONF_DUMP])
yield remote_base.build_triggers(config)
yield cg.register_component(var, config)
cg.add(var.set_tolerance(config[CONF_TOLERANCE]))
cg.add(var.set_buffer_size(config[CONF_BUFFER_SIZE]))
cg.add(var.set_filter_us(config[CONF_FILTER]))
cg.add(var.set_idle_us(config[CONF_IDLE]))

View File

@@ -0,0 +1,18 @@
from esphome.components import binary_sensor, remote_base
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.const import CONF_NAME
DEPENDENCIES = ['remote_receiver']
BASE_SCHEMA = binary_sensor.BINARY_SENSOR_SCHEMA.extend({}, extra=cv.ALLOW_EXTRA)
CONFIG_SCHEMA = cv.nameable(cv.All(BASE_SCHEMA, remote_base.validate_binary_sensor(BASE_SCHEMA)))
def to_code(config):
var = yield remote_base.build_binary_sensor(config)
cg.add(var.set_name(config[CONF_NAME]))
yield binary_sensor.register_binary_sensor(var, config)

View File

@@ -0,0 +1,59 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/remote_base/remote_base.h"
namespace esphome {
namespace remote_receiver {
#ifdef ARDUINO_ARCH_ESP8266
struct RemoteReceiverComponentStore {
static void gpio_intr(RemoteReceiverComponentStore *arg);
/// Stores the time (in micros) that the leading/falling edge happened at
/// * An even index means a falling edge appeared at the time stored at the index
/// * An uneven index means a rising edge appeared at the time stored at the index
volatile uint32_t *buffer{nullptr};
/// The position last written to
volatile uint32_t buffer_write_at;
/// The position last read from
uint32_t buffer_read_at{0};
bool overflow{false};
uint32_t buffer_size{1000};
uint8_t filter_us{10};
ISRInternalGPIOPin *pin;
};
#endif
class RemoteReceiverComponent : public remote_base::RemoteReceiverBase, public Component {
public:
RemoteReceiverComponent(GPIOPin *pin) : RemoteReceiverBase(pin) {}
void setup() override;
void dump_config() override;
void loop() override;
float get_setup_priority() const override { return setup_priority::DATA; }
void set_buffer_size(uint32_t buffer_size) { this->buffer_size_ = buffer_size; }
void set_filter_us(uint8_t filter_us) { this->filter_us_ = filter_us; }
void set_idle_us(uint32_t idle_us) { this->idle_us_ = idle_us; }
protected:
#ifdef ARDUINO_ARCH_ESP32
void decode_rmt_(rmt_item32_t *item, size_t len);
#endif
#ifdef ARDUINO_ARCH_ESP32
RingbufHandle_t ringbuf_;
#endif
#ifdef ARDUINO_ARCH_ESP8266
RemoteReceiverComponentStore store_;
HighFrequencyLoopRequester high_freq_;
#endif
uint32_t buffer_size_{};
uint8_t filter_us_{10};
uint32_t idle_us_{10000};
};
} // namespace remote_receiver
} // namespace esphome

View File

@@ -0,0 +1,154 @@
#include "remote_receiver.h"
#include "esphome/core/log.h"
#ifdef ARDUINO_ARCH_ESP32
namespace esphome {
namespace remote_receiver {
static const char *TAG = "remote_receiver.esp32";
void RemoteReceiverComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up Remote Receiver...");
rmt_config_t rmt{};
rmt.channel = this->channel_;
rmt.gpio_num = gpio_num_t(this->pin_->get_pin());
rmt.clk_div = this->clock_divider_;
rmt.mem_block_num = 1;
rmt.rmt_mode = RMT_MODE_RX;
if (this->filter_us_ == 0) {
rmt.rx_config.filter_en = false;
} else {
rmt.rx_config.filter_en = true;
rmt.rx_config.filter_ticks_thresh = this->from_microseconds(this->filter_us_);
}
rmt.rx_config.idle_threshold = this->from_microseconds(this->idle_us_);
esp_err_t error = rmt_config(&rmt);
if (error != ESP_OK) {
this->error_code_ = error;
this->mark_failed();
return;
}
error = rmt_driver_install(this->channel_, this->buffer_size_, 0);
if (error != ESP_OK) {
this->error_code_ = error;
this->mark_failed();
return;
}
error = rmt_get_ringbuf_handle(this->channel_, &this->ringbuf_);
if (error != ESP_OK) {
this->error_code_ = error;
this->mark_failed();
return;
}
error = rmt_rx_start(this->channel_, true);
if (error != ESP_OK) {
this->error_code_ = error;
this->mark_failed();
return;
}
}
void RemoteReceiverComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Remote Receiver:");
LOG_PIN(" Pin: ", this->pin_);
ESP_LOGCONFIG(TAG, " Channel: %d", this->channel_);
ESP_LOGCONFIG(TAG, " Clock divider: %u", this->clock_divider_);
ESP_LOGCONFIG(TAG, " Tolerance: %u%%", this->tolerance_);
ESP_LOGCONFIG(TAG, " Filter out pulses shorter than: %u us", this->filter_us_);
ESP_LOGCONFIG(TAG, " Signal is done after %u us of no changes", this->idle_us_);
if (this->is_failed()) {
ESP_LOGE(TAG, "Configuring RMT driver failed: %s", esp_err_to_name(this->error_code_));
}
}
void RemoteReceiverComponent::loop() {
size_t len = 0;
auto *item = (rmt_item32_t *) xRingbufferReceive(this->ringbuf_, &len, 0);
if (item != nullptr) {
this->decode_rmt_(item, len);
vRingbufferReturnItem(this->ringbuf_, item);
if (this->temp_.empty())
return;
this->call_listeners_dumpers_();
}
}
void RemoteReceiverComponent::decode_rmt_(rmt_item32_t *item, size_t len) {
bool prev_level = false;
uint32_t prev_length = 0;
this->temp_.clear();
int32_t multiplier = this->pin_->is_inverted() ? -1 : 1;
ESP_LOGVV(TAG, "START:");
for (size_t i = 0; i < len; i++) {
if (item[i].level0) {
ESP_LOGVV(TAG, "%u A: ON %uus (%u ticks)", i, this->to_microseconds(item[i].duration0), item[i].duration0);
} else {
ESP_LOGVV(TAG, "%u A: OFF %uus (%u ticks)", i, this->to_microseconds(item[i].duration0), item[i].duration0);
}
if (item[i].level1) {
ESP_LOGVV(TAG, "%u B: ON %uus (%u ticks)", i, this->to_microseconds(item[i].duration1), item[i].duration1);
} else {
ESP_LOGVV(TAG, "%u B: OFF %uus (%u ticks)", i, this->to_microseconds(item[i].duration1), item[i].duration1);
}
}
ESP_LOGVV(TAG, "\n");
this->temp_.reserve(len / 4);
for (size_t i = 0; i < len; i++) {
if (item[i].duration0 == 0u) {
// Do nothing
} else if (bool(item[i].level0) == prev_level) {
prev_length += item[i].duration0;
} else {
if (prev_length > 0) {
if (prev_level) {
this->temp_.push_back(this->to_microseconds(prev_length) * multiplier);
} else {
this->temp_.push_back(-int32_t(this->to_microseconds(prev_length)) * multiplier);
}
}
prev_level = bool(item[i].level0);
prev_length = item[i].duration0;
}
if (this->to_microseconds(prev_length) > this->idle_us_) {
break;
}
if (item[i].duration1 == 0u) {
// Do nothing
} else if (bool(item[i].level1) == prev_level) {
prev_length += item[i].duration1;
} else {
if (prev_length > 0) {
if (prev_level) {
this->temp_.push_back(this->to_microseconds(prev_length) * multiplier);
} else {
this->temp_.push_back(-int32_t(this->to_microseconds(prev_length)) * multiplier);
}
}
prev_level = bool(item[i].level1);
prev_length = item[i].duration1;
}
if (this->to_microseconds(prev_length) > this->idle_us_) {
break;
}
}
if (prev_length > 0) {
if (prev_level) {
this->temp_.push_back(this->to_microseconds(prev_length) * multiplier);
} else {
this->temp_.push_back(-int32_t(this->to_microseconds(prev_length)) * multiplier);
}
}
}
} // namespace remote_receiver
} // namespace esphome
#endif

View File

@@ -0,0 +1,123 @@
#include "remote_receiver.h"
#include "esphome/core/log.h"
#include "esphome/core/helpers.h"
#ifdef ARDUINO_ARCH_ESP8266
namespace esphome {
namespace remote_receiver {
static const char *TAG = "remote_receiver.esp8266";
void ICACHE_RAM_ATTR HOT RemoteReceiverComponentStore::gpio_intr(RemoteReceiverComponentStore *arg) {
const uint32_t now = micros();
// If the lhs is 1 (rising edge) we should write to an uneven index and vice versa
const uint32_t next = (arg->buffer_write_at + 1) % arg->buffer_size;
if (uint32_t(arg->pin->digital_read()) != next % 2)
return;
const uint32_t last_change = arg->buffer[arg->buffer_write_at];
if (now - last_change <= arg->filter_us)
return;
arg->buffer[arg->buffer_write_at = next] = now;
if (next == arg->buffer_read_at) {
arg->overflow = true;
}
}
void RemoteReceiverComponent::setup() {
ESP_LOGCONFIG(TAG, "Setting up Remote Receiver...");
this->pin_->setup();
auto &s = this->store_;
s.filter_us = this->filter_us_;
s.pin = this->pin_->to_isr();
s.buffer_size = this->buffer_size_;
this->high_freq_.start();
if (s.buffer_size % 2 != 0) {
// Make sure divisible by two. This way, we know that every 0bxxx0 index is a space and every 0bxxx1 index is a mark
s.buffer_size++;
}
s.buffer = new uint32_t[s.buffer_size];
// First index is a space.
if (this->pin_->digital_read()) {
s.buffer_write_at = s.buffer_read_at = 1;
s.buffer[1] = 0;
s.buffer[0] = 0;
} else {
s.buffer_write_at = s.buffer_read_at = 0;
s.buffer[0] = 0;
}
this->pin_->attach_interrupt(RemoteReceiverComponentStore::gpio_intr, &this->store_, CHANGE);
}
void RemoteReceiverComponent::dump_config() {
ESP_LOGCONFIG(TAG, "Remote Receiver:");
LOG_PIN(" Pin: ", this->pin_);
if (this->pin_->digital_read()) {
ESP_LOGW(TAG, "Remote Receiver Signal starts with a HIGH value. Usually this means you have to "
"invert the signal using 'inverted: True' in the pin schema!");
}
ESP_LOGCONFIG(TAG, " Buffer Size: %u", this->buffer_size_);
ESP_LOGCONFIG(TAG, " Tolerance: %u%%", this->tolerance_);
ESP_LOGCONFIG(TAG, " Filter out pulses shorter than: %u us", this->filter_us_);
ESP_LOGCONFIG(TAG, " Signal is done after %u us of no changes", this->idle_us_);
}
void RemoteReceiverComponent::loop() {
auto &s = this->store_;
if (s.overflow) {
s.buffer_read_at = s.buffer_write_at;
s.overflow = false;
ESP_LOGW(TAG, "Data is coming in too fast! Try increasing the buffer size.");
return;
}
// copy write at to local variables, as it's volatile
const uint32_t write_at = s.buffer_write_at;
const uint32_t dist = (s.buffer_size + write_at - s.buffer_read_at) % s.buffer_size;
// signals must at least one rising and one leading edge
if (dist <= 1)
return;
const uint32_t now = micros();
if (now - s.buffer[write_at] < this->idle_us_)
// The last change was fewer than the configured idle time ago.
// TODO: Handle case when loop() is not called quickly enough to catch idle
return;
ESP_LOGVV(TAG, "read_at=%u write_at=%u dist=%u now=%u end=%u", s.buffer_read_at, write_at, dist, now,
s.buffer[write_at]);
// Skip first value, it's from the previous idle level
s.buffer_read_at = (s.buffer_read_at + 1) % s.buffer_size;
uint32_t prev = s.buffer_read_at;
s.buffer_read_at = (s.buffer_read_at + 1) % s.buffer_size;
const uint32_t reserve_size = 1 + (s.buffer_size + write_at - s.buffer_read_at) % s.buffer_size;
this->temp_.clear();
this->temp_.reserve(reserve_size);
int32_t multiplier = s.buffer_read_at % 2 == 0 ? 1 : -1;
for (uint32_t i = 0; prev != write_at; i++) {
int32_t delta = s.buffer[s.buffer_read_at] - s.buffer[prev];
if (uint32_t(delta) >= this->idle_us_) {
// already found a space longer than idle. There must have been two pulses
break;
}
ESP_LOGVV(TAG, " i=%u buffer[%u]=%u - buffer[%u]=%u -> %d", i, s.buffer_read_at, s.buffer[s.buffer_read_at], prev,
s.buffer[prev], multiplier * delta);
this->temp_.push_back(multiplier * delta);
prev = s.buffer_read_at;
s.buffer_read_at = (s.buffer_read_at + 1) % s.buffer_size;
multiplier *= -1;
}
s.buffer_read_at = (s.buffer_size + s.buffer_read_at - 1) % s.buffer_size;
this->temp_.push_back(this->idle_us_ * multiplier);
this->call_listeners_dumpers_();
}
} // namespace remote_receiver
} // namespace esphome
#endif