ESPHome 2025.5.0
Loading...
Searching...
No Matches
esp32_camera.h
Go to the documentation of this file.
1#pragma once
2
3#ifdef USE_ESP32
4
9#include <esp_camera.h>
10#include <freertos/FreeRTOS.h>
11#include <freertos/queue.h>
12
13namespace esphome {
14namespace esp32_camera {
15
16class ESP32Camera;
17
18/* ---------------- enum classes ---------------- */
20
41
43 ESP32_GAINCEILING_2X = GAINCEILING_2X,
44 ESP32_GAINCEILING_4X = GAINCEILING_4X,
45 ESP32_GAINCEILING_8X = GAINCEILING_8X,
46 ESP32_GAINCEILING_16X = GAINCEILING_16X,
47 ESP32_GAINCEILING_32X = GAINCEILING_32X,
48 ESP32_GAINCEILING_64X = GAINCEILING_64X,
49 ESP32_GAINCEILING_128X = GAINCEILING_128X,
50};
51
56
64
74
75/* ---------------- CameraImage class ---------------- */
77 public:
78 CameraImage(camera_fb_t *buffer, uint8_t requester);
79 camera_fb_t *get_raw_buffer();
80 uint8_t *get_data_buffer();
81 size_t get_data_length();
82 bool was_requested_by(CameraRequester requester) const;
83
84 protected:
85 camera_fb_t *buffer_;
86 uint8_t requesters_;
87};
88
90 uint8_t *data;
91 size_t length;
92};
93
94/* ---------------- CameraImageReader class ---------------- */
96 public:
97 void set_image(std::shared_ptr<CameraImage> image);
98 size_t available() const;
99 uint8_t *peek_data_buffer();
100 void consume_data(size_t consumed);
101 void return_image();
102
103 protected:
104 std::shared_ptr<CameraImage> image_;
105 size_t offset_{0};
106};
107
108/* ---------------- ESP32Camera class ---------------- */
109class ESP32Camera : public EntityBase, public Component {
110 public:
111 ESP32Camera();
112
113 /* setters */
114 /* -- pin assignment */
115 void set_data_pins(std::array<uint8_t, 8> pins);
116 void set_vsync_pin(uint8_t pin);
117 void set_href_pin(uint8_t pin);
118 void set_pixel_clock_pin(uint8_t pin);
119 void set_external_clock(uint8_t pin, uint32_t frequency);
120 void set_i2c_pins(uint8_t sda, uint8_t scl);
121 void set_reset_pin(uint8_t pin);
122 void set_power_down_pin(uint8_t pin);
123 /* -- image */
125 void set_jpeg_quality(uint8_t quality);
126 void set_vertical_flip(bool vertical_flip);
127 void set_horizontal_mirror(bool horizontal_mirror);
128 void set_contrast(int contrast);
129 void set_brightness(int brightness);
130 void set_saturation(int saturation);
132 /* -- exposure */
134 void set_aec2(bool aec2);
135 void set_ae_level(int ae_level);
136 void set_aec_value(uint32_t aec_value);
137 /* -- gains */
139 void set_agc_value(uint8_t agc_value);
140 void set_agc_gain_ceiling(ESP32AgcGainCeiling gain_ceiling);
141 /* -- white balance */
143 /* -- test */
144 void set_test_pattern(bool test_pattern);
145 /* -- framerates */
146 void set_max_update_interval(uint32_t max_update_interval);
147 void set_idle_update_interval(uint32_t idle_update_interval);
148 /* -- frame buffer */
149 void set_frame_buffer_mode(camera_grab_mode_t mode);
150 void set_frame_buffer_count(uint8_t fb_count);
151
152 /* public API (derivated) */
153 void setup() override;
154 void loop() override;
155 void dump_config() override;
156 float get_setup_priority() const override;
157 /* public API (specific) */
158 void start_stream(CameraRequester requester);
159 void stop_stream(CameraRequester requester);
160 void request_image(CameraRequester requester);
162
163 void add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&callback);
164 void add_stream_start_callback(std::function<void()> &&callback);
165 void add_stream_stop_callback(std::function<void()> &&callback);
166
167 protected:
168 /* internal methods */
169 bool has_requested_image_() const;
170 bool can_return_image_() const;
171
172 static void framebuffer_task(void *pv);
173
174 /* attributes */
175 /* camera configuration */
176 camera_config_t config_{};
177 /* -- image */
178 bool vertical_flip_{true};
180 int contrast_{0};
184 /* -- exposure */
186 bool aec2_{false};
187 int ae_level_{0};
188 uint32_t aec_value_{300};
189 /* -- gains */
191 uint8_t agc_value_{0};
193 /* -- white balance */
195 /* -- Test */
196 bool test_pattern_{false};
197 /* -- framerates */
198 uint32_t max_update_interval_{1000};
199 uint32_t idle_update_interval_{15000};
200
201 esp_err_t init_error_{ESP_OK};
202 std::shared_ptr<CameraImage> current_image_;
207 CallbackManager<void(std::shared_ptr<CameraImage>)> new_image_callback_{};
210
212 uint32_t last_update_{0};
213};
214
215// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
216extern ESP32Camera *global_esp32_camera;
217
218class ESP32CameraImageTrigger : public Trigger<CameraImageData> {
219 public:
221 parent->add_image_callback([this](const std::shared_ptr<esp32_camera::CameraImage> &image) {
222 CameraImageData camera_image_data{};
223 camera_image_data.length = image->get_data_length();
224 camera_image_data.data = image->get_data_buffer();
225 this->trigger(camera_image_data);
226 });
227 }
228};
229
231 public:
233 parent->add_stream_start_callback([this]() { this->trigger(); });
234 }
235
236 protected:
237};
239 public:
241 parent->add_stream_stop_callback([this]() { this->trigger(); });
242 }
243
244 protected:
245};
246
247} // namespace esp32_camera
248} // namespace esphome
249
250#endif
BedjetMode mode
BedJet operating mode.
uint16_le_t frequency
Definition bl0942.h:6
bool was_requested_by(CameraRequester requester) const
CameraImage(camera_fb_t *buffer, uint8_t requester)
std::shared_ptr< CameraImage > image_
void set_image(std::shared_ptr< CameraImage > image)
void set_i2c_pins(uint8_t sda, uint8_t scl)
void set_agc_value(uint8_t agc_value)
void set_test_pattern(bool test_pattern)
std::shared_ptr< CameraImage > current_image_
void set_jpeg_quality(uint8_t quality)
CallbackManager< void(std::shared_ptr< CameraImage >)> new_image_callback_
void set_wb_mode(ESP32WhiteBalanceMode mode)
ESP32AgcGainCeiling agc_gain_ceiling_
void add_image_callback(std::function< void(std::shared_ptr< CameraImage >)> &&callback)
void set_vertical_flip(bool vertical_flip)
void set_aec_value(uint32_t aec_value)
void request_image(CameraRequester requester)
void set_special_effect(ESP32SpecialEffect effect)
void set_aec_mode(ESP32GainControlMode mode)
CallbackManager< void()> stream_stop_callback_
float get_setup_priority() const override
void set_data_pins(std::array< uint8_t, 8 > pins)
void add_stream_start_callback(std::function< void()> &&callback)
void set_frame_size(ESP32CameraFrameSize size)
void set_external_clock(uint8_t pin, uint32_t frequency)
void set_max_update_interval(uint32_t max_update_interval)
void add_stream_stop_callback(std::function< void()> &&callback)
void stop_stream(CameraRequester requester)
void set_horizontal_mirror(bool horizontal_mirror)
void set_frame_buffer_count(uint8_t fb_count)
void set_agc_gain_ceiling(ESP32AgcGainCeiling gain_ceiling)
void set_agc_mode(ESP32GainControlMode mode)
void set_frame_buffer_mode(camera_grab_mode_t mode)
void set_idle_update_interval(uint32_t idle_update_interval)
CallbackManager< void()> stream_start_callback_
void start_stream(CameraRequester requester)
static void framebuffer_task(void *pv)
ESP32Camera * global_esp32_camera
Providing packet encoding functions for exchanging data with a remote host.
Definition a01nyub.cpp:7