#include "LnkFecBus.h"
#include "Runtime.h"

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

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

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

void TLnkFecBusCfg::Init()
{
	CtlIndex = 0;
	IsShow = 0;

	IsPermission =0;
	
	Sta =0;
}

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

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

void TLnkFecBusCfg::DrawSelf(void)
{
	RenderBackGround();
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "联动->联动设备控制总线FECBUS");

	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);

	TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "总线使能");
	TextRender_string24(Lv.Grid.Left[0] + 40, Lv.Grid.Top[0] +40, clNearWhite, "开启");
	
	TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "控制信号发送使能");
	TextRender_string24(Lv.Grid.Left[1] + 40, Lv.Grid.Top[1] +40, clNearWhite, "开启");
	
	
	TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "控制信号接收使能");
	TextRender_string24(Lv.Grid.Left[2] + 40, Lv.Grid.Top[2] +40, clNearWhite, "开启");
	
	
}

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

void TLnkFecBusCfg::ShowSta()
{
	if(Sta == 0){
		TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "          ");
	}else
	if(Sta == 1){
		TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "保存成功  ");
	}else
	if(Sta == -1){
		TextRender_string24(ResultTipPoint.x, ResultTipPoint.y, clNearWhite, ItemColor, "保存失败  ");
	}
}

void TLnkFecBusCfg::ShowSta(int aSta)
{
	Sta = aSta;
	ShowSta();
}

void TLnkFecBusCfg::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
		ShowSta();
	}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);
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "总线使能");
		TextRender_string24(Lv.Grid.Left[0] + 40, Lv.Grid.Top[0] +40, clNearWhite, "开启");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "控制信号发送使能");
		TextRender_string24(Lv.Grid.Left[1] + 40, Lv.Grid.Top[1] +40, clNearWhite, "开启");
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "控制信号接收使能");
		TextRender_string24(Lv.Grid.Left[2] + 40, Lv.Grid.Top[2] +40, clNearWhite, "开启");
		ShowSta();
		ShowCfg();
		Check4Selected();
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
	}
}


