#include "fNetworkReg.h"
#include "ProtoC2C.h"
#include "Runtime.h"
#include "server.h"

#define dDescpOffSet	0x1000


#define dAddrSdRam_TypeList		(dAddrSdRam_Commom)
#define dAddrSdRam_DescpGotList	(dAddrSdRam_Commom	+ 256)
#define dAddrSdRam_DescpContent	(dAddrSdRam_Commom	+ 1024)
#define dAddrSdRam_ExistTable		(dAddrSdRam_Commom	+ 0x80000)

extern "C"{
	#include "user_norflash.h"
	#include "user_eeprom.h"
	#include "uart_key_drv.h"
	#include "task.h"
}

void TfNetworkReg::Init(int x1, int y1, int aW, int aH, int OwnerX, int OwnerY, u32 aColor)
{
	#define CH	30	//Caption Height
	unsigned short Left,Right, Top, Bottom, Width, Height, i;
	Bound.SetBySize(OwnerX + x1, OwnerY + y1, aW, aH);
	Left = Bound.Left +3;
	Top = Bound.Top+3;
	Width = aW - 7;
	Height = aH - 7;
	Right = Bound.Right -4;
	Bottom = Bound.Bottom -4;
	Caption.Init(Left,	Top,		Width, CH, 0, 0, clWhite, clMaroon);
	SText.Init	(429,		Top,		370,   CH, 0, 0, clWhite, clMaroon);
	
	Content.Set(Left, 	Top+CH, 	Right, 	Bottom);
	
	Color = aColor;
	#undef CH

	Caption.Border.Color = clGray;
	
	IsShowing = 0;
	IsOnGoing = 0;
	LoadingIndex = 0;
	IsLoadingDescp = 0;
	IsViewList = 0;
	CtlIndex = 1;
	Done = 0;
	
	DescpGo.IsStart = 0;
}

void TfNetworkReg::sDelayMs(unsigned int aMs)
{
	volatile unsigned int mTick;
	volatile unsigned int Tick;
	for(Tick=0; Tick<aMs; Tick++){
		for(mTick=0; mTick<160000; mTick++);
		sys_error_led_task();
	}
}

void TfNetworkReg::RenderBackGround(void)
{
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);
}

void TfNetworkReg::RenderBackGround(unsigned int aClr)
{
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, aClr);
}

void TfNetworkReg::DrawSelf(void)
{
	unsigned int x, y, x2, y2, w, h;
	
	VertLineRender(Bound.Left,		Bound.Top, 			Bound.Height -1,			0xFFFFFFFF);
	VertLineRender(Bound.Right -1,	Bound.Top +1,		Bound.Height -2,			0xFF808080);
	VertLineRender(Bound.Right,		Bound.Top,			Bound.Height,			0xFF404040);
	
	HorizLineRender(Bound.Left, 		Bound.Top, 			Bound.Width - 1, 			0xFFFFFFFF);
	HorizLineRender(Bound.Left +1, 	Bound.Bottom -1,  	Bound.Width - 2, 			0xFF808080);
	HorizLineRender(Bound.Left,		Bound.Bottom,		Bound.Width,			0xFF404040);
	
	x = Bound.Left +2 ;	
	x2 = Bound.Right -2;
	y = Bound.Top +2;
	y2 = Bound.Bottom - 2;
	w = Bound.Width -4;
	h = Bound.Height - 4;
	VertLineRender	(x,		y, 		h-1,	0xFF808080);
	HorizLineRender	(x,		y, 		w-1,	0xFF808080);
		
	VertLineRender	(x+1,		y+1, 		h-2,	0xFF404040);
	HorizLineRender	(x+1,		y+1, 		w-2,	0xFF404040);
	
	VertLineRender	(x2-1,	y+1, 		h-2,	0xFFD4D0C8);
	HorizLineRender	(x+1,		y2-1, 	w-2,	0xFFD4D0C8);

	VertLineRender	(x2,		y, 	  	h,	0xFFFFFFFF);
	HorizLineRender	(x,		y2, 		w,	0xFFFFFFFF);
	
	RenderBackGround();
}


void TfNetworkReg::DrawCaption2(void)
{	
	if(IsOnGoing == 0){
		TMyString::sFromStr("TAB键在面板与机器列表之间切换", SText.Text);
	}else{
		TMyString::sClear(SText.Text);
	}
	SText.Show();
}

void TfNetworkReg::Show(void)
{	
	IsShowing = 1;
	RenderBackGround();
	DrawSelf();
	ShowCaption();
	DrawCaption2();
}

void TfNetworkReg::FullRedraw(int Prm)
{	
	if(Prm == 0){
		IsShowing = 1;
		RenderBackGround();
		DrawSelf();
		ShowCaption();
		DrawCaption2();
		aPanel->Show();
		if(IsLoadingDescp){
			if(DescpGo.IsStart){
				ShowDescpState();
			}
		}else{
			DrawListStatic();
		}
	}else	
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		aPanel->Show();
		if(IsLoadingDescp){
			if(DescpGo.IsStart){
				ShowDescpState();
			}
		}else{
			DrawListStatic();
		}
	}
}

void TfNetworkReg::ShowCaption()
{
	TMyString::sFromStr("系统设置->组网机器自动注册",  Caption.Text);
	Caption.Show();
}

void TfNetworkReg::SetPanelCaptionColor()
{
	if(IsViewList == 0){
		aPanel->Caption.Color = clDeepBlue;
		aPanel->Caption.TextColor = clNearWhite;
	}else{
		aPanel->Caption.Color = clMedGray;
		aPanel->Caption.TextColor = clNearWhite;
	}
}

