#include "RuntimeData.h"
#include "RegisterManual.h"

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

#define dWWidth	24
#define dWHeigh	24
#define dRoundR	7

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

void TRegisterManual::DrawFixText()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "回路->手动注册");	
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "回路号");
		TextRender_string24(Lv.Grid.Left[0] + 160, Lv.Grid.Top[0] +6, clNearWhite, "机器号");
		TextRender_string24(Lv.Grid.Left[0] + 260, Lv.Grid.Top[0] +6, clNearWhite, "本机");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] + 40, clNearWhite, "地址号");
		TextRender_string24(Lv.Grid.Left[0] + 260, Lv.Grid.Top[0] +40, clNearWhite, "类型:");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "二次码");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +40, clNearWhite, "分区号");
		TextRender_string24(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 4, clNearWhite, ItemColor, "设备定义");
		TextRender_string24(Lv.Grid.Left[3] +10, Lv.Grid.Top[3] + 4, clNearWhite, ItemColor, "输入信号");
		TextRender_string24(Lv.Grid.Left[3] +42, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "反馈");
		TextRender_string24(Lv.Grid.Left[3] +140, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "报警");
		TextRender_string24(Lv.Grid.Left[3] +236, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "监管");
		TextRender_string24(Lv.Grid.Left[4] +10, Lv.Grid.Top[4] + 4, clNearWhite, ItemColor, "操作");
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +60, Lv.Grid.Top[4] + 36, 76, 30, 7, clNearWhite, "注册", 14, clNearBlack);
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +150, Lv.Grid.Top[4] + 36, 76, 30, 7, clNearWhite, "清除", 14, clNearBlack);
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Loop->Register By Manual");	
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "LoopID");
		TextRender_string24(Lv.Grid.Left[0] + 160, Lv.Grid.Top[0] +6, clNearWhite, "CtlID");
		TextRender_string24(Lv.Grid.Left[0] + 260, Lv.Grid.Top[0] +6, clNearWhite, "Local");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] + 40, clNearWhite, "Addr");
		TextRender_string24(Lv.Grid.Left[0] + 260, Lv.Grid.Top[0] +40, clNearWhite, "Type:");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "AuxID");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +40, clNearWhite, "Zone");
		TextRender_string24(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 4, clNearWhite, ItemColor, "Assign");
		TextRender_string24(Lv.Grid.Left[3] +10, Lv.Grid.Top[3] + 4, clNearWhite, ItemColor, "Signal");
		TextRender_string24(Lv.Grid.Left[3] +42, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "FBCK");
		TextRender_string24(Lv.Grid.Left[3] +140, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "Fire");
		TextRender_string24(Lv.Grid.Left[3] +236, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "SPER");
		TextRender_string24(Lv.Grid.Left[4] +10, Lv.Grid.Top[4] + 4, clNearWhite, ItemColor, "OPR");
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +60, Lv.Grid.Top[4] + 36, 76, 30, 7, clNearWhite, "Reg", 14, clNearBlack);
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +150, Lv.Grid.Top[4] + 36, 76, 30, 7, clNearWhite, "Del", 14, clNearBlack);
	}
}

void TRegisterManual::Init()
{
	IsViewList = 0;
	IsPermission = 0;
	ShowingResultOdd	 =0;
	
	IsOnGo =0;
	IsOnShow =0;
	IsWait4Rpy = 0;
	IsRecData = 0;
	
	Tag.Reset();
	LastSta = 0;
	List4What = 0;
	Pitch = 1;
	
	
	SignalInputType =0;
	Cover.Reset();
}

