ESPHome  2024.5.0
animation.cpp
Go to the documentation of this file.
1 #include "animation.h"
2 
3 #include "esphome/core/hal.h"
4 
5 namespace esphome {
6 namespace animation {
7 
8 Animation::Animation(const uint8_t *data_start, int width, int height, uint32_t animation_frame_count,
10  : Image(data_start, width, height, type),
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) {}
18 void 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 
26 int Animation::get_current_frame() const { return this->current_frame_; }
28  this->current_frame_++;
29  if (loop_count_ && this->current_frame_ == loop_end_frame_ &&
33  }
35  this->loop_current_iteration_ = 1;
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 
50 void 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 = image_type_to_width_stride(this->width_, this->type_) * this->height_;
66  this->data_start_ = this->animation_data_start_ + image_size * this->current_frame_;
67 }
68 
69 } // namespace animation
70 } // namespace esphome
void set_frame(int frame)
Selects a specific frame within the animation.
Definition: animation.cpp:50
int image_type_to_width_stride(int width, ImageType type)
Definition: image.h:32
uint32_t get_animation_frame_count() const
Definition: animation.cpp:25
ImageType type_
Definition: image.h:57
const uint8_t * animation_data_start_
Definition: animation.h:29
void set_loop(uint32_t start_frame, uint32_t end_frame, int count)
Definition: animation.cpp:18
Animation(const uint8_t *data_start, int width, int height, uint32_t animation_frame_count, image::ImageType type)
Definition: animation.cpp:8
uint8_t type
const uint8_t * data_start_
Definition: image.h:58
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7