
#include "Popups.h"
#include "Menus.h"

uint8_t   PopupSeatBeltDrReq;
uint8_t   PopupSeatBeltPaReq;

uint8_t   PopupESCStatBackup;
uint8_t   PopupHDCStatBackup;

uint8_t hyq_test;
uint8_t hyq_test1;

uint8_t   CAN_ERROR;
void Popups_Management_Service(void)
{
  uint8_t PopupMsg;
  uint8_t PopupSnd;
  
  IPC_LCD_ON_REQ_POPUPS = 0;
    
  
  Popups_Polling();
  
  hyq_test1 =  Popup_Get_Current_Sound();
  hyq_test = Popup_Get_Current_Message();  
  
  PopupMsg = Popup_Get_Current_Message();    //图片
  PopupSnd = Popup_Get_Current_Sound();      //声音

  if ((PopupMsg < POPUP_MSG_NONE))    /////////////////////////////////////
  {
    IPC_LCD_ON_REQ_POPUPS = 1;
    IO_Popups_Display_Request();

    if (IO_POPUP_DISPLAY_ENABLE)
    {
      if (IO_DISP_FUNC_SWITCH_FLAG)
        IO_DISP_FUNC_SWITCH_FLAG = 0;
      Popups_Displaying(PopupMsg);
      Popups_Sound_Playing(PopupSnd);

     // if (IO_CAN_KEY_ENTER == IO_KEY_PRESS)
      //{
     //   IO_CAN_KEY_ENTER = IO_KEY_IDLE;
     //   Popup_Mask_Request();
     // } 
    }
    MenuDisplay = 0;   //默认行车电脑界面位车速
  }
  else
    IO_Popups_Display_Release();
}

