#include "RuntimeData.h"
#include "DevProperty.h"
#include "DisplayPadDescpDownload.h"
#include "DevMask.h"
#include "server.h"


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

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

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

static const short svGrid1X[8] = {
	16, 128, 246, 392, 600, 620, 630, 640
};

void TDisplayPadDescpDownload::DrawFixText()
{
	if(LanguageEnCn==0){
			TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "工程->层显描述下载");
			TextRender_string24(Lv.Grid.Left[0] +16, Lv.Grid.Top[0] +  4, clNearWhite, ItemColor, "选择要下载的类型");
			TextRender_string24(Lv.Grid.Left[0] +700, Lv.Grid.Top[0] +  4, clNearWhite, ItemColor, "后台运行");
			TextRender_string24(svGrid1X[0] + 32, 	Lv.Grid.Top[0] +  36, 	clNearWhite, 	ItemColor, "烟感");
			TextRender_string24(svGrid1X[1] + 32, 	Lv.Grid.Top[0] +  36, 	clNearWhite, 	ItemColor, "温感");
			TextRender_string24(svGrid1X[2] + 32, 	Lv.Grid.Top[0] +  36, 	clNearWhite, 	ItemColor, "手报");
			TextRender_string24(svGrid1X[3] + 32, 	Lv.Grid.Top[0] +  36, 	clNearWhite, 	ItemColor, "输入模块");
			TextRender_string24(svGrid1X[4] + 32, 	Lv.Grid.Top[0] +  36, 	clNearWhite, 	ItemColor, "输入输出模块");
				
			TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] +  4, clNearWhite, ItemColor, "层显回路号");
			TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 38, clNearWhite, ItemColor, "层显地址号");
			
			TextRender_string24(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] +  4, clNearWhite, ItemColor, "注释起始回路");
			TextRender_string24(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] + 38, clNearWhite, ItemColor, "注释起始地址");
			
			TextRender_string24(Lv.Grid.Left[3] +16, Lv.Grid.Top[3] +  4, clNearWhite, ItemColor, "注释结束回路");
			TextRender_string24(Lv.Grid.Left[3] +16, Lv.Grid.Top[3] + 38, clNearWhite, ItemColor, "注释结束地址");
				
			if(State == Idle){
				Tag.Inx = 0;
				Tag.InxItem = 0;
				TRoundRect::sDrawBySize(Lv.Grid.Left[4] +16, Lv.Grid.Top[4] + 8, 120, 30, 7, clNearWhite, "开始下载", 14, clNearBlack);
				TRoundRect::sDrawBySize(Lv.Grid.Left[4] +154, Lv.Grid.Top[4] + 8, 120, 30, 7, clNearWhite, "结束下载", 14, clNearBlack);
			}else{
				Tag.Inx = 8;
				Tag.InxItem = 0;
				TRoundRect::sDrawBySize(Lv.Grid.Left[4] +16, Lv.Grid.Top[4] + 8, 120, 30, 7, clNearWhite, "开始下载", 14, clSilver);
				TRoundRect::sDrawBySize(Lv.Grid.Left[4] +154, Lv.Grid.Top[4] + 8, 120, 30, 7, clNearWhite, "结束下载", 14, clNearBlack);
			}
				
	}else{
			TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Project->Download Descp To Display Pad");
			TextRender_string24(Lv.Grid.Left[0] +16, Lv.Grid.Top[0] +  4, clNearWhite, ItemColor, "Selecte Dev Types");
			TextRender_string24(Lv.Grid.Left[0] +700, Lv.Grid.Top[0] +  4, clNearWhite, ItemColor, "BackEnd");
			TextRender_string24(svGrid1X[0] + 32, 	Lv.Grid.Top[0] +  36, 	clNearWhite, 	ItemColor, "Smoke");
			TextRender_string24(svGrid1X[1] + 32, 	Lv.Grid.Top[0] +  36, 	clNearWhite, 	ItemColor, "Tempe");
			TextRender_string24(svGrid1X[2] + 32, 	Lv.Grid.Top[0] +  36, 	clNearWhite, 	ItemColor, "Hand Pad");
			TextRender_string24(svGrid1X[3] + 32, 	Lv.Grid.Top[0] +  36, 	clNearWhite, 	ItemColor, "Output Module");
			TextRender_string24(svGrid1X[4] + 32, 	Lv.Grid.Top[0] +  36, 	clNearWhite, 	ItemColor, "In&Out Module");
				
			TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] +  4, clNearWhite, ItemColor, "Loop Display");
			TextRender_string24(Lv.Grid.Left[1] +16, Lv.Grid.Top[1] + 38, clNearWhite, ItemColor, "Addr Display");
			
			TextRender_string24(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] +  4, clNearWhite, ItemColor, "Loop Begin");
			TextRender_string24(Lv.Grid.Left[2] +16, Lv.Grid.Top[2] + 38, clNearWhite, ItemColor, "Addr Begin");
			
			TextRender_string24(Lv.Grid.Left[3] +16, Lv.Grid.Top[3] +  4, clNearWhite, ItemColor, "Loop End");
			TextRender_string24(Lv.Grid.Left[3] +16, Lv.Grid.Top[3] + 38, clNearWhite, ItemColor, "Addr End");
				
			if(State == Idle){
				Tag.Inx = 0;
				Tag.InxItem = 0;
				TRoundRect::sDrawBySize(Lv.Grid.Left[4] +16, Lv.Grid.Top[4] + 8, 120, 30, 7, clNearWhite, "Download", 14, clNearBlack);
				TRoundRect::sDrawBySize(Lv.Grid.Left[4] +154, Lv.Grid.Top[4] + 8, 120, 30, 7, clNearWhite, "  Stop  ", 14, clNearBlack);
			}else{
				Tag.Inx = 8;
				Tag.InxItem = 0;
				TRoundRect::sDrawBySize(Lv.Grid.Left[4] +16, Lv.Grid.Top[4] + 8, 120, 30, 7, clNearWhite, "Download", 14, clSilver);
				TRoundRect::sDrawBySize(Lv.Grid.Left[4] +154, Lv.Grid.Top[4] + 8, 120, 30, 7, clNearWhite,"  Stop  ", 14, clNearBlack);
			}	
	}
}

