#include "ctp_drv.h"
#include "tp_drv.h"
#include "delay.h"
#include "lcd_func.h"

//-------------------------------------------- 适配CTP触摸IC: GT911、CJT540、FT5216 ------------------------------
#if TOUCHPANEL == CAPACITIVE

extern u16 LCD_XSIZE_TFT, LCD_YSIZE_TFT;

//touch
UINT8 First_press=0;
UINT16 First_pressX,First_pressY;
TP_INFO gTpInfo;

//rtp
TP_xfac gTpxfac;
TP_xoff gTpxoff;
TP_yfac gTpyfac;
TP_yoff gTpyoff;


u8 READ_BUFF[128];
u8 WRITE_BUFF[20];
u8 ctp_active_index =0;

/*****************电容触摸屏相关IO口初始化函数************************/
//---------------------------------清写入和读出缓冲器-------------------------//
void ClearBuff(unsigned char *p)
{
   int i = 0;
	for(; i<20; i++)
	{
	    *(p + i) = 0;
	}
}
/*******************获取中断脚电平函数，当有触摸触摸屏时返回值为0************************/
u8 TP_Get_INT(void)
{
	if(GET_INT_Bit)
		return 1;
	else
		return 0;
}
/******************************************************************************
 * DESCRIPTION: 写触摸屏寄存器
 ******************************************************************************/
u8 CTP_BufferWrite(u8* pBuffer, u16 WriteAddr, u16 NumByte)
{
	u8 ret=0,i;
	
	I2C_Start();
	
	I2C_write_byte((CTP_ADDR) | 0);             //从地址+写操作
	I2C_WaitAck();
	if((WriteAddr>>8)!=0)
	{
		I2C_write_byte(WriteAddr>>8);              //写寄存器地址
		I2C_WaitAck();
	}
		I2C_write_byte(WriteAddr&0xFF);
		I2C_WaitAck();

	while(NumByte--)
	{
		I2C_write_byte(* pBuffer);               //写数据
		I2C_WaitAck();
		pBuffer++;
	}	
	
	I2C_Stop();
	
	return 0;	
}

/******************************************************************************
 * DESCRIPTION: 从触摸屏中读出数据
 ******************************************************************************/
u8 CTP_BufferRead(u8* pBuffer, u16 ReadAddr, u16 NumByte)
{
	I2C_Start();
	I2C_write_byte(CTP_ADDR | 0);        		//从地址+写操作
	I2C_WaitAck();
	if((ReadAddr>>8)!=0)
	{
		I2C_write_byte(ReadAddr>>8);         	//写寄存器地址					
		I2C_WaitAck();
	}
		I2C_write_byte(ReadAddr&0xFF);						
		I2C_WaitAck();
	I2C_Stop();
	
	I2C_Start();
	I2C_write_byte(CTP_ADDR | 0x01);     		//从地址+读操作
	I2C_WaitAck();
	while(NumByte--)
	{
		*pBuffer = I2C_read_byte();
		if(NumByte == 0)	I2C_SendAck(1);
		else							I2C_SendAck(0);
		pBuffer++;
	}
	I2C_Stop();
	  
	return 0;	
}

