// For SWTR

#include "RTE.h"

#include <stddef.h>  // Defines NULL
#include <stdbool.h> // Defines true
#include <stdlib.h>  // Defines EXIT_FAILURE
#include "../OsekCom/OsekCom.h"
#include "../Speaker/Speaker.h"
#include "TouchPanel/TouchPanel.h"
// For SWTL

#include "forceSnsr.h"
#include "forceSnsr_Cfg.h"
#include "forcedetect.h"
#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 "TouchPanel/TouchPanel.h"
#include "forceSnsr/forcedetect.h"
#include "TLE9263/TLE926x_Main.h"
#include "DiagnosticR/UDS/UDS_Services_Common.h"
#include "plib_systick.h"
#include "FunctionState.h"
#include "SysDiagDetect.h"

// #include "Global.h"
#ifndef uint8_t
#define uint8_t unsigned char
#endif

#ifndef uint16_t
#define uint16_t unsigned short
#endif

#ifndef uint32_t
#define uint32_t unsigned int
#endif
extern uint8 CurrentPosition;

uint16 Fr08_send_flag;

uint8_t Two_Finger_Y_count = 0;
uint8_t Two_Finger_X_count = 0;
uint8_t X_count = 0;
uint8_t Y_count = 0;

uint8_t touch_reset2times_flag = 0;

uint16_t Touch_Sensor_Sts00;
uint16_t Touch_Sensor_Sts01;
uint16_t Touch_Sensor_Sts02;
uint16_t Touch_Sensor_Sts03;
uint16_t Touch_Sensor_Sts04;
uint16_t Touch_Sensor_Sts05;
uint16_t Touch_Sensor_Sts06;
uint16_t Touch_Sensor_Sts07;
uint16_t Touch_Sensor_Sts08;
uint16_t Touch_Sensor_Sts09;
uint16_t Touch_Sensor_Sts10;
uint16_t Touch_Sensor_Sts11;
uint16_t Touch_Sensor_Sts12;
uint16_t Touch_Sensor_Sts13;
uint16_t Touch_Sensor_Sts14;
uint8_t Allow_Touch_flag = 0,Allow_Touch_counter=0;

uint16_t Touch_Sensor_Ref00;
uint16_t Touch_Sensor_Ref01;
uint16_t Touch_Sensor_Ref02;
uint16_t Touch_Sensor_Ref03;
uint16_t Touch_Sensor_Ref04;
uint16_t Touch_Sensor_Ref05;
uint16_t Touch_Sensor_Ref06;
uint16_t Touch_Sensor_Ref07;
uint16_t Touch_Sensor_Ref08;
uint16_t Touch_Sensor_Ref09;
uint16_t Touch_Sensor_Ref10;
uint16_t Touch_Sensor_Ref11;
uint16_t Touch_Sensor_Ref12;
uint16_t Touch_Sensor_Ref13;
uint16_t Touch_Sensor_Ref14;

uint16_t Touch_Sensor_CC00;
uint16_t Touch_Sensor_CC01;
uint16_t Touch_Sensor_CC02;
uint16_t Touch_Sensor_CC03;
uint16_t Touch_Sensor_CC04;
uint16_t Touch_Sensor_CC05;
uint16_t Touch_Sensor_CC06;
uint16_t Touch_Sensor_CC07;
uint16_t Touch_Sensor_CC08;
uint16_t Touch_Sensor_CC09;
uint16_t Touch_Sensor_CC10;
uint16_t Touch_Sensor_CC11;
uint16_t Touch_Sensor_CC12;
uint16_t Touch_Sensor_CC13;
uint16_t Touch_Sensor_CC14;

uint16_t Touch_Sensor_delta00;
uint16_t Touch_Sensor_delta01;
uint16_t Touch_Sensor_delta02;
uint16_t Touch_Sensor_delta03;
uint16_t Touch_Sensor_delta04;
uint16_t Touch_Sensor_delta05;
uint16_t Touch_Sensor_delta06;
uint16_t Touch_Sensor_delta07;
uint16_t Touch_Sensor_delta08;
uint16_t Touch_Sensor_delta09;
uint16_t Touch_Sensor_delta10;
uint16_t Touch_Sensor_delta11;
uint16_t Touch_Sensor_delta12;
uint16_t Touch_Sensor_delta13;
uint16_t Touch_Sensor_delta14;

// volatile uint8_t TouchBoard_RC;

// volatile uint8_t TouchBoard_Sts;
// volatile uint8_t TouchBoard_X;
// volatile uint8_t TouchBoard_Y;
uint8_t RTE_Get_TouchBoard_CRC(void)
{
    return 0;
}

// Input Sensor
// uint16_t RTE_Get_Power_AD_SNS(void);

// uint8_t RTE_Get_Pressure_Sensor_xxxx(void);//IIC DATA?

// Not active,Touch,Touch and Press,invalid from sensor

uint8_t RTE_Get_SldVolCtrlSts(void) // 0 slip idle   1 decrease ,2 increase , 3 decrease double , 4 increase double ,5 decrease third, 6 increase third
{
    TP_SlideLevel_Enum UD_SlideLevel = TP_SlideLevel_NONE;
    return (uint8_t)UD_SlideLevel;
}

uint8_t RTE_Get_TouchBoard_Up_Sts(void)
{
    /* y:a7, x: e6 */
    /* y:8d, x: eb */
    /* y:75, x: ed */
    /* y:6a, x: eb */
    /* y:75, x: eb */
    Buttons_SignalType Signal = BUTTON_RELEASE;
    if (CurrentPosition == 4)
    {
        Signal = TouchPanel_BtnSignalRead(BTN_UP);
    }
    if (Allow_Touch_flag == 0)
    {
        Signal = 0;
    }

    return Signal;
}

uint8_t RTE_Get_TouchBoard_Down_Sts(void)
{
    /* x:26, y: 96 */
    /* x:20, y: 80 */
    /* x:20, y: 80 */
    Buttons_SignalType Signal = BUTTON_RELEASE;
    if (CurrentPosition == 5)
    {
        Signal = TouchPanel_BtnSignalRead(BTN_DOWN);
    }
    if (Allow_Touch_flag == 0)
    {
        Signal = 0;
    }

    return Signal;
}

uint8_t RTE_Get_TouchBoard_Left_Sts(void)
{
    /* x:95, y: ec */
    /* x:97, y: e9 */
    Buttons_SignalType Signal = BUTTON_RELEASE;
    if (CurrentPosition == 6)
    {
        Signal = TouchPanel_BtnSignalRead(BTN_LEFT);
    }
    if (Allow_Touch_flag == 0)
    {
        Signal = 0;
    }

    return Signal;
}

uint8_t RTE_Get_TouchBoard_Right_Sts(void)
{
    /* x:98, y: 22 */
    /* x:9d, y: 27 */
    /* x:94, y: 25 */
    Buttons_SignalType Signal = BUTTON_RELEASE;
    if (CurrentPosition == 7)
    {
        Signal = TouchPanel_BtnSignalRead(BTN_RIGHT);
    }
    if (Allow_Touch_flag == 0)
    {
        Signal = 0;
    }

    return Signal;
}