//______________________________________________________________________________________________________________________________
void TfNetworkReg::InitPanel(void)
{
	int i,x,y,x2,y2, aW,aH;
	unsigned int aClr, bClr;
	x = Content.Left;
	y = Content.Top;
	x2 = Content.Right;
	y2 = Content.Bottom;
	aW = Content.Width;
	aH = Content.Height;
	
	ClearData();
	LoadData();
	
	if(aPanel == nullptr)return;
	
	aPanel->Init(100,6,600,74,x,y,clFrmFace, bvRaised);
	TMyString::sFromStr("搜索组网机器", aPanel->Caption.Text);
	aPanel->Caption.Visible = 1;
	aPanel->Caption.Enable = 1;
	
	x = aPanel->Content.Left;
	y = aPanel->Content.Top;
	
	aPanel->Btn[0].Init(60,4,220,30,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[1].Init(340,4,220,30,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("    开始搜索    ");
	aPanel->Btn[1].Caption.FromStr("加载区域机描述字");
	
	aPanel->Btn[0].Visible = 1;
	aPanel->Btn[1].Visible = 1;
	aPanel->Btn[0].Enable = 1;
	aPanel->Btn[1].Enable = 1;
	
	aPanel->Btn[0].SetCaptionLeftSpacing(6);
	aPanel->Btn[1].SetCaptionLeftSpacing(6);
	
	if(MainCtl.fData.Split.NetworkMode != 1)
		aPanel->Btn[1].Enable = 0;

	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
	
	DrawList();	
}

void TfNetworkReg::InitPanel4Going(void)
{
	int i,x,y,x2,y2, aW,aH;
	unsigned int aClr, bClr;
	x = Content.Left;
	y = Content.Top;
	x2 = Content.Right;
	y2 = Content.Bottom;
	aW = Content.Width;
	aH = Content.Height;
	
	ClearData();
	
	if(aPanel == nullptr)return;
	
	aPanel->Init(100,6,600,74,x,y,clFrmFace, bvRaised);
	TMyString::sFromStr("正在搜索组网机器", aPanel->Caption.Text);
	aPanel->Caption.Visible = 1;
	aPanel->Caption.Enable = 1;
	
	x = aPanel->Content.Left;
	y = aPanel->Content.Top;
	
	aPanel->SText[0].Init(32,4,110,30,x,y,clNearBlack,clFrmFace);
	TMyString::sFromStr("正在搜索", aPanel->SText[0].Text);
	aPanel->SText[0].TextColor = clNearBlack;
	
	aPanel->SText[1].Init(2,4,30,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[1].TextClear();
	TMyString::sAddOn2Dg(1, aPanel->SText[1].Text);
	
	aPanel->SText[2].Init(446,4,102,30,x,y,clNearBlack,clFrmFace);
	TMyString::sFromStr("机器数量", aPanel->SText[2].Text);
	aPanel->SText[2].TextColor = clNearBlack;
	
	//aPanel->SText[3].Init(130,4,60,30,x,y,clNearBlack,clFrmFace);
	//aPanel->SText[3].TextClear();
	//TMyString::sFromStr("搜索完成", aPanel->SText[3].Text);
	
	aPanel->Edit[0].Init(550,4,40,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[0].Str.FromStr("00");
	aPanel->Edit[0].Enable =0;
	aPanel->Edit[0].Visible =1;
	
	RenderBackGround();
	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
	
	DrawList();
}


void TfNetworkReg::InitPanel4LoadDescp(void)
{
	int i,x,y,x2,y2, aW,aH;
	unsigned int aClr, bClr;
	x = Content.Left;
	y = Content.Top;
	x2 = Content.Right;
	y2 = Content.Bottom;
	aW = Content.Width;
	aH = Content.Height;
	
	ClearData();
	LoadData();
	
	if(aPanel == nullptr)return;
	
	aPanel->Init(100,6,600,74,x,y,clFrmFace, bvRaised);
	TMyString::sFromStr("加载描述字", aPanel->Caption.Text);
	aPanel->Caption.Visible = 1;
	aPanel->Caption.Enable = 1;
	
	x = aPanel->Content.Left;
	y = aPanel->Content.Top;
	
	aPanel->Btn[0].Init(60,4,160,30,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[1].Init(340,4,160,30,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("  全部加载  ");
	aPanel->Btn[1].Caption.FromStr("按区域机加载");
	
	aPanel->Btn[0].Visible = 1;
	aPanel->Btn[1].Visible = 1;
	aPanel->Btn[0].Enable = 1;
	aPanel->Btn[1].Enable = 1;
	
	aPanel->Btn[0].SetCaptionLeftSpacing(6);
	aPanel->Btn[1].SetCaptionLeftSpacing(6);
	
	aPanel->Edit[0].Init(290,4,40,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[0].Enable = 1;
	aPanel->Edit[0].Visible = 1;
	if(ProtoC2C.SlaveCount >0){
		LoadingIndex = 0;
		LoadingNum = ProtoC2C.SlaveList[LoadingIndex];
		aPanel->Edit[0].Str.FromUInt2Dg(LoadingNum);
	}else{
		LoadingNum = 0xFF;
		aPanel->Edit[0].Str.FromStr("xx");
		aPanel->Btn[1].Enable = 0;
	}
	RenderBackGround();
	aPanel->Show();
	CtlIndex = 2;
	Check4SelectedInDescp();
	aPanel->ReDrawItems();
	
}

//_____________________________________________________________________________________________________________________________

void TfNetworkReg::ClearData()
{
	int i,x;
	unsigned int aAddr;
	//Clear
	for(i=0; i<64; i++){
		*(volatile unsigned char *)(dAddrSdRam_TypeList + i) = 0;
		*(volatile unsigned char *)(dAddrSdRam_DescpGotList + i) = 0;
		*(volatile unsigned char *)(dAddrSdRam_ExistTable +i) =0;
	}
	for(i=0; i<64; i++){
		aAddr = dAddrSdRam_DescpContent  + i*32;
		for(x=0; x<32; x++){
			*(volatile unsigned char *)(aAddr + x) = 0;
		}
	}
	FoundIndex = 0;
	aNewGot = 0;
	SearchNumExist = 0;
	
	SearchingNum =1;
	SearchingTick =0;
	Done = 0;
}

void TfNetworkReg::ShowSearchCompleted()
{
	int x, pNum;
	unsigned int aAddr;
	
	IncMaster = 0;
	for(x=0; x<Count; x++){
		aAddr = dAddrSdRam_ExistTable + x;
		pNum = *(volatile unsigned char *)(aAddr);
		aAddr = dAddrSdRam_TypeList + pNum;
		pNum = *(volatile unsigned char *)(aAddr);
		
		if(pNum == dNetworkTypeMaster){
			IncMaster =1;
		}
	}
	
	for(x=0; x<Count; x++){
		aAddr = dAddrSdRam_ExistTable + x;
		ProtoC2C.SlaveList[x] = *(volatile unsigned char *)(aAddr);
	}
						
	if(MainCtl.fData.Split.NetworkMode == 1){
		if(IncMaster == 0){
			TMyString::sFromStr("搜索完成,按确定键保存", aPanel->SText[0].Text);
		}else{
			TMyString::sFromStr("请确保网络内只有一个集中机", aPanel->SText[0].Text);
		}
	}else{
		TMyString::sFromStr("搜索完成,只有集中机可操作保存", aPanel->SText[0].Text);
	}
	aPanel->SText[0].TextColor = clNearBlack;
	
	aPanel->SText[1].TextClear();	
	
	aPanel->SText[0].Show();
	aPanel->SText[1].Show();
}

void TfNetworkReg::ShowDescpState()
{
		TextRender_string24	(18, 	160 ,	clNearBlack, Color,	"正在加载机器号: ");
		if(DescpGo.P0 <= dPORT_MAX_COUNT) 	TextRender_string24	(18, 	200 ,	clNearBlack, Color,	"正在加载回路号:");
		else 						TextRender_string24	(18, 	200 ,	clNearBlack, Color,	"正在加载多线盘 ");
		
		TextRender_string24	(18, 	240 ,	clNearBlack, 	"剩余处理数:");
		TextRender_string24	(18, 	280 ,	clNearBlack, 	"未注册或空白数:");
		TextRender_string24	(18, 	320 ,	clNearBlack, 	"已传输数:");
		TextRender_string24	(18, 	360 ,	clNearBlack, 	"有效条目数:");
	
	
		TextDigitRender3_24	(210, 160 ,	clNearBlack, Color,	DescpGo.pNum);	
		if(DescpGo.P0 <= dPORT_MAX_COUNT)TextDigitRender3_24	(210, 200 ,	clNearBlack, Color,	DescpGo.P0);
		else TextDigitRender3_24	(210, 200 ,	clNearBlack, Color,	0);
		
		TextDigitRender3_24	(210, 240 ,	clNearBlack, Color,	DescpGo.RemainCount);
		TextDigitRender3_24	(210, 280 ,	clNearBlack, Color,	DescpGo.EmptyCount );
		if(DescpGo.P0 <= dPORT_MAX_COUNT)TextDigitRender3_24	(210, 320 ,	clNearBlack, Color,	250 - DescpGo.RemainCount);
		else TextDigitRender3_24	(210, 320 ,	clNearBlack, Color,	160 - DescpGo.RemainCount);
		TextDigitRender5Left24	(210, 360 ,	clNearBlack, Color,	DescpGo.AvaEntry);
	
	if(DescpGo.Err){
		TextRender_string24	(18, 	400 ,	clNearBlack, Color,	"传输错误, 请退出        ");
	}else
	if(DescpGo.IsSaving){
		if(DescpGo.P0 <= dPORT_MAX_COUNT) 	TextRender_string24	(18, 	400 ,	clNearBlack, Color,	"正在保存回路,请等待  ");
		else 						TextRender_string24	(18, 	400 ,	clNearBlack, Color,	"正在保存多线盘,请等待");
	}else
	if(DescpGo.IsSaveOk){
		if(DescpGo.P0 <= dPORT_MAX_COUNT) 	TextRender_string24	(18, 	400 ,	clNearBlack, Color,	"回路保存完成,请等待  ");
		else 						TextRender_string24	(18, 	400 ,	clNearBlack, Color,	"多线盘保存完成,请等待");
	}else
	if(DescpGo.Completed){
		TextRender_string24	(18, 	400 ,	clNearBlack, Color,	"操作完成, 请退出        ");
	}
}


void TfNetworkReg::ShowSaveOk()
{
	TMyString::sFromStr("保存成功", aPanel->SText[0].Text);
	aPanel->SText[0].TextColor = clNearBlack;
	
	aPanel->SText[1].TextClear();	
	
	aPanel->SText[0].Show();
	aPanel->SText[1].Show();
}

void TfNetworkReg::ShowSaveNotOk()
{
	TMyString::sFromStr("写入失败", aPanel->SText[0].Text);
	aPanel->SText[0].TextColor = clNearBlack;
	
	aPanel->SText[1].TextClear();	
	
	aPanel->SText[0].Show();
	aPanel->SText[1].Show();
}

void TfNetworkReg::LoadData()
{
	int i,x;
	unsigned int aAddr;
	//Load Here
	Count = 0;
	
	//Clac
	PageCount = Count / 10;
	if(Count % 10){
		PageCount++;
	}
	PageIndex = 0;
}

void TfNetworkReg::LoadData2Last()
{
	int i,x;
	unsigned int aAddr;
	//Load Here
	Count = 0;
	for(i=0; i<dNETWORK_MACHINE_COUNT; i++){
		aAddr = dAddrSdRam_ExistTable + i;
		if(*(volatile unsigned char *)(aAddr)){
			Count++;
		}
	}
	
	//Clac
	PageCount = Count / 10;
	if(Count % 10){
		PageCount++;
	}
	PageIndex = PageCount -1;
	if(PageIndex < 0)PageIndex = 0;
}


void TfNetworkReg::DrawList()
{
	int x, y, z;
	unsigned char aP0, aMcType;
	unsigned int aClr, aAddr, wc;
	unsigned int fInx,sInx, fCount, pNum;

	if(IsViewList)aClr = clBlue; else aClr = clDeepGray;

	RectFillRender		(13, 	159, 	786, 188, aClr);
	TextRender_string24	(18, 	161 ,	clNearWhite, 	"机器列表");
	
	RectFillRender(6,152,10,430,aClr);
	RectFillRender(6,152,792,156,aClr);
	RectFillRender(6,430,792,434,aClr);
	RectFillRender(788,152,792,434,aClr);
	
	RectFillRender(11,189,787,429,clMoneyGreen);
	
	wc =0;
	fInx = PageIndex * 10;
	for(; fInx<Count; fInx++){
		aAddr = dAddrSdRam_ExistTable + fInx;
		pNum = *(volatile unsigned char *)(aAddr);
		
		TMyString::sFrom2Dg(fInx,Text);
		TMyString::sAddOnStr(".机器号:",Text);
		TMyString::sFrom2Dg(pNum,Text);

		aAddr = dAddrSdRam_TypeList + pNum;
		pNum = *(volatile unsigned char *)(aAddr);
		
		if(pNum == dNetworkTypeMaster){
			TMyString::sAddOnStr("  类型:集中机 ",Text);
		}else{
			TMyString::sAddOnStr("  类型:区域机 ",Text);
		}
		
		wc++;
		if(wc>=10)break;
	}
	
	sInx = PageIndex * 10;
	TVScrollBar::sDrawByPoint(767, 190, 786, 428, aClr, clGray, Count, 14, sInx);
}

void TfNetworkReg::DrawListStatic()
{
	int x, y, z;
	unsigned char aP0, aMcType;
	unsigned int aClr, aAddr, wc;
	unsigned int fInx,sInx, fCount, pNum;

	if(IsViewList)aClr = clBlue; else aClr = clDeepGray;

	RectFillRender		(13, 	159, 	786, 188, aClr);
	TextRender_string24	(18, 	161 ,	clNearWhite, 	"机器列表");
	
	RectFillRender(6,152,10,430,aClr);
	RectFillRender(6,152,792,156,aClr);
	RectFillRender(6,430,792,434,aClr);
	RectFillRender(788,152,792,434,aClr);
	
	RectFillRender(11,189,787,429,clMoneyGreen);
	
	wc =0;
	fInx = PageIndex * 10;
	for(; fInx<Count; fInx++){
		aAddr = dAddrSdRam_ExistTable + fInx;
		pNum = *(volatile unsigned char *)(aAddr);
		
		TMyString::sFrom2Dg(fInx,Text);
		TMyString::sAddOnStr(".机器号:",Text);
		TMyString::sFrom2Dg(pNum,Text);

		aAddr = dAddrSdRam_TypeList + pNum;
		pNum = *(volatile unsigned char *)(aAddr);
		
		if(pNum == dNetworkTypeMaster){
			TMyString::sAddOnStr("  类型:集中机 ",Text);
		}else{
			TMyString::sAddOnStr("  类型:区域机 ",Text);
		}
		
		TextRender_string24(20, 192 + wc * 28, clNearBlack, Text);
		
		wc++;
		if(wc>=10)break;
	}
	
	sInx = PageIndex * 10;
	TVScrollBar::sDrawByPoint(767, 190, 786, 428, aClr, clGray, Count, 14, sInx);
}

void TfNetworkReg::ClearOutText()
{
	TMyString::sClear(aPanel->SText[0].Text);
	aPanel->SText[0].Show();
}

void TfNetworkReg::Check4Selected()
{
	aPanel->Btn[0].Selected = 0;
	aPanel->Btn[1].Selected = 0;
	if(IsOnGoing == 0){
		if(IsViewList ==0){
			if(CtlIndex == 0){
				aPanel->Btn[0].Selected = 1;
			}else
			if(CtlIndex == 1){
				aPanel->Btn[1].Selected = 1;
			}
		}
	}
}

void TfNetworkReg::Check4SelectedInDescp()
{
	aPanel->Btn[0].Selected = 0;
	aPanel->Edit[0].Selected = 0;
	aPanel->Btn[1].Selected = 0;

	if(CtlIndex == 0){
		aPanel->Btn[0].Selected = 1;
	}else
	if(CtlIndex == 1){
		aPanel->Edit[0].Selected = 1;
	}
	if(CtlIndex == 2){
		aPanel->Btn[1].Selected = 1;
	}
}

void TfNetworkReg::SendNwList()
{
	//First Pkg     
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Identification =0;
	ProtoC2C.TxMsg.Data.Priority = 1;
	
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 4;
	ProtoC2C.TxMsg.Data.Data[0] =0;
	ProtoC2C.TxMsg.Data.Data[1] = ProtoC2C.MasterNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = ProtoC2C.SlaveCount;
	
	ProtoC2C.oBufInsert();
	
	//2th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =1;
	ProtoC2C.TxMsg.Data.Data[1] = ProtoC2C.MasterNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = ProtoC2C.SlaveCount;
	ProtoC2C.TxMsg.Data.Data[4] = ProtoC2C.SlaveList[0];
	ProtoC2C.TxMsg.Data.Data[5] = ProtoC2C.SlaveList[1];
	ProtoC2C.TxMsg.Data.Data[6] = ProtoC2C.SlaveList[2];
	ProtoC2C.TxMsg.Data.Data[7] = ProtoC2C.SlaveList[3];
	
	ProtoC2C.oBufInsert();

	//3th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =2;
	ProtoC2C.TxMsg.Data.Data[1] = ProtoC2C.MasterNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = ProtoC2C.SlaveCount;
	ProtoC2C.TxMsg.Data.Data[4] = ProtoC2C.SlaveList[4];
	ProtoC2C.TxMsg.Data.Data[5] = ProtoC2C.SlaveList[5];
	ProtoC2C.TxMsg.Data.Data[6] = ProtoC2C.SlaveList[6];
	ProtoC2C.TxMsg.Data.Data[7] = ProtoC2C.SlaveList[7];
	
	ProtoC2C.oBufInsert();
	
	//4th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =3;
	ProtoC2C.TxMsg.Data.Data[1] = ProtoC2C.MasterNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = ProtoC2C.SlaveCount;
	ProtoC2C.TxMsg.Data.Data[4] = ProtoC2C.SlaveList[8];
	ProtoC2C.TxMsg.Data.Data[5] = ProtoC2C.SlaveList[9];
	ProtoC2C.TxMsg.Data.Data[6] = ProtoC2C.SlaveList[10];
	ProtoC2C.TxMsg.Data.Data[7] = ProtoC2C.SlaveList[11];
	
	ProtoC2C.oBufInsert();
	
	//5th Pkg     
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 8;
	ProtoC2C.TxMsg.Data.Data[0] =4;
	ProtoC2C.TxMsg.Data.Data[1] = ProtoC2C.MasterNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_SaveData;
	ProtoC2C.TxMsg.Data.Data[3] = ProtoC2C.SlaveCount;
	ProtoC2C.TxMsg.Data.Data[4] = ProtoC2C.SlaveList[12];
	ProtoC2C.TxMsg.Data.Data[5] = ProtoC2C.SlaveList[13];
	ProtoC2C.TxMsg.Data.Data[6] = ProtoC2C.SlaveList[14];
	ProtoC2C.TxMsg.Data.Data[7] = ProtoC2C.SlaveList[15];
	
	ProtoC2C.oBufInsert();
}



TGuiMsgReturn TfNetworkReg::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Err;
	unsigned char x,y,aCh;
	unsigned int aAddr;
	TDMix aFullPath;

	if(IsOnGoing){
		switch(aKey){
			case VK_EXECUTE:
				if(Done){
					//Do Save Here
					if( (MainCtl.fData.Split.NetworkMode == 1) && (IncMaster == 0) ){
						ProtoC2C.MasterNum = MainCtl.fData.Split.MyNum;
						Count = GetCtlCount();
						ProtoC2C.SlaveCount = Count;
						for(x=0; x<dNETWORK_MACHINE_COUNT; x++)ProtoC2C.SlaveList[x] = 0;
						for(x=0; x<Count; x++){
							aAddr = dAddrSdRam_ExistTable + x;
							ProtoC2C.SlaveList[x] = *(volatile unsigned char *)(aAddr);
						}
						Record.SaveNwCfg(ProtoC2C.MasterNum, Count, ProtoC2C.SlaveList);
						Record.ReadNwCfg();
						if( (ProtoC2C.MasterNum == MainCtl.fData.Split.MyNum) && (Count == ProtoC2C.SlaveCount) ){
							ShowSaveOk();
							SendNwList();
							Record.OperateAdd_RegCanNetwork(1, MainCtl.fData.Split.MyNum, Count);
						}else{
							ShowSaveNotOk();
						}
					}
					ProtoC2C.SyncCheckOnline();
				}
				break;
			case VK_RETURN:
				IsShowing = 0;
				IsOnGoing = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}else
	if(IsLoadingDescp){
		switch(aKey){
			case VK_LEFT:
				if(DescpGo.IsStart == 0){
					if(CtlIndex >0){
						CtlIndex--;
						Check4SelectedInDescp();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}
				}
				break;
			case VK_RIGHT:
				if(DescpGo.IsStart == 0){
					if(CtlIndex <2){
						CtlIndex++;
						Check4SelectedInDescp();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}
				}
				break;
			case VK_UP:
				if(DescpGo.IsStart == 0){
					if(ProtoC2C.SlaveCount == 0){
						LoadingNum = 0xFF;
					}else{
						if(CtlIndex == 1){
							if(LoadingIndex < (ProtoC2C.SlaveCount-1)){
								LoadingIndex++;
								LoadingNum = ProtoC2C.SlaveList[LoadingIndex];
								aPanel->Edit[0].Str.FromUInt2Dg(LoadingNum);
								aPanel->Edit[0].Show();
							}
						}
					}
				}
				break;
			case VK_DOWN:
				if(DescpGo.IsStart == 0){
					if(ProtoC2C.SlaveCount == 0){
						LoadingNum = 0xFF;
					}else{
						if(CtlIndex == 1){
							if(LoadingIndex >0){
								LoadingIndex--;
								LoadingNum = ProtoC2C.SlaveList[LoadingIndex];
								aPanel->Edit[0].Str.FromUInt2Dg(LoadingNum);
								aPanel->Edit[0].Show();
							}
						}
					}
				}
				break;
			case VK_EXECUTE:
				if(DescpGo.IsStart == 0){
					if(CtlIndex ==0){
						if(ProtoC2C.SlaveCount){
							ClearDescpGoData();
							DescpGo.BusyTick = 0;
							DescpGo.IsBusy = 1;
							RectFillRender(16,160,760,429,clMoneyGreen);
							TextRender_string24	(18, 	160 ,	clNearBlack, 	"等待区域机预加载");
							DescpGo.IsAll = 1;
							DescpGo.IsStart = 1;
						}
					}else
					if(CtlIndex  ==2){
						if(ProtoC2C.SlaveCount){
							//Get pNum
							if( (LoadingNum >0) && (LoadingNum <= dNETWORK_MACHINE_COUNT) ){
								ClearDescpGoData();
								DescpGo.BusyTick = 0;
								DescpGo.IsBusy = 1;
								RectFillRender(16,160,760,429,clMoneyGreen);
								TextRender_string24	(18, 	160 ,	clNearBlack, 	"等待区域机预加载");
								DescpGo.pNum = LoadingNum;
								DescpGo.IsAll = 0;
								DescpGo.IsStart = 1;
							}
						}
					}
				}
				break;
			case VK_RETURN:
				IsLoadingDescp = 0;
				DescpGo.IsStart = 0;
				InitPanel();
				break;			
		}
	}else{
		if(IsViewList){
			switch(aKey){
				case VK_UP:
					if(PageIndex >0){
						PageIndex--;
						DrawList();
					}
					break;
				case VK_DOWN:
					if(PageIndex <(PageCount-1)){
						PageIndex++;
						DrawList();
					}
					break;
				case VK_TAB:
					IsViewList = 0;
					DrawList();
					Check4Selected();
					SetPanelCaptionColor();
					aPanel->ReDrawItems();
					break;
				case VK_RETURN:
					IsShowing = 0;
					aMsg = guiMsgReturn;
					break;
			}		
		}else{
			switch(aKey){
				case VK_LEFT:
					if(CtlIndex >0){
						CtlIndex--;
						Check4Selected();
						aPanel->ReDrawItems();
					}
					break;
				case VK_RIGHT:
					if(CtlIndex <1){
						CtlIndex++;
						Check4Selected();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}
					break;
				case VK_TAB:
					IsViewList = 1;
					DrawList();
					Check4Selected();
					SetPanelCaptionColor();
					aPanel->ReDrawItems();
					break;
				case VK_EXECUTE:
					if(CtlIndex == 0){
						ClearData();
						IsOnGoing = 1;
						IsViewList = 1;
						InitPanel4Going();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}else
					if(CtlIndex == 1){
						IsLoadingDescp = 1;
						InitPanel4LoadDescp();
						SetPanelCaptionColor();
						aPanel->ReDrawItems();
					}
					break;
				case VK_RETURN:
					IsShowing = 0;
					aMsg = guiMsgReturn;
					break;
			}
		}		
	}

	return aMsg;	
}

void TfNetworkReg::SendfNetMsg(unsigned char aSubCmd, unsigned char *data, int Len)
{

}

void TfNetworkReg::PushfNetMsg(unsigned char aSubCmd, unsigned char *data, int Len)
{
	//In Ext CAN 
	//Cmd=251, D0=CTLBYTE, D1=SubCmd, D2=CtlNum, D3=(Type,Index,IndexCompleted), D4..7=Text
	
	//In Uart
	//CmdC2C_RpyExist		:	D0=Len, D1=Pri, D2=CMD, D3=CTLBYTE D4=SubCmd 		D5=CtlNum  D6=Exist	(0xFF)
	//CmdC2C_RpyType		:    	D0=Len, D1=Pri, D2=CMD, D3=CTLBYTE D4=SubCmd 		D5=CtlNum  D6=Type	(Master or Slave, See Define)
	//CmdC2C_RpyNameLen	:	D0=Len, D1=Pri, D2=CMD, D3=CTLBYTE D4=SubCmd 		D5=CtlNum  D6=NameLen	(0x)
	//CmdC2C_RpyNameContent	:    	D0=Len, D1=Pri, D2=CMD, D3=CTLBYTE D4=SubCmd 		D5=CtlNum  D6=Index	(Index X, D4=ThisPkgByteLen, D7..10=Char[0..3])
	//CmdC2C_RpyNameContent	:    	D0=Len, D1=Pri, D2=CMD, D3=CTLBYTE D4=SubCmd 		D5=CtlNum  D6=Index	(When Index = 0xFF, Completed)
	int i,x, aLen;
	unsigned int aAddr;
	unsigned char ada;
	unsigned char aP0;
	
	RecBlock = 1;
	Path = data[1];
	ada = data[3];
	switch(aSubCmd){
		case CmdC2C_RpyExist:
			break;
		case CmdC2C_RpyType:
			if( (ada == dNetworkTypeMaster) || (ada == dNetworkTypeSlave) ){
				aP0 = Path;
				if( (aP0) && (aP0 <=dNETWORK_MACHINE_COUNT) ){
					SearchNumExist = 1;
					aAddr = dAddrSdRam_TypeList + aP0;
					*(volatile unsigned char *)(aAddr)= ada;
					aAddr = dAddrSdRam_ExistTable + FoundIndex;
					*(volatile unsigned char *)(aAddr)= aP0;
					if(FoundIndex <=dNETWORK_MACHINE_COUNT)
						FoundIndex++;
					Count++;
				}
			}
			break;
		case CmdC2C_RpyNameLen:
			
			break;
		case CmdC2C_RpyNameContent:
			if(ada == 0xFF){
				aP0 = Path -1;
				if(aP0 <64){
					aNewGot = 1;
					aAddr = dAddrSdRam_DescpGotList + aP0;
					*(volatile unsigned char *)(aAddr)= 1;
				}
			}else{
				if(ada < 8){
					x = ada * 4;
					aLen = data[4];
					aP0 = Path -1;
					if(  (aLen < 5) && (aP0 < 64) ){
						aAddr = dAddrSdRam_DescpContent + aP0 * 32;
						for(i=0; i<aLen; i++){
							*(volatile unsigned char *)(aAddr + i)= data[5 + i];
						}
					}
				}
			}
			break;
	}	
}

unsigned char TfNetworkReg::GetRecType(unsigned char aNum)
{
	unsigned char aType;
	unsigned int aP0, aAddr;
	aP0 = aNum -1;
	aAddr = dAddrSdRam_TypeList + aP0;
	aType = *(volatile unsigned char *)(aAddr + aP0);
	return aType;
}

unsigned char TfNetworkReg::CheckRecTypeAva(unsigned char aNum)
{
	unsigned char aType;
	unsigned int aP0, aAddr;
	aP0 = aNum -1;
	aAddr = dAddrSdRam_TypeList + aP0;
	aType = *(volatile unsigned char *)(aAddr + aP0);
	if( (aType == dNetworkTypeMaster) || (aType == dNetworkTypeSlave) ){
		return 1;
	}else{
		return 0;
	}
}

unsigned char TfNetworkReg::CheckDescpAva(unsigned char aNum)
{
	unsigned char aOk;
	unsigned int aP0, aAddr;
	aP0 = aNum -1;
	aAddr = dAddrSdRam_DescpGotList + aP0;
	aOk = *(volatile unsigned char *)(aAddr + aP0);
	return aOk;
}

unsigned char TfNetworkReg::GetCtlCount()
{
	unsigned char aType;
	unsigned int i, aAddr;

	Count =0;
	CountMater =0;
	CountSlave =0;
	aAddr = dAddrSdRam_TypeList;
	for(i=1; i<=dNETWORK_MACHINE_COUNT; i++){
		aType = *(volatile unsigned char *)(aAddr + i);
		if( (aType == dNetworkTypeMaster) || (aType == dNetworkTypeSlave) ){
			Count ++;
			if(aType == dNetworkTypeMaster) CountMater++;
			else CountSlave++;
		}
	}
	return Count;
}

void TfNetworkReg::DrawCount()
{
	aPanel->Edit[0].Str.FromInt(Count);
	aPanel->Edit[0].Show();
}

void TfNetworkReg::SendAskType(unsigned char aNum)
{
	//CmdC2C_RpyType		:    	D0=Len, D1=Pri, D2=CMD, 		D3=CTLBYTE D4=SubCmd 		D5=CtlNum  D6=Type	(Master or Slave, See Define)
	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Identification =0;
	ProtoC2C.TxMsg.Data.Priority = 1;
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 4;
	ProtoC2C.TxMsg.Data.Data[0] =0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_AskType;
	ProtoC2C.TxMsg.Data.Data[3] = aNum;
	
	ProtoC2C.oBufInsert();
}

void TfNetworkReg::SendAskDescp(unsigned char aNum)
{
	//CmdC2C_RpyNameContent	:    	D0=Len, D1=Pri, D2=CMD, 		D3=CTLBYTE D4=SubCmd 		D5=CtlNum  D6=Index	(Index X, D4=ThisPkgByteLen, D7..10=Char[0..3])

	ProtoC2C.TxMsg.Data.Routing =0;
	ProtoC2C.TxMsg.Data.Identification =0;
	ProtoC2C.TxMsg.Data.Priority = 1;
	ProtoC2C.TxMsg.Data.Cmd = dNetworkCmd;
	ProtoC2C.TxMsg.Data.Len = 4;
	ProtoC2C.TxMsg.Data.Data[0] =0;
	ProtoC2C.TxMsg.Data.Data[1] = MainCtl.fData.Split.MyNum;
	ProtoC2C.TxMsg.Data.Data[2] = CmdC2C_AskNameContent;	
	ProtoC2C.TxMsg.Data.Data[3] = aNum ;
	
	ProtoC2C.oBufInsert();
}

void TfNetworkReg::SavePortDescp(unsigned char aPNum, unsigned char aP0)
{
	unsigned int fAddr, sdAddr;
	unsigned int i,m;
	volatile unsigned int x, Sta, xy, inx, inx2;
	unsigned int P0;
	
	if(aPNum == 0)return;
	if(aPNum > dNETWORK_MACHINE_COUNT)return;
	if(aP0 ==0)return;
	if(aP0 >= dPORT_MAX_COUNT)return;
	
	P0 = aP0-1;
	do{
		sDelayMs(5);
		Sta = nor_get_busy_state(NwDescpUsingChip[aPNum]);
	}while(Sta);
	
	//Erase 2 * 4K Blocks
	for(x=0; x<2; x++){
		do{
			sDelayMs(20);
			Sta = nor_get_busy_state(NwDescpUsingChip[aPNum]);
		}while(Sta);
		fAddr = NwDescpUsingFlashAddr[aPNum] + (SizeOf4K * (P0 * 2 + x) );
		nor_erase_sector_4k(NwDescpUsingChip[aPNum],fAddr);
		do{
			sDelayMs(50);
			Sta = nor_get_busy_state(NwDescpUsingChip[aPNum]);
		}while(Sta);
	}
	
	for(x=0; x<32; x++){
		fAddr = NwDescpUsingFlashAddr[aPNum] + (SizeOf4K * (P0 * 2)) + (x * 256);
		for(xy=0; xy<8; xy++){
			inx = x*8 + xy;
			for(i=0; i<8;i++){
				inx2 = xy*8 + i;
				ReadWriteData.D32[inx2] = PortDevDescp.Body[inx].D32[i];
			}
		}
		
		if(ReadWriteData.D32[0] == 0xFFFFFFFF)ReadWriteData.D32[0] = 0;
		if(ReadWriteData.D32[8] == 0xFFFFFFFF)ReadWriteData.D32[8] = 0;
		if(ReadWriteData.D32[16] == 0xFFFFFFFF)ReadWriteData.D32[16] = 0;
		if(ReadWriteData.D32[24] == 0xFFFFFFFF)ReadWriteData.D32[24] = 0;
		if(ReadWriteData.D32[32] == 0xFFFFFFFF)ReadWriteData.D32[32] = 0;
		if(ReadWriteData.D32[40] == 0xFFFFFFFF)ReadWriteData.D32[40] = 0;
		if(ReadWriteData.D32[48] == 0xFFFFFFFF)ReadWriteData.D32[48] = 0;
		if(ReadWriteData.D32[56] == 0xFFFFFFFF)ReadWriteData.D32[56] = 0;
		
		nor_write_page(NwDescpUsingChip[aPNum], fAddr, ReadWriteData.D8);
		do{
			sDelayMs(5);
			Sta = nor_get_busy_state(NwDescpUsingChip[aPNum]);
		}while(Sta);
	}
}

void TfNetworkReg::ReadPortDescp(unsigned char aPNum, unsigned char aP0)
{
	unsigned int fAddr, sdAddr;
	unsigned int i,m;
	volatile unsigned int x, Sta;
	unsigned int Pr, P0;
	
	if(aPNum == 0)return;
	if(aPNum > dNETWORK_MACHINE_COUNT)return;
	if(aP0 ==0)return;
	if(aP0 >=dPORT_MAX_COUNT)return;
	
	SysLoopTick = 0;
	Pr = aPNum -1;
	P0 = aP0-1;
	do{
		sDelayMs(5);
		Sta = nor_get_busy_state(NwDescpUsingChip[aPNum]);
	}while(Sta);
	
	for(x=0; x<32; x++){
		
		SysLoopTick = 0;
		fAddr = NwDescpUsingFlashAddr[aPNum] + (SizeOf4K * (P0 * 2)) + (x * 256);
		sdAddr = dAddrSdRam_DescpNw + (Pr * dONE_CTRL_DESCP_BYTES) + (P0 * 8192) + (x * 256);
		nor_read_page(NwDescpUsingChip[aPNum], fAddr, ReadWriteData.D8);
		
		if(ReadWriteData.D32[0] == 0xFFFFFFFF)ReadWriteData.D32[0] = 0;
		if(ReadWriteData.D32[8] == 0xFFFFFFFF)ReadWriteData.D32[8] = 0;
		if(ReadWriteData.D32[16] == 0xFFFFFFFF)ReadWriteData.D32[16] = 0;
		if(ReadWriteData.D32[24] == 0xFFFFFFFF)ReadWriteData.D32[24] = 0;
		if(ReadWriteData.D32[32] == 0xFFFFFFFF)ReadWriteData.D32[32] = 0;
		if(ReadWriteData.D32[40] == 0xFFFFFFFF)ReadWriteData.D32[40] = 0;
		if(ReadWriteData.D32[48] == 0xFFFFFFFF)ReadWriteData.D32[48] = 0;
		if(ReadWriteData.D32[56] == 0xFFFFFFFF)ReadWriteData.D32[56] = 0;
		
		sdAddr = dAddrSdRam_DescpNw + (Pr * dONE_CTRL_DESCP_BYTES) + (P0 * 8192) + (x * 256);
		for(i=0; i<64;i++){
			*(volatile unsigned int *)(sdAddr + i*4) = ReadWriteData.D32[i];
		}
		sys_error_led_task();
	}
}


void TfNetworkReg::SaveDirectPadDescp(unsigned char aPNum)
{
	unsigned int fAddr, sdAddr;
	unsigned int i,m;
	volatile unsigned int x, Sta, xy, inx, inx2;
	
	if(aPNum == 0)return;
	if(aPNum > dNETWORK_MACHINE_COUNT)return;

	do{
		sDelayMs(5);
		Sta = nor_get_busy_state(NwDescpUsingChip[aPNum]);
	}while(Sta);
	
	//Erase 2 * 4K Blocks
	for(x=0; x<2; x++){
		do{
			sDelayMs(20);
			Sta = nor_get_busy_state(NwDescpUsingChip[aPNum]);
		}while(Sta);
		fAddr = NwDescpUsingFlashAddr[aPNum] + 327680 + (x * 4096);
		nor_erase_sector_4k(NwDescpUsingChip[aPNum],fAddr);
		do{
			sDelayMs(50);
			Sta = nor_get_busy_state(NwDescpUsingChip[aPNum]);
		}while(Sta);
	}
	
	for(x=0; x<32; x++){
		fAddr = NwDescpUsingFlashAddr[aPNum] + 327680 + (x * 256);
		for(xy=0; xy<8; xy++){
			inx = x*8 + xy;
			for(i=0; i<8;i++){
				inx2 = xy*8 + i;
				ReadWriteData.D32[inx2] = PortDevDescp.Body[inx].D32[i];
			}
		}
		
		if(ReadWriteData.D32[0] == 0xFFFFFFFF)ReadWriteData.D32[0] = 0;
		if(ReadWriteData.D32[8] == 0xFFFFFFFF)ReadWriteData.D32[8] = 0;
		if(ReadWriteData.D32[16] == 0xFFFFFFFF)ReadWriteData.D32[16] = 0;
		if(ReadWriteData.D32[24] == 0xFFFFFFFF)ReadWriteData.D32[24] = 0;
		if(ReadWriteData.D32[32] == 0xFFFFFFFF)ReadWriteData.D32[32] = 0;
		if(ReadWriteData.D32[40] == 0xFFFFFFFF)ReadWriteData.D32[40] = 0;
		if(ReadWriteData.D32[48] == 0xFFFFFFFF)ReadWriteData.D32[48] = 0;
		if(ReadWriteData.D32[56] == 0xFFFFFFFF)ReadWriteData.D32[56] = 0;
		
		nor_write_page(NwDescpUsingChip[aPNum], fAddr, ReadWriteData.D8);
		do{
			sDelayMs(5);
			Sta = nor_get_busy_state(NwDescpUsingChip[aPNum]);
		}while(Sta);
	}
}

void TfNetworkReg::ReadDirectPadDescp(unsigned char aPNum)
{
	unsigned int fAddr, sdAddr;
	unsigned int i,m;
	volatile unsigned int x, Sta;
	unsigned int Pr;
	
	if(aPNum == 0)return;
	if(aPNum > dNETWORK_MACHINE_COUNT)return;
	
	Pr = aPNum -1;
	
	SysLoopTick = 0;
	do{
		sDelayMs(5);
		Sta = nor_get_busy_state(NwDescpUsingChip[aPNum]);
	}while(Sta);
	
	for(x=0; x<32; x++){
		SysLoopTick = 0;
		fAddr = NwDescpUsingFlashAddr[aPNum] + 327680 + (x * 256);
		sdAddr = dAddrSdRam_DescpNw + (Pr * dONE_CTRL_DESCP_BYTES) + 327680 + (x * 256);
		nor_read_page(NwDescpUsingChip[aPNum], fAddr, ReadWriteData.D8);
		
		if(ReadWriteData.D32[0] == 0xFFFFFFFF)ReadWriteData.D32[0] = 0;
		if(ReadWriteData.D32[8] == 0xFFFFFFFF)ReadWriteData.D32[8] = 0;
		if(ReadWriteData.D32[16] == 0xFFFFFFFF)ReadWriteData.D32[16] = 0;
		if(ReadWriteData.D32[24] == 0xFFFFFFFF)ReadWriteData.D32[24] = 0;
		if(ReadWriteData.D32[32] == 0xFFFFFFFF)ReadWriteData.D32[32] = 0;
		if(ReadWriteData.D32[40] == 0xFFFFFFFF)ReadWriteData.D32[40] = 0;
		if(ReadWriteData.D32[48] == 0xFFFFFFFF)ReadWriteData.D32[48] = 0;
		if(ReadWriteData.D32[56] == 0xFFFFFFFF)ReadWriteData.D32[56] = 0;
		
		for(i=0; i<64;i++){
			*(volatile unsigned int *)(sdAddr + i*4) = ReadWriteData.D32[i];
		}
		sys_error_led_task();
	}
}

void TfNetworkReg::Task1000Ms()
{
	if(IsShowing){
		if(IsOnGoing){
			PreventPermissionDown();
			PreventEventShow();
			if(Done){
				
			}else{
				if(SearchingTick >(dNETWORK_MACHINE_COUNT +4)){
					SearchingTick = (dNETWORK_MACHINE_COUNT +4);
					Count = GetCtlCount();
					DrawCount();
					LoadData2Last();
					DrawListStatic();
					//ShowSearchCompleted();
					Done = 1;
				}else
				if( (SearchingTick) && (SearchingTick <= dNETWORK_MACHINE_COUNT) ){
					SearchingNum = SearchingTick;
					SendAskType(SearchingNum);
					aPanel->SText[1].TextClear();
					TMyString::sAddOn2Dg(SearchingNum, aPanel->SText[1].Text);
					aPanel->SText[1].Show();
					
					Count = GetCtlCount();
					DrawCount();
					LoadData2Last();
					DrawListStatic();
				}else{
					Count = GetCtlCount();
					DrawCount();
					LoadData2Last();
					DrawListStatic();
				}
				
				SearchingTick++;
			}
		}
	}
}

void TfNetworkReg::PortExistGet(unsigned char aNum, unsigned char aP0, unsigned char aExist)
{
	if(DescpGo.pNum == aNum){
		if(  (aP0) && (aP0 <= (dPORT_MAX_COUNT +1)) ){
			DescpGo.P0ExistTable[aP0] = aExist;
		}
	}
}

void TfNetworkReg::DescpListGet(unsigned char aNum, unsigned char aP0, unsigned char Addr, unsigned char *Data)
{
	unsigned char Inx = Data[0];
	unsigned int Pst;
	if( (aNum == DescpGo.pNum) && (aP0 == DescpGo.P0) ){
		if( Inx <8 ){
			DescpGo.P1GetTable[Addr] |= 1<< (Inx);
			Pst = Inx * 4;
			PortDevDescp.Body[Addr].D8[Pst++] = Data[4];
			PortDevDescp.Body[Addr].D8[Pst++] = Data[5];
			PortDevDescp.Body[Addr].D8[Pst++] = Data[6];
			PortDevDescp.Body[Addr].D8[Pst] = Data[7];
		}else
		if( Inx == 0x33 ){
			DescpGo.P1GetTable[Addr] = 0x80;
		}
	}
	DescpGo.BusyTick=0;
}

void TfNetworkReg::ClearDescpContent()
{
	int i,x;
	for(i=0; i<256; i++){
		for(x=0; x<8; x++){
			PortDevDescp.Body[i].D32[x] = 0;
		}
	}
}

void TfNetworkReg::ClearDescpGoData()
{
	int x;
	DescpGo.pNumInx =0;
	DescpGo.NoUpdateTick =0;
	DescpGo.WaitTimeOutTick = 0;
	DescpGo.ExistPortAskRetryTimes = 0;
	DescpGo.P0State =0;
	DescpGo.P1State =0;
	DescpGo.P0 = 1;
	DescpGo.P1 = 1;
	DescpGo.IsAll =0;
	DescpGo.Tick =0;
	DescpGo.Err =0;
	DescpGo.IsSaving =0;
	DescpGo.IsSaveOk =0;
	DescpGo.Completed =0;
	DescpGo.AvaEntry = 0;
	for(x=0; x<42; x++)DescpGo.P0ExistTable[x] = 0;
	for(x=0; x<255; x++)DescpGo.P1GetTable[x] = 0;	
	ClearDescpContent();
}


void TfNetworkReg::DescpGoAll()
{
	int i, P1LostCount, aLt;
	unsigned int aAddr;
	PreventPermissionDown();
	PreventEventShow();
	if(DescpGo.pNumInx < ProtoC2C.SlaveCount){
		if(DescpGo.P0State == 0){
			//Send Ask is This Port Exist
			DescpGo.pNum = ProtoC2C.SlaveList[DescpGo.pNumInx];
			ProtoC2C.PortExistAsk(DescpGo.pNum, DescpGo.P0);
			DescpGo.WaitTimeOutTick = 0;
			DescpGo.ExistPortAskRetryTimes = 0;
			DescpGo.P0State = 1;
			if(DescpGo.P0 <= dPORT_MAX_COUNT)DescpGo.RemainCount = 250;
			else DescpGo.RemainCount = 160;
			ShowDescpState();
		}else
		if(DescpGo.P0State == 1){
			//Try Get Is Exist
			if(DescpGo.P0ExistTable[DescpGo.P0] == 0xFF){
				for(i=0; i<255; i++)DescpGo.P1GetTable[i] = 0;
				DescpGo.P0State = 2;
				ProtoC2C.DescpOnePortAsk(DescpGo.pNum, DescpGo.P0);
				if(DescpGo.P0 <= dPORT_MAX_COUNT)DescpGo.RemainCount = 250;
				else DescpGo.RemainCount = 160;
				DescpGo.Tick = 0;
			}else
			if(DescpGo.P0ExistTable[DescpGo.P0] == 0x10){
				if(DescpGo.P0 < (dPORT_MAX_COUNT +1)){
					DescpGo.P0++;
					DescpGo.P0State = 0;
				}else{
					DescpGo.pNumInx++;
					DescpGo.P0 =1;
					DescpGo.P0State = 0;
				}
			}else{
				DescpGo.WaitTimeOutTick++;
				if(DescpGo.WaitTimeOutTick > 200){
					DescpGo.WaitTimeOutTick=0;
					DescpGo.ExistPortAskRetryTimes++;
					if(DescpGo.ExistPortAskRetryTimes >3){
						if(DescpGo.P0 < (dPORT_MAX_COUNT+1)){
							DescpGo.P0++;
							__nop();
							DescpGo.P0State = 0;
						}else{
							DescpGo.pNumInx++;
							DescpGo.P0 =1;
							DescpGo.P0State = 0;
						}
					}else{
						ProtoC2C.PortExistAsk(DescpGo.pNum, DescpGo.P0);
						DescpGo.WaitTimeOutTick = 0;
					}
				}
			}
		}else
		if(DescpGo.P0State == 2){
			DescpGo.Tick++;
			DescpGo.NoUpdateTick++;
			if(DescpGo.Tick < 5000){
				if( (DescpGo.Tick % 100) == 99){
					//check Ok
					DescpGo.IsSaveOk = 0;
					DescpGo.IsSaving = 0;
					P1LostCount = 0;
					DescpGo.EmptyCount = 0;
					if(DescpGo.P0 <= dPORT_MAX_COUNT){
						for(i=1; i<251; i++){
							if(DescpGo.P1GetTable[i] == 0){
								P1LostCount++;
							}
							if(DescpGo.P1GetTable[i] == 0x80){
								DescpGo.EmptyCount++;
							}
						}
					}else{
						for(i=1; i<161; i++){
							if(DescpGo.P1GetTable[i] == 0){
								P1LostCount++;
							}
							if(DescpGo.P1GetTable[i] == 0x80){
								DescpGo.EmptyCount++;
							}
						}
					}
					
					if(P1LostCount == 0){
						if(DescpGo.P0 <= dPORT_MAX_COUNT){
							SavePortDescp(DescpGo.pNum, DescpGo.P0);
							ReadPortDescp(DescpGo.pNum, DescpGo.P0);
							DescpGo.AvaEntry += (250 - DescpGo.EmptyCount);
							DescpGo.RemainCount = 0;
						}else{
							SaveDirectPadDescp(DescpGo.pNum);
							ReadDirectPadDescp(DescpGo.pNum);
							DescpGo.AvaEntry += (160 - DescpGo.EmptyCount);
							DescpGo.RemainCount = 0;
						}
	
						if(DescpGo.P0 < (dPORT_MAX_COUNT +1) ){
							DescpGo.P0++;
							DescpGo.P0State = 0;
							ClearDescpContent();
						}else{
							DescpGo.pNumInx++;
							DescpGo.P0 = 1;
							DescpGo.P0State = 0;
							ClearDescpContent();
						}
						DescpGo.IsSaveOk = 1;
						ShowDescpState();
						return;
					}
					if(DescpGo.RemainCount != P1LostCount){
						DescpGo.NoUpdateTick = 0;
						DescpGo.RemainCount = P1LostCount;
					}
					if(DescpGo.NoUpdateTick >200){
						DescpGo.NoUpdateTick = 0;
						if(P1LostCount < 13){
							//Retry Lost P1
							aLt = 0;
							for(i=0; i<12; i++)DescpGo.RetryP1[i] =0;
							if(DescpGo.P0 <= dPORT_MAX_COUNT){
								for(i=1; i<251; i++){
									if( (DescpGo.P1GetTable[i] != 0xFF) && (DescpGo.P1GetTable[i] != 0x80) ){
										DescpGo.RetryP1[aLt] = i;
										aLt++;
										if(aLt >= 12)break;
									}
								}
							}else{
								for(i=1; i<161; i++){
									if( (DescpGo.P1GetTable[i] != 0xFF) && (DescpGo.P1GetTable[i] != 0x80) ){
										DescpGo.RetryP1[aLt] = i;
										aLt++;
										if(aLt >= 12)break;
									}
								}
							}
							if(DescpGo.RetryP1[0])
								ProtoC2C.DescpListAsk(DescpGo.pNum, DescpGo.P0, 1, &DescpGo.RetryP1[0]);
							if(DescpGo.RetryP1[4])
								ProtoC2C.DescpListAsk(DescpGo.pNum, DescpGo.P0, 2, &DescpGo.RetryP1[4]);
							if(DescpGo.RetryP1[8])
								ProtoC2C.DescpListAsk(DescpGo.pNum, DescpGo.P0, 3, &DescpGo.RetryP1[8]);
						}else{
							//It's Too Many Lost , Show Err, exit
							DescpGo.Err =1;
							DescpGo.IsSaveOk = 0;
							DescpGo.IsSaving = 0;
							DescpGo.IsStart = 0;
						}
					}
					ShowDescpState();
				}
			}else{
				//Show Err, exit
				DescpGo.Err =1;
				DescpGo.IsStart = 0;
				DescpGo.IsSaveOk = 0;
				DescpGo.IsSaving = 0;
				ShowDescpState();
			}
		}
	}else{
		//Show Completed
		DescpGo.IsStart = 0;
		DescpGo.IsSaveOk = 0;
		DescpGo.IsSaving = 0;
		DescpGo.Completed = 1;
		ShowDescpState();
	}
}


void TfNetworkReg::DescpGoOneCtl()
{
	int i, P1LostCount, aLt;
	unsigned int aAddr;
	int Cmpl;
	
	PreventPermissionDown();
	PreventEventShow();
	Cmpl = 0;

	if(DescpGo.P0State == 0){
		//Send Ask is This Port Exist
		ProtoC2C.PortExistAsk(DescpGo.pNum, DescpGo.P0);
		DescpGo.WaitTimeOutTick = 0;
		DescpGo.ExistPortAskRetryTimes = 0;
		DescpGo.P0State = 1;
		ShowDescpState();
	}else
	if(DescpGo.P0State == 1){
		//Try Get Is Exist
		if(DescpGo.P0ExistTable[DescpGo.P0] == 0xFF){
			for(i=0; i<255; i++)DescpGo.P1GetTable[i] = 0;
			DescpGo.P0State = 2;
			ProtoC2C.DescpOnePortAsk(DescpGo.pNum, DescpGo.P0);
			if(DescpGo.P0 <= dPORT_MAX_COUNT)DescpGo.RemainCount = 250;
			else DescpGo.RemainCount = 160;
			DescpGo.Tick = 0;
		}else
		if(DescpGo.P0ExistTable[DescpGo.P0] == 0x10){
			if(DescpGo.P0 < (dPORT_MAX_COUNT +1)){
				DescpGo.P0++;
				DescpGo.P0State = 0;
			}else{
				DescpGo.pNumInx++;
				DescpGo.P0 =1;
				DescpGo.P0State = 0;
			}
		}else{
			DescpGo.WaitTimeOutTick++;
			if(DescpGo.WaitTimeOutTick > 200){
				DescpGo.WaitTimeOutTick=0;
				DescpGo.ExistPortAskRetryTimes++;
				if(DescpGo.ExistPortAskRetryTimes >3){
					if(DescpGo.P0 < (dPORT_MAX_COUNT+1)){
						DescpGo.P0++;
						__nop();
						DescpGo.P0State = 0;
					}else{
						DescpGo.pNumInx++;
						DescpGo.P0 =1;
						DescpGo.P0State = 0;
					}
				}else{
					ProtoC2C.PortExistAsk(DescpGo.pNum, DescpGo.P0);
					DescpGo.WaitTimeOutTick = 0;
				}
			}
		}
	}else
	if(DescpGo.P0State == 2){
		DescpGo.Tick++;
		DescpGo.NoUpdateTick++;
		if(DescpGo.Tick < 5000){
			if( (DescpGo.Tick % 100) == 99){
				//check Ok
				DescpGo.IsSaveOk = 0;
				DescpGo.IsSaving = 0;
				P1LostCount = 0;
				DescpGo.EmptyCount = 0;
				if(DescpGo.P0 <= dPORT_MAX_COUNT){
					for(i=1; i<251; i++){
						if(DescpGo.P1GetTable[i] == 0){
							P1LostCount++;
						}
						if(DescpGo.P1GetTable[i] == 0x80){
							DescpGo.EmptyCount++;
						}
					}
				}else{
					for(i=1; i<161; i++){
						if(DescpGo.P1GetTable[i] == 0){
							P1LostCount++;
						}
						if(DescpGo.P1GetTable[i] == 0x80){
							DescpGo.EmptyCount++;
						}
					}
				}
				
				if(P1LostCount == 0){
					if(DescpGo.P0 <= dPORT_MAX_COUNT){
						SavePortDescp(DescpGo.pNum, DescpGo.P0);
						ReadPortDescp(DescpGo.pNum, DescpGo.P0);
						DescpGo.AvaEntry += (250 - DescpGo.EmptyCount);
						DescpGo.RemainCount = 0;
					}else{
						SaveDirectPadDescp(DescpGo.pNum);
						ReadDirectPadDescp(DescpGo.pNum);
						DescpGo.AvaEntry += (160 - DescpGo.EmptyCount);
						DescpGo.RemainCount = 0;
					}

					if(DescpGo.P0 < (dPORT_MAX_COUNT +1) ){
						DescpGo.P0++;
						DescpGo.P0State = 0;
						ClearDescpContent();
					}else{
						Cmpl = 1;
					}
					DescpGo.IsSaveOk = 1;
					ShowDescpState();
					if(Cmpl){
						DescpGo.IsStart = 0;
						DescpGo.IsSaveOk = 0;
						DescpGo.IsSaving = 0;
						DescpGo.Completed = 1;
						ShowDescpState();
					}
					return;
				}
				if(DescpGo.RemainCount != P1LostCount){
					DescpGo.NoUpdateTick = 0;
					DescpGo.RemainCount = P1LostCount;
				}
				if(DescpGo.NoUpdateTick >200){
					DescpGo.NoUpdateTick = 0;
					if(P1LostCount < 13){
						//Retry Lost P1
						aLt = 0;
						for(i=0; i<12; i++)DescpGo.RetryP1[i] =0;
						if(DescpGo.P0 <= dPORT_MAX_COUNT){
							for(i=1; i<251; i++){
								if( (DescpGo.P1GetTable[i] != 0xFF) && (DescpGo.P1GetTable[i] != 0x80) ){
									DescpGo.RetryP1[aLt] = i;
									aLt++;
									if(aLt >= 12)break;
								}
							}
						}else{
							for(i=1; i<161; i++){
								if( (DescpGo.P1GetTable[i] != 0xFF) && (DescpGo.P1GetTable[i] != 0x80) ){
									DescpGo.RetryP1[aLt] = i;
									aLt++;
									if(aLt >= 12)break;
								}
							}
						}
						if(DescpGo.RetryP1[0])
							ProtoC2C.DescpListAsk(DescpGo.pNum, DescpGo.P0, 1, &DescpGo.RetryP1[0]);
						if(DescpGo.RetryP1[4])
							ProtoC2C.DescpListAsk(DescpGo.pNum, DescpGo.P0, 2, &DescpGo.RetryP1[4]);
						if(DescpGo.RetryP1[8])
							ProtoC2C.DescpListAsk(DescpGo.pNum, DescpGo.P0, 3, &DescpGo.RetryP1[8]);
					}else{
						//It's Too Many Lost , Show Err, exit
						DescpGo.Err =1;
						DescpGo.IsSaveOk = 0;
						DescpGo.IsSaving = 0;
						DescpGo.IsStart = 0;
					}
				}
				ShowDescpState();
			}
		}else{
			//Show Err, exit
			DescpGo.Err =1;
			DescpGo.IsStart = 0;
			DescpGo.IsSaveOk = 0;
			DescpGo.IsSaving = 0;
			ShowDescpState();
		}
	}
}


void TfNetworkReg::Task10Ms()
{
	if(IsShowing){
		if(DescpGo.IsStart){
			if(DescpGo.IsAll){
				if(DescpGo.IsBusy){
					DescpGo.BusyTick++;
					if(DescpGo.BusyTick > 500){
						DescpGo.IsBusy = 0;
					}
				}else{
					DescpGoAll();
				}
			}else{
				if(DescpGo.IsBusy){
					DescpGo.BusyTick++;
					if(DescpGo.BusyTick > 500){
						DescpGo.IsBusy = 0;
					}
				}else{
					DescpGoOneCtl();
				}
			}
		}
	}
}

void TfNetworkReg::sRecoverSetDefaultNetworkDescp()
{
	unsigned int fAddr;
	unsigned int i,m;
	volatile unsigned int x, Sta;
	
	unsigned char tPNum, tPn; 
	
	//Erase 2 * 4K Blocks
	for(tPNum =1; tPNum<dNETWORK_MACHINE_COUNT+1; tPNum++){
		for(tPn=0; tPn<dPORT_MAX_COUNT; tPn++){
			SysLoopTick = 0;
			do{
				sDelayMs(5);
				Sta = nor_get_busy_state(NwDescpUsingChip[tPNum]);
			}while(Sta);
			for(x=0; x<2; x++){
				do{
					sDelayMs(20);
					Sta = nor_get_busy_state(NwDescpUsingChip[tPNum]);
				}while(Sta);
				fAddr = NwDescpUsingFlashAddr[tPNum] + (SizeOf4K * (tPn * 2 + x) );
				nor_erase_sector_4k(NwDescpUsingChip[tPNum],fAddr);
				do{
					sDelayMs(50);
					Sta = nor_get_busy_state(NwDescpUsingChip[tPNum]);
				}while(Sta);
			}
			LoopCheck1S();			
		}
	}

	for(tPNum =1; tPNum<dNETWORK_MACHINE_COUNT+1; tPNum++){
		SysLoopTick = 0;
		do{
			sDelayMs(5);
			Sta = nor_get_busy_state(NwDescpUsingChip[tPNum]);
		}while(Sta);
		//Erase 2 * 4K Blocks
		for(x=0; x<2; x++){
			SysLoopTick = 0;
			do{
				sDelayMs(20);
				Sta = nor_get_busy_state(NwDescpUsingChip[tPNum]);
			}while(Sta);
			fAddr = NwDescpUsingFlashAddr[tPNum] + 327680 + (x * 4096);
			nor_erase_sector_4k(NwDescpUsingChip[tPNum],fAddr);
			do{
				sDelayMs(50);
				Sta = nor_get_busy_state(NwDescpUsingChip[tPNum]);
			}while(Sta);
			LoopCheck1S();
		}	
	}
}

void TfNetworkReg::ReadNwDevDescp()
{
	unsigned char aCtl, aInx;
	for(aCtl=0; aCtl<dNETWORK_MACHINE_COUNT; aCtl++){
		for(aInx=0; aInx<dPORT_MAX_COUNT; aInx++){
			ReadPortDescp(aCtl+1, aInx+1);
		}
		ReadDirectPadDescp(aCtl+1);
	}
}

int TfNetworkReg::ExtRequst(unsigned char Prm)
{
	DescpGo.IsStart = 0;
	IsShowing =0;
	IsOnGoing =0;
	return 0;
}

void TfNetworkReg::LoadDescpShowCtlNum()
{
	
}

void TfNetworkReg::LoadDescpShowCircuit()
{
	
}

