ESPHome  2024.5.1
esp32_camera.cpp
Go to the documentation of this file.
1 #ifdef USE_ESP32
2 
3 #include "esp32_camera.h"
4 #include "esphome/core/log.h"
5 #include "esphome/core/hal.h"
6 
7 #include <freertos/task.h>
8 
9 namespace esphome {
10 namespace esp32_camera {
11 
12 static const char *const TAG = "esp32_camera";
13 
14 /* ---------------- public API (derivated) ---------------- */
16  global_esp32_camera = this;
17 
18  /* initialize time to now */
19  this->last_update_ = millis();
20 
21  /* initialize camera */
22  esp_err_t err = esp_camera_init(&this->config_);
23  if (err != ESP_OK) {
24  ESP_LOGE(TAG, "esp_camera_init failed: %s", esp_err_to_name(err));
25  this->init_error_ = err;
26  this->mark_failed();
27  return;
28  }
29 
30  /* initialize camera parameters */
32 
33  /* initialize RTOS */
34  this->framebuffer_get_queue_ = xQueueCreate(1, sizeof(camera_fb_t *));
35  this->framebuffer_return_queue_ = xQueueCreate(1, sizeof(camera_fb_t *));
36  xTaskCreatePinnedToCore(&ESP32Camera::framebuffer_task,
37  "framebuffer_task", // name
38  1024, // stack size
39  nullptr, // task pv params
40  1, // priority
41  nullptr, // handle
42  1 // core
43  );
44 }
45 
47  auto conf = this->config_;
48  ESP_LOGCONFIG(TAG, "ESP32 Camera:");
49  ESP_LOGCONFIG(TAG, " Name: %s", this->name_.c_str());
50  ESP_LOGCONFIG(TAG, " Internal: %s", YESNO(this->internal_));
51  ESP_LOGCONFIG(TAG, " Data Pins: D0:%d D1:%d D2:%d D3:%d D4:%d D5:%d D6:%d D7:%d", conf.pin_d0, conf.pin_d1,
52  conf.pin_d2, conf.pin_d3, conf.pin_d4, conf.pin_d5, conf.pin_d6, conf.pin_d7);
53  ESP_LOGCONFIG(TAG, " VSYNC Pin: %d", conf.pin_vsync);
54  ESP_LOGCONFIG(TAG, " HREF Pin: %d", conf.pin_href);
55  ESP_LOGCONFIG(TAG, " Pixel Clock Pin: %d", conf.pin_pclk);
56  ESP_LOGCONFIG(TAG, " External Clock: Pin:%d Frequency:%u", conf.pin_xclk, conf.xclk_freq_hz);
57 #ifdef USE_ESP_IDF // Temporary until the espressif/esp32-camera library is updated
58  ESP_LOGCONFIG(TAG, " I2C Pins: SDA:%d SCL:%d", conf.pin_sscb_sda, conf.pin_sscb_scl);
59 #else
60  ESP_LOGCONFIG(TAG, " I2C Pins: SDA:%d SCL:%d", conf.pin_sccb_sda, conf.pin_sccb_scl);
61 #endif
62  ESP_LOGCONFIG(TAG, " Reset Pin: %d", conf.pin_reset);
63  switch (this->config_.frame_size) {
64  case FRAMESIZE_QQVGA:
65  ESP_LOGCONFIG(TAG, " Resolution: 160x120 (QQVGA)");
66  break;
67  case FRAMESIZE_QCIF:
68  ESP_LOGCONFIG(TAG, " Resolution: 176x155 (QCIF)");
69  break;
70  case FRAMESIZE_HQVGA:
71  ESP_LOGCONFIG(TAG, " Resolution: 240x176 (HQVGA)");
72  break;
73  case FRAMESIZE_QVGA:
74  ESP_LOGCONFIG(TAG, " Resolution: 320x240 (QVGA)");
75  break;
76  case FRAMESIZE_CIF:
77  ESP_LOGCONFIG(TAG, " Resolution: 400x296 (CIF)");
78  break;
79  case FRAMESIZE_VGA:
80  ESP_LOGCONFIG(TAG, " Resolution: 640x480 (VGA)");
81  break;
82  case FRAMESIZE_SVGA:
83  ESP_LOGCONFIG(TAG, " Resolution: 800x600 (SVGA)");
84  break;
85  case FRAMESIZE_XGA:
86  ESP_LOGCONFIG(TAG, " Resolution: 1024x768 (XGA)");
87  break;
88  case FRAMESIZE_SXGA:
89  ESP_LOGCONFIG(TAG, " Resolution: 1280x1024 (SXGA)");
90  break;
91  case FRAMESIZE_UXGA:
92  ESP_LOGCONFIG(TAG, " Resolution: 1600x1200 (UXGA)");
93  break;
94  case FRAMESIZE_FHD:
95  ESP_LOGCONFIG(TAG, " Resolution: 1920x1080 (FHD)");
96  break;
97  case FRAMESIZE_P_HD:
98  ESP_LOGCONFIG(TAG, " Resolution: 720x1280 (P_HD)");
99  break;
100  case FRAMESIZE_P_3MP:
101  ESP_LOGCONFIG(TAG, " Resolution: 864x1536 (P_3MP)");
102  break;
103  case FRAMESIZE_QXGA:
104  ESP_LOGCONFIG(TAG, " Resolution: 2048x1536 (QXGA)");
105  break;
106  case FRAMESIZE_QHD:
107  ESP_LOGCONFIG(TAG, " Resolution: 2560x1440 (QHD)");
108  break;
109  case FRAMESIZE_WQXGA:
110  ESP_LOGCONFIG(TAG, " Resolution: 2560x1600 (WQXGA)");
111  break;
112  case FRAMESIZE_P_FHD:
113  ESP_LOGCONFIG(TAG, " Resolution: 1080x1920 (P_FHD)");
114  break;
115  case FRAMESIZE_QSXGA:
116  ESP_LOGCONFIG(TAG, " Resolution: 2560x1920 (QSXGA)");
117  break;
118  default:
119  break;
120  }
121 
122  if (this->is_failed()) {
123  ESP_LOGE(TAG, " Setup Failed: %s", esp_err_to_name(this->init_error_));
124  return;
125  }
126 
127  sensor_t *s = esp_camera_sensor_get();
128  auto st = s->status;
129  ESP_LOGCONFIG(TAG, " JPEG Quality: %u", st.quality);
130  // ESP_LOGCONFIG(TAG, " Framebuffer Count: %u", conf.fb_count);
131  ESP_LOGCONFIG(TAG, " Contrast: %d", st.contrast);
132  ESP_LOGCONFIG(TAG, " Brightness: %d", st.brightness);
133  ESP_LOGCONFIG(TAG, " Saturation: %d", st.saturation);
134  ESP_LOGCONFIG(TAG, " Vertical Flip: %s", ONOFF(st.vflip));
135  ESP_LOGCONFIG(TAG, " Horizontal Mirror: %s", ONOFF(st.hmirror));
136  ESP_LOGCONFIG(TAG, " Special Effect: %u", st.special_effect);
137  ESP_LOGCONFIG(TAG, " White Balance Mode: %u", st.wb_mode);
138  // ESP_LOGCONFIG(TAG, " Auto White Balance: %u", st.awb);
139  // ESP_LOGCONFIG(TAG, " Auto White Balance Gain: %u", st.awb_gain);
140  ESP_LOGCONFIG(TAG, " Auto Exposure Control: %u", st.aec);
141  ESP_LOGCONFIG(TAG, " Auto Exposure Control 2: %u", st.aec2);
142  ESP_LOGCONFIG(TAG, " Auto Exposure Level: %d", st.ae_level);
143  ESP_LOGCONFIG(TAG, " Auto Exposure Value: %u", st.aec_value);
144  ESP_LOGCONFIG(TAG, " AGC: %u", st.agc);
145  ESP_LOGCONFIG(TAG, " AGC Gain: %u", st.agc_gain);
146  ESP_LOGCONFIG(TAG, " Gain Ceiling: %u", st.gainceiling);
147  // ESP_LOGCONFIG(TAG, " BPC: %u", st.bpc);
148  // ESP_LOGCONFIG(TAG, " WPC: %u", st.wpc);
149  // ESP_LOGCONFIG(TAG, " RAW_GMA: %u", st.raw_gma);
150  // ESP_LOGCONFIG(TAG, " Lens Correction: %u", st.lenc);
151  // ESP_LOGCONFIG(TAG, " DCW: %u", st.dcw);
152  ESP_LOGCONFIG(TAG, " Test Pattern: %s", YESNO(st.colorbar));
153 }
154 
156  // check if we can return the image
157  if (this->can_return_image_()) {
158  // return image
159  auto *fb = this->current_image_->get_raw_buffer();
160  xQueueSend(this->framebuffer_return_queue_, &fb, portMAX_DELAY);
161  this->current_image_.reset();
162  }
163 
164  // request idle image every idle_update_interval
165  const uint32_t now = millis();
166  if (this->idle_update_interval_ != 0 && now - this->last_idle_request_ > this->idle_update_interval_) {
167  this->last_idle_request_ = now;
168  this->request_image(IDLE);
169  }
170 
171  // Check if we should fetch a new image
172  if (!this->has_requested_image_())
173  return;
174  if (this->current_image_.use_count() > 1) {
175  // image is still in use
176  return;
177  }
178  if (now - this->last_update_ <= this->max_update_interval_)
179  return;
180 
181  // request new image
182  camera_fb_t *fb;
183  if (xQueueReceive(this->framebuffer_get_queue_, &fb, 0L) != pdTRUE) {
184  // no frame ready
185  ESP_LOGVV(TAG, "No frame ready");
186  return;
187  }
188 
189  if (fb == nullptr) {
190  ESP_LOGW(TAG, "Got invalid frame from camera!");
191  xQueueSend(this->framebuffer_return_queue_, &fb, portMAX_DELAY);
192  return;
193  }
194  this->current_image_ = std::make_shared<CameraImage>(fb, this->single_requesters_ | this->stream_requesters_);
195 
196  ESP_LOGD(TAG, "Got Image: len=%u", fb->len);
197  this->new_image_callback_.call(this->current_image_);
198  this->last_update_ = now;
199  this->single_requesters_ = 0;
200 }
201 
203 
204 /* ---------------- constructors ---------------- */
206  this->config_.pin_pwdn = -1;
207  this->config_.pin_reset = -1;
208  this->config_.pin_xclk = -1;
209  this->config_.ledc_timer = LEDC_TIMER_0;
210  this->config_.ledc_channel = LEDC_CHANNEL_0;
211  this->config_.pixel_format = PIXFORMAT_JPEG;
212  this->config_.frame_size = FRAMESIZE_VGA; // 640x480
213  this->config_.jpeg_quality = 10;
214  this->config_.fb_count = 1;
215 
216  global_esp32_camera = this;
217 }
218 
219 /* ---------------- setters ---------------- */
220 /* set pin assignment */
221 void ESP32Camera::set_data_pins(std::array<uint8_t, 8> pins) {
222  this->config_.pin_d0 = pins[0];
223  this->config_.pin_d1 = pins[1];
224  this->config_.pin_d2 = pins[2];
225  this->config_.pin_d3 = pins[3];
226  this->config_.pin_d4 = pins[4];
227  this->config_.pin_d5 = pins[5];
228  this->config_.pin_d6 = pins[6];
229  this->config_.pin_d7 = pins[7];
230 }
231 void ESP32Camera::set_vsync_pin(uint8_t pin) { this->config_.pin_vsync = pin; }
232 void ESP32Camera::set_href_pin(uint8_t pin) { this->config_.pin_href = pin; }
233 void ESP32Camera::set_pixel_clock_pin(uint8_t pin) { this->config_.pin_pclk = pin; }
234 void ESP32Camera::set_external_clock(uint8_t pin, uint32_t frequency) {
235  this->config_.pin_xclk = pin;
236  this->config_.xclk_freq_hz = frequency;
237 }
238 void ESP32Camera::set_i2c_pins(uint8_t sda, uint8_t scl) {
239 #ifdef USE_ESP_IDF // Temporary until the espressif/esp32-camera library is updated
240  this->config_.pin_sscb_sda = sda;
241  this->config_.pin_sscb_scl = scl;
242 #else
243  this->config_.pin_sccb_sda = sda;
244  this->config_.pin_sccb_scl = scl;
245 #endif
246 }
247 void ESP32Camera::set_reset_pin(uint8_t pin) { this->config_.pin_reset = pin; }
248 void ESP32Camera::set_power_down_pin(uint8_t pin) { this->config_.pin_pwdn = pin; }
249 
250 /* set image parameters */
252  switch (size) {
254  this->config_.frame_size = FRAMESIZE_QQVGA;
255  break;
257  this->config_.frame_size = FRAMESIZE_QCIF;
258  break;
260  this->config_.frame_size = FRAMESIZE_HQVGA;
261  break;
263  this->config_.frame_size = FRAMESIZE_QVGA;
264  break;
266  this->config_.frame_size = FRAMESIZE_CIF;
267  break;
269  this->config_.frame_size = FRAMESIZE_VGA;
270  break;
272  this->config_.frame_size = FRAMESIZE_SVGA;
273  break;
275  this->config_.frame_size = FRAMESIZE_XGA;
276  break;
278  this->config_.frame_size = FRAMESIZE_SXGA;
279  break;
281  this->config_.frame_size = FRAMESIZE_UXGA;
282  break;
284  this->config_.frame_size = FRAMESIZE_FHD;
285  break;
287  this->config_.frame_size = FRAMESIZE_P_HD;
288  break;
290  this->config_.frame_size = FRAMESIZE_P_3MP;
291  break;
293  this->config_.frame_size = FRAMESIZE_QXGA;
294  break;
296  this->config_.frame_size = FRAMESIZE_QHD;
297  break;
299  this->config_.frame_size = FRAMESIZE_WQXGA;
300  break;
302  this->config_.frame_size = FRAMESIZE_P_FHD;
303  break;
305  this->config_.frame_size = FRAMESIZE_QSXGA;
306  break;
307  }
308 }
309 void ESP32Camera::set_jpeg_quality(uint8_t quality) { this->config_.jpeg_quality = quality; }
310 void ESP32Camera::set_vertical_flip(bool vertical_flip) { this->vertical_flip_ = vertical_flip; }
311 void ESP32Camera::set_horizontal_mirror(bool horizontal_mirror) { this->horizontal_mirror_ = horizontal_mirror; }
312 void ESP32Camera::set_contrast(int contrast) { this->contrast_ = contrast; }
313 void ESP32Camera::set_brightness(int brightness) { this->brightness_ = brightness; }
314 void ESP32Camera::set_saturation(int saturation) { this->saturation_ = saturation; }
316 /* set exposure parameters */
318 void ESP32Camera::set_aec2(bool aec2) { this->aec2_ = aec2; }
319 void ESP32Camera::set_ae_level(int ae_level) { this->ae_level_ = ae_level; }
320 void ESP32Camera::set_aec_value(uint32_t aec_value) { this->aec_value_ = aec_value; }
321 /* set gains parameters */
323 void ESP32Camera::set_agc_value(uint8_t agc_value) { this->agc_value_ = agc_value; }
324 void ESP32Camera::set_agc_gain_ceiling(ESP32AgcGainCeiling gain_ceiling) { this->agc_gain_ceiling_ = gain_ceiling; }
325 /* set white balance */
327 /* set test mode */
328 void ESP32Camera::set_test_pattern(bool test_pattern) { this->test_pattern_ = test_pattern; }
329 /* set fps */
330 void ESP32Camera::set_max_update_interval(uint32_t max_update_interval) {
331  this->max_update_interval_ = max_update_interval;
332 }
333 void ESP32Camera::set_idle_update_interval(uint32_t idle_update_interval) {
334  this->idle_update_interval_ = idle_update_interval;
335 }
336 
337 /* ---------------- public API (specific) ---------------- */
338 void ESP32Camera::add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&callback) {
339  this->new_image_callback_.add(std::move(callback));
340 }
341 void ESP32Camera::add_stream_start_callback(std::function<void()> &&callback) {
342  this->stream_start_callback_.add(std::move(callback));
343 }
344 void ESP32Camera::add_stream_stop_callback(std::function<void()> &&callback) {
345  this->stream_stop_callback_.add(std::move(callback));
346 }
348  this->stream_start_callback_.call();
349  this->stream_requesters_ |= (1U << requester);
350 }
352  this->stream_stop_callback_.call();
353  this->stream_requesters_ &= ~(1U << requester);
354 }
355 void ESP32Camera::request_image(CameraRequester requester) { this->single_requesters_ |= (1U << requester); }
357  sensor_t *s = esp_camera_sensor_get();
358  /* update image */
359  s->set_vflip(s, this->vertical_flip_);
360  s->set_hmirror(s, this->horizontal_mirror_);
361  s->set_contrast(s, this->contrast_);
362  s->set_brightness(s, this->brightness_);
363  s->set_saturation(s, this->saturation_);
364  s->set_special_effect(s, (int) this->special_effect_); // 0 to 6
365  /* update exposure */
366  s->set_exposure_ctrl(s, (bool) this->aec_mode_);
367  s->set_aec2(s, this->aec2_); // 0 = disable , 1 = enable
368  s->set_ae_level(s, this->ae_level_); // -2 to 2
369  s->set_aec_value(s, this->aec_value_); // 0 to 1200
370  /* update gains */
371  s->set_gain_ctrl(s, (bool) this->agc_mode_);
372  s->set_agc_gain(s, (int) this->agc_value_); // 0 to 30
373  s->set_gainceiling(s, (gainceiling_t) this->agc_gain_ceiling_);
374  /* update white balance mode */
375  s->set_wb_mode(s, (int) this->wb_mode_); // 0 to 4
376  /* update test pattern */
377  s->set_colorbar(s, this->test_pattern_);
378 }
379 
380 /* ---------------- Internal methods ---------------- */
382 bool ESP32Camera::can_return_image_() const { return this->current_image_.use_count() == 1; }
384  while (true) {
385  camera_fb_t *framebuffer = esp_camera_fb_get();
386  xQueueSend(global_esp32_camera->framebuffer_get_queue_, &framebuffer, portMAX_DELAY);
387  // return is no-op for config with 1 fb
388  xQueueReceive(global_esp32_camera->framebuffer_return_queue_, &framebuffer, portMAX_DELAY);
389  esp_camera_fb_return(framebuffer);
390  }
391 }
392 
393 ESP32Camera *global_esp32_camera; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
394 
395 /* ---------------- CameraImageReader class ---------------- */
396 void CameraImageReader::set_image(std::shared_ptr<CameraImage> image) {
397  this->image_ = std::move(image);
398  this->offset_ = 0;
399 }
401  if (!this->image_)
402  return 0;
403 
404  return this->image_->get_data_length() - this->offset_;
405 }
406 void CameraImageReader::return_image() { this->image_.reset(); }
407 void CameraImageReader::consume_data(size_t consumed) { this->offset_ += consumed; }
408 uint8_t *CameraImageReader::peek_data_buffer() { return this->image_->get_data_buffer() + this->offset_; }
409 
410 /* ---------------- CameraImage class ---------------- */
411 CameraImage::CameraImage(camera_fb_t *buffer, uint8_t requesters) : buffer_(buffer), requesters_(requesters) {}
412 
413 camera_fb_t *CameraImage::get_raw_buffer() { return this->buffer_; }
414 uint8_t *CameraImage::get_data_buffer() { return this->buffer_->buf; }
415 size_t CameraImage::get_data_length() { return this->buffer_->len; }
417  return (this->requesters_ & (1 << requester)) != 0;
418 }
419 
420 } // namespace esp32_camera
421 } // namespace esphome
422 
423 #endif
std::shared_ptr< CameraImage > current_image_
Definition: esp32_camera.h:199
void set_test_pattern(bool test_pattern)
ESP32WhiteBalanceMode wb_mode_
Definition: esp32_camera.h:191
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:19
void request_image(CameraRequester requester)
void add_stream_start_callback(std::function< void()> &&callback)
void set_vertical_flip(bool vertical_flip)
void set_agc_mode(ESP32GainControlMode mode)
void start_stream(CameraRequester requester)
void set_max_update_interval(uint32_t max_update_interval)
void set_wb_mode(ESP32WhiteBalanceMode mode)
void set_jpeg_quality(uint8_t quality)
void add_image_callback(std::function< void(std::shared_ptr< CameraImage >)> &&callback)
float get_setup_priority() const override
void stop_stream(CameraRequester requester)
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:25
ESP32GainControlMode aec_mode_
Definition: esp32_camera.h:182
CallbackManager< void()> stream_stop_callback_
Definition: esp32_camera.h:206
void set_aec_mode(ESP32GainControlMode mode)
ESP32Camera * global_esp32_camera
BedjetMode mode
BedJet operating mode.
Definition: bedjet_codec.h:151
void add_stream_stop_callback(std::function< void()> &&callback)
ESP32GainControlMode agc_mode_
Definition: esp32_camera.h:187
void set_external_clock(uint8_t pin, uint32_t frequency)
CallbackManager< void()> stream_start_callback_
Definition: esp32_camera.h:205
uint16_le_t frequency
Definition: bl0942.h:21
void set_data_pins(std::array< uint8_t, 8 > pins)
void set_agc_gain_ceiling(ESP32AgcGainCeiling gain_ceiling)
CallbackManager< void(std::shared_ptr< CameraImage >)> new_image_callback_
Definition: esp32_camera.h:204
static void framebuffer_task(void *pv)
ESP32AgcGainCeiling agc_gain_ceiling_
Definition: esp32_camera.h:189
void set_horizontal_mirror(bool horizontal_mirror)
bool was_requested_by(CameraRequester requester) const
constexpr const char * c_str() const
Definition: string_ref.h:68
void set_image(std::shared_ptr< CameraImage > image)
ESP32SpecialEffect special_effect_
Definition: esp32_camera.h:180
void set_aec_value(uint32_t aec_value)
void set_frame_size(ESP32CameraFrameSize size)
virtual void mark_failed()
Mark this component as failed.
Definition: component.cpp:118
void set_agc_value(uint8_t agc_value)
CameraImage(camera_fb_t *buffer, uint8_t requester)
void set_special_effect(ESP32SpecialEffect effect)
This is a workaround until we can figure out a way to get the tflite-micro idf component code availab...
Definition: a01nyub.cpp:7
void set_idle_update_interval(uint32_t idle_update_interval)
void set_i2c_pins(uint8_t sda, uint8_t scl)
void set_brightness(int brightness)
void set_saturation(int saturation)