#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <Adafruit_FT6206.h>
// TFT Pins
#define TFT_CS 5
#define TFT_DC 2
#define TFT_RST 4
// I2C Touch
#define I2C_SDA 21
#define I2C_SCL 22
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC, TFT_RST);
Adafruit_FT6206 ctp = Adafruit_FT6206();
String inputValue = "";
// Keypad layout
const char* keys[4][3] = {
{"1","2","3"},
{"4","5","6"},
{"7","8","9"},
{"C","0","OK"}
};
int keyWidth = 80;
int keyHeight = 60;
int startX = 40;
int startY = 80;
void drawKeypad() {
tft.fillScreen(ILI9341_BLACK);
// Display input field
tft.drawRect(20, 20, 280, 40, ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(30, 30);
tft.print(inputValue);
// Draw buttons
for(int row=0; row<4; row++){
for(int col=0; col<3; col++){
int x = startX + col * (keyWidth + 10);
int y = startY + row * (keyHeight + 10);
tft.fillRect(x, y, keyWidth, keyHeight, ILI9341_BLUE);
tft.drawRect(x, y, keyWidth, keyHeight, ILI9341_WHITE);
tft.setTextColor(ILI9341_WHITE);
tft.setTextSize(2);
tft.setCursor(x + keyWidth/2 - 10, y + keyHeight/2 - 10);
tft.print(keys[row][col]);
}
}
}
void setup() {
Serial.begin(115200);
Wire.begin(I2C_SDA, I2C_SCL);
tft.begin();
tft.setRotation(0); // Portrait
if (!ctp.begin(40)) {
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
drawKeypad();
}
void loop() {
if (!ctp.touched()) return;
TS_Point p = ctp.getPoint();
// Map touch coordinates (may need adjustment)
int x = map(p.y, 0, 240, 0, 320);
int y = map(p.x, 0, 320, 0, 240);
for(int row=0; row<4; row++){
for(int col=0; col<3; col++){
int btnX = startX + col * (keyWidth + 10);
int btnY = startY + row * (keyHeight + 10);
if(x > btnX && x < (btnX + keyWidth) &&
y > btnY && y < (btnY + keyHeight)) {
String key = keys[row][col];
if(key == "C"){
inputValue = "";
}
else if(key == "OK"){
Serial.println("Entered: " + inputValue);
}
else{
inputValue += key;
}
drawKeypad();
delay(300); // debounce
}
}
}
}
Loading
ili9341-cap-touch
ili9341-cap-touch