/* Author : Prateek Hegde
Algo : TFT Buttons creation
Based on the inputs from the button data is sent over UART based on the selection
*/
#include <Adafruit_GFX.h>
#include <MCUFRIEND_kbv.h>
#include <SPI.h>
#include "TouchScreen_kbv.h"
MCUFRIEND_kbv tft;
// Assign human-readable names to some common 16-bit color values:
#define BLACK 0x0000
#define BLUE 0x001F
#define RED 0xF800
#define GREEN 0x07E0
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#ifndef min
#define min(a, b) (((a) < (b)) ? (a) : (b))
#endif
static int buttonCount = 1;
static bool pageFlag = true;
int XP = 6, YP = A1, XM = A2, YM = 7; //most common configuration
TouchScreen_kbv ts(XP, YP, XM, YM, 300); //re-initialised after diagnose
TSPoint_kbv tp;
void readResistiveTouch(void);
bool ISPRESSED(void);
static int y1,z1,y2,z2,y3,z3,y4,z4,y5,z5,y6,z6;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
uint16_t ID = tft.readID(); // Read the Device Id from the SPI Interface
Serial.print("ID = 0x");
Serial.println(ID, HEX);
if (ID == 0xD3D3) ID = 0x9481; // write-only shield
// ID = 0x9329; // force ID
tft.begin(ID);
tft.setRotation(135);
/**Caliberation part/
tft.fillScreen(0xFFFF);
tft.setCursor(0,0);
tft.setTextColor(0xF120);
tft.setTextSize(3);
/*******Process Automation *************/
tft.fillScreen(0xFFFF);
tft.setCursor(0,0);
tft.setTextColor(0xF120);
tft.setTextSize(3);
tft.print(" PROCESS AUTOMATION ");
tft.setCursor(0,50);
tft.setTextSize(2);
tft.print("Process 1");
tft.fillRect(0,30,200,75,0x07E0);
tft.fillRect(0,135,200,75,0x07E0);
tft.fillRect(210,30,200,75,0x07E0);
tft.fillRect(210,135,200,75,0x07E0);
tft.fillRect(0,230,200,75,0x07E0);
tft.fillRect(210,230,200,75,0x07E0);
tft.setCursor(0,50);
tft.setTextSize(2);
tft.print("Process 1");
tft.setCursor(0,155);
tft.print("Process 3");
tft.setCursor(220,50);
tft.print("Process 2");
tft.setCursor(220,155);
tft.print("Process 4");
tft.setCursor(0,250);
tft.print("Process 5");
tft.setCursor(220,250);
tft.print("Process 6");
}
void loop() {
// put your main code here, to run repeatedly:
if(pageFlag == true)
{
pageFlag = false;
tft.fillScreen(0xFFFF);
tft.setCursor(0,0);
tft.setTextColor(0xF120);
tft.setTextSize(3);
tft.print(" PROCESS AUTOMATION");
tft.fillRect(125,80,200,65,0x07E0);
Serial.println(buttonCount);
tft.setCursor(150,110);
tft.print("Process " + String(buttonCount));
// tft.fillRect(390,280,100,50,0x07E0);
tft.setCursor(400,250);
// tft.print("--->");
//tft.fillRect(0,280,100,50,0x07E0);
tft.setCursor(0,250);
tft.print("<---");
}
if(ISPRESSED() == true)
{
readResistiveTouch();
// Serial.println("tp.x=" + String(tp.x) + ", tp.y=" + String(tp.y) + ", tp.z =" + String(tp.z));
// Serial.println("y1=" + String(y1) + ",z1=" + String(z1)+ ", y2 =" + String(y2));
// Serial.println("y1=" + String(y1) + ",z1=" + String(z1) + ", y2 =" + String(y2) + ",z2 =" + String(z2));
// Serial.println("y3=" + String(y3) + ",z3=" + String(z3) + ", y4 =" + String(y4) + ",z4 =" + String(z4));
// Serial.println("y5=" + String(y5) + ",z5=" + String(z5) + ", y6 =" + String(y6) + ",z6 =" + String(z6));
// if(tp.z > (z1-10) && tp.z < (z1+10) && tp.y > (y1-15) && (tp.y < (y1+15)))
// if( tp.z >1055 && tp.z < 1082 && tp.y > (920) && (tp.y < (960)))
if( tp.z >1055 && tp.z < 1082 && tp.y > (920))
{
// Serial.println("Pressed Process " + String(buttonCount));
Serial.println(buttonCount);
buttonCount++;
pageFlag = true;
}
else if( tp.x < 66 && tp.z >1151 && tp.z < 1192 || tp.y < 850)
{
Serial.println("Go Back");
//buttonCount = buttonCount - 1;
buttonCount++;
pageFlag = true;
}
// else if(tp.z > 1031 && tp.z < 1053 && tp.y >970 && tp.y < 990 )
// else if(tp.z > 1031 && tp.z < 1062 && tp.y > 960 )
/* else if(tp.z > 1031 && tp.z < 1052 && tp.y > 980 )
{
Serial.println("Go Next");
buttonCount++;
pageFlag = true;
}*/
if(buttonCount > 6 || buttonCount < 1)
{
buttonCount = 1;
}
}
}
void readResistiveTouch(void)
{
tp = ts.getPoint();
pinMode(YP, OUTPUT); //restore shared pins
pinMode(XM, OUTPUT);
digitalWrite(YP, HIGH); //because TFT control pins
digitalWrite(XM, HIGH);
// Serial.println("tp.x=" + String(tp.x) + ", tp.y=" + String(tp.y) + ", tp.z =" + String(tp.z));
}
bool ISPRESSED(void)
{
// .kbv this was too sensitive !!
// now touch has to be stable for 50ms
int count = 0;
bool state, oldstate;
while (count < 10) {
readResistiveTouch();
if(tp.y == 1023)
{
return false;
}
state = tp.z > 200; //ADJUST THIS VALUE TO SUIT YOUR SCREEN e.g. 20 ... 250
if (state == oldstate) count++;
else count = 0;
oldstate = state;
delay(5);
}
return oldstate;
}
This is the code I am using