

#define GLOBALS_VARIABLE
//#define _RESOURCE_TXT

#include "Include.h"


//unsigned int FuelResistResult;
//INT8U SoundAlarm_Now  ;
//INT8U poPAlarm_Now  ;
extern INT8U TEST121;
extern TFTLCDDrawingFIFOStruct     TFTLCDDrawingFIFO;
void main(void)
{
	Power_Management_Init();
	Task_Scheduler_Init();
	EnableInterrupts;
	for (;;)
	{
		_FEED_COP();
		CheckOSC();
		Power_Management_Service();
		Task_Scheduling_Service();
		EEPROM_RW_Service();
		TFT_LCD_Display_Update_Service();
		DoCAN_Communication_Service();
		UDS_Server_Application_Service();
		LED_Driver_Service();
		Power_HALT_Service();
		if(TEST121 < TFTLCDDrawingFIFO.Depth)
		{
			TEST121 = TFTLCDDrawingFIFO.Depth;
		}

		TASK_1ms_ALL
		{
			COM_TX_Process();
			//CoCAN_Signal_Update_Service();//shican
			//Flash_3HZ_LampBlink_Time();
		}
		TASK_2ms_GROUP_0
		{
			Analog_Signal_Conv_Service();
			Analog_Update();
		}
		TASK_2ms_GROUP_1				   
		{
			UDS_10ms_Service();
		    	
		     	//SoundAlarm_Now = Popup_Get_Current_Sound();	 
			 Buzzer_Play_Handle(Popup_Get_Current_Sound());
			Data_PDC_Service();
			Data_AC_Temp_Processing_Service();
			Data_Cruise_Processing_Service();
		}
		TASK_10ms_GROUP_0
		{
		    	NVM_Data_Update_Service();
		}
		TASK_10ms_GROUP_1
		{
	        		EEPROM_Access_Service();
		}
		TASK_10ms_GROUP_2
		{  
			Data_Driving_Time_OFF_Reset();   
			
		}										 
		TASK_10ms_GROUP_3
		{
			GET_Data_For_YB();
			NM_D_BusOff_Recover();
		}
		TASK_10ms_GROUP_4
		{
	            	Line_In_Debounce_Service();
					COM_RX_Process();//shi+can
		}
		TASK_10ms_GROUP_5
		{
			 //Data_Engine_Speed_Processing_Service();
			 Data_Consumed_Power_Service();	
		}
		TASK_10ms_GROUP_8
		{
			Sound_Turn_Lamp_Service();
			Sound_Outdoor_Temp_Service();
		}
		TASK_10ms_GROUP_9
		{
		    	Button_Service();
		    	Menu_TPMS_Display_Service();
		}
		TASK_20ms_GROUP_0
		{
            		Power_Status_Update_Service(AnalogKL30Voltage , AnalogKL15Voltage);            
		}
		TASK_20ms_GROUP_1
		{
		    	//Data_LDW_Service();
		    		
			Data_ODO_Processing();	
			
			Data_BattPower_Processing_Service();
			Data_Batt_MaxCellVolt_Processing_Service();
			Data_Batt_MinCellVolt_Processing_Service();
			Data_Batt_MaxTemp_Processing_Service();
			Data_Batt_MinTemp_Processing_Service();
			Data_SOC_Processing_Service();
			Data_Avg_EVCons_Service();//平均电耗
			Data_EV_DTE_Service();// 纯电续航里程
			Data_Elec_Fuel_Service();//点+油续航
			Data_ResiduePwr_Service();//剩余功率
			Data_BattVolt_Processing_Service();//工作电压
			Data_BattCurr_Processing_Service();//工作电流
			Data_ErrCode_Processing_Service();//故障码
		}
		TASK_20ms_GROUP_2
		{
			Data_Vehicle_Speed_Processing_Service();
		}
		TASK_20ms_GROUP_5
		{
			Gauges_Management_Service();	
		}
		TASK_50ms_GROUP_0
		{
	            	Flash_Sync_Signal_Generation_Service();
					
		}
		TASK_50ms_GROUP_1
		{
			LED_Driver_Scan_Refresh();
			//DMM_Processing_Service();
			PopupAlarm = Popup_Get_Current_Message(); 
		}
		TASK_50ms_GROUP_2
		{
			Lamp_Management_Service();  
		}
		TASK_100ms_GROUP_0
		{
            		RTI_Timing_Service();
            		GET_Data_To_DSP(); 
            		
		}
		TASK_100ms_GROUP_1
		{
			Data_Drive_Info_Group1_Processing_Service();
			Data_Drive_Info_Group2_Processing_Service();
			
			
		}
		TASK_100ms_GROUP_2
		{
		    Data_Drive_Info_Group3_Processing_Service(); 
		}
		TASK_100ms_GROUP_3
		{
		    	LED_Driver_WorkCheck();
		}
		TASK_100ms_GROUP_4
		{
			Popups_Polling();
            		Sounds_Polling();
			Popup_Scheduling_Service();
			//poPAlarm_Now = Popup_Get_Current_Message();
		}
		TASK_100ms_GROUP_5
		{
            		OTA_Detect();
            		Page_Manage_Service(); 
            		BL_Management_service();			
		}
		TASK_100ms_GROUP_6
		{
			Power_Management_Time_Handle();
			Gui_Display_Service();	
		}
		TASK_100ms_GROUP_7
		{
		}
		TASK_100ms_GROUP_8
		{
			
			
		}
	}
}

