/*
 * TouchCommon.h
 *
 *  Created on: Nov 10, 2021
 *      Author: Administrator
 */
#ifndef INCLUDE_TOUCHCOMMON_H_
#define INCLUDE_TOUCHCOMMON_H_


#include "typedef.h"
#include "TouchConfig.h"
#include "global.h"

#if(1)//DEBUG)
  #define PRINT(format, ...)    printf(format, ##__VA_ARGS__)
#else
  #define PRINT(X...)
#endif

//#define NEW_REVOLVE_PROTOCAL
#define UART_ENABLE

#define RESOLUTION_X  (16384-1)//(4096-1)//(RESOLUTION_X-1)//
#define RESOLUTION_Y  (9600-1)//(4096-1)//(RESOLUTION_Y-1)//if (g_nResolutionX == 0) g_nResolutionX = 1920;

#ifdef WACOM_DRIVER
  #define FIVE_POINTS_PACKAGE
  //#define TWO_POINTS_PACKAGE
#else
#define TWO_POINTS_PACKAGE
//#define SIX_POINTS_PACKAGE //for xp system
//#define FIVE_POINTS_PACKAGE
//#define TEN_POINTS_PACKAGE
#endif

#ifdef SIX_POINTS_PACKAGE
  #define MAX_POINT 6
  #define PACKAGE_POINT_NUM 6
#endif

#ifdef TEN_POINTS_PACKAGE
  #define MAX_POINT 10
  #define PACKAGE_POINT_NUM 10
  #define BLE_PACKAGE_POINT_NUM 2
#endif

#ifdef FIVE_POINTS_PACKAGE
  #define MAX_POINT 10
  #define PACKAGE_POINT_NUM 5
  #define BLE_PACKAGE_POINT_NUM 2
#endif

#ifdef TWO_POINTS_PACKAGE
  #define MAX_POINT 10
  #define PACKAGE_POINT_NUM 2
  #define BLE_PACKAGE_POINT_NUM 2
#endif


#ifdef ONE_POINTS_PACKAGE
  #define MAX_POINT 10
  #define PACKAGE_POINT_NUM 1
  #define BLE_PACKAGE_POINT_NUM 1
#endif

#ifdef WACOM_DRIVER
  #define PACKAGE_POINT_SIZE  (PACKAGE_POINT_NUM * 11 + 3)
#else
  #ifdef FINGER_PRESSURE
    #define PACKAGE_POINT_SIZE  (PACKAGE_POINT_NUM * 7 + 2)
  #else
    #define PACKAGE_POINT_SIZE  (PACKAGE_POINT_NUM * 5 + 2)
    #define PACKAGE_RAM_SIZE  (PACKAGE_POINT_SIZE + 4 - (PACKAGE_POINT_SIZE%4))
    #define BLE_PACKAGE_POINT_SIZE  (BLE_PACKAGE_POINT_NUM * 5 + 2)
  #endif
#endif

#ifdef WACOM_DRIVER
  #define STYLUS_POINT_SIZE   27
#else
  #ifdef STYLUS_TILT
  #define STYLUS_POINT_SIZE   10
  #else
  #define STYLUS_POINT_SIZE   8
  #endif
#endif


#define ABS_MOUSE_PACKAGE_SIZE   6
#define MOUSE_PACKAGE_SIZE     5

#define DEVICE_MODE_MOUSE 0
#define DEVICE_MODE_SIGLETOUCH 1
#define DEVICE_MODE_MULTITOUCH  2

#ifdef UART_ENABLE
typedef enum EN_UART_STATE{
  UART_STATE_IDLE,
  UART_STATE_RECV_CMD,
  UART_STATE_CHECK_CMD,
  UART_STATE_RECV_PARAM,
  UART_STATE_ERROR = 0xFF,
}UART_STATE;

#if 0
typedef enum EN_UART_COMMAND{
  UART_COMMAND_180 = 0x33,
  UART_COMMAND_0 = 0x34,
  UART_COMMAND_90 = 0x35,
  UART_COMMAND_270 = 0x36,
}UART_COMMAND;
#else
typedef enum EN_UART_COMMAND{
  EN_UART_DIS_USB = 0xA1,
  EN_USB_DIS_UART = 0xA2,
  EN_USB_EN_UART = 0xA3,
  EN_BLE_APPLE_REPORT=0xA4,
  EN_BLE_WIN_REPORT = 0xA5,
//  DIS_BLE_TOUCH = 0xA6,
//  EN_BLE_TOUCH = 0xA7,

  USB_REPORT_FIRST = 0xA6,
  BLE_REPORT_FIRST = 0xA7,

  GET_DEVICE_NAME = 0xA8,
  GET_BLE_CONNECT_STATE = 0xA9,

  ERASE_ALL_BONDS = 0xAB,
  ENABLE_GRAB_CONNECT = 0xAC,
  DISCONNECT_BLE = 0xAD,

  UART_COMMAND_B_180 = 0x5C,
  UART_COMMAND_B_0 = 0x5A,
  UART_COMMAND_B_90 = 0x5B,
  UART_COMMAND_B_270 = 0x5D,

  UART_SWITCH_0  = 0x7A,
  UART_SWITCH_90 = 0x5E,
  UART_SWITCH_270 = 0x5F,
  UART_SWITCH_180 = 0x60,

  UART_COMMAND_A_0 = 0x6A,
  UART_COMMAND_A_90 = 0x6B,
  UART_COMMAND_A_180 = 0x6C,
  UART_COMMAND_A_270 = 0x6D,

  SET_OS_TYPE_NEW_MAC = 0xB1,
  SET_OS_TYPE_OLD_MAC = 0xB2,
}UART_COMMAND;
#endif
#ifdef NEW_REVOLVE_PROTOCAL
#pragma pack(1)
typedef struct ST_UART_LONG_CMD{
    UINT8 rs0;
    UART_COMMAND uart_cmd;
    UINT16 ax;
    UINT16 ay;
    UINT16 dx;
    UINT16 dy;
    UINT16 mx;
    UINT16 my;
    UINT16 Ax;
    UINT16 Ay;
    UINT16 Dx;
    UINT16 Dy;
    UINT8 CheckSum;
    UINT8 rs1;
}UART_LONG_CMD;

