Touch screen operation on Arduino DUE

Published on October 08, 2013

Touch screen operation on Arduino DUE

  • Tutorial
In the previous article “Working with a TFT display on ARDUINO DUE”, we examined in detail the commands of the UTFT base library designed for working with TFT displays on Arduino. In this article, we will look at Utouch - an addition to the base library that allows you to work with the touch screen.

1. General information


This add-on was developed by the same author as the base library. The latest version can be downloaded here:
http://www.henningkarlsen.com/electronics/library.php?id=55
This add-on is not independent and requires the connection of the UTFT base library.

So, let's take a closer look at the Utouch add-on, designed to work with the touch screen that most TFT displays are equipped with.

2. Getting started with the touch screen


To work with the Utouch add-on, you must load it into your program and perform initialization. Naturally, the UTFT base library should be loaded and initialized. We remind you that for our display, which was discussed in the previous article, the initialization is as follows:
#include <UTFT.h>
UTFT myGLCD(CTE32HR,25,26,27,28);

Now we load the add-on and create an object - a touch screen called myTouch. As parameters, the initialization command receives the pin numbers to which the TCLK, TCS, TDIN, TDOUT, and IRQ signals are connected. These parameters should be obtained from the manufacturer of the display or nameplate through which the display is connected to the controller.

In our touch screen model, the above-mentioned signals are connected, respectively, to pins 6, 5, 4, 3 and 2. We remind you that the nameplate circuit with which the display is connected can be found here:
http://coldtears.lin3.siteonlinetest.com/ files / CTE_DUE_shield.zip
and the diagram of the display module itself is here:
http://coldtears.lin3.siteonlinetest.com/files/3.2b.zip

So for our display we must insert the following lines into the program:
#include <UTouch.h>
UTouch myTouch(6,5,4,3,2);

Next, initialize the void setup procedure in the procedure () display and touch screen:
void setup() {
myGLCD.InitLCD();
myTouch.InitTouch();
}

The InitTouch command, like the InitLCD command of the base library, sets the vertical or horizontal orientation of the display. The default orientation is horizontal. Everything, now you can work with our touch screen. Consider a very simple example with which we can make sure that the touch screen is working. We compile the following program and load it into the controller memory:

#include <UTFT.h>
UTFT myGLCD(CTE32HR,25,26,27,28);
#include <UTouch.h>
UTouch myTouch(6,5,4,3,2);
void setup() {
  myGLCD.InitLCD();
  myTouch.InitTouch();
  myGLCD.clrScr();
  myGLCD.fillScr(255,0,0);
}
void loop() {
if (myTouch.dataAvailable()) {
myGLCD.fillScr(0,255,0);
}else {myGLCD.fillScr(255,0,0);}
}

When the program starts, the display turns red. Now touch the screen at any point - and the display turns green.

3. Touch screen calibration


Before you continue, you must perform another important procedure - touch screen calibration. In the examples of programs that come with the Utouch add-on, there is one very important example called UTouch_Calibration .
To perform the calibration, you must open this example through the menu:
“File-> Examples-> UTouch-> Arduino-> UTouch_Calibration”.

ATTENTION! Before compiling, do not forget to replace the lines UTFT myGLCD (....) And UTouch myTouch (....) With those that are needed for our display.

Also, when calibrating, you must correctly select the TOUCH_ORIENTATION parameter. Information on the value of this parameter for the display model used can be found in the UTouch_Supported_display_modules.pdf file, which is supplied with the Utouch library and is located in its root folder. For our display, for example, PORTRAIT should be set, even though the display itself is oriented horizontally.

ATTENTION! This value must be set only when calibrating the touch screen; subsequently, the screen orientation must coincide with the orientation of the display module.

At startup, the program displays an explanatory text on the display and waits for a touch on the touch screen. Then the display shows 8 cross marks at the edges of the display and a red window with PRESS in the center. The instruction of developers informs that it is necessary to touch the highlighted cross and hold the touch until the backlight on it goes out and the message “RELEASE” appears in the red field in the center. This simple operation must be repeated for all crosses in turn.
By the way, the developers advise you to perform this operation with a stylus or a pointed stick, without using your fingers, this will improve the accuracy of calibration. At the end of the calibration, three numbers in hexadecimal format will be displayed. For example, these are:

CAL_X 0xBD95ECC8UL
CAL_Y 0x5EBC8000UL
CAL_S 0x0013F1DFUL

