#include <TFT_eSPI.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_FT6206.h>
#define TFT_CS 15
#define TFT_DC 2
#define TFT_RST 4
#define TFT_MOSI 23
#define TFT_SCLK 18
#define TFT_MISO 19
#define TFT_LED 3.3V
TFT_eSPI tft = TFT_eSPI();
Adafruit_FT6206 ctp = Adafruit_FT6206();
#define KEY_ROWS 4
#define KEY_COLS 10
#define KEY_WIDTH 30
#define KEY_HEIGHT 40
#define KEY_SPACING 5
#define START_X 10
#define START_Y 100
const char* keyMap[KEY_ROWS][KEY_COLS] = {
{"Q", "W", "E", "R", "T", "Y", "U", "I", "O", "P"},
{"A", "S", "D", "F", "G", "H", "J", "K", "L", ";"},
{"Z", "X", "C", "V", "B", "N", "M", ",", ".", "/"},
{" ", " ", " ", " ", " ", " ", " ", " ", " ", " "}
};
String inputText = "";
void setup(void) {
Serial.begin(115200);
tft.begin();
tft.setRotation(1);
if (!ctp.begin(40)) {
Serial.println("Couldn't start FT6206 touchscreen controller");
while (1);
}
Serial.println("Capacitive touchscreen started");
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE, TFT_BLACK);
tft.setTextSize(2);
for (int row = 0; row < KEY_ROWS; row++) {
for (int col = 0; col < KEY_COLS; col++) {
int x = START_X + col * (KEY_WIDTH + KEY_SPACING);
int y = START_Y + row * (KEY_HEIGHT + KEY_SPACING);
tft.fillRoundRect(x, y, KEY_WIDTH, KEY_HEIGHT, 5, TFT_DARKGREY);
tft.drawRoundRect(x, y, KEY_WIDTH, KEY_HEIGHT, 5, TFT_WHITE);
tft.drawString(keyMap[row][col], x + KEY_WIDTH / 4, y + KEY_HEIGHT / 4, 2);
}
}
}
char getKey(int x, int y) {
for (int row = 0; row < KEY_ROWS; row++) {
for (int col = 0; col < KEY_COLS; col++) {
int keyX = START_X + col * (KEY_WIDTH + KEY_SPACING);
int keyY = START_Y + row * (KEY_HEIGHT + KEY_SPACING);
if (x > keyX && x < keyX + KEY_WIDTH && y > keyY && y < keyY + KEY_HEIGHT) {
return keyMap[row][col][0];
}
}
}
return 0;
}
void showText(String text) {
tft.fillRect(0, 0, tft.width(), START_Y - 10, TFT_BLACK);
tft.setTextColor(TFT_GREEN, TFT_BLACK);
tft.setTextSize(3);
tft.drawString(text, 10, 10, 2);
}
void loop() {
if (!ctp.touched()) {
return;
}
TS_Point p = ctp.getPoint();
int x = map(p.x, 0, 240, 0, tft.width());
int y = map(p.y, 0, 320, 0, tft.height());
char key = getKey(x, y);
if (key != 0) {
Serial.print("Key Pressed: ");
Serial.println(key);
inputText += key;
showText(inputText);
}
}