/********************************** (C) COPYRIGHT ******************************
* File Name          :usb_driver.C
* Author             : DL
* Version            : V1.1
* Date               : 2018/10/21
* Description        : A demo for USB compound device created by CH552, support
					   touch panel , and HID-compliant device.
********************************************************************************/
#include "typedef.h"
#include "TouchInclude.h"
#include "usb_hid.h"
#include "usb_desc.h"
#include "api.h"
#include "ble_hid_service.h"
//#include "app_hid_event.h"

//#pragma  NOAREGS
#define FLASH_Base   0x08004000
#define APP_FALG_ADDR   (FLASH_Base - 256)
#define HASH_FLAG_ADDR  (APP_FALG_ADDR + 4)

//extern void GT_RD_Reg_HS(UINT16 reg,UINT8 *buf,UINT8 len);
extern BOOL SearchAddress( UINT8 num );
extern BOOL GetBondingMacAddr(UINT8* MacAddr);

UINT8 TouchNum = 0;
INT16 CheckPos = MAX_POINT;
UINT8 Buffer_Queue;
PACKAGE_INFO PackageInfo;
UINT32 wReadAddress;
UINT16 wReadLen;
BOOL IsDebugMode;
BOOL DisableDigiterInterface = 0;

BOOL OutDebugMode;
UINT8 PenToFinger = 0;
//UINT8 const Version[] = VERSION;
UINT8 print_log = 1;
BOOL ReportExtInfoEn = 0;

extern CHIP_TYPE ChipType;

extern BOOL EnumOK;
#ifdef STYLUS_TILT
extern UINT16 StylusTiltX, StylusTiltY;
#endif
#define BOOT_ADDR  0x3800
//void (*EnterUpdateMode)();

//static UINT8 USBDebugTaskId = INVALID_TASK_ID;

extern UINT8 DeviceMode;


#ifdef CHIP_TYPE_GT738X
extern void AppendReleaseFrame(void);
#endif

#ifdef UART_ENABLE
extern UINT8 TxBuff[16];
#endif

#define DEF_USB_EP1_FS_SIZE     (PACKAGE_POINT_SIZE *(MAX_POINT/PACKAGE_POINT_NUM))
#define DEF_USB_EP2_FS_SIZE 256
UINT8 USBFS_EP1_Buf[DEF_USB_EP1_FS_SIZE + 10];


