#include "CleanUp.h"
#include "Runtime.h"
#include "record.h"

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

void TCleanUp::Init(int x1, int y1, int aW, int aH, int OwnerX, int OwnerY, u32 aColor)
{
	#define CH	30	//Caption Height
	unsigned short Left,Right, Top, Bottom, Width, Height, i;
	Bound.SetBySize(OwnerX + x1, OwnerY + y1, aW, aH);
	Left = Bound.Left +3;
	Top = Bound.Top+3;
	Width = aW - 7;
	Height = aH - 7;
	Right = Bound.Right -4;
	Bottom = Bound.Bottom -4;
	Caption.Init(Left,Top,Width, CH, 0, 0, clWhite, clMaroon);
	Content.Set(Left, Top+CH, Right, Bottom);
	Color = aColor;
	#undef CH
	
	Caption.Border.Color = clGray;
	TMyString::sFromStr("系统设置->清除处理", Caption.Text);
	Caption.Visible = 1;
	
	CtlIndex = 0;
	OnActive = 0;
}

void TCleanUp::DelayMs(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 TCleanUp::RenderBackGround(void)
{
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);
}

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

void TCleanUp::DrawSelf(void)
{
	unsigned int x, y, x2, y2, w, h;
	
	VertLineRender(Bound.Left,		Bound.Top, 			Bound.Height -1,			0xFFFFFFFF);
	VertLineRender(Bound.Right -1,	Bound.Top +1,		Bound.Height -2,			0xFF808080);
	VertLineRender(Bound.Right,		Bound.Top,			Bound.Height,			0xFF404040);
	
	HorizLineRender(Bound.Left, 		Bound.Top, 			Bound.Width - 1, 			0xFFFFFFFF);
	HorizLineRender(Bound.Left +1, 	Bound.Bottom -1,  	Bound.Width - 2, 			0xFF808080);
	HorizLineRender(Bound.Left,		Bound.Bottom,		Bound.Width,			0xFF404040);
	
	x = Bound.Left +2 ;	
	x2 = Bound.Right -2;
	y = Bound.Top +2;
	y2 = Bound.Bottom - 2;
	w = Bound.Width -4;
	h = Bound.Height - 4;
	VertLineRender	(x,		y, 		h-1,	0xFF808080);
	HorizLineRender	(x,		y, 		w-1,	0xFF808080);
		
	VertLineRender	(x+1,		y+1, 		h-2,	0xFF404040);
	HorizLineRender	(x+1,		y+1, 		w-2,	0xFF404040);
	
	VertLineRender	(x2-1,	y+1, 		h-2,	0xFFD4D0C8);
	HorizLineRender	(x+1,		y2-1, 	w-2,	0xFFD4D0C8);

	VertLineRender	(x2,		y, 	  	h,	0xFFFFFFFF);
	HorizLineRender	(x,		y2, 		w,	0xFFFFFFFF);
}

void TCleanUp::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	Caption.Show();
	
	TextRender_string24(66, 240, clNearBlack, "1.上下键切换编辑项目");
	TextRender_string24(66, 280, clNearBlack, "2.选定项目按确定键执行");
}

void TCleanUp::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
		Caption.Show();
		
		TextRender_string24(66, 240, clNearBlack, "1.上下键切换编辑项目");
		TextRender_string24(66, 280, clNearBlack, "2.选定项目按确定键执行");
		
		aPanel->Show();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		TextRender_string24(66, 240, clNearBlack, "1.上下键切换编辑项目");
		TextRender_string24(66, 280, clNearBlack, "2.选定项目按确定键执行");
		
		aPanel->Show();
		
		//if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}


//______________________________________________________________________________________________________________________________

void TCleanUp::Check4Selected(void)
{
	unsigned int i;
	for(i=0; i<9;i++)aPanel->Btn[i].Selected = 0;
	if(1){
		switch(CtlIndex){
			case 0:
				aPanel->Btn[0].Selected = 1;
				break;
			case 1:
				aPanel->Btn[1].Selected = 1;
				break;
			case 2:
				aPanel->Btn[2].Selected = 1;
				break;
			case 3:
				aPanel->Btn[3].Selected = 1;
				break;
			case 4:
				aPanel->Btn[4].Selected = 1;
				break;
			case 5:
				aPanel->Btn[5].Selected = 1;
				break;
			case 6:
				aPanel->Btn[6].Selected = 1;
				break;
			case 7:
				aPanel->Btn[7].Selected = 1;
				break;
			case 8:
				aPanel->Btn[8].Selected = 1;
				break;
			default:

				break;
		}	
	}
}