void Popups_Polling(void)
{                 
  if ((IO_DISPLAY_MODE == IO_DISP_MODE_IGN_ON) &&(IO_DISP_TEST_OFF_TIME >= 10) )    
  {
      if(PepslcuAlarm==1)    //  PEPS 系统异常
       {
          Popup_Request(POPUP_PEPS_Fault);
       }
      else
         Popup_Delete(POPUP_PEPS_Fault);   //  PEPS 系统异常
      if(PepslcuAlarm==2)    //  ESCL 异常
       {
          Popup_Request(POPUP_ESCL_Fault);
       }
      else
         Popup_Delete(POPUP_ESCL_Fault);      //  ESCL 异常
       if(PepslcuAlarm==3)    //  发动机预热，禁止启动
       {
          Popup_Request(POPUP_Engine_Prewarming);
       }
      else
         Popup_Delete(POPUP_Engine_Prewarming);   //  发动机预热，禁止启动
      if(PepslcuAlarm==4)    //  发动机预热结束
       {
          Popup_Request(POPUP_Prewarming_End);
       }
      else
         Popup_Delete(POPUP_Prewarming_End);     //  发动机预热结束
      if(PepslcuAlarm==5)    // "Press Brake Start" Indication On   3踩刹车启动
       {
           Popup_Request(POPUP_Press_Brake_Start);
       }
     else
         Popup_Delete(POPUP_Press_Brake_Start);   //  踩刹车启动
      if(PepslcuAlarm==6)    //  “Press clutch Start”Indication On      踩离合启动
       {
         Popup_Request(POPUP_Press_clutch_Start);
       }
      else
        Popup_Delete(POPUP_Press_clutch_Start);   //  踩离合启动
            //PEPS仪表报警提示
      if(PepslcuAlarm==7)    //  " Key Not Found" Indication On     无钥匙
      {
          Popup_Request(POPUP_Key_Not_Found);
      }
      else
         Popup_Delete(POPUP_Key_Not_Found);       // 无钥匙
      if(PepslcuAlarm==8)    //  钥匙不在车内
       {
          Popup_Request(POPUP_No_Key_Incar);
       }
      else
        {
         Popup_Delete(POPUP_No_Key_Incar);       //  钥匙不在车内
         Sound_Delete(SND_noKeyInCar);
         }
      if(PepslcuAlarm==9)   // "N Level Start" Indication On       4N档启动
       {
          Popup_Request(POPUP_N_Level_Start);
       }
       else
          Popup_Delete(POPUP_N_Level_Start);     // N档启动
      if(PepslcuAlarm==10)    //   “Press SSB by key to Start”Indication On    6未检测到钥匙下，请用钥匙末端按启动开关
       {
           Popup_Request(POPUP_Press_SSB_by_key);
       }
      else
         Popup_Delete(POPUP_Press_SSB_by_key);     //未检测到钥匙下，请用钥匙末端按启动开关
      if(PepslcuAlarm==11)    //  未挂入N 档下电
       {
          Popup_Request(POPUP_POWEROFF_WithoutN);
       }
      else
      {
         Popup_Delete(POPUP_POWEROFF_WithoutN);        //  未挂入N 档下电
         Sound_Delete(SND_POffWithoutN);
       }  
       if(PepslcuAlarm==12)    // "  Key Battery Low" Indication On   1电量低
      {
         Popup_Request(POPUP_Key_Battery_Low);
      }
      else
        {
            Popup_Delete(POPUP_Key_Battery_Low);        // 电量低
        }
       if(PepslcuAlarm==14)    // 运行状态错误，请下电后重新启动
      {
         Popup_Request(POPUP_Running_Err);
      }
      else
        {
            Popup_Delete(POPUP_Running_Err);        // 运行状态错误，请下电后重新启动
        }
    /* 该报警仅需要声音
     if(PepslcuAlarm==13)    //  闭锁时钥匙在车内
       {
          Popup_Request(POPUP_Lock_WithKeyInCar);
       }
      else
         Popup_Clear(POPUP_Lock_WithKeyInCar);       //  闭锁时钥匙在车内
       */ 
       if(Child_Loca_State==1)        //童锁状态
          Popup_Request(POPUP_Child_Loca_State);
       else  
          Popup_Delete(POPUP_Child_Loca_State);   //童锁状态
       
       if(LowPress_ReleaseLimit==1)            //低压释放限制状态
          Popup_Request(POPUP_LowPress_ReleaseLimit);
       else  
          Popup_Delete(POPUP_LowPress_ReleaseLimit);    //低压释放限制状态
       
      if(IO_DISP_TEST_OFF_TIME > 102)
      {
          if(NVM_CONFIG_TPMS==0)
          {
              if(!(BCM_TPMS_STATUS & CAN_MSG_LOST))
              {
                  if(TireLocation==0)                     //左轮胎
                  {
                    if( CTIWheelSensSts==1)       //有信号
                    {
                        Popup_Delete(POPUP_noSignal_Left); 
                        if(TireIDStudy==0)             //学习了
                        {
                            
                            if( TireHighTempWarning==1)           //胎温高
                            {
                                 Popup_Request(POPUP_HighTEMP_Left);
                            }
                            else  if( TireHighTempWarning==0)
                            {
                                 Popup_Delete(POPUP_HighTEMP_Left); 
                            }
                            if(TirePressureState==1)         //Tire pressure high
                            {
                                 Popup_Delete(POPUP_LowPRESSURE_Left); 
                                 Popup_Request(POPUP_HighPRESSURE_Left);
                            }
                            else if(TirePressureState==2)   // Tire pressure low
                            {
                                 Popup_Delete(POPUP_HighPRESSURE_Left); 
                                 Popup_Request(POPUP_LowPRESSURE_Left);
                            }
                            else if(TirePressureState==0)
                            {
                                 Popup_Delete(POPUP_HighPRESSURE_Left); 
                                 Popup_Delete(POPUP_LowPRESSURE_Left); 
                            }
                            Popup_Delete(POPUP_TireIDStudy_Left);
                        }
                        else
                        {
                           Popup_Delete(POPUP_HighTEMP_Left);
                           Popup_Delete(POPUP_HighPRESSURE_Left);
                           Popup_Delete(POPUP_LowPRESSURE_Left); 
                           if(TireIDStudy==1)
                           Popup_Request(POPUP_TireIDStudy_Left);
                        }                  
                    }
                     else          //无信号
                    {
                        Popup_Request(POPUP_noSignal_Left);
                        Popup_Delete(POPUP_HighTEMP_Left);
                        Popup_Delete(POPUP_HighPRESSURE_Left);
                        Popup_Delete(POPUP_LowPRESSURE_Left); 
                        Popup_Delete(POPUP_TireIDStudy_Left); 
                    }                
                  }
                  else if(TireLocation==1)                  //右轮胎
                  {
                    if( CTIWheelSensSts==1)       //有信号
                    {
                       Popup_Clear(POPUP_noSignal_Right);  //无信号
                       if(TireIDStudy==0)
                       {
                           if( TireHighTempWarning==1)           //胎温高
                          {
                             Popup_Request(POPUP_HighTEMP_Right);
                          }
                          else  if( TireHighTempWarning==0)
                          {
                             Popup_Delete(POPUP_HighTEMP_Right);   //胎温高
                          }
                           if(TirePressureState==1)         //Tire pressure high
                          {
                             Popup_Delete(POPUP_LowPRESSURE_Right);
                             Popup_Request(POPUP_HighPRESSURE_Right);
                          }
                          else if(TirePressureState==2)   // Tire pressure low
                          {
                             Popup_Delete(POPUP_HighPRESSURE_Right);
                             Popup_Request(POPUP_LowPRESSURE_Right);
                          }
                          else if(TirePressureState==0)
                          {
                               Popup_Delete(POPUP_HighPRESSURE_Right);
                               Popup_Delete(POPUP_LowPRESSURE_Right);
                          }
                          Popup_Delete(POPUP_TireIDStudy_Right);
                      }
                      else if(TireIDStudy==2)
                      {  
                         Popup_Request(POPUP_TireIDStudy_Right); 
                         }          
                    }
                     else          //无信号
                    {
                       Popup_Request(POPUP_noSignal_Right);
                       Popup_Delete(POPUP_HighTEMP_Right);   //胎温高
                       Popup_Delete(POPUP_HighPRESSURE_Right);   //Tire pressure high
                       Popup_Delete(POPUP_LowPRESSURE_Right);    // Tire pressure low
                       Popup_Delete(POPUP_TireIDStudy_Right);  // 右轮ID未学习                    
                    }
                  }
              }
              else       //无报文
              {
                 Popup_Delete(POPUP_noSignal_Left);        //无信号
                 Popup_Delete(POPUP_HighTEMP_Left);       //胎温高
                 Popup_Delete(POPUP_HighPRESSURE_Left);   //Tire pressure high
                 Popup_Delete(POPUP_LowPRESSURE_Left);    // Tire pressure low
                 Popup_Delete(POPUP_noSignal_Right);  //无信号
                 Popup_Delete(POPUP_HighTEMP_Right);   //胎温高
                 Popup_Delete(POPUP_HighPRESSURE_Right);   //Tire pressure high
                 Popup_Delete(POPUP_LowPRESSURE_Right);    // Tire pressure low
                 Popup_Delete(POPUP_TireIDStudy_Left);    //左轮ID未学习
                 Popup_Delete(POPUP_TireIDStudy_Right);  // 右轮ID未学习           
              }
          }
          else    //未配置
          {
               Popup_Clear(POPUP_noSignal_Left);        //无信号
               Popup_Clear(POPUP_HighTEMP_Left);       //胎温高
               Popup_Clear(POPUP_HighPRESSURE_Left);   //Tire pressure high
               Popup_Clear(POPUP_LowPRESSURE_Left);    // Tire pressure low
               Popup_Clear(POPUP_noSignal_Right);  //无信号
               Popup_Clear(POPUP_HighTEMP_Right);   //胎温高
               Popup_Clear(POPUP_HighPRESSURE_Right);   //Tire pressure high
               Popup_Clear(POPUP_LowPRESSURE_Right);    // Tire pressure low
               Popup_Clear(POPUP_TireIDStudy_Left);    //左轮ID未学习
               Popup_Clear(POPUP_TireIDStudy_Right);  // 右轮ID未学习      
          }
      }
  }
  else         //非IGON
  {  
     Popup_Clear(POPUP_noSignal_Left);        //无信号
     Popup_Clear(POPUP_HighTEMP_Left);       //胎温高
     Popup_Clear(POPUP_HighPRESSURE_Left);   //Tire pressure high
     Popup_Clear(POPUP_LowPRESSURE_Left);    // Tire pressure low
     Popup_Clear(POPUP_noSignal_Right);  //无信号
     Popup_Clear(POPUP_HighTEMP_Right);   //胎温高
     Popup_Clear(POPUP_HighPRESSURE_Right);   //Tire pressure high
     Popup_Clear(POPUP_LowPRESSURE_Right);    // Tire pressure low
     Popup_Clear(POPUP_TireIDStudy_Left);    //左轮ID未学习
     Popup_Clear(POPUP_TireIDStudy_Right);  // 右轮ID未学习
     
     
     
    // Popup_Clear(POPUP_Lock_WithKeyInCar);       //  闭锁时钥匙在车内
     Popup_Clear(POPUP_Child_Loca_State);   //童锁状态
     Popup_Clear(POPUP_LowPress_ReleaseLimit);    //低压释放限制状态
    Sound_Delete(SND_noKeyInCar);
    Sound_Delete(SND_POffWithoutN);
  
    if (SYS_IGN_OFF_DISP_MODE)
    {
         if(PepslcuAlarm==1)    //  PEPS 系统异常
         {
            Popup_Request(POPUP_PEPS_Fault);
         }
        else
           Popup_Delete(POPUP_PEPS_Fault);   //  PEPS 系统异常
        if(PepslcuAlarm==2)    //  ESCL 异常
         {
            Popup_Request(POPUP_ESCL_Fault);
         }
        else
           Popup_Delete(POPUP_ESCL_Fault);      //  ESCL 异常
         if(PepslcuAlarm==3)    //  发动机预热，禁止启动
         {
            Popup_Request(POPUP_Engine_Prewarming);
         }
        else
           Popup_Delete(POPUP_Engine_Prewarming);   //  发动机预热，禁止启动
        if(PepslcuAlarm==4)    //  发动机预热结束
         {
            Popup_Request(POPUP_Prewarming_End);
         }
        else
           Popup_Delete(POPUP_Prewarming_End);     //  发动机预热结束
        if(PepslcuAlarm==5)    // "Press Brake Start" Indication On   3踩刹车启动
         {
             Popup_Request(POPUP_Press_Brake_Start);
         }
       else
           Popup_Delete(POPUP_Press_Brake_Start);   //  踩刹车启动
        if(PepslcuAlarm==6)    //  “Press clutch Start”Indication On      踩离合启动
         {
           Popup_Request(POPUP_Press_clutch_Start);
         }
        else
          Popup_Delete(POPUP_Press_clutch_Start);   //  踩离合启动
              //PEPS仪表报警提示
        if(PepslcuAlarm==7)    //  " Key Not Found" Indication On     无钥匙
        {
            Popup_Request(POPUP_Key_Not_Found);
        }
        else
           Popup_Delete(POPUP_Key_Not_Found);       // 无钥匙
        if(PepslcuAlarm==8)    //  钥匙不在车内
         {
            Popup_Request(POPUP_No_Key_Incar);
         }
        else
          {
           Popup_Delete(POPUP_No_Key_Incar);       //  钥匙不在车内
           Sound_Delete(SND_noKeyInCar);
           }
        if(PepslcuAlarm==9)   // "N Level Start" Indication On       4N档启动
         {
            Popup_Request(POPUP_N_Level_Start);
         }
         else
            Popup_Delete(POPUP_N_Level_Start);     // N档启动
        if(PepslcuAlarm==10)    //   “Press SSB by key to Start”Indication On    6未检测到钥匙下，请用钥匙末端按启动开关
         {
             Popup_Request(POPUP_Press_SSB_by_key);
         }
        else
           Popup_Delete(POPUP_Press_SSB_by_key);     //未检测到钥匙下，请用钥匙末端按启动开关
        if(PepslcuAlarm==11)    //  未挂入N 档下电
         {
            Popup_Request(POPUP_POWEROFF_WithoutN);
         }
        else
        {
           Popup_Delete(POPUP_POWEROFF_WithoutN);        //  未挂入N 档下电
           Sound_Delete(SND_POffWithoutN);
         }  
         if(PepslcuAlarm==12)    // "  Key Battery Low" Indication On   1电量低
        {
           Popup_Request(POPUP_Key_Battery_Low);
        }
        else
          {
              Popup_Delete(POPUP_Key_Battery_Low);        // 电量低
          }
       if(PepslcuAlarm==14)    // 运行状态错误，请下电后重新启动
      {
         Popup_Request(POPUP_Running_Err);
      }
      else
        {
            Popup_Delete(POPUP_Running_Err);        // 运行状态错误，请下电后重新启动
        }  
    }
    else 
    {
         Popup_Clear(POPUP_PEPS_Fault);   //  PEPS 系统异常
         Popup_Clear(POPUP_ESCL_Fault);      //  ESCL 异常
         Popup_Clear(POPUP_Engine_Prewarming);   //  发动机预热，禁止启动
         Popup_Clear(POPUP_Prewarming_End);     //  发动机预热结束
         Popup_Clear(POPUP_Press_Brake_Start);   //  踩刹车启动
         Popup_Clear(POPUP_Press_clutch_Start);   //  踩离合启动
         Popup_Clear(POPUP_Key_Not_Found);       // 无钥匙
         Popup_Clear(POPUP_No_Key_Incar);       //  钥匙不在车内
         Popup_Clear(POPUP_N_Level_Start);     // N档启动
         Popup_Clear(POPUP_Press_SSB_by_key);     //未检测到钥匙下，请用钥匙末端按启动开关
         Popup_Clear(POPUP_POWEROFF_WithoutN);        //  未挂入N 档下电
         Popup_Clear(POPUP_Key_Battery_Low);        // 电量低
         Popup_Clear(POPUP_Running_Err);        // 运行状态错误，请下电后重新启动
    }
  }
}

