diff --git a/esphome/components/light/addressable_light.cpp b/esphome/components/light/addressable_light.cpp index 641b0b96a8..bbb475d534 100644 --- a/esphome/components/light/addressable_light.cpp +++ b/esphome/components/light/addressable_light.cpp @@ -85,8 +85,46 @@ ESPColorView ESPRangeView::operator[](int32_t index) const { index = interpret_index(index, this->size()); return (*this->parent_)[index]; } +ESPRangeIterator ESPRangeView::begin() { return {*this, this->begin_}; } +ESPRangeIterator ESPRangeView::end() { return {*this, this->end_}; } +void ESPRangeView::set_red(uint8_t red) { + for (auto c : *this) + c.set_red(red); +} +void ESPRangeView::set_green(uint8_t green) { + for (auto c : *this) + c.set_green(green); +} +void ESPRangeView::set_blue(uint8_t blue) { + for (auto c : *this) + c.set_blue(blue); +} +void ESPRangeView::set_white(uint8_t white) { + for (auto c : *this) + c.set_white(white); +} +void ESPRangeView::set_effect_data(uint8_t effect_data) { + for (auto c : *this) + c.set_effect_data(effect_data); +} +void ESPRangeView::fade_to_white(uint8_t amnt) { + for (auto c : *this) + c.fade_to_white(amnt); +} +void ESPRangeView::fade_to_black(uint8_t amnt) { + for (auto c : *this) + c.fade_to_white(amnt); +} +void ESPRangeView::lighten(uint8_t delta) { + for (auto c : *this) + c.lighten(delta); +} +void ESPRangeView::darken(uint8_t delta) { + for (auto c : *this) + c.darken(delta); +} -ESPColorView ESPRangeView::Iterator::operator*() const { return (*this->range_->parent_)[this->i_]; } +ESPColorView ESPRangeIterator::operator*() const { return this->range_.parent_->get(this->i_); } int32_t HOT interpret_index(int32_t index, int32_t size) { if (index < 0) diff --git a/esphome/components/light/addressable_light.h b/esphome/components/light/addressable_light.h index b008b0c04c..a933ca3dea 100644 --- a/esphome/components/light/addressable_light.h +++ b/esphome/components/light/addressable_light.h @@ -372,23 +372,10 @@ class AddressableLight; int32_t interpret_index(int32_t index, int32_t size); +class ESPRangeIterator; + class ESPRangeView : public ESPColorSettable { public: - class Iterator { - public: - Iterator(ESPRangeView *range, int32_t i) : range_(range), i_(i) {} - Iterator operator++() { - this->i_++; - return *this; - } - bool operator!=(const Iterator &other) const { return this->i_ != other.i_; } - ESPColorView operator*() const; - - protected: - ESPRangeView *range_; - int32_t i_; - }; - ESPRangeView(AddressableLight *parent, int32_t begin, int32_t an_end) : parent_(parent), begin_(begin), end_(an_end) { if (this->end_ < this->begin_) { this->end_ = this->begin_; @@ -396,8 +383,8 @@ class ESPRangeView : public ESPColorSettable { } ESPColorView operator[](int32_t index) const; - Iterator begin() { return {this, this->begin_}; } - Iterator end() { return {this, this->end_}; } + ESPRangeIterator begin(); + ESPRangeIterator end(); void set(const ESPColor &color) override; ESPRangeView &operator=(const ESPColor &rhs) { @@ -439,50 +426,40 @@ class ESPRangeView : public ESPColorSettable { return *this; } - void set_red(uint8_t red) override { - for (auto c : *this) - c.set_red(red); - } - void set_green(uint8_t green) override { - for (auto c : *this) - c.set_green(green); - } - void set_blue(uint8_t blue) override { - for (auto c : *this) - c.set_blue(blue); - } - void set_white(uint8_t white) override { - for (auto c : *this) - c.set_white(white); - } - void set_effect_data(uint8_t effect_data) override { - for (auto c : *this) - c.set_effect_data(effect_data); - } - void fade_to_white(uint8_t amnt) override { - for (auto c : *this) - c.fade_to_white(amnt); - } - void fade_to_black(uint8_t amnt) override { - for (auto c : *this) - c.fade_to_white(amnt); - } - void lighten(uint8_t delta) override { - for (auto c : *this) - c.lighten(delta); - } - void darken(uint8_t delta) override { - for (auto c : *this) - c.darken(delta); - } + void set_red(uint8_t red) override; + void set_green(uint8_t green) override; + void set_blue(uint8_t blue) override; + void set_white(uint8_t white) override; + void set_effect_data(uint8_t effect_data) override; + void fade_to_white(uint8_t amnt) override; + void fade_to_black(uint8_t amnt) override; + void lighten(uint8_t delta) override; + void darken(uint8_t delta) override; int32_t size() const { return this->end_ - this->begin_; } protected: + friend ESPRangeIterator; + AddressableLight *parent_; int32_t begin_; int32_t end_; }; +class ESPRangeIterator { + public: + ESPRangeIterator(const ESPRangeView &range, int32_t i) : range_(range), i_(i) {} + ESPRangeIterator operator++() { + this->i_++; + return *this; + } + bool operator!=(const ESPRangeIterator &other) const { return this->i_ != other.i_; } + ESPColorView operator*() const; + + protected: + ESPRangeView range_; + int32_t i_; +}; + class AddressableLight : public LightOutput, public Component { public: virtual int32_t size() const = 0; @@ -495,8 +472,8 @@ class AddressableLight : public LightOutput, public Component { return ESPRangeView(this, from, to); } ESPRangeView all() { return ESPRangeView(this, 0, this->size()); } - ESPRangeView::Iterator begin() { return this->all().begin(); } - ESPRangeView::Iterator end() { return this->all().end(); } + ESPRangeIterator begin() { return this->all().begin(); } + ESPRangeIterator end() { return this->all().end(); } void shift_left(int32_t amnt) { if (amnt < 0) { this->shift_right(-amnt); diff --git a/esphome/components/light/addressable_light_effect.h b/esphome/components/light/addressable_light_effect.h index 545af6a0f2..edde7ad626 100644 --- a/esphome/components/light/addressable_light_effect.h +++ b/esphome/components/light/addressable_light_effect.h @@ -308,14 +308,15 @@ class AddressableFlickerEffect : public AddressableLightEffect { explicit AddressableFlickerEffect(const std::string &name) : AddressableLightEffect(name) {} void apply(AddressableLight &it, const ESPColor ¤t_color) override { const uint32_t now = millis(); - const uint8_t delta_intensity = 255 - this->intensity_; + const uint8_t intensity = this->intensity_; + const uint8_t inv_intensity = 255 - intensity; if (now - this->last_update_ < this->update_interval_) return; this->last_update_ = now; fast_random_set_seed(random_uint32()); for (auto var : it) { - const uint8_t flicker = fast_random_8() % this->intensity_; - var = (var.get() * delta_intensity) + (current_color * flicker); + const uint8_t flicker = fast_random_8() % intensity; + var = (var.get() * inv_intensity) + (current_color * flicker); } } void set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; } diff --git a/esphome/components/wifi/wifi_component_esp8266.cpp b/esphome/components/wifi/wifi_component_esp8266.cpp index 11981b801b..f1cef045f8 100644 --- a/esphome/components/wifi/wifi_component_esp8266.cpp +++ b/esphome/components/wifi/wifi_component_esp8266.cpp @@ -476,6 +476,7 @@ void WiFiComponent::wifi_scan_done_callback_(void *arg, STATUS status) { if (status != OK) { ESP_LOGV(TAG, "Scan failed! %d", status); + this->retry_connect(); return; } auto *head = reinterpret_cast(arg);