#include "DevMask.h"
#include "Runtime.h"
#include "DirectPadCfg.h"

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

#define clOdd	clMoneyGreen
#define clEven	clWhiteBlue

#define clEnable	clDeepGreen
#define clDisable	clSilver

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

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

const short GridX [5] = {10, 160, 300, 500, 700};
const short GridX2[5] = {156, 296, 496, 696, 786};
const short GridY [10] = {86, 126, 496, 696, 786};
const short GridY2[10] = {156, 296, 496, 696, 786};

class TItemList *TDirectPadCfg::sIList;

void TDirectPadCfg::DrawFixText()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "多线盘");
		TextRender_string24(Lv.Grid.Left[0] + 200, Lv.Grid.Top[0] +6, clNearWhite, "盘使能");
		TextRender_string24(Lv.Grid.Left[0] + 320, Lv.Grid.Top[0] +6, clNearWhite, "机器号");
		TextRender_string24(Lv.Grid.Left[0] + 414, Lv.Grid.Top[0] +6, clNearWhite, "本机");
		TextRender_string24(Lv.Grid.Left[0] + 640, 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] + 280, Lv.Grid.Top[0] + 40, clNearWhite, "注释信息:");
		TextRender_string24(Lv.Grid.Left[0] + 160, Lv.Grid.Top[0] + 40, clNearWhite, "共");
		TextRender_string24(Lv.Grid.Left[0] + 196, Lv.Grid.Top[0] + 40, clNearWhite, "8点");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "二次码");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +40, clNearWhite, "分区号");
		TextRender_string24(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 6, clNearWhite, ItemColor, "定义类型");

		TextRender_string24(Lv.Grid.Left[3] +10, Lv.Grid.Top[3] + 4, clNearWhite, ItemColor, "故障检测");
		TextRender_string24(Lv.Grid.Left[3] +50, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "不检测故障");
		TextRender_string24(Lv.Grid.Left[4] +10, Lv.Grid.Top[4] + 4, clNearWhite, ItemColor, "启用状态");
		TextRender_string24(Lv.Grid.Left[4] +50, Lv.Grid.Top[4] + 36, clNearWhite, ItemColor, "启用");
		TextRender_string24(Lv.Grid.Left[4] +176, Lv.Grid.Top[4] + 36, clNearWhite, ItemColor, "禁止");		
	}else{
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "Brd ID");
		TextRender_string24(Lv.Grid.Left[0] + 200, Lv.Grid.Top[0] +6, clNearWhite, "Enabled");
		TextRender_string24(Lv.Grid.Left[0] + 320, Lv.Grid.Top[0] +6, clNearWhite, "CtlID");
		TextRender_string24(Lv.Grid.Left[0] + 414, Lv.Grid.Top[0] +6, clNearWhite, " Local");
		TextRender_string24(Lv.Grid.Left[0] + 640, Lv.Grid.Top[0] +6, clNearWhite, "State");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] + 40, clNearWhite, "Node");
		TextRender_string24(Lv.Grid.Left[0] + 280, Lv.Grid.Top[0] + 40, clNearWhite, "    Note:");
		TextRender_string24(Lv.Grid.Left[0] + 160, Lv.Grid.Top[0] + 40, clNearWhite, "8Pads");
		//TextRender_string24(Lv.Grid.Left[0] + 196, Lv.Grid.Top[0] + 40, clNearWhite, "8点");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "AuxID");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +40, clNearWhite, "Zone");
		TextRender_string24(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 6, clNearWhite, ItemColor, "Assign Type");	

		TextRender_string24(Lv.Grid.Left[3] +10, Lv.Grid.Top[3] + 4, clNearWhite, ItemColor, "Inspection");
		TextRender_string24(Lv.Grid.Left[3] +50, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "No Insp");
		TextRender_string24(Lv.Grid.Left[4] +10, Lv.Grid.Top[4] + 4, clNearWhite, ItemColor, "Enabled");
		TextRender_string24(Lv.Grid.Left[4] +50, Lv.Grid.Top[4] + 36, clNearWhite, ItemColor, "Enabled");
		TextRender_string24(Lv.Grid.Left[4] +176, Lv.Grid.Top[4] + 36, clNearWhite, ItemColor, "Disabled");			
	}
}

void TDirectPadCfg::Init(int aMode)
{
	IsPermission = 0;
	EdittingDescp = 0;
	ResultPst = 0;
	Tag.Reset();
	IsExtInput = 1;
	EnterMethod = aMode;
	ShowResultTick = 0;
	if(EnterMethod == 0){
		Tag.InxMin = 0;
	}else{
		Tag.InxMin = 1;
	}
	Tag.Inx = Tag.InxMin;
}

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

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

void TDirectPadCfg::FullRedraw(int Prm)
{
	IsShow = 1;
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], ContentColor);
		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);

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

		DrawIsPadDeActive();
		IList->STextState.Show();
		IList->EditPortNum.Show();
		IList->EditAddrNum.Show();
		IList->EditDescpShow.Show();

		IList->EditUserCode.Show();
		IList->EditAreaAssign.Show();

		IList->Edit[0].Show();
		IList->STextModuleInDefineType.Show();
		DrawPadBoardCfgList();
		
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
		if(EdittingDescp){}
	}
}

void TDirectPadCfg::DrawState()
{
	unsigned char aSta;
	if(Path0 && Path1 && (Path0 <= dDIRECTCTL_MAX_COUNT) && (Path1 <= dDIRECTCTL_KEY_MAX_COUNT) ){
		if(DirectCtlBoard[Path0-1].FaultState[Path1-1]){
			TCircle::sRender(IList->STextState.Bound.Left -12, IList->STextState.Bound.Top +14, 10, clOrange, 0xF);
			if(LanguageEnCn==0)IList->STextState.SetText("故障", 24); else IList->STextState.SetText("FLT.", 24);
		}else
		if(DirectCtlBoard[Path0-1].FeedbackState[Path1-1]){
			TCircle::sRender(IList->STextState.Bound.Left -12, IList->STextState.Bound.Top +14, 10, clBlue, 0xF);
			if(LanguageEnCn==0)IList->STextState.SetText("反馈", 24);else IList->STextState.SetText("FBCK", 24);
		}else
		if(DirectCtlBoard[Path0-1].OutputState[Path1-1]){
			TCircle::sRender(IList->STextState.Bound.Left -12, IList->STextState.Bound.Top +14, 10, clBlue, 0xF);
			if(LanguageEnCn==0)IList->STextState.SetText("启动", 24);else IList->STextState.SetText("ACT.", 24);
		}else{
			TCircle::sRender(IList->STextState.Bound.Left -12, IList->STextState.Bound.Top +14, 10, clDeepGreen, 0xF);
			if(LanguageEnCn==0)IList->STextState.SetText("正常", 24);else IList->STextState.SetText("IDLE", 24);
		}
	}else{
		TCircle::sRender(IList->STextState.Bound.Left -12, IList->STextState.Bound.Top +14, 10, ItemColor, 0xF);
		IList->STextState.SetText("----", 24);
	}
	IList->STextState.Show();
}

