From a72c3ea9d736d1145fe9df4592e64498b1127465 Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Fri, 31 May 2019 13:38:56 +0200 Subject: [PATCH] Fix light partition (#584) * Fix light partition Fixes https://github.com/esphome/issues/issues/365 * Lint --- esphome/components/fastled_base/__init__.py | 3 +-- .../components/fastled_base/fastled_light.h | 2 +- .../components/light/addressable_light.cpp | 21 +++++++++++++++++++ esphome/components/light/addressable_light.h | 18 ++++++++++++++-- esphome/components/light/types.py | 2 +- esphome/components/neopixelbus/light.py | 2 +- .../neopixelbus/neopixelbus_light.h | 2 +- esphome/components/partition/light.py | 1 + .../components/partition/light_partition.h | 2 +- 9 files changed, 44 insertions(+), 9 deletions(-) diff --git a/esphome/components/fastled_base/__init__.py b/esphome/components/fastled_base/__init__.py index aad30198af..7354f9ae9f 100644 --- a/esphome/components/fastled_base/__init__.py +++ b/esphome/components/fastled_base/__init__.py @@ -5,8 +5,7 @@ from esphome.const import CONF_OUTPUT_ID, CONF_NUM_LEDS, CONF_RGB_ORDER, CONF_MA from esphome.core import coroutine fastled_base_ns = cg.esphome_ns.namespace('fastled_base') -FastLEDLightOutput = fastled_base_ns.class_('FastLEDLightOutput', cg.Component, - light.AddressableLight) +FastLEDLightOutput = fastled_base_ns.class_('FastLEDLightOutput', light.AddressableLight) RGB_ORDERS = [ 'RGB', diff --git a/esphome/components/fastled_base/fastled_light.h b/esphome/components/fastled_base/fastled_light.h index f9ae2f58d5..0729941e31 100644 --- a/esphome/components/fastled_base/fastled_light.h +++ b/esphome/components/fastled_base/fastled_light.h @@ -16,7 +16,7 @@ namespace esphome { namespace fastled_base { -class FastLEDLightOutput : public Component, public light::AddressableLight { +class FastLEDLightOutput : public light::AddressableLight { public: /// Only for custom effects: Get the internal controller. CLEDController *get_controller() const { return this->controller_; } diff --git a/esphome/components/light/addressable_light.cpp b/esphome/components/light/addressable_light.cpp index 640d8112b1..641b0b96a8 100644 --- a/esphome/components/light/addressable_light.cpp +++ b/esphome/components/light/addressable_light.cpp @@ -4,6 +4,8 @@ namespace esphome { namespace light { +static const char *TAG = "light.addressable"; + const ESPColor ESPColor::BLACK = ESPColor(0, 0, 0, 0); const ESPColor ESPColor::WHITE = ESPColor(255, 255, 255, 255); @@ -92,5 +94,24 @@ int32_t HOT interpret_index(int32_t index, int32_t size) { return index; } +void AddressableLight::call_setup() { + this->setup_internal_(); + this->setup(); + +#ifdef ESPHOME_LOG_HAS_VERY_VERBOSE + this->set_interval(5000, [this]() { + const char *name = this->state_parent_ == nullptr ? "" : this->state_parent_->get_name().c_str(); + ESP_LOGVV(TAG, "Addressable Light '%s' (effect_active=%s next_show=%s)", name, YESNO(this->effect_active_), + YESNO(this->next_show_)); + for (int i = 0; i < this->size(); i++) { + auto color = this->get(i); + ESP_LOGVV(TAG, " [%2d] Color: R=%3u G=%3u B=%3u W=%3u", i, color.get_red_raw(), color.get_green_raw(), + color.get_blue_raw(), color.get_white_raw()); + } + ESP_LOGVV(TAG, ""); + }); +#endif +} + } // namespace light } // namespace esphome diff --git a/esphome/components/light/addressable_light.h b/esphome/components/light/addressable_light.h index b4249097db..b008b0c04c 100644 --- a/esphome/components/light/addressable_light.h +++ b/esphome/components/light/addressable_light.h @@ -335,13 +335,21 @@ class ESPColorView : public ESPColorSettable { void darken(uint8_t delta) override { this->set(this->get().darken(delta)); } ESPColor get() const { return ESPColor(this->get_red(), this->get_green(), this->get_blue(), this->get_white()); } uint8_t get_red() const { return this->color_correction_->color_uncorrect_red(*this->red_); } + uint8_t get_red_raw() const { return *this->red_; } uint8_t get_green() const { return this->color_correction_->color_uncorrect_green(*this->green_); } + uint8_t get_green_raw() const { return *this->green_; } uint8_t get_blue() const { return this->color_correction_->color_uncorrect_blue(*this->blue_); } + uint8_t get_blue_raw() const { return *this->blue_; } uint8_t get_white() const { if (this->white_ == nullptr) return 0; return this->color_correction_->color_uncorrect_white(*this->white_); } + uint8_t get_white_raw() const { + if (this->white_ == nullptr) + return 0; + return *this->white_; + } uint8_t get_effect_data() const { if (this->effect_data_ == nullptr) return 0; @@ -475,7 +483,7 @@ class ESPRangeView : public ESPColorSettable { int32_t end_; }; -class AddressableLight : public LightOutput { +class AddressableLight : public LightOutput, public Component { public: virtual int32_t size() const = 0; ESPColorView operator[](int32_t index) const { return this->get_view_internal(interpret_index(index, this->size())); } @@ -530,13 +538,18 @@ class AddressableLight : public LightOutput { this->correction_.set_max_brightness(ESPColor(uint8_t(roundf(red * 255.0f)), uint8_t(roundf(green * 255.0f)), uint8_t(roundf(blue * 255.0f)), uint8_t(roundf(white * 255.0f)))); } - void setup_state(LightState *state) override { this->correction_.calculate_gamma_table(state->get_gamma_correct()); } + void setup_state(LightState *state) override { + this->correction_.calculate_gamma_table(state->get_gamma_correct()); + this->state_parent_ = state; + } void schedule_show() { this->next_show_ = true; } #ifdef USE_POWER_SUPPLY void set_power_supply(power_supply::PowerSupply *power_supply) { this->power_.set_parent(power_supply); } #endif + void call_setup() override; + protected: bool should_show_() const { return this->effect_active_ || this->next_show_; } void mark_shown_() { @@ -559,6 +572,7 @@ class AddressableLight : public LightOutput { #ifdef USE_POWER_SUPPLY power_supply::PowerSupplyRequester power_; #endif + LightState *state_parent_{nullptr}; }; } // namespace light diff --git a/esphome/components/light/types.py b/esphome/components/light/types.py index 9be6cc8319..185105831d 100644 --- a/esphome/components/light/types.py +++ b/esphome/components/light/types.py @@ -7,7 +7,7 @@ LightState = light_ns.class_('LightState', cg.Nameable, cg.Component) # Fake class for addressable lights AddressableLightState = light_ns.class_('LightState', LightState) LightOutput = light_ns.class_('LightOutput') -AddressableLight = light_ns.class_('AddressableLight') +AddressableLight = light_ns.class_('AddressableLight', cg.Component) AddressableLightRef = AddressableLight.operator('ref') LightColorValues = light_ns.class_('LightColorValues') diff --git a/esphome/components/neopixelbus/light.py b/esphome/components/neopixelbus/light.py index f07bdc80d7..694ac028fc 100644 --- a/esphome/components/neopixelbus/light.py +++ b/esphome/components/neopixelbus/light.py @@ -7,7 +7,7 @@ from esphome.const import CONF_CLOCK_PIN, CONF_DATA_PIN, CONF_METHOD, CONF_NUM_L from esphome.core import CORE neopixelbus_ns = cg.esphome_ns.namespace('neopixelbus') -NeoPixelBusLightOutputBase = neopixelbus_ns.class_('NeoPixelBusLightOutputBase', cg.Component, +NeoPixelBusLightOutputBase = neopixelbus_ns.class_('NeoPixelBusLightOutputBase', light.AddressableLight) NeoPixelRGBLightOutput = neopixelbus_ns.class_('NeoPixelRGBLightOutput', NeoPixelBusLightOutputBase) NeoPixelRGBWLightOutput = neopixelbus_ns.class_('NeoPixelRGBWLightOutput', diff --git a/esphome/components/neopixelbus/neopixelbus_light.h b/esphome/components/neopixelbus/neopixelbus_light.h index 25e10d9bbd..5e8097187e 100644 --- a/esphome/components/neopixelbus/neopixelbus_light.h +++ b/esphome/components/neopixelbus/neopixelbus_light.h @@ -48,7 +48,7 @@ enum class ESPNeoPixelOrder { }; template -class NeoPixelBusLightOutputBase : public Component, public light::AddressableLight { +class NeoPixelBusLightOutputBase : public light::AddressableLight { public: NeoPixelBus *get_controller() const { return this->controller_; } diff --git a/esphome/components/partition/light.py b/esphome/components/partition/light.py index cfc709854f..62434e7a19 100644 --- a/esphome/components/partition/light.py +++ b/esphome/components/partition/light.py @@ -33,4 +33,5 @@ def to_code(config): conf[CONF_TO] - conf[CONF_FROM] + 1)) var = cg.new_Pvariable(config[CONF_OUTPUT_ID], segments) + yield cg.register_component(var, config) yield light.register_light(var, config) diff --git a/esphome/components/partition/light_partition.h b/esphome/components/partition/light_partition.h index b68e3404f1..8085c43720 100644 --- a/esphome/components/partition/light_partition.h +++ b/esphome/components/partition/light_partition.h @@ -24,7 +24,7 @@ class AddressableSegment { int32_t dst_offset_; }; -class PartitionLightOutput : public light::AddressableLight, public Component { +class PartitionLightOutput : public light::AddressableLight { public: explicit PartitionLightOutput(std::vector segments) : segments_(segments) { int32_t off = 0;