//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 "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

#if 0
static uint8_t autoCrc_Table[256] =

         {

                  0x00u, 0x1du, 0x3au, 0x27u, 0x74u, 0x69u, 0x4eu, 0x53u,

                  0xe8u, 0xf5u, 0xd2u, 0xcfu, 0x9cu, 0x81u, 0xa6u, 0xbbu,

                  0xcdu, 0xd0u, 0xf7u, 0xeau, 0xb9u, 0xa4u, 0x83u, 0x9eu,

                  0x25u, 0x38u, 0x1fu, 0x02u, 0x51u, 0x4cu, 0x6bu, 0x76u,

                  0x87u, 0x9au, 0xbdu, 0xa0u, 0xf3u, 0xeeu, 0xc9u, 0xd4u,

                  0x6fu, 0x72u, 0x55u, 0x48u, 0x1bu, 0x06u, 0x21u, 0x3cu,

                  0x4au, 0x57u, 0x70u, 0x6du, 0x3eu, 0x23u, 0x04u, 0x19u,

                  0xa2u, 0xbfu, 0x98u, 0x85u, 0xd6u, 0xcbu, 0xecu, 0xf1u,

                  0x13u, 0x0eu, 0x29u, 0x34u, 0x67u, 0x7au, 0x5du, 0x40u,

                  0xfbu, 0xe6u, 0xc1u, 0xdcu, 0x8fu, 0x92u, 0xb5u, 0xa8u,

                  0xdeu, 0xc3u, 0xe4u, 0xf9u, 0xaau, 0xb7u, 0x90u, 0x8du,

                  0x36u, 0x2bu, 0x0cu, 0x11u, 0x42u, 0x5fu, 0x78u, 0x65u,

                  0x94u, 0x89u, 0xaeu, 0xb3u, 0xe0u, 0xfdu, 0xdau, 0xc7u,

                  0x7cu, 0x61u, 0x46u, 0x5bu, 0x08u, 0x15u, 0x32u, 0x2fu,

                  0x59u, 0x44u, 0x63u, 0x7eu, 0x2du, 0x30u, 0x17u, 0x0au,

                  0xb1u, 0xacu, 0x8bu, 0x96u, 0xc5u, 0xd8u, 0xffu, 0xe2u,

                  0x26u, 0x3bu, 0x1cu, 0x01u, 0x52u, 0x4fu, 0x68u, 0x75u,

                  0xceu, 0xd3u, 0xf4u, 0xe9u, 0xbau, 0xa7u, 0x80u, 0x9du,

                  0xebu, 0xf6u, 0xd1u, 0xccu, 0x9fu, 0x82u, 0xa5u, 0xb8u,

                  0x03u, 0x1eu, 0x39u, 0x24u, 0x77u, 0x6au, 0x4du, 0x50u,

                  0xa1u, 0xbcu, 0x9bu, 0x86u, 0xd5u, 0xc8u, 0xefu, 0xf2u,

                  0x49u, 0x54u, 0x73u, 0x6eu, 0x3du, 0x20u, 0x07u, 0x1au,

                  0x6cu, 0x71u, 0x56u, 0x4bu, 0x18u, 0x05u, 0x22u, 0x3fu,

                  0x84u, 0x99u, 0xbeu, 0xa3u, 0xf0u, 0xedu, 0xcau, 0xd7u,

                  0x35u, 0x28u, 0x0fu, 0x12u, 0x41u, 0x5cu, 0x7bu, 0x66u,

                  0xddu, 0xc0u, 0xe7u, 0xfau, 0xa9u, 0xb4u, 0x93u, 0x8eu,

                  0xf8u, 0xe5u, 0xc2u, 0xdfu, 0x8cu, 0x91u, 0xb6u, 0xabu,

                  0x10u, 0x0du, 0x2au, 0x37u, 0x64u, 0x79u, 0x5eu, 0x43u,

                  0xb2u, 0xafu, 0x88u, 0x95u, 0xc6u, 0xdbu, 0xfcu, 0xe1u,

                  0x5au, 0x47u, 0x60u, 0x7du, 0x2eu, 0x33u, 0x14u, 0x09u,

                  0x7fu, 0x62u, 0x45u, 0x58u, 0x0bu, 0x16u, 0x31u, 0x2cu,

                  0x97u, 0x8au, 0xadu, 0xb0u, 0xe3u, 0xfeu, 0xd9u, 0xc4u

         };
