ESPHome  2025.2.0
online_image.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include "esphome/core/defines.h"
7 #include "esphome/core/helpers.h"
8 
9 #include "image_decoder.h"
10 
11 namespace esphome {
12 namespace online_image {
13 
14 using t_http_codes = enum {
15  HTTP_CODE_OK = 200,
16  HTTP_CODE_NOT_MODIFIED = 304,
17  HTTP_CODE_NOT_FOUND = 404,
18 };
19 
29  PNG,
31  BMP,
32 };
33 
40  public image::Image,
41  public Parented<esphome::http_request::HttpRequestComponent> {
42  public:
52  OnlineImage(const std::string &url, int width, int height, ImageFormat format, image::ImageType type,
53  image::Transparency transparency, uint32_t buffer_size);
54 
55  void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override;
56 
57  void update() override;
58  void loop() override;
59  void map_chroma_key(Color &color);
60 
62  void set_url(const std::string &url) {
63  if (this->validate_url_(url)) {
64  this->url_ = url;
65  }
66  }
67 
74  void set_placeholder(image::Image *placeholder) { this->placeholder_ = placeholder; }
75 
80  void release();
81 
87  size_t resize_download_buffer(size_t size) { return this->download_buffer_.resize(size); }
88 
89  void add_on_finished_callback(std::function<void()> &&callback);
90  void add_on_error_callback(std::function<void()> &&callback);
91 
92  protected:
93  bool validate_url_(const std::string &url);
94 
96 
97  uint32_t get_buffer_size_() const { return get_buffer_size_(this->buffer_width_, this->buffer_height_); }
98  int get_buffer_size_(int width, int height) const { return (this->get_bpp() * width + 7u) / 8u * height; }
99 
100  int get_position_(int x, int y) const { return (x + y * this->buffer_width_) * this->get_bpp() / 8; }
101 
102  ESPHOME_ALWAYS_INLINE bool is_auto_resize_() const { return this->fixed_width_ == 0 || this->fixed_height_ == 0; }
103 
117  size_t resize_(int width, int height);
118 
130  void draw_pixel_(int x, int y, Color color);
131 
132  void end_connection_();
133 
136 
137  std::shared_ptr<http_request::HttpContainer> downloader_{nullptr};
138  std::unique_ptr<ImageDecoder> decoder_{nullptr};
139 
140  uint8_t *buffer_;
148 
151 
152  std::string url_{""};
153 
155  const int fixed_width_;
157  const int fixed_height_;
176 
177  time_t start_time_;
178 
179  friend bool ImageDecoder::set_size(int width, int height);
180  friend void ImageDecoder::draw(int x, int y, int w, int h, const Color &color);
181 };
182 
183 template<typename... Ts> class OnlineImageSetUrlAction : public Action<Ts...> {
184  public:
186  TEMPLATABLE_VALUE(std::string, url)
187  void play(Ts... x) override {
188  this->parent_->set_url(this->url_.value(x...));
189  this->parent_->update();
190  }
191 
192  protected:
194 };
195 
196 template<typename... Ts> class OnlineImageReleaseAction : public Action<Ts...> {
197  public:
199  void play(Ts... x) override { this->parent_->release(); }
200 
201  protected:
203 };
204 
206  public:
208  parent->add_on_finished_callback([this]() { this->trigger(); });
209  }
210 };
211 
212 class DownloadErrorTrigger : public Trigger<> {
213  public:
214  explicit DownloadErrorTrigger(OnlineImage *parent) {
215  parent->add_on_error_callback([this]() { this->trigger(); });
216  }
217 };
218 
219 } // namespace online_image
220 } // namespace esphome
bool validate_url_(const std::string &url)
void draw_pixel_(int x, int y, Color color)
Draw a pixel into the buffer.
void set_placeholder(image::Image *placeholder)
Set the image that needs to be shown as long as the downloaded image is not available.
Definition: online_image.h:74
int buffer_width_
Actual width of the current image.
Definition: online_image.h:166
void add_on_finished_callback(std::function< void()> &&callback)
OnlineImage(const std::string &url, int width, int height, ImageFormat format, image::ImageType type, image::Transparency transparency, uint32_t buffer_size)
Construct a new OnlineImage object.
const int fixed_height_
height requested on configuration, or 0 if non specified.
Definition: online_image.h:157
uint16_t x
Definition: tt21100.cpp:17
std::unique_ptr< ImageDecoder > decoder_
Definition: online_image.h:138
void set_url(const std::string &url)
Set the URL to download the image from.
Definition: online_image.h:62
int buffer_height_
Actual height of the current image.
Definition: online_image.h:175
This class simplifies creating components that periodically check a state.
Definition: component.h:283
int get_bpp() const
Definition: image.h:34
uint8_t h
Definition: bl0906.h:209
const int fixed_width_
width requested on configuration, or 0 if non specified.
Definition: online_image.h:155
uint16_t y
Definition: tt21100.cpp:18
Download an image from a given URL, and decode it using the specified decoder.
Definition: online_image.h:39
std::shared_ptr< http_request::HttpContainer > downloader_
Definition: online_image.h:137
void add_on_error_callback(std::function< void()> &&callback)
esphome::http_request::HttpRequestComponent * parent_
Definition: helpers.h:549
Automatically detect from MIME type.
Definition: online_image.h:25
CallbackManager< void()> download_error_callback_
Definition: online_image.h:135
size_t download_buffer_initial_size_
This is the initial size of the download buffer, not the current size.
Definition: online_image.h:147
uint8_t type
size_t resize_(int width, int height)
Resize the image buffer to the requested dimensions.
TEMPLATABLE_VALUE(std::string, url) void play(Ts... x) override
Definition: online_image.h:186
size_t resize_download_buffer(size_t size)
Resize the download buffer.
Definition: online_image.h:87
void draw(int x, int y, int w, int h, const Color &color)
Fill a rectangle on the display_buffer using the defined color.
bool set_size(int width, int height)
Request the image to be resized once the actual dimensions are known.
CallbackManager< void()> download_finished_callback_
Definition: online_image.h:134
void release()
Release the buffer storing the image.
int get_position_(int x, int y) const
Definition: online_image.h:100
int get_buffer_size_(int width, int height) const
Definition: online_image.h:98
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
enum { HTTP_CODE_OK=200, HTTP_CODE_NOT_MODIFIED=304, HTTP_CODE_NOT_FOUND=404, } t_http_codes
Definition: online_image.h:18
void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override
ImageFormat
Format that the image is encoded with.
Definition: online_image.h:23
RAMAllocator< uint8_t > allocator_
Definition: online_image.h:95
Helper class to easily give an object a parent of type T.
Definition: helpers.h:538
ESPHOME_ALWAYS_INLINE bool is_auto_resize_() const
Definition: online_image.h:102