uint8_t RTE_Get_TouchBoard_Center_Sts(void)
{
    Buttons_SignalType Signal = BUTTON_RELEASE;
    if (CurrentPosition == 8)
    {
        Signal = TouchPanel_BtnSignalRead(BTN_CONFIRM);
    }
    if (Allow_Touch_flag == 0)
    {
        Signal = 0;
    }

    return Signal;
}

uint8_t g_VOICE_Touch_And_Below_1N = 0;
uint8_t RTE_Get_TouchBoard_VOICE1_Sts(void) // TJP1
{

    Buttons_SignalType Signal = BUTTON_RELEASE;
    static uint8_t Voice_Zero_Count = 0;
    if (CurrentPosition == 1)
    {
        Signal = k_voice_touch_Sts;
    }
    if (Signal != 0) // PANEL_PRESS_LEVEL0 == Press_Level &&
    {
        g_VOICE_Touch_And_Below_1N = 0x01;
        Voice_Zero_Count = 0;
    }
    else
    {
        Voice_Zero_Count++;
        if (Voice_Zero_Count > 6)
        {
            g_VOICE_Touch_And_Below_1N = 0x00;
            Voice_Zero_Count = 7;
        }
    }
    if (Allow_Touch_flag == 0)
    {
        Signal = 0;
    }

    return Signal;
}
uint8_t RTE_Get_TouchBoard_VOICE2_Sts(void) // TJP2
{
    Buttons_SignalType Signal = BUTTON_RELEASE;
    // Signal = TouchPanel_BtnSignalRead(BTN_VOICE2);

    return Signal;
}

uint8_t g_MENU_Touch_And_Below_1N = 0;
uint8_t RTE_Get_TouchBoard_MENU1_Sts(void) // Resume1
{
    Buttons_SignalType Signal = BUTTON_RELEASE;
    static uint8_t Menu_Zero_Count = 0;
    if (CurrentPosition == 3)
    {
        Signal = k_menu_touch_Sts; // TouchPanel_BtnSignalRead(BTN_MENU1);
    }
    if (Signal != 0) // PANEL_PRESS_LEVEL0 == Press_Level &&
    {
        g_MENU_Touch_And_Below_1N = 0x01;
        Menu_Zero_Count = 0;
    }
    else
    {
        Menu_Zero_Count++;
        if (Menu_Zero_Count > 6)
        {
            g_MENU_Touch_And_Below_1N = 0x00;
            Menu_Zero_Count = 7;
        }
    }
    if (Allow_Touch_flag == 0)
    {
        Signal = 0;
    }

    return Signal;
}
uint8_t RTE_Get_TouchBoard_MENU2_Sts(void) // Resume2
{
    Buttons_SignalType Signal = BUTTON_RELEASE;
    // Signal = TouchPanel_BtnSignalRead(BTN_MENU2);

    return Signal;
}

uint8_t g_VOL_Touch_And_Below_1N = 0;
uint8_t RTE_Get_TouchBoard_VOL1_Sts(void) // Cancel1
{
    Buttons_SignalType Signal = BUTTON_RELEASE;
    static uint8_t VOL_Zero_Count = 0;
    if (CurrentPosition == 2)
    {
        //  PanelPress_LevelType Press_Level;
        Signal = k_vol_touch_Sts; // TouchPanel_BtnSignalRead(BTN_VOL1);
        // Press_Level = TouchPanel_PressLevelRead();
    }

    if (Signal != 0) // PANEL_PRESS_LEVEL0 == Press_Level &&
    {
        g_VOL_Touch_And_Below_1N = 0x01;
        VOL_Zero_Count = 0;
    }
    else
    {
        VOL_Zero_Count++;
        if (VOL_Zero_Count > 6)
        {
            g_VOL_Touch_And_Below_1N = 0x00;
            VOL_Zero_Count = 7;
        }
    }
    if (Allow_Touch_flag == 0)
    {
        Signal = 0;
    }

    return Signal;
}
uint8_t RTE_Get_TouchBoard_VOL2_Sts(void) // Cancel2
{
    Buttons_SignalType Signal = BUTTON_RELEASE;
    // Signal = TouchPanel_BtnSignalRead(BTN_VOL2);

    return Signal;
}

// touch borad left/right slide: 0:idle, 1:short slide left 2:short slide right 3:long slide left 4:long slide right
uint8_t RTE_Get_TouchBoard_Left_Right_Slide_Sts(void)
{
    uint8_t retval = SIG_SWPLERISTSRI_SWPLERIREQSTS_IDLE;
    return retval;
}

// touch borad Up/Down slide: 0:idle, 1:short slide up 2:short slide down 3:long slide up 4:long slide down
uint8_t RTE_Get_TouchBoard_Up_Down_Slide_Sts(void)
{
    uint8_t retval = SIG_SWPUPDWNSTSRI_SWPUPDWNREQSTS_IDLE;
    return retval;
}

// uint8_t RTE_Get_INTB_PRSS_SNS(void);

// uint8_t RTE_Get_X1_SNS(void);
// uint8_t RTE_Get_X2_SNS(void);
// uint8_t RTE_Get_X3_SNS(void);
// uint8_t RTE_Get_X4_SNS(void);
// uint8_t RTE_Get_X5_SNS(void);

#define X_Length 6
#define Y_Length 6

#define Initial_Movement_Hysteresis 10
#define Next_Movement_Hysteresis 5

uint8_t X_Buf[X_Length];
uint8_t Y_Buf[Y_Length];
uint8_t X_CNT = 0;
uint8_t Y_CNT = 0;
uint8_t Stable_CNT_X = 0;
uint8_t Stable_CNT_Y = 0;
uint8_t X_Update_Flg = 1;
uint8_t Y_Update_Flg = 1;
uint8_t X_Update = 0;
uint8_t Y_Update = 0;
uint8_t rtn_X = 0;
uint8_t rtn_Y = 0;

