#include "DeviceSetup.h"
#include "gvalue.h"
#include "Runtime.h"
#include "server.h"

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

static const char NonTypeText[12]= {"未知"};

void TDeviceSetup::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, 	Right, 	Bottom);
	
	Color = aColor;
	#undef CH

	Caption.Border.Color = clGray;
	
	IsShowing = 0;
	SetupMode = 0;

	CtlIndex = 0;
}

void TDeviceSetup::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 TDeviceSetup::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 TDeviceSetup::RenderBackGround(void)
{
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);
}

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

void TDeviceSetup::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 TDeviceSetup::Show(void)
{	
	IsShowing = 1;
	RenderBackGround();
	DrawSelf();
	ShowCaption();
}

void TDeviceSetup::FullRedraw(int Prm)
{	
	if(Prm == 0){
		IsShowing = 1;
		RenderBackGround();
		DrawSelf();
		ShowCaption();
		aPanel->Show();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		aPanel->Show();
	}
}

void TDeviceSetup::ShowCaption()
{
	TMyString::sFromStr("系统设置->设备定义",  Caption.Text);
	Caption.Show();
}

//______________________________________________________________________________________________________________________________
void TDeviceSetup::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;
	
	if(aPanel == nullptr)return;
	
	aPanel->Init(200,40,400,260,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(96,16,220,36,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[1].Init(96,66,220,36,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[2].Init(96,116,220,36,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[3].Init(96,166,220,36,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("回路设备连续定义");
	aPanel->Btn[1].Caption.FromStr("回路设备一般修改");
	aPanel->Btn[2].Caption.FromStr("回路设备快速定义");
	aPanel->Btn[3].Caption.FromStr(" 多线盘节点定义 ");
	
	aPanel->Btn[0].Visible = 1;
	aPanel->Btn[1].Visible = 1;
	aPanel->Btn[2].Visible = 1;
	aPanel->Btn[3].Visible = 1;
	aPanel->Btn[0].Enable = 1;
	aPanel->Btn[1].Enable = 1;
	aPanel->Btn[2].Enable = 1;
	aPanel->Btn[3].Enable = 1;
	
	aPanel->Btn[0].SetCaptionLeftSpacing(6);
	aPanel->Btn[1].SetCaptionLeftSpacing(6);
	aPanel->Btn[2].SetCaptionLeftSpacing(6);
	aPanel->Btn[3].SetCaptionLeftSpacing(6);
	
	RenderBackGround();
	SetupMode = 0;
	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
}

void TDeviceSetup::DrawTip(void)
{
	if(SetupMode == 1)
		TextRender_string24(60, 60, clNearBlack, Color, "回路设备连续定义");
	if(SetupMode == 2)
		TextRender_string24(60, 60, clNearBlack, Color, "回路设备一般修改");
	if(SetupMode == 3)
		TextRender_string24(60, 60, clNearBlack, Color, "回路设备快速定义");
	if(SetupMode == 4)
		TextRender_string24(60, 60, clNearBlack, Color, " 多线盘节点定义 ");
	
	if(SetupMode == 4){
		TextRender_string24(16, 110, clNearBlack, Color, "1.设定为消防泵的");
		TextRender_string24(16, 142, clNearBlack, Color, "  指派类型无效");
		
		TextRender_string24(16, 210, clNearBlack, Color, "2.设置键查询键更改类型");

		TextRender_string24(16, 246, clNearBlack, Color, "3.上下键切换选项");
		TextRender_string24(16, 282, clNearBlack, Color, "4.左右键数字键编辑");
		TextRender_string24(16, 318, clNearBlack, Color, "5.确认键保存");
	}else{
		TextRender_string24(16, 110, clNearBlack, Color, "1.联动类型未指派的");
		TextRender_string24(16, 142, clNearBlack, Color, "  按原设备类型执行联动");
		TextRender_string24(16, 174, clNearBlack, Color, "  指派后行为覆盖原类型");
		
		TextRender_string24(16, 210, clNearBlack, Color, "2.设置键查询键更改类型");

		TextRender_string24(16, 246, clNearBlack, Color, "3.上下键切换选项");
		TextRender_string24(16, 282, clNearBlack, Color, "4.左右键数字键编辑");
		TextRender_string24(16, 318, clNearBlack, Color, "5.确认键保存");
		
		if(SetupMode == 1){
			TextRender_string24(16, 356, clNearBlack, Color, "6.保存后地址及二次码");
			TextRender_string24(16, 392, clNearBlack, Color, "  自动加一");
		}else
		if(SetupMode == 2){
			TextRender_string24(16, 356, clNearBlack, Color, "6.保存后重新加载");
		}
	}
	
}

void TDeviceSetup::InitPanel4Setup(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;
	
	if(aPanel == nullptr)return;
	
	aPanel->Init(300,26,480,320,x,y,clFrmFace, bvRaised);
	aPanel->Caption.TextClear();
	aPanel->Caption.Color=clFrmFace;
	aPanel->Caption.Visible = 1;
	aPanel->Caption.Enable = 1;

	
	x = aPanel->Content.Left;
	y = aPanel->Bound.Top + 4;
	
	aPanel->SText[0].Init(6,6,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(6,46,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(6,86,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[3].Init(6,126,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[4].Init(6,166,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[5].Init(6,206,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[6].Init(26,240,400,30,x,y,clNearBlack,clFrmFace);
	
	aPanel->SText[11].Init(170,126,48,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[12].Init(222,126,246,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[13].Init(222,166,246,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[20].Init(270,6,200,30,x,y,clNearBlack,clFrmFace);
	
	TMyString::sFromStr("回路号:", aPanel->SText[0].Text);
	TMyString::sFromStr("地址号:", aPanel->SText[1].Text);
	TMyString::sFromStr("二次码:", aPanel->SText[2].Text);
	TMyString::sFromStr("原设备类型:", aPanel->SText[3].Text);
	TMyString::sFromStr("指派联动类型:", aPanel->SText[4].Text);
	TMyString::sFromStr("位置描述信息:", aPanel->SText[5].Text);
	TMyString::sFromStr("12345678901234567890123456789012", aPanel->SText[6].Text);
	
	TMyString::sFromStr("xxx", aPanel->SText[11].Text);
	TMyString::sFromStr("12345678901234567890", aPanel->SText[12].Text);
	TMyString::sFromStr("12345678901234567890", aPanel->SText[13].Text);
	TMyString::sFromStr(" ", aPanel->SText[20].Text);
	
	aPanel->Edit[0].Init(170,6,48,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[1].Init(170,46,48,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[2].Init(170,86,120,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[3].Init(170,166,48,32,x,y,clNearBlack,clFrmFace);

	for(i=0; i<4; i++){
		aPanel->Edit[i].Enable =1;
		aPanel->Edit[i].Visible =1;
	}
	aPanel->Edit[0].Str.FromStr("xx");
	aPanel->Edit[1].Str.FromStr("xxx");
	aPanel->Edit[2].Str.FromStr("xxxxxxxx");
	aPanel->Edit[3].Str.FromStr("xxx");
	aPanel->Edit[0].MaxLen = 2;
	aPanel->Edit[1].MaxLen = 3;
	aPanel->Edit[2].MaxLen = 8;
	aPanel->Edit[3].MaxLen = 3;
	aPanel->Edit[0].SetPstTail();
	aPanel->Edit[1].SetPstTail();
	aPanel->Edit[2].SetPstTail();
	aPanel->Edit[3].SetPstTail();
	
	RenderBackGround();
	DrawTip();
	CtlIndex =0;
	aPanel->Show();
	Check4SelectedInSetup();
	aPanel->ReDrawItems();
	LoadStart();
}


void TDeviceSetup::InitPanel4Fast(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;
	
	if(aPanel == nullptr)return;
	
	aPanel->Init(300,46,480,280,x,y,clFrmFace, bvRaised);
	aPanel->Caption.TextClear();
	aPanel->Caption.Color=clFrmFace;
	aPanel->Caption.Visible = 1;
	aPanel->Caption.Enable = 1;
	
	x = aPanel->Content.Left;
	y = aPanel->Bound.Top + 4;
	
	aPanel->SText[0].Init(6,6,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(6,46,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(6,86,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[3].Init(6,126,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[4].Init(6,166,110,30,x,y,clNearBlack,clFrmFace);
	//aPanel->SText[5].Init(6,206,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[6].Init(6,206,110,30,x,y,clNearBlack,clFrmFace);
	//aPanel->SText[7].Init(6,286,110,30,x,y,clNearBlack,clFrmFace);
	//aPanel->SText[8].Init(26,320,400,30,x,y,clNearBlack,clFrmFace);
	
	//aPanel->SText[11].Init(170,206,102,30,x,y,clNearBlack,clFrmFace);
	//aPanel->SText[12].Init(220,206,102,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[13].Init(220,206,246,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[20].Init(270,6,200,30,x,y,clNearBlack,clFrmFace);
	
	TMyString::sFromStr("回路号:", aPanel->SText[0].Text);
	TMyString::sFromStr("起始地址:", aPanel->SText[1].Text);
	TMyString::sFromStr("结束地址:", aPanel->SText[2].Text);
	TMyString::sFromStr("二次码:", aPanel->SText[3].Text);
	TMyString::sFromStr("步长:", aPanel->SText[4].Text);
	//TMyString::sFromStr("原设备类型", aPanel->SText[5].Text);
	TMyString::sFromStr("指派联动类型:", aPanel->SText[6].Text);
	//TMyString::sFromStr("位置描述信息:", aPanel->SText[7].Text);
	//TMyString::sFromStr("12345678901234567890123456789012", aPanel->SText[8].Text);
	
	//TMyString::sFromStr("xxx", aPanel->SText[11].Text);
	//TMyString::sFromStr("12345678901234567890", aPanel->SText[12].Text);
	TMyString::sFromStr("12345678901234567890", aPanel->SText[13].Text);
	TMyString::sFromStr(" ", aPanel->SText[20].Text);
	
	aPanel->Edit[0].Init(170,6,48,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[1].Init(170,46,48,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[2].Init(170,86,48,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[3].Init(170,126,120,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[4].Init(170,166,80,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[5].Init(170,206,48,32,x,y,clNearBlack,clFrmFace);
	
	for(i=0; i<6; i++){
		aPanel->Edit[i].Enable =1;
		aPanel->Edit[i].Visible =1;
	}
	aPanel->Edit[0].Str.FromStr("xx");
	aPanel->Edit[1].Str.FromStr("xxx");
	aPanel->Edit[2].Str.FromStr("xxx");
	aPanel->Edit[3].Str.FromStr("xxxxxxxx");
	aPanel->Edit[4].Str.FromStr("xxxx");
	aPanel->Edit[5].Str.FromStr("---");
	aPanel->Edit[6].Str.FromStr("---");
	aPanel->Edit[0].MaxLen = 2;
	aPanel->Edit[1].MaxLen = 3;
	aPanel->Edit[2].MaxLen = 3;
	aPanel->Edit[3].MaxLen = 8;
	aPanel->Edit[4].MaxLen = 4;
	aPanel->Edit[5].MaxLen = 3;
	aPanel->Edit[0].SetPstTail();
	aPanel->Edit[1].SetPstTail();
	aPanel->Edit[2].SetPstTail();
	aPanel->Edit[3].SetPstTail();
	aPanel->Edit[4].SetPstTail();
	aPanel->Edit[5].SetPstTail();
	
	RenderBackGround();
	DrawTip();
	CtlIndex =0;
	aPanel->Show();
	Check4SelectedInFast();
	aPanel->ReDrawItems();
	LoadStart();
}


void TDeviceSetup::InitPanel4DirectPad(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;
	
	if(aPanel == nullptr)return;
	
	aPanel->Init(300,26,480,320,x,y,clFrmFace, bvRaised);
	aPanel->Caption.TextClear();
	aPanel->Caption.Color=clFrmFace;
	aPanel->Caption.Visible = 1;
	aPanel->Caption.Enable = 1;

	
	x = aPanel->Content.Left;
	y = aPanel->Bound.Top + 4;
	
	aPanel->SText[0].Init(6,6,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(6,46,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(6,86,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[3].Init(6,126,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[4].Init(6,166,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[5].Init(6,206,110,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[6].Init(26,240,400,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[7].Init(222,46,80,30,x,y,clNearBlack,clFrmFace);
	
	aPanel->SText[11].Init(170,126,48,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[12].Init(222,126,246,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[13].Init(222,166,246,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[20].Init(270,6,200,30,x,y,clNearBlack,clFrmFace);
	
	TMyString::sFromStr("多线盘号:", aPanel->SText[0].Text);
	TMyString::sFromStr("节点号:", aPanel->SText[1].Text);
	TMyString::sFromStr("二次码:", aPanel->SText[2].Text);
	TMyString::sFromStr("已设定消防泵:", aPanel->SText[3].Text);
	TMyString::sFromStr("指派联动类型:", aPanel->SText[4].Text);
	TMyString::sFromStr("位置描述信息:", aPanel->SText[5].Text);
	TMyString::sFromStr("12345678901234567890123456789012", aPanel->SText[6].Text);
	TMyString::sFromStr("未注册", aPanel->SText[7].Text);
	
	TMyString::sFromStr("xxx", aPanel->SText[11].Text);
	TMyString::sFromStr(" ", aPanel->SText[12].Text);
	TMyString::sFromStr("12345678901234567890", aPanel->SText[13].Text);
	TMyString::sFromStr(" ", aPanel->SText[20].Text);
	
	aPanel->Edit[0].Init(170,6,48,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[1].Init(170,46,48,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[2].Init(170,86,120,32,x,y,clNearBlack,clFrmFace);
	aPanel->Edit[3].Init(170,166,48,32,x,y,clNearBlack,clFrmFace);

	for(i=0; i<4; i++){
		aPanel->Edit[i].Enable =1;
		aPanel->Edit[i].Visible =1;
	}
	aPanel->Edit[0].Str.FromStr("xx");
	aPanel->Edit[1].Str.FromStr("xxx");
	aPanel->Edit[2].Str.FromStr("xxxxxxxx");
	aPanel->Edit[3].Str.FromStr("xxx");
	aPanel->Edit[0].MaxLen = 2;
	aPanel->Edit[1].MaxLen = 3;
	aPanel->Edit[2].MaxLen = 8;
	aPanel->Edit[3].MaxLen = 3;
	aPanel->Edit[0].SetPstTail();
	aPanel->Edit[1].SetPstTail();
	aPanel->Edit[2].SetPstTail();
	aPanel->Edit[3].SetPstTail();
	
	RenderBackGround();
	DrawTip();
	CtlIndex =0;
	aPanel->Show();
	Check4SelectedInSetup();
	aPanel->ReDrawItems();
	LoadStart();
}


//_____________________________________________________________________________________________________________________________

void TDeviceSetup::ShowSaveOk()
{
	TMyString::sFromStr("保存成功", aPanel->SText[20].Text);
	aPanel->SText[20].Show();
}

void TDeviceSetup::ShowSaveNotOk()
{
	TMyString::sFromStr("写入失败", aPanel->SText[20].Text);
	aPanel->SText[20].Show();
}

void TDeviceSetup::ShowCheckDataFail()
{
	TMyString::sFromStr("请检查设置值", aPanel->SText[20].Text);
	aPanel->SText[20].Show();
}

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

void TDeviceSetup::Check4Selected()
{
	aPanel->Btn[0].Selected = 0;
	aPanel->Btn[1].Selected = 0;
	aPanel->Btn[2].Selected = 0;
	aPanel->Btn[3].Selected = 0;
	if(CtlIndex == 0){
		aPanel->Btn[0].Selected = 1;
	}else
	if(CtlIndex == 1){
		aPanel->Btn[1].Selected = 1;
	}else
	if(CtlIndex == 2){
		aPanel->Btn[2].Selected = 1;
	}else
	if(CtlIndex == 3){
		aPanel->Btn[3].Selected = 1;
	}
}

void TDeviceSetup::Check4SelectedInSetup()
{
	aPanel->Edit[0].Selected = 0;
	aPanel->Edit[1].Selected = 0;
	aPanel->Edit[2].Selected = 0;
	aPanel->Edit[3].Selected = 0;

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

void TDeviceSetup::Check4SelectedInFast()
{
	aPanel->Edit[0].Selected = 0;
	aPanel->Edit[1].Selected = 0;
	aPanel->Edit[2].Selected = 0;
	aPanel->Edit[3].Selected = 0;
	aPanel->Edit[4].Selected = 0;
	aPanel->Edit[5].Selected = 0;

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

void TDeviceSetup::GetNewPathAddr()
{
	unsigned int iVal;
	iVal = aPanel->Edit[0].Str.ToInteger();
	if( (iVal >0) && (iVal<=dPORT_MAX_COUNT) ){
		Path0 = iVal;
	}else{
		Path0 = 0;
	}
	iVal = aPanel->Edit[1].Str.ToInteger();
	if( (iVal >0) && (iVal<=250) ){
		Addr = iVal;
	}else{
		Addr = 0;
	}
}

void TDeviceSetup::LoadData(int Inc)
{
	unsigned int aAddr,i;
	unsigned char aP0, aP1;
	if(  (SetupMode ==1) || (SetupMode == 2) ){
		if(  (Path0>0) && (Path0<dPORT_MAX_COUNT) && (Addr>0) && (Addr<=250) ){
			aP0 = Path0-1;
			aP1 = Addr-1;
			if(Port[aP0].ExistTable[aP1]){
				IsFound =1;
			}else{
				IsFound =0;
			}
		}else{
			IsFound =0;
		}
		if(IsFound){
			if(SetupMode ==1){
				if(Inc){
					aPanel->Edit[2].Str.FromUInt8Dg(UserCode.Full);
					aPanel->Edit[3].Str.FromUInt3Dg(AssignType);
				}else{
					AssignType = Port[aP0].AssignType[aP1];
					
					aPanel->Edit[2].Str.FromUInt8Dg(Port[aP0].UcList[aP1].Full);
					aPanel->Edit[3].Str.FromUInt3Dg(AssignType);
				}
			}else
			if(SetupMode ==2){
				aPanel->Edit[2].Str.FromUInt8Dg(Port[aP0].UcList[aP1].Full);
				aPanel->Edit[3].Str.FromUInt3Dg(Port[aP0].AssignType[aP1]);
			}
			TMyString::sFrom3Dg(Port[aP0].dTypeTable[aP1], aPanel->SText[11].Text);
			TMyString::sFromStr(StrTypeShortName[Port[aP0].dTypeTable[aP1]], aPanel->SText[12].Text);
			TMyString::sFromStr(StrTypeShortName[AssignType], aPanel->SText[13].Text);
			
			aAddr = dAddrSdRam_Descp + (aP0 * 256 * 32) + (aP1 * 32);
			for(i=0; i<8; i++){
				ReadWriteData.D32[i] = *(volatile unsigned int *)(aAddr + (i *4));
			}
			for(i=0; i<32; i++){
				aPanel->SText[6].Text[i] = ReadWriteData.D8[i];
			}
			aPanel->SText[6].Text[32] = '\0';
			aPanel->Edit[2].Show();
			aPanel->Edit[3].Show();
			aPanel->SText[11].Show();
			aPanel->SText[12].Show();
			aPanel->SText[13].Show();
			aPanel->SText[6].Show();
		}else{
			aPanel->Edit[2].Str.FromUInt8Dg(0);
			aPanel->Edit[3].Str.FromUInt3Dg(0);
			TMyString::sFromStr("xxx", aPanel->SText[11].Text);
			TMyString::sFromStr("无效", aPanel->SText[12].Text);
			TMyString::sFromStr("无效", aPanel->SText[13].Text);
			
			TMyString::sFromStr("此地址未登记", aPanel->SText[6].Text);
			
			
			aPanel->Edit[2].Show();
			aPanel->Edit[3].Show();
			aPanel->SText[11].Show();
			aPanel->SText[12].Show();
			aPanel->SText[13].Show();
			aPanel->SText[6].Show();
		}
	}else
	if(SetupMode == 3){
		
	}
}


void TDeviceSetup::DrawFast()
{
	unsigned int aAddr,i;
	unsigned char aP0, aP1;

	if(SetupMode == 3){
		
	}
}

void TDeviceSetup::FindType(int Dir)
{
	unsigned char aInx;
	int i;
	aInx = AssignType;
	if(Dir == -1){
		for(i=0; i<257; i++){
			aInx--;
			if(  (StrTypeShortName[aInx][0] != NonTypeText[0]) && (StrTypeShortName[aInx][1] != NonTypeText[1])  ){
				AssignType = aInx;
				break;
			}
		}
	}else{
		for(i=0; i<257; i++){
			aInx++;
			if(  (StrTypeShortName[aInx][0] != NonTypeText[0]) && (StrTypeShortName[aInx][1] != NonTypeText[1])  ){
				AssignType = aInx;
				break;
			}
		}		
	}
	
	if( (SetupMode == 1) || (SetupMode == 2) || (SetupMode == 4)){
		aPanel->Edit[3].Str.FromUInt3Dg(AssignType);
		aPanel->Edit[3].Show();
	}else
	if(SetupMode == 3){
		aPanel->Edit[5].Str.FromUInt3Dg(AssignType);
		aPanel->Edit[5].Show();
	}
	TMyString::sFromStr(StrTypeShortName[AssignType], aPanel->SText[13].Text);
	aPanel->SText[13].Show();
}

void TDeviceSetup::CheckDirectPadAva()
{
	int iVal0, iVal1;
	unsigned char OldAva;
	OldAva = DirectPadAva;
	iVal0 = aPanel->Edit[0].Str.ToInteger();
	iVal1 = aPanel->Edit[1].Str.ToInteger();
	
	if( (iVal0 !=0) && (iVal0 <= dDIRECTCTL_MAX_COUNT) && (iVal1 !=0) && (iVal1 <= dDIRECTCTL_KEY_MAX_COUNT) ){
		DirectPadAva = 1;
		DirectPadNum = iVal0 -1;
		NodeNum = iVal1 -1;
	}else{
		DirectPadAva = 0;
	}
	
	if( DirectPadAva == 0 ){
		aPanel->SText[7].Enable =0;
		aPanel->SText[7].TextClear();
		aPanel->SText[7].Show();
		aPanel->Edit[2].Enable = 0;
		aPanel->Edit[3].Enable = 0;
		aPanel->Edit[2].Str.Clear();
		aPanel->Edit[3].Str.Clear();
		aPanel->Edit[2].Show();
		aPanel->Edit[3].Show();
	}else{
		if(OldAva != DirectPadAva){
			aPanel->SText[7].Enable =1;
			aPanel->Edit[2].Enable = 1;
			aPanel->Edit[3].Enable = 1;
		}
		aPanel->Edit[2].Show();
		aPanel->Edit[3].Show();
	}
}

void TDeviceSetup::DirectPadDataShow()
{
	unsigned int aAddr,i;
	unsigned char aP0, aP1;
	
	if(DirectPadAva == 0 ) return;
		
		UserCode.Full = DirectCtlBoard[DirectPadNum].UcList[NodeNum].Full;
		AssignType = DirectCtlBoard[DirectPadNum].AssignType[NodeNum];
		if(DirectCtlBoard[DirectPadNum].IsRegisted[NodeNum]){
			TMyString::sFromStr("已注册", aPanel->SText[7].Text);
		}else{
			TMyString::sFromStr("未注册", aPanel->SText[7].Text);
		}
		if(DirectCtlBoard[DirectPadNum].IsPump[NodeNum]){
			TMyString::sFromStr("是", aPanel->SText[11].Text);
		}else{
			TMyString::sFromStr("否", aPanel->SText[11].Text);
		}
		
		aPanel->Edit[0].Str.FromUInt2Dg(DirectPadNum +1);
		aPanel->Edit[1].Str.FromUInt3Dg(NodeNum +1);
		aPanel->Edit[2].Str.FromUInt8Dg(UserCode.Full);
		aPanel->Edit[3].Str.FromUInt3Dg(AssignType);
		aPanel->Edit[0].Show();
		aPanel->Edit[1].Show();
		aPanel->Edit[2].Show();
		aPanel->Edit[3].Show();
		TMyString::sFromStr(StrTypeShortName[AssignType], aPanel->SText[13].Text);
		aPanel->SText[11].Show();
		aPanel->SText[13].Show();
		
		aP1 = DirectPadNum * dDIRECTCTL_KEY_MAX_COUNT + (NodeNum +1);
		aAddr = dAddrSdRam_Descp + (40 * 256 * 32) + (aP1 * 32);
		for(i=0; i<8; i++)ReadWriteData.D32[i] = *(volatile unsigned int *)(aAddr + (i) *4);
		for(i=0; i<32; i++)aPanel->SText[6].Text[i] = ReadWriteData.D8[i];
		aPanel->SText[6].Text[32] = '\0';		
		
		aPanel->SText[6].Show();
		aPanel->SText[7].Show();
}

void TDeviceSetup::GetMinMax(unsigned int aP0)
{
	unsigned int p0, x;
	if(aP0 == 0)return;
	if(aP0 >dPORT_MAX_COUNT)return;
	
	p0 = aP0;
	
	Port[p0].UserCodeMin = 0xFFFFFFFF;
	Port[p0].UserCodeMax = 0;
	
	for(x=0;x<256;x++){
		if(Port[p0].UcList[x].Full < Port[p0].UserCodeMin){
			Port[p0].UserCodeMin = Port[p0].UcList[x].Full;
		}
		if(Port[p0].UcList[x].Full > Port[p0].UserCodeMax){
			Port[p0].UserCodeMax = Port[p0].UcList[x].Full;
		}
	}
}


TGuiMsgReturn TDeviceSetup::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Err;
	unsigned char x,y,aCh;
	unsigned char Path0Old, AddrOld;
	unsigned int aAddr, aUcFull;
	int iVal;
	int SaveOk;
	TDMix aFullPath;

	if(SetupMode == 1){
		//Continute
		switch(aKey){
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case VK_LEFT:
			case VK_RIGHT:
				Path0Old = Path0;
				AddrOld = Addr;
				if(CtlIndex <4){
					aPanel->Edit[CtlIndex].KeyIn(aKey);
					aPanel->Edit[CtlIndex].Show();
				}
				if(CtlIndex <2){
					GetNewPathAddr();
					if( (Path0Old != Path0) || (AddrOld != Addr) ){
						LoadData(0);
					}
				}else
				if(CtlIndex == 3){
					iVal = aPanel->Edit[3].Str.ToInteger();
					if(iVal < 256){
						AssignType = iVal;
					}else{
						AssignType = 255;
						aPanel->Edit[CtlIndex].Str.FromUInt3Dg(AssignType);
						aPanel->Edit[CtlIndex].Show();
					}
					aPanel->SText[13].SetText(StrTypeShortName[AssignType],24);
					aPanel->SText[13].Show();
				}
				break;
			case VK_DELETE:
				Path0Old = Path0;
				AddrOld = Addr;
				if(CtlIndex <4){
					aPanel->Edit[CtlIndex].KeyIn(aKey);
					aPanel->Edit[CtlIndex].Show();
				}
				if(CtlIndex <2){
					GetNewPathAddr();
					if( (Path0Old != Path0) || (AddrOld != Addr) ){
						LoadData(0);
					}
				}else
				if(CtlIndex == 3){
					iVal = aPanel->Edit[3].Str.ToInteger();
					if(iVal < 256){
						AssignType = iVal;
					}else{
						AssignType = 255;
						aPanel->Edit[CtlIndex].Str.FromUInt3Dg(AssignType);
						aPanel->Edit[CtlIndex].Show();
					}
					aPanel->SText[13].SetText(StrTypeShortName[AssignType],24);
					aPanel->SText[13].Show();
				}
				break;
			case VK_UP:
				if(CtlIndex >0){
					CtlIndex--;
					Check4SelectedInSetup();
					aPanel->ReDrawItems();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <3){
					CtlIndex++;
					Check4SelectedInSetup();
					aPanel->ReDrawItems();
				}
				break;
			case dfKEY_SETUP:
				if(CtlIndex == 3){
					FindType(-1);
				}
				break;
			case dfKEY_QUERY:
				if(CtlIndex == 3){
					FindType(1);
				}
				break;
			case VK_EXECUTE:
				if(PrepData2Write()){
					Write2Flash();
					Record.ReadDevUserSet();
					if(Port[Path0-1].UcList[Addr-1].Full == UserCode.Full){
						SaveOk =1;
						ShowSaveOk();
						LoadNext();
						LoadData(1);
					}else{
						SaveOk =0;
						ShowSaveNotOk();
					}
				}else{
					ShowCheckDataFail();
				}
				break;
			case VK_RETURN:
				CtlIndex =0;
				InitPanel();
				break;
		}
		if( (VK_EXECUTE != aKey) && (VK_RETURN != aKey) ){
			ClearOutText();
		}
	}else
	if(SetupMode == 2){
		// Inh
		switch(aKey){
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case VK_LEFT:
			case VK_RIGHT:
				Path0Old = Path0;
				AddrOld = Addr;
				if(CtlIndex <4){
					aPanel->Edit[CtlIndex].KeyIn(aKey);
					aPanel->Edit[CtlIndex].Show();
				}
				if(CtlIndex <2){
					GetNewPathAddr();
					if( (Path0Old != Path0) || (AddrOld != Addr) ){
						LoadData(0);
					}
				}else
				if(CtlIndex == 3){
					iVal = aPanel->Edit[3].Str.ToInteger();
					if(iVal < 256){
						AssignType = iVal;
					}else{
						AssignType = 255;
						aPanel->Edit[CtlIndex].Str.FromUInt3Dg(AssignType);
						aPanel->Edit[CtlIndex].Show();
					}
					aPanel->SText[13].SetText(StrTypeShortName[AssignType],24);
					aPanel->SText[13].Show();
				}
				break;
			case VK_DELETE:
				Path0Old = Path0;
				AddrOld = Addr;
				if(CtlIndex <4){
					aPanel->Edit[CtlIndex].KeyIn(aKey);
					aPanel->Edit[CtlIndex].Show();
				}
				if(CtlIndex <2){
					GetNewPathAddr();
					if( (Path0Old != Path0) || (AddrOld != Addr) ){
						LoadData(0);
					}
				}else
				if(CtlIndex == 3){
					iVal = aPanel->Edit[3].Str.ToInteger();
					if(iVal < 256){
						AssignType = iVal;
					}else{
						AssignType = 255;
						aPanel->Edit[CtlIndex].Str.FromUInt3Dg(AssignType);
						aPanel->Edit[CtlIndex].Show();
					}
					aPanel->SText[13].SetText(StrTypeShortName[AssignType],24);
					aPanel->SText[13].Show();
				}
				break;
			case dfKEY_SETUP:
				if(CtlIndex == 3){
					FindType(-1);
				}
				break;
			case dfKEY_QUERY:
				if(CtlIndex == 3){
					FindType(1);
				}
				break;
			case VK_UP:
				if(CtlIndex >0){
					CtlIndex--;
					Check4SelectedInSetup();
					aPanel->ReDrawItems();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <3){
					CtlIndex++;
					Check4SelectedInSetup();
					aPanel->ReDrawItems();
				}
				break;
			case VK_EXECUTE:
				if(PrepData2Write()){
					Write2Flash();
					Record.ReadDevUserSet();
					if(Port[Path0-1].UcList[Addr-1].Full == UserCode.Full){
						SaveOk =1;
						ShowSaveOk();
						LoadData(0);
					}else{
						SaveOk =0;
						ShowSaveNotOk();
					}
				}else{
					ShowCheckDataFail();
				}
				break;
			case VK_RETURN:
				CtlIndex =1;
				InitPanel();
				break;
		}
		if( (VK_EXECUTE != aKey) && (VK_RETURN != aKey) ){
			ClearOutText();
		}
	}else
	if(SetupMode == 3){
		//Fast
		switch(aKey){
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case VK_LEFT:
			case VK_RIGHT:
				if(CtlIndex <6){
					aPanel->Edit[CtlIndex].KeyIn(aKey);
					aPanel->Edit[CtlIndex].Show();
				}
				if(CtlIndex == 5){
					iVal = aPanel->Edit[CtlIndex].Str.ToInteger();
					if(iVal >255){
						AssignType=255;
						aPanel->Edit[CtlIndex].Str.FromUInt3Dg(AssignType);
					}else{
						AssignType = iVal;
					}
					aPanel->Edit[CtlIndex].Show();
					aPanel->SText[13].SetText(StrTypeShortName[AssignType], 24);
					aPanel->SText[13].Show();
				}
				break;
			case VK_DELETE:
				if(CtlIndex <6){
					aPanel->Edit[CtlIndex].KeyIn(aKey);
					aPanel->Edit[CtlIndex].Show();
				}
				if(CtlIndex == 5){
					iVal = aPanel->Edit[CtlIndex].Str.ToInteger();
					if(iVal >255){
						AssignType=255;
						aPanel->Edit[CtlIndex].Str.FromUInt3Dg(AssignType);
					}else{
						AssignType = iVal;
					}
					aPanel->Edit[CtlIndex].Show();
					aPanel->SText[13].SetText(StrTypeShortName[AssignType], 24);
					aPanel->SText[13].Show();
				}
				break;
			case VK_UP:
				if(CtlIndex >0){
					CtlIndex--;
					Check4SelectedInFast();
					aPanel->ReDrawItems();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <5){
					CtlIndex++;
					Check4SelectedInFast();
					aPanel->ReDrawItems();
				}
				break;
			case dfKEY_SETUP:
				if(CtlIndex == 5){
					FindType(-1);
				}
				break;
			case dfKEY_QUERY:
				if(CtlIndex == 5){
					FindType(1);
				}
				break;
			case VK_EXECUTE:
				if(PrepData2Write()){
					Write2Flash();
					Record.ReadDevUserSet();
					aUcFull = UserCode.Full;
					SaveOk =1;
					for(x=AddrStart-1; x<AddrEnd; x++){
						if(aUcFull != Port[Path0-1].UcList[x].Full){
							SaveOk = 0;
							break;
						}
						aUcFull += Pitch;
					}
					if(SaveOk){
						ShowSaveOk();
					}else{
						ShowSaveNotOk();
					}
				}else{
					ShowCheckDataFail();
				}
				break;
			case VK_RETURN:
				CtlIndex =2;
				InitPanel();
				break;
		}
		if( (VK_EXECUTE != aKey) && (VK_RETURN != aKey) ){
			ClearOutText();
		}	
	}else
	if(SetupMode ==4){
		switch(aKey){
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case VK_LEFT:
			case VK_RIGHT:
				Path0Old = DirectPadNum;
				AddrOld = NodeNum;
				if(CtlIndex <4){
					aPanel->Edit[CtlIndex].KeyIn(aKey);
					aPanel->Edit[CtlIndex].Show();
				}
				if(CtlIndex <2){
					CheckDirectPadAva();
					DirectPadDataShow();
				}else
				if(CtlIndex == 3){
					iVal = aPanel->Edit[3].Str.ToInteger();
					if(iVal < 256){
						AssignType = iVal;
					}else{
						AssignType = 255;
						aPanel->Edit[CtlIndex].Str.FromUInt3Dg(AssignType);
						aPanel->Edit[CtlIndex].Show();
					}
					aPanel->SText[13].SetText(StrTypeShortName[AssignType],24);
					aPanel->SText[13].Show();
				}
				break;
			case VK_DELETE:
				Path0Old = Path0;
				AddrOld = Addr;
				if(CtlIndex <4){
					aPanel->Edit[CtlIndex].KeyIn(aKey);
					aPanel->Edit[CtlIndex].Show();
				}
				if(CtlIndex <2){
					CheckDirectPadAva();
					DirectPadDataShow();
				}else
				if(CtlIndex == 3){
					iVal = aPanel->Edit[3].Str.ToInteger();
					if(iVal < 256){
						AssignType = iVal;
					}else{
						AssignType = 255;
						aPanel->Edit[CtlIndex].Str.FromUInt3Dg(AssignType);
						aPanel->Edit[CtlIndex].Show();
					}
					aPanel->SText[13].SetText(StrTypeShortName[AssignType],24);
					aPanel->SText[13].Show();
				}
				break;
			case VK_UP:
				if(CtlIndex >0){
					CtlIndex--;
					Check4SelectedInSetup();
					aPanel->ReDrawItems();
				}
				break;
			case VK_DOWN:
				if(CtlIndex < 1){
					CtlIndex++;
					Check4SelectedInSetup();
					aPanel->ReDrawItems();
				}else
				if(CtlIndex <3){
					if(DirectPadAva){
						CtlIndex++;
						Check4SelectedInSetup();
						aPanel->ReDrawItems();
					}
				}
				break;
			case dfKEY_SETUP:
				if(CtlIndex == 3){
					FindType(-1);
				}
				break;
			case dfKEY_QUERY:
				if(CtlIndex == 3){
					FindType(1);
				}
				break;
			case VK_EXECUTE:
				if(PrepData2Write()){
					WriteDirectPad2Flash();
					Record.ReadDirectPadUserSet();  
					if(DirectCtlBoard[DirectPadNum].UcList[NodeNum].Full == UserCode.Full){
						SaveOk =1;
						ShowSaveOk();
					}else{
						SaveOk =0;
						ShowSaveNotOk();
					}
				}else{
					ShowCheckDataFail();
				}
				break;
			case VK_RETURN:
				CtlIndex =3;
				InitPanel();
				break;
		}
		if( (VK_EXECUTE != aKey) && (VK_RETURN != aKey) ){
			ClearOutText();
		}
	}else{
		//Start to Selecte Setup Mode
		switch(aKey){
			case VK_UP:
				if(CtlIndex >0){
					CtlIndex--;
					Check4Selected();
					aPanel->ReDrawItems();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <3){
					CtlIndex++;
					Check4Selected();
					aPanel->ReDrawItems();
				}
				break;
			case VK_EXECUTE:
				if(CtlIndex == 0){
					SetupMode = 1;
					InitPanel4Setup();
				}else
				if(CtlIndex == 1){
					SetupMode = 2;
					InitPanel4Setup();
				}else
				if(CtlIndex == 2){
					SetupMode = 3;
					InitPanel4Fast();
				}else
				if(CtlIndex == 3){
					SetupMode = 4;
					InitPanel4DirectPad();
				}
				break;
			case VK_RETURN:
				SetupMode = 0;
				IsShowing = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}

	return aMsg;	
}

void TDeviceSetup::LoadStart()
{
	int aP0, aP1,i;
	unsigned int aAddr;
	IsFound = 0;
	if( (SetupMode == 1) || (SetupMode == 2) ){
		for(aP0=0; aP0<dPORT_MAX_COUNT; aP0++){
			if(MainCtl.fData.Split.ExistTablePort[aP0]){
				for(aP1=0; aP1<250; aP1++){
					if(Port[aP0].ExistTable[aP1]){
						IsFound =1;
						Path0 = aP0+1;
						Addr = aP1+1;
						UserCode.Full = Port[aP0].UcList[aP1].Full;
						AssignType = Port[aP0].AssignType[aP1];
						aPanel->Edit[0].Str.FromUInt2Dg(Path0);
						aPanel->Edit[1].Str.FromUInt3Dg(Addr);
						aPanel->Edit[2].Str.FromUInt8Dg(UserCode.Full);
						aPanel->Edit[3].Str.FromUInt3Dg(AssignType);
						aPanel->Edit[0].Show();
						aPanel->Edit[1].Show();
						aPanel->Edit[2].Show();
						aPanel->Edit[3].Show();
						TMyString::sFrom3Dg(Port[aP0].dTypeTable[aP1], aPanel->SText[11].Text);
						TMyString::sFromStr(StrTypeShortName[Port[aP0].dTypeTable[aP1]], aPanel->SText[12].Text);
						TMyString::sFromStr(StrTypeShortName[AssignType], aPanel->SText[13].Text);
						aPanel->SText[11].Show();
						aPanel->SText[12].Show();
						aPanel->SText[13].Show();
						
						aAddr = dAddrSdRam_Descp + (aP0 * 256 * 32) + (aP1 * 32);
						for(i=0; i<8; i++){
							ReadWriteData.D32[i] = *(volatile unsigned int *)(aAddr + (i *4));
						}
						for(i=0; i<32; i++){
							aPanel->SText[6].Text[i] = ReadWriteData.D8[i];
						}
						aPanel->SText[6].Text[32] = '\0';
						aPanel->SText[6].Show();
						break;
					}
				}
			}
			if(IsFound){
				break;
			}
		}
	}else
	if(SetupMode == 3){
		for(aP0=0; aP0<dPORT_MAX_COUNT; aP0++){
			if(MainCtl.fData.Split.ExistTablePort[aP0]){
				for(aP1=0; aP1<250; aP1++){
					if(Port[aP0].ExistTable[aP1]){
						IsFound =1;
						Path0 = aP0+1;
						AddrStart = aP1+1;
						AddrEnd = aP1+2;
						if(AddrEnd > 250)AddrEnd=250;
						UserCode.Full = 1010101;
						Pitch = 1;
						AssignType = 0;
						
						aPanel->Edit[0].Str.FromUInt2Dg(Path0);
						aPanel->Edit[1].Str.FromUInt3Dg(AddrStart);
						aPanel->Edit[2].Str.FromUInt3Dg(AddrStart);
						aPanel->Edit[3].Str.FromUInt8Dg(UserCode.Full);
						aPanel->Edit[4].Str.FromUInt4Dg(Pitch);
						aPanel->Edit[5].Str.FromUInt3Dg(AssignType);
						
						aPanel->Edit[0].Show();
						aPanel->Edit[1].Show();
						aPanel->Edit[2].Show();
						aPanel->Edit[3].Show();
						aPanel->Edit[4].Show();
						aPanel->Edit[5].Show();
						
						TMyString::sFromStr(StrTypeShortName[AssignType], aPanel->SText[13].Text);
						aPanel->SText[13].Show();
						break;
					}
				}
			}
			if(IsFound){
				break;
			}
		}
	}else
	if(SetupMode == 4){	
		DirectPadNum =0;
		NodeNum =0;
		DirectPadAva = 1;
		
		DirectPadDataShow();

	}
}

void TDeviceSetup::LoadNext()
{
	int aP0, aP1, i;
	int FoundHere;
	FoundHere = 0;
	if( (Path0>0) && (Path0<=dPORT_MAX_COUNT) && (Addr >0) && (Addr<=250) ){
		aP0 = Path0-1;
		aP1 = Addr -1;
		aP1++;
		if(aP1<=250){
			for(; aP1<250; aP1++){
				if(Port[aP0].ExistTable[aP1]){
					FoundHere = 1;
					if(UserCode.Full < 99999999)UserCode.Full++;
					IsFound =1;
					Path0 = aP0+1;
					Addr = aP1+1;
					aPanel->Edit[0].Str.FromUInt2Dg(Path0);
					aPanel->Edit[1].Str.FromUInt3Dg(Addr);
					aPanel->Edit[0].Show();
					aPanel->Edit[1].Show();
					break;
				}
			}
		}
		if(FoundHere == 0){
			for(i=0; i<dPORT_MAX_COUNT; i++){
				aP0++;
				if(aP0 >= dPORT_MAX_COUNT) aP0 = 0;
				if(MainCtl.fData.Split.ExistTablePort[aP0]){
					for(aP1=0; aP1<250; aP1++){
						if(Port[aP0].ExistTable[aP1]){
							FoundHere = 1;
							if(UserCode.Full < 99999999)UserCode.Full++;
							IsFound =1;
							Path0 = aP0+1;
							Addr = aP1+1;
							aPanel->Edit[0].Str.FromUInt2Dg(Path0);
							aPanel->Edit[1].Str.FromUInt3Dg(Addr);
							aPanel->Edit[0].Show();
							aPanel->Edit[1].Show();
							break;
						}
						if(FoundHere){
							break;
						}
					}
				}
				if(FoundHere){
					break;
				}
			}
		}
	}else{
		for(aP0=0; aP0<dPORT_MAX_COUNT; aP0++){
			if(MainCtl.fData.Split.ExistTablePort[aP0]){
				for(aP1=0; aP1<250; aP1++){
					if(Port[aP0].ExistTable[aP1]){
						FoundHere = 1;
						IsFound =1;
						Path0 = aP0+1;
						Addr = aP1+1;
						aPanel->Edit[0].Str.FromUInt2Dg(Path0);
						aPanel->Edit[1].Str.FromUInt3Dg(Addr);
						aPanel->Edit[0].Show();
						aPanel->Edit[1].Show();
						break;
					}
				}
			}
			if(FoundHere){
				break;
			}
		}
	}
}

int TDeviceSetup::PrepData2Write()
{
	int i;
	unsigned char aP0, aP1;
	if( (SetupMode == 1) || (SetupMode == 2) ){
		Path0 = aPanel->Edit[0].Str.ToInteger();
		Addr = aPanel->Edit[1].Str.ToInteger();
		UserCode.Full = aPanel->Edit[2].Str.ToInteger();
		AssignType = aPanel->Edit[3].Str.ToInteger();
		if(  (Path0>0) && (Path0 <= dPORT_MAX_COUNT) && (Addr >0) && (Addr <= 250) ){ 
			//if(  (UserCode.Full <= 99999999) && (AssignType >0) && (AssignType < 256)  ){
			if(  (UserCode.Full <= 99999999) && (AssignType < 256)  ){
				return 1;
			}
		}
	}else
	if(SetupMode == 3){
		Path0 = aPanel->Edit[0].Str.ToInteger();
		AddrStart = aPanel->Edit[1].Str.ToInteger();
		AddrEnd = aPanel->Edit[2].Str.ToInteger();
		UserCode.Full = aPanel->Edit[3].Str.ToInteger();
		Pitch = aPanel->Edit[4].Str.ToInteger();
		AssignType = aPanel->Edit[5].Str.ToInteger();
		if(  (Path0>0) && (Path0 <= dPORT_MAX_COUNT) && (AddrStart >0) && (AddrStart <= 250) && (AddrEnd >0) && (AddrEnd <= 250) ){ 
			//if(  (UserCode.Full <= 99999999) && (AssignType >0) && (AssignType < 256)  ){
			if(  (UserCode.Full <= 99999999) && (AssignType < 256)  ){
				return 1;
			}
		}
	}else
	if(SetupMode == 4){
		UserCode.Full = aPanel->Edit[2].Str.ToInteger();
		AssignType = aPanel->Edit[3].Str.ToInteger();
		if(  (DirectPadNum < dDIRECTCTL_MAX_COUNT) && (NodeNum < dDIRECTCTL_KEY_MAX_COUNT) ){ 
			//if(  (UserCode.Full <= 99999999) && (AssignType >0) && (AssignType < 256)  ){
			if(  (UserCode.Full <= 99999999)  && (AssignType < 256)  ){
				return 1;
			}
		}
	}
	return 0;
}

void TDeviceSetup::Write2Flash()
{
	unsigned int fAddr;
	unsigned int i;
	unsigned int aUcFull;
	volatile unsigned int x, GetSta;
	unsigned char aP0, aP1, wP1;
	
	do{
		DelayMs(3);
		GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
	}while(GetSta);
	
	//Erase 1 * 4K Blocks
	if( (Path0 >0) && (Path0 <= dPORT_MAX_COUNT) ){
		aP0 = Path0 -1;
		aP1 = Addr -1;
		fAddr = dFlashAddrDevUserCode + (aP0 * 4096);
		nor_erase_sector_4k(dFlashAddrDevUserCodeChip,fAddr);
		DelayMs(100);
		
		do{
			DelayMs(10);
			GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
		}while(GetSta);
		
		aUcFull = UserCode.Full;
		for(i=0; i<4; i++){
			for(x=0;x<64;x++){
				ReadWriteData.D32[x] = Port[aP0].UcList[i*64 + x].Full;
				if( (SetupMode == 1) || (SetupMode == 2) ){
					if( (i*64 + x) == (Addr -1))ReadWriteData.D32[x] = UserCode.Full;
				}else
				if(SetupMode == 3){
					wP1 = i*64 + x;
					if( (wP1>= (AddrStart -1)) && (wP1 <= (AddrEnd-1) )  ){
						ReadWriteData.D32[x] = aUcFull;
						aUcFull += Pitch;
					}
				}
			}
			fAddr = dFlashAddrDevUserCode + (aP0 * 4096) + (i * 256);
			nor_write_page(dFlashAddrDevUserCodeChip, fAddr, ReadWriteData.D8);
			do{
				DelayMs(5);
				GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
			}while(GetSta);
		}
		
		for(x=0;x<256;x++){
			ReadWriteData.D8[x] = Port[aP0].AssignType[x];
			if( (SetupMode == 1) || (SetupMode == 2) ){
				if(x == (Addr -1) )ReadWriteData.D8[x] = AssignType;
			}else
			if(SetupMode == 3){
				wP1 = AddrStart -1;
				if( (x >= wP1) && (x <= (AddrEnd-1)) ){
					ReadWriteData.D8[x] = AssignType;
				}
			}
		}
		fAddr = dFlashAddrDevUserCode + (aP0 * 4096) + (i * 256);
		nor_write_page(dFlashAddrDevUserCodeChip, fAddr, ReadWriteData.D8);
		do{
			DelayMs(5);
			GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
		}while(GetSta);
	}
}

void TDeviceSetup::WriteDirectPad2Flash()
{
	unsigned int fAddr;
	unsigned int i;
	volatile unsigned int x, GetSta;
	unsigned char aP0, aP1, wP1;
	
	do{
		DelayMs(3);
		GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
	}while(GetSta);

	//Erase 1 * 4K Blocks
	if( (DirectPadNum < dDIRECTCTL_MAX_COUNT) && (NodeNum < dDIRECTCTL_KEY_MAX_COUNT) ){
		aP0 = DirectPadNum;
		aP1 = NodeNum;
		fAddr = dFlashAddrDirectPadUserCode + (aP0 * 4096);
		nor_erase_sector_4k(dFlashAddrDevUserCodeChip,fAddr);
		DelayMs(100);
		
		do{
			DelayMs(10);
			GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
		}while(GetSta);
		
		for(x=0;x<8;x++){
			ReadWriteData.D32[x] = DirectCtlBoard[aP0].UcList[x].Full;
		}
		for(x=0;x<8;x++){
			ReadWriteData.D8[x +32] = DirectCtlBoard[aP0].AssignType[x];
		}
		ReadWriteData.D32[NodeNum] = UserCode.Full;
		ReadWriteData.D8[NodeNum + 32] = AssignType;
		
		fAddr = dFlashAddrDirectPadUserCode + (aP0 * 4096) + (0 * 256);
		nor_write_page(dFlashAddrDevUserCodeChip, fAddr, ReadWriteData.D8);
		do{
			DelayMs(5);
			GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
		}while(GetSta);
	}
}

void TDeviceSetup::ClearAll()
{
	unsigned int fAddr;
	volatile unsigned int x, GetSta;

	do{
		DelayMs(3);
		GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
	}while(GetSta);
	
	//Erase 1 * 4K Blocks
	for( x=0; x<dPORT_MAX_COUNT; x++ ){
		fAddr = dFlashAddrDevUserCode + (x * 4096);
		nor_erase_sector_4k(dFlashAddrDevUserCodeChip,fAddr);
		DelayMs(100);
		
		do{
			DelayMs(10);
			GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
		}while(GetSta);
	}	
}


void TDeviceSetup::Task1000Ms()
{
	if(IsShowing){

	}
}

void TDeviceSetup::Task10Ms()
{
	if(IsShowing){

	}
}

int TDeviceSetup::ExtRequst(unsigned char Prm)
{
	IsShowing =0;
	SetupMode =0;
	return 0;
}