//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TCleanUp::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn MBoxMsg = guiMsgNone;
	int iVal;

	switch(aKey){
		case VK_UP:
			if(CtlIndex >0){
				CtlIndex--;
				Check4Selected();
				aPanel->ReDrawItems();
			}
			break;
		case VK_DOWN:
			if(CtlIndex <7){
				CtlIndex++;
				Check4Selected();
				aPanel->ReDrawItems();
			}
			break;
		case VK_TAB:
			break;
		case VK_EXECUTE:
			//aPanel->Btn[0].Caption.FromStr("  清除回路终端设备的登记  ");
			//aPanel->Btn[1].Caption.FromStr("      清除总线盘配置      ");
			//aPanel->Btn[2].Caption.FromStr("      清除多线盘配置      ");
			//aPanel->Btn[3].Caption.FromStr("    清除所有联动表达式    ");
			//aPanel->Btn[4].Caption.FromStr("    清除本机终端设备注释  ");
			//aPanel->Btn[5].Caption.FromStr("       清除组网配置       ");
			//aPanel->Btn[6].Caption.FromStr("  清除网络加载的设备注释  ");
	
			//aPanel->Btn[7].Caption.FromStr("         预设描述         ");
			if(CtlIndex == 0){
				cRegister();
				Record.OperateAdd_CleanUp(0);
			}else
			if(CtlIndex == 1){
				cBusPad();
				Record.OperateAdd_CleanUp(1);
			}else
			if(CtlIndex == 2){
				cDirectPad();
				Record.OperateAdd_CleanUp(2);
			}else
			if(CtlIndex == 3){
				cLinkage();
				Record.OperateAdd_CleanUp(3);
			}else
			if(CtlIndex == 4){
				cDescp();
				Record.OperateAdd_CleanUp(4);
			}else
			if(CtlIndex == 5){
				cNetwork();
				Record.OperateAdd_CleanUp(5);
			}else
			if(CtlIndex == 6){
				cDescpNw();
				Record.OperateAdd_CleanUp(6);
			}else
			if(CtlIndex == 7){
				cUserCodeAssignType();
				Record.OperateAdd_CleanUp(7);
			}else
			if(CtlIndex == 8){
				FillDescp();
			}
			break;
		case VK_RETURN:
			aMsg = guiMsgReturn;
			break;
	}
	
	if(aKey != VK_EXECUTE){
		TextRender_string24(20, 110, clNearBlack, Color, "                          ");
	}
	
	return aMsg;	
}

