#include "RuntimeData.h"
#include "RealTimeData.h"

#define dfDataLen	0x10000
#define dfDataPieceLen	0x2000

static const unsigned char stCmd[8] = {221,222,223,224,225,226,227,228};
static const unsigned char stPrm[8] = {0,1,2,3,4,5,6,7};
static const unsigned char stHexNum[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};

void TRealTimeData::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);
	
	CtlIndex = 0;
	IsStart = 0;
	IsShowing = 0;
	DrawLine = 0;
	
	ResetData();
}

void TRealTimeData::RenderBackGround(void)
{
	RectFillRender(Content.Left, Content.Top, Content.Right, Content.Bottom, Color);
}

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

void TRealTimeData::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 TRealTimeData::DrawCaption2(void)
{	

}

void TRealTimeData::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	Caption.Show();
	DrawCaption2();
	IsShowing = 1;
	
	TextRender_string24(300, 400, clNearBlack, "输入回路号地址号后按确认键");
}

void TRealTimeData::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
		Caption.Show();
		DrawCaption2();
		TextRender_string24(300, 400, clNearBlack, "输入回路号地址号后按确认键");
		aPanel->ReDrawItems();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		aPanel->ReDrawItems();
	}
}

void TRealTimeData::SetAndShowCaption(unsigned char aType)
{
	TMyString::sFromStr(" zzzzzzz",  Caption.Text);
}

void TRealTimeData::ShowState()
{
	if(IsStart){
		TMyString::sFromStr("正在接收", aPanel->SText[2].Text);
	}else{
		TMyString::sFromStr("  已停止", aPanel->SText[2].Text);
	}
	aPanel->SText[2].Show();
}

//______________________________________________________________________________________________________________________________

void TRealTimeData::Check4Selected(void)
{
	unsigned int i;
	aPanel->Edit[0].Selected = 0;
	aPanel->Edit[1].Selected = 0;
	switch(CtlIndex){
		case 0:
			aPanel->Edit[0].Selected = 1;
			break;
		case 1:
			aPanel->Edit[1].Selected = 1;
			break;
		default:

			break;
	}		
}

void TRealTimeData::ResetData()
{
	int Top, i;
	unsigned int aAddr;
	
	aAddr =  dAddrSdRam_Commom;
	for(i=0; i<dfDataLen; i++){
		 *(volatile unsigned char *)(aAddr + i) = 0;
	}
	PrcIndex =0;
	InputIndex =0;
}

void TRealTimeData::Task1000Ms()
{
	if(IsShowing){
		if(SendTick > 20){
			SendTick = 0;
			SendCmd(IsStart);
		}
		DrawList();
	}
}

void TRealTimeData::DrawList()
{
	int i, m, aInx, Num, Num10, Tail, x, y;
	unsigned int aAddr;
	
	while(PrcIndex != InputIndex){
		if(InputIndex >= dfDataPieceLen){
			ResetData();
			break;
		}
		PrcIndex++;
		
		aAddr =  dAddrSdRam_Commom + PrcIndex * 8;
		if(*(volatile unsigned char *)(aAddr) == 0)continue;
		
		aInx = *(volatile unsigned char *)(aAddr + 3);
		if(aInx == 0){
			RecData[0] = *(volatile unsigned char *)(aAddr + 4);
			RecData[1] = *(volatile unsigned char *)(aAddr + 5);
			RecData[2] = *(volatile unsigned char *)(aAddr + 6);
			RecData[3] = *(volatile unsigned char *)(aAddr + 7);
			for(i=4; i<16; i++){
				RecData[i] = 0;
			}
			DrawLine++;
			if(DrawLine >7){
				RectFillRender(Content.Left+8, Content.Top+ 60, Content.Right - 100, Content.Bottom - 40, Color);
				DrawLine = 0;
			}
			y = Content.Top + 60;
			y = y + DrawLine * 32;
			x = Content.Left + 10;
			for(i=0; i<4; i++){
				Num = RecData[i] % 16;
				Num10 = RecData[i] / 16 % 16;
				gui_write_EN2412(x,y,clNearBlack, stHexNum[Num10]);
				x += 12;
				gui_write_EN2412(x,y,clNearBlack, stHexNum[Num]);
				x += 24;
			}
		}else{
			if(aInx < 4){
				RecData[aInx * 4 + 0] = *(volatile unsigned char *)(aAddr + 4);
				RecData[aInx * 4 + 1] = *(volatile unsigned char *)(aAddr + 5);
				RecData[aInx * 4 + 2] = *(volatile unsigned char *)(aAddr + 6);
				RecData[aInx * 4 + 3] = *(volatile unsigned char *)(aAddr + 7);
				y = Content.Top + 60;
				y = y + DrawLine * 32;
				x = Content.Left + 10 + aInx * 170;
				for(i=0; i<4; i++){
					Num = RecData[aInx*4 +i] % 16;
					Num10 = RecData[aInx*4 +i] / 16 % 16;
					gui_write_EN2412(x,y,clNearBlack, stHexNum[Num10]);
					x += 12;
					gui_write_EN2412(x,y,clNearBlack, stHexNum[Num]);
					x += 24;
				}
			}
		}
	}
}


