#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
#define TFT_CS 8
#define TFT_DC 10
#define TFT_MOSI 11
#define TFT_SCK 13
#define TFT_RST 9
#define TFT_MISO 12
// Colors
#define BLACK 0x0000
#define WHITE 0xFFFF
#define RED 0xF800
#define GREEN 0x07E0
// initialize ILI9341 TFT library
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCK, TFT_RST, TFT_MISO);
// Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
Adafruit_FT6206 ts = Adafruit_FT6206();
void drawKeypad(){
tft.fillRect(1,79,78,58,WHITE);
tft.fillRect(81,79,78,58,WHITE);
tft.fillRect(161,79,78,58,WHITE);
tft.fillRect(1,141,78,58,WHITE);
tft.fillRect(81,141,78,58,WHITE);
tft.fillRect(161,141,78,58,WHITE);
tft.fillRect(1,202,78,58,WHITE);
tft.fillRect(81,202,78,58,WHITE);
tft.fillRect(161,202,78,58,WHITE);
tft.fillRect(1,263,78,58,WHITE);
tft.fillRect(81,263,78,58,WHITE);
tft.fillRect(161,263,78,58,WHITE);
tft.setTextColor(BLACK);
tft.setCursor(31,98);
tft.println("1");
tft.setCursor(111,98);
tft.println("2");
tft.setCursor(191,98);
tft.println("3");
tft.setCursor(31,160);
tft.println("4");
tft.setCursor(111,160);
tft.println("5");
tft.setCursor(191,160);
tft.println("6");
tft.setCursor(31,221);
tft.println("7");
tft.setCursor(111,221);
tft.println("8");
tft.setCursor(191,221);
tft.println("9");
tft.setCursor(31,282);
tft.println(".");
tft.setCursor(111,282);
tft.println("0");
tft.setCursor(191,282);
tft.println(">");
}
double keypadInput(){
Serial.println("Keypad Input Started!");
String input="";
bool flag=true;
while(flag){
tft.setCursor(4,32);
tft.setTextColor(WHITE,BLACK);
tft.println(input);
Serial.println("Waiting");
while(!ts.touched());
TS_Point p = ts.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
if(p.y>=79&&p.y<141){
if(p.x>=0&&p.x<81){input=input+"1";
Serial.println(1);}
else if(p.x>=81&&p.x<161){input=input+"2";
Serial.println(2);}
else if(p.x>=161&&p.x<=240){input=input+"3";
Serial.println(3);}
}
else if(p.y>=141&&p.y<202){
if(p.x>=0&&p.x<81){input=input+"4";
Serial.println(4);}
else if(p.x>=81&&p.x<161){input=input+"5";
Serial.println(5);}
else if(p.x>=161&&p.x<=240){input=input+"6";
Serial.println(6);}
}
else if(p.y>=202&&p.y<263){
if(p.x>=0&&p.x<81){input=input+"7";
Serial.println(7);}
else if(p.x>=81&&p.x<161){input=input+"8";
Serial.println(8);}
else if(p.x>=161&&p.x<=240){input=input+"9";
Serial.println(9);}
}
else if(p.y>=263&&p.y<=320){
if(p.x>=0&&p.x<81){input=input+".";
Serial.println(".");}
else if(p.x>=81&&p.x<161){input=input+"0";
Serial.println(0);}
else if(p.x>=161&&p.x<=240){flag=false;
Serial.println(flag);}
}
delay(100);
}
return input.toDouble();
}
void setup() {
Serial.begin(192000);
Serial.println("CAM Theory Project");
tft.begin();
// read diagnostics (optional but can help debug problems)
uint8_t x = tft.readcommand8(ILI9341_RDMODE);
Serial.print("Display Power Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDMADCTL);
Serial.print("MADCTL Mode: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDPIXFMT);
Serial.print("Pixel Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDIMGFMT);
Serial.print("Image Format: 0x"); Serial.println(x, HEX);
x = tft.readcommand8(ILI9341_RDSELFDIAG);
Serial.print("Self Diagnostic: 0x"); Serial.println(x, HEX);
if (!ts.begin(0x27)) {
Serial.println("Couldn't start touchscreen controller");
while (1);
}
}
void loop(void) {
tft.fillRect(1, 1, 239, 158, WHITE);
tft.fillRect(1, 163, 239, 158, WHITE);
tft.setTextSize(3);
tft.setTextColor(BLACK);
tft.setCursor(88,68);
tft.println("STEP");
tft.setCursor(40,229);
tft.println("CONTINUOS");
int option = 0;
while(!ts.touched());
TS_Point p = ts.getPoint();
p.y = map(p.y, 0, 320, 320, 0);
if(p.y<=160){
tft.fillScreen(BLACK);
tft.setTextColor(WHITE);
tft.setCursor(4,4);
tft.println("DEGREE: ");
drawKeypad();
double degree=keypadInput();
Serial.println(degree);
while(1);
}
}