diff --git a/Firmware/Core/Cam/Cam.c b/Firmware/Core/Cam/Cam.c index 52a085b..69e6841 100644 --- a/Firmware/Core/Cam/Cam.c +++ b/Firmware/Core/Cam/Cam.c @@ -133,7 +133,35 @@ void Cam_Init(I2C_HandleTypeDef *hi2c, SPI_HandleTypeDef *hspi) Cam_I2C_write(hi2c, (uint16_t)0x3801, 0xb0); // TIMING HORIZONTAL START - ALSO FOR MIRROR Cam_I2C_write(hi2c, (uint16_t)0x4407, 0x04); // COMPRESSION CONTROL - Cam_I2C_write_bulk(hi2c, ov5642_320x240); + //Cam_I2C_write_bulk(hi2c, ov5642_320x240); + //Cam_I2C_write_bulk(hi2c, ov5642_640x480); + + Cam_I2C_write_bulk(hi2c, ov5642_1280x960); + + // Setup camera, H-sync: High, V-sync:high, Sensor_delay: no Delay, FIFO_mode:FIFO enabled, power_mode:Low_power + Cam_SPI_write(hspi, 0x03, 0x02); + Cam_SPI_write(hspi, 0x01, 0x00); // Capture Control Register - Set to capture n+1 frames + + HAL_Delay(5); +} + +void Cam_Refresh(I2C_HandleTypeDef *hi2c, SPI_HandleTypeDef *hspi){ + Cam_I2C_write(hi2c, (uint16_t)0x3008, 0x80); + + Cam_I2C_write_bulk(hi2c, OV5642_QVGA_Preview); + + Cam_I2C_write_bulk(hi2c, OV5642_JPEG_Capture_QSXGA); + + //Cam_I2C_write_bulk(hi2c, OV5642_720P_Video_setting); + + Cam_I2C_write(hi2c, (uint16_t)0x3818, 0xa8); // TIMING CONTROL - ENABLE COMPRESSION, THUMBNAIL MODE DISABLE, VERTICAL FLIP, MIRROR + Cam_I2C_write(hi2c, (uint16_t)0x3621, 0x10); // REGISTER FOR CORRECT MIRROR FUNCTION + Cam_I2C_write(hi2c, (uint16_t)0x3801, 0xb0); // TIMING HORIZONTAL START - ALSO FOR MIRROR + Cam_I2C_write(hi2c, (uint16_t)0x4407, 0x04); // COMPRESSION CONTROL + + //Cam_I2C_write_bulk(hi2c, ov5642_320x240); + //ov5642_1024x768 + Cam_I2C_write_bulk(hi2c, ov5642_1024x768); // Setup camera, H-sync: High, V-sync:high, Sensor_delay: no Delay, FIFO_mode:FIFO enabled, power_mode:Low_power Cam_SPI_write(hspi, 0x03, 0x02); diff --git a/Firmware/Core/Cam/Cam.h b/Firmware/Core/Cam/Cam.h index f874027..f7bd48c 100644 --- a/Firmware/Core/Cam/Cam.h +++ b/Firmware/Core/Cam/Cam.h @@ -18,6 +18,8 @@ int Cam_I2C_write(I2C_HandleTypeDef *hi2c, uint16_t address, uint8_t data); int Cam_I2C_write_struct(I2C_HandleTypeDef *hi2c, sensor_reg reg); void Cam_I2C_write_bulk(I2C_HandleTypeDef *hi2c, const struct sensor_reg regList[]); void Cam_Init(I2C_HandleTypeDef *hi2c, SPI_HandleTypeDef *hspi); +void Cam_Refresh(I2C_HandleTypeDef *hi2c, SPI_HandleTypeDef *hspi); + int Cam_FIFO_length(SPI_HandleTypeDef *hspi); void Cam_Capture(SPI_HandleTypeDef *hspi); diff --git a/Firmware/Core/Src/main.c b/Firmware/Core/Src/main.c index d22d61d..e9d071c 100644 --- a/Firmware/Core/Src/main.c +++ b/Firmware/Core/Src/main.c @@ -271,85 +271,76 @@ int main(void) // Wait for power stabilization //HAL_Delay(1000); - Cam_Init(&hi2c1, &hspi1); + #define CDC_BUFF_SIZE 10000 - int currentSendingIndex = 0; + int last_sent_idx = 0; + int buff_stop_idx = 0; + uint16_t image_size = 0; + uint8_t cdc_buff[CDC_BUFF_SIZE]; - - Cam_Capture(&hspi1); - - uint16_t image_size = Cam_FIFO_length(&hspi1); - - uint8_t image_data[10000]; - memset(image_data, 0x00, 10000); - - Cam_Start_Burst_Read(&hspi1); - - //HAL_SPI_Receive_DMA(&hspi1, image_data, 10000); - HAL_SPI_Receive(&hspi1, image_data, 10000, HAL_MAX_DELAY); - - LED_On(); - while (SPI_Rx_Done_Flag == 0) - { - // Wait for SPI transfer to finish - break; - } - - CS_Off(); - SPI_Rx_Done_Flag = 0; /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) - { - + { tud_task(); - if(currentSendingIndex >= 9949){ - //if(false){ - currentSendingIndex = 0; - //free(image_data); - for(int i = 0; i < 10000; i++) image_data[i] = 0x00; + if(buff_stop_idx >= (int)image_size){ + + buff_stop_idx = 0; + last_sent_idx = 0; + + for(int i = 0; i < CDC_BUFF_SIZE; i++) cdc_buff[i] = 0x00; + + CS_Off(); + CS_On(); + + tud_task(); Cam_Init(&hi2c1, &hspi1); + + tud_task(); Cam_Capture(&hspi1); - image_size = Cam_FIFO_length(&hspi1); - - //image_data = malloc((image_size + (image_size%10)) * sizeof(uint8_t)); - //memset(image_data, 0x00, image_size + (image_size%10)); + tud_task(); + image_size = Cam_FIFO_length(&hspi1); Cam_Start_Burst_Read(&hspi1); - HAL_SPI_Receive(&hspi1, image_data, 10000, HAL_MAX_DELAY); - //Debug_LED_On(); + continue; - while (SPI_Rx_Done_Flag == 0) - { - // Wait for SPI transfer to finish - //tud_task(); - break; - } - - CS_Off(); - //SPI_Rx_Done_Flag = 0; - if(image_size < 1){ - currentSendingIndex = 10000; - LED_On(); - } - + //HAL_SPI_Receive(&hspi1, cdc_buff, CDC_BUFF_SIZE, HAL_MAX_DELAY); } else { - //tud_cdc_write("11111111\r\n", 10); - //tud_cdc_write_flush(); - //tud_cdc_write(&sendT[currentSendingIndex], 50); - tud_cdc_write(&image_data[currentSendingIndex], 50); + int number_to_read = 0; + for(int i = 0; i < CDC_BUFF_SIZE; i++) cdc_buff[i] = 0x00; + if((buff_stop_idx + CDC_BUFF_SIZE) > (int) image_size){ + number_to_read = (int) image_size - buff_stop_idx; + } + else{ + number_to_read = CDC_BUFF_SIZE; + } + + HAL_SPI_Receive(&hspi1, cdc_buff, number_to_read, HAL_MAX_DELAY); + + buff_stop_idx = buff_stop_idx + number_to_read; + } + + int current_sending_idx = 0; + do{ + tud_task(); + + tud_cdc_write(&cdc_buff[current_sending_idx], 50); tud_cdc_write_flush(); - currentSendingIndex = currentSendingIndex + 50; - HAL_Delay(5); + current_sending_idx = current_sending_idx + 50; + last_sent_idx = last_sent_idx + 50; + HAL_Delay(3); } + while(last_sent_idx < buff_stop_idx); + + LED_On(); /* USER CODE END WHILE */ diff --git a/Receiver/Displayer.cpp b/Receiver/Displayer.cpp index 9303ba2..f3ca6ca 100644 --- a/Receiver/Displayer.cpp +++ b/Receiver/Displayer.cpp @@ -11,7 +11,7 @@ int Displayer::createWindow() { window = SDL_CreateWindow("Image Viewer", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1920, 1080, SDL_WINDOW_SHOWN); if (!window) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create window: %s", SDL_GetError()); - SDL_Quit(); + //SDL_Quit(); return 1; } @@ -23,15 +23,29 @@ int Displayer::renderWindow(){ if (!renderer) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create renderer: %s", SDL_GetError()); - SDL_DestroyWindow(window); - SDL_Quit(); + //SDL_DestroyWindow(window); + //SDL_Quit(); return 1; } return 0; } +#define PRINT_BUFFER 0 + int Displayer::imageFromVector(std::vector* img){ + #if PRINT_BUFFER + std::cout << "______________" << std::endl; + + for(int i = 0; i<(int)(img)->size(); i++) + { + std::cout << "0x" << std::hex << std::setfill('0') << std::setw(2) << static_cast((*img)[i]) << " "; + } + std::cout << std::endl; + + std::cout << "______________" << std::endl; + #endif + imageRwops = SDL_RWFromMem(img->data(), img->size()); return 0; } @@ -40,10 +54,10 @@ int Displayer::createImageSurface(){ imageSurface = IMG_Load_RW(imageRwops, 0); if (!imageSurface) { SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to load image: %s", IMG_GetError()); - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDL_Quit(); - return 1; + //SDL_DestroyRenderer(renderer); + //SDL_DestroyWindow(window); + //SDL_Quit(); + //return 1; } return 0; @@ -130,8 +144,8 @@ int Displayer::windowLoop(){ return 0; } + int Displayer::vectorToTexture(std::vector* img){ - imageFromVector(img); createImageSurface(); textureFromSurface(); diff --git a/Receiver/Receiver.cpp b/Receiver/Receiver.cpp index 286da3a..e03a1ee 100644 --- a/Receiver/Receiver.cpp +++ b/Receiver/Receiver.cpp @@ -53,9 +53,9 @@ int Receiver::initSerial(){ return 1; } -int Receiver::readCdcData(uint8_t (*character)[50]) { - memset(*character, 0x00, 50); - int bytesRead = read(cdcFile, character, 50); +int Receiver::readCdcData(uint8_t (*character)[CDC_FRAME_SIZE]) { + memset(*character, 0x00, CDC_FRAME_SIZE); + int bytesRead = read(cdcFile, character, CDC_FRAME_SIZE); if (bytesRead == -1) { std::cerr << "Error in read" << std::endl; return -1; @@ -65,21 +65,24 @@ int Receiver::readCdcData(uint8_t (*character)[50]) { return -1; } - /* PRINT WHAT IS RECEIVED - for(int i = 0; i<50; i++) - { - printHex((*character)[i]); - //std::cout << unsigned(character[i]); - std::cout << " "; - } - std::cout << std::endl << std::endl; - */ + #if 0 // PRINT WHAT IS RECEIVED + + for(int i = 0; i firstLen){ (*character)[i] = 0x00; @@ -118,7 +121,7 @@ int Receiver::simulateRead(unsigned char (*character)[50]){ } int Receiver::fillBuffer(){ - unsigned char character[50]; + unsigned char character[CDC_FRAME_SIZE]; std::vector tempVec{}; Receiver::readCdcData(&character); @@ -183,6 +186,7 @@ void Receiver::bufferToDisplay(){ currentBufferIndexMutex.lock(); int buffIdx = currentBufferIndex; currentBufferIndexMutex.unlock(); + std::cout << buffIdx << std::endl; switch(buffIdx){ case 0: dis->vectorToTexture(&buffer1); @@ -205,15 +209,15 @@ void Receiver::initTextures(){ dis->vectorToTexture(&buffer_initial); } -int Receiver::findSequence(unsigned char (*str)[50], unsigned char ch1, unsigned char ch2){ +int Receiver::findSequence(unsigned char (*str)[CDC_FRAME_SIZE], unsigned char ch1, unsigned char ch2){ if(sequenceEndedFF){ sequenceEndedFF = 0; if((*str)[0] == ch2){ return 0; } } - for(int i=0; i<50; i++){ - if((*str)[i] == ch1 && i < 50){ + for(int i=0; iinitSerial(); while (true) { - unsigned char character[50]; + unsigned char character[CDC_FRAME_SIZE]; (*receiverPtr)->readCdcData(&character); if((*receiverPtr)->findStart(&character) != -1) {std::cout << "start" << std::endl;} @@ -21,7 +21,7 @@ void readLoop(Receiver ** receiverPtr){ else{ std::cout << "-"; } - std::this_thread::sleep_for(std::chrono::milliseconds(5)); + std::this_thread::sleep_for(std::chrono::milliseconds(3)); } close(cdcFile); @@ -36,7 +36,7 @@ void receiverLoop(Receiver ** receiverPtr){ while(1){ (*receiverPtr)->fillBuffer(); (*receiverPtr)->bufferToDisplay(); - std::this_thread::sleep_for(std::chrono::milliseconds(5)); + std::this_thread::sleep_for(std::chrono::milliseconds(3)); } } diff --git a/Receiver/main.hpp b/Receiver/main.hpp index 0f94b82..05aae11 100644 --- a/Receiver/main.hpp +++ b/Receiver/main.hpp @@ -13,4 +13,6 @@ #include #include #include -#include \ No newline at end of file +#include + +#define CDC_FRAME_SIZE 50 \ No newline at end of file