Hi everyone,
I try to get acquainted with touch screens. I have this (link) TFT-Colour-Device (driver chip: ILI9341) with a touch screen. I found that the Mcufriend-library-examples work with this shield, as long as I have it plugged onto an UNO. But in my project I need a Mega 2560, so I tried to port an example to the Mega. The "TouchScreen_Calibr_native" example from the named library told me, that I have to use different pins for the TouchScreen on the Mega than on the Uno: XP=8, XM=A2, YP=A3, YM=9. It gave also the calibration data: x=map(p.y, LEFT=72, RT=892, 0, 320) and y=map(p.x, TOP=924, BOT=111, 0, 240). With the following code I succeeded in using this shield on the Mega:
/*** COPY-PASTE from Serial Terminal:
const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x9341
const int TS_LEFT=116,TS_RT=936,TS_TOP=68,TS_BOT=891;
PORTRAIT CALIBRATION 240 x 320
x = map(p.x, LEFT=116, RT=936, 0, 240)
y = map(p.y, TOP=68, BOT=891, 0, 320)
LANDSCAPE CALIBRATION 320 x 240
x = map(p.y, LEFT=68, RT=891, 0, 320)
y = map(p.x, TOP=936, BOT=116, 0, 240)
Werte aus TouchScreen_Calibr_native
*/
#define MINPRESSURE 10 // set pressure thresholds for touchscreen
#define MAXPRESSURE 1000
#include <Adafruit_GFX.h> // graphics library
#include <MCUFRIEND_kbv.h> // screen control library
MCUFRIEND_kbv tft; // define tft object
// configure the Analog ports
#define LCD_CS A3 // Chip Select goes to Analog 3
#define LCD_CD A2 // Command/Data goes to Analog 2
#define LCD_WR A1 // LCD Write goes to Analog 1
#define LCD_RD A0 // LCD Read goes to Analog 0
#define LCD_RESET A4 // Can alternately just connect to Arduino's reset pin
// X
#define TS_MINX 72
#define TS_MAXX 892
// Y
#define TS_MINY 111
#define TS_MAXY 924
// MCUFRIEND UNO shield shares pins with the TFT.
#define YP A3 //A3 for ILI9320
#define YM 9 //9
#define XM A2
#define XP 8 //8
#include <TouchScreen.h>
TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300); // define touch object
TSPoint tp; //Touchscreen_due branch uses Point
// Define some colours for use in screens
#define BLACK 0x0000
#define BLUE 0x001F
#define GREEN 0x07E0
#define YELLOW 0xFFE0
#define WHITE 0xFFFF
#define CHERRY 0xF020
// for help defining colours, go to: http://www.barth-dev.de/online/rgb565-color-picker/
/* click on the colour square and pick your colour. Then lookunder the RGB565 heading
and you'll get the hex byte representation for that colour
*/
long lastTouch = millis();// last touch time for minimum delay between touches
long tThresh = 200;// minimum time between touches
int mode = 0;// current display function - starts with 0 - menu screen
int tMode = 0;// the current touch mode - starts with 0 - menu screen
//Adafruit_GFX tft(LCD_CS, LCD_CD, LCD_WR, LCD_RD, LCD_RESET);
//TouchScreen ts = TouchScreen(XP, YP, XM, YM, 300);
boolean buttonEnabled = true;
void setup() {
Serial.begin(9600);
Serial.println("Starting...");
tft.reset();
uint16_t ID = tft.readID(); // get the screen's ID
Serial.println(ID, HEX);
tft.begin(ID);// assign the ID to the screen object
// tft.begin(0x9341);
tft.setRotation(1);
tft.fillScreen(BLACK);
//Draw white frame
tft.drawRect(0,0,319,240,WHITE);
//Print "Einheit" Text
tft.setCursor(20,50);
tft.setTextColor(WHITE);
tft.setTextSize(3);
tft.print("Welche Einheit?");
//Create Red Button mW
tft.fillRect(30,180, 100, 40, CHERRY);
tft.drawRect(30,180,100,40,WHITE);
tft.setCursor(50,190);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("mW/m2");
//Create Red Button µE
tft.fillRect(200,180, 100, 40, CHERRY);
tft.drawRect(200,180,100,40,WHITE);
tft.setCursor(215,190);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("microE");
}
void loop()
{
TSPoint p = ts.getPoint(); //Get touch point
if (p.z > ts.pressureThreshhold) {
Serial.print("X = "); Serial.print(p.x);
Serial.print("\tY = "); Serial.print(p.y);
Serial.print("\n");
/* p.x = map(p.x, 892, 72, 0, 320); // (p.x, TS_MAXX, TS_MINX, 0, 320);
p.y = map(p.y, 924, 111, 0, 240); // (p.y, TS_MAXY, TS_MINY, 0, 240);*/
p.x = map(p.x, TS_MAXX, TS_MINX, 0, 240); // (p.x, TS_MAXX, TS_MINX, 0, 320);
p.y = map(p.y, TS_MAXY, TS_MINY, 0, 320); // (p.y, TS_MAXY, TS_MINY, 0, 240);
if(p.x>30 && p.x<180 && p.y>180 && p.y<220 && buttonEnabled)// The user has pressed inside the red rectangle
{
buttonEnabled = false; //Disable button
//This is important, because the libraries are sharing pins
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//Erase the screen
tft.fillScreen(BLACK);
//Draw frame
tft.drawRect(0,0,319,240,WHITE);
tft.setCursor(50,50);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("Einheit: mW/m2");
}
else if(p.x>200 && p.x<300 && p.y>180 && p.y<220 && buttonEnabled)// The user has pressed inside the red rectangle
{
buttonEnabled = false; //Disable button
//This is important, because the libraries are sharing pins
pinMode(XM, OUTPUT);
pinMode(YP, OUTPUT);
//Erase the screen
tft.fillScreen(BLACK);
//Draw frame
tft.drawRect(0,0,319,240,WHITE);
tft.setCursor(50,50);
tft.setTextColor(WHITE);
tft.setTextSize(2);
tft.print("Einheit: microE");
}
delay(10);
}
}
But this code has a large drawback: The mapping does not work properly. I tried different combination of the values, but no one worked as it should. The touch points to activate the buttons are all outside of the buttons. So I know, the touchscreen works in principle on the Mega, but the mapping of the point is not in order. On the Uno (with YP=A2, XM=A3, YM=8, XP=9) everything is fine.
What am I doing wrong with the mapping?
Thanks
-richard