Skip to content

Commit

Permalink
Fix addressable effects (#590)
Browse files Browse the repository at this point in the history
  • Loading branch information
OttoWinter committed Jun 1, 2019
1 parent 1a4efa1 commit 2fb3970
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 59 deletions.
40 changes: 39 additions & 1 deletion esphome/components/light/addressable_light.cpp
Expand Up @@ -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)
Expand Down
87 changes: 32 additions & 55 deletions esphome/components/light/addressable_light.h
Expand Up @@ -372,32 +372,19 @@ 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_;
}
}

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) {
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
7 changes: 4 additions & 3 deletions esphome/components/light/addressable_light_effect.h
Expand Up @@ -308,14 +308,15 @@ class AddressableFlickerEffect : public AddressableLightEffect {
explicit AddressableFlickerEffect(const std::string &name) : AddressableLightEffect(name) {}
void apply(AddressableLight &it, const ESPColor &current_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; }
Expand Down
1 change: 1 addition & 0 deletions esphome/components/wifi/wifi_component_esp8266.cpp
Expand Up @@ -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<bss_info *>(arg);
Expand Down

0 comments on commit 2fb3970

Please sign in to comment.