#pragma pack(1)
typedef union{
  UART_COMMAND uart_cmd;
  UART_LONG_CMD uart_long_cmd;
  UINT8 buf[sizeof(UART_LONG_CMD)];
}UART_CMD_BUF;



#else
typedef struct ST_UART_LONG_CMD{
    UINT8 rs0;
    UART_COMMAND uart_cmd;
    UINT16 x_start;
    UINT16 x_end;
    UINT16 y_start;
    UINT16 y_end;
    UINT16 x_size;
    UINT16 y_size;
    UINT8 CheckSum;
    UINT8 rs1;
}UART_LONG_CMD;

typedef union{
  UART_COMMAND uart_cmd;
  UART_LONG_CMD uart_long_cmd;
  UINT8 buf[sizeof(UART_LONG_CMD)];
}UART_CMD_BUF;
#endif
#endif


/* 设备结构体信息定义 */
typedef struct
{
    UINT8 Tip_Switch;
    UINT8 Contact_Identifier;
    UINT16 X_pos;   /* coordinate X */
    UINT16 Y_pos;   /* coordinate Y */
    UINT16 Resolution_Multi;
    UINT16 X_pos_back;
    UINT16 Y_pos_back;
} POINTER;

//#define   TRX_SEL_PIN     (GPIO_PIN_4)   //PA4
//#define   TRX_SEL_IN()    (gpio_read_data(GPIOA_REG)&TRX_SEL_PIN)
//#define   RX_RESET_PIN    (GPIO_PIN_0)   //PB0

#define   BIT0                    (0X01)
#define   BIT1                    (0X02)
#define   BIT2                    (0X04)
#define   BIT3                    (0X08)
#define   BIT4                    (0X10)
#define   BIT5                    (0X20)
#define   BIT6                    (0X40)
#define   BIT7                    (0X80)
extern BOOL BleReportPosEn;
extern BOOL UsbReportPosEn;
extern UINT8 GT_CMD_WR;
extern UINT8 GT_CMD_RD;
extern POINTER TP[10];                          /* 按照最大支持的定义 */
extern UINT8 TouchNumber;
extern UINT16 Press_Flag;
extern UINT16 StylusPress;
extern CHIP_TYPE ChipType;
extern BOOL isBerlinSeries;
extern BOOL bIsJDSeries;
extern BOOL GioInterruptFlag;
extern UINT8 TouchNum;
extern INT16 CheckPos;
extern UINT8 DeviceMode;
extern UINT8 BleDeviceMode;
extern UINT8 ShareBuf[10];
extern UINT8 NoTouchTimer;
extern UINT16 OutRangeFlag;
extern UINT16 Press_Flag;
extern UINT16 Press_Flag_Bak0;
extern UINT16 Press_Flag_Bak1;
extern UINT16 Press_Flag_Bak2;
extern UINT16 Press_Flag_Bak3;
extern BOOL UartReportPosEn;
extern BOOL ReleaseFlag;
extern UINT16 wESDCheckTime;
extern BOOL enable_INT_irq;
extern BOOL enable_ESD_check;
extern UINT8 IdleTimer;
extern UINT16 UsbInitTimer;
#ifdef NEW_REVOLVE_PROTOCAL
extern UART_CMD_BUF TpOption;
#endif
extern void (*TouchProc)(void);
void GpioFuncInit(void);
BOOL IsBerlinSeries(void);
BOOL IsBerlinChipType(CHIP_TYPE type);
void CheckTouchInt();
UINT16 GetCheckSum8(UINT8* pBuf, UINT8 len);
BOOL CheckUsbStatus();
void AppendReleaseFrame();
void IICTouchInit();
void ResetTouchChip();
void CheckUart0Comand( void );
void UartUpdatePos(POINTER * pTP);
BOOL SearchAddress( UINT8 num );
//BOOL IIC_WR(UINT8* wBuf, UINT8 wLen, BOOL wStop, UINT8* rBuf, UINT8 rLen, BOOL rStop);
BOOL PosConvert(POINTER* TP, UINT8 index);
void USART2_SendData(PUINT8 txbuf, UINT16 length);
void TIM3_Init( UINT16 arr, UINT16 psc );
#endif /* INCLUDE_TOUCHCOMMON_H_ */
