Jan Kamidra / TS
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers TS.h Source File

TS.h

00001 /* TS library is for the 4-wire resistive Touch Screen (without an interface) 
00002 *  which is usually included on TFT LCD like are MCUFriend displays.
00003 *  The pins of the Touch Screen are shared with display interface and it is 
00004 *  not possible to read the TouchScreen and write to the display at the "same time".
00005 *  Also the TouchScreen coordinates must be read one by one and TouchScreen's pins must be
00006 *  configurted for reading for one Axis (X) and then for second one (Y).
00007 *
00008 *  TS library mabe by Jan Kamidra 28.6.2020 under Mbed and its tools (Mbed Studio online).
00009 */
00010 
00011 /** Example for testing 
00012 * @code 
00013 
00014 #include "mbed.h"
00015 #include "TS.h"
00016 
00017 #define YP A3
00018 #define YM D9
00019 #define XM A2
00020 #define XP D8
00021 #define RD A0
00022 
00023 TS_config config = {2955,325,3070,470,240,320};     // that are minimum and maxim mV for all axis  
00024 TouchScreen ts(YP,YM,XP,XM,config);
00025 
00026 int main()
00027 {
00028     printf("Example starting..\n");
00029     DigitalInOut(A0,PIN_OUTPUT,PushPullNoPull,1); //this line is necessary only without display driver
00030     DigitalOut led(LED1);
00031     TS_point point;
00032     //ts.setOrientation(TouchScreen::TS_PORTRAITE);
00033 
00034     while (true){
00035         if(ts.getPoint(&point)) printf("X: %d, Y: %d\n",point.x,point.y);
00036         led = !led;
00037         thread_sleep_for(1000);
00038     }
00039 }
00040 * @/code
00041 */
00042 
00043 #ifndef TS_H
00044 #define TS_H
00045 
00046 #include "TS_porting.h"
00047 
00048 struct TS_point{
00049     /** Coordinates for XY axis calculated for selected orientation*/
00050     int x = 0, y = 0;
00051 
00052     /** Coordinates in mV for XY plates of the TouchScreen (orientation is not included)*/
00053     int xV = 0, yV = 0;
00054 
00055     /** Check if the current point is equal to another point
00056     *
00057     * @param p The other point being checked for equivalence
00058     * @return  return true according to whether the points are equal else false
00059     */
00060     bool operator==(TS_point);
00061 
00062     /** Check if the current point is not equal to another point
00063     *
00064     * @param p The other point being checked for equivalence
00065     * @return  return false according to whether the points are not equal else true
00066     */
00067     bool operator!=(TS_point);
00068 };
00069 
00070 
00071 
00072 struct TS_config{
00073    unsigned int TS_adc_Xmax,    // max value for X in mV
00074                 TS_adc_Xmin,    // max value for X in mV
00075                 TS_adc_Ymax,    // max value for Y in mV
00076                 TS_adc_Ymin,    // min value for Y in mV
00077                 TFTWIDTH,       // Display width
00078                 TFTHEIGHT;      // Display height
00079 };
00080 
00081 
00082 class TouchScreen: protected TouchScreenPort {
00083     public:
00084         enum TS_Orientation{
00085             TS_PORTRAITE        = 0,
00086             TS_LANDSCAPE        = 1,
00087             TS_PORTRAITE_REV    = 2,
00088             TS_LANDSCAPE_REV    = 3
00089         }; 
00090 
00091         /** Constructor of a new Touch Screen object
00092         *
00093         * @param pinYP      Y+ pin of Y plate
00094         * @param pinYM      Y- pin of Y plate
00095         * @param pinXP      X+ pin of X plate
00096         * @param pinXM      M- pin of X plate
00097         * @param config     Necessary configuration via TS_config struct
00098         */
00099         TouchScreen(int pinYP,int pinYM, int pinXP, int pinXM, TS_config configuration);
00100 
00101         /** Set the orientation 
00102         *
00103         * @param orientation    It is set via the TS_Orientation enum
00104         *                       TS_PORTRAITE(default), TS_LANDSCAPE,TS_PORTRAITE_REV and TS_LANDSCAPE_REV 
00105         */
00106         void setOrientation(TS_Orientation orientation);
00107 
00108         /** Get coordinates XY via struct TS_point
00109         *
00110         * @param point  pointer to the TS_point where coordinates will be stored.
00111         * @return       return true if data are valid, else false.
00112         *               That mean if data are in range of TouchScren plates defined in TS_config.
00113         */
00114         bool getPoint(TS_point *point);
00115 
00116 
00117     private:
00118         enum Axis{
00119             setX    = 0,
00120             setY    = 1
00121         };
00122         enum Mode{
00123             pInput  = 0,
00124             pOutput = 1
00125         };
00126         int             pYP, pYM, pXP, pXM;
00127         TS_config       conf;
00128         TS_Orientation  ts_orient   = TS_PORTRAITE;
00129         float           pxX, pxY;
00130         /** Pins setting for measuring on the plates (X/Y) or for releasing pins for further use.*/
00131         int     getCoords(Axis axis);
00132         /** ADC X value normalization*/
00133         float   nrm_adcX(unsigned int x);
00134         /** ADC Y value normalization*/
00135         float   nrm_adcY(unsigned int y);
00136 };
00137 #endif