//12 June 2018. Edited for use with DUE and UNO screen 2.8"
//multi wire TFT
#include <MCUFRIEND_kbv.h>
#include <Adafruit_GFX.h>
#include <TouchScreen.h>
MCUFRIEND_kbv tft;
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
#define TS_MINX 167 //
#define TS_MINY 228 //
#define TS_MAXX 880 //
#define TS_MAXY 860 //
#define YP A2 // must be an analog pin, use "An" notation!
#define XM A3 // must be an analog pin, use "An" notation!
#define YM 8 // can be a digital pin
#define XP 9 // can be a digital pin
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define LIGHTGREY 0xDEDB
#define GREY 0xCE79
#define MINPRESSURE 10
#define MAXPRESSURE 1000
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 365); //.......p0
int X, Y, Z;
boolean buttonEnabled = true;
char currentPage;
void setup() {
Serial.begin(9600);
Serial.print("Starting...");
//....
tft.reset();
uint16_t identifier = tft.readID();
tft.begin(identifier);
tft.setRotation(2);
// tft.fillScreen(LIGHTGREY);
{
HomeScreen();
currentPage = '0';
}
}
void loop() //...............p1
{
//HomeScreen
if (currentPage == '0') {
TSPoint p = ts.getPoint(); //Get touch point
if (p.z > ts.pressureThreshhold) {
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\n");
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(13, LOW);
p.y = map(p.y, TS_MINY, TS_MAXY, 0, 240);
p.x = map(p.x, TS_MINX, TS_MAXX, 0, 320);
X = tft.height() - map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
Y = map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
Z = p.z;
{
//contact us co-ords
if (p.x > 50 && p.x < 95 && p.y > 5 && p.y < 180 && buttonEnabled)
{
buttonEnabled = false; //Disable button
//This is important, because the libraries are sharing pins
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
currentPage = '1';
tft.fillScreen(LIGHTGREY);
contactus();
}
}
//.....................................p2.............................
{
//about us co-ords
if (p.x > 120 && p.x < 165 && p.y > 5 && p.y < 180 && buttonEnabled)
{
buttonEnabled = false; //Disable button
//This is important, because the libraries are sharing pins
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
currentPage = '2';
tft.fillScreen(LIGHTGREY);
aboutus();
}
//calculator co-ords
if (p.x > 195 && p.x < 225 && p.y > 5 && p.y < 180 && buttonEnabled)
{
buttonEnabled = false; //Disable button
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
currentPage = '3';
tft.fillScreen(LIGHTGREY);
calculator();
}
}
delay(10); // .....................p3
}
} //end current page 0 or HomeScreen
// contactus screen ===========================================
{
if (currentPage == '1') {
Serial.print("correct ...........location but");
// menu button co-ords
TSPoint p = ts.getPoint(); //Get touch point
if (p.z > ts.pressureThreshhold) {
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\n");
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(13, LOW);
p.y = map(p.y, TS_MINY, TS_MAXY, 0, 240);
p.x = map(p.x, TS_MINX, TS_MAXX, 0, 320);
X = tft.height() - map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
Y = map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
Z = p.z;
if (p.x > 10 && p.x < 80 && p.y > 180 && p.y < 240 && buttonEnabled) { //menu button XXL co ords
buttonEnabled = false; //Disable button
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
Serial.print("button is .......jipee............working");
//goto home screen
HomeScreen();
currentPage = '0';
}
}
}
} //end contactus screen .......p4 ==============
} //loop end
void HomeScreen()
{
tft.fillScreen(LIGHTGREY);
// print "contact us" text
tft.fillRect(60, 60, 200, 40, BLUE);
tft.drawRect(60, 60, 200, 40, RED);
tft.setCursor(100, 70);
tft.setTextColor(GREEN);
tft.setTextSize(2); //...........
tft.print("Contact Us");
//print "about us" text
tft.fillRect(60, 120, 200, 40, BLUE);
tft.drawRect(60, 120, 200, 40, CYAN);
tft.setCursor(110, 130);
tft.setTextColor(CYAN);
tft.setTextSize(2);
tft.print("About Us");
//Print "calculator" text
tft.fillRect(60, 180, 200, 40, BLUE);
tft.drawRect(60, 180, 200, 40, CYAN);
tft.setCursor(100, 190);
tft.setTextColor(CYAN);
tft.setTextSize(2);
tft.print("Calculator"); //........p5
}
void contactus()
{
tft.fillScreen(LIGHTGREY);
//display "contact us" contents
tft.setCursor(30, 50);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("contactus screen ..............");
//create " menu" button ...... ..........p6
tft.fillRect(10, 10, 90, 30, BLUE);
tft.drawRect(10, 10, 90, 30, WHITE);
tft.setCursor(20, 15);
tft.setTextColor(CYAN);
tft.setTextSize(2);
tft.print("Menu >");
}
void aboutus()
{
tft.fillScreen(LIGHTGREY);
// display " about us " contents
tft.setCursor(50, 50);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("about us");
//create " menu" button
tft.fillRect(10, 10, 90, 30, BLUE);
tft.drawRect(10, 10, 90, 30, CYAN);
tft.setCursor(20, 15);
tft.setTextColor(CYAN);
tft.setTextSize(2);
tft.print("Menu >");
}
void calculator()
{
tft.print("calculator screen");
tft.fillScreen(LIGHTGREY);
//display "contact us" contents
tft.setCursor(30, 50);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("calculator screen");
//create " menu" button ........... .....p6
tft.fillRect(10, 10, 90, 30, BLUE);
tft.drawRect(10, 10, 90, 30, WHITE);
tft.setCursor(20, 15);
tft.setTextColor(CYAN);
tft.setTextSize(2);
tft.print("Menu >");
/*
// ................. menu co-ords ...............
TSPoint p = ts.getPoint(); //Get touch point
if (p.z > ts.pressureThreshhold) {
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\n");
digitalWrite(13, HIGH);
TSPoint p = ts.getPoint();
digitalWrite(13, LOW);
p.y = map(p.y, TS_MINY, TS_MAXY, 0, 240);
p.x = map(p.x, TS_MINX, TS_MAXX, 0, 320);
X = tft.height() - map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
Y = map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
Z = p.z;
if (p.x > 10 && p.x < 30 && p.y > 200 && p.y < 240 && buttonEnabled) // The user has pressed inside the blue rectangle
{
buttonEnabled = false; //Disable button
//This is important, because the libraries are sharing pins
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//Erase the screen
tft.fillScreen(BLACK);
HomeScreen();
}*/
}
Members
When the 'contact us' button is pressed it goes to a sub menu with a 'menu' button at the top. When the 'menu' button is pressed it should go back to the HomeScreen.
I think I've got the '{ & }' wrong. Not quite sure.
Any help would be appreciated to get this working.
Thanks in advance