uint8_t RTE_Get_X_SNS(void)
{


#if 1
    uint8_t i;
    uint8_t j;
    uint8 x_pos;
    uint8 temp;

    // uint8 rtn=0;
    GetSurface_Position(&x_pos, NULL_PTR);
    X_Buf[X_CNT++] = x_pos;
    if (X_CNT == X_Length)
    {
        X_CNT = 0;
    }
    for (j = 0; j < X_Length - 1; j++)
    {
        for (i = 0; i < X_Length - 1 - j; i++)
        {
            if (X_Buf[i] > X_Buf[i + 1])
            {
                temp = X_Buf[i];
                X_Buf[i] = X_Buf[i + 1];
                X_Buf[i + 1] = temp;
            }
        }
    }
    if (X_Buf[X_Length - 1] - X_Buf[0] < Initial_Movement_Hysteresis)
    {
        Stable_CNT_X++;
        if (X_Update_Flg == 1)
        {
            X_Update = (X_Buf[0] + X_Buf[X_Length - 1]) / 2;
        }
        if (Stable_CNT_X > 10)
        {
            Stable_CNT_X = 10;
            X_Update_Flg = 0;
            rtn_X = X_Update;
            if ((X_Buf[0] + X_Buf[X_Length - 1]) / 2 - X_Update > 0)
            {
                if ((X_Buf[0] + X_Buf[X_Length - 1]) / 2 - X_Update > Next_Movement_Hysteresis)
                {
                    Stable_CNT_X = 0;
                    X_Update_Flg = 1;
                }
            }
            else
            {
                if (X_Update - (X_Buf[0] + X_Buf[X_Length - 1]) / 2 > Next_Movement_Hysteresis)
                {
                    Stable_CNT_X = 0;
                    X_Update_Flg = 1;
                }
            }
        }
    }
    else
    {
        Stable_CNT_X = 0;
        X_Update_Flg = 1;
        rtn_X = (X_Buf[X_Length / 2] + X_Buf[X_Length / 2 - 1]) / 2;
    }
    
    if(Allow_Touch_flag==0)
    {
      rtn_X = 0;
    }
    if (x_pos == 0)
    {
        rtn_X = 0;
    }
    return rtn_X;

#endif
}


uint8_t RTE_Get_Y_SNS(void)
{

#if 1
    uint8_t i;
    uint8_t j;
    uint8 y_pos;
    uint8 temp;

    GetSurface_Position(NULL_PTR, &y_pos);
    Y_Buf[Y_CNT++] = y_pos;
    if (Y_CNT == Y_Length)
    {
        Y_CNT = 0;
    }
    for (j = 0; j < Y_Length - 1; j++)
    {
        for (i = 0; i < Y_Length - 1 - j; i++)
        {
            if (Y_Buf[i] > Y_Buf[i + 1])
            {
                temp = Y_Buf[i];
                Y_Buf[i] = Y_Buf[i + 1];
                Y_Buf[i + 1] = temp;
            }
        }
    }

    if (Y_Buf[Y_Length - 1] - Y_Buf[0] < Initial_Movement_Hysteresis)
    {
        Stable_CNT_Y++;
        if (Y_Update_Flg == 1)
        {
            Y_Update = (Y_Buf[0] + Y_Buf[Y_Length - 1]) / 2;
        }
        if (Stable_CNT_Y > 10)
        {
            Stable_CNT_Y = 10;
            Y_Update_Flg = 0;
            rtn_Y = Y_Update;
            if ((Y_Buf[0] + Y_Buf[Y_Length - 1]) / 2 - Y_Update > 0)
            {
                if ((Y_Buf[0] + Y_Buf[Y_Length - 1]) / 2 - Y_Update > Next_Movement_Hysteresis)
                {
                    Stable_CNT_Y = 0;
                    Y_Update_Flg = 1;
                }
            }
            else
            {
                if (Y_Update - (Y_Buf[0] + Y_Buf[Y_Length - 1]) / 2 > Next_Movement_Hysteresis)
                {
                    Stable_CNT_Y = 0;
                    Y_Update_Flg = 1;
                }
            }
        }
    }
    else
    {
        Stable_CNT_Y = 0;
        Y_Update_Flg = 1;

        rtn_Y = (Y_Buf[Y_Length / 2] + Y_Buf[Y_Length / 2 - 1]) / 2;
    }
    
    if(Allow_Touch_flag==0)
    {
      rtn_Y = 0;
    }
    
    if (y_pos == 0)
    {
        rtn_Y = 0;
    }
    return rtn_Y;
#endif
}

uint8_t g_XY_Touch_And_Below_1N = 0;

uint8_t RTE_Get_TouchBoard_XY_Sts(void) // Not active,Touch,Touch and Press,invalid from sensor
{
    // PanelPress_LevelType Press_Level;
    Buttons_SignalType XY_Sts = BUTTON_RELEASE;

    XY_Sts = TouchSurface_XY_StsRead();

    // Press_Level = TouchPanel_PressLevelRead();
    if (TouchSurface_is_TouchActive() == TRUE) // PANEL_PRESS_LEVEL0 == Press_Level &&
    {
        g_XY_Touch_And_Below_1N = 0x01;
    }
    else
    {
        g_XY_Touch_And_Below_1N = 0x00;
    }
    if(Allow_Touch_flag==0)
    {
        XY_Sts = 0;
    }
    return XY_Sts;
}

#if 1
uint8_t RTE_Get_TouchBoard_XY_Touch_And_Below_1N(void) // 0 false   1 true
{
    uint8_t ret_val = 0;
    uint16_t Two_Finger_Y_data[5] = {};
    uint16_t Two_Finger_X_data[5] = {};
    uint16_t th_x[5], th_y[5];

    uint8_t i, m;

    Two_Finger_X_data[0] = Touch_Sensor_delta09;
    Two_Finger_X_data[1] = Touch_Sensor_delta08;
    Two_Finger_X_data[2] = Touch_Sensor_delta07;
    Two_Finger_X_data[3] = Touch_Sensor_delta06;
    Two_Finger_X_data[4] = Touch_Sensor_delta05;

    Two_Finger_Y_data[0] = Touch_Sensor_delta14;
    Two_Finger_Y_data[1] = Touch_Sensor_delta13;
    Two_Finger_Y_data[2] = Touch_Sensor_delta12;
    Two_Finger_Y_data[3] = Touch_Sensor_delta11;
    Two_Finger_Y_data[4] = Touch_Sensor_delta10;

    th_x[0] = qtlib_key_configs_set1[9].channel_threshold;
    th_x[1] = qtlib_key_configs_set1[8].channel_threshold;
    th_x[2] = qtlib_key_configs_set1[7].channel_threshold;
    th_x[3] = qtlib_key_configs_set1[6].channel_threshold;
    th_x[4] = qtlib_key_configs_set1[5].channel_threshold;

    th_y[0] = qtlib_key_configs_set1[14].channel_threshold;
    th_y[1] = qtlib_key_configs_set1[13].channel_threshold;
    th_y[2] = qtlib_key_configs_set1[12].channel_threshold;
    th_y[3] = qtlib_key_configs_set1[11].channel_threshold;
    th_y[4] = qtlib_key_configs_set1[10].channel_threshold;

    

    Two_Finger_Y_count = 0;
    for (i = 0; i < 5; i++)
    {
        if (Two_Finger_Y_data[i] > th_y[i])
        {
            Two_Finger_Y_count++;
        }
    }

    i = 0;
    Y_count = 0;
    while (Two_Finger_Y_data[i] <= th_y[i++] && i < 5)
        ;
    i--;
    for (; i < 5; i++)
    {
        if (Two_Finger_Y_data[i] <= th_y[i])
            break;
        else
            Y_count++;
    }

    Two_Finger_X_count = 0;
    for (m = 0; m < 5; m++)
    {
        if (Two_Finger_X_data[m] > th_x[m])
        {
            Two_Finger_X_count++;
        }
    }
    m = 0;
    X_count = 0;
    while (Two_Finger_X_data[m] <= th_x[m++] && m < 5)
        ;
    m--;
    for (; m < 5; m++)
    {
        if (Two_Finger_X_data[m] <= th_x[m])
            break;
        else
            X_count++;
    }

    //    触发的通道数量      //连续触发的通道数量
    if ((Two_Finger_Y_count > 4 && Two_Finger_X_data[0]>th_x[0]) || Y_count != Two_Finger_Y_count || Two_Finger_X_count > 4 || X_count != Two_Finger_X_count)
    {
        //满足防误触
        Allow_Touch_counter++;
        if (Allow_Touch_counter >= 10)//40ms
        {
            Allow_Touch_counter = 10;
            Allow_Touch_flag = 0;
        }
    }
    else
    {
        Allow_Touch_counter = 0;
    }

    if(Allow_Touch_flag == 1)
    {

        if (g_XY_Touch_And_Below_1N == TRUE || g_VOICE_Touch_And_Below_1N == TRUE ||
            g_MENU_Touch_And_Below_1N == TRUE || g_VOL_Touch_And_Below_1N == TRUE)
        {
            ret_val = 0x01;
        }
    }
    return ret_val;
}
#endif

