This is the David Smart RA8875 Library with mods for working with FRDM-K64F

Revision:
197:853d08e2fb53
Parent:
196:56820026701b
--- a/RA8875_Touch.cpp	Tue Feb 11 21:26:59 2020 +0000
+++ b/RA8875_Touch.cpp	Tue Feb 11 21:51:42 2020 +0000
@@ -15,9 +15,9 @@
 // INFO("Stuff to show %d", var); // new-line is automatically appended
 //
 #if (defined(DEBUG) && !defined(TARGET_LPC11U24))
-#define INFO(x, ...) std::printf("[INF %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
-#define WARN(x, ...) std::printf("[WRN %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
-#define ERR(x, ...)  std::printf("[ERR %s %4d] "x"\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+#define INFO(x, ...) std::printf("[INF %s %4d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+#define WARN(x, ...) std::printf("[WRN %s %4d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
+#define ERR(x, ...)  std::printf("[ERR %s %4d] " x "\r\n", DEBUG, __LINE__, ##__VA_ARGS__);
 static void HexDump(const char * title, const uint8_t * p, int count)
 {
     int i;
@@ -60,9 +60,9 @@
         r = FT5206_Init();
     } else {
         INFO("TouchPanelInit: TP_RES");
-        //TPCR0: Set enable bit, default sample time, wakeup, and ADC clock
+        //RA8875_TPCR0: Set enable bit, default sample time, wakeup, and ADC clock
         WriteCommand(RA8875_TPCR0, TP_ENABLE | TP_ADC_SAMPLE_DEFAULT_CLKS | TP_ADC_CLKDIV_DEFAULT);
-        // TPCR1: Set auto/manual, Ref voltage, debounce, manual mode params
+        // RA8875_TPCR1: Set auto/manual, Ref voltage, debounce, manual mode params
         WriteCommand(RA8875_TPCR1, TP_MODE_DEFAULT | TP_DEBOUNCE_DEFAULT);
         WriteCommand(RA8875_INTC1, ReadCommand(RA8875_INTC1) | RA8875_INT_TP);        // reg RA8875_INTC1: Enable Touch Panel Interrupts (D2 = 1)
         WriteCommand(RA8875_INTC2, RA8875_INT_TP);                            // reg RA8875_INTC2: Clear any TP interrupt flag
@@ -87,6 +87,7 @@
 {
     if (useTouchPanel == TP_GSL1680) {
         INFO("TouchPanelInit: TP_GSL1680");
+        /// @todo Added support for TP_GSL1680
     } else if (useTouchPanel == TP_FT5206) {
         INFO("TouchPanelInit: TP_FT5206");
         TouchPanelInit();
@@ -101,9 +102,9 @@
                 !(bTpAdcClkDiv <= TP_ADC_CLKDIV_128) || \
                 !(bTpAdcSampleTime <= TP_ADC_SAMPLE_65536_CLKS) \
           ) return bad_parameter;
-        // Construct the config byte for TPCR0 and write them
+        // Construct the config byte for RA8875_TPCR0 and write them
         WriteCommand(RA8875_TPCR0, bTpEnable | bTpAdcClkDiv | bTpAdcSampleTime);    // Note: Wakeup is never enabled
-        // Construct the config byte for TPCR1 and write them
+        // Construct the config byte for RA8875_TPCR1 and write them
         WriteCommand(RA8875_TPCR1, bTpDebounce | bTpManualMode);    // Note: Always uses internal Vref.
         // Set up the interrupt flag and enable bits
         WriteCommand(RA8875_INTC1, ReadCommand(RA8875_INTC1) | RA8875_INT_TP);        // reg RA8875_INTC1: Enable Touch Panel Interrupts (D2 = 1)
@@ -170,7 +171,7 @@
 
     timeout.start();
     while (TouchPanelA2DFiltered(&x, &y) && timeout.read() < maxwait_s) {
-        wait_ms(20);
+        wait_us(20000);
         if (idle_callback) {
             if (external_abort == (*idle_callback)(touchcal_wait, 0)) {
                 return external_abort;
@@ -198,7 +199,7 @@
         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);
+            wait_us(20000);
             if (idle_callback) {
                 if (external_abort == (*idle_callback)(touchcal_wait, 0)) {
                     return external_abort;
@@ -213,7 +214,7 @@
         foreground(Blue);
         printf(" (%4d,%4d)\r\n", x,y);
         while (TouchPanelA2DFiltered(&x, &y) && timeout.read() < maxwait_s) {
-            wait_ms(20);
+            wait_us(20000);
             if (idle_callback) {
                 if (external_abort == (*idle_callback)(touchcal_wait, 0)) {
                     return external_abort;
@@ -221,7 +222,7 @@
             }
         }
         for (int t=0; t<100; t++) {
-            wait_ms(20);
+            wait_us(20000);
             if (idle_callback) {
                 if (external_abort == (*idle_callback)(touchcal_wait, 0)) {
                     return external_abort;
@@ -344,8 +345,8 @@
             #ifdef DEBUG
             static TouchCode_t lastTS = no_touch;
             if (lastTS != ts)
-            INFO("Touch[0] %2d (%4d,%4d)", touchInfo[0].touchCode,
-                touchInfo[0].coordinates.x, touchInfo[0].coordinates.y);
+                INFO("Touch[0] %2d (%4d,%4d)", touchInfo[0].touchCode,
+                    touchInfo[0].coordinates.x, touchInfo[0].coordinates.y);
             lastTS = ts;
             #endif
         } else {
@@ -460,7 +461,7 @@
 
 void RA8875::_TouchTicker(void)
 {
-    INFO("_TouchTicker()");
+    //INFO("_TouchTicker()");
     if (timeSinceTouch.read_us() > NOTOUCH_TIMEOUT_uS) {
         touchSample = 0;
         if (touchState == held) {
@@ -508,7 +509,7 @@
         ybuf[touchSample] =  ReadCommand(RA8875_TPYH) << 2 | ( (ReadCommand(RA8875_TPXYL) & 0xC) >> 2 );   // D[9:2] from reg RA8875_TPYH, D[1:0] from reg RA8875_TPXYL[3:2]
         xbuf[touchSample] =  ReadCommand(RA8875_TPXH) << 2 | ( (ReadCommand(RA8875_TPXYL) & 0x3)      );   // D[9:2] from reg RA8875_TPXH, D[1:0] from reg RA8875_TPXYL[1:0]
         // Check for a complete set
-        if(++touchSample == TPBUFSIZE) {
+        if (++touchSample == TPBUFSIZE) {
             // Buffers are full, so process them using Finn's method described in Analog Dialogue No. 44, Feb 2010
             // This requires sorting the samples in order of size, then discarding the top 25% and
             //   bottom 25% as noise spikes. Finally, the middle 50% of the values are averaged to
@@ -517,7 +518,7 @@
             InsertionSort(xbuf, TPBUFSIZE);
             // Average the middle half of the  Y values and report them
             j = 0;
-            for(i = (TPBUFSIZE/4) - 1; i < TPBUFSIZE - TPBUFSIZE/4; i++ ) {
+            for (i = (TPBUFSIZE/4) - 1; i < TPBUFSIZE - TPBUFSIZE/4; i++ ) {
                 j += ybuf[i];
             }
             *y = lastY = j * (float)2/TPBUFSIZE;    // This is the average
@@ -530,10 +531,10 @@
             // Tidy up and return
             if (touchState == touch || touchState == held) {
                 touchState = held;
-                INFO("held");
+                //INFO("held");
             } else {
                 touchState = touch;
-                INFO("touch");
+                //INFO("touch");
             }
             ret = touchState;
             touchSample = 0;             // Ready to start on the next set of data samples
@@ -542,12 +543,14 @@
             if (touchState == touch || touchState == held) {
                 *x = lastX;
                 *y = lastY;
+                #ifdef DEBUG
                 {
                     static TouchCode_t lastTC = no_touch;
                     if (lastTC != touchState)
-                        INFO("held");
+                        //INFO("held");
                     lastTC = touchState;
                 }
+                #endif
                 ret = touchState = held;
             } else {
                 INFO("else %d, %d", touchState, touchSample);