Hello Guys,
i am currently trying to make an on screen keyboard and display what is being typed, i would also like to save what is typed as a string. currently i have set up the following code, the keyboard manages to upload to the screen but i lose touch feedback once i draw the keyboard, i was wondering if someone could collaborate on this.
thank you in advance
hardware:
arduino mega2560
RA8875 --> RA8875 Driver Board for 40-pin TFT Touch Displays - 800x480 Max : ID 1590 : $39.95 : Adafruit Industries, Unique & fun DIY electronics and kits
7.0" 40-pin TFT Display - 800x480 with Touchscreen --> 7.0 40-pin TFT Display - 800x480 with Touchscreen : ID 2354 : $34.95 : Adafruit Industries, Unique & fun DIY electronics and kits
#include <SPI.h>
#include "Adafruit_GFX.h"
#include "Adafruit_RA8875.h"
// Library only supports hardware SPI at this time
// Connect SCLK to UNO Digital #13 (Hardware SPI clock)
// Connect MISO to UNO Digital #12 (Hardware SPI MISO)
// Connect MOSI to UNO Digital #11 (Hardware SPI MOSI)
#define RA8875_INT 24
#define RA8875_CS 22
#define RA8875_RESET 23
Adafruit_RA8875 tft = Adafruit_RA8875(RA8875_CS, RA8875_RESET); // LCD Screen
uint16_t tx, ty;
//#define IsWithin(x, a, b) ((x>=a)&&(x<=b))
const char textLimit = 18;
char MyBuffer[textLimit];
const char Mobile_KB[3][13] = {
{0, 13, 10, 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', 'O', 'P'},
{1, 12, 9, 'A', 'S', 'D', 'F', 'G', 'H', 'J', 'K', 'L'},
{3, 10, 7, 'Z', 'X', 'C', 'V', 'B', 'N', 'M'},
};
const char Mobile_NumKeys[3][13] = {
{0, 13, 10, '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'},
{0, 13, 10, '-', '/', ':', ';', '(', ')', '
, '&', '@', '"'},
{5, 8, 5, '.', '\', '?', '!', '''}
};
const char Mobile_SymKeys[3][13] = {
{0, 13, 10, '[', ']', '{', '}', '#', '%', '^', '*', '+', '='},
{4, 9, 6, '_', '\', '|', '~', '<', '>'}, //4
{5, 8, 5, '.', '\', '?', '!', '''}
};
const char textLimit = 25;
char MyBuffer[textLimit];
void MakeKB_Button(const char type[][13])
{
tft.textMode();
tft.setTextSize(2);
tft.setTextColor(0xffff, 0xf000);
for (int y = 0; y < 3; y++)
{
int ShiftRight = 15 * type[y][0];
for (int x = 3; x < 13; x++)
{
if (x >= (type[y][1])) break;
drawButton(15 + (30 * (x - 3)) + ShiftRight, 100 + (30 * y), 20, 25); // this will draw the button on the screen by so many pixels
tft.textSetCursor(20 + (30 * (x - 3)) + ShiftRight, 105 + (30 * y));
tft.print(char (type[y][x]));
}
}
//drawbutton(xStart, yStart, width, height);
//ShiftKey
drawButton(15, 160, 35, 25);
tft.textSetCursor(27, 168);
tft.print('^');
//Special Characters
drawButton(15, 190, 35, 25);
tft.textSetCursor(21, 195);
tft.print(F("SP"));
//BackSpace
drawButton(270, 160, 35, 25);
tft.textSetCursor(276, 165);
tft.print(F("BS"));
//Return
drawButton(270, 190, 35, 25);
tft.textSetCursor(276, 195);
tft.print(F("RT"));
//Spacebar
drawButton(60, 190, 200, 25);
tft.textSetCursor(105, 195);
tft.print(F("SPACE BAR"));
}
void drawButton(int x, int y, int w, int h)
{
// grey
tft.fillRoundRect(x - 3, y + 3, w, h, 3, 0x8888); //Button Shading
// white
tft.fillRoundRect(x, y, w, h, 3, 0xffff);// outter button color
//red
tft.fillRoundRect(x + 1, y + 1, w - 1 * 2, h - 1 * 2, 3, 0xf800); //inner button color
}
void GetKeyPress(char * textBuffer)
{
char key = 0;
static bool shift = false, special = false, back = false, lastSp = false, lastSh = false;
static char bufIndex = 0;
if (tft.touched())
{
//ShiftKey
if (TouchButton(15, 160, 35, 25))
{
shift = !shift;
delay(200);
}
//Special Characters
if (TouchButton(15, 190, 35, 25))
{
special = !special;
delay(200);
}
if (special != lastSp || shift != lastSh)
{
if (special)
{
if (shift)
{
tft.fillScreen(RA8875_BLUE);
MakeKB_Button(Mobile_SymKeys);
}
else
{
tft.fillScreen(RA8875_BLUE);
MakeKB_Button(Mobile_NumKeys);
}
}
else
{
tft.fillScreen(RA8875_BLUE);
MakeKB_Button(Mobile_KB);
tft.setTextColor(0xffff, 0xf800);
}
if (special)
tft.setTextColor(0x0FF0, 0xf800);
else
tft.setTextColor(0xFFFF, 0xf800);
tft.setCursor(21, 195);
tft.print(F("SP"));
if (shift)
tft.setTextColor(0x0FF0, 0xf800);
else
tft.setTextColor(0xffff, 0xf800);
tft.setCursor(27, 168);
tft.print('^');
lastSh = shift;
lastSp = special;
lastSh = shift;
}
for (int y = 0; y < 3; y++)
{
int ShiftRight;
if (special)
{
if (shift)
ShiftRight = 15 * (Mobile_SymKeys[y][0]);
else
ShiftRight = 15 * (Mobile_NumKeys[y][0]);
}
else
ShiftRight = 15 * (Mobile_KB[y][0]);
for (int x = 3; x < 13; x++)
{
if (x >= (special ? (shift ? (Mobile_SymKeys[y][1]) : (Mobile_NumKeys[y][1])) : (Mobile_KB[y][1]) )) break;
if (TouchButton(15 + (30 * (x - 3)) + ShiftRight, 100 + (30 * y), 20, 25)) // this will draw the button on the screen by so many pixels
{
if (bufIndex < (textLimit - 1))
{
delay(200);
if (special)
{
if (shift)
textBuffer[bufIndex] = (Mobile_SymKeys[y][x]);
else
textBuffer[bufIndex] = (Mobile_NumKeys[y][x]);
}
else
textBuffer[bufIndex] = ((Mobile_KB[y][x]) + (shift ? 0 : ('a' - 'A')));
bufIndex++;
}
break;
}
}
}
//Spacebar
if (TouchButton(60, 190, 200, 25))
{
textBuffer[bufIndex++] = ' ';
delay(200);
}
//BackSpace
if (TouchButton(270, 160, 35, 25))
{
if ((bufIndex) > 0)
bufIndex--;
textBuffer[bufIndex] = 0;
tft.setTextColor(0, RA8875_BLUE);
tft.setCursor(15, 80);
tft.print(F(" "));
delay(200);
}
//Return
if (TouchButton(270, 190, 35, 25))
{
Serial.println(textBuffer);
while (bufIndex > 0)
{
bufIndex--;
textBuffer[bufIndex] = 0;
}
tft.setTextColor(0, RA8875_BLUE);
tft.setCursor(15, 80);
tft.print(F(" "));
}
}
tft.setTextColor(0xffff, 0xf800);
tft.setCursor(15, 80);
tft.print(textBuffer);
}
void setup() {
Serial.begin(9600);
Serial.println("RA8875 start");
/* Initialize the display using 'RA8875_480x80', 'RA8875_480x128', 'RA8875_480x272' or 'RA8875_800x480' */
if (!tft.begin(RA8875_800x480)) {
Serial.println("RA8875 Not Found!");
while (1);
}
Serial.println("Found RA8875");
tft.displayOn(true);
tft.GPIOX(true); // Enable TFT - display enable tied to GPIOX
tft.PWM1config(true, RA8875_PWM_CLK_DIV1024); // PWM output for backlight
tft.PWM1out(255);
tft.fillScreen(RA8875_WHITE);
MakeKB_Button(Mobile_KB);
pinMode(RA8875_INT, INPUT);
digitalWrite(RA8875_INT, HIGH);
Serial.print("Status: "); Serial.println(tft.readStatus(), HEX);
Serial.println("Waiting for touch events ...");
}
void loop()
{
GetKeyPress(MyBuffer);
}
byte TouchButton(uint16_t tx, uint16_t ty, int w, int h)
{
float xScale = 1024.0F / tft.width();
float yScale = 1024.0F / tft.height();
/* Wait around for touch events */
if (! digitalRead(RA8875_INT))
{
if (tft.touched())
{
Serial.print("Touch: ");
tft.touchRead(&tx, &ty);
Serial.print(tx); Serial.print(", "); Serial.println(ty);
}
}
}
//if ((x >= 0) && (x < _width) && (y >= 0) && (y < _height))