#ifndef CONFIG_H_
#define CONFIG_H_

#include "ItemList.h"

#define DevDispType              10

typedef union{
	unsigned char D8[88];
	unsigned int D32[22];
	struct{
		unsigned char head;
		unsigned char cmd;
		unsigned char count;
		unsigned char len;
		unsigned char data[84];
   }prm;
}TUartConfigInfo;

typedef enum {
	  cfgCMD04 = 0x04,
    cfgCMD07 = 0x07,
    cfgCMD08 = 0x08,
    cfgCMD09 = 0x09,
//loop
    cfgCMD11 = 0x11,
    cfgCMD12 = 0x12,
    cfgCMD15 = 0x15,
    cfgCMD17 = 0x17,
//bus
    cfgCMD21 = 0x21,
    cfgCMD22 = 0x22,
    cfgCMD25 = 0x25,
    cfgCMD27 = 0x27,
//dir
    cfgCMD31 = 0x31,
    cfgCMD32 = 0x32,
    cfgCMD35 = 0x35,
    cfgCMD37 = 0x37,
//area
    cfgCMD41 = 0x41,
    cfgCMD42 = 0x42,
    cfgCMD45 = 0x45,
    cfgCMD47 = 0x47,
//link
    cfgCMD51 = 0x51,
    cfgCMD52 = 0x52,
    cfgCMD57 = 0x57,
//disp
    cfgCMD61 = 0x61,
    cfgCMD62 = 0x62,
    cfgCMD65 = 0x65,
    cfgCMD67 = 0x67,
//all desc
    cfgCMD71 = 0x71,
//load cfg file
    cfgCMD77 = 0x77,
//restart
    cfgCMD7F = 0x7F,
}TcfgUartCmd;

typedef union{
	unsigned char D8[56];
	unsigned int D32[14];
	struct{
		unsigned char ctlAddr;
		unsigned char loopCount;
		unsigned char busPanelCount;
		unsigned char dirPanelCount;
		
		unsigned short areaCount;
		unsigned short linkCount;
		__UINT64_TYPE__ loopCfgTab;
		unsigned int busCfgTab;
		unsigned int dirCfgTab;
		char desc[32];
	}cfg;
}TConfigCtlData;

typedef union{
	unsigned char D8[48];
	unsigned int D32[12];
	struct{
		unsigned char id;
		unsigned char devId;
		unsigned short devType;
		unsigned short assignType;
		unsigned short devProperty;
		unsigned short areaId;
		unsigned short delayTime;
		unsigned int devNum;
		char desc[32];
	}cfg;
}TConfigLoopData;

typedef union{
	unsigned char D8[24];
	unsigned int D32[6];
	struct{
		unsigned char id;
		unsigned char devId;
		unsigned short devType;
		unsigned char ord;
		unsigned char dispMode;
		unsigned short areaId[3];
		unsigned int devNum[3];
	}cfg;
}TConfigLoopDispData;

typedef union{
	unsigned char D8[44];
	unsigned int D32[11];
	struct{
		unsigned char id;
		unsigned char keyId;
		unsigned char keyCount;
		unsigned char ctlMode;
		unsigned short devType;
		unsigned short areaId;
		unsigned int serNum;
		char desc[32];
	}cfg;
}TConfigBusPanelData;

typedef union{
	unsigned char D8[44];
	unsigned int D32[11];
	struct{
		unsigned char id;
		unsigned char keyId;
		unsigned char isAble;
		unsigned char errDetFlag;
		unsigned char outProp;
		unsigned char ctlDevType;
		unsigned short areaId;
		unsigned int serNum;
		char desc[32];
	}cfg;
}TConfigDirPanelData;

typedef union{
	unsigned char D8[56];
	unsigned int D32[14];
	struct{
		unsigned short id;
		unsigned short rsv2;
		unsigned int serNum;
		char profile[16];
		char desc[32];
	}cfg;
}TConfigAreaData;

typedef union{
	unsigned char D8[512];
	unsigned int D32[128];
	struct{
		char inEqn[16][16];
		char outEqn[16][16];
	}prm;
}TEquation;

typedef union{
	unsigned char D8[524];
	unsigned int D32[131];
	struct{
		unsigned char inCount;
		unsigned char outCount;
		unsigned short inTab;
		unsigned short outTab;
		unsigned short id;
		unsigned char isStop;
		unsigned char isMask;
		unsigned char isForceAutoExec;
		unsigned char uartFlag;
		TEquation eqn;
	}cfg;
}TConfigLinkData;

typedef union{
	unsigned char D8[24];
	unsigned int D32[6];
	struct{
		unsigned char head;
		unsigned char cmd;
		unsigned char count;
		unsigned char len;
		unsigned short id;
		unsigned char isInput;
		unsigned char ord;
		char eqn[16];
	}formula;
	struct{
		unsigned char head;
		unsigned char cmd;
		unsigned char count;
		unsigned char len;
		unsigned short id;
		unsigned char endFlag;
		unsigned char inCount;
		unsigned char outCount;
		unsigned char isStop;
		unsigned char isMask;
		unsigned char isForceAutoExec;
		char rsv[12];
	}cfg;
}TUartCfgLinkData;

