HCTFT_Touch - Touch Screen Library TFT Touch screens

Useful guides, libraries, and example sketches to support our Arduino based products.
admin
Site Admin
Posts: 866
Joined: Sun Aug 05, 2012 4:02 pm

HCTFT_Touch - Touch Screen Library TFT Touch screens

Post by admin » Wed Jun 11, 2014 2:14 pm

Image



Description:

This library provides basic touch screen support for our range of colour TFT resistive touch screens available via our webstore. The supported TFT panels include a resistive touch screen with a ADS7843 / TSC2046 touch screen controller and although this library is written for our screens, it should work with any panel using this controller.

PLEASE NOTE: For MCUFriend displays there are many different types with different chipsets. It is not practical for us to support every version. Therefore we can only support versions purchased directly from us.


You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area

On Windows:
My Documents\Arduino\libraries\

On Mac:
Documents/Arduino/libraries/
or similarly for Linux.

To use the library just include the HCTFT_Touch.h header file and create an instance of the TFT touch screen library. E.g:

Code: Select all

HCTFT_Touch Touch(ScreenType);
Where ScreenType is one of the currently supported screens:

ELECFREAKS_2_4_WITH_TFT01_UNO (Elecfreaks 2.4" TFT HCARDU0083)
ELECFREAKS_3_2_WITH_TFT01_MEGA (Elecfreaks 3.2" TFT HCARDU0097)
MCUFRIEND_3_5_MEGA; (Mcufriend 3.5" TFT HCARDU0096)



The following functions are available with this library:

Code: Select all

Touch.Pressed();
Checks the screen to see if it is currently pressed. Returns Boolean TRUE if pressed, FALSE if not pressed.

Code: Select all

ReadAxis(Axis);
Reads either the X or Y axis of the currently pressed screen as a raw value of type INT from the touch screen controller. Where Axis is the X or Y axis to read. To specify which axis use the predefined boolean values XAXIS or YAXIS.

Code: Select all

GetCoord(Axis);
Same as ReadAxis(Axis); but returns the X or Y coordinate as a function of the screen resolution in pixels. Note that if the returned value is not accurate for your screen you can edit the header file HCTFT_Calibration.h which contains the maximum and minimum raw screen values for the usable area of the screen. You can use the ReadAxis(Axis); function to obtain these values.


Example Sketch:

Code: Select all

/* FILE:    HCTFT_Touch_Example_1
   DATE:    10/06/14
   VERSION: 0.2
   AUTHOR:  Andrew Davies
   
   V0.2 (17_06_2015): Updated for our latest TFT displays (HCARDU0097 & HCARDU0096)

This is an example of how to use the HCTFT_Touch library. The library provides the 
ability to read the current status of the touch screen. It does not provide any 
functions for controlling the TFT screen itself but is intended for providing touch 
screen support to any existing sketches or libraries. However the library will work 
standalone as this example sketch will demonstrate.

The example will read the current state of the touch screen and output it to the serial
port as raw X and Y coordinates, and as the current position in pixels. No information 
will be displayed on the TFT screen itself whilst this example is running.

To use this sketch you will need to uncomment the appropriate initialiser below for 
your particular display.

You may copy, alter and reuse this code in any way you like, but please leave
reference to HobbyComponents.com in your comments if you redistribute this code.
This software may not be used directly for the purpose of selling products that
directly compete with Hobby Components Ltd's own range of products.

THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
REASON WHATSOEVER.
*/

#include <HCTFT_Touch.h>

/* Uncomment one of the lines below to to create an instance of the TFT_Touch 
   library to match your screen */
//HCTFT_Touch Touch(ELECFREAKS_2_4_WITH_TFT01_UNO); // Elecfreaks 2.4" TFT (HCARDU0083)
//HCTFT_Touch Touch(ELECFREAKS_3_2_WITH_TFT01_MEGA); // Elecfreaks 3.2" TFT (HCARDU0097)
//HCTFT_Touch Touch(MCUFRIEND_3_5_MEGA); // Mcufriend 3.5" TFT (HCARDU0096)


void setup() 
{
  Serial.begin(9600);
}

/* Main program */
void loop()
{
  int X_RawData;
  int Y_RawData;
  int X_Coord;
  int Y_Coord;
  
  /* Check if the touch screen is currently pressed */
  if(Touch.Pressed())
  {
    /* Read the raw data for the X and Y axis */
    Y_RawData = Touch.ReadAxis(YAXIS);
    X_RawData = Touch.ReadAxis(XAXIS);
    
    /* Read the current X and Y axis as pixel co-ordinates */
    Y_Coord = Touch.GetCoord(YAXIS);
    X_Coord = Touch.GetCoord(XAXIS);

    /* Output the results to the serial port */
    Serial.print(Y_RawData);
    Serial.print(" : ");
    Serial.print(X_RawData);
    Serial.print(" : ");    
    Serial.print(Y_Coord);
    Serial.print(" : ");
    Serial.println(X_Coord);
    delay(100);
  }
}

Library:
HCTFT_Touch.zip
You do not have the required permissions to view the files attached to this post.

DIY_Monster
Posts: 13
Joined: Thu Nov 20, 2014 3:35 pm

Re: HCTFT_Touch - Touch Screen Library for 2.4" TFT Panel

Post by DIY_Monster » Fri May 15, 2015 10:40 pm

Hello, Please could someone tell me why using this library the X and Y seem to be swapped?
If the display is used in landscape say, the max X co-ords are shorter than may Y co-ords.

This makes locating a print area extremely difficult as the print co-ords do not correspond with the Touch read co-ords.

Have I missed something?

Many thanks.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCTFT_Touch - Touch Screen Library for 2.4" TFT Panel

Post by andrew » Sat May 16, 2015 7:33 am

I'm not aware of any problems. Are you using one of our TFT displays with it and can I confirm that it's the HCTFT_Touch library that has the problem and not the UTFT library? Also have you tired the example sketch in the first post of the thread for this display:

http://forum.hobbycomponents.com/viewto ... =75&t=1632
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

DIY_Monster
Posts: 13
Joined: Thu Nov 20, 2014 3:35 pm

Re: HCTFT_Touch - Touch Screen Library for 2.4" TFT Panel

Post by DIY_Monster » Sat May 16, 2015 11:09 am

Thanks Andrew, Yes I am using the the HCTFT_Touch.zip and the unaltered example sketch provided. When run, if I drag my finger from top middle down to bottom middle (held in landscape) I get the following data on the serial monitor.

Y : X : Y : X < written here for clarity
2052 : 3642 : 119 : 6
2027 : 3641 : 120 : 6
2160 : 3433 : 112 : 25
2084 : 3357 : 118 : 33
2064 : 3204 : 119 : 47
2080 : 2989 : 118 : 67
2063 : 2780 : 119 : 87
2098 : 2522 : 116 : 111
2126 : 2214 : 115 : 139
2112 : 1992 : 116 : 160
2116 : 1877 : 115 : 171
2102 : 1686 : 116 : 189
2128 : 1437 : 114 : 213
2085 : 1285 : 117 : 226
1997 : 1150 : 123 : 239
2158 : 952 : 112 : 257
2068 : 868 : 118 : 264
2022 : 748 : 121 : 276
2016 : 661 : 122 : 285
1988 : 544 : 124 : 295
1980 : 499 : 124 : 299

I am using:
Uno HCARDU0090
Adapter HCARDU0084 - 01
TFT HCARDU0083 - 01
Arduino 1.6.4 running on Ubuntu

At first I thought that the native mode of the screen was portrait but if I draw to the screen using the sensed co-ords they do not correspond.
I used this sketch to prove my point:

// START SKETCH
#include <UTFT.h>
#include <HCTFT_Touch.h>

#define DCS_DIO 8
#define DCLK_DIO 15
#define DIN_DIO 14
#define DOUT_DIO 9
HCTFT_Touch myTouch(DCS_DIO, DCLK_DIO, DIN_DIO, DOUT_DIO);

#define RS_DIO A5
#define WR_DIO A4
#define CS_DIO A3
#define RST_DIO A2
UTFT myGLCD(ILI9325D_8, RS_DIO, WR_DIO, CS_DIO, RST_DIO);

int x, y;

void setup()
{
// Initial setup
myGLCD.InitLCD(); // landscape
myGLCD.clrScr();
myGLCD.setBackColor (0, 0, 0);
}

void loop()
{
if (myTouch.Pressed())
{
x = myTouch.GetCoord(XAXIS);
y = myTouch.GetCoord(YAXIS);
myGLCD.setColor(255, 255, 255);
myGLCD.fillCircle(x, y, 4);
}
} // END SKETCH

If using my finger I drag a horizontal line, I get a vertical line painted on the screen and vice-versa. I have not looked at the data sheet for this screen but would expect that the stated 240x320 resolution correspond with height x width (landscape) as the width is longer. But, the touch sensor implies that the height is 0-320 and the width is 0-240.

I have verified the result using my 2nd new screen and adapter. Using the supplied UTouchButtonTest in the UTFT library the printing co-ors do match my expectations , 240 high, 320 wide. Thanks in advance.

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCTFT_Touch - Touch Screen Library for 2.4" TFT Panel

Post by andrew » Mon May 18, 2015 9:07 am

It does look like something has changed. I'll take a look at it and let you know as soon I have a fix.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

admin
Site Admin
Posts: 866
Joined: Sun Aug 05, 2012 4:02 pm

Re: HCTFT_Touch - Touch Screen Library for 2.4" TFT Panel

Post by admin » Mon May 18, 2015 9:36 am

I've made an update to the library. You'll need to re-download it and replace your existing one. Could let me know if its fixed the problem and I'll make it a permanent update. Thanks.

vespapierre
Posts: 3
Joined: Sat Nov 14, 2015 1:22 pm

Re: HCTFT_Touch - Touch Screen Library TFT Touch screens

Post by vespapierre » Sat Nov 14, 2015 1:44 pm

Hi,
I am using MCUfriend 3.5 tft touch screen with mega board. I have finally found your libraries...At the touch screen example 2 the screens shows as like mirrored!! If I check the screen in a mirror it is fine :)
Can you help me in this?
Thanks!

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCTFT_Touch - Touch Screen Library TFT Touch screens

Post by andrew » Sat Nov 14, 2015 2:06 pm

We've not seen this issue before. If you didn't purchase it from us could you confirm that it defiantly is an MCUFriend branded display? Also could you tell me if it is mirrored about the horizontal or vertical axis?
Last edited by andrew on Mon Nov 16, 2015 10:56 am, edited 1 time in total.
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

vespapierre
Posts: 3
Joined: Sat Nov 14, 2015 1:22 pm

Re: HCTFT_Touch - Touch Screen Library TFT Touch screens

Post by vespapierre » Sat Nov 14, 2015 2:47 pm

Hi,
yes it is mcufriend branded, it looks as the same as yours in the shop!
This is in landscape... I think both direction...If a mirror is on left side of the screen, this can be read...Also if mirror is at the bottom of the screen
This is at touch example 2.
With draw example if I touch on the right bottom it shows left top!
Thanks!

andrew
Site Admin
Posts: 1374
Joined: Sun Aug 05, 2012 4:15 pm

Re: HCTFT_Touch - Touch Screen Library TFT Touch screens

Post by andrew » Mon Nov 16, 2015 11:01 am

Ok that will explain it. There are lots of different versions of MCUFriend displays out there with different chipsets. The library is written to work with the versions we sell only. Try this...


In the library file HCTFT.cpp find the MCUFriend section by looking for the following line:

Code: Select all

#ifdef MCUFRIEND_3_5_MEGA 
Then below it find the following lines:

Code: Select all

			_CommandOut(0x36); //Set_address_mode
			_DataOut(0x2A); //Horizontal flip, BGR order
And change the second line to:

Code: Select all

_DataOut(0xEA); //Horizontal flip, BGR order
Comments made by this poster do not necessarily reflect the views of Hobby Components Ltd.

Post Reply

Return to “Arduino”