/** * @brief Saves the picture in microSD. * @param None * @retval None */ void Save_Picture(void) { FRESULT res1, res2; /* FatFs function common result code */ uint32_t byteswritten = 0; /* File write count */ static uint32_t counter = 0; uint8_t str[30]; /* Check if the SD card is plugged in the slot */ if(BSP_SD_IsDetected() != SD_PRESENT) { Error_Handler(); } else { /* Format the string */ sprintf((char *)str, "image_%lu.bmp", counter); /*##-1- Prepare the image to be saved ####################################*/ Prepare_Picture(); /*##-2- Create and Open a new bmp file object with write access ##########*/ if(f_open(&MyFile, (const char*)str, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) { /* 'image.bmp' file Open for write Error */ Error_Handler(); } else { /*##-3- Write data to the BMP file #####################################*/ /* Write the BMP header */ res1 = f_write(&MyFile, (uint32_t *)aBMPHeader, 54, (void *)&byteswritten); /* Write the BMP file */ res2 = f_write(&MyFile, (uint32_t *)CONVERTED_FRAME_BUFFER, ((BSP_LCD_GetYSize() - 80)*(BSP_LCD_GetXSize() - 80)*2), (void *)&byteswritten); if((res1 != FR_OK) || (res2 != FR_OK) || (byteswritten == 0)) { /* 'image.bmp' file Write or EOF Error */ Error_Handler(); } else { /*##-4- Close the open BMP file ######################################*/ f_close(&MyFile); /* Success of the demo: no error occurrence */ BSP_LED_On(LED1); /* Wait for 2s */ HAL_Delay(2000); /* Select Layer 1 */ BSP_LED_Off(LED1); counter++; } } } }
/** * @brief Saves the picture in microSD. * @param None * @retval None */ static void Save_Picture(void) { FRESULT res1 = FR_OK; FRESULT res2 = FR_OK; FRESULT res3 = FR_OK; /* FatFs function common result code */ uint32_t byteswritten = 0; /* File write count */ uint32_t bmpHeaderByteCnt = 0; uint32_t bmpFileInfoHeaderByteCnt = 0; uint32_t bmpFilePixelBytesCnt = 0; static uint32_t counter = 0; uint8_t str[30]; uint16_t tmp_size; /* Check if the SD card is plugged in the slot */ if(BSP_SD_IsDetected() != SD_PRESENT) { BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 20, (uint8_t*)"No SD card detected !!", RIGHT_MODE); Error_Handler(); } else { BSP_LCD_SetFont(&Font16); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 20, (uint8_t*)"Saving BMP to SD card", RIGHT_MODE); /* Format the string */ sprintf((char *)str, "image_%lu.bmp", counter); /* -- Prepare Bitmap file (BMP) header */ bmpFileHeader.bfType = 0x4D42; /* BMP file type */ /* Offset in bytes from start of file to first pixel data = size in bytes of complete BMP header */ /* careful the structure is padded on multiple of 32 bits by the compiler : the Padding should be removed */ bmpFileHeader.bOffBits = sizeof(BitMapFileHeader_Typedef) + sizeof(BitMapFileInfoHeader_Typedef) - sizeof(uint32_t) - sizeof(uint16_t); /* BMP complete file size is size of pad in RGB888 : 24bpp = 3 bytes per pixel + complete header size */ bmpFileHeader.bfSize = ((BSP_LCD_GetXSize() - 80) * (BSP_LCD_GetYSize() - 80) * RGB888_BYTE_PER_PIXEL); bmpFileHeader.bfSize += bmpFileHeader.bOffBits; bmpFileHeader.bfReserved1 = 0x0000; bmpFileHeader.bfReserved2 = 0x0000; bmpFileInfoHeader.biSize = 40; /* 40 bytes in bitmap info header */ bmpFileInfoHeader.biWidth = (BSP_LCD_GetXSize() - 80); bmpFileInfoHeader.biHeight = (BSP_LCD_GetYSize() - 80); bmpFileInfoHeader.biPlanes = 1; /* one single plane */ bmpFileInfoHeader.biBitCount = 24; /* RGB888 : 24 bits per pixel */ bmpFileInfoHeader.biCompression = 0; /* no compression */ /* This is number of pixel bytes in file : sizeX * sizeY * RGB888_BYTE_PER_PIXEL */ bmpFileInfoHeader.biSizeImage = ((BSP_LCD_GetXSize() - 80) * (BSP_LCD_GetYSize() - 80) * RGB888_BYTE_PER_PIXEL); bmpFileInfoHeader.biXPelsPerMeter = 0; /* not used */ bmpFileInfoHeader.biYPelsPerMeter = 0; /* not used */ bmpFileInfoHeader.biClrUsed = 0; /* not used */ bmpFileInfoHeader.biClrImportant = 0; /* not used */ /* -- End Prepare Bitmap file (BMP) header */ /*##-1- Prepare the image to be saved ####################################*/ Prepare_Picture(); /* Disable the LTDC to avoid charging the bandwidth for nothing while the BMP file is */ /* written to SD card */ LTDC_Operation(0); /*##-2- Create and Open a new bmp file object with write access ##########*/ if(f_open(&MyFile, (const char*)str, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) { /* 'image.bmp' file Open for write Error */ BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 20, (uint8_t*)" BMP File Creation Error !!", RIGHT_MODE); Error_Handler(); } else { /*##-3- Write data to the BMP file #####################################*/ /* Write the BMP header step 1 : first write BMP header : all but padding is written to file */ res1 = f_write(&MyFile, (uint16_t *)&(bmpFileHeader.bfType), sizeof(uint16_t), (void *)&bmpHeaderByteCnt); byteswritten += bmpHeaderByteCnt; /* LSB of size in bytes of BMP file */ tmp_size = (uint16_t)(bmpFileHeader.bfSize & 0x0000FFFF); res1 = f_write(&MyFile, (uint16_t *)&(tmp_size), sizeof(uint16_t), (void *)&bmpHeaderByteCnt); byteswritten += bmpHeaderByteCnt; /* MSB of size in bytes of BMP file */ tmp_size = (uint16_t)((bmpFileHeader.bfSize & 0xFFFF0000) >> 16); res1 = f_write(&MyFile, (uint16_t *)&(tmp_size), sizeof(uint16_t), (void *)&bmpHeaderByteCnt); byteswritten += bmpHeaderByteCnt; res1 = f_write(&MyFile, (uint16_t *)&(bmpFileHeader.bfReserved1), sizeof(uint16_t), (void *)&bmpHeaderByteCnt); byteswritten += bmpHeaderByteCnt; res1 = f_write(&MyFile, (uint16_t *)&(bmpFileHeader.bfReserved2), sizeof(uint16_t), (void *)&bmpHeaderByteCnt); byteswritten += bmpHeaderByteCnt; res1 = f_write(&MyFile, (uint32_t *)&(bmpFileHeader.bOffBits), sizeof(uint32_t), (void *)&bmpHeaderByteCnt); byteswritten += bmpHeaderByteCnt; if(res1 != FR_OK) { /* Reactivate LTDC */ LTDC_Operation(1); f_close(&MyFile); BSP_LCD_ClearStringLine(BSP_LCD_GetYSize() - 20); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 20, (uint8_t*)" BMP File Header Saving Error !!", RIGHT_MODE); Error_Handler(); } byteswritten += bmpHeaderByteCnt; if(res1 == FR_OK) { /* Write the BMP header step 2 : second write BMP file info header */ res2 = f_write(&MyFile, (BitMapFileInfoHeader_Typedef *)&bmpFileInfoHeader, sizeof(BitMapFileInfoHeader_Typedef), (void *)&bmpFileInfoHeaderByteCnt); if(res2 != FR_OK) { /* Reactivate LTDC */ LTDC_Operation(1); f_close(&MyFile); BSP_LCD_ClearStringLine(BSP_LCD_GetYSize() - 20); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 20, (uint8_t*)" BMP File Header Info Saving Error !!", RIGHT_MODE); Error_Handler(); } } byteswritten += bmpFileInfoHeaderByteCnt; if((res1 == FR_OK) && (res2 == FR_OK)) { /* Write pixel data in the the BMP file */ res3 = f_write(&MyFile, (uint8_t *)p_bmp_converted_pixel_data, bmpFileInfoHeader.biSizeImage, (void *)&bmpFilePixelBytesCnt); /* Reactivate LTDC */ LTDC_Operation(1); if(res3 != FR_OK) { if(res3 == FR_DISK_ERR) { f_close(&MyFile); BSP_LCD_ClearStringLine(BSP_LCD_GetYSize() - 20); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 20, (uint8_t*)" File Saving Error DISKERR !!", RIGHT_MODE); } Error_Handler(); } } byteswritten += bmpFilePixelBytesCnt; if((res1 != FR_OK) || (res2 != FR_OK) || (res3 != FR_OK) || (byteswritten == 0)) { /* 'image.bmp' file Write or EOF Error */ BSP_LCD_ClearStringLine(BSP_LCD_GetYSize() - 20); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 20, (uint8_t*)" BMP File Saving Error !!", RIGHT_MODE); Error_Handler(); } else { /*##-4- Close the open BMP file ######################################*/ f_close(&MyFile); /* Success of the demo: no error occurrence */ BSP_LED_On(LED1); BSP_LCD_ClearStringLine(BSP_LCD_GetYSize() - 20); BSP_LCD_DisplayStringAt(0, BSP_LCD_GetYSize() - 20, (uint8_t*)" BMP File Saved.", RIGHT_MODE); /* Wait for 2s */ HAL_Delay(2000); BSP_LCD_ClearStringLine(BSP_LCD_GetYSize() - 20); /* Select Layer 1 */ BSP_LED_Off(LED1); counter++; } } } }
/** * @brief Save the picture in USB Disk. * @param None * @retval None */ void Save_Picture(void) { FRESULT res1, res2; /* FatFs function common result code */ uint32_t byteswritten; /* File write count */ BSP_LCD_SetLayerVisible(1, ENABLE); BSP_LCD_SetColorKeying(1, LCD_COLOR_WHITE); /* Set foreground Layer */ BSP_LCD_SelectLayer(1); BSP_LCD_SetTextColor(LCD_COLOR_DARKRED); BSP_LCD_SetFont(&Font20); /* Turn LED1 and LED3 Off */ BSP_LED_Off(LED1); BSP_LED_Off(LED3); if (Appli_state == APPLICATION_RUNNIG) { BSP_LCD_DisplayStringAt(10, (BSP_LCD_GetYSize()-100), (uint8_t *)"Saving ... ", RIGHT_MODE); /*##-1- Prepare the image to be saved ####################################*/ Prepare_Picture(); /*##-2- Create and Open a new bmp file object with write access ##########*/ if(f_open(&MyFile, "image.bmp", FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) { /* 'image.bmp' file Open for write Error */ Error_Handler(); } else { /*##-3- Write data to the BMP file #####################################*/ /* Write the BMP header */ if (BSP_LCD_GetXSize() == 640) { /* if ampire 640x480 LCD is used */ res1 = f_write(&MyFile, (uint32_t *)aBMPHeader1, 54, (void *)&byteswritten); } else { /* if ampire 480x272 LCD is used */ res1 = f_write(&MyFile, (uint32_t *)aBMPHeader2, 54, (void *)&byteswritten); } /* Write the bmp file */ res2 = f_write(&MyFile, (uint32_t *)CONVERTED_FRAME_BUFFER, ((BSP_LCD_GetYSize()-60)*(BSP_LCD_GetXSize()-60)*3), (void *)&byteswritten); if((res1 != FR_OK) || (res2 != FR_OK) || (byteswritten == 0)) { /* 'image' file Write or EOF Error */ BSP_LED_On(LED3); BSP_LCD_DisplayStringAt(10, (BSP_LCD_GetYSize()-100), (uint8_t *)" Aborted...", RIGHT_MODE); /* Wait for 2s */ HAL_Delay(2000); /* Disable the Layer 2 */ BSP_LCD_SetLayerVisible(1, DISABLE); /* Select Layer 1 */ BSP_LCD_SelectLayer(0); } else { /*##-4- Close the open text file #####################################*/ f_close(&MyFile); /* Success of the demo: no error occurrence */ BSP_LED_On(LED1); BSP_LCD_SetTextColor(LCD_COLOR_DARKGREEN); BSP_LCD_DisplayStringAt(10, (BSP_LCD_GetYSize()-100), (uint8_t *)" Saved ", RIGHT_MODE); /* Wait for 2s */ HAL_Delay(2000); /* Disable the Layer 2 */ BSP_LCD_SetLayerVisible(1, DISABLE); /* Select Layer 1 */ BSP_LCD_SelectLayer(0); } } } else { /* USB not connected */ BSP_LCD_DisplayStringAt(10, (BSP_LCD_GetYSize()-100), (uint8_t *)"USB KO... ", RIGHT_MODE); /* Wait for 2s */ HAL_Delay(2000); /* Disable the Layer 2 */ BSP_LCD_SetLayerVisible(1, DISABLE); /* Select Layer 1 */ BSP_LCD_SelectLayer(0); } }
/** * @brief Saves the picture in microSD. * @param None * @retval None */ void Save_Picture(void) { FRESULT res1, res2; /* FatFs function common result code */ uint32_t byteswritten = 0; /* File write count */ static uint32_t counter = 0; uint8_t str[30]; BSP_LCD_SetLayerVisible(1, ENABLE); BSP_LCD_SetColorKeying(1, LCD_COLOR_WHITE); /* Set foreground Layer */ BSP_LCD_SelectLayer(1); BSP_LCD_SetTextColor(LCD_COLOR_DARKRED); BSP_LCD_SetFont(&Font24); /* Initialize IOE */ BSP_IO_Init(); /* Check if the SD card is plugged in the slot */ if(BSP_SD_IsDetected() != SD_PRESENT) { BSP_LCD_DisplayStringAt(20, (BSP_LCD_GetYSize()-125), (uint8_t *)"SD Not Connected", RIGHT_MODE); BSP_LCD_DisplayStringAt(20, (BSP_LCD_GetYSize()-100), (uint8_t *)"Please inser SDCard", RIGHT_MODE); } else { BSP_LCD_DisplayStringAt(20, (BSP_LCD_GetYSize()-100), (uint8_t *)"Saving ..", RIGHT_MODE); /* Format the string */ sprintf((char *)str,"image_%d.bmp", (int)counter); /*##-1- Prepare the image to be saved ######################################*/ Prepare_Picture(); /*##-2- Create and Open a new bmp file object with write access ##########*/ if(f_open(&MyFile, (const char*)str, FA_CREATE_ALWAYS | FA_WRITE) != FR_OK) { /* 'image.bmp' file Open for write Error */ Error_Handler(); } else { /*##-3- Write data to the BMP file #####################################*/ /* Write the BMP header */ if (BSP_LCD_GetXSize() == 640) { /* if ampire 640x480 LCD is used */ res1 = f_write(&MyFile, (uint32_t *)aBMPHeader1, 54, (void *)&byteswritten); } else { /* if ampire 480x272 LCD is used */ res1 = f_write(&MyFile, (uint32_t *)aBMPHeader2, 54, (void *)&byteswritten); } /* Write the bmp file */ res2 = f_write(&MyFile, (uint32_t *)CONVERTED_FRAME_BUFFER, ((BSP_LCD_GetYSize()-60)*(BSP_LCD_GetXSize()-60)*3), (void *)&byteswritten); if((res1 != FR_OK) || (res2 != FR_OK) || (byteswritten == 0)) { /* 'image' file Write or EOF Error */ Error_Handler(); } else { /*##-4- Close the open bmp file ######################################*/ f_close(&MyFile); /* Success of the demo: no error occurrence */ BSP_LED_On(LED1); BSP_LCD_SetTextColor(LCD_COLOR_DARKGREEN); BSP_LCD_DisplayStringAt(20, (BSP_LCD_GetYSize()-100), (uint8_t *)" Saved ", RIGHT_MODE); /* Select Layer 1 */ BSP_LED_Off(LED1); counter++; } } } /* Wait for 2s */ HAL_Delay(2000); /* Disable the Layer 2 */ BSP_LCD_SetLayerVisible(1, DISABLE); /* Clear the LCD Foreground layer */ BSP_LCD_Clear(LCD_COLOR_WHITE); BSP_LCD_SelectLayer(0); }