void TRegisterManual::DelayMs(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 TRegisterManual::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

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

void TRegisterManual::DrawSelf(void)
{
	RenderBackGround();
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);


	
	for(int i=0; i<2; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	

	IList->EditPortNum.Init(Lv.Grid.Left[0] + 86, Lv.Grid.Top[0] + 2, 50, 30, 0, 0, 1, clNearWhite);
	

	TextDigitRender2Right24(Lv.Grid.Left[0] + 232, Lv.Grid.Top[0] +6, clNearWhite, MainCtl.fData.Split.MyNum);

	

	//Addr Start Editor
	IList->EditAddrNum.Init(Lv.Grid.Left[0] + 86, Lv.Grid.Top[0] + 36, 50, 30, 0, 0, 1, clNearWhite);
	TextRender_string24(Lv.Grid.Left[0] + 140, Lv.Grid.Top[0] + 40, clNearWhite, "-");
	//Addr End Editor
	IList->EditDescpShow.Init(Lv.Grid.Left[0] + 156, Lv.Grid.Top[0] + 36, 50, 30, 0, 0, 1, clNearWhite);
	

	//dType Editor
	IList->Edit[0].Init(Lv.Grid.Left[0] + 326, Lv.Grid.Top[0] + 36, 50, 30, 0, 0, 1, clNearWhite);
	IList->STextDType.Init(Lv.Grid.Left[0] + 380, Lv.Grid.Top[0] + 40, 240, 30, 0, 0, clNearWhite, ItemColor);
	if(LanguageEnCn==0){
		IList->STextDType.SetText("未定义", 24);
	}else{
		IList->STextDType.SetText("undefine", 24);
	}
	
	IList->STextDType.Enable = 1;
	IList->STextDType.Visible =1;
	IList->STextCtlNum.Enable = 1;
	IList->STextCtlNum.Visible =1;
	
	IList->EditPortNum.SetMaxLen(2);
	IList->EditAddrNum.SetMaxLen(3);
	IList->EditDescpShow.SetMaxLen(3);
	IList->Edit[0].SetMaxLen(3);
	IList->EditPortNum.Enable = 1;
	IList->EditPortNum.Visible = 1;
	IList->EditAddrNum.Enable = 1;
	IList->EditAddrNum.Visible = 1;
	IList->EditDescpShow.Enable = 1;
	IList->EditDescpShow.Visible = 1;
	IList->Edit[0].Enable = 1;
	IList->Edit[0].Visible = 1;
	
	//User Code & Area Assign

	IList->EditUserCode.Init(Lv.Grid.Left[1] + 84, Lv.Grid.Top[1] + 6, 108, 30, 0, 0, 1, clNearWhite);
	IList->EditAreaAssign.Init(Lv.Grid.Left[1] + 84, Lv.Grid.Top[1] + 40, 108, 30, 0, 0, 1, clNearWhite);
	
	IList->EditUserCode.Enable = 1;
	IList->EditUserCode.Visible =1;
	IList->EditUserCode.SetMaxLen(8);
	IList->EditAreaAssign.Enable = 1;
	IList->EditAreaAssign.Visible =1;
	IList->EditAreaAssign.SetMaxLen(5);
	
	for(int i=2; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	

	
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	
	DrawListDetail();
	
	IList->Edit[1].Init(Lv.Grid.Left[2] +110, Lv.Grid.Top[2] + 4, 100,30,0,0,1,clNearBlack);
	IList->Edit[1].SetMaxLen(3);
	IList->Edit[1].Str.Clear();
	IList->STextModuleInDefineType.Init(Lv.Grid.Left[2] +6, Lv.Grid.Top[2] + 38, 240,30,0,0,clNearWhite, ItemColor);
	IList->STextModuleInDefineType.Enable = 1;
	IList->STextModuleInDefineType.Visible = 1;
	if(LanguageEnCn==0)IList->STextModuleInDefineType.SetText("未定义",24);
	else IList->STextModuleInDefineType.SetText("undefine",24);

	IList->STextLastState.Init(Lv.Grid.Left[4] +120, Lv.Grid.Top[4] + 4, 156, 30, 0, 0, clNearWhite, ItemColor);
	IList->STextLastState.TextClear();
	
	IList->EditPortNum.SetText("01",24);
	IList->EditAddrNum.SetText("001",24);
	IList->EditDescpShow.SetText("002",24);
	IList->EditUserCode.SetText("00000000",24);
	IList->EditAreaAssign.SetText("0000",24);
	IList->Edit[0].SetText("000",24);
	IList->Edit[1].SetText("000",24);
	
	IList->EditPortNum.Show();
	IList->EditAddrNum.Show();
	IList->EditDescpShow.Show();
	IList->EditUserCode.Show();
	IList->EditAreaAssign.Show();
	IList->Edit[0].Show();
	IList->STextDType.Show();
	IList->Edit[1].Show();
	IList->STextModuleInDefineType.Show();
	
	DrawFixText();
	
	RedrawModuleInSignalInType();
	
	Check4Selected();
	
	ShowCover();
}

void TRegisterManual::ShowCoverAuxId()	//1 Cover ,0 ignore
{
	if(LanguageEnCn==0){
		if(Cover.AuxId){
			TextRender_string24(Lv.Grid.Left[1] + 200, Lv.Grid.Top[1] + 6, clNearWhite, ItemColor, 	"覆盖  ");
		}else{
			TextRender_string24(Lv.Grid.Left[1] + 200, Lv.Grid.Top[1] + 6, clNearWhite, ItemColor, 	"不覆盖");
		}
	}else{
		if(Cover.AuxId){
			TextRender_string24(Lv.Grid.Left[1] + 200, Lv.Grid.Top[1] + 6, clNearWhite, ItemColor, 	"Cover ");
		}else{
			TextRender_string24(Lv.Grid.Left[1] + 200, Lv.Grid.Top[1] + 6, clNearWhite, ItemColor, 	"Ignore");
		}
	}
}

void TRegisterManual::ShowCoverZone()
{
	if(LanguageEnCn==0){
		if(Cover.Zone){
			TextRender_string24(Lv.Grid.Left[1] + 200, Lv.Grid.Top[1] + 36, clNearWhite, ItemColor, 	"覆盖  ");
		}else{
			TextRender_string24(Lv.Grid.Left[1] + 200, Lv.Grid.Top[1] + 36, clNearWhite, ItemColor, 	"不覆盖");
		}
	}else{
		if(Cover.Zone){
			TextRender_string24(Lv.Grid.Left[1] + 200, Lv.Grid.Top[1] + 36, clNearWhite, ItemColor, 	"Cover ");
		}else{
			TextRender_string24(Lv.Grid.Left[1] + 200, Lv.Grid.Top[1] + 36, clNearWhite, ItemColor, 	"Ignore");
		}
	}	
}

void TRegisterManual::ShowCoverAssignType()
{
	if(LanguageEnCn==0){
		if(Cover.AssignType){
			TextRender_string24(Lv.Grid.Left[1] + 216, Lv.Grid.Top[2] + 6, clNearWhite, ItemColor, 	"覆盖  ");
		}else{
			TextRender_string24(Lv.Grid.Left[1] + 216, Lv.Grid.Top[2] + 6, clNearWhite, ItemColor, 	"不覆盖");
		}
	}else{
		if(Cover.AssignType){
			TextRender_string24(Lv.Grid.Left[1] + 216, Lv.Grid.Top[2] + 6, clNearWhite, ItemColor, 	"Cover ");
		}else{
			TextRender_string24(Lv.Grid.Left[1] + 216, Lv.Grid.Top[2] + 6, clNearWhite, ItemColor, 	"Ignore");
		}
	}	
}

void TRegisterManual::ShowCoverSignal()
{
	if(LanguageEnCn==0){
		if(Cover.Signal){
			TextRender_string24(Lv.Grid.Left[1] + 216, Lv.Grid.Top[3] + 6, clNearWhite, ItemColor, 	"覆盖  ");
		}else{
			TextRender_string24(Lv.Grid.Left[1] + 216, Lv.Grid.Top[3] + 6, clNearWhite, ItemColor, 	"不覆盖");
		}
	}else{
		if(Cover.Signal){
			TextRender_string24(Lv.Grid.Left[1] + 216, Lv.Grid.Top[3] + 6, clNearWhite, ItemColor, 	"Cover ");
		}else{
			TextRender_string24(Lv.Grid.Left[1] + 216, Lv.Grid.Top[3] + 6, clNearWhite, ItemColor, 	"Ignore");
		}
	}	
}

void TRegisterManual::ShowCover()
{
	ShowCoverAuxId();
	ShowCoverZone();
	ShowCoverAssignType();
	ShowCoverSignal();
}

void TRegisterManual::DrawCommonByExReturn()
{
	for(int i=0; i<2; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	
	TextDigitRender2Right24(Lv.Grid.Left[0] + 232, Lv.Grid.Top[0] +6, clNearWhite, MainCtl.fData.Split.MyNum);
	
	for(int i=2; i<3; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);

	
	if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	DrawListDetail();
	
	DrawFixText();
	
	IList->EditPortNum.Show();
	IList->EditAddrNum.Show();
	IList->EditDescpShow.Show();
	IList->EditUserCode.Show();
	IList->EditAreaAssign.Show();
	IList->Edit[0].Show();
	IList->STextDType.Show();
	IList->Edit[1].Show();
	IList->STextModuleInDefineType.Show();
	
	Check4Selected();	
}

void TRegisterManual::DrawListDetail()
{
	int i;
	if(List4What == 0){
		if(LanguageEnCn==0)TextRender_string24(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top + 6, clNearWhite, SmallPanelColor, 	"注册类型列表 数量:");
		else TextRender_string24(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top + 6, clNearWhite, SmallPanelColor, 			"Hardware Dev Type:");
		for(i=0;i<8;i++)TextDrawCount[i]=0;
		TMyString::sFrom3Dg(HtEntryCount,TextDrawCount);
		TMyString::sFillFixLen(TextDrawCount, 4);
		TextRender_string24(Lv.PanelSmall.Left + 230, Lv.PanelSmall.Top + 6, clNearWhite, SmallPanelColor, TextDrawCount);
	}else
	if(List4What == 1){
		if(LanguageEnCn==0)TextRender_string24(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top + 6, clNearWhite, SmallPanelColor, 	"指派类型列表 数量:");
		else TextRender_string24(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top + 6, clNearWhite, SmallPanelColor, 			"Assign Type: ");
		for(i=0;i<8;i++)TextDrawCount[i]=0;
		TMyString::sFrom3Dg(AtEntryCount,TextDrawCount);
		TMyString::sFillFixLen(TextDrawCount, 4);
		TextRender_string24(Lv.PanelSmall.Left + 230, Lv.PanelSmall.Top + 6, clNearWhite, SmallPanelColor, TextDrawCount);
	}else
	if(List4What == 2){
		if(LanguageEnCn==0)TextRender_string24(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top + 6, clNearWhite, SmallPanelColor, 	"有效区域列表 数量:");
		else TextRender_string24(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top + 6, clNearWhite, SmallPanelColor, 			"List Of Zone:");
		for(i=0;i<8;i++)TextDrawCount[i]=0;
		TMyString::sFrom4Dg(AreaEntryCount,TextDrawCount);
		TMyString::sFillFixLen(TextDrawCount, 4);
		TextRender_string24(Lv.PanelSmall.Left + 230, Lv.PanelSmall.Top + 6, clNearWhite, SmallPanelColor, TextDrawCount);
	}
}


void TRegisterManual::Show(void)
{	
	LoadHwTypeListStr();
	LoadAssignTypeListStr();	
	LoadAreaInf();
	
	RenderBackGround();
	DrawSelf();
	IsOnGo =1;
	IsOnShow =1;
	
	if(List4What == 0)ShowHardwareTypeList();
	else if(List4What == 1)ShowAssignTypeList();
	else	if(List4What == 2)ShowAreaList();
}

void TRegisterManual::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
		if(List4What == 0)ShowHardwareTypeList();
		else if(List4What == 1)ShowAssignTypeList();
		else	if(List4What == 2)ShowAreaList();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], ContentColor);
		DrawCommonByExReturn();
		if(List4What == 0)ShowHardwareTypeList();
		else if(List4What == 1)ShowAssignTypeList();
		else	if(List4What == 2)ShowAreaList();
		
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}

