#include "AMT630H.h"
#include <stdint.h>
#include "UartProtocol.h"
#include "Components.h"
#include "PicBin.h"
#include "GpioUser.h"
//#include "AMT630H_Datas.h"
#define AMT630H_ERR_TIME            10000U
#define AMT630H_RELEASE_MODE        1U
unsigned int AMT630H_InitTime = 0;
uint8_t g_sysFlag_Lcdcheck = 0;
/* Initialization */
LCD_TFT_SOUND_PARA_ST LcdSoundParamterData;
SOFTWARE_VERSION_ST amt630Version;

uint8_t Amt630hInitFinishFlag = 0;

uint32_t Amt630hInitTimerCnts = 0;
uint32_t Amt630hSequentialTimerCnts = 0;
uint32_t Amt630hFlashCheckTimerCnts = 0;
uint32_t Amt630hServiceTimerCnts = 0;


/* SOC Error */
uint16_t AMT630H_REQ_Time[ALIVE_COUNTER_TOTAL] = { 0 };
uint8_t AMT630H_Err = 0U;
uint8_t AMT630H_AliveCounter[ALIVE_COUNTER_TOTAL] = { 0 };
uint16_t AMT630H_CrcErrTime = 0;
uint32_t AMT630H_CrcErrCounter = 0;
uint8_t AMT630H_CrcErrMaxCounter = 0;

/* Flash Check */
#define  FLASH_CHECK_TIME_NUM       20000
s_AMT630H_FlashCheck_t AMT630H_FlashCheck;

/* Delay Time */
unsigned long Delay_Time = 0;
unsigned long Delay_Time_Set = 0;

/* Refresh Time */
unsigned long AMT630H_RefreshTime = 0;
static uint8_t AMT630H_Animation_Start = 0;
static uint8_t AMT630H_Animation_Finish = 0;
static uint16_t AMT630H_Animation_DispPic = 0;
static uint16_t AMT630H_Animation_RefreshTime = 0;
static uint16_t AMT630H_Animation_RefreshTime_default = 0;
static const uint16_t BootAnimationList[AMT630H_ANIMATION_PIC_NUM] = {
    0,
    // Pic_0001_0_0,Pic_0758_0_0,Pic_0759_0_0,Pic_0760_0_0,Pic_0761_0_0,Pic_0762_0_0,Pic_0763_0_0,
    // Pic_0764_0_0,Pic_0765_0_0,Pic_0766_0_0,Pic_0767_0_0,Pic_0768_0_0,Pic_0769_0_0,Pic_0770_0_0,
    // Pic_0771_0_0,Pic_0772_0_0,Pic_0773_0_0,Pic_0774_0_0,Pic_0775_0_0,Pic_0776_0_0, Pic_0777_0_0,
    //Pic_0778_0_0,Pic_0779_0_0,Pic_0780_0_0,Pic_0781_0_0,Pic_0782_0_0,Pic_0783_0_0,Pic_0784_0_0,
    // Pic_0785_0_0,Pic_0786_0_0,Pic_0787_0_0,Pic_0788_0_0,Pic_0789_0_0,Pic_0790_0_0,Pic_0791_0_0,
    //Pic_0792_0_0,Pic_0793_0_0,Pic_0794_0_0,Pic_0795_0_0,Pic_0796_0_0,Pic_0797_0_0,Pic_0798_0_0,
    // Pic_0799_0_0,Pic_0800_0_0,Pic_0801_0_0,Pic_0802_0_0,Pic_0803_0_0, Pic_0804_0_0,Pic_0805_0_0,
    // Pic_0806_0_0,Pic_0807_0_0,Pic_0808_0_0,Pic_0809_0_0,Pic_0810_0_0,Pic_0811_0_0,Pic_0812_0_0,
    // Pic_0813_0_0,Pic_0814_0_0,Pic_0815_0_0,Pic_0816_0_0,Pic_0817_0_0,Pic_0818_0_0, Pic_0819_0_0,
    // Pic_0820_0_0,Pic_0821_0_0,Pic_0822_0_0,Pic_0823_0_0,Pic_0824_0_0,Pic_0825_0_0,Pic_0826_0_0,
    // Pic_0827_0_0,Pic_0828_0_0, Pic_0829_0_0,Pic_0830_0_0,Pic_0831_0_0, Pic_0832_0_0,Pic_0833_0_0,
    // Pic_0834_0_0,Pic_0835_0_0,Pic_0836_0_0, Pic_0837_0_0,Pic_0838_0_0,Pic_0839_0_0,Pic_0840_0_0,
    // Pic_0841_0_0,Pic_0842_0_0,Pic_0843_0_0,Pic_0844_0_0,Pic_0845_0_0,Pic_0846_0_0,Pic_0847_0_0,
    // Pic_0848_0_0,Pic_0849_0_0,Pic_0850_0_0,Pic_0851_0_0,Pic_0852_0_0,Pic_0853_0_0,Pic_0854_0_0,
    // Pic_0855_0_0,Pic_0856_0_0,Pic_0857_0_0,Pic_0858_0_0, Pic_0859_0_0,Pic_0859_0_0,Pic_0859_0_0,
    // Pic_0859_0_0,Pic_0860_0_0, Pic_0860_0_0,Pic_0860_0_0,Pic_0860_0_0,
    //psd已更新开机动画，fullbin等待odo trip补齐再开放
};
void AMT630H_Animation_PlayProc(uint16_t PicIndex)
{
    unsigned char *pBuf;
    uint32_t tmpLen = 0;
    SetPageGuid(GRAPHICS_PAGE_0, 1, 0, 0); 
    SetPageBackground(GRAPHICS_PAGE_0, PicIndex);
    PackedTransfer_Page();
  //  PackedTransfer_GetSwVer();

}
/* Timer */
void AMT630H_Timer(unsigned char Tms)
{
    unsigned char i = 0U;

    for(i = 0; i < ALIVE_COUNTER_TOTAL; i++)
    {
        if(AMT630H_REQ_Time[i] < AMT630H_ERR_TIME)
        {
            AMT630H_REQ_Time[i] += Tms;
        }
    }
    if(AMT630H_RefreshTime < AMT630H_REFRESH_TIME)
    {
        AMT630H_RefreshTime += Tms;
    }
    if(Delay_Time < Delay_Time_Set)
    {
        Delay_Time += Tms;
    }
    if(AMT630H_InitTime < 5000)
    {
        AMT630H_InitTime += Tms;
    }
    if(AMT630H_Animation_Finish == 0)
    {
        if(AMT630H_Animation_RefreshTime < AMT630H_ANIMATION_TIME)
        {
            AMT630H_Animation_RefreshTime += Tms;
        }
    }
    else
    {
        AMT630H_Animation_RefreshTime = 0;
    }
}
void Delay_Time_Ctrl(unsigned long Tms)
{
    Delay_Time = 0;
    Delay_Time_Set = Tms;

    while(Delay_Time < Delay_Time_Set)
    {
        RTE_WDT_Clear();
    }

    Delay_Time = 0;
    Delay_Time_Set = 0;
}