uint8_t RTE_Get_Pad_XY_trig(void)
{
    Buttons_SignalType XY_Sts = BUTTON_RELEASE;
    XY_Sts = TouchSurface_is_TouchActive();

    if(Allow_Touch_flag==0)
    {
        XY_Sts = 0;
    }
    return XY_Sts;
}
uint8_t RTE_Get_Voice_XY_trig(void)
{
    Buttons_SignalType Signal;
    Signal = k_voice_touch_Sts;

    if(Allow_Touch_flag==0)
    {
        Signal = 0;
    }
    return Signal;
}
uint8_t RTE_Get_Menu_XY_trig(void)
{
    Buttons_SignalType Signal;
    Signal = k_menu_touch_Sts; // TouchButton_is_TouchActive(BTN_MENU1);

    if(Allow_Touch_flag==0)
    {
        Signal = 0;
    }
    return Signal;
}
uint8_t RTE_Get_Vol_XY_trig(void)
{
    Buttons_SignalType Signal;
    Signal = k_vol_touch_Sts; // TouchButton_is_TouchActive(BTN_VOL1);

    if(Allow_Touch_flag==0)
    {
        Signal = 0;
    }
    return Signal;
}

uint8_t RTE_Get_ACT_FAULT_OUT_SNS(void)
{
    return 0;
}
uint8_t RTE_Get_ACT_LD_OUT_SNS(void)
{
    return 0;
}

// Input CAN
uint8_t rtn_data = 0;

// LED on off control
uint8_t RTE_Get_CAN_ActvnOfSteerWhlIllmn(void)
{
    // uint8_t temp = 0;
    rtn_data = 0;

    (void)ReceiveMessage(SIG_ACTVNOFSTEERWHLILLMN, &rtn_data);

    return rtn_data;
}
// LED Day/Night Mode switch
uint8_t RTE_Get_CAN_TwliBriSts(void)
{
    // uint8_t temp = 0;
    rtn_data = 0;

    (void)ReceiveMessage(SIG_TWLIBRISTS, &rtn_data);

    return rtn_data;
}

uint8_t RTE_Get_CAN_IntrBriSts(void)
{
    // uint8_t temp = 0;
    rtn_data = 0;

    (void)ReceiveMessage(SIG_INTRBRISTS, &rtn_data);

    return rtn_data;
}

uint8_t RTE_Get_CAN_SteerWhlTouchBdLeFaildFb(void)
{
    // uint8_t temp = 0;
    rtn_data = 0;

    //(void)ReceiveMessage(SIG_INTRBRISTS, &rtn_data);

    return rtn_data;
}

uint8_t RTE_Get_CAN_SwtOfKeyTone(void)
{
    // uint8_t temp = 0;
    rtn_data = 0;

    (void)ReceiveMessage(SIG_SWTOFKEYTONE, &rtn_data);

    return rtn_data;
}

// Vibration control
uint8_t RTE_Get_CAN_VibrationFbToSwtp(void)
{
    // uint8_t temp = 0;
    rtn_data = 0;

    //(void)ReceiveMessage(SIG_SWTOFKEYTONE, &rtn_data);

    return rtn_data;
}

uint8_t RTE_Get_CAN_IhuPrivateDHUCanFr01_Frame_Timeout(void) // 0x030 frame
{
    bool result = false;
    // if (ReadFlagRxErrorSig(SIG_) == COM_TRUE)
    //{
    //	result = true;
    // }
    return result;
}

// Output Control

void RTE_Set_illumination_BL_PWM(uint32_t Percent)
{
    /* to set PWM for LED ,PWM from 0 to 100*/
    uint32_t temp = 0;
    static uint32_t local_percent = 0xffffffff;
    // uint8_t LED_Delay = 0;
    // LED_Delay = SYSTICK_Get1_5_S_Flag();
    // if(LED_Delay==1)
    // {
    if (Fuction_State == Function_State_A)
    {
        if (local_percent != Percent)
        {
            local_percent = Percent;
            if (Percent == 100000 || Percent == 0) // Day mode
            {
                temp = (7999 / 100) * Percent / 1000;
                (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
            }
            else // Night mode
            {
                if (Percent == 625)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 729)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 937)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 1146)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 1354)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 1563)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 1875)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 2292)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 2813)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 3333)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 4063)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 4896)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 5938)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 7188)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 8646)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
                else if (Percent == 10417)
                {
                    temp = (7999 / 100) * Percent / 1000;
                    temp /= 2;
                    (void)TCC0_PWM24bitDutySet(TCC0_CHANNEL0, (uint16_t)temp);
                }
            }
        }
    }
    // }
}

// void RTE_Set_Power_AD_Ctrl(uint8_t Value);

// ACT control??
uint8_t trigger_ACT = 0;
void RTE_Set_ACT_Vibration(uint8_t Sts)
{
    if (Sts == 1)
    {
        if (trigger_ACT == 0)
        {
            trigger_ACT = 1;
            //    speeker_Tig_Once(50, SA51024_STRENGTH_LEVEL3);
        }
    }
    else
    {
        trigger_ACT = 0;
    }
}
// void RTE_Set_Gain0(uint8_t Value);
// void RTE_Set_Gain1(uint8_t Value);
// void RTE_Set_SDZ(uint8_t Value);
// void RTE_Set_INP(uint8_t Value);

// Output CAN
void RTE_Set_CAN_RiMFctActSgCe(uint8_t Value)
{
    uint16_t Press_signal = Get_forcedetect_force_value();
    if (Press_signal < 50 || Press_signal >= 2048)
    {
        Value = 0;
    }
    InitMessage(SIG_RIMFCTACTSGCE, &Value);
}

