Esp32 to tft lcd with micro sd

hi im making an lcd thats controlled by an esp32s2 devkitc-1

in short the display shows an image with som values that are clearly wrong but i dont really know how to fix it


#include <Arduino.h>
#include <SD.h>
#include <SPI.h>
#include <TFT_eSPI.h> // Hardware-specific library

TFT_eSPI tft = TFT_eSPI();       // Invoke custom library

#define TFT_WR  15  // Write strobe control pin - must use a pin in the range 0-31
#define TFT_DC  16  // Data Command control pin - must use a pin in the range 0-31  (LCD_RS on my display)
#define TFT_CS  17  // Chip select control pin
#define TFT_RST 18  // Reset pin
#define TFT_RD  7

#define TFT_D0  3   // Must use pins in the range 0-31 for the data bus
#define TFT_D1  8   // so a single register write sets/clears all bits
#define TFT_D2  14
#define TFT_D3  13
#define TFT_D4  12
#define TFT_D5  11
#define TFT_D6  10
#define TFT_D7  9

#define SD_CS   39
#define SD_MOSI 40
#define SD_MISO 41
#define SD_SCK  42

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

  tft.init();
  tft.setRotation(1);
  tft.fillScreen(TFT_GREEN);
  delay(2000);
  tft.fillScreen(TFT_BLUE);
  delay(2000);
  tft.fillScreen(TFT_RED);

  Serial.println("Initializing SD card...");

  // Manually define SPI pins
  SPI.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);

  // Initialize SD card
  if (!SD.begin(SD_CS, SPI)) {
    Serial.println("SD Card initialization failed!");
    return;
  }

  Serial.println("SD Card initialized successfully.");

  // List files in the root directory
  File root = SD.open("/");
  printDirectory(root, 0);
}


void printDirectory(File dir, int numTabs) {
  while (true) {
    File entry = dir.openNextFile();
    if (!entry) {
      // No more files
      break;
    }
    
    for (int i = 0; i < numTabs; i++) {
      Serial.print("\t");
    }
    
    Serial.print(entry.name());
    
    if (entry.isDirectory()) {
      Serial.println("/");
      printDirectory(entry, numTabs + 1); // Recursive call for subdirectories
    } else {
      Serial.print("\t");
      Serial.print(entry.size(), DEC);
      Serial.println(" bytes");
    }
    
    entry.close();
  }
}


void drawBMP(const char *filename, int x, int y) {
  File bmpFile = SD.open(filename);
  if (!bmpFile) {
    Serial.println("File not found");
    return;
  }

  uint32_t dataOffset;
  bmpFile.seek(10);
  bmpFile.read((uint8_t*)&dataOffset, 4); // Read the pixel data offset from the BMP header

  uint16_t w = 480, h = 320; // Image dimensions
  uint8_t row[w * 3]; // BMP stores pixels in 24-bit RGB format
  uint16_t convertedRow[w]; // Converted to 16-bit color

  tft.startWrite();
  bmpFile.seek(dataOffset); // Move to the start of pixel data

  for (int16_t rowIndex = h - 1; rowIndex >= 0; rowIndex--) {
    bmpFile.read(row, w * 3);
    
    for (int i = 0; i < w; i++) {
      uint8_t b = row[i * 3];       // Blue channel
      uint8_t g = row[i * 3 + 1];   // Green channel
      uint8_t r = row[i * 3 + 2];   // Red channel
      convertedRow[i] = tft.color565(b, r, g);
    }
    
    tft.pushImage(x, y + rowIndex, w, 1, convertedRow);
  }

  tft.endWrite();
  bmpFile.close();
}

void loop() {
  drawBMP("/Civic.bmp", 0, 0);
  while (true); // Stop execution after drawing
}

tft.color565(b, r, g);

right now wrong because i wanted to test if it would fix it (it did in fact not)

Perhaps r, g, b?

yeah i've tried but the image is still messed up

im dumb

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.