#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include "RTClib.h"
RTC_DS1307 rtc;
//#define BTN_PIN 5
#define TFT_DC 2
#define TFT_CS 15
#define BUT1 13
#define BUT2 12
#define BUT3 14
#define BUT4 27
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
unsigned long aggiungi = 1671228083; //https://www.epochconverter.com/
//unsigned long aggiungi = 604860;
bool UnixTime = true, mostraLista = true;
int hour, minute, second, giorni, giornoset, minPrec, schermata, modifica, sceltaLinea;
String nomeGiornoShort[7] = {"Dom", "Lun", "Mar", "Mer", "Gio", "Ven", "Sab"};
String nomeGiorno[7] = {"Domenica", "Lunedi", "Martedi", "Mercoledi", "Giovedi", "Venerdi", "Sabato"};
String records[3][5] = {{"X", "Lun", "12:00", "12:30", "USC1"}, {"X", "Mar", "12:30", "12:33", "USC2"}, {" ", "Mer", "13:30", "14:33", "USC3"}};
int recordsLungh = 3;
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
tft.begin();
tft.setRotation(1);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.print("HELLO");
delay(1000);
tft.fillScreen(ILI9341_BLACK);
orologio();
visualPulsanti(schermata);
pinMode(BUT1, INPUT);
pinMode(BUT2, INPUT);
pinMode(BUT3, INPUT);
pinMode(BUT4, INPUT);
if (! rtc.begin()) {
Serial.println("Couldn't find RTC");
Serial.flush();
abort();
} else {
DateTime now = rtc.now();
Serial.println(now.unixtime());
aggiungi = now.unixtime(); //AGGIORNA ORARIO CON RTC_DS1307
}
}
void loop() {
switch (schermata) {
case 0:
orologio();
break;
case 1:
settaggio();
break;
case 2:
timers();
break;
default:
orologio();
break;
}
delay(10); // this speeds up the simulation
}
void timers() {
if (mostraLista) {
tft.fillRect(0, 0, 305, 200, ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(0, 0);
tft.setTextColor(ILI9341_WHITE);
tft.println(" ESCI");
for (int i; i < recordsLungh; i++) {
tft.println(" " + records[i][0] + " " + records[i][1] + " " + records[i][2] + " - " + records[i][3] + " " + records[i][4]);
}
/*
tft.println(" "+records[0][0]+" "+records[0][1]+" "+records[0][2]+" - "+records[0][3]+" "+records[0][4]);
tft.println(" "+records[1][0]+" "+records[1][1]+" "+records[1][2]+" - "+records[1][3]+" "+records[1][4]);
*/
mostraLista = false;
switch (sceltaLinea) {
case 0:
tft.drawRect(4, 0, 300, 14, ILI9341_RED);
break;
case 1:
tft.drawRect(4, 16, 300, 14, ILI9341_RED);
break;
case 2:
tft.drawRect(4, 32, 300, 14, ILI9341_RED);
break;
case 3:
tft.drawRect(4, 48, 300, 14, ILI9341_RED);
break;
}
}
if (digitalRead(BUT1) == HIGH) {
Serial.println("PREMUTO Su");
sceltaLinea--;
mostraLista = true;
if (sceltaLinea < 0)sceltaLinea = 0;
delay(200);
}
if (digitalRead(BUT2) == HIGH) {
Serial.println("PREMUTO Ent");
if (sceltaLinea == 0) {
mostraLista = true;
tft.fillScreen(ILI9341_BLACK);
schermata = 0;
visualPulsanti(schermata);
calcolaOrario();
visualOrario();
} else {
tft.fillScreen(ILI9341_BLACK);
visualOrario();
visualPulsanti(3);
}
delay(200);
}
if (digitalRead(BUT3) == HIGH) {
Serial.println("PREMUTO Giu");
sceltaLinea++;
if (sceltaLinea > recordsLungh) sceltaLinea = recordsLungh;
mostraLista = true;
delay(200);
}
}
// SETTAGGIO
void settaggio() {
tft.setTextSize(2);
tft.setCursor(90, 3);
tft.setTextColor(ILI9341_RED);
tft.print("Modifica");
switch (modifica) {
case 0:
tft.drawRect(0, 41, 300, 27, ILI9341_RED);
break;
case 1:
tft.drawRect(0, 41, 300, 27, ILI9341_BLACK);
tft.drawRect(55, 99, 82, 55, ILI9341_RED);
break;
case 2:
tft.drawRect(55, 99, 82, 55, ILI9341_BLACK);
tft.drawRect(178, 99, 82, 55, ILI9341_RED);
break;
}
if (digitalRead(BUT1) == HIGH) {
Serial.println("PREMUTO +");
switch (modifica) {
case 0:
aggiungi += 86400;
calcolaOrario();
visualOrario();
break;
case 1:
aggiungi += 3600;
calcolaOrario();
visualOrario();
break;
case 2:
aggiungi += 60;
calcolaOrario();
visualOrario();
break;
}
delay(200);
}
if (digitalRead(BUT2) == HIGH) {
Serial.println("PREMUTO OK");
modifica++;
if (modifica > 2) {
modifica = 0;
tft.fillScreen(ILI9341_BLACK);
schermata = 0;
visualPulsanti(schermata);
calcolaOrario();
visualOrario();
}
delay(200);
}
if (digitalRead(BUT3) == HIGH) {
Serial.println("PREMUTO -");
switch (modifica) {
case 0:
aggiungi -= 86400;
calcolaOrario();
visualOrario();
break;
case 1:
aggiungi -= 3600;
calcolaOrario();
visualOrario();
break;
case 2:
aggiungi -= 60;
calcolaOrario();
visualOrario();
break;
}
delay(200);
}
}
void orologio()
{
calcolaOrario();
if (minute != minPrec) {
visualOrario();
minPrec = minute;
Serial.println();
}
if (digitalRead(BUT1) == HIGH) {
Serial.println("PREMUTO Set");
schermata = 1;
tft.fillScreen(ILI9341_BLACK);
visualPulsanti(schermata);
visualOrario();
delay(200);
}
if (digitalRead(BUT2) == HIGH) {
Serial.println("PREMUTO 2");
delay(200);
}
if (digitalRead(BUT3) == HIGH) {
Serial.println("PREMUTO Tim");
schermata = 2;
tft.fillScreen(ILI9341_BLACK);
visualPulsanti(schermata);
delay(200);
}
}
void calcolaOrario() {
unsigned long mTime = secondi();
hour = (mTime / 3600) % 24;
//if (UnixTime) hour += 1; // Aggiunge fuso orario
minute = (mTime / 60) % 60;
// second = mTime % 60;
giorni = mTime / 86400;
giornoset = giorni % 7;
if (UnixTime) giornoset = (giorni + 4) % 7;
}
unsigned long secondi() {
unsigned long sec = millis() / 1000 + aggiungi;
return sec;
}
void visualOrario() {
tft.setTextColor(ILI9341_DARKGREY);
tft.setCursor(75, 45);
tft.fillRect(0, 45, 300, 22, ILI9341_BLACK);
tft.setTextSize(3);
//tft.fillScreen(ILI9341_BLACK);
tft.fillRect(55, 99, 80, 55, ILI9341_BLACK);
tft.fillRect(178, 99, 80, 55, ILI9341_BLACK);
tft.print(nomeGiorno[giornoset]);
tft.setTextColor(ILI9341_WHITE);
//tft.println(" - ");
tft.setTextSize(7);
tft.setCursor(55, 100);
if (hour < 10) tft.print("0");
tft.print(hour);
tft.print(":");
if (minute < 10) tft.print("0");
tft.print(minute);
/*tft.print(":");
if (second < 10) tft.print("0");
tft.println(second);
*/
}
void visualPulsanti(int schermata) {
tft.setTextColor(ILI9341_WHITE);
switch (schermata) {
case 0:
tft.fillRoundRect(0, 200, 70, 45, 3, ILI9341_DARKGREY);
tft.setTextSize(3);
tft.setCursor(10, 210);
tft.print("Set");
tft.fillRoundRect(230, 200, 70, 45, 3, ILI9341_DARKGREY);
tft.setCursor(237, 210);
tft.print("Tim");
break;
case 1:
tft.fillRoundRect(0, 200, 70, 45, 3, ILI9341_DARKGREY);
tft.setTextSize(3);
tft.setCursor(25, 210);
tft.print("+");
tft.fillRoundRect(115, 200, 70, 45, 3, ILI9341_DARKGREY);
tft.setCursor(135, 210);
tft.print("OK");
tft.fillRoundRect(230, 200, 70, 45, 3, ILI9341_DARKGREY);
tft.setCursor(257, 210);
tft.print("-");
break;
case 2:
tft.fillRoundRect(0, 200, 70, 45, 3, ILI9341_DARKGREY);
tft.setTextSize(3);
tft.setCursor(20, 210);
tft.print("Su");
tft.fillRoundRect(115, 200, 70, 45, 3, ILI9341_DARKGREY);
tft.setCursor(125, 210);
tft.print("Ent");
tft.fillRoundRect(230, 200, 70, 45, 3, ILI9341_DARKGREY);
tft.setCursor(240, 210);
tft.print("Giu");
break;
case 3:
tft.fillRoundRect(0, 200, 70, 45, 3, ILI9341_DARKGREY);
tft.setTextSize(3);
tft.setCursor(20, 210);
tft.print("<");
tft.fillRoundRect(115, 200, 70, 45, 3, ILI9341_DARKGREY);
tft.setCursor(125, 210);
tft.print("Ent");
tft.fillRoundRect(230, 200, 70, 45, 3, ILI9341_DARKGREY);
tft.setCursor(240, 210);
tft.print(">");
break;
}
}