#include "RuntimeData.h"
#include "DescpWrite.h"
#include "DevMask.h"
#include "EncodeConv.h"

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

//#define dAddr_DescpBuf	dAddrSdRam_Commom
TReadWriteData TDescpWrite::ReadWriteData;

void TDescpWrite::DrawFixText()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "回路->注释编辑");
		TextRender_string24(Lv.Grid.Left[0] + 254, Lv.Grid.Top[0] +6, clNearWhite, "本机");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] + 40, clNearWhite, "地址号");
		
		TextRender_string24(Lv.Grid.Left[0] + 138, Lv.Grid.Top[0] + 38, clNearWhite, "-");
		TextRender_string24(Lv.Grid.Left[0] + 370, Lv.Grid.Top[0] + 6, clNearWhite, "起始地址注释信息:(设置键编辑)");
			
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "回路号");
		TextRender_string24(Lv.Grid.Left[0] + 160, Lv.Grid.Top[0] +6, clNearWhite, "机器号");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] + 6, clNearWhite, "执行操作");
		TRoundRect::sDrawBySize(Lv.Grid.Left[1] +90, Lv.Grid.Top[1] + 36, 68, 30, 5, clNearWhite, "保存", 10, clNearBlack);
	
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] + 6, clNearWhite, "超纲汉字输入 Unicode");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Loop->Device Annotation");
		TextRender_string24(Lv.Grid.Left[0] + 254, Lv.Grid.Top[0] +6, clNearWhite, " Local");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] + 40, clNearWhite, "Addr");
		TextRender_string24(Lv.Grid.Left[0] + 138, Lv.Grid.Top[0] + 38, clNearWhite, "-");
		TextRender_string24(Lv.Grid.Left[0] + 370, Lv.Grid.Top[0] + 6, clNearWhite, "Begin Addr Note; Key 'Set' To Edit");
			
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "LoopID");
		TextRender_string24(Lv.Grid.Left[0] + 160, Lv.Grid.Top[0] +6, clNearWhite, "CtlID");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] + 6, clNearWhite, "Operation");
		TRoundRect::sDrawBySize(Lv.Grid.Left[1] +90, Lv.Grid.Top[1] + 36, 68, 30, 5, clNearWhite, "Save", 10, clNearBlack);
	
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] + 6, clNearWhite, "Enter Unicode16 Code");
	}
}

void TDescpWrite::Init()
{
	IsShow = 0;
	IsEditting = 0;
	IsExtInput = 1;
	IsShowPresetList = 0;
	IsPermission =0;
	PermissionCmd =0;
	TextCodeOk =0;
	
	Path0 = 1;
	Path1Start = 1;
	Path1End = 2;
	
	Tag.Reset();
	TagPreSetList.Reset();
	TagPanel.Reset();
	
	TextCp936[0] = '\0';
	TextCp936[1] = '\0';
	
	Ava = 0;
	for(int i=0; i<8; i++){
		TextList[i][32] = 0;
	}
}

void TDescpWrite::PreLoadReset()
{
	int x;
	for(int i=0; i<8; i++){
		for(x=0; x<36; x++)
			TextList[i][x] = 0;

	}
}

void TDescpWrite::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

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

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

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

	//IList->STextCtlNum.Init(Lv.Grid.Left[0] + 236, Lv.Grid.Top[0] + 6, 120, 30, 0, 0, clNearWhite, ItemColor);
	IList->EditPortNum.Init(Lv.Grid.Left[0] + 86, Lv.Grid.Top[0] + 2, 50, 30, 0, 0, 1, clNearWhite);
	IList->EditAddrNum.Init(Lv.Grid.Left[0] + 86, Lv.Grid.Top[0] + 36, 50, 30, 0, 0, 1, clNearWhite);
	IList->Edit[0].Init(Lv.Grid.Left[0] + 152, Lv.Grid.Top[0] + 36, 50, 30, 0, 0, 1, clNearWhite);
	IList->EditDescpShow.Init(Lv.Grid.Left[0] + 390, Lv.Grid.Top[0] + 36, 400, 30, 0, 0, 1, clNearWhite);
	
	//IList->STextCtlNum.Enable = 1;
	//IList->STextCtlNum.Visible =1;
	
	IList->EditPortNum.SetMaxLen(2);
	IList->EditAddrNum.SetMaxLen(3);
	IList->Edit[0].SetMaxLen(3);
	IList->EditPortNum.Enable = 1;
	IList->EditPortNum.Visible = 1;
	IList->EditAddrNum.Enable = 1;
	IList->EditAddrNum.Visible = 1;
	IList->Edit[0].Enable = 1;
	IList->Edit[0].Visible = 1;
	IList->EditDescpShow.Enable = 1;
	IList->EditDescpShow.Visible = 1;
	
	for(int i=2; i<5; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	
	IList->Edit[1].Init(Lv.Grid.Left[2] + 90, Lv.Grid.Top[2] + 36, 106, 30, 0, 0, 1, clNearWhite);
	IList->Edit[1].TextClear();
	IList->Edit[1].SetMaxLen(8);
	IList->Edit[1].Enable = 1;
	IList->Edit[1].Visible = 1;
	
	//IList->STextCtlNum.Show();
	
	IList->EditPortNum.SetText("01", 24);
	IList->EditAddrNum.SetText("001", 24);
	IList->Edit[0].SetText("002", 24);
	
	IList->EditPortNum.Show();
	IList->EditAddrNum.Show();
	IList->Edit[0].Show();
	IList->EditDescpShow.Init(Lv.Grid.Left[0] + 390, Lv.Grid.Top[0] + 36, 400, 30, 0, 0, 1, clNearWhite);
	
	FirstLoadText2Edit();
	IList->EditDescpShow.Show();
	
	DrawFixText();
	
	Path0Ava =1;
	Path1StartAva =1;
	Path1EndAva =1;
	
	Tag.Inx = 1;
	Check4Selected();
	
	DrawPreSetList();
	DrawPreSetListTip();
}



