#include "TransmiteDeviceCfg.h"
#include "Runtime.h"


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

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

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

static const int xFirst	=  10;
static const int xSec	= 120;
static const int xThird	= 230;
static const int xFourth= 340;
static const int xFifth	= 450;
static const int xSixth		= 560;
static const int xSeventh	= 661;

void TTransmiteDev::Init()
{
	CtlIndex = 0;
	IsShowing = 0;
	IsOnGoing = 0;
	ReadWrite =0;
	
	IsPermission =0;
	
	OnOff.Fire =1;
	OnOff.Lnk =0;
	OnOff.Sv =1;
	OnOff.Fault =1;
	OnOff.Mask =0;
	OnOff.Opa =1;
	OnOff.Other =0;
	
	State.DeviceOn =0;
	State.Connection =0;
	State.Rsv1 =1;
	State.Rsv2 =2;
}

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

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

void TTransmiteDev::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[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "接口选择");
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "数据信息选择");
		TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clNearWhite, "开启", 12, clNearBlack);
		TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clNearWhite, "关闭", 12, clNearBlack);
		TextRender_string24(xFirst + 30, 	Lv.Grid.Top[1] + 40, clNearWhite, "RS485");
		TextRender_string24(xSec +30, 	Lv.Grid.Top[1] + 40, clNearWhite, "CAN");
		TextRender_string24(xFirst + 30, 	Lv.Grid.Top[2] + 40, clNearWhite, "火警");
		TextRender_string24(xSec +30, 	Lv.Grid.Top[2] + 40, clNearWhite, "联动");
		TextRender_string24(xThird + 30, 	Lv.Grid.Top[2] + 40, clNearWhite, "监管");
		TextRender_string24(xFourth +30, 	Lv.Grid.Top[2] + 40, clNearWhite, "故障");
		TextRender_string24(xFifth + 30, 	Lv.Grid.Top[2] + 40, clNearWhite, "屏蔽");
		TextRender_string24(xSixth +30, 	Lv.Grid.Top[2] + 40, clNearWhite, "操作");
		TextRender_string24(xSeventh + 30, 	Lv.Grid.Top[2] + 40, clNearWhite, "其他");
	}else{
		TextRender_string24(Lv.Caption.Left + 10, Lv.Caption.Top +4, clNearWhite, CaptionColor, "Data Transmission->Device");
		TextRender_string24(Lv.Grid.Left[0] + 10, Lv.Grid.Top[0] +6, clNearWhite, "Device Setting");
		TextRender_string24(Lv.Grid.Left[1] + 10, Lv.Grid.Top[1] +6, clNearWhite, "Port Choice");
		TextRender_string24(Lv.Grid.Left[2] + 10, Lv.Grid.Top[2] +6, clNearWhite, "Data Select");
		TRoundRect::sDrawBySize(xFirst, 	Lv.Grid.Top[0] + 36, 108, 30, 7, clNearWhite, "Enable", 12, clNearBlack);
		TRoundRect::sDrawBySize(160, 		Lv.Grid.Top[0] + 36, 108, 30, 7, clNearWhite, "Disable", 12, clNearBlack);
		TextRender_string24(xFirst + 30, 	Lv.Grid.Top[1] + 40, clNearWhite, "RS485");
		TextRender_string24(xSec +30, 	Lv.Grid.Top[1] + 40, clNearWhite, "CAN");
		TextRender_string24(xFirst + 30, 	Lv.Grid.Top[2] + 40, clNearWhite, "Fire");
		TextRender_string24(xSec +30, 	Lv.Grid.Top[2] + 40, clNearWhite, "Link");
		TextRender_string24(xThird + 30, 	Lv.Grid.Top[2] + 40, clNearWhite, "SUPV");
		TextRender_string24(xFourth +30, 	Lv.Grid.Top[2] + 40, clNearWhite, "Fail");
		TextRender_string24(xFifth + 30, 	Lv.Grid.Top[2] + 40, clNearWhite, "Mask");
		TextRender_string24(xSixth +30, 	Lv.Grid.Top[2] + 40, clNearWhite, "OPER");
		TextRender_string24(xSeventh + 30, 	Lv.Grid.Top[2] + 40, clNearWhite, "Other");
	}
}

void TTransmiteDev::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);
	DrawFixText();
	Check4Selected();
	DrawSetting();
}

void TTransmiteDev::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	DrawConnState();
}

void TTransmiteDev::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<3; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
		DrawFixText();
		DrawConnState();
		DrawSetting();
		if(IsPermission){
			PmsIntervene4Gui.ReDraw4GuiRecall();
		}else{
			Check4Selected();
		}
	}
}

