Members
The code below returns a number instead of a character. The numbers 0 to 9 returns the correct number as its in sequence with the "0", "1" etc.
Anything after that ie "Q","W" returns the number 10 and 11.
How can code be used/added to convert the number into the corresponding key touch?
/*Hard copy printed 25 july
For a capacitive touch screen.
Creates a grid of numbered rectangles, each of
which will trap touches and report its id.
Any of the four screen rotations may be used.
++ Program copied from Arduino forum++
**and edited for use with DUE and 2.8" UNO shield ILI9341
multi wire.
*/
#include <Adafruit_GFX.h>
#include <TouchScreen.h>
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
// Default values for UNO shield. Multi wire.
#define LCD_CS A3
#define LCD_CD A2
#define LCD_WR A1
#define LCD_RD A0
#define LCD_RESET A4
#define YP A2 // must be an analog pin, use "An" notation!
#define XM A3 // must be an analog pin, use "An" notation!
#define YM 8 // can be a digital pin
#define XP 9 // can be a digital pin
#define isWithin(x,a,b) ((x>=a) && (x<=b))
#define MINPRESSURE 10
#define MAXPRESSURE 1000
#define TS_MINX 167
#define TS_MINY 228
#define TS_MAXX 880
#define TS_MAXY 860
#define BLACK 0x0000
#define BLUE 0x001F
#define WHITE 0xFFFF
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 365);
int X, Y, Z;
typedef struct {
int left;
int top;
int width;
int height;
} rectangle;
const byte rotation = 2;
const byte col = 10;
const byte row = 4;
rectangle rect[col * row];
const char *btnTitle[col * row] = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "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", "/", "<", "#"};
void showGrid()
{
int left, top;
int l = 5; //LEFT
int t = 225; //TOP
int w = 20; //WIDTH
int h = 20; //HEIGHT
int hgap = 3;
int vgap = 3;
byte id = 0;
for (byte j = 0; j < row; j++)
{
for (byte i = 0; i < col; i++)
{
left = l + i * (w + vgap);
top = t + j * (h + hgap);
rect[id].left = left;
rect[id].top = top;
rect[id].width = w;
rect[id].height = h;
tft.drawRect( left, top, w, h, BLUE);
tft.setCursor(left + 4, top + 2);
tft.print(btnTitle[id]);
id++;
}
}
}
void showZeroZero() {
// tft.setCursor(0, 0);
// tft.print("0,0");
}
void setup() {
Serial.begin(9600);
tft.reset();
uint16_t identifier = tft.readID();
tft.begin(identifier);
tft.setRotation(rotation); //2
tft.fillScreen(BLACK);
tft.setTextColor(WHITE);
tft.setTextSize(2);
showZeroZero();
showGrid();
}
void loop() {
static uint16_t x, y;
TSPoint p = ts.getPoint();
if (p.z > ts.pressureThreshhold) {
p.y = map(p.y, TS_MINY, TS_MAXY, 0, 240);
p.x = map(p.x, TS_MINX, TS_MAXX, 0, 320);
X = tft.height() - map(p.x, TS_MINX, TS_MAXX, 0, tft.height());
Y = map(p.y, TS_MINY, TS_MAXY, 0, tft.width());
Z = p.z;
if (rotation == 2) {
x = p.x;
y = 240-p.y;
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
}
for (byte count = 0; count < (col * row); count++)
{
if (isWithin(y, rect[count].left, (rect[count].left + rect[count].width)) && isWithin(x, rect[count].top, (rect[count].top + rect[count].height)))
{
showTouchData (x, y, count);
}
}
}
}
void showTouchData(int xCoord, int yCoord, int id) {
tft.fillRect (10, 30, tft.width() - 10, 20, BLACK);
tft.setCursor(10, 30);
tft.print(F(" id=")); tft.print(id);
}
Thanks in advance