[solved] Mapping TFT-Touchscreen

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

I have a few similar ones (though i got mine on AliExpress for a lot less) and i found that the pinout for the Mega was the same, with the exception of the pins used for the SD card (Mega has different SPI pins) There are some drawbacks to these TFT's , being that they work best on an UNO (fastest), but the library sucks up nearly 20kb, that doesn't leave much in the way of programming on an UNO. They are also quite power hungry, up to such an extend that powering an UNO + TFT of a 9vBlock is not going to work (Darn...) Also they seem to be all a little different. i have some with different orientation, and of course calibration is an issue. As i said they are all a bit different (at least the ones i have) but i would go with the same pinout on the Mega as on the Uno, basically you should be able to just stick it in there.

Wiring is OK, the display is simply plugged into the Mega. Touch and TFT both work, but I have no idea about the correct mapping. The sensitive fields don't correspond with the buttons.

Chances are that it may be the orientation that is wrong. I ended up writing a sketch that sorts it out by making you slide to the edge, then storing the values in EEPROM and load them into another sketch. I don't know what calibrator you are using. The MCUfriend library is not perfect, and the examples are a bit strange.

I tried all orientations (0,1,2,3). The calibration comes from "touchScreen_calibr_native" (see first post).

You should have asked this question in Displays...David would've seen this question there and helped you... :smiley:

So it would be senseful to move this thread to "Displays"? Is there a way to do this? In "How to use this forum" I didn't find a proper way for this. Is it possible? Perhaps by the moderator?

Moving a thread is only possible by Global Moderators...

click "report to moderator" in your original post and request that it be moved.

There is the simplest sketch you can make, that just displays the coordinates of the spot you've just pressed. That should clarify what is going on. i suspect that either the X or the Y (or both) of the touch coordinates are inverted in relation to the TFT coordinates.

This is the calibrator i use which i created from a modified example. No support provided, i use it and it works for me, but that's all. (attached for exceeds 9000 chars)

TFT_TS_Calibrater.ino (15.1 KB)

Sorry, no success, be it on a Uno or a Mega. I also tried different pins for TS: XP 8, XM A2, YP A3, YM 9 (for Mega). Only a grey flickering screen. I'll ask to move the thread to "Displays"
Thank you,
-richard

This is the result of running the Calibration sketch on the Uno.

/*** 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;

It looks perfectly normal i.e. LEFT-RT = ~800 and TOP-BOT = ~850

It will work on every other Arduino. e.g. Uno, Leo, Mega2560, Zero, Due, ...

Please copy-paste your Calibration lines into the TouchShield_kbv and Button_Simple examples.
Then run on a Uno.
And run on a Mega.

The accuracy should be 100% when you run on another 5V Arduino.
The accuracy might be 95-100% on a 3.3V Due.

Likewise, if you calibrate on 3.3V the accuracy will be 95-100% on a 5V target.

Note that the calibration is designed for Portrait. Look at the TouchShield example to see how to map automatically for different rotations

Ask if you have a problem.

You should always run examples before trying to write your own sketches.
Study the example code. Read any explanatory comments.

David.

Thanks for the reply. I tried all the examples and they work fine, both on an Uno and on a Mega. But when I run my own sketch, the mapping is totally different and unusable.
From the calibration program I got the following information:

const int XP=8,XM=A2,YP=A3,YM=9; //240x320 ID=0x9341
const int TS_LEFT=110,TS_RT=923,TS_TOP=75,TS_BOT=894;

The rotation is (1) (landscape). The mapping section is as follows:

  p.x = map(p.x, TS_RT, TS_LEFT, 0, 320);
  p.y = map(p.y, TS_TOP, TS_BOT, 0, 240);

The permutation of the values did not help.

Draw a Portrait Rectangle on a sheet of paper:

-------------
|    Top    |
|           |
|           |
|           |
|           |
| L       R |
|           |
|           |
|           |
|           |
|    Bot    |
-------------

The Touchscreen is fixed. It always returns p.x as a position between Left and Right.

Now rotate to Landscape. It now represents a vertical position.

In Touch_Shield.new.ino you can see the raw numbers that are returned by ts.getPoint()

        tft.print("tp.x=" + String(tp.x) + " tp.y=" + String(tp.y) + "   ");

And how you map these numbers to pixel coordinates in the "Paint" section of the program:

        switch (Orientation) {
            case 0:
                xpos = map(tp.x, TS_LEFT, TS_RT, 0, tft.width());
                ypos = map(tp.y, TS_TOP, TS_BOT, 0, tft.height());
                break;
            case 1:
                xpos = map(tp.y, TS_TOP, TS_BOT, 0, tft.width());
                ypos = map(tp.x, TS_RT, TS_LEFT, 0, tft.height());
                break;
            case 2:
                xpos = map(tp.x, TS_RT, TS_LEFT, 0, tft.width());
                ypos = map(tp.y, TS_BOT, TS_TOP, 0, tft.height());
                break;
            case 3:
                xpos = map(tp.y, TS_BOT, TS_TOP, 0, tft.width());
                ypos = map(tp.x, TS_LEFT, TS_RT, 0, tft.height());
                break;
        }

Take your piece of paper and rotate it to each Orientation value i.e. PORTRAIT, LANDSCAPE, PORTRAIT_REV, LANDSCAPE_REV

It is much easier to stick with a single Portrait Calibration. And just do the appropriate mapping.
Most applications stick to a single Orientation and never change. There is no harm in using the same switch(tft.getRotation()) sequence in every sketch.

Then your Touch buttons will be correct even if you change the "logical" screen to Landscape.

That is the way that the TouchScreen.h library works. i.e. returns ADC values for a fixed X, Y panel.
Other Touch libraries do the mapping for you. (but this involves a library configuration file and knowledge of which TFT library you are using)
Since these Mcufriend Shields vary so much, I find it preferable to put the "Calibration lines" into the user sketch.

David.

I think I understood. The touchscreen coordinates remain the same, independently of the rotation of the tft-screen. So if I have landscape orientation I have to determine the touch points as if the screen would be rotated be 90 degrees. The "rotation() command" only affects the display, not the touch screen.

-richard

Yes. Note that xpos is determined by mapping p.y e.g.

                xpos = map(tp.y, TS_TOP, TS_BOT, 0, tft.width());

Seriously. It is difficult to picture this in your head. Rotating a paper drawing makes it clear.

Thanks for your elaborate explanation!
-richard