void TDirectPadCfg::DrawSelf(void)
{
	RenderBackGround();
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	if(LanguageEnCn==0){
		if(EnterMethod)
			TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "手动控制盘->信息->多线配置");
		else
			TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "手动控制盘->多线配置                        ");
	}else{
		if(EnterMethod)
			TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Manual Control Unit->Information->Direct Pad Boards Config");
		else
			TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Manual Control Unit->Direct Pad Boards Config             ");
	}
	
	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.PanelSmall.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	
	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);
	
	DrawFixText();
	
	IList->STextCtlNum.Init(Lv.Grid.Left[0] + 236, Lv.Grid.Top[0] + 6, 120, 30, 0, 0, clNearWhite, ItemColor);
	TextDigitRender2Right24(Lv.Grid.Left[0] + 392, Lv.Grid.Top[0] +6, clNearWhite, MainCtl.fData.Split.MyNum);
	IList->STextState.Init(Lv.Grid.Left[0] + 730, Lv.Grid.Top[0] + 2, 68, 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->EditDescpShow.Init(Lv.Grid.Left[0] + 390, Lv.Grid.Top[0] + 36, 400, 30, 0, 0, 1, clNearWhite);
	
	IList->STextDType.Enable = 1;
	IList->STextDType.Visible =1;
	IList->STextState.Enable = 1;
	IList->STextState.Visible =1;
	IList->STextRegisted.Enable = 1;
	IList->STextRegisted.Visible =1;
	//IList->STextCtlNum.Enable = 1;
	//IList->STextCtlNum.Visible =1;
	IList->EditPortNum.TextClear();
	IList->EditAddrNum.TextClear();
	IList->EditPortNum.SetMaxLen(2);
	IList->EditAddrNum.SetMaxLen(2);
	if(EnterMethod)
		IList->EditPortNum.Enable = 0;
	else
		IList->EditPortNum.Enable = 1;
	IList->EditPortNum.Visible = 1;
	IList->EditAddrNum.Enable = 1;
	IList->EditAddrNum.Visible = 1;
	IList->EditDescpShow.Enable = 1;
	IList->EditDescpShow.Visible = 1;
	
	//User Code & Area Assign
	IList->EditUserCode.Init(Lv.Grid.Left[1] + 84, Lv.Grid.Top[1] + 6, 108, 30, 0, 0, 1, clNearWhite);
	IList->EditAreaAssign.Init(Lv.Grid.Left[1] + 84, Lv.Grid.Top[1] + 40, 108, 30, 0, 0, 1, clNearWhite);
	
	IList->EditUserCode.Enable = 1;
	IList->EditUserCode.Visible =1;
	IList->EditUserCode.SetMaxLen(8);
	IList->EditAreaAssign.Enable = 1;
	IList->EditAreaAssign.Visible =1;
	IList->EditAreaAssign.SetMaxLen(4);
	
	IList->Edit[0].Init(Lv.Grid.Left[2] +146, Lv.Grid.Top[2] + 4, 48,30,0,0,1,clNearBlack);
	IList->Edit[0].SetMaxLen(3);
	IList->Edit[0].Str.Clear();
	IList->STextModuleInDefineType.Init(Lv.Grid.Left[2] +4, Lv.Grid.Top[2] + 40, 260,30,0,0,clNearWhite,ItemColor);
	//if(Path0Valid && Path1Valid){
	//	unsigned char tType = DirectCtlBoard[Path0-1].AssignType[Path1-1];
	//	IList->Edit[0].Str.FromUInt3Dg(tType);
	//	if(tType == 0){
	//		IList->STextModuleInDefineType.SetText("未指派类型" , 24);
	//	}else{
	//		IList->STextModuleInDefineType.SetText(StrTypeShortName[tType] , 24);
	//	}
	//}else{
	//	IList->STextModuleInDefineType.SetText("----" , 24);
	//}
	//IList->Edit[0].Show(); 
	//IList->STextModuleInDefineType.Show();
	
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	if(Lv.PanelSmall.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
}

void TDirectPadCfg::DrawPadBoardCfgList()
{
	int i, x, y;
	x = Lv.Panel.Left+6;
	y = Lv.Panel.Top +2;
	if(LanguageEnCn==0){
		TextRender_string24(x, y, clNearBlack, PanelColor, "节点  二次码    分区   故障检测   启用");
	}else{
		TextRender_string24(x, y, clNearBlack, PanelColor, "Node   AuxID    Zone   CheckLine  InUse");
	}
	int TopCnt = dDIRECTCTL_KEY_MAX_COUNT;
	if( TopCnt >8) TopCnt = 8;
	for(i=0; i<TopCnt; i++){
		x = Lv.Panel.Left+6;
		y+=28;
		TextDigitRender2Right24(x, y, clNearBlack, PanelColor, i+1);
		x+=60;
		List.TextReset();
		TMyString::sAddOn8Dg(DirectCtlBoard[Path0-1].UcList[i].Full, List.Text);
		TextRender_string24(x, y, clNearBlack, PanelColor, List.Text);
		x+=132;
		TextDigitRender4Right24(x, y, clNearBlack, PanelColor, DirectCtlBoard[Path0-1].Area[i]);
		x+=96;
		if(LanguageEnCn==0){
			if(DirectCtlBoard[Path0-1].IsCheckError[i]){
				TextRender_string24(x, y, clNearBlack, PanelColor, "不检测");
			}else{
				TextRender_string24(x, y, clNearBlack, PanelColor, " 开启 ");
			}
			x+=122;
			if(DirectCtlBoard[Path0-1].IsRegisted[i]){
				TextRender_string24(x, y, clNearBlack, PanelColor, "启用");
			}else{
				TextRender_string24(x, y, clNearBlack, PanelColor, " -- ");
			}
		}else{
			if(DirectCtlBoard[Path0-1].IsCheckError[i]){
				TextRender_string24(x, y, clNearBlack, PanelColor, "Ignore");
			}else{
				TextRender_string24(x, y, clNearBlack, PanelColor, " Insp ");
			}
			x+=122;
			if(DirectCtlBoard[Path0-1].IsRegisted[i]){
				TextRender_string24(x, y, clNearBlack, PanelColor, "Yes");
			}else{
				TextRender_string24(x, y, clNearBlack, PanelColor, " -- ");
			}
		}
	}
}

void TDirectPadCfg::ReLoadAndDrawDescp()
{
	unsigned char tP0, tP1, Ava, tType, tSta;
	unsigned int tAddr;

	Ava = 0;
	unsigned int aAddr, i;
	unsigned int p0, p1;
	unsigned char *p;
	
	if(Path0 <1)return;
	if(Path1 <1)return;

	if( Path0 <= dDIRECTCTL_MAX_COUNT ){
		if(Path1 <= dDIRECTCTL_KEY_MAX_COUNT){
			p0 = Path0 -1;
			p1 = (p0 * dDIRECTCTL_KEY_MAX_COUNT) + (Path1);
			p0 = dPORT_MAX_COUNT;
			aAddr = dAddrSdRam_Descp + (p0 * 8192) + (p1*32);
			for(i=0; i<32; i++){
				IList->EditDescpShow.Str.Text[i] = *(volatile unsigned char *)(aAddr + i);
			}
			IList->EditDescpShow.Str.Text[32] = '\0';
			IList->EditDescpShow.Str.Text[33] = '\0';
			IList->EditDescpShow.SetPstTail();
			IList->EditDescpShow.Show();
			IList->EditDescpShow.ShowCursor();
		}
	}
}

void TDirectPadCfg::DrawUserCodeAssignAreaAssignType()
{
	int iVal;
	IList->EditUserCode.Str.FromUInt8Dg(DirectCtlBoard[Path0-1].UcList[Path1-1].Full);
	IList->EditUserCode.Show();
	IList->EditAreaAssign.Str.FromUInt4Dg(DirectCtlBoard[Path0-1].Area[Path1-1]);
	IList->EditAreaAssign.Show();
	IList->Edit[0].Str.FromUInt3Dg(DirectCtlBoard[Path0-1].AssignType[Path1-1]);
	IList->Edit[0].Show();
	iVal = IList->Edit[0].Str.ToInteger();
	if( (iVal > 255) || (iVal <0) ){
		iVal = 0;
		IList->STextModuleInDefineType.SetText("                ",24);
	}else{
		if(LanguageEnCn==0)
			IList->STextModuleInDefineType.SetText(StrTypeShortName[iVal],24);
		else
			IList->STextModuleInDefineType.SetText(StrTypeShortNameEn[iVal],24);
	}
	IList->STextModuleInDefineType.Show();
}



void TDirectPadCfg::SetPath(unsigned char aCtlNum, unsigned char aPath0, unsigned char aPath1)
{
	unsigned char tP0, tP1, tT;
	//this->CtlNum = aCtlNum;
	if(aPath0 && (aPath0 <= dDIRECTCTL_MAX_COUNT)) {
		Path0Valid = 1;
		IList->EditPortNum.SelectedColor = clBlue;
	}else{
		Path0Valid = 0;
		IList->EditPortNum.SelectedColor = clRed;
	}
	if(aPath1 && (aPath1 <= dDIRECTCTL_KEY_MAX_COUNT)){
		Path1Valid = 1; 
		IList->EditAddrNum.SelectedColor = clBlue;
	}else{
		Path1Valid = 0;
		IList->EditAddrNum.SelectedColor = clRed;
	}
	this->Path0 = aPath0;
	this->Path1 = aPath1;
	
	IList->EditPortNum.Str.FromUInt2Dg(aPath0);
	IList->EditAddrNum.Str.FromUInt2Dg(aPath1);
	IList->EditPortNum.Show();
	IList->EditAddrNum.Show();
	
	DrawState();
	ReLoadAndDrawDescp();
	DrawUserCodeAssignAreaAssignType();
	LoadCfg4Edit(aPath0, aPath1);
	
	DrawPadBoardCfgList();
	
	Check4Selected();
}

void TDirectPadCfg::Show(void)
{
	DrawSelf();
	IsShow = 1;
}

void TDirectPadCfg::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 TDirectPadCfg::Hide()
{
	IsShow = 0;
}

void TDirectPadCfg::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 <= dDIRECTCTL_MAX_COUNT ){
		if(aP1 <= dDIRECTCTL_KEY_MAX_COUNT){
			p0 = aP0 - 1;
			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 TDirectPadCfg::Redraw4DescpEdit()
{
	IList->EditDescpShow.Selected = 1;
	IList->EditDescpShow.SetPstTail();
	IList->EditDescpShow.Show();
	
	RectFillRender(aPanel->Bound.Left, aPanel->Bound.Top, aPanel->Bound.Right, aPanel->Bound.Bottom, ContentColor);
	for(int i=1; i<4; 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.PanelSmall.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "二次码");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +40, clNearWhite, "分区号");
		TextRender_string24(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 6, clNearWhite, ItemColor, "定义类型");
		TextRender_string24(Lv.Grid.Left[3] +10, Lv.Grid.Top[3] + 4, clNearWhite, ItemColor, "故障检测");
		TextRender_string24(Lv.Grid.Left[3] +50, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "开启");
	}else{
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "AuxID");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +40, clNearWhite, "Zone");
		TextRender_string24(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 6, clNearWhite, ItemColor, "Assign Type");
		TextRender_string24(Lv.Grid.Left[3] +10, Lv.Grid.Top[3] + 4, clNearWhite, ItemColor, "Inspection");
		TextRender_string24(Lv.Grid.Left[3] +50, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "In USe");
	}
	IList->EditUserCode.Show();
	IList->EditAreaAssign.Show();
	
	IList->Edit[0].Show();
	IList->STextModuleInDefineType.Show();
	
	DrawISCheckFault();
	
	DrawPadBoardCfgList();
}

