Xinyuan-LilyGO / T-Display-S3

MIT License
729 stars 172 forks source link

Trying to use SD card for images #249

Open Ron230 opened 1 month ago

Ron230 commented 1 month ago

I am trying to show different images on the e paper. The images should be saved on the sd card. I have the pictures in the correct file format like in the draw images example. I have about 30 Images saved there and they should be shown in the order they are saved. I need to read the image, show it on the display, go into deep sleep and after some time show the next image. When I upload my code the e paper does nothing. The SD card is formatted correctly and gets detected in the demo program. I also updated the microprocessor version, the e paper version. I also managed to show my own images when I include them but not from the sd card. The code is without deepsleep at the moment but will be implemented when the images can be shown.

`#include

include

include

include "epd_driver.h" // Include the correct e-paper driver header

define SD_MISO 16

define SD_MOSI 15

define SD_SCLK 11

define SD_CS 42 // Adjust pin according to your wiring

uint8_t *framebuffer; File root; File currentFile; bool isFileOpen = false;

void setup() { Serial.begin(115200);

// Initialize e-paper display
Serial.println("Initializing e-paper display...");
epd_init();
framebuffer = (uint8_t *)heap_caps_malloc(EPD_WIDTH * EPD_HEIGHT / 2, MALLOC_CAP_SPIRAM);
memset(framebuffer, 0xFF, EPD_WIDTH * EPD_HEIGHT / 2);

// Initialize SD card
Serial.println("Initializing SD card...");
SPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
if (!SD.begin(SD_CS)) {
    Serial.println("Card Mount Failed");
    return;
}
Serial.println("SD Card initialized.");

// Open root directory
root = SD.open("/");
if (!root) {
    Serial.println("Failed to open root directory");
    return;
}
Serial.println("Root directory opened.");

}

void loop() { if (!isFileOpen) { currentFile = root.openNextFile(); if (!currentFile) { // If no more files, restart reading from the beginning root.rewindDirectory(); currentFile = root.openNextFile(); }

    if (currentFile && !currentFile.isDirectory()) {
        String fileName = currentFile.name();
        if (fileName.endsWith(".h")) {
            Serial.println("Displaying: " + fileName);
            readFile(SD, fileName.c_str());
            isFileOpen = true;
        } else {
            currentFile.close();
        }
    } else {
        if (currentFile) currentFile.close();
    }
}

if (isFileOpen) {
    displayImageFromFile(currentFile);
    delay(6000);  // Wait for 6 seconds before displaying the next image
    currentFile.close();
    isFileOpen = false;
}

}

void readFile(fs::FS &fs, const char *path) { Serial.printf("Reading file: %s\n", path);

File file = fs.open(path); if (!file) { Serial.println("Failed to open file for reading"); return; }

Serial.print("Read from file: "); while (file.available()) { Serial.write(file.read()); } file.close(); }

void displayImageFromFile(File file) { Serial.println("Reading file content..."); // Read file content into a buffer size_t fileSize = file.size(); char fileContent = (char )heap_caps_malloc(fileSize + 1, MALLOC_CAP_SPIRAM); if (!fileContent) { Serial.println("Failed to allocate memory for file content"); return; } file.readBytes(fileContent, fileSize); fileContent[fileSize] = '\0'; // Null-terminate the string

// Extract image width, height, and data from the file content
uint32_t width = extractUInt32(fileContent, "pic1_width");
uint32_t height = extractUInt32(fileContent, "pic1_height");
const uint8_t *data = extractImageData(fileContent, "pic1_data");

if (data && width > 0 && height > 0) {
    Rect_t area = {
        .x = 0,
        .y = 0,
        .width = width,
        .height = height
    };
    Serial.println("Displaying image on e-paper...");
    epd_poweron();
    epd_clear();
    epd_draw_grayscale_image(area, (uint8_t *)data);
    epd_poweroff();
    Serial.println("Image displayed.");
} else {
    Serial.println("Failed to extract image data.");
}

heap_caps_free(fileContent);

}

uint32_t extractUInt32(const char content, const char key) { String searchString = String(key) + " = "; const char *found = strstr(content, searchString.c_str()); if (found) { return strtoul(found + searchString.length(), NULL, 10); } return 0; }

const uint8_t extractImageData(const char content, const char key) { String searchString = String(key) + " = {"; const char found = strstr(content, searchString.c_str()); if (found) { found += searchString.length(); return (const uint8_t *)found; } return nullptr; } `

github-actions[bot] commented 1 week ago

This issue is stale because it has been open for 30 days with no activity.