ESPHome 2026.5.0b1
Loading...
Searching...
No Matches
animation.cpp
Go to the documentation of this file.
1#include "animation.h"
2
3#include "esphome/core/hal.h"
4
6
7Animation::Animation(const uint8_t *data_start, int width, int height, uint32_t animation_frame_count,
9 : Image(data_start, width, height, type, transparent),
10 animation_data_start_(data_start),
11 current_frame_(0),
12 animation_frame_count_(animation_frame_count),
13 loop_start_frame_(0),
14 loop_end_frame_(animation_frame_count_),
15 loop_count_(0),
16 loop_current_iteration_(1) {}
17void Animation::set_loop(uint32_t start_frame, uint32_t end_frame, int count) {
18 loop_start_frame_ = std::min(start_frame, animation_frame_count_);
19 loop_end_frame_ = std::min(end_frame, animation_frame_count_);
20 loop_count_ = count;
22}
23
25int Animation::get_current_frame() const { return this->current_frame_; }
27 this->current_frame_++;
28 if (loop_count_ && static_cast<uint32_t>(this->current_frame_) == loop_end_frame_ &&
32 }
33 if (static_cast<uint32_t>(this->current_frame_) >= animation_frame_count_) {
35 this->current_frame_ = 0;
36 }
37
38 this->update_data_start_();
39}
41 this->current_frame_--;
42 if (this->current_frame_ < 0) {
43 this->current_frame_ = this->animation_frame_count_ - 1;
44 }
45
46 this->update_data_start_();
47}
48
49void Animation::set_frame(int frame) {
50 unsigned abs_frame = abs(frame);
51
52 if (abs_frame < this->animation_frame_count_) {
53 if (frame >= 0) {
54 this->current_frame_ = frame;
55 } else {
56 this->current_frame_ = this->animation_frame_count_ - abs_frame;
57 }
58 }
59
60 this->update_data_start_();
61}
62
64 uint32_t image_size = this->get_width_stride() * this->height_;
65 // RGB565 with an alpha channel stores the alpha plane immediately after the RGB
66 // plane within each frame, so the per-frame stride includes the alpha bytes.
68 image_size += static_cast<uint32_t>(this->width_) * this->height_;
69 }
70 this->data_start_ = this->animation_data_start_ + image_size * this->current_frame_;
71}
72
73} // namespace esphome::animation
uint32_t get_animation_frame_count() const
Definition animation.cpp:24
const uint8_t * animation_data_start_
Definition animation.h:29
Animation(const uint8_t *data_start, int width, int height, uint32_t animation_frame_count, image::ImageType type, image::Transparency transparent)
Definition animation.cpp:7
void set_loop(uint32_t start_frame, uint32_t end_frame, int count)
Definition animation.cpp:17
void set_frame(int frame)
Selects a specific frame within the animation.
Definition animation.cpp:49
const uint8_t * data_start_
Definition image.h:54
size_t get_width_stride() const
Return the stride of the image in bytes, that is, the distance in bytes between two consecutive rows ...
Definition image.h:37
ImageType type_
Definition image.h:53
Transparency transparency_
Definition image.h:55
uint16_t type
@ TRANSPARENCY_ALPHA_CHANNEL
Definition image.h:21
@ IMAGE_TYPE_RGB565
Definition image.h:15
static void uint32_t