void TDirectPadCfg::PermissionCmdReturnDraw()
{
	int x,y;
	Check4Selected();
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	DrawPadBoardCfgList();
}

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

	x = 20;
	y = 150;
	x2 = 799;
	y2 = 340;
	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 To Switch Input Method,Key Set To Save",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 Numbers   ", aPanel->SText[0].Text);
	}
	aPanel->SText[0].Enable = 1;
	aPanel->SText[0].Visible = 1;
	
	aPanel->Edit[ 0].Init(10,40,420,30,x,y,1,clNearBlack);
	aPanel->Edit[ 0].Str.FromStr(IList->EditDescpShow.Str.Text);
	
	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->Show();
	aPanel->Caption.Show();
	aPanel->SText[0].Show();
	aPanel->Edit[0].Show();
	aPanel->Edit[0].ShowCursor();
	
	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);
		TMyString::sFromStr("                         ", aPanel->SText[0].Text);
	}
	aPanel->SText[0].Show();
}


void TDirectPadCfg::ClearSelected()
{
	switch(Tag.Inx){
		case 0:
			IList->EditPortNum.Selected = 0;
			IList->EditPortNum.Show();
			break;
		case 1:
			TRoundSquare::sSelected(Lv.Grid.Left[0] +170, Lv.Grid.Top[0] + 6, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 2:
			IList->EditAddrNum.Selected = 0;
			IList->EditAddrNum.Show();
			break;
		case 3:
			IList->EditDescpShow.Selected = 0;
			IList->EditDescpShow.Show();
			break;
		case 4:
			IList->EditUserCode.Selected = 0;
			IList->EditUserCode.Show();
			break;
		case 5:
			IList->EditAreaAssign.Selected = 0;
			IList->EditAreaAssign.Show();
			break;
		case 6:
			IList->Edit[0].Selected = 0;
			IList->Edit[0].Show();
			break;
		case 7:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +12, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 8:
			TRoundSquare::sSelected(Lv.Grid.Left[4] +12, Lv.Grid.Top[4] + 36, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 9:
			TRoundSquare::sSelected(Lv.Grid.Left[4] +140, Lv.Grid.Top[4] + 36, dWWidth, dWHeigh, 4, ItemColor);
			break;
	}
}

void TDirectPadCfg::Check4Selected()
{
	switch(Tag.Inx){
		case 0:
			IList->EditPortNum.Selected = 1;
			IList->EditPortNum.SetPstTail();
			IList->EditPortNum.Show();
			IList->EditPortNum.ShowCursor();
			break;
		case 1:
			TRoundSquare::sSelected(Lv.Grid.Left[0] +170, Lv.Grid.Top[0] + 6, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 2:
			IList->EditAddrNum.Selected = 1;
			IList->EditAddrNum.SetPstTail();
			IList->EditAddrNum.Show();
			IList->EditAddrNum.ShowCursor();
			break;
		case 3:
			IList->EditDescpShow.Selected = 1;
			IList->EditDescpShow.SetPstTail();
			IList->EditDescpShow.Show();
			IList->EditDescpShow.ShowCursor();
			break;
		case 4:
			IList->EditUserCode.Selected = 1;
			IList->EditUserCode.SetPstTail();
			IList->EditUserCode.Show();
			IList->EditUserCode.ShowCursor();
			break;
		case 5:
			IList->EditAreaAssign.Selected = 1;
			IList->EditAreaAssign.SetPstTail();
			IList->EditAreaAssign.Show();
			IList->EditAreaAssign.ShowCursor();
			break;
		case 6:
			IList->Edit[0].Selected = 1;
			IList->Edit[0].SetPstTail();
			IList->Edit[0].Show();
			IList->Edit[0].ShowCursor();
			break;
		case 7:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +12, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 8:
			TRoundSquare::sSelected(Lv.Grid.Left[4] +12, Lv.Grid.Top[4] + 36, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 9:
			TRoundSquare::sSelected(Lv.Grid.Left[4] +140, Lv.Grid.Top[4] + 36, dWWidth, dWHeigh, 4, clNearWhite);
			break;
	}	
}

TGuiMsgReturn TDirectPadCfg::KeyIn(unsigned char aKey)
{
	int ind,i,aIndex;
	unsigned char aP0;
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn SubRtnMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg = guiMsgNone;
	int HadPrc = 0;
	int iVal;
	JustShowResult = 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(EdittingDescp){
		const char *p;
		if(aKey == dfKEY_SETUP){
			//Save and return
			IList->EditDescpShow.Str.FromStr(this->aPanel->Edit[0].Str.Text);
			IList->EditDescpShow.Show();
			EdittingDescp = 0;
			Redraw4DescpEdit();
		}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();
					TMyString::sFromStr("当前快捷输入数字", aPanel->SText[0].Text);
					aPanel->SText[0].Show();
				}
			}else
			if(aKey == VK_RETURN){
				//Just return
				Redraw4DescpEdit();
				EdittingDescp = 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("                  ", aPanel->SText[0].Text);
						}
						aPanel->SText[0].Show();
					}
					break;
				case VK_EXECUTE:

					break;
				case VK_RETURN:
					Redraw4DescpEdit();
					EdittingDescp = 0;
					break;
			}
		}
	}else{
		switch(aKey){		
			case VK_0:
			case VK_1:
			case VK_2:
			case VK_3:
			case VK_4:
			case VK_5:
			case VK_6:
			case VK_7:
			case VK_8:
			case VK_9:
			case VK_DELETE:
			case VK_LEFT:
			case VK_RIGHT:
				if(Tag.Inx == 0){
					if(aKey == VK_RIGHT){
						if(IList->EditPortNum.Pst >= IList->EditPortNum.Str.GetEndPst()){
							ClearSelected();
							Tag.Inx = 1;
							Check4Selected();
							HadPrc = 1;
						}
					}
					if(HadPrc == 0){
						IList->EditPortNum.KeyIn(aKey);
						iVal = IList->EditPortNum.Str.ToInteger();
						if( (iVal) && (iVal <= dDIRECTCTL_MAX_COUNT) ){
							IList->EditPortNum.SelectedColor = clBlue;
							Path0Valid = 1;
						}else{
							IList->EditPortNum.SelectedColor = clRed;
							Path0Valid = 0;
						}
						if(Path0 != iVal){
							Path0 = iVal;
							DrawState();
							ReLoadAndDrawDescp();
							DrawUserCodeAssignAreaAssignType();
							LoadCfg4Edit(Path0, Path1);
							DrawPadBoardCfgList();
						}
						IList->EditPortNum.Show();
						IList->EditPortNum.ShowCursor();
					}
				}else
				if(Tag.Inx == 1){
					if(VK_LEFT == aKey){
						if(Tag.Inx > Tag.InxMin){
							ClearSelected();
							Tag.Inx = 0;
							Check4Selected();
						}
					}
				}else
				if(Tag.Inx == 2){
					if(aKey == VK_RIGHT){
						if(IList->EditAddrNum.Pst >= IList->EditAddrNum.Str.GetEndPst()){
							ClearSelected();
							Tag.Inx = 3;
							Check4Selected();
							HadPrc = 1;
						}
					}
					if(HadPrc == 0){
						IList->EditAddrNum.KeyIn(aKey);
						iVal = IList->EditAddrNum.Str.ToInteger();
						if( (iVal) && (iVal <= dDIRECTCTL_KEY_MAX_COUNT) ){
							IList->EditAddrNum.SelectedColor = clBlue;
							Path1Valid =1;
						}else{
							IList->EditAddrNum.SelectedColor = clRed;
							Path1Valid = 0;
						}
						if(Path1 != iVal){
							Path1 = iVal;
							DrawState();
							ReLoadAndDrawDescp();
							DrawUserCodeAssignAreaAssignType();
							LoadCfg4Edit(Path0, Path1);
						}
						IList->EditAddrNum.Show();
						IList->EditAddrNum.ShowCursor();
					}	
				}else
				if(Tag.Inx == 3){
					//DescpShow
					if(aKey == VK_LEFT){
						ClearSelected();
						Tag.Inx = 2;
						Check4Selected();
						HadPrc =1;
					}
				}else
				if(Tag.Inx == 4){
					//User Code
					IList->EditUserCode.KeyIn(aKey);
					IList->EditUserCode.Show();
					IList->EditUserCode.ShowCursor();
				}else
				if(Tag.Inx == 5){
					//Area 
					IList->EditAreaAssign.KeyIn(aKey);
					IList->EditAreaAssign.Show();
					IList->EditAreaAssign.ShowCursor();
				}else
				if(Tag.Inx == 6){
					//Define Type 
					IList->Edit[0].KeyIn(aKey);
					iVal = IList->Edit[0].Str.ToInteger();
					if( (iVal > 255) || (iVal <0) ){
						iVal = 0;
						IList->Edit[0].SelectedColor = clRed;
						IList->STextModuleInDefineType.SetText("                ",24);
					}else{
						IList->Edit[0].SelectedColor = clBlue;
						if(LanguageEnCn==0)
							IList->STextModuleInDefineType.SetText(StrTypeShortName[iVal],24);
						else
							IList->STextModuleInDefineType.SetText(StrTypeShortNameEn[iVal],24);
					}
					IList->Edit[0].Show();
					IList->Edit[0].ShowCursor();
					IList->STextModuleInDefineType.Show();
				}else
				if(Tag.Inx == 8){
					//Define Type 
					if(aKey == VK_RIGHT){
						ClearSelected();
						Tag.Inx = 9;
						Check4Selected();
					}
				}else
				if(Tag.Inx == 9){
					//Define Type 
					if(aKey == VK_LEFT){
						ClearSelected();
						Tag.Inx = 8;
						Check4Selected();
					}
				}
				break;
			case VK_UP:
				if(Tag.Inx > Tag.InxMin){
					ClearSelected();
					Tag.Inx--;
					Check4Selected();
				}else{
					ClearSelected();
					Tag.Inx =9;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(Tag.Inx < 9){
					ClearSelected();
					Tag.Inx++;
					Check4Selected();
				}else{
					ClearSelected();
					Tag.Inx = Tag.InxMin;
					Check4Selected();
				}
				break;
			case VK_TAB:

				break;
			case dfKEY_ENCN:

				break;
			case dfKEY_SETUP:	
				if(Tag.Inx == 1){
					Rt.IsPadDeActive = Rt.IsPadDeActive ? 0 : 1;
					DrawIsPadDeActive();
				}else
				if(Tag.Inx == 3){
					PermissionCmd = CmdEditDescp;
					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 == 7){
					Rt.IsCheckFault = Rt.IsCheckFault ? 0 : 1;
					DrawISCheckFault();
				}else
				if( (Tag.Inx == 8) || (Tag.Inx == 9) ){
					Rt.IsNodeUsing = Rt.IsNodeUsing ? 0 : 1;
					DrawIsNodeUsing();
				}
				break;
			case VK_F2:	

				break;
			case VK_EXECUTE:
				if( (Path0) && (Path1) && (Path0 <= dDIRECTCTL_MAX_COUNT) && (Path1 <=dDIRECTCTL_KEY_MAX_COUNT) ) {
					if(Tag.Inx == 1){
						//Save CFG - PadActive,
						ResultPst = 0;
						PermissionCmd = CmdWriteCfg;
						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 == 3){
						PermissionCmd = CmdWriteDescp;
						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 == 4){
						//Save UserCode
						PermissionCmd = CmdWriteUserCode;
						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 == 5){
						//Save Area Code
						PermissionCmd = CmdWriteAreaCode;
						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){
						//Save Assign Type
						PermissionCmd = CmdWriteAssignType;
						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 == 7){
						//Save CFG - 
						ResultPst = 3; 
						PermissionCmd = CmdWriteCfg;
						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 == 8) || (Tag.Inx == 9) ){
						//Save CFG - 
						ResultPst = 4; 
						PermissionCmd = CmdWriteCfg;
						if(SystemPermission >2){
							PermissionCmdGO();
						}else{
							IsPermission = 1;
							PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 3);
						}
					}
				}
				break;
		}
		
		if(aKey == VK_RETURN){
			IsShow = 0;
			aMsg = guiMsgReturn;
		}
	}
	
	if(JustShowResult ==0)TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "                ");
	return aMsg;
}

