/*******************************************************************************
*
*
* File:        app_tp_for_custom.c
* Description  tp 协议处理文件，包括报点协议转换、start/sleep/awake 等固定传输数据的配置
* Version      V0.1
* Date         2021-10-14
* Author       zhanghz
*******************************************************************************/
#include "test_cfg_global.h"

#if _DEMO_MI8SE_EN
#include "app_tp_for_custom_mi8se.h"
#include "app_tp_phone_transfer_data_mi8se.h"
#include "app_tp_screen_transfer_data_mi8se.h"
#include "tau_log.h"
#include "tau_delay.h"
#include "hal_pwm.h"


//static uint8_t s8_event_id = 0x00;
bool tp_flag = false;


uint8_t s8_host_num[11] = {0x02,0x02,0x04,0x06,0x08,0x0A,0x0C,0x0E,0x10,0x12,0x14};		// 有多少个报点对应的返回值。模组为0和1时都是读1个报点
uint8_t s8_host_id[10] = {0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90};				// 各个报点的状态。BIT3~BIT0: 3起始/5连续/4抬起

extern uint8_t read_point;
extern uint8_t s_screen_number[2];
extern uint8_t s_screen_read_bak[80];

io_pad_e g_screen_input_rst_pad = IO_PAD_TD_TPRSTN;
io_pad_e g_screen_input_int_pad = IO_PAD_TD_INT;
io_pad_e g_phone_input_rst_pad  = IO_PAD_AP_TPRSTN;
io_pad_e g_phone_output_int_pad = IO_PAD_AP_INT;

#ifdef USE_FOR_SUMSUNG_S10
uint8_t Flag_EA_EN=0;

uint8_t Flag_touch_count = 0;
uint8_t touch_send_flag = 0;
uint8_t touch_recv_flag = 0;
	
uint8_t touchnum_bak = 0;
uint8_t Flag_blacklight_EN=0;
uint16_t first_touch=0;
uint16_t first_send=0;
uint16_t flnger_state_flg=0;
extern uint16_t u16TouchID;	
#endif

/*************************************报点协议 master 相关 buffer ****************************************/

uint8_t screen_data_write_1[1] = {0x04};                                        //触点/坐标 数据读取对应的发送buffer
uint8_t screen_data_write_2[1] = {0x08};                                        //配置buffer
#ifdef USE_FOR_SUMSUNG_S10
uint8_t screen_data_write_3[1] = {0x00};                                        //配置buffer
#else
uint8_t screen_data_write_3[1] = {0x06};                                        //配置buffer
#endif

uint8_t screen_data_write_4[2] = {0xFF,0x00};
uint8_t screen_data_write_5[2] = {0x1F,0xFF};
uint8_t screen_data_write_6[2] = {0x0E,0x24};
uint8_t screen_data_write_7[2] = {0x24,0x01};
uint8_t screen_data_write_8[2] = {0x0D,0x00};
uint8_t screen_data_write_9[2] = {0x0E,0x00};
uint8_t screen_data_write_10[2] = {0x0E,0x07};
uint8_t screen_data_write_11[1] = {0x0D};
uint8_t screen_data_write_12[1] = {0x05};

st_screen_data screen_reg_int_data[] = {{screen_data_write_1, 1, 2, SCREEN_TRANSFER_READ},\
	{screen_data_write_2, 1, 2, SCREEN_TRANSFER_READ},\
	{screen_data_write_3, 1, 8, SCREEN_TRANSFER_READ}
};
const uint8_t screen_reg_int_data_size = sizeof(screen_reg_int_data) / sizeof(st_screen_data);//结构体数组的元素个数，数组按照协议发送数据先后排列
/***********************************报点协议 master 相关 buffer 截止**************************************/

/***********************************开机屏幕初始化 master 相关 buffer ************************************/
// 数组按照协议发送数据先后排列
#if 1

#if 0
st_screen_const_data screen_reg_start_data[] = {{screen_data_write_4, 2, 2, SCREEN_TRANSFER_WRITE},\
	{screen_data_write_11, 1, 1, SCREEN_TRANSFER_READ},\
	{screen_data_write_8, 2, 2, SCREEN_TRANSFER_WRITE},\
	{screen_data_write_9, 2, 2, SCREEN_TRANSFER_WRITE},\
	{screen_data_write_12, 1, 1, SCREEN_TRANSFER_READ},\
	{screen_data_write_10, 2, 2, SCREEN_TRANSFER_WRITE},\
};

#else
st_screen_const_data screen_reg_start_data[] = {{screen_data_write_4, 2, 2, SCREEN_TRANSFER_WRITE},\
	{screen_data_write_5, 2, 2, SCREEN_TRANSFER_WRITE},\
	{screen_data_write_6, 2, 8, SCREEN_TRANSFER_WRITE},\
	{screen_data_write_7, 2, 8, SCREEN_TRANSFER_WRITE},\
};
#endif

#else
st_screen_const_data screen_reg_start_data[] = {{screen_data_write_1, 1, 2, SCREEN_TRANSFER_READ},\
	{screen_data_write_2, 1, 2, SCREEN_TRANSFER_READ}
};
#endif

const uint8_t screen_reg_start_data_size = sizeof(screen_reg_start_data) / sizeof(st_screen_const_data);
/*********************************开机屏幕初始化 master 相关 buffer 截止**********************************/

#ifdef TP_ADD_ESD_CHECK  // 解决待机有时无TP的问题
bool tp_get_ac = false;
bool ap_tp_int_enable = false;
static bool ap_int_first = true;
static bool ap_int_second = true;
static volatile bool g_send_ed = false;
#endif

/*************************************报点协议 slave 相关 buffer******************************************/
uint8_t phone_reg_point_data[] = {0xB6, 0x00, 0x23};
uint8_t phone_reg_point_back[] = {0x05, 0x02};

uint8_t phone_reg_coord_data[] = {0x86};                                          //坐标 数据反馈对应的接收数据

