⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 hw_touchpanel.c

📁 瑞星微公司RK27XX系列芯片的SDK开发包
💻 C
字号:
/******************************************************************/
/*   Copyright (C) 2007 ROCK-CHIPS FUZHOU . All Rights Reserved.  */
/*******************************************************************
File :  hw_touchpanel.c
Desc :  Touch panel接口函数的实现

Author : huangxinyu
Date : 2007-09-24
Notes :

$Log: hw_touchpanel.c,v $
Revision 1.2  2008/06/19 04:43:37  Administrator
代码整理!

Revision 1.1.1.1  2008/05/07 04:15:08  Administrator
no message

Revision 1.1.1.1  2008/03/06 13:29:08  Lingzhaojun
no message

*********************************************************************/

#include "include.h"
#include  <stdlib.h>
#include "hw_include.h"
#include "hwapi_adc.h"
#include "hwapi_gpio.h"
#include "hw_touchpanel.h"

#if (CHIPTYPE == RK2700)
#define  X_CON   GPIOPortC_Pin3     //GPIOPortA_Pin7
#define  Y_CON   GPIOPortC_Pin2     //GPIOPortF_Pin2
#else
#define  X_CON   GPIOPortC_Pin3
#define  Y_CON   GPIOPortC_Pin2
#endif

//1 Touchpanel 竖屏
#if 0
static POINT gTouchCalibrateADPoint[] =
{
    {711, 668},     // 中心校准点
    {574, 828},     // 左上校准点
    {575, 504},      // 右上校准点
    {849, 827},     // 左下校准点
    {850, 501}       // 右下校准点
};
#else
#if 0
static POINT gTouchCalibrateADPoint[] =
{
    {668, 711},     // 中心校准点
    {506, 575},     // 左上校准点
    {503, 851},     // 右上校准点
    {829, 578},     // 左下校准点
    {829, 852}      // 右下校准点
};
#endif
static POINT gTouchCalibrateADPoint[] =
{
    {711, 668},     // 中心校准点
    {441, 341},     // 左上校准点
    {993, 340},     // 右上校准点
    {439, 1007},     // 左下校准点
    {997, 999}      // 右下校准点
};
#endif
#if 0
static POINT gTouchCalibrateLogicPoint[] =
{
    {X_RANGE >> 1, Y_RANGE >> 1},                           // 中心校准点
    {X_RANGE >> 2, Y_RANGE >> 2},                           // 左上校准点
    {((X_RANGE >> 1) + (X_RANGE >> 2)), Y_RANGE >> 2},          // 右上校准点
    {X_RANGE >> 2, ((Y_RANGE >> 1) + (Y_RANGE >> 2))},          // 左下校准点
    {((X_RANGE >> 1) + (X_RANGE >> 2)), ((Y_RANGE >> 1) + (Y_RANGE >> 2))}       // 右下校准点

};
#else
#if 0
static POINT gTouchCalibrateLogicPoint[] =
{
    {X_RANGE >> 1, Y_RANGE >> 1},                               // 中心校准点
    {X_RANGE >> 2, Y_RANGE >> 2},                               // 左上校准点
    {((X_RANGE >> 1) + (X_RANGE >> 2)), Y_RANGE >> 2},          // 右上校准点
    {X_RANGE >> 2, ((Y_RANGE >> 1) + (Y_RANGE >> 2))},          // 左下校准点
    {((X_RANGE >> 1) + (X_RANGE >> 2)), ((Y_RANGE >> 1) + (Y_RANGE >> 2))}   // 右下校准点
};
#endif
static POINT gTouchCalibrateLogicPoint[] =
{
    {X_RANGE >> 1, Y_RANGE >> 1},               // 中心校准点
    {0, 0},                                     // 左上校准点
    {0, Y_RANGE},                               // 右上校准点
    {X_RANGE, 0},                               // 左下校准点
    {X_RANGE, Y_RANGE}                          // 右下校准点
};
#endif
static POINT gTouchPointBak;
static POINT gTouchADPoint;
static POINT gTouchADPointBak;
static UINT16 gTouchCount;
static BOOL gIsCalibration = FALSE;
static UINT8 gCalibrateCount = 0;
static TOUCH_STATE gTouchState = TOUCH_NONE;
static UINT8 gTouchStateCount = 0;
static BOOL  gTouchXScan = FALSE;
static BOOL  gTouchYScan = TRUE;
/**************************************************************************
* 函数描述:  Touchpanel 的坐标转换
* 入口参数:  pTouchPixelPoint -- 坐标点
* 出口参数:  无
* 返回值:    无
* 注  释:    当Touchpanel原点和Lcd原点不一致时需要坐标转换
***************************************************************************/
void Touch_TransCoordinate(POINT* pTouchPixelPoint)
{
#ifdef Touch_90
    INT16 temp;
    temp = Y_RANGE - pTouchPixelPoint->y;
    pTouchPixelPoint->y = pTouchPixelPoint->x;
    pTouchPixelPoint->x = temp;

    pTouchPixelPoint->x = X_RANGE - pTouchPixelPoint->x;
    pTouchPixelPoint->y = Y_RANGE - pTouchPixelPoint->y;
#else

#endif
}

