#include "rtp.h"
#include "lcd.h"
#include <math.h>
#include "data.h"
#include "function.h"
#include "delay.h"
#include "adc_drv.h"
#include "eport_drv.h"
#include "pwm_drv.h"
#include "i2c_drv.h"
#include "tp_drv.h"
#include "function_drv.h"


#if R_TOUCH_FLAG
//X+:AIN[4]
//X-:AIN[6]
//Y+:AIN[5]
//Y-:AIN[7]
void ADC_Initial(void)
{
	ADC->CFGR2 |= ((10-1)<<8);         	 		//10MHz/10=1MHz
	ADC->CFGR2 |= 0x20;											// 启动计数位	
//	ADC->CFGR2 |= 2*72/6/12;							// 启动计数位	

	ADC->CFGR1 &=~(1<<20);								//不连续转换
	ADC->CFGR1 &=~(7<<24);								//序列长度为1单位
	ADC->CFGR1 &=~(3<<8);									//分辨率:12
	ADC->CFGR1 &=~(1<<10);								//右对齐
	ADC->SMPR = 0x20;	//采样时间
}

UINT16 Get_ADC_Val(UINT8 ADC_Channel_x)
{
	UINT32 adcisr = 0,i;
	
	UINT16 Val = 0;
	
	ADC_Initial();
	
	ADC->CHSELR1 = ADC_Channel_x;
	ADC_Cmd(ADC_EN);
	
	for (i = 0; i < 4; i++)
	{
		ADC_StartConv();
		DelayMS(1);
		while (EOSEQ !=(ADC->ISR & EOSEQ));
		ADC->ISR |= EOSEQ;
		ADC_StopConv();
		
		if(i == 0)
		{// 第一个数丢掉
			adcisr = (ADC->uFIFO_DAT.FIFO)&0xFFFF;
			adcisr = 0;
			continue;
		}
		adcisr += (ADC->uFIFO_DAT.FIFO)&0xFFFF;			
	}
	
	ADC_Cmd(ADC_DIS);

	return ((adcisr>>3)&0x0fff);
}

UINT8  TP_IF_PenDown(void)
{
	UINT16 ain0_Value;

	RTP_XM_OUT; RTP_XM_UP;   RTP_XM_LEVEL(0);       //X-配置为GPIO上拉输出低电平
	RTP_XP_OUT; RTP_XP_UP;   RTP_XP_LEVEL(0);       //X+配置为GPIO上拉输出高电平
	RTP_YM_IN; RTP_YM_UP;									      //Y-配置为GPIO上拉输入
	RTP_YP_IN; RTP_YP_UP;         							//Y+配置为GPIO上拉输入

	DelayUS(5);
	
//	Get_ADC_Val(ADCCH_0);
	ain0_Value = Get_ADC_Val(ADCCH_0);	// Y+取ADC值
	
//	printf("AIN0=%d\r\n",ain0_Value);

	if(ain0_Value<1000)
	{
//		printf("down:AIN0=%d\r\n",ain0_Value);
		return 0;
	}
	else 
	{
//		printf("up:AIN0=%d\r\n",ain0_Value);
		return 1;
	}
}

UINT16 TP_ReadX_Ver(void)
{
	UINT16 xValue0,tmpX;

	RTP_XM_OUT; RTP_XM_UP; RTP_XM_LEVEL(0);       //X-配置为输出 0
	RTP_XP_OUT; RTP_XP_UP; RTP_XP_LEVEL(1);       //X+配置为输出 1
	RTP_YM_IN; RTP_YM_DOWN;									      //Y-配置为输入
	RTP_YP_IN; RTP_YP_DOWN;         							//Y+配置为输入
	
	DelayUS(5);

	xValue0 =Get_ADC_Val(ADCCH_0);       	// Y+取ADC值
//	xValue0+=Get_ADC_Val(ADCCH_0);
//	xValue0+=Get_ADC_Val(ADCCH_0);
	
	tmpX= xValue0/1;
//	printf("XAD:%d\r\n",tmpX);
	return tmpX;
}

