Fix issues in CapturePhoto method
- Added max attempts limit to avoid infinite loops which would let the flash on continuously Additional cleanups: - Removed redundant frame buffer handling - Added error handling for semaphore take failure - Ensured semaphore release on capture failurepull/25/head
parent
51f6249c4d
commit
8089ed4de8
|
|
@ -261,7 +261,10 @@ void Camera::ReinitCameraModule() {
|
|||
*/
|
||||
void Camera::CapturePhoto() {
|
||||
if (false == StreamOnOff) {
|
||||
if (xSemaphoreTake(frameBufferSemaphore, portMAX_DELAY)) {
|
||||
if (!xSemaphoreTake(frameBufferSemaphore, portMAX_DELAY)) {
|
||||
log->AddEvent(LogLevel_Error, F("Failed to take frame buffer semaphore"));
|
||||
return;
|
||||
}
|
||||
|
||||
/* check flash, and enable FLASH LED */
|
||||
if (true == CameraFlashEnable) {
|
||||
|
|
@ -269,40 +272,38 @@ void Camera::CapturePhoto() {
|
|||
delay(CameraFlashTime);
|
||||
}
|
||||
|
||||
/* get train photo */
|
||||
FrameBuffer = esp_camera_fb_get();
|
||||
esp_camera_fb_return(FrameBuffer);
|
||||
|
||||
int attempts = 0;
|
||||
const int maxAttempts = 5;
|
||||
do {
|
||||
log->AddEvent(LogLevel_Info, F("Taking photo..."));
|
||||
|
||||
/* capture final photo */
|
||||
delay(5); // delay for camera stabilization. test it
|
||||
FrameBuffer = esp_camera_fb_get();
|
||||
if (!FrameBuffer) {
|
||||
log->AddEvent(LogLevel_Error, F("Camera capture failed! photo"));
|
||||
xSemaphoreGive(frameBufferSemaphore); // Release semaphore before returning
|
||||
return;
|
||||
}
|
||||
|
||||
} else {
|
||||
/* copy photo from buffer to string array */
|
||||
char buf[150] = { '\0' };
|
||||
uint8_t ControlFlag = (uint8_t)FrameBuffer->buf[15];
|
||||
sprintf(buf, "The picture has been saved. Size: %d bytes, Photo resolution: %zu x %zu", FrameBuffer->len, FrameBuffer->width, FrameBuffer->height);
|
||||
log->AddEvent(LogLevel_Info, buf);
|
||||
|
||||
/* check corrupted photo */
|
||||
if (ControlFlag != 0x00) {
|
||||
log->AddEvent(LogLevel_Error, "Camera capture failed! photo " + String(ControlFlag, HEX));
|
||||
FrameBuffer->len = 0;
|
||||
|
||||
} else {
|
||||
log->AddEvent(LogLevel_Info, "Photo OK! " + String(ControlFlag, HEX));
|
||||
}
|
||||
|
||||
esp_camera_fb_return(FrameBuffer);
|
||||
}
|
||||
|
||||
/* check if photo is correctly saved */
|
||||
attempts++;
|
||||
if (attempts >= maxAttempts) {
|
||||
log->AddEvent(LogLevel_Error, F("Failed to capture a valid photo after max attempts"));
|
||||
break;
|
||||
}
|
||||
} while (!(FrameBuffer->len > 100));
|
||||
|
||||
/* Disable flash */
|
||||
|
|
@ -312,7 +313,6 @@ void Camera::CapturePhoto() {
|
|||
}
|
||||
xSemaphoreGive(frameBufferSemaphore);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
|||
Loading…
Reference in New Issue