//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TRealTimeData::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	Updata = 0;
	
	if( 	(aKey == VK_DELETE) || 
		(aKey == VK_0) ||	
		(aKey == VK_1) ||
		(aKey == VK_2) ||
		(aKey == VK_3) ||
		(aKey == VK_4) ||
		(aKey == VK_5) ||
		(aKey == VK_6) ||	
		(aKey == VK_7) ||
		(aKey == VK_8) ||
		(aKey == VK_9) ||
		(aKey == VK_LEFT) ||
		(aKey == VK_RIGHT)
					){
		if(aPanel->Edit[0].Selected){
			aPanel->Edit[0].KeyIn(aKey);
			aPanel->Edit[0].ReDraw();
		}else
		if(aPanel->Edit[1].Selected){
			aPanel->Edit[1].KeyIn(aKey);
			aPanel->Edit[1].ReDraw();
		}
	}
	
	if(aKey == VK_UP){
		if(CtlIndex >0){
			CtlIndex--;
			Check4Selected();
			aPanel->ReDrawItems();
		}
	}else
	if(aKey == VK_DOWN){
		if(CtlIndex <1){
			CtlIndex++;
			Check4Selected();
			aPanel->ReDrawItems();
		}
	}else
	if(aKey == VK_TAB){
		if(CtlIndex == 0){
			CtlIndex =1;
		}else{
			CtlIndex =0;
		}
		Check4Selected();
		aPanel->ReDrawItems();
	}
	if(aKey == VK_EXECUTE){
		//Save Data Here
		if(IsStart == 0){
			IsStart = 1;
		}else{
			IsStart = 0;
		}
		SendTick = 0;
		ShowState();
		SendCmd(IsStart);
	}else
	if(aKey == VK_RETURN){
		IsShowing = 0;
		IsStart = 0;
		SendTick = 0;
		SendCmd(IsStart);
		aMsg = guiMsgReturn;
	}
	
	return aMsg;	
}

void TRealTimeData::InitPanel(void)
{
	int x,y, aW,aH,i;
	
	x = Content.Left;
	y = Content.Top;
	aW = Content.Width;
	aH = 40;
	
	aPanel->Init(x,y,aW,aH,0,0,clFrmFace, bvRaised);
	
	aPanel->Caption.Init(1,1,aW-3,32,x,y,clFrmFace,clFrmFace);
	aPanel->Caption.Text[0] = 0;
	
	aPanel->SText[0].Init(70,10,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[1].Init(320,10,60,30,x,y,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(560,10,100,30,x,y,clNearBlack,clFrmFace);
	
	TMyString::sFromStr("回路号:", aPanel->SText[0].Text);
	TMyString::sFromStr("地址号:", aPanel->SText[1].Text);
	TMyString::sFromStr("  已停止", aPanel->SText[2].Text);
	
	aPanel->Edit[0].Init(170,10,40,30,x,y,clNearBlack,clNearWhite);
	aPanel->Edit[1].Init(420,10,50,30,x,y,clNearBlack,clNearWhite);

	for(i=0; i<2;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.Clear();
	}
	aPanel->Edit[0].Str.FromStr("01");
	aPanel->Edit[1].Str.FromStr("001");
	aPanel->Edit[0].SetMaxLen(2);
	aPanel->Edit[1].SetMaxLen(3);
	aPanel->Edit[0].Pst = 1;
	aPanel->Edit[1].Pst = 2;
	
	aPanel->Caption.Visible = 1;
	
	//aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
	
	ShowState();
}

void TRealTimeData::SendCmd(int StartStop)
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	unsigned char aP0,aP1;
	
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	if(StartStop){
		aIde.BitF.Cmd = cmQueryRuntimeData;
	}else{
		aIde.BitF.Cmd = cmQueryAbout;
	}
	
	Len = 4;
	
	//Path 0
	iVal = aPanel->Edit[0].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	aP0 = cVal;
	
	//Path 1
	iVal = aPanel->Edit[1].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	aP1 = cVal;
	
	Path0 = aP0;
	Path1 = aP1;
	
	//Prm 
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = aP0;
	Data[3] = aP1;
	
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;

	InnerCan.CmdSendCustom(aIde, Data, Len);
}

void TRealTimeData::SendCmdAbort()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	unsigned char aP0,aP1;
	
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	aIde.BitF.Cmd = cmQueryAbout;

	
	Len = 4;
	
	//Prm 
	Data[0] = 0;
	Data[1] = 0;
	Data[2] = Path0;
	Data[3] = Path1;
	
	
	aIde.BitF.Pri = 1;
	aIde.BitF.sLvl = 1;
	aIde.BitF.dLvl = 7;
	aIde.BitF.sAddr = 0;
	aIde.BitF.dAddr = 127;

	InnerCan.CmdSendCustom(aIde, Data, Len);
}


void TRealTimeData::PushOneDataReport(u8 aP0, u8 aP1, u8 aInx, u8 D0, u8 D1, u8 D2, u8 D3)
{
	unsigned int aAddr;
	InputIndex++;
	if(InputIndex >= dfDataPieceLen){
		InputIndex = 0;
	}
	aAddr =  dAddrSdRam_Commom + InputIndex * 8;
	*(volatile unsigned char *)(aAddr + 0) = 1;
	*(volatile unsigned char *)(aAddr + 1) = aP0;
	*(volatile unsigned char *)(aAddr + 2) = aP1;
	*(volatile unsigned char *)(aAddr + 3) = aInx;
	*(volatile unsigned char *)(aAddr + 4) = D0;
	*(volatile unsigned char *)(aAddr + 5) = D1;
	*(volatile unsigned char *)(aAddr + 6) = D2;
	*(volatile unsigned char *)(aAddr + 7) = D3;
}

int TRealTimeData::ExtRequst(unsigned char Prm)
{
	IsShowing =0;
	IsStart = 0;
	SendCmdAbort();
	return 0;
}





