#include "DevMask.h"
#include "RuntimeData.h"
#include "record.h"

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

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

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

#define TextTopOffSet	6
#define ListLineCnt	10

#define ipdWidth0  	400
#define ipdWidth1		390


#define dSdAddrMaskTemp		dAddrSdRam_Commom
#define dMaskCountPerPage	32
#define dMaskCountPerS4K	256

class TItemList *TDevMask::sIList;

void TDevMask::DrawFixText()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "回路->部件屏蔽");
		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[0] + 260, Lv.Grid.Top[0] +6, clNearWhite, "本机");
		TextRender_string24(Lv.Grid.Left[0] + 340, 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[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] +42, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "反馈");
		TextRender_string24(Lv.Grid.Left[3] +140, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "报警");
		TextRender_string24(Lv.Grid.Left[3] +236, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "监管");
		TextRender_string24(Lv.Grid.Left[4] +10, Lv.Grid.Top[4] + 4, clNearWhite, ItemColor, "操作");
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +60, Lv.Grid.Top[4] + 36, 76, 30, 7, clNearWhite, "屏蔽", 14, clNearBlack);
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +150, Lv.Grid.Top[4] + 36, 124, 30, 7, clNearWhite, "解除屏蔽", 14, clNearBlack);
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Loop->Device Blocking");
		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[0] + 260, Lv.Grid.Top[0] +6, clNearWhite, "Local");
		TextRender_string24(Lv.Grid.Left[0] + 340, Lv.Grid.Top[0] +6, clNearWhite, "Type:");
		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, "Addr  ");
		TextRender_string24(Lv.Grid.Left[0] + 280, Lv.Grid.Top[0] + 40, clNearWhite, "    Note:");
		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");
		TextRender_string24(Lv.Grid.Left[3] +10, Lv.Grid.Top[3] + 4, clNearWhite, ItemColor, "Signal");
		TextRender_string24(Lv.Grid.Left[3] +42, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "FBCK");
		TextRender_string24(Lv.Grid.Left[3] +140, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "Fire");
		TextRender_string24(Lv.Grid.Left[3] +236, Lv.Grid.Top[3] + 36, clNearWhite, ItemColor, "SUPV");
		TextRender_string24(Lv.Grid.Left[4] +10, Lv.Grid.Top[4] + 4, clNearWhite, ItemColor, "OPR");
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +60, Lv.Grid.Top[4] + 36, 76, 30, 7, clNearWhite, "Mask", 14, clNearBlack);
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +150, Lv.Grid.Top[4] + 36, 124, 30, 7, clNearWhite, " Clear  ", 14, clNearBlack);
	}
}

void TDevMask::Init()
{
	IsPermission = 0;
	Tag.Reset();
	
	SettingData.IsGoMask = 0;
	IsShow = 0;
}

void TDevMask::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 TDevMask::RenderBackGround(void)
{
	RectFillRender(ContentGeometry.Left, ContentGeometry.Top, ContentGeometry.Right, ContentGeometry.Bottom, ContentColor);
}

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


void TDevMask::DrawSelf(void)
{
	RenderBackGround();
	
	
	
	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->STextDType.Init(Lv.Grid.Left[0] + 400, Lv.Grid.Top[0] + 2, 240, 30, 0, 0, clNearWhite, ItemColor);
	IList->STextState.Init(Lv.Grid.Left[0] + 730, Lv.Grid.Top[0] + 2, 68, 30, 0, 0, clNearWhite, ItemColor);
	IList->STextRegisted.Init(Lv.Grid.Left[0] + 160, Lv.Grid.Top[0] + 36, 120, 30, 0, 0, clNearWhite, ItemColor);
	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->EditDescpShow.Init(Lv.Grid.Left[0] + 390, Lv.Grid.Top[0] + 36, 400, 30, 0, 0, 1, clNearWhite);
	
	IList->STextDType.Enable = 0;
	IList->STextDType.Visible =1;
	IList->STextCtlNum.Enable = 1;
	IList->STextCtlNum.Visible =1;
	
	IList->EditPortNum.SetMaxLen(2);
	IList->EditAddrNum.SetMaxLen(3);
	IList->EditDescpShow.SetMaxLen(3);
	IList->EditPortNum.Enable = 1;
	IList->EditPortNum.Visible = 1;
	IList->EditAddrNum.Enable = 1;
	IList->EditAddrNum.Visible = 1;
	IList->EditDescpShow.Enable = 0;
	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 = 0;
	IList->EditUserCode.Visible =1;
	IList->EditUserCode.SetMaxLen(8);
	IList->EditAreaAssign.Enable = 0;
	IList->EditAreaAssign.Visible =1;
	IList->EditAreaAssign.SetMaxLen(5);
	
	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);
	
	//DrawListDetail();
	DrawFixText();
	
	IList->Edit[0].Init(Lv.Grid.Left[2] +110, Lv.Grid.Top[2] + 4, 100,30,0,0,1,clNearBlack);
	IList->Edit[0].Str.Clear();
	IList->Edit[0].Enable = 0;
	IList->Edit[0].Visible = 1;
	IList->STextModuleInDefineType.Init(Lv.Grid.Left[2] +60, Lv.Grid.Top[2] + 38, 156,30,0,0,clNearWhite, ItemColor);
	IList->STextModuleInDefineType.Enable = 1;
	IList->STextModuleInDefineType.Visible = 1;
	if(LanguageEnCn==0)IList->STextModuleInDefineType.SetText("未定义",24);
	else IList->STextModuleInDefineType.SetText("Undefine",24);

	IList->STextLastState.Init(Lv.Grid.Left[4] +120, Lv.Grid.Top[4] + 4, 156, 30, 0, 0, clNearWhite, ItemColor);
	IList->STextLastState.TextClear();
	
	IList->EditPortNum.SetText("01",24);
	IList->EditAddrNum.SetText("001",24);
	IList->EditDescpShow.SetText("------",24);
	IList->EditUserCode.SetText("00000001",24);
	IList->EditAreaAssign.SetText("0001",24);
	IList->Edit[0].SetText("000",24);
	
	IList->EditPortNum.Show();
	IList->EditAddrNum.Show();
	IList->EditDescpShow.Show();
	IList->EditUserCode.Show();
	IList->EditAreaAssign.Show();
	IList->Edit[0].Show();
	IList->STextDType.Show();
	IList->STextModuleInDefineType.Show();
	
	RedrawModuleInSignalInType();
	
	Check4Selected();
	
	
	
	Path0=1;
	Path1=1;
	Path0Avalble =1;
	Path1Avalble =1;
	ShowItemDetail();
}

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

void TDevMask::FullRedraw(int Prm)
{
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
		RedrawCommonByExReturn();
		DrawHtInf(0);	
	}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);
		RedrawCommonByExReturn();
		DrawHtInf(1);
		
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}

void TDevMask::RedrawCommonByExReturn()
{
	if( (Path0Avalble && Path1Avalble) ){
		if(IsRegisted){
			if(Port[Path0-1].ExistTable[Path1-1] == 0xA5){
				DrawState(Port[Path0-1].StateTable[Path1-1]);
			}else{
				TCircle::sRender(IList->STextState.Bound.Left -12, IList->STextState.Bound.Top +14, 10, ItemColor, 0xF);
				IList->STextLastState.TextClear();
			}
		}else{
			TCircle::sRender(IList->STextState.Bound.Left -12, IList->STextState.Bound.Top +14, 10, ItemColor, 0xF);
			IList->STextLastState.TextClear();
		}
	}else{
		TCircle::sRender(IList->STextState.Bound.Left -12, IList->STextState.Bound.Top +14, 10, ItemColor, 0xF);
		IList->STextLastState.TextClear();
	}
	
	DrawFixText();
	

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

	
	IList->EditPortNum.Show();
	IList->EditAddrNum.Show();
	IList->STextCtlType.Show();
	
	IList->STextDType.Show();
	IList->STextState.Show();
	IList->STextRegisted.Show();
	IList->EditDescpShow.Show();
		
	IList->EditUserCode.Show();
	IList->EditAreaAssign.Show();
	
	IList->STextLastState.Show();
	
	IList->Edit[0].Show();
	IList->STextModuleInDefineType.Show();
}

void TDevMask::DrawHtInf(int aPrm)
{
	
}

void TDevMask::RedrawModuleInSignalInType()
{
	if(SignalInputType == 1){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 12, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 110, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 206, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}else
	if(SignalInputType == 2){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 12, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 110, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 206, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);		
	}else
	if(SignalInputType == 3){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 12, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 110, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 206, 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);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 110, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[3] + 206, Lv.Grid.Top[3] + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
}

void TDevMask::ClearSelected()
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			IList->EditPortNum.Selected = 0;
			IList->EditPortNum.Show();
			IList->EditPortNum.ShowCursor();
			break;
		case 1:
			IList->EditAddrNum.Selected = 0;
			IList->EditAddrNum.Show();
			IList->EditAddrNum.ShowCursor();
			break;
		case 2:
			if(LanguageEnCn==0)tText="屏蔽"; else tText="Mask";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +60, Lv.Grid.Top[4] + 36, 76, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 3:
			if(LanguageEnCn==0)tText="解除屏蔽"; else tText=" Clear  ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +150, Lv.Grid.Top[4] + 36, 124, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
	}
}