void TRegisterManual::RedrawModuleInSignalInType()
{
	if(SignalInputType == 1){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 12, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 110, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 206, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}else
	if(SignalInputType == 2){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 12, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 110, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 206, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);		
	}else
	if(SignalInputType == 3){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 12, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 110, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 206, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 12, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 110, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 206, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
}

void TRegisterManual::ClearSelected()
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			IList->EditPortNum.Selected = 0;
			IList->EditPortNum.Show();
			IList->EditPortNum.ShowCursor();
			break;
		case 1:
			IList->EditAddrNum.Selected = 0;
			IList->EditAddrNum.Show();
			IList->EditAddrNum.ShowCursor();
			break;
		case 2:
			IList->EditDescpShow.Selected = 0;
			IList->EditDescpShow.Show();
			IList->EditDescpShow.ShowCursor();
			break;
		case 3:
			IList->Edit[0].Selected = 0;
			IList->Edit[0].Show();
			IList->Edit[0].ShowCursor();
			break;
		case 4:
			IList->EditUserCode.Selected = 0;
			IList->EditUserCode.Show();
			IList->EditUserCode.ShowCursor();
			break;
		case 5:
			IList->EditAreaAssign.Selected = 0;
			IList->EditAreaAssign.Show();
			IList->EditAreaAssign.ShowCursor();
			break;
		case 6:
			IList->Edit[1].Selected = 0;
			IList->Edit[1].Show();
			IList->Edit[1].ShowCursor();
			break;
		case 7:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +12, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 8:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +110, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 9:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +206, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 10:
			if(LanguageEnCn==0)tText="注册"; else tText="Reg ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +60, Lv.Grid.Top[4] + 36, 76, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 11:
			if(LanguageEnCn==0)tText="清除"; else tText="Del ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +150, Lv.Grid.Top[4] + 36, 76, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
	}
}

//______________________________________________________________________________________________________________________________
void TRegisterManual::Check4Selected(void)
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			IList->EditPortNum.Selected = 1;
			IList->EditPortNum.SetPstTail();
			IList->EditPortNum.Show();
			IList->EditPortNum.ShowCursor();
			break;
		case 1:
			IList->EditAddrNum.Selected = 1;
			IList->EditAddrNum.SetPstTail();
			IList->EditAddrNum.Show();
			IList->EditAddrNum.ShowCursor();
			break;
		case 2:
			IList->EditDescpShow.Selected = 1;
			IList->EditDescpShow.SetPstTail();
			IList->EditDescpShow.Show();
			IList->EditDescpShow.ShowCursor();
			break;
		case 3:
			IList->Edit[0].Selected = 1;
			IList->Edit[0].SetPstTail();
			IList->Edit[0].Show();
			IList->Edit[0].ShowCursor();
			break;
		case 4:
			IList->EditUserCode.Selected = 1;
			IList->EditUserCode.SetPstTail();
			IList->EditUserCode.Show();
			IList->EditUserCode.ShowCursor();
			break;
		case 5:
			IList->EditAreaAssign.Selected = 1;
			IList->EditAreaAssign.SetPstTail();
			IList->EditAreaAssign.Show();
			IList->EditAreaAssign.ShowCursor();
			break;
		case 6:
			IList->Edit[1].Selected = 1;
			IList->Edit[1].SetPstTail();
			IList->Edit[1].Show();
			IList->Edit[1].ShowCursor();
			break;
		case 7:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +12, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 8:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +110, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 9:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +206, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 10:
			if(LanguageEnCn==0)tText="注册"; else tText="Reg ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +60, Lv.Grid.Top[4] + 36, 76, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
		case 11:
			if(LanguageEnCn==0)tText="清除"; else tText="Del ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +150, Lv.Grid.Top[4] + 36, 76, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
	}
}