void TDirectPadCfg::PermissionCmdGO()
{
	int aVal, IsSaveFail;
	TUserCode tUserCode;
	IsSaveFail = 0;
	switch(PermissionCmd){
		case CmdEditDescp:
			IList->EditDescpShow.Selected = 0;
			IList->EditDescpShow.Show();
			InitPanel();
			EdittingDescp = 1;
			IsExtInput = 1;
			break;
		case CmdWriteUserCode:
		case CmdWriteAreaCode:
		case CmdWriteAssignType:
		case CmdWriteCfg:
		case CmdWriteDescp:
			if( Path0 && Path1 && (Path0 <= dDIRECTCTL_MAX_COUNT) && (Path1<=dDIRECTCTL_KEY_MAX_COUNT) ){
				aVal = IList->EditUserCode.Str.ToInteger();
				if( (aVal < 100000000) && (aVal>-1) ){
					tUserCode.Full = aVal;
					WriteUserCode2Flash(Path0, Path1, tUserCode);
					Record.ReadDirectPadUserSet(Path0);
					if(aVal != DirectCtlBoard[Path0-1].UcList[Path1-1].Full){
						IsSaveFail = 1;
					}
				}else{
					IsSaveFail =1;
				}
			}else{
				IsSaveFail =1;
			}
			if( Path0 && Path1 && (Path0 <= dDIRECTCTL_MAX_COUNT) && (Path1<=dDIRECTCTL_KEY_MAX_COUNT) ){
				aVal = IList->Edit[0].Str.ToInteger();
				if( (aVal < 256) && (aVal>-1) ){
					const unsigned char aAssignType = static_cast<unsigned char>(aVal);
					WriteAssignType2Flash(Path0, Path1, aAssignType);
					Record.ReadDirectPadUserSet(Path0);
					if(aVal != DirectCtlBoard[Path0-1].AssignType[Path1-1]){
						IsSaveFail = 2;
					}
				}else{
					IsSaveFail =2;
				}
			}else{
				IsSaveFail =2;
			}
			if(IsSaveFail ==1){
				if(LanguageEnCn==0){
					TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "用户码保存失败");
				}else{
					TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "Failed To Save AuxID     ");
				}
				ShowResultTick =5;
				JustShowResult = 1;
				return;
			}
			if(IsSaveFail ==2){
				if(LanguageEnCn==0){
					TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "类型保存失败  ");
				}else{
					TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "Failed To Save Type      ");
				}
				ShowResultTick =5;
				JustShowResult = 1;
				return;
			}
			IList->EditUserCode.Str.FromUInt8Dg(DirectCtlBoard[Path0-1].UcList[Path1-1].Full);
			IList->EditUserCode.Show();
			IList->Edit[0].Str.FromUInt3Dg(DirectCtlBoard[Path0-1].AssignType[Path1-1]);
			IList->Edit[0].Show();
			//___________________________________________________________________________________________________________
			if( Path0 && Path1 && (Path0 <= dDIRECTCTL_MAX_COUNT) && (Path1<=dDIRECTCTL_KEY_MAX_COUNT) ){
				aVal = IList->EditAreaAssign.Str.ToInteger();
				aVal &= 0x0000FFFF;
				if(aVal < dAreaCountMax){
					AreaNum = aVal;
					WriteAreaNum2Flash(Path0, Path1, AreaNum);
					Record.ReadDirectPadAreaNum();
					if(aVal != DirectCtlBoard[Path0-1].Area[Path1-1]){
						IsSaveFail = 1;						
					}
					IList->EditAreaAssign.Str.FromUInt4Dg(DirectCtlBoard[Path0-1].Area[Path1-1]);
					IList->EditAreaAssign.Show();
				}else{
					IsSaveFail =1;
				}
			}else{
				IsSaveFail =1;
			}
			if(IsSaveFail){
				if(LanguageEnCn==0){
					TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "区域号指派失败");
				}else{
					TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "Failed To Save Zone       ");
				}
				ShowResultTick =5;
				JustShowResult = 1;
				return;
			}
			Write2Flash(Path0, Path1);
			ReadDirectPadCfg(Path0);
			if( (Rt.IsPadDeActive == DirectCtlBoard[Path0-1].PadIsProhibit) && (Rt.IsNodeUsing == DirectCtlBoard[Path0-1].IsRegisted[Path1-1]) && (Rt.IsCheckFault == DirectCtlBoard[Path0-1].IsCheckError[Path1-1]) ){
				
			}else{
				if(LanguageEnCn==0){
					TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "配置保存失败  ");
				}else{
					TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "Failed To Save Configure  ");
				}
				ShowResultTick =5;
				JustShowResult = 1;
				IsSaveFail =1;
			}
			LoadCfg4Edit(Path0, Path1);
			DrawPadBoardCfgList();
			if(IsSaveFail)return;
			
			TMyString::sFromStr(IList->EditDescpShow.Str.Text, TextHasSave);
			sRestoreDescp(Path0, Path1, IList->EditDescpShow.Str.Text);
			WriteDescp2Flash();
			ReLoadAndDrawDescp();
			if(TMyString::sCompStr32Char(IList->EditDescpShow.Str.Text, TextHasSave) != 0){
				if(LanguageEnCn==0){
					TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "描述保存失败  ");
				}else{
					TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "Failed To Save Note      ");
				}
				ShowResultTick =5;
				IsSaveFail =1;
			}
			if(IsSaveFail)return;
			
			ShowResultTick =5;
			JustShowResult = 1;
			if(LanguageEnCn==0){
				TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "保存成功    ");
			}else{
				TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "Saved Completed                ");
			}
			break;
	}
}

