#include <TFT_eSPI.h>
#include <SPI.h> // this is needed for display
#include <Wire.h> // this is needed for FT6206
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
Adafruit_FT6206 ctp = Adafruit_FT6206();
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 5
#define TFT_DC 17
#define TFT_MOSI 23
#define TFT_MISO 19
#define TFT_SCLK 18
#define TFT_SCL 22
#define TFT_SDA 21
//color
#define BLACK 0x0000 // Use black for chart background.
#define BLUE 0x001F // Define up to 7 "standard" colors for pie slices.
#define RED 0xF800 // More than 7 slices stretches use of this small display!
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define GRAY 0xc658
//page postion
int page_no =0;
int x_pos = 0; // X position
int y_pos = 50; // Y position
int width = 100; // Width of the rectangle
int height = 30; // Height of the rectangle
uint16_t fillColor = TFT_BLUE; // Fill color of the rectangle
uint16_t textColor = TFT_WHITE; // Text color
int textSize = 1; // Text size
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
//TFT_eSPI tft = TFT_eSPI();
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
tft.begin();
tft.setRotation(0);
//String text = "MENU";
//btn_1(x_pos, y_pos, width, height, text);
page_0();
if (! ctp.begin(40)) { // pass in 'sensitivity' coefficient
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
}
void loop() {
// Check if the touchscreen is pressed
if (ctp.touched()) {
// 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);
tft.fillCircle(p.x, p.y, 2, TFT_WHITE);
Serial.print("x,y = ");
Serial.print(p.x);
Serial.print(",");
Serial.println(p.y);
if(page_no ==0 && p.x >= 0 && p.x <=100 && p.y >=50 && p.y <=80 ){
clearScreen();
String text = "New Position";
btn_1(x_pos, y_pos, width, height, text);
}//test check pos
}//check touch screen scope
delay(100); // Small delay to avoid flooding the serial monitor
}
void btn_1(int x, int y, int width, int height, String txt){
// Fill the rectangle
tft.fillRect(x, y_pos, width, height, fillColor);
// Set text properties
tft.setTextColor(textColor); // Set text color
tft.setTextSize(textSize); // Set text size
tft.setCursor(x_pos+5, y_pos+5); // Set cursor position inside the rectangle
// Draw text inside the filled rectangle
tft.println(txt); // Print text
}//btn 1 scope
void btn_p1(){
// Fill the rectangle
int x_pos = 10; // X position
int y_pos = 100; // Y position
int width = 100; // Width of the rectangle
int height = 30; // Height of the rectangle
uint16_t fillColor = GRAY; // Fill color of the rectangle
uint16_t textColor = BLACK; // Text color
int textSize = 2; // Text size
String txt = "PAGE 1";
// Fill the rectangle
tft.fillRect(x_pos, y_pos, width, height, fillColor);
// Set text properties
tft.setTextColor(textColor); // Set text color
tft.setTextSize(textSize); // Set text size
tft.setCursor(x_pos, y_pos); // Set cursor position inside the rectangle
tft.println(txt);//print text
}//btn page 2 scope
void btn_p2(){
// Fill the rectangle
int x_pos = 120; // X position
int y_pos = 100; // Y position
int width = 100; // Width of the rectangle
int height = 30; // Height of the rectangle
uint16_t fillColor = GRAY; // Fill color of the rectangle
uint16_t textColor = BLACK; // Text color
int textSize = 2; // Text size
String txt = "PAGE 2";
// Fill the rectangle
tft.fillRect(x_pos, y_pos, width, height, fillColor);
// Set text properties
tft.setTextColor(textColor); // Set text color
tft.setTextSize(textSize); // Set text size
tft.setCursor(x_pos+10, y_pos+10); // Set cursor position inside the rectangle
tft.println(txt);//print text
}//btn page 2 scope
//page 0 - main menu function
void page_0(){
int x_pos = 50; // X position
int y_pos = 0; // Y position
int width = 100; // Width of the rectangle
int height = 30; // Height of the rectangle
uint16_t fillColor = GRAY; // Fill color of the rectangle
uint16_t textColor = YELLOW; // Text color
int textSize = 2; // Text size
String txt = "Main Menu";
// Set text properties
tft.setTextColor(textColor); // Set text color
tft.setTextSize(textSize); // Set text size
tft.setCursor(x_pos, y_pos); // Set cursor position inside the rectangle
tft.println(txt);//print text
//draw btn
btn_p1();
btn_p2();
}//page 0 main menu scope
//clearScreen function
void clearScreen() {
tft.fillScreen(ILI9341_BLACK); // Fill the screen with a black color
// Optionally, you can choose other colors like ILI9341_WHITE, ILI9341_RED, etc.
}
Loading
ili9341-cap-touch
ili9341-cap-touch