2022-12-19 04:11 AM
We are using SPI TFT 3.2" ili9341 with STM32F072RBT6 board. We were able to accomplish successful display of BMP image but not able to view the image in portrait mode loaded from left side. If we try to change the orientation, the image does not load. This is the TFT...
https://thinkrobotics.in/products/inches-spi-tft-display-module-1?variant=16353637564488
attaching the code below.
int displayImage(const char* fname) {
// UART_Printf("Openning %s...\r\n", fname);
FIL file;
FRESULT res = f_open(&file, fname, FA_READ);
if(res != FR_OK) {
//ILI9341_WriteString(0, 10, "f_open() failed, res = %d\r\n", Font_7x10, ILI9341_RED, ILI9341_WHITE);
return -1;
}
// ILI9341_WriteString(0, 20, "File opened, reading...\r\n", Font_7x10, ILI9341_RED, ILI9341_WHITE);
unsigned int bytesRead;
uint8_t header[34];
res = f_read(&file, header, sizeof(header), &bytesRead);
if(res != FR_OK) {
//ILI9341_WriteString(0, 30, "f_read() failed, res = %d\r\n", Font_7x10, ILI9341_RED, ILI9341_WHITE);
f_close(&file);
return -2;
}
if((header[0] != 0x42) || (header[1] != 0x4D)) {
//ILI9341_WriteString(0, 40, "Wrong BMP signature: 0x%02X 0x%02X\r\n", Font_7x10, ILI9341_RED, ILI9341_WHITE);
f_close(&file);
return -3;
}
uint32_t imageOffset = header[10] | (header[11] << 8) | (header[12] << 16) | (header[13] << 24);
uint32_t imageWidth = header[18] | (header[19] << 8) | (header[20] << 16) | (header[21] << 24);
uint32_t imageHeight = header[22] | (header[23] << 8) | (header[24] << 16) | (header[25] << 24);
uint16_t imagePlanes = header[26] | (header[27] << 8);
uint16_t imageBitsPerPixel = header[28] | (header[29] << 8);
uint32_t imageCompression = header[30] | (header[31] << 8) | (header[32] << 16) | (header[33] << 24);
res = f_lseek(&file, imageOffset);
if(res != FR_OK) {
snprintf(buff, sizeof(buff), "f_lseek() failed, res = %d\r\n ", res);
//ILI9341_WriteString(0, 120, buff, Font_7x10, ILI9341_RED, ILI9341_WHITE);
f_close(&file);
return -6;
}
// row size is aligned to 4 bytes
uint8_t imageRow[(ILI9341_LCD_PIXEL_WIDTH * 3 + 3) & ~3];
for(uint32_t y = 0; y < imageHeight; y++) {
uint32_t rowIdx = 0;
res = f_read(&file, imageRow, sizeof(imageRow), &bytesRead);
if(res != FR_OK) {
snprintf(buff, sizeof(buff), "f_read() failed, res = %d\r\n", res);
//ILI9341_WriteString(0, 130, buff, Font_7x10, ILI9341_RED, ILI9341_WHITE);
f_close(&file);
return -7;
}
for(uint32_t x = 0; x < imageWidth; x++) {
uint8_t b = imageRow[rowIdx++];
uint8_t g = imageRow[rowIdx++];
uint8_t r = imageRow[rowIdx++];
uint16_t color565 = ILI9341_COLOR565(r, g, b);
// ILI9341_DrawPixel(x, imageHeight - y - 1, color565);
ili9341_WritePixel(x, imageHeight - y - 1, color565);
}
}
res = f_close(&file);
if(res != FR_OK) {
// UART_Printf("f_close() failed, res = %d\r\n", res);
snprintf(buff, sizeof(buff), "f_close() failed, res = %d\r\n", res);
//ILI9341_WriteString(0, 140, buff, Font_7x10, ILI9341_RED, ILI9341_WHITE);
return -8;
}
return 0;
}