void TDirectPadCfg::WriteDescp2Flash()
{
	TDescpWrite::sWrite2Flash(dADDR_DIRECTPAD_START);
}

void TDirectPadCfg::sWriteUserCodeAssignType(unsigned char aPath0)
{
	unsigned int fAddr;
	unsigned int i;
	volatile unsigned int x, GetSta;
	unsigned char aP0, aP1;
	TDMix tCrc1,tCrc2;
	
	if(sIList==nullptr)return;
	
	do{
		sDelayMs(3);
		GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
	}while(GetSta);

	//Erase 1 * 4K Blocks
	if( (aPath0 >0) && (aPath0 <= dDIRECTCTL_MAX_COUNT) ){
			aP0 = aPath0 -1;
			sIList->ReadWriteData.Reset();
			fAddr = dFlashAddrDirectPadUserCode + (aP0 * 4096);
			nor_erase_sector_4k(dFlashAddrDevUserCodeChip,fAddr);
			sDelayMs(100);
			
			do{
				sDelayMs(10);
				GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
			}while(GetSta);
			
			Crc32Reset();
			for(x=0;x<dDIRECTCTL_KEY_MAX_COUNT;x++){
				sIList->ReadWriteData.D32[x] = DirectCtlBoard[aP0].UcList[x].Full;
				Crc32Feed(sIList->ReadWriteData.D32[x]);
			}
			tCrc1.D32 = Crc32GetResult();
			
			Crc32Reset();
			for(x=0;x<dDIRECTCTL_KEY_MAX_COUNT;x++){
				sIList->ReadWriteData.D8[x + 32] = DirectCtlBoard[aP0].AssignType[x];
				Crc32Feed(static_cast<unsigned int>(sIList->ReadWriteData.D8[x + 32]));
			}
			tCrc2.D32 = Crc32GetResult();
			
			sIList->ReadWriteData.D8[32+8 +0] =tCrc1.D8[0];
			sIList->ReadWriteData.D8[32+8 +1] =tCrc1.D8[1];
			sIList->ReadWriteData.D8[32+8 +2] =tCrc1.D8[2];
			sIList->ReadWriteData.D8[32+8 +3] =tCrc1.D8[3];
			sIList->ReadWriteData.D8[32+8 +4] =tCrc2.D8[0];
			sIList->ReadWriteData.D8[32+8 +5] =tCrc2.D8[1];
			sIList->ReadWriteData.D8[32+8 +6] =tCrc2.D8[2];
			sIList->ReadWriteData.D8[32+8 +7] =tCrc2.D8[3];

			nor_write_page(dFlashAddrDevUserCodeChip, fAddr, sIList->ReadWriteData.D8);
			do{
				sDelayMs(5);
				GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
			}while(GetSta);

			do{
				sDelayMs(5);
				GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
			}while(GetSta);
	}	
}