//______________________________________________________________________________________________________________________________
void TDevMask::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:
			IList->EditAddrNum.Selected = 1;
			IList->EditAddrNum.SetPstTail();
			IList->EditAddrNum.Show();
			IList->EditAddrNum.ShowCursor();
			break;
		case 2:
			if(LanguageEnCn==0)tText="屏蔽"; else tText="Mask";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +60, Lv.Grid.Top[4] + 36, 76, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
		case 3:
			if(LanguageEnCn==0)tText="解除屏蔽"; else tText=" Clear  ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +150, Lv.Grid.Top[4] + 36, 124, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
	}
}

void TDevMask::GetMsgViaIndex(int aInx)
{
	volatile unsigned int sAddr;
	if(aInx < CtlCenter.ieMsgMask.Count){
		sAddr = dAddrSdRam_MsgMask + aInx* 24;
		MaskMsg.D32[0] = *(volatile unsigned int *)(sAddr + 0);
		MaskMsg.D32[1] = *(volatile unsigned int *)(sAddr + 4);
		MaskMsg.D32[2] = *(volatile unsigned int *)(sAddr + 8);
		MaskMsg.D32[3] = *(volatile unsigned int *)(sAddr + 12);
		MaskMsg.D32[4] = *(volatile unsigned int *)(sAddr + 16);
		MaskMsg.D32[5] = *(volatile unsigned int *)(sAddr + 20);
	}
}

void TDevMask::ClearMsgViaIndex(int aInx)
{
	volatile unsigned int sAddr;
	if(aInx < CtlCenter.ieMsgMask.Count){
		sAddr = dAddrSdRam_MsgMask + aInx* 24;
		*(volatile unsigned int *)(sAddr + 0) = 0;
		*(volatile unsigned int *)(sAddr + 4) = 0;
		*(volatile unsigned int *)(sAddr + 8) = 0;
		*(volatile unsigned int *)(sAddr + 12) = 0;
		*(volatile unsigned int *)(sAddr + 16) = 0;
		*(volatile unsigned int *)(sAddr + 20) = 0;
	}
}

void TDevMask::CopyMsgViaIndex(int aInx)
{
	volatile unsigned int sAddr;
	sAddr = dAddrSdRam_MsgMask + aInx* 24;
	
	MaskMsg.Body.iByte0 = 0;
	MaskMsg.Body.State = 1;
	
	*(volatile unsigned int *)(sAddr + 0) = MaskMsg.D32[0];
	*(volatile unsigned int *)(sAddr + 4) = MaskMsg.D32[1];
	*(volatile unsigned int *)(sAddr + 8) = MaskMsg.D32[2];
	*(volatile unsigned int *)(sAddr + 12) = MaskMsg.D32[3];
	*(volatile unsigned int *)(sAddr + 16) = MaskMsg.D32[4];
	*(volatile unsigned int *)(sAddr + 20) = MaskMsg.D32[5];
}

void TDevMask::GetTempMsgViaIndex(int aInx)
{
	volatile unsigned int sAddr;
	if(aInx < ExistEntryCount){
		sAddr = dSdAddrMaskTemp + aInx* 24;
		MaskMsg.D32[0] = *(volatile unsigned int *)(sAddr + 0);
		MaskMsg.D32[1] = *(volatile unsigned int *)(sAddr + 4);
		MaskMsg.D32[2] = *(volatile unsigned int *)(sAddr + 8);
		MaskMsg.D32[3] = *(volatile unsigned int *)(sAddr + 12);
		MaskMsg.D32[4] = *(volatile unsigned int *)(sAddr + 16);
		MaskMsg.D32[5] = *(volatile unsigned int *)(sAddr + 20);
	}
}

void TDevMask::ClearTempMsgViaIndex(int aInx)
{
	volatile unsigned int sAddr;
	if(aInx < ExistEntryCount){
		sAddr = dSdAddrMaskTemp + aInx* 24;
		*(volatile unsigned int *)(sAddr + 0) = 0;
		*(volatile unsigned int *)(sAddr + 4) = 0;
		*(volatile unsigned int *)(sAddr + 8) = 0;
		*(volatile unsigned int *)(sAddr + 12) = 0;
		*(volatile unsigned int *)(sAddr + 16) = 0;
		*(volatile unsigned int *)(sAddr + 20) = 0;
	}
}

void TDevMask::CopyTempMsgViaIndex(int aInx)
{
	volatile unsigned int sAddr;
	sAddr = dSdAddrMaskTemp + aInx* 24;
	
	MaskMsg.Body.iByte0 = 0;
	MaskMsg.Body.State = 1;
	
	*(volatile unsigned int *)(sAddr + 0) = MaskMsg.D32[0];
	*(volatile unsigned int *)(sAddr + 4) = MaskMsg.D32[1];
	*(volatile unsigned int *)(sAddr + 8) = MaskMsg.D32[2];
	*(volatile unsigned int *)(sAddr + 12) = MaskMsg.D32[3];
	*(volatile unsigned int *)(sAddr + 16) = MaskMsg.D32[4];
	*(volatile unsigned int *)(sAddr + 20) = MaskMsg.D32[5];
}


void TDevMask::AddRemoveViaTempMsgList(unsigned char aP0, unsigned char aP1,  unsigned int IsAdd)
{
	unsigned char P0, P1;
	volatile unsigned int IsFound;
	unsigned int FirstNullIndex, Cnt;
	volatile int i;
	TDMix aAddr;
	P0 = aP0-1;  P1 = aP1-1;
	
	if(P0 >= dPORT_MAX_COUNT)return;
	IsFound = 0;
	if(IsAdd){
		Port[P0].StateTable[P1] |= dEpStateBitMask;
		for(i=0; i<ExistEntryCount; i++){
			GetTempMsgViaIndex(i);
			if(  (MaskMsg.Body.P0 == aP0) && (MaskMsg.Body.P1 == aP1) ){
				IsFound = 1;
			}
		}
		if(IsFound == 0){
			Port[P0].StateTable[P1] |= dEpStateBitMask;
			
			MaskMsg.D32[0] = 0;
			MaskMsg.D32[1] = 0;
			MaskMsg.D32[2] = 0;
			MaskMsg.D32[3] = 0;
			MaskMsg.D32[4] = 0;
			MaskMsg.D32[5] = 0;
			MaskMsg.Body.PCtlNum = MainCtl.fData.Split.MyNum;
			MaskMsg.Body.P0 = aP0;
			MaskMsg.Body.P1 = aP1;
			
			MaskMsg.Body.Second = DateTime.Time.Split.Second;
			MaskMsg.Body.Minute = DateTime.Time.Split.Minute;
			MaskMsg.Body.Hour = DateTime.Time.Split.Hour;
			MaskMsg.Body.Day = DateTime.Date.Split.Day;
			MaskMsg.Body.Month = DateTime.Date.Split.Month;
			MaskMsg.Body.Year = DateTime.Date.Split.Year;
			MaskMsg.Body.State = 1;
			
			MaskMsg.Body.IsMask = 0xA5;
			MaskMsg.Body.dType = Port[P0].dTypeTable[P1];
			
			MaskMsg.Body.UserCode = Port[P0].UcList[P1].Full;
			MaskMsg.Body.AreaNum = Port[P0].Area[P1];
			
			CopyTempMsgViaIndex(ExistEntryCount);
			ExistEntryCount++;
		}
	}else{
		Port[P0].StateTable[P1] &= ~dEpStateBitMask;

		FirstNullIndex = ieMSG_MASK_MAX_COUNT +1000;
		Cnt = 0;
		for(i=0; i<ExistEntryCount; i++){
			//To Find Dual, And Clear it
			GetTempMsgViaIndex(i);
			aAddr.D8[0] = MainCtl.fData.Split.MyNum;
			aAddr.D8[1] = aP0;
			aAddr.D8[2] = aP1;
			aAddr.D8[3] = 0;
			
			if(MaskMsg.D32[2] == 0){
				if(FirstNullIndex > ieMSG_MASK_MAX_COUNT)FirstNullIndex = i;
			}else
			if( MaskMsg.D32[2] ==  aAddr.D32 ){
					ClearTempMsgViaIndex(i);
					if(FirstNullIndex > ieMSG_MASK_MAX_COUNT){
						FirstNullIndex = i;
					}
			}else{
				if(FirstNullIndex < ieMSG_MASK_MAX_COUNT){
					CopyTempMsgViaIndex(FirstNullIndex);
					ClearTempMsgViaIndex(i);
					FirstNullIndex++;
				}
				Cnt++;
			}
		}
		ExistEntryCount = Cnt;
	}
}


