Increased SPI frequency from 5Mhz to 10MHz

Fork of RA8875 by David Smart

Revision:
77:9206c13aa527
Parent:
75:ca78388cfd77
Child:
78:faf49c381591
--- a/RA8875.cpp	Fri Nov 28 22:53:35 2014 +0000
+++ b/RA8875.cpp	Fri Dec 26 21:34:28 2014 +0000
@@ -70,13 +70,12 @@
 #define POLLWAITuSec 10
 
 // Private RawKeyMap for the Keyboard interface
-static const uint8_t KeyMap[22] = 
-    {
-     0, 
-     1,  2,  3,  4,  5,  6,  7,  8,  9, 10,
+static const uint8_t KeyMap[22] = {
+    0,
+    1,  2,  3,  4,  5,  6,  7,  8,  9, 10,
     11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
     255
-    };
+};
 
 
 RA8875::RA8875(PinName mosi, PinName miso, PinName sclk, PinName csel, PinName reset, const char *name)
@@ -272,14 +271,307 @@
         touchready = 0;
     return touchready;
 }
+
+/*   The following section is derived from Carlos E. Vidales.
+ *
+ *   Copyright (c) 2001, Carlos E. Vidales. All rights reserved.
+ *
+ *   This sample program was written and put in the public domain 
+ *    by Carlos E. Vidales.  The program is provided "as is" 
+ *    without warranty of any kind, either expressed or implied.
+ *   If you choose to use the program within your own products
+ *    you do so at your own risk, and assume the responsibility
+ *    for servicing, repairing or correcting the program should
+ *    it prove defective in any manner.
+ *   You may copy and distribute the program's source code in any 
+ *    medium, provided that you also include in each copy an
+ *    appropriate copyright notice and disclaimer of warranty.
+ *   You may also modify this program and distribute copies of
+ *    it provided that you include prominent notices stating 
+ *    that you changed the file(s) and the date of any change,
+ *    and that you do not charge any royalties or licenses for 
+ *    its use.
+ * 
+ *   This file contains functions that implement calculations 
+ *    necessary to obtain calibration factors for a touch screen
+ *    that suffers from multiple distortion effects: namely, 
+ *    translation, scaling and rotation.
+ *
+ *   The following set of equations represent a valid display 
+ *    point given a corresponding set of touch screen points:
+ *
+ *                                              /-     -\
+ *              /-    -\     /-            -\   |       |
+ *              |      |     |              |   |   Xs  |
+ *              |  Xd  |     | A    B    C  |   |       |
+ *              |      |  =  |              | * |   Ys  |
+ *              |  Yd  |     | D    E    F  |   |       |
+ *              |      |     |              |   |   1   |
+ *              \-    -/     \-            -/   |       |
+ *                                              \-     -/
+ *    where:
+ *           (Xd,Yd) represents the desired display point 
+ *                    coordinates,
+ *           (Xs,Ys) represents the available touch screen
+ *                    coordinates, and the matrix
+ *           /-   -\
+ *           |A,B,C|
+ *           |D,E,F| represents the factors used to translate
+ *           \-   -/  the available touch screen point values
+ *                    into the corresponding display 
+ *                    coordinates.
+ *    Note that for practical considerations, the utilities 
+ *     within this file do not use the matrix coefficients as
+ *     defined above, but instead use the following 
+ *     equivalents, since floating point math is not used:
+ *            A = An/Divider 
+ *            B = Bn/Divider 
+ *            C = Cn/Divider 
+ *            D = Dn/Divider 
+ *            E = En/Divider 
+ *            F = Fn/Divider 
+ *    The functions provided within this file are:
+ *          setCalibrationMatrix() - calculates the set of factors
+ *                                    in the above equation, given
+ *                                    three sets of test points.
+ *               getDisplayPoint() - returns the actual display
+ *                                    coordinates, given a set of
+ *                                    touch screen coordinates.
+ * translateRawScreenCoordinates() - helper function to transform
+ *                                    raw screen points into values
+ *                                    scaled to the desired display
+ *                                    resolution.
+ */
+
+/**********************************************************************
+ *
+ *     Function: setCalibrationMatrix()
+ *
+ *  Description: Calling this function with valid input data
+ *                in the display and screen input arguments 
+ *                causes the calibration factors between the
+ *                screen and display points to be calculated,
+ *                and the output argument - matrixPtr - to be 
+ *                populated.
+ *
+ *               This function needs to be called only when new
+ *                calibration factors are desired.
+ *               
+ *  
+ *  Argument(s): displayPtr (input) - Pointer to an array of three 
+ *                                     sample, reference points.
+ *               screenPtr (input) - Pointer to the array of touch 
+ *                                    screen points corresponding 
+ *                                    to the reference display points.
+ *               matrixPtr (output) - Pointer to the calibration 
+ *                                     matrix computed for the set 
+ *                                     of points being provided.
+ *
+ *
+ *  From the article text, recall that the matrix coefficients are
+ *   resolved to be the following:
+ *
+ *
+ *      Divider =  (Xs0 - Xs2)*(Ys1 - Ys2) - (Xs1 - Xs2)*(Ys0 - Ys2)
+ *
+ *
+ *
+ *                 (Xd0 - Xd2)*(Ys1 - Ys2) - (Xd1 - Xd2)*(Ys0 - Ys2)
+ *            A = ---------------------------------------------------
+ *                                   Divider
+ *
+ *
+ *                 (Xs0 - Xs2)*(Xd1 - Xd2) - (Xd0 - Xd2)*(Xs1 - Xs2)
+ *            B = ---------------------------------------------------
+ *                                   Divider
+ *
+ *
+ *                 Ys0*(Xs2*Xd1 - Xs1*Xd2) + 
+ *                             Ys1*(Xs0*Xd2 - Xs2*Xd0) + 
+ *                                           Ys2*(Xs1*Xd0 - Xs0*Xd1)
+ *            C = ---------------------------------------------------
+ *                                   Divider
+ *
+ *
+ *                 (Yd0 - Yd2)*(Ys1 - Ys2) - (Yd1 - Yd2)*(Ys0 - Ys2)
+ *            D = ---------------------------------------------------
+ *                                   Divider
+ *
+ *
+ *                 (Xs0 - Xs2)*(Yd1 - Yd2) - (Yd0 - Yd2)*(Xs1 - Xs2)
+ *            E = ---------------------------------------------------
+ *                                   Divider
+ *
+ *
+ *                 Ys0*(Xs2*Yd1 - Xs1*Yd2) + 
+ *                             Ys1*(Xs0*Yd2 - Xs2*Yd0) + 
+ *                                           Ys2*(Xs1*Yd0 - Xs0*Yd1)
+ *            F = ---------------------------------------------------
+ *                                   Divider
+ *
+ *
+ *       Return: OK - the calibration matrix was correctly 
+ *                     calculated and its value is in the 
+ *                     output argument.
+ *               NOT_OK - an error was detected and the 
+ *                         function failed to return a valid
+ *                         set of matrix values.
+ *                        The only time this sample code returns
+ *                        NOT_OK is when Divider == 0
+ *
+ *
+ *
+ *                 NOTE!    NOTE!    NOTE!
+ *
+ *  setCalibrationMatrix() and getDisplayPoint() will do fine
+ *  for you as they are, provided that your digitizer         
+ *  resolution does not exceed 10 bits (1024 values).  Higher
+ *  resolutions may cause the integer operations to overflow
+ *  and return incorrect values.  If you wish to use these   
+ *  functions with digitizer resolutions of 12 bits (4096    
+ *  values) you will either have to a) use 64-bit signed     
+ *  integer variables and math, or b) judiciously modify the 
+ *  operations to scale results by a factor of 2 or even 4.  
+ *
+ */
+RetCode_t RA8875::TouchPanelCalibrate(point_t * displayPtr, point_t * screenPtr, tpMatrix_t * matrixPtr)
+{
+    RetCode_t retValue = noerror;
+
+    tpMatrix.Divider = ((screenPtr[0].x - screenPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
+                       ((screenPtr[1].x - screenPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
+
+    if( tpMatrix.Divider == 0 )  {
+        retValue = bad_parameter;
+    }  else   {
+        tpMatrix.An = ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].y - screenPtr[2].y)) -
+                      ((displayPtr[1].x - displayPtr[2].x) * (screenPtr[0].y - screenPtr[2].y)) ;
+
+        tpMatrix.Bn = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].x - displayPtr[2].x)) -
+                      ((displayPtr[0].x - displayPtr[2].x) * (screenPtr[1].x - screenPtr[2].x)) ;
+
+        tpMatrix.Cn = (screenPtr[2].x * displayPtr[1].x - screenPtr[1].x * displayPtr[2].x) * screenPtr[0].y +
+                      (screenPtr[0].x * displayPtr[2].x - screenPtr[2].x * displayPtr[0].x) * screenPtr[1].y +
+                      (screenPtr[1].x * displayPtr[0].x - screenPtr[0].x * displayPtr[1].x) * screenPtr[2].y ;
+
+        tpMatrix.Dn = ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].y - screenPtr[2].y)) -
+                      ((displayPtr[1].y - displayPtr[2].y) * (screenPtr[0].y - screenPtr[2].y)) ;
+
+        tpMatrix.En = ((screenPtr[0].x - screenPtr[2].x) * (displayPtr[1].y - displayPtr[2].y)) -
+                      ((displayPtr[0].y - displayPtr[2].y) * (screenPtr[1].x - screenPtr[2].x)) ;
+
+        tpMatrix.Fn = (screenPtr[2].x * displayPtr[1].y - screenPtr[1].x * displayPtr[2].y) * screenPtr[0].y +
+                      (screenPtr[0].x * displayPtr[2].y - screenPtr[2].x * displayPtr[0].y) * screenPtr[1].y +
+                      (screenPtr[1].x * displayPtr[0].y - screenPtr[0].x * displayPtr[1].y) * screenPtr[2].y ;
+        if (matrixPtr)
+            memcpy(matrixPtr, &tpMatrix, sizeof(tpMatrix_t));
+    }
+    return( retValue ) ;
+}
+
+/**********************************************************************
+ *
+ *     Function: getDisplayPoint()
+ *
+ *  Description: Given a valid set of calibration factors and a point
+ *                value reported by the touch screen, this function
+ *                calculates and returns the true (or closest to true)
+ *                display point below the spot where the touch screen 
+ *                was touched.
+ * 
+ *
+ * 
+ *  Argument(s): displayPtr (output) - Pointer to the calculated
+ *                                      (true) display point.
+ *               screenPtr (input) - Pointer to the reported touch
+ *                                    screen point.
+ *               matrixPtr (input) - Pointer to calibration factors
+ *                                    matrix previously calculated
+ *                                    from a call to 
+ *                                    setCalibrationMatrix()
+ * 
+ *
+ *  The function simply solves for Xd and Yd by implementing the 
+ *   computations required by the translation matrix.  
+ * 
+ *                                              /-     -\
+ *              /-    -\     /-            -\   |       |
+ *              |      |     |              |   |   Xs  |
+ *              |  Xd  |     | A    B    C  |   |       |
+ *              |      |  =  |              | * |   Ys  |
+ *              |  Yd  |     | D    E    F  |   |       |
+ *              |      |     |              |   |   1   |
+ *              \-    -/     \-            -/   |       |
+ *                                              \-     -/
+ * 
+ *  It must be kept brief to avoid consuming CPU cycles.
+ * 
+ *
+ *       Return: OK - the display point was correctly calculated 
+ *                     and its value is in the output argument.
+ *               NOT_OK - an error was detected and the function
+ *                         failed to return a valid point.
+ *
+ *
+ *
+ *                 NOTE!    NOTE!    NOTE!
+ *
+ *  setCalibrationMatrix() and getDisplayPoint() will do fine
+ *  for you as they are, provided that your digitizer         
+ *  resolution does not exceed 10 bits (1024 values).  Higher
+ *  resolutions may cause the integer operations to overflow
+ *  and return incorrect values.  If you wish to use these   
+ *  functions with digitizer resolutions of 12 bits (4096    
+ *  values) you will either have to a) use 64-bit signed     
+ *  integer variables and math, or b) judiciously modify the 
+ *  operations to scale results by a factor of 2 or even 4.  
+ *
+ */
+RetCode_t RA8875::TouchPanelPoint(point_t * TouchPoint)
+{
+    RetCode_t retValue = no_touch;
+    point_t screenpoint = {0, 0};
+    
+    if (TouchPanelReadRaw(&screenpoint.x, &screenpoint.y)) {
+        retValue = touch;
+        if (tpMatrix.Divider != 0 ) {
+            /* Operation order is important since we are doing integer */
+            /*  math. Make sure you add all terms together before      */
+            /*  dividing, so that the remainder is not rounded off     */
+            /*  prematurely.                                           */
+            TouchPoint->x = ( (tpMatrix.An * screenpoint.x) +
+                              (tpMatrix.Bn * screenpoint.y) +
+                              tpMatrix.Cn
+                            ) / tpMatrix.Divider ;
+
+            TouchPoint->y = ( (tpMatrix.Dn * screenpoint.x) +
+                              (tpMatrix.En * screenpoint.y) +
+                              tpMatrix.Fn
+                            ) / tpMatrix.Divider ;
+        } else {
+            retValue = bad_parameter ;
+        }
+    }
+    return( retValue );
+}
+
+
+RetCode_t RA8875::TouchPanelSetMatrix(tpMatrix_t * matrixPtr)
+{
+    if (matrixPtr == NULL || matrixPtr->Divider == 0)
+        return bad_parameter;
+    memcpy(&tpMatrix, matrixPtr, sizeof(tpMatrix_t));
+    return noerror;
+}
+
 // #### end of touch panel code additions
 
 