typedef union{
	unsigned char D8[256];
	unsigned short D16[128];
	unsigned int D32[64];
}TNorFlashPageData;

typedef union{
	unsigned char D8[4];
	unsigned int D32;
}Tint;

class TConfig{
	public:
		unsigned int iLinkBufIndex;
		unsigned int oLinkBufIndex;
	
		unsigned int usbConState;
		unsigned int loadProgress;
	
		unsigned int uartRxFlag;
		unsigned int usbCdcRxFlag;
		TUartConfigInfo * pCfgTxPkg;
		TUartConfigInfo * pCfgRxPkg;
		unsigned char cfgRpyBuf[128];
	
		unsigned int pSectorTmpAddr;
		unsigned int fileSize;
		unsigned int fileAnalysisErr;
		unsigned int dataAnalysisErr;
	
		unsigned int loopDevCount[48];
		unsigned int loopDevDispCount[48];
		unsigned int busKeyCount[24];
		unsigned int dirKeyCount[24];
		unsigned int areaCount;
		unsigned int linkCount;
	
		TConfigCtlData ctlPrm;
		TConfigLoopData loopPrm;
		TConfigLoopDispData loopDispPrm;
		TConfigBusPanelData busPrm;
		TConfigDirPanelData dirPrm;
		TConfigAreaData areaPrm;
		TConfigLinkData linkPrm;
	
		unsigned char loopCfgDevDispExistTab[48];
		unsigned char loopCfgDevTab[256];
		unsigned char busCfgKeyTab[128];
		unsigned char dirCfgKeyTab[8];
		unsigned char areaCfgTab[1024];
		unsigned char linkCfgTab[2048];
		
		TNorFlashPageData nFlashPageData;
		unsigned int norFlashAddr;
		
		//class TItemList	*IList;
		class TItemList	ItemList;
	
	public:
		TConfig(){};
		void Init();
			
		void iLinkTXBuf(unsigned char * pData);
		void oLinkTXBuf();
			
		void WaitChipIdle(unsigned int aCnt);
			
		void CfgPrmInit();
			
		void DelayMs(unsigned int aMs);
			
		unsigned short Crc16Data(unsigned char* apData,unsigned int aLen);
		unsigned int CheckCrc16(unsigned char* apData,unsigned int aLen);	
			
		unsigned int AsiiToData(unsigned char * apResult, char* apData,unsigned int aLen);
		unsigned int AsiiToData(unsigned short * apResult, char* apData,unsigned int aLen);
		unsigned int AsiiToData(unsigned int * apResult, char* apData,unsigned int aLen);
		unsigned int AsiiToData(__UINT64_TYPE__ * apResult, char* apData,unsigned int aLen);
		void GetDesc16(char * apSetData, char * apGetData);
		void GetDesc16(char * apSetData, unsigned char * apGetData);
		void GetDesc32(char * apSetData, char * apGetData);
		void GetDesc32(char * apSetData, unsigned char * apGetData);
			
		unsigned int GetUsbState(void);
		int ReadFile(char* apName);
		void GetFileLine();
		void GetFileLine(unsigned int * apItemCount);
			
		void GetCtlCfgPrm();
			
		void GetLoopCfgDevPrm(unsigned int aLineNum);	
		void GetBusCfgKeyPrm(unsigned int aLineNum);
		void GetDirCfgKeyPrm(unsigned int aLineNum);
		void GetAreaCfgPrm(unsigned int aLineNum);
		void GetLinkCfgPrm(unsigned int aLineNum);
		void GetLoopCfgDispPrm(unsigned int aLineNum);
			
		int LoadCfgFile();
		
		void WriteProjectName();
		
		void WriteLoopCfgDevTabTypeDisp();
		void WriteLoopCfgAllDisp();
		void WriteLoopCfgOneDisp(unsigned int aLoopId, unsigned int aDispId);
		void WriteLoopDevUserCodeAssignType();
		void WriteLoopOneDevUserCodeAssignType(unsigned int aLoopId, unsigned int aDevId);
		void WriteOneLoopDevUserCodeAssignType(unsigned int aLoopId);
		void WriteLoopDevArea();
		void WriteLoopOneDevArea(unsigned int aLoopId, unsigned int aDevId);
		void WriteOneLoopDevArea(unsigned int aLoopId);
		void WriteLoopDevDescp();
		void WriteLoopOneDevDescp(unsigned int aLoopId, unsigned int aDevId);
		void WriteOneLoopDevDescp(unsigned int aLoopId);
		
		void WriteBusCfgKeyNormal();
		void WriteBusOneKeyNormal(unsigned int aBusId, unsigned int aKeyId);
		void WriteOneBusKeyNormal(unsigned int aBusId);
		
