ESPHome  2023.3.1
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 
32 };
33 
35  ESP32_GAINCEILING_2X = GAINCEILING_2X,
36  ESP32_GAINCEILING_4X = GAINCEILING_4X,
37  ESP32_GAINCEILING_8X = GAINCEILING_8X,
38  ESP32_GAINCEILING_16X = GAINCEILING_16X,
39  ESP32_GAINCEILING_32X = GAINCEILING_32X,
40  ESP32_GAINCEILING_64X = GAINCEILING_64X,
41  ESP32_GAINCEILING_128X = GAINCEILING_128X,
42 };
43 
47 };
48 
55 };
56 
65 };
66 
67 /* ---------------- CameraImage class ---------------- */
68 class CameraImage {
69  public:
70  CameraImage(camera_fb_t *buffer, uint8_t requester);
71  camera_fb_t *get_raw_buffer();
72  uint8_t *get_data_buffer();
73  size_t get_data_length();
74  bool was_requested_by(CameraRequester requester) const;
75 
76  protected:
77  camera_fb_t *buffer_;
78  uint8_t requesters_;
79 };
80 
81 /* ---------------- CameraImageReader class ---------------- */
83  public:
84  void set_image(std::shared_ptr<CameraImage> image);
85  size_t available() const;
86  uint8_t *peek_data_buffer();
87  void consume_data(size_t consumed);
88  void return_image();
89 
90  protected:
91  std::shared_ptr<CameraImage> image_;
92  size_t offset_{0};
93 };
94 
95 /* ---------------- ESP32Camera class ---------------- */
96 class ESP32Camera : public Component, public EntityBase {
97  public:
98  ESP32Camera(const std::string &name);
99  ESP32Camera();
100 
101  /* setters */
102  /* -- pin assignment */
103  void set_data_pins(std::array<uint8_t, 8> pins);
104  void set_vsync_pin(uint8_t pin);
105  void set_href_pin(uint8_t pin);
106  void set_pixel_clock_pin(uint8_t pin);
107  void set_external_clock(uint8_t pin, uint32_t frequency);
108  void set_i2c_pins(uint8_t sda, uint8_t scl);
109  void set_reset_pin(uint8_t pin);
110  void set_power_down_pin(uint8_t pin);
111  /* -- image */
112  void set_frame_size(ESP32CameraFrameSize size);
113  void set_jpeg_quality(uint8_t quality);
114  void set_vertical_flip(bool vertical_flip);
115  void set_horizontal_mirror(bool horizontal_mirror);
116  void set_contrast(int contrast);
117  void set_brightness(int brightness);
118  void set_saturation(int saturation);
119  void set_special_effect(ESP32SpecialEffect effect);
120  /* -- exposure */
121  void set_aec_mode(ESP32GainControlMode mode);
122  void set_aec2(bool aec2);
123  void set_ae_level(int ae_level);
124  void set_aec_value(uint32_t aec_value);
125  /* -- gains */
126  void set_agc_mode(ESP32GainControlMode mode);
127  void set_agc_value(uint8_t agc_value);
128  void set_agc_gain_ceiling(ESP32AgcGainCeiling gain_ceiling);
129  /* -- white balance */
130  void set_wb_mode(ESP32WhiteBalanceMode mode);
131  /* -- test */
132  void set_test_pattern(bool test_pattern);
133  /* -- framerates */
134  void set_max_update_interval(uint32_t max_update_interval);
135  void set_idle_update_interval(uint32_t idle_update_interval);
136 
137  /* public API (derivated) */
138  void setup() override;
139  void loop() override;
140  void dump_config() override;
141  float get_setup_priority() const override;
142  /* public API (specific) */
143  void add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&f);
144  void start_stream(CameraRequester requester);
145  void stop_stream(CameraRequester requester);
146  void request_image(CameraRequester requester);
147  void update_camera_parameters();
148 
149  void add_stream_start_callback(std::function<void()> &&callback);
150  void add_stream_stop_callback(std::function<void()> &&callback);
151 
152  protected:
153  /* internal methods */
154  bool has_requested_image_() const;
155  bool can_return_image_() const;
156 
157  static void framebuffer_task(void *pv);
158 
159  /* attributes */
160  /* camera configuration */
161  camera_config_t config_{};
162  /* -- image */
163  bool vertical_flip_{true};
164  bool horizontal_mirror_{true};
165  int contrast_{0};
166  int brightness_{0};
167  int saturation_{0};
169  /* -- exposure */
171  bool aec2_{false};
172  int ae_level_{0};
173  uint32_t aec_value_{300};
174  /* -- gains */
176  uint8_t agc_value_{0};
178  /* -- white balance */
180  /* -- Test */
181  bool test_pattern_{false};
182  /* -- framerates */
183  uint32_t max_update_interval_{1000};
184  uint32_t idle_update_interval_{15000};
185 
186  esp_err_t init_error_{ESP_OK};
187  std::shared_ptr<CameraImage> current_image_;
188  uint8_t single_requesters_{0};
189  uint8_t stream_requesters_{0};
190  QueueHandle_t framebuffer_get_queue_;
193  CallbackManager<void()> stream_start_callback_{};
194  CallbackManager<void()> stream_stop_callback_{};
195 
196  uint32_t last_idle_request_{0};
197  uint32_t last_update_{0};
198 };
199 
200 // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
202 
204  public:
206  parent->add_stream_start_callback([this]() { this->trigger(); });
207  }
208 
209  protected:
210 };
212  public:
214  parent->add_stream_stop_callback([this]() { this->trigger(); });
215  }
216 
217  protected:
218 };
219 
220 } // namespace esp32_camera
221 } // namespace esphome
222 
223 #endif
void setup()
std::shared_ptr< CameraImage > current_image_
Definition: esp32_camera.h:187
const char * name
Definition: stm32flash.h:78
void loop()
std::shared_ptr< CameraImage > image_
Definition: esp32_camera.h:91
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)
Definition: a4988.cpp:4