//读坐标---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
bool CTP_ReadXY(UINT8 tpType,UINT16 *tpX,UINT16 *tpY)
{
	UINT8 temp=0;
	UINT16 x,y;
	
	if(ctp_active_index==0) return 0;
	
	if(cjt540flag)
	{
		READ_BUFF[0]=0;
		CTP_BufferRead(READ_BUFF,0xD000,7);
		temp=READ_BUFF[0];
		//printf("temp = 0x%x \r\n",temp);
		if(((temp & 0x0F) >> 1) ==0x03) //buffer status 为1时才是数据有效
		{
			x = (READ_BUFF[1]<<4)|((READ_BUFF[3]>>4)&0x0F);
			y = (READ_BUFF[2]<<4)|(READ_BUFF[3]&0x0F);
			
			gTpInfo.x[0] = x;
			gTpInfo.y[0] = y;
			
			gTpInfo.sta = 1;
			if(gTpInfo.x[0]>LCD_XSIZE_TFT)	gTpInfo.x[0] = LCD_XSIZE_TFT;
			if(gTpInfo.y[0]>LCD_YSIZE_TFT)	gTpInfo.y[0] = LCD_YSIZE_TFT;
			if(First_press==0)
			{
				First_press=1;
				First_pressX= gTpInfo.x[0];
				First_pressY= gTpInfo.y[0];			
			}			
			//printf("x=%d y=%d\r\n",x,y);
			//printf("gTpInfo.x=%d gTpInfo.y=%d\r\n",gTpInfo.x[0],gTpInfo.y[0]);
			
			*tpX=gTpInfo.x[0];
			*tpY=gTpInfo.y[0];
			
			READ_BUFF[0]=0xAB;
			CTP_BufferWrite(READ_BUFF,0xD000,1);
			return 1;
		}
		else
		{
			gTpInfo.sta = 0;
			First_press=0;
			return 0;
		}
	}
	else if(gt911flag)
	{
		CTP_BufferRead(READ_BUFF,0x814E,1);
		temp=READ_BUFF[0]&0x8F;
		//printf("temp = 0x%x  \r\n",temp);
 		if(temp>0x80)   		    															// buffer status ==1 && number of touch points > 0
		{
				CTP_BufferRead(READ_BUFF,0x814F,5*8);   //read
				//printf("track id = 0x%x   \r\n",READ_BUFF[0] ); //track id
				READ_BUFF[0]=0;
				CTP_BufferWrite(READ_BUFF,0x814E,1);   						//clear
				
				x = READ_BUFF[2]<<8 | READ_BUFF[1];
				y = READ_BUFF[4]<<8 | READ_BUFF[3];
				
				gTpInfo.x[0] = x*2/3;                
				gTpInfo.y[0] = y*2/3;

				if(gTpInfo.x[0]>LCD_XSIZE_TFT)	gTpInfo.x[0] = LCD_XSIZE_TFT;
				if(gTpInfo.y[0]>LCD_YSIZE_TFT)	gTpInfo.y[0] = LCD_YSIZE_TFT;

				if(First_press==0)
				{
					First_press=1;
					First_pressX= gTpInfo.x[0];
					First_pressY= gTpInfo.y[0];
				//printf("First_press  x=%d y=%d\r\n",First_pressX,First_pressY);
				}
				//printf("x=%d y=%d\r\n",gTpInfo.x[0],gTpInfo.y[0]);
					
				*tpX=gTpInfo.x[0];
				*tpY=gTpInfo.y[0];
				
				gTpInfo.sta = 1;
				gTpInfo.readspace=0;
				return 1;
		}
		else if(gTpInfo.sta&&(temp==0))               			//wait data prepare
		{
			gTpInfo.readspace++;         
			if(gTpInfo.readspace>8) gTpInfo.sta = 0;
			return 0;
		}
		else                                         				 //left
		{
			READ_BUFF[0]=0;
			CTP_BufferWrite(READ_BUFF,0x814E,1);

			gTpInfo.sta = 0;
			gTpInfo.readspace=0;
			First_press=0;
			return 0;				
		}
	}
	else if(ft5216flag)
	{
		CTP_BufferRead(READ_BUFF,0x02,1);																		//Number of touch points
		if(READ_BUFF[0]>0) 
		{
			CTP_BufferRead(READ_BUFF,0x03,5*6);
			temp = READ_BUFF[0]>>6;					  																 //bits 00: 按下  01:抬起  10：保持   11:无事件
		}else temp = 3;
		
		if(temp==0 || temp==2)
		{
			x = (READ_BUFF[1]<<8) |READ_BUFF[2];		
			y = (READ_BUFF[3]<<8) |READ_BUFF[4];

			gTpInfo.x[0] = x;                
			gTpInfo.y[0] = y;
			
			if(gTpInfo.x[0]>LCD_XSIZE_TFT)	gTpInfo.x[0] = LCD_XSIZE_TFT;
			if(gTpInfo.y[0]>LCD_YSIZE_TFT)	gTpInfo.y[0] = LCD_YSIZE_TFT;

			if(First_press==0)
			{
				First_press=1;
				First_pressX= gTpInfo.x[0];
				First_pressY= gTpInfo.y[0];			
			}
				//printf("x=%d y=%d\r\n",gTpInfo.x[0],gTpInfo.y[0]);
			
			*tpX=gTpInfo.x[0];
			*tpY=gTpInfo.y[0];
			
			gTpInfo.sta = 1;
			return 1;
		}
		else if(temp==3 || temp==1)
		{
			gTpInfo.sta = 0;
			First_press=0;
			return 0;
		}
	}
}


UINT8 CTP_Scan(void)
{
	/*
	if(ctp_active_index==1)
	{
		//printf("read\r\n");
		CTP_ReadXY();
		ctp_active_index = 0;
		return 1;
	}
	else
	{
		return 0;
	}
	*/
}
 
 
 
void CTP_Init(u8 tpType)
{
	CTP_io_Init();
	gTpInfo.scan = CTP_Scan;
}

#endif

