chore: cleanup and mild refactoring

This commit is contained in:
Philip Stark 2023-05-12 17:37:21 +02:00
parent 70458d4ed9
commit 519b36bfed
3 changed files with 26 additions and 189 deletions

View file

@ -22,9 +22,9 @@ struct SegmentCoords {
uint16_t y2;
};
class BrightnessTransformer {
class BrightnessTransitionTransformer {
public:
virtual ~BrightnessTransformer() = default;
virtual ~BrightnessTransitionTransformer() = default;
void setup(const uint8_t start_values, const uint8_t target_values, uint32_t length) {
this->start_time_ = millis();
@ -33,29 +33,22 @@ public:
this->target_values_ = target_values;
this->start();
}
/// Indicates whether this transformation is finished.
virtual bool is_finished() { return this->get_progress_() >= 1.0f; }
/// This will be called before the transition is started.
virtual void start() {}
/// This will be called while the transformer is active to apply the transition to the light. Can either write to the
/// light directly, or return LightColorValues that will be applied.
virtual optional<uint8_t> apply() = 0;
/// This will be called after transition is finished.
// virtual optional<uint8_t> apply() = 0;
virtual optional<uint8_t> apply() {
float v = BrightnessTransitionTransformer::smoothed_progress(this->get_progress_());
return esphome::lerp(v, this->start_values_, this->target_values_);
}
virtual void stop() {}
void reset() {
this->start_time_ = millis();
}
const uint8_t &get_start_values() const { return this->start_values_; }
const uint8_t &get_target_values() const { return this->target_values_; }
protected:
/// The progress of this transition, on a scale of 0 to 1.
// The progress of this transition, on a scale of 0 to 1.
float get_progress_() {
uint32_t now = esphome::millis();
if (now < this->start_time_)
@ -65,29 +58,16 @@ protected:
return clamp((now - this->start_time_) / float(this->length_), 0.0f, 1.0f);
}
static float smoothed_progress(float x) { return x * x * x * (x * (x * 6.0f - 15.0f) + 10.0f); }
uint32_t start_time_;
uint32_t length_;
uint8_t start_values_;
uint8_t target_values_;
};
class BrightnessTransitionTransformer : public BrightnessTransformer {
public:
optional<uint8_t> apply() override {
float v = BrightnessTransitionTransformer::smoothed_progress(this->get_progress_());
return esphome::lerp(v, this->start_values_, this->target_values_);
}
protected:
// This looks crazy, but it reduces to 6x^5 - 15x^4 + 10x^3 which is just a smooth sigmoid-like
// transition from 0 to 1 on x = [0, 1]
static float smoothed_progress(float x) { return x * x * x * (x * (x * 6.0f - 15.0f) + 10.0f); }
};
class Wordclock : public esphome::PollingComponent {
public:
// Wordclock();
Wordclock(
esphome::time::RealTimeClock *time,
esphome::addressable_light::AddressableLightDisplay *display,
@ -96,9 +76,8 @@ public:
void setup();
void update();
// void setup_color(uint8_t brightness, Color on_color, Color off_color);
void set_brightness(uint8_t brightness) {
this->brightness = brightness;
this->brightness = brightness;
}
void set_on_color(Color on_color) {
this->on_color = on_color;
@ -131,17 +110,6 @@ this->brightness = brightness;
});
}
// void add_hour_segment(uint8_t index, uint16_t segment_id) {
// this->hours->at(index).segments->push_back(segment_id);
// }
// void add_minute_segment(uint8_t index, uint16_t segment_id) {
// this->minutes->at(index).segments->push_back(segment_id);
// }
// void add_hour(uint8_t index, std::vector<uint16_t> *segments);
// void add_minute(uint8_t index, std::vector<uint16_t> *segments);
// void add_hour_offset(uint8_t index, int8_t offset) {
// this->minutes->at(index).hour_offset = offset;
// }
protected:
// ESPHome Components
esphome::time::RealTimeClock *time;
@ -170,14 +138,9 @@ protected:
std::vector<uint16_t> *previous_segments;
uint32_t current_position{0};
Color get_next_color_base_(uint32_t position, uint32_t speed, uint16_t width, const Color &current_color);
Color get_next_color(uint32_t position, const Color &current_color);
// Color
// Color get_on_color() {
// return this->on_color * this->brightness;
// }
// Color get_off_color() {
// return this->off_color * this->brightness;
// }
Color on_color {Color(0xFFFFFF)};
Color off_color {Color(0x000000)};
uint8_t brightness{255};
@ -186,21 +149,8 @@ protected:
int8_t find_hour(uint8_t target_value);
int8_t find_minute(uint8_t target_value);
void find_difference(std::vector<uint16_t> *a_vec, std::vector<uint16_t> *b_vec);
// void draw_segment(uint16_t segment_id) {
// this->draw_segment(segment_id, this->brightness);
// }
// void draw_segment(uint16_t segment_id, uint8_t brightness) {
// this->draw_segment(segment_id, this->on_color * brightness);
// }
// void draw_segment(uint16_t segment_id, Color color) {
// SegmentCoords s = this->segments->at(segment_id);
// // ESP_LOGD("wordclock.cpp", "brightness[%d] * color[%06x] = %06x", brightness, color.raw_32, (color * brightness).raw_32);
// this->display->line(s.x1, s.y1, s.x2, s.y2, color);
// }
void segment_effect_base(uint16_t segment_id, bool to_effect, Color base_color, uint8_t transition_progress) {
// ESP_LOGD("wordclock.cpp", "brightness[%d] * color[%06x] = %06x", brightness, color.raw_32, (color * brightness).raw_32);
SegmentCoords s = this->segments->at(segment_id);
int x1 = s.x1; int y1 = s.y1; int x2 = s.x2; int y2 = s.y2;
Color color_to_draw;
@ -215,11 +165,8 @@ protected:
color_to_draw = base_color.gradient(effect_color, transition_progress);
} else {
color_to_draw = effect_color.gradient(base_color, transition_progress);
// c = base_color.gradient(this->get_next_color(x1 + y1, Color(0)), transition_progress);
}
this->display->draw_pixel_at(x1, y1, color_to_draw);
// this->display->draw_pixel_at(x1, y1, this->get_next_color(this->current_position++, Color(0)));
if (x1 == x2 && y1 == y2) break;
int32_t e2 = 2 * err;
if (e2 >= dy) {
@ -242,10 +189,6 @@ protected:
void apply_segment_effect(uint16_t segment_id) {
segment_effect_base(segment_id, true, Color(0), 255);
}
// void disable_segment(uint16_t segment_id, Color off_color, uint8_t transition_progress) {
// SegmentCoords s = this->segments->at(segment_id);
// this->display->line(s.x1, s.y1, s.x2, s.y2, color);
// }
void start_idle_animation() {
this->display->fill(this->off_color);
this->display->set_enabled(false);
@ -259,13 +202,7 @@ protected:
this->previous_segments->clear();
this->current_segments->clear();
}
// std::vector<std::vector<uint16_t>> *minutes;
// std::vector<std::vector<uint16_t>> *hours;
// std::vector<int8_t> *hour_offsets;
};
} // namespace wordclock
} // namespace esphome