brw1

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RA8875_Touch.cpp Source File

RA8875_Touch.cpp

00001 /// This file contains the RA8875 Touch panel methods.
00002 ///
00003 
00004 #include "RA8875.h"
00005 
00006 // ### Touch Panel support code additions begin here
00007 
00008 RetCode_t RA8875::TouchPanelInit(void)
00009 {
00010     //TPCR0: Set enable bit, default sample time, wakeup, and ADC clock
00011     WriteCommand(TPCR0, TP_ENABLE | TP_ADC_SAMPLE_DEFAULT_CLKS | TP_ADC_CLKDIV_DEFAULT);
00012     // TPCR1: Set auto/manual, Ref voltage, debounce, manual mode params
00013     WriteCommand(TPCR1, TP_MODE_DEFAULT | TP_DEBOUNCE_DEFAULT);
00014     WriteCommand(INTC1, ReadCommand(INTC1) | RA8875_INT_TP);        // reg INTC1: Enable Touch Panel Interrupts (D2 = 1)
00015     WriteCommand(INTC2, RA8875_INT_TP);                            // reg INTC2: Clear any TP interrupt flag
00016     return noerror;
00017 }
00018 
00019 RetCode_t RA8875::TouchPanelInit(uint8_t bTpEnable, uint8_t bTpAutoManual, uint8_t bTpDebounce, uint8_t bTpManualMode, uint8_t bTpAdcClkDiv, uint8_t bTpAdcSampleTime)
00020 {
00021     // Parameter bounds check
00022     if( \
00023             !(bTpEnable == TP_ENABLE || bTpEnable == TP_ENABLE) || \
00024             !(bTpAutoManual == TP_MODE_AUTO || bTpAutoManual == TP_MODE_MANUAL) || \
00025             !(bTpDebounce == TP_DEBOUNCE_OFF || bTpDebounce == TP_DEBOUNCE_ON) || \
00026             !(bTpManualMode <= TP_MANUAL_LATCH_Y) || \
00027             !(bTpAdcClkDiv <= TP_ADC_CLKDIV_128) || \
00028             !(bTpAdcSampleTime <= TP_ADC_SAMPLE_65536_CLKS) \
00029       ) return bad_parameter;
00030     // Construct the config byte for TPCR0 and write them
00031     WriteCommand(TPCR0, bTpEnable | bTpAdcClkDiv | bTpAdcSampleTime);    // Note: Wakeup is never enabled
00032     // Construct the config byte for TPCR1 and write them
00033     WriteCommand(TPCR1, bTpManualMode | bTpDebounce | bTpManualMode);    // Note: Always uses internal Vref.
00034     // Set up the interrupt flag and enable bits
00035     WriteCommand(INTC1, ReadCommand(INTC1) | RA8875_INT_TP);        // reg INTC1: Enable Touch Panel Interrupts (D2 = 1)
00036     WriteCommand(INTC2, RA8875_INT_TP);                            // reg INTC2: Clear any TP interrupt flag
00037     return noerror;
00038 }
00039 
00040     // +----------------------------------------------------+
00041     // |                                                    |
00042     // |  1                                                 |
00043     // |                                                    |
00044     // |                                                    |
00045     // |                                               2    |
00046     // |                                                    |
00047     // |                                                    |
00048     // |                         3                          |
00049     // |                                                    |
00050     // +----------------------------------------------------+
00051 
00052 
00053 RetCode_t RA8875::TouchPanelCalibrate(tpMatrix_t * matrix)
00054 {
00055     return TouchPanelCalibrate(NULL, matrix);
00056 }
00057 
00058 
00059 RetCode_t RA8875::TouchPanelCalibrate(const char * msg, tpMatrix_t * matrix)
00060 {
00061     point_t pTest[3];
00062     point_t pSample[3];
00063     loc_t x,y;
00064     
00065     cls();
00066     if (msg)
00067         puts(msg);
00068     SetTextCursor(0,height()/2);
00069     pTest[0].x = 50;            pTest[0].y = 50;
00070     pTest[1].x = width() - 50;  pTest[1].y = height()/2;
00071     pTest[2].x = width()/2;     pTest[2].y = height() - 50;
00072     for (int i=0; i<3; i++) {
00073         foreground(Blue);
00074         printf(" (%3d,%3d) => ", pTest[i].x, pTest[i].y);
00075         line(pTest[i].x-10, pTest[i].y, pTest[i].x+10, pTest[i].y, White);
00076         line(pTest[i].x, pTest[i].y-10, pTest[i].x, pTest[i].y+10, White);
00077         while (!TouchPanelA2DFiltered(&x, &y))
00078             wait_ms(20);
00079         pSample[i].x = x;
00080         pSample[i].y = y;
00081         line(pTest[i].x-10, pTest[i].y, pTest[i].x+10, pTest[i].y, Black);
00082         line(pTest[i].x, pTest[i].y-10, pTest[i].x, pTest[i].y+10, Black);
00083         foreground(Blue);
00084         printf(" (%4d,%4d)\r\n", x,y);
00085         while (TouchPanelA2DFiltered(&x, &y))
00086             wait_ms(20);
00087         wait(2);
00088     }
00089     return TouchPanelComputeCalibration(pTest, pSample, matrix);
00090 }
00091 
00092 
00093 /**********************************************************************
00094  *
00095  *     Function: getDisplayPoint()
00096  *
00097  *  Description: Given a valid set of calibration factors and a point
00098  *                value reported by the touch screen, this function
00099  *                calculates and returns the true (or closest to true)
00100  *                display point below the spot where the touch screen 
00101  *                was touched.
00102  * 
00103  *
00104  * 
00105  *  Argument(s): displayPtr (output) - Pointer to the calculated
00106  *                                      (true) display point.
00107  *               screenPtr (input) - Pointer to the reported touch
00108  *                                    screen point.
00109  *               matrixPtr (input) - Pointer to calibration factors
00110  *                                    matrix previously calculated
00111  *                                    from a call to 
00112  *                                    setCalibrationMatrix()
00113  * 
00114  *
00115  *  The function simply solves for Xd and Yd by implementing the 
00116  *   computations required by the translation matrix.  
00117  * 
00118  *                                              /-     -\
00119  *              /-    -\     /-            -\   |       |
00120  *              |      |     |              |   |   Xs  |
00121  *              |  Xd  |     | A    B    C  |   |       |
00122  *              |      |  =  |              | * |   Ys  |
00123  *              |  Yd  |     | D    E    F  |   |       |
00124  *              |      |     |              |   |   1   |
00125  *              \-    -/     \-            -/   |       |
00126  *                                              \-     -/
00127  * 
00128  *  It must be kept brief to avoid consuming CPU cycles.
00129  *
00130  *       Return: OK - the display point was correctly calculated 
00131  *                     and its value is in the output argument.
00132  *               NOT_OK - an error was detected and the function
00133  *                         failed to return a valid point.
00134  *
00135  *                 NOTE!    NOTE!    NOTE!
00136  *
00137  *  setCalibrationMatrix() and getDisplayPoint() will do fine
00138  *  for you as they are, provided that your digitizer         
00139  *  resolution does not exceed 10 bits (1024 values).  Higher
00140  *  resolutions may cause the integer operations to overflow
00141  *  and return incorrect values.  If you wish to use these   
00142  *  functions with digitizer resolutions of 12 bits (4096    
00143  *  values) you will either have to a) use 64-bit signed     
00144  *  integer variables and math, or b) judiciously modify the 
00145  *  operations to scale results by a factor of 2 or even 4.  
00146  *
00147  */
00148 bool RA8875::TouchPanelReadable(point_t * TouchPoint)
00149 {
00150     bool touched = false;
00151     point_t screenpoint = {0, 0};
00152     
00153     if (TouchPanelA2DFiltered(&screenpoint.x, &screenpoint.y)) {
00154         touched = true;
00155         if (tpMatrix.Divider != 0 && TouchPoint) {
00156             /* Operation order is important since we are doing integer */
00157             /*  math. Make sure you add all terms together before      */
00158             /*  dividing, so that the remainder is not rounded off     */
00159             /*  prematurely.                                           */
00160             TouchPoint->x = ( (tpMatrix.An * screenpoint.x) +
00161                               (tpMatrix.Bn * screenpoint.y) +
00162                               tpMatrix.Cn
00163                             ) / tpMatrix.Divider ;
00164 
00165             TouchPoint->y = ( (tpMatrix.Dn * screenpoint.x) +
00166                               (tpMatrix.En * screenpoint.y) +
00167                               tpMatrix.Fn
00168                             ) / tpMatrix.Divider ;
00169         } else {
00170             touched = false;
00171         }
00172     }
00173     return touched;
00174 }
00175 
00176 
00177 RetCode_t RA8875::TouchPanelSetMatrix(tpMatrix_t * matrixPtr)
00178 {
00179     if (matrixPtr == NULL || matrixPtr->Divider == 0)
00180         return bad_parameter;
00181     memcpy(&tpMatrix, matrixPtr, sizeof(tpMatrix_t));
00182     return noerror;
00183 }
00184 
00185 
00186 bool RA8875::TouchPanelA2DFiltered(loc_t *x, loc_t *y)
00187 {
00188     unsigned char touchready;
00189     static int xbuf[TPBUFSIZE], ybuf[TPBUFSIZE], sample = 0;
00190     int i, j, temp;
00191 
00192     if( (ReadCommand(INTC2) & RA8875_INT_TP) ) {        // Test for TP Interrupt pending in register INTC2
00193         // Get the next data samples
00194         ybuf[sample] =  ReadCommand(TPYH) << 2 | ( (ReadCommand(TPXYL) & 0xC) >> 2 );   // D[9:2] from reg TPYH, D[1:0] from reg TPXYL[3:2]
00195         xbuf[sample] =  ReadCommand(TPXH) << 2 | ( (ReadCommand(TPXYL) & 0x3)      );   // D[9:2] from reg TPXH, D[1:0] from reg TPXYL[1:0]
00196         // Check for a complete set
00197         if(++sample == TPBUFSIZE) {
00198             // Buffers are full, so process them using Finn's method described in Analog Dialogue No. 44, Feb 2010
00199             // This requires sorting the samples in order of size, then discarding the top 25% and
00200             //   bottom 25% as noise spikes. Finally, the middle 50% of the values are averaged to
00201             //   reduce Gaussian noise.
00202 
00203             // Sort the Y buffer using an Insertion Sort
00204             for(i = 1; i <= TPBUFSIZE; i++) {
00205                 temp = ybuf[i];
00206                 j = i;
00207                 while( j && (ybuf[j-1] > temp) ) {
00208                     ybuf[j] = ybuf[j-1];
00209                     j = j-1;
00210                 }
00211                 ybuf[j] = temp;
00212             } // End of Y sort
00213             // Sort the X buffer the same way
00214             for(i = 1; i <= TPBUFSIZE; i++) {
00215                 temp = xbuf[i];
00216                 j = i;
00217                 while( j && (xbuf[j-1] > temp) ) {
00218                     xbuf[j] = xbuf[j-1];
00219                     j = j-1;
00220                 }
00221                 xbuf[j] = temp;
00222             } // End of X sort
00223             // Average the middle half of the  Y values and report them
00224             j = 0;
00225             for(i = (TPBUFSIZE/4) - 1; i < TPBUFSIZE - TPBUFSIZE/4; i++ ) {
00226                 j += ybuf[i];
00227             }
00228             *y = j * (float)2/TPBUFSIZE;    // This is the average
00229             // Average the middle half of the  X values and report them
00230             j = 0;
00231             for(i = (TPBUFSIZE/4) - 1; i < TPBUFSIZE - TPBUFSIZE/4; i++ ) {
00232                 j += xbuf[i];
00233             }
00234             *x = j * (float)2/TPBUFSIZE;    // This is the average
00235             // Tidy up and return
00236             touchready = 1;
00237             sample = 0;             // Ready to start on the next set of data samples
00238         } else {
00239             // Buffer not yet full, so do not return any results yet
00240             touchready = 0;
00241         }
00242         WriteCommand(INTC2, RA8875_INT_TP);            // reg INTC2: Clear that TP interrupt flag
00243     } // End of initial if -- data has been read and processed
00244     else
00245         touchready = 0;         // Touch Panel "Int" was not set
00246     return touchready;
00247 }
00248 
00249 bool RA8875::TouchPanelA2DRaw(loc_t *x, loc_t *y)
00250 {
00251     unsigned char touchready;
00252 
00253     if( (ReadCommand(INTC2) & RA8875_INT_TP) ) {        // Test for TP Interrupt pending in register INTC2
00254         *y =  ReadCommand(TPYH) << 2 | ( (ReadCommand(TPXYL) & 0xC) >> 2 );   // D[9:2] from reg TPYH, D[1:0] from reg TPXYL[3:2]
00255         *x =  ReadCommand(TPXH) << 2 | ( (ReadCommand(TPXYL) & 0x3)      );   // D[9:2] from reg TPXH, D[1:0] from reg TPXYL[1:0]
00256         WriteCommand(INTC2, RA8875_INT_TP);            // reg INTC2: Clear that TP interrupt flag
00257         touchready = 1;
00258     } else
00259         touchready = 0;
00260     return touchready;
00261 }
00262 
00263 /*   The following section is derived from Carlos E. Vidales.
00264  *
00265  *   Copyright (c) 2001, Carlos E. Vidales. All rights reserved.
00266  *
00267  *   This sample program was written and put in the public domain 
00268  *    by Carlos E. Vidales.  The program is provided "as is" 
00269  *    without warranty of any kind, either expressed or implied.
00270  *   If you choose to use the program within your own products
00271  *    you do so at your own risk, and assume the responsibility
00272  *    for servicing, repairing or correcting the program should
00273  *    it prove defective in any manner.
00274  *   You may copy and distribute the program's source code in any 
00275  *    medium, provided that you also include in each copy an
00276  *    appropriate copyright notice and disclaimer of warranty.
00277  *   You may also modify this program and distribute copies of
00278  *    it provided that you include prominent notices stating 
00279  *    that you changed the file(s) and the date of any change,
00280  *    and that you do not charge any royalties or licenses for 
00281  *    its use.
00282  * 
00283  *   This file contains functions that implement calculations 
00284  *    necessary to obtain calibration factors for a touch screen
00285  *    that suffers from multiple distortion effects: namely, 
00286  *    translation, scaling and rotation.
00287  *
00288  *   The following set of equations represent a valid display 
00289  *    point given a corresponding set of touch screen points:
00290  *
00291  *                                              /-     -\
00292  *              /-    -\     /-            -\   |       |
00293  *              |      |     |              |   |   Xs  |
00294  *              |  Xd  |     | A    B    C  |   |       |
00295  *              |      |  =  |              | * |   Ys  |
00296  *              |  Yd  |     | D    E    F  |   |       |
00297  *              |      |     |              |   |   1   |
00298  *              \-    -/     \-            -/   |       |
00299  *                                              \-     -/
00300  *    where:
00301  *           (Xd,Yd) represents the desired display point 
00302  *                    coordinates,
00303  *           (Xs,Ys) represents the available touch screen
00304  *                    coordinates, and the matrix
00305  *           /-   -\
00306  *           |A,B,C|
00307  *           |D,E,F| represents the factors used to translate
00308  *           \-   -/  the available touch screen point values
00309  *                    into the corresponding display 
00310  *                    coordinates.
00311  *    Note that for practical considerations, the utilities 
00312  *     within this file do not use the matrix coefficients as
00313  *     defined above, but instead use the following 
00314  *     equivalents, since floating point math is not used:
00315  *            A = An/Divider 
00316  *            B = Bn/Divider 
00317  *            C = Cn/Divider 
00318  *            D = Dn/Divider 
00319  *            E = En/Divider 
00320  *            F = Fn/Divider 
00321  *    The functions provided within this file are:
00322  *          setCalibrationMatrix() - calculates the set of factors
00323  *                                    in the above equation, given
00324  *                                    three sets of test points.
00325  *               getDisplayPoint() - returns the actual display
00326  *                                    coordinates, given a set of
00327  *                                    touch screen coordinates.
00328  * translateRawScreenCoordinates() - helper function to transform
00329  *                                    raw screen points into values
00330  *                                    scaled to the desired display
00331  *                                    resolution.
00332  */
00333 
00334 /**********************************************************************
00335  *
00336  *     Function: setCalibrationMatrix()
00337  *
00338  *  Description: Calling this function with valid input data
00339  *                in the display and screen input arguments 
00340  *                causes the calibration factors between the
00341  *                screen and display points to be calculated,
00342  *                and the output argument - matrixPtr - to be 
00343  *                populated.
00344  *
00345  *               This function needs to be called only when new
00346  *                calibration factors are desired.
00347  *               
00348  *  
00349  *  Argument(s): displayPtr (input) - Pointer to an array of three 
00350  *                                     sample, reference points.
00351  *               screenPtr (input) - Pointer to the array of touch 
00352  *                                    screen points corresponding 
00353  *                                    to the reference display points.
00354  *               matrixPtr (output) - Pointer to the calibration 
00355  *                                     matrix computed for the set 
00356  *                                     of points being provided.
00357  *
00358  *
00359  *  From the article text, recall that the matrix coefficients are
00360  *   resolved to be the following:
00361  *
00362  *
00363  *      Divider =  (Xs0 - Xs2)*(Ys1 - Ys2) - (Xs1 - Xs2)*(Ys0 - Ys2)
00364  *
00365  *
00366  *
00367  *                 (Xd0 - Xd2)*(Ys1 - Ys2) - (Xd1 - Xd2)*(Ys0 - Ys2)
00368  *            A = ---------------------------------------------------
00369  *                                   Divider
00370  *
00371  *
00372  *                 (Xs0 - Xs2)*(Xd1 - Xd2) - (Xd0 - Xd2)*(Xs1 - Xs2)
00373  *            B = ---------------------------------------------------
00374  *                                   Divider
00375  *
00376  *
00377  *                 Ys0*(Xs2*Xd1 - Xs1*Xd2) + 
00378  *                             Ys1*(Xs0*Xd2 - Xs2*Xd0) + 
00379  *                                           Ys2*(Xs1*Xd0 - Xs0*Xd1)
00380  *            C = ---------------------------------------------------
00381  *                                   Divider
00382  *
00383  *
00384  *                 (Yd0 - Yd2)*(Ys1 - Ys2) - (Yd1 - Yd2)*(Ys0 - Ys2)
00385  *            D = ---------------------------------------------------
00386  *                                   Divider
00387  *
00388  *
00389  *                 (Xs0 - Xs2)*(Yd1 - Yd2) - (Yd0 - Yd2)*(Xs1 - Xs2)
00390  *            E = ---------------------------------------------------
00391  *                                   Divider
00392  *
00393  *
00394  *                 Ys0*(Xs2*Yd1 - Xs1*Yd2) + 
00395  *                             Ys1*(Xs0*Yd2 - Xs2*Yd0) + 
00396  *                                           Ys2*(Xs1*Yd0 - Xs0*Yd1)
00397  *            F = ---------------------------------------------------
00398  *                                   Divider
00399  *
00400  *
00401  *       Return: OK - the calibration matrix was correctly 
00402  *                     calculated and its value is in the 
00403  *                     output argument.
00404  *               NOT_OK - an error was detected and the 
00405  *                         function failed to return a valid
00406  *                         set of matrix values.
00407  *                        The only time this sample code returns
00408  *                        NOT_OK is when Divider == 0
00409  *
00410  *
00411  *
00412  *                 NOTE!    NOTE!    NOTE!
00413  *
00414  *  setCalibrationMatrix() and getDisplayPoint() will do fine
00415  *  for you as they are, provided that your digitizer         
00416  *  resolution does not exceed 10 bits (1024 values).  Higher
00417  *  resolutions may cause the integer operations to overflow
00418  *  and return incorrect values.  If you wish to use these   
00419  *  functions with digitizer resolutions of 12 bits (4096    
00420  *  values) you will either have to a) use 64-bit signed     
00421  *  integer variables and math, or b) judiciously modify the 
00422  *  operations to scale results by a factor of 2 or even 4.  
00423  *
00424  */
00425 RetCode_t RA8875::TouchPanelComputeCalibration(point_t * displayPtr, point_t * screenPtr, tpMatrix_t * matrixPtr)
00426 {
00427     RetCode_t retValue = noerror;
00428 
00429     tpMatrix.Divider = ((screenPtr[0].x - screenPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
00430                        ((screenPtr[1].x - screenPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
00431 
00432     if( tpMatrix.Divider == 0 )  {
00433         retValue = bad_parameter;
00434     }  else   {
00435         tpMatrix.An = ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
00436                       ((displayPtr[1].x - displayPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
00437 
00438         tpMatrix.Bn = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].x - displayPtr[2].x)) -
00439                       ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].x - screenPtr[2].x)) ;
00440 
00441         tpMatrix.Cn = (screenPtr[2].x * displayPtr[1].x - screenPtr[1].x * displayPtr[2].x) * screenPtr[0].y +
00442                       (screenPtr[0].x * displayPtr[2].x - screenPtr[2].x * displayPtr[0].x) * screenPtr[1].y +
00443                       (screenPtr[1].x * displayPtr[0].x - screenPtr[0].x * displayPtr[1].x) * screenPtr[2].y ;
00444 
00445         tpMatrix.Dn = ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].y - screenPtr[2].y)) -
00446                       ((displayPtr[1].y - displayPtr[2].y) * (screenPtr[0].y - screenPtr[2].y)) ;
00447 
00448         tpMatrix.En = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].y - displayPtr[2].y)) -
00449                       ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].x - screenPtr[2].x)) ;
00450 
00451         tpMatrix.Fn = (screenPtr[2].x * displayPtr[1].y - screenPtr[1].x * displayPtr[2].y) * screenPtr[0].y +
00452                       (screenPtr[0].x * displayPtr[2].y - screenPtr[2].x * displayPtr[0].y) * screenPtr[1].y +
00453                       (screenPtr[1].x * displayPtr[0].y - screenPtr[0].x * displayPtr[1].y) * screenPtr[2].y ;
00454         if (matrixPtr)
00455             memcpy(matrixPtr, &tpMatrix, sizeof(tpMatrix_t));
00456     }
00457     return( retValue ) ;
00458 }
00459 
00460 // #### end of touch panel code additions