#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_SWTL_App_ert_rtw/P417_SWTL_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"

static TouchButtons_type key_status[6] = {0};

static Buttons_SignalType Buttons_Signal[BTN_MAX_CH_NUM] = {BUTTON_RELEASE};

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 uint16 TouchPanel_PressRawData = 0;

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

uint8 K_Vibra_Trig_adas = 0;
uint8 K_Vibra_Trig_cancel = 0;
uint8 K_Vibra_Trig_resume = 0;

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

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 RESUME1_DEFAULT 339
#define ADAS1_DEFAULT 412
#define CANCEL1_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

struct Point
{
    float x;
    float y;
};


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_Right[4] = {
    {76, 21},
    {135, 87},
    {135, 169},
    {96, 255},
};

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

struct Point Point_Confirm[4] = {
    {140, 92},
    {206, 92},
    {206, 164},
    {140, 164},
};

// force Trig threshold
static uint16 Button0_GetRESUME1_Trig_Threshold(void)
{
    uint16 temp = Calib_GetData(0U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = RESUME1_DEFAULT;
    }
    else
    {
        rtn = Calib_GetData(0U);
    }

    return rtn;
    // return 800;
}


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

    return rtn;
}


static uint16 Button5_GetCANCEL1_Trig_Threshold(void)
{
    uint16 temp = Calib_GetData(2U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = CANCEL1_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_GetRESUME1_Release_Threshold(void)
{
    uint16 temp = Calib_GetData(0U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = RESUME1_DEFAULT / 2;
    }
    else
    {
        rtn = Calib_GetData(0U) / 2;
    }

    return rtn;
}


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

    return rtn;
}


static uint16 Button5_GetCANCEL1_Release_Threshold(void)
{
    uint16 temp = Calib_GetData(2U);
    uint16 rtn = 0;
    if (temp == 0)
    {
        rtn = CANCEL1_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(&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 = 100;
    }
    TC2_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_PressRawData = 0;
    for (index = 0; index < DEF_NUM_CHANNELS; index++)
    {
        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;
    }
}



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)  && (CurrentPosition>0 && CurrentPosition<9))
    {
        ret = TRUE;
    }

    return ret;
}

