#include "main.h"
#include "Task.h"
#include "TYW_stdint.h"
#include "GenDelay.h"
#include "COM_CAN.h"
#include "RTE_GPIO.h"
#include "DoCAN_ISO15765.h"
#include "Flash_synchronizer.h"
#include "RTE_ADC.h"
#include "init.h"
#include "TFT_LCD.h"
#include "TM035HFZGZx.h"
#include "UDS_ISO14229_Server.h"
#include "CAN_Lib.h"
#include "Display_Info.h"
#include "Key.h"
#include "Check_Ctrl.h"
#include "FreIn_User.h"
#include "../UART/UART.h"
#include "string.h"
#include "api_RS485.h"
#include "RTE_WDT.h"
#include "R485_Communication_Matrix.h"
#include "Analog_Signals.h"

/*******************************************************************************
 *                                                                              *
 *   ϵͳ��������ģʽ(SYS_MODE_OFF / SYS_MODE_ON / SYS_MODE_STANDBY )�����б�   *
 *                                                                              *
 *******************************************************************************/

/*==============================================================================
αʵʱ����
------------------------------------------------------------------------------*/
uint8_t time100ms = 0;
uint8_t finish = 0;
uint8_t AutoFlag = 0;
uint32_t send0x42time = 0;
uint32_t bleOFFtoONtime = 0;
uint8_t startONfalg = 0;
uint32_t startIGONtime = 0;
uint8_t compareresult;
const uint16_t TurnRL_Volate []              = {25,15,5};
extern uint8_t keyopenble;
uint8_t buzzerflag = 0;
const unsigned char gImage_cuo1[] = {24, 24,0X00,0X00,0X00,0X00,0X30,0X70,0XE0,0XC0,0X80,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,0XC0,0X70,0X30,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X83,0XC7,0X66,0X38,0X38,0X7C,0XE6,0XC3,0X81,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X0C,0X0C,0X06,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X03,0X07,0X0E,0X0C,0X00,0X00,0X00,0X00,};
const unsigned char gImage_dui1[] = {24, 24,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X80,0XC0,0XE0,0X40,0X00,0X00,0X00,0X00,0X08,0X18,0X30,0X70,0XC0,0X80,0X00,0X00,0X80,0XC0,0XE0,0X70,0X38,0X1C,0X0E,0X07,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X01,0X03,0X07,0X03,0X01,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,0X00,};
//  nowAD1 = 0;
// uint16_t nowAD2 = 0;
uint8_t leftTurnLightVoltageOK = 0;
uint8_t rightTurnLightVoltageOK = 0;
uint8_t changeZeroFlag = 0;
void Check_TurnLights_Voltage_Service(void)
{
    static uint32_t voltageCheckTimer = 0;
    static uint16_t processedLeftVoltage = 0;
    static uint16_t processedRightVoltage = 0;
    // Can_Write_Fun(2);    //报文外发
    // COM_RX_Process( );

    uint16_t currentRightVoltage = (uint16_t)ADC_Read_Signal(ADC_CH_KL30_VOLTAGE);

    if(currentRightVoltage < 1000)
    {
        currentRightVoltage = 0;
    }
    uint16_t currentLeftVoltage = (uint16_t)ADC_Read_Signal(ADC_CH_10_VOLTAGE);

    if(currentLeftVoltage < 1000)
    {
        currentLeftVoltage = 0;
    }
        processedLeftVoltage = currentLeftVoltage;
        processedLeftVoltage/=100;
        if(MENU_CHECK_STEP == 0)
        {
            leftTurnLightVoltageOK = 0;
            rightTurnLightVoltageOK = 0;
        }
        processedRightVoltage = currentRightVoltage;
        processedRightVoltage/=100;
        if(voltageCheckTimer < 100)
        {
            voltageCheckTimer++;
        }
        else
        {
            voltageCheckTimer = 0;
            if(MENU_CHECK_STEP == 2 || MENU_CHECK_STEP == 11)
            {
                if(processedLeftVoltage >= 80 && processedLeftVoltage <= 160 && leftTurnLightVoltageOK == 0)
                {
                    leftTurnLightVoltageOK = 1;
                }
                else if(processedLeftVoltage  == 0 && leftTurnLightVoltageOK == 1)
                {
                    leftTurnLightVoltageOK = 2;
                }
                else if(leftTurnLightVoltageOK == 2 && (processedLeftVoltage >= 80 && processedLeftVoltage <= 160))
                {
                    leftTurnLightVoltageOK = 3;
                }
                GUI_General_Digit_Display(processedLeftVoltage, Num_15, 3, 2, TurnRL_Volate, 40+25+25+25+25+25+25+25+30);
            }
            if(MENU_CHECK_STEP == 3 || MENU_CHECK_STEP == 11)
            {
                if(processedRightVoltage >= 80 && processedRightVoltage <=160 && rightTurnLightVoltageOK == 0)
                {
                    rightTurnLightVoltageOK = 1;
                }
                else if(processedRightVoltage  == 0 && rightTurnLightVoltageOK == 1)
                {
                    rightTurnLightVoltageOK = 2;
                }
                else if(rightTurnLightVoltageOK == 2 && (processedRightVoltage >= 80 && processedRightVoltage <= 160))
                {
                    rightTurnLightVoltageOK = 3;
                }
                GUI_General_Digit_Display(processedRightVoltage, Num_15, 3, 2, TurnRL_Volate, 40+25+25+25+25+25+25+25+25+30);
            }
            
            
        }
        if(MENU_CHECK_STEP > 2 && leftTurnLightVoltageOK != 3)
        {
            MENU_CHECK_STEP = 2;
        }
        else if(MENU_CHECK_STEP > 3 && rightTurnLightVoltageOK != 3)
        {
            MENU_CHECK_STEP = 3;
        }
        else
        {
            ;
        }
}
















