JPEGDEC with ST7789 Adafruit breakout board

Good morning/afternoon/evening,

I’ve been trying to use the “fast” JPEG decoding library in combination with an ATSAMD51 (Adafruit Itsybitsy M4) and an Adafruit 1.54" SPI based ST7789 display.

JPEGDEC library from bitbank2 includes several sample code, one of them precisely using Adafruit library, but relying on a hard coded JPG file (ie read “binary data embedded in a header file”), which I was able to adapt to my particular display.

Now I’ve been trying to use another sample code which reads SD card JPG image in sequence and display them, but for a different display (so some calls are incompatible with my display library).

This is where my issue start:
1/ I took the JPEGDraw function from the sample that I had working and replaced it in the sample code provided
2/ while the loop occur, the SD card open function seem to work (since it can list the file), but there is no display on my screen aside from the debug statement I’m printing directly (the various “tft.print”)
3/ it actually perform the loop once, any further “loop” will actually not read from SD anymore and jump directly to the end of the loop (at least from what I can see in my debug statements). This seems to be related to the entry statement, but I would have thought that entry.close() would reset it ?

Thanks for any guidance you can share, code below

// A simple image slideshow which reads all the .JPG files from the root
// directory of a SD card and shows each for 1 second on an ILI9341 display.

#include <JPEGDEC.h>
// #include <ILI9341_t3.h>
// #include <Bounce.h>

#include <SD.h>
#include <Adafruit_ST7789.h> // Hardware-specific library for ST7789
#include <Adafruit_GFX.h>
#include <SD.h>

#define SD_CS    10 // SD card select pin
#define TFT_CS  9 // TFT select pin
#define TFT_DC   5 // TFT display/command pin
#define TFT_RST  7 // Or set to -1 and connect to Arduino RESET pin

// SdFat                SD;         // SD card filesystem
// Adafruit_Image       img;        // An image loaded into RAM
 int32_t              width  = 0, // BMP image dimensions
                     height = 0;

Adafruit_ST7789 tft = Adafruit_ST7789(TFT_CS, TFT_DC, TFT_RST);
JPEGDEC jpeg;

// Setup - initialize ILI9341 display, wait for serial monitor, open SD card
void setup() {
  
  Serial.begin(115200);
  tft.init(240,240);
  tft.setRotation(3);
  tft.fillScreen(ST77XX_BLACK);
  tft.setTextColor(ST77XX_YELLOW);
  tft.setTextSize(2);
  tft.println("Waiting for Arduino Serial Monitor...");

  while (!Serial); // wait up to 3 seconds for Arduino Serial Monitor
  Serial.println("ILI9341 Slideshow");
  tft.fillScreen(ST77XX_BLACK);
  tft.setCursor(0, 0);

  while (!SD.begin(SD_CS)) {
    Serial.println("Unable to access SD Card");
    tft.println("Unable to access SD Card");
    delay(1000);
  }
}

// Functions to access a file on the SD card
File myfile;

void * myOpen(const char *filename, int32_t *size) {
  myfile = SD.open(filename);
  *size = myfile.size();
  return &myfile;
}
void myClose(void *handle) {
  if (myfile) myfile.close();
}
int32_t myRead(JPEGFILE *handle, uint8_t *buffer, int32_t length) {
  if (!myfile) return 0;
  return myfile.read(buffer, length);
}
int32_t mySeek(JPEGFILE *handle, int32_t position) {
  if (!myfile) return 0;
  return myfile.seek(position);
}

// Function to draw pixels to the display
void JPEGDraw(JPEGDRAW *pDraw) {
      tft.dmaWait(); // Wait for prior writePixels() to finish
  tft.setAddrWindow(pDraw->x, pDraw->y, pDraw->iWidth, pDraw->iHeight);
  tft.writePixels(pDraw->pPixels, pDraw->iWidth * pDraw->iHeight, true, false); // Use DMA, big-endian
}

// Main loop, scan for all .JPG files on the card and display them
void loop() {
  int filecount = 0;
  tft.setCursor(0, 0);
  File dir = SD.open("/");
  while (true) {
    File entry = dir.openNextFile();
    if (!entry) break;
    if (entry.isDirectory() == false) {
      const char *name = entry.name();
      const int len = strlen(name);
      if (len > 3 && strcasecmp(name + len - 3, "JPG") == 0) {
        Serial.print("File: ");
        Serial.println(name);
        tft.print("File: ");
        tft.println(name);
        jpeg.open((const char *)name, myOpen, myClose, myRead, mySeek, JPEGDraw);
        jpeg.decode(0, 0, 0);
        jpeg.close();
        filecount = filecount + 1;
        Serial.println(filecount);
        delay(1000);
        }
      }
      entry.close();
    }
    Serial.println("Exit While True statement");
    
  if (filecount == 0) {
    Serial.println("No .JPG files found");
    tft.println("No .JPG files found");
    delay(2000);
    }

}