#include <SPI.h>
#include <XPT2046_Touchscreen.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_GFX.h>
#define TFT_CS 5
#define TFT_DC 16
#define TFT_MOSI 23
#define TFT_CLK 18
#define TFT_RST 17
#define TFT_MISO 19
#define TS_CS 21
#define ROTATION 0 // Ajustar para formato vertical
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
XPT2046_Touchscreen ts(TS_CS);
// Valores de calibración
float xCalM = -0.06;
float xCalC = 250.89;
float yCalM = -0.09;
float yCalC = 348.88;
class ScreenPoint {
public:
int16_t x;
int16_t y;
ScreenPoint() {}
ScreenPoint(int16_t xIn, int16_t yIn) : x(xIn), y(yIn) {}
};
ScreenPoint getScreenCoords(int16_t x, int16_t y) {
int16_t xCoord = round((x * xCalM) + xCalC);
int16_t yCoord = round((y * yCalM) + yCalC);
if (xCoord < 0) xCoord = 0;
if (xCoord >= tft.width()) xCoord = tft.width() - 1;
if (yCoord < 0) yCoord = 0;
if (yCoord >= tft.height()) yCoord = tft.height() - 1;
return ScreenPoint(xCoord, yCoord);
}
// Configuración del teclado
const int numRows = 4;
const int numCols = 3;
const int buttonWidth = 70;
const int buttonHeight = 60; // Aumentar la altura de las teclas
const int margin = 5;
const int keyboardHeight = numRows * buttonHeight + (numRows - 1) * margin;
const int startX = 10;
const int startY = 60; // Ajustar la posición para cubrir el espacio en blanco
String inputText = "";
void drawButton(int x, int y, String text, int color, int textColor) {
tft.fillRect(x, y, buttonWidth, buttonHeight, color);
tft.setTextColor(textColor);
tft.setCursor(x + buttonWidth / 4, y + buttonHeight / 4);
tft.setTextSize(2);
tft.print(text);
}
void drawKeyboard() {
int xPos, yPos;
String keys[numRows][numCols] = {
{"1", "2", "3"},
{"4", "5", "6"},
{"7", "8", "9"},
{"<-", "0", "✓"}
};
tft.fillScreen(ILI9341_CYAN); // Fondo de pantalla cambiado a un color bonito
// Dibujar el campo de entrada en la parte superior
tft.setTextColor(ILI9341_BLACK); // Color de texto negro para la entrada
tft.setTextSize(2); // Tamaño del texto para "Input:"
tft.setCursor(10, 20); // Ajustar posición para el texto "Input:"
tft.print("Input: ");
tft.setTextSize(3); // Tamaño del texto para la entrada
tft.setCursor(10 + 80, 20); // Ajustar la posición del texto de entrada (separar más)
tft.print(inputText);
// Dibujar el teclado en la parte inferior
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
xPos = startX + col * (buttonWidth + margin);
yPos = tft.height() - keyboardHeight + row * (buttonHeight + margin);
int buttonColor = (row == 3 && col == 0) ? ILI9341_RED : // Color especial para el botón "<-"
(row == 3 && col == 2) ? ILI9341_GREEN : // Color especial para el botón "✓"
ILI9341_ORANGE; // Color general para los demás botones
drawButton(xPos, yPos, keys[row][col], buttonColor, ILI9341_BLACK);
}
}
}
void showError(String message) {
tft.fillRect(0, 40, tft.width(), 40, ILI9341_RED); // Fondo rojo para el mensaje de error
tft.setTextColor(ILI9341_WHITE); // Color del texto blanco
tft.setTextSize(2); // Tamaño del texto
tft.setCursor(10, 50); // Ajustar la posición del mensaje de error
tft.print(message);
delay(2000); // Mostrar mensaje por 2 segundos
drawKeyboard(); // Redibujar el teclado para ocultar el mensaje de error
}
void setup() {
Serial.begin(9600);
pinMode(TS_CS, OUTPUT);
digitalWrite(TS_CS, HIGH);
pinMode(TFT_CS, OUTPUT);
digitalWrite(TFT_CS, HIGH);
tft.begin();
tft.setRotation(ROTATION);
tft.fillScreen(ILI9341_CYAN); // Fondo inicial cambiado
ts.begin();
ts.setRotation(ROTATION);
drawKeyboard();
}
void loop() {
if (ts.touched()) {
TS_Point p = ts.getPoint();
ScreenPoint s = getScreenCoords(p.x, p.y);
int x = s.x;
int y = s.y;
int xPos, yPos;
String keys[numRows][numCols] = {
{"1", "2", "3"},
{"4", "5", "6"},
{"7", "8", "9"},
{"<-", "0", "✓"}
};
for (int row = 0; row < numRows; row++) {
for (int col = 0; col < numCols; col++) {
xPos = startX + col * (buttonWidth + margin);
yPos = tft.height() - keyboardHeight + row * (buttonHeight + margin);
if (x >= xPos && x <= xPos + buttonWidth && y >= yPos && y <= yPos + buttonHeight) {
String key = keys[row][col];
if (key == "<-") {
if (inputText.length() > 0) {
inputText.remove(inputText.length() - 1);
}
} else if (key == "✓") {
if (inputText.length() > 0 && inputText.length() <= 2) {
if (inputText == "00" || inputText == "0") {
showError("Monto invalido");
inputText = ""; // Limpiar entrada para nuevo intento
} else {
Serial.println("Confirmado: " + inputText);
inputText = "";
}
} else {
showError("Ingrese 1 o 2 digitos");
}
} else {
if (inputText.length() < 2) { // Limitar a dos dígitos
inputText += key;
}
}
drawKeyboard();
delay(200); // debounce
}
}
}
}
}Loading
ili9341-cap-touch
ili9341-cap-touch