#include <TFT_eSPI.h>
#include <SPI.h> // this is needed for display
#include <Wire.h> // this is needed for FT6206
#include <Adafruit_FT6206.h>
// The FT6206 uses hardware I2C (SCL/SDA)
Adafruit_FT6206 ctp = Adafruit_FT6206();
// The display also uses hardware SPI, plus #9 & #10
#define TFT_CS 15
#define TFT_DC 2
#define TFT_MOSI 23
#define TFT_SCLK 18
TFT_eSPI tft = TFT_eSPI(); // Invoke custom library with default width and height
#define BLACK 0x0000
#define BLUE 0x2015
#define BLUE2 0x0DDE
#define RED 0xF800
#define GREEN 0x07E0
#define GREEN2 0x43A2
#define CYAN 0x07FF
#define MAGENTA 0xF81F
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define GRAY 0x31A6
#define PINK 0xF81F
char option;
int number, number1, number2;
bool result = false;
void setup() {
Serial.begin(115200);
//Serial.println(F("Cap Touch Paint!"));
tft.begin();
if (! ctp.begin(40)) { // pass in 'sensitivity' coefficient
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
Serial.println("Capacitive touchscreen started");
tft.fillScreen(WHITE);
drawButtons();
}
void loop() {
if (ctp.touched()) {
TS_Point p = ctp.getPoint();
p.x = map(p.x, 0, 240, 240, 0);
p.y = map(p.y, 0, 320, 320, 0);
selectedButton( p.x, p.y);
showNumber();
delay(250);
}
}
void selectedButton(int x, int y) {
char key;
if (y >= 60 && y < 112) {
if (x >= 0 && x < 90) {
key = '(';
} else if (x >= 90 && x < 180) {
key = ')';
} else {
option = '/';
number1 = number;
number = 0;
tft.setCursor(210, 20);
tft.setTextColor(CYAN);
tft.setTextSize(3);
tft.print("/");
key = '/';
}
}
if (y >= 112 && y < 164) {
if (x >= 0 && x < 60) {
if (number == 0)
number = 7;
else
number = (number * 10) + 7;
key = '7';
} else if (x >= 60 && x < 120) {
if (number == 0)
number = 8;
else
number = (number * 10) + 8;
key = '8';
} else if (x >= 120 && x < 180) {
if (number == 0)
number = 9;
else
number = (number * 10) + 9;
key = '9';
} else {
option = '*';
number1 = number;
number = 0;
tft.setCursor(210, 20);
tft.setTextColor(CYAN);
tft.setTextSize(3);
tft.print("x");
key = '*';
}
}
if (y >= 164 && y < 216) {
if (x >= 0 && x < 60) {
if (number == 0)
number = 4;
else
number = (number * 10) + 4;
key = '4';
} else if (x >= 60 && x < 120) {
if (number == 0)
number = 5;
else
number = (number * 10) + 5;
key = '5';
} else if (x >= 120 && x < 180) {
if (number == 0)
number = 6;
else
number = (number * 10) + 6;
key = '6';
} else {
option = '-';
number1 = number;
number = 0;
tft.setCursor(210, 20);
tft.setTextColor(CYAN);
tft.setTextSize(3);
tft.print("-");
key = '-';
}
}
if (y >= 216 && y < 268) {
if (x >= 0 && x < 60) {
if (number == 0)
number = 1;
else
number = (number * 10) + 1;
key = '1';
} else if (x >= 60 && x < 120) {
if (number == 0)
number = 2;
else
number = (number * 10) + 2;
key = '2';
} else if (x >= 120 && x < 180) {
if (number == 0)
number = 3;
else
number = (number * 10) + 3;
key = '3';
} else {
option = '+';
number1 = number;
number = 0;
tft.setCursor(210, 20);
tft.setTextColor(CYAN);
tft.setTextSize(3);
tft.print("+");
key = '+';
}
}
if (y >= 268 && y < 320) {
if (x >= 0 && x < 60) {
number1 = number2 = 0;
number = 0;
result = false;
tft.fillRect(5, 5, 230, 55, GRAY);
key = 'C';
} else if (x >= 60 && x < 120) {
if (number == 0)
number = 0;
else
number = (number * 10) + 0;
key = '0';
} else {
number2 = number;
result = true;
calcResult();
showNumber();
key = '=';
}
}
}
void drawBox() {
tft.drawFastHLine(0, 1, 240, WHITE);
tft.drawFastVLine(0, 0, 320, WHITE);
tft.drawFastVLine(238, 0, 320, WHITE);
tft.drawFastHLine(0, 60, 240, WHITE);
tft.drawFastVLine(90, 60, 52, WHITE);
tft.drawFastVLine(180, 60, 52, WHITE);
for (int y = 112; y <= 216; y += 52) {
tft.drawFastHLine(0, y, 240, WHITE);
tft.drawFastVLine(60, y, 52, WHITE);
tft.drawFastVLine(120, y, 52, WHITE);
tft.drawFastVLine(180, y, 52, WHITE);
}
tft.drawFastHLine(0, 268, 240, WHITE);
tft.drawFastVLine(60, 268, 52, WHITE);
tft.drawFastVLine(120, 268, 52, WHITE);
tft.drawFastHLine(0, 319, 240, WHITE);
}
void drawButtons() {
// x_start, y_start, altura, largura, cor
tft.fillRect(0, 0, 240, 60, GRAY);
//tft.fillRect(0, 0, 240, 60, WHITE);
//Primeira linha
tft.fillRect(0, 60, 90, 52, BLACK);
tft.fillRect(90, 60, 90, 52, BLACK);
tft.fillRect(180, 60, 60, 52, GRAY);
for (int y = 112; y <= 216; y += 52) {
tft.fillRect(0, y, 60, 52, BLACK);
tft.fillRect(60, y, 60, 52, BLACK);
tft.fillRect(120, y, 60, 52, BLACK);
tft.fillRect(180, y, 60, 52, GRAY);
}
//Quinta linha
tft.fillRect(0, 268, 60, 52, RED);
tft.fillRect(60, 268, 60, 52, BLACK);
tft.fillRect(120, 268, 120, 52, GREEN);
drawBox();
fillButtons();
}
void fillButtons() {
tft.setCursor(38, 75);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("(");
tft.setCursor(128, 75);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print(")");
tft.setCursor(204, 77);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("/");
tft.setCursor(204, 127);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("x");
tft.setCursor(204, 180);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("-");
tft.setCursor(204, 233);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("+");
tft.setCursor(170, 280);
tft.setTextColor(WHITE);
tft.setTextSize(4);
tft.print("=");
tft.setCursor(22, 283);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("C");
tft.setCursor(83, 283);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("0");
tft.setCursor(22, 127);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("7");
tft.setCursor(22, 180);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("4");
tft.setCursor(22, 233);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("1");
tft.setCursor(83, 127);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("8");
tft.setCursor(83, 180);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("5");
tft.setCursor(83, 233);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("2");
tft.setCursor(143, 127);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("9");
tft.setCursor(143, 180);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("6");
tft.setCursor(143, 233);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("3");
}
void showNumber() {
if (result)
tft.fillRect(5, 5, 230, 55, GRAY);
tft.fillRect(5, 5, 200, 55, GRAY);
tft.setCursor(10, 20);
tft.setTextSize(4);
tft.setTextColor(WHITE);
tft.println(number);
}
void calcResult() {
switch (option) {
case '+':
number = number1 + number2;
break;
case '-':
number = number1 - number2;
break;
case '*':
number = number1 * number2;
break;
case '/':
number = number1 / number2;
break;
}
}