diff --git a/build-number.txt b/build-number.txt index 1303c35..761fcd3 100644 --- a/build-number.txt +++ b/build-number.txt @@ -1 +1 @@ -497 +499 diff --git a/src/ov7670.c b/src/ov7670.c index 7f9bf74..0409b64 100644 --- a/src/ov7670.c +++ b/src/ov7670.c @@ -154,6 +154,7 @@ uint8_t LineBuffer[CAMERA_IMAGE_WIDTH + 40]; int LineCount = 0; static int FrameCount = 0; volatile int Camera_Captured = 0; +unsigned int BlackPixels = 0; static uint8_t ReadRegister(uint8_t reg) { @@ -310,7 +311,9 @@ void TIM1_CC_IRQHandler(void) CurrentLine = 0; FrameCount++; - if(FrameCount == 5) + // Check if the last frame's exposure is reasonable + if(BlackPixels >= (unsigned)(CAMERA_PIXELS * CAMERA_EXPOSURE_LOW) + && BlackPixels <= (unsigned)(CAMERA_PIXELS * CAMERA_EXPOSURE_HIGH)) { Camera_Captured = 1; // Disable everything @@ -318,6 +321,9 @@ void TIM1_CC_IRQHandler(void) TIM1->CR1 = 0; } + // Reset black pixel counter + BlackPixels = 0; + // Dummy read TIM1->CCR2; TIM1->SR &= ~TIM_SR_CC2IF; @@ -371,6 +377,7 @@ void TIM3_IRQHandler(void) error = pixel; ImageBuffer[(line * CAMERA_IMAGE_WIDTH + i) / 8] |= 0x80 >> (i % 8); + BlackPixels++; } else { diff --git a/src/ov7670.h b/src/ov7670.h index 95b9050..23b7fa9 100644 --- a/src/ov7670.h +++ b/src/ov7670.h @@ -5,6 +5,12 @@ #define CAMERA_IMAGE_WIDTH 160 #define CAMERA_IMAGE_HEIGHT 144 +#define CAMERA_PIXELS (CAMERA_IMAGE_WIDTH * CAMERA_IMAGE_HEIGHT) + +// Maximum and minimum share of black pixels for an image to be accepted +#define CAMERA_EXPOSURE_LIMIT 0.4 +#define CAMERA_EXPOSURE_LOW (0.5 - CAMERA_EXPOSURE_LIMIT) +#define CAMERA_EXPOSURE_HIGH (0.5 + CAMERA_EXPOSURE_LIMIT) // Use 2D Floyd-Steinberg dithering. Uncomment to use simple line-wise // dithering.