void RTE_Set_CAN_RiMFctActSgDn(uint8_t Value)
{
    uint16_t Press_signal = Get_forcedetect_force_value();
    if (Press_signal < 50 || Press_signal >= 2048)
    {
        Value = 0;
    }
    InitMessage(SIG_RIMFCTACTSGDN, &Value);
}

void RTE_Set_CAN_RiMFctActSgLe(uint8_t Value)
{
    uint16_t Press_signal = Get_forcedetect_force_value();
    if (Press_signal < 50 || Press_signal >= 2048)
    {
        Value = 0;
    }
    InitMessage(SIG_RIMFCTACTSGLE, &Value);
}

void RTE_Set_CAN_RiMFctActSgRi(uint8_t Value)
{
    uint16_t Press_signal = Get_forcedetect_force_value();
    if (Press_signal < 50 || Press_signal >= 2048)
    {
        Value = 0;
    }
    InitMessage(SIG_RIMFCTACTSGRI, &Value);
}

void RTE_Set_CAN_RiMFctActSgUp(uint8_t Value)
{
    uint16_t Press_signal = Get_forcedetect_force_value();
    if (Press_signal < 50 || Press_signal >= 2048)
    {
        Value = 0;
    }
    InitMessage(SIG_RIMFCTACTSGUP, &Value);
}

void RTE_Set_CAN_SteerWhlTouchBdVoice(uint8_t Value)
{
    uint16_t Press_signal = Get_forcedetect_force_value();
    if (Press_signal < 50 || Press_signal >= 2048)
    {
        Value = 0;
    }
    InitMessage(SIG_STEERWHLTOUCHBDVOICE, &Value);
}

void RTE_Set_CAN_SwpLeRiStsRi(uint8_t Value)
{
    InitMessage(SIG_SWPLERISTSRI, &Value);
}

void RTE_Set_CAN_SwpUpDwnStsRi(uint8_t Value)
{
    InitMessage(SIG_SWPUPDWNSTSRI, &Value);
}

void RTE_Set_CAN_SldVolCtrlSts(uint8_t Value)
{
    InitMessage(SIG_SLDVOLCTRLSTS, &Value);
}

void RTE_Set_CAN_DiagcFailrTouchPanSWTRCmnFltSts(uint8_t Value)
{
    // InitMessage(SIG_DIAGCFAILRTOUCHPANSWTRCMNFLTSTS, &Value);
}

void RTE_Set_CAN_DiagcFailrTouchPanSWTRTouchdFltSts(uint8_t Value)
{
    // InitMessage(SIG_DIAGCFAILRTOUCHPANSWTRTOUCHDFLTSTS, &Value);
}

void RTE_Set_CAN_DiagcFailrTouchPanSWTRVibrationFltSts(uint8_t Value)
{
    // InitMessage(SIG_DIAGCFAILRTOUCHPANSWTRVIBRATIONFLTSTS, &Value);
}

void RTE_Set_CAN_DiagcFailrTouchPanSWTRSnsrFltSts(uint8_t Value)
{
    // InitMessage(SIG_DIAGCFAILRTOUCHPANSWTRSNSRFLTSTS, &Value);
}

void RTE_Set_CAN_SteerWhlTouchBdMenu(uint8_t Value)
{
    uint16_t Press_signal = Get_forcedetect_force_value();
    if (Press_signal < 50 || Press_signal >= 2048)
    {
        Value = 0;
    }
    InitMessage(SIG_STEERWHLTOUCHBDMENU, &Value);
}

void RTE_Set_CAN_SteerWhlTouchBdDn(uint8_t Value)
{
    uint16_t Press_signal = Get_forcedetect_force_value();
    if (Press_signal < 50 || Press_signal >= 2048)
    {
        Value = 0;
    }
    InitMessage(SIG_STEERWHLTOUCHBDDN, &Value);
}

void RTE_Set_CAN_SteerWhlTouchBdRiChks(uint8_t Value)
{
    InitMessage(SIG_STEERWHLTOUCHBDRICHKS, &Value);
}

void RTE_Set_CAN_SteerWhlTouchBdRiCntr(uint8_t Value)
{
    InitMessage(SIG_STEERWHLTOUCHBDRICNTR, &Value);
    // TouchBoard_RC=Value;
}

uint8_t g_last_SteerWhlTouchBdSts = BUTTON_RELEASE;
void RTE_Set_CAN_SteerWhlTouchBdRiSteerWhlTouchBdSts(uint8_t Value)
{
    uint16_t Press_signal = Get_forcedetect_force_value();
    if (Press_signal < 50 || Press_signal >= 2048)
    {
        Value = 0;
    }
    if (Value != 0 && Touch_Sensor_Fault != Touch_NoFault)
    {
        Value = 0x03;
    }
    
    InitMessage(SIG_STEERWHLTOUCHBDRISTEERWHLTOUCHBDSTS, &Value);
    // TouchBoard_Sts=Value;
}

void RTE_Set_CAN_SteerWhlTouchBdRiTouchPosnX(uint8_t Value)
{
    uint16_t Press_signal = Get_forcedetect_force_value();
    if (Press_signal < 50 || Press_signal >= 2048)
    {
        Value = 0;
    }
    InitMessage(SIG_STEERWHLTOUCHBDRITOUCHPOSNX, &Value);
    // TouchBoard_X=Value;
}
void RTE_Set_CAN_SteerWhlTouchBdRiTouchPosnY(uint8_t Value)
{
    uint16_t Press_signal = Get_forcedetect_force_value();
    if (Press_signal < 50 || Press_signal >= 2048)
    {
        Value = 0;
    }
    InitMessage(SIG_STEERWHLTOUCHBDRITOUCHPOSNY, &Value);
    // TouchBoard_Y=Value;
}