void Sys_Run_Mode_Pseudo_Real_Time_Tasks(void)
{    
    SD_FMQ = 1;
    DoCAN_Communication_Service( );
    if ( CAN_Msg_Tx_Enable != 0 )
    {
        UDS_Server_Application_Service( );
    }
    
    TFT_LCD_Display_Update_Service( );
    Fre_In_Service(FRE_VEHICLE);
    RTE_ADC_Services( );
    // MENU_CHECK_STEP = 11;
    //Fre_In_Service(FRE_ENGINE);
    // LINE_FUEL_RES03 = 1;

}
/*============================================================================*/

/*==============================================================================
2ms����ִ������ ��0��
------------------------------------------------------------------------------*/


void Sys_Run_Mode_2ms_Tasks_Group(void)
{
    
    

    // }
    Check_TurnLights_Voltage_Service();
        Get_static_Current();
}

/*==============================================================================
2ms����ִ������ ��0��
------------------------------------------------------------------------------*/
void Sys_Run_Mode_5ms_Tasks_Group(void)
{

}

/*==============================================================================
10ms����ִ������ ��0��
------------------------------------------------------------------------------*/
// uint8_t testdata[3] = {0,1,2};
uint8_t Openble[20] = {0};
uint32_t clearodotime;
uint32_t devicename = 0;
uint8_t sartigon = 0;
uint8_t sendflag = 0;
void Sys_Run_Mode_10ms_Tasks_Group(void)
{
    // BusOff_Service( );
    Key_Service( );
    if(devicename<0xFFFF)
    {
        devicename++;
    }
    else
    {
        devicename = 0;
    }
    if(firstpowerflag == 2)
    {
        if(clearOdoFlag == 0)
        {
         backsend();
            // RS485_TX_finish = 0;
            if(RS485_TX_finish == 0)
            {
                LINE_OUT_NEG_09 = 1;
                if(LINE_OUT_NEG_09 == 1)
                {
                    if(connectbleFlag == 1)
                    {
                        MENU_CHECK_STEP = 16;
                    }
                    // if(connectbleFlag == 0)
                    // {
                        if(zhenduanflag == 0)
                        {
                            // R485_Send_Line_0x47(MENU_CHECK_STEP);
                            RS485_User_Tx_Data();
                            comparestart = 0;
                        }
                        else if(zhenduanflag == 1)
                        {
                            if(writeflag == 1 || writeflag ==3)
                            {
                                R485_Send_0x47();
                            }
                            else
                            {
                                R485_Send_0x80();
                            }
                            

                            
                            comparestart = 0;
                        }
                        else if(zhenduanflag == 2)
                        {
                            R485_Sendcheck_0x47();
                        }
                        else
                        {
                            ;
                        }
                    // }
                    // else
                    // { 
                    //     if(RS485_TX_finish == 0)
                    //     {
                            
                    //         if(startONfalg == 0)
                    //         {
                    //              if(send0x42time<130)
                    //             {
                    //                 // if(sendflag == 1)
                    //                 // {
                    //                     R485_Send_0x42();
                    //                 //     sendflag = 0;
                    //                 // }
                                    

                                    
                    //                 // queren = 0;
                    //             }
                    //             else
                    //             {
                    //                 // bleOFFtoONtime = 0;
                    //                 LINE_OUT_NEG_01 = 0;
                    //                 LINE_OUT_NEG_02 = 0;
                    //                 LINE_OUT_NEG_03 = 0;
                    //                 LINE_OUT_NEG_04 = 0; 
                    //                 startONfalg = 1;
                    //                 Display_TFT_Clear();
                    //             }
                    //         }
                    //         else
                    //         {
                    //         if(startIGONtime  >= 50)
                    //         {
                    //             LINE_OUT_NEG_01 = 1;
                    //             LINE_OUT_NEG_02 = 1;
                    //             LINE_OUT_NEG_03 = 1;
                    //             LINE_OUT_NEG_04 = 1; 
                    //             if(startIGONtime >= 100)
                    //             {
                    //                 Openble[0] = 0x59;
                    //                 Openble[1] = 0x44;
                    //                 Openble[2] = 0xA;
                    //                 Openble[3] = 0x8A;
                    //                 Openble[4] = 0xA;
                    //                 Openble[5] = 0xA;
                    //                 Openble[6] = name2;
                    //                 Openble[7] = name1;
                    //                 Openble[8] = 1;
                                    
                    //                 Openble[9] = btmac[0];
                    //                 Openble[10] = btmac[1];
                    //                 Openble[11] = btmac[2];
                    //                 Openble[12] = btmac[3];
                    //                 Openble[13] = btmac[4];
                    //                 Openble[14] = btmac[5];

                    //                 Openble[15] = 0x4B;
                    //                 Openble[16] = 0x4A;
                                    
                    //                     // RS485_User_Tx_Data();
                    //                 if(sendflag == 1)
                    //                 {
                    //                     R485_Send_0x41();
                    //                     sendflag = 0;
                    //                 }
                    //                 else
                    //                 {
                    //                     R485_Send_0x80();
                    //                     sendflag = 1;
                    //                 }
                    //                 blename[4] = 0xFF;
                    //                 // if(send0x42time % 20 == 0)
                    //                 // {
                    //                     UART_Ch2_Send_Multiple_Byte(Openble,sizeof(Openble));
                    //                     // if(startIGONtime > 500)
                    //                     // {
                    //                     //     queren = 1;
                    //                     // }
                                        
                    //                 // }
                    //             }


                    //         }
                    //         }
                    //         RS485_send_time = 0;
                    //         RS485_TX_finish = 1;  
                    //     }
                    //     else
                    //     {

                    //     }
                                 
                    // }
                    
                    
                }
                // else
                // {
                //     RS485_TX_finish = 0;
                // }
                
            }
            else
            {
                // backsend();
                LINE_OUT_NEG_09 = 0;
                if(LINE_OUT_NEG_09 == 0)
                {
                    Recv_Byte();
                }
                
                
                
            }
            
        }
        else
        {
            MENU_CHECK_STEP = 3;
            backsend();
            // RS485_TX_finish = 0;
            // backsend();
             if(RS485_TX_finish == 0)
             {
                LINE_OUT_NEG_09 = 1;
                if(1 == LINE_OUT_NEG_09)
                {
                    RS485_User_Tx_Data();
                }
                
             }
             else
             {
                 LINE_OUT_NEG_09 = 0;
                 if (LINE_OUT_NEG_09 == 0)
                 {
                     Recv_Byte();
                 }
             }
            
            
            if(clearodotime < 3000)
            {
                clearodotime++;
            }
            else
            {
                 clearodotime = 0;
            }
                
            if(clearodotime >= 1500)
            {
                // Port_SetPortPinLevel(2,9,PortGpioHigh);
                // queren = 1;
            }
            else
            {
                queren = 0;
            }
        }
    }
   

}