uint8_t phone_reg_coord_back[80] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; //坐标 数据反馈对应的数据buffer


st_reg_data phone_reg_int_data[] = { \
    {3, 2, phone_reg_point_data, phone_reg_point_back}, \
    {1, 80, phone_reg_coord_data, phone_reg_coord_back} \
};
uint8_t phone_reg_int_size = sizeof(phone_reg_int_data) / sizeof(phone_reg_int_data[0]);
/***********************************报点协议 slave 相关 buffer 截止***************************************/

/****************************开机阶段slave需要反馈的数据及其对应的接收开头********************************/

st_reg_const_data phone_reg_const_data[] = { \
/*    {sizeof(phone_b6_00_28_data), sizeof(phone_b6_00_28_back), phone_b6_00_28_data, phone_b6_00_28_back}, \
    {sizeof(phone_b6_00_08_data), sizeof(phone_b6_00_08_back), phone_b6_00_08_data, phone_b6_00_08_back}, \
    {sizeof(phone_b6_00_04_data), sizeof(phone_b6_00_04_back), phone_b6_00_04_data, phone_b6_00_04_back}, \
    {sizeof(phone_b8_00_08_data), sizeof(phone_b8_00_08_back), phone_b8_00_08_data, phone_b8_00_08_back}, \
    {sizeof(phone_ac_data), sizeof(phone_ac_back), phone_ac_data, phone_ac_back}, \
    {sizeof(phone_a7_data), sizeof(phone_a7_back), phone_a7_data, phone_a7_back}, \
    {sizeof(phone_84_data), sizeof(phone_84_back), phone_84_data, phone_84_back}, \
    {sizeof(phone_aa_00_data), sizeof(phone_aa_00_back), phone_aa_00_data, phone_aa_00_back}, \
    {sizeof(phone_aa_01_data), sizeof(phone_aa_01_back), phone_aa_01_data, phone_aa_01_back}, \
    {sizeof(phone_d0_00_5a_data), sizeof(phone_d0_00_5a_back), phone_d0_00_5a_data, phone_d0_00_5a_back}, \
    {sizeof(phone_d0_00_5c_data), sizeof(phone_d0_00_5c_back), phone_d0_00_5c_data, phone_d0_00_5c_back}, \
    {sizeof(phone_d0_00_64_data), sizeof(phone_d0_00_64_back), phone_d0_00_64_data, phone_d0_00_64_back}, \
    {sizeof(phone_d0_00_88_data), sizeof(phone_d0_00_88_back), phone_d0_00_88_data, phone_d0_00_88_back}, \
    {sizeof(phone_d0_00_7a_data), sizeof(phone_d0_00_7a_back), phone_d0_00_7a_data, phone_d0_00_7a_back}, \
    {sizeof(phone_d0_76_04_data), sizeof(phone_d0_76_04_back), phone_d0_76_04_data, phone_d0_76_04_back}, \
    {sizeof(phone_d0_1f_6e_data), sizeof(phone_d0_1f_6e_back), phone_d0_1f_6e_data, phone_d0_1f_6e_back}, \
    {sizeof(phone_d0_00_50_data), sizeof(phone_d0_00_50_back), phone_d0_00_50_data, phone_d0_00_50_back}, \
    {sizeof(phone_d0_1f_b3_data), sizeof(phone_d0_1f_b3_back), phone_d0_1f_b3_data, phone_d0_1f_b3_back}, \
    {sizeof(phone_d0_1f_6c_data), sizeof(phone_d0_1f_6c_back), phone_d0_1f_6c_data, phone_d0_1f_6c_back}, \
    {sizeof(phone_d0_1f_b2_data), sizeof(phone_d0_1f_b2_back), phone_d0_1f_b2_data, phone_d0_1f_b2_back}, \
    {sizeof(phone_d0_1f_72_data), sizeof(phone_d0_1f_72_back), phone_d0_1f_72_data, phone_d0_1f_72_back}, \
    {sizeof(phone_d0_00_06_data), sizeof(phone_d0_00_06_back), phone_d0_00_06_data, phone_d0_00_06_back}, \
    {sizeof(phone_d0_23_ac_data), sizeof(phone_d0_23_ac_back), phone_d0_23_ac_data, phone_d0_23_ac_back}, \
    {sizeof(phone_d0_24_2c_data), sizeof(phone_d0_24_2c_back), phone_d0_24_2c_data, phone_d0_24_2c_back}, \
    {sizeof(phone_d0_24_ac_data), sizeof(phone_d0_24_ac_back), phone_d0_24_ac_data, phone_d0_24_ac_back}, \
    {sizeof(phone_d0_25_2c_data), sizeof(phone_d0_25_2c_back), phone_d0_25_2c_data, phone_d0_25_2c_back}, \
    {sizeof(phone_d0_25_ac_data), sizeof(phone_d0_25_ac_back), phone_d0_25_ac_data, phone_d0_25_ac_back}, \
    {sizeof(phone_d0_26_2c_data), sizeof(phone_d0_26_2c_back), phone_d0_26_2c_data, phone_d0_26_2c_back}, \
    {sizeof(phone_d0_26_ac_data), sizeof(phone_d0_26_ac_back), phone_d0_26_ac_data, phone_d0_26_ac_back}, \
    {sizeof(phone_d0_27_2c_data), sizeof(phone_d0_27_2c_back), phone_d0_27_2c_data, phone_d0_27_2c_back}  \
   */
};
uint8_t phone_reg_const_data_size = sizeof(phone_reg_const_data) / sizeof(phone_reg_const_data[0]);
/***************************开机阶段slave需要反馈的数据及其对应的接收开头截止******************************/