/**************************************************************************
* 函数描述:  得到Touchpanel的校准系数矩阵
* 入口参数:  pLogicPoint -- 校准的逻辑坐标点
*            pADPoint -- 校准的AD采样点
* 出口参数:  matrixPtr -- 校准的系数矩阵
* 返回值:    无
***************************************************************************/
int Touch_GetCalibrationMatrix(POINT * pLogicPoint, POINT * pADPoint, CALI_MATRIX * matrixPtr)
{
    // 将百分比放大
    matrixPtr->Ax = (abs(pLogicPoint[TopLeftPoint].x - pLogicPoint[CenterPoint].x) << 8) / abs(pADPoint[TopLeftPoint].x - pADPoint[CenterPoint].x);
    matrixPtr->Ay = (abs(pLogicPoint[TopLeftPoint].y - pLogicPoint[CenterPoint].y) << 8) / abs(pADPoint[TopLeftPoint].y - pADPoint[CenterPoint].y);
    matrixPtr->Bx = (abs(pLogicPoint[TopRightPoint].x - pLogicPoint[CenterPoint].x) << 8) / abs(pADPoint[TopRightPoint].x - pADPoint[CenterPoint].x);
    matrixPtr->By = (abs(pLogicPoint[TopRightPoint].y - pLogicPoint[CenterPoint].y) << 8) / abs(pADPoint[TopRightPoint].y - pADPoint[CenterPoint].y);
    matrixPtr->Cx = (abs(pLogicPoint[BottomLeftPoint].x - pLogicPoint[CenterPoint].x) << 8) / abs(pADPoint[BottomLeftPoint].x - pADPoint[CenterPoint].x);
    matrixPtr->Cy = (abs(pLogicPoint[BottomLeftPoint].y - pLogicPoint[CenterPoint].y) << 8) / abs(pADPoint[BottomLeftPoint].y - pADPoint[CenterPoint].y);
    matrixPtr->Dx = (abs(pLogicPoint[BottomRightPoint].x - pLogicPoint[CenterPoint].x) << 8) / abs(pADPoint[BottomRightPoint].x - pADPoint[CenterPoint].x);
    matrixPtr->Dy = (abs(pLogicPoint[BottomRightPoint].y - pLogicPoint[CenterPoint].y) << 8) / abs(pADPoint[BottomRightPoint].y - pADPoint[CenterPoint].y);

    return 0 ;

}