void TDisplayPadDescpDownload::Init()
{
	IsPermission =0;
	SelectedList.Reset();
	DpPort = 1;
	DpAddr = 1;
	StartPort =1;
	EndPort = 2;
	StartAddr = 1;
	EndAddr = 250;
	State = Idle;
	Tag.Inx = 0;
	Tag.InxItem = 0;
	WriteResult =0;
	pPanel = nullptr;
}

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

void TDisplayPadDescpDownload::DrawSelf(void)
{
	RenderBackGround();

	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	
	for(int i=0; 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);
	RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	
	
	

	pPanel->Edit[0].Init(Lv.Grid.Left[1] +174, Lv.Grid.Top[1] + 2, 100,30,0,0,1,clNearBlack);
	pPanel->Edit[0].SetMaxLen(2);
	pPanel->Edit[0].Str.Clear();
	TMyString::sFrom2Dg(DpPort,Text);
	pPanel->Edit[0].SetText(Text, 24);
	pPanel->Edit[0].SetPstTail();
	pPanel->Edit[0].Show(); 
	
	pPanel->Edit[1].Init(Lv.Grid.Left[1] +174, Lv.Grid.Top[1] + 38, 100,30,0,0,1,clNearBlack);
	pPanel->Edit[1].SetMaxLen(3);
	pPanel->Edit[1].Str.Clear();
	TMyString::sFrom3Dg(DpAddr,Text);
	pPanel->Edit[1].SetText(Text, 24);
	pPanel->Edit[1].Show();
	
	pPanel->Edit[2].Init(Lv.Grid.Left[2] +174, Lv.Grid.Top[2] + 2, 100,30,0,0,1,clNearBlack);
	pPanel->Edit[2].SetMaxLen(2);
	pPanel->Edit[2].Str.Clear();
	TMyString::sFrom2Dg(StartPort,Text);
	pPanel->Edit[2].SetText(Text, 24);
	pPanel->Edit[2].Show();
	
	pPanel->Edit[3].Init(Lv.Grid.Left[1] +174, Lv.Grid.Top[2] + 38, 100,30,0,0,1,clNearBlack);
	pPanel->Edit[3].SetMaxLen(3);
	pPanel->Edit[3].Str.Clear();
	TMyString::sFrom3Dg(StartAddr,Text);
	pPanel->Edit[3].SetText(Text, 24);
	pPanel->Edit[3].Show();
	
	pPanel->Edit[4].Init(Lv.Grid.Left[2] +174, Lv.Grid.Top[3] + 2, 100,30,0,0,1,clNearBlack);
	pPanel->Edit[4].SetMaxLen(2);
	pPanel->Edit[4].Str.Clear();
	TMyString::sFrom2Dg(EndPort,Text);
	pPanel->Edit[4].SetText(Text, 24);
	pPanel->Edit[4].Show();
	
	pPanel->Edit[5].Init(Lv.Grid.Left[1] +174, Lv.Grid.Top[3] + 38, 100,30,0,0,1,clNearBlack);
	pPanel->Edit[5].SetMaxLen(3);
	pPanel->Edit[5].Str.Clear();
	TMyString::sFrom3Dg(EndAddr,Text);
	pPanel->Edit[5].SetText(Text, 24);
	pPanel->Edit[5].Show();
	
	DrawFixText();
	
	DrawItemTypeSelected();
	
	DrawPadList();
	
	if(State == Writing)Tag.Inx =7;
	Check4Selected();
	
}

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