UINT16 TP_ReadY_Ver(void)
{
	UINT16 yValue0,tmpY;
	
	RTP_XM_IN;  RTP_XM_DOWN;        				   					//X-配置为输入
	RTP_XP_IN;  RTP_XP_DOWN;         					 					//X+配置为输入
	RTP_YM_OUT; RTP_YM_UP; RTP_YM_LEVEL(0);							//Y-配置为输出 0
	RTP_YP_OUT; RTP_YP_UP; RTP_YP_LEVEL(1);        			//Y+配置为输出 1
	
	DelayUS(5);

	yValue0 =Get_ADC_Val(ADCCH_1);       	// X+取ADC值
//	yValue0+=Get_ADC_Val(ADCCH_1);
//	yValue0+=Get_ADC_Val(ADCCH_1);

	tmpY= yValue0/1;
//	printf("YAD:%d\r\n",tmpY);

	return tmpY;
}

UINT16 TP_ReadX_Hor(void)
{
	UINT16 xValue0,tmpX;
	
	RTP_XM_IN;  RTP_XM_DOWN;        				   					//X-配置为输入
	RTP_XP_IN;  RTP_XP_DOWN;         					 					//X+配置为输入
	RTP_YM_OUT; RTP_YM_UP; RTP_YM_LEVEL(0);							//Y-配置为输出 0
	RTP_YP_OUT; RTP_YP_UP; RTP_YP_LEVEL(1);        			//Y+配置为输出 1
	
	DelayUS(5);

	xValue0 =Get_ADC_Val(ADCCH_1);       	// X+取ADC值
//	yValue0+=Get_ADC_Val(ADCCH_1);
//	yValue0+=Get_ADC_Val(ADCCH_1);

	tmpX= xValue0/1;
//	printf("YAD:%d\r\n",tmpY);

	return tmpX;
}

UINT16 TP_ReadY_Hor(void)
{
	UINT16 yValue0,tmpY;

	RTP_XM_OUT; RTP_XM_UP; RTP_XM_LEVEL(0);       //X-配置为输出 0
	RTP_XP_OUT; RTP_XP_UP; RTP_XP_LEVEL(1);       //X+配置为输出 1
	RTP_YM_IN; RTP_YM_DOWN;									      //Y-配置为输入
	RTP_YP_IN; RTP_YP_DOWN;         						  //Y+配置为输入
	
	DelayUS(5);

	yValue0 =Get_ADC_Val(ADCCH_0);        	// Y+取ADC值       
//	xValue0+=Get_ADC_Val(ADCCH_0);
//	xValue0+=Get_ADC_Val(ADCCH_0);
	
	tmpY= yValue0/1;
//	printf("XAD:%d\r\n",tmpX);
	return tmpY;
}

void LT_TpDelay(void)
{
	UINT8 t = 1;
	while(t--)
	{
		__asm("nop");
		__asm("nop");
		__asm("nop");
		__asm("nop");
		__asm("nop");

	}
}

UINT8 RTP_Init(void)
{
	ADC_Initial();
	DelayMS(10);
	if(LT_TpGetAdjdata()!=1)
	{
		DelayMS(100);
//		LCD_BL_Init(249);			//背光设置 0~499 
		RTP_Calibrate();
	}
	gTpInfo.scan = LT_TpScan;

	return 1;
}

//读取x,y坐标
//x,y:读取到的坐标值
UINT8 TP_Read_XY(UINT16 *x,UINT16 *y)
{
#if Horizontal_screen
	*x=TP_ReadX_Hor();
	*y=TP_ReadY_Hor();
#else
	*x=TP_ReadX_Ver();
	*y=TP_ReadY_Ver();
#endif

//	printf("XAD:%d  ",*x);
//	printf("YAD:%d\r\n\r\n",*y);
	if(*x>4095||*y>4095)	return 0;//读数失败
	return 1;
}