UINT8 Absolute_Up_Pack( POINTER * pTP)
{
    UINT8 k,offset;
	UINT8* p;

    TouchNum = 0;

    for(k = 0; k < MAX_POINT; k++)
    {
        if(Press_Flag & (1L << k))
        {
            TouchNum++;
        }
    }


    memset(USBFS_EP1_Buf, 0 , DEF_USB_EP1_FS_SIZE);

    k = 0;
    CheckPos = MAX_POINT - 1;
#ifdef STYLUS_REPORT
    if(Press_Flag & 0x8000) //stylus
    {
		CheckPos--;

        USBFS_EP1_Buf[k++] = HID_RPT_ID_STYLUS_IN;

        USBFS_EP1_Buf[1] = 0;

        if(pTP[9].Tip_Switch != 0 || (Press_Flag & 0x1000))
        {
            //if(pTP[9].Resolution_Multi==0)
            {
								//pTP[9].Resolution_Multi = 0;
                USBFS_EP1_Buf[1] |= PEN_HOVER; //hover
            }
            if(StylusPress > 0 && (Press_Flag_Bak1 & 0x8000)!=0)
            {
                USBFS_EP1_Buf[1] |= PEN_TIP;//tip
            }

            if(Press_Flag & 0x1000)
            {
                USBFS_EP1_Buf[1] = PEN_HOVER | PEN_INVERT;

                if(Press_Flag_Bak0 & Press_Flag_Bak1 & 0x1000)
                {
                  if(StylusPress > 0)
                    USBFS_EP1_Buf[1] |= PEN_ERASER;
                }

                Press_Flag_Bak0 |= 0x8000;
            }

            if(Press_Flag & 0x2000)
            {
                USBFS_EP1_Buf[1] |= PEN_BARREL;
            }
        }


        p = (UINT8*)(pTP + 9);
		k++;
        USBFS_EP1_Buf[k++] = p[2];
        USBFS_EP1_Buf[k++] = p[3];
        USBFS_EP1_Buf[k++] = p[4];
        USBFS_EP1_Buf[k++] = p[5];
        USBFS_EP1_Buf[k++] = StylusPress;
        USBFS_EP1_Buf[k++] = StylusPress>>8;

	#ifdef STYLUS_TILT
		USBFS_EP1_Buf[k++] = (INT16)StylusTiltX / 20;
		USBFS_EP1_Buf[k++] = (INT16)StylusTiltY / 20;
	#endif
		k = 4 + STYLUS_POINT_SIZE - (STYLUS_POINT_SIZE%4);

    #if USB_HID_EN
		usb_hid_buffer_push(EP_INDEX1, USBFS_EP1_Buf,STYLUS_POINT_SIZE);
	#endif
    }
#endif

    for(; CheckPos >= 0; CheckPos--)
    {
        offset = 0;
        k = 0;
        USBFS_EP1_Buf[k++] = HID_RPT_ID_TOUCH_IN;

        for(; CheckPos >= 0; CheckPos--)
        {

            if(Press_Flag & (1L << CheckPos))
            {
        #if LE_HID_DIGITIZER
            ble_hid_send_touch_cmd(pTP[CheckPos].Tip_Switch, CheckPos, pTP[CheckPos].X_pos, pTP[CheckPos].Y_pos);
        #elif LE_HID_MOUSE
            ble_hid_send_mouse_cmd(pTP[CheckPos].Tip_Switch,1, 1);
        #endif
                p = (UINT8*)(pTP + CheckPos);

                USBFS_EP1_Buf[k++] = p[0] + (CheckPos << 4);//(p[1] << 4);//
                USBFS_EP1_Buf[k++] = p[2];
                USBFS_EP1_Buf[k++] = p[3];
                USBFS_EP1_Buf[k++] = p[4];
                USBFS_EP1_Buf[k++] = p[5];
            }
            if(k - offset >= PACKAGE_POINT_SIZE - 1)
            {
                break;
            }
        }
        offset += PACKAGE_POINT_SIZE - 1;

        if(offset < PACKAGE_POINT_SIZE * 2 - 1)
        {
  #ifdef STYLUS_REPORT
            if(Press_Flag & 0x8000)
            {
                USBFS_EP1_Buf[offset] = TouchNum - 1;
            }
            else
  #endif
            {
                USBFS_EP1_Buf[offset] = TouchNum;
            }

        }
        else
        {
            USBFS_EP1_Buf[offset] = 0;
        }
#ifdef FIVE_POINTS_PACKAGE
        offset += 2;  //4 bytes
#else
        offset++;
#endif
        if (bTransmitEn && !UsbReportPosEn)  //2.4G
        {
        #ifdef UART_ENABLE
          if (TouchNum <= 5)
          {
            memcpy(&USBFS_EP1_Buf[DEF_USB_EP1_FS_SIZE], TxBuff, 10);
            //RF_Enqueue(1, RF_PACKAGE_REPORT, PACKAGE_RAM_SIZE + 10, Ep1Buffer);
            //wireless_send_test(DEF_USB_EP1_FS_SIZE + 10, USBFS_EP1_Buf);
          }
          else
        #endif
          {
            //RF_Enqueue(1, RF_PACKAGE_REPORT, offset, Ep1Buffer);
            //wireless_send_test(offset, USBFS_EP1_Buf);
          }
        }
        else if(UsbReportPosEn)  //usb
        {
        #if USB_HID_EN
           usb_hid_buffer_push(EP1_INDEX, USBFS_EP1_Buf,PACKAGE_POINT_SIZE);
        #endif
        }
        else  //ble
        {
        #if LE_HID_MULTITOUCH
            ble_hid_task_enqueue(BLE_HID_CMD_ID_MULTITOUCH, USBFS_EP1_Buf + 1, PACKAGE_POINT_SIZE - 1);
        #endif
        }
    }
    return 0;
}