/**************************************************************************
* 函数描述:  Touchpanel 的去抖动
* 入口参数:  adPoint -- AD采样值
*            times -- 抖动次数
* 出口参数:  TRUE -- 去抖完成
*            FALSE -- 去抖未完成
* 返回值:    无
***************************************************************************/
BOOL Touch_RemoveDither(POINT adPoint , UINT16 times)
{
    if ((adPoint.x > X_MAX_LIMIT) || (adPoint.y > Y_MAX_LIMIT))
    {
        return FALSE;
    }

    if ((gTouchADPointBak.x == 0) || (gTouchADPointBak.y == 0))
        return FALSE;

    // 若两次AD值差别很大,说明是干扰值
    if ((abs(adPoint.x -gTouchADPointBak.x) > 8) || (abs(adPoint.y - gTouchADPointBak.y) > 8))
    {
        gTouchADPointBak.x = adPoint.x;
        gTouchADPointBak.y = adPoint.y;
        gTouchCount = 0;
        return FALSE;
    }

    if (gTouchCount > times)
    {
        gTouchADPointBak.x = adPoint.x;
        gTouchADPointBak.y = adPoint.y;
        return TRUE;
    }
    else
    {
        gTouchADPointBak.x = adPoint.x;
        gTouchADPointBak.y = adPoint.y;
        gTouchCount++;
        return FALSE;
    }

}

/**************************************************************************
* 函数描述:  得到Touchpanel坐标点
* 入口参数:  adPoint
* 出口参数:  pPixelPoint
* 返回值:    无
***************************************************************************/
void Touch_GetPixelPoint(POINT adPoint, POINT* pPixelPoint)
{
    CALI_MATRIX  matrixPtr;
    INT16 tempx, tempy;

    tempx = adPoint.x;
    tempy = adPoint.y;

    Touch_GetCalibrationMatrix(gTouchCalibrateLogicPoint, gTouchCalibrateADPoint, &matrixPtr);

    if (tempx >= gTouchCalibrateADPoint[CenterPoint].x)
    {
        if (tempy >= gTouchCalibrateADPoint[CenterPoint].y)   // 右下区域(D区)
        {
            pPixelPoint->x = (((UINT32)(tempx - gTouchCalibrateADPoint[CenterPoint].x) * matrixPtr.Dx) >> 8) + (X_RANGE >> 1);
            pPixelPoint->y = (((UINT32)(tempy - gTouchCalibrateADPoint[CenterPoint].y) * matrixPtr.Dy) >> 8) + (Y_RANGE >> 1);

        }
        else  // 右上区域(B区)
        {
            pPixelPoint->x = (((UINT32)(tempx - gTouchCalibrateADPoint[CenterPoint].x) * matrixPtr.Bx) >> 8) + (X_RANGE >> 1);
            pPixelPoint->y = (((UINT32)(tempy - gTouchCalibrateADPoint[TopRightPoint].y) * matrixPtr.By) >> 8);
        }
    }
    else
    {
        if (tempy <= gTouchCalibrateADPoint[CenterPoint].y)  // 左上区域(A区)
        {
            pPixelPoint->x = (((UINT32)(tempx - gTouchCalibrateADPoint[TopLeftPoint].x) * matrixPtr.Ax) >> 8);
            pPixelPoint->y = (((UINT32)(tempy - gTouchCalibrateADPoint[TopLeftPoint].y) * matrixPtr.Ay) >> 8);

        }
        else       // 左下区域(C区)
        {
            pPixelPoint->x = (((UINT32)(tempx - gTouchCalibrateADPoint[TopLeftPoint].x) * matrixPtr.Cx) >> 8);
            pPixelPoint->y = (((UINT32)(tempy -  gTouchCalibrateADPoint[CenterPoint].y) * matrixPtr.Cy) >> 8) + (Y_RANGE >> 1);
        }
    }

    if (pPixelPoint->x < 0) pPixelPoint->x = 0;
    if (pPixelPoint->y < 0) pPixelPoint->y = 0;
    if (pPixelPoint->x > ScreenW) pPixelPoint->x = ScreenW;
    if (pPixelPoint->y > ScreenH) pPixelPoint->y = ScreenH;

    Touch_TransCoordinate(pPixelPoint);

}

/**************************************************************************
* 函数描述:  Touchpanel校准初始化
* 入口参数:  无
* 出口参数:  无
* 返回值:    无
***************************************************************************/
void Touch_CalibrateInit(void)
{
    gIsCalibration = TRUE;
    gCalibrateCount = 0;
}