uint8_t Pre_309_req;
uint8_t Remain_frame;
uint8_t Remain_cnt;
uint8_t Remain_X = 0;
uint8_t Remain_Y = 0;
uint8_t Frame_0x309_RC = 15;
void RTE_Set_SEND_CAN_0x309_immediately(uint8_t Value) // called immediately in 10ms task when touch,or touch and press in XY area
{
    uint8_t Current_309_req;
    Current_309_req = Value;

    if (Value == 1)
    {
        if (Frame_0x309_RC > 14)
        {
            Frame_0x309_RC = 0;
        }
        else
        {
            Frame_0x309_RC++;
        }
        InitMessage(SIG_STEERWHLTOUCHBDRICNTR, &Frame_0x309_RC);
        // Send a 0x309 frame

        OsekComTxReqFrmSwtrPrivateDHUCanFr01();
    }
    if (Value == 0 && Pre_309_req == 1)
    {
        Remain_frame = 1;
    }
    Pre_309_req = Current_309_req;

    if (Remain_frame == 1)
    {
        InitMessage(SIG_STEERWHLTOUCHBDRITOUCHPOSNX, &Remain_X);
        InitMessage(SIG_STEERWHLTOUCHBDRITOUCHPOSNY, &Remain_Y);
        InitMessage(SIG_STEERWHLTOUCHBDRISTEERWHLTOUCHBDSTS, &Remain_Y);
        if (Remain_cnt < 2)
        {
            if (Frame_0x309_RC > 14)
            {
                Frame_0x309_RC = 0;
            }
            else
            {
                Frame_0x309_RC++;
            }
            InitMessage(SIG_STEERWHLTOUCHBDRICNTR, &Frame_0x309_RC);
            OsekComTxReqFrmSwtrPrivateDHUCanFr01();
            Remain_cnt++;
        }
        else
        {
            Remain_frame = 0;
            Remain_cnt = 0;
        }
    }
}
void RTE_Set_SEND_CAN_0x307_25ms(uint8_t Value) // when touch,or touch and press in 3 button area,0x305 is sent every 25ms
{
    if (Value == 1)
    {

        // Send a 0x307 frame

        OsekComTxReqFrmSwtrPrivateDHUCanFr02();
    }
}

void RTE_Set_SEND_CAN_0x307_1000ms(uint8_t Value) // when  3 button area is not active,0x305 is Sent every 1000ms
{
    if (Value == 1)
    {

        // Send a 0x307 frame

        OsekComTxReqFrmSwtrPrivateDHUCanFr02();
    }
}

uint8_t UbInitValue = 1;
void RTE_Set_All_UB(void)
{
    InitMessage(SIG_DIAGCFAILRTOUCHPANSWTR_UB, &UbInitValue);
    InitMessage(SIG_RIMFCTACTSGCE_UB, &UbInitValue);
    InitMessage(SIG_RIMFCTACTSGDN_UB, &UbInitValue);
    InitMessage(SIG_RIMFCTACTSGLE_UB, &UbInitValue);
    InitMessage(SIG_RIMFCTACTSGRI_UB, &UbInitValue);
    InitMessage(SIG_RIMFCTACTSGUP_UB, &UbInitValue);
    InitMessage(SIG_SLDVOLCTRLSTS_UB, &UbInitValue);
    InitMessage(SIG_STEERWHLTOUCHBDDN_UB, &UbInitValue);
    InitMessage(SIG_STEERWHLTOUCHBDMENU_UB, &UbInitValue);
    InitMessage(SIG_STEERWHLTOUCHBDRI_UB, &UbInitValue);
    InitMessage(SIG_STEERWHLTOUCHBDVOICE_UB, &UbInitValue);
    InitMessage(SIG_SWPLERISTSRI_UB, &UbInitValue);
    InitMessage(SIG_SWPUPDWNSTSRI_UB, &UbInitValue);
    InitMessage(SIG_SWTRPARTNOCMPL_UB, &UbInitValue);
    InitMessage(SIG_SWTRSERNO_UB, &UbInitValue);
}

