#include "ClearLinkageCfg.h"
#include "Runtime.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

static const int xFirst	=  10;
class TItemList *TClearLinkageCfg::sIList;

void TClearLinkageCfg::Init()
{
	CtlIndex = 0;
	IsShowing = 0;
	
	PermissionCmd =0;
	IsPermission =0;
	
	Eraser.OnGo = 0;
	Eraser.Step = 0xFF;
}

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

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

void TClearLinkageCfg::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, "清除联动编程");
		TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clBlue, "确定", 12, clNearWhite);
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Advanced Setup->Clear Linkage Expression");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "Linkage Expression");
		TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clBlue, "Clear", 6, clNearWhite);
	}
}

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

void TClearLinkageCfg::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	IsShowing =1;
}

void TClearLinkageCfg::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], ContentColor);
		for(int i=0; i<Lv.Grid.UsingCount; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
		DrawFixText();
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}


//______________________________________________________________________________________________________________________________

void TClearLinkageCfg::PermissionCmdReturnDraw()
{
	RectFillRender(0, Lv.Grid.Bottom[0] + 20, 799, Lv.Grid.Bottom[0] + 20 + 160, ContentColor);
	if(LanguageEnCn==0)	TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clBlue, "确定", 12, clNearWhite);
	else 				TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clBlue, "Clear", 6, clNearWhite);
}

void TClearLinkageCfg::sClear4Index(unsigned int aGroup)
{
	volatile unsigned  int aFlashState;
	unsigned int fAddr4K;
	unsigned int fAddr4Index;
	unsigned int sAddr4K;
	int i,x, ord;
	TDMix tCrc;
	unsigned int ToWriteInx = aGroup * 4;
	
	if(sIList==nullptr)return;
	sIList->ReadWriteData.Reset();
	
	fAddr4K = dFlashAddrLinkageExp + ((ToWriteInx * 1024) & 0x00FF'F000);
	for(i=0; i<4; i++){
		Crc32Reset();
		fAddr4Index = fAddr4K + (i * 1024);
		for(ord=0; ord<4; ord++){
			for(x=0; x<64; x++){
				sIList->ReadWriteData.D32[x] = 0;
				if(ord<3 or x<63)Crc32Feed(sIList->ReadWriteData.D32[x]);
			}
			if(ord==3)sIList->ReadWriteData.D32[63]=Crc32GetResult();
			do{
				sDelayMs(2);
				aFlashState = nor_get_busy_state(dFlashLinkageExpChipX);
			}while(aFlashState);
			nor_write_page(dFlashLinkageExpChipX, fAddr4Index, sIList->ReadWriteData.D8);
			fAddr4Index+=256;
		}
		do{
			sDelayMs(2);
			aFlashState = nor_get_busy_state(dFlashLinkageExpChipX);
		}while(aFlashState);
		InterLink.CheckStorageCrc(ToWriteInx+1);
		do{
			sDelayMs(2);
			aFlashState = nor_get_busy_state(dFlashLinkageExpChipX);
		}while(aFlashState);
		ToWriteInx++;
		LoopCheck1S();
	}
}

void TClearLinkageCfg::sDoClear()
{
	volatile unsigned  int aFlashState;
	unsigned int i, GroupCnt;
	unsigned int fAddr64K;
	for(i=0; i<dFlashBlock64kCount4LinkageExp; i++){
		SysLoopTick = 0;
		fAddr64K = dFlashAddrLinkageExp + (i * 0x10000);
		do{
			sDelayMs(2);
			aFlashState = nor_get_busy_state(dFlashLinkageExpChipX);
		}while(aFlashState);
		nor_erase_sector_64k(dFlashLinkageExpChipX,fAddr64K);
		do{
			sDelayMs(400);
			aFlashState = nor_get_busy_state(dFlashLinkageExpChipX);
		}while(aFlashState);
		LoopCheck1S();
	}
	
	GroupCnt = dLinkageOutputBufMaxCOunt / 4;
	for(i=0; i<GroupCnt; i++){
		sClear4Index(i);
	}
	
	SysLoopTick = 0;
	LoopCheck1S();
	do{
		sDelayMs(2);
		aFlashState = nor_get_busy_state(dFlashLinkageExpChipX);
	}while(aFlashState);
	
	InterLink.Init();
	InterLink.LoadExpression2Ram();
}

void TClearLinkageCfg::PermissionCmdGO()
{
	if(PermissionCmd == 1){
		//Go Action
		if(LanguageEnCn==0)	TextRender_string24(400, Lv.Grid.Top[0]+ 36, clNearWhite, ItemColor, "正在清除联动编程");
		else 				TextRender_string24(400, Lv.Grid.Top[0]+ 36, clNearWhite, ItemColor, "Clearing   ");
		Eraser.OnGo = 0xB6;
		Eraser.Step = 0;
		sDoClear();
		InterLink.Init();
		InterLink.LoadExpression2Ram();	
		if(LanguageEnCn==0)	TextRender_string24(400, Lv.Grid.Top[0]+ 36, clNearWhite, ItemColor, "清除完成        ");
		else 				TextRender_string24(400, Lv.Grid.Top[0]+ 36, clNearWhite, ItemColor, "Cleared        ");
		Record.OperateAdd_CleanUp(3);
		Eraser.OnGo =0;
		
	}
}

//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TClearLinkageCfg::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg = guiMsgNone;
	unsigned char aP0,aP1,Err;
	unsigned char x,y,aCh;
	unsigned int aAddr, HadPrc;
	TDMix aFullPath;

	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;
	}else{
		switch(aKey){
			case VK_EXECUTE:
				if( (CtlIndex == 0) && (Eraser.OnGo == 0) ){
					//Active
					PermissionCmd = 1;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						if(LanguageEnCn==0)	TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clNearWhite, "确定", 12, clNearBlack);
						else 				TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clNearWhite, "Clear", 6, clNearBlack);
						PmsIntervene4Gui.Show4Gui(100, Lv.Grid.Bottom[0] + 40, 600 , 120, 3);
					}
				}
				break;
			case VK_RETURN:
				IsShowing = 0;
				if(Eraser.OnGo){
					ForceExit();
				}
				aMsg = guiMsgReturn;
				break;
		}
	}

	return aMsg;	
}

