1

I am a relatively beginner programmer, and I've been combing for an answer for a while now. Coming up empty handed, I decided it was time to reach out. This is my first stack overflow post, be gentle.

I am trying to build a program to retrieves a jpeg files from SPIFFS and loads image on the ESP32 TFT. I've managed access the files in spiffs, to display the photos, but the file names are hardcoded into the program currently. I would like the program to read the file names, and load into drawJpeg(). I am struggling to figure out how to obtain a return from my getFileName() function. Code below:

#define FS_NO_GLOBALS
#include <Arduino.h>
#include <FS.h>
#include <string.h>
#include <iostream>

// #ifdef ESP32
#include "SPIFFS.h" // ESP32 only
// #endif

#include <TFT_eSPI.h>      // Hardware-specific library
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library

// JPEG decoder library
#include <JPEGDecoder.h>
#include "JPEG_functions.h"

// Declaration
void getFileName();

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

    Serial.printf("\nSerial Debugger active!\n");

    // Initialize TFT
    tft.begin();
    tft.setRotation(1);
    tft.fillScreen(TFT_BLUE);
    delay(10);

// Initialize SPIFFS
    if (!SPIFFS.begin(true))
    {
        Serial.println("SPIFFS initialisation failed!");
    }
    Serial.println("\nSPIFFS Initialisation Complete.\n");

    getFileName();


    tft.setRotation(1);
    tft.fillScreen(TFT_RED);
}

void loop()
{

    drawJpeg("/001.jpg", 0, 0);
    delay(2500);
    drawJpeg("/002.jpg", 0, 0);
    delay(2500);
    drawJpeg("/003.jpg", 0, 0);
    delay(2500);
    drawJpeg("/004.jpg", 0, 0);
    delay(2500);
    drawJpeg("/005.jpg", 0, 0); // 240 x 320 image
    delay(2500); 

}

void getFileName()
{
    // Open SPIFFS
    File root = SPIFFS.open("/");
    File file = root.openNextFile();


    // Print SPIFFS file names
    while (file)
    {
        Serial.print("FILE DETECTED\n");
        Serial.print("FILE: ");
        Serial.println(file.name());
        file = root.openNextFile();
    }

}

I've tried a number of changes that result in programming not compiling. I am missing a basic understanding about how to handle the File file variable and call to name().

realreject
  • 13
  • 4

1 Answers1

0

What exactly do you plan to do with those files? If you want to go over each file and display it, then you don't need to involve any functions for determining the file name - the File already does that with method name(). Just do what you want to do in the main loop() and you're done.

void loop()
{
    File root = SPIFFS.open("/");
    File file = root.openNextFile();
    while (file)
        Serial.println(file.name());
        drawJpeg(file.name(), 0, 0);
        delay(2500);
        file = root.openNextFile();
}
Tarmo
  • 3,728
  • 1
  • 8
  • 25
  • Thank you! I was over complicating it! Any suggested on how to add forward slash to the call? drawJpeg requires the filename to have / before. The end goal of my project is simply to learn more about C/C++. My project is an ESP32-S3 Lilygo T-Display using sprites to display a menu over top a scrolling jpeg sprite. My original project was using an array to store the images in PROGMEM, and switch between the two background images with press of the GPIO 14 button. I could only hold two images in PROGMEM, so I've ventured towards SPIFFS to hold more images. – realreject Mar 04 '23 at 23:25
  • There are two solutions. The better solution is to ask the file for its full name including directory paths: `file.fullName()` instead of `file.name()`. If that doesn't work, we can fall back to plain old string concatenation. Since C++ natively doesn't have that, we'll need to ask Arduino `String` type to do the work for us: `drawJpeg(String("/") + file.name())` – Tarmo Mar 05 '23 at 15:04