void TDisplayPadDescpDownload::FullRedraw(int Prm)
{
	RenderBackGround();

	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	for(int i=0; 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);
	RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	
	DrawFixText();
	pPanel->Edit[0].Show(); 
	pPanel->Edit[1].Show();
	pPanel->Edit[2].Show();
	pPanel->Edit[3].Show();
	pPanel->Edit[4].Show();
	pPanel->Edit[5].Show();
	
	DrawItemTypeSelected();
	
	DrawPadList();
	
	Check4Selected();
	
	if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
}

void TDisplayPadDescpDownload::PermissionCmdReturnDraw()
{
	RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	DrawWriteResult();
}

void TDisplayPadDescpDownload::DrawItemTypeSelected()
{
	if(SelectedList.SenSmoker){
		TRoundSquare::sDrawBySize(svGrid1X[0], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(svGrid1X[0], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	if(SelectedList.SenTemper){
		TRoundSquare::sDrawBySize(svGrid1X[1], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(svGrid1X[1], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	if(SelectedList.HandPadReport){
		TRoundSquare::sDrawBySize(svGrid1X[2], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(svGrid1X[2], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	if(SelectedList.ModuleIn){
		TRoundSquare::sDrawBySize(svGrid1X[3], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(svGrid1X[3], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	if(SelectedList.ModuleInout){
		TRoundSquare::sDrawBySize(svGrid1X[4], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(svGrid1X[4], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
}

void TDisplayPadDescpDownload::DrawWriteResult()
{
	if(IsShow == 0)return;
	const int OffSet = 200;
	if(LanguageEnCn==0){
		if( (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0) ){
			if(State == Finished){
				TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 2, 	clNearWhite, PanelColor, "传输完成");
			}else
			if(State == Writing){
				TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 2, 	clNearWhite, PanelColor, "正在传输");	
			}else{
				TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 2, 	clNearWhite, PanelColor, "无传输  ");
			}
			
			TextRender_string24(Lv.Panel.Left +160, 		Lv.Panel.Top + 2, 	clNearWhite, PanelColor, "已用时(秒):");
			TextDigitRender8_24(Lv.Panel.Left +292, 		Lv.Panel.Top + 2, 	clNearWhite, PanelColor, RunData.WasteTime);
			
			TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 32, 	clNearWhite, PanelColor, "总数量:");
			TextDigitRender5_24(Lv.Panel.Left +OffSet, 	Lv.Panel.Top + 32, 	clNearWhite, PanelColor, RunData.Total);
			
			TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 62, 	clNearWhite, PanelColor, "当前写入回路号:");
			TextDigitRender2_24(Lv.Panel.Left +OffSet, 	Lv.Panel.Top + 62, 	clNearWhite, PanelColor, RunData.WritingPort);
			
			TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 92, 	clNearWhite, PanelColor, "当前写入地址号:");
			TextDigitRender3_24(Lv.Panel.Left +OffSet, 	Lv.Panel.Top + 92, 	clNearWhite, PanelColor, RunData.WritingAddr);
			
			TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 122, 	clNearWhite, PanelColor, "已写入数量:");
			TextDigitRender5_24(Lv.Panel.Left +OffSet, 	Lv.Panel.Top + 122, 	clNearWhite, PanelColor, RunData.WroteCount);
			
			TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 152, 	clNearWhite, PanelColor, "写入失败数量:");
			TextDigitRender5_24(Lv.Panel.Left +OffSet, 	Lv.Panel.Top + 152, 	clRed,	 PanelColor, RunData.FailCount);
			
			TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 182, 	clNearWhite, PanelColor, "重试次数:");
			TextDigitRender6_24(Lv.Panel.Left +OffSet, 	Lv.Panel.Top + 182, 	clNearWhite, PanelColor, RunData.TotalRetryTimes);
		}
	}else{
		if( (IsSelfChecking == 0) && (PmsIntervene.IsShow == 0) ){
			if(State == Finished){
				TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 2, 	clNearWhite, PanelColor, "Completed  ");
			}else
			if(State == Writing){
				TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 2, 	clNearWhite, PanelColor, "Transmiting");	
			}else{
				TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 2, 	clNearWhite, PanelColor, "IDLE       ");
			}
			
			TextRender_string24(Lv.Panel.Left +160, 		Lv.Panel.Top + 2, 	clNearWhite, PanelColor, "Waste Time:");
			TextDigitRender8_24(Lv.Panel.Left +292, 		Lv.Panel.Top + 2, 	clNearWhite, PanelColor, RunData.WasteTime);
			
			TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 32, 	clNearWhite, PanelColor, "Total:");
			TextDigitRender5_24(Lv.Panel.Left +OffSet, 	Lv.Panel.Top + 32, 	clNearWhite, PanelColor, RunData.Total);
			
			TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 62, 	clNearWhite, PanelColor, "Writing Loop:");
			TextDigitRender2_24(Lv.Panel.Left +OffSet, 	Lv.Panel.Top + 62, 	clNearWhite, PanelColor, RunData.WritingPort);
			
			TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 92, 	clNearWhite, PanelColor, "Writing Addr:");
			TextDigitRender3_24(Lv.Panel.Left +OffSet, 	Lv.Panel.Top + 92, 	clNearWhite, PanelColor, RunData.WritingAddr);
			
			TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 122, 	clNearWhite, PanelColor, "Been Finished:");
			TextDigitRender5_24(Lv.Panel.Left +OffSet, 	Lv.Panel.Top + 122, 	clNearWhite, PanelColor, RunData.WroteCount);
			
			TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 152, 	clNearWhite, PanelColor, "Failed To Write:");
			TextDigitRender5_24(Lv.Panel.Left +OffSet, 	Lv.Panel.Top + 152, 	clRed,	 PanelColor, RunData.FailCount);
			
			TextRender_string24(Lv.Panel.Left +4, 		Lv.Panel.Top + 182, 	clNearWhite, PanelColor, "Retey Times:");
			TextDigitRender6_24(Lv.Panel.Left +OffSet, 	Lv.Panel.Top + 182, 	clNearWhite, PanelColor, RunData.TotalRetryTimes);
		}	
	}
}

