ESPHome 2025.6.3
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 this->etag_ = "";
67 this->last_modified_ = "";
68 }
69
71 template<typename V> void add_request_header(const std::string &header, V value) {
72 this->request_headers_.push_back(std::pair<std::string, TemplatableValue<std::string> >(header, value));
73 }
74
81 void set_placeholder(image::Image *placeholder) { this->placeholder_ = placeholder; }
82
87 void release();
88
94 size_t resize_download_buffer(size_t size) { return this->download_buffer_.resize(size); }
95
96 void add_on_finished_callback(std::function<void(bool)> &&callback);
97 void add_on_error_callback(std::function<void()> &&callback);
98
99 protected:
100 bool validate_url_(const std::string &url);
101
103
104 uint32_t get_buffer_size_() const { return get_buffer_size_(this->buffer_width_, this->buffer_height_); }
105 int get_buffer_size_(int width, int height) const { return (this->get_bpp() * width + 7u) / 8u * height; }
106
107 int get_position_(int x, int y) const { return (x + y * this->buffer_width_) * this->get_bpp() / 8; }
108
109 ESPHOME_ALWAYS_INLINE bool is_auto_resize_() const { return this->fixed_width_ == 0 || this->fixed_height_ == 0; }
110
124 size_t resize_(int width, int height);
125
137 void draw_pixel_(int x, int y, Color color);
138
139 void end_connection_();
140
143
144 std::shared_ptr<http_request::HttpContainer> downloader_{nullptr};
145 std::unique_ptr<ImageDecoder> decoder_{nullptr};
146
147 uint8_t *buffer_;
155
158
159 std::string url_{""};
160
161 std::vector<std::pair<std::string, TemplatableValue<std::string> > > request_headers_;
162
164 const int fixed_width_;
166 const int fixed_height_;
188 std::string etag_ = "";
192 std::string last_modified_ = "";
193
195
196 friend bool ImageDecoder::set_size(int width, int height);
197 friend void ImageDecoder::draw(int x, int y, int w, int h, const Color &color);
198};
199
200template<typename... Ts> class OnlineImageSetUrlAction : public Action<Ts...> {
201 public:
203 TEMPLATABLE_VALUE(std::string, url)
204 void play(Ts... x) override {
205 this->parent_->set_url(this->url_.value(x...));
206 this->parent_->update();
207 }
208
209 protected:
211};
212
213template<typename... Ts> class OnlineImageReleaseAction : public Action<Ts...> {
214 public:
216 void play(Ts... x) override { this->parent_->release(); }
217
218 protected:
220};
221
222class DownloadFinishedTrigger : public Trigger<bool> {
223 public:
225 parent->add_on_finished_callback([this](bool cached) { this->trigger(cached); });
226 }
227};
228
230 public:
232 parent->add_on_error_callback([this]() { this->trigger(); });
233 }
234};
235
236} // namespace online_image
237} // 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:539
This class simplifies creating components that periodically check a state.
Definition component.h:331
An STL allocator that uses SPI or internal RAM.
Definition helpers.h:684
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.
std::string etag_
The value of the ETag HTTP header provided in the last response.
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
CallbackManager< void(bool)> download_finished_callback_
void draw_pixel_(int x, int y, Color color)
Draw a pixel into the buffer.
std::vector< std::pair< std::string, TemplatableValue< std::string > > > request_headers_
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_request_header(const std::string &header, V value)
Add the request header.
RAMAllocator< uint8_t > allocator_
void add_on_finished_callback(std::function< void(bool)> &&callback)
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.
std::string last_modified_
The value of the Last-Modified HTTP header provided in the last response.
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