#include "RuntimeData.h"
#include "BusPadLnk.h"
#include "DevMask.h"

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

#define clEnable	clDeepGreen
#define clDisable	clSilver

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

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

#define dAddr_DescpBuf	dAddrSdRam_Commom

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

static const char CharBcdText[20]= {"0123456789*------\0"};

const short AddrLeft[8] = {20,125,260,440,620,630,640,650};
const short AddrTop[8] = {88, 130,172,214,256,298,252,296};

class TItemList *TBusPadLnk::sIList;

void TBusPadLnk::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] + 160, Lv.Grid.Top[0] + 40, clNearWhite, "共");
		TextRender_string24(Lv.Grid.Left[0] + 236, Lv.Grid.Top[0] + 40, clNearWhite, "点");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "匹配方式");
		TextRender_string24(Lv.Grid.Left[1] + 40, Lv.Grid.Top[1] +40, clNearWhite, "分区");
		TextRender_string24(Lv.Grid.Left[1] + 164, Lv.Grid.Top[1] +40, clNearWhite, "二次码");
		//User Code & Area Assign	
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite,  "受控设备二次码");
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +38, clNearWhite, "受控设备分区号");
		TextRender_string24(Lv.Grid.Left[3] +10, Lv.Grid.Top[3] + 6, clNearWhite, ItemColor, "受控设备类型");
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +60, Lv.Grid.Top[4] + 6, 76, 30, 7, clNearWhite, "保存", 14, ItemColor);
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +150, Lv.Grid.Top[4] + 6, 76, 30, 7, clNearWhite, "清除", 14, ItemColor);
	}else{
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "BusBrd");
		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] + 160, Lv.Grid.Top[0] + 40, clNearWhite, "共");
		TextRender_string24(Lv.Grid.Left[0] + 236, Lv.Grid.Top[0] + 40, clNearWhite, "Pads");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "Matching Method");
		TextRender_string24(Lv.Grid.Left[1] + 40, Lv.Grid.Top[1] +40, clNearWhite, "Zone");
		TextRender_string24(Lv.Grid.Left[1] + 164, Lv.Grid.Top[1] +40, clNearWhite, "AuxID");
		//User Code & Area Assign	
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite,  "Matching AuxID");
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +38, clNearWhite, "Matching Zone");
		TextRender_string24(Lv.Grid.Left[3] +10, Lv.Grid.Top[3] + 6, clNearWhite, ItemColor, "Matching Dev Type");
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +60, Lv.Grid.Top[4] + 6, 76, 30, 7, clNearWhite, "Save", 14, ItemColor);
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +150, Lv.Grid.Top[4] + 6, 76, 30, 7, clNearWhite, "Del", 14, ItemColor);
	}
}

void TBusPadLnk::Init(u8 aMode)
{
	IsPermission = 0;
	IsViewList = 0;
	ResultPst = 0;
	Tag.Reset();
	EnterMethod = aMode;
	ShowwingResult = 0;
	JustShowResult = 0;
	if(EnterMethod == 0){
		Tag.InxMin = 0;
	}else{
		Tag.InxMin = 1;
	}
	Tag.Inx = Tag.InxMin;
}

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

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


void TBusPadLnk::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->Bus Pad Boards Config");
		else
			TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Manual Control Unit->Bus 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);

	//TextRender_string24(Lv.Grid.Left[0] + 280, Lv.Grid.Top[0] + 40, clNearWhite, "分区号注释信息:");
	
	IList->STextState.Init(Lv.Grid.Left[0] + 730, Lv.Grid.Top[0] + 2, 68, 30, 0, 0, clNearWhite, ItemColor);

	IList->Edit[1].Init(Lv.Grid.Left[0] +186, Lv.Grid.Top[0] + 38, 48,30,0,0,1,clNearBlack);
	IList->Edit[1].SetMaxLen(3);
	IList->Edit[1].Str.Clear();

	
	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(3);
	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;
	
	IList->EditUserCode.Init(Lv.Grid.Left[2] + 178, Lv.Grid.Top[2] + 6, 108, 30, 0, 0, 1, clNearWhite);
	IList->EditAreaAssign.Init(Lv.Grid.Left[2] + 178, Lv.Grid.Top[2] + 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(5);
	

	IList->Edit[0].Init(Lv.Grid.Left[3] +236, Lv.Grid.Top[3] + 4, 48,30,0,0,1,clNearBlack);
	IList->Edit[0].SetMaxLen(3);
	IList->Edit[0].Str.Clear();
	IList->STextModuleInDefineType.Init(Lv.Grid.Left[3] +4, Lv.Grid.Top[3] + 40, 160,30,0,0,clNearWhite,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);
}

