#include "RuntimeData.h"
#include "FloorDisplay.h"
#include "DevMask.h"

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

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

#define dAddr_DescpBuf	dAddrSdRam_Commom

const short AddrLeft[8] = {10,60,170,420,480,788,788,788};
const short AddrTop[8] = {42,84,126,168,210,252,296,338};
const short AddrTextLeft[8] = {12,62,172,422,482,790,790,790};
const short AddrTextTop[8] = {50,92,134,176,218,260,304,346};

const short DescpLeft[4] = {10,120,788,420};
const short DescpTop[8] = {42,84,126,168,210,252,296,338};
const short DescpTextLeft[4] = {12,122,172,422};
const short DescpTextTop[8] = {50,92,134,176,218,260,304,346};

void TFloorDisplay::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(Left+450,Top,340, 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 =0;
	SubCtlIndex =0;
	IsUsing = 0;
	WhatSet = 0;
	
	Caption.Border.Color = clGray;
	
	Lv.DevCount = 0;
	Lv.SelectedLineNum = 0;
	Lv.SelectedIndex = 0;
	Lv.TopIndex = 0;
}

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

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

void TFloorDisplay::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 TFloorDisplay::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 TFloorDisplay::DrawCaption(void)
{	
	TMyString::sFromStr("通用设置->楼层显示",  Caption.Text);
	Caption.Show();
}

void TFloorDisplay::DrawCaption2(void)
{
	TMyString::sFromStr("                           ", SText.Text);
	SText.Show();
}

void TFloorDisplay::DrawCaption24Addr(void)
{	
	//TMyString::sFromStr("按TAB键切换至保存或重新读取", SText.Text);
	//SText.Show();
}

void TFloorDisplay::DrawCaption24Descp(void)
{
	//TMyString::sFromStr("按TAB键切换至保存或重新读取", SText.Text);
	//SText.Show();
}

void TFloorDisplay::Show()
{
	DrawSelf();
	DrawCaption();
	TextRender_string24(250, 300 ,clNearBlack, "1.上下键切换选择项目; ");
	TextRender_string24(250, 340 ,clNearBlack, "2.确定键进入");
}

void FullRedraw()
{
	
}

void TFloorDisplay::Check4Selected(void)
{
	aPanel->Btn[0].Selected = 0;
	aPanel->Btn[1].Selected = 0;
	aPanel->Btn[2].Selected = 0;
	switch(CtlIndex){
		case 0:
			aPanel->Btn[0].Selected = 1;
			break;
		case 1:
			aPanel->Btn[1].Selected = 1;
			break;
		case 2:
			aPanel->Btn[2].Selected = 1;
			break;
		default:
			CtlIndex = 0;
			aPanel->Btn[0].Selected = 1;
			break;
	}		
}

void TFloorDisplay::Check4Selected4Global(void)
{
	aPanel->Edit[0].Selected = 0;
	aPanel->Btn[0].Selected = 0;
	aPanel->Btn[1].Selected = 0;
	switch(CtlIndex){
		case 0:
			aPanel->Edit[0].Selected = 1;
			break;
		case 1:
			aPanel->Btn[0].Selected = 1;
			break;
		case 2:
			aPanel->Btn[1].Selected = 1;
			break;
		default:
			CtlIndex = 0;
			aPanel->Edit[0].Selected = 1;
			break;
	}		
}

void TFloorDisplay::InitPanel(void)
{
	int x,y,x2,y2, aW,aH,i, yt;
	
	RectPanel.Set(250,150, 550, 290);
	
	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->Btn[0].Init(40,5,220,34,x,yt,clNearBlack,clNearWhite);
	aPanel->Btn[1].Init(40,45,220,34,x,yt,clNearBlack,clNearWhite);
	aPanel->Btn[2].Init(40,85,220,34,x,yt,clNearBlack,clNearWhite);
	aPanel->Btn[0].Caption.FromStr("  全局参数设置  ");
	aPanel->Btn[1].Caption.FromStr("层显设备地址分配");
	aPanel->Btn[2].Caption.FromStr("层(区)描述字设置");
	for(i=0; i<3;i++){
		aPanel->Btn[i].Enable = 1;
		aPanel->Btn[i].Visible = 1;
		aPanel->Btn[i].SetCaptionLeftSpacing(10);
		aPanel->Btn[i].SelectedColor = clBlue;
	}

	aPanel->Caption.Visible = 1;
	
	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
}


