ESPHome 2025.5.0
Loading...
Searching...
No Matches
online_image.h
Go to the documentation of this file.
1#pragma once
2
8
9#include "image_decoder.h"
10
11namespace esphome {
12namespace online_image {
13
14using t_http_codes = enum {
15 HTTP_CODE_OK = 200,
16 HTTP_CODE_NOT_MODIFIED = 304,
17 HTTP_CODE_NOT_FOUND = 404,
18};
19
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
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
183template<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
196template<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
213 public:
215 parent->add_on_error_callback([this]() { this->trigger(); });
216 }
217};
218
219} // namespace online_image
220} // namespace esphome
uint8_t h
Definition bl0906.h:2
virtual void play(Ts... x)=0
Helper class to easily give an object a parent of type T.
Definition helpers.h:538
This class simplifies creating components that periodically check a state.
Definition component.h:301
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:683
void trigger(Ts... x)
Definition automation.h:96
int get_bpp() const
Definition image.h:34
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.
Download an image from a given URL, and decode it using the specified decoder.
size_t resize_download_buffer(size_t size)
Resize the download buffer.
CallbackManager< void()> download_finished_callback_
size_t resize_(int width, int height)
Resize the image buffer to the requested dimensions.
bool validate_url_(const std::string &url)
int get_position_(int x, int y) const
void draw_pixel_(int x, int y, Color color)
Draw a pixel into the buffer.
friend void ImageDecoder::draw(int x, int y, int w, int h, const Color &color)
void add_on_error_callback(std::function< void()> &&callback)
std::unique_ptr< ImageDecoder > decoder_
void add_on_finished_callback(std::function< void()> &&callback)
RAMAllocator< uint8_t > allocator_
int buffer_width_
Actual width of the current image.
int get_buffer_size_(int width, int height) const
ESPHOME_ALWAYS_INLINE bool is_auto_resize_() const
void set_url(const std::string &url)
Set the URL to download the image from.
std::shared_ptr< http_request::HttpContainer > downloader_
const int fixed_width_
width requested on configuration, or 0 if non specified.
CallbackManager< void()> download_error_callback_
const int fixed_height_
height requested on configuration, or 0 if non specified.
int buffer_height_
Actual height of the current image.
void set_placeholder(image::Image *placeholder)
Set the image that needs to be shown as long as the downloaded image is not available.
size_t download_buffer_initial_size_
This is the initial size of the download buffer, not the current size.
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.
void release()
Release the buffer storing the image.
TEMPLATABLE_VALUE(std::string, url) void play(Ts... x) override
uint8_t type
ImageFormat
Format that the image is encoded with.
@ AUTO
Automatically detect from MIME type.
enum { HTTP_CODE_OK=200, HTTP_CODE_NOT_MODIFIED=304, HTTP_CODE_NOT_FOUND=404, } t_http_codes
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6