static void tp_int_high_timer_callback(void *data)
{
	uint8_t ii, jj=0;
		
	if(touch_recv_flag == 0)	// 1ms主机没有过来读取数据，自动拉高
	{
		hal_gpio_set_output_data(g_phone_output_int_pad, IO_LVL_HIGH);
	}
	else  // 25ms 后无触摸报点启动release事件
	{
		
		for(jj=0; jj<10; jj++)
		{
			ii = jj;
			phone_reg_coord_back[8*jj+0] = 0x33;							//13 press 33 leave 23 move
			phone_reg_coord_back[8*jj+1] = (jj<<4)|0x01;			  		 		//报点id
			phone_reg_coord_back[8*jj+2] = 0x00;			   			//x 低八位
			phone_reg_coord_back[8*jj+3] = 0x00;	   //bit0-bit3:x高四位;bit4-bit7:y低四位;
			phone_reg_coord_back[8*jj+4] = 0x00; 				   //y 高八位
			phone_reg_coord_back[8*jj+5] = 0x00; 								  		//zz八位
			//touch type：0:普通手指触摸；1:盘旋；2:保护套；3:手套;4:尖笔;5:手掌;6:潮湿的;7:接近;8:轻摇
			phone_reg_coord_back[8*jj+6] = 0x00;								  //bit0-bit5:z只有6位;bit6-bit7:touch type的高两位			
			phone_reg_coord_back[8*jj+7] = 9-jj;	
		}

		Flag_touch_count = jj;

		hal_gpio_set_output_data(g_phone_output_int_pad, IO_LVL_LOW);	//拉低TP中断脚,通知AP读取TP数据
	}
} 
void tp_int_high_timer_init(void)
{
	hal_timer_init(TIMER_NUM3);
    hal_timer_start(TIMER_NUM3, 25, tp_int_high_timer_callback, NULL);
}

/**************************************************************************
* @name : app_tp_screen_analysis_const
* @brief : screen start 阶段数据解析函数，由客户定制
* @param[in] :
* @return :
* @retval :
**************************************************************************/
uint8_t app_tp_screen_analysis_const(uint8_t transfer_now, uint8_t *rxbuffer, size_t data_size)
{
    static uint8_t app_tp_count = 0;

    app_tp_count++;
    if (app_tp_count >  20)                    //初始化不成功，重新reset screen
    {
        app_tp_count = 0;
        app_tp_screen_init();
        return 0;
    }

    if (transfer_now == 0)
    {
        if (rxbuffer[2] == 0x03)
        {
            TAU_LOGD("TD TP init done\n");
            return 1;
        }
        else
        {
            TAU_LOGD("TD TP return false [%d]\n", rxbuffer[2]);
            return 0;
        }
    }

    return transfer_now + 1;
}

