From e6b20283caf08b764620b616ac1760909bab25d6 Mon Sep 17 00:00:00 2001 From: Miroslav Pivovarsky Date: Mon, 27 May 2024 18:26:28 +0200 Subject: [PATCH] added check for failed photo capture --- ESP32_PrusaConnectCam/camera.cpp | 14 ++++++++++++-- ESP32_PrusaConnectCam/camera.h | 1 + ESP32_PrusaConnectCam/mcu_cfg.h | 1 + 3 files changed, 14 insertions(+), 2 deletions(-) diff --git a/ESP32_PrusaConnectCam/camera.cpp b/ESP32_PrusaConnectCam/camera.cpp index f3a5903..a5e9fd2 100644 --- a/ESP32_PrusaConnectCam/camera.cpp +++ b/ESP32_PrusaConnectCam/camera.cpp @@ -31,6 +31,7 @@ Camera::Camera(Configuration* i_conf, Logs* i_log, uint8_t i_FlashPin) { PhotoExifData.len = 0; PhotoExifData.offset = 0; PhotoSending = false; + CameraCaptureFailedCounter = 0; } /** @@ -310,7 +311,8 @@ void Camera::CapturePhoto() { delay(5); // delay for camera stabilization. test it FrameBuffer = esp_camera_fb_get(); if (!FrameBuffer) { - log->AddEvent(LogLevel_Error, F("Camera capture failed! photo")); + CameraCaptureFailedCounter++; + log->AddEvent(LogLevel_Error, F("Camera capture failed! photo. Attempt: "), String(CameraCaptureFailedCounter)); xSemaphoreGive(frameBufferSemaphore); // Release semaphore before returning return; } @@ -321,11 +323,12 @@ void Camera::CapturePhoto() { log->AddEvent(LogLevel_Info, buf); if (ControlFlag != 0x00) { - log->AddEvent(LogLevel_Error, F("Camera capture failed! photo "), String(ControlFlag, HEX)); + log->AddEvent(LogLevel_Error, "Camera capture failed! flag: " + String(ControlFlag, HEX)); FrameBuffer->len = 0; } else { log->AddEvent(LogLevel_Info, F("Photo OK! "), String(ControlFlag, HEX)); + CameraCaptureFailedCounter = 0; /* generate exif header */ update_exif_from_cfg(imageExifRotation); @@ -354,6 +357,13 @@ void Camera::CapturePhoto() { } xSemaphoreGive(frameBufferSemaphore); } + + /* Reinit camera module if photo capture failed */ + if (CameraCaptureFailedCounter > CAMERA_MAX_FAIL_CAPTURE) { + log->AddEvent(LogLevel_Error, F("Camera capture failed! photo max attempts")); + CameraCaptureFailedCounter = 0; + ReinitCameraModule(); + } } /** diff --git a/ESP32_PrusaConnectCam/camera.h b/ESP32_PrusaConnectCam/camera.h index e34f2a5..3e17baf 100644 --- a/ESP32_PrusaConnectCam/camera.h +++ b/ESP32_PrusaConnectCam/camera.h @@ -75,6 +75,7 @@ private: float StreamAverageFps; ///< stream average fps uint16_t StreamAverageSize; ///< stream average size PhotoExifData_t PhotoExifData; ///< photo exif data + uint8_t CameraCaptureFailedCounter; ///< camera capture failed counter Configuration *config; ///< pointer to Configuration object Logs *log; ///< pointer to Logs object diff --git a/ESP32_PrusaConnectCam/mcu_cfg.h b/ESP32_PrusaConnectCam/mcu_cfg.h index de6f8db..95b6b57 100644 --- a/ESP32_PrusaConnectCam/mcu_cfg.h +++ b/ESP32_PrusaConnectCam/mcu_cfg.h @@ -18,6 +18,7 @@ #define SW_BUILD __DATE__ " " __TIME__ ///< build number #define CONSOLE_VERBOSE_DEBUG false ///< enable/disable verbose debug log level for console #define DEVICE_HOSTNAME "Prusa-ESP32cam" ///< device hostname +#define CAMERA_MAX_FAIL_CAPTURE 10 ///< maximum count for failed capture /* ------------ PRUSA BACKEND CFG --------------*/ #define HOST_URL_CAM_PATH "/c/snapshot" ///< path for sending photo to prusa connect