#if 1
void RTE_Set_All_Test_Value(void) // 2ms
{
#if 1
    uint16_t Press_baseline;
    uint16_t Press_signal, Press_preload;
    uint8_t i,frccdata[8]={0};
    static uint32_t Press_reset_counter[16], Press_repeat_counter[16], baseline_error_counter, baseline_error_repeat_counter;
    Press_baseline = Get_forcedetect_basline_value();
    Press_signal = Get_forcedetect_force_value();
    Press_preload = Get_forcedetect_preload_value();
    // uint16 temp = CurrentPosition;
    InitMessage(SIG_SWTRPRESSBASELINE, &Press_baseline); // Press_baseline
    if (Press_signal < 4000)
    {
        InitMessage(SIG_SWTRPRESSSIGNAL, &Press_signal); // Press_signal-Convert to N: TouchPanel_PressNValue = ((float32)Press_signal / (float32)0xF2);
    }
    // InitMessage(SIG_SWTRPRESSSIGNAL, &Fr08_send_flag);
#endif

    for (i = 0; i < 15; i++)
    {
        if (get_sensor_node_signal(i) > (get_sensor_node_reference(i) + qtlib_key_configs_set1[i].channel_threshold))
        {
            Press_reset_counter[i]++;
            if (Press_reset_counter[i] > 25000) // 50s = 50000 ms
            {
                if (Press_repeat_counter[i] == 0)
                {
                    Press_repeat_counter[i] = 55000;
                    Press_reset_counter[i] = 0;
                    // update_sensor_node_reference(i,get_sensor_node_signal(i));
                }
                else
                {
                    touch_reset2times_flag = 1;
                }
            }
        }
        else
        {
            Press_reset_counter[i] = 0;
        }

        if (Press_repeat_counter[i] > 0)
        {
            Press_repeat_counter[i]--;
        }
    }

    for (i = 0; i < 15; i++)
    {
        if (get_sensor_node_reference(i) == 0)
        {
            baseline_error_counter++;
            if (baseline_error_counter > 25000) // 50s
            {
                if (baseline_error_repeat_counter != 0)
                {
                    touch_reset2times_flag = 1;
                }
                else
                {
                    baseline_error_repeat_counter = 55000; // 110S
                    ADC0_Initialize();
                    ADC0_Enable();
                }
            }
            break;
        }
    }
    if (i == 15)
    {
        baseline_error_counter = 0;
    }
    if (baseline_error_repeat_counter > 0)
    {
        baseline_error_repeat_counter--;
    }

    Touch_Sensor_Sts00 = get_sensor_node_signal(0U);
    Touch_Sensor_Sts01 = get_sensor_node_signal(1U);
    Touch_Sensor_Sts02 = get_sensor_node_signal(2U);
    Touch_Sensor_Sts03 = get_sensor_node_signal(3U);
    Touch_Sensor_Sts04 = get_sensor_node_signal(4U);
    Touch_Sensor_Sts05 = get_sensor_node_signal(5U);
    Touch_Sensor_Sts06 = get_sensor_node_signal(6U);
    Touch_Sensor_Sts07 = get_sensor_node_signal(7U);
    Touch_Sensor_Sts08 = get_sensor_node_signal(8U);
    Touch_Sensor_Sts09 = get_sensor_node_signal(9U);
    Touch_Sensor_Sts10 = get_sensor_node_signal(10U);
    Touch_Sensor_Sts11 = get_sensor_node_signal(11U);
    Touch_Sensor_Sts12 = get_sensor_node_signal(12U);
    Touch_Sensor_Sts13 = get_sensor_node_signal(13U);
    Touch_Sensor_Sts14 = get_sensor_node_signal(14U);

    // get_sensor_state

    Touch_Sensor_Ref00 = get_sensor_node_reference(0U);
    Touch_Sensor_Ref01 = get_sensor_node_reference(1U);
    Touch_Sensor_Ref02 = get_sensor_node_reference(2U);
    Touch_Sensor_Ref03 = get_sensor_node_reference(3U);
    Touch_Sensor_Ref04 = get_sensor_node_reference(4U);
    Touch_Sensor_Ref05 = get_sensor_node_reference(5U);
    Touch_Sensor_Ref06 = get_sensor_node_reference(6U);
    Touch_Sensor_Ref07 = get_sensor_node_reference(7U);
    Touch_Sensor_Ref08 = get_sensor_node_reference(8U);
    Touch_Sensor_Ref09 = get_sensor_node_reference(9U);
    Touch_Sensor_Ref10 = get_sensor_node_reference(10U);
    Touch_Sensor_Ref11 = get_sensor_node_reference(11U);
    Touch_Sensor_Ref12 = get_sensor_node_reference(12U);
    Touch_Sensor_Ref13 = get_sensor_node_reference(13U);
    Touch_Sensor_Ref14 = get_sensor_node_reference(14U);

    Touch_Sensor_CC00 = get_sensor_cc_val(0U);
    Touch_Sensor_CC01 = get_sensor_cc_val(1U);
    Touch_Sensor_CC02 = get_sensor_cc_val(2U);
    Touch_Sensor_CC03 = get_sensor_cc_val(3U);
    Touch_Sensor_CC04 = get_sensor_cc_val(4U);
    Touch_Sensor_CC05 = get_sensor_cc_val(5U);
    Touch_Sensor_CC06 = get_sensor_cc_val(6U);
    Touch_Sensor_CC07 = get_sensor_cc_val(7U);
    Touch_Sensor_CC08 = get_sensor_cc_val(8U);
    Touch_Sensor_CC09 = get_sensor_cc_val(9U);
    Touch_Sensor_CC10 = get_sensor_cc_val(10U);
    Touch_Sensor_CC11 = get_sensor_cc_val(11U);
    Touch_Sensor_CC12 = get_sensor_cc_val(12U);
    Touch_Sensor_CC13 = get_sensor_cc_val(13U);
    Touch_Sensor_CC14 = get_sensor_cc_val(14U);

    frccdata[0] = (uint8_t)Touch_Sensor_CC00;
    frccdata[1] = Touch_Sensor_CC00>>8;
    frccdata[2] = (uint8_t)Touch_Sensor_CC01;
    frccdata[3] = Touch_Sensor_CC01>>8;
    frccdata[4] = (uint8_t)Touch_Sensor_CC02;
    frccdata[5] = Touch_Sensor_CC02>>8;
    frccdata[6] = (uint8_t)Touch_Sensor_CC03;
    frccdata[7] = Touch_Sensor_CC03>>8;
    OsekComSetFR09data(frccdata);

    frccdata[0] = (uint8_t)Touch_Sensor_CC04;
    frccdata[1] = Touch_Sensor_CC04>>8;
    frccdata[2] = (uint8_t)Touch_Sensor_CC05;
    frccdata[3] = Touch_Sensor_CC05>>8;
    frccdata[4] = (uint8_t)Touch_Sensor_CC06;
    frccdata[5] = Touch_Sensor_CC06>>8;
    frccdata[6] = (uint8_t)Touch_Sensor_CC07;
    frccdata[7] = Touch_Sensor_CC07>>8;
    OsekComSetFR0Adata(frccdata);

    frccdata[0] = (uint8_t)Touch_Sensor_CC08;
    frccdata[1] = Touch_Sensor_CC08>>8;
    frccdata[2] = (uint8_t)Touch_Sensor_CC09;
    frccdata[3] = Touch_Sensor_CC09>>8;
    frccdata[4] = (uint8_t)Touch_Sensor_CC10;
    frccdata[5] = Touch_Sensor_CC10>>8;
    frccdata[6] = (uint8_t)Touch_Sensor_CC11;
    frccdata[7] = Touch_Sensor_CC11>>8;
    OsekComSetFR0Bdata(frccdata);

    frccdata[0] = (uint8_t)Touch_Sensor_CC12;
    frccdata[1] = Touch_Sensor_CC12>>8;
    frccdata[2] = (uint8_t)Touch_Sensor_CC13;
    frccdata[3] = Touch_Sensor_CC13>>8;
    frccdata[4] = (uint8_t)Touch_Sensor_CC14;
    frccdata[5] = Touch_Sensor_CC14>>8;
    OsekComSetFR0Cdata(frccdata);

    Touch_Sensor_delta00 = Touch_Sensor_Sts00 > Touch_Sensor_Ref00 ? Touch_Sensor_Sts00 - Touch_Sensor_Ref00 : 0;
    Touch_Sensor_delta01 = Touch_Sensor_Sts01 > Touch_Sensor_Ref01 ? Touch_Sensor_Sts01 - Touch_Sensor_Ref01 : 0;
    Touch_Sensor_delta02 = Touch_Sensor_Sts02 > Touch_Sensor_Ref02 ? Touch_Sensor_Sts02 - Touch_Sensor_Ref02 : 0;
    Touch_Sensor_delta03 = Touch_Sensor_Sts03 > Touch_Sensor_Ref03 ? Touch_Sensor_Sts03 - Touch_Sensor_Ref03 : 0;
    Touch_Sensor_delta04 = Touch_Sensor_Sts04 > Touch_Sensor_Ref04 ? Touch_Sensor_Sts04 - Touch_Sensor_Ref04 : 0;
    Touch_Sensor_delta05 = Touch_Sensor_Sts05 > Touch_Sensor_Ref05 ? Touch_Sensor_Sts05 - Touch_Sensor_Ref05 : 0;
    Touch_Sensor_delta06 = Touch_Sensor_Sts06 > Touch_Sensor_Ref06 ? Touch_Sensor_Sts06 - Touch_Sensor_Ref06 : 0;
    Touch_Sensor_delta07 = Touch_Sensor_Sts07 > Touch_Sensor_Ref07 ? Touch_Sensor_Sts07 - Touch_Sensor_Ref07 : 0;
    Touch_Sensor_delta08 = Touch_Sensor_Sts08 > Touch_Sensor_Ref08 ? Touch_Sensor_Sts08 - Touch_Sensor_Ref08 : 0;
    Touch_Sensor_delta09 = Touch_Sensor_Sts09 > Touch_Sensor_Ref09 ? Touch_Sensor_Sts09 - Touch_Sensor_Ref09 : 0;
    Touch_Sensor_delta10 = Touch_Sensor_Sts10 > Touch_Sensor_Ref10 ? Touch_Sensor_Sts10 - Touch_Sensor_Ref10 : 0;
    Touch_Sensor_delta11 = Touch_Sensor_Sts11 > Touch_Sensor_Ref11 ? Touch_Sensor_Sts11 - Touch_Sensor_Ref11 : 0;
    Touch_Sensor_delta12 = Touch_Sensor_Sts12 > Touch_Sensor_Ref12 ? Touch_Sensor_Sts12 - Touch_Sensor_Ref12 : 0;
    Touch_Sensor_delta13 = Touch_Sensor_Sts13 > Touch_Sensor_Ref13 ? Touch_Sensor_Sts13 - Touch_Sensor_Ref13 : 0;
    Touch_Sensor_delta14 = Touch_Sensor_Sts14 > Touch_Sensor_Ref14 ? Touch_Sensor_Sts14 - Touch_Sensor_Ref14 : 0;

    // Touch_Sensor_Sts13=g_XY_Touch_And_Below_1N;

    InitMessage(SIG_SWTRSENSOR1_SIGNAL, &Touch_Sensor_Sts00);
    InitMessage(SIG_SWTRSENSOR2_SIGNAL, &Touch_Sensor_Sts01);
    InitMessage(SIG_SWTRSENSOR3_SIGNAL, &Touch_Sensor_Sts02);
    InitMessage(SIG_SWTRSENSOR4_SIGNAL, &Touch_Sensor_Sts03);
    InitMessage(SIG_SWTRSENSOR5_SIGNAL, &Touch_Sensor_Sts04);
    InitMessage(SIG_SWTRSENSOR6_SIGNAL, &Touch_Sensor_Sts05);
    InitMessage(SIG_SWTRSENSOR7_SIGNAL, &Touch_Sensor_Sts06);
    InitMessage(SIG_SWTRSENSOR8_SIGNAL, &Touch_Sensor_Sts07);
    InitMessage(SIG_SWTRSENSOR9_SIGNAL, &Touch_Sensor_Sts08);
    InitMessage(SIG_SWTRSENSOR10_SIGNAL, &Touch_Sensor_Sts09);
    InitMessage(SIG_SWTRSENSOR11_SIGNAL, &Touch_Sensor_Sts10);
    InitMessage(SIG_SWTRSENSOR12_SIGNAL, &Touch_Sensor_Sts11);
    InitMessage(SIG_SWTRSENSOR13_SIGNAL, &Touch_Sensor_Sts12);
    InitMessage(SIG_SWTRSENSOR14_SIGNAL, &Touch_Sensor_Sts13);
    InitMessage(SIG_SWTRSENSOR15_SIGNAL, &Touch_Sensor_Sts14);

    InitMessage(SIG_SWTRSENSOR1_BASELINE, &Touch_Sensor_Ref00);
    InitMessage(SIG_SWTRSENSOR2_BASELINE, &Touch_Sensor_Ref01);
    InitMessage(SIG_SWTRSENSOR3_BASELINE, &Touch_Sensor_Ref02);
    InitMessage(SIG_SWTRSENSOR4_BASELINE, &Touch_Sensor_Ref03);
    InitMessage(SIG_SWTRSENSOR5_BASELINE, &Touch_Sensor_Ref04);
    InitMessage(SIG_SWTRSENSOR6_BASELINE, &Touch_Sensor_Ref05);
    InitMessage(SIG_SWTRSENSOR7_BASELINE, &Touch_Sensor_Ref06);
    InitMessage(SIG_SWTRSENSOR8_BASELINE, &Touch_Sensor_Ref07);
    InitMessage(SIG_SWTRSENSOR9_BASELINE, &Touch_Sensor_Ref08);
    InitMessage(SIG_SWTRSENSOR10_BASELINE, &Touch_Sensor_Ref09);
    InitMessage(SIG_SWTRSENSOR11_BASELINE, &Touch_Sensor_Ref10);
    InitMessage(SIG_SWTRSENSOR12_BASELINE, &Touch_Sensor_Ref11);
    InitMessage(SIG_SWTRSENSOR13_BASELINE, &Touch_Sensor_Ref12);
    InitMessage(SIG_SWTRSENSOR14_BASELINE, &Touch_Sensor_Ref13);
    InitMessage(SIG_SWTRSENSOR15_BASELINE, &Touch_Sensor_Ref14);

    g16_DiagTouchPressSenRes[0][0] = Touch_Sensor_Ref00;
    g16_DiagTouchPressSenRes[0][1] = Touch_Sensor_Ref01;
    g16_DiagTouchPressSenRes[0][2] = Touch_Sensor_Ref02;
    g16_DiagTouchPressSenRes[0][3] = Touch_Sensor_Ref03;
    g16_DiagTouchPressSenRes[0][4] = Touch_Sensor_Ref04;
    g16_DiagTouchPressSenRes[0][5] = Touch_Sensor_Ref05;
    g16_DiagTouchPressSenRes[0][6] = Touch_Sensor_Ref06;
    g16_DiagTouchPressSenRes[0][7] = Touch_Sensor_Ref07;
    g16_DiagTouchPressSenRes[0][8] = Touch_Sensor_Ref08;
    g16_DiagTouchPressSenRes[0][9] = Touch_Sensor_Ref09;
    g16_DiagTouchPressSenRes[0][10] = Touch_Sensor_Ref10;
    g16_DiagTouchPressSenRes[0][11] = Touch_Sensor_Ref11;
    g16_DiagTouchPressSenRes[0][12] = Touch_Sensor_Ref12;
    g16_DiagTouchPressSenRes[0][13] = Touch_Sensor_Ref13;
    g16_DiagTouchPressSenRes[0][14] = Touch_Sensor_Ref14;

    g16_DiagTouchPressSenRes[1][0] = Touch_Sensor_Sts00;
    g16_DiagTouchPressSenRes[1][1] = Touch_Sensor_Sts01;
    g16_DiagTouchPressSenRes[1][2] = Touch_Sensor_Sts02;
    g16_DiagTouchPressSenRes[1][3] = Touch_Sensor_Sts03;
    g16_DiagTouchPressSenRes[1][4] = Touch_Sensor_Sts04;
    g16_DiagTouchPressSenRes[1][5] = Touch_Sensor_Sts05;
    g16_DiagTouchPressSenRes[1][6] = Touch_Sensor_Sts06;
    g16_DiagTouchPressSenRes[1][7] = Touch_Sensor_Sts07;
    g16_DiagTouchPressSenRes[1][8] = Touch_Sensor_Sts08;
    g16_DiagTouchPressSenRes[1][9] = Touch_Sensor_Sts09;
    g16_DiagTouchPressSenRes[1][10] = Touch_Sensor_Sts10;
    g16_DiagTouchPressSenRes[1][11] = Touch_Sensor_Sts11;
    g16_DiagTouchPressSenRes[1][12] = Touch_Sensor_Sts12;
    g16_DiagTouchPressSenRes[1][13] = Touch_Sensor_Sts13;
    g16_DiagTouchPressSenRes[1][14] = Touch_Sensor_Sts14;

    g16_DiagTouchPressSenRes[2][0] = Press_baseline;
    g16_DiagTouchPressSenRes[2][1] = Press_signal;
    g16_DiagTouchPressSenRes[2][2] = Press_preload;

    uint16_t vol_test_sig = (uint16_t)k_vol_touch_Sts;
    InitMessage(SIG_SWTRSENSOR16_SIGNAL, &vol_test_sig);
}

#endif