Buttons_SignalType TouchSurface_XY_StsRead(void)
{
    Buttons_SignalType XY_Sts = BUTTON_RELEASE;

    if ((TouchPanel_SurfaceStatus & TOUCH_ACTIVE) && (CurrentPosition>0 && CurrentPosition<9))
    {

        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;
}

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

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

    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;
    Backup_Force_Value = Touch_Sensor_delta03 + Touch_Sensor_delta04 + Touch_Sensor_delta05 + Touch_Sensor_delta06 + Touch_Sensor_delta07 +
                         +Touch_Sensor_delta08 + Touch_Sensor_delta09 + Touch_Sensor_delta10 + Touch_Sensor_delta11 + Touch_Sensor_delta12;


    if (CurrentPosition != 0xff)
    {
        tempForcePress = g_Trig_Threshold;
        tempForceRelease = g_Release_Threshold;
    }

    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() || SPEAKER_START == 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))
        {   
            P_force_Reach2_flag = 0;
            ret_Press = PANEL_PRESS_LEVEL0;

            Vibra_force_level = PANEL_PRESS_LEVEL0;
        }
        else if (FilterValue >= ((float32)tempForceRelease / (float32)0xF2) &&
                 FilterValue < ((float32)tempForcePress / (float32)0xF2))
        { 
            if (P_force_Reach2_flag == 1)
            {
                Vibra_force_level = PANEL_PRESS_LEVEL2;
                ret_Press = PANEL_PRESS_LEVEL2;
            }
            else
            {
                Vibra_force_level = PANEL_PRESS_LEVEL1;
                ret_Press = PANEL_PRESS_LEVEL1;
            }
        }

        else
        {   
            P_force_Reach2_flag = 1;
            
            ret_Press = PANEL_PRESS_LEVEL2;

            Vibra_force_level = PANEL_PRESS_LEVEL2;
        }
    }
    else // backup When force sensor error happened
    {
        if (Backup_Force_Value <= 100)
        {   
            P_force_Reach2_flag = 0;
            ret_Press = PANEL_PRESS_LEVEL0;
            Vibra_force_level = PANEL_PRESS_LEVEL0;
        }
        else if (Backup_Force_Value >= 100 &&
                 Backup_Force_Value < 200)
        { 
            if (P_force_Reach2_flag == 1)
            {

                Vibra_force_level = PANEL_PRESS_LEVEL2;
                ret_Press = PANEL_PRESS_LEVEL2;

            }
            else
            {
                Vibra_force_level = PANEL_PRESS_LEVEL1;
                ret_Press = PANEL_PRESS_LEVEL1;

            }
        }

        else
        {   
            P_force_Reach2_flag = 1;

            ret_Press = PANEL_PRESS_LEVEL2;

            Vibra_force_level = PANEL_PRESS_LEVEL2;
        }
    }

    LastRawData = RawData;
    Last_PressLevel = ret_Press;

    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:
                BSignal = BUTTON_TOUCH;
                break;
            case PANEL_PRESS_LEVEL1:
                BSignal = BUTTON_TOUCH;
                break;
            case PANEL_PRESS_LEVEL2:
                BSignal = BUTTON_TOUCH_AND_PRESS;
                break;
            default:
                break;
            }
        }
    }

    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_RESUME1 || ChId == BTN_ADAS1 || ChId == BTN_CANCEL1)
    {
        key_status[ChId].status = get_sensor_state(ChId);
        key_status[ChId+3].status = get_sensor_state(ChId+13);
        if (QTM_KEY_STATE_DETECT == (key_status[ChId].status) && QTM_KEY_STATE_DETECT == (key_status[ChId+3].status))
        { 
            if (Force_Sens_Err == 0)
            {
                switch (TouchPanel_PressLevel)
                {
                case PANEL_PRESS_LEVEL0:
                    BSignal = BUTTON_TOUCH;
                case PANEL_PRESS_LEVEL1:
                    BSignal = BUTTON_TOUCH;
                    break;
                case PANEL_PRESS_LEVEL2:
                    BSignal = BUTTON_TOUCH_AND_PRESS;
                    break;
                default:
                    break;
                }
            }
            else // backup When force sensor error happened
            {
                BSignal = BUTTON_TOUCH;
            }

        }
        else
        {
            // Touch No detect
            key_status[ChId].RunCycle = 0x00;
        }
    }
    return BSignal;
}




// ~{EP6OR;8v5cJG7qTZH}=GPNDZ2?~}
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);
}

// ~{EP6OR;8v5cJG7qTZKD1_PNDZ2?~}
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 = 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 = origin_x;
    /*******************************************/

    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);
}