/**************************************************************************
* @name : app_tp_screen_analysis_int
* @brief : screen 报点数据解析函数，由客户定制
* @param[in] :
* @return :
* @retval :
*修改TP第3步：转报点格式！！！！！！
*模组报点rxbuffer[]，转换后报点phone_reg_coord_back[]
**************************************************************************/
uint8_t app_tp_screen_analysis_int(uint8_t transfer_now, uint8_t *rxbuffer, size_t data_size)
{

#ifdef USE_FOR_SUMSUNG_S10
uint8_t ii, jj,i,j,k;
uint8_t temp_flag,temp,touchnum;
uint8_t temp_8,touch_id;	
uint16_t temp16 = 1;
uint32_t xx, yy, zz;
uint8_t send_point = 0;	 // 当前需要发送的报点数。抬起时也要发。

static uint16_t point_counter =0;

	send_point =0;
	touchnum =0;
	touchnum = rxbuffer[2];
	if(touchnum>0x10)
		touchnum = 0;	
/*	
	for(ii=0;ii<64;ii++)
	{
		printf("%2x ",rxbuffer[ii]);
	}
	printf("\n");
*/	
#if   0
	if(point_counter == 0)
	{
	//	point_counter = 1;
		phone_reg_coord_back[0] = 0x13;
		phone_reg_coord_back[1] = 0x01;
		phone_reg_coord_back[2] = 0xE9;
		phone_reg_coord_back[3] = 0x61;
		phone_reg_coord_back[4] = 0x25;
		phone_reg_coord_back[5] = 0x00;
		phone_reg_coord_back[6] = 0x00;
		phone_reg_coord_back[7] = 0x00;
	}
	else if(point_counter == 1)
	{
		
		phone_reg_coord_back[0] = 0x23;
		phone_reg_coord_back[1] = 0x01;
		phone_reg_coord_back[2] = 0xE9;
		phone_reg_coord_back[3] = 0x61;
		phone_reg_coord_back[4] = 0x25;
		phone_reg_coord_back[5] = 0x00;
		phone_reg_coord_back[6] = 0x00;
		phone_reg_coord_back[7] = 0x00;
	}
	else if(point_counter == 10)
	{
//		point_counter++;
		phone_reg_coord_back[0] = 0x23;
		phone_reg_coord_back[1] = 0x01;
		phone_reg_coord_back[2] = 0xF5;
		phone_reg_coord_back[3] = 0xE1;
		phone_reg_coord_back[4] = 0x25;
		phone_reg_coord_back[5] = 0x00;
		phone_reg_coord_back[6] = 0x00;
		phone_reg_coord_back[7] = 0x00;
	}
	
	point_counter++;
	if(point_counter == 1000)
	{
		point_counter = 0;
		phone_reg_coord_back[0] = 0x33;
		phone_reg_coord_back[1] = 0x01;
		phone_reg_coord_back[2] = 0xF5;
		phone_reg_coord_back[3] = 0xE1;
		phone_reg_coord_back[4] = 0x25;
		phone_reg_coord_back[5] = 0x00;
		phone_reg_coord_back[6] = 0x00;
		phone_reg_coord_back[7] = 0x00;
	}

	send_point = 1;

	if(temp16 || send_point)
	{
		tp_flag =true;
		TAU_LOGD("TD TP done %d \n",point_counter);
		hal_gpio_set_output_data(g_phone_output_int_pad, IO_LVL_LOW);	//拉低TP中断脚,通知AP读取TP数据
	}
	return screen_reg_int_data_size + 1;
		
#else
	for(ii =0; ii<5; ii++)
	{
		phone_reg_coord_back[7]=0;
	}


//	if(Flag_touch_count == 0)	// 发送已完成
	{

//	TAU_LOGD("[%2x][%2x][%2x][%2x][%2x][%2x][%2x][%2x]\n",rxbuffer[0],rxbuffer[1],rxbuffer[2],rxbuffer[3],rxbuffer[4],rxbuffer[5],rxbuffer[6],rxbuffer[7]);
	send_point = rxbuffer[2];  // 获取报点数据

	if( send_point <= 10 && (rxbuffer[3]!= 0x00) && (rxbuffer[4]!= 0x00) )	// 报点数据不超过10点 并且有报点数据
	{
		

		for(ii =0; ii<10; ii++)
		{
			temp_flag = s8_host_id[ii]&0xF0;
			temp_8 = rxbuffer[6*ii+3]&0xf0;
			touch_id=(rxbuffer[6*ii+5]>>4)&0x0f;
			if(temp_8==0x00)//((temp16 >>ii) &0x01)  // 对应ID有报点
			{
		//		send_point++;
				s8_host_id[ii]=0x13;
			}
			else if(temp_8== 0x80)  // 滑动
			{
		//		send_point++;
				s8_host_id[ii]=0x23;
			}
			else if(temp_8== 0x40)  // 抬起
			{
				send_point++;
				s8_host_id[ii] = 0x33;
			}
		}

	////	phone_reg_point_back[1] = s8_host_num[send_point];		//设置要发送报点个数的寄存器
	//	printf("send_point %d ",send_point);
		
		jj =0;
		for(ii =0; ii<10; ii++)
		{
			temp_8 = rxbuffer[6*ii+3]&0xf0;
			temp_flag = s8_host_id[ii]&0x30;		
			touch_id = (rxbuffer[6*ii+5]>>4)&0x0f;
			touch_id = (touch_id<<4);
			if(temp_flag) //高2BIT不为0，表明要发送报点
			{
				xx = ((rxbuffer[6*ii+3]&0x0f) << 8) | (rxbuffer[6*ii+4]);
				yy = ((rxbuffer[6*ii+5]&0x0f) << 8) | (rxbuffer[6*ii+6]);
				zz = rxbuffer[6*ii+7];
				
				xx = xx * INPUT_WIDTH_VALUE / OUTPUT_WIDTH_VALUE;
				if(xx > 4095)
					xx = 4095;
				yy = yy * INPUT_HEIGHT_VALUE / OUTPUT_HEIGHT_VALUE;
				if(yy > 4095)
					yy = 4095;
				
				if (temp_flag ==0x30) // 抬起
				{
				#if 1			
					phone_reg_coord_back[8*jj+0] = s8_host_id[ii];							//13 press 33 leave 23 move
					phone_reg_coord_back[8*jj+1] = touch_id|0x01;			  		 		//报点id
					phone_reg_coord_back[8*jj+2] = (uint8_t)(xx& 0xFF);			   			//x 低八位
					phone_reg_coord_back[8*jj+3] = ((yy & 0x0F) << 4) | ((xx>>8) & 0x0F);	   //bit0-bit3:x高四位;bit4-bit7:y低四位;
					phone_reg_coord_back[8*jj+4] = (uint8_t)((yy>>4)& 0xFF); 				   //y 高八位
					phone_reg_coord_back[8*jj+5] = 0x00; 								  		//zz八位
					//touch type：0:普通手指触摸；1:盘旋；2:保护套；3:手套;4:尖笔;5:手掌;6:潮湿的;7:接近;8:轻摇
					phone_reg_coord_back[8*jj+6] = 0x00;								  //bit0-bit5:z只有6位;bit6-bit7:touch type的高两位
					
					if(send_point>jj)
						phone_reg_coord_back[8*jj+7] = send_point-jj-1;	
					else
						phone_reg_coord_back[8*jj+7] = 0x00;
				#else
					phone_reg_coord_back[8*jj+0] = s8_host_id[ii];
					phone_reg_coord_back[8*jj+1] = touch_id|0x01;
					phone_reg_coord_back[8*jj+2] = s_screen_read_bak[8*ii+2];
					phone_reg_coord_back[8*jj+3] = s_screen_read_bak[8*ii+3];
					phone_reg_coord_back[8*jj+4] = s_screen_read_bak[8*ii+4];
					phone_reg_coord_back[8*jj+5] = s_screen_read_bak[8*ii+5];
					phone_reg_coord_back[8*jj+6] = s_screen_read_bak[8*ii+6];
		//			phone_reg_coord_back[8*jj+7] = 0x00;
					if(send_point>=jj)
						phone_reg_coord_back[8*jj+7] = 0x00;
					else
						phone_reg_coord_back[8*jj+7] = send_point-jj-1;
				#endif
					
				}
				else
				{
	/*				xx = ((rxbuffer[6*ii+3]&0x0f) << 8) | (rxbuffer[6*ii+4]);
					yy = ((rxbuffer[6*ii+5]&0x0f) << 8) | (rxbuffer[6*ii+6]);
					zz = rxbuffer[6*ii+7];
					
					xx = xx * INPUT_WIDTH_VALUE / OUTPUT_WIDTH_VALUE;
					if(xx > 4095)
						xx = 4095;
					yy = yy * INPUT_HEIGHT_VALUE / OUTPUT_HEIGHT_VALUE;
					if(yy > 4095)
						yy = 4095;
	*/
					
					phone_reg_coord_back[8*jj+0] = s8_host_id[ii];							//13 press 33 leave 23 move
					phone_reg_coord_back[8*jj+1] = touch_id|0x01;			  		 		//报点id
					phone_reg_coord_back[8*jj+2] = (uint8_t)(xx& 0xFF);			   			//x 低八位
					phone_reg_coord_back[8*jj+3] = ((yy & 0x0F) << 4) | ((xx>>8) & 0x0F);	   //bit0-bit3:x高四位;bit4-bit7:y低四位;
					phone_reg_coord_back[8*jj+4] = (uint8_t)((yy>>4)& 0xFF); 				   //y 高八位
					phone_reg_coord_back[8*jj+5] = 0x00; 								  		//zz八位
					//touch type：0:普通手指触摸；1:盘旋；2:保护套；3:手套;4:尖笔;5:手掌;6:潮湿的;7:接近;8:轻摇
					phone_reg_coord_back[8*jj+6] = 0x00;								  //bit0-bit5:z只有6位;bit6-bit7:touch type的高两位
				//	phone_reg_coord_back[8*jj+7] = 0x00;
					phone_reg_coord_back[8*jj+7] = send_point-jj-1; 					  //bit0-bit5:buffer里面剩余多少个事件;bit6-bit7：touch type 低两位

				}

			#if 0//备份
				s_screen_read_bak[8*ii+0] =phone_reg_coord_back[8*jj+0];
				s_screen_read_bak[8*ii+1] =phone_reg_coord_back[8*jj+1];
				s_screen_read_bak[8*ii+2] =phone_reg_coord_back[8*jj+2];
				s_screen_read_bak[8*ii+3] =phone_reg_coord_back[8*jj+3];
				s_screen_read_bak[8*ii+4] =phone_reg_coord_back[8*jj+4];
				s_screen_read_bak[8*ii+5] =phone_reg_coord_back[8*jj+5];
				s_screen_read_bak[8*ii+6] =phone_reg_coord_back[8*jj+6];
				s_screen_read_bak[8*ii+7] =phone_reg_coord_back[8*jj+7];
			#endif	
				jj++;
			}

			if(jj >= send_point) break;
		}

	
		touchnum_bak = touchnum;
		phone_data_B1[1] =0x86;
		Flag_touch_count = send_point; // 记录报点个数
		touch_send_flag = 0;

		if(temp16 || send_point)
		{
			touch_recv_flag = 0;
			tp_flag =true;
			hal_gpio_set_output_data(g_phone_output_int_pad, IO_LVL_LOW);	//拉低TP中断脚,通知AP读取TP数据
			tp_int_high_timer_init();
		}
	}
	}
	return screen_reg_int_data_size + 1;
#endif
	
#endif

}