/******************************************************************************
��������
��  �ܣ������������ʼ��
��  ������
����ֵ����
******************************************************************************/
void Task_Scheduler_Init(void)
{
	uint8_t i;
	uint8_t *p;

	ScheduleCtrlCnt = 0;
	ScheduleUpdate.TaskFlagAll = 0;

	p = (uint8_t *)(&TaskGroup);
	for (i = 0; i < sizeof(TaskGroup); i++)
		*(p + i) = 0xFF;
}

/******************************************************************************
��������
��  �ܣ�������ȷ�����
��  ������
����ֵ����
*******************************************************************************
ע  �⣺�÷��������뱻ʵʱ����
******************************************************************************/
void Task_Scheduling_Service(void)
{
	ScheduleUpdate.TaskFlagAll = 0; //��������±�־

	if (API_1ms_Timer_Update())
	{
		ScheduleUpdate.TaskFlagAll = TaskScheduleTable[ScheduleCtrlCnt];

		if (ScheduleUpdate.Task.Flag2ms) //���µ�ǰ2ms��������
		{
			if (TaskGroup.ID2ms)
				TaskGroup.ID2ms = 0;
			else
				TaskGroup.ID2ms = 1;
		}

		if (ScheduleUpdate.Task.Flag10ms) //���µ�ǰ10ms��������
		{
			if (TaskGroup.ID10ms >= 9)
				TaskGroup.ID10ms = 0;
			else
				TaskGroup.ID10ms++;
		}

		if (ScheduleUpdate.Task.Flag20ms) //���µ�ǰ20ms��������
		{
			if (TaskGroup.ID20ms >= 9)
				TaskGroup.ID20ms = 0;
			else
				TaskGroup.ID20ms++;
		}

		if (ScheduleUpdate.Task.Flag50ms) //���µ�ǰ50ms��������
		{
			if (TaskGroup.ID50ms >= 9)
				TaskGroup.ID50ms = 0;
			else
				TaskGroup.ID50ms++;
		}

		if (ScheduleUpdate.Task.Flag100ms) //���µ�ǰ100ms��������
		{
			if (TaskGroup.ID100ms >= 9)
				TaskGroup.ID100ms = 0;
			else
				TaskGroup.ID100ms++;
		}

		if (ScheduleCtrlCnt >= 9) //���µ��ȼ�����
			ScheduleCtrlCnt = 0;
		else
			ScheduleCtrlCnt++;
	}
}