void TTransmiteDev::DrawConnState()
{
	if(LanguageEnCn==0){
		if(State.DeviceOn){
			TextRender_string24(Lv.Grid.Left[0] + 160, 	Lv.Grid.Top[0] + 6, clDeepGreen, ItemColor, "已开启");
		}else{
			TextRender_string24(Lv.Grid.Left[0] + 160, 	Lv.Grid.Top[0] + 6, clRed, ItemColor, "已关闭");
		}
	}else{
		if(State.DeviceOn){
			TextRender_string24(Lv.Grid.Left[0] + 220, 	Lv.Grid.Top[0] + 6, clDeepGreen, ItemColor, 	"Enabled ");
		}else{
			TextRender_string24(Lv.Grid.Left[0] + 220, 	Lv.Grid.Top[0] + 6, clRed, ItemColor, 		"Disabled");
		}
	}
}


//______________________________________________________________________________________________________________________________
void TTransmiteDev::ClearSelected(void)
{
	switch(CtlIndex){
		case 0:	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, 108, 30, 7, clNearWhite, "Enable", 12, clNearBlack);
				break;
		case 1:	if(LanguageEnCn==0)	TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clNearWhite, "关闭", 12, clNearBlack); 
				else 				TRoundRect::sDrawBySize(160, 		Lv.Grid.Top[0] + 36, 108, 30, 7, clNearWhite, "Disable", 12, clNearBlack); 
				break;
	
		case 2:	TRoundSquare::sSelected(xFirst, Lv.Grid.Top[1]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		case 3:	TRoundSquare::sSelected(xSec, Lv.Grid.Top[1]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		
		case 4:	TRoundSquare::sSelected(xFirst, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		case 5:	TRoundSquare::sSelected(xSec, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		case 6:	TRoundSquare::sSelected(xThird, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		case 7:	TRoundSquare::sSelected(xFourth, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		case 8:	TRoundSquare::sSelected(xFifth, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		case 9:	TRoundSquare::sSelected(xSixth, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		case 10:	TRoundSquare::sSelected(xSeventh, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, ItemColor); break;
		default:

			break;
	}		
}

void TTransmiteDev::Check4Selected(void)
{
	switch(CtlIndex){
		case 0:	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, 108, 30, 7, clBlue, "Enable", 12, clNearWhite);  
				break;
		case 1:	if(LanguageEnCn==0)	TRoundRect::sDrawBySize(xSec, 	Lv.Grid.Top[0] + 36, 72, 30, 7, clBlue, "关闭", 12, clNearWhite); 
				else 				TRoundRect::sDrawBySize(160, 	Lv.Grid.Top[0] + 36, 108, 30, 7, clBlue, "Disable", 12, clNearWhite);
				break;
		case 2:	TRoundSquare::sSelected(xFirst, Lv.Grid.Top[1]+40, dWWidth, dWHeigh, 4, clNearWhite); break;
		case 3:	TRoundSquare::sSelected(xSec, Lv.Grid.Top[1]+40, dWWidth, dWHeigh, 4, clNearWhite); break;
		
		case 4:	TRoundSquare::sSelected(xFirst, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, clNearWhite); break;
		case 5:	TRoundSquare::sSelected(xSec, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, clNearWhite); break;
		case 6:	TRoundSquare::sSelected(xThird, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, clNearWhite); break;
		case 7:	TRoundSquare::sSelected(xFourth, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, clNearWhite); break;
		case 8:	TRoundSquare::sSelected(xFifth, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, clNearWhite); break;
		case 9:	TRoundSquare::sSelected(xSixth, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, clNearWhite); break;
		case 10:	TRoundSquare::sSelected(xSeventh, Lv.Grid.Top[2]+40, dWWidth, dWHeigh, 4, clNearWhite); break;

		default:

			break;
	}		
}

void TTransmiteDev::PermissionCmdReturnDraw()
{
	RectFillRender(0, Lv.Grid.Top[1], 799, Lv.Grid.Bottom[2], ContentColor);
	for(int i=1; i<3; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	DrawFixText();
	DrawSetting();
	Check4Selected();
}

void TTransmiteDev::PermissionCmdGO()
{
	if(PermissionCmd == 1){
		//Go Active
		IsOnGoing = 1;
	}else
	if(PermissionCmd == 2){
		//Do Close
		IsOnGoing = 0;
	}
}

void TTransmiteDev::DrawSetting()
{
	if(LineType == 0){
		TRoundSquare::sDrawBySize(xFirst, 	Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(xSec,   	Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}else{
		TRoundSquare::sDrawBySize(xFirst,   Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(xSec, 	Lv.Grid.Top[1] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}
	
	if(OnOff.Fire){
		TRoundSquare::sDrawBySize(xFirst, 	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(xFirst,   Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	if(OnOff.Lnk){
		TRoundSquare::sDrawBySize(xSec, 	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(xSec,   	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	if(OnOff.Sv){
		TRoundSquare::sDrawBySize(xThird, 	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(xThird, 	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	if(OnOff.Fault){
		TRoundSquare::sDrawBySize(xFourth, 	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(xFourth,   Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	if(OnOff.Mask){
		TRoundSquare::sDrawBySize(xFifth, 	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(xFifth,   	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	if(OnOff.Opa){
		TRoundSquare::sDrawBySize(xSixth, 	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(xSixth,   	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	if(OnOff.Other){
		TRoundSquare::sDrawBySize(xSeventh, 	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(xSeventh,   	Lv.Grid.Top[2] + 40, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
}


//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TTransmiteDev::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_RIGHT:
				switch(CtlIndex){
					case 0:
					case 2:
					case 4:
					case 5:
					case 6:
					case 7:
					case 8:
					case 9:
						ClearSelected();
						CtlIndex++;
						Check4Selected();
						break;
					default:
						break;
				}
				break;				
			case VK_LEFT:
				switch(CtlIndex){
					case 1:
					case 3:
					case 5:
					case 6:
					case 7:
					case 8:
					case 9:
					case 10:
						ClearSelected();
						CtlIndex--;
						Check4Selected();
						break;
					default:
						break;
				}
				break;
			case VK_UP:
				if(CtlIndex > 5){
					ClearSelected();
					CtlIndex =3;
					Check4Selected();
				}else
				if(CtlIndex > 1){
					ClearSelected();
					CtlIndex-=2;
					Check4Selected();
				}
				break;
			case VK_DOWN:
				if(CtlIndex < 4){
					ClearSelected();
					CtlIndex+=2;
					Check4Selected();
				}
				break;
			case  dfKEY_SETUP:
				if(CtlIndex == 2){
					LineType = LineType ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 3){
					LineType = LineType ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 4){
					OnOff.Fire = OnOff.Fire ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 5){
					OnOff.Lnk = OnOff.Lnk ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 6){
					OnOff.Sv = OnOff.Sv ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 7){
					OnOff.Fault = OnOff.Fault ? 0 : 1;
					DrawSetting();
				}
				if(CtlIndex == 8){
					OnOff.Mask = OnOff.Mask ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 9){
					OnOff.Opa = OnOff.Opa ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 10){
					OnOff.Other = OnOff.Other ? 0 : 1;
					DrawSetting();
				}
				break;
			case VK_TAB:
				
				break;
			case VK_EXECUTE:
				if(CtlIndex == 0){
					//Active
					PermissionCmd = 1;
					if(SystemPermission > 2){
						PermissionCmdGO();
					}else{
						IsPermission = 1;
						ClearSelected();
						PmsIntervene4Gui.Show4Gui(100, Lv.Grid.Top[1] + 12, 600 , 120, 3);
					}
				}else
				if(CtlIndex == 1){
					//Close
					PermissionCmd = 2;
					PermissionCmdGO();
				}
				if(CtlIndex == 2){
					LineType = LineType ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 3){
					LineType = LineType ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 4){
					OnOff.Fire = OnOff.Fire ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 5){
					OnOff.Lnk = OnOff.Lnk ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 6){
					OnOff.Sv = OnOff.Sv ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 7){
					OnOff.Fault = OnOff.Fault ? 0 : 1;
					DrawSetting();
				}
				if(CtlIndex == 8){
					OnOff.Mask = OnOff.Mask ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 9){
					OnOff.Opa = OnOff.Opa ? 0 : 1;
					DrawSetting();
				}else
				if(CtlIndex == 10){
					OnOff.Other = OnOff.Other ? 0 : 1;
					DrawSetting();
				}
				break;
			case VK_RETURN:
				IsShowing = 0;
				aMsg = guiMsgReturn;
				break;
		}
	}

	return aMsg;	
}

void TTransmiteDev::sRecoverSetDefault()
{
	SysLoopTick = 0;
}

int TTransmiteDev::ExtRequst(unsigned char Prm)
{
	IsShowing =0;
	return 0;
}

void TTransmiteDev::Task1000Ms()
{
	
}






