#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#define TFT_DC 1
#define TFT_CS 2
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// ESP32-C6 Pin Mapping Example (Adjust according to your wiring)
//#define TFT_CS 6
//#define TFT_DC 7
//#define TFT_RST 5
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Custom helper function to inject the mirror bit directly into the hardware register
void setMirroredRotation(uint8_t rotationIndex) {
// First, let the Adafruit library set up internal width/height boundaries normally
tft.setRotation(rotationIndex);
uint8_t madctl_value = 0;
// Read standard Adafruit configurations and inject the mirror bit
switch (rotationIndex) {
case 0: madctl_value = 0x48 ^ 0x40; break; // Standard Portrait -> Mirrored
case 1: madctl_value = 0x28 ^ 0x40; break; // Standard Landscape -> Mirrored
case 2: madctl_value = 0x88 ^ 0x40; break; // Inverted Portrait -> Mirrored
case 3: madctl_value = 0xE8 ^ 0x40; break; // Inverted Landscape -> Mirrored
}
// Transmit command directly to the ILI9341 Register 0x36 (MADCTL)
tft.startWrite();
tft.writeCommand(ILI9341_MADCTL);
tft.spiWrite(madctl_value);
tft.endWrite();
}
void setup() {
Serial.begin(115200);
// Initialize the screen
tft.begin();
// Use our custom function instead of standard tft.setRotation()
// Try indices 0, 1, 2, or 3 depending on whether you want portrait or landscape
setMirroredRotation(0);
// Draw test text to confirm it's readable
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(20, 20);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.println("Mirror Fixed!");
}
void loop() {
// Your code here
}5V
3V3
3V3