#include "RuntimeData.h"
#include "DisplayPadCfg.h"
#include "DevMask.h"

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

#define dfAddrLineNum 6
#define dfDescpLineNum 4
#define dfDescpMax	250

#define dAddr_DescpBuf	dAddrSdRam_Commom

const short AddrLeft[8] = {20,130,230,350,470,590,590,600};
const short AddrTop[8] = {88, 130,172,214,256,298,252,296};

void TDisplayPadCfg::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);
	Content.Set(Left, Top+CH, Right, Bottom);
	
	RectPanel.Set(Left + 420,Top + CH, Right - 4, Top + CH + 6 + 284);
	Color = aColor;
	#undef CH
	
	CtlIndex =11;
	Loaded.P0Found =0;
	Loaded.P1Found =0;
	Loaded.Inx =0;
	Loaded.P0 = dPORT_MAX_COUNT -1;
	
	Caption.Border.Color = clGray;
	
	Sta = 0;
	
}

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

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

void TDisplayPadCfg::RenderContent(void)
{
	#define FixColColor	clMedGray
	#define OddColor		clNearMoneyGreen
	unsigned int x,y, y2, clr;
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);	
}

void TDisplayPadCfg::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);
	
	RenderContent();
}

void TDisplayPadCfg::DrawCaption(void)
{	
	TMyString::sFromStr("通用设置->楼层显示设置",  Caption.Text);
	Caption.Show();
}

void TDisplayPadCfg::Show()
{
	DrawSelf();
	DrawCaption();
	TextRender_string24(80, 360 ,clNearBlack, "1.TAB键更改回路号,设置键上一个层显,查询键下一个层显");
	TextRender_string24(80, 400 ,clNearBlack, "2.上下左右数字键编辑地址段,按确定键保存");
}

void TDisplayPadCfg::ShowSta()
{
	if(Sta == 0){
		TextRender_string24(560, 92, clNearBlack, clFrmFace, "          ");
	}else
	if(Sta == 1){
		TextRender_string24(560, 92, clNearBlack, clFrmFace, "保存成功  ");
	}	
}

void TDisplayPadCfg::ShowSta(int aSta)
{
	Sta = aSta;
	ShowSta();

}