These three values ​​must be placed in the UTouch_CD.h file located in the root folder of the UTouch add-on instead of the CAL_X, CAL_Y and CAL_S values ​​that were originally specified there. After that, the display will work correctly.

Now let’s take a closer look at the UTouch add-on commands:

UTouch - creates the base class of the touch screen with the given name, the connection method is indicated as the parameters.

The command has the following form:
Utouch _NAME_ (TCLK, TCS, TDIN, TDOUT, IRQ);

where _NAME_ is an arbitrary name of the “touch screen” object, which will be used as a prefix for any access to it, and TCLK, TCS, TDIN, TDOUT and IRQ are the pin numbers to which the corresponding touch screen signals are connected.

A line with this command is located in the definition area and must precede any other UTouch commands. The specified object name should be used as a prefix to all subsequent UTouch commands.

InitTouch - initializes the touch screen and sets the horizontal or vertical orientation. The orientation identifier is specified as a parameter.

If you specify PORTRAIT or 0 in this command, the vertical orientation will be selected, if you specify LANDSCAPE or 1 - horizontal. Please note that the orientation of the touch screen must match the orientation of the display. By default (without parameters) the command sets the horizontal orientation.

setPrecision - sets the accuracy of determining the coordinates of the touch point on the touch screen. An accuracy identifier is passed as a parameter.

In total, four levels of accuracy are defined, encoded by numbers from 1 to 4:

PREC_LOW - low accuracy;
PREC_MEDIUM - average accuracy;
PREC_HI - high accuracy;
PREC_EXTREME - very high accuracy.

Naturally, the level of accuracy is inversely proportional to the time taken to determine the coordinates. Optimal for most tasks is the level of PREC_MEDIUM. Developers do not recommend the use of high and very high accuracy for operations requiring a quick response.

The difference between accuracy levels can be illustrated by the UTouch_QuickDraw example that comes with the UTouch library. This program allows you to draw on the display using the touch screen:
#include <UTFT.h>
UTFT myGLCD(CTE32HR,25,26,27,28);
#include <UTouch.h>
UTouch myTouch(6,5,4,3,2);
void setup() {
  myGLCD.InitLCD();
  myGLCD.clrScr();
  myTouch.InitTouch(PORTRAIT);
  myTouch.setPrecision(PREC_MEDIUM);
}
void loop() {
  long x, y;
  while (myTouch.dataAvailable() == true)  {
    myTouch.read();
    x = myTouch.getX();
    y = myTouch.getY();
    if ((x!=-1) and (y!=-1)) {
      myGLCD.drawPixel (x, y);
    }
  }
}

Try compiling this example with different values ​​for the parameter of the setPrecision command and you will immediately feel the difference.

dataAvailable - a function that returns a boolean true if, at the time of its call, a touch on the touch screen is fixed. The rest of the time - returns false.

This function can be used both to determine the moment of contact and its duration. In our first example, which we gave in Section 2, the design:
if (myTouch.dataAvailable()) {
myGLCD.fillScr(0,255,0);
}else {myGLCD.fillScr(255,0,0);}

contained a call to the dataAvailable function, which triggered a change in the color of the display, which remained for the entire time of the touch. Consider another example - measuring the time it takes to touch in milliseconds:

#include <UTFT.h>
UTFT myGLCD(CTE32HR,25,26,27,28);
extern uint8_t BigFont[];
#include <UTouch.h>
UTouch myTouch(6,5,4,3,2);
void setup() {
myGLCD.InitLCD();
myGLCD.setFont(BigFont);
myTouch.InitTouch();
myGLCD.clrScr();
}
void loop() {
unsigned long timeOld;
unsigned long timeNew;
if (myTouch.dataAvailable() == true){
timeOld=millis();
while (myTouch.dataAvailable() == true) {
timeNew=millis();
}
myGLCD.clrScr();
myGLCD.printNumI(timeNew-timeOld, 100, 100);
}
}

at the moment the touch display is touched, the program saves the current value of the millis function in the variable timeOld. Further, as long as the touch is saved, the program constantly updates the current millis value in the timeNew variable. As soon as the touch has stopped, the difference between these two variables is displayed, which will be equal to the touch time in milliseconds.

