KSM edits to RA8875

Dependents:   Liz_Test_Code

RA8875_Touch_FT5206.cpp

Committer:
WiredHome
Date:
2019-02-24
Revision:
165:695c24cc5197
Child:
166:53fd4a876dac

File content as of revision 165:695c24cc5197:



#include "RA8875.h"

//#define DEBUG "RAFT" // RA8875 FT5206
// ...
// 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__);
static void HexDump(const char * title, const uint8_t * p, int count)
{
    int i;
    char buf[100] = "0000: ";

    if (*title)
        INFO("%s", title);
    for (i=0; i<count; ) {
        sprintf(buf + strlen(buf), "%02X ", *(p+i));
        if ((++i & 0x0F) == 0x00) {
            INFO("%s", buf);
            if (i < count)
                sprintf(buf, "%04X: ", i);
            else
                buf[0] = '\0';
        }
    }
    if (strlen(buf))
        INFO("%s", buf);
}
#else
#define INFO(x, ...)
#define WARN(x, ...)
#define ERR(x, ...)
#define HexDump(a, b, c)
#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 FT5206_EventFlagToTouchCode[4] = {
    touch,      // 00b Put Down
    release,    // 01b Put Up
    held,       // 10b Contact
    no_touch    // 11b Reserved
};


RetCode_t RA8875::FT5206_Init() {
    char data[2] = {FT5206_DEVICE_MODE, 0};

    m_i2c->write(m_addr, data, 2);
    return noerror;
}

uint8_t RA8875::FT5206_TouchPositions(void) {
    uint8_t valXH;
    uint8_t valYH;

    //INFO("FT5206_TouchPositions()");
    numberOfTouchPoints = FT5206_ReadRegU8(FT5206_TD_STATUS) & 0xF;
    //INFO("  numOfTouchPoints %d", numberOfTouchPoints);
    gesture = FT5206_ReadRegU8(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 intentionally falls through each lower case.
    switch (5) {    // numberOfTouchPoints
        case 5:
            valXH  = FT5206_ReadRegU8(FT5206_TOUCH5_XH);
            valYH  = FT5206_ReadRegU8(FT5206_TOUCH5_YH);
            touchInfo[4].touchCode = FT5206_EventFlagToTouchCode[valXH >> 6];
            touchInfo[4].touchID   = (valYH >> 4);
            touchInfo[4].coordinates.x = (valXH & 0x0f)*256 + FT5206_ReadRegU8(FT5206_TOUCH5_XL);
            touchInfo[4].coordinates.y = (valYH & 0x0f)*256 + FT5206_ReadRegU8(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  = FT5206_ReadRegU8(FT5206_TOUCH4_XH);
            valYH  = FT5206_ReadRegU8(FT5206_TOUCH4_YH);
            touchInfo[3].touchCode = FT5206_EventFlagToTouchCode[valXH >> 6];
            touchInfo[3].touchID   = (valYH >> 4);
            touchInfo[3].coordinates.x = (valXH & 0x0f)*256 + FT5206_ReadRegU8(FT5206_TOUCH4_XL);
            touchInfo[3].coordinates.y = (valYH & 0x0f)*256 + FT5206_ReadRegU8(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  = FT5206_ReadRegU8(FT5206_TOUCH3_XH);
            valYH  = FT5206_ReadRegU8(FT5206_TOUCH3_YH);
            touchInfo[2].touchCode = FT5206_EventFlagToTouchCode[valXH >> 6];
            touchInfo[2].touchID   = (valYH >> 4);
            touchInfo[2].coordinates.x = (valXH & 0x0f)*256 + FT5206_ReadRegU8(FT5206_TOUCH3_XL);
            touchInfo[2].coordinates.y = (valYH & 0x0f)*256 + FT5206_ReadRegU8(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  = FT5206_ReadRegU8(FT5206_TOUCH2_XH);
            valYH  = FT5206_ReadRegU8(FT5206_TOUCH2_YH);
            touchInfo[1].touchCode = FT5206_EventFlagToTouchCode[valXH >> 6];
            touchInfo[1].touchID   = (valYH >> 4);
            touchInfo[1].coordinates.x = (valXH & 0x0f)*256 + FT5206_ReadRegU8(FT5206_TOUCH2_XL);
            touchInfo[1].coordinates.y = (valYH & 0x0f)*256 + FT5206_ReadRegU8(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  = FT5206_ReadRegU8(FT5206_TOUCH1_XH);
            valYH  = FT5206_ReadRegU8(FT5206_TOUCH1_YH);
            touchInfo[0].touchCode = FT5206_EventFlagToTouchCode[valXH >> 6];
            touchInfo[0].touchID   = (valYH >> 4);
            touchInfo[0].coordinates.x = (valXH & 0x0f)*256 + FT5206_ReadRegU8(FT5206_TOUCH1_XL);
            touchInfo[0].coordinates.y = (valYH & 0x0f)*256 + FT5206_ReadRegU8(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;
}