#include #include #include #include #include // File paths const char *imageFile1 = "/images/01_smiley.jpg"; const char *imageFile2 = "/images/20_smiley.jpg"; const char *imageFile3 = "/images/smiley_mix21.jpg"; // TFT and SD TFT_eSPI tft = TFT_eSPI(); void setup() { Serial.begin(115200); delay(1000); // Initialize TFT tft.begin(); tft.setRotation(2); // Adjust as needed tft.fillScreen(TFT_WHITE); // Initialize SD card if (!SD.begin()) { Serial.println("SD card initialization failed!"); return; } Serial.println("SD card initialized."); // Display images displayJpeg(imageFile1, 0); displayJpeg(imageFile2, 1); displayJpeg(imageFile3, 2); } void loop() { // Main loop code (if any) } void displayJpeg(const char *filename, int imageIndex) { // Open the file File jpegFile = SD.open(filename, FILE_READ); if (!jpegFile) { Serial.println("Failed to open file for reading"); return; } // Decode the JPEG file if (JpegDec.decodeSdFile(jpegFile)) { jpegRender(imageIndex); } else { Serial.println("JPEG file format not supported!"); } // Close the file jpegFile.close(); } void jpegRender(int imageIndex) { uint16_t *pImg; uint16_t mcu_w = JpegDec.MCUWidth; uint16_t mcu_h = JpegDec.MCUHeight; uint32_t max_x = JpegDec.width; uint32_t max_y = JpegDec.height; // Calculate the vertical offset int16_t y_offset = 0; // Calculate the horizontal offset based on the imageIndex int16_t x_offset = 0; if (imageIndex == 0) { // First image positioned at 1/4 of TFT width x_offset = tft.width() / 4 - max_x / 2; } else if (imageIndex == 1) { // Second image positioned at 3/4 of TFT width x_offset = 3 * tft.width() / 4 - max_x / 2; } else { // Center third image at the bottom x_offset = (tft.width() - max_x) / 2; y_offset = tft.height() - max_y; } bool swapBytes = tft.getSwapBytes(); tft.setSwapBytes(true); while (JpegDec.read()) { pImg = JpegDec.pImage; int mcu_x = JpegDec.MCUx * mcu_w; int mcu_y = JpegDec.MCUy * mcu_h; uint32_t win_w = (mcu_x + mcu_w <= max_x) ? mcu_w : (max_x % mcu_w); uint32_t win_h = (mcu_y + mcu_h <= max_y) ? mcu_h : (max_y % mcu_h); if ((mcu_x + win_w) <= tft.width() && (mcu_y + win_h) <= tft.height()) { tft.pushImage(mcu_x + x_offset, mcu_y + y_offset, win_w, win_h, pImg); } else if ((mcu_y + win_h) >= tft.height()) { JpegDec.abort(); } } tft.setSwapBytes(swapBytes); }