/**************************************************************************
* 函数描述:  Touchpanel上电初始化
* 入口参数:  无
* 出口参数:  无
* 返回值:    无
***************************************************************************/
void Touch_PowerOnInit(void)
{
    GPIO_SetPinDirection(X_CON, GPIO_OUT);
    GPIO_SetPinDirection(Y_CON, GPIO_OUT);
    gTouchADPoint.x = 0;
    gTouchADPoint.y = 0;
}

/**************************************************************************
* 函数描述:  得到Touchpanel状态
* 入口参数:  TouchPoint -- Touch坐标点
* 出口参数:  无
* 返回值:    Touchpanel的状态
***************************************************************************/
TOUCH_STATE Touch_GetState(POINT TouchPoint)
{
    TOUCH_STATE temp = TOUCH_NONE;

    if (gTouchState == TOUCH_SHORTPRESS)
    {
        if ((TouchPoint.x <= X_RANGE) && (TouchPoint.y <= Y_RANGE))
        {
            if ((TouchPoint.x == gTouchPointBak.x) && (TouchPoint.y == gTouchPointBak.y))
            {
                gTouchStateCount++;
            }
        }
        else
        {
            temp = TOUCH_UP;
        }
        if (gTouchStateCount == 5)
        {
            temp = TOUCH_LONGPRESS;
        }
    }
    else if (gTouchState == TOUCH_UP)
    {
        if ((TouchPoint.x <= X_RANGE) && (TouchPoint.y <= Y_RANGE))
        {
            temp = TOUCH_SHORTPRESS;
        }
    }

    gTouchPointBak.x = TouchPoint.x;
    gTouchPointBak.y = TouchPoint.y;

    return temp;
}

/**************************************************************************
* 函数描述:  Touchpanel的掉电反初始化
* 入口参数:  无
* 出口参数:  无
* 返回值:    无
***************************************************************************/
void Touch_PowerOffDeInit(void)
{


}

/**************************************************************************
* 函数描述:  Touchpanel扫描检测
* 入口参数:  adcValue -- AD采样值
*            adcChannel -- AD通道
* 出口参数:  无
* 返回值:    无
***************************************************************************/
void TouchScanning(unsigned short adcValue, ADC_channel_t adcChannel)
{
    TOUCH_MSG *touchMsg;
    POINT  pixelpoint;
    POINT  adPoint;
    touchMsg = TMALLOC(TOUCH_MSG);     // window.c中收到此消息会free
    if (adcChannel == Adc_channel1)     // AD采样y方向值
    {
        gTouchADPoint.y = adcValue;
        gTouchYScan = TRUE;
        GPIO_SetPinLevel(X_CON, GPIO_HIGH);
        GPIO_SetPinLevel(Y_CON, GPIO_LOW);
    }
    else if (adcChannel == Adc_channel2)    // AD采样x方向值
    {
        gTouchADPoint.x = adcValue;
        gTouchXScan = TRUE;
        GPIO_SetPinLevel(X_CON, GPIO_LOW);
        GPIO_SetPinLevel(Y_CON, GPIO_HIGH);
    }

    if ((gTouchXScan == TRUE) && (gTouchYScan == TRUE))
    {
        gTouchXScan = FALSE;
        gTouchYScan = FALSE;
        //    if(Touch_RemoveDither(gTouchADPoint,4)==FALSE)  // 去抖
        //      return;

        Touch_GetPixelPoint(gTouchADPoint, &pixelpoint);
        touchMsg->x = pixelpoint.x;
        touchMsg->y = pixelpoint.y;
        touchMsg->TouchState = Touch_GetState(pixelpoint);
        if (gIsCalibration == TRUE)
        {
            if (gCalibrateCount < 5)
            {
                gTouchCalibrateADPoint[gCalibrateCount].x = adPoint.x;
                gTouchCalibrateADPoint[gCalibrateCount].y = adPoint.y;
                gCalibrateCount ++;
            }
            else
            {
                gIsCalibration = FALSE;
                gCalibrateCount = 0;
            }
        }

#ifndef DRIVER_ONLY
        RockOSSendMsg(MBGUI , MS_TMGU_TouchInd , (void*)touchMsg) ;
#endif
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -