#include "TouchPanel.h"
// #include "TouchPanel_Cfg.h"
#include "Std_Types.h"
#include "device.h"
#include "touch/touch.h"
#include "math.h"
#include "stdlib.h"

#include <stddef.h>      // Defines NULL
#include <stdbool.h>     // Defines true
#include <stdlib.h>      // Defines EXIT_FAILURE
#include "definitions.h" // SYS function prototypes
#include "OsekCom/OsekCom.h"
#include "Speaker/Speaker.h"
#include "P417_SWTR_App_ert_rtw/P417_SWTR_App.h"
#include "TouchPanel/TouchPanel.h"
#include "forceSnsr/forcedetect.h"
#include "TLE9263/TLE926x_Main.h"
#include "RTE.h"
#include "calib_public.h"
#include "smartee.h"
#include "SysDiagDetect.h"

// #define PRESS_DEBUG
static uint16 sensor_signal[DEF_NUM_CHANNELS] = {0};
static uint16 sensor_reference[DEF_NUM_CHANNELS] = {0};
static TouchButtons_type key_status[BTN_MAX_CH_NUM - 5U] = {0};
static TouchSurface_type TouchSurface;
static TouchScroller_type TouchScroller;
static Buttons_SignalType Buttons_Signal[BTN_MAX_CH_NUM] = {BUTTON_RELEASE};
static TouchSlideEvent_type TouchSlideEvent;
TouchSurface_Convert_type Surface_Convert;
static PanelPress_LevelType TouchPanel_PressLevel = PANEL_PRESS_LEVEL0;
static float32 TouchPanel_PressNValue = 0.0f; // 0N
static uint8 TouchPanel_SurfaceStatus = 0;
static uint8 TouchPanel_ScrollerStatus = 0;
static uint16 TouchPanel_PressRawData = 0;
// extern volatile uint8 Vibra_force_level;

// kailong add
volatile uint16 g_Trig_Threshold = 350;
volatile uint16 g_Release_Threshold = 175;
volatile uint8 K_Vibra_Trig = 0;
volatile uint8 Reach2_flag = 0x00;
volatile uint8 Vibra_force_level = 0;
volatile uint8 last_Vibra_force_level = 0;
uint16 Over3N_vibra_req = 0x00;
uint16 Below1P5N_vibra_req = 0x00;

uint8 UDS_Vibration_Gain = 0;
uint8 UDS_DAC_Timer_Period = 80;
uint8 Touch_Flag = TOUCH_KET_OFF;


#ifdef PRESS_DEBUG
extern void CanTx(t_can_handler can_handler, bool notif, uint32_t idtp, uint16_t len, t_can_data can_data);
#endif

uint8 CurrentPosition = 0xff; // 0xff no position 1:fun1 2:fun2 3:fun3 4:up 5:down 6:left 7:right 8:ok
uint8 Surface_Button = 0xff;
#define Voice1_DEFAULT 339
#define Vol1_DEFAULT 412
#define Menu1_DEFAULT 576
#define BTN_UP_DEFAULT 269
#define BTN_DOWN_DEFAULT 398
#define BTN_LEFT_DEFAULT 261
#define BTN_RIGHT_DEFAULT 372
#define BTN_CONFIRM_DEFAULT 303
// force Trig threshold
static uint16 Button0_GetVoice1_Trig_Threshold(void)
{
    uint16 temp = Calib_GetData(0U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = Voice1_DEFAULT;
    }
    else
    {
        rtn = Calib_GetData(0U);
    }

    return rtn;
}


static uint16 Button3_GetVol1_Trig_Threshold(void)
{
    uint16 temp = Calib_GetData(1U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = Vol1_DEFAULT;
    }
    else
    {
        rtn = Calib_GetData(1U);
    }

    return rtn;
}



static uint16 Button5_GetMenu1_Trig_Threshold(void)
{
    uint16 temp = Calib_GetData(2U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = Menu1_DEFAULT;
    }
    else
    {
        rtn = Calib_GetData(2U);
    }

    return rtn;
}

static uint16 Button6_GetBTN_UP_Trig_Threshold(void)
{
    uint16 temp = Calib_GetData(3U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = BTN_UP_DEFAULT;
    }
    else
    {
        rtn = Calib_GetData(3U);
    }

    return rtn;
}

static uint16 Button7_GetBTN_DOWN_Trig_Threshold(void)
{
    uint16 temp = Calib_GetData(4U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = BTN_DOWN_DEFAULT;
    }
    else
    {
        rtn = Calib_GetData(4U);
    }

    return rtn;
}

static uint16 Button8_GetBTN_LEFT_Trig_Threshold(void)
{
    uint16 temp = Calib_GetData(5U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = BTN_LEFT_DEFAULT;
    }
    else
    {
        rtn = Calib_GetData(5U);
    }

    return rtn;
}

static uint16 Button9_GetBTN_RIGHT_Trig_Threshold(void)
{
    uint16 temp = Calib_GetData(6U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = BTN_RIGHT_DEFAULT;
    }
    else
    {
        rtn = Calib_GetData(6U);
    }

    return rtn;
}

static uint16 Button10_GetBTN_CONFIRM_Trig_Threshold(void)
{
    uint16 temp = Calib_GetData(7U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = BTN_CONFIRM_DEFAULT;
    }
    else
    {
        rtn = Calib_GetData(7U);
    }

    return rtn;
}

// force Release threshold
static uint16 Button0_GetVoice1_Release_Threshold(void)
{
    uint16 temp = Calib_GetData(0U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = Voice1_DEFAULT / 2;
    }
    else
    {
        rtn = Calib_GetData(0U) / 2;
    }

    return rtn;
}


static uint16 Button3_GetVol1_Release_Threshold(void)
{
    uint16 temp = Calib_GetData(1U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = Vol1_DEFAULT / 2;
    }
    else
    {
        rtn = Calib_GetData(1U) / 2;
    }

    return rtn;
}


static uint16 Button5_GetMenu1_Release_Threshold(void)
{
    uint16 temp = Calib_GetData(2U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = Menu1_DEFAULT / 2;
    }
    else
    {
        rtn = Calib_GetData(2U) / 2;
    }

    return rtn;
}

static uint16 Button6_GetBTN_UP_Release_Threshold(void)
{
    uint16 temp = Calib_GetData(3U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = BTN_UP_DEFAULT / 2;
    }
    else
    {
        rtn = Calib_GetData(3U) / 2;
    }

    return rtn;
}

static uint16 Button7_GetBTN_DOWN_Release_Threshold(void)
{
    uint16 temp = Calib_GetData(4U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = BTN_DOWN_DEFAULT / 2;
    }
    else
    {
        rtn = Calib_GetData(4U) / 2;
    }

    return rtn;
}

static uint16 Button8_GetBTN_LEFT_Release_Threshold(void)
{
    uint16 temp = Calib_GetData(5U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = BTN_LEFT_DEFAULT / 2;
    }
    else
    {
        rtn = Calib_GetData(5U) / 2;
    }

    return rtn;
}

static uint16 Button9_GetBTN_RIGHT_Release_Threshold(void)
{
    uint16 temp = Calib_GetData(6U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = BTN_RIGHT_DEFAULT / 2;
    }
    else
    {
        rtn = Calib_GetData(6U) / 2;
    }

    return rtn;
}

static uint16 Button10_GetBTN_CONFIRM_Release_Threshold(void)
{
    uint16 temp = Calib_GetData(7U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = BTN_CONFIRM_DEFAULT / 2;
    }
    else
    {
        rtn = Calib_GetData(7U) / 2;
    }

    return rtn;
}

/**
 * \brief Sets buffers to a specified byte value.
 * \param pDestP buffer with set value
 * \param u8ValueP byte value
 * \param BytesCountP number of bytes
 * \return None
 */
static void LIB_Mem_MemSet(void *const pDestP, const uint8 u8ValueP, const uint32 u32BytesCountP)
{

    uint8 *pu8BufL = (uint8 *)pDestP;
    uint32 u32IdxL;

    if ((NULL_PTR != pu8BufL))
    {
        u32IdxL = u32BytesCountP;

        while (u32IdxL > (uint32)0)
        {
            --u32IdxL;

            (*pu8BufL) = u8ValueP;
            pu8BufL = &pu8BufL[1];
        }
    }
}

static float32 AlideAverage_Filter(uint8 mode, float32 RawData)
{
#define MAX_ALIDE_AVG_NUM 8
    static uint16 CalcAverageNum = 0;
    static uint16 DIndex = 0;
    static uint16 Mode_Status = 0;
    static float32 RawData_Table[MAX_ALIDE_AVG_NUM] = {0};
    uint16 i;
    float32 Sum = 0;
    float32 AverageValue = 0;
    if (mode == 0x00)
    {
        CalcAverageNum = 0;
        DIndex = 0;
        Mode_Status = 0;
        for (i = 0; i < MAX_ALIDE_AVG_NUM; i++)
        {
            RawData_Table[i] = 0.0f;
        }
    }
    else
    {
        if (Mode_Status == 0x00)
        {
            if (CalcAverageNum < MAX_ALIDE_AVG_NUM)
            {
                RawData_Table[CalcAverageNum++] = RawData;
                for (i = 0; i < CalcAverageNum; i++)
                {
                    Sum += RawData_Table[i];
                }
            }
            if (CalcAverageNum >= MAX_ALIDE_AVG_NUM)
            {
                Mode_Status = 0x01;
                DIndex = 0;
            }
            AverageValue = Sum / ((float32)CalcAverageNum + 0.000001f);
        }
        else
        {
            RawData_Table[DIndex++] = RawData;
            if (DIndex >= MAX_ALIDE_AVG_NUM)
            {
                DIndex = 0x00;
            }
            for (i = 0; i < MAX_ALIDE_AVG_NUM; i++)
            {
                Sum += RawData_Table[i];
            }
            AverageValue = Sum / MAX_ALIDE_AVG_NUM;
        }
    }
    return AverageValue;
}

void TouchPanel_init(void)
{
    uint8 index;
    // touch_init();
    uint8_t eedata[5];
    LIB_Mem_MemSet(&TouchSurface, 0x00U, sizeof(TouchSurface_type));
    LIB_Mem_MemSet(&TouchScroller, 0x00U, sizeof(TouchScroller_type));
    LIB_Mem_MemSet(&TouchSlideEvent, 0x00U, sizeof(TouchSlideEvent_type));
    LIB_Mem_MemSet(&Surface_Convert, 0x00U, sizeof(TouchSurface_Convert_type));
    SmartEE_Read(0x780, eedata, 3);
    if (eedata[0] < 4)
    {
        UDS_Vibration_Gain = eedata[0];
    }
    else
    {
        UDS_Vibration_Gain = SA51024_STRENGTH_LEVEL0;
    }
    if (eedata[2] < 255 && eedata[2] >= 40)
    {
        UDS_DAC_Timer_Period = eedata[2];
    }
    else
    {
        UDS_DAC_Timer_Period = 110;
    }
    TC0_REGS->COUNT16.TC_CC[0U] = UDS_DAC_Timer_Period;
    AlideAverage_Filter(0, 0);
    TouchPanel_PressLevel = PANEL_PRESS_LEVEL0;
    TouchPanel_PressNValue = 0.0f; // 0N
    TouchPanel_SurfaceStatus = 0;
    TouchPanel_ScrollerStatus = 0;
    TouchPanel_PressRawData = 0;
    for (index = 0; index < DEF_NUM_CHANNELS; index++)
    {
        sensor_signal[index] = 0x00;
        sensor_reference[index] = 0x00;
        if (index < (BTN_MAX_CH_NUM - 5u))
        {
            key_status[index].status = 0x00;
            key_status[index].RunCycle = 0x00;
        }
        if (index < BTN_MAX_CH_NUM)
        {
            Buttons_Signal[index] = BUTTON_RELEASE;
        }
    }
}

void GetSurface_Position(uint8 *x, uint8 *y)
{
    if (x != NULL_PTR)
    {
        *x = Surface_Convert.convert_x;
    }
    if (y != NULL_PTR)
    {
        *y = Surface_Convert.convert_y;
    }
}

void GetScroller_Position(uint8 *x, uint8 *y)
{
    if (x != NULL_PTR)
    {
        *x = TouchScroller.x_position;
    }
    if (y != NULL_PTR)
    {
        *y = TouchScroller.y_position;
    }
}

TP_SlideDirection_Enum TouchPanel_SlideEventRead(void)
{
    TP_SlideDirection_Enum Dir = TP_SlideDirection_NONE;
    if (TouchSlideEvent.Event == TRUE)
    {
        Dir = TouchSlideEvent.Direction;
    }
    return Dir;
}

TP_SlideLevel_Enum TouchPanel_SlideUpDownLevelRead(void)
{
    TP_SlideLevel_Enum SlideLevel = TP_SlideLevel_NONE;
    if (TouchSlideEvent.Event == TRUE)
    {
        SlideLevel = TouchSlideEvent.UpDown_SlideLevel;
    }
    return SlideLevel;
}

Buttons_SignalType TouchPanel_BtnSignalRead(Buttons_ChType ChId)
{
    Buttons_SignalType Signal = BUTTON_RELEASE;

    if (ChId < BTN_MAX_CH_NUM)
    {
        Signal = Buttons_Signal[ChId];
    }

    return Signal;
}

PanelPress_LevelType TouchPanel_PressLevelRead(void)
{
    return TouchPanel_PressLevel;
}

boolean TouchSurface_is_TouchActive(void)
{
    boolean ret = FALSE;
    if (TouchPanel_SurfaceStatus & TOUCH_ACTIVE)
    {
        ret = TRUE;
    }

    return ret;
}

boolean TouchButton_is_TouchActive(Buttons_ChType BtId)
{
    boolean ret = FALSE;
    if (QTM_KEY_STATE_DETECT == (key_status[BtId].status) && BtId < BTN_UP)
    {
        ret = TRUE;
    }

    return ret;
}

boolean TouchScroller_is_TouchActive(void)
{
    boolean ret = FALSE;
    if (TouchPanel_ScrollerStatus & TOUCH_ACTIVE)
    {
        ret = TRUE;
    }

    return ret;
}

Buttons_SignalType TouchSurface_XY_StsRead(void)
{
    Buttons_SignalType XY_Sts = BUTTON_RELEASE;
    if (TouchPanel_SurfaceStatus & TOUCH_ACTIVE)
    {

        switch (TouchPanel_PressLevel)
        {
        case PANEL_PRESS_LEVEL0:
            XY_Sts = BUTTON_TOUCH;
            break;
        case PANEL_PRESS_LEVEL1:
            XY_Sts = BUTTON_TOUCH;
            break;
        case PANEL_PRESS_LEVEL2:
            XY_Sts = BUTTON_TOUCH_AND_PRESS;
            break;
        default:
            break;
        }
    }


    return XY_Sts;
}

// kailong

#if 0
uint8 Vibra_PressCheck(void)
{
  uint16 RawData = 0;
	uint16 temp_RawData = 0;
	static uint16 LastRawData = 0;
	static PanelPress_LevelType  Last_PressLevel = PANEL_PRESS_LEVEL0; 
	PanelPress_LevelType ret_Press = PANEL_PRESS_LEVEL0;
	float32 FilterValue;
	float32 PressNValueTable[3][2] = {
		{1.5f, 3.0f},
		{0.5f, 3.0f},
		{1.0f, 1.8f}
	};
		
	if(Last_PressLevel > PANEL_PRESS_LEVEL2){ Last_PressLevel = PANEL_PRESS_LEVEL0;}
    ////if(TRUE == GET_TouchPanel_is_Press())
	temp_RawData = (0xfff & Get_forcedetect_force_value());	
	if(SPEAKER_UPDATE_CYCLE == Get_Speaker_Main_State())
	{
		RawData = LastRawData;
	}
	else
	{
		if(temp_RawData&0x800)//negative
		{
			RawData = 0x00;
		}else{
			RawData = temp_RawData;
		}
		
		if(abs(RawData - LastRawData) > (10*0xF2))
		{
			RawData = LastRawData;
		}
	}
	
	TouchPanel_PressRawData = RawData;
	TouchPanel_PressNValue = ((float32)RawData / (float32)0xF2);
	FilterValue = AlideAverage_Filter(1, TouchPanel_PressNValue);
	if(FilterValue < PressNValueTable[Last_PressLevel][0]){//< 1N
		ret_Press = PANEL_PRESS_LEVEL0;
        //Vibra_force_level=PANEL_PRESS_LEVEL0;
	}else if(FilterValue >= PressNValueTable[Last_PressLevel][0] && 
	 	     FilterValue < PressNValueTable[Last_PressLevel][1]){//1N~3N
		ret_Press = PANEL_PRESS_LEVEL1;
        //Vibra_force_level=PANEL_PRESS_LEVEL1;
	}else{//>=3
		ret_Press = PANEL_PRESS_LEVEL2;
        //Vibra_force_level=PANEL_PRESS_LEVEL2;
	}
    
    

	LastRawData = RawData;
	Last_PressLevel = ret_Press;
    return ret_Press;
}

#endif

// kailong

static PanelPress_LevelType TouchPanel_PressCheck(void)
{
    // static uint16 ForceHcount = 0;
    // static uint16 ForceMcount = 0;
    // static uint16 ForceLcount = 0;

    static uint16 tempForcePress = 0;
    static uint16 tempForceRelease = 0;

    uint16 RawData = 0;
    uint16 temp_RawData = 0;
    static uint16 LastRawData = 0;
    static PanelPress_LevelType Last_PressLevel = PANEL_PRESS_LEVEL0;
    static uint8 P_force_Reach2_flag = 0;
    PanelPress_LevelType ret_Press = PANEL_PRESS_LEVEL0;
    float32 FilterValue;
    uint16 Backup_Force_Value_Pad;
    uint16 Backup_Force_Value_Button;
    Backup_Force_Value_Pad = Touch_Sensor_delta05 + Touch_Sensor_delta06 + Touch_Sensor_delta07 + Touch_Sensor_delta08 + Touch_Sensor_delta09 +
                             +Touch_Sensor_delta10 + Touch_Sensor_delta11 + Touch_Sensor_delta12 + Touch_Sensor_delta13 + Touch_Sensor_delta14;
    Backup_Force_Value_Button = Touch_Sensor_delta00 + Touch_Sensor_delta01 + Touch_Sensor_delta02 + Touch_Sensor_delta03 + Touch_Sensor_delta04;
    // float32 PressNValueTable[3][2] = {
    //	{1.21f, 1.73f},
    //	{0.5f, 3.0f},
    //	{1.0f, 1.8f}
    // };

    if (CurrentPosition != 0xff)
    {
        tempForcePress = g_Trig_Threshold;
        tempForceRelease = g_Release_Threshold;
    }
    // else
    //{
    //  return PANEL_PRESS_LEVEL0;
    //}

    if (Last_PressLevel > PANEL_PRESS_LEVEL2)
    {
        Last_PressLevel = PANEL_PRESS_LEVEL0;
    }
    ////if(TRUE == GET_TouchPanel_is_Press())
    temp_RawData = (0xfff & Get_forcedetect_force_value());
    if (SPEAKER_UPDATE_CYCLE == Get_Speaker_Main_State())
    {
        RawData = LastRawData;
    }
    else
    {
        if (temp_RawData & 0x800) // negative
        {
            RawData = 0x00;
        }
        else
        {
            RawData = temp_RawData;
        }

        if (abs(RawData - LastRawData) > (10 * 0xF2))
        {
            RawData = LastRawData;
        }
    }

    TouchPanel_PressRawData = RawData;
    TouchPanel_PressNValue = ((float32)RawData / (float32)0xF2);
    FilterValue = AlideAverage_Filter(1, TouchPanel_PressNValue);

    if (Force_Sens_Err == 0)
    {
        if (FilterValue <= ((float32)tempForceRelease / (float32)0xF2))
        {   //< 1.5N
            // ForceHcount = 0;
            // ForceMcount = 0;
            // ForceLcount++;
            P_force_Reach2_flag = 0;
            // if(ForceLcount > 40)
            //{
            //   ForceLcount = 40;

            ret_Press = PANEL_PRESS_LEVEL0;

            //}
            Vibra_force_level = PANEL_PRESS_LEVEL0;
        }
        else if (FilterValue >= ((float32)tempForceRelease / (float32)0xF2) &&
                 FilterValue < ((float32)tempForcePress / (float32)0xF2))
        { // 1.5N~3N

            // ForceHcount = 0;
            // ForceMcount++;
            // ForceLcount = 0;
            // if(ForceMcount > 40)
            // {
            //  ForceMcount = 40;
            if (P_force_Reach2_flag == 1)
            {
                // if(TOUCH_SCREEN_CONTACT_AREA_INVALID_NUM > get_contact_size())
                //{

                Vibra_force_level = PANEL_PRESS_LEVEL2;
                ret_Press = PANEL_PRESS_LEVEL2;
                //}
                // else
                //{
                //	ret_Press = PANEL_PRESS_LEVEL0;
                //	Vibra_force_level=PANEL_PRESS_LEVEL0;
                //}
            }
            else
            {
                // if(TOUCH_SCREEN_CONTACT_AREA_INVALID_NUM > get_contact_size())
                //{

                Vibra_force_level = PANEL_PRESS_LEVEL1;
                ret_Press = PANEL_PRESS_LEVEL1;
                //}
                // else
                //{
                // ret_Press = PANEL_PRESS_LEVEL0;
                // Vibra_force_level=PANEL_PRESS_LEVEL0;
                //}
            }
        }

        else
        {   //>=3
            // ForceHcount++;
            // ForceMcount = 0;
            // ForceLcount = 0;
            P_force_Reach2_flag = 1;
            // if(ForceHcount > 40)
            // {
            //  ForceHcount = 40;

            // if(TOUCH_SCREEN_CONTACT_AREA_INVALID_NUM > get_contact_size())
            //{
            ret_Press = PANEL_PRESS_LEVEL2;

            //}
            // else
            //{
            //	ret_Press = PANEL_PRESS_LEVEL0;
            //	Vibra_force_level=PANEL_PRESS_LEVEL0;
            //}

            Vibra_force_level = PANEL_PRESS_LEVEL2;
        }
    }
    else // backup When force sensor error happened
    {
        if (CurrentPosition <= 3) // Button
        {
            if (Backup_Force_Value_Button <= 35)
            {   //< 1.5N
                // ForceHcount = 0;
                // ForceMcount = 0;
                // ForceLcount++;
                P_force_Reach2_flag = 0;
                // if(ForceLcount > 40)
                //{
                //   ForceLcount = 40;

                ret_Press = PANEL_PRESS_LEVEL0;

                //}
                Vibra_force_level = PANEL_PRESS_LEVEL0;
            }
            else if (Backup_Force_Value_Button > 35 &&
                     Backup_Force_Value_Button < 70)
            { // 1.5N~3N

                // ForceHcount = 0;
                // ForceMcount++;
                // ForceLcount = 0;
                // if(ForceMcount > 40)
                // {
                //  ForceMcount = 40;
                if (P_force_Reach2_flag == 1)
                {
                    // if(TOUCH_SCREEN_CONTACT_AREA_INVALID_NUM > get_contact_size())
                    //{

                    Vibra_force_level = PANEL_PRESS_LEVEL2;
                    ret_Press = PANEL_PRESS_LEVEL2;
                    //}
                    // else
                    //{
                    //	ret_Press = PANEL_PRESS_LEVEL0;
                    //	Vibra_force_level=PANEL_PRESS_LEVEL0;
                    //}
                }
                else
                {
                    // if(TOUCH_SCREEN_CONTACT_AREA_INVALID_NUM > get_contact_size())
                    //{

                    Vibra_force_level = PANEL_PRESS_LEVEL1;
                    ret_Press = PANEL_PRESS_LEVEL1;
                    //}
                    // else
                    //{
                    // ret_Press = PANEL_PRESS_LEVEL0;
                    // Vibra_force_level=PANEL_PRESS_LEVEL0;
                    //}
                }
            }

            else
            {   //>=3
                // ForceHcount++;
                // ForceMcount = 0;
                // ForceLcount = 0;
                P_force_Reach2_flag = 1;
                // if(ForceHcount > 40)
                // {
                //  ForceHcount = 40;

                // if(TOUCH_SCREEN_CONTACT_AREA_INVALID_NUM > get_contact_size())
                //{
                ret_Press = PANEL_PRESS_LEVEL2;

                //}
                // else
                //{
                //	ret_Press = PANEL_PRESS_LEVEL0;
                //	Vibra_force_level=PANEL_PRESS_LEVEL0;
                //}

                Vibra_force_level = PANEL_PRESS_LEVEL2;
            }
        }
        else // Pad
        {
            if (Backup_Force_Value_Pad <= 100)
            {   //< 1.5N
                // ForceHcount = 0;
                // ForceMcount = 0;
                // ForceLcount++;
                P_force_Reach2_flag = 0;
                // if(ForceLcount > 40)
                //{
                //   ForceLcount = 40;

                ret_Press = PANEL_PRESS_LEVEL0;

                //}
                Vibra_force_level = PANEL_PRESS_LEVEL0;
            }
            else if (Backup_Force_Value_Pad > 100 &&
                     Backup_Force_Value_Pad < 200)
            { // 1.5N~3N

                // ForceHcount = 0;
                // ForceMcount++;
                // ForceLcount = 0;
                // if(ForceMcount > 40)
                // {
                //  ForceMcount = 40;
                if (P_force_Reach2_flag == 1)
                {
                    // if(TOUCH_SCREEN_CONTACT_AREA_INVALID_NUM > get_contact_size())
                    //{

                    Vibra_force_level = PANEL_PRESS_LEVEL2;
                    ret_Press = PANEL_PRESS_LEVEL2;
                    //}
                    // else
                    //{
                    //	ret_Press = PANEL_PRESS_LEVEL0;
                    //	Vibra_force_level=PANEL_PRESS_LEVEL0;
                    //}
                }
                else
                {
                    // if(TOUCH_SCREEN_CONTACT_AREA_INVALID_NUM > get_contact_size())
                    //{

                    Vibra_force_level = PANEL_PRESS_LEVEL1;
                    ret_Press = PANEL_PRESS_LEVEL1;
                    //}
                    // else
                    //{
                    // ret_Press = PANEL_PRESS_LEVEL0;
                    // Vibra_force_level=PANEL_PRESS_LEVEL0;
                    //}
                }
            }

            else
            {   //>=3
                // ForceHcount++;
                // ForceMcount = 0;
                // ForceLcount = 0;
                P_force_Reach2_flag = 1;
                // if(ForceHcount > 40)
                // {
                //  ForceHcount = 40;

                // if(TOUCH_SCREEN_CONTACT_AREA_INVALID_NUM > get_contact_size())
                //{
                ret_Press = PANEL_PRESS_LEVEL2;

                //}
                // else
                //{
                //	ret_Press = PANEL_PRESS_LEVEL0;
                //	Vibra_force_level=PANEL_PRESS_LEVEL0;
                //}

                Vibra_force_level = PANEL_PRESS_LEVEL2;
            }
        }
    }

    LastRawData = RawData;
    Last_PressLevel = ret_Press;
#ifdef PRESS_DEBUG
    {
        uint8 data[8] = {0};
        uint8 data2[8] = {0};
        uint16 g_sss = TouchScroller.y_position;
        data[0] = ((GET_TouchPanel_is_Press()) << 2);
        data[1] = ret_Press;
        //   data[5] =(uint8)(TouchPanel_PressRawData & 0xff) ;
        //   data[4] =(uint8)(( TouchPanel_PressRawData &0xff00)>>8);

        data[5] = (uint8)(g_sss & 0xff);
        data[4] = (uint8)((g_sss & 0xff00) >> 8);
        CanTx(0, true, 0x111, 8, data);

        data2[0] = key_status[0].status;
        data2[1] = key_status[1].status;
        data2[2] = key_status[2].status;
        data2[3] = key_status[3].status;
        data2[4] = key_status[4].status;
        CanTx(0, true, 0x222, 8, data2);
    }
#endif
    return ret_Press;
}

static Buttons_SignalType TouchSurface_BntCheck(Buttons_ChType ChId, TouchPanel_EventType TP_Event)
{
    Buttons_SignalType BSignal = BUTTON_RELEASE;
    // static uint8 PB_force_Reach2_flag = 0;
    // uint8 x_pos = 0, y_pos = 0;
    uint8 data_ready = 0x00;
    // GetSurface_Position(&x_pos, &y_pos);
    switch (ChId)
    {
    case BTN_UP:
        if (Surface_Button == 1) // 37
        {
            data_ready = 0x01;
        }
        break;
    case BTN_DOWN:
        if (Surface_Button == 2) // 223
        {
            data_ready = 0x01;
        }
        break;
    case BTN_LEFT:
        if (Surface_Button == 3)
        {
            data_ready = 0x01;
        }
        break;
    case BTN_RIGHT:
        if (Surface_Button == 4)
        {
            data_ready = 0x01;
        }
        break;
    case BTN_CONFIRM:
        if (Surface_Button == 5)
        {
            data_ready = 0x01;
        }
        break;
    default:
        break;
    }

    if (data_ready != 0x00)
    {
        if (TP_Event == TOUCH_PANEL_PRESS_EVENT)
        {

            switch (TouchPanel_PressLevel)
            {
            case PANEL_PRESS_LEVEL0:
                // PB_force_Reach2_flag = 0;
                BSignal = BUTTON_TOUCH;
            case PANEL_PRESS_LEVEL1:
                // if(PB_force_Reach2_flag == 1)
                //{
                // BSignal = BUTTON_TOUCH_AND_PRESS;
                // }
                // else
                //{
                BSignal = BUTTON_TOUCH;
                // }
                break;
            case PANEL_PRESS_LEVEL2:
                // PB_force_Reach2_flag = 1;
                BSignal = BUTTON_TOUCH_AND_PRESS;
                break;
            default:
                break;
            }
        }
        else if (TP_Event == TOUCH_PANEL_BERR_EVENT)
        {
            BSignal = BUTTON_RELEASE; //////////////BUTTON_INVALID;
        }
    }

    return BSignal;
}

static Buttons_SignalType TouchButton_BntCheck(Buttons_ChType ChId)
{
    Buttons_SignalType BSignal = BUTTON_RELEASE;
    // static uint8 TB_force_Reach2_flag = 0;
    if (ChId < BTN_UP)
    {
        key_status[ChId].status = get_sensor_state(ChId);
        if (QTM_KEY_STATE_DETECT == (key_status[ChId].status))
        { // KEY_TOUCHED_MASK
            // Touch detect
            key_status[ChId].RunCycle++;
            if (TP_RUNNING_ERROR_CYCLE <= key_status[ChId].RunCycle)
            {
                key_status[ChId].RunCycle = TP_RUNNING_ERROR_CYCLE;
                BSignal = BUTTON_RELEASE; // BUTTON_INVALID;
            }
            else
            {
                switch (TouchPanel_PressLevel)
                {
                case PANEL_PRESS_LEVEL0:
                    // TB_force_Reach2_flag = 0;
                    BSignal = BUTTON_TOUCH;
                case PANEL_PRESS_LEVEL1:
                    // if(TB_force_Reach2_flag == 1)
                    //{
                    // BSignal = BUTTON_TOUCH_AND_PRESS;
                    // }
                    // else
                    //{
                    BSignal = BUTTON_TOUCH;
                    //}
                    break;
                case PANEL_PRESS_LEVEL2:
                    // TB_force_Reach2_flag = 1;
                    BSignal = BUTTON_TOUCH_AND_PRESS;
                    break;
                default:
                    break;
                }
            }
        }
        else
        {
            // Touch No detect
            key_status[ChId].RunCycle = 0x00;
        }
    }
    return BSignal;
}

static float32 GetSlide_Angle(float32 dx, float32 dy)
{
    return (atan2(dy, dx) * 180 / PI);
}

/**************************************************************/
//                 x						X(y)
//                 ^						^
//                 |		mapping			|
//                 |        =======>        |
//                 |						|
//    y <----------|(0,0)	   X(-y)<-------|(0,0)
/*************************************************************/
static void CheckSlide_Level(TP_SlideDirection_Enum SlideDir, uint8 AbsX, uint8 AbsY)
{
    switch (SlideDir)
    {
    case TP_SlideDirection_ShortUP:
    case TP_SlideDirection_LongUP:
        TouchSlideEvent.UpDown_SlideDistance = AbsY;
        if (TouchSlideEvent.UpDown_SlideDistance > SLIDE_UD_LEVEL1_DISTANCE && TouchSlideEvent.UpDown_SlideDistance <= SLIDE_UD_LEVEL2_DISTANCE)
        {
            TouchSlideEvent.UpDown_SlideLevel = TP_SlideLevel1_UP;
        }
        else if (TouchSlideEvent.UpDown_SlideDistance > SLIDE_UD_LEVEL2_DISTANCE && TouchSlideEvent.UpDown_SlideDistance <= SLIDE_UD_LEVEL3_DISTANCE)
        {
            TouchSlideEvent.UpDown_SlideLevel = TP_SlideLevel2_UP;
        }
        else if (TouchSlideEvent.UpDown_SlideDistance > SLIDE_UD_LEVEL3_DISTANCE)
        {
            TouchSlideEvent.UpDown_SlideLevel = TP_SlideLevel3_UP;
        }
        else
        {
            TouchSlideEvent.UpDown_SlideLevel = TP_SlideLevel_NONE;
        }
        break;
    case TP_SlideDirection_ShortDOWN:
    case TP_SlideDirection_LongDOWN:
        TouchSlideEvent.UpDown_SlideDistance = AbsY;
        if (TouchSlideEvent.UpDown_SlideDistance > SLIDE_UD_LEVEL1_DISTANCE && TouchSlideEvent.UpDown_SlideDistance <= SLIDE_UD_LEVEL2_DISTANCE)
        {
            TouchSlideEvent.UpDown_SlideLevel = TP_SlideLevel1_DOWN;
        }
        else if (TouchSlideEvent.UpDown_SlideDistance > SLIDE_UD_LEVEL2_DISTANCE && TouchSlideEvent.UpDown_SlideDistance <= SLIDE_UD_LEVEL3_DISTANCE)
        {
            TouchSlideEvent.UpDown_SlideLevel = TP_SlideLevel2_DOWN;
        }
        else if (TouchSlideEvent.UpDown_SlideDistance > SLIDE_UD_LEVEL3_DISTANCE)
        {
            TouchSlideEvent.UpDown_SlideLevel = TP_SlideLevel3_DOWN;
        }
        else
        {
            TouchSlideEvent.UpDown_SlideLevel = TP_SlideLevel_NONE;
        }
        break;
    default:
        TouchSlideEvent.UpDown_SlideDistance = 0x00;
        TouchSlideEvent.UpDown_SlideLevel = TP_SlideLevel_NONE;
        break;
    }
}
static TP_SlideDirection_Enum GetSlide_Direction(uint8 startX, uint8 startY, uint8 endX, uint8 endY)
{
    TP_SlideDirection_Enum result = TP_SlideDirection_NONE;
    sint32 CoordMapping_sx, CoordMapping_sy;
    sint32 CoordMapping_ex, CoordMapping_ey;
    uint16 absx, absy;
    sint32 dy, dx;
    float32 angle;

    CoordMapping_sx = (1u * (sint32)startX);
    CoordMapping_sy = (-1u * (sint32)startY);
    CoordMapping_ex = (1u * (sint32)endX);
    CoordMapping_ey = (-1u * (sint32)(endY));
    dx = CoordMapping_ex - CoordMapping_sx;
    dy = CoordMapping_ey - CoordMapping_sy;

    absx = abs(dx);
    absy = abs(dy);
    if (TouchSurface.Event == TOUCH_PANEL_MOVE_EVENT && (absx > SLIDE_LR_MIN_DISTANCE || absy > SLIDE_UD_MIN_DISTANCE))
    {
        /*Ignore small Slide*/
        angle = GetSlide_Angle(dx, dy);
        if (angle >= -45.0 && angle < 45.0)
        {
            if (absx > SLIDE_LR_SHORT_DISTANCE_NUM)
            {
                result = TP_SlideDirection_LongRIGHT;
            }
            else
            {
                result = TP_SlideDirection_ShortRIGHT;
            }
        }
        else if (angle >= 45.0 && angle < 135.0)
        {
            if (absy > SLIDE_UD_SHORT_DISTANCE_NUM)
            {
                result = TP_SlideDirection_LongUP;
            }
            else
            {
                result = TP_SlideDirection_ShortUP;
            }
        }
        else if (angle >= -135.0 && angle < -45.0)
        {
            if (absy > SLIDE_UD_SHORT_DISTANCE_NUM)
            {
                result = TP_SlideDirection_LongDOWN;
            }
            else
            {
                result = TP_SlideDirection_ShortDOWN;
            }
        }
        else if ((angle >= 135.0 && angle <= 180.0) || (angle >= -180.0 && angle < -135.0))
        {
            if (absx > SLIDE_LR_SHORT_DISTANCE_NUM)
            {
                result = TP_SlideDirection_LongLEFT;
            }
            else
            {
                result = TP_SlideDirection_ShortLEFT;
            }
        }
        CheckSlide_Level(result, absx, absy);
    }

    return result;
}

#if 0
uint8 KalmanFilter(uint8 inData, uint8 Dtype)
{
    static float32  kalman[2] = {0}; 
    static float32  p[2] = {10, 10};
    float32  q = 0.001;
    float32  r = 0.001;
    float32  kg = 0;
    if(Dtype < 2)
    {
	    p[Dtype] += q;
	    kg = p[Dtype] / ( p[Dtype] + r );
	    kalman[Dtype] = kalman[Dtype] + (kg * (inData - kalman[Dtype]));
	    p[Dtype] = (1 - kg) * p[Dtype];
    }    
    return (uint8)kalman[Dtype]; 
}
#endif

struct Point
{
    float x;
    float y;
};

// 判断一个点是否在三角形内部
uint8_t point_in_triangle(struct Point p, struct Point p1, struct Point p2, struct Point p3)
{
    float s = p1.y * p3.x - p1.x * p3.y + (p3.y - p1.y) * p.x + (p1.x - p3.x) * p.y;
    float t = p1.x * p2.y - p1.y * p2.x + (p1.y - p2.y) * p.x + (p2.x - p1.x) * p.y;
    if ((s < 0) != (t < 0))
        return 0;
    float A = -p2.y * p3.x + p1.y * (p3.x - p2.x) + p1.x * (p2.y - p3.y) + p2.x * p3.y;
    if (A < 0.0)
    {
        s = -s;
        t = -t;
        A = -A;
    }
    return (s > 0 && t > 0 && (s + t) < A);
}

// 判断一个点是否在四边形内部
uint8_t point_in_quadrilateral(struct Point p, struct Point q1, struct Point q2, struct Point q3, struct Point q4)
{
    return point_in_triangle(p, q1, q2, q3) || point_in_triangle(p, q1, q3, q4);
}

