#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 REDBUTTON_PIN 12 // Cambia estos pines según tus conexiones
#define GREENBUTTON_PIN 13 // Cambia estos pines según tus conexiones
Adafruit_FT6206 ctp = Adafruit_FT6206();
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
#define REDBUTTON_X 10
#define REDBUTTON_Y 100
#define REDBUTTON_W 100
#define REDBUTTON_H 50
#define GREENBUTTON_X 130
#define GREENBUTTON_Y 100
#define GREENBUTTON_W 100
#define GREENBUTTON_H 50
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(REDBUTTON_PIN, INPUT_PULLUP);
pinMode(GREENBUTTON_PIN, 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() {
// 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");
// 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);
}
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 > REDBUTTON_X) && (p.x < (REDBUTTON_X + REDBUTTON_W)) &&
(p.y > REDBUTTON_Y) && (p.y < (REDBUTTON_Y + REDBUTTON_H))) {
// Botón rojo tocado
settingMode = true;
selectedField = 0; // Establecer horas inicialmente
} else if ((p.x > GREENBUTTON_X) && (p.x < (GREENBUTTON_X + GREENBUTTON_W)) &&
(p.y > GREENBUTTON_Y) && (p.y < (GREENBUTTON_Y + GREENBUTTON_H))) {
// Botón verde tocado
// Puedes agregar funcionalidad adicional aquí si es necesario
}
}
}
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;
}