void TDevMask::AddRemoveViaCtlCenterMsg(unsigned char aP0, unsigned char aP1,  unsigned int aUc, unsigned int IsAdd)
{
	unsigned char P0, P1;
	volatile unsigned int IsFound;
	unsigned int FirstNullIndex, Cnt;
	volatile int i;
	TDMix aAddr;
	P0 = aP0-1;  P1 = aP1-1;
	
	if(P0 >= dPORT_MAX_COUNT)return;
	IsFound = 0;
	if(IsAdd){
		Port[P0].StateTable[P1] |= dEpStateBitMask;
		for(i=0; i<CtlCenter.ieMsgMask.Count; i++){
			GetMsgViaIndex(i);
			if(  (MaskMsg.Body.P0 == aP0) && (MaskMsg.Body.P1 == aP1) ){
				IsFound = 1;
			}
		}
		if(IsFound == 0){
			Port[P0].StateTable[P1] |= dEpStateBitMask;
			
			MaskMsg.D32[0] = 0;
			MaskMsg.D32[1] = 0;
			MaskMsg.D32[2] = 0;
			MaskMsg.D32[3] = 0;
			MaskMsg.D32[4] = 0;
			MaskMsg.D32[5] = 0;
			MaskMsg.Body.PCtlNum = MainCtl.fData.Split.MyNum;
			MaskMsg.Body.P0 = aP0;
			MaskMsg.Body.P1 = aP1;
			
			MaskMsg.Body.Second = DateTime.Time.Split.Second;
			MaskMsg.Body.Minute = DateTime.Time.Split.Minute;
			MaskMsg.Body.Hour = DateTime.Time.Split.Hour;
			MaskMsg.Body.Day = DateTime.Date.Split.Day;
			MaskMsg.Body.Month = DateTime.Date.Split.Month;
			MaskMsg.Body.Year = DateTime.Date.Split.Year;
			MaskMsg.Body.State = 1;
			
			MaskMsg.Body.IsMask = 0xA5;
			MaskMsg.Body.dType = Port[P0].dTypeTable[P1];
			
			MaskMsg.Body.UserCode = Port[P0].UcList[P1].Full;
			MaskMsg.Body.AreaNum = Port[P0].Area[P1];
			
			CopyMsgViaIndex(CtlCenter.ieMsgMask.Count);
			CtlCenter.ieMsgMask.Count++;
			CtlCenter.ieMsgMask.ReflashIndex++;
			CtlCenter.ieGot = 1;
			//EventShow.MsgList[4].Count = CtlCenter.ieMsgMask.Count;
			//EventShow.MsgList[4].NewCount = CtlCenter.ieMsgMask.Count;
			//EventShow.TryGetMaskData(2, 20240428);
		}
	}else{
		Port[P0].StateTable[P1] &= ~dEpStateBitMask;

		FirstNullIndex = ieMSG_MASK_MAX_COUNT +1000;
		Cnt = 0;
		for(i=0; i<CtlCenter.ieMsgMask.Count; i++){
			//To Find Dual, And Clear it
			GetMsgViaIndex(i);
			aAddr.D8[0] = MainCtl.fData.Split.MyNum;
			aAddr.D8[1] = aP0;
			aAddr.D8[2] = aP1;
			aAddr.D8[3] = 0;
			
			if(MaskMsg.D32[2] == 0){
				if(FirstNullIndex > ieMSG_MASK_MAX_COUNT)FirstNullIndex = i;
			}else
			if( MaskMsg.D32[2] ==  aAddr.D32 ){
					ClearMsgViaIndex(i);
					if(FirstNullIndex > ieMSG_MASK_MAX_COUNT){
						FirstNullIndex = i;
					}
					CtlCenter.ieMsgMask.ReflashIndex++;
					CtlCenter.ieGot = 1;
			}else{
				if(FirstNullIndex < ieMSG_MASK_MAX_COUNT){
					CopyMsgViaIndex(FirstNullIndex);
					ClearMsgViaIndex(i);
					FirstNullIndex++;
				}
				Cnt++;
			}
		}
		CtlCenter.ieMsgMask.Count = Cnt;
		//EventShow.MsgList[4].Count = CtlCenter.ieMsgMask.Count;
		//EventShow.MsgList[4].NewCount = CtlCenter.ieMsgMask.Count;
		//EventShow.TryGetMaskData(2, 20240428);
	}
}

