From 77fb02729e3aeea28743a74d2ea2ee4205d96a46 Mon Sep 17 00:00:00 2001 From: davestubbs Date: Mon, 8 Aug 2022 23:28:05 +0100 Subject: [PATCH] Added support for setting the current animation frame (#3681) --- esphome/components/display/display_buffer.cpp | 12 ++++++++++++ esphome/components/display/display_buffer.h | 6 ++++++ 2 files changed, 18 insertions(+) diff --git a/esphome/components/display/display_buffer.cpp b/esphome/components/display/display_buffer.cpp index ca866a43d2..97c08dae24 100644 --- a/esphome/components/display/display_buffer.cpp +++ b/esphome/components/display/display_buffer.cpp @@ -591,6 +591,18 @@ void Animation::prev_frame() { } } +void Animation::set_frame(int frame) { + unsigned abs_frame = abs(frame); + + if (abs_frame < this->animation_frame_count_) { + if (frame >= 0) { + this->current_frame_ = frame; + } else { + this->current_frame_ = this->animation_frame_count_ - abs_frame; + } + } +} + DisplayPage::DisplayPage(display_writer_t writer) : writer_(std::move(writer)) {} void DisplayPage::show() { this->parent_->show_page(this); } void DisplayPage::show_next() { this->next_->show(); } diff --git a/esphome/components/display/display_buffer.h b/esphome/components/display/display_buffer.h index dc6bbdf350..d2d3f2ed77 100644 --- a/esphome/components/display/display_buffer.h +++ b/esphome/components/display/display_buffer.h @@ -491,6 +491,12 @@ class Animation : public Image { void next_frame(); void prev_frame(); + /** Selects a specific frame within the animation. + * + * @param frame If possitive, advance to the frame. If negative, recede to that frame from the end frame. + */ + void set_frame(int frame); + protected: int current_frame_; int animation_frame_count_;