触摸屏校正算法C实现

2019-04-13 22:09发布

/*
=======================================================================================
=== 函数功能:触摸屏重校正
=== 校正原理:x = a1 * Xts + a2
                   y = b1 * Yts + b2
=======================================================================================
*/
#define XLiftInitData 20
#define XRightInitData GUI_LCM_XMAX - XLiftInitData
#define Xlong   5
#define YUpInitData    XLiftInitData
#define YDownInitData GUI_LCM_YMAX - YUpInitData
#define Ylong   5   typedef struct xycoorbase{
       uint  X;
       uint  Y;
}XYCOORBASE;   typedef struct touchscreenbase{
       uchar vSignEnable;                                 //坐标数据有效位 1:有效
       uint ADS7843Xcoor;
       uint ADS7843Ycoor;
       uchar Y;
       uint  X;
       float perXts_xA;
       float perXts_xB; 
       float perYts_yA;
       float perYts_yB; 
}TOUCHSCREENBASE; TOUCHSCREENBASE TSCoor;                  // 触摸屏输入坐标信息
XYCOORBASE code vInitSetXYts[5]={{XLiftInitData,YUpInitData},
         {XRightInitData,YUpInitData},
          {XLiftInitData,YDownInitData},
         {XRightInitData,YDownInitData},
         {GUI_LCM_XMAX>>1,GUI_LCM_YMAX>>1} };
uchar TouchScreenReInit(void)
{    uchar vCycle;
      uint x,y;
       XYCOORBASE vInitInputXYts[5];
       for(vCycle=0;vCycle<5;vCycle++)
       {     GUI_HLine(vInitSetXYts[vCycle].X-Xlong,vInitSetXYts[vCycle].Y,vInitSetXYts[vCycle].X+Xlong);
              GUI_RLine(vInitSetXYts[vCycle].X,vInitSetXYts[vCycle].Y-Xlong,vInitSetXYts[vCycle].Y+Xlong);
              ModeWaitTSInput(0,0);
                vInitInputXYts[vCycle].X = TSCoor.ADS7843Xcoor;
                vInitInputXYts[vCycle].Y = TSCoor.ADS7843Ycoor;                 GUI_HLineClr(vInitSetXYts[vCycle].X-Xlong,vInitSetXYts[vCycle].Y,vInitSetXYts[vCycle].X+Xlong);
                GUI_RLineClr(vInitSetXYts[vCycle].X,vInitSetXYts[vCycle].Y-Xlong,vInitSetXYts[vCycle].Y+Xlong);
                Delayms(200);
        }
        vInitInputXYts[0].X = vInitInputXYts[0].X  + vInitInputXYts[2].X ;
        vInitInputXYts[1].X = vInitInputXYts[1].X  + vInitInputXYts[3].X ;
        vInitInputXYts[0].X = abs(vInitInputXYts[1].X-vInitInputXYts[0].X);
        vInitInputXYts[2].X = vInitSetXYts[0].X + vInitSetXYts[2].X;
        vInitInputXYts[3].X = vInitSetXYts[1].X + vInitSetXYts[3].X;
        vInitInputXYts[2].X = abs(vInitInputXYts[3].X-vInitInputXYts[2].X);
        TSCoor.perXts_xA = (float)vInitInputXYts[2].X /(float)vInitInputXYts[0].X;         TSCoor.perXts_xB = ((float)vInitInputXYts[3].X - TSCoor.perXts_xA * (float)vInitInputXYts[1].X)/2;
 
        vInitInputXYts[0].Y = vInitInputXYts[0].Y + vInitInputXYts[1].Y;
        vInitInputXYts[1].Y = vInitInputXYts[2].Y + vInitInputXYts[3].Y;
        vInitInputXYts[0].Y = abs(vInitInputXYts[1].Y-vInitInputXYts[0].Y);
        vInitInputXYts[2].Y = vInitSetXYts[0].Y + vInitSetXYts[1].Y;
        vInitInputXYts[3].Y = vInitSetXYts[2].Y + vInitSetXYts[3].Y;
        vInitInputXYts[2].Y = abs(vInitInputXYts[3].Y-vInitInputXYts[2].Y);
        TSCoor.perYts_yA = (float)vInitInputXYts[2].Y /(float)vInitInputXYts[0].Y;          TSCoor.perYts_yB = ((float)vInitInputXYts[3].Y - TSCoor.perYts_yA * (float)vInitInputXYts[1].Y)/2;         x = vInitInputXYts[4].X *TSCoor.perXts_xA + TSCoor.perXts_xB;
        y = vInitInputXYts[4].Y *TSCoor.perYts_yA + TSCoor.perYts_yB;
        x = abs(x-vInitSetXYts[4].X)+abs(y-vInitSetXYts[4].Y);
        if(x>10)
        {       GUI_PutString(100,100,"Touch Screen Calibrate Failed");
                return 0;
        }
        else
        {       GUI_PutString(70,100,"Touch Screen Calibrate succeed");
                GUI_PutString(70,120,"degree of precision     %");
                GUI_PutNum(185,120,100-x);
                SaveDatato120Flash(&TSCoor,sizeof(TOUCHSCREENBASE));
                return 1;
        }
}