void TDescpWrite::Show()
{
	IsShow = 1;
	DrawSelf();


}

void TDescpWrite::FullRedraw(int Prm)
{
	if(Prm == 0){
		DrawSelf();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		for(int i=0; i<3; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
		if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
		DrawCommonByExReturn();
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}

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

	TextDigitRender2Right24(Lv.Grid.Left[0] + 232, Lv.Grid.Top[0] +6, clNearWhite, MainCtl.fData.Split.MyNum);
	
	//IList->STextCtlNum.Init(Lv.Grid.Left[0] + 236, Lv.Grid.Top[0] + 6, 120, 30, 0, 0, clNearWhite, ItemColor);
	TextRender_string24(Lv.Grid.Left[0] + 138, Lv.Grid.Top[0] + 38, clNearWhite, "-");
	
	for(int i=2; i<3; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	DrawFixText();
	
	//IList->STextCtlNum.Show();
	
	IList->EditPortNum.Show();
	IList->EditAddrNum.Show();
	IList->Edit[0].Show();
	IList->EditDescpShow.Show();
	
	Check4Selected();
	
	DrawPreSetList();
	//DrawPreSetListTip();	
}

void TDescpWrite::FirstLoadText2Edit()
{
	unsigned int tAddr;

	tAddr = dAddrSdRam_Descp + ((Path0-1) * 256 * 32) + ( (Path1Start-1) * 32);
	for(int g=0; g<32; g++){
		IList->EditDescpShow.Str.Text[g] = *(volatile unsigned char *)(tAddr + g);
		if(  (IList->EditDescpShow.Str.Text[g] == '\0') || (IList->EditDescpShow.Str.Text[g] == 0) ){
			break;
		}
	}
	IList->EditDescpShow.Str.Text[32] = '\0';
	IList->EditDescpShow.Str.Text[33] = '\0';
	IList->EditDescpShow.SetPstTail();
}

void TDescpWrite::ReLoadText2Edit()
{
	FirstLoadText2Edit();
}

void TDescpWrite::LoadText2Edit()
{
	unsigned int tAddr;
	int NewP1Start, NewP1End, AddrCnt;
	
	if(Path1End < dEP_MAX_COUNT_PER_PORT){
		AddrCnt = Path1End - Path1Start +1;
		NewP1Start = Path1End +1;
		NewP1End = NewP1Start + AddrCnt -1;
		if(NewP1End > dEP_MAX_COUNT_PER_PORT)NewP1End = dEP_MAX_COUNT_PER_PORT;
		
		Path1Start = static_cast<unsigned char>(NewP1Start);
		Path1End = static_cast<unsigned char>(NewP1End);
		
		IList->EditAddrNum.Str.FromUInt3Dg(Path1Start);
		IList->Edit[0].Str.FromUInt3Dg(Path1End);
		IList->EditAddrNum.SetPstTail();
		IList->Edit[0].SetPstTail();
		IList->EditAddrNum.Show();
		IList->Edit[0].Show();
		
		tAddr = dAddrSdRam_Descp + ((Path0-1) * 256 * 32) + ( (Path1Start-1) * 32);
		if(*(volatile unsigned char *)(tAddr) != '\0'){
			for(int g=0; g<32; g++){
				IList->EditDescpShow.Str.Text[g] = *(volatile unsigned char *)(tAddr + g);
				if(  (IList->EditDescpShow.Str.Text[g] == '\0') || (IList->EditDescpShow.Str.Text[g] == 0) ){
					break;
				}
			}
		}
	}else{
		tAddr = dAddrSdRam_Descp + ((Path0-1) * 256 * 32) + ( (Path1Start-1) * 32);
		for(int g=0; g<32; g++){
			IList->EditDescpShow.Str.Text[g] = *(volatile unsigned char *)(tAddr + g);
			if(  (IList->EditDescpShow.Str.Text[g] == '\0') || (IList->EditDescpShow.Str.Text[g] == 0) ){
				break;
			}
		}
	}

	IList->EditDescpShow.Str.Text[32] = '\0';
	IList->EditDescpShow.Str.Text[33] = '\0';
	IList->EditDescpShow.SetPstTail();
}

void TDescpWrite::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 TDescpWrite::InitPanel()
{
	int x,y,x2,y2, aW,aH,i;

	x =  10;
	y = Lv.Grid.Top[2];
	x2 =  789;
	y2 = Lv.Grid.Bottom[4];
	aW = x2 - x +1;
	aH = y2 -y +1;
	
	aPanel->Init(x,y,aW,aH,0,0,clMoneyGreen, bvRaised);
	
	aPanel->Caption.Init(1,1,aW-3,32,x,y,clNearWhite,clBlue);
	if(LanguageEnCn==0){
		aPanel->Caption.SetText("按中/EN键切换输入法,设置键更改,返回键放弃更改并返回",24);
	}else{
		aPanel->Caption.SetText("Key'中/EN' Switch Input Method,Key 'Set' To Edit",24);
	}
	aPanel->Caption.Enable = 1;
	aPanel->Caption.Visible = 1;

	aPanel->SText[0].Init(460,40,300,34,x,y,clNearBlack,clMoneyGreen);
	if(LanguageEnCn==0){
		TMyString::sFromStr("当前快捷输入数字", aPanel->SText[0].Text);
	}else{
		TMyString::sFromStr("Enter Number Keys", aPanel->SText[0].Text);
	}
	aPanel->SText[0].Enable = 1;
	aPanel->SText[0].Visible = 1;
	
	aPanel->SText[1].Init(460,40,300,34,x,y,clNearBlack,clMoneyGreen);
	if(LanguageEnCn==0){
		TMyString::sFromStr("装载预设内容", aPanel->SText[1].Text);
	}else{
		TMyString::sFromStr("Load From PreLoad", aPanel->SText[1].Text);
	}
	aPanel->SText[1].Enable = 1;
	aPanel->SText[1].Visible = 1;
	
	aPanel->SText[2].Init(460,40,300,34,x,y,clNearBlack,clMoneyGreen);
	if(LanguageEnCn==0){
		TMyString::sFromStr("保存到预设内容", aPanel->SText[2].Text);
	}else{
		TMyString::sFromStr("Save To PreLoad", aPanel->SText[2].Text);
	}
	aPanel->SText[2].Enable = 1;
	aPanel->SText[2].Visible = 1;
	
	aPanel->Edit[ 0].Init(10,40,420,30,x,y,1,clNearBlack);
	aPanel->Edit[ 0].Str.FromStr("gggggg");
	
	for(i=0; i<1;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(32);
		aPanel->Edit[i].SetPstTail();
		aPanel->Edit[i].Selected = 1;
	}
	
	aPanel->Edit[ 1].Init(10,80,36,30,x,y,1,clNearBlack);
	aPanel->Edit[ 1].Str.FromStr("1");
	for(i=1; i<2;i++){
		//aPanel->Edit[i].Enable = 1;
		//aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(1);
		aPanel->Edit[i].SetPstTail();
		aPanel->Edit[i].Selected = 0;
	}
	
	aPanel->Edit[ 2].Init(10,120,36,30,x,y,1,clNearBlack);
	aPanel->Edit[ 2].Str.FromStr("1");
	for(i=1; i<2;i++){
		//aPanel->Edit[i].Enable = 1;
		//aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(1);
		aPanel->Edit[i].SetPstTail();
		aPanel->Edit[i].Selected = 0;
	}
	if(EditWhat>7)
		aPanel->Edit[0].Str.FromStr(IList->EditDescpShow.Str.Text);
	else
		aPanel->Edit[0].Str.FromStr(TextList[EditWhat]);
	aPanel->Edit[0].SetPstTail();
	
	aPanel->Show();
	aPanel->Caption.Show();
	aPanel->SText[0].Show();
	//aPanel->SText[1].Show();
	//aPanel->SText[2].Show();
	aPanel->Edit[0].Show();
	aPanel->Edit[0].ShowCursor();
	//aPanel->Edit[1].Show();
	//aPanel->Edit[2].Show();
	
	IsExtInput = 1;
	CnInput.Init(aPanel->Bound.Left + 10, aPanel->Bound.Top +80, 760, 90, 0, 0, clGray);
	CnInput.Show();
	if(LanguageEnCn==0)TMyString::sFromStr("请根据以下输入法提示输入", aPanel->SText[0].Text);
	else TMyString::sFromStr("Pay Attention To The Tip", aPanel->SText[0].Text);
	aPanel->SText[0].Show();	
	IsExtInput = 1;

}

void TDescpWrite::ClearSelected(void)
{
	switch(Tag.Inx){
		case 0:
			
			break;
		case 1:
			IList->EditPortNum.Selected = 0;
			IList->EditPortNum.Show();
			break;
		case 2:
			IList->EditAddrNum.Selected = 0;
			IList->EditAddrNum.Show();
			break;
		case 3:
			IList->Edit[0].Selected = 0;
			IList->Edit[0].Show();
			break;
		case 4:
			IList->EditDescpShow.Selected = 0;
			IList->EditDescpShow.Show();
			break;
		case 5:
			char *tText;
			if(LanguageEnCn==0)tText="保存";else tText="Save";
			TRoundRect::sDrawBySize(Lv.Grid.Left[1] +90, Lv.Grid.Top[1] + 36, 68, 30, 5, clNearWhite, tText, 10, clNearBlack);
			break;
		case 6:
			IList->Edit[1].Selected = 0;
			IList->Edit[1].Show();
			break;
	}		
}

void TDescpWrite::Check4Selected(void)
{
	switch(Tag.Inx){
		case 0:
			
			break;
		case 1:
			IList->EditPortNum.Selected = 1;
			IList->EditPortNum.SetPstTail();
			IList->EditPortNum.Show();
			IList->EditPortNum.ShowCursor();
			break;
		case 2:
			IList->EditAddrNum.Selected = 1;
			IList->EditAddrNum.SetPstTail();
			IList->EditAddrNum.Show();
			IList->EditAddrNum.ShowCursor();
			break;
		case 3:
			IList->Edit[0].Selected = 1;
			IList->Edit[0].SetPstTail();
			IList->Edit[0].Show();
			IList->Edit[0].ShowCursor();
			break;
		case 4:
			IList->EditDescpShow.Selected = 1;
			IList->EditDescpShow.CorrectPst();
			IList->EditDescpShow.Show();
			IList->EditDescpShow.ShowCursor();
			break;
		case 5:
			char *tText;
			if(LanguageEnCn==0)tText="保存";else tText="Save";
			TRoundRect::sDrawBySize(Lv.Grid.Left[1] +90, Lv.Grid.Top[1] + 36, 68, 30, 5, clBlue, tText, 10, clNearWhite);
			break;
		case 6:
			IList->Edit[1].Selected = 1;
			IList->Edit[1].SetPstTail();
			IList->Edit[1].Show();
			IList->Edit[1].ShowCursor();
			break;
	}			
}
/*
void TDescpWrite::LoadDescp(int aP0, int aP1, char *p)
{
	unsigned int aAddr, i;
	unsigned int p0, p1;
	
	if(aP0 <1)return;
	if(aP1 <1)return;

	if(aP0 <= dPORT_MAX_COUNT){
		p0 = aP0-1;
		p1 = aP1-1;
		aAddr = dAddrSdRam_Descp + (p0 * 8192) + (p1*32);
		for(i=0; i<32; i++){
			*p = *(volatile unsigned char *)(aAddr + i);
			p++;
		}
		*p = '\0';
		 aPanel->Edit[2].SetPstTail();
	}else
	if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
		if(aP1 <= dDIRECTCTL_KEY_MAX_COUNT){
			p0 = aP0 - dADDR_DIRECTPAD_START;
			p1 = (p0 * dDIRECTCTL_KEY_MAX_COUNT) + (aP1);
			p0 = dPORT_MAX_COUNT;
			aAddr = dAddrSdRam_Descp + (p0 * 8192) + (p1*32);
			for(i=0; i<32; i++){
				*p = *(volatile unsigned char *)(aAddr + i);
				p++;
			}
			*p = '\0';
			 aPanel->Edit[2].SetPstTail();
		}
	}
}
*/

void TDescpWrite::sRestoreDescp(int aP0, int aP1, char *p)
{
	unsigned int aAddr, i;
	unsigned int p0, p1;
	
	if(aP0 <1)return;
	if(aP1 <1)return;

	if(aP0 <= dPORT_MAX_COUNT){
		p0 = aP0-1;
		p1 = aP1-1;
		aAddr = dAddrSdRam_Descp + (p0 * 8192) + (p1*32);
		for(i=0; i<32; i++){
			*(volatile unsigned char *)(aAddr + i) = *p;
			p++;
		}
		*p = '\0';
	}else
	if( (aP0 >= dADDR_DIRECTPAD_START) && (aP0 <= dADDR_DIRECTPAD_END) ){
		if(aP1 <= dDIRECTCTL_KEY_MAX_COUNT){
			p0 = aP0 - dADDR_DIRECTPAD_START;
			p1 = (p0 * dDIRECTCTL_KEY_MAX_COUNT) + (aP1);
			p0 = dPORT_MAX_COUNT;
			aAddr = dAddrSdRam_Descp + (p0 * 8192) + (p1*32);
			for(i=0; i<32; i++){
				*(volatile unsigned char *)(aAddr + i) = *p;
				p++;
			}
			*p = '\0';
		}
	}
}

void TDescpWrite::AnalyTextWrite()
{
	int i, FixLen, LastCharPst, Cnt, FirstZeroPst;
	/*
	char TextNum[16];
	for(i=0; i<36; i++)Text2WriteFix[i] = 0;
	for(i=0; i<16; i++)TextNum[i] = 0;
	Num2WriteCount = 0;
	LastCharPst =0;
	FirstZeroPst = 31;
	for(i=0; i<32; i++){
		if(IList->EditDescpShow.Str.Text[i] == 0){
			FirstZeroPst = i;
			break;
		}
	}
	for(i=FirstZeroPst; i>-1; i--){
		if(IList->EditDescpShow.Str.Text[i] == 0)continue;
		if( (IList->EditDescpShow.Str.Text[i] >= '0') && (IList->EditDescpShow.Str.Text[i] <= '9')){
			Num2WriteCount ++;
		}else{
			LastCharPst = i;
			break;
		}
		if(Num2WriteCount >7)break;
	}
	
	for(i=0; i<=LastCharPst; i++){
		Text2WriteFix[i] = IList->EditDescpShow.Str.Text[i];
	}
	
	for(i=LastCharPst+1, Cnt =0; (i<32) && (Cnt<8); i++, Cnt++){
		TextNum[Cnt] = IList->EditDescpShow.Str.Text[i];
	}
	
	Num2Write = TMyString::sToInteger(TextNum);
	if(Num2Write == 0) Num2Write =1;
	*/
	
	
	for(i=0; i<32; i++){
		Text2WriteFix[i] = IList->EditDescpShow.Str.Text[i];
		if(Text2WriteFix[i] == '\0')break;
	}
	Text2WriteFix[32] =0;
}

void TDescpWrite::FillTextWrite()
{
	int tP0, tStart, tEnd;
	int t,i, Cnt;
	unsigned int tNum, aAddr;
	if(Path0 == 0) return;
	if(Path0 > dPORT_MAX_COUNT) return;
	if(Path1Start == 0) return;
	if(Path1Start > dEP_MAX_COUNT_PER_PORT) return;
	if(Path1End == 0) return;
	if(Path1End > dEP_MAX_COUNT_PER_PORT) return;
	
	tP0 = Path0-1;
	tStart = Path1Start -1;
	tEnd = Path1End -1;
	tNum = Num2Write;
	
	for(t=tStart; t<=tEnd; t++){
		for(i=0; i<48; i++)TextWritting[i] = 0;
		TMyString::sFromStr(Text2WriteFix , TextWritting);
		//if( (tNum >999'9999) && (Num2WriteCount < 8) )Num2WriteCount =8;
		//else if( (tNum > 99'9999) && (Num2WriteCount < 7) )Num2WriteCount =7;
		//else if( (tNum >  9'9999) && (Num2WriteCount < 6) )Num2WriteCount =6;
		//else if( (tNum >    9999) && (Num2WriteCount < 5) )Num2WriteCount =5;
		//else if( (tNum >     999) && (Num2WriteCount < 4) )Num2WriteCount =4;
		//else if( (tNum >      99) && (Num2WriteCount < 3) )Num2WriteCount =3;
		//else if( (tNum >       9) && (Num2WriteCount < 2) )Num2WriteCount =2;
		//else if( (             1) && (Num2WriteCount < 1) )Num2WriteCount =1;
		//TMyString::sAddOn_N_Dg(Num2WriteCount, tNum, TextWritting);
		aAddr = dAddrSdRam_Descp + (tP0 * 8192) + (t*32);
		for(i=0; i<32; i++){
			*(volatile unsigned char *)(aAddr + i) = TextWritting[i];
		}
		//tNum++;
	}
}

void TDescpWrite::EditDescpReturn()
{

}

void TDescpWrite::Redraw4DescpEdit()
{
	IList->EditDescpShow.SetPstTail();
	IList->EditDescpShow.Show();
	
	RectFillRender(aPanel->Bound.Left, aPanel->Bound.Top, aPanel->Bound.Right, aPanel->Bound.Bottom, ContentColor);
	for(int i=2; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	if(LanguageEnCn==0)
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] + 6, clNearWhite, "超纲汉字输入");
	else 
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] + 6, clNearWhite, "Enter Unicode16 Code");
	
	DrawPreSetList();
	DrawPreSetListTip();
	IList->Edit[1].Show();
	if(IsShowPresetList == 0)
		Check4Selected();
}