void TBusPadLnk::DrawKeyCount()
{
	if( (Rt.KeyCount != 48) && (Rt.KeyCount != 64) && (Rt.KeyCount != 128) ){
		Rt.KeyCount = 48;
	}
	IList->Edit[1].Str.FromUInt3Dg(Rt.KeyCount);
	IList->Edit[1].Show();
}

void TBusPadLnk::Show()
{
	DrawSelf();
}

void TBusPadLnk::FullRedraw(int Prm)
{
	int i, x, y;
	if(Prm == 0){
		Show();
	}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);
		DrawFixText();
		TextDigitRender2Right24(Lv.Grid.Left[0] + 392, Lv.Grid.Top[0] +6, clNearWhite, MainCtl.fData.Split.MyNum);
		IList->STextState.Show();
		IList->Edit[1].Show();
		IList->EditPortNum.Show();
		IList->EditAddrNum.Show();
		DrawIsPadDeActive();
		//Match mode
		DrawMatchMode();
		//User Code & Area Assign	
		IList->EditUserCode.Show();
		IList->EditAreaAssign.Show();	
		DrawPadBoardCfgList();
		IsViewList ? ClearSelected() : Check4Selected();
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}

void TBusPadLnk::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 TBusPadLnk::DrawMatchMode()
{
	if(Rt.IsMatchAreaAssign){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 12, Lv.Grid.Top[1] + 38, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 136, Lv.Grid.Top[1] + 38, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);		
	}else{	
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 12, Lv.Grid.Top[1] + 38, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 136, Lv.Grid.Top[1] + 38, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}		
}