//_____________________________________________________________________________________________________________________________

void TRegisterManual::ShowHardwareTypeList()
{
	int i, x, y;
	unsigned int aClr, aAddr, Index, wc;
	if(IsViewList){
		aClr = clBlue;
	}else{
		aClr = clMedGray;
	}
	
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Bottom,PanelColor);
	
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Left +4,Lv.Panel.Bottom,aClr);
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Top +4,aClr);
	RectFillRender(Lv.Panel.Right-4,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Bottom,aClr);
	RectFillRender(Lv.Panel.Left,Lv.Panel.Bottom -4,Lv.Panel.Right,Lv.Panel.Bottom,aClr);
	
	i = HtListIndex * 16;
	wc =0;
	for(; i<HtEntryCount; i++){
		x = (wc % 2) * 240 + Lv.Panel.Left +6;
		y = wc / 2 * 30 + Lv.Panel.Top +8;
		aAddr = dAddrSdRam_Register + (i * 4);
		Index = *(volatile unsigned int *)(aAddr);
		if(Index < 256){
			TextDigitRender3Right24(x,y,clBlack, Index);
			TextRender_string24(x+36,y,clBlack,".");
			if(LanguageEnCn==0){
				TextRender_string24(x+48,y,clBlack,StrHwType[Index]);
			}else{
				TextRender_string24(x+48,y,clBlack,StrHwTypeEn[Index]);
			}
		}
		wc++;
		if(wc >= 16)break;
	}
	i = HtListIndex * 16;
	TVScrollBar::sDrawByPoint(Lv.Panel.Right-24, Lv.Panel.Top +4, Lv.Panel.Right -4, Lv.Panel.Bottom -4, clBlue, clGray, HtEntryCount, 16, i);
}

void TRegisterManual::ShowAssignTypeList()
{
	int i, x, y;
	unsigned int aClr, aAddr, Index, wc;
	if(IsViewList){
		aClr = clBlue;
	}else{
		aClr = clMedGray;
	}
	
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Bottom,PanelColor);
	
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Left +4,Lv.Panel.Bottom,aClr);
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Top +4,aClr);
	RectFillRender(Lv.Panel.Right-4,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Bottom,aClr);
	RectFillRender(Lv.Panel.Left,Lv.Panel.Bottom -4,Lv.Panel.Right,Lv.Panel.Bottom,aClr);
	
	i = AtListIndex * 8;
	wc =0;
	for(; i<AtEntryCount; i++){
		x = 0 + Lv.Panel.Left +6;
		y = wc * 30 + Lv.Panel.Top +8;
		aAddr = dAddrSdRam_Register + 1024 + (i * 4);
		Index = *(volatile unsigned int *)(aAddr);
		if(Index < 256){
			TextDigitRender3Right24(x,y,clBlack, Index);
			TextRender_string24(x+36,y,clBlack,".");
			if(LanguageEnCn==0){
				TextRender_string24(x+48,y,clBlack,StrTypeShortName[Index]);
			}else{
				TextRender_string24(x+48,y,clBlack,StrTypeShortNameEn[Index]);
			}
		}
		wc++;
		if(wc >= 8)break;
	}
	i = AtListIndex * 8;
	TVScrollBar::sDrawByPoint(Lv.Panel.Right-24, Lv.Panel.Top +4, Lv.Panel.Right -4, Lv.Panel.Bottom -4, clBlue, clGray, AtEntryCount, 8, i);
}
		
void TRegisterManual::ShowAreaList()
{
	//__________________________________________
	int i, x, y;
	unsigned int aClr, aAddr, Index, wc;
	if(IsViewList){
		aClr = clBlue;
	}else{
		aClr = clMedGray;
	}
	
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Bottom,PanelColor);
	
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Left +4,Lv.Panel.Bottom,aClr);
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Top +4,aClr);
	RectFillRender(Lv.Panel.Right-4,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Bottom,aClr);
	RectFillRender(Lv.Panel.Left,Lv.Panel.Bottom -4,Lv.Panel.Right,Lv.Panel.Bottom,aClr);
}

void TRegisterManual::RedrawSta()
{
	IList->STextLastState.Show();
}

void TRegisterManual::LoadHwTypeListStr()
{
	int i;
	unsigned int aAddr;
	char *cText;
	if(LanguageEnCn==0){
		cText = "未";
	}else{
		cText = "un";
	}
	HtEntryCount = 0;
	for(i=0; i<256; i++){
		aAddr = dAddrSdRam_Register + (i * 4);
		*(volatile unsigned int *)(aAddr) = 0;

	}
	if(LanguageEnCn==0){
		for(i=0; i<256; i++){
			if( (StrHwType[i][0] != cText[0]) || (StrHwType[i][1] != cText[1]) ){
				aAddr = dAddrSdRam_Register + (HtEntryCount * 4);
				*(volatile unsigned int *)(aAddr) = i;
				HtEntryCount++;
			}else{
				continue;
			}
		}
	}else{
		for(i=0; i<256; i++){
			if( (StrHwTypeEn[i][0] != cText[0]) || (StrHwTypeEn[i][1] != cText[1]) ){
				aAddr = dAddrSdRam_Register + (HtEntryCount * 4);
				*(volatile unsigned int *)(aAddr) = i;
				HtEntryCount++;
			}else{
				continue;
			}
		}
	}
	HtPageCount = HtEntryCount / 16;
	if(HtEntryCount % 16){
		HtPageCount++;
	}
	
	HtListIndex = 0;
}

