Guillermo Stedile / RA8875

Dependencies:   GPS

Dependents:   SNOCC_V1 SNOCC_V2

Fork of RA8875 by SNOCC

Files at this revision

API Documentation at this revision

Comitter:
WiredHome
Date:
Sun Dec 28 21:50:28 2014 +0000
Parent:
80:cc4fab58179c
Child:
82:f7d300f26540
Commit message:
Refactored constructor() and init().; Added rect_t as a rectangle type.; Added new rect() methods that use rect_t.; Added touch panel calibration (and some renamed touch* methods).

Changed in this revision

DisplayDefs.h Show annotated file Show diff for this revision Revisions of this file
RA8875.cpp Show annotated file Show diff for this revision Revisions of this file
RA8875.h Show annotated file Show diff for this revision Revisions of this file
RA8875_Touch.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/DisplayDefs.h	Sun Dec 28 19:58:18 2014 +0000
+++ b/DisplayDefs.h	Sun Dec 28 21:50:28 2014 +0000
@@ -39,6 +39,15 @@
     loc_t y;             ///< y value in the point
 } point_t;
 
+/// type that manages rectangles, which are pairs of points. It is recommended
+/// that p1 contains the top-left point and p2 contains the bottom-right point,
+/// even though eventually this should not matter.
+typedef struct
+{
+    point_t p1;         ///< p1 defines one point on the rectangle
+    point_t p2;         ///< p2 defines the opposite point on the rectangle
+} rect_t;
+
 typedef struct
 {
     int32_t An, Bn, Cn, Dn, En, Fn, Divider;
--- a/RA8875.cpp	Sun Dec 28 19:58:18 2014 +0000
+++ b/RA8875.cpp	Sun Dec 28 21:50:28 2014 +0000
@@ -102,9 +102,8 @@
 //{
 //}
 
-RetCode_t RA8875::init(bool poweron, int width, int height, int color_bpp)
+RetCode_t RA8875::init(int width, int height, int color_bpp, bool poweron, bool keypadon, bool touchscreenon)
 {
-    INFO("init(%d,%d,%d,%d)", poweron, width, height, color_bpp);
     font = NULL;                        // no external font, use internal.
     pKeyMap = DefaultKeyMap;            // set default key map
     _select(false);                      // deselect the display
@@ -157,6 +156,10 @@
     Power(poweron);
     if (poweron)
         Backlight_u8(255);
+    if (keypadon)
+        KeypadInit();
+    if (touchscreenon)
+        TouchPanelInit();
 #ifdef PERF_METRICS
     performance.start();
     ClearPerformance();
@@ -1025,6 +1028,10 @@
     return noerror;
 }
 
+RetCode_t RA8875::fillrect(rect_t r, color_t color, fill_t fillit)
+{
+    return rect(r.p1.x, r.p1.y, r.p2.x, r.p2.y, color, fillit);
+}
 
 RetCode_t RA8875::fillrect(loc_t x1, loc_t y1, loc_t x2, loc_t y2,
                            color_t color, fill_t fillit)
@@ -1032,6 +1039,10 @@
     return rect(x1,y1,x2,y2,color,fillit);
 }
 
+RetCode_t RA8875::rect(rect_t r, color_t color, fill_t fillit)
+{
+    return rect(r.p1.x, r.p1.y, r.p2.x, r.p2.y, color, fillit);
+}
 
 RetCode_t RA8875::rect(loc_t x1, loc_t y1, loc_t x2, loc_t y2,
                        color_t color, fill_t fillit)
@@ -2256,7 +2267,7 @@
                 wait_ms(20);
             wait(2);
         }