void TDescpWrite::PermissionCmdReturnDraw()
{
	DrawPreSetList();
}

void TDescpWrite::PermissionCmdGO()
{
	if(PermissionCmd == 1){
		TextRender_string24(Lv.Grid.Left[1] +176,Lv.Grid.Top[1] +4, clNearWhite,ItemColor,"        ");
		AnalyTextWrite();
		FillTextWrite();
		sWrite2Flash(Path0);
		if(LanguageEnCn==0)
			TextRender_string24(Lv.Grid.Left[1] +176,Lv.Grid.Top[1] +4, clNearWhite,"保存完成");
		else
			TextRender_string24(Lv.Grid.Left[1] +176,Lv.Grid.Top[1] +4, clNearWhite,"Saved OK");
		LoadText2Edit();
		IList->EditDescpShow.Show();	
	}else
	if(PermissionCmd == 2){
		IList->EditDescpShow.Selected = 0;
		IList->EditDescpShow.Show();
		EditWhat = 0xFF;
		InitPanel();
		IsEditting = 1;
		IsExtInput = 1;					
	}
}

void TDescpWrite::DrawPreSetList()
{
	int i, x, y;
	unsigned int aClr;
	if(IsShowPresetList){
		aClr = clBlue;
	}else{
		aClr = clMedGray;
	}
	
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Bottom,PanelColor);
	
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Left +4,Lv.Panel.Bottom,aClr);
	RectFillRender(Lv.Panel.Left,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Top +4,aClr);
	RectFillRender(Lv.Panel.Right-4,Lv.Panel.Top,Lv.Panel.Right,Lv.Panel.Bottom,aClr);
	RectFillRender(Lv.Panel.Left,Lv.Panel.Bottom -4,Lv.Panel.Right,Lv.Panel.Bottom,aClr);

	for(i=0; i<8; i++){
		x = Lv.Panel.Left +6;
		y = i * 30 + Lv.Panel.Top +8;
		if( (i == TagPreSetList.Inx) && (IsShowPresetList) ){
			aClr = clNearWhite;
		}else{
			aClr = clBlack;
		}
		TextDigitRender1Right24(x,y,aClr, i+1);
		TextRender_string24(x+12,y,aClr,".");
		TextRender_string24(x+24,y,aClr,TextList[i]);
	}
}

