/******************************************************************************
文 件 名：main.h
功能描述：主程序头文件
作    者：张暄
版    本：V1.0
日    期：2016.11.1
******************************************************************************/

#ifndef _MAIN_H_
#define _MAIN_H_

#include <hidef.h>            /* common defines and macros */
#include "derivative.h"       /* derivative-specific definitions */
#include "stdint.h"
#include "comdef.h"
#include "CRG.h"
#include "API.h"
#include "GPIO.h"
#include "PWM.h"
//#include "MSCAN0_J1939.h"
//#include "DoCAN_J1939.h"
#include "DoCAN_ISO15765_Config.h"
#include "DoCAN_ISO15765.h"
#include "SCI0_UART.h"
#include "SCI1_UART.h"       
#include "Analog_Signal_Conversion.h"
#include "Communication_Over_CAN.h"
#include "Simulated_IIC_Master.h"
#include "PowerManagement.h"
#include "LED.h"
#include "Line_In.h"
#include "EEPROM_24Cxx_Remap.h"
#include "Stepper_Motor.h"
#include "UDS_ISO14229_Services.h"
#include "UDS_ISO14229_Server.h"
#include "Non_volatile_Memorys.h"
#include "Flash_synchronizer.h"
#include "TFT_LCD.h"
#include "Data_Processor.h"
#include "Warning_Processor.h"
#include "Line_In.h"
#include "Segmented_Gauges.h "
#include "Telltales.h"
#include "BackLight.h"
#include "Popup_Scheduler.h"
#include "Popups.h"
#include "Drive_Info.h"
#include "TFT_LCD.h" 
#include "LCD_SEG.h"
#include "GUI.h"
#include "Gauges.h"
#include "IPC.h"
#include "TSzhen.h"
 
#include "PowerManagement.h"
#include "Buzzer.h"
/******************************************************************************
任务周期与分组
******************************************************************************/
#define   TASK_1ms_ALL          if(ScheduleUpdate.Task.Flag1ms)

#define   TASK_2ms_GROUP_0      if((ScheduleUpdate.Task.Flag2ms) && (TaskGroup.ID2ms == 0))
#define   TASK_2ms_GROUP_1      if((ScheduleUpdate.Task.Flag2ms) && (TaskGroup.ID2ms == 1))

#define   TASK_10ms_GROUP_0     if((ScheduleUpdate.Task.Flag10ms) && (TaskGroup.ID10ms == 0))
#define   TASK_10ms_GROUP_1     if((ScheduleUpdate.Task.Flag10ms) && (TaskGroup.ID10ms == 1))
#define   TASK_10ms_GROUP_2     if((ScheduleUpdate.Task.Flag10ms) && (TaskGroup.ID10ms == 2))
#define   TASK_10ms_GROUP_3     if((ScheduleUpdate.Task.Flag10ms) && (TaskGroup.ID10ms == 3))
#define   TASK_10ms_GROUP_4     if((ScheduleUpdate.Task.Flag10ms) && (TaskGroup.ID10ms == 4))
#define   TASK_10ms_GROUP_5     if((ScheduleUpdate.Task.Flag10ms) && (TaskGroup.ID10ms == 5))
#define   TASK_10ms_GROUP_6     if((ScheduleUpdate.Task.Flag10ms) && (TaskGroup.ID10ms == 6))
#define   TASK_10ms_GROUP_7     if((ScheduleUpdate.Task.Flag10ms) && (TaskGroup.ID10ms == 7))
#define   TASK_10ms_GROUP_8     if((ScheduleUpdate.Task.Flag10ms) && (TaskGroup.ID10ms == 8))
#define   TASK_10ms_GROUP_9     if((ScheduleUpdate.Task.Flag10ms) && (TaskGroup.ID10ms == 9))

#define   TASK_20ms_GROUP_0     if((ScheduleUpdate.Task.Flag20ms) && (TaskGroup.ID20ms == 0))
#define   TASK_20ms_GROUP_1     if((ScheduleUpdate.Task.Flag20ms) && (TaskGroup.ID20ms == 1))
#define   TASK_20ms_GROUP_2     if((ScheduleUpdate.Task.Flag20ms) && (TaskGroup.ID20ms == 2))
#define   TASK_20ms_GROUP_3     if((ScheduleUpdate.Task.Flag20ms) && (TaskGroup.ID20ms == 3))
#define   TASK_20ms_GROUP_4     if((ScheduleUpdate.Task.Flag20ms) && (TaskGroup.ID20ms == 4))
#define   TASK_20ms_GROUP_5     if((ScheduleUpdate.Task.Flag20ms) && (TaskGroup.ID20ms == 5))
#define   TASK_20ms_GROUP_6     if((ScheduleUpdate.Task.Flag20ms) && (TaskGroup.ID20ms == 6))
#define   TASK_20ms_GROUP_7     if((ScheduleUpdate.Task.Flag20ms) && (TaskGroup.ID20ms == 7))
#define   TASK_20ms_GROUP_8     if((ScheduleUpdate.Task.Flag20ms) && (TaskGroup.ID20ms == 8))
#define   TASK_20ms_GROUP_9     if((ScheduleUpdate.Task.Flag20ms) && (TaskGroup.ID20ms == 9))

