1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-18 19:22: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

@@ -0,0 +1,46 @@
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.components import light, power_supply
from esphome.const import CONF_OUTPUT_ID, CONF_NUM_LEDS, CONF_RGB_ORDER, CONF_MAX_REFRESH_RATE, \
CONF_POWER_SUPPLY
from esphome.core import coroutine
fastled_base_ns = cg.esphome_ns.namespace('fastled_base')
FastLEDLightOutput = fastled_base_ns.class_('FastLEDLightOutput', cg.Component,
light.AddressableLight)
RGB_ORDERS = [
'RGB',
'RBG',
'GRB',
'GBR',
'BRG',
'BGR',
]
BASE_SCHEMA = light.ADDRESSABLE_LIGHT_SCHEMA.extend({
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_variable_id(FastLEDLightOutput),
cv.Required(CONF_NUM_LEDS): cv.positive_not_null_int,
cv.Optional(CONF_RGB_ORDER): cv.one_of(*RGB_ORDERS, upper=True),
cv.Optional(CONF_MAX_REFRESH_RATE): cv.positive_time_period_microseconds,
cv.Optional(CONF_POWER_SUPPLY): cv.use_variable_id(power_supply.PowerSupply),
}).extend(cv.COMPONENT_SCHEMA)
@coroutine
def new_fastled_light(config):
var = cg.new_Pvariable(config[CONF_OUTPUT_ID])
yield cg.register_component(var, config)
if CONF_MAX_REFRESH_RATE in config:
cg.add(var.set_max_refresh_rate(config[CONF_MAX_REFRESH_RATE]))
if CONF_POWER_SUPPLY in config:
var_ = yield cg.get_variable(config[CONF_POWER_SUPPLY])
cg.add(var.set_power_supply(var_))
yield light.register_light(var, config)
cg.add_library('FastLED', '3.2.0')
yield var

View File

@@ -0,0 +1,61 @@
#include "fastled_light.h"
#include "esphome/core/log.h"
namespace esphome {
namespace fastled_base {
static const char *TAG = "fastled";
void FastLEDLightOutput::setup() {
ESP_LOGCONFIG(TAG, "Setting up FastLED light...");
this->controller_->init();
this->controller_->setLeds(this->leds_, this->num_leds_);
this->effect_data_ = new uint8_t[this->num_leds_];
if (!this->max_refresh_rate_.has_value()) {
this->set_max_refresh_rate(this->controller_->getMaxRefreshRate());
}
}
void FastLEDLightOutput::dump_config() {
ESP_LOGCONFIG(TAG, "FastLED light:");
ESP_LOGCONFIG(TAG, " Num LEDs: %u", this->num_leds_);
ESP_LOGCONFIG(TAG, " Max refresh rate: %u", *this->max_refresh_rate_);
}
void FastLEDLightOutput::loop() {
if (!this->should_show_())
return;
uint32_t now = micros();
// protect from refreshing too often
if (*this->max_refresh_rate_ != 0 && (now - this->last_refresh_) < *this->max_refresh_rate_) {
return;
}
this->last_refresh_ = now;
this->mark_shown_();
ESP_LOGVV(TAG, "Writing RGB values to bus...");
#ifdef USE_POWER_SUPPLY
if (this->power_supply_ != nullptr) {
bool is_on = false;
for (int i = 0; i < this->num_leds_; i++) {
if (bool(this->leds_[i])) {
is_on = true;
break;
}
}
if (is_on && !this->has_requested_high_power_) {
this->power_supply_->request_high_power();
this->has_requested_high_power_ = true;
}
if (!is_on && this->has_requested_high_power_) {
this->power_supply_->unrequest_high_power();
this->has_requested_high_power_ = false;
}
}
#endif
this->controller_->showLeds();
}
} // namespace fastled_base
} // namespace esphome

View File