void TDirectPadCfg::WriteUserCode2Flash(unsigned char aPath0, unsigned char aPath1, TUserCode aUc)
{
	unsigned char aP0, aP1;
	
	//Erase 1 * 4K Blocks
	if( (aPath0 >0) && (aPath0 <= dDIRECTCTL_MAX_COUNT) ){
		if( (aPath1 >0) && (aPath1 <= dDIRECTCTL_KEY_MAX_COUNT) ){
			aP0 = aPath0 -1;
			aP1 = aPath1 -1;
			DirectCtlBoard[aP0].UcList[aP1].Full = aUc.Full;
			sIList = IList;
			sWriteUserCodeAssignType(aPath0);
		}
	}	
}

void TDirectPadCfg::WriteAssignType2Flash(unsigned char aPath0, unsigned char aPath1, unsigned char aAssignType)
{
	unsigned char aP0, aP1;

	//Erase 1 * 4K Blocks
	if( (aPath0 >0) && (aPath0 <= dDIRECTCTL_MAX_COUNT) ){
		if( (aPath1 >0) && (aPath1 <= dDIRECTCTL_KEY_MAX_COUNT) ){
			aP0 = aPath0 -1;
			aP1 = aPath1 -1;
			DirectCtlBoard[aP0].AssignType[aP1] = aAssignType;
			sIList = IList;
			sWriteUserCodeAssignType(aPath0);
		}
	}	
}

