@Bodmer,
I have adapted your SPIFFS_Jpg.ino example to render any JPG that it finds on SPIFFS
I have upgraded my ESP8266 to v2.5.2
SPIFFS works differently on ESP8266 or ESP32 e.g. walking the Directory.
Your Library class certainly makes everything tidier.
David.
// Example for library:
// https://github.com/Bodmer/TJpg_Decoder
// This example if for an ESP8266 or ESP32, it renders a Jpeg file
// that is stored in a SPIFFS file. The test image is in the sketch
// "data" folder (press Ctrl+K to see it). You must upload the image
// to SPIFFS using the ESP8266 or ESP32 Arduino IDE upload menu option.
// Include the jpeg decoder library
#include <TJpg_Decoder.h>
// Include SPIFFS
#define FS_NO_GLOBALS
#include <FS.h>
#ifdef ESP32
#include "SPIFFS.h" // ESP32 only
#endif
// Include the TFT library https://github.com/Bodmer/TFT_eSPI
#include "SPI.h"
#include <TFT_eSPI.h> // Hardware-specific library
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library
// This next function will be called during decoding of the jpeg file to
// render each block to the TFT. If you use a different TFT library
// you will need to adapt this function to suit.
bool tft_output(int16_t x, int16_t y, uint16_t w, uint16_t h, uint16_t* bitmap)
{
// Stop further decoding as image is running off bottom of screen
if ( y >= tft.height() ) return 0;
// This function will clip the image block rendering automatically at the TFT boundaries
tft.pushImage(x, y, w, h, bitmap);
// This might work instead if you adapt the sketch to use the Adafruit_GFX library
// tft.drawRGBBitmap(x, y, bitmap, w, h);
// Return 1 to decode next block
return 1;
}
void setup()
{
Serial.begin(115200);
Serial.println("\n\n Testing TJpg_Decoder library");
// Initialise SPIFFS
if (!SPIFFS.begin()) {
Serial.println("SPIFFS initialisation failed!");
while (1) yield(); // Stay here twiddling thumbs waiting
}
Serial.println("\r\nInitialisation done.");
// Initialise the TFT
tft.begin();
tft.setTextColor(0xFFFF, 0x0000);
tft.fillScreen(TFT_BLACK);
tft.setSwapBytes(true); // We need to swap the colour bytes (endianess)
// The jpeg image can be scaled by a factor of 1, 2, 4, or 8
TJpgDec.setJpgScale(1);
// The decoder must be given the exact name of the rendering function above
TJpgDec.setCallback(tft_output);
}
void doSomething(const char *name)
{
tft.fillScreen(TFT_RED);
// Time recorded for test purposes
uint32_t t = millis();
// Get the width and height in pixels of the jpeg if you wish
uint16_t w = 0, h = 0, scale;
TJpgDec.getFsJpgSize(&w, &h, name); // Note name preceded with "/"
tft.setRotation(w > h ? 1 : 0);
for (scale = 1; scale <= 8; scale <<= 1) {
if (w <= tft.width() * scale && h <= tft.height() * scale) break;
}
TJpgDec.setJpgScale(scale);
// Draw the image, top left at 0,0
TJpgDec.drawFsJpg(0, 0, name);
// How much time did rendering take (ESP8266 80MHz 291ms, 160MHz 167ms, ESP32 SPI 125ms, 8bit parallel 105ms
t = millis() - t;
char buf[80];
sprintf(buf, "%s %dx%d 1/%d %ld ms", name, w, h, scale, t);
tft.setCursor(0, tft.height() - 8);
tft.print(buf);
Serial.println(buf);
delay(2000);
}
//====================================================================================
// Loop
//====================================================================================
#if defined(ESP32)
void loop()
{
File directory = SPIFFS.open("/");
File f;
while (f = directory.openNextFile()) {
String bum = f.name();
const char *s = bum.c_str();
if (strstr(s, ".jpg") != NULL) {
doSomething(s);
}
}
}
#else //old ESP8266 has different SPIFFS methods
void loop()
{
fs::Dir directory = SPIFFS.openDir("/");
while (directory.next()) {
String bum = directory.fileName();
const char *s = bum.c_str();
if (strstr(s, ".jpg") != NULL) {
doSomething(s);
}
}
}
#endif