void TDescpWrite::DrawPreSetListTip()
{
	//注释暂存区:上下键切换条目,
	if(LanguageEnCn==0)TextRender_string24(Lv.PanelSmall.Left +6,Lv.PanelSmall.Top +4, clNearWhite,	"设置键从编辑栏装载,查询键加载到编辑栏");
	else TextRender_string24(Lv.PanelSmall.Left +6,Lv.PanelSmall.Top +4, clNearWhite,			"Key Set Load Form Editor,Key Query SaveTo");
}

TGuiMsgReturn TDescpWrite::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg;
	int i,iVal, ava;
	unsigned char NewP0;
	unsigned char NewP1;
	const char *p;
	unsigned int Inx;
	int HasPrc = 0;
	if(IsPermission){
		PmsRtnMsg = PmsIntervene4Gui.KeyIn(aKey);
		if(PmsRtnMsg == guiMsgOkAndReturn){
			//Enter And Run Go
			IsPermission = 0;
			PermissionCmdReturnDraw();
			PermissionCmdGO();
		}else
		if(PmsRtnMsg == guiMsgReturn){
			//Not Enter Redraw
			IsPermission = 0;
			PermissionCmdReturnDraw();
		}
	}else
	if(IsEditting){
		const char *p;
		if(aKey == dfKEY_SETUP){
			if(EditWhat>7){
				IList->EditDescpShow.Str.FromStr(aPanel->Edit[0].Str.Text);
				TMyString::sBrokeFixLen(aPanel->Edit[0].Str.Text, 32);
			}else{
				TMyString::sFromStr(aPanel->Edit[0].Str.Text, TextList[EditWhat]);
				TMyString::sBrokeFixLen(TextList[EditWhat], 32);
			}
			Redraw4DescpEdit();
			IsEditting = 0;
		}else
		if(IsExtInput){
			if(aKey == dfKEY_ENCN){
				CnInput.KeyIn(aKey);
				if(0){
					IsExtInput = 0;
					CnInput.OverPrint(this->aPanel->Color);
					aPanel->Show();
					aPanel->Caption.Show();
					aPanel->SText[0].Show();
					aPanel->Edit[0].Show();
					aPanel->Edit[0].ShowCursor();
					if(LanguageEnCn==0)TMyString::sFromStr(	"当前快捷输入数字 ", aPanel->SText[0].Text);
					else TMyString::sFromStr(			"Enter Number Keys", aPanel->SText[0].Text);
					aPanel->SText[0].Show();
				}
			}else
			if(aKey == VK_RETURN){
				//Just return
				Redraw4DescpEdit();
				IsEditting = 0;
			}else{
				if(  (aKey == VK_DELETE)  &&  (CnInput.sEdit.Pst == -1) ){
					aPanel->Edit[0].KeyIn(aKey);
					aPanel->Edit[0].Show();
					aPanel->Edit[0].ShowCursor();
				}else{
					p = CnInput.KeyIn(aKey);
					if(p != nullptr){
						if(CnInput.GotChar[0] != '\0'){
							aPanel->Edit[0].Pst = aPanel->Edit[0].Str.Insert(CnInput.GotChar, aPanel->Edit[0].Pst);
							aPanel->Edit[0].Str.BrokeFixLen(32);
							aPanel->Edit[0].CorrectPst();
							aPanel->Edit[0].Show();
						}
					}
				}
			}
		}else{
			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:
				case VK_DELETE:
					aPanel->Edit[0].KeyIn(aKey);
					aPanel->Edit[0].Str.BrokeFixLen(32);
					aPanel->Edit[0].CorrectPst();
					aPanel->Edit[0].Show();
					break;
				case dfKEY_ENCN:
					if(IsExtInput == 0){
						IsExtInput = 1;
						CnInput.Init(aPanel->Bound.Left + 10, aPanel->Bound.Top +80, 760, 90, 0, 0, clGray);
						CnInput.Show();
						if(LanguageEnCn==0)TMyString::sFromStr(	"当前根据输入法输入", aPanel->SText[0].Text);
						else TMyString::sFromStr(			"Pay Attention To The Tip", aPanel->SText[0].Text);
						aPanel->SText[0].Show();
					}
					break;
				case VK_EXECUTE:

					break;
				case VK_RETURN:
					Redraw4DescpEdit();
					IsEditting = 0;
					break;
			}
		}
	}else{
		if(IsShowPresetList == 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_LEFT:
				case VK_RIGHT:
				case VK_DELETE:
					if(Tag.Inx == 1){
						if( (aKey == VK_RIGHT) && (Path0Ava) ){
							if(IList->EditPortNum.Pst >= IList->EditPortNum.Str.GetEndPst()){
								if(Tag.Inx > Tag.InxMin){
									ClearSelected();
									Tag.Inx--;
									Check4Selected();
									HasPrc = 1;
								}
							}
						}
						if(HasPrc == 0){
							IList->EditPortNum.KeyIn(aKey);
							iVal = IList->EditPortNum.Str.ToInteger();
							if( (iVal >0) && (iVal <= dPORT_MAX_COUNT) ){
								IList->EditPortNum.SelectedColor = clBlue;
								Path0Ava = 1;
								Path0= iVal;
								if(Path1StartAva){
									ReLoadText2Edit();
									IList->EditDescpShow.Show();
								}
							}else{
								IList->EditPortNum.SelectedColor = clRed;
								Path0Ava = 0;
							}
							IList->EditPortNum.Show();
						}
					}else
					if(Tag.Inx == 2){
						if( (aKey == VK_RIGHT) && (Path1StartAva) ){
							if(IList->EditAddrNum.Pst >= IList->EditAddrNum.Str.GetEndPst()){
								ClearSelected();
								Tag.Inx++;
								Check4Selected();
								HasPrc = 1;
							}
						}
						if(HasPrc == 0){
							IList->EditAddrNum.KeyIn(aKey);
							iVal = IList->EditAddrNum.Str.ToInteger();
							if( (iVal >0) && (iVal <= dEP_MAX_COUNT_PER_PORT) ){
								IList->EditAddrNum.SelectedColor = clBlue;
								Path1StartAva =1;
								Path1Start = iVal;
								if(Path0Ava){
									ReLoadText2Edit();
									IList->EditDescpShow.Show();
								}
							}else{
								IList->EditAddrNum.SelectedColor = clRed;
								Path1StartAva =0;
							}
							IList->EditAddrNum.Show();				
						}
					}else
					if(Tag.Inx == 3){
						if( (aKey == VK_LEFT) && (Path1EndAva) ){
							if(IList->Edit[0].Pst < 0){
								ClearSelected();
								Tag.Inx--;
								Check4Selected();
								HasPrc = 1;
							}
						}else
						if( (aKey == VK_RIGHT) && (Path1EndAva) ){
							if(IList->Edit[0].Pst >= IList->Edit[0].Str.GetEndPst()){
								ClearSelected();
								Tag.Inx++;
								Check4Selected();
								HasPrc = 1;
							}
						}
						if(HasPrc == 0){
							IList->Edit[0].KeyIn(aKey);
							iVal = IList->Edit[0].Str.ToInteger();
							if( (iVal >0) && (iVal <= dEP_MAX_COUNT_PER_PORT) ){
								IList->Edit[0].SelectedColor = clBlue;
								Path1EndAva =1;
								Path1End = iVal;
							}else{
								IList->Edit[0].SelectedColor = clRed;
								Path1EndAva =0;
							}
							IList->Edit[0].Show();
						}
					}else
					if(Tag.Inx == 4){
						if( (aKey == VK_LEFT) ){
							if(IList->EditDescpShow.Pst < 0){
								ClearSelected();
								Tag.Inx--;
								Check4Selected();
								HasPrc = 1;
							}
						}
						if(HasPrc == 0){
							IList->EditDescpShow.KeyIn(aKey);
							IList->EditDescpShow.Show();
						}
					}else
					if(Tag.Inx == 6){
						IList->Edit[1].KeyIn(aKey);
						IList->Edit[1].Show();
						if(IList->Edit[1].Str.GetLength() > 0){
							UnicodeCode = IList->Edit[1].Str.ToInteger();
							Cp936Code = UnicodeToCP936(UnicodeCode); 
							TextCodeOk =0;
							if(Cp936Code < 32){
								TextCodeOk =1;
							}else
							if(Cp936Code == 127 ){
								TextCodeOk =1;
							}else
							if(Cp936Code >127){
								if( (Cp936Code < 0x8140) || (Cp936Code > 0xFEFE) ) {
									TextCodeOk =2;
								}
							}
							if(TextCodeOk == 0){
								if(Cp936Code == 0){
									TextCp936[0] =0;
									TextCp936[1] =0;
								}else{
									if( Cp936Code > 0xFF ){
										if( (Cp936Code & 0x8000) == 0x8000){
											TextCp936[0] = static_cast<char>((Cp936Code >> 8)& 0x00FF);
											TextCp936[1] = static_cast<char>(Cp936Code & 0x00FF);
										}else{
											TextCp936[0] =0;
											TextCp936[1] =0;
										}
									}else{
										TextCp936[0] = static_cast<char>(Cp936Code & 0x00FF);
										TextCp936[1] = 0;
									}
									TextCp936[2] = 0;
								}
								TextRender_string24(IList->Edit[1].Bound.Left - 40,IList->Edit[1].Bound.Top +2, ItemColor, ItemColor,"  ");
								TextRender_string24(IList->Edit[1].Bound.Left - 40,IList->Edit[1].Bound.Top +2, clNearWhite,TextCp936);
							}else{
								if(TextCodeOk ==1){
									TextCp936[0] = '?';
									TextCp936[1] = 0;
									TextRender_string24(IList->Edit[1].Bound.Left - 40,IList->Edit[1].Bound.Top +2, clNearWhite, ItemColor,"? ");
								}else{
									TextCp936[0] = '?';
									TextCp936[1] = '?';
									TextRender_string24(IList->Edit[1].Bound.Left - 40,IList->Edit[1].Bound.Top +2, clNearWhite, ItemColor,"??");
								}
								TextCp936[2] = 0;
							}
							TextDigitRender5Right24(IList->Edit[1].Bound.Right +6,IList->Edit[1].Bound.Top +2, clNearWhite,ItemColor,Cp936Code);
						}else{
							TextRender_string24(IList->Edit[1].Bound.Left - 40,IList->Edit[1].Bound.Top +2, clNearWhite, ItemColor,"  ");
							TextRender_string24(IList->Edit[1].Bound.Right +6, IList->Edit[1].Bound.Top +2, clNearWhite, ItemColor,"     ");
							TextCp936[0] = '\0';
							TextCp936[1] = '\0';
						}
					}					
					break;
				case VK_UP:
					if(Tag.Inx > Tag.InxMin){
						//RestoreDescp(Path0, Path1, aPanel->Edit[2].Str.Text);
						if(Path0Ava && Path1StartAva && Path1EndAva){
							ClearSelected();
							Tag.Inx--;
							Check4Selected();
						}
					}
					break;
				case VK_DOWN:
					if(Tag.Inx <6){
						//RestoreDescp(Path0, Path1, aPanel->Edit[2].Str.Text);
						if(Path0Ava && Path1StartAva && Path1EndAva){
							ClearSelected();
							Tag.Inx++;
							Check4Selected();
						}
					}
					break;
				case VK_TAB:
					IsShowPresetList = 1;
					ClearSelected();
					DrawPreSetList();
					break;
				case dfKEY_SETUP:
					if(Tag.Inx == 4){
						PermissionCmd = 2;
						if(SystemPermission >2){
							PermissionCmdGO();
						}else{
							IsPermission = 1;
							PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 3);
						}
					}
					break;
				case VK_EXECUTE:
					if(Tag.Inx == 5){
						//Save Here
						PermissionCmd = 1;
						if(SystemPermission >2){
							PermissionCmdGO();
						}else{
							IsPermission = 1;
							PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 3);
						}
					}else
					if(Tag.Inx == 6){
						if(TextCp936[0]){
							IList->EditDescpShow.Str.Insert(TextCp936, IList->EditDescpShow.Pst);
							IList->EditDescpShow.CorrectPst();
							IList->EditDescpShow.Str.BrokeFixLen(32);
							IList->EditDescpShow.CorrectPst();
							IList->EditDescpShow.Show();
						}
					}
					break;
				case VK_RETURN:
					aMsg = guiMsgReturn;
					break;
			}
		}else{
			switch(aKey){
				case VK_UP:
					if(TagPreSetList.Inx >0){
						TagPreSetList.Inx--;
					}else{
						TagPreSetList.Inx=7;
					}
					DrawPreSetList();
					break;
				case VK_DOWN:
					if(TagPreSetList.Inx <7){
						TagPreSetList.Inx++;
					}else{
						TagPreSetList.Inx=0;
					}
					DrawPreSetList();
					break;
				case dfKEY_SETUP:
					Inx = TagPreSetList.Inx;
					if(Inx >7)Inx = 0;
					for(i=0; i<32; i++){
						TextList[Inx][i] = IList->EditDescpShow.Str.Text[i];
					}
					TextList[Inx][32] =0;
					TextList[Inx][33] =0;
					DrawPreSetList();
					break;
				case dfKEY_QUERY:
					unsigned int i, Inx;
					Inx = TagPreSetList.Inx;
					if(Inx >7)Inx = 0;
					for(i=0; i<32; i++){
						 IList->EditDescpShow.Str.Text[i] = TextList[Inx][i];
					}
					IList->EditDescpShow.Str.Text[32] =0;
					IList->EditDescpShow.Str.Text[33] =0;
					IList->EditDescpShow.Show();
					break;
				case VK_TAB:
					IsShowPresetList = 0;
					DrawPreSetList();
					Check4Selected();
					break;
				case VK_EXECUTE:
					IList->EditDescpShow.Selected = 0;
					IList->EditDescpShow.Show();
					EditWhat = TagPreSetList.Inx;
					InitPanel();
					IsEditting = 1;
					IsExtInput = 0;
					break;
				case VK_RETURN:
					aMsg = guiMsgReturn;
					break;
			}
		}			
	}
	return aMsg;	
}