void TDirectPadCfg::sWrite2Flash(unsigned char aPath0)
{
	unsigned int fAddr;
	volatile unsigned int x, GetSta;
	unsigned char aP0, aP1;
	TDMix tCrc;
	
	if(sIList==nullptr)return;
	sIList->ReadWriteData.Reset();
	
	if(aPath0 == 0)return;
	if(aPath0 > dDIRECTCTL_MAX_COUNT)return;

	do{
		sDelayMs(3);
		GetSta = nor_get_busy_state(dFlashDirectPadLnkChipX);
	}while(GetSta);
	
	//Erase 1 * 4K Blocks
	aP0 = aPath0 -1;
	fAddr = dFlashAddrDirectPadLnk + (aP0 * 4096);
	nor_erase_sector_4k(dFlashDirectPadLnkChipX,fAddr);
	sDelayMs(100);
	
	do{
		sDelayMs(10);
		GetSta = nor_get_busy_state(dFlashDirectPadLnkChipX);
	}while(GetSta);
	Crc32Reset();
	for(x=0;x<dDIRECTCTL_KEY_MAX_COUNT;x++){
		sIList->ReadWriteData.D8[x*8 +0] = DirectCtlBoard[aP0].IsRegisted[x];
		sIList->ReadWriteData.D8[x*8 +1] = DirectCtlBoard[aP0].IsCheckError[x];
		sIList->ReadWriteData.D8[x*8 +2] = 0;
		sIList->ReadWriteData.D8[x*8 +3] = 0;
		sIList->ReadWriteData.D8[x*8 +4] = 0;
		sIList->ReadWriteData.D8[x*8 +5] = 0;
		sIList->ReadWriteData.D8[x*8 +6] = 0;
		sIList->ReadWriteData.D8[x*8 +7] = DirectCtlBoard[aP0].PadIsProhibit;
		
		Crc32Feed(static_cast<unsigned int>(DirectCtlBoard[aP0].IsRegisted[x]));
		Crc32Feed(static_cast<unsigned int>(DirectCtlBoard[aP0].IsCheckError[x]));
		Crc32Feed(static_cast<unsigned int>(DirectCtlBoard[aP0].PadIsProhibit));
	}
	tCrc.D32 = Crc32GetResult();
	sIList->ReadWriteData.D32[dDIRECTCTL_KEY_MAX_COUNT*2+0] = tCrc.D32;
	fAddr = dFlashAddrDirectPadLnk + (aP0 * 4096);
	nor_write_page(dFlashDirectPadLnkChipX, fAddr, sIList->ReadWriteData.D8);
	do{
		sDelayMs(5);
		GetSta = nor_get_busy_state(dFlashDirectPadLnkChipX);
	}while(GetSta);	
}

void TDirectPadCfg::Write2Flash(unsigned char aPath0, unsigned char aPath1)
{
	unsigned int fAddr;
	volatile unsigned int x, GetSta;
	unsigned char aP0, aP1;
	TDMix tCrc;
	
	if(aPath0 == 0)return;
	if(aPath0 > dDIRECTCTL_MAX_COUNT)return;
	if(aPath1 == 0)return;
	if(aPath1 > dDIRECTCTL_KEY_MAX_COUNT)return;
	
	aP0 = aPath0-1;
	aP1 = aPath1-1;
	
	DirectCtlBoard[aP0].IsRegisted[aP1] = Rt.IsNodeUsing;;
	DirectCtlBoard[aP0].IsCheckError[aP1] = Rt.IsCheckFault;
	//DirectCtlBoard[aP0].IsPump[aP1]=0;
	DirectCtlBoard[aP0].IsProhibitLinkage[aP1]=0;
	DirectCtlBoard[aP0].IsOutputState[aP1]=0;
	DirectCtlBoard[aP0].PadIsProhibit = Rt.IsPadDeActive;
	
	sIList=IList;
	sWrite2Flash(aPath0);
}

void TDirectPadCfg::ReadDirectPadCfg(unsigned char aPath0)
{
	Record.LoadDirectPadCfg(aPath0);
}

void TDirectPadCfg::DrawIsPadDeActive()
{
	if(Rt.IsPadDeActive){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[0] + 170, Lv.Grid.Top[0] + 6, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);		
	}else{	
		TRoundSquare::sDrawBySize(Lv.Grid.Left[0] + 170, Lv.Grid.Top[0] + 6, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}
}

void TDirectPadCfg::DrawIsNodeUsing()
{
	if(Rt.IsNodeUsing){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[4] + 12, Lv.Grid.Top[4] + 36, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[4] + 140, Lv.Grid.Top[4] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);		
	}else{	
		TRoundSquare::sDrawBySize(Lv.Grid.Left[4] + 12, Lv.Grid.Top[4] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[4] + 140, Lv.Grid.Top[4] + 36, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}	
}

void TDirectPadCfg::DrawISCheckFault()
{
	if(Rt.IsCheckFault){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 12, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdClr , 1);	
	}else{	
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 12, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}	
}