void TRegisterManual::LoadAssignTypeListStr()
{
	int i;
	unsigned int aAddr;
	char *cText;
	if(LanguageEnCn==0){
		cText = "未";
	}else{
		cText = "un";
	}
	AtEntryCount = 0;
	for(i=0; i<256; i++){
		aAddr = dAddrSdRam_Register + 1024 + (i * 4);
		*(volatile unsigned int *)(aAddr) = 0;

	}
	if(LanguageEnCn==0){
		for(i=0; i<256; i++){
			if( (StrTypeShortName[i][0] != cText[0]) || (StrTypeShortName[i][1] != cText[1]) ){
				aAddr = dAddrSdRam_Register + 1024 + (AtEntryCount * 4);
				*(volatile unsigned int *)(aAddr) = i;
				AtEntryCount++;
			}else{
				continue;
			}
		}
	}else{
		for(i=0; i<256; i++){
			if( (StrTypeShortNameEn[i][0] != cText[0]) || (StrTypeShortNameEn[i][1] != cText[1]) ){
				aAddr = dAddrSdRam_Register + 1024 + (AtEntryCount * 4);
				*(volatile unsigned int *)(aAddr) = i;
				AtEntryCount++;
			}else{
				continue;
			}
		}
	}
	AtPageCount = AtEntryCount / 8;
	if(AtEntryCount % 8){
		AtPageCount++;
	}
	
	AtListIndex = 0;
}

void TRegisterManual::LoadAreaInf()
{
	AreaEntryCount = 0;
	AreaListIndex = 0;
}

void TRegisterManual::PermissionCmdReturnDraw()
{
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	DrawListDetail();
	if(List4What == 0)ShowHardwareTypeList();
	else if(List4What == 1)ShowAssignTypeList();
	else	if(List4What == 2)ShowAreaList();
}

void TRegisterManual::PermissionCmdGO()
{
	if(PermissionCmd){
		SendCmdOneAddr(1);
		IsWait4Rpy =1;
		IsAdd =1;
		IsRecData =0;
		TimeOut =0;
		IList->STextLastState.TextClear();
		IList->STextLastState.Show();
	}else{
		SendCmdOneAddr(0);
		IsWait4Rpy =1;
		IsAdd =0;
		IsRecData =0;
		TimeOut =0;
		IList->STextLastState.TextClear();
		IList->STextLastState.Show();
	}
}