//_____________________________________________________________________________________________________________________________
void TDevMask::DrawState(unsigned char aSta)
{
	if(aSta & dEpStateBitFire){
		TCircle::sRender(IList->STextState.Bound.Left -12, IList->STextState.Bound.Top +14, 10, clRed, 0xF);
		if(LanguageEnCn==0)IList->STextState.SetText("报警", 24);
		else IList->STextState.SetText("Fire", 24);
	}else 
	if(aSta & dEpStateBitStart){
		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("ACT.", 24);
	}else 
	if(aSta & dEpStateBitSv){
		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("SUPV", 24);
	}else 
	if(aSta & dEpStateBitFault){
		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(aSta & dEpStateBitFeedBack){
		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("FB  ", 24);
	}else 
	if(aSta & dEpStateBitMask){
		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("Mask", 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);
	}
	IList->STextState.Show();
}

void TDevMask::ShowItemDetail()
{
	int ClearInf = 0;
	if( (Path0Avalble && Path1Avalble) ){
		if(Port[Path0-1].ExistTable[Path1-1] == 0xA5){
			IsRegisted = 1;
			dType = Port[Path0-1].dTypeTable[Path1-1];
			AssignType = Port[Path0-1].AssignType[Path1-1];
			if(LanguageEnCn==0)IList->STextDType.SetText(StrHwType[dType],24);
			else IList->STextDType.SetText(StrHwTypeEn[dType],24);
			DrawState(Port[Path0-1].StateTable[Path1-1]);
			if(LanguageEnCn==0){
				IList->STextRegisted.SetText("已注册", 24);
				IList->STextModuleInDefineType.SetText(StrTypeShortName[AssignType],24);
			}else{
				IList->STextRegisted.SetText("Registered", 24);
				IList->STextModuleInDefineType.SetText(StrTypeShortNameEn[AssignType],24);
			}
			IList->Edit[0].Str.FromUInt3Dg(AssignType);
		}else{
			IsRegisted = 0;
			IList->STextDType.SetText("----", 24);
			IList->STextState.SetText("----", 24);
			if(LanguageEnCn==0)IList->STextRegisted.SetText("未注册", 24);
			else IList->STextRegisted.SetText("Un Reg", 24);
			IList->Edit[0].Str.FromStr("---");
			IList->STextModuleInDefineType.TextClear();
		}
		unsigned int tAddr = dAddrSdRam_Descp + ((Path0-1) * 256 * 32) + ((Path1-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->EditUserCode.Str.FromUInt8Dg(Port[Path0-1].UcList[Path1-1].Full);
		IList->EditAreaAssign.Str.FromUInt4Dg(Port[Path0-1].Area[Path1-1]);
	}else{
		IsRegisted = 0;
		ClearInf = 1;
	}
	if(ClearInf){
		IList->STextDType.SetText("------",24);
		IList->STextState.SetText("----", 24);
		IList->STextRegisted.SetText("------", 24);
		IList->EditDescpShow.Str.Text[0] = '\0';
		IList->EditDescpShow.Str.Text[1] = '\0';
		IList->EditUserCode.Str.FromStr("--------");
		IList->EditAreaAssign.Str.FromStr("----");
		IList->Edit[0].Str.FromStr("---");
		IList->STextModuleInDefineType.TextClear();
		
		TCircle::sRender(IList->STextState.Bound.Left -12, IList->STextState.Bound.Top +14, 10, ItemColor, 0xF);
	}
	IList->STextDType.Show();
	IList->STextState.Show();
	IList->STextRegisted.Show();
	IList->EditDescpShow.Show();
		
	IList->EditUserCode.Show();
	IList->EditAreaAssign.Show();
	
	IList->Edit[0].Show();
	IList->STextModuleInDefineType.Show();
	
}

void TDevMask::sWaitChipIdle()
{
	volatile unsigned int Sta;
	do{
		sDelayMs(10);
		Sta = nor_get_busy_state(dFlashMaskChip);
	}while(Sta);
}

void TDevMask::WaitChipIdle2()
{
	volatile unsigned int Sta;
	do{
		sDelayMs(10);
		Sta = nor_get_busy_state(dFlashMaskChip);
	}while(Sta);
}

void TDevMask::ClearAllMask()
{
	unsigned int fAddr, i;
	sWaitChipIdle();
	fAddr = dFlashAddrMask;
	for(i=0; i<4; i++){
		WaitChipIdle2();
		nor_erase_sector_64k(dFlashMaskChip, fAddr);
		fAddr = fAddr + SizeOf64K;
		sDelayMs(200);
	}
	WaitChipIdle2();
}

void TDevMask::AddMaskMsg2TempList(int Index, TRecordMaskMsg aMsg)
{
	volatile unsigned int sAddr;
	sAddr = dSdAddrMaskTemp + (Index * 24);
	*(volatile unsigned int *)(sAddr +  0) =  aMsg.D32[0];
	*(volatile unsigned int *)(sAddr +  4) =  aMsg.D32[1];
	*(volatile unsigned int *)(sAddr +  8) =  aMsg.D32[2];
	*(volatile unsigned int *)(sAddr + 12) =  aMsg.D32[3];
	*(volatile unsigned int *)(sAddr + 16) =  aMsg.D32[4];
	*(volatile unsigned int *)(sAddr + 20) =  aMsg.D32[5];

}

void TDevMask::AddMaskMsg2DymList(int Index, TRecordMaskMsg aMsg)
{
	volatile unsigned int sAddr;
	sAddr = dAddrSdRam_MsgMask + (Index * 24);
	*(volatile unsigned int *)(sAddr +  0) =  aMsg.D32[0];
	*(volatile unsigned int *)(sAddr +  4) =  aMsg.D32[1];
	*(volatile unsigned int *)(sAddr +  8) =  aMsg.D32[2];
	*(volatile unsigned int *)(sAddr + 12) =  aMsg.D32[3];
	*(volatile unsigned int *)(sAddr + 16) =  aMsg.D32[4];
	*(volatile unsigned int *)(sAddr + 20) =  aMsg.D32[5];

}

int TDevMask::ReadMaskFromFlash2Temp(int xArea)
{
	volatile int i, x, PathAva;
	volatile unsigned int BaseAddr, fAddr, MaskCount, suExit;
	unsigned char tP0,tP1;
	TRecordMaskMsg tMaskMsg;
	
	MaskCount = 0;
	sWaitChipIdle();
	
	//Page Count =  (ieMSG_MASK_MAX_COUNT=10000 * EntrySize=16) / 16 = 10000;
	
	for(i=0; i<dPORT_MAX_COUNT; i++){
		for(x=0; x<256;x++){
			Port[i].StateTable[x] &= ~dEpStateBitMask;
		}
	}
	if(xArea==0){
		BaseAddr = dFlashAddrMask;
	}else{
		BaseAddr = dFlashAddrMask_II;
	}
	
	suExit = 0;
	for(i=0; i<1000; i++){
		fAddr = BaseAddr + (i * 256);
		nor_read_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
		for(x=0; x<16; x++){
			tMaskMsg.D32[0] = ReadWriteData.D32[x*4 +0];	
			tMaskMsg.D32[1] = ReadWriteData.D32[x*4 +1];	
			tMaskMsg.D32[2] = ReadWriteData.D32[x*4 +2];
			tMaskMsg.D32[3] = ReadWriteData.D32[x*4 +3];
			
			PathAva = 0;
			if( (tMaskMsg.Body.P0) && (tMaskMsg.Body.P1) ){
				if( (tMaskMsg.Body.P0 <= dPORT_MAX_COUNT) && (tMaskMsg.Body.P1 <= dEP_MAX_COUNT_PER_PORT) ){
					PathAva =1;
					tP0 = tMaskMsg.Body.P0 -1;
					tP1 = tMaskMsg.Body.P1 -1;
				}
			}
			if(PathAva){
				tMaskMsg.Body.UserCode = Port[tP0].UcList[tP1].Full;
				tMaskMsg.Body.AreaNum = Port[tP0].Area[tP1];
			}else{
				tMaskMsg.Body.UserCode =0;
				tMaskMsg.Body.AreaNum =0;
			}
			
			tMaskMsg.Body.PCtlNum = MainCtl.fData.Split.MyNum;
			tMaskMsg.Body.P2 = 0;
			
			if(tMaskMsg.Body.IsMask == 0xA5){
				tMaskMsg.Body.iByte0 = 0;
				tMaskMsg.Body.State = 1;
				if(MaskCount< ieMSG_MASK_MAX_COUNT){
					AddMaskMsg2TempList(MaskCount, tMaskMsg);
					MaskCount++;
				}
				if(PathAva){
					Port[tP0].StateTable[tP1] |= dEpStateBitMask;
				}
			}else{
				suExit = 1;
			}
		}
		if(suExit)break;
	}
	return MaskCount;
	//ExRecord.SetStateCodeMask();
	//FecBusCRT.SetStateCodeMask();
}

void TDevMask::CombMsg4Master()
{
	int i;
	ProtoC2C.MasterDoMaskListRemove(MainCtl.fData.Split.MyNum);
	for(i=0; i<ExistEntryCount; i++){
		GetTempMsgViaIndex(i);
		ProtoC2C.MasterDoMaskAdd(MaskMsg, CtlCenter.ieMsgMask.Count);
		CtlCenter.ieMsgMask.Count++;
		CtlCenter.ieMsgMask.ReflashIndex++;
		CtlCenter.ieGot = 1;
	}
}

void TDevMask::Erase(int xArea, int ForCount)
{
	volatile unsigned int fAddr, fAddr4Crc;
	int IsErase4K;
	int ToBeErase64K_Count;
	int ToBeErase4K_Count;
	int i;
	if(xArea==0){
		fAddr = dFlashAddrMask;
		fAddr4Crc = dFlashAddrMask + dMaskCrcCntOffset;
	}else{
		fAddr = dFlashAddrMask_II;
		fAddr4Crc = dFlashAddrMask_II + dMaskCrcCntOffset;
	}
	
	ToBeErase64K_Count = ForCount / 4096;
	if(ToBeErase64K_Count <1){
		IsErase4K =1;
	}else{
		IsErase4K = 0;
	}
	
	if(IsErase4K){
		ToBeErase4K_Count = ForCount / 256;
		ToBeErase4K_Count ++;
		sWaitChipIdle();
		for(i=0; i<ToBeErase4K_Count; i++){
			nor_erase_sector_4k(dFlashMaskChip, fAddr);
			sDelayMs(50);
			sWaitChipIdle();
			fAddr = fAddr + SizeOf4K;
		}
		sWaitChipIdle();
		nor_erase_sector_4k(dFlashMaskChip, fAddr4Crc);
		sDelayMs(50);
		sWaitChipIdle();		
	}else{
		ToBeErase64K_Count++;
		if(ToBeErase64K_Count >4)ToBeErase64K_Count=4;
		sWaitChipIdle();
		for(i=0; i<ToBeErase64K_Count; i++){
			nor_erase_sector_64k(dFlashMaskChip, fAddr);
			sDelayMs(200);
			sWaitChipIdle();
			fAddr = fAddr + SizeOf64K;
		}
		if(ToBeErase64K_Count <4){
			sWaitChipIdle();
			nor_erase_sector_4k(dFlashMaskChip, fAddr4Crc);
			sDelayMs(50);
			sWaitChipIdle();
		}
	}
}

void TDevMask::EraseBackupAfterWrote()
{
	sWaitChipIdle();
	nor_erase_sector_4k(dFlashMaskChip, dFlashAddrMask_II + dMaskCrcCntOffset);
	sDelayMs(50);
	sWaitChipIdle();
}

void TDevMask::WriteToFlashMaster()
{
	volatile int i, x, aInx;
	volatile unsigned int fAddr, sAddr, MaskCount, suExit, ToWritePage;
	int IsErase4K;
	int ToBeErase64K_Count;
	int ToBeErase4K_Count;
	
	sWaitChipIdle();
	Erase(1,ExistEntryCount);
	
	Crc32Reset();
	if(ExistEntryCount > 0){
		suExit = 0;
		ToWritePage = ExistEntryCount / 16 +1;
		for(i=0; i<ToWritePage; i++){
			for(x=0; x<16; x++){
				aInx = i * 16 + x;
				sAddr = dSdAddrMaskTemp + (aInx * 24);
				if(aInx < ExistEntryCount){
					ReadWriteData.D32[x *4 +0] = *(volatile unsigned int *)(sAddr +0);
					ReadWriteData.D32[x *4 +1] = *(volatile unsigned int *)(sAddr +4);
					ReadWriteData.D32[x *4 +2] = *(volatile unsigned int *)(sAddr +8);
					ReadWriteData.D32[x *4 +3] = *(volatile unsigned int *)(sAddr +12);
					Crc32Feed(ReadWriteData.D32[x *4 +0]);
					Crc32Feed(ReadWriteData.D32[x *4 +1]);
					Crc32Feed(ReadWriteData.D32[x *4 +2]);
					Crc32Feed(ReadWriteData.D32[x *4 +3]);
					
				}else{
					ReadWriteData.D32[x *4 +0] = 0xFFFFFFFF;
					ReadWriteData.D32[x *4 +1] = 0xFFFFFFFF;
					ReadWriteData.D32[x *4 +2] = 0xFFFFFFFF;
					ReadWriteData.D32[x *4 +3] = 0xFFFFFFFF;
					suExit = 1;
				}
			}
			fAddr = dFlashAddrMask_II + (i * 256);
			nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
			sWaitChipIdle();
			if(suExit)break;
		}
		Crc32Feed(ExistEntryCount);
		ReadWriteData.D32[0] = ExistEntryCount;
		ReadWriteData.D32[1] = Crc32GetResult();
		ReadWriteData.D32[2] = dMaskBackupExistIdf;
		fAddr = dFlashAddrMask_II + dMaskCrcCntOffset;
		nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
		sWaitChipIdle();
	}else{
		ReadWriteData.D32[0] = 0;
		ReadWriteData.D32[1] = dMaskNonDataIdf;
		ReadWriteData.D32[2] = dMaskBackupExistIdf;
		fAddr = dFlashAddrMask_II + dMaskCrcCntOffset;
		nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
		sWaitChipIdle();
	}
	
	Erase(0,ExistEntryCount);
	Crc32Reset();
	if(ExistEntryCount > 0){
		suExit = 0;
		ToWritePage = ExistEntryCount / 16 +1;
		for(i=0; i<ToWritePage; i++){
			for(x=0; x<16; x++){
				aInx = i * 16 + x;
				sAddr = dSdAddrMaskTemp + (aInx * 24);
				if(aInx < ExistEntryCount){
					ReadWriteData.D32[x *4 +0] = *(volatile unsigned int *)(sAddr +0);
					ReadWriteData.D32[x *4 +1] = *(volatile unsigned int *)(sAddr +4);
					ReadWriteData.D32[x *4 +2] = *(volatile unsigned int *)(sAddr +8);
					ReadWriteData.D32[x *4 +3] = *(volatile unsigned int *)(sAddr +12);
					Crc32Feed(ReadWriteData.D32[x *4 +0]);
					Crc32Feed(ReadWriteData.D32[x *4 +1]);
					Crc32Feed(ReadWriteData.D32[x *4 +2]);
					Crc32Feed(ReadWriteData.D32[x *4 +3]);
					
				}else{
					ReadWriteData.D32[x *4 +0] = 0xFFFFFFFF;
					ReadWriteData.D32[x *4 +1] = 0xFFFFFFFF;
					ReadWriteData.D32[x *4 +2] = 0xFFFFFFFF;
					ReadWriteData.D32[x *4 +3] = 0xFFFFFFFF;
					suExit = 1;
				}
			}
			fAddr = dFlashAddrMask + (i * 256);
			nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
			sWaitChipIdle();
			if(suExit)break;
		}
		Crc32Feed(ExistEntryCount);
		ReadWriteData.D32[0] = ExistEntryCount;
		ReadWriteData.D32[1] = Crc32GetResult();
		fAddr = dFlashAddrMask + dMaskCrcCntOffset;
		nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
		sWaitChipIdle();
	}else{
		ReadWriteData.D32[0] = 0;
		ReadWriteData.D32[1] = dMaskNonDataIdf;
		fAddr = dFlashAddrMask + dMaskCrcCntOffset;
		nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
		sWaitChipIdle();
	}
	
	EraseBackupAfterWrote();
}

void TDevMask::WriteToFlashSlave()
{
	volatile int i, x, aInx;
	volatile unsigned int fAddr, sAddr, MaskCount, suExit, ToWritePage;
	int IsErase4K;
	int ToBeErase64K_Count;
	int ToBeErase4K_Count;
	
	sWaitChipIdle();
	Erase(1,CtlCenter.ieMsgMask.Count);
	Crc32Reset();
	if(CtlCenter.ieMsgMask.Count > 0){
		suExit = 0;
		ToWritePage = CtlCenter.ieMsgMask.Count / 16 +1;
		for(i=0; i<ToWritePage; i++){
			for(x=0; x<16; x++){
				aInx = i * 16 + x;
				sAddr = dAddrSdRam_MsgMask + (aInx * 24);
				if(aInx < CtlCenter.ieMsgMask.Count){
					ReadWriteData.D32[x *4 +0] = *(volatile unsigned int *)(sAddr +0);
					ReadWriteData.D32[x *4 +1] = *(volatile unsigned int *)(sAddr +4);
					ReadWriteData.D32[x *4 +2] = *(volatile unsigned int *)(sAddr +8);
					ReadWriteData.D32[x *4 +3] = *(volatile unsigned int *)(sAddr +12);
					Crc32Feed(ReadWriteData.D32[x *4 +0]);
					Crc32Feed(ReadWriteData.D32[x *4 +1]);
					Crc32Feed(ReadWriteData.D32[x *4 +2]);
					Crc32Feed(ReadWriteData.D32[x *4 +3]);
				}else{
					ReadWriteData.D32[x *4 +0] = 0xFFFFFFFF;
					ReadWriteData.D32[x *4 +1] = 0xFFFFFFFF;
					ReadWriteData.D32[x *4 +2] = 0xFFFFFFFF;
					ReadWriteData.D32[x *4 +3] = 0xFFFFFFFF;
					suExit = 1;
				}
			}
			fAddr = dFlashAddrMask_II + (i * 256);
			nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
			sWaitChipIdle();
			if(suExit)break;
		}
		Crc32Feed(CtlCenter.ieMsgMask.Count);
		ReadWriteData.D32[0] = CtlCenter.ieMsgMask.Count;
		ReadWriteData.D32[1] = Crc32GetResult();
		ReadWriteData.D32[2] = dMaskBackupExistIdf;
		fAddr = dFlashAddrMask_II + dMaskCrcCntOffset;
		nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
		sWaitChipIdle();
	}else{
		ReadWriteData.D32[0] = 0;
		ReadWriteData.D32[1] = dMaskNonDataIdf;
		ReadWriteData.D32[2] = dMaskBackupExistIdf;
		fAddr = dFlashAddrMask_II + dMaskCrcCntOffset;
		nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
		sWaitChipIdle();
	}
	
	Erase(0,CtlCenter.ieMsgMask.Count);
	Crc32Reset();
	if(CtlCenter.ieMsgMask.Count > 0){
		suExit = 0;
		ToWritePage = CtlCenter.ieMsgMask.Count / 16 +1;
		for(i=0; i<ToWritePage; i++){
			for(x=0; x<16; x++){
				aInx = i * 16 + x;
				sAddr = dAddrSdRam_MsgMask + (aInx * 24);
				if(aInx < CtlCenter.ieMsgMask.Count){
					ReadWriteData.D32[x *4 +0] = *(volatile unsigned int *)(sAddr +0);
					ReadWriteData.D32[x *4 +1] = *(volatile unsigned int *)(sAddr +4);
					ReadWriteData.D32[x *4 +2] = *(volatile unsigned int *)(sAddr +8);
					ReadWriteData.D32[x *4 +3] = *(volatile unsigned int *)(sAddr +12);
					Crc32Feed(ReadWriteData.D32[x *4 +0]);
					Crc32Feed(ReadWriteData.D32[x *4 +1]);
					Crc32Feed(ReadWriteData.D32[x *4 +2]);
					Crc32Feed(ReadWriteData.D32[x *4 +3]);
				}else{
					ReadWriteData.D32[x *4 +0] = 0xFFFFFFFF;
					ReadWriteData.D32[x *4 +1] = 0xFFFFFFFF;
					ReadWriteData.D32[x *4 +2] = 0xFFFFFFFF;
					ReadWriteData.D32[x *4 +3] = 0xFFFFFFFF;
					suExit = 1;
				}
			}
			fAddr = dFlashAddrMask + (i * 256);
			nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
			sWaitChipIdle();
			if(suExit)break;
		}
		Crc32Feed(CtlCenter.ieMsgMask.Count);
		ReadWriteData.D32[0] = CtlCenter.ieMsgMask.Count;
		ReadWriteData.D32[1] = Crc32GetResult();
		fAddr = dFlashAddrMask + dMaskCrcCntOffset;
		nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
		sWaitChipIdle();
	}else{
		ReadWriteData.D32[0] = 0;
		ReadWriteData.D32[1] = dMaskNonDataIdf;
		fAddr = dFlashAddrMask + dMaskCrcCntOffset;
		nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
		sWaitChipIdle();
	}
	
	EraseBackupAfterWrote();
}

int TDevMask::CheckStorageData()
{
	volatile int i, x;
	volatile unsigned int fAddr, sAddr, MaskCount, suExit, tUc;
	volatile unsigned int ExistCode, rCrc, rCnt, GetSta;
	int GoReadAndWrite;
	TRecordMaskMsg tMaskMsg;

	MaskCount = 0;
	sWaitChipIdle();
	
	int Error =0;
	suExit = 0;
	Crc32Reset();
	sWaitChipIdle();
	for(i=0; i<1000; i++){
		SysLoopTick = 0;
		fAddr = dFlashAddrMask + (i * 256);
		sDelayMs(1);
		nor_read_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
		for(x=0; x<16; x++){
			tMaskMsg.D32[0] = ReadWriteData.D32[x*4 +0];	
			tMaskMsg.D32[1] = ReadWriteData.D32[x*4 +1];	
			tMaskMsg.D32[2] = ReadWriteData.D32[x*4 +2];
			tMaskMsg.D32[3] = ReadWriteData.D32[x*4 +3];
			tMaskMsg.D32[4] = 0;
			if(tMaskMsg.Body.IsMask == 0xA5){
				Crc32Feed(tMaskMsg.D32[0]);
				Crc32Feed(tMaskMsg.D32[1]);
				Crc32Feed(tMaskMsg.D32[2]);
				Crc32Feed(tMaskMsg.D32[3]);
				if(MaskCount< ieMSG_MASK_MAX_COUNT){
					MaskCount++;
				}else{
					//Error Here
					Error = 1;
					suExit = 1;
					break;
				}
				//Add to CRT____________________________________
			}else{
				suExit = 1;
			}
		}
		if(suExit)break;
	}
	sWaitChipIdle();
	fAddr = dFlashAddrMask + dMaskCrcCntOffset;
	nor_read_page(dFlash4AreaChip, fAddr, ReadWriteData.D8);
	rCnt = ReadWriteData.D32[0];
	rCrc = ReadWriteData.D32[1];
	this->Crc = rCrc;
	Crc32Feed(rCnt);
	if(MaskCount==0 and rCnt==0 and rCrc==dMaskNonDataIdf){
		if(CrcFlag){
			CrcFlag=0;
			CtlCenter.ieAddBrdFaultCancel(41,TBoardFaultType::StorageMaskData);
		}
	}else{
		if(Error==0 and rCrc==Crc32GetResult()){
			if(CrcFlag){
				CrcFlag=0;
				CtlCenter.ieAddBrdFaultCancel(41,TBoardFaultType::StorageMaskData);
			}
		}else{
			//error here
			CrcFlag=1;
			CtlCenter.ieAddBrdFault(41,TBoardFaultType::StorageMaskData);
		}
	}
	return MaskCount;
}

int TDevMask::ReadMaskFromFlash()
{
	volatile int i, x;
	volatile unsigned int fAddr, sAddr, MaskCount, suExit, tUc;
	volatile unsigned int ExistCode, rCrc, rCnt, GetSta;
	int GoReadAndWrite;
	TRecordMaskMsg tMaskMsg;
	TRecordCommon aRC;

	MaskCount = 0;
	
	//Page Count =  (ieMSG_MASK_MAX_COUNT=10000 * EntrySize=16) / 16 = 10000;
	for(i=0; i<dPORT_MAX_COUNT; i++){
		for(x=0; x<256;x++){
			Port[i].StateTable[x] &= ~dEpStateBitMask;
		}
	}
	
	//Read Backup
	GoReadAndWrite = 0;
	SysLoopTick = 0;
	sWaitChipIdle();
	fAddr = dFlashAddrMask_II + dMaskCrcCntOffset;
	nor_read_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
	rCnt = ReadWriteData.D32[0];
	rCrc = ReadWriteData.D32[1];
	ExistCode = ReadWriteData.D32[2];
	if(ExistCode == dMaskBackupExistIdf){
		//Procedure , Check Crc Ok
		if(rCnt==0){
			if(rCrc==dMaskNonDataIdf){
				//None Data
				GoReadAndWrite =1;
			}
		}else{
			int bCnt = ReadMaskFromFlash2Temp(1);
			if(bCnt==rCnt){
				Crc32Reset();
				for(i=0; i<bCnt; i++){
					sAddr = dSdAddrMaskTemp + i* 24;
					Crc32Feed(*(volatile unsigned int *)(sAddr +  0));
					Crc32Feed(*(volatile unsigned int *)(sAddr +  4));
					Crc32Feed(*(volatile unsigned int *)(sAddr +  8));
					Crc32Feed(*(volatile unsigned int *)(sAddr + 12));
				}
				Crc32Feed(bCnt);
			}
			if(bCnt==rCnt and rCrc==Crc32GetResult()){
				//Write to Flash
				GoReadAndWrite = 1;
			}
		}
		if(GoReadAndWrite){
			Erase(0,rCnt);
			Crc32Reset();
			if(rCnt > 0){
				suExit = 0;
				int ToWritePage = rCnt / 16 +1;
				for(i=0; i<ToWritePage; i++){
					for(x=0; x<16; x++){
						int aInx = i * 16 + x;
						sAddr = dSdAddrMaskTemp + (aInx * 24);
						if(aInx < rCnt){
							ReadWriteData.D32[x *4 +0] = *(volatile unsigned int *)(sAddr +0);
							ReadWriteData.D32[x *4 +1] = *(volatile unsigned int *)(sAddr +4);
							ReadWriteData.D32[x *4 +2] = *(volatile unsigned int *)(sAddr +8);
							ReadWriteData.D32[x *4 +3] = *(volatile unsigned int *)(sAddr +12);
							Crc32Feed(ReadWriteData.D32[x *4 +0]);
							Crc32Feed(ReadWriteData.D32[x *4 +1]);
							Crc32Feed(ReadWriteData.D32[x *4 +2]);
							Crc32Feed(ReadWriteData.D32[x *4 +3]);
							
						}else{
							ReadWriteData.D32[x *4 +0] = 0xFFFFFFFF;
							ReadWriteData.D32[x *4 +1] = 0xFFFFFFFF;
							ReadWriteData.D32[x *4 +2] = 0xFFFFFFFF;
							ReadWriteData.D32[x *4 +3] = 0xFFFFFFFF;
							suExit = 1;
						}
					}
					fAddr = dFlashAddrMask + (i * 256);
					sWaitChipIdle();
					nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
					sWaitChipIdle();
					if(suExit)break;
				}
				Crc32Feed(rCnt);
				ReadWriteData.D32[0] = rCnt;
				ReadWriteData.D32[1] = Crc32GetResult();
				fAddr = dFlashAddrMask + dMaskCrcCntOffset;
				nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
				sWaitChipIdle();
			}else{
				ReadWriteData.D32[0] = 0;
				ReadWriteData.D32[1] = dMaskNonDataIdf;
				fAddr = dFlashAddrMask + dMaskCrcCntOffset;
				nor_write_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
				sWaitChipIdle();
			}
			
			EraseBackupAfterWrote();
		}
	}
	
	int Error =0;
	MaskCount =0;
	suExit = 0;
	Crc32Reset();
	sWaitChipIdle();
	for(i=0; i<1000; i++){
		SysLoopTick = 0;
		fAddr = dFlashAddrMask + (i * 256);
		sDelayMs(1);
		nor_read_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
		for(x=0; x<16; x++){
			tMaskMsg.D32[0] = ReadWriteData.D32[x*4 +0];	
			tMaskMsg.D32[1] = ReadWriteData.D32[x*4 +1];	
			tMaskMsg.D32[2] = ReadWriteData.D32[x*4 +2];
			tMaskMsg.D32[3] = ReadWriteData.D32[x*4 +3];
			tMaskMsg.D32[4] = 0;
			
			tMaskMsg.Body.PCtlNum = MainCtl.fData.Split.MyNum;
			tMaskMsg.Body.P2 = 0;
			
			if(tMaskMsg.Body.IsMask == 0xA5){
				Crc32Feed(tMaskMsg.D32[0]);
				Crc32Feed(tMaskMsg.D32[1]);
				Crc32Feed(tMaskMsg.D32[2]);
				Crc32Feed(tMaskMsg.D32[3]);
				tMaskMsg.Body.iByte0 = 0;
				tMaskMsg.Body.State = 1;
				if(MaskCount< ieMSG_MASK_MAX_COUNT){
					tUc =0;
					if(tMaskMsg.Body.P0 && tMaskMsg.Body.P1){
						if( (tMaskMsg.Body.P0<=dPORT_MAX_COUNT) && (tMaskMsg.Body.P1<=dEP_MAX_COUNT_PER_PORT) ){
							tUc = Port[tMaskMsg.Body.P0-1].UcList[tMaskMsg.Body.P1-1].Full;
						}
					}
					tMaskMsg.Body.UserCode = tUc;
					AddMaskMsg2DymList(MaskCount, tMaskMsg);
					MaskCount++;
				}else{
					//Error Here
					Error = 1;
					suExit = 1;
					break;
				}
				tMaskMsg.Body.P0--;
				if(tMaskMsg.Body.P0<dPORT_MAX_COUNT){
					tMaskMsg.Body.P1--;
					Port[tMaskMsg.Body.P0].StateTable[tMaskMsg.Body.P1] |= dEpStateBitMask;
					tMaskMsg.D32[4] = Port[tMaskMsg.Body.P0].UcList[tMaskMsg.Body.P1].Full;
				}
				//Add to CRT____________________________________
				if(MainCtl.fData.Split.NetworkMode == 1){
					aRC.D32[0] = tMaskMsg.D32[0];
					aRC.D32[1] = tMaskMsg.D32[1];
					aRC.D32[2] = tMaskMsg.D32[2];
					aRC.D32[3] = tMaskMsg.D32[3];
					FecBusCRT.iFecBusCRTOperateInsert(aRC);
				}
				//______________________________________________
			}else{
				suExit = 1;
			}
		}
		if(suExit)break;
	}
	sWaitChipIdle();
	fAddr = dFlashAddrMask + dMaskCrcCntOffset;
	nor_read_page(dFlashMaskChip, fAddr, ReadWriteData.D8);
	rCnt = ReadWriteData.D32[0];
	rCrc = ReadWriteData.D32[1];
	this->Crc = rCrc;
	Crc32Feed(rCnt);
	if(MaskCount==0 and rCnt==0 and rCrc==dMaskNonDataIdf){
		if(CrcFlag){
			CrcFlag=0;
			CtlCenter.ieAddBrdFaultCancel(41,TBoardFaultType::StorageMaskData);
		}
	}else{
		if(Error==0 and rCrc==Crc32GetResult()){
			if(CrcFlag){
				CrcFlag=0;
				CtlCenter.ieAddBrdFaultCancel(41,TBoardFaultType::StorageMaskData);
			}
		}else{
			//error here
			CrcFlag=1;
			CtlCenter.ieAddBrdFault(41,TBoardFaultType::StorageMaskData);
		}
	}
	return MaskCount;
}


void TDevMask::PermissionCmdReturnDraw()
{
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);

}

void TDevMask::PermissionCmdGO()
{
	TDMix aFullPath;
	unsigned int tUc;
	if(PermissionCmd){
		SettingData.TimeOutTick = 0;
		SettingData.RecOK = 0;
		SettingData.IsGoMask = 1;
		SendToDevice(1);
		IList->STextLastState.TextClear();
		IList->STextLastState.Show();
	}else{
		if(Path0 && (Path0 <= dPORT_MAX_COUNT)){
			
		}else{
			return;
		}
		if(MainCtl.fData.Split.ExistTablePort[Path0-1] == 0xA5){
			SettingData.TimeOutTick = 0;
			SettingData.RecOK = 0;
			SettingData.IsGoMask = 2;
			SendToDevice(0);
			IList->STextLastState.TextClear();
			IList->STextLastState.Show();
		}else{
			if(MainCtl.fData.Split.NetworkMode == 1){
				if(1){
					aFullPath.D8[0] = MainCtl.fData.Split.MyNum;
					aFullPath.D8[1] = Path0;
					aFullPath.D8[2] = Path1;
					aFullPath.D8[3] = Path0;
					tUc = 0;
					if(Path0 && Path1){
						if( (Path0 <=dPORT_MAX_COUNT) && (Path1 <= dEP_MAX_COUNT_PER_PORT) ){
							tUc = Port[Path0-1].UcList[Path1-1].Full;
						}
					}
					Record.OperateAdd_UnMask(aFullPath, dType, tUc);
					ExistEntryCount = ReadMaskFromFlash2Temp(0);
					AddRemoveViaTempMsgList(Path0, Path1,0);
					WriteToFlashMaster();
					ExistEntryCount = ReadMaskFromFlash2Temp(0);
					CombMsg4Master();
					(void)CheckStorageData();
					ExRecord.SetStateCodeMask();
					FecBusCRT.SetStateCodeMask();
					if(CrcFlag==0){
						if(LanguageEnCn==0)IList->STextLastState.SetText("取消成功",24);
						else IList->STextLastState.SetText("Clear OK  ",24);
					}else{
						if(LanguageEnCn==0)IList->STextLastState.SetText("取消失败",24);
						else IList->STextLastState.SetText("Clr Failed",24);
					}
					IList->STextLastState.Show();
					SettingData.IsGoMask = 0;
					
					if(CtlCenter.ieMsgMask.Count == 0){
						if(AlarmSoundWhat > 3)AlarmSoundWhat = 0x07;
					}
				}					
			}else{
				if(1){
					aFullPath.D8[0] = MainCtl.fData.Split.MyNum;
					aFullPath.D8[1] = Path0;
					aFullPath.D8[2] = Path1;
					aFullPath.D8[3] = 0;
					tUc = 0;
					if(Path0 && Path1){
						if( (Path0 <=dPORT_MAX_COUNT) && (Path1 <= dEP_MAX_COUNT_PER_PORT) ){
							tUc = Port[Path0-1].UcList[Path1-1].Full;
						}
					}
					Record.OperateAdd_UnMask(aFullPath, dType, tUc);
					ExRecord.SetStateCodeMask();
					FecBusCRT.SetStateCodeMask();
					AddRemoveViaCtlCenterMsg(Path0, Path1, tUc, 0);
					WriteToFlashSlave();
					CtlCenter.MaskCountSet(ReadMaskFromFlash());
					if(CrcFlag==0){
						if(LanguageEnCn==0)IList->STextLastState.SetText("取消成功",24);
						else IList->STextLastState.SetText("Clear OK  ",24);
					}else{
						if(LanguageEnCn==0)IList->STextLastState.SetText("取消失败",24);
						else IList->STextLastState.SetText("Clr Failed",24);
					}
					IList->STextLastState.Show();
					SettingData.IsGoMask = 0;
					
					if(CtlCenter.ieMsgMask.Count == 0){
						if(AlarmSoundWhat > 3)AlarmSoundWhat = 0x07;
					}
				}
			}
		}
	}
}

void TDevMask::CheckToDo()
{
	unsigned char tDType, tSta;
	if( (Path0Avalble) && (Path1Avalble) ){
		tDType = Port[Path0-1].dTypeTable[Path1-1];
		tSta = Port[Path0-1].StateTable[Path1-1];
		if( (tDType == dgTYPE_HAND_REPORT) || (tDType == dgType_VAALRAM) ){
			SettingData.DevTypeNotAllow = 1;
		}else{
			SettingData.DevTypeNotAllow = 0;
		}
		if( tSta & (dEpStateBitFire + dEpStateBitStart + dEpStateBitSv + dEpStateBitFault + dEpStateBitFeedBack + dEpStateBitDelayStart) ) {
			SettingData.DevStateNotAllow = 1;
		}else{
			SettingData.DevStateNotAllow = 0;
		}
		SettingData.PathError = 0;
	}else{
		SettingData.PathError = 1;
	}
}

int TDevMask::CheckInputErrState(int IsMask)
{
	if(SettingData.PathError){
		if(LanguageEnCn==0)IList->STextLastState.SetText("路径错误",24);
		else IList->STextLastState.SetText("Path Err ",24);
		IList->STextLastState.Show();
		return 0;
	}else
	if(SettingData.DevStateNotAllow){
		if(LanguageEnCn==0)IList->STextLastState.SetText("状态不允许",24);
		else IList->STextLastState.SetText("State Err",24);
		IList->STextLastState.Show();
		return 0;
	}else
	if(SettingData.DevTypeNotAllow){
		if(LanguageEnCn==0)IList->STextLastState.SetText("类型不允许",24);
		else IList->STextLastState.SetText("DType Err",24);
		IList->STextLastState.Show();
		return 0;
	}
	if(IsMask){
		if(CtlCenter.ieMsgMask.Count >= ieMSG_MASK_MAX_COUNT){
			if(LanguageEnCn==0)IList->STextLastState.SetText("数量达上限",24);
			else IList->STextLastState.SetText("Count Err",24);
			IList->STextLastState.Show();
			return 0;
		}
	}
	return 1;
}


TGuiMsgReturn TDevMask::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	unsigned char aType;
		
	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();
		}
		return aMsg;
	}
	
	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){
				IList->EditPortNum.KeyIn(aKey);
				int iVal = IList->EditPortNum.Str.ToInteger();
				if( (iVal > 0) && (iVal <= dPORT_MAX_COUNT) ){
					IList->EditPortNum.SelectedColor = clBlue;
					Path0 = iVal;
					Path0Avalble = 1;
				}else{
					IList->EditPortNum.SelectedColor = clRed;
					Path0Avalble = 0;
				}
				IList->EditPortNum.Show();
				ShowItemDetail();
			}else
			if(Tag.Inx == 1){
				IList->EditAddrNum.KeyIn(aKey);
				int iVal = IList->EditAddrNum.Str.ToInteger();
				if( (iVal > 0) && (iVal <= dEP_MAX_COUNT_PER_PORT) ){
					IList->EditAddrNum.SelectedColor = clBlue;
					Path1 = iVal;
					Path1Avalble = 1;
				}else{
					IList->EditAddrNum.SelectedColor = clRed;
					Path1Avalble = 0;
				}
				IList->EditAddrNum.Show();
				ShowItemDetail();
			}
			break;
	}
	if(aKey == VK_UP){
		if(Tag.Inx>0){
			if(Tag.Inx == 1){
				if(Path1Avalble){
					ClearSelected();
					Tag.Inx--;
					Check4Selected();
				}
			}else{
				ClearSelected();
				Tag.Inx--;
				Check4Selected();
			}
		}else{
			if(Path0Avalble){
				ClearSelected();
				Tag.Inx=3;
				Check4Selected();
			}
		}
	}else
	if(aKey == VK_DOWN){
		if(Tag.Inx<3){
			if(Tag.Inx == 0){
				if(Path0Avalble){
					ClearSelected();
					Tag.Inx++;
					Check4Selected();
				}
			}else
			if(Tag.Inx == 1){
				if(Path1Avalble){
					ClearSelected();
					Tag.Inx++;
					Check4Selected();
				}
			}else{
				ClearSelected();
				Tag.Inx++;
				Check4Selected();
			}
		}else{
			ClearSelected();
			Tag.Inx=0;
			Check4Selected();
		}
	}else
	if(aKey == VK_F1){
		//Go Modify
	}else
	if(aKey == VK_TAB){

	}
	if(aKey == VK_EXECUTE){
		//Save Data Here
		//Addr Mode
		if(Path0Avalble && Path1Avalble && (IsRegisted || (Tag.Inx == 3) ) ){
			if(Tag.Inx == 2){
				//Triget To Mask Here
				CheckToDo();
				if(CheckInputErrState(1)){
					if(SystemPermission > 1){
						PermissionCmd = 1;
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						PermissionCmd = 1;
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 2);
					}
				}
			}else
			if(Tag.Inx == 3){
				//Triget To UnMask Here
				CheckToDo();
				if(CheckInputErrState(0)){
					if(SystemPermission > 1){
						PermissionCmd = 0;
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						PermissionCmd = 0;
						PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 2);
					}
				}
			}
		}
	}else
	if(aKey == VK_RETURN){
		IsShow = 0;
		SettingData.IsGoMask = 0;
		SettingData.RecOK = 0;
		aMsg = guiMsgReturn;
	}
	return aMsg;	
}