void touch_Position_pro(void)
{
    uint8 pad_trig_x, pad_trig_y; 
    if (QTM_KEY_STATE_DETECT == get_sensor_state(BTN_ADAS1) && QTM_KEY_STATE_DETECT == get_sensor_state(BTN_ADAS1 + 13))
    {
        CurrentPosition = 2;
        K_Vibra_Trig_adas = 1;
    }
    else
    {
        K_Vibra_Trig_adas = 0;
    }

    if (QTM_KEY_STATE_DETECT == get_sensor_state(BTN_RESUME1) && QTM_KEY_STATE_DETECT == get_sensor_state(BTN_RESUME1 + 13))
    {

        CurrentPosition = 1;
        K_Vibra_Trig_resume = 1;
    }
    else
    {
        K_Vibra_Trig_resume = 0;
    }

    if (QTM_KEY_STATE_DETECT == get_sensor_state(BTN_CANCEL1) && QTM_KEY_STATE_DETECT == get_sensor_state(BTN_CANCEL1 + 13))
    {
        CurrentPosition = 3;
        K_Vibra_Trig_cancel = 1;
    }
    else
    {
        K_Vibra_Trig_cancel = 0;
    }

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

        
        // 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 != 1 && CurrentPosition != 2 && CurrentPosition != 3)
            {
                CurrentPosition = 4;
                K_Vibra_Trig_pad = 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 != 1 && CurrentPosition != 2 && CurrentPosition != 3)
            {
                CurrentPosition = 5;
                K_Vibra_Trig_pad = 1;
                Surface_Button = 2;
            }
        }

        else if (point_in_quadrilateral(Current_XY, Point_Right[0], Point_Right[1], Point_Right[2], Point_Right[3]))
        {
            if (CurrentPosition != 1 && CurrentPosition != 2 && CurrentPosition != 3)
            {
                CurrentPosition = 7;
                K_Vibra_Trig_pad = 1;
                Surface_Button = 4;
            }
        }

        else if (point_in_quadrilateral(Current_XY, Point_Left[0], Point_Left[1], Point_Left[2], Point_Left[3]))
        {
            if (CurrentPosition != 1 && CurrentPosition != 2 && CurrentPosition != 3)
            {
                CurrentPosition = 6;
                K_Vibra_Trig_pad = 1;
                Surface_Button = 3;
            }
        }

        else if (point_in_quadrilateral(Current_XY, Point_Confirm[0], Point_Confirm[1], Point_Confirm[2], Point_Confirm[3]))
        {
            if (CurrentPosition != 1 && CurrentPosition != 2 && CurrentPosition != 3)
            {
                CurrentPosition = 8;
                K_Vibra_Trig_pad = 1;
                Surface_Button = 5;
            }
        }
    }
    else
    {

        K_Vibra_Trig_pad = 0;
        Surface_Button = 0;
    }
    
}

void Threshold_Select_Process(void)
{
    switch (CurrentPosition)
    {
    case 1:
        g_Trig_Threshold = Button0_GetRESUME1_Trig_Threshold();
        g_Release_Threshold = Button0_GetRESUME1_Release_Threshold();
        break;
    case 2:
        g_Trig_Threshold = Button3_GetADAS1_Trig_Threshold();
        g_Release_Threshold = Button3_GetADAS1_Release_Threshold();
        break;
    case 3:
        g_Trig_Threshold = Button5_GetCANCEL1_Trig_Threshold();
        g_Release_Threshold = Button5_GetCANCEL1_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;
    }
}


extern uint8_t Allow_Touch_flag;
// static uint16_t touch_delay = 500;
void TouchPanel_MainFunction(void)
{
    uint8 index;
    uint8 Temp_x, Temp_y;
    uint8 count = 0;

    touch_process(); 
    TouchPanel_PressLevel = TouchPanel_PressCheck();
    if (measurement_done_touch == 1)
    {

        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);
        }
        else
        {

            Surface_Convert.convert_x = 0x00;
            Surface_Convert.convert_y = 0x00;
        }
        touch_Position_pro();
    
        Threshold_Select_Process();

        

        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++;
            }
        }

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

        

        measurement_done_touch = 0;
    }

    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 ((K_Vibra_Trig_pad == 1 || K_Vibra_Trig_adas == 1 || K_Vibra_Trig_cancel == 1 || K_Vibra_Trig_resume == 1) && Vibra_force_level == 2 && Allow_Touch_flag == 1)
        {
            Reach2_flag = 1;
        }

        if ((K_Vibra_Trig_pad == 1 || K_Vibra_Trig_adas == 1 || K_Vibra_Trig_cancel == 1 || K_Vibra_Trig_resume == 1) && 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)
            {
                Touch_Flag = TOUCH_KET_OFF;
                speeker_Tig_Once(49, UDS_Vibration_Gain);
            }
            Below1P5N_vibra_req = 0;
            Reach2_flag = 0;
        }

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


    
}
