ESPHome 2025.5.0
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
5namespace esphome {
6namespace animation {
7
8Animation::Animation(const uint8_t *data_start, int width, int height, uint32_t animation_frame_count,
10 : Image(data_start, width, height, type, transparent),
11 animation_data_start_(data_start),
12 current_frame_(0),
13 animation_frame_count_(animation_frame_count),
14 loop_start_frame_(0),
15 loop_end_frame_(animation_frame_count_),
16 loop_count_(0),
17 loop_current_iteration_(1) {}
18void Animation::set_loop(uint32_t start_frame, uint32_t end_frame, int count) {
19 loop_start_frame_ = std::min(start_frame, animation_frame_count_);
20 loop_end_frame_ = std::min(end_frame, animation_frame_count_);
21 loop_count_ = count;
23}
24
26int Animation::get_current_frame() const { return this->current_frame_; }
28 this->current_frame_++;
33 }
36 this->current_frame_ = 0;
37 }
38
39 this->update_data_start_();
40}
42 this->current_frame_--;
43 if (this->current_frame_ < 0) {
44 this->current_frame_ = this->animation_frame_count_ - 1;
45 }
46
47 this->update_data_start_();
48}
49
50void Animation::set_frame(int frame) {
51 unsigned abs_frame = abs(frame);
52
53 if (abs_frame < this->animation_frame_count_) {
54 if (frame >= 0) {
55 this->current_frame_ = frame;
56 } else {
57 this->current_frame_ = this->animation_frame_count_ - abs_frame;
58 }
59 }
60
61 this->update_data_start_();
62}
63
65 const uint32_t image_size = this->get_width_stride() * this->height_;
66 this->data_start_ = this->animation_data_start_ + image_size * this->current_frame_;
67}
68
69} // namespace animation
70} // namespace esphome
uint32_t get_animation_frame_count() const
Definition animation.cpp:25
const uint8_t * animation_data_start_
Definition animation.h:30
Animation(const uint8_t *data_start, int width, int height, uint32_t animation_frame_count, image::ImageType type, image::Transparency transparent)
Definition animation.cpp:8
void set_loop(uint32_t start_frame, uint32_t end_frame, int count)
Definition animation.cpp:18
void set_frame(int frame)
Selects a specific frame within the animation.
Definition animation.cpp:50
const uint8_t * data_start_
Definition image.h:55
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:38
uint8_t type
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7