Ejemplo 1: LED y Button Push
Vamos probar los LEDs y los botón pulsador, vamos hacer un ejemplo de funcionamiento.
1. Pines
OLED
| ESP32-S3 | Descripciónn |
|---|---|
| 12 | TFT_SCK |
| 11 | TFT_MOSI |
| 10 | TFT_RST |
| 9 | TFT_DC |
| 8 | TFT_CS |
MiroSD
| ESP32-S3 | Descripciónn |
|---|---|
| 4 | SD_SCK |
| 5 | SD_MISO |
| 6 | SD_MOSI |
| 7 | SD_CS |
2. Codigo
#include <SPI.h>
#include <SD.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7789.h>
#include <vector>
// Pantalla (bus FSPI)
#define TFT_SCK 12
#define TFT_MOSI 11
#define TFT_RST 10
#define TFT_DC 9
#define TFT_CS 8
// SD (bus HSPI)
#define SD_SCK 4
#define SD_MISO 5
#define SD_MOSI 6
#define SD_CS 7
const int ANCHO = 240, ALTO = 240; // cambia a 240, 320 si es tu pantalla
const int TAM_TEXTO = 2; // 1 = letra pequeña, 2 = mediana
const int ALTO_LINEA = 8 * TAM_TEXTO + 2;
const int Y_INICIO = 8 * TAM_TEXTO + 8; // espacio para el título
const int MAX_ENTRADAS = 200; // límite para no agotar la RAM
const unsigned long MS_PAGINA = 3000;
Adafruit_ST7789 tft(TFT_CS, TFT_DC, TFT_RST);
SPIClass spiSD(HSPI);
struct Entrada {
String nombre;
String tam;
bool esDir;
};
std::vector<Entrada> lista;
int pagina = 0, lineasPorPagina, totalPaginas;
unsigned long ultimoCambio = 0;
String formatoTam(size_t b) {
if (b < 1024) return String(b) + "B";
if (b < 1048576) return String(b / 1024.0, 1) + "K";
return String(b / 1048576.0, 1) + "M";
}
void leerDir(fs::FS &fs, const char *ruta, uint8_t niveles, int sangria) {
File dir = fs.open(ruta);
if (!dir || !dir.isDirectory()) return;
File f = dir.openNextFile();
while (f && lista.size() < MAX_ENTRADAS) {
String pref = "";
for (int i = 0; i < sangria; i++) pref += " ";
if (f.isDirectory()) {
lista.push_back({pref + f.name() + "/", "", true});
if (niveles > 0) leerDir(fs, f.path(), niveles - 1, sangria + 1);
} else {
lista.push_back({pref + f.name(), formatoTam(f.size()), false});
}
f.close();
f = dir.openNextFile();
}
dir.close();
}
void mensaje(const char *txt, uint16_t color) {
tft.fillScreen(ST77XX_BLACK);
tft.setTextColor(color);
tft.setCursor(4, ALTO / 2 - 8);
tft.print(txt);
}
void dibujarPagina() {
int anchoChar = 6 * TAM_TEXTO;
int maxChars = ANCHO / anchoChar;
tft.fillScreen(ST77XX_BLACK);
// Título
tft.fillRect(0, 0, ANCHO, Y_INICIO - 4, ST77XX_BLUE);
tft.setTextColor(ST77XX_WHITE);
tft.setCursor(4, 2);
tft.printf("SD %d/%d", pagina + 1, totalPaginas);
// Líneas
int desde = pagina * lineasPorPagina;
int hasta = min((int)lista.size(), desde + lineasPorPagina);
for (int i = desde; i < hasta; i++) {
int y = Y_INICIO + (i - desde) * ALTO_LINEA;
Entrada &e = lista[i];
int espacioNombre = maxChars - (e.tam.length() > 0 ? e.tam.length() + 1 : 0);
String nom = e.nombre;
if ((int)nom.length() > espacioNombre) {
nom = nom.substring(0, espacioNombre - 1) + "~";
}
tft.setCursor(0, y);
tft.setTextColor(e.esDir ? ST77XX_YELLOW : ST77XX_WHITE);
tft.print(nom);
if (e.tam.length()) {
tft.setCursor(ANCHO - e.tam.length() * anchoChar, y);
tft.setTextColor(ST77XX_CYAN);
tft.print(e.tam);
}
}
}
void setup() {
Serial.begin(115200);
SPI.begin(TFT_SCK, -1, TFT_MOSI);
tft.init(ANCHO, ALTO);
tft.setRotation(0); // prueba 1, 2 o 3 si sale girada
tft.setTextWrap(false);
tft.setTextSize(TAM_TEXTO);
mensaje("Leyendo SD...", ST77XX_WHITE);
spiSD.begin(SD_SCK, SD_MISO, SD_MOSI, SD_CS);
if (!SD.begin(SD_CS, spiSD, 4000000)) {
mensaje("Error al montar SD", ST77XX_RED);
return;
}
leerDir(SD, "/", 3, 0);
if (lista.empty()) {
mensaje("SD vacia", ST77XX_YELLOW);
return;
}
lineasPorPagina = (ALTO - Y_INICIO) / ALTO_LINEA;
totalPaginas = (lista.size() + lineasPorPagina - 1) / lineasPorPagina;
dibujarPagina();
ultimoCambio = millis();
}
void loop() {
if (totalPaginas > 1 && millis() - ultimoCambio >= MS_PAGINA) {
pagina = (pagina + 1) % totalPaginas;
dibujarPagina();
ultimoCambio = millis();
}
}