#include "GT7385.h"
#include "CT_I2C.h"
#include "stm32746g_LCD.h"

GT7385_Dev Dev_Now,Dev_Backup;

static void GT7385_Reset_Sequence()
{
	GPIO_InitTypeDef GPIO_InitStruct;
	
	GT7385_RST_CLK();
	GT7385_INT_CLK();
	
  GPIO_InitStruct.Pin = GT7385_RST_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
  HAL_GPIO_Init(GT7385_RST_PORT, &GPIO_InitStruct);
	
	GPIO_InitStruct.Pin = GT7385_INT_PIN;
  HAL_GPIO_Init(GT7385_INT_PORT, &GPIO_InitStruct);
	
	HAL_GPIO_WritePin(GT7385_RST_PORT,GT7385_RST_PIN,GPIO_PIN_RESET);    
	HAL_Delay(100);
	HAL_GPIO_WritePin(GT7385_INT_PORT,GT7385_INT_PIN,GPIO_PIN_RESET);    
	HAL_Delay(100);
	HAL_GPIO_WritePin(GT7385_RST_PORT,GT7385_RST_PIN,GPIO_PIN_SET);    
	HAL_Delay(300);
	
	GPIO_InitStruct.Pin = GT7385_INT_PIN;
  GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GT7385_INT_PORT, &GPIO_InitStruct);
	
	HAL_Delay(100);
}

static uint8_t GT7385_WR_Reg(uint16_t reg,uint8_t *buf,uint8_t len)
{
	uint8_t i;
	uint8_t ret=0;
	CT_I2C_Start();	
 	CT_I2C_Send_Byte(CT_CMD_WR);   
	CT_I2C_Wait_Ack();
	CT_I2C_Send_Byte(reg>>8);   	
	CT_I2C_Wait_Ack(); 	 										  		   
	CT_I2C_Send_Byte(reg&0XFF);   	
	CT_I2C_Wait_Ack();  
	for(i=0;i<len;i++)
	{	   
    CT_I2C_Send_Byte(buf[i]);  
		ret=CT_I2C_Wait_Ack();
		if(ret)break;  
	}
  CT_I2C_Stop();					
	return ret; 
}

static void GT7385_RD_Reg(uint16_t reg,uint8_t *buf,uint8_t len)
{
	uint8_t i;
 	CT_I2C_Start();	
 	CT_I2C_Send_Byte(CT_CMD_WR);  
	CT_I2C_Wait_Ack();
 	CT_I2C_Send_Byte(reg>>8);   
	CT_I2C_Wait_Ack(); 	 										  		   
 	CT_I2C_Send_Byte(reg&0XFF);   	
	CT_I2C_Wait_Ack();  
// 	CT_I2C_Stop();  

 	CT_I2C_Start();  	 	   
	CT_I2C_Send_Byte(CT_CMD_RD);     
	CT_I2C_Wait_Ack();	   
	for(i=0;i<len;i++)
	{	   
		buf[i]=CT_I2C_Read_Byte(i==(len-1)?0:1); 
	} 
  CT_I2C_Stop();   
}

static uint8_t GT7385_ReadStatue(void)
{
	uint8_t buf[4] = {0xAA, 0x00, 0x56, 0x00};
	
	GT7385_WR_Reg(GT7385_COMMAND_REG, buf, 3);
	HAL_Delay(100);
	
	GT7385_RD_Reg(GT7385_PRODUCT_ID_REG, buf, 4);
	printf("TouchPad_ID: 0x%x, 0x%x, 0x%x, 0x%x\r\n",buf[0],buf[1],buf[2],buf[3]);
	return *buf;
}

static uint16_t GT7385_ReadFirmwareVersion(void)
{
	uint8_t buf[3],FW_CID;
	uint16_t FW;

	GT7385_RD_Reg(GT7385_FIRMWARE_VERSION_REG, buf, 3);
	
	FW_CID = (buf[0]>>4) * 10 + buf[0]&0x0f ;
	FW = ((buf[1]>>4) * 10 + buf[1]&0x0f) * 100 + (buf[2]>>4) * 10 + buf[2]&0x0f;

	printf("FirmwareVersion_CID:  0x%x\r\nFirmwareVersion: 0x%x, 0x%x\r\n",buf[0],buf[1],buf[2]);
	return FW;
}

