Reputation: 13
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().
Upvotes: 1
Views: 514
Reputation: 4770
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();
}
Upvotes: 0