mirror of
https://github.com/esphome/esphome.git
synced 2024-11-23 07:28:10 +01:00
Fix addressable effects (#590)
This commit is contained in:
parent
4e520d13dd
commit
f921997ee6
4 changed files with 76 additions and 59 deletions
|
@ -85,8 +85,46 @@ ESPColorView ESPRangeView::operator[](int32_t index) const {
|
||||||
index = interpret_index(index, this->size());
|
index = interpret_index(index, this->size());
|
||||||
return (*this->parent_)[index];
|
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) {
|
int32_t HOT interpret_index(int32_t index, int32_t size) {
|
||||||
if (index < 0)
|
if (index < 0)
|
||||||
|
|
|
@ -372,23 +372,10 @@ class AddressableLight;
|
||||||
|
|
||||||
int32_t interpret_index(int32_t index, int32_t size);
|
int32_t interpret_index(int32_t index, int32_t size);
|
||||||
|
|
||||||
|
class ESPRangeIterator;
|
||||||
|
|
||||||
class ESPRangeView : public ESPColorSettable {
|
class ESPRangeView : public ESPColorSettable {
|
||||||
public:
|
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) {
|
ESPRangeView(AddressableLight *parent, int32_t begin, int32_t an_end) : parent_(parent), begin_(begin), end_(an_end) {
|
||||||
if (this->end_ < this->begin_) {
|
if (this->end_ < this->begin_) {
|
||||||
this->end_ = this->begin_;
|
this->end_ = this->begin_;
|
||||||
|
@ -396,8 +383,8 @@ class ESPRangeView : public ESPColorSettable {
|
||||||
}
|
}
|
||||||
|
|
||||||
ESPColorView operator[](int32_t index) const;
|
ESPColorView operator[](int32_t index) const;
|
||||||
Iterator begin() { return {this, this->begin_}; }
|
ESPRangeIterator begin();
|
||||||
Iterator end() { return {this, this->end_}; }
|
ESPRangeIterator end();
|
||||||
|
|
||||||
void set(const ESPColor &color) override;
|
void set(const ESPColor &color) override;
|
||||||
ESPRangeView &operator=(const ESPColor &rhs) {
|
ESPRangeView &operator=(const ESPColor &rhs) {
|
||||||
|
@ -439,50 +426,40 @@ class ESPRangeView : public ESPColorSettable {
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
void set_red(uint8_t red) override {
|
void set_red(uint8_t red) override;
|
||||||
for (auto c : *this)
|
void set_green(uint8_t green) override;
|
||||||
c.set_red(red);
|
void set_blue(uint8_t blue) override;
|
||||||
}
|
void set_white(uint8_t white) override;
|
||||||
void set_green(uint8_t green) override {
|
void set_effect_data(uint8_t effect_data) override;
|
||||||
for (auto c : *this)
|
void fade_to_white(uint8_t amnt) override;
|
||||||
c.set_green(green);
|
void fade_to_black(uint8_t amnt) override;
|
||||||
}
|
void lighten(uint8_t delta) override;
|
||||||
void set_blue(uint8_t blue) override {
|
void darken(uint8_t delta) 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);
|
|
||||||
}
|
|
||||||
int32_t size() const { return this->end_ - this->begin_; }
|
int32_t size() const { return this->end_ - this->begin_; }
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
friend ESPRangeIterator;
|
||||||
|
|
||||||
AddressableLight *parent_;
|
AddressableLight *parent_;
|
||||||
int32_t begin_;
|
int32_t begin_;
|
||||||
int32_t end_;
|
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 {
|
class AddressableLight : public LightOutput, public Component {
|
||||||
public:
|
public:
|
||||||
virtual int32_t size() const = 0;
|
virtual int32_t size() const = 0;
|
||||||
|
@ -495,8 +472,8 @@ class AddressableLight : public LightOutput, public Component {
|
||||||
return ESPRangeView(this, from, to);
|
return ESPRangeView(this, from, to);
|
||||||
}
|
}
|
||||||
ESPRangeView all() { return ESPRangeView(this, 0, this->size()); }
|
ESPRangeView all() { return ESPRangeView(this, 0, this->size()); }
|
||||||
ESPRangeView::Iterator begin() { return this->all().begin(); }
|
ESPRangeIterator begin() { return this->all().begin(); }
|
||||||
ESPRangeView::Iterator end() { return this->all().end(); }
|
ESPRangeIterator end() { return this->all().end(); }
|
||||||
void shift_left(int32_t amnt) {
|
void shift_left(int32_t amnt) {
|
||||||
if (amnt < 0) {
|
if (amnt < 0) {
|
||||||
this->shift_right(-amnt);
|
this->shift_right(-amnt);
|
||||||
|
|
|
@ -308,14 +308,15 @@ class AddressableFlickerEffect : public AddressableLightEffect {
|
||||||
explicit AddressableFlickerEffect(const std::string &name) : AddressableLightEffect(name) {}
|
explicit AddressableFlickerEffect(const std::string &name) : AddressableLightEffect(name) {}
|
||||||
void apply(AddressableLight &it, const ESPColor ¤t_color) override {
|
void apply(AddressableLight &it, const ESPColor ¤t_color) override {
|
||||||
const uint32_t now = millis();
|
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_)
|
if (now - this->last_update_ < this->update_interval_)
|
||||||
return;
|
return;
|
||||||
this->last_update_ = now;
|
this->last_update_ = now;
|
||||||
fast_random_set_seed(random_uint32());
|
fast_random_set_seed(random_uint32());
|
||||||
for (auto var : it) {
|
for (auto var : it) {
|
||||||
const uint8_t flicker = fast_random_8() % this->intensity_;
|
const uint8_t flicker = fast_random_8() % intensity;
|
||||||
var = (var.get() * delta_intensity) + (current_color * flicker);
|
var = (var.get() * inv_intensity) + (current_color * flicker);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }
|
void set_update_interval(uint32_t update_interval) { this->update_interval_ = update_interval; }
|
||||||
|
|
|
@ -476,6 +476,7 @@ void WiFiComponent::wifi_scan_done_callback_(void *arg, STATUS status) {
|
||||||
|
|
||||||
if (status != OK) {
|
if (status != OK) {
|
||||||
ESP_LOGV(TAG, "Scan failed! %d", status);
|
ESP_LOGV(TAG, "Scan failed! %d", status);
|
||||||
|
this->retry_connect();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
auto *head = reinterpret_cast<bss_info *>(arg);
|
auto *head = reinterpret_cast<bss_info *>(arg);
|
||||||
|
|
Loading…
Reference in a new issue