'Problem with Arduino OV7670 content display

Recently I tried to use Arduino UNO with OV7670 camera and ST7789 display.

Plan was to fill screen with camera content and with red color over camera bounds.

Code:

uint16_t lineContent[SCR_WD];
uint16_t *line = lineContent;

int TFT_RST = 10;
int TFT_CS  = 9;
int TFT_DC  = 8;
Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);

BufferedCameraOV7670_QQVGA_10hz camera(CameraOV7670::PIXEL_RGB565);
//BufferedCameraOV7670_QQVGA_10hz camera(CameraOV7670::RESOLUTION_QVGA_320x240, CameraOV7670::PIXEL_RGB565, 32);

void setup() {
  Serial.begin(9600);
  Serial.print("Setup");
  bool cameraInitialized = camera.init();

  tft.init(SCR_WD, SCR_HT);
  tft.setRotation(0);
  tft.fillScreen(ST77XX_WHITE);
  
  Serial.print("\n");
}

uint8_t screenLineIndex;
uint8_t base = 0;
uint16_t offset = 0;
static const uint16_t byteCountForDisplay = camera.getPixelBufferLength();

void loop() {
  
  noInterrupts();
  camera.waitForVsync();
  camera.ignoreVerticalPadding();
  tft.startWrite();
  
  uint8_t cll = camera.getLineLength();
  uint8_t clc = camera.getLineCount();

  Serial.print(byteCountForDisplay);
  
  uint16_t camLineContent[cll];
  
  for (uint16_t i = 0; i < SCR_HT; i+=2) {
    if(
      i%1 == 0 && // avoiding some lines number
      i < clc // if line number is less then camera VRes
      )
    {
      camera.readLine();

      //fill camera line points
      for (uint16_t c = 0; c < byteCountForDisplay/2; c++) {
        int16_t jID = (c*2);
        camLineContent[c] = (
                    camera.getPixelByte(jID)<<8
                    | camera.getPixelByte(jID+1)
                  );
      }
    }

    //fill screen
    for (uint16_t j = 0; j < SCR_WD; j++) {
      
      int16_t color = 
                      (i < clc) && (j < cll)? // if fits in camera rect
                        camLineContent[j]:
                        ST77XX_RED;
                        
      int16_t lineContentID = j;
                        
      lineContent[lineContentID] = color;
    }
    //finally write a line
    tft.writePixels(line,SCR_WD);
  }
  
  tft.endWrite();
  interrupts();
  base++;
}

Everything works well if I set screen resolution same as camera, but if not I have this -

enter image description here

Camera content duplicated several times.
It looks that my image about how it works is wrong.
Can you help me, please ?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source