/**************************************************************************
* @name : hal_tp_phone_analysis_data
* @brief : phone 数据解析函数，由客户定制
* @param[in] :
* @return :
* @retval :
*修改TP第4步：抓取主机TP数据并回复！！！！！！
**************************************************************************/
void app_tp_phone_analysis_data(uint8_t *rxbuffer, size_t rxbuffer_size, const uint8_t **txbuffer, size_t *txbuffer_size)
{
#ifdef USE_FOR_SUMSUNG_S10
static uint8_t phone_A6_flag = 0;
static uint8_t phone_FA_flag = 0;	
static uint8_t phone_86_flag = 0;

	if( (rxbuffer_size ==3) &&(rxbuffer[0] ==0xA6))
	{
		printf("%d\n",phone_A6_flag);
		if (phone_A6_flag == 0)
		{
			*txbuffer = phone_data_A6_0;
			*txbuffer_size = sizeof(phone_data_A6_0);
		}
		else if (phone_A6_flag == 1)
		{
			*txbuffer = phone_data_A6_1;
			*txbuffer_size = sizeof(phone_data_A6_1);
		}
		else if (phone_A6_flag == 2)
		{
			*txbuffer = phone_data_A6_2;
			*txbuffer_size = sizeof(phone_data_A6_2);
		}
		else if (phone_A6_flag == 3)
		{
			*txbuffer = phone_data_A6_3;
			*txbuffer_size = sizeof(phone_data_A6_3);
		}
		else if (phone_A6_flag == 4)
		{
			*txbuffer = phone_data_A6_4;
			*txbuffer_size = sizeof(phone_data_A6_4);
		}
		else if (phone_A6_flag == 5)
		{
			*txbuffer = phone_data_A6_5;
			*txbuffer_size = sizeof(phone_data_A6_5);
		}
		else 
		{
			*txbuffer = phone_data_A6_4;
			*txbuffer_size = sizeof(phone_data_A6_4);
		}
		phone_A6_flag++;
		if (phone_A6_flag > 6)
			phone_A6_flag = 0;
	}
	if( (rxbuffer_size == 5) &&(rxbuffer[0] ==0xFA))
	{
		if (phone_FA_flag == 0)
		{
			*txbuffer = phone_data_FA_0;
			*txbuffer_size = sizeof(phone_data_FA_0);
		}
		else if (phone_FA_flag == 1)
		{
			*txbuffer = phone_data_FA_1;
			*txbuffer_size = sizeof(phone_data_FA_1);
		}
		else if (phone_FA_flag == 2)
		{
			*txbuffer = phone_data_FA_2;
			*txbuffer_size = sizeof(phone_data_FA_2);
		}
		else
		{
			*txbuffer = phone_data_FA_2;
			*txbuffer_size = sizeof(phone_data_FA_2);
		}
	   	
		phone_FA_flag++;
		if (phone_FA_flag > 6)
			phone_FA_flag = 0;
	}
	
	if(rxbuffer_size ==1)
	{
		switch(rxbuffer[0])
		{
			case 0x43:
				*txbuffer = phone_data_43;
				*txbuffer_size = sizeof(phone_data_43);
			break;
			case 0xC3:
				*txbuffer = phone_data_43;
				*txbuffer_size = sizeof(phone_data_43);
			break;
			case 0x83:
				*txbuffer = phone_data_43;
				*txbuffer_size = sizeof(phone_data_43);
			break;
			case 0x87:
				*txbuffer = phone_data_43;
				*txbuffer_size = sizeof(phone_data_43);
			break;
			case 0x86:
					
					touch_recv_flag++;
			//		if((touch_recv_flag%2) == 0)
					{
			//			printf("%d\n",phone_86_flag);
			//			if(phone_86_flag<25)	touch_recv_flag = 0;
						
						if (phone_86_flag == 0)
						{
							*txbuffer = phone_data_86_0;
							*txbuffer_size = sizeof(phone_data_86_0);
							phone_86_flag++;
						}
						else if (phone_86_flag == 1)
						{
							*txbuffer = phone_data_86_0;
							*txbuffer_size = sizeof(phone_data_86_0);
							phone_86_flag++;
						}
						else if (phone_86_flag == 2)
						{
							*txbuffer = phone_data_86_1;
							*txbuffer_size = sizeof(phone_data_86_1);
							phone_86_flag++;
						}
						else if (phone_86_flag == 3)
						{
							*txbuffer = phone_data_86_1;
							*txbuffer_size = sizeof(phone_data_86_1);
							phone_86_flag++;
						}
						else if (phone_86_flag == 4)
						{
							*txbuffer = phone_data_86_2;
							*txbuffer_size = sizeof(phone_data_86_2);
							phone_86_flag++;
						}
						else if (phone_86_flag == 5)
						{
							*txbuffer = phone_data_86_3;
							*txbuffer_size = sizeof(phone_data_86_3);
							phone_86_flag++;
						}
						else if (phone_86_flag == 6)
						{
							*txbuffer = phone_data_86_4;
							*txbuffer_size = sizeof(phone_data_86_4);
							phone_86_flag++;
						}
						else if (phone_86_flag == 7)
						{
							*txbuffer = phone_data_86_5;
							*txbuffer_size = sizeof(phone_data_86_5);
							phone_86_flag++;
						}
						else if (phone_86_flag == 8)
						{
							*txbuffer = phone_data_86_6;
							*txbuffer_size = sizeof(phone_data_86_6);
							phone_86_flag++;
						}
						else if (phone_86_flag == 9)
						{
							*txbuffer = phone_data_86_7;
							*txbuffer_size = sizeof(phone_data_86_7);
							phone_86_flag++;
						}
						else if (phone_86_flag == 10)
						{
							*txbuffer = phone_data_86_8;
							*txbuffer_size = sizeof(phone_data_86_8);
							phone_86_flag++;
						}
						else if (phone_86_flag == 11)
						{
							*txbuffer = phone_data_86_9;
							*txbuffer_size = sizeof(phone_data_86_9);
							phone_86_flag++;
						}
						else if (phone_86_flag == 12)
						{
							*txbuffer = phone_data_86_10;
							*txbuffer_size = sizeof(phone_data_86_10);
							phone_86_flag++;
						}
						else if (phone_86_flag == 13)
						{
							*txbuffer = phone_data_86_11;
							*txbuffer_size = sizeof(phone_data_86_11);
							phone_86_flag++;
						}
						else if (phone_86_flag == 14)
						{
							*txbuffer = phone_data_86_12;
							*txbuffer_size = sizeof(phone_data_86_12);
							phone_86_flag++;
						}
						else if (phone_86_flag == 15)
						{
							*txbuffer = phone_reg_coord_back;
							*txbuffer_size = sizeof(phone_reg_coord_back);
							phone_86_flag++;
						}
						else if (phone_86_flag == 16)
						{
							*txbuffer = phone_data_86_13;
							*txbuffer_size = sizeof(phone_data_86_13);
							phone_86_flag++;
						}
						else if (phone_86_flag == 17)
						{
							*txbuffer = phone_data_86_14;
							*txbuffer_size = sizeof(phone_data_86_14);
							phone_86_flag++;
						}
						else if (phone_86_flag == 18)
						{
							*txbuffer = phone_data_86_15;
							*txbuffer_size = sizeof(phone_data_86_15);
							phone_86_flag++;
						}
						else if (phone_86_flag == 19)
						{
							*txbuffer = phone_data_86_16;
							*txbuffer_size = sizeof(phone_data_86_16);
							phone_86_flag++;
						}
						else if (phone_86_flag == 20)
						{
							*txbuffer = phone_data_86_17;
							*txbuffer_size = sizeof(phone_data_86_17);
							phone_86_flag++;
						}
						else if (phone_86_flag == 21)
						{
							*txbuffer = phone_data_86_18;
							*txbuffer_size = sizeof(phone_data_86_18);
							phone_86_flag++;
						}
						else if (phone_86_flag == 22)
						{
							*txbuffer = phone_data_86_19;
							*txbuffer_size = sizeof(phone_data_86_19);
							phone_86_flag++;
						}
						else if (phone_86_flag == 23)
						{
							*txbuffer = phone_data_86_20;
							*txbuffer_size = sizeof(phone_data_86_20);
							phone_86_flag++;
						}
						else if (phone_86_flag == 24)
						{
							*txbuffer = phone_data_86_21;
							*txbuffer_size = sizeof(phone_data_86_21);
							phone_86_flag++;
						}
						else
						{
					//		printf("A\n");
							printf("%d\n",Flag_touch_count);
							 if(Flag_touch_count > 1)
							 {
							// 	printf("%d\n",Flag_touch_count);
								touch_send_flag++;
								if(touch_send_flag == 1)
								{
									*txbuffer = phone_reg_coord_back;
									*txbuffer_size = 8;
						//			printf("A");
								}
								else
								{
									touch_send_flag = 0;
									touch_recv_flag = 0;
									Flag_touch_count = 0;
									
								 	*txbuffer = &phone_reg_coord_back[8];
									*txbuffer_size = 8*(Flag_touch_count-1);
									hal_gpio_set_output_data(g_phone_output_int_pad, IO_LVL_HIGH);
						//			printf("B\n");
								}					
							 }
							 else
							 {
							 	if(Flag_touch_count == 1)	// 两指存在 Flag_touch_count = 0 情况
							 	{
							 		*txbuffer = phone_reg_coord_back;
									*txbuffer_size = 8;
							 	}
								touch_send_flag = 0;
								touch_recv_flag = 0;
								Flag_touch_count = 0;
								hal_gpio_set_output_data(g_phone_output_int_pad, IO_LVL_HIGH);
						//		printf("C\n");
							 }
					 	}
					}
			break;
			default:

			break;
			
		}
	}

////////////////////////////////////////////////////////////////////////////////////////////


#endif

}

