#include <Adafruit_ILI9341.h>
#include <Adafruit_GFX.h>
#include <Adafruit_FT6206.h>
#include "Fonts/FreeMono9pt7b.h"
#include "Fonts/FreeSerif9pt7b.h"
Adafruit_ILI9341 tft(10,9);
Adafruit_FT6206 ts;
int Touch_x() {
TS_Point point = ts.getPoint();
return 240-point.x;
}
int Touch_y() {
TS_Point point = ts.getPoint();
return 320-point.y;
}
char *wd[] {
"1","2","3","4","5","6","7","8","9","0",
"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","","",
"",","," "," "," "," "," "," ","."
};
char *WD[] {
"1","2","3","4","5","6","7","8","9","0",
"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","","",
"",","," "," "," "," "," "," ","."
};
char *symbol[] {
"1","2","3","4","5","6","7","8","9","0",
"%","/","|","=","[","]","<",">","{","}",
"@","#","$","_","&","-","+","(",")","",
"*","","'",":",";","!","?","","",""
};
uint16_t textColor[] {
0x39E7, 0xF800, 0xFC00, 0xFFE0, 0x07E0, 0x07FF, 0x001F, 0xF81F,
0x0000, 0x7800, 0x79E0, 0x7BE0, 0x03E0, 0x03EF, 0x000F, 0x780F
};
void drawKey(int x, int y, int length, int width, uint16_t *color) {
tft.drawRoundRect(x,y, length,width, 3, color);
tft.drawLine(x+3,y+width, x+length-2,y+width, color);
tft.drawLine(x+length,y+3, x+length,y+width-2, color);
tft.drawPixel(x+1, y+width-1, color);
tft.drawPixel(x+length-1, y+width-1, color);
tft.drawPixel(x+length-2, y+width-1, color);
tft.drawPixel(x+length-1, y+width-2, color);
tft.drawPixel(x+length-1, y+1, color);
}
void drawKeyPressed(int x, int y, int length, int width, uint16_t *color) {
tft.drawRoundRect(x,y, length,width, 3, color);
tft.drawLine(x+3,y+width, x+length-2,y+width, 0xFFFF);
tft.drawLine(x+length,y+3, x+length,y+width-2, 0xFFFF);
tft.drawPixel(x+1, y+width-1, 0xFFFF);
tft.drawPixel(x+length-1, y+width-1, 0xFFFF);
tft.drawPixel(x+length-2, y+width-1, 0xFFFF);
tft.drawPixel(x+length-1, y+width-2, 0xFFFF);
tft.drawPixel(x+length-1, y+1, 0xFFFF);
}
void drawShiftLogo(int x, int y, uint16_t *color) {
tft.drawLine(x,y+6, x+6,y, color);
tft.drawLine(x+6,y, x+12,y+6, color);
tft.drawLine(x,y+6, x+3,y+6, color);
tft.drawLine(x+9,y+6, x+12,y+6, color);
tft.drawLine(x+3,y+6, x+3,y+12, color);
tft.drawLine(x+9,y+6, x+9,y+12, color);
tft.drawLine(x+3,y+12, x+9,y+12, color);
}
void fillShiftLogo(int x, int y, uint16_t *color) {
for(int i=0; i<7; i++) {
tft.drawLine(x+6-i,y+i, x+6+i,y+i, color);
tft.drawLine(x+3,y+6+i, x+9,y+6+i, color);
}
}
void drawBackspace(int x, int y, uint16_t *color) {
tft.drawLine(x,y+6, x+4,y, color);
tft.drawLine(x+4,y, x+14,y, color);
tft.drawLine(x,y+6, x+4,y+12, color);
tft.drawLine(x+4,y+12, x+14,y+12, color);
tft.drawLine(x+14,y, x+14,y+12, color);
tft.drawLine(x+6,y+2, x+12,y+10, color);
tft.drawLine(x+6,y+10, x+12,y+2, color);
}
void drawEnter(int x, int y, uint16_t *color) {
tft.drawLine(x,y+4, x+10,y+4, color);
tft.drawLine(x+10,y, x+10,y+4, color);
tft.drawLine(x,y+4, x+4,y, color);
tft.drawLine(x,y+4, x+4,y+8, color);
}
void drawPencilColor(int x, int y, uint16_t *color) {
for(int i=0; i<3; i++) {
tft.drawLine(x+2-i,y+i, x+2+i,y+i, color);
tft.drawLine(x+(2*i),y+4, x+(2*i),y+14, color);
}
}
int16_t wordCount=0;
int prevCursor_x[200];
int prevCursor_y[200];
void setup() {
tft.begin();
ts.begin();
tft.fillScreen(0xFFFF);
}
int x=10;
int y=200;
int cursor_x = 5;
int cursor_y = 20;
bool shift = false;
int line = 0;
int type;
int n=0;
int selectTextColor = 0;
void loop() {
if(n==0) {
for(int i=0; i<4; i++) {
for(int j=0; j<10; j++) {
drawKey(x+(j*22),y+(i*22), 20,20, 0x0000);
tft.setFont(&FreeMono9pt7b);
char *text;
if(shift) {
tft.setCursor(x+5+(j*22), y+13+(i*22));
tft.setTextColor(0xFFFF);
tft.print(wd[j+(10*i)]);
drawShiftLogo(13,269, 0xFFFF);
fillShiftLogo(13,269, 0x0000);
text = WD[j+(10*i)];
}
else {
tft.setCursor(x+5+(j*22), y+13+(i*22));
tft.setTextColor(0xFFFF);
tft.print(WD[j+(10*i)]);
fillShiftLogo(13,269, 0xFFFF);
drawShiftLogo(13,269, 0x0000);
text = wd[j+(10*i)];
}
tft.setCursor(x+5+(j*22), y+13+(i*22));
tft.setTextColor(0x0000);
tft.print(text);
}
}
drawKey(10,288, 20,20, 0x0000);
drawKey(32,288, 20,20, 0x0000);
drawKey(10+(8*22),288, 20,20, 0x0000);
drawKey(10+(9*22),288, 20,20, 0x0000);
drawKey(54,288, 130,20, 0x0000);
tft.setCursor(18+22, 212+(4*22));
tft.setTextColor(0x0000);
tft.print(",");
tft.setCursor(16+(8*22), 214+(4*22));
tft.print(".");
tft.setCursor(5,10);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0x000F);
drawBackspace(210, 270, 0x0000);
drawEnter(212, 294, 0x0000);
drawPencilColor(17+(22*8), 268, textColor[selectTextColor]);
n=1;
}
else if(n==1) {
int x1; int y1;
int w; int h;
if(ts.touched()) {
int xt = Touch_x();
int yt = Touch_y();
tft.setTextColor(textColor[selectTextColor]);
if(xt>10 && xt<10+(9*22)+22 && yt>200 && yt<288+22) {
type = ((xt-10)/22)+(10*((yt-200)/22));
if((type>=0 && type<30) || (type>30 && type<38)) {
drawKeyPressed(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
char *text;
if(shift) {
text = WD[type];
n=0;
shift = false;
}
else {
text = wd[type];
}
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0xFFFF);
tft.setFont(&FreeSerif9pt7b);
tft.setCursor(cursor_x,cursor_y);
tft.print(text);
wordCount++;
tft.getTextBounds(text, cursor_x,cursor_y, &x1,&y1, &w,&h);
prevCursor_x[wordCount] = x1;
prevCursor_y[wordCount] = y1;
cursor_x += w+1;
if(cursor_x>232) {
cursor_x = 5;
cursor_y +=21;
line++;
}
cursor_y = 20+(line*21);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0x000F);
delay(200);
drawKey(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
}
else if(type == 30) {
drawKeyPressed(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
if(shift) {
shift = false;
}
else {
shift = true;
}
delay(200);
drawKey(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
n=0;
}
else if(type == 38) {
drawKeyPressed(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
tft.fillRoundRect(32,224, 8*22,40, 3, 0xFFFF);
tft.drawRoundRect(34,226, 8*22-4,36, 3, 0x0000);
for(int i=0; i<2; i++) {
for(int j=0; j<8; j++) {
tft.fillRoundRect(36+(16*j),228+(17*i), 14,15, 2, textColor[(8*i)+j]);
}
}
tft.drawRoundRect(35+(16*(selectTextColor%8)),227+(17*(selectTextColor/8)), 16,17, 2, 0x07FF);
delay(200);
drawKey(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
n=3;
}
else if(type == 39) {
drawKeyPressed(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0xFFFF);
tft.fillRect(prevCursor_x[wordCount],prevCursor_y[wordCount], 16,22, 0xFFFF);
cursor_x = prevCursor_x[wordCount];
if(prevCursor_y[wordCount] < cursor_y-21) {
line--;
cursor_y -= 21;
}
wordCount--;
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0x000F);
delay(200);
drawKey(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
}
else if(type == 41 || type == 48) {
drawKeyPressed(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0xFFFF);
tft.setCursor(cursor_x,cursor_y);
tft.print(wd[type]);
wordCount++;
tft.getTextBounds(wd[type], cursor_x,cursor_y, &x1,&y1, &w,&h);
prevCursor_x[wordCount] = x1;
prevCursor_y[wordCount] = y1;
cursor_x += w+1;
if(cursor_x>232) {
cursor_x = 5;
cursor_y +=21;
line++;
}
cursor_y = 20+(line*21);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0x000F);
delay(200);
drawKey(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
}
else if(type>=42 && type<=47) {
drawKeyPressed(54, 200+22*floor((yt-200)/22), 130,20, 0x0000);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0xFFFF);
tft.setCursor(cursor_x, cursor_y);
tft.print(wd[type]);
wordCount++;
tft.getTextBounds(wd[type], cursor_x,cursor_y, &x1,&y1, &w,&h);
prevCursor_x[wordCount] = x1;
prevCursor_y[wordCount] = y1;
cursor_x += 7;
if(cursor_x>232) {
cursor_x = 5;
cursor_y +=21;
line++;
}
cursor_y = 20+(line*21);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0x000F);
delay(200);
drawKey(54, 200+22*floor((yt-200)/22), 130,20, 0x0000);
}
else if(type == 49) {
drawKeyPressed(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0xFFFF);
wordCount++;
prevCursor_x[wordCount] = cursor_x;
prevCursor_y[wordCount] = cursor_y-7;
line++;
cursor_y = 20+(line*21);
cursor_x = 5;
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0x000F);
delay(200);
drawKey(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
}
else if(type == 40) {
drawKeyPressed(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
for(int i=0; i<4; i++) {
for(int j=0; j<10; j++) {
tft.fillRect(12+(22*j),202+(22*i), 16,16, 0xFFFF);
tft.setFont(&FreeMono9pt7b);
tft.setCursor(15+(22*j), 213+(22*i));
tft.setTextColor(0x0000);
tft.print(symbol[j+10*i]);
}
}
drawBackspace(210, 270, 0x0000);
n=2;
delay(200);
drawKey(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
}
}
delay(200);
}
}
else if(n==2) {
int x1; int y1;
int w; int h;
if(ts.touched()) {
int xt = Touch_x();
int yt = Touch_y();
tft.setFont(&FreeSerif9pt7b);
tft.setTextColor(textColor[selectTextColor]);
if(xt>10 && xt<10+(9*22)+22 && yt>200 && yt<288+22) {
type = ((xt-10)/22)+(10*((yt-200)/22));
if((type>=0 && type<30) || (type>30 && type<37)) {
drawKeyPressed(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0xFFFF);
tft.setCursor(cursor_x, cursor_y);
tft.print(symbol[type]);
wordCount++;
tft.getTextBounds(symbol[type], cursor_x,cursor_y, &x1,&y1, &w,&h);
prevCursor_x[wordCount] = x1;
prevCursor_y[wordCount] = y1;
cursor_x += w+1;
if(cursor_x > 232) {
cursor_x = 5;
line++;
}
cursor_y = 20+(21*line);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0x000F);
delay(200);
drawKey(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
}
else if(type == 38) {
drawKeyPressed(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
tft.fillRoundRect(32,224, 8*22,40, 3, 0xFFFF);
tft.drawRoundRect(34,226, 8*22-4,36, 3, 0x0000);
for(int i=0; i<2; i++) {
for(int j=0; j<8; j++) {
tft.fillRoundRect(36+(16*j),228+(17*i), 14,15, 2, textColor[(8*i)+j]);
}
}
tft.drawRoundRect(35+(16*(selectTextColor%8)),227+(17*(selectTextColor/8)), 16,17, 2, 0x07FF);
delay(200);
drawKey(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
n=3;
}
else if(type == 39) {
drawKeyPressed(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0xFFFF);
tft.fillRect(prevCursor_x[wordCount],prevCursor_y[wordCount], 16,22, 0xFFFF);
cursor_x = prevCursor_x[wordCount];
if(prevCursor_y[wordCount] < cursor_y-21) {
line--;
cursor_y -= 21;
}
wordCount--;
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0x000F);
delay(200);
drawKey(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
}
else if(type == 41 || type == 48) {
drawKeyPressed(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0xFFFF);
tft.setCursor(cursor_x,cursor_y);
tft.print(wd[type]);
wordCount++;
tft.getTextBounds(wd[type], cursor_x,cursor_y, &x1,&y1, &w,&h);
prevCursor_x[wordCount] = x1;
prevCursor_y[wordCount] = y1;
cursor_x += w+1;
if(cursor_x>232) {
cursor_x = 5;
cursor_y +=21;
line++;
}
cursor_y = 20+(line*21);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0x000F);
delay(200);
drawKey(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
}
else if(type>=42 && type<=47) {
drawKeyPressed(54, 200+22*floor((yt-200)/22), 130,20, 0x0000);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0xFFFF);
tft.setCursor(cursor_x, cursor_y);
tft.print(wd[type]);
wordCount++;
tft.getTextBounds(wd[type], cursor_x,cursor_y, &x1,&y1, &w,&h);
prevCursor_x[wordCount] = x1;
prevCursor_y[wordCount] = y1;
cursor_x += 7;
if(cursor_x>232) {
cursor_x = 5;
cursor_y +=21;
line++;
}
cursor_y = 20+(line*21);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0x000F);
delay(200);
drawKey(54, 200+22*floor((yt-200)/22), 130,20, 0x0000);
}
else if(type == 49) {
drawKeyPressed(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0xFFFF);
wordCount++;
prevCursor_x[wordCount] = cursor_x;
prevCursor_y[wordCount] = cursor_y-7;
line++;
cursor_y = 20+(line*21);
cursor_x = 5;
tft.drawLine(cursor_x+1,cursor_y+3, cursor_x+1,cursor_y-13, 0x000F);
delay(200);
drawKey(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
}
else if(type == 40) {
drawKeyPressed(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
for(int i=0; i<4; i++) {
for(int j=0; j<10; j++) {
tft.fillRect(12+(22*j),202+(22*i), 16,16, 0xFFFF);
}
}
n=0;
delay(200);
drawKey(10+22*floor((xt-10)/22), 200+22*floor((yt-200)/22), 20,20, 0x0000);
}
}
}
delay(200);
}
else if(n==3) {
int xt = Touch_x();
int yt = Touch_y();
if(ts.touched()) {
if(xt>=36 && xt<=(36+(16*8)) && yt>=226 && yt<=264) {
tft.drawRoundRect(35+(16*(selectTextColor%8)),227+(17*(selectTextColor/8)), 16,17, 2, 0xFFFF);
selectTextColor = ((xt-36)/16)+(8*((yt-228)/17));
tft.drawRoundRect(35+(16*(selectTextColor%8)),227+(17*(selectTextColor/8)), 16,17, 2, 0x07FF);
drawPencilColor(17+(22*8), 268, textColor[selectTextColor]);
}
else if((xt>0 && xt<36 && yt>0 && yt<320) || (xt>=36 && xt<36+(16*8) && yt>0 && yt<226) || (xt>=36 && xt<36+(16*8) && yt>264 && yt<320) || (xt>36+(16*8) && xt<240 && yt>0 && yt<320)) {
tft.fillRoundRect(32,224, 8*22,40, 3, 0xFFFF);
n=0;
}
}
}
}