void TDisplayPadDescpDownload::DrawPadList()
{
	unsigned char tP0, tP1;
	int x,y, Cnt;
	RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	if(LanguageEnCn==0)TextRender_string24(Lv.PanelSmall.Left + 6, Lv.PanelSmall.Top + 2, clNearWhite, SmallPanelColor, 	"回路层显地址:");
	else TextRender_string24(Lv.PanelSmall.Left + 6, Lv.PanelSmall.Top + 2, clNearWhite, SmallPanelColor, 			"List Of Disp:");
	tP0 = DpPort -1;
	if(tP0 < dPORT_MAX_COUNT){
		Cnt = 0;
		x = Lv.PanelSmall.Left + 6;
		y = Lv.PanelSmall.Top + 2 + 32;
		if(MainCtl.fData.Split.ExistTablePort[tP0]){
			for(tP1=0; tP1<dEP_MAX_COUNT_PER_PORT; tP1++){
				if(Port[tP0].dTypeTable[tP1] == dgTYPE_DISPLAY_PAD){
					Cnt++;
					if(Cnt < 7){
						x = Lv.PanelSmall.Left + 2 + 156 + 6 + ((Cnt-1) * 48);
						y = Lv.PanelSmall.Top + 2;
						TextDigitRender3_24(x, 	y, clNearWhite, SmallPanelColor, tP1+1);
					}else
					if(Cnt < 17){
						x = Lv.PanelSmall.Left + 2 +6 + ((Cnt-7) * 48);
						y = Lv.PanelSmall.Top + 2 +32;
						TextDigitRender3_24(x, 	y, clNearWhite, SmallPanelColor, tP1+1);
					}
				}
			}
		}
	}
}

void TDisplayPadDescpDownload::InitWriteData()
{
	RunData.Reset();
	CheckScope();
}

void TDisplayPadDescpDownload::CheckScope()
{
	int tP,tA, tP0,tP1;
	RunData.Total =0;
	if(StartPort==0)return;
	if(EndPort ==0)return;
	if(StartAddr==0)return;
	if(EndAddr == 0)return;
	if(StartPort>dPORT_MAX_COUNT)return;
	if(EndPort>dPORT_MAX_COUNT)return;
	if(StartAddr>dEP_MAX_COUNT_PER_PORT)return;
	if(EndAddr>dEP_MAX_COUNT_PER_PORT)return;

	tP = StartPort; 
	tA = StartAddr;
	for(;tP<=EndPort;tP++){
		tP0 = tP-1;
		for(; tA<dEP_MAX_COUNT_PER_PORT; tA++){
			tP1 = tA -1;
			//if(MainCtl.fData.Split.ExistTablePort[tP0]){
				if(SelectedList.SenSmoker){
					if(Port[tP0].dTypeTable[tP1] == dgTYPE_SMOKE_DETECTOR){RunData.Total++;continue;}
				}
				if(SelectedList.SenTemper){
					if(Port[tP0].dTypeTable[tP1] == dgTYPE_TEMPE_DETECTOR){RunData.Total++;continue;}
				}
				if(SelectedList.HandPadReport){
					if(Port[tP0].dTypeTable[tP1] == dgTYPE_HAND_REPORT){RunData.Total++;continue;}
				}
				if(SelectedList.ModuleIn){
					if(Port[tP0].dTypeTable[tP1] == dgTYPE_IN_MODULE){RunData.Total++;continue;}
				}
				if(SelectedList.ModuleInout){
					if( 	(Port[tP0].dTypeTable[tP1] == dgTYPE_INOUT_MODULE_NonSource) ||
						(Port[tP0].dTypeTable[tP1] == dgTYPE_INOUT_MODULE_Source) ){RunData.Total++;continue;}
				}
				if(Port[tP0].dTypeTable[tP1] == 0){RunData.Total++;}
			//}
			if( (tP >= EndPort) && (tA >= EndAddr) ){
				return;
			}
		}
		tA = 1;
	}
}