-RetCode_t RA8875::KeypadInit(bool scanEnable, bool longDetect, uint8_t sampleTime, uint8_t scanFrequency, 
-    uint8_t longTimeAdjustment, bool interruptEnable, bool wakeupEnable)
+RetCode_t RA8875::KeypadInit(bool scanEnable, bool longDetect, uint8_t sampleTime, uint8_t scanFrequency,
+                             uint8_t longTimeAdjustment, bool interruptEnable, bool wakeupEnable)
 {
     uint8_t value = 0;
-    
+
     if (sampleTime > 3 || scanFrequency > 7 || longTimeAdjustment  > 3)
         return bad_parameter;
     value |= (scanEnable) ? 0x80 : 0x00;
@@ -287,12 +579,12 @@
     value |= (sampleTime & 0x03) << 4;
     value |= (scanFrequency & 0x07);
     WriteCommand(0xC0, value);   // KSCR1 - Enable Key Scan (and ignore possibility of an error)
-    
+
     value = 0;
     value |= (wakeupEnable) ? 0x80 : 0x00;
     value |= (longTimeAdjustment & 0x03) << 2;
     WriteCommand(0xC1, value);  // KSCR2 - (and ignore possibility of an error)
-    
+
     value = ReadCommand(0xF0);          // (and ignore possibility of an error)
     value &= ~0x10;
     value |= (interruptEnable) ? 0x10 : 0x00;
@@ -313,14 +605,14 @@
 uint8_t RA8875::getc(void)
 {
     //#define GETC_DEV
-    #ifdef GETC_DEV
+#ifdef GETC_DEV
     uint8_t keyCode1, keyCode2;
-    #endif
+#endif
     uint8_t keyCode3;
     static uint8_t count = 0;
     uint8_t col, row;
     uint8_t key;
-    
+
     while (!readable()) {
         wait_us(POLLWAITuSec);
         // COUNTIDLETIME(POLLWAITuSec);     // As it is voluntary to call the getc and pend. Don't tally it.
@@ -331,31 +623,31 @@
     switch (keyNumReg) {
         case 0x01:      // one key
             keyCode3 = ReadCommand(0xC2);
-            #ifdef GETC_DEV
+#ifdef GETC_DEV
             keyCode2 = 0;
             keyCode1 = 0;
-            #endif
+#endif
             break;
         case 0x02:      // two keys
             keyCode3 = ReadCommand(0xC3);
-            #ifdef GETC_DEV
+#ifdef GETC_DEV
             keyCode2 = ReadCommand(0xC2);
             keyCode1 = 0;
-            #endif
+#endif
             break;
         case 0x03:      // three keys
             keyCode3 = ReadCommand(0xC4);
-            #ifdef GETC_DEV
+#ifdef GETC_DEV
             keyCode2 = ReadCommand(0xC3);
             keyCode1 = ReadCommand(0xC2);
-            #endif
+#endif
             break;
         default:         // no keys (key released)
             keyCode3 = 0xFF;
-            #ifdef GETC_DEV
+#ifdef GETC_DEV
             keyCode2 = 0;
             keyCode1 = 0;
-            #endif
+#endif
             break;
     }
     if (keyCode3 == 0xFF)
@@ -370,7 +662,7 @@
         key = pKeyMap[key];
         key |= (keyCode3 & 0x80);   // combine the key held flag
     }
-    #if GETC_DEV // for Development only
+#if GETC_DEV // for Development only
     SetTextCursor(0, 20);
     printf("   Reg: %02x\r\n", keyNumReg);
     printf("  key1: %02x\r\n", keyCode1);
@@ -378,7 +670,7 @@
     printf("  key3: %02x\r\n", keyCode3);
     printf(" count: %02X\r\n", count);
     printf("   key: %02X\r\n", key);
-    #endif
+#endif
     WriteCommand(0xF1, 0x10);       // Clear KS status
     return key;
 }
@@ -1524,10 +1816,10 @@
 
 RetCode_t RA8875::PrintScreen(uint16_t layer, loc_t x, loc_t y, dim_t w, dim_t h, const char *Name_BMP)
 {
-    #if 1
+#if 1
     (void)layer;
     return PrintScreen(x, y, w, h, Name_BMP);
-    #else
+#else
     // This is the deprecated interface and with the changes it is no longer implemented correctly.
     uint16_t curLayer = GetDrawingLayer();
     RetCode_t ret = SelectDrawingLayer(layer);
@@ -1536,7 +1828,7 @@
     }
     SelectDrawingLayer(curLayer);
     return ret;
-    #endif
+#endif
 }
 
 RetCode_t RA8875::PrintScreen(loc_t x, loc_t y, dim_t w, dim_t h, const char *Name_BMP)