void TClearLinkageCfg::Task1000Ms()
{
	//dFlashBlock64kCount4LinkageExp = 32
	volatile unsigned  int uiVal;
	
	return;
	if(IsShowing){
		if(Eraser.OnGo == 0xB6){
			if(Eraser.Step < dFlashBlock64kCount4LinkageExp){
				sDelayMs(2);
				uiVal = nor_get_busy_state(dFlashLinkageExpChipX);
				if(uiVal)return;
				uiVal = dFlashAddrLinkageExp + (Eraser.Step * 0x10000);
				nor_erase_sector_64k(dFlashLinkageExpChipX,uiVal);
				Eraser.Step++;
			}else
			if(Eraser.Step < 33){
				sDelayMs(2);
				uiVal = nor_get_busy_state(dFlashLinkageExpChipX);
				if(uiVal)return;
				Eraser.Step++;
			}else{
				sDelayMs(2);
				uiVal = nor_get_busy_state(dFlashLinkageExpChipX);
				if(uiVal)return;
				InterLink.Init();
				InterLink.LoadExpression2Ram();
				
				if(LanguageEnCn==0)	TextRender_string24(400, Lv.Grid.Top[0]+ 36, clNearWhite, ItemColor, "清除完成        ");
				else 				TextRender_string24(400, Lv.Grid.Top[0]+ 36, clNearWhite, ItemColor, "Cleared        ");
				Record.OperateAdd_CleanUp(3);
				Eraser.OnGo =0;
			}
		}
	}	
}

void TClearLinkageCfg::ForceExit()
{
	InterLink.Init();
	InterLink.LoadExpression2Ram();
	Record.OperateAdd_CleanUp(0xF3);
	Eraser.OnGo =0;
}

int TClearLinkageCfg::ExtRequst(unsigned char Prm)
{
	IsShowing =0;
	if(Eraser.OnGo){
		ForceExit();
	}
	return 0;
}





