Arduino 2.8 inch Resistive TFT-Touch screen uses Flip, Rotate and Call-Back funtions.
Latest change 2016-06-12
Reactions to ....... or ......... You have to type this, no cut&paste.
The Libraries I found for the 2.8 inch. 240x320 pixel TFT screen with resistive touch features did not satisfy my needs.
I present here a modified and extended library which allows rotating and flipping the coordiate system and a neat, generalized way to do the book-keeping for the touch-areas using callback functions.
Click if you want to use the TFT-Touchscreen with an Arduino NANO
I have 2 demo projects here, one is a half finished Graphical User Interface (GUI) for a tangential vinyl record player I am building, the other is an Alarm clock to remind me to take my medicine.
They both make use of the rotation / flipping of the coordinate system and the use of callback functions for the touch-screen bookkeeping.
Download the .zip packed demo project, and unzip it in a suitable directory.
It contains 3 directories: FW_PillAlarm, FW_TangentPlayer and SeeedTouch.
Copy the SeeedTouch directory as is to your .../Arduino<version>/Libraries directory
Verify that you do not have the TFTv2.cpp and TFTv2.h files anywhere in your .../Arduino<version>/Libraries directory. These files need to know certain properties of your project and so need to be in the project directory.
The demo directories contain:
- FW_TangentPlayer.ino. or FW_PillAlarm, the main file. The other files are identical in the two demo directories.
- TFTv2.cpp and TFTv2.h. These files are the slightly modified original files for the 2.8" display. Do not put these files in your .../Arduino<version>/libraries/ directory, that will not work, you have to keep them in your project directory only.
- LibTFT2p8.cpp and LibTFT2p8.h These form a kind of "shell" around the TFTv2 library to allow to rotate and flip the coordinate system. Also some shapes like line, rectangle and circle can be drawn with adjustable line-thickness.
- Simplefont.h. This is the original Simplefont character map, modified by me to include all combinations of flip and rotate.
- controls.cpp and controls.h. Contains drawing functions for some controls I use in my application(s). You may need other controls, just add or modify them.
Some guidelines for using this library:
Set the desired flip and rotate parameters in the file LibTFT2p8.h by un- / commenting the #defines for FLIP and ROTATE.
Note that changeing the flip/rotate status at runtime is not supported. It would take a lot of extra code and execution time, so I dit not implement it that way.
Do not call drawing functions from within an interrupt service routine. Because the drawing functions themselves partly use interrupts, strange behaviour and hang-ups can be expected. In stead, if you really need to (re)draw from inside an interrupt routine, just set a flag in your interrupt routine, and test that flag in the "loop" and call the drawing routines from there.
If you also use Ethernet or (not tested by me) SD-card facilities there will/may be a conflict on the SPI-bus. I found that not connecting the pin on which the TFT talks (MISO) resolves the problem. Screens do not need to talk back as you may expect.
The TFT initialisation produces some serial output. Just ignore that or comment it out.
Drawing and redrawing controls: using the files "controls.cpp" and "controls.h"
Calling one of these functions draws the control on the specified location and -when specified- enters the touch sensitive area and a pointer to a function into the TouchList.
The pointer points to the function you want to have called when the associated area is touched.
Such functions must be void, they cannot accept or return any value.
(C/C++ does allow function pointers to non-void functions, but that is not supported here)
Set the "#define NUMBEROFTOUCHAREAS in LibTFT2p8.h" to the largest number of touch areas you want to use in any screen layout.
Set the boolean "EnterInTouchList" to true only for the first time you draw a control. If a control has to be redrawn
with only different color(s) set "EnterInTouchList" to false to prevent a new entry into the TouchList.
If you need to change the position or the associated callback function for any control then you need to
use "ClearScreen()" and redraw the whole screen. "ClearScreen()" also clears the TouchList.
If a control has to be redrawn with a different shape then first clear the whole area of the control.
Put a statement like "int dummy = tft_DetectTouch();" in the "loop" of your .ino file. This calls the callback function.
Take care that "tft_DetectTouch()" is not called to often, this will result in multiple calls during one touch.
You may need a time-out mechanism to prevent such multiple callback's. This library does not provide such.
A "delay(some_time);" in your "loop" may do the job.
If you need to use different pins for the touch function be shure that all 4 pins must be able to be digital outputs, while 2 pins must be able be analog inputs.
So using analog only inputs like A6 and A7 on the ATmega328 will not work.
Note that the #define's for the 4 touch pins in file TFTv2.h are overruled by SeeedTouchScreen.h in the .../libraries/SeeedTouch/
So if you need to use other pins move that file to your project directory, make the change there, and use #include "SeeedTouchScreen.h" in stead of #include <SeeedTouchScreen.h>
The example projects demonstrate the use of certain subsets of these controls.
Feel free to modify this library for your needs, create more controls etc.
This library is / these files are published without any guarantee for proper operation or absence of side-effects.