#include <SPI.h> // this is needed for display
#include <Wire.h> // this is needed for FT6206
#include <Adafruit_FT6206.h>
#include <Adafruit_ILI9341.h>
#include "background.h"
#include <Adafruit_GFX.h>
#include "time.h"
#include <Fonts/FreeSans24pt7b.h>
#include <Fonts/FreeSans12pt7b.h>
#include <Fonts/FreeSans9pt7b.h>
#include <WiFi.h>
// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_FT6206 ctp = Adafruit_FT6206();
const int freq = 5000;
const int ledChannel = 0;
const int resolution = 8;
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 15
#define TFT_DC 2
#define TFT_MOSI 23
#define TFT_SCLK 18
int i =0;
//Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
//TFT_eSPI tft = TFT_eSPI(); // Invoke custom library with default width and height
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
int pwm = 0;
int color = ILI9341_RED;
int oldcolor, currentcolor;
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec = 0;
const int daylightOffset_sec = -3 * 3600;
void setup(void) {
Serial.begin(115200);
pinMode(27, OUTPUT);
tft.begin();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi conectado.");
// Init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
if (! ctp.begin(40)) { // pass in 'sensitivity' coefficient
Serial.println("Não foi possível iniciar o touch!");
while (1);
}
Serial.println("Touch iniciado.");
unsigned long start;
tft.drawRGBBitmap(0,0,BBitmap,B_WIDTH, B_HEIGHT);
tft.drawRoundRect(30, 0, 180, 180, 180, tft.color565(128, 128, 128));
for(int i = 179; i > 150;i--){
tft.drawRoundRect((120-(i/2)), (90-(i/2)), i, i, i, tft.color565(128, 128, 128));
}
tft.drawCircle(40, 280, 30, tft.color565(128, 128, 128));
tft.fillCircle(40, 280, 30, color);
tft.drawCircle(200, 280, 30, tft.color565(128, 128, 128));
tft.fillCircle(200, 280, 30, color);
ledcSetup(ledChannel, freq, resolution);
// attach the channel to the GPIO to be controlled
ledcAttachPin(27, ledChannel);
ledcWrite(ledChannel, pwm);
}
void loop() {
// Wait for a touch
//digitalWrite(27,HIGH);
// Retrieve a point
TS_Point p = ctp.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
if((10<p.x&&p.x < 70)&& (p.y > 240 && p.y < 310)){
color = ILI9341_GREEN;
tft.fillCircle(40, 280, 30, color);
pwm--;
delay(150);
color = ILI9341_RED;
tft.fillCircle(40, 280, 30, color);
ledcWrite(ledChannel, pwm);
if(pwm == -1){
pwm = 0;
}
}
tft.setFont(&FreeSans24pt7b);
tft.setTextColor(tft.color565(255, 255, 255));
tft.setCursor(25, 290);
tft.println("<");
if((170<p.x&&p.x < 230)&& (p.y > 240 && p.y < 310)){
color = ILI9341_GREEN;
tft.fillCircle(200, 280, 30, color);
pwm++;
delay(150);
color = ILI9341_RED;
tft.fillCircle(200, 280, 30, color);
ledcWrite(ledChannel, pwm);
if(pwm == 256){
pwm = 255;
}
}
tft.setFont(&FreeSans24pt7b);
tft.setTextColor(tft.color565(255, 255, 255));
tft.setCursor(190, 290);
tft.println(">");
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Falha ao obter o tempo.");
return;
}
//Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S");
//int mes;
String mes;
char dateDay[10];
char dateMonth[10];
char dateYear[10];
char timeHour[10];
char timeMinute[10];
char timeSecond[10];
strftime(dateDay, 10, "%d", &timeinfo);
strftime(dateMonth, 10, "%B", &timeinfo);
strftime(dateYear, 10, "%Y", &timeinfo);
strftime(timeHour, 10, "%H", &timeinfo);
strftime(timeMinute, 10, "%M", &timeinfo);
strftime(timeSecond, 10, "%S", &timeinfo);
//Serial.println(dateMonth);
String confM = dateMonth;
String confD = dateDay;
String confY = dateYear;
String confH = timeHour;
String confMi = timeMinute;
String confS = timeSecond;
if (confM == "January") {
mes = "01";
} else if (confM == "February") {
mes = "02";
} else if (confM == "March") {
mes = "03";
} else if (confM == "April") {
mes = "04";
} else if (confM == "May") {
mes = "05";
} else if (confM == "June") {
mes = "06";
} else if (confM == "July") {
mes = "07";
} else if (confM == "August") {
mes = "08";
} else if (confM == "September") {
mes = "09";
} else if (confM == "October") {
mes = "10";
} else if (confM == "November") {
mes = "11";
} else if (confM == "December") {
mes = "12";
}
String data = confD+"/"+mes+"/"+confY;
String time = confH+":"+confMi+":"+confS;
tft.setFont(&FreeSans12pt7b);
tft.setTextColor(tft.color565(255, 255, 255));
tft.setCursor(75, 75);
tft.println(time);
tft.setFont(&FreeSans9pt7b);
tft.setCursor(75, 95);
tft.println(data);
delay(1000);
tft.setFont(&FreeSans12pt7b);
tft.setTextColor(tft.color565(0, 0, 0));
tft.setCursor(75, 75);
tft.println(time);
tft.setFont(&FreeSans9pt7b);
tft.setCursor(75, 95);
tft.println(data);
// flip it around to match the screen.
Serial.println(pwm);
// Print out the remapped (rotated) coordinates
Serial.print("("); Serial.print(p.x);
Serial.print(", "); Serial.print(p.y);
Serial.println(")");
}