@@ -0,0 +1,252 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/helpers.h"
#include "esphome/components/light/addressable_light.h"
#ifdef USE_POWER_SUPPLY
#include "esphome/components/power_supply/power_supply.h"
#endif
#define FASTLED_ESP8266_RAW_PIN_ORDER
#define FASTLED_ESP32_RAW_PIN_ORDER
#define FASTLED_RMT_BUILTIN_DRIVER true
// Avoid annoying compiler messages
#define FASTLED_INTERNAL
#include "FastLED.h"
namespace esphome {
namespace fastled_base {
class FastLEDLightOutput : public Component, public light::AddressableLight {
public:
/// Only for custom effects: Get the internal controller.
CLEDController *get_controller() const { return this->controller_; }
inline int32_t size() const override { return this->num_leds_; }
inline light::ESPColorView operator[](int32_t index) const override {
return light::ESPColorView(&this->leds_[index].r, &this->leds_[index].g, &this->leds_[index].b, nullptr,
&this->effect_data_[index], &this->correction_);
}
/// Set a maximum refresh rate in µs as some lights do not like being updated too often.
void set_max_refresh_rate(uint32_t interval_us) { this->max_refresh_rate_ = interval_us; }
#ifdef USE_POWER_SUPPLY
void set_power_supply(power_supply::PowerSupply *power_supply) { this->power_supply_ = power_supply; }
#endif
/// Add some LEDS, can only be called once.
CLEDController &add_leds(CLEDController *controller, int num_leds) {
this->controller_ = controller;
this->num_leds_ = num_leds;
this->leds_ = new CRGB[num_leds];
for (int i = 0; i < this->num_leds_; i++)
this->leds_[i] = CRGB::Black;
return *this->controller_;
}
template<ESPIChipsets CHIPSET, uint8_t DATA_PIN, uint8_t CLOCK_PIN, EOrder RGB_ORDER, uint8_t SPI_DATA_RATE>
CLEDController &add_leds(int num_leds) {
switch (CHIPSET) {
case LPD8806: {
static LPD8806Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case WS2801: {
static WS2801Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case WS2803: {
static WS2803Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case SM16716: {
static SM16716Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case P9813: {
static P9813Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case DOTSTAR:
case APA102: {
static APA102Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case SK9822: {
static SK9822Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER, SPI_DATA_RATE> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
}
}
template<ESPIChipsets CHIPSET, uint8_t DATA_PIN, uint8_t CLOCK_PIN> CLEDController &add_leds(int num_leds) {
switch (CHIPSET) {
case LPD8806: {
static LPD8806Controller<DATA_PIN, CLOCK_PIN> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case WS2801: {
static WS2801Controller<DATA_PIN, CLOCK_PIN> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case WS2803: {
static WS2803Controller<DATA_PIN, CLOCK_PIN> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case SM16716: {
static SM16716Controller<DATA_PIN, CLOCK_PIN> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case P9813: {
static P9813Controller<DATA_PIN, CLOCK_PIN> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case DOTSTAR:
case APA102: {
static APA102Controller<DATA_PIN, CLOCK_PIN> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case SK9822: {
static SK9822Controller<DATA_PIN, CLOCK_PIN> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
}
}
template<ESPIChipsets CHIPSET, uint8_t DATA_PIN, uint8_t CLOCK_PIN, EOrder RGB_ORDER>
CLEDController &add_leds(int num_leds) {
switch (CHIPSET) {
case LPD8806: {
static LPD8806Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case WS2801: {
static WS2801Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case WS2803: {
static WS2803Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case SM16716: {
static SM16716Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case P9813: {
static P9813Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case DOTSTAR:
case APA102: {
static APA102Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
case SK9822: {
static SK9822Controller<DATA_PIN, CLOCK_PIN, RGB_ORDER> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
}
}
#ifdef FASTLED_HAS_CLOCKLESS
template<template<uint8_t DATA_PIN, EOrder RGB_ORDER> class CHIPSET, uint8_t DATA_PIN, EOrder RGB_ORDER>
CLEDController &add_leds(int num_leds) {
static CHIPSET<DATA_PIN, RGB_ORDER> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
template<template<uint8_t DATA_PIN, EOrder RGB_ORDER> class CHIPSET, uint8_t DATA_PIN>
CLEDController &add_leds(int num_leds) {
static CHIPSET<DATA_PIN, RGB> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
template<template<uint8_t DATA_PIN> class CHIPSET, uint8_t DATA_PIN> CLEDController &add_leds(int num_leds) {
static CHIPSET<DATA_PIN> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
#endif
template<template<EOrder RGB_ORDER> class CHIPSET, EOrder RGB_ORDER> CLEDController &add_leds(int num_leds) {
static CHIPSET<RGB_ORDER> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
template<template<EOrder RGB_ORDER> class CHIPSET> CLEDController &add_leds(int num_leds) {
static CHIPSET<RGB> CONTROLLER;
return add_leds(&CONTROLLER, num_leds);
}
#ifdef FASTLED_HAS_BLOCKLESS
template<EBlockChipsets CHIPSET, int NUM_LANES, EOrder RGB_ORDER> CLEDController &add_leds(int num_leds) {
switch (CHIPSET) {
#ifdef PORTA_FIRST_PIN
case WS2811_PORTA:
return add_leds(
new InlineBlockClocklessController<NUM_LANES, PORTA_FIRST_PIN, NS(320), NS(320), NS(640), RGB_ORDER>(),
num_leds);
case WS2811_400_PORTA:
return add_leds(
new InlineBlockClocklessController<NUM_LANES, PORTA_FIRST_PIN, NS(800), NS(800), NS(900), RGB_ORDER>(),
num_leds);
case WS2813_PORTA:
return add_leds(new InlineBlockClocklessController<NUM_LANES, PORTA_FIRST_PIN, NS(320), NS(320), NS(640),
RGB_ORDER, 0, false, 300>(),
num_leds);
case TM1803_PORTA:
return add_leds(
new InlineBlockClocklessController<NUM_LANES, PORTA_FIRST_PIN, NS(700), NS(1100), NS(700), RGB_ORDER>(),
num_leds);
case UCS1903_PORTA:
return add_leds(
new InlineBlockClocklessController<NUM_LANES, PORTA_FIRST_PIN, NS(500), NS(1500), NS(500), RGB_ORDER>(),
num_leds);
#endif
}
}
template<EBlockChipsets CHIPSET, int NUM_LANES> CLEDController &add_leds(int num_leds) {
return add_leds<CHIPSET, NUM_LANES, GRB>(num_leds);
}
#endif
// ========== INTERNAL METHODS ==========
// (In most use cases you won't need these)
light::LightTraits get_traits() override {
auto traits = light::LightTraits();
traits.set_supports_brightness(true);
traits.set_supports_rgb(true);
return traits;
}
void setup() override;
void dump_config() override;
void loop() override;
float get_setup_priority() const override { return setup_priority::HARDWARE; }
void clear_effect_data() override {
for (int i = 0; i < this->size(); i++)
this->effect_data_[i] = 0;
}
protected:
CLEDController *controller_{nullptr};
CRGB *leds_{nullptr};
uint8_t *effect_data_{nullptr};
int num_leds_{0};
uint32_t last_refresh_{0};
optional<uint32_t> max_refresh_rate_{};
#ifdef USE_POWER_SUPPLY
power_supply::PowerSupply *power_supply_{nullptr};
bool has_requested_high_power_{false};
#endif
};
} // namespace fastled_base
} // namespace esphome