static void TouchPanel_XY_Convert(uint8 origin_x, uint8 origin_y)
{
#define Y_OFFSET 40
#define START_X 95
#define START_Y 240
#define END_X 255
#define END_Y 197
    TouchXY_type start, end;
    float32 k, b, y;

    uint8 temp_x, temp_y;
    uint8 convert_x, convert_y;
    float32 f_x;
    temp_x = 255u - origin_y;
    f_x = (((float32)temp_x) / (255.0f / 160.0f)); //  255u-95u
    if (f_x - (uint32)(f_x) < 0.5f)
    {
        convert_x = (uint8)f_x + 95u;
    }
    else
    {
        convert_x = (uint8)f_x + 1u + 95u;
    }

    temp_y = 255u - origin_x;
    convert_y = temp_y;
    /*******************************************/

    start.x = START_X;
    start.y = START_Y;
    end.x = END_X;
    end.y = END_Y;

    k = ((end.y - start.y) / (end.x - start.x));
    b = (start.y - k * start.x);
    y = k * convert_x + b;
    convert_y = (uint8)(((float32)temp_y * (y - Y_OFFSET)) / 255 + Y_OFFSET);

    Surface_Convert.convert_x = convert_x; // KalmanFilter(convert_x, 0);
    Surface_Convert.convert_y = convert_y; // KalmanFilter(convert_y, 0);
}