void TDisplayPadDescpDownload::DoTransmiting()
{
	int GoContinue =0;
	int GoRetry =0;
	int GoSend = 0;
	unsigned char tP, tA, tP0, tP1;
	if(RpyData.HasSend){
		//Wait
		RpyData.WaitTick++;
		if(RpyData.RecResult){
			if(RpyData.RecSta == 1){
				RpyData.HasSend =0;
				GoContinue =1;
				RunData.WroteCount++;
			}else{
				if(RunData.RetryTimes < 3){
					RunData.RetryTimes++;
					GoRetry =1;
					RunData.TotalRetryTimes++;
				}else{
					RpyData.HasSend =0;
					GoContinue =1;
					RunData.FailCount++;
				}
			}
		}else
		if(RpyData.WaitTick>10){
			if(RunData.RetryTimes < 3){
				RunData.RetryTimes++;
				GoRetry =1;
				RunData.TotalRetryTimes++;
			}else{
				RpyData.HasSend =0;
				GoContinue =1;
				RunData.FailCount++;
			}
		}
	}else{
		GoContinue =1;
	}
	
	if(GoRetry){
		SendCmdWriteOnePoint(RunData.WritingPort, RunData.WritingAddr);
		RpyData.HasSend =1;
	}
	
	if(GoContinue){
		if( (RunData.WritingPort > EndPort) || ((RunData.WritingPort >= EndPort) && (RunData.WritingAddr > EndAddr)) ) {
			State = Finished;
			return;
		}
		RunData.RetryTimes =0;
		if(1){
			if( (RunData.WritingPort == 0) || (RunData.WritingAddr ==0) ){
				tP = StartPort;
				tA = StartAddr;
			}else{
				tP = RunData.WritingPort;
				tA = RunData.WritingAddr;
				if(tA >= dEP_MAX_COUNT_PER_PORT){
					if(tP >= dPORT_MAX_COUNT){
						State = Finished;
						return;
					}else{
						tA =1;
						tP++;
					}
				}else{
					tA++;
				}
			}
			for(; tP<=EndPort; tP++){
				tP0 =tP-1;
				for(; tA<=dEP_MAX_COUNT_PER_PORT; tA++){
					tP1 =tA-1;
					if(SelectedList.SenSmoker){
						if(Port[tP0].dTypeTable[tP1] == dgTYPE_SMOKE_DETECTOR){
							GoSend =1;
						}
					}
					if(SelectedList.SenTemper){
						if(Port[tP0].dTypeTable[tP1] == dgTYPE_TEMPE_DETECTOR){
							GoSend =1;
						}
					}
					if(SelectedList.HandPadReport){
						if(Port[tP0].dTypeTable[tP1] == dgTYPE_HAND_REPORT){
							GoSend =1;
						}
					}
					if(SelectedList.ModuleIn){
						if(Port[tP0].dTypeTable[tP1] == dgTYPE_IN_MODULE){
							GoSend =1;
						}
					}
					if(SelectedList.ModuleInout){
						if( 	(Port[tP0].dTypeTable[tP1] == dgTYPE_INOUT_MODULE_NonSource) ||
							(Port[tP0].dTypeTable[tP1] == dgTYPE_INOUT_MODULE_Source) ){
								GoSend =1;
						}
					}
					if(Port[tP0].dTypeTable[tP1] == 0)
						GoSend =1;
					if(GoSend){
						RunData.WritingPort = tP;
						RunData.WritingAddr = tA;
						SendCmdWriteOnePoint(RunData.WritingPort, RunData.WritingAddr);
						RpyData.HasSend =1;
						RpyData.WaitTick =0;
						RpyData.RecSta =0;
						RpyData.RecResult =0;
						goto DoNothingReturnHere;
					}
					if( (tP > EndPort) || ((tP >= EndPort) && (tA > EndAddr)) ) {
						State = Finished;
						goto DoNothingReturnHere;
					}
				}
				tA =1;
			}
			if( (tP > EndPort) || ((tP >= EndPort) && (tA > EndAddr)) ) {
				State = Finished;
				return;
			}
			DoNothingReturnHere:
				return;
			
		}
	}
}