void TDirectPadCfg::LoadCfg4Edit(unsigned char aPath0, unsigned aPath1)
{
	unsigned char aP0, aP1;
	if(aPath0 == 0)return;
	if(aPath0 > dDIRECTCTL_MAX_COUNT)return;
	if(aPath1 == 0)return;
	if(aPath1 > dDIRECTCTL_KEY_MAX_COUNT)return;
	
	aP0 = aPath0 -1;
	aP1 = aPath1 -1;
	
	Rt.IsPadDeActive = DirectCtlBoard[aP0].PadIsProhibit;
	Rt.IsNodeUsing = DirectCtlBoard[aP0].IsRegisted[aP1];
	Rt.IsCheckFault = DirectCtlBoard[aP0].IsCheckError[aP1];
	
	DrawIsPadDeActive();
	DrawIsNodeUsing();
	DrawISCheckFault();
}

void TDirectPadCfg::sWriteAreaNum()
{
	unsigned int fAddr;
	unsigned int i;
	unsigned int aUcFull;
	volatile unsigned int x, GetSta;
	unsigned char aP0, aP1, wP1;
	TDMix tCrc;
	
	do{
		sDelayMs(3);
		GetSta = nor_get_busy_state(dFlash4DevAreaNumChip);
	}while(GetSta);
	
	if(sIList==nullptr)return;
	sIList->ReadWriteData.Reset();
	
	//Erase 1 * 4K Blocks
	fAddr = dFlash4DevAreaNum + (40 / 8 * 4096);
	nor_erase_sector_4k(dFlash4DevAreaNumChip,fAddr);
	sDelayMs(100);
	
	LoopCheck1S();
	
	do{
		sDelayMs(10);
		GetSta = nor_get_busy_state(dFlash4DevAreaNumChip);
	}while(GetSta);
	for(i=0; i<10; i++){
		Crc32Reset();
		for(x=0;x<dDIRECTCTL_KEY_MAX_COUNT;x++){
			sIList->ReadWriteData.D16[i*(dDIRECTCTL_KEY_MAX_COUNT+2) + x] = DirectCtlBoard[i].Area[x];
			Crc32Feed(static_cast<unsigned int>(DirectCtlBoard[i].Area[x]));
		}
		tCrc.D32 = Crc32GetResult();
		sIList->ReadWriteData.D16[i*(dDIRECTCTL_KEY_MAX_COUNT+2) + dDIRECTCTL_KEY_MAX_COUNT +0] = tCrc.D16[0];
		sIList->ReadWriteData.D16[i*(dDIRECTCTL_KEY_MAX_COUNT+2) + dDIRECTCTL_KEY_MAX_COUNT +1] = tCrc.D16[1];
		
	}
	fAddr = dFlash4DevAreaNum + (40 * 512) + (0 * 256);
	nor_write_page(dFlash4DevAreaNumChip, fAddr, sIList->ReadWriteData.D8);
	do{
		sDelayMs(5);
		GetSta = nor_get_busy_state(dFlash4DevAreaNumChip);
	}while(GetSta);
		
	for(i=10; i<20; i++){
		Crc32Reset();
		for(x=0;x<dDIRECTCTL_KEY_MAX_COUNT;x++){
			sIList->ReadWriteData.D16[(i-10)*(dDIRECTCTL_KEY_MAX_COUNT+2) + x] = DirectCtlBoard[i].Area[x];
			Crc32Feed(static_cast<unsigned int>(DirectCtlBoard[i].Area[x]));
		}
		tCrc.D32 = Crc32GetResult();
		sIList->ReadWriteData.D16[(i-10)*(dDIRECTCTL_KEY_MAX_COUNT+2) + dDIRECTCTL_KEY_MAX_COUNT +0] = tCrc.D16[0];
		sIList->ReadWriteData.D16[(i-10)*(dDIRECTCTL_KEY_MAX_COUNT+2) + dDIRECTCTL_KEY_MAX_COUNT +1] = tCrc.D16[1];
	}
	fAddr = dFlash4DevAreaNum + (40 * 512) + (1 * 256);
	nor_write_page(dFlash4DevAreaNumChip, fAddr, sIList->ReadWriteData.D8);
	do{
		sDelayMs(5);
		GetSta = nor_get_busy_state(dFlash4DevAreaNumChip);
	}while(GetSta);
}

void TDirectPadCfg::WriteAreaNum2Flash(unsigned char aPath0, unsigned char aAddr, unsigned short aNum)
{
	unsigned char aP0, aP1;
	
	if(aPath0 <1)return;
	if(aPath0 >dDIRECTCTL_MAX_COUNT)return;
	if(aAddr <1)return;
	if(aAddr >dDIRECTCTL_KEY_MAX_COUNT)return;
	
	aP0 = aPath0 -1;
	aP1 = aAddr -1;
	DirectCtlBoard[aP0].Area[aP1] = aNum;
	sIList = IList;
	sWriteAreaNum();
}

void TDirectPadCfg::Task1000Ms()
{
	if(IsShow){
		DrawState();
		if(ShowResultTick){
			ShowResultTick--;
			if(ShowResultTick == 0){
				TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "                              ");
			}
		}
	}
}


void TDirectPadCfg::sRecoverSetDefault()
{
	unsigned int fAddr;
	unsigned int i;
	volatile unsigned int x, GetSta;
	
	SysLoopTick = 0;
	do{
		sDelayMs(3);
		GetSta = nor_get_busy_state(dFlash4DevAreaNumChip);
	}while(GetSta);
	
	//Cfg
	for(i=0; i<dDIRECTCTL_MAX_COUNT; i++){
		SysLoopTick = 0;
		for(x=0;x<dDIRECTCTL_KEY_MAX_COUNT;x++){
			DirectCtlBoard[i].IsRegisted[x] =0;
			DirectCtlBoard[i].IsCheckError[x] =0;
			DirectCtlBoard[i].IsPump[x] =0;
			DirectCtlBoard[i].IsProhibitLinkage[x] =0;
			DirectCtlBoard[i].IsOutputState[x] =0;
		}
		DirectCtlBoard[i].PadIsProhibit =1;
		DirectCtlBoard[i].PadHasConfig =0;
		sWrite2Flash(i+1);
	}
	Record.LoadDirectPadCfg();
	
	for(i=1;i<=dDIRECTCTL_MAX_COUNT; i++){
		LoopCheck1S();
		for(x=0; x<dDIRECTCTL_KEY_MAX_COUNT; x++){
			DirectCtlBoard[i-1].UcList[x].Full=0;
			DirectCtlBoard[i-1].AssignType[x]=0;
			DirectCtlBoard[i-1].Area[x]=0;
		}
		sWriteUserCodeAssignType(i);
	}
	Record.ReadDirectPadUserSet();
	
	//Area Num
	sWriteAreaNum();
	Record.ReadDirectPadAreaNum();
}

int TDirectPadCfg::ExtRequst(unsigned char Prm)
{
	IsShow = 0;
	return 0;
}






