#include <SPI.h>
#include <Wire.h>
#include <Adafruit_FT6206.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <TimeLib.h>
#define TFT_CS 15
#define TFT_DC 2
#define SET_BUTTON_X 30
#define SET_BUTTON_Y 280
#define SET_BUTTON_R 15
#define UP_BUTTON_X 70
#define UP_BUTTON_Y 260
#define UP_BUTTON_W 40
#define UP_BUTTON_H 40
#define DOWN_BUTTON_X 130
#define DOWN_BUTTON_Y 260
#define DOWN_BUTTON_W 40
#define DOWN_BUTTON_H 40
#define SAVE_BUTTON_X 210
#define SAVE_BUTTON_Y 280
#define SAVE_BUTTON_R 15
Adafruit_FT6206 ctp = Adafruit_FT6206();
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
boolean settingMode = false;
int selectedField = 0; // 0: Horas, 1: Minutos, 2: Segundos
void setup() {
Serial.begin(115200);
tft.begin();
ctp.begin(40); // Coeficiente de sensibilidad
tft.setRotation(4); // Gira la pantalla en 0 grados (horizontal)
pinMode(SET_BUTTON_X, INPUT_PULLUP);
pinMode(SAVE_BUTTON_X, INPUT_PULLUP);
pinMode(UP_BUTTON_X, INPUT_PULLUP);
pinMode(DOWN_BUTTON_X, INPUT_PULLUP);
if (!getTimeFromRTC()) {
Serial.println("No se pudo obtener la hora del RTC.");
// Puedes establecer una hora predeterminada aquí
}
// Mostrar reloj inicialmente
showClock();
}
void loop() {
if (settingMode) {
// Código para manejar el ajuste de la hora
// Esto se implementará más tarde
} else {
// Actualizar el reloj
showClock();
delay(1000); // Esperar un segundo
}
// Comprobar entrada táctil
checkTouch();
}
void showClock() {
// Actualizar el tiempo actual
int currentHour = hour();
int currentMinute = minute();
int currentSecond = second();
char currentTime[9];
sprintf(currentTime, "%02d:%02d:%02d", currentHour, currentMinute, currentSecond);
// Borrar el área donde se muestra el tiempo
tft.fillRect(50, 80, 160, 40, ILI9341_BLACK);
// Escribir el nuevo tiempo
tft.setCursor(50, 80);
tft.setTextSize(3);
tft.setTextColor(ILI9341_WHITE);
tft.print(currentTime);
// Mostrar el texto "Programador De Riego"
tft.setTextSize(2);
tft.setTextColor(ILI9341_WHITE);
tft.setCursor(0, 30); // Ajusta la posición según sea necesario
tft.print("Programador De Riego");
// Dibujar botones
drawButtons();
}
void checkTouch() {
if (ctp.touched()) {
TS_Point p = ctp.getPoint();
// Comprobar si el toque cae dentro de los límites de los botones
if ((p.x > SET_BUTTON_X - SET_BUTTON_R) && (p.x < (SET_BUTTON_X + SET_BUTTON_R)) &&
(p.y > SET_BUTTON_Y - SET_BUTTON_R) && (p.y < (SET_BUTTON_Y + SET_BUTTON_R))) {
// Botón SET tocado
settingMode = true;
selectedField = 0; // Establecer horas inicialmente
Serial.println("Botón SET tocado");
} else if ((p.x > SAVE_BUTTON_X - SAVE_BUTTON_R) && (p.x < (SAVE_BUTTON_X + SAVE_BUTTON_R)) &&
(p.y > SAVE_BUTTON_Y - SAVE_BUTTON_R) && (p.y < (SAVE_BUTTON_Y + SAVE_BUTTON_R))) {
// Botón GUARDAR tocado
// Implementar funcionalidad de guardar aquí
Serial.println("Botón GUARDAR tocado");
} else if ((p.x > UP_BUTTON_X) && (p.x < (UP_BUTTON_X + UP_BUTTON_W)) &&
(p.y > UP_BUTTON_Y) && (p.y < (UP_BUTTON_Y + UP_BUTTON_H))) {
// Botón UP tocado
// Implementar funcionalidad de aumento aquí
Serial.println("Botón UP tocado");
} else if ((p.x > DOWN_BUTTON_X) && (p.x < (DOWN_BUTTON_X + DOWN_BUTTON_W)) &&
(p.y > DOWN_BUTTON_Y) && (p.y < (DOWN_BUTTON_Y + DOWN_BUTTON_H))) {
// Botón DOWN tocado
// Implementar funcionalidad de disminución aquí
Serial.println("Botón DOWN tocado");
}
}
}
boolean getTimeFromRTC() {
// Implementar esta función para obtener la hora del RTC
// Devuelve true si es exitoso, false en caso contrario
// Es posible que necesites modificar esto según tu módulo RTC específico
return false;
}
void drawButtons() {
// Dibujar botón SET (redondo)
tft.fillRoundRect(SET_BUTTON_X - SET_BUTTON_R, SET_BUTTON_Y - SET_BUTTON_R, 2 * SET_BUTTON_R, 2 * SET_BUTTON_R, 5, ILI9341_GREEN);
// Dibujar botón GUARDAR (redondo)
tft.fillRoundRect(SAVE_BUTTON_X - SAVE_BUTTON_R, SAVE_BUTTON_Y - SAVE_BUTTON_R, 2 * SAVE_BUTTON_R, 2 * SAVE_BUTTON_R, 5, ILI9341_RED);
// Dibujar botón UP (flecha hacia arriba)
int upButtonXCenter = UP_BUTTON_X + UP_BUTTON_W / 2;
int upButtonYCenter = UP_BUTTON_Y + UP_BUTTON_H / 2;
tft.fillTriangle(upButtonXCenter, UP_BUTTON_Y, UP_BUTTON_X, upButtonYCenter + UP_BUTTON_H / 2, UP_BUTTON_X + UP_BUTTON_W, upButtonYCenter + UP_BUTTON_H / 2, ILI9341_GREEN);
// Dibujar botón DOWN (flecha hacia abajo)
int downButtonXCenter = DOWN_BUTTON_X + DOWN_BUTTON_W / 2;
int downButtonYCenter = DOWN_BUTTON_Y + DOWN_BUTTON_H / 2;
tft.fillTriangle(downButtonXCenter, DOWN_BUTTON_Y + DOWN_BUTTON_H, DOWN_BUTTON_X, downButtonYCenter - DOWN_BUTTON_H / 2, DOWN_BUTTON_X + DOWN_BUTTON_W, downButtonYCenter - DOWN_BUTTON_H / 2, ILI9341_GREEN);
}