void TDevMask::SendToDevice(int IsAdd)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	unsigned char aP0,aP1;
	
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	if(IsAdd == 1){
		aIde.BitF.Cmd = cmEpDeviceMask;
	}else{
		aIde.BitF.Cmd = cmEpDeviceMaskRemove;
	}

	Len = 4;
	
	//Prm 
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = Path0;
	Data[3] = Path1;
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;

	//InnerCan.CmdSendCustom(aIde, Data, Len);
	
	InnerCan.Buf4GuiSend.Insert(aIde, Data, Len);
}

void TDevMask::PushOneDataRec(unsigned char aCmd, unsigned char CtlNum, unsigned char aP0, unsigned char aP1)
{
	if(IsShow){
		if(SettingData.IsGoMask == 1){
			if(aCmd == cmEpDeviceMask){
				if( (aP0 == Path0) && (aP1 == Path1) ) {
					SettingData.RecOK = 1;
				}
			}
		}else
		if(SettingData.IsGoMask == 2){
			if(aCmd == cmEpDeviceMaskRemove){
				if( (aP0 == Path0) && (aP1 == Path1) ) {
					SettingData.RecOK = 1;
				}
			}
		}
	}
}

void TDevMask::Task1000Ms()
{
	TDMix aFullPath;
	unsigned int tUc;
	if(IsSelfChecking)return;
	if(IsShow){
		if( (Path0) and (Path1 ) ){
			if( (Path0 <= dPORT_MAX_COUNT) && (Path1 <= dEP_MAX_COUNT_PER_PORT) ){
				if(IsRegisted){
					const unsigned char tSta = Port[Path0-1].StateTable[Path1-1];
					DrawState(tSta);
				}
			}
		}
		if(SettingData.IsGoMask){
			SettingData.TimeOutTick++;
			if(SettingData.TimeOutTick > 6){
				SettingData.IsGoMask = 0;
				if(LanguageEnCn==0)IList->STextLastState.SetText("操作失败",24);
				else IList->STextLastState.SetText("OPR Fail",24);
				IList->STextLastState.Show();
			}else{
				if(MainCtl.fData.Split.NetworkMode == 1){
					if(SettingData.IsGoMask == 1){
						if(SettingData.RecOK){
							aFullPath.D8[0] = MainCtl.fData.Split.MyNum;
							aFullPath.D8[1] = Path0;
							aFullPath.D8[2] = Path1;
							aFullPath.D8[3] = 0;
							tUc = 0;
							if(Path0 && Path1){
								if( (Path0 <=dPORT_MAX_COUNT) && (Path1 <= dEP_MAX_COUNT_PER_PORT) ){
									tUc = Port[Path0-1].UcList[Path1-1].Full;
								}
							}
							Record.OperateAdd_Mask(aFullPath, dType, tUc);
							ExistEntryCount = ReadMaskFromFlash2Temp(0);
							AddRemoveViaTempMsgList(Path0, Path1,1);
							WriteToFlashMaster();
							ExistEntryCount = ReadMaskFromFlash2Temp(0);
							CombMsg4Master();
							(void)CheckStorageData();
							ExRecord.SetStateCodeMask();
							FecBusCRT.SetStateCodeMask();
							if(CrcFlag==0){
								if(LanguageEnCn==0)IList->STextLastState.SetText("屏蔽成功",24);
								else IList->STextLastState.SetText("Blocked OK",24);
							}else{
								if(LanguageEnCn==0)IList->STextLastState.SetText("屏蔽失败",24);
								else IList->STextLastState.SetText("Blk Failed",24);
							}
							IList->STextLastState.Show();
							SettingData.IsGoMask = 0;
							
							if(CtlCenter.ieMsgMask.Count >0){
								if(AlarmSoundWhat > 3)AlarmSoundWhat = 4;
							}
						}
					}else
					if(SettingData.IsGoMask == 2){
						if(SettingData.RecOK){
							aFullPath.D8[0] = MainCtl.fData.Split.MyNum;
							aFullPath.D8[1] = Path0;
							aFullPath.D8[2] = Path1;
							aFullPath.D8[3] = Path0;
							tUc = 0;
							if(Path0 && Path1){
								if( (Path0 <=dPORT_MAX_COUNT) && (Path1 <= dEP_MAX_COUNT_PER_PORT) ){
									tUc = Port[Path0-1].UcList[Path1-1].Full;
								}
							}
							Record.OperateAdd_UnMask(aFullPath, dType, tUc);
							ExistEntryCount = ReadMaskFromFlash2Temp(0);
							AddRemoveViaTempMsgList(Path0, Path1,0);
							WriteToFlashMaster();
							ExistEntryCount = ReadMaskFromFlash2Temp(0);
							CombMsg4Master();
							(void)CheckStorageData();
							ExRecord.SetStateCodeMask();
							FecBusCRT.SetStateCodeMask();
							if(CrcFlag==0){
								if(LanguageEnCn==0)IList->STextLastState.SetText("取消成功",24);
								else IList->STextLastState.SetText("Clear OK  ",24);
							}else{
								if(LanguageEnCn==0)IList->STextLastState.SetText("取消失败",24);
								else IList->STextLastState.SetText("Clr Failed",24);
							}
							IList->STextLastState.Show();
							SettingData.IsGoMask = 0;
							
							if(CtlCenter.ieMsgMask.Count == 0){
								if(AlarmSoundWhat > 3)AlarmSoundWhat = 0x07;
							}
						}
					}						
				}else{
					if(SettingData.IsGoMask == 1){
						if(SettingData.RecOK){
							aFullPath.D8[0] = MainCtl.fData.Split.MyNum;
							aFullPath.D8[1] = Path0;
							aFullPath.D8[2] = Path1;
							aFullPath.D8[3] = 0;
							tUc = 0;
							if(Path0 && Path1){
								if( (Path0 <=dPORT_MAX_COUNT) && (Path1 <= dEP_MAX_COUNT_PER_PORT) ){
									tUc = Port[Path0-1].UcList[Path1-1].Full;
								}
							}
							Record.OperateAdd_Mask(aFullPath, dType, tUc);
							AddRemoveViaCtlCenterMsg(Path0, Path1,tUc,1);
							WriteToFlashSlave();
							CtlCenter.MaskCountSet(ReadMaskFromFlash());
							if(CrcFlag==0){
								if(LanguageEnCn==0)IList->STextLastState.SetText("屏蔽成功",24);
								else IList->STextLastState.SetText("Blocked OK",24);
							}else{
								if(LanguageEnCn==0)IList->STextLastState.SetText("屏蔽失败",24);
								else IList->STextLastState.SetText("Blk Failed",24);
							}
							IList->STextLastState.Show();
							SettingData.IsGoMask = 0;
							
							if(CtlCenter.ieMsgMask.Count >0){
								if(AlarmSoundWhat > 3)AlarmSoundWhat = 4;
							}
						}
					}else
					if(SettingData.IsGoMask == 2){
						if(SettingData.RecOK){
							aFullPath.D8[0] = MainCtl.fData.Split.MyNum;
							aFullPath.D8[1] = Path0;
							aFullPath.D8[2] = Path1;
							aFullPath.D8[3] = 0;
							tUc = 0;
							if(Path0 && Path1){
								if( (Path0 <=dPORT_MAX_COUNT) && (Path1 <= dEP_MAX_COUNT_PER_PORT) ){
									tUc = Port[Path0-1].UcList[Path1-1].Full;
								}
							}
							Record.OperateAdd_UnMask(aFullPath, dType, tUc);
							ExRecord.SetStateCodeMask();
							FecBusCRT.SetStateCodeMask();
							AddRemoveViaCtlCenterMsg(Path0, Path1, tUc, 0);
							WriteToFlashSlave();
							CtlCenter.MaskCountSet(ReadMaskFromFlash());
							if(CrcFlag==0){
								if(LanguageEnCn==0)IList->STextLastState.SetText("取消成功",24);
								else IList->STextLastState.SetText("Clear OK  ",24);
							}else{
								if(LanguageEnCn==0)IList->STextLastState.SetText("取消失败",24);
								else IList->STextLastState.SetText("Clr Failed",24);
							}
							IList->STextLastState.Show();
							SettingData.IsGoMask = 0;
							
							if(CtlCenter.ieMsgMask.Count == 0){
								if(AlarmSoundWhat > 3)AlarmSoundWhat = 0x07;
							}
						}
					}
				}
			}
		}
	}
}