#endif


//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;
    UD_SlideLevel = TouchPanel_SlideUpDownLevelRead();
    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;
	Signal = TouchPanel_BtnSignalRead(BTN_UP);
	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;
	Signal = TouchPanel_BtnSignalRead(BTN_DOWN);
    
	return Signal;
}

uint8_t RTE_Get_TouchBoard_Left_Sts(void)
{
    /* x:95, y: ec */
    /* x:97, y: e9 */
	Buttons_SignalType Signal;
	Signal = TouchPanel_BtnSignalRead(BTN_LEFT);
    
	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;
	Signal = TouchPanel_BtnSignalRead(BTN_RIGHT);
    
	return Signal;
}

uint8_t RTE_Get_TouchBoard_Center_Sts(void)
{
	Buttons_SignalType Signal;
	Signal = TouchPanel_BtnSignalRead(BTN_CONFIRM);
   
	return Signal;
}

uint8_t g_VOICE_Touch_And_Below_1N = 0;
uint8_t RTE_Get_TouchBoard_VOICE1_Sts(void)//TJP1
{
#if 0
    Buttons_SignalType Signal;
   // PanelPress_LevelType Press_Level;
	Signal = TouchPanel_BtnSignalRead(BTN_VOICE1);
	//Press_Level = TouchPanel_PressLevelRead();
	if( TouchButton_is_TouchActive(BTN_VOICE1) == TRUE)//PANEL_PRESS_LEVEL0 == Press_Level &&
	{
		g_VOICE_Touch_And_Below_1N = 0x01;
	}else{
		g_VOICE_Touch_And_Below_1N = 0x00;
	}	

    return Signal;
#endif
    Buttons_SignalType Signal;
  Signal=k_voice_touch_Sts;
  if( Signal != 0 )//PANEL_PRESS_LEVEL0 == Press_Level &&
	{
		g_VOICE_Touch_And_Below_1N = 0x01;
	}else{
		g_VOICE_Touch_And_Below_1N = 0x00;
	}	
  
  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;
   // PanelPress_LevelType Press_Level;
	Signal = TouchPanel_BtnSignalRead(BTN_MENU1);	
	//Press_Level = TouchPanel_PressLevelRead();
	if( TouchButton_is_TouchActive(BTN_MENU1) == TRUE)//PANEL_PRESS_LEVEL0 == Press_Level &&
	{
		g_MENU_Touch_And_Below_1N = 0x01;
	}else{
		g_MENU_Touch_And_Below_1N = 0x00;
	}	

    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;
  //  PanelPress_LevelType Press_Level;
	Signal = TouchPanel_BtnSignalRead(BTN_VOL1);	
	//Press_Level = TouchPanel_PressLevelRead();
	if(TouchButton_is_TouchActive(BTN_VOL1) == TRUE)//PANEL_PRESS_LEVEL0 == Press_Level && 
	{
		g_VOL_Touch_And_Below_1N = 0x01;
	}else{
		g_VOL_Touch_And_Below_1N = 0x00;
	}	

	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;
#if 1
	TP_SlideDirection_Enum SlideDir;
	SlideDir = TouchPanel_SlideEventRead();
	switch(SlideDir)
	{
		case TP_SlideDirection_ShortLEFT:
			retval = SIG_SWPLERISTSRI_SWPLERIREQSTS_SHORTSLIDELEFT;
		break;
		case TP_SlideDirection_LongLEFT:
			retval = SIG_SWPLERISTSRI_SWPLERIREQSTS_LONGSLIDELEFT;
		break;
		case TP_SlideDirection_ShortRIGHT:
			retval = SIG_SWPLERISTSRI_SWPLERIREQSTS_SHORTSLIDERIGHT;
		break;
		case TP_SlideDirection_LongRIGHT:
			retval = SIG_SWPLERISTSRI_SWPLERIREQSTS_LONGSLIDERIGHT;
		break;
		default: break;
	}
#endif
     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;
#if 1
	TP_SlideDirection_Enum SlideDir;
	SlideDir = TouchPanel_SlideEventRead();
	switch(SlideDir)
	{
		case TP_SlideDirection_ShortUP:
			retval = SIG_SWPUPDWNSTSRI_SWPUPDWNREQSTS_SHORTSLIDEUP;
		break;
		case TP_SlideDirection_LongUP:
			retval = SIG_SWPUPDWNSTSRI_SWPUPDWNREQSTS_LONGSLIDEUP;
		break;
		case TP_SlideDirection_ShortDOWN:
			retval = SIG_SWPUPDWNSTSRI_SWPUPDWNREQSTS_SHORTSLIDEDOWN;
		break;
		case TP_SlideDirection_LongDOWN:
			retval = SIG_SWPUPDWNSTSRI_SWPUPDWNREQSTS_LONGSLIDEDOWN;
		break;
		default: break;
	}

#endif
        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 0
    uint8_t count;
    uint16_t sum_x=0;
    uint8 x_pos;
	GetSurface_Position(&x_pos, NULL_PTR);
    X_Buf[X_CNT++]=x_pos;
    if(X_CNT==X_Length)
    {
        X_CNT=0;
    }
    for(count=0;count<X_Length;count++)
    {
        sum_x+=(uint16_t)X_Buf[count];
    }
   if(x_pos==0)
    {
       sum_x=0;
    }
    return (uint8_t)(sum_x/X_Length);
#endif
    
#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(x_pos==0)
    {
       rtn_X=0;
    }
   return rtn_X; 
    
#endif
    
}