void TFloorDisplay::InitPanel4Global(void)
{
	int x,y,x2,y2, aW,aH,i, yt;
	
	RectPanel.Set(250,150, 550, 290);
	
	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->Edit[0].Init(50,5,200,34,x,yt,clNearBlack,clNearWhite);
	for(i=0; i<1;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
	}
	aPanel->Edit[0].SetMaxLen(15);
	aPanel->Edit[0].SetInputMaskEn();
	aPanel->Edit[0].Str.FromStr("----未知详情----");
	aPanel->Edit[0].Pst = 1;
	aPanel->Edit[0].IsShowCursor = 0;

	aPanel->Btn[0].Init(40,45,220,34,x,yt,clNearBlack,clNearWhite);
	aPanel->Btn[1].Init(40,85,220,34,x,yt,clNearBlack,clNearWhite);
	aPanel->Btn[0].Caption.FromStr("    读取参数    ");
	aPanel->Btn[1].Caption.FromStr("    保存参数    ");
	for(i=0; i<2;i++){
		aPanel->Btn[i].Enable = 1;
		aPanel->Btn[i].Visible = 1;
		aPanel->Btn[i].SetCaptionLeftSpacing(10);
		aPanel->Btn[i].SelectedColor = clBlue;
	}

	aPanel->Caption.Visible = 1;
	
	aPanel->Show();
	Check4Selected4Global();
	aPanel->ReDrawItems();
}