void TDescpWrite::sWrite2Flash(unsigned char aPath0)
{
	unsigned int aP0;
	unsigned int aP1;
	unsigned int fAddr, sdAddr, GetSta;
	unsigned int i,m;
	unsigned int wP1;
	volatile unsigned int x;
	if( (aPath0 >= dADDR_DIRECTPAD_START) && (aPath0 <= dADDR_DIRECTPAD_END) ){
		aP0 = 40;
	}else{
		aP0 = aPath0 -1;
	}
	if(aP0 >= (dPORT_MAX_COUNT +1))return;
	
	//Erase 2 * 4K Blocks
	for(x=0; x<2; x++){
		do{
			sDelayMs(5);
			GetSta = nor_get_busy_state(0);
		}while(GetSta);
		fAddr = dFlashAddrDescpBase + (aP0 * 8192) + (4096 * x);
		nor_erase_sector_4k(0,fAddr);
		sDelayMs(45);
		do{
			sDelayMs(5);
			wP1 = nor_get_busy_state(0);
		}while(wP1);
	}
	
	//Write 2 Blocks Via 32 Pages * 8 Items 
	for(x=0; x<32; x++){
		do{
			sDelayMs(1);
			GetSta = nor_get_busy_state(0);
		}while(GetSta);
		m = aP0;
		fAddr = dFlashAddrDescpBase + (m * 8192) + (x * 256);
		sdAddr = dAddrSdRam_Descp + (m * 8192) + (x * 256);
		for(i=0; i<256;i++){
			ReadWriteData.D8[i] = *(volatile unsigned char *)(sdAddr +i);
		}
		nor_write_page(0, fAddr, ReadWriteData.D8);
		do{
			sDelayMs(1);
			wP1 = nor_get_busy_state(0);
		}while(wP1);
	}
	
	sReadFromFlash(aPath0);
}