void app_tp_phone_analysis_data_custom(uint8_t *rxbuffer, size_t rxbuffer_size,  uint8_t *phonetxbuffer, size_t *txbuffer_size)

{
#ifdef USE_FOR_SUMSUNG_S10
static uint8_t phone_A6_flag = 0;
static uint8_t phone_FA_flag = 0;	
static uint16_t phone_86_flag = 0;
static uint8_t phone_poweron_flag = 0;

	uint8_t *txbuffer;
	
	if( (rxbuffer_size ==3) &&(rxbuffer[0] ==0xA6))
	{
		printf("A6 %d\n",phone_A6_flag);
		if (phone_A6_flag == 0)
		{
			txbuffer = (uint8_t*)phone_data_A6_0;
			*txbuffer_size = sizeof(phone_data_A6_0);
		}
		else if (phone_A6_flag == 1)
		{
			txbuffer = (uint8_t*)phone_data_A6_1;
			*txbuffer_size = sizeof(phone_data_A6_1);
		}
		else if (phone_A6_flag == 2)
		{
			txbuffer = (uint8_t*)phone_data_A6_4;
			*txbuffer_size = sizeof(phone_data_A6_4);
		}
		else if (phone_A6_flag == 3)
		{
			txbuffer = (uint8_t*)phone_data_A6_3;
			*txbuffer_size = sizeof(phone_data_A6_3);
		}
		else if (phone_A6_flag == 4)
		{
			txbuffer = (uint8_t*)phone_data_A6_4;
			*txbuffer_size = sizeof(phone_data_A6_4);
		}
		else if (phone_A6_flag == 5)
		{
			txbuffer = (uint8_t*)phone_data_A6_5;
			*txbuffer_size = sizeof(phone_data_A6_5);
		}
		else if (phone_A6_flag == 6)
		{
			txbuffer = (uint8_t*)phone_data_A6_4;
			*txbuffer_size = sizeof(phone_data_A6_4);
		}
		else 
		{
			txbuffer = (uint8_t*)phone_data_A6_6;
			*txbuffer_size = sizeof(phone_data_A6_6);
		}
		phone_A6_flag++;
		if (phone_A6_flag > 7)
			phone_A6_flag = 0;
	}
	if( (rxbuffer_size == 5) &&(rxbuffer[0] ==0xFA))
	{
		if (phone_FA_flag == 0)
		{
			txbuffer = (uint8_t*)phone_data_FA_0;
			*txbuffer_size = sizeof(phone_data_FA_0);
		}
		else if (phone_FA_flag == 1)
		{
			txbuffer = (uint8_t*)phone_data_FA_1;
			*txbuffer_size = sizeof(phone_data_FA_1);
		}
		else if (phone_FA_flag == 2)
		{
			txbuffer = (uint8_t*)phone_data_FA_2;
			*txbuffer_size = sizeof(phone_data_FA_2);
		}
		else
		{
			txbuffer = (uint8_t*)phone_data_FA_2;
			*txbuffer_size = sizeof(phone_data_FA_2);
		}
	   	
		phone_FA_flag++;
		if (phone_FA_flag > 6)
			phone_FA_flag = 0;
	}
	
	if(rxbuffer_size ==1)
	{
		switch(rxbuffer[0])
		{
			case 0x43:
				txbuffer = (uint8_t*)phone_data_43;
				*txbuffer_size = sizeof(phone_data_43);
			break;
			case 0xC3:
				txbuffer = (uint8_t*)phone_data_43;
				*txbuffer_size = sizeof(phone_data_43);
			break;
			case 0x83:
				txbuffer = (uint8_t*)phone_data_43;
				*txbuffer_size = sizeof(phone_data_43);
			break;
			case 0x87:
				txbuffer = (uint8_t*)phone_data_43;
				*txbuffer_size = sizeof(phone_data_43);
			break;
			case 0x86:

					if(phone_poweron_flag == 0)	// 开机回值
					{
						printf("86 %d\n",phone_86_flag);
						if(phone_86_flag < 3200)
						{
							phone_86_flag++;
						}
						else
						{
							phone_poweron_flag = 1;
						}
						
						if (phone_86_flag == 0)
						{
							txbuffer = (uint8_t*)phone_data_86_0;
							*txbuffer_size = sizeof(phone_data_86_0);
						}
						else if (phone_86_flag == 1)
						{
							txbuffer = (uint8_t*)phone_data_86_0;
							*txbuffer_size = sizeof(phone_data_86_0);
						}
						else if (phone_86_flag == 2)
						{
							txbuffer = (uint8_t*)phone_data_86_1;
							*txbuffer_size = sizeof(phone_data_86_1);
						}
						else if (phone_86_flag == 3)
						{
							txbuffer = (uint8_t*)phone_data_86_1;
							*txbuffer_size = sizeof(phone_data_86_1);
						}
						else if (phone_86_flag == 4)
						{
							txbuffer = (uint8_t*)phone_data_86_2;
							*txbuffer_size = sizeof(phone_data_86_2);							
						}
						else if (phone_86_flag == 5)
						{
							txbuffer = (uint8_t*)phone_data_86_3;
							*txbuffer_size = sizeof(phone_data_86_3);
							if(phone_A6_flag == 0)	// 开机时有A6指令读取，唤醒时无此指令读取数据
							{
								phone_poweron_flag = 1;
							}
						}
						else if (phone_86_flag == 6)
						{
							txbuffer = (uint8_t*)phone_data_86_4;
							*txbuffer_size = sizeof(phone_data_86_4);
						}
						else if (phone_86_flag == 7)
						{
							txbuffer = (uint8_t*)phone_data_86_5;
							*txbuffer_size = sizeof(phone_data_86_5);
						}
						else if (phone_86_flag == 8)
						{
							txbuffer = (uint8_t*)phone_data_86_6;
							*txbuffer_size = sizeof(phone_data_86_6);
						}
						else if (phone_86_flag == 9)
						{
							txbuffer = (uint8_t*)phone_data_86_7;
							*txbuffer_size = sizeof(phone_data_86_7);
						}
						else if (phone_86_flag == 10)
						{
							txbuffer = (uint8_t*)phone_data_86_8;
							*txbuffer_size = sizeof(phone_data_86_8);
						}
						else if (phone_86_flag == 11)
						{
							txbuffer = (uint8_t*)phone_data_86_9;
							*txbuffer_size = sizeof(phone_data_86_9);
						}
						else if (phone_86_flag == 12)
						{
							txbuffer = (uint8_t*)phone_data_86_10;
							*txbuffer_size = sizeof(phone_data_86_10);
						}
						else if (phone_86_flag == 13)
						{
							txbuffer = (uint8_t*)phone_data_86_11;
							*txbuffer_size = sizeof(phone_data_86_11);
						}
						else if (phone_86_flag == 14)
						{
							txbuffer = (uint8_t*)phone_data_86_12;
							*txbuffer_size = sizeof(phone_data_86_12);
						}
						else if (phone_86_flag == 15)
						{
							txbuffer = (uint8_t*)phone_reg_coord_back;
							*txbuffer_size = sizeof(phone_reg_coord_back);
						}
						else if (phone_86_flag == 16)
						{
							txbuffer = (uint8_t*)phone_data_86_13;
							*txbuffer_size = sizeof(phone_data_86_13);
						}
						else if (phone_86_flag == 17)
						{
							txbuffer = (uint8_t*)phone_data_86_14;
							*txbuffer_size = sizeof(phone_data_86_14);
						}
						else if (phone_86_flag == 18)
						{
							txbuffer = (uint8_t*)phone_data_86_15;
							*txbuffer_size = sizeof(phone_data_86_15);
						}
						else if (phone_86_flag == 19)
						{
							txbuffer = (uint8_t*)phone_data_86_16;
							*txbuffer_size = sizeof(phone_data_86_16);
						}
						else if (phone_86_flag == 20)
						{
							txbuffer = (uint8_t*)phone_data_86_17;
							*txbuffer_size = sizeof(phone_data_86_17);
						}
						else if (phone_86_flag == 21)
						{
							txbuffer = (uint8_t*)phone_data_86_18;
							*txbuffer_size = sizeof(phone_data_86_18);
						}
						else if (phone_86_flag == 22)
						{
							txbuffer = (uint8_t*)phone_data_86_19;
							*txbuffer_size = sizeof(phone_data_86_19);
						}
						else if (phone_86_flag == 23)
						{
							txbuffer = (uint8_t*)phone_data_86_20;
							*txbuffer_size = sizeof(phone_data_86_20);
						}
						else if (phone_86_flag == 24)
						{
							txbuffer = (uint8_t*)phone_data_86_21;
							*txbuffer_size = sizeof(phone_data_86_21);
						}
						else if (phone_86_flag == 1800)
						{
							txbuffer = (uint8_t*)phone_data_86_1800;
							*txbuffer_size = sizeof(phone_data_86_1800);
						}
						else if (phone_86_flag == 3000)
						{
							txbuffer = (uint8_t*)phone_data_86_1800;
							*txbuffer_size = sizeof(phone_data_86_1800);
						}
				/*		else if (phone_86_flag == 4000)
						{
							txbuffer = (uint8_t*)phone_data_86_4000;
							*txbuffer_size = sizeof(phone_data_86_4000);
						}
				*/
						else
						{
							txbuffer = (uint8_t*)phone_reg_coord_back;
							*txbuffer_size = 8;
						}
						
					}
					else
					{
				//		printf("A\n");
				//		printf("touch %d\n",Flag_touch_count);
						touch_recv_flag++;
				
						 if(Flag_touch_count > 1)
						 {
						// 	printf("%d\n",Flag_touch_count);
							touch_send_flag++;
							if(touch_send_flag == 1)
							{
								txbuffer = (uint8_t*)phone_reg_coord_back;
								*txbuffer_size = 8;
					//			printf("A");
							}
							else
							{
								touch_send_flag = 0;
							
							 	txbuffer = (uint8_t*)&phone_reg_coord_back[8];
								*txbuffer_size = (8*(Flag_touch_count-1));
								hal_gpio_set_output_data(g_phone_output_int_pad, IO_LVL_HIGH);
							
								Flag_touch_count = 0;
					//			printf("B\n");
							}					
						 }
						 else
						 {
			//			 	if(Flag_touch_count == 1)	// 两指存在 Flag_touch_count = 0 情况
						 	{
						 		txbuffer = (uint8_t*)phone_reg_coord_back;
								*txbuffer_size = 8;
						 	}
							
							touch_send_flag = 0;
							Flag_touch_count = 0;
							hal_gpio_set_output_data(g_phone_output_int_pad, IO_LVL_HIGH);
					//		printf("C\n");
						 }
				 	}
			break;
			default:

			break;
			
		}
	}
	//printf("1111   %p \n",*phonetxbuffer);
	s_slave_txbuffer = txbuffer;
	//printf("222    %p \n",*phonetxbuffer);
////////////////////////////////////////////////////////////////////////////////////////////


#endif

}



#endif

