#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
#include <TouchScreen.h>
#define MINPRESSURE 10
#define MAXPRESSURE 40000
const int XP = 27, XM = 32, YP = 33, YM = 14; //ID=0x9341
const int TS_LEFT = -2600, TS_RT = 630, TS_TOP = 700, TS_BOT = -2460;
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
Adafruit_GFX_Button on_btn;
int pixel_x, pixel_y; //Touch_getXY() updates global vars
bool Touch_getXY(void)
{
pixel_x=0;
pixel_y=0;
TSPoint p = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
p.z=abs(p.z);
if(!(p.z)==0)
{
Serial.println(p.z);
Serial.print("pixel_x:");
Serial.print(p.x);
Serial.print(", pixel_y:");
Serial.println(p.y);
}
bool pressed = (p.z>10);
if (pressed)
{
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
pixel_x = map(p.y, TS_LEFT, TS_RT, 0, tft.width()); //.kbv makes sense to me
pixel_y = map(p.x, TS_TOP, TS_BOT, 0, tft.height());
Serial.print("x:");
Serial.print(pixel_x);
Serial.print(", y:");
Serial.println(pixel_y);
Serial.println("Tocuh Working");
p.z=0;
}
return pressed;
}
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
int page=1,Selection =1;
bool down;
unsigned long old =0;
unsigned long current =0;
long interval=0;
void setup(void)
{
Serial.begin(115200);
delay(1000);
uint16_t ID = tft.readID();
Serial.print("TFT ID = 0x");
Serial.println(ID, HEX);
Serial.println("Calibrate for your Touch Panel");
if (ID == 0xD3D3) ID = 0x9486; // write-only shield
tft.begin(ID);
tft.setRotation(1); //PORTRAIT
tft.setTextSize(3);
tft.fillScreen(RED);
HomePage();
Selection_1();
}
void loop(void)
{
down = Touch_getXY();
if (down)
{
Selection =Selection+1;
if(Selection>5)
Selection=1;
}
else
return;
HomePage();
if(Selection==1)
Selection_1();
else if(Selection==2)
Selection_2();
else if(Selection==3)
Selection_3();
else if(Selection==4)
Selection_4();
else if(Selection==5)
Selection_5();
delay(1000);
}
void HomePage()
{
tft.setTextColor(BLACK);
tft.setTextSize(3);
tft.fillRect(0,0,320,40,GREEN);
tft.setCursor(10,10);
tft.print("TeachMe Something");
tft.fillRect(0,40,320,200,MAGENTA);
tft.fillRect(280,40,40,200,BLUE);
tft.setTextColor(WHITE);
tft.setCursor(10,50);
tft.print("1.Temperature");
tft.setCursor(10,90);
tft.print("2.Humidity");
tft.setCursor(10,130);
tft.print("3.Moisture");
tft.setCursor(10,130);
tft.print("3.Moisture");
tft.setCursor(10,170);
tft.print("4.Light");
tft.setCursor(10,210);
tft.print("5.Air Quality");
}
void Selection_1()
{
tft.fillRect(0,40,280,40,RED);
tft.setCursor(10,50);
tft.print("1.Temperature");
}
void Selection_2()
{
tft.fillRect(0,80,280,40,RED);
tft.setCursor(10,90);
tft.print("2.Humidity");
}
void Selection_3()
{
tft.fillRect(0,120,280,40,RED);
tft.setCursor(10,130);
tft.print("3.Moisture");
}
void Selection_4()
{
tft.fillRect(0,160,280,40,RED);
tft.setCursor(10,170);
tft.print("4.Light");
}
void Selection_5()
{
tft.fillRect(0,200,280,40,RED);
tft.setCursor(10,210);
tft.print("5.Air Quality");
}
Loading
esp32-s3-devkitc-1
esp32-s3-devkitc-1