void TBusPadLnk::DrawState()
{
	unsigned char aSta;
	if(PadNum && KeyNum && (PadNum <= dHANDCTL_MAX_COUNT) && (KeyNum <= dHANDCTL_KEY_MAX_COUNT) ){
		if(HandCtlBoard[PadNum-1].KeyFbState[KeyNum-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(HandCtlBoard[PadNum-1].KeyState[KeyNum-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 TBusPadLnk::ReLoadAndDrawAreaDescp()
{
	unsigned char tP0, tP1, Ava, tType, tSta;
	unsigned int tAddr;

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

	if( PadNum <= dHANDCTL_MAX_COUNT ){
		if(KeyNum <= dHANDCTL_KEY_MAX_COUNT){
			p0 = PadNum -1;
			p1 = (p0 * dHANDCTL_KEY_MAX_COUNT) + (KeyNum);
			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 TBusPadLnk::DrawUserCodeAssignAreaAssignType()
{
	int iVal;
	if( (PadNum == 0) || (KeyNum ==0) || (PadNum >dHANDCTL_MAX_COUNT) || (KeyNum >dHANDCTL_KEY_MAX_COUNT) ) {
		IList->EditUserCode.Str.FromStr("--------");
		IList->EditUserCode.Show();
		IList->EditAreaAssign.Str.FromStr("----");
		IList->EditAreaAssign.Show();
		IList->Edit[0].Str.FromStr("---");
		IList->Edit[0].Show();
		IList->STextModuleInDefineType.SetText("                ",24);
		IList->STextModuleInDefineType.Show();

	}else{		
		LoadUcBcdText(PadNum-1,KeyNum-1);
		IList->EditUserCode.Show();
		IList->EditAreaAssign.Str.FromUInt4Dg(HandCtlBoard[PadNum-1].Area[KeyNum-1]);
		IList->EditAreaAssign.Show();
		AssignType = HandCtlBoard[PadNum-1].AssignType[KeyNum-1];
		IList->Edit[0].Str.FromUInt3Dg(AssignType);
		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 TBusPadLnk::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 TBusPadLnk::LoadCfg4Edit(unsigned char aPath0, unsigned aPath1)
{
	unsigned char aP0, aP1;
	if(aPath0 == 0)return;
	if(aPath0 > dHANDCTL_MAX_COUNT)return;
	if(aPath1 == 0)return;
	if(aPath1 > dHANDCTL_KEY_MAX_COUNT)return;
	
	aP0 = aPath0 -1;
	aP1 = aPath1 -1;
	
	
	AssignType = HandCtlBoard[aP0].AssignType[aP1];
	Rt.IsPadDeActive = HandCtlBoard[aP0].PadIsProhibit;
	Rt.IsMatchAreaAssign = HandCtlBoard[aP0].IsMatchAreaAssign[aP1];
	Rt.KeyCount = HandCtlBoard[aP0].KeyCount;
	
	LoadUcBcdText(aP0, aP1);
	
	DrawKeyCount();
	DrawIsPadDeActive();
	DrawMatchMode();
}

void TBusPadLnk::DrawPadBoardCfgList()
{
	int i, z, x, y;
	
	unsigned int aClr;
	IsViewList ? aClr=clBlue : 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);
	
	if(PadNum==0) return;
	if(PadNum > dHANDCTL_MAX_COUNT) return;
	
	x = Lv.Panel.Left+6;
	y = Lv.Panel.Top +6;
	if(LanguageEnCn==0){
		TextRender_string24(x, y, clNearBlack, PanelColor, "节点  二次码  分区号  匹配方式  设备类型");
	}else{
		TextRender_string24(x, y, clNearBlack, PanelColor, "Node   AuxID   Zone   M-Method  Dev Type");
	}
	int TopCnt = dHANDCTL_KEY_MAX_COUNT;
	for(i=0,z=List.CurrentPage*8; (i<8) && (z<TopCnt); i++, z++){
		z<Rt.KeyCount ? aClr=clNearBlack : aClr=clNearWhite;
		x = Lv.Panel.Left+6;
		y+=28;
		TextDigitRender3Right24(x, y, aClr, PanelColor, z+1);
		x+=60;
		List.TextReset();
		unsigned int f, aOrd;
		for(int g=0; g<8; g++){
			f = HandCtlBoard[PadNum-1].UcList[z].Full;
			f >>= ((7-g)*4);
			aOrd = (f & 0x0000'000F);
			List.Text[g] = CharBcdText[aOrd];
		}
		List.Text[8] = '\0';
		TextRender_string24(x, y, aClr, PanelColor, List.Text);
		x+=120;
		TextDigitRender4Right24(x, y, aClr, PanelColor, HandCtlBoard[PadNum-1].Area[z]);
		x+=96;
		if(LanguageEnCn==0){
			if(HandCtlBoard[PadNum-1].IsMatchAreaAssign[z]){
				TextRender_string24(x, y, aClr, PanelColor, " 分区 ");
			}else{
				TextRender_string24(x, y, aClr, PanelColor, "二次码");
			}
		}else{
			if(HandCtlBoard[PadNum-1].IsMatchAreaAssign[z]){
				TextRender_string24(x, y, aClr, PanelColor, " Zone ");
			}else{
				TextRender_string24(x, y, aClr, PanelColor, "AuxID");
			}
		}
		x+=136;
		TextDigitRender3Right24(x,y,aClr, PanelColor, HandCtlBoard[PadNum-1].AssignType[z]);
	}
}

void TBusPadLnk::SetPath(unsigned char aCtlNum, unsigned char aPath0, unsigned char aPath1)
{
	unsigned char tP0, tP1, tT;
	//this->CtlNum = aCtlNum;
	//#define dHANDCTL_MAX_COUNT		20
	//#define dHANDCTL_KEY_MAX_COUNT	128
	if(aPath0 && (aPath0 <= dHANDCTL_MAX_COUNT)) {
		PadNumValid = 1;
		IList->EditPortNum.SelectedColor = clBlue;
	}else{
		PadNumValid = 0;
		IList->EditPortNum.SelectedColor = clRed;
	}
	if(aPath1 && (aPath1 <= dHANDCTL_KEY_MAX_COUNT)){
		KeyNumValid = 1; 
		IList->EditAddrNum.SelectedColor = clBlue;
	}else{
		KeyNumValid = 0;
		IList->EditAddrNum.SelectedColor = clRed;
	}
	this->PadNum = aPath0;
	this->KeyNum = aPath1;
	
	IList->EditPortNum.Str.FromUInt2Dg(aPath0);
	IList->EditAddrNum.Str.FromUInt3Dg(aPath1);
	IList->EditPortNum.Show();
	IList->EditAddrNum.Show();
	
	LoadCfg4Edit(aPath0, aPath1);
	DrawState();
	//ReLoadAndDrawAreaDescp();
	DrawUserCodeAssignAreaAssignType();
	List.PageCount = dHANDCTL_KEY_MAX_COUNT / 8;
	List.CurrentPage = (KeyNum-1) / 8;
	if(List.CurrentPage > (dHANDCTL_KEY_MAX_COUNT / 8))List.CurrentPage =0;
	DrawPadBoardCfgList();
	
	Check4Selected();							
}

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

void TBusPadLnk::ClearSelected(void)
{
	char *tText;
	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->Edit[1].Selected = 0;
			IList->Edit[1].Show();
			break;
		case 4:
			TRoundSquare::sSelected(Lv.Grid.Left[1] +12, Lv.Grid.Top[1] + 38, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 5:
			TRoundSquare::sSelected(Lv.Grid.Left[1] +136, Lv.Grid.Top[1] + 38, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 6:
			IList->EditUserCode.Selected = 0;
			IList->EditUserCode.Show();
			break;
		case 7:
			IList->EditAreaAssign.Selected = 0;
			IList->EditAreaAssign.Show();
			break;
		case 8:
			IList->Edit[0].Selected = 0;
			IList->Edit[0].Show();
			break;
		case 9:
			if(LanguageEnCn==0)tText= "保存";else tText="Save";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +60, Lv.Grid.Top[4] + 6, 76, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 10:
			if(LanguageEnCn==0)tText= "清除";else tText="Del ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +150, Lv.Grid.Top[4] + 6, 76, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		
	}
}

void TBusPadLnk::Check4Selected(void)
{
	char *tText;
	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->Edit[1].Selected = 1;
			IList->Edit[1].SetPstTail();
			IList->Edit[1].Show();
			IList->Edit[1].ShowCursor();
			break;
		case 4:
			TRoundSquare::sSelected(Lv.Grid.Left[1] +12, Lv.Grid.Top[1] + 38, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 5:
			TRoundSquare::sSelected(Lv.Grid.Left[1] +136, Lv.Grid.Top[1] + 38, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 6:
			IList->EditUserCode.Selected = 1;
			IList->EditUserCode.SetPstTail();
			IList->EditUserCode.Show();
			IList->EditUserCode.ShowCursor();
			break;
		case 7:
			IList->EditAreaAssign.Selected = 1;
			IList->EditAreaAssign.SetPstTail();
			IList->EditAreaAssign.Show();
			IList->EditAreaAssign.ShowCursor();
			break;
		case 8:
			IList->Edit[0].Selected = 1;
			IList->Edit[0].SetPstTail();
			IList->Edit[0].Show();
			IList->Edit[0].ShowCursor();
			break;
		case 9:
			if(LanguageEnCn==0)tText= "保存";else tText="Save";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +60, Lv.Grid.Top[4] + 6, 76, 30, 7, clDeepBlue, tText, 14, clNearWhite);
			break;
		case 10:
			if(LanguageEnCn==0)tText= "清除";else tText="Del ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +150, Lv.Grid.Top[4] + 6, 76, 30, 7, clDeepBlue, tText, 14, clNearWhite);
			break;
		
	}
}

void TBusPadLnk::LoadUcBcdText(unsigned char aP0, unsigned char aP1)
{
	int i;
	unsigned int f, aOrd;
	f = HandCtlBoard[aP0].UcList[aP1].Full;
	for(i=0; i<8; i++){
		f = HandCtlBoard[aP0].UcList[aP1].Full;
		f >>= ((7-i)*4);
		aOrd = (f & 0x0000'000F);
		IList->EditUserCode.Str.Text[i] = CharBcdText[aOrd];

	}
	IList->EditUserCode.Str.Text[8] = '\0';
}

void TBusPadLnk::ModifyKeyCount()
{
	if(Rt.KeyCount == 48){
		Rt.KeyCount = 64;
	}else
	if(Rt.KeyCount == 64){
		Rt.KeyCount = 128;
	}else
	if(Rt.KeyCount == 128){
		Rt.KeyCount = 48;
	}else{
		Rt.KeyCount = 48;
	}
	IList->Edit[1].Str.FromUInt3Dg(Rt.KeyCount);
	IList->Edit[1].Show();
}

void TBusPadLnk::RestoreUcBcdText(unsigned char aP0, unsigned char aP1)
{
	int i;
	unsigned int f, Val;
	uCode.Full = 0;
	for(i=0; i<8; i++){
		f = 11;
		if( (IList->EditUserCode.Str.Text[i] >= '0') && (IList->EditUserCode.Str.Text[i] <= '9') ) {
			f = IList->EditUserCode.Str.Text[i] - '0';
		}
		if(IList->EditUserCode.Str.Text[i] == '*')f = 10;
		f <<= ((7-i) * 4);
		uCode.Full |= f;
	}
}

void TBusPadLnk::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 TBusPadLnk::PermissionCmdGO()
{
	int aVal;
	unsigned char aP0, aP1;
	TUserCode tUserCode;
	if(PadNum == 0)return;
	if(KeyNum == 0)return;
	if(PadNum > dHANDCTL_MAX_COUNT)return;
	if(KeyNum > dHANDCTL_KEY_MAX_COUNT)return;
	aP0 = PadNum -1;
	aP1 = KeyNum -1;
	if( (PermissionCmd == CmdWrite) || (PermissionCmd == CmdClear) ){
		if(PermissionCmd == CmdWrite){
			RestoreUcBcdText(aP0, aP1);
			aVal = IList->EditAreaAssign.Str.ToInteger();
			if( (aVal > -1) && (aVal <= dAreaCountMax) ){
				AreaNum = aVal;
			}else{
				AreaNum = 0;
			}
			aVal = IList->Edit[0].Str.ToInteger();
			if( (aVal < 256) && (aVal>0) ){
				AssignType = static_cast<unsigned char>(aVal);
			}else{
				AssignType = 0;
			}
		}else{
			uCode.Full = 0;
			AreaNum = 0;
			AssignType = 0;
		}
		Write2Flash();
		LoadBusPadLnk();
		if( (Rt.IsPadDeActive == HandCtlBoard[aP0].PadIsProhibit) && (Rt.KeyCount == HandCtlBoard[aP0].KeyCount) && (Rt.IsMatchAreaAssign == HandCtlBoard[aP0].IsMatchAreaAssign[aP1]) 
			&& (uCode.Full == HandCtlBoard[aP0].UcList[aP1].Full) && (AssignType == HandCtlBoard[aP0].AssignType[aP1]) /*&& (AreaNum == HandCtlBoard[aP0].Area[aP1])*/){
			ShowwingResult = 1;
			JustShowResult = 1;
			HandCtlBoard[aP0].SendPadCfg2Pad(aP0);
			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, "Save OK         ");
		}else{
			ShowwingResult = 1;
			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, "Failed To Save  ");
		}
		SetPath(0, PadNum, KeyNum);
	}
}

TGuiMsgReturn TBusPadLnk::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(IsViewList){
		switch(aKey){		
			case VK_UP:
				if(List.CurrentPage >0){
					List.CurrentPage--;
					DrawPadBoardCfgList();
				}
				break;
			case VK_DOWN:
				if(List.CurrentPage < (dHANDCTL_KEY_MAX_COUNT / 8 -1) ){
					List.CurrentPage++;
					DrawPadBoardCfgList();
				}
				break;
			case VK_TAB:
				IsViewList = 0;
				DrawPadBoardCfgList();
				Check4Selected();
				break;
		}
		if(aKey == VK_RETURN){
			IsShow = 0;
			aMsg = guiMsgReturn;
		}		
	}else{
		switch(aKey){ 
			case dfKEY_ASTERISK:
				if(Tag.Inx == 6){
				//User Code
					IList->EditUserCode.KeyIn(aKey);
					IList->EditUserCode.Show();
					IList->EditUserCode.ShowCursor();
				}
				break;
			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;
							PadNumValid = 1;
						}else{
							IList->EditPortNum.SelectedColor = clRed;
							PadNumValid = 0;
						}
						if(PadNum != iVal){
							PadNum = iVal;
							LoadCfg4Edit(PadNum, KeyNum);
							DrawState();
							DrawUserCodeAssignAreaAssignType();
							List.PageCount = dHANDCTL_KEY_MAX_COUNT / 8;
							List.CurrentPage = (KeyNum-1) / 8;
							if(List.CurrentPage > (dHANDCTL_KEY_MAX_COUNT / 8))List.CurrentPage =0;
							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 <= dHANDCTL_KEY_MAX_COUNT) ){
							IList->EditAddrNum.SelectedColor = clBlue;
							KeyNumValid =1;
						}else{
							IList->EditAddrNum.SelectedColor = clRed;
							KeyNumValid = 0;
						}
						if(KeyNum != iVal){
							KeyNum = iVal;
							LoadCfg4Edit(PadNum, KeyNum);
							DrawState();
							DrawUserCodeAssignAreaAssignType();
							List.PageCount = dHANDCTL_KEY_MAX_COUNT / 8;
							List.CurrentPage = (KeyNum-1) / 8;
							if(List.CurrentPage > (dHANDCTL_KEY_MAX_COUNT / 8))List.CurrentPage =0;
							DrawPadBoardCfgList();
						}
						IList->EditAddrNum.Show();
						IList->EditAddrNum.ShowCursor();
					}	
				}else
				if(Tag.Inx == 3){
					//KeyCount
					ModifyKeyCount();
				}else
				if(Tag.Inx == 4){
					if(aKey == VK_RIGHT){
						ClearSelected();
						Tag.Inx = 5;
						Check4Selected();
					}
				}else
				if(Tag.Inx == 5){
					if(aKey == VK_LEFT){
						ClearSelected();
						Tag.Inx = 4;
						Check4Selected();
					}
				}else
				if(Tag.Inx == 6){
					//User Code
					IList->EditUserCode.KeyIn(aKey);
					IList->EditUserCode.Show();
					IList->EditUserCode.ShowCursor();
				}else
				if(Tag.Inx == 7){
					//Area 
					IList->EditAreaAssign.KeyIn(aKey);
					IList->EditAreaAssign.Show();
					IList->EditAreaAssign.ShowCursor();
				}else
				if(Tag.Inx == 8){
					//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);
					}
					AssignType = iVal;
					IList->Edit[0].Show();
					IList->Edit[0].ShowCursor();
					IList->STextModuleInDefineType.Show();
				}else
				if(Tag.Inx == 9){
					if(aKey == VK_RIGHT){
						ClearSelected();
						Tag.Inx = 10;
						Check4Selected();
					}
				}else
				if(Tag.Inx == 10){
					if(aKey == VK_LEFT){
						ClearSelected();
						Tag.Inx = 9;
						Check4Selected();
					}
				}
				break;
			case VK_UP:
				if(Tag.Inx > Tag.InxMin){
					ClearSelected();
					Tag.Inx--;
					Check4Selected();
				}else{
					ClearSelected();
					Tag.Inx =10;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(Tag.Inx < 10){
					ClearSelected();
					Tag.Inx++;
					Check4Selected();
				}else{
					ClearSelected();
					Tag.Inx = Tag.InxMin;
					Check4Selected();
				}
				break;
			case VK_TAB:
				IsViewList = 1;
				ClearSelected();
				List.PageCount = dHANDCTL_KEY_MAX_COUNT / 8;
				List.CurrentPage = KeyNum / 8;
				if(List.CurrentPage > (dHANDCTL_KEY_MAX_COUNT / 8))List.CurrentPage =0;
				DrawPadBoardCfgList();
				break;
			case dfKEY_ENCN:

				break;
			case dfKEY_SETUP:	
				if(Tag.Inx == 1){
					Rt.IsPadDeActive = Rt.IsPadDeActive ? 0 : 1;
					DrawIsPadDeActive();
				}else
				if( (Tag.Inx == 4) || (Tag.Inx ==5) ){
					Rt.IsMatchAreaAssign = Rt.IsMatchAreaAssign ? 0 : 1;
					DrawMatchMode();
				}
				break;
			case VK_F2:	

				break;
			case VK_EXECUTE:
				if(Tag.Inx == 1){
					Rt.IsPadDeActive = Rt.IsPadDeActive ? 0 : 1;
					DrawIsPadDeActive();
				}else
				if( (Tag.Inx == 4) || (Tag.Inx ==5) ){
					Rt.IsMatchAreaAssign = Rt.IsMatchAreaAssign ? 0 : 1;
					DrawMatchMode();
				}else
				if( (PadNum) && (KeyNum) && (PadNum <= dHANDCTL_MAX_COUNT) && (KeyNum <=dHANDCTL_KEY_MAX_COUNT) ) {
					if( (Tag.Inx == 9) || (Tag.Inx == 10) ){
						//Save CFG - 
						ResultPst = 4; 
						if(Tag.Inx == 9)PermissionCmd = CmdWrite; else PermissionCmd = CmdClear;
						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( (ShowwingResult) && (JustShowResult == 0) ){
		ShowwingResult = 0;
		JustShowResult = 0;
		TextRender_string24(Lv.PanelSmall.Left+ 10, Lv.PanelSmall.Top +2, clNearWhite, ItemColor, "                ");
	}
	return aMsg;	
}

void TBusPadLnk::sWriteOnePad(unsigned char aPad)
{
	unsigned int fAddr;
	unsigned int i,x;
	volatile unsigned int GetSta;
	unsigned char aP0;
	
	//HandCtlBoard[aP0].UcList[aP1].Full = uCode.Full;
	//HandCtlBoard[aP0].Area[aP1] = AreaNum;
	//HandCtlBoard[aP0].AssignType[aP1] = AssignType;
	//HandCtlBoard[aP0].IsMatchAreaAssign[aP1] = Rt.IsMatchAreaAssign;
	//HandCtlBoard[aP0].PadIsProhibit = Rt.IsPadDeActive;
	//HandCtlBoard[aP0].KeyCount = Rt.KeyCount;
	
	if(sIList==nullptr)return;
	if(aPad==0)return;
	if(aPad>dHANDCTL_KEY_MAX_COUNT)return;
	aP0 = aPad-1;
	sIList->ReadWriteData.Reset();
	LoopCheck1S();
	do{
		sDelayMs(5);
		GetSta = nor_get_busy_state(dFlashBusPadLnkChipX);
	}while(GetSta);
	
	fAddr = dFlashAddrBusPadLnk + (aP0 * 4096);
	nor_erase_sector_4k(dFlashBusPadLnkChipX,fAddr);
	sDelayMs(100);
	do{
		sDelayMs(10);
		GetSta = nor_get_busy_state(dFlashBusPadLnkChipX);
	}while(GetSta);
	LoopCheck1S();
	Crc32Reset();
	//UcList
	for(i=0; i<2; i++){
		for(x=0;x<64;x++){
			sIList->ReadWriteData.D32[x] = HandCtlBoard[aP0].UcList[i*64 +x].Full;
			Crc32Feed(HandCtlBoard[aP0].UcList[i*64 +x].Full);
		}
		fAddr = dFlashAddrBusPadLnk + (aP0 * 4096) + (i * 256);
		nor_write_page(dFlashBusPadLnkChipX, fAddr, sIList->ReadWriteData.D8);
		do{
			sDelayMs(5);
			GetSta = nor_get_busy_state(dFlashBusPadLnkChipX);
		}while(GetSta);
	}
	LoopCheck1S();
	//AssignType
	for(x=0;x<dHANDCTL_KEY_MAX_COUNT;x++){
		sIList->ReadWriteData.D8[x] = HandCtlBoard[aP0].AssignType[x];
		Crc32Feed(static_cast<unsigned int>(HandCtlBoard[aP0].AssignType[x]));
		
	}
	fAddr = dFlashAddrBusPadLnk + (aP0 * 4096) + (2 * 256);
	nor_write_page(dFlashBusPadLnkChipX, fAddr, sIList->ReadWriteData.D8);
	do{
		sDelayMs(5);
		GetSta = nor_get_busy_state(dFlashBusPadLnkChipX);
	}while(GetSta);
	LoopCheck1S();
	//IsMatchAreaAssign
	for(x=0;x<dHANDCTL_KEY_MAX_COUNT;x++){
		sIList->ReadWriteData.D8[x] = HandCtlBoard[aP0].IsMatchAreaAssign[x];
		Crc32Feed(static_cast<unsigned int>(HandCtlBoard[aP0].IsMatchAreaAssign[x]));	
	}
	fAddr = dFlashAddrBusPadLnk + (aP0 * 4096) + (3 * 256);
	nor_write_page(dFlashBusPadLnkChipX, fAddr, sIList->ReadWriteData.D8);
	do{
		sDelayMs(5);
		GetSta = nor_get_busy_state(dFlashBusPadLnkChipX);
	}while(GetSta);
	LoopCheck1S();
	//Area Code
	for(x=0;x<dHANDCTL_KEY_MAX_COUNT;x++){
		sIList->ReadWriteData.D16[x] = HandCtlBoard[aP0].Area[x];
		Crc32Feed(static_cast<unsigned int>(HandCtlBoard[aP0].Area[x]));
	}
	fAddr = dFlashAddrBusPadLnk + (aP0 * 4096) + (4 * 256);
	nor_write_page(dFlashBusPadLnkChipX, fAddr, sIList->ReadWriteData.D8);
	do{
		sDelayMs(5);
		GetSta = nor_get_busy_state(dFlashBusPadLnkChipX);
	}while(GetSta);
	LoopCheck1S();
	//IsPadDeActive, KeyCount, Crc;
	sIList->ReadWriteData.D8[0] = HandCtlBoard[aP0].PadIsProhibit;
	sIList->ReadWriteData.D8[1] = HandCtlBoard[aP0].KeyCount;
	Crc32Feed(static_cast<unsigned int>(HandCtlBoard[aP0].PadIsProhibit));
	Crc32Feed(static_cast<unsigned int>(HandCtlBoard[aP0].KeyCount));
	
	sIList->ReadWriteData.D32[1] = Crc32GetResult();
	fAddr = dFlashAddrBusPadLnk + (aP0 * 4096) + (5 * 256);
	nor_write_page(dFlashBusPadLnkChipX, fAddr, sIList->ReadWriteData.D8);
	do{
		sDelayMs(5);
		GetSta = nor_get_busy_state(dFlashBusPadLnkChipX);
	}while(GetSta);
}

void TBusPadLnk::Write2Flash()
{
	unsigned int i;
	unsigned char aP0, aP1;
	
	if(PadNum == 0)return;
	if(KeyNum == 0)return;
	if(PadNum > dHANDCTL_MAX_COUNT) return;
	if(KeyNum > dHANDCTL_KEY_MAX_COUNT) return;
	if(PadNumValid == 0)return;
	if(KeyNumValid == 0)return;
	
	//Erase 1 * 4K Blocks
	aP0 = PadNum -1;
	aP1 = KeyNum -1;

	HandCtlBoard[aP0].UcList[aP1].Full = uCode.Full;
	HandCtlBoard[aP0].Area[aP1] = AreaNum;
	HandCtlBoard[aP0].AssignType[aP1] = AssignType;
	HandCtlBoard[aP0].IsMatchAreaAssign[aP1] = Rt.IsMatchAreaAssign;
	HandCtlBoard[aP0].PadIsProhibit = Rt.IsPadDeActive;
	HandCtlBoard[aP0].KeyCount = Rt.KeyCount;
	
	sIList = IList;
	sWriteOnePad(PadNum);
}


void TBusPadLnk::LoadBusPadLnk()
{
	if(PadNum == 0)return;
	if(PadNum > dHANDCTL_MAX_COUNT)return;
	Record.LoadBusPadLnk(PadNum);	
}

void TBusPadLnk::sRecoverSetDefault()
{
	unsigned char aP0;
	//Erase 1 * 4K Blocks
	for(aP0=0; aP0 < dHANDCTL_MAX_COUNT; aP0++){
		SysLoopTick = 0;
		for(int x=0; x<dHANDCTL_KEY_MAX_COUNT; x++){
			HandCtlBoard[aP0].UcList[x].Full = 0;
			HandCtlBoard[aP0].Area[x] = 0;
			HandCtlBoard[aP0].AssignType[x] = 0;
			HandCtlBoard[aP0].IsMatchAreaAssign[x] = 0;
		}
		HandCtlBoard[aP0].PadIsProhibit = 1;
		HandCtlBoard[aP0].KeyCount = 64;	
		sWriteOnePad(aP0+1);
	}
	SysLoopTick = 0;
	Record.LoadBusPadLnk();
}


































