ESPHome  2023.11.6
esp32_camera.h
Go to the documentation of this file.
1 #pragma once
2 
3 #ifdef USE_ESP32
4 
8 #include "esphome/core/helpers.h"
9 #include <esp_camera.h>
10 #include <freertos/FreeRTOS.h>
11 #include <freertos/queue.h>
12 
13 namespace esphome {
14 namespace esp32_camera {
15 
16 class ESP32Camera;
17 
18 /* ---------------- enum classes ---------------- */
20 
40 };
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 
55 };
56 
63 };
64 
73 };
74 
75 /* ---------------- CameraImage class ---------------- */
76 class CameraImage {
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 
89 /* ---------------- CameraImageReader class ---------------- */
91  public:
92  void set_image(std::shared_ptr<CameraImage> image);
93  size_t available() const;
94  uint8_t *peek_data_buffer();
95  void consume_data(size_t consumed);
96  void return_image();
97 
98  protected:
99  std::shared_ptr<CameraImage> image_;
100  size_t offset_{0};
101 };
102 
103 /* ---------------- ESP32Camera class ---------------- */
104 class ESP32Camera : public Component, public EntityBase {
105  public:
106  ESP32Camera();
107 
108  /* setters */
109  /* -- pin assignment */
110  void set_data_pins(std::array<uint8_t, 8> pins);
111  void set_vsync_pin(uint8_t pin);
112  void set_href_pin(uint8_t pin);
113  void set_pixel_clock_pin(uint8_t pin);
114  void set_external_clock(uint8_t pin, uint32_t frequency);
115  void set_i2c_pins(uint8_t sda, uint8_t scl);
116  void set_reset_pin(uint8_t pin);
117  void set_power_down_pin(uint8_t pin);
118  /* -- image */
119  void set_frame_size(ESP32CameraFrameSize size);
120  void set_jpeg_quality(uint8_t quality);
121  void set_vertical_flip(bool vertical_flip);
122  void set_horizontal_mirror(bool horizontal_mirror);
123  void set_contrast(int contrast);
124  void set_brightness(int brightness);
125  void set_saturation(int saturation);
126  void set_special_effect(ESP32SpecialEffect effect);
127  /* -- exposure */
128  void set_aec_mode(ESP32GainControlMode mode);
129  void set_aec2(bool aec2);
130  void set_ae_level(int ae_level);
131  void set_aec_value(uint32_t aec_value);
132  /* -- gains */
133  void set_agc_mode(ESP32GainControlMode mode);
134  void set_agc_value(uint8_t agc_value);
135  void set_agc_gain_ceiling(ESP32AgcGainCeiling gain_ceiling);
136  /* -- white balance */
137  void set_wb_mode(ESP32WhiteBalanceMode mode);
138  /* -- test */
139  void set_test_pattern(bool test_pattern);
140  /* -- framerates */
141  void set_max_update_interval(uint32_t max_update_interval);
142  void set_idle_update_interval(uint32_t idle_update_interval);
143 
144  /* public API (derivated) */
145  void setup() override;
146  void loop() override;
147  void dump_config() override;
148  float get_setup_priority() const override;
149  /* public API (specific) */
150  void add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&f);
151  void start_stream(CameraRequester requester);
152  void stop_stream(CameraRequester requester);
153  void request_image(CameraRequester requester);
154  void update_camera_parameters();
155 
156  void add_stream_start_callback(std::function<void()> &&callback);
157  void add_stream_stop_callback(std::function<void()> &&callback);
158 
159  protected:
160  /* internal methods */
161  bool has_requested_image_() const;
162  bool can_return_image_() const;
163 
164  static void framebuffer_task(void *pv);
165 
166  /* attributes */
167  /* camera configuration */
168  camera_config_t config_{};
169  /* -- image */
170  bool vertical_flip_{true};
171  bool horizontal_mirror_{true};
172  int contrast_{0};
173  int brightness_{0};
174  int saturation_{0};
176  /* -- exposure */
178  bool aec2_{false};
179  int ae_level_{0};
180  uint32_t aec_value_{300};
181  /* -- gains */
183  uint8_t agc_value_{0};
185  /* -- white balance */
187  /* -- Test */
188  bool test_pattern_{false};
189  /* -- framerates */
190  uint32_t max_update_interval_{1000};
191  uint32_t idle_update_interval_{15000};
192 
193  esp_err_t init_error_{ESP_OK};
194  std::shared_ptr<CameraImage> current_image_;
195  uint8_t single_requesters_{0};
196  uint8_t stream_requesters_{0};
197  QueueHandle_t framebuffer_get_queue_;
200  CallbackManager<void()> stream_start_callback_{};
201  CallbackManager<void()> stream_stop_callback_{};
202 
203  uint32_t last_idle_request_{0};
204  uint32_t last_update_{0};
205 };
206 
207 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
209 
211  public:
213  parent->add_stream_start_callback([this]() { this->trigger(); });
214  }
215 
216  protected:
217 };
219  public:
221  parent->add_stream_stop_callback([this]() { this->trigger(); });
222  }
223 
224  protected:
225 };
226 
227 } // namespace esp32_camera
228 } // namespace esphome
229 
230 #endif
void setup()
std::shared_ptr< CameraImage > current_image_
Definition: esp32_camera.h:194
void loop()
std::shared_ptr< CameraImage > image_
Definition: esp32_camera.h:99
void add_stream_start_callback(std::function< void()> &&callback)
ESP32Camera * global_esp32_camera
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:151
void add_stream_stop_callback(std::function< void()> &&callback)
uint16_le_t frequency
Definition: bl0942.h:21
bool was_requested_by(CameraRequester requester) const
CameraImage(camera_fb_t *buffer, uint8_t requester)
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7