#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
// TFT pins
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 8
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
// Capacitive Touch
Adafruit_FT6206 cts = Adafruit_FT6206();
String expression = "";
const int btnW = 80;
const int btnH = 60;
const int btnLeft = 0;
const int btnTop = 120;
char buttons[4][4] = {
{'1','2','3','+'},
{'4','5','6','-'},
{'7','8','9','*'},
{'C','0','=','/'}
};
void drawButtons() {
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(3);
for(int r=0; r<4; r++){
for(int c=0; c<4; c++){
int x = btnLeft + c * btnW;
int y = btnTop + r * btnH;
tft.drawRect(x, y, btnW, btnH, ILI9341_WHITE);
tft.setCursor(x + btnW/2 - 10, y + btnH/2 - 14);
tft.print(buttons[r][c]);
}
}
}
void showExpression() {
tft.fillRect(0, 0, 240, 100, ILI9341_BLACK);
tft.setTextColor(ILI9341_GREEN);
tft.setTextSize(3);
tft.setCursor(10, 40);
tft.print(expression);
}
float calculate(String expr){
int opIndex = -1;
char op = 0;
for(int i=1; i<expr.length(); i++){
char c = expr.charAt(i);
if(c=='+'||c=='-'||c=='*'||c=='/'){ op = c; opIndex = i; break; }
}
if(opIndex == -1) return expr.toFloat();
float a = expr.substring(0, opIndex).toFloat();
float b = expr.substring(opIndex+1).toFloat();
switch(op){
case '+': return a+b;
case '-': return a-b;
case '*': return a*b;
case '/': return (b != 0 ? a/b : 0);
}
return 0;
}
void setup() {
tft.begin();
cts.begin();
tft.setRotation(0); // <<< ASL ORIENTATSIYA (VERTIKAL)
tft.fillScreen(ILI9341_BLACK);
drawButtons();
showExpression();
}
void loop() {
if (!cts.touched()) return;
TS_Point p = cts.getPoint();
// FT6206 sensor koordinatalari (VERTIKAL uchun to‘g‘ri)
int tx = p.x;
int ty = p.y;
int col = tx / btnW;
int row = (ty - btnTop) / btnH;
if (row >= 0 && row < 4 && col >= 0 && col < 4) {
char v = buttons[row][col];
if (v == 'C') expression = "";
else if (v == '=') expression = String(calculate(expression));
else expression += v;
showExpression();
delay(200);
}
}