feat: add rainbow effect for segments

This commit is contained in:
Philip Stark 2023-05-12 17:16:24 +02:00
parent dd1408c5cb
commit b712b90be1
3 changed files with 93 additions and 25 deletions

View file

@ -164,9 +164,13 @@ protected:
std::vector<uint16_t> *added_segments;
std::vector<uint16_t> *removed_segments;
std::vector<uint16_t> *staying_segments;
std::vector<uint16_t> *current_segments;
std::vector<uint16_t> *previous_segments;
uint32_t current_position{0};
Color get_next_color(uint32_t position, const Color &current_color);
// Color
// Color get_on_color() {
// return this->on_color * this->brightness;
@ -185,14 +189,63 @@ protected:
// 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);
// 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);
this->display->line(s.x1, s.y1, s.x2, s.y2, color);
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;
Color effect_color;
const int32_t dx = abs(x2 - x1), sx = x1 < x2 ? 1 : -1;
const int32_t dy = -abs(y2 - y1), sy = y1 < y2 ? 1 : -1;
int32_t err = dx + dy;
while (true) {
effect_color = this->get_next_color(x1 + y1, Color(0));
if (to_effect) {
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) {
err += dy;
x1 += sx;
}
if (e2 <= dx) {
err += dx;
y1 += sy;
}
}
}
void enable_segment_effect(uint16_t segment_id, Color off_color, uint8_t transition_progress) {
segment_effect_base(segment_id, true, off_color, transition_progress);
}
void disable_segment_effect(uint16_t segment_id, Color off_color, uint8_t transition_progress) {
segment_effect_base(segment_id, false, off_color, transition_progress);
}
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);