void TDisplayPadCfg::FullRedraw(int Prm)
{
	if(Prm == 0){
		Show();
		aPanel->Show();
		ShowSta();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		aPanel->Show();
		ShowSta();
		
		//if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}

void TDisplayPadCfg::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 TDisplayPadCfg::Check4Selected(void)
{
	int i;
	for(i=11; i<23; i++){
		aPanel->Edit[i].Selected = 0;
	}
	if(  (CtlIndex > 10) && (CtlIndex < 23)  ){
		aPanel->Edit[CtlIndex].Selected = 1;
	}
	
}

void TDisplayPadCfg::InitPanel(void)
{
	int x,y,x2,y2, aW,aH,i, yt;

	RectPanel.Set(80,80, 720, 350);
	
	x = RectPanel.Left;
	y = RectPanel.Top;
	x2 = RectPanel.Right;
	y2 = RectPanel.Bottom;
	aW = RectPanel.Width;
	aH = RectPanel.Height;
	
	yt = y+6;
	
	aPanel->Init(x,y,aW,aH,0,0,clFrmFace, bvRaised);
	
	aPanel->Caption.Init(1,1,aW-3,32,x,y,clFrmFace,clFrmFace);

	aPanel->SText[0].Init(20,0,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(AddrLeft[0]+6,AddrTop[1],70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(AddrLeft[0]+6,AddrTop[2],70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[3].Init(AddrLeft[0]+6,AddrTop[3],70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[4].Init(AddrLeft[1]+6,88,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[5].Init(AddrLeft[1]+122,88,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[6].Init(AddrLeft[1]+236,88,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[7].Init(AddrLeft[1]+356,88,70,30,x,yt,clNearBlack,clFrmFace);

	TMyString::sFromStr("回路 层显地址  描述字", aPanel->SText[0].Text);
	TMyString::sFromStr("匹配段1", aPanel->SText[1].Text);
	TMyString::sFromStr("匹配段2", aPanel->SText[2].Text);
	TMyString::sFromStr("匹配段3", 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);
	for(i=0; i<8;i++){
		aPanel->SText[i].Enable = 1;
		aPanel->SText[i].Visible = 1;
	}
	
	aPanel->Edit[0].Init(26 ,36 + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[1].Init(104,36 + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[2].Init(200,36 + 1,390,30,x,yt,clNearBlack,clNearWhite);
	
	for(i=0; i<2;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(3);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.FromStr("001");
		aPanel->Edit[i].Pst = 2;
		aPanel->Edit[i].Selected = 0;
	}
	aPanel->Edit[2].Enable = 0;
	aPanel->Edit[2].Visible = 1;
	aPanel->Edit[2].SetMaxLen(34);
	aPanel->Edit[2].Str.FromStr("未加载");
	
	aPanel->Edit[11].Init(AddrLeft[1] + 26,AddrTop[1] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[12].Init(AddrLeft[2] + 32,AddrTop[1] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[13].Init(AddrLeft[3] + 36,AddrTop[1] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[14].Init(AddrLeft[4] + 32,AddrTop[1] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	
	aPanel->Edit[15].Init(AddrLeft[1] + 26,AddrTop[2] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[16].Init(AddrLeft[2] + 32,AddrTop[2] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[17].Init(AddrLeft[3] + 36,AddrTop[2] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[18].Init(AddrLeft[4] + 32,AddrTop[2] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	
	aPanel->Edit[19].Init(AddrLeft[1] + 26,AddrTop[3] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[20].Init(AddrLeft[2] + 32,AddrTop[3] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[21].Init(AddrLeft[3] + 36,AddrTop[3] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[22].Init(AddrLeft[4] + 32,AddrTop[3] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	
	for(i=11; i<23;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].SetMaxLen(3);
		aPanel->Edit[i].Str.FromStr("000");
		aPanel->Edit[i].Pst = 2;
		aPanel->Edit[i].Selected = 0;
	}
	
	aPanel->Edit[11].SetMaxLen(2);
	aPanel->Edit[11].Str.FromStr("00");
	aPanel->Edit[11].Pst = 1;
	aPanel->Edit[12].SetMaxLen(2);
	aPanel->Edit[12].Str.FromStr("00");
	aPanel->Edit[12].Pst = 1;
	aPanel->Edit[15].SetMaxLen(2);
	aPanel->Edit[15].Str.FromStr("00");
	aPanel->Edit[15].Pst = 1;
	aPanel->Edit[16].SetMaxLen(2);
	aPanel->Edit[16].Str.FromStr("00");
	aPanel->Edit[16].Pst = 1;
	aPanel->Edit[19].SetMaxLen(2);
	aPanel->Edit[19].Str.FromStr("00");
	aPanel->Edit[19].Pst = 1;
	aPanel->Edit[20].SetMaxLen(2);
	aPanel->Edit[20].Str.FromStr("00");
	aPanel->Edit[20].Pst = 1;

	aPanel->Caption.Visible = 1;
	
	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
	
	x = RectPanel.Left;
	y = RectPanel.Top;
	for(i=0; i<6; i++){
		VertLineRender(AddrLeft[i] + 80,	y + AddrTop[0], 		166,	clGray);
	}
	for(i=0; i<5; i++){
		HorizLineRender(AddrLeft[0]  + 80,	y + AddrTop[i],		570,	clGray);
	}
	
	FindNextPadViaCircuit();
}

void TDisplayPadCfg::DrawItem()
{
	unsigned int fAddr, iP0, iP1;
	if(Loaded.P0Found){
		aPanel->Edit[0].Str.FromUInt2Dg(Loaded.P0);
		if(Loaded.P1Found){
			iP0 = Loaded.P0 -1;
			iP1 = Loaded.P1 -1;
			fAddr = dAddrSdRam_Descp + (iP0 * 8192) + (iP1 * 32);
			aPanel->Edit[1].Str.FromUInt3Dg(Loaded.P1);
			ReadWriteData.D32[0] = *(volatile unsigned int *)(fAddr + 0);
			ReadWriteData.D32[1] = *(volatile unsigned int *)(fAddr + 4);
			ReadWriteData.D32[2] = *(volatile unsigned int *)(fAddr + 8);
			ReadWriteData.D32[3] = *(volatile unsigned int *)(fAddr + 12);
			ReadWriteData.D32[4] = *(volatile unsigned int *)(fAddr + 16);
			ReadWriteData.D32[5] = *(volatile unsigned int *)(fAddr + 20);
			ReadWriteData.D32[6] = *(volatile unsigned int *)(fAddr + 24);
			ReadWriteData.D32[7] = *(volatile unsigned int *)(fAddr + 28);
			ReadWriteData.D32[8] = 0;
			if(ReadWriteData.D8[0] == '\0'){
				aPanel->Edit[2].Str.FromStr("未定义描述字");
			}else{
				aPanel->Edit[2].Str.FromStr(ReadWriteData.D8);
			}
			
			LoadData();
		}
	}else{
		aPanel->Edit[0].Str.FromStr("xx");
		aPanel->Edit[1].Str.FromStr("xxx");
	}
	
	aPanel->Edit[0].Show();
	aPanel->Edit[1].Show();
	aPanel->Edit[2].Show();
}

int TDisplayPadCfg::FindNextPadViaCircuit()
{
	int i,x;
	unsigned char aP0 = Loaded.P0;
	unsigned char InxCount;
	unsigned int aAddr;
	Loaded.P0Found = 0;
	aP0--;
	if(aP0 >= dPORT_MAX_COUNT)aP0 = 0; 
	for(i=0; i<dPORT_MAX_COUNT; i++){
		aP0++;
		if(aP0 >= dPORT_MAX_COUNT)aP0 = 0;
		if(Port[aP0].DisplayPadCount >0){
			InxCount = Port[aP0].DisplayPadCount;
			if(InxCount > dDISPLAYPAD_MAX_COUNT){
				InxCount = dDISPLAYPAD_MAX_COUNT;
			}
			Loaded.P0 = aP0+1;
			Loaded.P0Found = 1;
			break;
		}
	}
	if(Loaded.P0Found){
		Loaded.Inx = 0;
		Loaded.P1 = Port[aP0].DisplayPadAddr[Loaded.Inx];
		Loaded.P1Found = 1;
	}
	
	aP0 = Loaded.P0Found -1;
	if(Loaded.P0Found && Loaded.P1Found){
		if(aP0 <= dPORT_MAX_COUNT){
			aAddr = dAddrSdRam_Descp + (aP0 * 256 * 32) + ((Loaded.P1-1) * 32);
			for(i=0; i<32; i++){
				aPanel->Edit[2].Str.Text[i] = *(volatile unsigned char *)(aAddr + i);
			}
			aPanel->Edit[2].Str.Text[32] = '\0';
		}else{
			aPanel->Edit[2].Str.Clear();
		}
	}else{
		aPanel->Edit[2].Str.Clear();
	}
	
	DrawItem();
	return 0;
}

int TDisplayPadCfg::FindPrvPadViaAddr()
{
	int i,x,Inx;
	unsigned char InxCount;
	unsigned char aP0 = Loaded.P0;
	unsigned int aAddr;
	aP0--;
	if(aP0 < dPORT_MAX_COUNT){
		if(Loaded.P0Found){
			Inx = Loaded.Inx;
			InxCount = Port[aP0].DisplayPadCount;
			if(InxCount > dDISPLAYPAD_MAX_COUNT){
				InxCount = dDISPLAYPAD_MAX_COUNT;
			}
			for(x=0; x<InxCount; x++){
				Inx--;
				if(Inx < 0 )Inx = dDISPLAYPAD_MAX_COUNT -1;
				if(Port[aP0].DisplayPadAddr[Inx]){
					Loaded.Inx = Inx;
					Loaded.P1 = Port[aP0].DisplayPadAddr[Inx];
					Loaded.P1Found = 1;
					break;
				}
			}
		}
	}
	
	aP0 = Loaded.P0Found -1;
	if(Loaded.P0Found && Loaded.P1Found){
		if(aP0 <= dPORT_MAX_COUNT){
			aAddr = dAddrSdRam_Descp + (aP0 * 256 * 32) + ((Loaded.P1-1) * 32);
			for(i=0; i<32; i++){
				aPanel->Edit[2].Str.Text[i] = *(volatile unsigned char *)(aAddr + i);
			}
			aPanel->Edit[2].Str.Text[32] = '\0';
		}else{
			aPanel->Edit[2].Str.Clear();
		}
	}else{
		aPanel->Edit[2].Str.Clear();
	}
	
	DrawItem();
	return 0;
}

int TDisplayPadCfg::FindNextPadViaAddr()
{
	int i,x,Inx;
	unsigned char InxCount;
	unsigned char aP0 = Loaded.P0;
	unsigned int aAddr;
	aP0--;
	Loaded.P1Found = 0;
	if(aP0 < dPORT_MAX_COUNT){
		if(Loaded.P0Found){
			Inx = Loaded.Inx;
			InxCount = Port[aP0].DisplayPadCount;
			if(InxCount > dDISPLAYPAD_MAX_COUNT){
				InxCount = dDISPLAYPAD_MAX_COUNT;
			}
			for(x=0; x<InxCount; x++){
				Inx++;
				if(Inx >= InxCount)Inx = 0;
				if(Port[aP0].DisplayPadAddr[Inx]){
					Loaded.Inx = Inx;
					Loaded.P1 = Port[aP0].DisplayPadAddr[Inx];
					Loaded.P1Found = 1;
					break;
				}
			}
		}
	}
	
	aP0 = Loaded.P0Found -1;
	if(Loaded.P0Found && Loaded.P1Found){
		if(aP0 <= dPORT_MAX_COUNT){
			aAddr = dAddrSdRam_Descp + (aP0 * 256 * 32) + ((Loaded.P1-1) * 32);
			for(i=0; i<32; i++){
				aPanel->Edit[2].Str.Text[i] = *(volatile unsigned char *)(aAddr + i);
			}
			aPanel->Edit[2].Str.Text[32] = '\0';
		}else{
			aPanel->Edit[2].Str.Clear();
		}
	}else{
		aPanel->Edit[2].Str.Clear();
	}
	
	DrawItem();
	return 0;
}

void TDisplayPadCfg::LoadData()
{
	int i;
	unsigned char aP0 = Loaded.P0;
	aP0--;
	if(aP0 < dPORT_MAX_COUNT){
		//AddrAssign[0].CtlNum = DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[0].CtlNum;
		//AddrAssign[0].Path0 = DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[0].P0;
		//AddrAssign[0].BeginAddr = DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[0].BeginAddr;
		//AddrAssign[0].EndAddr = DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[0].EndAddr;
		//AddrAssign[1].CtlNum = DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[1].CtlNum;
		//AddrAssign[1].Path0 = DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[1].P0;
		//AddrAssign[1].BeginAddr = DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[1].BeginAddr;
		//AddrAssign[1].EndAddr = DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[1].EndAddr;
		//AddrAssign[2].CtlNum = DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[2].CtlNum;
		//AddrAssign[2].Path0 = DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[2].P0;
		//AddrAssign[2].BeginAddr = DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[2].BeginAddr;
		//AddrAssign[2].EndAddr = DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[2].EndAddr;
		
		aPanel->Edit[11].Str.FromUInt2Dg(AddrAssign[0].CtlNum);
		aPanel->Edit[12].Str.FromUInt2Dg(AddrAssign[0].Path0);
		aPanel->Edit[13].Str.FromUInt3Dg(AddrAssign[0].BeginAddr);
		aPanel->Edit[14].Str.FromUInt3Dg(AddrAssign[0].EndAddr);
		
		aPanel->Edit[15].Str.FromUInt2Dg(AddrAssign[1].CtlNum);
		aPanel->Edit[16].Str.FromUInt2Dg(AddrAssign[1].Path0);
		aPanel->Edit[17].Str.FromUInt3Dg(AddrAssign[1].BeginAddr);
		aPanel->Edit[18].Str.FromUInt3Dg(AddrAssign[1].EndAddr);
		
		aPanel->Edit[19].Str.FromUInt2Dg(AddrAssign[2].CtlNum);
		aPanel->Edit[20].Str.FromUInt2Dg(AddrAssign[2].Path0);
		aPanel->Edit[21].Str.FromUInt3Dg(AddrAssign[2].BeginAddr);
		aPanel->Edit[22].Str.FromUInt3Dg(AddrAssign[2].EndAddr);
		
		for(i=11; i<23; i++){
			aPanel->Edit[i].Show();
		}
	}
}

void TDisplayPadCfg::SaveData()
{
	unsigned char aP0;
	aP0 = Loaded.P0;
	aP0--;
	if(aP0 < dPORT_MAX_COUNT){
		AddrAssign[0].CtlNum = aPanel->Edit[11].Str.ToInteger();
		AddrAssign[0].Path0 = aPanel->Edit[12].Str.ToInteger();
		AddrAssign[0].BeginAddr = aPanel->Edit[13].Str.ToInteger();
		AddrAssign[0].EndAddr = aPanel->Edit[14].Str.ToInteger();
		AddrAssign[1].CtlNum = aPanel->Edit[15].Str.ToInteger();
		AddrAssign[1].Path0 = aPanel->Edit[16].Str.ToInteger();
		AddrAssign[1].BeginAddr = aPanel->Edit[17].Str.ToInteger();
		AddrAssign[1].EndAddr = aPanel->Edit[18].Str.ToInteger();
		AddrAssign[2].CtlNum = aPanel->Edit[19].Str.ToInteger();
		AddrAssign[2].Path0 = aPanel->Edit[20].Str.ToInteger();
		AddrAssign[2].BeginAddr = aPanel->Edit[21].Str.ToInteger();
		AddrAssign[2].EndAddr = aPanel->Edit[22].Str.ToInteger();
		
		//DisplayPadRt.Entry[Loaded.P0-1][Loaded.Inx].Spl.UsingPath[0].CtlNum	= AddrAssign[0].CtlNum;
		//DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[0].P0			= AddrAssign[0].Path0;		
		//DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[0].BeginAddr		= AddrAssign[0].BeginAddr;
		//DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[0].EndAddr		= AddrAssign[0].EndAddr;
		//DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[1].CtlNum			= AddrAssign[1].CtlNum;
		//DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[1].P0			= AddrAssign[1].Path0;
		//DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[1].BeginAddr		= AddrAssign[1].BeginAddr;
		//DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[1].EndAddr		= AddrAssign[1].EndAddr;
		//DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[2].CtlNum			= AddrAssign[2].CtlNum;
		//DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[2].P0			= AddrAssign[2].Path0;
		//DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[2].BeginAddr		= AddrAssign[2].BeginAddr;
		//DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.UsingPath[2].EndAddr		= AddrAssign[2].EndAddr;
		
		//DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.ThisP0 = Loaded.P0;
		//DisplayPadRt.Entry[aP0][Loaded.Inx].Spl.ThisP1 = Loaded.P1;
	}
}

TGuiMsgReturn TDisplayPadCfg::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	int uKey;
	unsigned char aIndex;
	char LastChr;
	const char *p;
	int i;
	switch(aKey){
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
		case VK_DELETE:
		case VK_LEFT:
		case VK_RIGHT:
			uKey = 0;
			if(VK_LEFT == aKey){
				switch(CtlIndex){
					case 12:
					case 13:
					case 14:
					case 16:
					case 17:
					case 18:
					case 20:
					case 21:
					case 22:
						if(aPanel->Edit[CtlIndex].Pst < 0){
							CtlIndex--;
							Check4Selected();
							aPanel->ReDrawItems();
							uKey = 1;
						}
						break;
				}
			}
			if(VK_RIGHT == aKey){
				switch(CtlIndex){
					case 11:
					case 12:
					case 15:
					case 16:
					case 19:
					case 20:
						if(aPanel->Edit[CtlIndex].Pst > 0){
							CtlIndex++;
							Check4Selected();
							aPanel->ReDrawItems();
							uKey = 1;
						}
						break;
					case 13:
					case 17:
					case 21:
						if(aPanel->Edit[CtlIndex].Pst > 1){
							CtlIndex++;
							Check4Selected();
							aPanel->ReDrawItems();
							uKey = 1;
						}
						break;
				}
			}
			if(uKey == 0){
				for(i=11; i<23; i++){
					if(aPanel->Edit[i].Selected){
						aPanel->Edit[i].KeyIn(aKey);
						aPanel->Edit[i].CorrectPst();
						aPanel->Edit[i].Show();
					}
				}
			}
			break;
		case VK_UP:
			if(CtlIndex > 14){
				CtlIndex -= 4;
				Check4Selected();
				aPanel->ReDrawItems();
			}
			break;
		case VK_DOWN:
			if(CtlIndex < 19){
				CtlIndex += 4;
				Check4Selected();
				aPanel->ReDrawItems();
			}
			break;
		case VK_TAB:
			FindNextPadViaCircuit();
			break;
		case dfKEY_SETUP:
			FindPrvPadViaAddr();
			break;
		case dfKEY_QUERY:
			FindNextPadViaAddr();
			break;
		case VK_EXECUTE:
			if(Loaded.P1Found){
				SaveData();
				Write2Flash();
				Record.ReadDisplayPadSet();
				LoadData();
				ShowSta(1);
			}
			break;
	}
	if(aKey == VK_RETURN){
		aMsg = guiMsgReturn;

	}
	
	if(aKey != VK_EXECUTE){
		ShowSta(0);
	}
	
	return aMsg;	
}


void TDisplayPadCfg::Write2Flash()
{
	unsigned int fAddr;
	unsigned int i;
	unsigned int Piece4K;
	volatile unsigned int x, wP0, GetSta;
	unsigned char aP0;
	
	if(Loaded.P0Found && Loaded.P1Found){
		
	}else{
		return;
	}
	
	do{
		DelayMs(2);
		GetSta = nor_get_busy_state(dFlashDisplayPadSetChip);
	}while(GetSta);
	
	//Erase 1 * 4K Blocks
	aP0 = Loaded.P0;
	aP0--;
	if(aP0 < dPORT_MAX_COUNT){
		wP0 = aP0 & 0xF0;
		Piece4K = aP0 / 16;
		fAddr = dFlashDisplayPadSet + (Piece4K * 4096);
		nor_erase_sector_4k(dFlashDisplayPadSetChip,fAddr);
		DelayMs(100);
		do{
			DelayMs(10);
			GetSta = nor_get_busy_state(dFlashDisplayPadSetChip);
		}while(GetSta);
		
		for(i=0; i<16; i++){
			for(x=0;x<16;x++){
				//ReadWriteData.D32[x*4 + 0] = DisplayPadRt.Entry[wP0 +i][x].D32[0];
				//ReadWriteData.D32[x*4 + 1] = DisplayPadRt.Entry[wP0 +i][x].D32[1];
				//ReadWriteData.D32[x*4 + 2] = DisplayPadRt.Entry[wP0 +i][x].D32[2];
				//ReadWriteData.D32[x*4 + 3] = DisplayPadRt.Entry[wP0 +i][x].D32[3];
			}
			fAddr = dFlashDisplayPadSet + (Piece4K * 4096) + (i * 256);
			nor_write_page(dFlashDisplayPadSetChip, fAddr, ReadWriteData.D8);
			do{
				DelayMs(5);
				GetSta = nor_get_busy_state(dFlashDisplayPadSetChip);
			}while(GetSta);
		}
	}
}
