void TCleanUp::InitPanel(void)
{
	int x,y,x2,y2,i;
	y = y+10;
	
	aPanel->Init(370,84,384,320,0,0,clFrmFace, bvRaised);
	aPanel->Caption.Init(371,86, 384-3 ,32,0,0,clFrmFace,clFrmFace);
	
	x = aPanel->Bound.Left;
	y = aPanel->Bound.Top;
	
	aPanel->Btn[0].Init(25,10,330,32,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[1].Init(25,46,330,32,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[2].Init(25,82,330,32,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[3].Init(25,118,330,32,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[4].Init(25,154,330,32,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[5].Init(25,190,330,32,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[6].Init(25,226,330,32,x,y,clNearBlack,clFrmFace);
	aPanel->Btn[7].Init(25,262,330,32,x,y,clNearBlack,clFrmFace);
	
	
	aPanel->Btn[8].Init(25,310,330,36,x,y,clNearBlack,clFrmFace);
	
	aPanel->Btn[0].Caption.FromStr("  清除回路终端设备的登记  ");
	aPanel->Btn[1].Caption.FromStr("      清除总线盘配置      ");
	aPanel->Btn[2].Caption.FromStr("      清除多线盘配置      ");
	aPanel->Btn[3].Caption.FromStr("    清除所有联动表达式    ");
	aPanel->Btn[4].Caption.FromStr("    清除本机终端设备注释  ");
	aPanel->Btn[5].Caption.FromStr("       清除组网配置       ");
	aPanel->Btn[6].Caption.FromStr("  清除网络加载的设备注释  ");
	aPanel->Btn[7].Caption.FromStr(" 清除用户码及设备类型指派 ");
	
	aPanel->Btn[8].Caption.FromStr("         预设描述         ");
	
	for(i=0; i<9;i++){
		aPanel->Btn[i].Enable = 1;
		aPanel->Btn[i].Visible = 1;
		aPanel->Btn[i].SetCaptionLeftSpacing(7);
		aPanel->Btn[i].SelectedColor = clBlue;
	}

	aPanel->Caption.Visible = 1;
	
	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
}

void TCleanUp::WaitChipIdle(unsigned int ChipX)
{
	volatile unsigned int Sta;
	do{
		DelayMs(10);
		Sta = nor_get_busy_state(ChipX);
	}while(Sta);
}

void TCleanUp::cRegister()
{
	volatile unsigned  int aFlashState, i, m, aWait, aAddr;
	
	TextRender_string24(20, 110, clNearBlack, Color, "开始清除注册信息");
	
	for(i=0; i<8; i++){
		WaitChipIdle(dFlashRegisterDataChip);
		aAddr = dFlashRegisterDataAddr + (0x000010000 * i);
		nor_erase_sector_64k(dFlashRegisterDataChip,aAddr);
	}
	
	for(m=0; m<40; m++){
		for(i=0; i<250; i++){
			Port[m].ExistTable[i] = 0;
			Port[m].dTypeTable[i] = 0;
			//EpDev[m][i].fData.Split.dType = 0;
			//EpDev[m][i].fData.Split.Id = 0;
		}
		MainCtl.fData.Split.ExistTablePort[m] = 0;
	}
	MainCtl.fData.Split.PortCount = 0;
	
	WaitChipIdle(dFlashRegisterDataChip);
	Record.ReadRegisterData();
	TextRender_string24(20, 110, clNearBlack, Color, "    清除完成    ");
}

void TCleanUp::cBusPad()
{
	unsigned int fAddr, sdAddr;
	unsigned int i,m, pc;
	volatile unsigned int aWait, x, wP1;
	
	TextRender_string24(20, 110, clNearBlack, Color, "开始清除总线盘配置");
	
	for(x=0; x<2; x++){
		WaitChipIdle(dFlashBusPadLnkChipX);
		fAddr = dFlashAddrBusPadLnk + (SizeOf64K * x);
		nor_erase_sector_64k(dFlashBusPadLnkChipX,fAddr);
	}
	
	WaitChipIdle(dFlashBusPadLnkChipX);
	Record.LoadBusPadLnk();
	TextRender_string24(20, 110, clNearBlack, Color, "    清除完成         ");	
}

void TCleanUp::cDirectPad()
{
	unsigned int fAddr, sdAddr;
	unsigned int i,m, pc;
	volatile unsigned int aWait, x, wP1;
	
	TextRender_string24(20, 110, clNearBlack, Color, "开始清除多线盘配置");
	
	for(x=0; x<2; x++){
		WaitChipIdle(dFlashDirectPadLnkChipX);
		fAddr = dFlashAddrDirectPadLnk + (SizeOf64K * x);
		nor_erase_sector_64k(dFlashDirectPadLnkChipX,fAddr);
	}
	
	WaitChipIdle(dFlashDirectPadLnkChipX);
	Record.LoadDirectPadCfg();
	TextRender_string24(20, 110, clNearBlack, Color, "    清除完成         ");		
}

void TCleanUp::cDescp()
{
	unsigned int fAddr, sdAddr;
	unsigned int i,m;
	volatile unsigned int aWait, x, wP1;
	
	TextRender_string24(20, 110, clNearBlack, Color, "开始清除注释信息");
	
	//Erase 8 * 64K Blocks
	for(x=0; x<8; x++){
		fAddr = dFlashAddrDescpBase + (SizeOf64K * x);
		WaitChipIdle(dEepDescpChip);
		nor_erase_sector_64k(dEepDescpChip,fAddr);
	}
	
	WaitChipIdle(dEepDescpChip);
	Record.ReadDescp();
	TextRender_string24(20, 110, clNearBlack, Color, "    清除完成    ");
}

void TCleanUp::cLinkage()
{
	unsigned int i;
	unsigned int fAddr64K;
	TextRender_string24(20, 110, clNearBlack, Color, "开始清除联动信息");
	for(i=0; i<dFlashBlock64kCount4LinkageExp; i++){
		fAddr64K = dFlashAddrLinkageExp + (i * 0x10000);
		WaitChipIdle(dFlashLinkageExpChipX);
		nor_erase_sector_64k(dFlashLinkageExpChipX,fAddr64K);
	}
	
	WaitChipIdle(dFlashLinkageExpChipX);
	InterLink.Init();
	InterLink.LoadExpression2Ram();
	TextRender_string24(20, 110, clNearBlack, Color, "    清除完成    ");
	
	//#define dFlashLinkageExpChipX	(1)
	//#define dFlashAddrLinkageExp	(0x00DE'0000)
	//#define dFlashBlock64kCount4LinkageExp	(32)
}

void TCleanUp::cUserCodeAssignType()
{
	unsigned int i;
	unsigned int fAddr64K;
	volatile unsigned int fAddr, GetSta;
	TextRender_string24(20, 110, clNearBlack, Color, "开始清除用户码  ");
	for(i=0; i<4; i++){
		fAddr64K = dFlashAddrDevUserCode + (i * 0x10000);
		WaitChipIdle(dFlashAddrDevUserCodeChip);
		nor_erase_sector_64k(dFlashAddrDevUserCodeChip,fAddr64K);
	}
	
	WaitChipIdle(1);
	for(i=0;i<64;i++)ReadWriteData.D32[i] =0;
	
	for(i=0; i<1024; i++){
		do{
			DelayMs(10);
			GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
		}while(GetSta);
		fAddr = dFlashAddrDevUserCode + (i * 256);
		nor_write_page(1, fAddr, ReadWriteData.D8);
		do{
			DelayMs(5);
			GetSta = nor_get_busy_state(dFlashAddrDevUserCodeChip);
		}while(GetSta);
	}
	
	Record.ReadDevUserSet();
	Record.ReadDirectPadUserSet();
	
	TextRender_string24(20, 110, clNearBlack, Color, "    清除完成    ");
}

void TCleanUp::cDescpNw()
{
	unsigned int fAddr, sdAddr;
	unsigned int i,m, pc;
	volatile unsigned int aWait, x, wP1;
	
	TextRender_string24(20, 110, clNearBlack, Color, "开始清除网络加载的注释信息");
	for(pc=1; pc<dNETWORK_MACHINE_COUNT+1; pc++){
		//Erase 8 * 64K Blocks
		for(x=0; x<6; x++){
			WaitChipIdle(NwDescpUsingChip[pc]);
			fAddr = NwDescpUsingFlashAddr[pc] + (SizeOf64K * x);
			nor_erase_sector_64k(NwDescpUsingChip[pc],fAddr);
		}	
	}
	
	WaitChipIdle(0);
	WaitChipIdle(1);
	Record.ReadDescpNw();
	TextRender_string24(20, 110, clNearBlack, Color, "    清除完成              ");
}

void TCleanUp::cNetwork()
{
	int x;
	if(  (MainCtl.fData.Split.NetworkMode == 1) || (MainCtl.fData.Split.NetworkMode == 2) ){
		ProtoC2C.MasterNum = 0;
		ProtoC2C.SlaveCount = 0;
		for(x=0; x<16; x++)ProtoC2C.SlaveList[x] =0;
		
		Record.SaveNwCfg(0,0,ProtoC2C.SlaveList);
		Record.ReadNwCfg();
		
		//Record.OperateAdd_ClearNetworkCfg();
		
		TextRender_string24(20, 110, clNearBlack, Color, "    清除完成              ");
	}
}

void TCleanUp::FillDescp()
{
	volatile unsigned int aAddr ,i, x, z, g, tt;
	
	TextRender_string24(20, 110, clNearBlack, Color, "开始填充         ");
	
	tt =0;
	 
	for(i=0; i<40; i++){
		for(x=0; x<251; x++){
			tt++;
			TMyString::sFromStr("预设描述,回路:", ReadWriteData.Text);
			TMyString::sAddOn2Dg(i+1, ReadWriteData.Text);
			TMyString::sAddOnStr("地址:", ReadWriteData.Text);
			TMyString::sAddOn3Dg(x+1, ReadWriteData.Text);
			TMyString::sAddOnStr("-BC", ReadWriteData.Text);
			if(x & 0x01)
				TMyString::sAddOn5Dg(tt, ReadWriteData.Text);
			
			aAddr = dAddrSdRam_Descp + (i * 256 * 32) + (x * 32);
			for(g=0; g<8; g++){
				*(volatile unsigned int *)(aAddr + (g*4)) = ReadWriteData.D32[g];
			}
		}
		DescpWrite.Write2FlashPreSet(i+1);
	}
	
	for(i=0; i<20; i++){
		for(x=0; x<8; x++){
			tt++;
			TMyString::sFromStr("预设描述,多线盘:", ReadWriteData.Text);
			TMyString::sAddOn2Dg(i+1, ReadWriteData.Text);
			TMyString::sAddOnStr("键号:", ReadWriteData.Text);
			TMyString::sAddOn3Dg(x+1, ReadWriteData.Text);
			TMyString::sAddOnStr("-", ReadWriteData.Text);
			if(x & 0x01)
				TMyString::sAddOn5Dg(tt, ReadWriteData.Text);
			
			aAddr = dAddrSdRam_Descp + (40 * 256 * 32) + ((i*8 + x +1) * 32);
			for(g=0; g<8; g++){
				*(volatile unsigned int *)(aAddr + (g*4)) = ReadWriteData.D32[g];
			}
		}
		
	}
	DescpWrite.Write2FlashPreSet(66);
	
	TextRender_string24(20, 110, clNearBlack, Color, "填充完成         ");
}




