ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
esp32_camera.cpp
Go to the documentation of this file.
1#ifdef USE_ESP32
2
3#include "esp32_camera.h"
5#include "esphome/core/hal.h"
6#include "esphome/core/log.h"
7
8#include <freertos/task.h>
9
11
12ESPHOME_LOG_TAG(TAG, "esp32_camera");
13static constexpr size_t FRAMEBUFFER_TASK_STACK_SIZE = 1792;
14#if ESPHOME_LOG_LEVEL < ESPHOME_LOG_LEVEL_VERBOSE
15static constexpr uint32_t FRAME_LOG_INTERVAL_MS = 60000;
16#endif
17#ifdef USE_ESP32_CAMERA_CAPTURE
18// Below the 5 s snapshot timeout of esp32_camera_web_server, at least 1 s after the longest capture_delay
19static constexpr uint32_t CAPTURE_TIMEOUT_MS = 4000;
20#endif
21
22static const char *frame_size_to_str(framesize_t size) {
23 switch (size) {
24 case FRAMESIZE_QQVGA:
25 return "160x120 (QQVGA)";
26 case FRAMESIZE_QCIF:
27 return "176x155 (QCIF)";
28 case FRAMESIZE_HQVGA:
29 return "240x176 (HQVGA)";
30 case FRAMESIZE_QVGA:
31 return "320x240 (QVGA)";
32 case FRAMESIZE_CIF:
33 return "400x296 (CIF)";
34 case FRAMESIZE_VGA:
35 return "640x480 (VGA)";
36 case FRAMESIZE_SVGA:
37 return "800x600 (SVGA)";
38 case FRAMESIZE_XGA:
39 return "1024x768 (XGA)";
40 case FRAMESIZE_SXGA:
41 return "1280x1024 (SXGA)";
42 case FRAMESIZE_UXGA:
43 return "1600x1200 (UXGA)";
44 case FRAMESIZE_FHD:
45 return "1920x1080 (FHD)";
46 case FRAMESIZE_P_HD:
47 return "720x1280 (P_HD)";
48 case FRAMESIZE_P_3MP:
49 return "864x1536 (P_3MP)";
50 case FRAMESIZE_QXGA:
51 return "2048x1536 (QXGA)";
52 case FRAMESIZE_QHD:
53 return "2560x1440 (QHD)";
54 case FRAMESIZE_WQXGA:
55 return "2560x1600 (WQXGA)";
56 case FRAMESIZE_P_FHD:
57 return "1080x1920 (P_FHD)";
58 case FRAMESIZE_QSXGA:
59 return "2560x1920 (QSXGA)";
60 default:
61 return "UNKNOWN";
62 }
63}
64
65static const char *pixel_format_to_str(pixformat_t format) {
66 switch (format) {
67 case PIXFORMAT_RGB565:
68 return "RGB565";
69 case PIXFORMAT_YUV422:
70 return "YUV422";
71 case PIXFORMAT_YUV420:
72 return "YUV420";
73 case PIXFORMAT_GRAYSCALE:
74 return "GRAYSCALE";
75 case PIXFORMAT_JPEG:
76 return "JPEG";
77 case PIXFORMAT_RGB888:
78 return "RGB888";
79 case PIXFORMAT_RAW:
80 return "RAW";
81 case PIXFORMAT_RGB444:
82 return "RGB444";
83 case PIXFORMAT_RGB555:
84 return "RGB555";
85 default:
86 return "UNKNOWN";
87 }
88}
89
90/* ---------------- public API (derivated) ---------------- */
92#ifdef USE_I2C
93 if (this->i2c_bus_ != nullptr) {
94 this->config_.sccb_i2c_port = this->i2c_bus_->get_port();
95 }
96#endif
97
98 /* initialize time to now */
99 this->last_update_ = millis();
100
101 /* initialize camera */
102 esp_err_t err = esp_camera_init(&this->config_);
103 if (err != ESP_OK) {
104 ESP_LOGE(TAG, "esp_camera_init failed: %s", esp_err_to_name(err));
105 this->init_error_ = err;
106 this->mark_failed();
107 return;
108 }
109
110 /* initialize camera parameters */
112
113 /* initialize RTOS */
114 this->framebuffer_get_queue_ = xQueueCreate(1, sizeof(camera_fb_t *));
115 this->framebuffer_return_queue_ = xQueueCreate(1, sizeof(camera_fb_t *));
116 xTaskCreatePinnedToCore(&ESP32Camera::framebuffer_task,
117 "framebuffer_task", // name
118 FRAMEBUFFER_TASK_STACK_SIZE, // stack size
119 this, // task pv params
120 1, // priority
121 nullptr, // handle
122 1 // core
123 );
124}
125
127 auto conf = this->config_;
128 ESP_LOGCONFIG(TAG,
129 "ESP32 Camera:\n"
130 " Name: %s\n"
131 " Internal: %s\n"
132 " Data Pins: D0:%d D1:%d D2:%d D3:%d D4:%d D5:%d D6:%d D7:%d\n"
133 " VSYNC Pin: %d\n"
134 " HREF Pin: %d\n"
135 " Pixel Clock Pin: %d\n"
136 " External Clock: Pin:%d Frequency:%u\n"
137 " I2C Pins: SDA:%d SCL:%d\n"
138 " Reset Pin: %d",
139 LOG_STR_ARG(this->get_log_name()), YESNO(this->is_internal()), conf.pin_d0, conf.pin_d1, conf.pin_d2,
140 conf.pin_d3, conf.pin_d4, conf.pin_d5, conf.pin_d6, conf.pin_d7, conf.pin_vsync, conf.pin_href,
141 conf.pin_pclk, conf.pin_xclk, conf.xclk_freq_hz, conf.pin_sccb_sda, conf.pin_sccb_scl, conf.pin_reset);
142
143 ESP_LOGCONFIG(TAG, " Resolution: %s", frame_size_to_str(this->config_.frame_size));
144 ESP_LOGCONFIG(TAG, " Pixel Format: %s", pixel_format_to_str(this->config_.pixel_format));
145#ifdef USE_ESP32_CAMERA_CAPTURE
146 ESP_LOGCONFIG(TAG, " Capture Delay: %u ms", this->capture_delay_);
147#endif
148
149 if (this->is_failed()) {
150 ESP_LOGE(TAG, " Setup Failed: %s", esp_err_to_name(this->init_error_));
151 return;
152 }
153
154 sensor_t *s = esp_camera_sensor_get();
155 if (s == nullptr) {
156 ESP_LOGE(TAG, " Camera sensor not available");
157 return;
158 }
159 auto st = s->status;
160 ESP_LOGCONFIG(TAG,
161 " JPEG Quality: %u\n"
162 " Framebuffer Count: %u\n"
163 " Framebuffer Location: %s\n"
164 " Contrast: %d\n"
165 " Brightness: %d\n"
166 " Saturation: %d\n"
167 " Vertical Flip: %s\n"
168 " Horizontal Mirror: %s\n"
169 " Special Effect: %u\n"
170 " White Balance Mode: %u",
171 st.quality, conf.fb_count, this->config_.fb_location == CAMERA_FB_IN_PSRAM ? "PSRAM" : "DRAM",
172 st.contrast, st.brightness, st.saturation, ONOFF(st.vflip), ONOFF(st.hmirror), st.special_effect,
173 st.wb_mode);
174 // ESP_LOGCONFIG(TAG, " Auto White Balance: %u", st.awb);
175 // ESP_LOGCONFIG(TAG, " Auto White Balance Gain: %u", st.awb_gain);
176 ESP_LOGCONFIG(TAG,
177 " Auto Exposure Control: %u\n"
178 " Auto Exposure Control 2: %u\n"
179 " Auto Exposure Level: %d\n"
180 " Auto Exposure Value: %u\n"
181 " AGC: %u\n"
182 " AGC Gain: %u\n"
183 " Gain Ceiling: %u",
184 st.aec, st.aec2, st.ae_level, st.aec_value, st.agc, st.agc_gain, st.gainceiling);
185 // ESP_LOGCONFIG(TAG, " BPC: %u", st.bpc);
186 // ESP_LOGCONFIG(TAG, " WPC: %u", st.wpc);
187 // ESP_LOGCONFIG(TAG, " RAW_GMA: %u", st.raw_gma);
188 // ESP_LOGCONFIG(TAG, " Lens Correction: %u", st.lenc);
189 // ESP_LOGCONFIG(TAG, " DCW: %u", st.dcw);
190 ESP_LOGCONFIG(TAG, " Test Pattern: %s", YESNO(st.colorbar));
191#ifdef USE_ESP32_CAMERA_WINDOW
192 this->dump_window_config_(s);
193#endif
194}
195
197 // Fast path: skip all work when truly idle
198 // (no current image, no pending requests, and not time for idle request yet)
200 if (!this->current_image_ && !this->has_requested_image_()) {
201 // Only check idle interval when we're otherwise idle
202 if (this->idle_update_interval_ != 0 && now - this->last_idle_request_ > this->idle_update_interval_) {
203 this->last_idle_request_ = now;
205 } else {
206 return;
207 }
208 }
209
210 // check if we can return the image
211 if (this->can_return_image_()) {
212 // return image
213#ifdef USE_ESP32_CAMERA_JPEG_CONVERSION
214 if (this->config_.pixel_format != PIXFORMAT_JPEG && this->config_.jpeg_quality > 0) {
215 // for non-JPEG format, we need to free the data and raw buffer
216 auto *jpg_buf = this->current_image_->get_data_buffer();
217 free(jpg_buf); // NOLINT(cppcoreguidelines-no-malloc)
218 auto *fb = this->current_image_->get_raw_buffer();
219 this->fb_allocator_.deallocate(fb, 1);
220 } else
221#endif
222 {
223 auto *fb = this->current_image_->get_raw_buffer();
224 xQueueSend(this->framebuffer_return_queue_, &fb, portMAX_DELAY);
225 }
226 this->current_image_.reset();
227 }
228
229 // Check if we should fetch a new image
230 if (!this->has_requested_image_())
231 return;
232 if (this->current_image_.use_count() > 1) {
233 // image is still in use
234 return;
235 }
237 return;
238#ifdef USE_ESP32_CAMERA_CAPTURE
239 // Requests that arrive after this wait for the next frame, so none skip the capture
240 const uint8_t requesters = this->single_requesters_.load();
241 if (this->advance_capture_(now, requesters))
242 return;
243#endif
244
245 // request new image
246 camera_fb_t *fb;
247 if (xQueueReceive(this->framebuffer_get_queue_, &fb, 0L) != pdTRUE) {
248 // no frame ready
249 ESP_LOGVV(TAG, "No frame ready");
250 return;
251 }
252
253 if (fb == nullptr) {
254 ESP_LOGW(TAG, "Got invalid frame from camera!");
255 xQueueSend(this->framebuffer_return_queue_, &fb, portMAX_DELAY);
256 return;
257 }
258
259#ifdef USE_ESP32_CAMERA_CAPTURE
260 if (this->advance_capture_with_frame_(fb)) {
261 ESP_LOGV(TAG, "Dropped frame started before the capture delay ended");
262 xQueueSend(this->framebuffer_return_queue_, &fb, portMAX_DELAY);
263 return;
264 }
265#endif
266
267#ifdef USE_ESP32_CAMERA_JPEG_CONVERSION
268 if (this->config_.pixel_format != PIXFORMAT_JPEG && this->config_.jpeg_quality > 0) {
269 // for non-JPEG format, we need to convert the frame to JPEG
270 uint8_t *jpg_buf;
271 size_t jpg_buf_len;
272 size_t width = fb->width;
273 size_t height = fb->height;
274 struct timeval timestamp = fb->timestamp;
275 bool ok = frame2jpg(fb, 100 - this->config_.jpeg_quality, &jpg_buf, &jpg_buf_len);
276 // return the original frame buffer to the queue
277 xQueueSend(this->framebuffer_return_queue_, &fb, portMAX_DELAY);
278 if (!ok) {
279 ESP_LOGE(TAG, "Failed to convert frame to JPEG!");
280#ifdef USE_ESP32_CAMERA_CAPTURE
281 this->finish_ended_capture_();
282#endif
283 return;
284 }
285 // create a new camera_fb_t for the JPEG data
286 fb = this->fb_allocator_.allocate(1);
287 if (fb == nullptr) {
288 ESP_LOGE(TAG, "Failed to allocate memory for camera frame buffer!");
289 free(jpg_buf); // NOLINT(cppcoreguidelines-no-malloc)
290#ifdef USE_ESP32_CAMERA_CAPTURE
291 this->finish_ended_capture_();
292#endif
293 return;
294 }
295 memset(fb, 0, sizeof(camera_fb_t));
296 fb->buf = jpg_buf;
297 fb->len = jpg_buf_len;
298 fb->width = width;
299 fb->height = height;
300 fb->format = PIXFORMAT_JPEG;
301 fb->timestamp = timestamp;
302 }
303#endif
304 // A request that arrives while this image is handed out is kept for the next frame
305#ifdef USE_ESP32_CAMERA_CAPTURE
306 this->single_requesters_.fetch_and(static_cast<uint8_t>(~requesters));
307#else
308 const uint8_t requesters = this->single_requesters_.exchange(0);
309#endif
310 this->current_image_ = std::make_shared<ESP32CameraImage>(fb, requesters | this->stream_requesters_);
311
312#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
313 ESP_LOGV(TAG, "Got Image: len=%u", fb->len);
314#else
315 // Initialize log time on first frame to ensure accurate interval measurement
316 if (this->frame_count_ == 0) {
317 this->last_log_time_ = now;
318 }
319 this->frame_count_++;
320 if (now - this->last_log_time_ >= FRAME_LOG_INTERVAL_MS) {
321 ESP_LOGD(TAG, "Received %u images in last %" PRIu32 "s", this->frame_count_, FRAME_LOG_INTERVAL_MS / 1000);
322 this->last_log_time_ = now;
323 this->frame_count_ = 0;
324 }
325#endif
326#ifdef USE_ESP32_CAMERA_CAPTURE
327 this->finish_ended_capture_();
328#endif
329 for (auto *listener : this->listeners_) {
330 listener->on_camera_image(this->current_image_);
331 }
332 this->last_update_ = now;
333}
334
335/* ---------------- constructors ---------------- */
337 this->config_.pin_pwdn = -1;
338 this->config_.pin_reset = -1;
339 this->config_.pin_xclk = -1;
340 this->config_.ledc_timer = LEDC_TIMER_0;
341 this->config_.ledc_channel = LEDC_CHANNEL_0;
342 this->config_.pixel_format = PIXFORMAT_JPEG;
343 this->config_.frame_size = FRAMESIZE_VGA; // 640x480
344 this->config_.jpeg_quality = 10;
345 this->config_.fb_count = 1;
346 this->config_.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
347 this->config_.fb_location = CAMERA_FB_IN_PSRAM;
348}
349
350/* ---------------- setters ---------------- */
351/* set pin assignment */
352void ESP32Camera::set_data_pins(std::array<uint8_t, 8> pins) {
353 this->config_.pin_d0 = pins[0];
354 this->config_.pin_d1 = pins[1];
355 this->config_.pin_d2 = pins[2];
356 this->config_.pin_d3 = pins[3];
357 this->config_.pin_d4 = pins[4];
358 this->config_.pin_d5 = pins[5];
359 this->config_.pin_d6 = pins[6];
360 this->config_.pin_d7 = pins[7];
361}
362void ESP32Camera::set_vsync_pin(uint8_t pin) { this->config_.pin_vsync = pin; }
363void ESP32Camera::set_href_pin(uint8_t pin) { this->config_.pin_href = pin; }
364void ESP32Camera::set_pixel_clock_pin(uint8_t pin) { this->config_.pin_pclk = pin; }
366 this->config_.pin_xclk = pin;
367 this->config_.xclk_freq_hz = frequency;
368}
369void ESP32Camera::set_i2c_pins(uint8_t sda, uint8_t scl) {
370 this->config_.pin_sccb_sda = sda;
371 this->config_.pin_sccb_scl = scl;
372}
373#ifdef USE_I2C
375 this->i2c_bus_ = i2c_bus;
376 this->config_.pin_sccb_sda = -1;
377 this->config_.pin_sccb_scl = -1;
378}
379#endif // USE_I2C
380void ESP32Camera::set_reset_pin(uint8_t pin) { this->config_.pin_reset = pin; }
381void ESP32Camera::set_power_down_pin(uint8_t pin) { this->config_.pin_pwdn = pin; }
382
383/* set image parameters */
385 switch (size) {
387 this->config_.frame_size = FRAMESIZE_QQVGA;
388 break;
390 this->config_.frame_size = FRAMESIZE_QCIF;
391 break;
393 this->config_.frame_size = FRAMESIZE_HQVGA;
394 break;
396 this->config_.frame_size = FRAMESIZE_QVGA;
397 break;
399 this->config_.frame_size = FRAMESIZE_CIF;
400 break;
402 this->config_.frame_size = FRAMESIZE_VGA;
403 break;
405 this->config_.frame_size = FRAMESIZE_SVGA;
406 break;
408 this->config_.frame_size = FRAMESIZE_XGA;
409 break;
411 this->config_.frame_size = FRAMESIZE_SXGA;
412 break;
414 this->config_.frame_size = FRAMESIZE_UXGA;
415 break;
417 this->config_.frame_size = FRAMESIZE_FHD;
418 break;
420 this->config_.frame_size = FRAMESIZE_P_HD;
421 break;
423 this->config_.frame_size = FRAMESIZE_P_3MP;
424 break;
426 this->config_.frame_size = FRAMESIZE_QXGA;
427 break;
429 this->config_.frame_size = FRAMESIZE_QHD;
430 break;
432 this->config_.frame_size = FRAMESIZE_WQXGA;
433 break;
435 this->config_.frame_size = FRAMESIZE_P_FHD;
436 break;
438 this->config_.frame_size = FRAMESIZE_QSXGA;
439 break;
440 }
441}
443 switch (format) {
445 this->config_.pixel_format = PIXFORMAT_RGB565;
446 break;
448 this->config_.pixel_format = PIXFORMAT_YUV422;
449 break;
451 this->config_.pixel_format = PIXFORMAT_YUV420;
452 break;
454 this->config_.pixel_format = PIXFORMAT_GRAYSCALE;
455 break;
457 this->config_.pixel_format = PIXFORMAT_JPEG;
458 break;
460 this->config_.pixel_format = PIXFORMAT_RGB888;
461 break;
463 this->config_.pixel_format = PIXFORMAT_RAW;
464 break;
466 this->config_.pixel_format = PIXFORMAT_RGB444;
467 break;
469 this->config_.pixel_format = PIXFORMAT_RGB555;
470 break;
471 }
472}
473void ESP32Camera::set_jpeg_quality(uint8_t quality) { this->config_.jpeg_quality = quality; }
474/* set exposure parameters */
475/* set gains parameters */
476/* set white balance */
477/* set test mode */
478/* set fps */
480 this->max_update_interval_ = max_update_interval;
481}
483 this->idle_update_interval_ = idle_update_interval;
484}
485/* set frame buffer parameters */
486void ESP32Camera::set_frame_buffer_mode(camera_grab_mode_t mode) { this->config_.grab_mode = mode; }
488 this->config_.fb_count = fb_count;
489 this->set_frame_buffer_mode(fb_count > 1 ? CAMERA_GRAB_LATEST : CAMERA_GRAB_WHEN_EMPTY);
490}
491void ESP32Camera::set_frame_buffer_location(camera_fb_location_t fb_location) {
492 this->config_.fb_location = fb_location;
493}
494
495/* ---------------- public API (specific) ---------------- */
497 for (auto *listener : this->listeners_) {
498 listener->on_stream_start();
499 }
500 this->stream_requesters_ |= (1U << requester);
501}
503 for (auto *listener : this->listeners_) {
504 listener->on_stream_stop();
505 }
506 this->stream_requesters_ &= ~(1U << requester);
507}
508void ESP32Camera::request_image(camera::CameraRequester requester) { this->single_requesters_ |= (1U << requester); }
511 sensor_t *s = esp_camera_sensor_get();
512 if (s == nullptr) {
513 return;
514 }
515 /* update image */
516 s->set_vflip(s, this->vertical_flip_);
517 s->set_hmirror(s, this->horizontal_mirror_);
518 s->set_contrast(s, this->contrast_);
519 s->set_brightness(s, this->brightness_);
520 s->set_saturation(s, this->saturation_);
521 s->set_special_effect(s, (int) this->special_effect_); // 0 to 6
522 /* update exposure */
523 s->set_exposure_ctrl(s, (bool) this->aec_mode_);
524 s->set_aec2(s, this->aec2_); // 0 = disable , 1 = enable
525 s->set_ae_level(s, this->ae_level_); // -2 to 2
526 s->set_aec_value(s, this->aec_value_); // 0 to 1200
527 /* update gains */
528 s->set_gain_ctrl(s, (bool) this->agc_mode_);
529 s->set_agc_gain(s, (int) this->agc_value_); // 0 to 30
530 s->set_gainceiling(s, (gainceiling_t) this->agc_gain_ceiling_);
531 /* update white balance mode */
532 s->set_wb_mode(s, (int) this->wb_mode_); // 0 to 4
533 /* update test pattern */
534 s->set_colorbar(s, this->test_pattern_);
535#ifdef USE_ESP32_CAMERA_WINDOW
536 // The window's readout address depends on mirror and flip
537 this->apply_window_();
538#endif
539}
540
541/* ---------------- Internal methods ---------------- */
543bool ESP32Camera::can_return_image_() const { return this->current_image_.use_count() == 1; }
544#ifdef USE_ESP32_CAMERA_CAPTURE
550// Advances the capture before a frame is fetched. Returns true while the delay runs and frames stay queued.
551bool ESP32Camera::advance_capture_(uint32_t now, uint8_t requesters) {
552 // A running stream is served as is, the delay never applies per frame
553 if (this->stream_requesters_ != 0) {
554 this->cancel_capture_();
555 return false;
556 }
557 switch (this->capture_state_) {
559 // Idle requests never use the delay
560 if ((requesters & ~(1U << camera::IDLE)) == 0)
561 return false;
563 this->capture_started_ = now;
564 ESP_LOGV(TAG, "Capture start, waiting %u ms", this->capture_delay_);
565 this->capture_callback_.call(true);
566 [[fallthrough]];
569 return true;
570 this->capture_ready_ms_ = static_cast<uint32_t>(millis_64());
572 break;
574 if (now - this->capture_started_ > CAPTURE_TIMEOUT_MS) {
575 ESP_LOGW(TAG, "No frame after capture delay, delivering next frame");
576 this->end_capture_();
577 }
578 break;
580 break;
581 }
582 return false;
583}
584// Advances the capture with a fetched frame. Returns true for a frame that started before the delay ended,
585// the first frame started after that ends the capture.
586bool ESP32Camera::advance_capture_with_frame_(const camera_fb_t *fb) {
588 return false;
589 // fb->timestamp is the esp_timer time when the driver started the frame. In 32 bit ms a frame
590 // that started before ready wraps to a value above the time since ready, even after hours of idle.
591 const uint32_t started_ms =
592 static_cast<uint32_t>(fb->timestamp.tv_sec) * 1000U + static_cast<uint32_t>(fb->timestamp.tv_usec) / 1000U;
593 const uint32_t now_ms = static_cast<uint32_t>(millis_64());
594 if (started_ms - this->capture_ready_ms_ > now_ms - this->capture_ready_ms_)
595 return true;
596 this->end_capture_();
597 return false;
598}
599// The state stays ENDED until a frame was delivered, so pending requests do not start a new capture.
602 ESP_LOGV(TAG, "Capture end");
603 this->capture_callback_.call(false);
604}
605// Called once the frame that ended a capture is delivered or lost, so a pending request starts a new capture
611#endif // USE_ESP32_CAMERA_CAPTURE
613 ESP32Camera *that = (ESP32Camera *) pv;
614 while (true) {
615 camera_fb_t *framebuffer = esp_camera_fb_get();
616 xQueueSend(that->framebuffer_get_queue_, &framebuffer, portMAX_DELAY);
617 // Only wake the main loop if there's a pending request to consume the frame
618 if (that->has_requested_image_()) {
620 }
621 // return is no-op for config with 1 fb
622 xQueueReceive(that->framebuffer_return_queue_, &framebuffer, portMAX_DELAY);
623 esp_camera_fb_return(framebuffer);
624 }
625}
626
627/* ---------------- ESP32CameraImageReader class ----------- */
628void ESP32CameraImageReader::set_image(std::shared_ptr<camera::CameraImage> image) {
629 this->image_ = std::static_pointer_cast<ESP32CameraImage>(image);
630 this->offset_ = 0;
631}
633 if (!this->image_)
634 return 0;
635
636 return this->image_->get_data_length() - this->offset_;
637}
639void ESP32CameraImageReader::consume_data(size_t consumed) { this->offset_ += consumed; }
640uint8_t *ESP32CameraImageReader::peek_data_buffer() { return this->image_->get_data_buffer() + this->offset_; }
641
642/* ---------------- ESP32CameraImage class ----------- */
643ESP32CameraImage::ESP32CameraImage(camera_fb_t *buffer, uint8_t requesters)
644 : buffer_(buffer), requesters_(requesters) {}
645
646camera_fb_t *ESP32CameraImage::get_raw_buffer() { return this->buffer_; }
647uint8_t *ESP32CameraImage::get_data_buffer() { return this->buffer_->buf; }
648size_t ESP32CameraImage::get_data_length() { return this->buffer_->len; }
650 return (this->requesters_ & (1 << requester)) != 0;
651}
652
653} // namespace esphome::esp32_camera
654
655#endif
BedjetMode mode
BedJet operating mode.
uint16_le_t frequency
Definition bl0942.h:6
void wake_loop_threadsafe()
Wake the main event loop from another thread or callback.
uint32_t IRAM_ATTR HOT get_loop_component_start_time() const
Get the cached time in milliseconds from when the current component started its loop execution.
void mark_failed()
Mark this component as failed.
bool is_failed() const
Definition component.h:287
bool is_internal() const
const LogString * get_log_name() const
Get the name for a "%s" log argument (LOG_STR_ARG).
void deallocate(T *p, size_t n)
Definition helpers.h:2453
T * allocate(size_t n)
Definition helpers.h:2423
Abstract image reader base class.
Definition camera.h:71
void set_i2c_pins(uint8_t sda, uint8_t scl)
void set_jpeg_quality(uint8_t quality)
ESP32AgcGainCeiling agc_gain_ceiling_
LazyCallbackManager< void(bool)> capture_callback_
void set_data_pins(std::array< uint8_t, 8 > pins)
void stop_stream(camera::CameraRequester requester) override
std::vector< camera::CameraListener * > listeners_
void set_i2c_id(i2c::InternalI2CBus *i2c_bus)
void set_pixel_format(ESP32CameraPixelFormat format)
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)
std::shared_ptr< ESP32CameraImage > current_image_
std::atomic< uint8_t > single_requesters_
camera::CameraImageReader * create_image_reader() override
bool advance_capture_(uint32_t now, uint8_t requesters)
void set_frame_buffer_count(uint8_t fb_count)
bool advance_capture_with_frame_(const camera_fb_t *fb)
void set_frame_buffer_location(camera_fb_location_t fb_location)
void set_frame_buffer_mode(camera_grab_mode_t mode)
void set_idle_update_interval(uint32_t idle_update_interval)
void start_stream(camera::CameraRequester requester) override
void request_image(camera::CameraRequester requester) override
std::atomic< uint8_t > stream_requesters_
static void framebuffer_task(void *pv)
RAMAllocator< camera_fb_t > fb_allocator_
bool was_requested_by(camera::CameraRequester requester) const override
ESP32CameraImage(camera_fb_t *buffer, uint8_t requester)
void set_image(std::shared_ptr< camera::CameraImage > image) override
std::shared_ptr< ESP32CameraImage > image_
void consume_data(size_t consumed) override
virtual int get_port() const =0
Returns the I2C port number.
uint32_t buffer
Definition gif.cpp:50
const char * format
CameraRequester
Different sources for filtering.
Definition camera.h:16
ESPHOME_LOG_TAG(TAG, "esp32_camera")
uint16_t size
Definition helpers.cpp:34
uint64_t millis_64()
Definition hal.cpp:29
uint32_t IRAM_ATTR HOT millis()
Definition hal.cpp:25
Application App
Global storage of Application pointer - only one Application can exist.
static void uint32_t
uint16_t timestamp
Definition tt21100.cpp:2