#include <Adafruit_GFX.h>
#include <SPI.h>
#include <Adafruit_ILI9341.h>
#include <Arduino.h>
#include <Adafruit_FT6206.h>
#include <Fonts/FreeSerifBold12pt7b.h>
#include "question_mark_bitmap.h"
#include "pleading_face_bitmap.h"
#include "partying_face_bitmap.h"
#define TFT_DC 2
#define TFT_CS 15
Adafruit_ILI9341 tft(TFT_CS, TFT_DC);
Adafruit_FT6206 ctp;
const uint16_t colors[] = {
ILI9341_GREEN, ILI9341_CYAN, ILI9341_MAGENTA, ILI9341_YELLOW,
ILI9341_RED, ILI9341_BLUE, ILI9341_WHITE, ILI9341_ORANGE
};
const uint8_t numColors = sizeof(colors) / sizeof(colors[0]);
uint8_t colorIndex = 0;
uint8_t textColorIndex = 0; // Índice de color para el texto "Gracias profe"
unsigned long lastColorChange = 0;
const unsigned long colorChangeInterval = 500;
bool showPleadingFace = false;
bool showParty = false;
const int16_t boxWidth = 80;
const int16_t boxHeight = 50;
const int16_t optionsY = 220;
int16_t yesX, noX;
void setup() {
Serial.begin(115200);
Serial.println("Inicializando ESP32 y pantalla TFT");
// Configurar los pines I2C en el ESP32-S3
Wire.begin(10, 8);
tft.begin();
if (!ctp.begin(40)) {
Serial.println("No se pudo iniciar el controlador táctil FT6206");
while (1);
}
Serial.println("Pantalla táctil capacitiva iniciada");
tft.setFont(&FreeSerifBold12pt7b);
tft.fillScreen(ILI9341_BLACK);
yesX = tft.width() / 4;
noX = 3 * tft.width() / 4;
drawOptions();
drawQuestion();
}
void drawQuestion() {
const char* line1 = "Quieres ser";
const char* line2 = "nuestro Padrino";
int16_t x1, y1;
uint16_t w1, h1;
tft.getTextBounds(line1, 0, 0, &x1, &y1, &w1, &h1);
int16_t iconSize = 16;
int16_t startX = (tft.width() - (w1 + iconSize)) / 2;
tft.drawBitmap(startX, 80, question_mark_open_bitmap, iconSize, iconSize, colors[colorIndex]);
tft.setCursor(startX + iconSize + 2, 70 + h1);
tft.setTextColor(colors[colorIndex]);
tft.print(line1);
tft.getTextBounds(line2, 0, 0, &x1, &y1, &w1, &h1);
startX = (tft.width() - (w1 + iconSize)) / 2;
tft.setCursor(startX, 80 + h1 + 20);
tft.print(line2);
tft.drawBitmap(startX + w1 + 2, 80 + h1 + 20 - iconSize, question_mark_close_bitmap, iconSize, iconSize, colors[colorIndex]);
}
void drawOptions() {
drawButton(yesX, optionsY, "SI", ILI9341_GREEN);
drawButton(noX, optionsY, "NO", ILI9341_RED);
}
void drawButton(int16_t x, int16_t y, const char* label, uint16_t color) {
// Dibuja el botón con esquinas redondeadas y relleno
tft.fillRoundRect(x - boxWidth / 2, y - boxHeight / 2, boxWidth, boxHeight, 10, color); // radio de 10 para esquinas redondeadas
tft.drawRoundRect(x - boxWidth / 2, y - boxHeight / 2, boxWidth, boxHeight, 10, ILI9341_WHITE); // borde blanco
// Centramos el texto dentro del botón
int16_t x1, y1;
uint16_t w, h;
tft.getTextBounds(label, 0, 0, &x1, &y1, &w, &h);
tft.setCursor(x - w / 2, y + h / 2);
tft.setTextColor(ILI9341_WHITE); // Color del texto en blanco
tft.print(label);
}
void loop() {
if (showPleadingFace) {
displayPleadingFace();
while (1); // Pausar el bucle principal una vez que se muestra el "pleading face"
}
if (showParty) {
displayPartyingFace();
while (1); // Pausar el bucle principal una vez que se muestra el "partying face"
}
if (millis() - lastColorChange >= colorChangeInterval) {
lastColorChange = millis();
colorIndex = (colorIndex + 1) % numColors;
textColorIndex = (textColorIndex + 1) % numColors; // Cambia el índice de color para el texto
drawQuestion();
}
if (ctp.touched()) {
TS_Point p = ctp.getPoint();
uint16_t x = map(p.x, 0, 240, tft.width(), 0);
uint16_t y = map(p.y, 0, 320, tft.height(), 0);
Serial.printf("Toque en: (%d, %d)\n", x, y);
if (isInsideButton(x, y, noX, optionsY)) {
showPleadingFace = true;
} else if (isInsideButton(x, y, yesX, optionsY)) {
showParty = true;
}
}
}
bool isInsideButton(int16_t x, int16_t y, int16_t btnX, int16_t btnY) {
return (x > btnX - boxWidth / 2 && x < btnX + boxWidth / 2 &&
y > btnY - boxHeight / 2 && y < btnY + boxHeight / 2);
}
void displayPartyingFace() {
tft.fillScreen(ILI9341_BLACK); // Limpiar la pantalla
// Mover la imagen 20 píxeles hacia arriba
int16_t startX = (tft.width() - 180) / 2;
int16_t startY = ((tft.height() - 180) / 2) - 20;
// Dibujar el bitmap en RGB565
tft.drawRGBBitmap(startX, startY, partying_face_bitmap, 180, 180);
// Bucle infinito para cambiar el color del texto en dos líneas
while (true) {
// Cambia el color del texto cada vez que se alcanza el intervalo
if (millis() - lastColorChange >= colorChangeInterval) {
lastColorChange = millis();
textColorIndex = (textColorIndex + 1) % numColors;
// Textos a mostrar
const char* line1 = "Gracias por todo";
const char* line2 = "te queremos mucho";
// Obtener el ancho de cada línea para centrarlas
int16_t x1, y1;
uint16_t w1, h1, w2, h2;
tft.getTextBounds(line1, 0, 0, &x1, &y1, &w1, &h1);
tft.getTextBounds(line2, 0, 0, &x1, &y1, &w2, &h2);
// Configurar el cursor y color del texto para la primera línea
tft.setCursor((tft.width() - w1) / 2, startY + 180 + 35);
tft.setTextColor(colors[textColorIndex]);
tft.setTextSize(1); // Tamaño de texto 1
tft.print(line1);
// Configurar el cursor y color del texto para la segunda línea
tft.setCursor((tft.width() - w2) / 2, startY + 180 + 35 + h1 + 5); // Espacio de 5px entre líneas
tft.setTextColor(colors[textColorIndex]);
tft.print(line2);
}
}
}
void displayPleadingFace() {
tft.fillScreen(ILI9341_BLACK); // Limpiar la pantalla
// Mover la imagen 20 píxeles hacia arriba
int16_t startX = (tft.width() - 180) / 2;
int16_t startY = ((tft.height() - 180) / 2) - 20;
// Dibujar el bitmap en RGB565
tft.drawRGBBitmap(startX, startY, pleading_face_bitmap, 180, 180);
// Bucle infinito para cambiar el color del texto
while (true) {
// Cambia el color del texto cada vez que se alcanza el intervalo
if (millis() - lastColorChange >= colorChangeInterval) {
lastColorChange = millis();
textColorIndex = (textColorIndex + 1) % numColors;
// Texto a mostrar
const char* line1 = "Po ke";
// Obtener el ancho del texto para centrarlo
int16_t x1, y1;
uint16_t w, h;
tft.setTextSize(2); // Tamaño de texto 2
tft.getTextBounds(line1, 0, 0, &x1, &y1, &w, &h);
// Configurar el cursor centrado en base al ancho del texto y dibujar
tft.setCursor((tft.width() - w) / 2, startY + 180 + 45); // Posición debajo de la imagen
tft.setTextColor(colors[textColorIndex]);
tft.print(line1);
}
}
}Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1
Loading
ili9341-cap-touch
ili9341-cap-touch