TGuiMsgReturn TRegisterManual::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg;
	unsigned char aP0,aP1,Err;
	unsigned char x,y,aCh;
	TDMix aFullPath;
	unsigned int IsHasPrc = 0;
	
	if(IsWait4Rpy){
		if(aKey != VK_RETURN)return aMsg;
	}	
	
	if(IsPermission){
		PmsRtnMsg = PmsIntervene4Gui.KeyIn(aKey);
		if(PmsRtnMsg == guiMsgOkAndReturn){
			//Enter And Run Go
			IsPermission = 0;
			PermissionCmdReturnDraw();
			PermissionCmdGO();
		}else
		if(PmsRtnMsg == guiMsgReturn){
			//Not Enter Redraw
			IsPermission = 0;
			PermissionCmdReturnDraw();
		}
	}else
	if(IsViewList == 0){
		switch(aKey){
			case VK_0:
			case VK_1:
			case VK_2:
			case VK_3:
			case VK_4:
			case VK_5:
			case VK_6:
			case VK_7:
			case VK_8:
			case VK_9:
			case VK_DELETE:
			case VK_LEFT:
			case VK_RIGHT:
				if(Tag.Inx == 0){
					IList->EditPortNum.KeyIn(aKey);
					IList->EditPortNum.Show();
				}else
				if(Tag.Inx == 1){
					if(aKey == VK_RIGHT){
						if(IList->EditAddrNum.Pst >= IList->EditAddrNum.Str.GetEndPst()){
							ClearSelected();
							Tag.Inx =2;
							Check4Selected();
							IsHasPrc = 1;
						}
					}
					if(IsHasPrc == 0){
						IList->EditAddrNum.KeyIn(aKey);
						IList->EditAddrNum.Show();
					}
				}else
				if(Tag.Inx == 2){
					if(aKey == VK_RIGHT){
						if(IList->EditDescpShow.Pst >= IList->EditDescpShow.Str.GetEndPst()){
							ClearSelected();
							Tag.Inx =3;
							Check4Selected();
							IsHasPrc = 1;
						}
					}else
					if(aKey == VK_LEFT){
						if(IList->EditDescpShow.Pst < 0){
							ClearSelected();
							Tag.Inx =1;
							Check4Selected();
							IsHasPrc = 1;
						}
					}
					if(IsHasPrc == 0){
						IList->EditDescpShow.KeyIn(aKey);
						IList->EditDescpShow.Show();
					}
				}else
				if(Tag.Inx == 3){
					if(aKey == VK_LEFT){
						if(IList->Edit[0].Pst < 0){
							ClearSelected();
							Tag.Inx =2;
							Check4Selected();
							IsHasPrc = 1;
						}
					}
					if(IsHasPrc == 0){
						IList->Edit[0].KeyIn(aKey);
						int iVal = IList->Edit[0].Str.ToInteger();
						if(iVal <0){
							iVal = 0;
							IList->Edit[0].Str.FromUInt3Dg(iVal);
						}else
						if(iVal >255){
							iVal = 0;
							IList->Edit[0].Str.FromUInt3Dg(iVal);
						}
						NewHardwareType = iVal;
						IList->Edit[0].Show();
						if(LanguageEnCn == 0)
							IList->STextDType.SetText(StrHwType[iVal],24);
						else
							IList->STextDType.SetText(StrHwTypeEn[iVal],24);
						IList->STextDType.Show();
					}
				}else
				if(Tag.Inx == 4){
					IList->EditUserCode.KeyIn(aKey);
					IList->EditUserCode.Show();
					int iVal = IList->EditUserCode.Str.ToInteger();
					if((iVal >0) && (iVal<100000000))Cover.AuxId=1; else Cover.AuxId=0;
					ShowCoverAuxId();
				}else
				if(Tag.Inx == 5){
					IList->EditAreaAssign.KeyIn(aKey);
					IList->EditAreaAssign.Show();
					int iVal = IList->EditAreaAssign.Str.ToInteger();
					if((iVal >0) && (iVal<=dAreaCountMax))Cover.Zone=1; else Cover.Zone=0;
					ShowCoverZone();
				}else
				if(Tag.Inx == 6){
					IList->Edit[1].KeyIn(aKey);
					IList->Edit[1].Show();
					int iVal = IList->Edit[1].Str.ToInteger();
					if(iVal <0)iVal = 0;
					if(iVal >255)iVal = 0;
					if(LanguageEnCn==0)IList->STextModuleInDefineType.SetText(StrTypeShortName[iVal],24);
					else IList->STextModuleInDefineType.SetText(StrTypeShortNameEn[iVal],24);
					IList->STextModuleInDefineType.Show();
					iVal = IList->Edit[1].Str.ToInteger();
					if((iVal >0) && (iVal<=254))Cover.AssignType=1; else Cover.AssignType=0;
					ShowCoverAssignType();
				}else
				if(Tag.Inx == 7){
					if(aKey == VK_RIGHT){
						ClearSelected();
						Tag.Inx =8;
						Check4Selected();
					}
				}else
				if(Tag.Inx == 8){
					if(aKey == VK_RIGHT){
						ClearSelected();
						Tag.Inx =9;
						Check4Selected();
					}else
					if(aKey == VK_LEFT){	
						ClearSelected();
						Tag.Inx =7;
						Check4Selected();
					}
				}else
				if(Tag.Inx == 9){
					if(aKey == VK_LEFT){
						ClearSelected();
						Tag.Inx =8;
						Check4Selected();
					}
				}
				break;
			case VK_UP:
				if(Tag.Inx >0){
					ClearSelected();
					Tag.Inx--;
					Check4Selected();
				}else{
					ClearSelected();
					Tag.Inx=11;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(Tag.Inx <11){
					ClearSelected();
					Tag.Inx++;
					Check4Selected();
				}else{
					ClearSelected();
					Tag.Inx=0;
					Check4Selected();
				}
				break;
			case VK_TAB:
				IsViewList = 1;
				ClearSelected();
				if(List4What == 0)ShowHardwareTypeList();
				else if(List4What == 1)ShowAssignTypeList();
				else	if(List4What == 2)ShowAreaList();
				break;
			case VK_EXECUTE:
				if(Tag.Inx == 7){
					if(SignalInputType==1)SignalInputType =0;
					else SignalInputType=1;
					RedrawModuleInSignalInType();
					if((SignalInputType >0) && (SignalInputType<4))Cover.Signal=1; else Cover.Signal=0;
					ShowCoverSignal();
				}else
				if(Tag.Inx == 8){
					if(SignalInputType==2)SignalInputType =0;
					else SignalInputType=2;
					RedrawModuleInSignalInType();
					if((SignalInputType >0) && (SignalInputType<4))Cover.Signal=1; else Cover.Signal=0;
					ShowCoverSignal();
				}else
				if(Tag.Inx == 9){
					if(SignalInputType==3)SignalInputType =0;
					else SignalInputType=3;
					RedrawModuleInSignalInType();
					if((SignalInputType >0) && (SignalInputType<4))Cover.Signal=1; else Cover.Signal=0;
					ShowCoverSignal();
				}else
				if(Tag.Inx == 10){
					//Add
					if(PrepUserCodeAssignTypeData2Write()){
						PermissionCmd = 1;
						if(SystemPermission >2){
							PermissionCmdGO();
						}else{
							IsPermission = 1;
							PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 3);
						}
					}else{
						if(LanguageEnCn==0)IList->STextLastState.SetText("数据错误",24);
						else IList->STextLastState.SetText("Data Err",24);
						IList->STextLastState.Show();
					}
				}else
				if(Tag.Inx == 11){
					//Remove
					if(PrepCheckToRemove()){
						PermissionCmd = 0;
						if(SystemPermission >2){
							PermissionCmdGO();
						}else{
							IsPermission = 1;
							PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 3);
						}
					}else{
						if(LanguageEnCn==0)IList->STextLastState.SetText("路径错误",24);
						else IList->STextLastState.SetText("Path Err",24);
						IList->STextLastState.Show();
					}
				}
				break;
			case VK_RETURN:
				IsOnGo =0;
				IsOnShow =0;
				IsWait4Rpy = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}else{
		switch(aKey){
			case VK_UP:
				if(List4What == 0){
					//Show dType
					if(HtListIndex >0){
						HtListIndex--;
						ShowHardwareTypeList();
					}
				}else
				if(List4What == 1){
					//Show Assign Type
					if(AtListIndex >0){
						AtListIndex--;
						ShowAssignTypeList();
					}
				}else
				if(List4What == 2){
					//Show Area Inf
					if(AreaListIndex >0){
						AreaListIndex--;
						ShowAreaList();
					}
				}
				break;
			case VK_DOWN:
				if(List4What == 0){
					if(HtListIndex < (HtPageCount -1)){
						HtListIndex++;
						ShowHardwareTypeList();
					}
				}else
				if(List4What == 1){
					//Show Assign Type
					if(AtListIndex < (AtPageCount -1)){
						AtListIndex++;
						ShowAssignTypeList();
					}
				}else
				if(List4What == 2){
					//Show Area Inf
					if(AreaListIndex < (AreaPageCount -1)){
						AreaListIndex++;
						ShowAreaList();
					}
				}
				break;
			case VK_TAB:
				IsViewList = 0;
				Check4Selected();
				if(List4What == 0)ShowHardwareTypeList();
				else if(List4What == 1)ShowAssignTypeList();
				else	if(List4What == 2)ShowAreaList();
				break;
			case VK_LEFT:
				if(List4What >0)List4What--;else List4What =2;
				DrawListDetail();
				if(List4What == 0)ShowHardwareTypeList();
				else if(List4What == 1)ShowAssignTypeList();
				else	if(List4What == 2)ShowAreaList();
				break;
			case VK_RIGHT:
				if(List4What <2)List4What++;else List4What =0;
				DrawListDetail();
				if(List4What == 0)ShowHardwareTypeList();
				else if(List4What == 1)ShowAssignTypeList();
				else	if(List4What == 2)ShowAreaList();
				break;
			case VK_EXECUTE:
				List4What ++ ;
				if(List4What > 2)List4What = 0;
				DrawListDetail();
				if(List4What == 0)ShowHardwareTypeList();
				else if(List4What == 1)ShowAssignTypeList();
				else	if(List4What == 2)ShowAreaList();
				break;
			case VK_RETURN:
				IsOnGo =0;
				IsOnShow =0;
				IsWait4Rpy = 0;
				aMsg = guiMsgReturn;
				break;
		}			
	}
	return aMsg;	
}