		void WriteDirCfgKeyNormal();
		void WriteDirOneKeyNormal(unsigned int aDirId, unsigned int aKeyId);
		void WriteOneDirKeyNormal(unsigned int aDirId);
		void WriteDirKeyUserCodeAssignType();
		void WriteDirOneKeyUserCodeAssignType(unsigned int aDirId, unsigned int aKeyId);
		void WriteOneDirKeyUserCodeAssignType(unsigned int aDirId);
		void WriteDirKeyArea();
		void WriteDirOneKeyArea(unsigned int aDirId, unsigned int aKeyId);
		void WriteOneDirKeyArea(unsigned int aDirId);
		void WriteDirCfgDescp();
		void WriteDirOneDescp(unsigned int aDirId, unsigned int aKeyId);
		void WriteOneDirDescp(unsigned int aDirId);
		
		void WriteAreaCfg();
		
		unsigned int WriteLinkPrmToNorFlash(TConfigLinkData * pLinkPrm);
		void Erase4LinkIndex(unsigned int aInx);
		void Erase64LinkIndex(unsigned int aInx);
		void Write1LinkIndex(unsigned int aInx);
		void Write4LinkIndex(unsigned int aInx);
		void WriteAllLinkCfgToNorFlash();
		void WriteOneLinkCfgToNorFlash(unsigned int aLinkId);
		void Clear4Index(unsigned int aGroup);
		
		void WriteAllUsbCfg();
		void WriteAllUartCfg();
		
		//---------------------write to sdram---------------------------------------------------------
		void WriteCtlPrmToSdram(TConfigCtlData * pCtlPrm);
		
		unsigned int WriteLoopDevPrmToSdram(TConfigLoopData * pLoopPrm);
		unsigned int WriteLoopDevDispPrmToSdram(TConfigLoopDispData * pLoopDispPrm);
		void ClearLoopCfgDevTabInSdram(unsigned int aId);
		void WriteLoopCfgDevTabToSdram(unsigned int aId, unsigned char * pLoopCfgDevTab);
		void WriteLoopCfgOneDevInTabToSdram(unsigned int aId, unsigned int aDevId);
		
		unsigned int WriteBusKeyPrmToSdram(TConfigBusPanelData * pBusPrm);
		void ClearBusCfgKeyTabToSdram(unsigned int aId);
		void WriteBusCfgKeyTabToSdram(unsigned int aId, unsigned char * pBusCfgKeyTab);
		void WriteBusCfgOneKeyInTabToSdram(unsigned int aId, unsigned int aKeyId);
		
		unsigned int WriteDirKeyPrmToSdram(TConfigDirPanelData * pDirPrm);
		void ClearDirCfgKeyTabToSdram(unsigned int aId);
		void WriteDirCfgKeyTabToSdram(unsigned int aId, unsigned char * pDirCfgKeyTab);
		void WriteDirCfgOneKeyInTabToSdram(unsigned int aId, unsigned int aKeyId);
		
		unsigned int WriteAreaPrmToSdram(TConfigAreaData * pAreaPrm);
		void WriteAreaCfgTabToSdram(unsigned char * pAreaCfgTab);
		
		void ClearOneLinkCfgToSdram(unsigned int aId);
		unsigned int WriteLinkCfgToSdram(TConfigLinkData * pLinkPrm);
		
		//---------------------read from sdram---------------------------------------------------------
		unsigned int ReadCtlPrmFromSdram(TConfigCtlData * pCtlPrm);
		unsigned int ReadLoopDevPrmFromSdram(unsigned char aId, unsigned char aDevId, TConfigLoopData * pLoopPrm);
		void ReadLoopCfgDevTabFromSdram(unsigned int ord, unsigned char * pLoopCfgDevTab);
		unsigned int ReadBusKeyPrmFromSdram(unsigned char aId, unsigned char aKeyId, TConfigBusPanelData * pBusPrm);
		void ReadBusCfgKeyTabFromSdram(unsigned int ord, unsigned char * pBusCfgKeyTab);
		unsigned int ReadDirKeyPrmFromSdram(unsigned char aId, unsigned char aKeyId, TConfigDirPanelData * pDirPrm);
		void ReadDirCfgKeyTabFromSdram(unsigned int ord, unsigned char * pDirCfgKeyTab);
		
		unsigned int ReadAreaPrmFromSdram(unsigned int aId, TConfigAreaData * pAreaPrm);
		
		//----------------------rpy-------------------------------------------------------------
		void rpyNoData();
		void rpyData(unsigned char * pData, unsigned int len);
		void rpyCmd09();
		void rpyCmd12();
		void rpyCmd22();
		void rpyCmd32();
		void rpyCmd42();
		void rpyCmd52();
		void rpyCmd62();
		
		//------------------------------------------------------------------------------------------
		void iBufProcess();
		void oBufProcess();
		
		void Task10Ms();
		void Task1000Ms();
		
		void OperationTask();
};

#endif