void TDevMask::RecallCrcFaultMsg()
{
	if(CrcFlag)
		CtlCenter.ieAddBrdFault(41,TBoardFaultType::StorageMaskData);
}

void TDevMask::sRecoverSetDefault()
{
	volatile int i, x, aInx;
	volatile unsigned int fAddr, AddrSd;
	volatile unsigned int GetSta;
	
	//User Code and Assign Type
	//First 32 Port
	if(sIList==nullptr)return;
	sIList->ReadWriteData.Reset();
	
	LoopCheck1S();
	do{
		sDelayMs(3);
		GetSta = nor_get_busy_state(dFlashMaskChip);
	}while(GetSta);

	fAddr = dFlashAddrMask;
	for(i=0; i<4; i++){
		LoopCheck1S();
		nor_erase_sector_64k(dFlashMaskChip, fAddr);
		sDelayMs(400);
		do{
			sDelayMs(3);
			GetSta = nor_get_busy_state(dFlashMaskChip);
		}while(GetSta);
		fAddr = fAddr + SizeOf64K;
	}
	//Mark Crc 4 None Data
	fAddr = dFlashAddrMask + dMaskCrcCntOffset;
	sIList->ReadWriteData.D32[0] = 0;
	sIList->ReadWriteData.D32[1] = dMaskNonDataIdf;
	nor_write_page(dFlashMaskChip, fAddr, sIList->ReadWriteData.D8);
	do{
		sDelayMs(5);
		GetSta = nor_get_busy_state(dFlashMaskChip);
	}while(GetSta);
	
	//Clear Backup
	fAddr = dFlashAddrMask_II;
	for(i=0; i<4; i++){
		LoopCheck1S();
		nor_erase_sector_64k(dFlashMaskChip, fAddr);
		sDelayMs(400);
		do{
			sDelayMs(3);
			GetSta = nor_get_busy_state(dFlashMaskChip);
		}while(GetSta);
		fAddr = fAddr + SizeOf64K;
	}
	
	LoopCheck1S();
	CtlCenter.CrcFlag.Mask =0;
	CtlCenter.ieMsgMask.Count = 0;
	CtlCenter.ieMsgMask.ReflashIndex++;
	for(i=0; i<ieMSG_MASK_MAX_COUNT; i++){
		AddrSd = dAddrSdRam_MsgMask + (i * 24);
		*(volatile unsigned int*)(AddrSd) 	= 0;
		*(volatile unsigned int*)(AddrSd + 4) 	= 0;
		*(volatile unsigned int*)(AddrSd + 8)	= 0;
		*(volatile unsigned int*)(AddrSd + 12)	= 0;
		*(volatile unsigned int*)(AddrSd + 16)	= 0;
		*(volatile unsigned int*)(AddrSd + 20)	= 0;
	}
}


int TDevMask::ExtRequst(unsigned char Prm)
{
	IsShow = 0;
	SettingData.IsGoMask = 0;
	SettingData.RecOK = 0;
	return 0;
}