void TDescpWrite::Write2FlashPreSet(unsigned char aPath0)
{
	unsigned int aP0;
	unsigned int aP1;
	unsigned int fAddr, sdAddr, GetSta;
	unsigned int i,m;
	unsigned int wP1;
	volatile unsigned int x;
	if( (aPath0 >= dADDR_DIRECTPAD_START) && (aPath0 <= dADDR_DIRECTPAD_END) ){
		aP0 = 40;
	}else{
		aP0 = aPath0 -1;
	}
	if(aP0 >= (dPORT_MAX_COUNT +1))return;
	
	//Erase 2 * 4K Blocks
	for(x=0; x<2; x++){
		do{
			sDelayMs(5);
			GetSta = nor_get_busy_state(0);
		}while(GetSta);
		fAddr = dFlashAddrDescpBase + (aP0 * 8192) + (4096 * x);
		nor_erase_sector_4k(0,fAddr);
		sDelayMs(45);
		do{
			sDelayMs(5);
			wP1 = nor_get_busy_state(0);
		}while(wP1);
	}
	
	//Write 2 Blocks Via 32 Pages * 8 Items 
	for(x=0; x<32; x++){
		do{
			sDelayMs(1);
			GetSta = nor_get_busy_state(0);
		}while(GetSta);
		m = aP0;
		fAddr = dFlashAddrDescpBase + (m * 8192) + (x * 256);
		sdAddr = dAddrSdRam_Descp + (m * 8192) + (x * 256);
		for(i=0; i<256;i++){
			ReadWriteData.D8[i] = *(volatile unsigned char *)(sdAddr +i);
		}
		nor_write_page(0, fAddr, ReadWriteData.D8);
		do{
			sDelayMs(1);
			wP1 = nor_get_busy_state(0);
		}while(wP1);
	}
	
	sReadFromFlash(aPath0);
}