//uint8_t RTE_Get_Y1_SNS(void);
//uint8_t RTE_Get_Y2_SNS(void);
//uint8_t RTE_Get_Y3_SNS(void);
//uint8_t RTE_Get_Y4_SNS(void);
//uint8_t RTE_Get_Y5_SNS(void);
uint8_t RTE_Get_Y_SNS(void)
{
#if 0
    uint8_t count;
    uint16_t sum_y=0;
    uint8 y_pos;
	GetSurface_Position(NULL_PTR, &y_pos);
    Y_Buf[Y_CNT++]=y_pos;
    if(Y_CNT==Y_Length)
    {
        Y_CNT=0;
    }
    for(count=0;count<Y_Length;count++)
    {
        sum_y+=(uint16_t)Y_Buf[count];
    }
   if(y_pos==0)
    {
       sum_y=0;
    }
    return (uint8_t)(sum_y/Y_Length);
#endif
 
#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(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;
	}	
    return XY_Sts;
}

uint8_t RTE_Get_TouchBoard_XY_Touch_And_Below_1N(void)   // 0 false   1 true
{
    uint8_t ret_val = 0;
	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;
}


uint8_t RTE_Get_Pad_XY_trig(void)
{
  Buttons_SignalType XY_Sts = BUTTON_RELEASE;
    XY_Sts =  TouchSurface_is_TouchActive();
    return XY_Sts;
}
uint8_t RTE_Get_Voice_XY_trig(void)
{
    Buttons_SignalType Signal;
	Signal=k_voice_touch_Sts;
    
    return Signal;

}
uint8_t RTE_Get_Menu_XY_trig(void)
{
    Buttons_SignalType Signal;
	Signal = TouchButton_is_TouchActive(BTN_MENU1);	
    
    return Signal;	
}
uint8_t RTE_Get_Vol_XY_trig(void)
{
    Buttons_SignalType Signal;
	Signal = TouchButton_is_TouchActive(BTN_VOL1);	
	
	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(local_percent != Percent)
	{
		local_percent = Percent;
		// temp = Percent * 0xff / 100;
		temp = (7999/100) * Percent/1000;
		//IoHw_PWM3_Setter(PWM_CHANNEL_CHANNEL0, temp);
        (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)
{
    InitMessage(SIG_RIMFCTACTSGCE, &Value);
}

void RTE_Set_CAN_RiMFctActSgDn(uint8_t Value)
{
    InitMessage(SIG_RIMFCTACTSGDN, &Value);
}

void RTE_Set_CAN_RiMFctActSgLe(uint8_t Value)
{
    InitMessage(SIG_RIMFCTACTSGLE, &Value);
}

void RTE_Set_CAN_RiMFctActSgRi(uint8_t Value)
{
    InitMessage(SIG_RIMFCTACTSGRI, &Value);
}

void RTE_Set_CAN_RiMFctActSgUp(uint8_t Value)
{
    InitMessage(SIG_RIMFCTACTSGUP, &Value);
}

void RTE_Set_CAN_SteerWhlTouchBdVoice(uint8_t Value)
{
    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)
{
    InitMessage(SIG_STEERWHLTOUCHBDMENU, &Value);
    
}



void RTE_Set_CAN_SteerWhlTouchBdDn(uint8_t Value)
{
    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)
{
    
   
    //Vibra_force_level=Vibra_PressCheck();
   // if((Vibra_force_level == 2 && last_Vibra_force_level != 2) ||
   // (Vibra_force_level != 1 && last_Vibra_force_level == 1))
   // {
   //     speeker_Tig_Once(49, SA51024_STRENGTH_LEVEL2);
   // }
   // g_last_SteerWhlTouchBdSts = Value;  
    //last_Vibra_force_level=Vibra_force_level; 	
    InitMessage(SIG_STEERWHLTOUCHBDRISTEERWHLTOUCHBDSTS, &Value);
    //TouchBoard_Sts=Value;
}


void RTE_Set_CAN_SteerWhlTouchBdRiTouchPosnX(uint8_t Value)
{
    InitMessage(SIG_STEERWHLTOUCHBDRITOUCHPOSNX, &Value);
   // TouchBoard_X=Value;
}
void RTE_Set_CAN_SteerWhlTouchBdRiTouchPosnY(uint8_t Value)
{
    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)
{
  uint16_t Press_baseline;
  uint16_t Press_signal;
  Press_baseline=Get_forcedetect_basline_value();
  Press_signal=Get_forcedetect_force_value();
  InitMessage(SIG_SWTRPRESSBASELINE, &Press_baseline);//Press_baseline
  InitMessage(SIG_SWTRPRESSSIGNAL, &Press_signal);//Press_signal-Convert to N: TouchPanel_PressNValue = ((float32)Press_signal / (float32)0xF2);
  
  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;
  
  
  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;
  
  //uint8_t Touch_Sensor_Sts13;
#if 1
  Touch_Sensor_Sts00 = abs(get_sensor_node_signal(0U)-get_sensor_node_reference(0U));
  Touch_Sensor_Sts01 = abs(get_sensor_node_signal(1U)-get_sensor_node_reference(1U));
  Touch_Sensor_Sts02= abs(get_sensor_node_signal(2U)-get_sensor_node_reference(2U));
  Touch_Sensor_Sts03= abs(get_sensor_node_signal(3U)-get_sensor_node_reference(3U));
  Touch_Sensor_Sts04= abs(get_sensor_node_signal(4U)-get_sensor_node_reference(4U));
  Touch_Sensor_Sts05= abs(get_sensor_node_signal(5U)-get_sensor_node_reference(5U));
  Touch_Sensor_Sts06= abs(get_sensor_node_signal(6U)-get_sensor_node_reference(6U));
  Touch_Sensor_Sts07= abs(get_sensor_node_signal(7U)-get_sensor_node_reference(7U));
  Touch_Sensor_Sts08= abs(get_sensor_node_signal(8U)-get_sensor_node_reference(8U));
  Touch_Sensor_Sts09=abs(get_sensor_node_signal(9U)-get_sensor_node_reference(9U));
  Touch_Sensor_Sts10= abs(get_sensor_node_signal(10U)-get_sensor_node_reference(10U));
  Touch_Sensor_Sts11= abs(get_sensor_node_signal(11U)-get_sensor_node_reference(11U));
  Touch_Sensor_Sts12= abs(get_sensor_node_signal(12U)-get_sensor_node_reference(12U));
  Touch_Sensor_Sts11= abs(get_sensor_node_signal(13U)-get_sensor_node_reference(13U));
  Touch_Sensor_Sts12= abs(get_sensor_node_signal(14U)-get_sensor_node_reference(14U));
#endif
 
#if 0
  
  Touch_Sensor_Sts00 = get_scroller_position(0x00);
  
  
#endif
  
  
  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_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_baseline;
   
   InitMessage(SIG_SWTRSENSOR16_SIGNAL, &g_XY_Touch_And_Below_1N);


}

#endif