KSM edits to RA8875

Dependents:   Liz_Test_Code

Revision:
81:01da2e34283d
Parent:
79:544eb4964795
Child:
83:7bad0068cca0
--- a/RA8875_Touch.cpp	Sun Dec 28 19:58:18 2014 +0000
+++ b/RA8875_Touch.cpp	Sun Dec 28 21:50:28 2014 +0000
@@ -37,6 +37,152 @@
     return noerror;
 }
 
+    // +----------------------------------------------------+
+    // |                                                    |
+    // |  1                                                 |
+    // |                                                    |
+    // |                                                    |
+    // |                                               2    |
+    // |                                                    |
+    // |                                                    |
+    // |                         3                          |
+    // |                                                    |
+    // +----------------------------------------------------+
+
+
+RetCode_t RA8875::TouchPanelCalibrate(tpMatrix_t * matrix)
+{
+    return TouchPanelCalibrate(NULL, matrix);
+}
+
+
+RetCode_t RA8875::TouchPanelCalibrate(const char * msg, tpMatrix_t * matrix)
+{
+    point_t pTest[3];
+    point_t pSample[3];
+    loc_t x,y;
+    
+    cls();
+    if (msg)
+        puts(msg);
+    SetTextCursor(0,height()/2);
+    pTest[0].x = 50;            pTest[0].y = 50;
+    pTest[1].x = width() - 50;  pTest[1].y = height()/2;
+    pTest[2].x = width()/2;     pTest[2].y = height() - 50;
+    for (int i=0; i<3; i++) {
+        foreground(Blue);
+        printf(" (%3d,%3d) => ", pTest[i].x, pTest[i].y);
+        line(pTest[i].x-10, pTest[i].y, pTest[i].x+10, pTest[i].y, White);
+        line(pTest[i].x, pTest[i].y-10, pTest[i].x, pTest[i].y+10, White);
+        while (!TouchPanelA2DFiltered(&x, &y))
+            wait_ms(20);
+        pSample[i].x = x;
+        pSample[i].y = y;
+        line(pTest[i].x-10, pTest[i].y, pTest[i].x+10, pTest[i].y, Black);
+        line(pTest[i].x, pTest[i].y-10, pTest[i].x, pTest[i].y+10, Black);
+        foreground(Blue);
+        printf(" (%4d,%4d)\r\n", x,y);
+        while (TouchPanelA2DFiltered(&x, &y))
+            wait_ms(20);
+        wait(2);
+    }
+    return TouchPanelComputeCalibration(pTest, pSample, matrix);
+}
+
+
+/**********************************************************************
+ *
+ *     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.  
+ *
+ */
+bool RA8875::TouchPanelReadable(point_t * TouchPoint)
+{
+    bool touched = false;
+    point_t screenpoint = {0, 0};
+    
+    if (TouchPanelA2DFiltered(&screenpoint.x, &screenpoint.y)) {
+        touched = true;
+        if (tpMatrix.Divider != 0 && TouchPoint) {
+            /* 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 {
+            touched = false;
+        }
+    }
+    return touched;
+}
+
+
+RetCode_t RA8875::TouchPanelSetMatrix(tpMatrix_t * matrixPtr)
+{
+    if (matrixPtr == NULL || matrixPtr->Divider == 0)
+        return bad_parameter;
+    memcpy(&tpMatrix, matrixPtr, sizeof(tpMatrix_t));
+    return noerror;
+}
+
+
 bool RA8875::TouchPanelA2DFiltered(loc_t *x, loc_t *y)
 {
     unsigned char touchready;
@@ -276,7 +422,7 @@
  *  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 RA8875::TouchPanelComputeCalibration(point_t * displayPtr, point_t * screenPtr, tpMatrix_t * matrixPtr)
 {
     RetCode_t retValue = noerror;
 
@@ -311,96 +457,4 @@
     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.  
- *
- */
-bool RA8875::TouchPanelReadable(point_t * TouchPoint)
-{
-    bool touched = false;
-    point_t screenpoint = {0, 0};
-    
-    if (TouchPanelA2DFiltered(&screenpoint.x, &screenpoint.y)) {
-        touched = true;
-        if (tpMatrix.Divider != 0 && TouchPoint) {
-            /* 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 {
-            touched = false;
-        }
-    }
-    return touched;
-}
-
-
-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