ESPHome  2024.11.1
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 { return (this->get_bpp() * width + 7u) / 8u * height; }
90 
91  int get_position_(int x, int y) const { return (x + y * this->buffer_width_) * this->get_bpp() / 8; }
92 
93  ESPHOME_ALWAYS_INLINE bool auto_resize_() const { return this->fixed_width_ == 0 || this->fixed_height_ == 0; }
94 
95  bool resize_(int width, int height);
96 
108  void draw_pixel_(int x, int y, Color color);
109 
110  void end_connection_();
111 
114 
115  std::shared_ptr<http_request::HttpContainer> downloader_{nullptr};
116  std::unique_ptr<ImageDecoder> decoder_{nullptr};
117 
118  uint8_t *buffer_;
120 
123 
124  std::string url_{""};
125 
127  const int fixed_width_;
129  const int fixed_height_;
148 
149  friend void ImageDecoder::set_size(int width, int height);
150  friend void ImageDecoder::draw(int x, int y, int w, int h, const Color &color);
151 };
152 
153 template<typename... Ts> class OnlineImageSetUrlAction : public Action<Ts...> {
154  public:
156  TEMPLATABLE_VALUE(std::string, url)
157  void play(Ts... x) override {
158  this->parent_->set_url(this->url_.value(x...));
159  this->parent_->update();
160  }
161 
162  protected:
164 };
165 
166 template<typename... Ts> class OnlineImageReleaseAction : public Action<Ts...> {
167  public:
169  void play(Ts... x) override { this->parent_->release(); }
170 
171  protected:
173 };
174 
176  public:
178  parent->add_on_finished_callback([this]() { this->trigger(); });
179  }
180 };
181 
182 class DownloadErrorTrigger : public Trigger<> {
183  public:
184  explicit DownloadErrorTrigger(OnlineImage *parent) {
185  parent->add_on_error_callback([this]() { this->trigger(); });
186  }
187 };
188 
189 } // namespace online_image
190 } // 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:138
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:129
uint16_t x
Definition: tt21100.cpp:17
std::unique_ptr< ImageDecoder > decoder_
Definition: online_image.h:116
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:147
This class simplifies creating components that periodically check a state.
Definition: component.h:283
int get_bpp() const
Definition: image.h:29
uint8_t h
Definition: bl0906.h:209
const int fixed_width_
width requested on configuration, or 0 if non specified.
Definition: online_image.h:127
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:37
ESPHOME_ALWAYS_INLINE bool auto_resize_() const
Definition: online_image.h:93
std::shared_ptr< http_request::HttpContainer > downloader_
Definition: online_image.h:115
void add_on_error_callback(std::function< void()> &&callback)
esphome::http_request::HttpRequestComponent * parent_
Definition: helpers.h:533
Automatically detect from MIME type.
Definition: online_image.h:25
CallbackManager< void()> download_error_callback_
Definition: online_image.h:113
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.
TEMPLATABLE_VALUE(std::string, url) void play(Ts... x) override
Definition: online_image.h:156
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:112
void release()
Release the buffer storing the image.
int get_position_(int x, int y) const
Definition: online_image.h:91
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
An STL allocator that uses SPI or internal RAM.
Definition: helpers.h:666
Helper class to easily give an object a parent of type T.
Definition: helpers.h:522