#define   TASK_50ms_GROUP_0     if((ScheduleUpdate.Task.Flag50ms) && (TaskGroup.ID50ms == 0))
#define   TASK_50ms_GROUP_1     if((ScheduleUpdate.Task.Flag50ms) && (TaskGroup.ID50ms == 1))
#define   TASK_50ms_GROUP_2     if((ScheduleUpdate.Task.Flag50ms) && (TaskGroup.ID50ms == 2))
#define   TASK_50ms_GROUP_3     if((ScheduleUpdate.Task.Flag50ms) && (TaskGroup.ID50ms == 3))
#define   TASK_50ms_GROUP_4     if((ScheduleUpdate.Task.Flag50ms) && (TaskGroup.ID50ms == 4))
#define   TASK_50ms_GROUP_5     if((ScheduleUpdate.Task.Flag50ms) && (TaskGroup.ID50ms == 5))
#define   TASK_50ms_GROUP_6     if((ScheduleUpdate.Task.Flag50ms) && (TaskGroup.ID50ms == 6))
#define   TASK_50ms_GROUP_7     if((ScheduleUpdate.Task.Flag50ms) && (TaskGroup.ID50ms == 7))
#define   TASK_50ms_GROUP_8     if((ScheduleUpdate.Task.Flag50ms) && (TaskGroup.ID50ms == 8))
#define   TASK_50ms_GROUP_9     if((ScheduleUpdate.Task.Flag50ms) && (TaskGroup.ID50ms == 9))

#define   TASK_100ms_GROUP_0    if((ScheduleUpdate.Task.Flag100ms) && (TaskGroup.ID100ms == 0))
#define   TASK_100ms_GROUP_1    if((ScheduleUpdate.Task.Flag100ms) && (TaskGroup.ID100ms == 1))
#define   TASK_100ms_GROUP_2    if((ScheduleUpdate.Task.Flag100ms) && (TaskGroup.ID100ms == 2))
#define   TASK_100ms_GROUP_3    if((ScheduleUpdate.Task.Flag100ms) && (TaskGroup.ID100ms == 3))
#define   TASK_100ms_GROUP_4    if((ScheduleUpdate.Task.Flag100ms) && (TaskGroup.ID100ms == 4))
#define   TASK_100ms_GROUP_5    if((ScheduleUpdate.Task.Flag100ms) && (TaskGroup.ID100ms == 5))
#define   TASK_100ms_GROUP_6    if((ScheduleUpdate.Task.Flag100ms) && (TaskGroup.ID100ms == 6))
#define   TASK_100ms_GROUP_7    if((ScheduleUpdate.Task.Flag100ms) && (TaskGroup.ID100ms == 7))
#define   TASK_100ms_GROUP_8    if((ScheduleUpdate.Task.Flag100ms) && (TaskGroup.ID100ms == 8))
#define   TASK_100ms_GROUP_9    if((ScheduleUpdate.Task.Flag100ms) && (TaskGroup.ID100ms == 9))

/******************************************************************************
任务调度表
******************************************************************************/
static const uint8_t TaskScheduleTable[10] = {0x07, 0x0F, 0x17, 0x0F, 0x07,
    0x0F, 0x17, 0x0F, 0x27, 0x0F };

/******************************************************************************
结构体声明
******************************************************************************/
//extern   uint8_t   Fuel_Seg ;
/*** 调度更新标志位定义结构 ***/
typedef struct
{
  uint8_t     Flag1ms     :   1;
  uint8_t     Flag2ms     :   1;
  uint8_t     Flag10ms    :   1;
  uint8_t     Flag20ms    :   1;
  uint8_t     Flag50ms    :   1;
  uint8_t     Flag100ms   :   1;
  uint8_t                 :   2;
} ScheduleUpdateStruct;

/*** 调度更新标志联合体 ***/
typedef union
{
  uint8_t              TaskFlagAll;
  ScheduleUpdateStruct Task;
} ScheduleUpdateUnion;

/*** 任务分组编号结构 ***/
typedef struct
{
  uint8_t     ID2ms;
  uint8_t     ID10ms;
  uint8_t     ID20ms;
  uint8_t     ID50ms;
  uint8_t     ID100ms;
  uint8_t     ID500ms;
} TaskGroupStruct;

extern uint16_t  DataDTE;
extern uint8_t   Fuel_Seg, FuelStatus;

extern uint16_t  DataAvgFuelConsByLF; 

extern uint16_t  FuelConsumeSum;
extern INT16U    FuelResistanceV;
extern uint8_t AverageFuel_LL ;
extern uint8_t AverageFuel_LH ;
extern uint8_t AverageFuel_HL ;
extern uint8_t AverageFuel_HH ;
extern uint16_t TankLevel_temp;
/******************************************************************************
函数声明
******************************************************************************/
void Task_Scheduler_Init(void);
void Task_Scheduling_Service(void);                  /* 此函数需要被实时调用 */
void Factory_ClearOdoM(void) ;                       //生产清理大计里程
void Fuel_Process(void);
#endif