So, now we can work with the “touch screen touch” event, we can determine the moment of touch, its duration and the moment of release. There is very little left to get the coordinates of the touch point. The following commands are intended for this:

read - starts the procedure for determining the coordinates of the touch point.

This command does not pass any parameters and does not return any value. It should be run when the dataAvailable function is true. The resulting coordinates in raw form are stored in the internal variables TP_X and TP_Y. However, this data is not reduced to the size of the display and its resolution, therefore, to get the coordinates of the touch point, special functions getX and getY are used.

getX - returns the value of the X-coordinate of the touch point in pixels relative to the left edge of the display.

getY - returns the value of the Y-coordinate of the touch point in pixels relative to the top edge of the display.

Provided that the calibration is correctly performed and the touch screen is accurate enough, the coordinates of the touch point must match the coordinates of a specific pixel. The return type for these two functions is int.

The following example, when you touch the touch screen, displays the coordinates of the touch point in pixels and in the "raw" format:
#include <UTFT.h>
UTFT myGLCD(CTE32HR,25,26,27,28);
extern uint8_t BigFont[];
#include <UTouch.h>
UTouch myTouch(6,5,4,3,2);
void setup() {
  myGLCD.InitLCD();
  myGLCD.setFont(BigFont);
  myTouch.InitTouch();
  myGLCD.clrScr();
}
void loop() {
int X_RAW;
int Y_RAW;
int X;
int Y;
if (myTouch.dataAvailable() == true){
myTouch.read();
X_RAW=myTouch.TP_X;
Y_RAW=myTouch.TP_Y;
X=myTouch.getX();
Y=myTouch.getY();
myGLCD.clrScr();
myGLCD.print("X", 100,80);
myGLCD.print("Y", 200,80);
myGLCD.print("PIX", 20,100);
myGLCD.print("RAW", 20,120);
myGLCD.printNumI(X, 100, 100);
myGLCD.printNumI(Y, 200, 100);
myGLCD.printNumI(X_RAW, 100, 120);
myGLCD.printNumI(Y_RAW, 200, 120);
}
}

Now, in order to process, for example, pressing a screen button, it is enough to make sure that the received coordinates of the touch point are inside the area occupied by the button on the display. Here is another example:
#include <UTFT.h>
UTFT myGLCD(CTE32HR,25,26,27,28);
extern uint8_t BigFont[];
#include <UTouch.h>
UTouch myTouch(6,5,4,3,2);
void setup() {
  myGLCD.InitLCD();
  myGLCD.setFont(BigFont);
  myTouch.InitTouch();
  myGLCD.clrScr();
}
void loop() {
int X;
int Y;
myGLCD.drawRect(100,100,150,150);
if (myTouch.dataAvailable() == true){
myTouch.read();
X=myTouch.getX();
Y=myTouch.getY();
if (X>100 && X<150 && Y>100 && Y<150) {
myGLCD.print("PRESSED", 100,200);
}else{myGLCD.print("       ",100,200);}
}
}

The program draws a rectangle-button on the display. When you touch the touch screen in the button area, “PRESSED” is displayed, when you touch outside the rectangle, the inscription goes out.

Another interesting example. This program draws a rectangular “window” on the display and allows you to resize it by dragging the lower right corner. Of course, this is not a full Drag & Drop but very similar:
#include <UTFT.h>
UTFT myGLCD(CTE32HR,25,26,27,28);
extern uint8_t BigFont[];
#include <UTouch.h>
UTouch myTouch(6,5,4,3,2);
int X=100;
int Y=100;
void setup() {
  myGLCD.InitLCD();
  myGLCD.setFont(BigFont);
  myTouch.InitTouch();
  myTouch.setPrecision(PREC_EXTREME);
  myGLCD.clrScr();
  myGLCD.drawRect(0,0,X,Y);
}
void loop() {
if (myTouch.dataAvailable() == true){
myGLCD.setColor(0,0,0);
myGLCD.drawRect(0,0,X,Y);
myTouch.read();
X=myTouch.getX();
Y=myTouch.getY();
myGLCD.setColor(255,255,255);
myGLCD.drawRect(0,0,X,Y);
}
}

4. Conclusion


So, now we can use the touch screen, we can determine the moment of touch and determine the coordinates of the point of touch, work with simple controls displayed on the display. In the next article, we plan to consider another addition to the UTFT library - UTFT_Buttons, which allows you to organize work with text and graphic on-screen buttons.