KSM edits to RA8875

Dependents:   Liz_Test_Code

Revision:
157:1565f38ca44b
Parent:
155:b3f225ae572c
Child:
165:695c24cc5197
--- a/RA8875_Touch.cpp	Mon Nov 19 02:21:41 2018 +0000
+++ b/RA8875_Touch.cpp	Wed Jan 02 23:42:15 2019 +0000
@@ -57,6 +57,8 @@
 
 RetCode_t RA8875::TouchPanelInit(void)
 {
+    RetCode_t r = noerror;
+    
     panelTouched = false;
     if (useTouchPanel == TP_CAP) {
         // Set to normal mode
@@ -81,8 +83,9 @@
         
         touchTimer.start();
         touchTimer.reset();
+        r = _internal_ts_cal();
     }
-    return noerror;
+    return r;
 }
 
 
@@ -125,6 +128,7 @@
             touchTicker.detach();
             touchTimer.stop();
         }
+        _internal_ts_cal();
     }
     return noerror;
 }
@@ -176,8 +180,11 @@
         }
     }
     cls();
-    if (msg)
-        puts(msg);
+    if (msg) {                  // User defines the message
+        if (*msg) puts(msg);    // If any
+    } else {                    // Default message
+        puts("Touch '+' to calibrate the touch panel");
+    }
     SetTextCursor(0,height()/2);
     pTest[0].x = 50;
     pTest[0].y = 50;
@@ -189,8 +196,9 @@
     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);
+        fillcircle(pTest[i].x,pTest[i].y, 20, White);
+        line(pTest[i].x-10, pTest[i].y, pTest[i].x+10, pTest[i].y, Blue);
+        line(pTest[i].x, pTest[i].y-10, pTest[i].x, pTest[i].y+10, Blue);
         while (!TouchPanelA2DFiltered(&x, &y) && timeout.read() < maxwait_s) {
             wait_ms(20);
             if (idle_callback) {
@@ -201,8 +209,9 @@
         }
         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);
+        fillcircle(pTest[i].x,pTest[i].y, 20, Black);   // Erase the target
+        //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) && timeout.read() < maxwait_s) {
@@ -363,6 +372,12 @@
     return noerror;
 }
 
+const tpMatrix_t * RA8875::TouchPanelGetMatrix()
+{
+    return &tpMatrix;
+}
+
+
 static void InsertionSort(int * buf, int bufsize)
 {
     int i, j;
@@ -696,6 +711,49 @@
     return( retValue ) ;
 }
 
+void RA8875::ResTouchPanelCfg(const char * _tpFQFN, const char * _tpCalMessage)
+{
+    tpFQFN = _tpFQFN;
+    tpCalMessage = _tpCalMessage;
+}
+
+
+RetCode_t RA8875::_internal_ts_cal()
+{
+    FILE * fh;
+    tpMatrix_t matrix;
+    RetCode_t r = noerror;
+    
+    if (tpFQFN) {
+        fh = fopen(tpFQFN, "rb");
+        if (fh) {
+            fread(&matrix, sizeof(tpMatrix_t), 1, fh);
+            fclose(fh);
+            TouchPanelSetMatrix(&matrix);
+        } else {
+            r = TouchPanelCalibrate(tpCalMessage, &matrix);
+            if (r == noerror) {
+                fh = fopen(tpFQFN, "wb");
+                if (fh) {
+                    fwrite(&matrix, sizeof(tpMatrix_t), 1, fh);
+                    fclose(fh);
+                    INFO("  tp cal written to '%s'.", tpFQFN);
+                } else {
+                    ERR("  couldn't open tpcal file '%s'.", tpFQFN);
+                    r = file_not_found;
+                }
+            } else {
+                ERR("error return: %d", r);
+            }
+            HexDump("TPCal", (const uint8_t *)&matrix, sizeof(tpMatrix_t));
+            cls();
+        }
+    }
+    return r;
+}
+
+
+
 ////////////////// Capacitive Touch Panel
 
 uint8_t RA8875::readRegister8(uint8_t reg) {