/*==============================================================================
20ms����ִ������ ��0��
------------------------------------------------------------------------------*/
uint8_t time100mssend = 0;
void Sys_Run_Mode_20ms_Tasks_Group0(void)
{
    
    // uint8_t data[8] = {0};
    // uint32_t SpdN = 0;
    // _CAN_Msg canMag;
    
    // //Total_Check( );
    // canMag.MsgID          = 0x8FFF00B;
    // FT_VehSpeedCalc_Main(FT_CurVehSpeed, &SpdN, SPEED_TO_CANMESSAGE);
    // FT_Fill_VehSpeedCANMessage(SpdN, data);
    // canMag.MsgDLC = 8u;
    // canMag.MsgPro = 1u;
    // memcpy(canMag.Msg, data, 8);
    // Can_Write(&canMag);
    if(time100mssend < 2)
    {
        time100mssend++;
    }
    else
    {
        // firstIGON = 0;
        
        time100mssend = 0;
    } 

}

/*==============================================================================
50ms����ִ������ ��0��
------------------------------------------------------------------------------*/
uint8_t powerstu = 0;
uint32_t timerms = 0;
void Sys_Run_Mode_50ms_Tasks_Group(void)
{
    static uint32_t timer_100ms = 0;
    if(timer_100ms <=1000)
    {
        timer_100ms++;
    }
    if(timerms <=1000)
    {
        timerms++;
    }
    
    
    
    // if ( timer_100ms >= 500 )
    // {
    //     if(powerstu == 0)
    //     {
    //         LINE_OUT_NEG_01 = 1;
    //         LINE_OUT_NEG_02 = 1;
    //         LINE_OUT_NEG_03 = 1;
    //         LINE_OUT_NEG_04 = 1;
    //         powerstu = 1;
    //         timer_100ms = 0;
    //         firstIGON = 1;
    //         timerms = 0;
    //     }
    //     else
    //     {
    //         LINE_OUT_NEG_01 = 0;
    //         LINE_OUT_NEG_02 = 0;
    //         LINE_OUT_NEG_03 = 0;
    //         LINE_OUT_NEG_04 = 0;
    //         powerstu = 0;
    //         timer_100ms = 400;
    //         firstIGON = 1;
    //         timerms = 0;
    //     }
        
    //     // Read_IPK_Session_Type( );//请求仪表发送 软硬件版本号 等数据。  
            
    // }
    if(timerms >=60)
    {
        firstIGON = 0;
    }
    firstIGON = 0;
       
        
}