-        display.TouchPanelCalibrate(pTest, pSample, &calmatrix);
+        display.TouchPanelComputeCalibration(pTest, pSample, &calmatrix);
         display.printf(" Writing calibration to tpcal.cfg\r\n");
         FILE * fh = fopen("/local/tpcal.cfg", "wb");
         if (fh) {
--- a/RA8875.h	Sun Dec 28 19:58:18 2014 +0000
+++ b/RA8875.h	Sun Dec 28 21:50:28 2014 +0000
@@ -25,6 +25,7 @@
 /// 
 /// @code 
 /// RA8875 lcd(p5, p6, p7, p12, NC, "tft");
+/// lcd.init();
 /// lcd.foreground(Blue);
 /// lcd.line(0,0, 479,271);
 /// ...
@@ -76,7 +77,7 @@
 #define Cyan        (color_t)(RGB(0,187,187))
 #define Red         (color_t)(RGB(187,0,0))
 #define Magenta     (color_t)(RGB(187,0,187))
-#define Brown       (color_t)(RGB(187,187,0))
+#define Brown       (color_t)(RGB(63,63,0))
 #define Gray        (color_t)(RGB(187,187,187))
 #define Charcoal    (color_t)(RGB(85,85,85))
 #define BrightBlue  (color_t)(RGB(0,0,255))
@@ -85,7 +86,7 @@
 #define BrightRed   (color_t)(RGB(255,0,0))
 #define Orange      (color_t)(RGB(255,85,85))
 #define Pink        (color_t)(RGB(255,85,255))
-#define Yellow      (color_t)(RGB(255,255,85))
+#define Yellow      (color_t)(RGB(187,187,0))
 #define White       (color_t)(RGB(255,255,255))
 
 #define DarkBlue    (color_t)(RGB(0,0,63))
@@ -121,7 +122,7 @@
 ///
 /// int main()
 /// {
-///     lcd.init(480,272,16);
+///     lcd.init();
 ///     lcd.printf("printing 3 x 2 = %d", 3*2);
 ///     lcd.circle(       400,25,  25,               BrightRed);
 ///     lcd.fillcircle(   400,25,  15,               RGB(128,255,128));
@@ -224,7 +225,7 @@
     ///
     /// int main()
     /// {
-    ///     lcd.init(true,255,480,272,16); // powerup, backlight full, w x h x c
+    ///     lcd.init();
     ///     lcd.printf("printing 3 x 2 = %d", 3*2);
     ///     lcd.circle(400,25, 25, BrightRed);
     /// }
@@ -249,15 +250,24 @@
     
     /// Initialize the driver.
     ///
+    /// @param[in] width in pixels to configure the display for. This parameter is optional
+    ///             and the default is 480.
+    /// @param[in] height in pixels to configure the display for. This parameter is optional
+    ///             and the default is 272.
+    /// @param[in] color_bpp can be either 8 or 16, but must be consistent
+    ///             with the width and height parameters. This parameter is optional
+    ///             and the default is 16.
     /// @param[in] power defines if the display should be left in the power-on or off state.
-    ///            If power is true (on), the backlight is set to 100%.
-    /// @param[in] width in pixels to configure the display for.
-    /// @param[in] height in pixels to configure the display for.
-    /// @param[in] color_bpp can be either 8 or 16, but must be consistent
-    ///     with the width and height parameters.
+    ///            If power is true (on), the backlight is set to 100%. This parameter is optional
+    ///             and the default is true (on). @see Power.
+    /// @param[in] keypadon defines if the keypad support should be enabled. This parameter is optional
+    ///             and the default is true (enabled). @see KeypadInit.
+    /// @param[in] touchscreeenon defines if the keypad support should be enabled. This parameter is optional
+    ///             and the default is true (enabled). @see TouchPanelInit.
     /// @returns success/failure code. @see RetCode_t.
     ///
-    RetCode_t init(bool poweron, int width, int height, int color_bpp);
+    RetCode_t init(int width = 480, int height = 272, int color_bpp = 16, 
+        bool poweron = true, bool keypadon = true, bool touchscreeenon = true);
     
     /// Get a pointer to the error code.
     ///
@@ -442,7 +452,7 @@
     /// @note The returned values are not in display (pixel) units but are in analog to
     ///     digital converter units.
     /// 
-    /// @note This API is usually not needed. @see TouchPanelCalibrate. 
+    /// @note This API is usually not needed. @see TouchPanelComputeCalibration. 
     ///     @see TouchPanelReadable.
     /// 
     /// @param[out] x is the x scale a/d value.
@@ -460,7 +470,7 @@
     /// @note The returned values are not in display (pixel) units but are in analog to
     ///     digital converter units.
     /// 
-    /// @note This API is usually not needed. @see TouchPanelCalibrate. 
+    /// @note This API is usually not needed. @see TouchPanelComputeCalibration. 
     ///     @see TouchPanelReadable.
     /// 
     /// @param[out] x is the x scale a/d value.
@@ -479,7 +489,7 @@
     /// matrix on the next power cycle. By doing so, it can avoid the
     /// need to calibrate on every power cycle.
     ///
-    /// @note The methods "TouchPanelCalibrate", "TouchPanelReadable", and
+    /// @note The methods "TouchPanelComputeCalibration", "TouchPanelReadable", and
     ///     indirectly the "TouchPanelSetMatrix" methods are all derived
     ///     from a program by Carlos E. Vidales. See the copyright note
     ///     for further details. See also the article
@@ -513,7 +523,40 @@
     ///             non-volatile memory to recover the calibration after a power fail.
     /// @returns success/failure code. @see RetCode_t.
     ///
-    RetCode_t TouchPanelCalibrate(point_t display[3], point_t screen[3], tpMatrix_t * matrix);
+    RetCode_t TouchPanelComputeCalibration(point_t display[3], point_t screen[3], tpMatrix_t * matrix);
+
+
+    /// Perform the touch panel calibration process.
+    ///
+    /// This method provides the easy "shortcut" to calibrating the touch panel.
+    /// The process will automatically generate the calibration points, present
+    /// the targets on-screen, detect the touches, compute the calibration
+    /// matrix, and optionally provide the calibration matrix to the calling code
+    /// for persistence in non-volatile memory.
+    ///
+    /// @param[out] matrix is an optional parameter to hold the calibration matrix 
+    ///             as a result of the calibration. This can be saved in  
+    ///             non-volatile memory to recover the calibration after a power fail.
+    /// @returns success/failure code. @see RetCode_t.
+    ///
+    RetCode_t TouchPanelCalibrate(tpMatrix_t * matrix);
+
+    /// Perform the touch panel calibration process.
+    ///
+    /// This method provides the easy "shortcut" to calibrating the touch panel.
+    /// The process will automatically generate the calibration points, present
+    /// the targets on-screen, detect the touches, compute the calibration
+    /// matrix, and optionally provide the calibration matrix to the calling code
+    /// for persistence in non-volatile memory.
+    ///
+    /// @param[in] msg is a text message to present on the screen during the
+    ///             calibration process.
+    /// @param[out] matrix is an optional parameter to hold the calibration matrix 
+    ///             as a result of the calibration. This can be saved in  
+    ///             non-volatile memory to recover the calibration after a power fail.
+    /// @returns success/failure code. @see RetCode_t.
+    ///
+    RetCode_t TouchPanelCalibrate(const char * msg, tpMatrix_t * matrix = NULL);
 
     /// Get the screen calibrated point of touch.
     ///
@@ -568,7 +611,7 @@
     /// Set the calibration matrix for the touch panel.
     ///
     /// This method is used to set the calibration matrix for the touch panel. After
-    /// performing the calibration (@see TouchPanelCalibrate), the matrix can be stored.
+    /// performing the calibration (@see TouchPanelComputeCalibration), the matrix can be stored.
     /// On a subsequence power cycle, the matrix may be restored from non-volatile and
     /// passed in to this method. It will then be held to perform the corrections when
     /// reading the touch panel point.
@@ -1166,12 +1209,36 @@
     /// @note As a side effect, this changes the current
     ///     foreground color for subsequent operations.
     ///
+    /// @param[in] rect defines the rectangle.
+    /// @param[in] color defines the foreground color.
+    /// @param[in] fillit is optional to FILL the rectangle. default is NOFILL.
+    /// @returns success/failure code. @see RetCode_t.
+    ///
+    RetCode_t rect(rect_t rect, color_t color, fill_t fillit);
+    
+     /// Draw a filled rectangle in the specified color
+    ///
+    /// @note As a side effect, this changes the current
+    ///     foreground color for subsequent operations.
+    ///
+    /// @param[in] rect defines the rectangle.
+    /// @param[in] color defines the foreground color.
+    /// @param[in] fillit is optional to FILL the rectangle. default is NOFILL.
+    /// @returns success/failure code. @see RetCode_t.
+    ///
+    RetCode_t fillrect(rect_t rect, color_t color, fill_t fillit = FILL);
+
+    /// Draw a rectangle in the specified color
+    ///
+    /// @note As a side effect, this changes the current
+    ///     foreground color for subsequent operations.
+    ///
     /// @param[in] x1 is the horizontal start of the line.
     /// @param[in] y1 is the vertical start of the line.
     /// @param[in] x2 is the horizontal end of the line.
     /// @param[in] y2 is the vertical end of the line.
     /// @param[in] color defines the foreground color.
-    /// @param[in] fillit is optional to FILL the rectangle. default is NOFILL.
+    /// @param[in] fillit is optional to FILL the rectangle. default is FILL.
     /// @returns success/failure code. @see RetCode_t.
     ///
     RetCode_t rect(loc_t x1, loc_t y1, loc_t x2, loc_t y2, 
--- 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