ESPHome  2022.11.3
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  0, // 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  ESP_LOGCONFIG(TAG, " I2C Pins: SDA:%d SCL:%d", conf.pin_sscb_sda, conf.pin_sscb_scl);
58  ESP_LOGCONFIG(TAG, " Reset Pin: %d", conf.pin_reset);
59  switch (this->config_.frame_size) {
60  case FRAMESIZE_QQVGA:
61  ESP_LOGCONFIG(TAG, " Resolution: 160x120 (QQVGA)");
62  break;
63  case FRAMESIZE_QCIF:
64  ESP_LOGCONFIG(TAG, " Resolution: 176x155 (QCIF)");
65  break;
66  case FRAMESIZE_HQVGA:
67  ESP_LOGCONFIG(TAG, " Resolution: 240x176 (HQVGA)");
68  break;
69  case FRAMESIZE_QVGA:
70  ESP_LOGCONFIG(TAG, " Resolution: 320x240 (QVGA)");
71  break;
72  case FRAMESIZE_CIF:
73  ESP_LOGCONFIG(TAG, " Resolution: 400x296 (CIF)");
74  break;
75  case FRAMESIZE_VGA:
76  ESP_LOGCONFIG(TAG, " Resolution: 640x480 (VGA)");
77  break;
78  case FRAMESIZE_SVGA:
79  ESP_LOGCONFIG(TAG, " Resolution: 800x600 (SVGA)");
80  break;
81  case FRAMESIZE_XGA:
82  ESP_LOGCONFIG(TAG, " Resolution: 1024x768 (XGA)");
83  break;
84  case FRAMESIZE_SXGA:
85  ESP_LOGCONFIG(TAG, " Resolution: 1280x1024 (SXGA)");
86  break;
87  case FRAMESIZE_UXGA:
88  ESP_LOGCONFIG(TAG, " Resolution: 1600x1200 (UXGA)");
89  break;
90  default:
91  break;
92  }
93 
94  if (this->is_failed()) {
95  ESP_LOGE(TAG, " Setup Failed: %s", esp_err_to_name(this->init_error_));
96  return;
97  }
98 
99  sensor_t *s = esp_camera_sensor_get();
100  auto st = s->status;
101  ESP_LOGCONFIG(TAG, " JPEG Quality: %u", st.quality);
102  // ESP_LOGCONFIG(TAG, " Framebuffer Count: %u", conf.fb_count);
103  ESP_LOGCONFIG(TAG, " Contrast: %d", st.contrast);
104  ESP_LOGCONFIG(TAG, " Brightness: %d", st.brightness);
105  ESP_LOGCONFIG(TAG, " Saturation: %d", st.saturation);
106  ESP_LOGCONFIG(TAG, " Vertical Flip: %s", ONOFF(st.vflip));
107  ESP_LOGCONFIG(TAG, " Horizontal Mirror: %s", ONOFF(st.hmirror));
108  ESP_LOGCONFIG(TAG, " Special Effect: %u", st.special_effect);
109  ESP_LOGCONFIG(TAG, " White Balance Mode: %u", st.wb_mode);
110  // ESP_LOGCONFIG(TAG, " Auto White Balance: %u", st.awb);
111  // ESP_LOGCONFIG(TAG, " Auto White Balance Gain: %u", st.awb_gain);
112  ESP_LOGCONFIG(TAG, " Auto Exposure Control: %u", st.aec);
113  ESP_LOGCONFIG(TAG, " Auto Exposure Control 2: %u", st.aec2);
114  ESP_LOGCONFIG(TAG, " Auto Exposure Level: %d", st.ae_level);
115  ESP_LOGCONFIG(TAG, " Auto Exposure Value: %u", st.aec_value);
116  ESP_LOGCONFIG(TAG, " AGC: %u", st.agc);
117  ESP_LOGCONFIG(TAG, " AGC Gain: %u", st.agc_gain);
118  ESP_LOGCONFIG(TAG, " Gain Ceiling: %u", st.gainceiling);
119  // ESP_LOGCONFIG(TAG, " BPC: %u", st.bpc);
120  // ESP_LOGCONFIG(TAG, " WPC: %u", st.wpc);
121  // ESP_LOGCONFIG(TAG, " RAW_GMA: %u", st.raw_gma);
122  // ESP_LOGCONFIG(TAG, " Lens Correction: %u", st.lenc);
123  // ESP_LOGCONFIG(TAG, " DCW: %u", st.dcw);
124  ESP_LOGCONFIG(TAG, " Test Pattern: %s", YESNO(st.colorbar));
125 }
126 
128  // check if we can return the image
129  if (this->can_return_image_()) {
130  // return image
131  auto *fb = this->current_image_->get_raw_buffer();
132  xQueueSend(this->framebuffer_return_queue_, &fb, portMAX_DELAY);
133  this->current_image_.reset();
134  }
135 
136  // request idle image every idle_update_interval
137  const uint32_t now = millis();
138  if (this->idle_update_interval_ != 0 && now - this->last_idle_request_ > this->idle_update_interval_) {
139  this->last_idle_request_ = now;
140  this->request_image(IDLE);
141  }
142 
143  // Check if we should fetch a new image
144  if (!this->has_requested_image_())
145  return;
146  if (this->current_image_.use_count() > 1) {
147  // image is still in use
148  return;
149  }
150  if (now - this->last_update_ <= this->max_update_interval_)
151  return;
152 
153  // request new image
154  camera_fb_t *fb;
155  if (xQueueReceive(this->framebuffer_get_queue_, &fb, 0L) != pdTRUE) {
156  // no frame ready
157  ESP_LOGVV(TAG, "No frame ready");
158  return;
159  }
160 
161  if (fb == nullptr) {
162  ESP_LOGW(TAG, "Got invalid frame from camera!");
163  xQueueSend(this->framebuffer_return_queue_, &fb, portMAX_DELAY);
164  return;
165  }
166  this->current_image_ = std::make_shared<CameraImage>(fb, this->single_requesters_ | this->stream_requesters_);
167 
168  ESP_LOGD(TAG, "Got Image: len=%u", fb->len);
169  this->new_image_callback_.call(this->current_image_);
170  this->last_update_ = now;
171  this->single_requesters_ = 0;
172 }
173 
175 
176 /* ---------------- constructors ---------------- */
177 ESP32Camera::ESP32Camera(const std::string &name) : EntityBase(name) {
178  this->config_.pin_pwdn = -1;
179  this->config_.pin_reset = -1;
180  this->config_.pin_xclk = -1;
181  this->config_.ledc_timer = LEDC_TIMER_0;
182  this->config_.ledc_channel = LEDC_CHANNEL_0;
183  this->config_.pixel_format = PIXFORMAT_JPEG;
184  this->config_.frame_size = FRAMESIZE_VGA; // 640x480
185  this->config_.jpeg_quality = 10;
186  this->config_.fb_count = 1;
187 
188  global_esp32_camera = this;
189 }
191 
192 /* ---------------- setters ---------------- */
193 /* set pin assignment */
194 void ESP32Camera::set_data_pins(std::array<uint8_t, 8> pins) {
195  this->config_.pin_d0 = pins[0];
196  this->config_.pin_d1 = pins[1];
197  this->config_.pin_d2 = pins[2];
198  this->config_.pin_d3 = pins[3];
199  this->config_.pin_d4 = pins[4];
200  this->config_.pin_d5 = pins[5];
201  this->config_.pin_d6 = pins[6];
202  this->config_.pin_d7 = pins[7];
203 }
204 void ESP32Camera::set_vsync_pin(uint8_t pin) { this->config_.pin_vsync = pin; }
205 void ESP32Camera::set_href_pin(uint8_t pin) { this->config_.pin_href = pin; }
206 void ESP32Camera::set_pixel_clock_pin(uint8_t pin) { this->config_.pin_pclk = pin; }
207 void ESP32Camera::set_external_clock(uint8_t pin, uint32_t frequency) {
208  this->config_.pin_xclk = pin;
209  this->config_.xclk_freq_hz = frequency;
210 }
211 void ESP32Camera::set_i2c_pins(uint8_t sda, uint8_t scl) {
212  this->config_.pin_sscb_sda = sda;
213  this->config_.pin_sscb_scl = scl;
214 }
215 void ESP32Camera::set_reset_pin(uint8_t pin) { this->config_.pin_reset = pin; }
216 void ESP32Camera::set_power_down_pin(uint8_t pin) { this->config_.pin_pwdn = pin; }
217 
218 /* set image parameters */
220  switch (size) {
222  this->config_.frame_size = FRAMESIZE_QQVGA;
223  break;
225  this->config_.frame_size = FRAMESIZE_QCIF;
226  break;
228  this->config_.frame_size = FRAMESIZE_HQVGA;
229  break;
231  this->config_.frame_size = FRAMESIZE_QVGA;
232  break;
234  this->config_.frame_size = FRAMESIZE_CIF;
235  break;
237  this->config_.frame_size = FRAMESIZE_VGA;
238  break;
240  this->config_.frame_size = FRAMESIZE_SVGA;
241  break;
243  this->config_.frame_size = FRAMESIZE_XGA;
244  break;
246  this->config_.frame_size = FRAMESIZE_SXGA;
247  break;
249  this->config_.frame_size = FRAMESIZE_UXGA;
250  break;
251  }
252 }
253 void ESP32Camera::set_jpeg_quality(uint8_t quality) { this->config_.jpeg_quality = quality; }
254 void ESP32Camera::set_vertical_flip(bool vertical_flip) { this->vertical_flip_ = vertical_flip; }
255 void ESP32Camera::set_horizontal_mirror(bool horizontal_mirror) { this->horizontal_mirror_ = horizontal_mirror; }
256 void ESP32Camera::set_contrast(int contrast) { this->contrast_ = contrast; }
257 void ESP32Camera::set_brightness(int brightness) { this->brightness_ = brightness; }
258 void ESP32Camera::set_saturation(int saturation) { this->saturation_ = saturation; }
260 /* set exposure parameters */
262 void ESP32Camera::set_aec2(bool aec2) { this->aec2_ = aec2; }
263 void ESP32Camera::set_ae_level(int ae_level) { this->ae_level_ = ae_level; }
264 void ESP32Camera::set_aec_value(uint32_t aec_value) { this->aec_value_ = aec_value; }
265 /* set gains parameters */
267 void ESP32Camera::set_agc_value(uint8_t agc_value) { this->agc_value_ = agc_value; }
268 void ESP32Camera::set_agc_gain_ceiling(ESP32AgcGainCeiling gain_ceiling) { this->agc_gain_ceiling_ = gain_ceiling; }
269 /* set white balance */
271 /* set test mode */
272 void ESP32Camera::set_test_pattern(bool test_pattern) { this->test_pattern_ = test_pattern; }
273 /* set fps */
274 void ESP32Camera::set_max_update_interval(uint32_t max_update_interval) {
275  this->max_update_interval_ = max_update_interval;
276 }
277 void ESP32Camera::set_idle_update_interval(uint32_t idle_update_interval) {
278  this->idle_update_interval_ = idle_update_interval;
279 }
280 
281 /* ---------------- public API (specific) ---------------- */
282 void ESP32Camera::add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&f) {
283  this->new_image_callback_.add(std::move(f));
284 }
285 void ESP32Camera::add_stream_start_callback(std::function<void()> &&callback) {
286  this->stream_start_callback_.add(std::move(callback));
287 }
288 void ESP32Camera::add_stream_stop_callback(std::function<void()> &&callback) {
289  this->stream_stop_callback_.add(std::move(callback));
290 }
292  this->stream_start_callback_.call();
293  this->stream_requesters_ |= (1U << requester);
294 }
296  this->stream_stop_callback_.call();
297  this->stream_requesters_ &= ~(1U << requester);
298 }
299 void ESP32Camera::request_image(CameraRequester requester) { this->single_requesters_ |= (1U << requester); }
301  sensor_t *s = esp_camera_sensor_get();
302  /* update image */
303  s->set_vflip(s, this->vertical_flip_);
304  s->set_hmirror(s, this->horizontal_mirror_);
305  s->set_contrast(s, this->contrast_);
306  s->set_brightness(s, this->brightness_);
307  s->set_saturation(s, this->saturation_);
308  s->set_special_effect(s, (int) this->special_effect_); // 0 to 6
309  /* update exposure */
310  s->set_exposure_ctrl(s, (bool) this->aec_mode_);
311  s->set_aec2(s, this->aec2_); // 0 = disable , 1 = enable
312  s->set_ae_level(s, this->ae_level_); // -2 to 2
313  s->set_aec_value(s, this->aec_value_); // 0 to 1200
314  /* update gains */
315  s->set_gain_ctrl(s, (bool) this->agc_mode_);
316  s->set_agc_gain(s, (int) this->agc_value_); // 0 to 30
317  s->set_gainceiling(s, (gainceiling_t) this->agc_gain_ceiling_);
318  /* update white balance mode */
319  s->set_wb_mode(s, (int) this->wb_mode_); // 0 to 4
320  /* update test pattern */
321  s->set_colorbar(s, this->test_pattern_);
322 }
323 
324 /* ---------------- Internal methods ---------------- */
326 bool ESP32Camera::can_return_image_() const { return this->current_image_.use_count() == 1; }
328  while (true) {
329  camera_fb_t *framebuffer = esp_camera_fb_get();
330  xQueueSend(global_esp32_camera->framebuffer_get_queue_, &framebuffer, portMAX_DELAY);
331  // return is no-op for config with 1 fb
332  xQueueReceive(global_esp32_camera->framebuffer_return_queue_, &framebuffer, portMAX_DELAY);
333  esp_camera_fb_return(framebuffer);
334  }
335 }
336 
337 ESP32Camera *global_esp32_camera; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
338 
339 /* ---------------- CameraImageReader class ---------------- */
340 void CameraImageReader::set_image(std::shared_ptr<CameraImage> image) {
341  this->image_ = std::move(image);
342  this->offset_ = 0;
343 }
345  if (!this->image_)
346  return 0;
347 
348  return this->image_->get_data_length() - this->offset_;
349 }
350 void CameraImageReader::return_image() { this->image_.reset(); }
351 void CameraImageReader::consume_data(size_t consumed) { this->offset_ += consumed; }
352 uint8_t *CameraImageReader::peek_data_buffer() { return this->image_->get_data_buffer() + this->offset_; }
353 
354 /* ---------------- CameraImage class ---------------- */
355 CameraImage::CameraImage(camera_fb_t *buffer, uint8_t requesters) : buffer_(buffer), requesters_(requesters) {}
356 
357 camera_fb_t *CameraImage::get_raw_buffer() { return this->buffer_; }
358 uint8_t *CameraImage::get_data_buffer() { return this->buffer_->buf; }
359 size_t CameraImage::get_data_length() { return this->buffer_->len; }
361  return (this->requesters_ & (1 << requester)) != 0;
362 }
363 
364 } // namespace esp32_camera
365 } // namespace esphome
366 
367 #endif
std::shared_ptr< CameraImage > current_image_
Definition: esp32_camera.h:187
void set_test_pattern(bool test_pattern)
const char * name
Definition: stm32flash.h:78
ESP32WhiteBalanceMode wb_mode_
Definition: esp32_camera.h:179
const float DATA
For components that import data from directly connected sensors like DHT.
Definition: component.cpp:18
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)
std::string name_
Definition: entity_base.h:54
void set_jpeg_quality(uint8_t quality)
void add_image_callback(std::function< void(std::shared_ptr< CameraImage >)> &&f)
float get_setup_priority() const override
void stop_stream(CameraRequester requester)
uint32_t IRAM_ATTR HOT millis()
Definition: core.cpp:26
ESP32GainControlMode aec_mode_
Definition: esp32_camera.h:170
CallbackManager< void()> stream_stop_callback_
Definition: esp32_camera.h:194
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:175
void set_external_clock(uint8_t pin, uint32_t frequency)
CallbackManager< void()> stream_start_callback_
Definition: esp32_camera.h:193
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:192
static void framebuffer_task(void *pv)
ESP32AgcGainCeiling agc_gain_ceiling_
Definition: esp32_camera.h:177
void set_horizontal_mirror(bool horizontal_mirror)
bool was_requested_by(CameraRequester requester) const
void set_image(std::shared_ptr< CameraImage > image)
ESP32SpecialEffect special_effect_
Definition: esp32_camera.h:168
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:112
void set_agc_value(uint8_t agc_value)
CameraImage(camera_fb_t *buffer, uint8_t requester)
void set_special_effect(ESP32SpecialEffect effect)
Definition: a4988.cpp:4
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)