extern void CanTx(t_can_handler can_handler, bool notif, uint32_t idtp, uint16_t len, t_can_data can_data);
// static uint16_t touch_delay = 500;
extern uint8_t Allow_Touch_flag;
void TouchPanel_MainFunction(void)
{
    uint8 index;
    uint8 x1, y1, x2, y2, Temp_x, Temp_y;
    uint8 touchEnd_flag = 0x00;

    uint8 voice_temp, voice_x, voice_y; // by kailong
    uint8 pad_trig_x, pad_trig_y;

    uint8 count = 0;


    touch_process();

    // add by kailong
    if (get_scroller_state(0) & TOUCH_ACTIVE)
    {
        voice_y = get_scroller_position(0x00);
        if (voice_y >= 10 && voice_y <= 73)
        {

            CurrentPosition = 1;
        }
        if (voice_y >= 103 && voice_y <= 160)
        {
            CurrentPosition = 2;
        }
        if (voice_y >= 190 && voice_y <= 250)
        {
            CurrentPosition = 3;
        }
    }
    else
    {
    }

    if ((TouchPanel_SurfaceStatus & TOUCH_ACTIVE) == 1)
    {
        struct Point Current_XY;

        struct Point Point_Up[4] = {
            {76, 0},
            {255, 41},
            {206, 87},
            {135, 87},
        };

        struct Point Point_Down[4] = {
            {135, 169},
            {206, 169},
            {255, 199},
            {106, 255},
        };

        struct Point Point_Left[4] = {
            {76, 21},
            {135, 87},
            {135, 169},
            {96, 255},
        };

        struct Point Point_Right[4] = {
            {206, 87},
            {255, 48},
            {255, 183},
            {206, 169},
        };

#if 1
        struct Point Point_Confirm[4] = {
            {140, 92},
            {206, 92},
            {206, 164},
            {140, 164},
        };
#endif
        // point_in_quadrilateral(Current_XY,Point_Up[0],Point_Up[1],Point_Up[2],Point_Up[3])

        GetSurface_Position(&pad_trig_x, &pad_trig_y);
        Current_XY.x = (float)pad_trig_x;
        Current_XY.y = (float)pad_trig_y;

        if (point_in_quadrilateral(Current_XY, Point_Up[0], Point_Up[1], Point_Up[2], Point_Up[3]))
        {
            // if(CurrentPosition == 0xff)
            //{
            CurrentPosition = 4;
            K_Vibra_Trig = 1;
            Surface_Button = 1;
            // }
        }
        else if (point_in_quadrilateral(Current_XY, Point_Down[0], Point_Down[1], Point_Down[2], Point_Down[3]))
        {
            // if(CurrentPosition == 0xff)
            //{
            CurrentPosition = 5;
            K_Vibra_Trig = 1;
            Surface_Button = 2;
            //}
        }
        else if (point_in_quadrilateral(Current_XY, Point_Left[0], Point_Left[1], Point_Left[2], Point_Left[3]))
        {
            // if(CurrentPosition == 0xff)
            //{
            CurrentPosition = 6;
            K_Vibra_Trig = 1;
            Surface_Button = 3;
            //}
        }
        else if (point_in_quadrilateral(Current_XY, Point_Right[0], Point_Right[1], Point_Right[2], Point_Right[3]))
        {
            // if(CurrentPosition == 0xff)
            //{
            CurrentPosition = 7;
            K_Vibra_Trig = 1;
            Surface_Button = 4;
            //}
        }

        else if (point_in_quadrilateral(Current_XY, Point_Confirm[0], Point_Confirm[1], Point_Confirm[2], Point_Confirm[3]))
        // else if(((pad_trig_x>= 150) && (pad_trig_x<= 196)) && ((pad_trig_y>= 102) && (pad_trig_y<= 158)))
        {
            // if(CurrentPosition == 0xff)
            //{
            CurrentPosition = 8;
            K_Vibra_Trig = 1;
            Surface_Button = 5;
            //}
        }
    }
    else
    {

        K_Vibra_Trig = 0;
        Surface_Button = 0;
    }

    // volatile uint8 g_Trig_Threshold=0;
    // volatile uint8 g_Release_Threshold=0;
    switch (CurrentPosition)
    {
    case 1:
        g_Trig_Threshold = Button0_GetVoice1_Trig_Threshold();
        g_Release_Threshold = Button0_GetVoice1_Release_Threshold();
        break;
    case 2:
        g_Trig_Threshold = Button5_GetMenu1_Trig_Threshold();
        g_Release_Threshold = Button5_GetMenu1_Release_Threshold();
        break;
    case 3:
        g_Trig_Threshold = Button3_GetVol1_Trig_Threshold();
        g_Release_Threshold = Button3_GetVol1_Release_Threshold();
        break;
    case 4:
        g_Trig_Threshold = Button6_GetBTN_UP_Trig_Threshold();
        g_Release_Threshold = Button6_GetBTN_UP_Release_Threshold();
        break;
    case 5:
        g_Trig_Threshold = Button7_GetBTN_DOWN_Trig_Threshold();
        g_Release_Threshold = Button7_GetBTN_DOWN_Release_Threshold();
        break;
    case 6:
        g_Trig_Threshold = Button8_GetBTN_LEFT_Trig_Threshold();
        g_Release_Threshold = Button8_GetBTN_LEFT_Release_Threshold();
        break;
    case 7:
        g_Trig_Threshold = Button9_GetBTN_RIGHT_Trig_Threshold();
        g_Release_Threshold = Button9_GetBTN_RIGHT_Release_Threshold();
        break;
    case 8:
        g_Trig_Threshold = Button10_GetBTN_CONFIRM_Trig_Threshold();
        g_Release_Threshold = Button10_GetBTN_CONFIRM_Release_Threshold();
        break;
    default:
        break;
    }

    TouchPanel_PressLevel = TouchPanel_PressCheck();

    // add by kailong
    // static uint8 Voice_force_Reach2_flag = 0;
    // static uint8 Vol_force_Reach2_flag = 0;
    // static uint8 Menu_force_Reach2_flag = 0;
    k_voice_touch_Sts = BUTTON_RELEASE;
    k_vol_touch_Sts = BUTTON_RELEASE;
    k_menu_touch_Sts = BUTTON_RELEASE;
    voice_temp = get_scroller_state(0);
    if (voice_temp & TOUCH_ACTIVE)
    {
        voice_y = get_scroller_position(0x00);
    }
    else
    {
        voice_y = 0x00;
    }
    GetSurface_Position(&voice_x, NULL_PTR);
    if (voice_y >= 10 && voice_y <= 73)
    {

        switch (TouchPanel_PressLevel)
        {
        case PANEL_PRESS_LEVEL0:
            // Voice_force_Reach2_flag = 0;
            k_voice_touch_Sts = BUTTON_TOUCH;
            break;
        case PANEL_PRESS_LEVEL1:
            // if(Voice_force_Reach2_flag == 1)
            // {
            // k_voice_touch_Sts = BUTTON_TOUCH_AND_PRESS;
            //}
            // else
            //{
            k_voice_touch_Sts = BUTTON_TOUCH;
            //}
            break;
        case PANEL_PRESS_LEVEL2:
            // Voice_force_Reach2_flag = 1;
            k_voice_touch_Sts = BUTTON_TOUCH_AND_PRESS;
            break;
        default:
            break;
        }
    }
    else if (voice_y >= 103 && voice_y <= 160)
    {

        switch (TouchPanel_PressLevel)
        {
        case PANEL_PRESS_LEVEL0:
            // Vol_force_Reach2_flag = 0;
            k_vol_touch_Sts = BUTTON_TOUCH;
            break;
        case PANEL_PRESS_LEVEL1:
            // if(Vol_force_Reach2_flag == 1)
            //{
            //     k_vol_touch_Sts = BUTTON_TOUCH_AND_PRESS;
            // }
            // else
            //{
            k_vol_touch_Sts = BUTTON_TOUCH;
            //}
            break;
        case PANEL_PRESS_LEVEL2:
            // Vol_force_Reach2_flag = 1;
            k_vol_touch_Sts = BUTTON_TOUCH_AND_PRESS;
            break;
        default:
            break;
        }
    }
    else if (voice_y >= 190 && voice_y <= 250)
    {

        switch (TouchPanel_PressLevel)
        {
        case PANEL_PRESS_LEVEL0:
            // Menu_force_Reach2_flag = 0;
            k_menu_touch_Sts = BUTTON_TOUCH;
            break;
        case PANEL_PRESS_LEVEL1:
            // if(Menu_force_Reach2_flag == 1)
            // {
            // k_menu_touch_Sts = BUTTON_TOUCH_AND_PRESS;
            //}
            // else
            // {
            k_menu_touch_Sts = BUTTON_TOUCH;
            // }
            break;
        case PANEL_PRESS_LEVEL2:
            // Menu_force_Reach2_flag = 1;
            k_menu_touch_Sts = BUTTON_TOUCH_AND_PRESS;
            break;
        default:
            break;
        }
    }

    // add by kailong

    if (measurement_done_touch == 1)
    {
        // process touch data
        for (index = 0; index < DEF_NUM_CHANNELS; index++)
        {
            sensor_signal[index] = get_sensor_node_signal(index);
            sensor_reference[index] = get_sensor_node_reference(index);
        }
        TouchScroller.x_position = 0x00;
        TouchPanel_ScrollerStatus = get_scroller_state(0);
        if (TouchPanel_ScrollerStatus & TOUCH_ACTIVE)
        {
            TouchScroller.y_position = get_scroller_position(0x00);
        }
        else
        {
            TouchScroller.y_position = 0x00;
        }
        TouchPanel_SurfaceStatus = get_surface_status();
        if (TouchPanel_SurfaceStatus & TOUCH_ACTIVE)
        {
            Temp_x = get_surface_position(HOR_POS);
            Temp_y = get_surface_position(VER_POS);
            TouchPanel_XY_Convert(Temp_x, Temp_y);
            TouchSurface.x_position = Surface_Convert.convert_x;
            TouchSurface.y_position = Surface_Convert.convert_y;

            if (TouchSurface.end_x_pos == 0x00 && TouchSurface.end_y_pos == 0x00)
            {
                TouchSurface.end_x_pos = TouchSurface.x_position;
                TouchSurface.end_y_pos = TouchSurface.y_position;
            }

            if (TouchSurface.Event == TOUCH_PANEL_NONE_EVENT)
            {
                x1 = TouchSurface.end_x_pos;
                y1 = TouchSurface.end_y_pos;
                x2 = TouchSurface.x_position;
                y2 = TouchSurface.y_position;
            }
            else
            {
                x1 = TouchSurface.origin_x_pos;
                y1 = TouchSurface.origin_y_pos;
                x2 = TouchSurface.x_position;
                y2 = TouchSurface.y_position;
            }

            TouchSurface.CheckCycle++;
            TouchSurface.TouchCycle++;
            if ((abs(x2 - x1) < TP_ERR_RANGE) && (abs(y2 - y1) < TP_ERR_RANGE))
            {
                //  sampling error  <=   +- TP_ERR_RANGE
                if (TouchSurface.CheckCycle >= TP_DEBOUNCE_CYCLE)
                {
                    if (TOUCH_PANEL_NONE_EVENT == TouchSurface.Event)
                    {
                        TouchSurface.origin_x_pos = (x1 + x2) / 2;
                        TouchSurface.origin_y_pos = (y1 + y2) / 2;
                        TouchSurface.Event = TOUCH_PANEL_PRESS_EVENT;
                    }
                    TouchSurface.CheckCycle = TP_DEBOUNCE_CYCLE;
                }
                TouchSurface.end_x_pos = x2;
                TouchSurface.end_y_pos = y2;
            }
            else
            {
                if (TOUCH_PANEL_PRESS_EVENT == TouchSurface.Event || TOUCH_PANEL_MOVE_EVENT == TouchSurface.Event)
                {
                    TouchSurface.Event = TOUCH_PANEL_MOVE_EVENT;
                }
                else
                {
                    TouchSurface.Event = TOUCH_PANEL_NONE_EVENT;
                    TouchSurface.origin_x_pos = 0x00;
                    TouchSurface.origin_y_pos = 0x00;
                }
                TouchSurface.end_x_pos = x2;
                TouchSurface.end_y_pos = y2;
            }
        }
        else
        {
            x1 = TouchSurface.origin_x_pos;
            y1 = TouchSurface.origin_y_pos;
            x2 = TouchSurface.end_x_pos;
            y2 = TouchSurface.end_y_pos;
            if (TOUCH_PANEL_MOVE_EVENT == TouchSurface.Event)
            {
                if ((abs(x2 - x1) < TP_ERR_RANGE) && (abs(y2 - y1) < TP_ERR_RANGE))
                {
                    TouchSurface.Event = TOUCH_PANEL_RES_EVENT;
                }
                else
                {
                    TouchSurface.Event = TOUCH_PANEL_MOVE_EVENT;
                    TouchSlideEvent.Direction = GetSlide_Direction(x1, y1, x2, y2);
                    if (TouchSlideEvent.Direction != TP_SlideDirection_NONE)
                    {
                        TouchSlideEvent.keep_cycle = SLIDE_CAN_EVENT_KEEP_CYCLE;
                        TouchSlideEvent.Event = TRUE;
                    }
                }
            }
            else if (TOUCH_PANEL_PRESS_EVENT == TouchSurface.Event)
            {
                // do nothing
            }
            else
            {
                TouchSurface.Event = TOUCH_PANEL_NONE_EVENT;
            }

            TouchSurface.end_x_pos = 0x00;
            TouchSurface.end_y_pos = 0x00;
            TouchSurface.CheckCycle = 0x00;
            TouchSurface.origin_x_pos = 0x00;
            TouchSurface.origin_y_pos = 0x00;
            TouchSurface.x_position = 0;
            TouchSurface.y_position = 0;
            TouchSurface.TouchCycle = 0;
            Surface_Convert.convert_x = 0x00;
            Surface_Convert.convert_y = 0x00;
            touchEnd_flag = 0x01;
        }

        if (TouchSurface.TouchCycle >= TP_RUNNING_ERROR_CYCLE)
        {
            TouchSurface.TouchCycle = TP_RUNNING_ERROR_CYCLE;
            TouchSurface.Event = TOUCH_PANEL_BERR_EVENT;
        }

        switch (CurrentPosition)
        {
        case 1:
        {
            Buttons_Signal[BTN_VOICE1] = TouchButton_BntCheck(BTN_VOICE1);
            if (Buttons_Signal[BTN_VOICE1] >= BUTTON_TOUCH)
            {
                break;
            }
            else
            {
                for (index = 0; index < BTN_MAX_CH_NUM; index++)
                {
                    if (index < (BTN_MAX_CH_NUM - 5U))
                    {
                        Buttons_Signal[index] = TouchButton_BntCheck(index);
                    }
                    else
                    {
                        Buttons_Signal[index] = TouchSurface_BntCheck(index, 1);
                    }

                    if (Buttons_Signal[index] < BUTTON_TOUCH)
                    {
                        count++;
                    }
                }
            }
            break;
        }
        case 2:
        {
            Buttons_Signal[BTN_VOL1] = TouchButton_BntCheck(BTN_VOL1);
            if (Buttons_Signal[BTN_VOL1] >= BUTTON_TOUCH)
            {
                break;
            }
            else
            {
                for (index = 0; index < BTN_MAX_CH_NUM; index++)
                {
                    if (index < (BTN_MAX_CH_NUM - 5U))
                    {
                        Buttons_Signal[index] = TouchButton_BntCheck(index);
                    }
                    else
                    {
                        Buttons_Signal[index] = TouchSurface_BntCheck(index, 1);
                    }

                    if (Buttons_Signal[index] < BUTTON_TOUCH)
                    {
                        count++;
                    }
                }
            }
            break;
        }
        case 3:
        {
            Buttons_Signal[BTN_MENU1] = TouchButton_BntCheck(BTN_MENU1);
            if (Buttons_Signal[BTN_MENU1] >= BUTTON_TOUCH)
            {
                break;
            }
            else
            {
                for (index = 0; index < BTN_MAX_CH_NUM; index++)
                {
                    if (index < (BTN_MAX_CH_NUM - 5U))
                    {
                        Buttons_Signal[index] = TouchButton_BntCheck(index);
                    }
                    else
                    {
                        Buttons_Signal[index] = TouchSurface_BntCheck(index, 1);
                    }

                    if (Buttons_Signal[index] < BUTTON_TOUCH)
                    {
                        count++;
                    }
                }
            }
            break;
        }
        case 4:
        {
            Buttons_Signal[BTN_UP] = TouchSurface_BntCheck(BTN_UP, 1);
            if (Buttons_Signal[BTN_UP] >= BUTTON_TOUCH)
            {
                break;
            }
            else
            {
                for (index = 0; index < BTN_MAX_CH_NUM; index++)
                {
                    if (index < (BTN_MAX_CH_NUM - 5U))
                    {
                        Buttons_Signal[index] = TouchButton_BntCheck(index);
                    }
                    else
                    {
                        Buttons_Signal[index] = TouchSurface_BntCheck(index, 1);
                    }

                    if (Buttons_Signal[index] < BUTTON_TOUCH)
                    {
                        count++;
                    }
                }
            }
            break;
        }
        case 5:
        {
            Buttons_Signal[BTN_DOWN] = TouchSurface_BntCheck(BTN_DOWN, 1);
            if (Buttons_Signal[BTN_DOWN] >= BUTTON_TOUCH)
            {
                break;
            }
            else
            {
                for (index = 0; index < BTN_MAX_CH_NUM; index++)
                {
                    if (index < (BTN_MAX_CH_NUM - 5U))
                    {
                        Buttons_Signal[index] = TouchButton_BntCheck(index);
                    }
                    else
                    {
                        Buttons_Signal[index] = TouchSurface_BntCheck(index, 1);
                    }

                    if (Buttons_Signal[index] < BUTTON_TOUCH)
                    {
                        count++;
                    }
                }
            }
            break;
        }
        case 6:
        {
            Buttons_Signal[BTN_LEFT] = TouchSurface_BntCheck(BTN_LEFT, 1);
            if (Buttons_Signal[BTN_LEFT] >= BUTTON_TOUCH)
            {
                break;
            }
            else
            {
                for (index = 0; index < BTN_MAX_CH_NUM; index++)
                {
                    if (index < (BTN_MAX_CH_NUM - 5U))
                    {
                        Buttons_Signal[index] = TouchButton_BntCheck(index);
                    }
                    else
                    {
                        Buttons_Signal[index] = TouchSurface_BntCheck(index, 1);
                    }

                    if (Buttons_Signal[index] < BUTTON_TOUCH)
                    {
                        count++;
                    }
                }
            }
            break;
        }
        case 7:
        {
            Buttons_Signal[BTN_RIGHT] = TouchSurface_BntCheck(BTN_RIGHT, 1);
            if (Buttons_Signal[BTN_RIGHT] >= BUTTON_TOUCH)
            {
                break;
            }
            else
            {
                for (index = 0; index < BTN_MAX_CH_NUM; index++)
                {
                    if (index < (BTN_MAX_CH_NUM - 5U))
                    {
                        Buttons_Signal[index] = TouchButton_BntCheck(index);
                    }
                    else
                    {
                        Buttons_Signal[index] = TouchSurface_BntCheck(index, 1);
                    }

                    if (Buttons_Signal[index] < BUTTON_TOUCH)
                    {
                        count++;
                    }
                }
            }
            break;
        }
        case 8:
        {
            Buttons_Signal[BTN_CONFIRM] = TouchSurface_BntCheck(BTN_CONFIRM, 1);
            if (Buttons_Signal[BTN_CONFIRM] >= BUTTON_TOUCH)
            {
                break;
            }
            else
            {
                for (index = 0; index < BTN_MAX_CH_NUM; index++)
                {
                    if (index < (BTN_MAX_CH_NUM - 5U))
                    {
                        Buttons_Signal[index] = TouchButton_BntCheck(index);
                    }
                    else
                    {
                        Buttons_Signal[index] = TouchSurface_BntCheck(index, 1);
                    }

                    if (Buttons_Signal[index] < BUTTON_TOUCH)
                    {
                        count++;
                    }
                }
            }
            break;
        }
        default:
        {
            for (index = 0; index < BTN_MAX_CH_NUM; index++)
            {
                if (index < (BTN_MAX_CH_NUM - 5U))
                {
                    Buttons_Signal[index] = TouchButton_BntCheck(index);
                }
                else
                {
                    Buttons_Signal[index] = TouchSurface_BntCheck(index, 1);
                }

                if (Buttons_Signal[index] < BUTTON_TOUCH)
                {
                    count++;
                }
            }
            break;
        }
        }

        if (count == 10)
        {
            CurrentPosition = 0xff;
        }

        if (touchEnd_flag != 0x00)
        {
            TouchSurface.Event = TOUCH_PANEL_NONE_EVENT;
        }

        measurement_done_touch = 0;
    }

    if (TouchSlideEvent.Event == TRUE)
    {
        if (TouchSlideEvent.keep_cycle != 0x00)
        {
            TouchSlideEvent.keep_cycle--;
        }
        if (TouchSlideEvent.keep_cycle == 0x00)
        {
            TouchSlideEvent.Direction = TP_SlideDirection_NONE;
            TouchSlideEvent.UpDown_SlideLevel = TP_SlideLevel_NONE;
            TouchSlideEvent.Event = FALSE;
        }
    }
    if (TouchSurface.Event == TOUCH_PANEL_BERR_EVENT)
    {
        TouchSurface.Event = TOUCH_PANEL_NONE_EVENT;
    }

    if (Vibra_force_level == 2 && last_Vibra_force_level != 2 &&
        Allow_Touch_flag == 1)
    {
        Over3N_vibra_req = 1;
        Below1P5N_vibra_req = 0;
    }
    else if (Vibra_force_level == 0 && last_Vibra_force_level != 0)
    {
        Below1P5N_vibra_req = 1;
        Over3N_vibra_req = 0;
    }
    last_Vibra_force_level = Vibra_force_level;
    if (Vibra_force_level == 2)
    {
        Reach2_flag = 1;
    }

    if ((K_Vibra_Trig == 1 || k_voice_touch_Sts != 0 || k_menu_touch_Sts != 0 || k_vol_touch_Sts != 0) && Over3N_vibra_req == 1 && Allow_Touch_flag == 1)
    {
        Touch_Flag = TOUCH_KET_ON;
        speeker_Tig_Once(49, UDS_Vibration_Gain);
        Over3N_vibra_req = 0;
    }
    else if (Below1P5N_vibra_req == 1 && Reach2_flag == 1) //
    {
        if (TOUCH_KET_ON == Touch_Flag)
        {
            speeker_Tig_Once(49, UDS_Vibration_Gain);
            Touch_Flag = TOUCH_KET_OFF;
        }
        Below1P5N_vibra_req = 0;
        Reach2_flag = 0;
    }

    if (CurrentPosition == 0xff)
    {
        Allow_Touch_flag = 1;
    }
}