#ifdef MOUSE_REPORT

#ifndef GESTURE
UINT8 mouse_right_click_time;


UINT8 GetTouchNum()
{
  UINT8 k, num = 0;
  for(k = 0; k < MAX_POINT; k++)
  {
    if(Press_Flag & (1L << k))
    {
      num++;
    }
  }
  return num;
}

void TrigleEp3Input(UINT8 len, UINT8* pBuf)
{
#if USB_HID_EN
  usb_hid_buffer_push(EP3_INDEX, pBuf, len);
#endif
}
void TrigleEp4Input(UINT8 len, UINT8* pBuf)
{
#if USB_HID_EN
    usb_hid_buffer_push(EP1_INDEX, pBuf, len);
#endif
}

void ReportMousePos(POINTER * pTP)
{
  static UINT16 PrePointX = 0xFFFF, PrePointY = 0xFFFF, PrePointY1 = 0xFFFF;
  static char nCnt = 0;
  static  BOOL bRightClickFlag = 0;
  UINT8 MouseReportBuffer[7];

  TouchNum = GetTouchNum();

  if(TouchNum == 0)
  {
    return;
  }

  if(((Press_Flag_Bak1 | Press_Flag_Bak2) & 0x3FF)==0) //��֡
  {
    PrePointX = pTP[0].X_pos;
    PrePointY = pTP[0].Y_pos;
    mouse_right_click_time = 0;
    nCnt = 0;
  }

  if(TouchNum==2)
  {
    if(nCnt > 3)
    {
        MouseReportBuffer[0] = HID_RPT_ID_ABS_MOUSE_IN;
        MouseReportBuffer[1] = 0;
        TrigleEp4Input(ABS_MOUSE_PACKAGE_SIZE, MouseReportBuffer);
        delay_ms(3);
    }

    MouseReportBuffer[4] = 0;
    if((Press_Flag_Bak1 & Press_Flag_Bak2 & 0x3FF)==0)
    {
      PrePointY = TP[0].Y_pos;
      PrePointY1 = TP[1].Y_pos;
    }
    if (TP[0].Y_pos + 256 < PrePointY && TP[1].Y_pos + 256  < PrePointY1) //Up
    {
        MouseReportBuffer[4] = 0xFF;
    }
    else if (TP[0].Y_pos > PrePointY + 256  && TP[1].Y_pos > PrePointY1 + 256 )   //Down
    {
        MouseReportBuffer[4] = 1;
    }

    if(MouseReportBuffer[4]!=0)
    {
        MouseReportBuffer[0] = HID_RPT_ID_MOUSE_IN;
        MouseReportBuffer[1] = 0;
        MouseReportBuffer[2] = 0;
        MouseReportBuffer[3] = 0;

      PrePointY = TP[0].Y_pos;
      PrePointY1 = TP[1].Y_pos;

      TrigleEp3Input(MOUSE_PACKAGE_SIZE, MouseReportBuffer);
    }
    nCnt = -5;
  }
  else if(nCnt > 3)
  {
  REPROT:
      MouseReportBuffer[0] = HID_RPT_ID_ABS_MOUSE_IN;
      MouseReportBuffer[2] = *(((PUINT8)&TP[0]) + 2);
      MouseReportBuffer[3] = *(((PUINT8)&TP[0]) + 3);// p[1];
      MouseReportBuffer[4] = *(((PUINT8)&TP[0]) + 4);//p[4];
      MouseReportBuffer[5] = *(((PUINT8)&TP[0]) + 5);//p[3];
      MouseReportBuffer[6] = 0;

    if(nCnt==4) //��֡���뱨һ֡�ɼ�
    {
        MouseReportBuffer[1] = 0;
      nCnt++;
      TrigleEp4Input(ABS_MOUSE_PACKAGE_SIZE, MouseReportBuffer);
      delay_ms(3);
      bRightClickFlag = 0;
    }

    if(nCnt>=5)
    {
        MouseReportBuffer[1] = 1;

      if(TP[0].X_pos < PrePointX + 200 && TP[0].X_pos + 200 > PrePointX //����
       && TP[0].Y_pos < PrePointY + 200 && TP[0].Y_pos + 200 > PrePointY)
      {
        if(mouse_right_click_time>=16)
        {
          //USBFS_EP1_Buf[1] |= 0x02;
          bRightClickFlag = 1;
        }
      }
      else
      {
        PrePointX = 0xFFFF;
      }
      TrigleEp4Input(ABS_MOUSE_PACKAGE_SIZE, MouseReportBuffer);

//      if(nCnt==8 && TP[0].Tip_Switch==0)
//      {
//        delay_ms(3);
//      }

      nCnt = 6;
    }

    if(TP[0].Tip_Switch==0)//�ɼ�
    {
    #if USBCONFIG
      if(bRightClickFlag && !config.mouse_right_click_dis)
    #else
      if(bRightClickFlag)
    #endif
      {
          MouseReportBuffer[1] = 0x03;
          TrigleEp4Input(ABS_MOUSE_PACKAGE_SIZE, MouseReportBuffer);

      }

      delay_ms(5);
      MouseReportBuffer[1] = 0;
      Press_Flag_Bak1 = 0;
      TrigleEp4Input(ABS_MOUSE_PACKAGE_SIZE, MouseReportBuffer);
    }
  }
  else
  {
    if(nCnt>=0 && TP[0].Tip_Switch==0)//�ɼ�
    {
      nCnt = 4;
      goto REPROT;
    }
    nCnt++;
  }
}
#else
 /*******************************************************************************
* Function Name  : ReportMousePos
* Description    : ƻ��ϵͳ��Щ��أ�
					1����ָ����������������ʱ��Ҫ�ȱ�һ֡�ɿ������ܱ���������Ҫ���ɿ�
					2��������Ҽ�������Ҽ�����Ҫ��2�Σ�������һֱ��̫�ࣨ���岻ȷ����������ɿ�
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
extern UINT8 byWheelUpDown;
extern BOOL bclickFlag;
extern BOOL bMoveFlag;
UINT8 TouchNumBak;
UINT16 Xcoorbak;
UINT16 Ycoorbak;
UINT8 TouchLeaveFlag = 0;
BOOL bTwoFingerCoorBak = 0;
UINT8 TouchCount = 0;		   //��ָ����֡����¼��Ҫ�ӳ�3֡���ϱ����
//UINT8 byWheelStopCount = 0;    //����ֹͣ��3ֹ֡ͣ

#ifdef CHIP_TYPE_GT738X
extern BOOL ReleaseFlag;
#endif

UINT8 GetTouchNum()
{
	UINT8 k, num = 0;
	for(k = 0; k < MAX_POINT; k++)
	{
		if(Press_Flag & (1L << k))
		{
			num++;
		}
	}
	return num;
}

void ReportMousePos(POINTER * pTP, UINT8 interface)
{
	static UINT16 PrePointXBak = 0xFFFF, PrePointYBak = 0xFFFF;

	//UINT8 i;
	UINT8* p;
	//UINT8 touchnumber = 0;



	TouchNum = GetTouchNum();

#ifdef CHIP_TYPE_GT738X
	if(TouchNum ==0 )
	{
		if(!ReleaseFlag)
		{
			TouchNumBak = 0;
			return;
		}
	}
#endif

	pEP3_RAM_Addr[0] = HID_RPT_ID_MOUSE_IN;

	if (Press_Flag_Bak1==0)
	{
		TouchLeaveFlag++;
		if (TouchLeaveFlag == 2)
		{
			bMoveFlag = 0;
		}

		if (TouchCount)
		{
			pEP3_RAM_Addr[1] = 1;
			TouchCount = 0;
		}
		else
		{
			if (bclickFlag == 1)
			{
				pEP3_RAM_Addr[1] = 0x02;
				bclickFlag = 0;
			}
			else
			{
				pEP3_RAM_Addr[1] = 0;
			}
			TouchNumBak = 0;
			//TouchCount = 0;

			bTwoFingerCoorBak = 0;
			//return;
		}
	}

	if(!(Press_Flag_Bak0&0x3FF) && (Press_Flag_Bak1&0x3FF))
	{
		if (TouchCount > 3)
		{
			pEP3_RAM_Addr[1] = 1;
			TouchCount = 0;
		}

		if (bTwoFingerCoorBak == 1)
		{
			if (bclickFlag == 1)
			{
				pEP3_RAM_Addr[1] = 0x02;
				bclickFlag = 0;
			}
			else
			{
				pEP3_RAM_Addr[1] = 0;
			}
			TouchCount = 0;

		}

		//Press_Flag_Bak1 = 0;

		if ((bTwoFingerCoorBak == 1) && (PrePointXBak != 0xFFFF))
		{
			pTP[0].X_pos = PrePointXBak;
			pTP[0].Y_pos = PrePointYBak;
		}

		PrePointXBak = pTP[0].X_pos;
		PrePointYBak = pTP[0].Y_pos;
	}
	else if (Press_Flag_Bak0)
	{
		TouchLeaveFlag = 0;

		if(TouchNum >= 2)
		{
			bTwoFingerCoorBak = 1;
		}


		if(TouchNum == 1 && TouchNumBak < 2)
		{
			if (TouchCount == 3)
			{
				TouchCount++;
				pEP3_RAM_Addr[1] = 0;
			}
			else if (TouchCount == 4)
			{
				pEP3_RAM_Addr[1] = 1;
			}
		}

		TouchNumBak = TouchNum;

		if ((bTwoFingerCoorBak == 1) && (PrePointXBak != 0xFFFF))
		{
			pTP[0].X_pos = PrePointXBak;
			pTP[0].Y_pos = PrePointYBak;
		}

		if(Press_Flag_Bak1==0 && Press_Flag_Bak2 ==0)
		{
			TouchCount = 1;

			pEP3_RAM_Addr[1] = 0;


			return;
		}
		else  //����֡
		{
			if (TouchCount < 3)
			{
				TouchCount++;

				return;
			}
		}
	}

	p = (UINT8*)(pTP + 0);
	{
		pEP3_RAM_Addr[2] = p[2];
		pEP3_RAM_Addr[3] = p[3];
		pEP3_RAM_Addr[4] = p[4];
		pEP3_RAM_Addr[5] = p[5];

		if(TouchNum == 2)
		{
			//�ж��л���
			if (byWheelUpDown == 1)
			{
				pEP3_RAM_Addr[1] = 0;
				pEP3_RAM_Addr[6] = 0x01;
				//byWheelStopCount = 3;
			}
			else if (byWheelUpDown == 2)
			{
				pEP3_RAM_Addr[1] = 0;
				pEP3_RAM_Addr[6] = 0xff;
				//byWheelStopCount = 3;
			}
			else
			{
				pEP3_RAM_Addr[6] = 0;
			}
		}
		else
		{
			pEP3_RAM_Addr[6] = 0;
		}

		if((Press_Flag&0x8000) && StylusPress == 0)
		{
			USBFS_EP1_Buf[1] = 0;
		}


		if(interface==0)
		{
			R16_UEP3_DMA =  (UINT16)(UINT32)pEP3_RAM_Addr;
			R8_UEP3_T_LEN = 7;
			R8_UEP3_CTRL = (R8_UEP3_CTRL & (~ MASK_UEP_T_RES)) | UEP_T_RES_ACK;
		}
		else
		{
			HidDev_Report(HID_RPT_ID_MOUSE_IN, 1, 6, pEP3_RAM_Addr + 1);
		}
	}
}
#endif
#endif

//void ReportToApple(POINTER * pTP)
//{
//	  UINT8* buf;
//		UINT8 k;
//
//    TouchNum = 0;
//		for(CheckPos = 0; CheckPos < 2; CheckPos++)
//		{
//			if(Press_Flag & (1L << CheckPos))
//			{
//				UINT8* p = (UINT8*)(pTP + CheckPos);
//				k = 0;
//
//				buf[k++] = CheckPos;
//
//				if(CheckPos == 0)
//				{
//					buf = pEP3_RAM_Addr;
//				}
//				else
//				{
//					buf = pEP4_RAM_Addr;
//				}
//
//				buf[k++] = p[0] + 2;
//				buf[k++] = CheckPos;
//				buf[k++] = p[2];
//				buf[k++] = p[3];
//				buf[k++] = p[4];
//				buf[k++] = p[5];
//				TouchNum++;
//
//				if(CheckPos == 0)
//				{
////					 R8_UEP3_T_LEN = 7;
////					 R8_UEP3_CTRL = (R8_UEP3_CTRL & (~ MASK_UEP_T_RES)) | UEP_T_RES_ACK;
//						 DevEp3In(7);
//				}
//				else
//				{
////					R8_UEP4_T_LEN = 7;
////					R8_UEP4_CTRL = (R8_UEP4_CTRL & (~ MASK_UEP_T_RES)) | UEP_T_RES_ACK;
//
//					DevEp4In(7);
//				}
//			}
//		}
//}



UINT8 package_index = 0;

//void JumpToIAP(void)
//{
//   FLASH_Unlock_Fast();
//   FLASH_ErasePage_Fast(APP_FALG_ADDR);
//   FLASH_Lock_Fast();
//   NVIC_SystemReset();
//}
//
void SoftResetChip()
{
   sw_reset_kick(sw_reset_source_get());
}



void CommandHandle( void )
{
  	UINT8 Ret = 1;

    if(PackageInfo.command == NO_OPERATE
    #if USB_HID_EN
       || ude_hid_isvalid(EP2_INDEX)==false
    #endif // USB_HID_EN
       || UsbReportPosEn==FALSE)
    {
        return;
    }
    switch(PackageInfo.command )
    {
    case I2C_READ:
    {
        PackageInfo.buf[64] = EDP2_INPUT_REPORT_ID;

        switch(PackageInfo.sub_command)
        {
          case GET_VERSION:
          {
            //memcpy(PackageInfo.buf + 65, Version, sizeof(Version));
            //printf( "GET_VERSION!\r\n" );

            break;
          }
        #if USBCONFIG
          case GET_CONFIG:
          {
            GetConfigFromFlash(PackageInfo.buf + 65);
            //memcpy(PackageInfo[QueueHead].buf + 65, (UINT8*)&config, sizeof(config));

            break;
          }
        #endif
          case CHECK_BLE_CONNECT:
          {
            //*(PackageInfo[QueueHead].buf + 65) = IsBLEConnected();// | bClientConnected;
            break;
          }
          case GET_TOUCH_CHIP_INFO:
          {
            Ret =  IICReadBytes(PackageInfo.address, PackageInfo.buf + 65, PackageInfo.len);

            break;
          }

      #if DEBUG == Debug_USB
          case DEBUG_PIPE:
          {
            print_log = 1;
            UINT8 len = pipe_read(&debug_pipe, (PackageInfo.buf + 66), 62);
            *(PackageInfo.buf + 65) = len;
            break;
          }
      #endif
          case GTXXX_ENTER_UPDATE_MODE:
          {
              PackageInfo.buf[65] = EntryUpdateMode();
              break;
          }
          case SYS_PIPE:
          {
            UINT8 len = pipe_read(&sys_pipe, (PackageInfo.buf + 66), 62);
            *(PackageInfo.buf + 65) = len;
            break;
          }
          case COOR_PIPE:
         {
           UINT8 len = pipe_read(&coor_pipe, (PackageInfo.buf + 66), 62);
           *(PackageInfo.buf + 65) = len;
           break;
         }
          case GET_BONDING_MAC:
          {
            //GetBondingMacAddr(PackageInfo[QueueHead].buf + 65);
            break;
          }
          case GET_DEVICE_MAC:
          {
            //GetMACAddress(TmpBuf);
            //memcpy(PackageInfo[QueueHead].buf + 65, TmpBuf, 6);
            break;
          }
          case CET_TOUCH_CHIP_TYPE:
          {
            PackageInfo.buf[65] = CET_TOUCH_CHIP_TYPE;
            PackageInfo.buf[66] = ChipType;
            break;
          }
          case CHECK_I2C_CONNECT:
          {
            *(PackageInfo.buf + 65) = Ret;
            break;
          }
          #if AUTO_TEST
          case GET_TEST_STATE:
          {
              PackageInfo.buf[65] = GET_TEST_STATE;
              PackageInfo.buf[66] = GetTestState();
              break;
          }
          #endif
          default:
          {

             UINT8 len = PackageInfo.len > 63 ? 63 : PackageInfo.len;

             Ret =  IICReadBytes(PackageInfo.address, PackageInfo.buf + 65, len);

             PackageInfo.address += len;
             PackageInfo.len -= len;

             if(Ret && PackageInfo.len!=0)
             {
             #if USB_HID_EN
                 usb_hid_buffer_push(EP2_INDEX, PackageInfo.buf+64, 64);
             #endif
                 return;
             }
             break;
           }
        }

        PackageInfo.command = NO_OPERATE;
    #if USB_HID_EN
        usb_hid_buffer_push(EP2_INDEX, PackageInfo.buf+64, 64);
    #endif
        break;
    }

    case I2C_WRITE:
    {
        if(PackageInfo.len > 0)
        {
            Ret =  IICWriteBytes(PackageInfo.address, PackageInfo.buf + COMMAND_HEARD_LEN, PackageInfo.len);
        }

        switch(PackageInfo.sub_command)
        {
        case RESET_TOUCH_IC:
        {
            ResetTouchChip();
            //IsDebugMode = 0;
            break;
        }
        case REBOOT_FROM_SRAM:
        {
            break;
        }
        case ENTRY_DEBUG_MODE:
        {
           IsDebugMode = 1;
           break;
        }
        case QUIT_DEBUG_MODE:
        {
            IsDebugMode = 0;
            break;
        }
        case S818X_ENTER_UPDATE_MODE:
        {
           //JumpToIAP();
           break;
        }
        case DISABLE_DIGITER_INTERFACE:
        {
          DisableDigiterInterface = 1;
          break;
        }
        case ENABLE_DIGITER_INTERFACE:
        {
          DisableDigiterInterface = 0;
          break;
        }
      #if USBCONFIG
        case SET_CONFIG:
          SaveConfig(PackageInfo.buf + COMMAND_HEARD_LEN);
          break;
      #endif
        case STOP_PRINT_LOG:
          print_log = 0;
          break;
        case ENABLE_INT_IRQ:
          enable_INT_irq = TRUE;
          break;
        case ENABLE_ESD_CHECK:
          enable_ESD_check = TRUE;
          break;
        case DISABLE_INT_IRQ:
          enable_INT_irq = FALSE;
           break;
         case DISABLE_ESD_CHECK:
           enable_ESD_check = FALSE;
           break;
        case RESET_USB_DEVICE:
             //SoftResetChip();
             break;
        case BLE_RF_TEST:
          //DTMRecCmdData(PackageInfo.buf + COMMAND_HEARD_LEN, PackageInfo.len, FALSE);
          break;
    #if AUTO_TEST
        case SET_TEST_RESULT:
        {
            if(PackageInfo.buf[COMMAND_HEARD_LEN])
            {
                ReturnTestResult(1);
            }
            else {
                ReturnTestResult(0);
            }
            break;
        }
    #endif
        default:
          break;
        }//end of switch(PackageInfo.sub_command)

        PackageInfo.command = NO_OPERATE;

        break;
    }

    default:
        PackageInfo.command = NO_OPERATE;
        break;
    }//end of switch(Command)

	if(Ret==0)
	{
	  SearchAddress(1);
	  //ResetTouchChip();
	}


	if(OutDebugMode)
	{
		UINT8 tmp[2] = {0,0};

		OutDebugMode = 0;

		if(ChipType == CHIP_TYPE_73XX)
		{
		    IICWriteBytes(0x8041, tmp, 2);
		    IICWriteBytes(0x8040, tmp, 1);
		}
		else if(ChipType == CHIP_TYPE_BERLINB ||ChipType == CHIP_TYPE_BERLIND)
		{
		    UINT8 tmp2[6] = {0x00,0x00,0x04,0x00,0x04,0x00};

		    IICWriteBytes(0x10174, tmp2, 6);
		}
		else if(ChipType == CHIP_TYPE_BERLINA)
		{
           UINT8 tmp2[7] = {0x00,0x00,0x04,0x00,0x04,0x00,0x00};

           IICWriteBytes(0x10180, tmp2, 7);
		}
		else
		{
		    IICWriteBytes(0x8046, tmp, 1);
		    IICWriteBytes(0x8040, tmp, 1);
		}
	}
}


BOOL Ep2ReporteParse(UINT8* buf)
{
    memcpy(PackageInfo.buf, buf, 64);

    if(isBerlinSeries)
    {
        PackageInfo.command = PackageInfo.buf[1] >> 7;

        PackageInfo.sub_command = PackageInfo.buf[1]&0x7F;

        PackageInfo.address = PackageInfo.buf[2];

        PackageInfo.address = (PackageInfo.address<<16) + ((UINT16)PackageInfo.buf[3] << 8) + PackageInfo.buf[4];

        PackageInfo.len = ((UINT16)PackageInfo.buf[5] << 8) + PackageInfo.buf[6];

        CommandHandle();

    }
    else
    {
      PackageInfo.command = (COMMAND_TYPE)PackageInfo.buf[1] & 0x03;

      PackageInfo.sub_command = (COMMAND_TYPE)PackageInfo.buf[2];

      PackageInfo.address = ((UINT16)PackageInfo.buf[3] << 8) + PackageInfo.buf[4];

      PackageInfo.len = ((UINT16)PackageInfo.buf[5] << 8) + PackageInfo.buf[6];

      CommandHandle();

    }
    return true;
}


void ReportInit( void )
{
    IsDebugMode = FALSE;


    wReadLen = 0;

    //todo
    PackageInfo.command = NO_OPERATE;
    PackageInfo.offset = 0;

  	Press_Flag_Bak3 = 0;
    Press_Flag_Bak2 = 0;
    Press_Flag_Bak0 = 0;
    Press_Flag_Bak1 = 0x0000;

	DisableDigiterInterface = 0;

	OutDebugMode = 1;

#if USBCONFIG
  if (config.report_type == EN_MULTITOUCH_ONLY)
#else
  if (1)
#endif
  {
    DeviceMode = DEVICE_MODE_MULTITOUCH;
  }
  else
  {
    DeviceMode = DEVICE_MODE_MOUSE;
  }

//		//KEY_CLEAR();
//	if(USBDebugTaskId==INVALID_TASK_ID)
//	{
//		USBDebugTaskId = TMOS_ProcessEventRegister(USBDebug_ProcessEvent);
//	}

}