//连续2次读取触摸屏IC,且这两次的偏差不能超过
//ERR_RANGE,满足条件,则认为读数正确,否则读数错误.	   
//该函数能大大提高准确度
//x,y:读取到的坐标值
//返回值:0,失败;1,成功。
#define ERR_RANGE 50 //误差范围 
UINT8 TP_Read_XY2(UINT16 *x,UINT16 *y)
{
	UINT16 x1,y1;
 	UINT16 x2,y2;
 	UINT8 flag;

	flag=TP_Read_XY(&x1,&y1);   
	if(flag==0)return(0);
	
	flag=TP_Read_XY(&x2,&y2);
	if(flag==0)return(0);   

//	printf("X1:%d  ",x1);
//	printf("Y1:%d\r\n\r\n",y1);
//	printf("X2:%d  ",x2);
//	printf("Y2:%d\r\n\r\n",y2);

	if(((x2<=x1&&x1<x2+ERR_RANGE)||(x1<=x2&&x2<x1+ERR_RANGE))&&((y2<=y1&&y1<y2+ERR_RANGE)||(y1<=y2&&y2<y1+ERR_RANGE)))//前后两次采样在+-50内
	{
		*x=(x1+x2)/2;
		*y=(y1+y2)/2;
//		printf("X:%d\r\n",*x);
//		printf("Y:%d\r\n",*y);
			return 1;
	}
	else 
	{
		return 0;
	}
} 


//显示校正点
void LT_TpDrawTouchPoint(UINT16 x,UINT16 y,UINT32 color)
{
	LT268_TFT_DrawLine(x-12,y,x+13,y,color); //横线
	LT268_TFT_DrawLine(x,y-12,x,y+13,color); //竖线
	LT268_TFT_DrawCircle(x,y,6,color);       //画中心圈
}

//扫描TP
//返回：1->触摸  0->无触摸
UINT8 Yp_Pulldown_Flag=1;
UINT8 RTP_read_Flag=0;
UINT16 RTP_time=0;

UINT8 LT_TpScan(void)
{

	Yp_Pulldown_Flag = TP_IF_PenDown();
	
	if(Yp_Pulldown_Flag==0 && RTP_read_Flag==0)
	{
		RTP_read_Flag=1;                                                                    
		RTP_time=0;		
//		TP_Read_XY2(&gTpInfo.px,&gTpInfo.py);
//			gTpInfo.x[1]=0;
//			gTpInfo.y[1]=320;
		if(	TP_Read_XY2(&gTpInfo.px,&gTpInfo.py))
		{
			gTpInfo.x[1] = gTpInfo.xfac * gTpInfo.px + gTpInfo.xoff;
			gTpInfo.y[1] = gTpInfo.yfac * gTpInfo.py + gTpInfo.yoff;

		}
		if(gTpInfo.x[1]>LCD_XSIZE_TFT)	gTpInfo.x[1] = LCD_XSIZE_TFT;
		if(gTpInfo.y[1]>LCD_YSIZE_TFT)	gTpInfo.y[1] = LCD_YSIZE_TFT;
		
//		if(gTpInfo.x[1]==0 &&(gTpInfo.y[1]==320||gTpInfo.y[1]==319))  { return 0; }//松手瞬间坐标丢失
		
		if(First_press==0)
		{
			First_press=1;
			First_pressX= gTpInfo.x[1];
			First_pressY= gTpInfo.y[1];			
		}
			gTpInfo.x[0] = gTpInfo.x[1];
			gTpInfo.y[0] = gTpInfo.y[1];			
//		printf("X0: %d   Y0:  %d  \r\n",First_pressX,First_pressY);
//		printf("X: %d   Y:  %d  \r\n",gTpInfo.x[0],gTpInfo.y[0]);

		if(gTpInfo.sta == 0)	gTpInfo.sta = 1;

		return 1;
 	}
	else if(RTP_read_Flag)
	{
		return 0;   
	}
	else
	{
		First_press=0;
		RTP_read_Flag=0;
		gTpInfo.sta = 0;
		return 0;   
	}		
	
}

//提示字符串
UINT8* const TP_REMIND_MSG_TBL=(UINT8 *)"Please correct the resistance screen";


