KSM edits to RA8875

Dependents:   Liz_Test_Code

Revision:
165:695c24cc5197
Parent:
157:1565f38ca44b
Child:
166:53fd4a876dac
--- a/RA8875_Touch.cpp	Thu Feb 14 12:34:52 2019 +0000
+++ b/RA8875_Touch.cpp	Sun Feb 24 00:40:00 2019 +0000
@@ -4,6 +4,8 @@
 /// to make them nearly transparent alternates for each other.
 ///
 #include "RA8875.h"
+#include "RA8875_Touch_FT5206.h"
+#include "RA8875_Touch_GSL1680.h"
 
 #define NOTOUCH_TIMEOUT_uS 100000
 #define TOUCH_TICKER_uS      1000
@@ -44,26 +46,18 @@
 #endif
 
 
-// Translate from FT5206 Event Flag to Touch Code to API-match the
-// alternate resistive touch screen driver common in the RA8875
-// displays.
-static const TouchCode_t EventFlagToTouchCode[4] = {
-    touch,      // 00b Put Down
-    release,    // 01b Put Up
-    held,       // 10b Contact
-    no_touch    // 11b Reserved
-};
-
-
 RetCode_t RA8875::TouchPanelInit(void)
 {
     RetCode_t r = noerror;
     
     panelTouched = false;
-    if (useTouchPanel == TP_CAP) {
+    if (useTouchPanel == TP_GSL1680) {
+        INFO("TouchPanelInit: TP_GSL1680");
+        /// @TODO Added support for TP_GSL1680
+    } else if (useTouchPanel == TP_FT5206) {
         // Set to normal mode
-        INFO("TouchPanelInit: TP_CAP");
-        writeRegister8(FT5206_DEVICE_MODE, 0);
+        INFO("TouchPanelInit: TP_FT5206");
+        r = FT5206_Init();
     } else {
         INFO("TouchPanelInit: TP_RES");
         //TPCR0: Set enable bit, default sample time, wakeup, and ADC clock
@@ -81,8 +75,8 @@
         touchTicker.attach_us(this, &RA8875::_TouchTicker, TOUCH_TICKER_uS);
         #endif
         
-        touchTimer.start();
-        touchTimer.reset();
+        timeSinceTouch.start();
+        timeSinceTouch.reset();
         r = _internal_ts_cal();
     }
     return r;
@@ -91,8 +85,11 @@
 
 RetCode_t RA8875::TouchPanelInit(uint8_t bTpEnable, uint8_t bTpAutoManual, uint8_t bTpDebounce, uint8_t bTpManualMode, uint8_t bTpAdcClkDiv, uint8_t bTpAdcSampleTime)
 {
-    if (useTouchPanel == TP_CAP) {
-        INFO("TouchPanelInit: TP_CAP");
+    if (useTouchPanel == TP_GSL1680) {
+        INFO("TouchPanelInit: TP_GSL1680");
+        /// @TODO Added support for TP_GSL1680
+    } else if (useTouchPanel == TP_FT5206) {
+        INFO("TouchPanelInit: TP_FT5206");
         TouchPanelInit();
     } else {
         INFO("TouchPanelInit: TP_RES");
@@ -122,11 +119,11 @@
             touchTicker.attach_us(this, &RA8875::_TouchTicker, TOUCH_TICKER_uS);
             #endif
 
-            touchTimer.start();
-            touchTimer.reset();
+            timeSinceTouch.start();
+            timeSinceTouch.reset();
         } else {
             touchTicker.detach();
-            touchTimer.stop();
+            timeSinceTouch.stop();
         }
         _internal_ts_cal();
     }
@@ -136,7 +133,9 @@
 
 int RA8875::TouchChannels(void)
 {
-    if (useTouchPanel == TP_CAP) {
+    if (useTouchPanel == TP_GSL1680) {
+        return 10;  // based on TP_GSL1680 hardware
+    } else if (useTouchPanel == TP_FT5206) {
         return 5;   // based on the FT5206 hardware
     } else if (useTouchPanel == TP_RES) {
         return 1;   // based on the RA8875 resistive touch driver
@@ -297,7 +296,11 @@
 {
     TouchCode_t ts = no_touch;
 
-    if (useTouchPanel == TP_RES) {
+    if (useTouchPanel == TP_FT5206) {
+        ;
+    } else if (useTouchPanel == TP_GSL1680) {
+        ;
+    } else if (useTouchPanel == TP_RES) {
         int a2dX = 0;
         int a2dY = 0;
         
@@ -325,9 +328,9 @@
             numberOfTouchPoints = 0;
         }
         touchInfo[0].touchCode = ts;
-    } else /* (useTouchPanel == TP_CAP) */ {
-        ;
     }
+    // For Resistive touch, panelTouched is computed above.
+    // For Cap Sense, panelTouched is set in another process
     if (panelTouched == true) {
         panelTouched = false;
         if (TouchPoint) {
@@ -398,13 +401,13 @@
 void RA8875::_TouchTicker(void)
 {
     INFO("_TouchTicker()");
-    if (touchTimer.read_us() > NOTOUCH_TIMEOUT_uS) {
+    if (timeSinceTouch.read_us() > NOTOUCH_TIMEOUT_uS) {
         touchSample = 0;
         if (touchState == held)
             touchState = release;
         else
             touchState = no_touch;
-        touchTimer.reset();
+        timeSinceTouch.reset();
     }
 }
 
@@ -413,7 +416,7 @@
     INFO("A2Raw");
     if( (ReadCommand(INTC2) & RA8875_INT_TP) ) {        // Test for TP Interrupt pending in register INTC2
         INFO("Int pending");
-        touchTimer.reset();
+        timeSinceTouch.reset();
         *y = ReadCommand(TPYH) << 2 | ( (ReadCommand(TPXYL) & 0xC) >> 2 );   // D[9:2] from reg TPYH, D[1:0] from reg TPXYL[3:2]
         *x = ReadCommand(TPXH) << 2 | ( (ReadCommand(TPXYL) & 0x3)      );   // D[9:2] from reg TPXH, D[1:0] from reg TPXYL[1:0]
         INFO("(x,y) = (%d,%d)", x, y);
@@ -434,7 +437,7 @@
     TouchCode_t ret = touchState;
 
     if( (ReadCommand(INTC2) & RA8875_INT_TP) ) {        // Test for TP Interrupt pending in register INTC2
-        touchTimer.reset();
+        timeSinceTouch.reset();
         // Get the next data samples
         ybuf[touchSample] =  ReadCommand(TPYH) << 2 | ( (ReadCommand(TPXYL) & 0xC) >> 2 );   // D[9:2] from reg TPYH, D[1:0] from reg TPXYL[3:2]
         xbuf[touchSample] =  ReadCommand(TPXH) << 2 | ( (ReadCommand(TPXYL) & 0x3)      );   // D[9:2] from reg TPXH, D[1:0] from reg TPXYL[1:0]
@@ -756,123 +759,25 @@
 
 ////////////////// Capacitive Touch Panel
 
-uint8_t RA8875::readRegister8(uint8_t reg) {
+uint8_t RA8875::FT5206_ReadRegU8(uint8_t reg) {
     char val;
     
-    //INFO("readRegister8(%02X) @ %X", reg, m_i2c);
     m_i2c->write(m_addr, (const char *)&reg, 1);
-    //INFO("  write(%X, ...)", m_addr);
     m_i2c->read(m_addr, &val, 1);
     return (uint8_t)val;
 }
 
-void RA8875::writeRegister8(uint8_t reg, uint8_t val) {
-    char data[2];
-    
-    data[0] = (char)reg;
-    data[1] = (char)val;
-    m_i2c->write((int)FT5206_I2C_ADDRESS, data, 2);
-}
-
-
 // Interrupt for touch detection
 void RA8875::TouchPanelISR(void)
 {
-    //INFO("TouchPanelISR()");
-    if (getTouchPositions())
-        panelTouched = true;
-    //INFO("   return");
+    if (useTouchPanel == TP_FT5206) {
+        if (FT5206_TouchPositions())
+            panelTouched = true;
+    } else if (useTouchPanel == TP_GSL1680) {
+        if (GSL1680_TouchPositions())
+            panelTouched = true;
+    }
 }
 
-uint8_t RA8875::getTouchPositions(void) {
-    uint8_t valXH;
-    uint8_t valYH;
-
-    //INFO("getTouchPositions()");
-    numberOfTouchPoints = readRegister8(FT5206_TD_STATUS) & 0xF;
-    //INFO("  numOfTouchPoints %d", numberOfTouchPoints);
-    gesture = readRegister8(FT5206_GEST_ID);
-    //INFO("  gesture %d", gesture);
-    
-    // If the switch statement was based only on numberOfTouchPoints, it would not
-    // be able to generate notification for 'release' events (as it is no longer touched).
-    // Therefore, forcing a 5, and it intentially falls through each lower case.
-    switch (5) {    // numberOfTouchPoints
-        case 5:
-            valXH  = readRegister8(FT5206_TOUCH5_XH);
-            valYH  = readRegister8(FT5206_TOUCH5_YH);
-            touchInfo[4].touchCode = EventFlagToTouchCode[valXH >> 6];
-            touchInfo[4].touchID   = (valYH >> 4);
-            touchInfo[4].coordinates.x = (valXH & 0x0f)*256 + readRegister8(FT5206_TOUCH5_XL);
-            touchInfo[4].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH5_YL);
-            if (touchInfo[4].touchCode)
-            INFO("  Touch[%d] code %d, id %2d, (%4d,%4d)",
-                5, 
-                touchInfo[4].touchCode, 
-                touchInfo[4].touchID,
-                touchInfo[4].coordinates.x, 
-                touchInfo[4].coordinates.y);
-        case 4:
-            valXH  = readRegister8(FT5206_TOUCH4_XH);
-            valYH  = readRegister8(FT5206_TOUCH4_YH);
-            touchInfo[3].touchCode = EventFlagToTouchCode[valXH >> 6];
-            touchInfo[3].touchID   = (valYH >> 4);
-            touchInfo[3].coordinates.x = (valXH & 0x0f)*256 + readRegister8(FT5206_TOUCH4_XL);
-            touchInfo[3].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH4_YL);
-            if (touchInfo[3].touchCode)
-            INFO("  Touch[%d] code %d, id %2d, (%4d,%4d)",
-                4, 
-                touchInfo[3].touchCode, 
-                touchInfo[3].touchID,
-                touchInfo[3].coordinates.x, 
-                touchInfo[3].coordinates.y);
-        case 3:
-            valXH  = readRegister8(FT5206_TOUCH3_XH);
-            valYH  = readRegister8(FT5206_TOUCH3_YH);
-            touchInfo[2].touchCode = EventFlagToTouchCode[valXH >> 6];
-            touchInfo[2].touchID   = (valYH >> 4);
-            touchInfo[2].coordinates.x = (valXH & 0x0f)*256 + readRegister8(FT5206_TOUCH3_XL);
-            touchInfo[2].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH3_YL);
-            if (touchInfo[2].touchCode)
-            INFO("  Touch[%d] code %d, id %2d, (%4d,%4d)",
-                3, 
-                touchInfo[2].touchCode, 
-                touchInfo[2].touchID,
-                touchInfo[2].coordinates.x, 
-                touchInfo[2].coordinates.y);
-        case 2:
-            valXH  = readRegister8(FT5206_TOUCH2_XH);
-            valYH  = readRegister8(FT5206_TOUCH2_YH);
-            touchInfo[1].touchCode = EventFlagToTouchCode[valXH >> 6];
-            touchInfo[1].touchID   = (valYH >> 4);
-            touchInfo[1].coordinates.x = (valXH & 0x0f)*256 + readRegister8(FT5206_TOUCH2_XL);
-            touchInfo[1].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH2_YL);
-            if (touchInfo[1].touchCode)
-            INFO("  Touch[%d] code %d, id %2d, (%4d,%4d)",
-                2, 
-                touchInfo[1].touchCode, 
-                touchInfo[1].touchID,
-                touchInfo[1].coordinates.x, 
-                touchInfo[1].coordinates.y);
-        case 1:
-            valXH  = readRegister8(FT5206_TOUCH1_XH);
-            valYH  = readRegister8(FT5206_TOUCH1_YH);
-            touchInfo[0].touchCode = EventFlagToTouchCode[valXH >> 6];
-            touchInfo[0].touchID   = (valYH >> 4);
-            touchInfo[0].coordinates.x = (valXH & 0x0f)*256 + readRegister8(FT5206_TOUCH1_XL);
-            touchInfo[0].coordinates.y = (valYH & 0x0f)*256 + readRegister8(FT5206_TOUCH1_YL);
-            if (touchInfo[0].touchCode)
-            INFO("  Touch[%d] code %d, id %2d, (%4d,%4d)",
-                1, 
-                touchInfo[0].touchCode, 
-                touchInfo[0].touchID,
-                touchInfo[0].coordinates.x, 
-                touchInfo[0].coordinates.y);
-            break;
-        default:
-            break;
-    }
-    return numberOfTouchPoints;
-}
 
 // #### end of touch panel code additions