TFT_ESPI Random SD Images

Hello All,

I have loaded and run the Example/Generic/ESP32_SDcard_jpeg (Bodmer) and it works well.

What I can't find how to do (I have tried my own ideas also searched the "net", but cant find how to display .jpg images randomly from the SD Card.

Is there a solution someone can provide using the example or another TFT_ESPI sketch.

I hope you are much smarter than me and can show me how to do it.

Many thanks,

Radical Data.

Make a list of the file names, then randomize the order in which they are accessed.

Since a random number generator will occasionally give you repeats, the usual way is to "shuffle" the order of the list, then go through it in order. Look up "shuffle array" for the method.

Hello jremington and all,

Thank you for your suggestion but it doesn't quite apply to my question.

Here is are a few lines from the program (sketch) I had referred to..........

tft.setRotation(2); // portrait
tft.fillScreen(random(0xFFFF));
drawSdJpeg("/Baboon40.jpg", 0, 0); // This draws a jpeg pulled off the SD Card
delay(2000);

Where it calls for ("/Baboon40.jpg", 0, 0) , I cant find a way to make it call for a random .jpg on the SD card rather than a seperate call for each .jpg on the SD card.

Hope this makes sense.

Thank you for any replies.

You can't, unless you know the actual file names. See post #2, which explains how to do what you want.

Hello jremington and all,

With this piece of script below, it does not require knowing file names, it just displays one after another ...............
File file;
File root = SD.open("/");
if(!root){
Serial.println("Failed to open root directory");
return;
}
file = root.openNextFile(); // Opens next file in root
while(file)
{
if(!file.isDirectory())
{
drawSdJpeg(file.name(), 0, 0); // This draws a jpeg pulled off the SD Card
delay(4000);
}
file = root.openNextFile(); // Opens next file in root
}

root.close();

So I am guessing that RANDOM is possible somehow.

Thank you.

That function reads the file name and then opens it. You could call it a random bunch of times.

Good luck with your project!

Solved Thanks to Bodmer

Yes, the drawSdJpeg function expects a string, so you need to create a string with the number in it plus the .jpg extension. For example:

  char filename [20]; // 19 chars max + terminating null
  int i = 42;
  itoa(i,filename,10); // Put string version of a number in filename, number base 10
  strcat(filename, ".jpg"); // add .jpg to end of string
  drawSdJpeg(filename, 0, 0); 

That (number in it) can be random generated.

You could use a switch--case scheme too: a 'random' number would be the case and that would result a filename call, without having to rename your files.

Hi there runaway_pancake,

Thank you for your suggestion.
I will have a look into it.

Thank you :slight_smile:

Hello there runaway_pancake,
Going with your suggestion, it would mean a Case (line) for every jpg file on the sd card.
If the sd card has 25 jpg's on it, that would be 25 individual case lines.
Correct me if I am wrong with that assumption.

Thank you,
Pedro1.

Your deduction is sound.