/*C*****************************************************************************
* NAME:         DOBCNT1010.c
*-------------------------------------------------------------------------------
* RELEASE:          
* REVISION:     1.0   
*-------------------------------------------------------------------------------
* PURPOSE:
* 双枪计数器应用程序
*******************************************************************************/

#include "config.h"
volatile TXHData_type TXHData;//定义指针数据
const u8  BoxVersion[11]="TXH_V1.4.7";

cup_t STM32CPU;
void TXH_Init(void)
{
	u8 i= 0;
	Pluse_InPut_Init();
  Timer_Init();
  FM_Init();
	TXHData.ROM_UpData.ROM_UpData_State.Flag.Update_Flag = 0;
	TXHData.ROM_UpData.ROM_UpData_State.Flag.DisableUpdate = 0;
	TXHData.ROM_UpData.ROM_UpData_State.Flag.RightRun_Flag = 3;
	FMItemWrite(CF_ROM_UpData,0);
	//GPSConnect_Init();
	AT45DB_Init();
	DS1307_Init();
	Log_Init();
	OLED_Init();
	Surface_Init();
	FindSysconfigFile();
	MD_Connect_Init();
	Multiplex_Port_Init();
	POS_Connect_Init();
	Pluse_Putout_Init();
	
	TXHData.ROM_UpData.ROM_UpData_State.Flag.Update_Flag = 0;
	TXHData.ROM_UpData.ROM_UpData_State.Flag.DisableUpdate = 0;
	TXHData.ROM_UpData.ROM_UpData_State.Flag.RightRun_Flag = 3;
	//TXHData.ROM_UpData.FrameTop = 0;
	FMItemWrite(CF_ROM_UpData,0);
	
	TXHData.OfflineAuthorizeNumberMax = 10000;//允许离线授权次数
  TXHData.OfflineAuthorize = 1;//允许离线授权
//	TXHData.PREXDATA_WaitUpNummber = 0;
//	TXHData.CTIME.Year  = 0x22;//年
//	TXHData.CTIME.Month = 0x01;//月
//	TXHData.CTIME.Day   = 0x11;//日
//	//TXHData.CTIME.Week  = 0x02;//周	
//	TXHData.CTIME.Hour  = 0x14;//时
//	TXHData.CTIME.Min   = 0x20;//分
//	TXHData.CTIME.Sec   = 0x00;//秒
//	DS1307_Write();
  //PCBEncryption();
	PCBEncryption_Check();
}
void TXH_Task(void)
{
	u8 Port = 0,DeviceNumber = 0;
  //GPSConnectTast();
	if(TXHData.CTIMEReadTimer.Flag == 1)
	{
	  TXHData.CTIMEReadTimer.Flag = 0;
		DS1307_Read();
		if(TXHData.SurfaceNow <= Surface_Wait)
      TXHData.DisplayOn = 1;
	  }
	if(TXHData.ErrState.Flag.e_errPCBCheck == 0)//PCB验证通过
	{
		for(Port = 0;Port < TXHData.MD_Port_Num_Top;Port ++)
		{
			if(TXHData.MDRestartTimer[Port].Flag == 1)
			{
				TXHData.MDRestartTimer[Port].Flag = 0;
				}
			if(TXHData.MDRestartTimer[Port].ON_OFF == 0)
			{
				if(TXHData.MBCountTimer[Port].Flag == 1 && TXHData.MD_Port_Data[Port].PortData.POSType)
				{
					TXHData.MBCountTimer[Port].Flag = 0;
					{
						switch(TXHData.MD_Port_Data[Port].PortData.POSType)
						{
							case PosBlueSky:
							case PosHongYang:
							case PosYGao:
							case PosLY:
							case PosJP:
							case PosYWY:
							case PosRFID:
							case PosPrice1_4:
							case PosPLC:
							{
								if(TXHData.MD_Port_Info[Port].LoopGunNextFlag == 1 || (TXHData.MD_Port_Info[Port].GunInfo[TXHData.MD_Port_Info[Port].pGunNow].Handshake == 0x0))
								{
									TXHData.MD_Port_Info[Port].pGunNow ++;
									if(TXHData.MD_Port_Info[Port].pGunReturn)
									{
										TXHData.MD_Port_Info[Port].pGunNow = TXHData.MD_Port_Info[Port].pGunReturn;
										TXHData.MD_Port_Info[Port].pGunReturn = 0;
										}
									if(TXHData.MD_Port_Info[Port].GunInfo[TXHData.MD_Port_Info[Port].pGunNow].Handshake != TXHData.MD_Port_Info[Port].Handshake_Top)
									{
										TXHData.MD_Port_Info[Port].GunInfo[TXHData.MD_Port_Info[Port].pGunNow].Handshake = 0x0;
										}
									}
								
								if(TXHData.MD_Port_Info[Port].pGunNow >= TXHData.MD_Port_Data[Port].PortData.DeviceNumber)
									TXHData.MD_Port_Info[Port].pGunNow = 0;
								MD_ConnectTXTask(Port,TXHData.MD_Port_Info[Port].pGunNow,TXHData.MD_Port_Info[Port].GunInfo[TXHData.MD_Port_Info[Port].pGunNow].MB_CMD_Next);
								}
							break;
							case PosDart:
							{
								if(TXHData.MD_Port_Info[Port].LoopGunNextFlag == 1 || (TXHData.MD_Port_Info[Port].GunInfo[TXHData.MD_Port_Info[Port].PumpNow].Handshake == 0x0))
								{
									TXHData.MD_Port_Info[Port].PumpNow ++;
									if(TXHData.MD_Port_Info[Port].pGunReturn)
									{
										TXHData.MD_Port_Info[Port].PumpNow = TXHData.MD_Port_Info[Port].pGunReturn;
										TXHData.MD_Port_Info[Port].pGunReturn = 0;
										}
									if(TXHData.MD_Port_Info[Port].GunInfo[TXHData.MD_Port_Info[Port].PumpNow].Handshake != TXHData.MD_Port_Info[Port].Handshake_Top)
									{
										TXHData.MD_Port_Info[Port].GunInfo[TXHData.MD_Port_Info[Port].PumpNow].Handshake = 0x0;
										}
									}
								if(TXHData.MD_Port_Info[Port].PumpNow >= TXHData.MD_Port_Info[Port].FatherDeviceTop)
									TXHData.MD_Port_Info[Port].PumpNow = 0;
								if(TXHData.MD_Port_Info[Port].pGunCapture)
								{
									TXHData.MD_Port_Info[Port].pGunReturn = TXHData.MD_Port_Info[Port].PumpNow;
									TXHData.MD_Port_Info[Port].PumpNow = TXHData.MD_Port_Info[Port].pGunCapture - 1;
									TXHData.MD_Port_Info[Port].pGunCapture = 0;
									}
								MD_ConnectTXTask(Port,TXHData.MD_Port_Info[Port].PumpNow,TXHData.MD_Port_Info[Port].FatherDevice[TXHData.MD_Port_Info[Port].PumpNow].MB_CMD_Next);
								}
							break;
							case PosSSLan:
							{
								if(TXHData.MD_Port_Info[Port].LoopGunNextFlag == 1 || (TXHData.MD_Port_Info[Port].FatherDevice[TXHData.MD_Port_Info[Port].PumpNow].Handshake == 0x0))
								{
									TXHData.MD_Port_Info[Port].PumpNow ++;
									if(TXHData.MD_Port_Info[Port].pGunReturn)
									{
										TXHData.MD_Port_Info[Port].PumpNow = TXHData.MD_Port_Info[Port].pGunReturn;
										TXHData.MD_Port_Info[Port].pGunReturn = 0;
										}
									if(TXHData.MD_Port_Info[Port].FatherDevice[TXHData.MD_Port_Info[Port].PumpNow].Handshake != TXHData.MD_Port_Info[Port].Handshake_Top)
									{
										TXHData.MD_Port_Info[Port].FatherDevice[TXHData.MD_Port_Info[Port].PumpNow].Handshake = 0x0;
										}
									}
								if(TXHData.MD_Port_Info[Port].PumpNow >= TXHData.MD_Port_Info[Port].FatherDeviceTop)
									TXHData.MD_Port_Info[Port].PumpNow = 0;
								if(TXHData.MD_Port_Info[Port].pGunCapture)
								{
									TXHData.MD_Port_Info[Port].pGunReturn = TXHData.MD_Port_Info[Port].PumpNow;
									TXHData.MD_Port_Info[Port].PumpNow = TXHData.MD_Port_Info[Port].pGunCapture - 1;
									TXHData.MD_Port_Info[Port].pGunCapture = 0;
									}
								MD_ConnectTXTask(Port,TXHData.MD_Port_Info[Port].PumpNow,TXHData.MD_Port_Info[Port].FatherDevice[TXHData.MD_Port_Info[Port].PumpNow].MB_CMD_Next);
								}
							break;
							case PosBlueSkyPlus:
							{
								if(TXHData.MD_Port_Info[Port].LoopGunNextFlag == 1 || (TXHData.MD_Port_Info[Port].GunInfo[TXHData.MD_Port_Info[Port].PumpNow].Handshake == 0x0))
								{
									TXHData.MD_Port_Info[Port].PumpNow ++;
									if(TXHData.MD_Port_Info[Port].pGunReturn)
									{
										TXHData.MD_Port_Info[Port].PumpNow = TXHData.MD_Port_Info[Port].pGunReturn;
										TXHData.MD_Port_Info[Port].pGunReturn = 0;
										}
									if(TXHData.MD_Port_Info[Port].GunInfo[TXHData.MD_Port_Info[Port].PumpNow].Handshake != TXHData.MD_Port_Info[Port].Handshake_Top)
									{
										TXHData.MD_Port_Info[Port].GunInfo[TXHData.MD_Port_Info[Port].PumpNow].Handshake = 0x0;
										}
									}
								if(TXHData.MD_Port_Info[Port].PumpNow >= TXHData.MD_Port_Info[Port].FatherDeviceTop)
									TXHData.MD_Port_Info[Port].PumpNow = 0;
//								if(TXHData.MD_Port_Info[Port].pGunCapture)
//								{
//									TXHData.MD_Port_Info[Port].pGunReturn = TXHData.MD_Port_Info[Port].PumpNow;
//									TXHData.MD_Port_Info[Port].PumpNow = TXHData.MD_Port_Info[Port].GunInfo[TXHData.MD_Port_Info[Port].pGunCapture].FatherNum;
//									TXHData.MD_Port_Info[Port].pGunCapture = 0;
//									}
								MD_ConnectTXTask(Port,TXHData.MD_Port_Info[Port].PumpNow,TXHData.MD_Port_Info[Port].FatherDevice[TXHData.MD_Port_Info[Port].PumpNow].MB_CMD_Next);
								}
							break;
							}
						}
					}
				}
			}
		if(TXHData.MD_MultiplexTimer.Flag == 1 && TXHData.MD_Multiplex_Port_Data.PortData[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow].POSType)
		{
			TXHData.MD_MultiplexTimer.Flag = 0;
			if(TXHData.MD_Multiplex_Port_Info.DevicerTypeNow < TXHData.MD_Multiplex_Port_Data.DeviceNumber - 1)
				TXHData.MD_Multiplex_Port_Info.DevicerTypeNow ++;
			else
				TXHData.MD_Multiplex_Port_Info.DevicerTypeNow = 0;
			if(TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow].Flag)
			{
				TXHData.MD_Multiplex_Port_Info.MultiplexDevicerTimer[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow].Flag = 0;
				switch(TXHData.MD_Multiplex_Port_Data.PortData[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow].POSType)
				{
					case Multiplex_Print:
					{
						TXHData.MD_MultiplexTimer.TimerCountMax =(115200/TXHData.MD_Multiplex_Port_Data.PortData[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow].BaudRate) * 66;//根据不同波特率计算等待时间
						}
					break;
					case Multiplex_YWY:
					{
						TXHData.MD_MultiplexTimer.TimerCountMax = 1000;
						}
					break;
					case Multiplex_Price:
					{
						TXHData.MD_MultiplexTimer.TimerCountMax = 100;
						}
					break;
					case Multiplex_GPS:
					{
						TXHData.MD_MultiplexTimer.TimerCountMax = 1000;
						}
					break;
					}
					
				Multiplex_PortTXTask(TXHData.MD_Multiplex_Port_Info.DevicerTypeNow,TXHData.MD_Multiplex_Port_Info.MB_CMD_Next[TXHData.MD_Multiplex_Port_Info.DevicerTypeNow]);
				}
			}	
		}
  if(TXHData.POS_Info.ConnectType == POS_Connect_Typ_Station)
	{
		if(TXHData.POS_RS232Timer.Flag == 1)
		{
			TXHData.POS_RS232Timer.Flag = 0;
			POS_ConnectTXTask(TXHData.POS_Info.ConnectType,TXHData.POS_Info.Link_Flag,TXHData.POS_Info.POS_CMD_Next);
			}
	  }
	else if(TXHData.HeartbeatTimer.Flag == 1)
	{
		TXHData.HeartbeatTimer.Flag = 0;
		POS_ConnectTXTask(TXHData.POS_Info.ConnectType,TXHData.POS_Info.Link_Flag,TXHData.POS_Info.POS_CMD_Next); 
		}
	if(TXHData.PowerDown.State == 0)//断电就不处理数据了，防止在断电期间操作存储数据
	{
		MD_ConnectRXTask();
		Multiplex_PortRXTask();
		if(TXHData.POS_Info.POS_ConnectRXTask_Flag)
		{
			TXHData.POS_Info.POS_ConnectRXTask_Flag = 0;
			POS_ConnectRXTask();
			}
		}
  SurfaceTast();
 }