void TRegisterManual::FillRegData()
{
	unsigned int i, x;
	if(P0 == 0)return;
	if(P0 > dPORT_MAX_COUNT)return;
	if(P1Start == 0)return;
	if(P1Start > 250)return;
	if(P1End == 0)return;
	if(P1End > 250)return;
	
	if(IsAdd){
		if(IsAllTypeModify){
			for(i=P1Start-1; i<P1End; i++){
				Port[P0-1].ExistTable[i] = 0xA5;
				Port[P0-1].dTypeTable[i] = NewHardwareType;
			}
		}else{
			for(i=P1Start-1; i<P1End; i++){
				if(Port[P0-1].dTypeTable[i] == TypeToBeModify){
					Port[P0-1].ExistTable[i] = 0xA5;
					Port[P0-1].dTypeTable[i] = NewHardwareType;
				}
			}
		}
	}else{
		if(IsAllTypeModify){
			for(i=P1Start-1; i<P1End; i++){
				Port[P0-1].ExistTable[i] = 0;
				Port[P0-1].dTypeTable[i] = 0;
			}
		}else{
			for(i=P1Start-1; i<P1End; i++){
				if(Port[P0-1].dTypeTable[i] == TypeToBeModify){
					Port[P0-1].ExistTable[i] = 0;
					Port[P0-1].dTypeTable[i] = 0;
				}
			}
		}
	}	
}

void TRegisterManual::Task1000Ms()
{
	if(IsWait4Rpy){
		if(IsOnGo && IsOnShow){
			if(IsRecData){
				if( (Data[2] == RecData[2]) && (Data[3] == RecData[3]) && (Data[4] == RecData[4]) && (Data[5] == RecData[5]) && (Data[6] == RecData[6]) && (Data[7] == RecData[7])){
					IsWait4Rpy = 0;
					if(LanguageEnCn==0){
						if(IsAdd){
							IList->STextLastState.SetText("注册成功",24);
						}else{
							IList->STextLastState.SetText("清除成功",24);
						}
					}else{
						if(IsAdd){
							IList->STextLastState.SetText("Reg OK  ",24);
						}else{
							IList->STextLastState.SetText("Del OK  ",24);
						}
					}
					if( (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0) )
						IList->STextLastState.Show();
					//Write to Flash Here
					FillRegData();
					SaveToFlash(P0);
					Record.OperateAdd_RegisterByManual(Data[2], Data[3], Data[4], Data[6], IsAdd);
					Record.ReadRegisterData();
					Record.ReadDisplayPadSet();
				
					Record.ReadDevUserSet();
					Record.ReadDevAreaNum();
					unsigned int aUcFull = this->UserCode;
					int SaveOk =1;
					if(IsAdd){
						for(int x=P1Start-1; x<P1End; x++){
							if(NewHardwareType != Port[P0-1].dTypeTable[x]){
								SaveOk = 0;
								break;
							}
						}
					}else{
						for(int x=P1Start-1; x<P1End; x++){
							if(0 != Port[P0-1].dTypeTable[x]){
								SaveOk = 0;
								break;
							}
						}
					}
					if(SaveOk){
						//ShowSaveOk
					}else{
						//ShowSaveNotOk
						if(LanguageEnCn==0){
							IList->STextLastState.SetText("保存失败",24);
						}else{
							IList->STextLastState.SetText("Save Err",24);
						}
						if( (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0) )
							IList->STextLastState.Show();
					}
				}else{
					//Fail
					IsWait4Rpy = 0;
					if(LanguageEnCn==0){
						if(IsAdd){
							IList->STextLastState.SetText("注册失败",24);
						}else{
							IList->STextLastState.SetText("清除失败",24);
						}
					}else{
						if(IsAdd){
							IList->STextLastState.SetText("Reg Err ",24);
						}else{
							IList->STextLastState.SetText("Del Err ",24);
						}
					}
					if( (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0) )
						IList->STextLastState.Show();
				}
				IsWait4Rpy = 0;
			}else{
				TimeOut++;
				if(TimeOut > 6){
					IsWait4Rpy = 0;
					if(LanguageEnCn==0)IList->STextLastState.SetText("回复超时",24);
					else IList->STextLastState.SetText("Time Out",24);
					if( (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0) )
						IList->STextLastState.Show();
				}
			}
		}else{
			TimeOut++;
			if(TimeOut > 10){
				IsWait4Rpy = 0;
			}
		}
	}
}

void TRegisterManual::SendCmdAddMultAddr(unsigned char aCc, unsigned char sAddr, unsigned char eAddr)
{
	Data[0] = 0;
	Data[1] = MainCtl.fData.Split.MyNum;
	Data[2] = aCc;
	Data[3] = sAddr;
	Data[4] = eAddr;
	Data[5] = TypeToBeModify;
	Data[6] = NewHardwareType;
	Data[7] = 1;
	
	InnerCan.OBufInsert(1, cmSinglePortAddrManualRegist, Data, 8);	
}

void TRegisterManual::SendCmdRemoveMultAddr(unsigned char aCc, unsigned char sAddr, unsigned char eAddr)
{
	Data[0] = 0;
	Data[1] = MainCtl.fData.Split.MyNum;
	Data[2] = aCc;
	Data[3] = sAddr;
	Data[4] = eAddr;
	Data[5] = TypeToBeModify;
	Data[6] = 0;//NewHardwareType;
	Data[7] = 0;
	
	InnerCan.OBufInsert(1, cmSinglePortAddrManualRegist, Data, 8);	
}

void TRegisterManual::SendCmdOneAddr(int AddOrRemove)
{
	IsAllTypeModify = 1;
	if(AddOrRemove){
		SendCmdAddMultAddr(P0,P1Start,P1End);
	}else{
		SendCmdRemoveMultAddr(P0, P1Start, P1End);
	}
	IsWait4Rpy = 1;

}

void TRegisterManual::PushRecData(unsigned char *p)
{
	RecData[0] = p[0];
	RecData[1] = p[1];
	RecData[2] = p[2];
	RecData[3] = p[3];
	RecData[4] = p[4];
	RecData[5] = p[5];
	RecData[6] = p[6];
	RecData[7] = p[7];
	IsRecData = 1;
}