void TDisplayPadDescpDownload::ClearSelected(void)
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			switch(Tag.InxItem){
				case 0: TRoundSquare::sSelected(svGrid1X[0], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, 4, ItemColor); break;
				case 1: TRoundSquare::sSelected(svGrid1X[1], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, 4, ItemColor); break;
				case 2: TRoundSquare::sSelected(svGrid1X[2], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, 4, ItemColor); break;
				case 3: TRoundSquare::sSelected(svGrid1X[3], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, 4, ItemColor); break;
				case 4: TRoundSquare::sSelected(svGrid1X[4], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, 4, ItemColor); break;
				case 5: TRoundSquare::sSelected(svGrid1X[5], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, 4, ItemColor); break;
			}
			break;
		case 1:
			pPanel->Edit[0].Selected = 0;
			pPanel->Edit[0].SetPstTail();
			pPanel->Edit[0].Show();
			break;
		case 2:
			pPanel->Edit[1].Selected = 0;
			pPanel->Edit[1].SetPstTail();
			pPanel->Edit[1].Show();
			break;
		case 3:
			pPanel->Edit[2].Selected = 0;
			pPanel->Edit[2].SetPstTail();
			pPanel->Edit[2].Show();
			break;
		case 4:
			pPanel->Edit[3].Selected = 0;
			pPanel->Edit[3].SetPstTail();
			pPanel->Edit[3].Show();
			break;
		case 5:
			pPanel->Edit[4].Selected = 0;
			pPanel->Edit[4].SetPstTail();
			pPanel->Edit[4].Show();
			break;
		case 6:
			pPanel->Edit[5].Selected = 0;
			pPanel->Edit[5].SetPstTail();
			pPanel->Edit[5].Show();
			break;
		case 7:
			if(LanguageEnCn==0)tText="开始下载"; else tText="DownLoad";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +16, Lv.Grid.Top[4] + 8, 120, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 8:
			if(LanguageEnCn==0)tText="结束下载"; else tText="  Stop  ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +154, Lv.Grid.Top[4] + 8, 120, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
	}		
}

void TDisplayPadDescpDownload::Check4Selected(void)
{
	char *tText;
	switch(Tag.Inx){
		case 0:
			switch(Tag.InxItem){
				case 0: TRoundSquare::sSelected(svGrid1X[0], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, 4, clNearWhite); break;
				case 1: TRoundSquare::sSelected(svGrid1X[1], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, 4, clNearWhite); break;
				case 2: TRoundSquare::sSelected(svGrid1X[2], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, 4, clNearWhite); break;
				case 3: TRoundSquare::sSelected(svGrid1X[3], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, 4, clNearWhite); break;
				case 4: TRoundSquare::sSelected(svGrid1X[4], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, 4, clNearWhite); break;
				case 5: TRoundSquare::sSelected(svGrid1X[5], Lv.Grid.Top[0] + 38, dWWidth, dWHeigh, 4, clNearWhite); break;
			}
			break;
		case 1:
			pPanel->Edit[0].Selected = 1;
			pPanel->Edit[0].SetPstTail();
			pPanel->Edit[0].Show();
			pPanel->Edit[0].ShowCursor();
			break;
		case 2:
			pPanel->Edit[1].Selected = 1;
			pPanel->Edit[1].SetPstTail();
			pPanel->Edit[1].Show();
			pPanel->Edit[1].ShowCursor();
			break;
		case 3:
			pPanel->Edit[2].Selected = 1;
			pPanel->Edit[2].SetPstTail();
			pPanel->Edit[2].Show();
			pPanel->Edit[2].ShowCursor();
			break;
		case 4:
			pPanel->Edit[3].Selected = 1;
			pPanel->Edit[3].SetPstTail();
			pPanel->Edit[3].Show();
			pPanel->Edit[3].ShowCursor();
			break;
		case 5:
			pPanel->Edit[4].Selected = 1;
			pPanel->Edit[4].SetPstTail();
			pPanel->Edit[4].Show();
			pPanel->Edit[4].ShowCursor();
			break;
		case 6:
			pPanel->Edit[5].Selected = 1;
			pPanel->Edit[5].SetPstTail();
			pPanel->Edit[5].Show();
			pPanel->Edit[5].ShowCursor();
			break;
		case 7:
			if(LanguageEnCn==0)tText="开始下载"; else tText="DownLoad";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +16, Lv.Grid.Top[4] + 8, 120, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
		case 8:
			if(LanguageEnCn==0)tText="结束下载"; else tText="  Stop  ";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +154, Lv.Grid.Top[4] + 8, 120, 30, 7, clBlue, tText, 14, clNearWhite);
			break;
	}		
}

void TDisplayPadDescpDownload::PushInnerCanData(unsigned char aCmd, unsigned char *pBuf)
{
	if(State == Writing){
		if(aCmd == cmSetDisplayPadPositionInf){
			//Write
			if(pBuf[2] == DpPort){
				if(pBuf[3] == DpAddr){
					if(pBuf[5] == dgTYPE_DISPLAY_PAD){
						RpyData.RecSta = pBuf[6];
						RpyData.RecResult =1;
					}
				}
			}
		}
	}
}