void GT7385_Scan(void)
{
	uint8_t buf[81];
  uint8_t Clearbuf = 0;
	uint8_t i;
	uint32_t PointColor[]={LCD_COLOR_BLUE,LCD_COLOR_GREEN,LCD_COLOR_RED,LCD_COLOR_MAGENTA,LCD_COLOR_YELLOW,LCD_COLOR_WHITE,LCD_COLOR_BROWN,LCD_COLOR_DARKCYAN,LCD_COLOR_GRAY,LCD_COLOR_ORANGE};
	
	if (Dev_Now.Touch == 1)
	{	
		Dev_Now.Touch = 0;
		GT7385_RD_Reg(GT7385_READ_XY_REG, buf, 1);		

		if ((buf[0]&0x80) == 0x00)
		{
			GT7385_WR_Reg(GT7385_READ_XY_REG, (uint8_t *)&Clearbuf, 1);
			//printf("%x\r\n",buf[0]);
			HAL_Delay(10);
		}
		else
		{
//			printf("bufstat:%x\r\n",buf[0]);
			Dev_Now.TouchpointFlag = buf[0];
			Dev_Now.TouchCount = buf[0]&0x0f;
			if (Dev_Now.TouchCount > 10)
			{
				GT7385_WR_Reg(GT7385_READ_XY_REG, (uint8_t *)&Clearbuf, 1);
				return ;
			}		
			GT7385_RD_Reg(GT7385_READ_XY_REG+1, &buf[1], Dev_Now.TouchCount*8);
			GT7385_WR_Reg(GT7385_READ_XY_REG, (uint8_t *)&Clearbuf, 1);
			
			printf("%d, %d\r\n",buf[6],buf[7]);
			
			for (i=0;i<Dev_Now.TouchCount;i++)
			{
				Dev_Now.Touchkeytrackid[i] = buf[1 + 8 * i];
				Dev_Now.X[i] = ((uint16_t)buf[3 + 8 * i] << 8) + buf[2 + 8 * i];
				Dev_Now.Y[i] = ((uint16_t)buf[5 + 8 * i] << 8) + buf[4 + 8 * i];
				Dev_Now.S[i] = ((uint16_t)buf[7 + 8 * i] << 8) + buf[6 + 8 * i];
			}

			for (i = 0; i< Dev_Backup.TouchCount;i++)
			{				
				BSP_LCD_SetTextColor(LCD_COLOR_BLACK);
				BSP_LCD_FillCircle(Dev_Backup.X[i],Dev_Backup.Y[i],20);	
				BSP_LCD_DrawVLine(Dev_Backup.X[i], 1, GT7385_MAX_HEIGHT - 1);
				BSP_LCD_DrawHLine(1, Dev_Backup.Y[i], GT7385_MAX_WIDTH - 1);
			}
			for (i=0;i<Dev_Now.TouchCount;i++)
			{
				if(Dev_Now.Y[i]<20)Dev_Now.Y[i]=20;
				if(Dev_Now.Y[i]>GT7385_MAX_HEIGHT-20)Dev_Now.Y[i]=GT7385_MAX_HEIGHT-20;
				if(Dev_Now.X[i]<20)Dev_Now.X[i]=20;
				if(Dev_Now.X[i]>GT7385_MAX_WIDTH-20)Dev_Now.X[i]=GT7385_MAX_WIDTH-20;
				
				Dev_Backup.X[i] = Dev_Now.X[i];
				Dev_Backup.Y[i] = Dev_Now.Y[i];
				Dev_Backup.TouchCount = Dev_Now.TouchCount;		

				BSP_LCD_SetTextColor(PointColor[i]);
				BSP_LCD_FillCircle(Dev_Now.X[i],Dev_Now.Y[i],20);	
				BSP_LCD_DrawVLine(Dev_Now.X[i], 1, GT7385_MAX_HEIGHT - 1);
				BSP_LCD_DrawHLine(1, Dev_Now.Y[i], GT7385_MAX_WIDTH - 1);
			}
		}	
	}
}

void GT7385_TEST(void)
{
//	printf("GT7385_TEST\r\n");
	GT7385_Reset_Sequence();

	CT_I2C_Init();
	
	GT7385_ReadStatue();
	
	GT7385_ReadFirmwareVersion();
	
	while(1)
	{
		GT7385_Scan();
	}
}