@@ -1751,14 +2043,14 @@
 void KeyPadTest(RA8875 & display, Serial & pc)
 {
     const uint8_t myMap[22] = {
-        0, 
+        0,
         'a', 'b', 'c', 'd', 'e',
         'f', 'g', 'h', 'i', 'j',
         'k', 'l', 'm', 'n', 'o',
         'p', 'q', 'r', 's', 't',
         'x'
     };
-    
+
     display.background(Black);
     display.foreground(Blue);
     display.cls();
@@ -2301,6 +2593,50 @@
 }
 
 
+void TouchPanelTest(RA8875 & display, Serial & pc)
+{
+    Timer t;
+    loc_t x, y;
+
+    display.background(Black);
+    display.foreground(Blue);
+    display.cls();
+    display.puts(0,0, "Touch Panel Test\r\n");
+    pc.printf("Touch Panel Test");
+    display.TouchPanelInit();
+    point_t pTest[3] =
+    { { 50, 50 }, {450, 50}, {450,250} };
+    point_t pSample[3];
+    for (int i=0; i<3; i++) {
+        display.foreground(Blue);
+        display.printf("(%3d,%3d) => ", pTest[i].x, pTest[i].y);
+        display.line(pTest[i].x-10, pTest[i].y, pTest[i].x+10, pTest[i].y, White);
+        display.line(pTest[i].x, pTest[i].y-10, pTest[i].x, pTest[i].y+10, White);
+        while (!display.TouchPanelReadRaw(&x, &y))
+            wait_ms(20);
+        pSample[i].x = x;
+        pSample[i].y = y;
+        display.line(pTest[i].x-10, pTest[i].y, pTest[i].x+10, pTest[i].y, Black);
+        display.line(pTest[i].x, pTest[i].y-10, pTest[i].x, pTest[i].y+10, Black);
+        display.foreground(Blue);
+        display.printf(" (%4d,%4d)\r\n", x,y);
+        while (display.TouchPanelReadRaw(&x, &y))
+            wait_ms(20);
+        wait(2);
+    }
+    display.TouchPanelCalibrate(pTest, pSample, NULL);
+    display.printf(" Calibration is complete.");
+    t.start();
+    do {
+        point_t point = {0, 0};
+        if (display.TouchPanelPoint(&point)) {
+            display.pixel(point.x, point.y, Red);
+        }
+    } while (t.read_ms() < 30000);
+    pc.printf(">");
+}
+
+
 void SpeedTest(RA8875 & display, Serial & pc)
 {
     Timer t;
@@ -2357,7 +2693,7 @@
                   "T - Triangles         P - Pixels  \r\n"
                   "C - Circles           E - Ellipses\r\n"
                   "A - Auto Test mode    S - Speed Test\r\n"
-                  "K - Keypad Test\r\n"
+                  "K - Keypad Test       s - touch screen test\r\n"
                   "p - print screen      r - reset  \r\n"
                   "l - layer test        w - wrapping text \r\n"
 #ifdef PERF_METRICS
@@ -2426,6 +2762,9 @@
             case 'S':
                 SpeedTest(lcd, pc);
                 break;
+            case 's':
+                TouchPanelTest(lcd, pc);
+                break;
             case 'T':
                 TriangleTest(lcd, pc);
                 break;