#include #include #include #include #include // Image paths and corresponding values const char *images1[] = {"/images/05_smiley.jpg", "/images/04_smiley.jpg", "/images/03_smiley.jpg", "/images/02_smiley.jpg", "/images/01_smiley.jpg"}; const int values1[] = {5, 4, 3, 2, 1}; const char *images2[] = {"/images/50_smiley.jpg", "/images/40_smiley.jpg", "/images/30_smiley.jpg", "/images/20_smiley.jpg", "/images/10_smiley.jpg"}; const int values2[] = {50, 40, 30, 20, 10}; struct ImageData { const char *src; int value; }; ImageData allImages[] = { {"/images/smiley_mix11.jpg", 11}, {"/images/smiley_mix12.jpg", 12}, {"/images/smiley_mix13.jpg", 13}, {"/images/smiley_mix14.jpg", 14}, {"/images/smiley_mix15.jpg", 15}, {"/images/smiley_mix21.jpg", 21}, {"/images/smiley_mix22.jpg", 22}, {"/images/smiley_mix23.jpg", 23}, {"/images/smiley_mix24.jpg", 24}, {"/images/smiley_mix25.jpg", 25}, {"/images/smiley_mix31.jpg", 31}, {"/images/smiley_mix32.jpg", 32}, {"/images/smiley_mix33.jpg", 33}, {"/images/smiley_mix34.jpg", 34}, {"/images/smiley_mix35.jpg", 35}, {"/images/smiley_mix41.jpg", 41}, {"/images/smiley_mix42.jpg", 42}, {"/images/smiley_mix43.jpg", 43}, {"/images/smiley_mix44.jpg", 44}, {"/images/smiley_mix45.jpg", 45}, {"/images/smiley_mix51.jpg", 51}, {"/images/smiley_mix52.jpg", 52}, {"/images/smiley_mix53.jpg", 53}, {"/images/smiley_mix54.jpg", 54}, {"/images/smiley_mix55.jpg", 55}, }; int currentIndex1 = 2; int currentIndex2 = 2; const int numImages1 = sizeof(images1) / sizeof(images1[0]); const int numImages2 = sizeof(images2) / sizeof(images2[0]); const int numAllImages = sizeof(allImages) / sizeof(allImages[0]); // 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 based on currentIndex displayJpeg(images1[currentIndex1], 0); displayJpeg(images2[currentIndex2], 1); int sumValue = values1[currentIndex1] + values2[currentIndex2]; const char *thirdImage = findImageByValue(sumValue); if (thirdImage) { displayJpeg(thirdImage, 2); } else { Serial.print("No image found with value "); Serial.println(sumValue); } } void loop() { // Main loop code (if any) } const char *findImageByValue(int value) { for (int i = 0; i < numAllImages; i++) { if (allImages[i].value == value) { return allImages[i].src; } } return nullptr; // Return nullptr if no image with the required value is found } void displayJpeg(const char *filename, int imageIndex) { Serial.print("Opening file: "); Serial.println(filename); // Open the file File jpegFile = SD.open(filename, FILE_READ); if (!jpegFile) { Serial.print("Failed to open file "); Serial.println(filename); return; } // Decode the JPEG file if (JpegDec.decodeSdFile(jpegFile)) { Serial.print("Decoding successful for "); Serial.println(filename); jpegRender(imageIndex); } else { Serial.print("JPEG file format not supported for "); Serial.println(filename); } // 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; Serial.print("Rendering image at index "); Serial.print(imageIndex); Serial.print(", width: "); Serial.print(max_x); Serial.print(", height: "); Serial.println(max_y); // Calculate the vertical and horizontal offsets int16_t x_offset = 0; int16_t y_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); }