void TRegisterManual::SaveToFlash(unsigned char aPath0)
{
	unsigned int aAddr;
	unsigned char aP0, aP1;
	volatile unsigned int aWait;
	volatile unsigned int aFlashState;
	int i,x, iVal;
	if(aPath0 == 0)return;
	if(aPath0 > dPORT_MAX_COUNT)return;
	
	aP0 = aPath0-1;
	do{
		DelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
	
	//Erase 
	aAddr = dFlashRegisterDataAddr + (dFlashRegisterPortDataLen * aP0) + (256 * 0);
	nor_erase_sector_4k(dFlashRegisterDataChip,aAddr);
	do{
		DelayMs(2);
		aFlashState = nor_get_busy_state(0);
	}while(aFlashState);
	
	//write ExistTable dType DisplaySetting
	TRegister::sIList = IList;
	TRegister::sWriteOnePort(aPath0);
	
	//Write AuxID, AssignType, Signal Type 
	if( (Cover.AuxId) || (Cover.AssignType) || (Cover.Signal) ){
		//AudId
		iVal = IList->EditUserCode.Str.ToInteger();
		AuxId = static_cast<unsigned int>(iVal);
		if(AuxId>99999999)AuxId=1;
		//Assign Type
		iVal = IList->Edit[1].Str.ToInteger();
		if(iVal<0) iVal =1;
		if(iVal>255)iVal =1;
		AssignType = static_cast<unsigned char>(iVal);
		//Signal Type
		if(SignalInputType){
			if(SignalInputType==1)Signal=1;
			if(SignalInputType==2)Signal=2;
			if(SignalInputType==3)Signal=4;
		}else{
			Cover.Signal = 0;
		}
		
		for(x=P1Start -1; (x>-1)and(x<P1End)and(x<dEP_MAX_COUNT_PER_PORT); x++){
			if(Cover.AuxId){
				ReadWriteData.D32[x] = AuxId;
				AuxId++;
				if(AuxId>99999999)AuxId=1;
			}
			if(Cover.AssignType)Port[aP0].AssignType[x]=AssignType;
			if(Cover.Signal)Port[aP0].FuncSetByte[x]=Signal;
		}
		
		//Write
		TDevProperty::sIList = IList;
		TDevProperty::sWriteAuxIdAssignTypeSignalType(aP0+1);
	}
	
	if(Cover.Zone){
		iVal = IList->EditAreaAssign.Str.ToInteger();
		if(iVal<1)iVal=1;
		if(iVal>dAreaCountMax)iVal=1;
		ZoneID = static_cast<unsigned short>(iVal);
		for(x=P1Start -1; (x>-1)and(x<P1End)and(x<dEP_MAX_COUNT_PER_PORT); x++){
			Port[aP0].Area[x] = ZoneID;
		}
		TDevProperty::sIList = IList;
		TDevProperty::sWriteAreaNumOnePort(aPath0);
	}
}

int TRegisterManual::PrepUserCodeAssignTypeData2Write()
{
	int iVal;
	if(3){
		//P0 = IList->EditPortNum.Str.ToInteger();
		//P1Start = IList->EditAddrNum.Str.ToInteger();
		//P1End = IList->EditDescpShow.Str.ToInteger();
		//this->UserCode = IList->EditUserCode.Str.ToInteger();
		
		iVal = IList->EditPortNum.Str.ToInteger();
		if(iVal<0)return 0;
		if(iVal>dPORT_MAX_COUNT)return 0;
		P0=iVal;
		
		iVal = IList->EditAddrNum.Str.ToInteger();
		if(iVal<1)return 0;
		if(iVal>dEP_MAX_COUNT_PER_PORT)return 0;
		P1Start = iVal;
		
		iVal = IList->EditDescpShow.Str.ToInteger();
		if(iVal<1)return 0;
		if(iVal>dEP_MAX_COUNT_PER_PORT)return 0;
		P1End = iVal;
		
		iVal = IList->Edit[0].Str.ToInteger();
		if(iVal <1)return 0;
		if(iVal >255)return 0;
		NewHardwareType = iVal;
		
		iVal = IList->Edit[1].Str.ToInteger();
		if(iVal <0)return 0;
		if(iVal >255)return 0;
		NewAssignType = iVal;
		
		iVal = IList->EditUserCode.Str.ToInteger();
		if(iVal>999999999)return 0;

		Pitch = 1;
		TypeToBeModify = 0;
		
		return 1;
	}
	return 0;
}

int TRegisterManual::PrepCheckToRemove()
{
	int iVal;
	if(3){
		//P0 = IList->EditPortNum.Str.ToInteger();
		//P1Start = IList->EditAddrNum.Str.ToInteger();
		//P1End = IList->EditDescpShow.Str.ToInteger();
		//this->UserCode = IList->EditUserCode.Str.ToInteger();
		
		iVal = IList->EditPortNum.Str.ToInteger();
		if(iVal<0)return 0;
		if(iVal>dPORT_MAX_COUNT)return 0;
		P0=iVal;
		
		iVal = IList->EditAddrNum.Str.ToInteger();
		if(iVal<1)return 0;
		if(iVal>dEP_MAX_COUNT_PER_PORT)return 0;
		P1Start = iVal;
		
		iVal = IList->EditDescpShow.Str.ToInteger();
		if(iVal<1)return 0;
		if(iVal>dEP_MAX_COUNT_PER_PORT)return 0;
		P1End = iVal;
		
		//iVal = IList->Edit[0].Str.ToInteger();
		//if(iVal <0)return 0;
		//if(iVal >255)return 0;
		//NewHardwareType = iVal;
		
		//iVal = IList->Edit[1].Str.ToInteger();
		//if(iVal <0)return 0;
		//if(iVal >255)return 0;
		//NewAssignType = iVal;
		
		//iVal = IList->EditUserCode.Str.ToInteger();
		//if(iVal>999999999)return 0;

		//Pitch = 1;
		TypeToBeModify = 0;
		
		return 1;
	}
	return 0;
}

int TRegisterManual::ExtRequst(unsigned char Prm)
{
	IsOnGo =0;
	IsOnShow =0;
	IsWait4Rpy = 0;
	return 0;
}