//触摸屏校准代码
//得到四个校准参数
UINT8 RTP_Calibrate(void)
{
	UINT8 cnt = 0;
	UINT16 outTime = 0;
	UINT16 d1,d2;
	UINT32 temp1,temp2;
	UINT16 posTemp[2][4] ;
	float fac;
	UINT8 flag = 0;
	UINT8 flag2 = 0;
	UINT8 show=0;
	LT268_TFT_DrawSquare_Fill(0,0,LCD_XSIZE_TFT-1,LCD_YSIZE_TFT-1,White);
	Display_Text(84,48,24,0,Red,White,(UINT8*)"Calibrate the touch screen");

	LT_TpDrawTouchPoint(20,20,Red);
	DelayMS(100);

	while(1)
	{
		if(flag2==1)
		{
			LT_TpDrawTouchPoint(LCD_XSIZE_TFT-20,LCD_YSIZE_TFT-20,White);
			LT_TpDrawTouchPoint(20,20,Red);
			Display_Text(40,40,24,0,Red,White,(UINT8*)"Check Failure,Please recheck!");
		}
		cnt=0;
		flag=0;
		gTpInfo.sta=0;
		while(1)//1
		{
			LT_TpScan();
			if(gTpInfo.sta)
			{
				posTemp[0][cnt] = gTpInfo.px;
				posTemp[1][cnt] = gTpInfo.py;
				flag=1;
				if(show==0)
				{
					show++;
					LT268_TFT_DrawSquare_Fill(0,0,LCD_XSIZE_TFT,LCD_YSIZE_TFT,White);
					LT_TpDrawTouchPoint(LCD_XSIZE_TFT-20,20,Red);//2
					}
				}
			if(gTpInfo.sta==0&&flag==1) break;
			}	
		DelayMS(300);
		cnt=1;
		flag=0;
		gTpInfo.sta=0;
		while(1)//2
		{
			LT_TpScan();
			if(gTpInfo.sta)
			{
				posTemp[0][cnt] = gTpInfo.px;
				posTemp[1][cnt] = gTpInfo.py;
				flag=1;
				if(show==1)
				{
					show++;
					LT268_TFT_DrawSquare_Fill(0,0,LCD_XSIZE_TFT,LCD_YSIZE_TFT,White);
					LT_TpDrawTouchPoint(20,LCD_YSIZE_TFT-20,Red);//3
				}
			}
			if(gTpInfo.sta==0&&flag==1) break;
		}	
		DelayMS(300);
		cnt=2;
		flag=0;
		gTpInfo.sta=0;
		while(1)//3
		{
			LT_TpScan();
			if(gTpInfo.sta)
			{
				posTemp[0][cnt] = gTpInfo.px;
				posTemp[1][cnt] = gTpInfo.py;
				flag=1;
				if(show==2)
				{
					show=0;
					LT268_TFT_DrawSquare_Fill(0,0,LCD_XSIZE_TFT,LCD_YSIZE_TFT,White);
					LT_TpDrawTouchPoint(LCD_XSIZE_TFT-20,LCD_YSIZE_TFT-20,Red);//4
					}
				}
				if(gTpInfo.sta==0&&flag==1) break;
			}
		DelayMS(300);
		cnt=3;
		flag=0;
		gTpInfo.sta=0;
		while(1)//4
		{
			LT_TpScan();
			if(gTpInfo.sta)
			{
				gTpInfo.sta=0;
				posTemp[0][cnt] = gTpInfo.px;
				posTemp[1][cnt] = gTpInfo.py;
				flag=1;
			}
			if(gTpInfo.sta==0&&flag==1) break;
		}

		temp1=abs(posTemp[0][0]-posTemp[0][1]);
		temp2=abs(posTemp[1][0]-posTemp[1][1]);
		temp1*=temp1;
		temp2*=temp2;
		d1=sqrt(temp1+temp2);

		temp1=abs(posTemp[0][2]-posTemp[0][3]);
		temp2=abs(posTemp[1][2]-posTemp[1][3]);
		temp1*=temp1;
		temp2*=temp2;
		d2=sqrt(temp1+temp2);
		fac = (float)d1/d2;

		if(fac<0.85f||fac>1.15f||d1==0||d2==0)
		{
			flag2=1;
			LT_TpDrawTouchPoint(LCD_XSIZE_TFT-20,LCD_YSIZE_TFT-20,White);
			LT_TpDrawTouchPoint(20,20,Red);
			Display_Text(40,40,24,0,Red,White,(UINT8*)"Check Failure,Please recheck!");
			DelayMS(1000);
			continue;
		}
	  //计算左右两条边长的比例
		temp1=abs(posTemp[0][0]-posTemp[0][2]);
		temp2=abs(posTemp[1][0]-posTemp[1][2]);
		temp1*=temp1;
		temp2*=temp2;
		d1=sqrt(temp1+temp2);

		temp1=abs(posTemp[0][1]-posTemp[0][3]);
		temp2=abs(posTemp[1][1]-posTemp[1][3]);
		temp1*=temp1;
		temp2*=temp2;
		d2=sqrt(temp1+temp2);

		//								LTPrintf("fac:%d \r\n",fac);
		if(fac<0.85f||fac>1.15f||d1==0||d2==0)
		{
			flag2=1;
			LT_TpDrawTouchPoint(LCD_XSIZE_TFT-20,LCD_YSIZE_TFT-20,White);
			LT_TpDrawTouchPoint(20,20,Red);
			Display_Text(40,40,24,0,Red,White,(UINT8*)"Check Failure,Please recheck!");
			DelayMS(1000);
			continue;

		}

		//计算对边长度的比例
		temp1=abs(posTemp[0][0]-posTemp[0][3]);
		temp2=abs(posTemp[1][0]-posTemp[1][3]);
		temp1*=temp1;
		temp2*=temp2;
		d1=sqrt(temp1+temp2);

		temp1=abs(posTemp[0][1]-posTemp[0][2]);
		temp2=abs(posTemp[1][1]-posTemp[1][2]);
		temp1*=temp1;
		temp2*=temp2;
		d2=sqrt(temp1+temp2);

		fac = (float)d1/d2;

		if(fac<0.85f||fac>1.15f||d1==0||d2==0)
		{
		flag2=1;
		LT_TpDrawTouchPoint(LCD_XSIZE_TFT-20,LCD_YSIZE_TFT-20,White);
		LT_TpDrawTouchPoint(20,20,Red);
		Display_Text(40,40,24,0,Red,White,(UINT8*)"Check Failure,Please recheck!");
		flag2=1;
		DelayMS(1000);
		continue;

		}

		//上下两边比例、左右两边比例、对角边的比例，都在范围内，则4个校正点符合要求
		gTpInfo.xfac=(float)(LCD_XSIZE_TFT-40)/(posTemp[0][1]-posTemp[0][0]);
		gTpInfo.xoff=(LCD_XSIZE_TFT-gTpInfo.xfac*(posTemp[0][1]+posTemp[0][0]))/2;
		gTpInfo.yfac=(float)(LCD_YSIZE_TFT-40)/(posTemp[1][2]-posTemp[1][0]);
		gTpInfo.yoff=(LCD_YSIZE_TFT-gTpInfo.yfac*(posTemp[1][2]+posTemp[1][0]))/2;

		gTpxfac.xfac=gTpInfo.xfac;
		gTpxoff.xoff=gTpInfo.xoff;
		gTpyfac.yfac=gTpInfo.yfac;
		gTpyoff.yoff=gTpInfo.yoff;

		LT268_TFT_DrawSquare_Fill(0,0,LCD_XSIZE_TFT,LCD_YSIZE_TFT,White);
		Display_Text(40,40,24,0,Red,White,(UINT8*)"Touch Screen Adjust OK!");//????
		DelayMS(1000);

		//保存4个参数
		LT_TpSaveAdjdata();
		return 1;
		}
}

#endif