void Popups_Displaying(uint8_t PopupMsg)
{
  if (PopupMsg < POPUP_MSG_NUMBER_TOTAL)
  {
    switch (PopupMsgAttributeTable[PopupMsg].Type)
    {
    case POPUP_TYPE_GENERAL :     GUI_General_Popup_Display( PopupMsg);
      break;
    //case POPUP_TYPE_RADAR   :     GUI_RADAR_Display(PopupMsg);
    //  break;
    //case POPUP_TYPE_OVSPD   :     GUI_Popup_Over_Speed_Display(PopupMsg);
    //  break;
    //case POPUP_TYPE_HOOVES  :     GUI_HOOVES_Display(PopupMsg);
    //  break;
    case POPUP_TYPE_TPMS    :     GUI_TPMS_Display(PopupMsg);
      break;
    //case POPUP_TYPE_CHARG   :    GUI_General_Charging_Popup_Display(PopupMsg);
      //break;
    default                 :
      break;
    }
  }
}
//播放声音
void Popups_Sound_Playing(uint8_t PopupSnd)
{
  uint8_t ReqCode;

  ReqCode = Popup_Get_Current_Sound_Req_Code();
  
  switch (PopupSnd)
  {
    /*case SND_RADAR      :
                                      if (RADAR_SOUND == RADAR_SND_1Hz)
                                      {
                                        Sound_Delete(SND_RADAR_2Hz);
                                        Sound_Delete(SND_RADAR_4Hz);
                                        Sound_Delete(SND_RADAR_CONTINUOUS);
                                        Sound_Request(SND_RADAR_1Hz, ReqCode);  
                                      }                                        
                                      else if (RADAR_SOUND == RADAR_SND_2Hz)
                                      {
                                        Sound_Delete(SND_RADAR_1Hz);
                                        Sound_Delete(SND_RADAR_4Hz);
                                        Sound_Delete(SND_RADAR_CONTINUOUS);
                                        Sound_Request(SND_RADAR_2Hz, ReqCode);
                                      }
                                      else if (RADAR_SOUND == RADAR_SND_4Hz)
                                      {
                                        Sound_Delete(SND_RADAR_1Hz);
                                        Sound_Delete(SND_RADAR_2Hz);
                                        Sound_Delete(SND_RADAR_CONTINUOUS);
                                        Sound_Request(SND_RADAR_4Hz, ReqCode);
                                      }
                                      else if (RADAR_SOUND == RADAR_SND_CONTINUOUS)
                                      {
                                        Sound_Delete(SND_RADAR_1Hz);
                                        Sound_Delete(SND_RADAR_2Hz);
                                        Sound_Delete(SND_RADAR_4Hz);
                                        Sound_Request(SND_RADAR_CONTINUOUS, ReqCode);
                                      }
                                      else
                                      {
                                        Sound_Delete(SND_RADAR_1Hz);
                                        Sound_Delete(SND_RADAR_2Hz);
                                        Sound_Delete(SND_RADAR_4Hz);
                                        Sound_Delete(SND_RADAR_CONTINUOUS);
                                      } 
                                     break;
      case   SND_YOUTIPIAN_ERROR: 
                                      if (ReqCode)
                                      {
                                        if (WARNING_DOOR_OPEN_SND_EN == 0)
                                           ReqCode = 0;
                                        Sound_Request(PopupSnd, ReqCode);
                                      }
                                      else
                                        Sound_Delete(PopupSnd);
                                      break;  
      */                                
      default                       :
                                      if (ReqCode)
                                      {
                                        Sound_Request(PopupSnd, ReqCode);
                                      }
                                      else
                                        Sound_Delete(PopupSnd);
                                      break;

  }   
}