void TDisplayPadDescpDownload::SendCmdWriteOnePoint(unsigned char aPort, unsigned char aAddr)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	volatile unsigned int tAddr;
	unsigned char tP0,tP1;
	unsigned int x;
	
	tP0 = aPort -1;
	tP1 = aAddr -1;
	if( (tP0 >= dPORT_MAX_COUNT) || (tP1 >= dEP_MAX_COUNT_PER_PORT) )return;
	tAddr = dAddrSdRam_Descp + (tP0 * 256 * 32) + (tP1 * 32);
	DescpData[0] = MainCtl.fData.Split.MyNum;
	DescpData[1] = aPort;
	DescpData[2] = aAddr;
	for(i=0;i<32;i++){
		DescpData[i+3] = *(volatile unsigned char *)(tAddr +i);
	}
	DescpData[35]=0;
	
	aIde.BitF.Cmd = cmSetDisplayPadPositionInf;
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	Data[0] = 0;
	Data[1] = DpPort;
	Data[2] = DpAddr;
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;
	
	x = 0;
	for(i=0;i<9;i++){
		Data[3] = i+1;
		Data[4]=DescpData[x++];
		Data[5]=DescpData[x++];
		Data[6]=DescpData[x++];
		Data[7]=DescpData[x++];
		InnerCan.Buf4GuiSend.Insert(aIde, Data, (i<8?8:7));
	}
}

void TDisplayPadDescpDownload::SendCmdWriteAbort(unsigned char aPort, unsigned char aAddr)
{
	
}