void TDescpWrite::sReadFromFlash(unsigned char aPath0)
{
	unsigned int aP0;
	unsigned int aP1;
	unsigned int fAddr, sdAddr;
	unsigned int i;
	unsigned int wP1;
	volatile unsigned int x;
	if( (aPath0 >= dADDR_DIRECTPAD_START) && (aPath0 <= dADDR_DIRECTPAD_END) ){
		aP0 = 40;
	}else{
		aP0 = aPath0 -1;
	}
	if(aP0 >= (dPORT_MAX_COUNT +1))return;
	
	for(x=0; x<32; x++){
		//2 * 8192 
		fAddr = dFlashAddrDescpBase + (aP0 * 8192) + (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 = dAddrSdRam_Descp + (aP0 * 8192) + (x * 256);
		for(i=0; i<64; i++){
			*(volatile unsigned int *)(sdAddr + i *4) = ReadWriteData.D32[i];
		}
	}
}

void TDescpWrite::ReadPreSetStr()
{
	
}

void TDescpWrite::WritePreSetStr()
{
	
}



void TDescpWrite::Write2Flash()
{
	/*unsigned int fAddr, sdAddr;
	unsigned int i,m;
	volatile unsigned int x, wP1;
	
	do{
		DelayMs(5);
		wP1 = nor_get_busy_state(0);
	}while(wP1);
	
	//Erase 8 * 64K Blocks
	for(x=0; x<8; x++){
		do{
			DelayMs(20);
			wP1 = nor_get_busy_state(0);
		}while(wP1);
		fAddr = dFlashAddrDescpBase + (SizeOf64K * x);
		nor_erase_sector_64k(0,fAddr);
		do{
			DelayMs(50);
			wP1 = nor_get_busy_state(0);
		}while(wP1);
	}
	
	for(m=0; m<dPORT_MAX_COUNT; m++){ 
		TextDigitRender2Right24(300, 400 ,clNearBlack, Color, m+1);
		for(x=0; x<32; x++){
			fAddr = dFlashAddrDescpBase + (m * 8192) + (x * 256);
			sdAddr = dAddr_DescpBuf + (m * 8192) + (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);
			do{
				DelayMs(20);
				wP1 = nor_get_busy_state(0);
			}while(wP1);
		}
	}
	
	TextRender_string24(300, 400 ,clNearBlack, Color, "  ");
	
	ReadFromFlash();*/
}

void TDescpWrite::ReadFromFlash()
{
	/*unsigned int fAddr, sdAddr;
	unsigned int x,i,m;
	unsigned int wP1;
	
	for(m=0; m<dPORT_MAX_COUNT; m++){
		for(x=0; x<32; x++){
			fAddr = dFlashAddrDescpBase + (m * 8192) + (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 + (m * 8192) + (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_Descp + (m * 8192) + (x * 256);
			for(i=0; i<64;i++){
				*(volatile unsigned int *)(sdAddr + i*4) = ReadWriteData.D32[i];
			}
		}
	}*/
}