uint8_t UartRevBuff[0x1000] = { 0 };
uint32_t UartRevCnts = 0;
void Amt630hUartRecvData(uint16_t u16UARTData)
{
    UartRevBuff[UartRevCnts++] = (u16UARTData & 0xff);
    if (UartRevCnts >= 0x1000) 
    {
        UartRevCnts = 0;
    }
}
void AMT630H_RstCtrl(void)
{
    RTE_GPIO_Set_Level(VCC_33_EN_MCU, 0);//630
    RTE_GPIO_Set_Level(VCC1V2_AMT_EN, 0);//630
    RTE_GPIO_Set_Level(STBYB_LCD_MCU, 0);//lcd
    RTE_GPIO_Set_Level(MCU_RSTB_LCD, 0);//lcd
    Delay_Time_Ctrl(10);
    RTE_GPIO_Set_Level(VCC1V2_AMT_EN, 1);   
    Delay_Time_Ctrl(15);    
    RTE_GPIO_Set_Level(VCC_33_EN_MCU, 1);
    Delay_Time_Ctrl(30);
    RTE_GPIO_Set_Level(x1530_EN_MCU, 1);//1530
    Delay_Time_Ctrl(10);
    RTE_GPIO_Set_Level(MCU_RSTB_LCD, 1); 
    Delay_Time_Ctrl(15); 
    RTE_GPIO_Set_Level(STBYB_LCD_MCU, 1); 
   
}
void Amt630hInit(void)
{
    uint8_t i = 0;
    AMT630H_RstCtrl();
    AMT630H_RefreshTime = AMT630H_REFRESH_TIME;
    // Error State Init
    AMT630H_Err = 0U;
    for(i = 0; i < ALIVE_COUNTER_TOTAL; i++)
    {
        AMT630H_REQ_Time[i] = 0U;
        AMT630H_AliveCounter[i] = 0;
    }
    AMT630H_CrcErrTime = 0;
    AMT630H_CrcErrCounter = 0;

    // Param Config
    LcdSoundParamterData.LcdWidth = 1024;
    LcdSoundParamterData.LcdHeight = 600;
    LcdSoundParamterData.LcdBpp = 32;
    LcdSoundParamterData.LcdVideoInputValid = 0;
    LcdSoundParamterData.Ark7116ResetPin = 0;
    LcdSoundParamterData.LcdInterfaceType = LCD_INTERFACE_LVDS;
    LcdSoundParamterData.LcdWiringMode = LCD_WIRING_MODE_BGR;
    LcdSoundParamterData.LcdWiringBitOrder = LCD_WIRING_BIT_ORDER_MSB;
    LcdSoundParamterData.LcdDotClkReverse = 0;  /* NuoMa 50pin TFT */
    LcdSoundParamterData.LvdsPanelFormat = LVDS_PANEL_FORMAT_TI;
    LcdSoundParamterData.LvdsPanelData = LVDS_PANEL_DATA_8BIT;

    LcdSoundParamterData.LcdTimingVBP = 12;
    LcdSoundParamterData.LcdTimingVFP = 20;
    LcdSoundParamterData.LcdTimingVSW = 4;

    LcdSoundParamterData.LcdTimingHBP = 160;
    LcdSoundParamterData.LcdTimingHFP = 160;
    LcdSoundParamterData.LcdTimingHSW = 24;

    LcdSoundParamterData.LcdClkFreq = 51200000;

    LcdSoundParamterData.SoundChannelNum = 0;
    LcdSoundParamterData.SoundFreq = AUDIO_FREQ_48K;
    SetLcdSoundParameter(&LcdSoundParamterData);
    SetSwVerBuf((uint8_t*)&amt630Version);

    User_PicIndexDataInit();
    ResetInitedState();    

    AnalysisUartData();

    AMT630H_InitTime = 0;

    AMT630H_Animation_Start = 0;
    AMT630H_Animation_Finish = 0;
    AMT630H_Animation_DispPic = 0;
    AMT630H_Animation_RefreshTime = 0;
    AMT630H_Animation_RefreshTime_default = AMT630H_ANIMATION_REFRESHTIME_DEF(0);

    Amt630hInitFinishFlag = 0;

    AMT630H_FlashCheck.Flag = 0;
    AMT630H_FlashCheck.InquireTime = 0;
    AMT630H_FlashCheck.CheckStateTime = 0;
    AMT630H_FlashCheck.Result = 0;
}
void AMT630H_Sleep(void)
{
    RTE_GPIO_Set_Level(STBYB_LCD_MCU, 0);    
    Delay_Time_Ctrl(30);    
    RTE_GPIO_Set_Level(VCC_33_EN_MCU, 0);
    RTE_GPIO_Set_Level(VCC1V2_AMT_EN, 0);
    RTE_GPIO_Set_Level(MCU_RSTB_LCD, 0);
    
}
static void AMT630H_AliveCounterMonitor(void)
{
    uint8_t i = 0;
    if(AMT630H_CrcErrCounter != GetCrcErrCnts())
    {
        AMT630H_CrcErrCounter = GetCrcErrCnts();
        AMT630H_CrcErrTime++;
        if(AMT630H_CrcErrTime >= 120)
        {
            AMT630H_Err = 1;
        }
        for(i = 0; i < ALIVE_COUNTER_TOTAL; i++)
        {
            AMT630H_REQ_Time[i] = 0;
        }
    }
    for(i = 0; i < ALIVE_COUNTER_TOTAL; i++)
    {
        if(AMT630H_AliveCounter[i] != GetAliveCounter(i))
        {
            AMT630H_AliveCounter[i] = GetAliveCounter(i);
            AMT630H_REQ_Time[i] = 0;
            if(AMT630H_CrcErrMaxCounter < AMT630H_CrcErrTime)
            {
                AMT630H_CrcErrMaxCounter = (unsigned char)AMT630H_CrcErrTime;
            }
            AMT630H_CrcErrTime = 0;
        }
        if(AMT630H_REQ_Time[i] >= AMT630H_ERR_TIME)
        {
            AMT630H_Err = 1;
        }
    }
}

