/*********************************************************************
*                SEGGER Microcontroller GmbH & Co. KG                *
*        Solutions for real time microcontroller applications        *
**********************************************************************
*                                                                    *
*        (c) 1996 - 2012  SEGGER Microcontroller GmbH & Co. KG       *
*                                                                    *
*        Internet: www.segger.com    Support:  support@segger.com    *
*                                                                    *
**********************************************************************

** emWin V5.16 - Graphical user interface for embedded applications **
All  Intellectual Property rights  in the Software belongs to  SEGGER.
emWin is protected by  international copyright laws.  Knowledge of the
source code may not be used to write a similar product.  This file may
only be used in accordance with the following terms:

The software has been licensed to  ARM LIMITED whose registered office
is situated at  110 Fulbourn Road,  Cambridge CB1 9NJ,  England solely
for  the  purposes  of  creating  libraries  for  ARM7, ARM9, Cortex-M
series,  and   Cortex-R4   processor-based  devices,  sublicensed  and
distributed as part of the  MDK-ARM  Professional  under the terms and
conditions  of  the   End  User  License  supplied  with  the  MDK-ARM
Professional. 
Full source code is available at: www.segger.com

We appreciate your understanding and fairness.
----------------------------------------------------------------------
File        : GUI_TOUCH_X.C
Purpose     : Config / System dependent externals for GUI
---------------------------END-OF-HEADER------------------------------
*/
#include "GUI.h"
#include "touch.h"
#include "ILI93xx.h"
#include "usart.h"


void GUI_TOUCH_X_ActivateX(void) 
{
 // XPT2046_WriteCMD(0x90);
}


void GUI_TOUCH_X_ActivateY(void)
{
  //XPT2046_WriteCMD(0xd0);
}

const u16 GT9147_TPX_TBL1[5]={GT_TP1_REG,GT_TP2_REG,GT_TP3_REG,GT_TP4_REG,GT_TP5_REG};
//GT9147扫描触摸屏(采用查询方式)
//mode:0,正常扫描.
//返回值:当前触屏状态.
//0,触屏无触摸;1,触屏有触摸
u8 GT9147Touch_Scan(u8 mode)
{
	u8 buf[4];
	u8 res=0;
	u8 temp;
	GT9147_RD_Reg(GT_GSTID_REG,&mode,1);//读取触摸点的状态  
	if((mode&0XF)&&((mode&0XF)<6))
	{
		temp=0XFF<<(mode&0XF);//将点的个数转换为1的位数,匹配tp_dev.sta定义 
		tp_dev.sta=(~temp)|TP_PRES_DOWN|TP_CATH_PRES; 
		if(tp_dev.sta&0X01)	//触摸有效?
		{
			GT9147_RD_Reg(GT9147_TPX_TBL1[0],buf,4);	//读取XY坐标值
			tp_dev.x[0]=((u16)buf[1]<<8)+buf[0];
			tp_dev.y[0]=((u16)buf[3]<<8)+buf[2];
		}
		res=1;
		if(tp_dev.x[0]==0 && tp_dev.y[0]==0)mode=0;	//读到的数据都是0,则忽略此次数据
	}
 	if(mode&0X80&&((mode&0XF)<6))
	{
		temp=0;
		GT9147_WR_Reg(GT_GSTID_REG,&temp,1);//清标志 		
	}

	if((mode&0X8F)==0X80)//无触摸点按下
	{ 
		if(tp_dev.sta&TP_PRES_DOWN)	//之前是被按下的
		{
			tp_dev.sta&=~(1<<7);	//标记按键松开
		}else						//之前就没有被按下
		{ 
			tp_dev.x[0]=0xffff;
			tp_dev.y[0]=0xffff;
			tp_dev.sta&=0XE0;	//清除点有效标记	
		}	 
	} 	
	return res;
}

//获取电容触摸屏的X轴值
int32_t  Get_CapacTouchValueX(u8 mode)
{
	u8 buf[4];
	int32_t lost_x;
	if(tp_dev.capactouchid == 2001)		//电容触摸芯片为OTT2001A
	{
		if(mode & 0X1F) //有触摸点按下时
		{
			OTT2001A_RD_Reg(0X0100,buf,4);	//此处只读取电容触摸屏的x[0],y[0]
			tp_dev.x[0] = (((u16)buf[2]<<8)+buf[3])*OTT_SCAL_X;
			lost_x = tp_dev.x[0]<<3;	//左移3位，扩大8倍
			if(lost_x < 0) lost_x=0;
		}else lost_x = 0;
	}
	else if(tp_dev.capactouchid == 9147) //电容触摸芯片为GT9147
	{
		GT9147Touch_Scan(0);
		lost_x = tp_dev.x[0]<<3; //左移3位，扩大8倍
	}
	return lost_x;
}

//获取电容触摸屏的Y轴值
int32_t Get_CapacTouchValueY(u8 mode)
{
	u8 buf[4];
	int32_t lost_y;
	if(tp_dev.capactouchid == 2001)		//电容触摸芯片为OTT2001A
	{
		if(mode & 0X1F) //有触摸点按下时
		{
			OTT2001A_RD_Reg(0X0100,buf,4);//此处只读取电容触摸屏的x[0],y[0]
			tp_dev.y[0] = (((u16)buf[0]<<8)+buf[1])*OTT_SCAL_Y;
			lost_y = tp_dev.y[0]<<3;		//左移3位 扩大8倍
			if(lost_y < 0) lost_y = 0;
		}else lost_y = 6576;
	}
	else if(tp_dev.capactouchid == 9147) //电容触摸芯片为GT9147
	{
		GT9147Touch_Scan(0);
		
		lost_y=tp_dev.y[0]<<3;//左移3位 扩大8倍
	}
	return lost_y;
}

int  GUI_TOUCH_X_MeasureX(void) 
{
	u8 mode; 
	int32_t xvalue;
	if(lcddev.id == 0X5510) //电容屏的触摸值获取
	{
		if(tp_dev.capactouchid == 2001) OTT2001A_RD_Reg(OTT_GSTID_REG,&mode,1);
		
		xvalue= Get_CapacTouchValueX(mode);
		return xvalue;
	}else				//电阻屏
	{
		return TP_Read_XOY(0XD0);  //CMD_RDX=0XD0
	}
}

int  GUI_TOUCH_X_MeasureY(void) 
{	
	u8 mode;
	int32_t yvalue;
	if(lcddev.id == 0X5510) //电容屏的触摸值获取
	{
		if(tp_dev.capactouchid == 2001) OTT2001A_RD_Reg(OTT_GSTID_REG,&mode,1);
		
		yvalue=Get_CapacTouchValueY(mode);
		return yvalue;
	}else				//电阻屏
	{
		return TP_Read_XOY(0X90);  //CMD_RDX=0XD0
	}
}