/*==============================================================================
100ms����ִ������ ��0��
------------------------------------------------------------------------------*/
uint16_t u16LJSText;
uint32_t AutoTime = 160;
uint32_t ONOFFtime;
uint8_t compareresult;
uint8_t flashtime = 0;
uint8_t showtime = 0;
uint8_t bleuarttime = 0;
void Sys_Run_Mode_100ms_Tasks_Group(void)
{
    static uint32_t loc_timer = 0;
    loc_timer++;
    if ( loc_timer >= 3 )
    {
        loc_timer = 0;
        // HW_CHECK_IPK_EXIST( );
        Recv_Byte1();
    }
    Display_PageNum();


    // Freln_Duty_HZ_Service();
    if(RS485_send_time<0xFFFFFFFF)
    {
        RS485_send_time++;
    }
    if(firstpowerflag == 2)
    {
        if(AutoFlag == 1)
        {
            if(AutoTime <= 200)
            {
                AutoTime++;
            }
            else
            {
                MENU_CHECK_STEP_ADD( );   
                if(MENU_CHECK_STEP == 0)
                {
                    MENU_CHECK_STEP = 0;
                }
                if(MENU_CHECK_STEP == 13)
                {
                    AutoTime = 0;
                }
                else
                {
                    AutoTime = 160;
                }
                
            }
            
        }
        if(Auto_ONOFF == 1)
        {
            if(ONOFFtime <=160)
            {
                ONOFFtime++;
            }
            else
            {
                
                if(LINE_OUT_NEG_01 == 1)
                {
                    LINE_OUT_NEG_01 = 0;
                    LINE_OUT_NEG_04 = 0;
                    ONOFFtime = 0;
                }
                else
                {
                    LINE_OUT_NEG_01 = 1;
                    LINE_OUT_NEG_04 = 1;
                    ONOFFtime = 0;
                }
            }
        }
        if(flashtime < 3)
        {
            flashtime++;
        }
        else
        {
            if(comparestart == 1)
            {
                compareresult = GetDataCompare();
                // Display_TFT_Clear();
                if(compareresult == 1)
                {
                    TFT_LCD_Draw_Bmp(200,   90+40,  ( uint8_t * )gImage_cuo1 );
                    // comparestart = 2;
                }
                else
                {
                    TFT_LCD_Draw_Bmp(200,   90+40,  ( uint8_t * )gImage_dui1 );  
                    // comparestart = 2;
                }
                comparestart = 0;
                // Display_TFT_Clear();
            }
            else if(comparestart == 0)
            {
                    Total_Check( );
                    if(zhenduanflag == 3)
                    {
                        if(compareresult == 1)
                        {
                            TFT_LCD_Draw_Bmp(200,   90+40,  ( uint8_t * )gImage_cuo1 );
                            // comparestart = 2;
                        }
                        else
                        {
                            TFT_LCD_Draw_Bmp(200,   90+40,  ( uint8_t * )gImage_dui1 );  
                            // comparestart = 2;
                        }
                        // comparestart = 1;
                    }
            

            }
            else
            {
                Total_Check();
            }
            flashtime = 0;
        }
        // if(recvflag111 == 1)
        // {
        //     blename[4] = 0xff;
        //     General_Number_Disp(blename, 3, 165); 
        //     recvflag111 = 0;
            
        // }
    }

         if(firstpowerflag == 1)
    {
        zhenduanflag = 2;
        Total_Check();
        firstpowerflag = 2;
        Display_TFT_Clear();
        if(compareresult == 1 && comparestart == 0)
        {
        //     Total_Check();
            
            if(compareresult == 1)
            {
                TFT_LCD_Draw_Bmp(200,   90+40,  ( uint8_t * )gImage_cuo1 );
                // comparestart = 2;
            }
            else
            {
                TFT_LCD_Draw_Bmp(200,   90+40,  ( uint8_t * )gImage_dui1 );  
                // comparestart = 2;
            }
            Total_Check();
            firstpowerflag = 3;
            // zhendlag = 5;
        }
         
    }
    else
    {
        if(compareresult == 1)
        {
            Total_Check();
            firstpowerflag = 3;
             zhenduanflag = 2;
            if(compareresult == 1)
            {
                TFT_LCD_Draw_Bmp(200,   90+40,  ( uint8_t * )gImage_cuo1 );
                // comparestart = 2;
            }
            else
            {
                TFT_LCD_Draw_Bmp(200,   90+40,  ( uint8_t * )gImage_dui1 );  
                // comparestart = 2;
            }
            // Total_Check();
            // zhenduanflag = 5;
        }
        // zhenduanflag = 2;
    }   
    //u16LJSText = TFT_LCD_REQ_FIFO_MAX_DEPTH;
}
/*=================================================================*/

