ESPHome  2024.10.3
online_image.h
Go to the documentation of this file.
1 #pragma once
2 
4 #include "esphome/core/defines.h"
5 #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,
30 };
31 
38  public image::Image,
39  public Parented<esphome::http_request::HttpRequestComponent> {
40  public:
50  OnlineImage(const std::string &url, int width, int height, ImageFormat format, image::ImageType type,
51  uint32_t buffer_size);
52 
53  void draw(int x, int y, display::Display *display, Color color_on, Color color_off) override;
54 
55  void update() override;
56  void loop() override;
57 
59  void set_url(const std::string &url) {
60  if (this->validate_url_(url)) {
61  this->url_ = url;
62  }
63  }
64 
71  void set_placeholder(image::Image *placeholder) { this->placeholder_ = placeholder; }
72 
77  void release();
78 
79  void add_on_finished_callback(std::function<void()> &&callback);
80  void add_on_error_callback(std::function<void()> &&callback);
81 
82  protected:
83  bool validate_url_(const std::string &url);
84 
86  Allocator allocator_{Allocator::Flags::ALLOW_FAILURE};
87 
88  uint32_t get_buffer_size_() const { return get_buffer_size_(this->buffer_width_, this->buffer_height_); }
89  int get_buffer_size_(int width, int height) const {
90  return std::ceil(image::image_type_to_bpp(this->type_) * width * height / 8.0);
91  }
92 
93  int get_position_(int x, int y) const {
94  return ((x + y * this->buffer_width_) * image::image_type_to_bpp(this->type_)) / 8;
95  }
96 
97  ESPHOME_ALWAYS_INLINE bool auto_resize_() const { return this->fixed_width_ == 0 || this->fixed_height_ == 0; }
98 
99  bool resize_(int width, int height);
100 
112  void draw_pixel_(int x, int y, Color color);
113 
114  void end_connection_();
115 
118 
119  std::shared_ptr<http_request::HttpContainer> downloader_{nullptr};
120  std::unique_ptr<ImageDecoder> decoder_{nullptr};
121 
122  uint8_t *buffer_;
124 
127 
128  std::string url_{""};
129 
131  const int fixed_width_;
133  const int fixed_height_;
152 
153  friend void ImageDecoder::set_size(int width, int height);
154  friend void ImageDecoder::draw(int x, int y, int w, int h, const Color &color);
155 };
156 
157 template<typename... Ts> class OnlineImageSetUrlAction : public Action<Ts...> {
158  public:
160  TEMPLATABLE_VALUE(std::string, url)
161  void play(Ts... x) override {
162  this->parent_->set_url(this->url_.value(x...));
163  this->parent_->update();
164  }
165 
166  protected:
168 };
169 
170 template<typename... Ts> class OnlineImageReleaseAction : public Action<Ts...> {
171  public:
173  void play(Ts... x) override { this->parent_->release(); }
174 
175  protected:
177 };
178 
180  public:
182  parent->add_on_finished_callback([this]() { this->trigger(); });
183  }
184 };
185 
186 class DownloadErrorTrigger : public Trigger<> {
187  public:
188  explicit DownloadErrorTrigger(OnlineImage *parent) {
189  parent->add_on_error_callback([this]() { this->trigger(); });
190  }
191 };
192 
193 } // namespace online_image
194 } // namespace esphome
bool resize_(int width, int height)
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:71
int buffer_width_
Actual width of the current image.
Definition: online_image.h:142
void add_on_finished_callback(std::function< void()> &&callback)
const int fixed_height_
height requested on configuration, or 0 if non specified.
Definition: online_image.h:133
uint16_t x
Definition: tt21100.cpp:17
std::unique_ptr< ImageDecoder > decoder_
Definition: online_image.h:120
void set_url(const std::string &url)
Set the URL to download the image from.
Definition: online_image.h:59
int buffer_height_
Actual height of the current image.
Definition: online_image.h:151
This class simplifies creating components that periodically check a state.
Definition: component.h:283
uint8_t h
Definition: bl0906.h:209
const int fixed_width_
width requested on configuration, or 0 if non specified.
Definition: online_image.h:131
uint16_t y
Definition: tt21100.cpp:18
ImageType type_
Definition: image.h:69
Download an image from a given URL, and decode it using the specified decoder.
Definition: online_image.h:37
ESPHOME_ALWAYS_INLINE bool auto_resize_() const
Definition: online_image.h:97
std::shared_ptr< http_request::HttpContainer > downloader_
Definition: online_image.h:119
void add_on_error_callback(std::function< void()> &&callback)
esphome::http_request::HttpRequestComponent * parent_
Definition: helpers.h:532
Automatically detect from MIME type.
Definition: online_image.h:25
CallbackManager< void()> download_error_callback_
Definition: online_image.h:117
uint8_t type
OnlineImage(const std::string &url, int width, int height, ImageFormat format, image::ImageType type, uint32_t buffer_size)
Construct a new OnlineImage object.
int image_type_to_bpp(ImageType type)
Definition: image.h:25
TEMPLATABLE_VALUE(std::string, url) void play(Ts... x) override
Definition: online_image.h:160
void draw(int x, int y, int w, int h, const Color &color)
Draw a rectangle on the display_buffer using the defined color.
CallbackManager< void()> download_finished_callback_
Definition: online_image.h:116
void release()
Release the buffer storing the image.
int get_position_(int x, int y) const
Definition: online_image.h:93
int get_buffer_size_(int width, int height) const
Definition: online_image.h:89
void set_size(int width, int height)
Request the image to be resized once the actual dimensions are known.
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
Helper class to easily give an object a parent of type T.
Definition: helpers.h:521