TGuiMsgReturn TDisplayPadDescpDownload::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg;
	unsigned char tCtlNum, aCc, Ow, Err;
	
	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;
	}
	
	if(State == Writing){
		switch(aKey){
			case VK_RIGHT:
				if(Tag.Inx ==7){
					ClearSelected();
					Tag.Inx++;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(Tag.Inx <8){
					ClearSelected();
					Tag.Inx++;
					Check4Selected();
				}
				break;
			case VK_EXECUTE:
				if(Tag.Inx == 8){
					if(State == Writing){
						PermissionCmd = 2;
						if(SystemPermission > 2){
							PermissionCmdGO();	
						}else{
							IsPermission = 1;
							PmsIntervene4Gui.Show4Gui(Lv.Grid.Left[1] +320, Lv.Grid.Top[1] + 16, 400, 140, 3);
						}
					}
				}
				break;
			case VK_RETURN:
				IsShow = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}else{
		switch(aKey){
			case '0':
			case '1':
			case '2':
			case '3':
			case '4':
			case '5':
			case '6':
			case '7':
			case '8':
			case '9':
			case VK_LEFT:
			case VK_RIGHT:
			case VK_DELETE:
				if(Tag.Inx ==0){
					if(aKey == VK_LEFT){
						if(Tag.InxItem >0){
							ClearSelected();
							Tag.InxItem--;
							Check4Selected();
						}
					}else
					if(aKey == VK_RIGHT){
						if(Tag.InxItem <4){
							ClearSelected();
							Tag.InxItem++;
							Check4Selected();
						}
					}
				}else
				if(Tag.Inx ==1){
					pPanel->Edit[0].KeyIn(aKey);
					aCc = pPanel->Edit[0].Str.ToInteger();
					if( (aCc == 0) || (aCc > dPORT_MAX_COUNT) ){
						pPanel->Edit[0].SelectedColor = clRed;
					}else{
						pPanel->Edit[0].SelectedColor = clBlue;
					}
					DpPort = aCc;
					pPanel->Edit[0].Show();
					pPanel->Edit[0].ShowCursor();
					DrawPadList();
				}else
				if(Tag.Inx ==2){
					pPanel->Edit[1].KeyIn(aKey);
					aCc = pPanel->Edit[1].Str.ToInteger();
					if( (aCc == 0) || (aCc > dEP_MAX_COUNT_PER_PORT) ){
						pPanel->Edit[1].SelectedColor = clRed;
					}else{
						pPanel->Edit[1].SelectedColor = clBlue;
					}
					DpAddr = aCc;
					pPanel->Edit[1].Show();
					pPanel->Edit[1].ShowCursor();
				}else
				
				if(Tag.Inx ==3){
					pPanel->Edit[2].KeyIn(aKey);
					aCc = pPanel->Edit[2].Str.ToInteger();
					if( (aCc == 0) || (aCc > dPORT_MAX_COUNT) ){
						pPanel->Edit[2].SelectedColor = clRed;
					}else{
						pPanel->Edit[2].SelectedColor = clBlue;
					}
					StartPort = aCc;
					pPanel->Edit[2].Show();
					pPanel->Edit[2].ShowCursor();
				}else
				if(Tag.Inx ==4){
					pPanel->Edit[3].KeyIn(aKey);
					aCc = pPanel->Edit[3].Str.ToInteger();
					if( (aCc == 0) || (aCc > dEP_MAX_COUNT_PER_PORT) ){
						pPanel->Edit[3].SelectedColor = clRed;
					}else{
						pPanel->Edit[3].SelectedColor = clBlue;
					}
					StartAddr = aCc;
					pPanel->Edit[3].Show();
					pPanel->Edit[3].ShowCursor();
				}else
				
				if(Tag.Inx ==5){
					pPanel->Edit[4].KeyIn(aKey);
					aCc = pPanel->Edit[4].Str.ToInteger();
					if( (aCc == 0) || (aCc > dPORT_MAX_COUNT) ){
						pPanel->Edit[4].SelectedColor = clRed;
					}else{
						pPanel->Edit[4].SelectedColor = clBlue;
					}
					EndPort = aCc;
					pPanel->Edit[4].Show();
					pPanel->Edit[4].ShowCursor();
				}else
				if(Tag.Inx ==6){
					pPanel->Edit[5].KeyIn(aKey);
					aCc = pPanel->Edit[5].Str.ToInteger();
					if( (aCc == 0) || (aCc > dEP_MAX_COUNT_PER_PORT) ){
						pPanel->Edit[5].SelectedColor = clRed;
					}else{
						pPanel->Edit[5].SelectedColor = clBlue;
					}
					EndAddr = aCc;
					pPanel->Edit[5].Show();
					pPanel->Edit[5].ShowCursor();
				}else
				if(Tag.Inx ==7){
					if(aKey == VK_RIGHT){
						ClearSelected();
						Tag.Inx++;
						Check4Selected();
					}
				}else
				if(Tag.Inx ==8){
					if(aKey == VK_LEFT){
						if(State != Writing){
							ClearSelected();
							Tag.Inx--;
							Check4Selected();
						}
					}
				}
				break;
			case VK_UP:
				if(Tag.Inx >0){
					if(State != Writing){
						ClearSelected();
						Tag.Inx--;
						Check4Selected();
					}
				}else{
					if(State != Writing){
						ClearSelected();
						Tag.Inx =8;
						Check4Selected();
					}
				}
				break;
			case VK_DOWN:
				if(Tag.Inx <8){
					ClearSelected();
					Tag.Inx++;
					Check4Selected();
				}else{
					if(State != Writing){
						ClearSelected();
						Tag.Inx =0;
						Check4Selected();
					}
				}
				break;
			case VK_TAB:

				break;
			case dfKEY_SETUP:
				if(Tag.Inx ==0){
					switch(Tag.InxItem){
						case 0: SelectedList.SenSmoker = SelectedList.SenSmoker ? 0 :1; break;
						case 1: SelectedList.SenTemper = SelectedList.SenTemper ? 0 :1; break;
						case 2: SelectedList.HandPadReport = SelectedList.HandPadReport ? 0 :1; break;
						case 3: SelectedList.ModuleIn = SelectedList.ModuleIn ? 0 :1; break;
						case 4: SelectedList.ModuleInout = SelectedList.ModuleInout ? 0 :1; break;
					}
					DrawItemTypeSelected();
				}
				break;
			case dfKEY_QUERY:

				break;
			case VK_EXECUTE:
				if(Tag.Inx == 7){
					if(State != Writing){
						if( (DpPort && DpAddr) && (DpPort <= dPORT_MAX_COUNT) && (DpAddr <= dEP_MAX_COUNT_PER_PORT) ){
							if( (StartPort && StartAddr) && (StartPort <= dPORT_MAX_COUNT) && (StartAddr <= dEP_MAX_COUNT_PER_PORT) ){
								if( (EndPort && EndAddr) && (EndPort <= dPORT_MAX_COUNT) && (EndAddr <= dEP_MAX_COUNT_PER_PORT) ){
									PermissionCmd = 1;
									if(SystemPermission > 2){
										PermissionCmdGO();	
									}else{
										IsPermission = 1;
										PmsIntervene4Gui.Show4Gui(Lv.Grid.Left[1] +320, Lv.Grid.Top[1] + 16, 400, 140, 3);
									}
								}
							}
						}
					}
				}
				break;
			case VK_RETURN:
				IsShow = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}
	
	return aMsg;	
}

void TDisplayPadDescpDownload::PermissionCmdGO()
{
	if(PermissionCmd == 1){
		InitWriteData();
		RpyData.Reset();
		State = Writing;
	}else
	if(PermissionCmd == 2){
		SendCmdWriteAbort(DpPort, DpAddr);
		State = Idle;
		RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	}
}

void TDisplayPadDescpDownload::Task1000Ms()
{
	if(State != Idle){
		if(RunData.WasteTime < 99999999)RunData.WasteTime++;
		DoTransmiting();
	}else{
		return;
	}
	if(IsShow == 0)return;
	if( (State != Idle) && (IsPermission == 0) ){
		DrawWriteResult();
	}
	if(State == Finished)State = Idle;
}

void TDisplayPadDescpDownload::ExtReset()
{
	State = Idle;
}

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