/*******************************************************************************
 *                                                                              *
 *                           ��ȷ��50us��ʱ�����б�                             *
 *                                                                              *
 *******************************************************************************/
/*==============================================================================
��ȷ��50us��ʱ���� ��ֹ�����޸��б��е�����
------------------------------------------------------------------------------*/
void Sys_Exact_50us_Tasks(void)
{
    GenDelay_Tick( );
    DoCAN_Timer_Update(50u);
}
/*============================================================================*/

/*******************************************************************************
 *                                                                              *
 *                           ��ȷ��100ms��ʱ�����б�                            *
 *                                                                              *
 *******************************************************************************/
/*==============================================================================
��ȷ��100ms��ʱ���� ��ֹ�����޸��б��е�����
------------------------------------------------------------------------------*/
uint32_t clearodotimeflash = 0;
void Sys_Exact_100ms_Tasks(void)
{
    if(connectbleFlag == 1)
    {
        if(send0x42time<0xFFFFFFFF)
        {
            send0x42time++;
        }
        if(bleOFFtoONtime<0xFFFFFFFF)
        {
            bleOFFtoONtime++;
        }
    }
    if(startONfalg)
    {
        if(startIGONtime < 0xFFFFFFFF)
        {
            startIGONtime++;
            if(startIGONtime % 20 == 0)
            {
                if(R485_0x80_msg[11] < 5)
                {
                    R485_0x80_msg[11]++;
                }
                else
                {
                    R485_0x80_msg[11] = 0;
                }
                
            }
        }
        else

        {
            startIGONtime = 0;
        }
    }
    if (clearodotimeflash < 0xFFFFFFFF)
    {
        clearodotimeflash++;
        if (clearodotimeflash % 20 == 0)
        {
            if (R485_0x80_msg[11] < 5)
            {
                R485_0x80_msg[11]++;
                R485_0x80_msg[10]++; 
            }
            else
            {
                R485_0x80_msg[11] = 0;
                R485_0x80_msg[10] = 0; 
            }
        }
        // CHECK_IPK_COUNT( );
    }
    else
    {
        clearodotimeflash = 0;
    }
}
/*============================================================================*/
void Sys_Enter_Sleep_Mode(void)
{
    // Sys_Sleep_Init( );
}