void TFloorDisplay::InitPanel4SetAddr(void)
{
	int x,y,x2,y2, aW,aH,i, yt;
	
	RectPanel.Set(6,76, 794, 434);
	
	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(0,0,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(416,0,70,30,x,yt,clNearBlack,clFrmFace);

	TMyString::sFromStr("序号 设备列表      设备描述", aPanel->SText[0].Text);
	TMyString::sFromStr("显址         楼层描述", aPanel->SText[1].Text);
	for(i=0; i<2;i++){
		aPanel->SText[i].Enable = 1;
		aPanel->SText[i].Visible = 1;
	}
	
	aPanel->Edit[0].Init(AddrLeft[3] + 1,AddrTop[0] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[1].Init(AddrLeft[3] + 1,AddrTop[1] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[2].Init(AddrLeft[3] + 1,AddrTop[2] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[3].Init(AddrLeft[3] + 1,AddrTop[3] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[4].Init(AddrLeft[3] + 1,AddrTop[4] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[5].Init(AddrLeft[3] + 1,AddrTop[5] + 1,48,30,x,yt,clNearBlack,clNearWhite);
	
	for(i=0; i<6;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->Btn[0].Init(420,300,168,34,x,yt,clNearBlack,clNearWhite);
	aPanel->Btn[1].Init(600,300,168,34,x,yt,clNearBlack,clNearWhite);
	aPanel->Btn[0].Caption.FromStr("重新读取设定");
	aPanel->Btn[1].Caption.FromStr("保存所有设定");
	for(i=0; i<2;i++){
		aPanel->Btn[i].Selected = 0;
		aPanel->Btn[i].Enable = 1;
		aPanel->Btn[i].Visible = 1;
		aPanel->Btn[i].SetCaptionLeftSpacing(10);
		aPanel->Btn[i].SelectedColor = clBlue;
	}

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

void TFloorDisplay::DescpBufInit()
{
	int i,x;
	unsigned int aAddr;
	aAddr = dAddr_DescpBuf;
	for(i=0; i<256; i++){
		for(x=0; x<32; x++){
			*(volatile unsigned char *)(aAddr + (i*32) + x) = 0;
		}
	}
}

void TFloorDisplay::LoadDescpFromSys()
{
	ReadFromFlash();
}

void TFloorDisplay::InitPanel4SetDescp(void)
{
	int x,y,x2,y2, aW,aH,i, yt;
	
	RectPanel.Set(6,76, 794, 336);
	
	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(6,0,70,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(12,264,700,30,x,yt,clNearBlack,Color);
	aPanel->SText[2].Init(12,290,700,30,x,yt,clNearBlack,Color);

	TMyString::sFromStr("楼层区域          楼层区域描述", aPanel->SText[0].Text);
	TMyString::sFromStr("1.中/英健切换输入法,上下键切换选中条目", aPanel->SText[1].Text);
	TMyString::sFromStr("2.TAB键在编辑与读取保存按钮之间切换切换", aPanel->SText[2].Text);
	
	aPanel->Edit[0].Init(DescpLeft[1] + 2,DescpTop[0],400,32,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[1].Init(DescpLeft[1] + 2,DescpTop[1],400,32,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[2].Init(DescpLeft[1] + 2,DescpTop[2],400,33,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[3].Init(DescpLeft[1] + 2,DescpTop[3],400,34,x,yt,clNearBlack,clNearWhite);
	
	for(i=0; i<4;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(32);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.FromStr("001");
		aPanel->Edit[i].Pst = 2;
		aPanel->Edit[i].Selected = 0;
	}
	
	aPanel->SText[11].Init(176,212,70,30,x,yt,clNearBlack,clFrmFace);
	TMyString::sFromStr("跳到此索引号:", aPanel->SText[11].Text);
	aPanel->Edit[11].Init(340,212,60,34,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[11].Enable = 0;
	aPanel->Edit[11].Visible = 1;
	aPanel->Edit[11].SetMaxLen(3);
	aPanel->Edit[11].SetInputMaskEn();
	aPanel->Edit[11].Str.FromStr("001");
	aPanel->Edit[11].Pst = 2;
	aPanel->Edit[11].Selected = 0;
	
	aPanel->Btn[0].Init(420,212,168,34,x,yt,clNearBlack,clNearWhite);
	aPanel->Btn[1].Init(600,212,168,34,x,yt,clNearBlack,clNearWhite);
	aPanel->Btn[0].Caption.FromStr("重新读取设定");
	aPanel->Btn[1].Caption.FromStr("保存所有设定");
	for(i=0; i<2;i++){
		aPanel->Btn[i].Selected = 0;
		aPanel->Btn[i].Enable = 1;
		aPanel->Btn[i].Visible = 1;
		aPanel->Btn[i].SetCaptionLeftSpacing(10);
		aPanel->Btn[i].SelectedColor = clBlue;
	}

	aPanel->Caption.Visible = 1;
	
	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
	
	x = RectPanel.Left;
	y = RectPanel.Top;
	for(i=0; i<3; i++){
		VertLineRender(DescpLeft[i],	y + DescpTop[0], 		170,	clGray);
	}
	for(i=0; i<5; i++){
		HorizLineRender(DescpLeft[0],	y + DescpTop[i],		776,	clGray);
	}
}

void TFloorDisplay::LoadDev()
{
	int i,a, inx;
	inx = 0;
	Lv.DevCount = 0;
	for(i=0; i<256; i++){
		AddrAssign[i].Path0 = 0;
		AddrAssign[i].Path1 = 0;
		AddrAssign[i].aAssign = 0;
	}
	for(i=0; i<dPORT_COUNT_USING; i++){
		for(a=0; a<dEP_MAX_COUNT_PER_PORT; a++){
			if(EpDev[i][a].fData.Split.dType == dgTYPE_DISPLAY_PAD){
				if(inx<256){
					AddrAssign[inx].Path0 = i;
					AddrAssign[inx].Path1 = a;
					inx++;
				}
				Lv.DevCount++;
			}
		}
	}
	Lv.TopIndex = 0;
	if(Lv.DevCount){
		Lv.SelectedIndex = 0;
		Lv.SelectedLineNum = 0;
	}else{
		Lv.SelectedIndex = -1;
		Lv.SelectedLineNum = -1;
	}
}

void TFloorDisplay::SetEditAddrStart()
{
	unsigned char aP0, aP1;
	short inx;
	if( Lv.SelectedLineNum> -1) {
		inx = Lv.SelectedLineNum + Lv.TopIndex;
		if(inx < Lv.DevCount){
			if(Lv.SelectedLineNum < 10){
				RestoreIndex = inx;
				RestoreEditIndex = Lv.SelectedLineNum;
				aPanel->Edit[Lv.SelectedLineNum].Str.FromUInt3Dg(AddrAssign[inx].aAssign);
				aPanel->Edit[Lv.SelectedLineNum].Selected = 1;
				aPanel->Edit[Lv.SelectedLineNum].Show();
			}
		}
	}
}

void TFloorDisplay::RestoreEditAddr()
{
	int eIndex;
	if(RestoreIndex < Lv.DevCount){
		AddrAssign[RestoreIndex].aAssign = aPanel->Edit[RestoreEditIndex].Str.ToInteger();
	}
}

void TFloorDisplay::DrawAddrList()
{
	int i, inx, x, y;
	inx = Lv.TopIndex;
	for(i=0; i<6; i++){
		if(inx < Lv.DevCount){
			y = RectPanel.Top + AddrTextTop[i]; 
			TMyString::sFrom3Dg(Lv.TopIndex +i + 1, Text.Body);
			TextRender_string24(AddrTextLeft[0], y ,clNearBlack, clFrmFace, Text.Body);
			TMyString::sClear(Text.Body);
			TMyString::sAddOn2Dg(AddrAssign[inx].Path0, Text.Body);
			TMyString::sAddOnStr("-", Text.Body);
			TMyString::sAddOn3Dg(AddrAssign[inx].Path1, Text.Body);
			TextRender_string24(AddrTextLeft[1] +3, y ,clNearBlack, clFrmFace, Text.Body);
			//TMyString::sFillFixLen(Text.Body, 20);
			//TextRender_string24(AddrTextLeft[2], AddrTextTop[i] ,clNearBlack, clFrmFace, Text.Body);
			aPanel->Edit[i].Str.FromUInt3Dg(AddrAssign[inx].aAssign);
			aPanel->Edit[i].Pst =2;
			aPanel->Edit[i].Show();
			//TMyString::sFillFixLen(Text.Body, 20);
			//TextRender_string24(AddrTextLeft[2], AddrTextTop[i] ,clNearBlack, clFrmFace, Text.Body);
			inx++;
		}else{
			y = RectPanel.Top + AddrTextTop[i]; 
			TextRender_string24(AddrTextLeft[0] +3, y ,clNearBlack, clFrmFace, "   ");
			TextRender_string24(AddrTextLeft[1] +3, y ,clNearBlack, clFrmFace, "      ");
			TextRender_string24(AddrTextLeft[2] +3, y ,clNearBlack, clFrmFace, "                    ");
			RectFillRender(aPanel->Edit[i].Bound.Left, aPanel->Edit[i].Bound.Top, aPanel->Edit[i].Bound.Right, aPanel->Edit[i].Bound.Bottom, clFrmFace);
			TextRender_string24(AddrTextLeft[4] +3, y ,clNearBlack, clFrmFace, "                    ");
			inx++;
		}
	}
}

void TFloorDisplay::DrawDescpList()
{
	int i, inx, x, y, m;
	unsigned int aAddr;
	aAddr = dAddr_DescpBuf;
	
	inx = Lv.TopIndex;
	for(i=0; i<dfDescpLineNum; i++){
		if(inx < dfDescpMax){
			TMyString::sFrom3Dg(Lv.TopIndex +i + 1, Text.Body);
			y = RectPanel.Top + AddrTextTop[i];
			TextRender_string24(AddrTextLeft[0], y ,clNearBlack, clFrmFace, Text.Body);
			aAddr = aAddr + inx *32;
			LoadDescp(inx, aPanel->Edit[i].Str.Text);
			aPanel->Edit[i].SetPstTail();
			aPanel->Edit[i].Show();
			inx++;
		}else{
			y = RectPanel.Top + AddrTextTop[i]; 
			TextRender_string24(AddrTextLeft[0] +3, y ,clNearBlack, clFrmFace, "   ");
			RectFillRender(aPanel->Edit[i].Bound.Left, aPanel->Edit[i].Bound.Top, aPanel->Edit[i].Bound.Right, aPanel->Edit[i].Bound.Bottom, clFrmFace);
			inx++;
		}
	}
}

void TFloorDisplay::RestoreDescpFromEdits()
{
	int i, inx, x, y, m;
	unsigned int aAddr;
	aAddr = dAddr_DescpBuf;
	
	inx = Lv.TopIndex;
	for(i=0; i<dfDescpLineNum; i++){
		if(inx < dfDescpMax){
			y = RectPanel.Top + AddrTextTop[i]; 
			aAddr = aAddr + inx *32;
			RestoreDescp(inx, aPanel->Edit[i].Str.Text);
			inx++;
		}else{
			y = RectPanel.Top + AddrTextTop[i]; 
			TextRender_string24(AddrTextLeft[0] +3, y ,clNearBlack, clFrmFace, "   ");
			RectFillRender(aPanel->Edit[i].Bound.Left, aPanel->Edit[i].Bound.Top, aPanel->Edit[i].Bound.Right, aPanel->Edit[i].Bound.Bottom, clFrmFace);
			inx++;
		}
	}	
}

void TFloorDisplay::LoadDescp(int aInx, char *p)
{
	unsigned int aAddr, i;
	aAddr = dAddr_DescpBuf;	
	for(i=0; i<32; i++){
		*p = *(volatile unsigned char *)(aAddr + aInx * 32 + i);
		p++;
	}
	*p = '\0';
}

void TFloorDisplay::RestoreDescp(int aInx, char *p)
{
	unsigned int aAddr, i;
	aAddr = dAddr_DescpBuf;	
	for(i=0; i<32; i++){
		*(volatile unsigned char *)(aAddr + aInx * 32 + i) = *p;
		p++;
	}
	*p = '\0';	
}

TGuiMsgReturn TFloorDisplay::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aIndex;
	char LastChr;
	const char *p;
	int i;
	if(WhatSet == 0){
		switch(aKey){
			case VK_UP:
				if(CtlIndex >0){
					CtlIndex--;
					Check4Selected();
					aPanel->ReDrawItems();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <2){
					CtlIndex++;
					Check4Selected();
					aPanel->ReDrawItems();
				}
				break;
			case VK_TAB:

				break;
			case VK_EXECUTE:
				if(CtlIndex == 0){
					WhatSet = 1;
					CtlIndex = 0;
					Lv.SelectedLineNum = 0;
					Lv.SelectedIndex = 0;
					Lv.TopIndex = 0;
					RenderContent();
					InitPanel4Global();
				}else
				if(CtlIndex == 1){
					WhatSet = 2;
					CtlIndex = 0;
					Lv.SelectedLineNum = 0;
					Lv.SelectedIndex = 0;
					Lv.TopIndex = 0;
					//RenderContent();
					DrawCaption24Addr();
					LoadDev();
					InitPanel4SetAddr();
					DrawAddrList();
					aPanel->Btn[0].Selected = 0;
					aPanel->Btn[1].Selected = 0;
					aPanel->Btn[0].Show();
					aPanel->Btn[1].Show();
				}else
				if(CtlIndex == 2){
					WhatSet = 3;
					CtlIndex = 0;
					Lv.SelectedLineNum = 0;
					Lv.SelectedIndex = 0;
					Lv.TopIndex = 0;
					RenderContent();
					InitPanel4SetDescp();
					aPanel->Edit[0].Selected = 1;
					aPanel->Btn[0].Selected = 0;
					aPanel->Btn[1].Selected = 0;
					aPanel->Btn[0].Show();
					aPanel->Btn[1].Show();
					DrawDescpList();
				}
				break;
			case VK_RETURN:
				aMsg = guiMsgReturn;
				break;
		}
	}else
	if(WhatSet == 1){
		switch(aKey){
			case VK_LEFT:
			case VK_RIGHT:
				if(CtlIndex == 0){
					GlobalPrm++;
					if(GlobalPrm > 2)GlobalPrm = 0;
					if(GlobalPrm == 0){
						aPanel->Edit[0].Str.FromStr("    全部显示    ");
					}else
					if(GlobalPrm == 1){
						aPanel->Edit[0].Str.FromStr("按照用户编码显示");
					}else
					if(GlobalPrm == 2){
						aPanel->Edit[0].Str.FromStr("相邻楼层编码显示");
					}
					aPanel->Edit[0].Show();
				}
				break;
			case VK_UP:
				if(CtlIndex >0){
					CtlIndex--;
					Check4Selected4Global();
					aPanel->ReDrawItems();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <2){
					CtlIndex++;
					Check4Selected4Global();
					aPanel->ReDrawItems();
				}
				break;
			case VK_TAB:

				break;
			case VK_EXECUTE:
				if(CtlIndex == 0){
					GlobalPrm++;
					if(GlobalPrm > 2)GlobalPrm = 0;
					if(GlobalPrm == 0){
						aPanel->Edit[0].Str.FromStr("    全部显示    ");
					}else
					if(GlobalPrm == 1){
						aPanel->Edit[0].Str.FromStr("按照用户编码显示");
					}else
					if(GlobalPrm == 2){
						aPanel->Edit[0].Str.FromStr("相邻楼层编码显示");
					}
					aPanel->Edit[0].Show();
				}else
				if(CtlIndex == 1){
					//Read Here
					
				}else
				if(CtlIndex == 2){
					//Write Here
					
				}
				break;
			case VK_RETURN:
				WhatSet = 0;
				Show();
				InitPanel();
				break;
		}
	}else
	if(WhatSet == 2){ 
		if(CtlIndex == 0){
			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:
					for(i=0; i<6; i++){
						if(aPanel->Edit[i].Selected){
							aPanel->Edit[i].KeyIn(aKey);
							aPanel->Edit[i].Show();
						}
					}
					break;
				case VK_UP:
					if(Lv.SelectedLineNum > 0){
						AddrAssign[Lv.SelectedIndex].aAssign = aPanel->Edit[Lv.SelectedLineNum].Str.ToInteger();
						aPanel->Edit[Lv.SelectedLineNum].Selected = 0;
						aPanel->Edit[Lv.SelectedLineNum].Show();
						Lv.SelectedLineNum--;
						Lv.SelectedIndex--;
						aPanel->Edit[Lv.SelectedLineNum].Str.FromUInt3Dg(AddrAssign[Lv.SelectedIndex].aAssign);
						aPanel->Edit[Lv.SelectedLineNum].Pst = 2;
						aPanel->Edit[Lv.SelectedLineNum].Selected = 1;
						aPanel->Edit[Lv.SelectedLineNum].Show();
					}else{
						if(Lv.TopIndex > 0){
							Lv.TopIndex -- ;
							Lv.SelectedIndex--;
							aPanel->Edit[0].Selected = 1;
							DrawAddrList();
						}
					}
					break;
				case VK_DOWN:
					if(Lv.SelectedLineNum < (dfAddrLineNum-1)){
						if(Lv.SelectedIndex < (Lv.DevCount-1)){
							AddrAssign[Lv.SelectedIndex].aAssign = aPanel->Edit[Lv.SelectedLineNum].Str.ToInteger();
							aPanel->Edit[Lv.SelectedLineNum].Selected = 0;
							aPanel->Edit[Lv.SelectedLineNum].Show();
							Lv.SelectedLineNum++;
							Lv.SelectedIndex++;
							aPanel->Edit[Lv.SelectedLineNum].Str.FromUInt3Dg(AddrAssign[Lv.SelectedIndex].aAssign);
							aPanel->Edit[Lv.SelectedLineNum].Pst = 2;
							aPanel->Edit[Lv.SelectedLineNum].Selected = 1;
							aPanel->Edit[Lv.SelectedLineNum].Show();
						}
					}else{
						if(Lv.SelectedIndex < (Lv.DevCount-1)){
							Lv.TopIndex ++;
							Lv.SelectedIndex++;
							DrawAddrList();
						}
					}
					break;
				case VK_TAB:
					//Show Save
					CtlIndex = 1;
					SubCtlIndex = 1;
					aPanel->Btn[0].Selected = 0;
					aPanel->Btn[1].Selected = 1;
					aPanel->Btn[0].Show();
					aPanel->Btn[1].Show();
					aPanel->Edit[Lv.SelectedLineNum].Selected = 0;
					aPanel->Edit[Lv.SelectedLineNum].Show();
					break;
				
			}
		}else
		if(CtlIndex ==1){
			switch(aKey){
				case VK_LEFT:
					SubCtlIndex = 0;
					aPanel->Btn[0].Selected = 1;
					aPanel->Btn[1].Selected = 0;
					aPanel->Btn[0].Show();
					aPanel->Btn[1].Show();
					break;
				case VK_RIGHT:
					SubCtlIndex = 1;
					aPanel->Btn[0].Selected = 0;
					aPanel->Btn[1].Selected = 1;
					aPanel->Btn[0].Show();
					aPanel->Btn[1].Show();
					break;
				case VK_EXECUTE:
					if(SubCtlIndex == 0){
						//Reload
					}else
					if(SubCtlIndex == 1){
						//Save
					}else{
						SubCtlIndex = 0;
						aPanel->Btn[0].Selected = 1;
						aPanel->Btn[1].Selected = 0;
						aPanel->Btn[0].Show();
						aPanel->Btn[1].Show();
					}
					break;
				case VK_TAB:
					//Show Save
					CtlIndex = 0;
					SubCtlIndex = 0;
					aPanel->Btn[0].Selected = 0;
					aPanel->Btn[1].Selected = 0;
					aPanel->Btn[0].Show();
					aPanel->Btn[1].Show();
					aPanel->Edit[Lv.SelectedLineNum].Selected = 1;
					aPanel->Edit[Lv.SelectedLineNum].Show();
					break;
			}
		}
		if(aKey == VK_RETURN){
			WhatSet = 0;
			Show();
			DrawCaption2();
			InitPanel();
		}
	}else
	if(WhatSet == 3){
		if(IsExtInput){
			if(aKey == VK_UP){
				IsExtInput = 0;
				CnInput.OverPrint(Color);
				aPanel->SText[1].Show();
				aPanel->SText[2].Show();
			}else
			if(aKey == VK_DOWN){
				IsExtInput = 0;
				CnInput.OverPrint(Color);
				aPanel->SText[1].Show();
				aPanel->SText[2].Show();
			}else
			if(aKey == VK_TAB){
				IsExtInput = 0;
				CnInput.OverPrint(Color);
				aPanel->SText[1].Show();
				aPanel->SText[2].Show();
			}else
			if(aKey == dfKEY_ENCN){
				CnInput.KeyIn(aKey);
				if(CnInput.InputMode == 0){
					IsExtInput = 0;
					CnInput.OverPrint(Color);
					aPanel->SText[1].Show();
					aPanel->SText[2].Show();
				}
			}else
			if(aKey == VK_RETURN){
				IsExtInput = 0;
				CnInput.OverPrint(Color);
				aPanel->SText[1].Show();
				aPanel->SText[2].Show();
			}else{
				p = CnInput.KeyIn(aKey);
				if(p != nullptr){
					if(pEdit != nullptr){
						if(CnInput.GotChar[0] != '\0'){
							pEdit->Pst = pEdit->Str.Insert(CnInput.GotChar, pEdit->Pst);
							pEdit->Str.BrokeFixLen(32);
							pEdit->CorrectPst();
							pEdit->Show();
						}
					}
				}
			}
		}else{
			if(CtlIndex == 0){
				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:
						for(i=0; i<4; i++){
							if(aPanel->Edit[i].Selected){
								aPanel->Edit[i].KeyIn(aKey);
								aPanel->Edit[i].Str.BrokeFixLen(32);
								aPanel->Edit[i].CorrectPst();
								aPanel->Edit[i].Show();
							}
						}
						break;
					case VK_UP:
						RestoreDescpFromEdits();
						if(Lv.SelectedLineNum > 0){
							aPanel->Edit[Lv.SelectedLineNum].Selected = 0;
							aPanel->Edit[Lv.SelectedLineNum].SetPstTail();
							aPanel->Edit[Lv.SelectedLineNum].Show();
							Lv.SelectedLineNum--;
							Lv.SelectedIndex--;
							aPanel->Edit[Lv.SelectedLineNum].Pst = 2;
							aPanel->Edit[Lv.SelectedLineNum].Selected = 1;
							aPanel->Edit[Lv.SelectedLineNum].SetPstTail();
							aPanel->Edit[Lv.SelectedLineNum].Show();
						}else{
							if(Lv.TopIndex > 0){
								Lv.TopIndex -- ;
								Lv.SelectedIndex--;
								aPanel->Edit[0].Selected = 1;
								DrawDescpList();
							}
						}
						break;
					case VK_DOWN:
						RestoreDescpFromEdits();
						if(Lv.SelectedLineNum < (dfDescpLineNum-1)){
							if(Lv.SelectedIndex < (dfDescpMax-1)){
								aPanel->Edit[Lv.SelectedLineNum].Selected = 0;
								aPanel->Edit[Lv.SelectedLineNum].SetPstTail();
								aPanel->Edit[Lv.SelectedLineNum].Show();
								Lv.SelectedLineNum++;
								Lv.SelectedIndex++;
								aPanel->Edit[Lv.SelectedLineNum].Pst = 2;
								aPanel->Edit[Lv.SelectedLineNum].Selected = 1;
								aPanel->Edit[Lv.SelectedLineNum].SetPstTail();
								aPanel->Edit[Lv.SelectedLineNum].Show();
							}
						}else{
							if(Lv.SelectedIndex < (dfDescpMax-1)){
								Lv.TopIndex ++;
								Lv.SelectedIndex++;
								DrawDescpList();
							}
						}
						break;
					case VK_TAB:
						//Show Save
						RestoreDescpFromEdits();
						CtlIndex = 1;
						SubCtlIndex = 1;
						aPanel->Btn[0].Selected = 1;
						aPanel->Btn[1].Selected = 0;
						aPanel->Btn[0].Show();
						aPanel->Btn[1].Show();
						aPanel->Edit[Lv.SelectedLineNum].Selected = 0;
						aPanel->Edit[Lv.SelectedLineNum].Show();
						break;
					case dfKEY_ENCN:
						IsExtInput = 1;
						pEdit = &aPanel->Edit[Lv.SelectedLineNum];
						CnInput.Init(20, 346, 760, 90, 0, 0, clFrmFace);
						CnInput.Show();
						break;
					
				}
			}else
			if(CtlIndex ==1){
				switch(aKey){
					case VK_LEFT:
						if(SubCtlIndex > 1){
							SubCtlIndex--;
							aPanel->Edit[11].Selected = 0;
							aPanel->Btn[0].Selected = 0;
							aPanel->Btn[1].Selected = 0;
							if(SubCtlIndex == 0){
								aPanel->Edit[11].Selected = 1;
								aPanel->Edit[11].Show();
								aPanel->Btn[0].Show();
								aPanel->Btn[1].Show();
							}else
							if(SubCtlIndex == 1){
								aPanel->Btn[0].Selected = 1;
								aPanel->Btn[0].Show();
								aPanel->Btn[1].Show();
							}								
						}
						break;
					case VK_RIGHT:
						if(SubCtlIndex < 2){
							SubCtlIndex++;
							aPanel->Edit[11].Selected = 0;
							aPanel->Btn[0].Selected = 0;
							aPanel->Btn[1].Selected = 0;
							if(SubCtlIndex == 1){
								aPanel->Btn[0].Selected = 1;
								aPanel->Edit[11].Show();
								aPanel->Btn[0].Show();
							}else
							if(SubCtlIndex == 2){
								aPanel->Btn[1].Selected = 1;
								aPanel->Btn[0].Show();
								aPanel->Btn[1].Show();
							}								
						}
						break;
					case VK_EXECUTE:
						if(SubCtlIndex == 0){
							
						}else
						if(SubCtlIndex == 1){
							//Reload
							ReadFromFlash();
						}else{
							//Save
							RestoreDescpFromEdits();
							Write2Flash();
						}
						break;
					case VK_TAB:
						//Show Save
						CtlIndex = 0;
						SubCtlIndex = 0;
						aPanel->Edit[11].Selected = 0;
						aPanel->Btn[0].Selected = 0;
						aPanel->Btn[1].Selected = 0;
						aPanel->Edit[11].Show();
						aPanel->Btn[0].Show();
						aPanel->Btn[1].Show();
						aPanel->Edit[Lv.SelectedLineNum].Selected = 1;
						aPanel->Edit[Lv.SelectedLineNum].Show();
						break;
				}
			}
			if(aKey == VK_RETURN){
				WhatSet = 0;
				Show();
				DrawCaption2();
				InitPanel();
			}
		}
	}
	return aMsg;	
}


void TFloorDisplay::Write2Flash()
{
	unsigned int fAddr, sdAddr;
	unsigned int i;
	volatile unsigned int aWait, x, wP1;
	
	wP1 =1;
	while(wP1){
		for(aWait=0; aWait<1000000;aWait++);
		wP1 = nor_get_busy_state(0);
	}
	
	//Erase 1 * 64K Blocks
	fAddr = dFlashAddrFloorDescp;
	nor_erase_sector_64k(0,fAddr);
	for(aWait=0; aWait<10000000;aWait++);
	wP1 =1;
	while(wP1){
		for(aWait=0; aWait<1000000;aWait++);
		wP1 = nor_get_busy_state(0);
	}
	 
	for(x=0; x<32; x++){
		fAddr = dFlashAddrFloorDescp + (x * 256);
		sdAddr = dAddr_DescpBuf + (x * 256);
		for(i=0; i<64;i++){
			ReadWriteData.D32[i] = *(volatile unsigned int *)(sdAddr + i *4);
		}
		
		if(ReadWriteData.D32[0] == 0xFFFFFFFF)ReadWriteData.D32[0] = 0;
		if(ReadWriteData.D32[8] == 0xFFFFFFFF)ReadWriteData.D32[8] = 0;
		if(ReadWriteData.D32[16] == 0xFFFFFFFF)ReadWriteData.D32[16] = 0;
		if(ReadWriteData.D32[24] == 0xFFFFFFFF)ReadWriteData.D32[24] = 0;
		if(ReadWriteData.D32[32] == 0xFFFFFFFF)ReadWriteData.D32[32] = 0;
		if(ReadWriteData.D32[40] == 0xFFFFFFFF)ReadWriteData.D32[40] = 0;
		if(ReadWriteData.D32[48] == 0xFFFFFFFF)ReadWriteData.D32[48] = 0;
		if(ReadWriteData.D32[56] == 0xFFFFFFFF)ReadWriteData.D32[56] = 0;
		
		nor_write_page(0, fAddr, ReadWriteData.D8);
		for(aWait=0; aWait<2000000; aWait++);
		wP1 =1;
		while(wP1){
			for(aWait=0; aWait<100000; aWait++);
			wP1 = nor_get_busy_state(0);
		}
	}

	ReadFromFlash();
}

void TFloorDisplay::ReadFromFlash()
{
	unsigned int fAddr, sdAddr;
	unsigned int x,i;
	unsigned int wP1;
	volatile unsigned int aWait;
	
	for(x=0; x<32; x++){
		fAddr = dFlashAddrFloorDescp + + (x * 256);
		nor_read_page(0, fAddr, ReadWriteData.D8);
		
		if(ReadWriteData.D32[0] == 0xFFFFFFFF)ReadWriteData.D32[0] = 0;
		if(ReadWriteData.D32[8] == 0xFFFFFFFF)ReadWriteData.D32[8] = 0;
		if(ReadWriteData.D32[16] == 0xFFFFFFFF)ReadWriteData.D32[16] = 0;
		if(ReadWriteData.D32[24] == 0xFFFFFFFF)ReadWriteData.D32[24] = 0;
		if(ReadWriteData.D32[32] == 0xFFFFFFFF)ReadWriteData.D32[32] = 0;
		if(ReadWriteData.D32[40] == 0xFFFFFFFF)ReadWriteData.D32[40] = 0;
		if(ReadWriteData.D32[48] == 0xFFFFFFFF)ReadWriteData.D32[48] = 0;
		if(ReadWriteData.D32[56] == 0xFFFFFFFF)ReadWriteData.D32[56] = 0;
		
		sdAddr = dAddr_DescpBuf + (x * 256);
		for(i=0; i<64;i++){
			*(volatile unsigned int *)(sdAddr + i*4) = ReadWriteData.D32[i];
		}
		
		if(ReadWriteData.D32[0] == 0xFFFFFFFF)ReadWriteData.D32[0] = 0;
		if(ReadWriteData.D32[8] == 0xFFFFFFFF)ReadWriteData.D32[8] = 0;
		if(ReadWriteData.D32[16] == 0xFFFFFFFF)ReadWriteData.D32[16] = 0;
		if(ReadWriteData.D32[24] == 0xFFFFFFFF)ReadWriteData.D32[24] = 0;
		if(ReadWriteData.D32[32] == 0xFFFFFFFF)ReadWriteData.D32[32] = 0;
		if(ReadWriteData.D32[40] == 0xFFFFFFFF)ReadWriteData.D32[40] = 0;
		if(ReadWriteData.D32[48] == 0xFFFFFFFF)ReadWriteData.D32[48] = 0;
		if(ReadWriteData.D32[56] == 0xFFFFFFFF)ReadWriteData.D32[56] = 0;
		
		sdAddr = dAddrSdRam_DescpFloor + (x * 256);
		for(i=0; i<64;i++){
			*(volatile unsigned int *)(sdAddr + i*4) = ReadWriteData.D32[i];
		}
	}
}