//______________________________________________________________________________________________________________________________
void TLnkFecBusCfg::ClearSelected(void)
{
	switch(CtlIndex){
		case 0:
			TRoundSquare::sSelected(Lv.Grid.Left[0] +10, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 1:
			TRoundSquare::sSelected(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 2:
			TRoundSquare::sSelected(Lv.Grid.Left[2] +10, Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, 4, ItemColor);
			break;
		default:

			break;
	}	
}

void TLnkFecBusCfg::Check4Selected(void)
{
	switch(CtlIndex){
		case 0:
			TRoundSquare::sSelected(Lv.Grid.Left[0] +10, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 1:
			TRoundSquare::sSelected(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 2:
			TRoundSquare::sSelected(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		default:

			break;
	}	
}

void TLnkFecBusCfg::ShowCfg()
{
	if(EnBus){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[0] +10, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(Lv.Grid.Left[0] +10, Lv.Grid.Top[0] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	
	if(EnTx){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] +10, Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	
	if(EnRx){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
}

void TLnkFecBusCfg::PermissionCmdGO()
{
	if(PermissionCmd == 1){
		RestoreData();
		WriteSetting();
		ReadSetting();
		LoadData();
		
		if(	(EnBus 		== FecBusCfg.BusEN) 		&&
			(EnTx 		== FecBusCfg.TxEN)		&&
			(EnRx 		== FecBusCfg.RxEN) ) {
				ShowSta(1);		
				///Record.OperateAdd_BroadcastVa(Broadcast.PadAddr, Broadcast.Addr, Broadcast.SyncMode);
		}else{
				ShowSta(-1);	
		}
	}
}

void TLnkFecBusCfg::PermissionCmdReturnDraw()
{
	RenderBackGround();
	RectFillRender(Lv.Caption.Left, Lv.Caption.Top, Lv.Caption.Right, Lv.Caption.Bottom, CaptionColor);
	TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "联动->联动设备控制总线FECBUS");

	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);

	TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "总线使能");
	TextRender_string24(Lv.Grid.Left[0] + 40, Lv.Grid.Top[0] +40, clNearWhite, "开启");
	
	TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "控制信号发送使能");
	TextRender_string24(Lv.Grid.Left[1] + 40, Lv.Grid.Top[1] +40, clNearWhite, "开启");
	
	
	TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "控制信号接收使能");
	TextRender_string24(Lv.Grid.Left[2] + 40, Lv.Grid.Top[2] +40, clNearWhite, "开启");
	
	ShowCfg();
}

//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TLnkFecBusCfg::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	TGuiMsgReturn PmsRtnMsg = guiMsgNone;
	int iVal;

	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_UP:
				if(CtlIndex >0){
					ClearSelected();
					CtlIndex--;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(CtlIndex <2){
					ClearSelected();
					CtlIndex++;
					Check4Selected();
				}
				break;	
			case dfKEY_SETUP:		//0x70		//  Key value for SETUP
				if( CtlIndex==0 ){
					EnBus = EnBus ? 0 : 1;
				}else	
				if( CtlIndex==1 ){
					EnTx = EnTx ? 0 : 1;
				}else
				if( CtlIndex==2 ){
					EnRx = EnRx ? 0 : 1;
				}
				ShowCfg();
				break;
			case VK_EXECUTE:
				//Save Data Here
				PermissionCmd = 1;
				if(SystemPermission > 2){
					PermissionCmdGO();
				}else{
					IsPermission = 1;
					ClearSelected();
					PmsIntervene4Gui.Show4Gui(Lv.Grid.Left[2] + 200, Lv.Grid.Top[2] + 16, 400, 140, 3);
				}
				break;
			case VK_RETURN:
				aMsg = guiMsgReturn;
				break;
			default :
				break;
		}
		
		if(VK_EXECUTE != aKey){
			ShowSta(0);
		}
	}
	
	return aMsg;	
}

void TLnkFecBusCfg::InitPanel(void)
{
	LoadData();
	Check4Selected();
	ShowCfg();
}

void TLnkFecBusCfg::LoadData()
{
	EnBus = FecBusCfg.BusEN;
	EnTx = FecBusCfg.TxEN;
	EnRx = FecBusCfg.RxEN;
	
	ShowCfg();
}

void TLnkFecBusCfg::RestoreData()
{
	FecBusCfg.BusEN 	=EnBus;
	FecBusCfg.TxEN 	=EnTx;
	FecBusCfg.RxEN 	=EnRx ;	
}

void TLnkFecBusCfg::ReadSetting()
{
	unsigned int tAddr;
	unsigned char tData[4];
	//Read ExistTable 
	tAddr = dEepAddrFecBusSetting +0;
	sDelayMs(10);
	eeprom_read_byte(0, tAddr, tData, 4);
	
	FecBusCfg.BusEN 	= tData[0];
	FecBusCfg.TxEN	= tData[1];
	FecBusCfg.RxEN 	=  tData[2];
	
	if(FecBusCfg.BusEN == 0xFF)FecBusCfg.BusEN =0;
	if(FecBusCfg.TxEN == 0xFF)FecBusCfg.TxEN =0;
	if(FecBusCfg.RxEN == 0xFF)FecBusCfg.RxEN = 0;
}

void TLnkFecBusCfg::WriteSetting()
{
	unsigned int tAddr;
	unsigned char tData[4];
	
	tData[0] = FecBusCfg.BusEN;
	tData[1] = FecBusCfg.TxEN;
	tData[2] = FecBusCfg.RxEN;
	tData[3] = 0 ;
	
	tAddr = dEepAddrFecBusSetting + 0;
	sDelayMs(10);
	eeprom_write_byte_via_page(0, tAddr, tData, 4);
	sDelayMs(10);	
}

void TLnkFecBusCfg::sRecoverSetDefault()
{
	unsigned int tAddr;
	unsigned char tData[4];
	
	tData[0] = 0;
	tData[1] = 0;
	tData[2] = 0;
	tData[3] = 0 ;
	
	tAddr = dEepAddrFecBusSetting + 0;
	sDelayMs(10);
	eeprom_write_byte_via_page(0, tAddr, tData, 4);
	sDelayMs(10);

	ReadSetting();	
}