void AMT630H_Main(void)
{
    unsigned char i;
    AnalysisUartData();
        
    if(GetLcdInitSt() != 1)
    {
        for(i = 0; i < ALIVE_COUNTER_TOTAL; i++)
        {
            AMT630H_REQ_Time[i] = 0U;           
        }
        return;
    }
    AMT630H_AliveCounterMonitor();
    if(AMT630H_Err == 1U)
    {
        AMT630H_Err = 0;
        Amt630hInit();
        AMT630H_Animation_Finish = 1;
        return;
    }
    // if(g_sysFlag_Lcdcheck == 1)
    // {
    //     if(AMT630H_FlashCheck.Flag == 0)
    //     {
    //         AMT630H_FlashCheck.Flag = 1;
    //         AMT630H_FlashCheck.InquireTime = 0;
    //         AMT630H_FlashCheck.CheckStateTime = 0;
    //         PackedTransfer_CheckFlash();           
    //         AMT630H_FlashCheck.Result = 0xFF;
    //     }
    //     if(AMT630H_FlashCheck.Flag == 1)
    //     {
    //         if(AMT630H_FlashCheck.InquireTime >= 100)
    //         {
    //             AMT630H_FlashCheck.InquireTime = 0;
    //             AMT630H_FlashCheck.Result = PackedTransfer_CheckFlashGetResult();
    //             if(AMT630H_FlashCheck.Result != 0xFF)
    //             {
    //                 if(AMT630H_FlashCheck.Result == 1)
    //                 {
    //                     PackedTransfer_CheckFlashForceDisplay(DISPLAY_CHECK_PASS, 3000);
    //                     AMT630H_FlashCheck.Flag = 2;
    //                 }
    //                 else if (AMT630H_FlashCheck.CheckStateTime >= FLASH_CHECK_TIME_NUM)
    //                 {
    //                     PackedTransfer_CheckFlashForceDisplay(DISPLAY_CHECK_FAIL, 60000);
    //                     AMT630H_FlashCheck.Flag = 2;
    //                 }
    //             }
                
    //         }
    //         else
    //         {
    //             PackedTransfer_CheckFlashForceDisplay(DISPLAY_CHECKING, 60000);
    //         }
    //     }
    // }
    if (SYS_OPR_STAT_IGN_ON)
    {
        if(1)
        {
            AMT630H_Animation_RefreshTime = 0;
            if(AMT630H_RefreshTime >= AMT630H_REFRESH_TIME)
            {                
                AMT630H_RefreshTime = 0;
                AMT630H_GUI_DisplayProc();
                //SetPageGuid(GRAPHICS_PAGE_0, 1, 0, 0);
                //SetPageBackground( GRAPHICS_PAGE_0, Pic_0001_0_0 );
                //SetPagePic(GRAPHICS_PAGE_0, Pic_0002_751_440);
                //SetPagePic(GRAPHICS_PAGE_0, Pic_0003_681_441);
                //SetPagePic(GRAPHICS_PAGE_0, Pic_0005_521_444);
                //SetPagePic(GRAPHICS_PAGE_0, Pic_0010_90_443);
                //SetPagePic(GRAPHICS_PAGE_0, Pic_0012_0_95);
                //PackedTransfer_Page();
            }
        }
        else
        {
            // AMT630H_RefreshTime = 0;
            // if(AMT630H_Animation_Start == 0)
            // {
            //     AMT630H_Animation_Start = 1;                
            //     AMT630H_Animation_RefreshTime_default = AMT630H_ANIMATION_REFRESHTIME_DEF(0);
            // }
            // if(AMT630H_Animation_RefreshTime >= AMT630H_Animation_RefreshTime_default)
            // {
            //     AMT630H_Animation_RefreshTime = 0;
            //     AMT630H_Animation_PlayProc(BootAnimationList[AMT630H_Animation_DispPic]);
            //     AMT630H_Animation_DispPic++;
            //     if(AMT630H_Animation_DispPic >= AMT630H_ANIMATION_PIC_NUM)
            //     {
            //         AMT630H_Animation_Finish = 1;
            //     }
            // }
        } 
        

    }
    else
    {
        AMT630H_Animation_RefreshTime = 0;
        if(AMT630H_RefreshTime >= AMT630H_REFRESH_TIME)
        {
            AMT630H_RefreshTime = 0;
            //AMT630H_GUI_DisplayProc();
        }
    }
}

unsigned char AMT630H_isInitFinish(void)
{
    if((AMT630H_InitTime >= 600) || (GetLcdInitSt() == 1))
    {
        return 1;
    }
    else
    {
        return 0;
    }
}