#include "Gui.h"
#include "DevProperty.h"

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

#define cmDeviceSetSignalInOutType	199
#define cmDeviceQuerySignalInOutType 	200

#define dWdClr		clSkyBlue
#define dWdDisabelClr	clNearWhite

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

void TDevProperty::DrawFixTextModuleInOut()
{
	if(LanguageEnCn==0){
		TextRender_string24(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 6, clNearWhite, ItemColor, "定义类型");
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +210, Lv.Grid.Top[2] + 40, 76, 30, 7, clNearWhite, "保存", 14, clNearBlack);
	
		TextRender_string24(Lv.Grid.Left[3] +10, ModuleRectLv3.Top + 6, clNearWhite, ItemColor, "输入信号");
		TextRender_string24(Lv.Grid.Left[3] +42, ModuleRectLv3.Top + 36, clNearWhite, ItemColor, "反馈");
		TextRender_string24(Lv.Grid.Left[3] +140, ModuleRectLv3.Top + 36, clNearWhite, ItemColor, "报警");
		TextRender_string24(Lv.Grid.Left[3] +236, ModuleRectLv3.Top + 36, clNearWhite, ItemColor, "监管");
		TextRender_string24(Lv.Grid.Left[3] +42, ModuleRectLv3.Top + 72, clNearWhite, ItemColor, "点动");
		TRoundRect::sDrawBySize(Lv.Grid.Left[3] +120, ModuleRectLv3.Top + 72, 76, 30, 7, clNearWhite, "读取", 14, clNearBlack);
		TRoundRect::sDrawBySize(Lv.Grid.Left[3] +210, ModuleRectLv3.Top + 72, 76, 30, 7, clNearWhite, "保存", 14, clNearBlack);

		unsigned int aClr;
		if(RunState == TRunModeCfg::RunModeAdjust)aClr = clNearBlack;else	aClr = clSilver;
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +4, ModuleRectLv4.Top + 6, 54, 30, 7, clNearWhite, "灯点", 4, aClr);
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +66, ModuleRectLv4.Top + 6, 104, 30, 7, clNearWhite, "模拟输入", 4, aClr);
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +180, ModuleRectLv4.Top + 6, 104, 30, 7, clNearWhite, "模拟启动", 4, aClr);		
	}else{
		TextRender_string24(Lv.Grid.Left[2] +10, Lv.Grid.Top[2] + 6, clNearWhite, ItemColor, "DEF TYPE");
		TRoundRect::sDrawBySize(Lv.Grid.Left[2] +210, Lv.Grid.Top[2] + 40, 76, 30, 7, clNearWhite, "Save", 14, clNearBlack);
	
		TextRender_string24(Lv.Grid.Left[3] +10, ModuleRectLv3.Top + 6, clNearWhite, ItemColor, "Signal");
		TextRender_string24(Lv.Grid.Left[3] +42, ModuleRectLv3.Top + 36, clNearWhite, ItemColor, "FBCK");
		TextRender_string24(Lv.Grid.Left[3] +140, ModuleRectLv3.Top + 36, clNearWhite, ItemColor, "Fire");
		TextRender_string24(Lv.Grid.Left[3] +236, ModuleRectLv3.Top + 36, clNearWhite, ItemColor, "SUPV");
		TextRender_string24(Lv.Grid.Left[3] +42, ModuleRectLv3.Top + 72, clNearWhite, ItemColor, "PulseE");
		TRoundRect::sDrawBySize(Lv.Grid.Left[3] +120, ModuleRectLv3.Top + 72, 76, 30, 7, clNearWhite, "Read", 14, clNearBlack);
		TRoundRect::sDrawBySize(Lv.Grid.Left[3] +210, ModuleRectLv3.Top + 72, 76, 30, 7, clNearWhite, "Save", 14, clNearBlack);

		unsigned int aClr;
		if(RunState == TRunModeCfg::RunModeAdjust)aClr = clNearBlack;else	aClr = clSilver;
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +4, ModuleRectLv4.Top + 6, 54, 30, 7, clNearWhite, "LED", 4, aClr);
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +66, ModuleRectLv4.Top + 6, 104, 30, 7, clNearWhite, "SimInput", 4, aClr);
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +180, ModuleRectLv4.Top + 6, 104, 30, 7, clNearWhite, "SimStart", 4, aClr);		
	}
}

void TDevProperty::FullRedrawhtModuleInOut(int Prm)
{
	if(Prm == 0){
		for(int i=2; 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);
		if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
		ReDrawCommonInf();
		DrawFixTextModuleInOut();
		IList->Edit[0].Show();
		IList->STextModuleInDefineType.Show();
		
		DrawLightOnStateModuleInOut();
		RedrawPanelData();
		Redraw4ModuleInOutSmallPanelData();
		if(EdittingDescp){
			CnInput.FullRedraw();
		}else{
			Check4SelectedHtModuleInOut();
		}
	}else{
		for(int i=2; i<3; 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);
		ReDrawCommonInf();
		DrawFixTextModuleInOut();
		IList->Edit[0].Show();
		IList->STextModuleInDefineType.Show();
		RedrawPanelData();
		Redraw4ModuleInOutSmallPanelData();
		if(EdittingDescp){
			CnInput.FullRedraw();
		}else{
			Check4SelectedHtModuleInOut();
		}
		if(IsPermission)PmsIntervene4Gui.ReDraw4GuiRecall();
		DrawLightOnStateModuleInOut();
	}
}

void TDevProperty::InitHtModuleInOut()
{
	PanelIsUsingText = 1;
	
	for(int i=2; i<3; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	RectFillRender(Lv.Grid.Left[3], ModuleRectLv3.Top, Lv.Grid.Right[3], ModuleRectLv3.Bottom, ItemColor);
	RectFillRender(Lv.Grid.Left[4], ModuleRectLv4.Top, Lv.Grid.Right[4], ModuleRectLv4.Bottom, ItemColor);
	
	DrawFixTextModuleInOut();
	
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	
	RectFillRender(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top + 6, Lv.PanelSmall.Left + 10 + 20, Lv.PanelSmall.Top + 6 + 20, clGreen);
	
	IList->Edit[0].Init(Lv.Grid.Left[2] +110, Lv.Grid.Top[2] + 4, 48,30,0,0,1,clNearBlack);
	IList->Edit[0].SetMaxLen(3);
	IList->Edit[0].Str.Clear();
	IList->STextModuleInDefineType.Init(Lv.Grid.Left[2] +4, Lv.Grid.Top[2] + 40, 196,30,0,0,clNearWhite,ItemColor);
	unsigned char tType = Port[Path0-1].AssignType[Path1-1];
	IList->Edit[0].Str.FromUInt3Dg(tType);
	if(LanguageEnCn==0){
		if(tType == 0){
			IList->STextModuleInDefineType.SetText("未指派类型" , 24);
		}else{
			IList->STextModuleInDefineType.SetText(StrTypeShortName[tType] , 24);
		}
	}else{
		if(tType == 0){
			IList->STextModuleInDefineType.SetText("NONE TYPE" , 24);
		}else{
			IList->STextModuleInDefineType.SetText(StrTypeShortNameEn[tType] , 24);
		}
	}
	IList->Edit[0].Show(); 
	IList->STextModuleInDefineType.Show();

	
	//ModuleInOutRt.IsWait4ReadSignalType = 1;
	//ModuleInOutRt.IsWait4WriteSignalType = 0;
	//ModuleInOutRt.RecRdy = 0;
	//ModuleInOutRt.RecCmd = 0;
	//ModuleInOutRt.WaitTick = 0;
	//TextRender_string24(Lv.Grid.Left[3] +160, ModuleRectLv3.Top + 4, clNearWhite, ItemColor, "获取中  ");
	Data.ModuleSignalInOutType = Port[Path0-1].FuncSetByte[Path1-1];
	RedrawModuleInOutSignalType();
	
	DrawLightOnStateModuleInOut();
}

void TDevProperty::EditDescpReturnHtModuleInOut()
{
	PanelIsUsingText = 1;
	
	for(int i=2; i<3; i++)RectFillRender(Lv.Grid.Left[i], Lv.Grid.Top[i], Lv.Grid.Right[i], Lv.Grid.Bottom[i], ItemColor);
	RectFillRender(Lv.Grid.Left[3], ModuleRectLv3.Top, Lv.Grid.Right[3], ModuleRectLv3.Bottom, ItemColor);
	
	DrawFixTextModuleInOut();
	
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
	if(Lv.Panel.Active)RectFillRender(Lv.PanelSmall.Left, Lv.PanelSmall.Top, Lv.PanelSmall.Right, Lv.PanelSmall.Bottom, SmallPanelColor);
	
	RectFillRender(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top + 6, Lv.PanelSmall.Left + 10 + 20, Lv.PanelSmall.Top + 6 + 20, clGreen);
	
	IList->Edit[0].Show();
	IList->STextModuleInDefineType.Show();
	
	RedrawModuleInOutSignalType(); 
	DrawLightOnStateModuleInOut();
	
	ReDrawPanel();
}

void TDevProperty::Redraw4ModuleInOutNewLoad()
{
	unsigned char tType = Port[Path0-1].AssignType[Path1-1];
	IList->Edit[0].Str.FromUInt3Dg(tType);
	if(LanguageEnCn==0){
		if(tType == 0){
			IList->STextModuleInDefineType.SetText("未指派类型" , 24);
		}else{
			IList->STextModuleInDefineType.SetText(StrTypeShortName[tType] , 24);
		}
	}else{
		if(tType == 0){
			IList->STextModuleInDefineType.SetText("None TYPE" , 24);
		}else{
			IList->STextModuleInDefineType.SetText(StrTypeShortNameEn[tType] , 24);
		}
	}
	IList->Edit[0].SetPstTail();
	IList->Edit[0].Show();
	IList->STextModuleInDefineType.Show();
	
	//ModuleInOutRt.IsWait4ReadSignalType = 1;
	//ModuleInOutRt.IsWait4WriteSignalType = 0;
	//ModuleInOutRt.RecRdy = 0;
	//ModuleInOutRt.RecCmd = 0;
	//ModuleInOutRt.WaitTick = 0;
	//TextRender_string24(Lv.Grid.Left[3] +160, ModuleRectLv3.Top + 4, clNearWhite, ItemColor, "获取中  ");
	//SendModuleSignalInOutTypeRead();
	
	Data.ModuleSignalInOutType = Port[Path0-1].FuncSetByte[Path1-1];
	RedrawModuleInOutSignalType();
	DrawLightOnStateModuleInOut();
	
	if(Lv.Panel.Active)RectFillRender(Lv.Panel.Left, Lv.Panel.Top, Lv.Panel.Right, Lv.Panel.Bottom, PanelColor);
}

void TDevProperty::Redraw4ModuleInOutSmallPanelData()
{
	RectFillRender(Lv.PanelSmall.Left + 10, Lv.PanelSmall.Top + 6, Lv.PanelSmall.Left + 10 + 20, Lv.PanelSmall.Top + 6 + 20, clGreen);

	TMyString::sClear(TextDraw);
	TMyString::sFrom2Dg(RuntimeData.voltage % 100, TextDraw);
	TMyString::sAddOnChar('.', TextDraw);
	TMyString::sAddOn1Dg(0, TextDraw);
	TMyString::sAddOnChar('V', TextDraw);
	TextRender_string24(Lv.PanelSmall.Left + 32, Lv.PanelSmall.Top + 4, clNearWhite, SmallPanelColor, TextDraw);
	
	DrawMac();
}

void TDevProperty::ClearSelectedHtModuleInOut()
{
	unsigned int aClr;
	char *tText;
	switch(Tag.Inx){
		case 0:
			
			break;
		case 1:
			IList->EditPortNum.Selected = 0;
			IList->EditPortNum.Show();
			break;
		case 2:
			IList->EditAddrNum.Selected = 0;
			IList->EditAddrNum.Show();
			break;
		case 3:
			IList->EditDescpShow.Selected = 0;
			IList->EditDescpShow.Show();
			break;
		case 4:
			IList->EditUserCode.Selected = 0;
			IList->EditUserCode.Show();
			break;
		case 5:
			IList->EditAreaAssign.Selected = 0;
			IList->EditAreaAssign.Show();
			break;
		case 6:
			IList->Edit[0].Selected = 0;
			IList->Edit[0].Show();
			break;
		case 7:
			if(LanguageEnCn==0)tText = "保存"; else tText = "Save";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +210, Lv.Grid.Top[2] + 40, 76, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 8:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +12, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 9:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +110, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 10:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +206, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 11:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +12, ModuleRectLv3.Top + 72, dWWidth, dWHeigh, 4, ItemColor);
			break;
		case 12:
			if(LanguageEnCn==0)tText = "读取"; else tText = "Read";
			TRoundRect::sDrawBySize(Lv.Grid.Left[3] +120, ModuleRectLv3.Top + 72, 76, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 13:
			if(LanguageEnCn==0)tText = "保存"; else tText = "Save";
			TRoundRect::sDrawBySize(Lv.Grid.Left[3] +210, ModuleRectLv3.Top + 72, 76, 30, 7, clNearWhite, tText, 14, clNearBlack);
			break;
		case 14:
			//TRoundRect::sDrawBySize(Lv.Grid.Left[4] +4, ModuleRectLv4.Top + 6, 54, 30, 7, clNearWhite, "灯点", 4, clNearBlack);
			DrawLightOnStateModuleInOut(-1);
			break;
		case 15:
			if(RunState == TRunModeCfg::RunModeAdjust)aClr = clNearBlack;else	aClr = clSilver;
			if(LanguageEnCn==0)tText = "模拟输入"; else tText = "SimInput";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +66, ModuleRectLv4.Top + 6, 104, 30, 7, clNearWhite, tText, 4, aClr);
			break;
		case 16:
			if(RunState == TRunModeCfg::RunModeAdjust)aClr = clNearBlack;else	aClr = clSilver;
			if(LanguageEnCn==0)tText = "模拟启动"; else tText = "SimStart";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +180, ModuleRectLv4.Top + 6, 104, 30, 7, clNearWhite, tText, 4, aClr);
			break;
	}
}

void TDevProperty::Check4SelectedHtModuleInOut()
{
	unsigned int aClr;
	char *tText;
	switch(Tag.Inx){
		case 0:
			
			break;
		case 1:
			IList->EditPortNum.Selected = 1;
			IList->EditPortNum.SetPstTail();
			IList->EditPortNum.Show();
			IList->EditPortNum.ShowCursor();
			break;
		case 2:
			IList->EditAddrNum.Selected = 1;
			IList->EditAddrNum.SetPstTail();
			IList->EditAddrNum.Show();
			IList->EditAddrNum.ShowCursor();
			break;
		case 3:
			IList->EditDescpShow.Selected = 1;
			IList->EditDescpShow.Show();
			break;
		case 4:
			IList->EditUserCode.Selected = 1;
			IList->EditUserCode.SetPstTail();
			IList->EditUserCode.Show();
			IList->EditUserCode.ShowCursor();
			break;
		case 5:
			IList->EditAreaAssign.Selected = 1;
			IList->EditAreaAssign.SetPstTail();
			IList->EditAreaAssign.Show();
			IList->EditAreaAssign.ShowCursor();
			break;
		case 6:
			IList->Edit[0].Selected = 1;
			IList->Edit[0].SetPstTail();
			IList->Edit[0].Show();
			IList->Edit[0].ShowCursor();
			break;
		case 7:
			if(LanguageEnCn==0)tText = "保存"; else tText = "Save";
			TRoundRect::sDrawBySize(Lv.Grid.Left[2] +210, Lv.Grid.Top[2] + 40, 76, 30, 7, clDeepBlue, tText, 14, clNearWhite);
			break;
		case 8:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +12, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 9:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +110, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 10:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +206, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 11:
			TRoundSquare::sSelected(Lv.Grid.Left[3] +12, ModuleRectLv3.Top + 72, dWWidth, dWHeigh, 4, clNearWhite);
			break;
		case 12:
			if(LanguageEnCn==0)tText = "读取"; else tText = "Read";
			TRoundRect::sDrawBySize(Lv.Grid.Left[3] +120, ModuleRectLv3.Top + 72, 76, 30, 7, clDeepBlue, tText, 14, clNearWhite);
			break;
		case 13:
			if(LanguageEnCn==0)tText = "保存"; else tText = "Save";
			TRoundRect::sDrawBySize(Lv.Grid.Left[3] +210, ModuleRectLv3.Top + 72, 76, 30, 7, clDeepBlue, tText, 14, clNearWhite);
			break;
		case 14:
			//TRoundRect::sDrawBySize(Lv.Grid.Left[4] +4, ModuleRectLv4.Top + 6, 54, 30, 7, clDeepBlue, "灯点", 4, clNearWhite);
			DrawLightOnStateModuleInOut();
			break;
		case 15:
			if(RunState == TRunModeCfg::RunModeAdjust)aClr = clNearWhite; else	aClr = clSilver;
			if(LanguageEnCn==0)tText = "模拟输入"; else tText = "SimInput";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +66, ModuleRectLv4.Top + 6, 104, 30, 7, clDeepBlue, tText, 4, aClr);
			break;
		case 16:
			if(RunState == TRunModeCfg::RunModeAdjust)aClr = clNearWhite; else aClr = clSilver;
			if(LanguageEnCn==0)tText = "模拟启动"; else tText = "SimStart";
			TRoundRect::sDrawBySize(Lv.Grid.Left[4] +180, ModuleRectLv4.Top + 6, 104, 30, 7, clDeepBlue, tText, 4, aClr);
			break;
	}	
}

void TDevProperty::RedrawModuleInOutDefineType()
{
	int iVal;
	iVal =IList->Edit[0].Str.ToInteger();
	if(LanguageEnCn==0){
		if( (iVal <0) || (iVal > 255) ){
			IList->Edit[0].SelectedColor = clRed;
			IList->STextModuleInDefineType.SetText("未指派类型",24);
		}else{
			IList->Edit[0].SelectedColor = clBlue;
			IList->STextModuleInDefineType.SetText(StrTypeShortName[iVal],24);
		}
	}else{
		if( (iVal <0) || (iVal > 255) ){
			IList->Edit[0].SelectedColor = clRed;
			IList->STextModuleInDefineType.SetText("NONE TYPE",24);
		}else{
			IList->Edit[0].SelectedColor = clBlue;
			IList->STextModuleInDefineType.SetText(StrTypeShortNameEn[iVal],24);
		}
	}
	IList->Edit[0].SetPstTail();
	IList->Edit[0].Show();
	IList->STextModuleInDefineType.Show();
}

void TDevProperty::RedrawModuleInOutSignalType()
{
	if((Data.ModuleSignalInOutType & 0x07) == 1){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 12, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 110, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 206, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}else
	if((Data.ModuleSignalInOutType & 0x07) == 2){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 12, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 110, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 206, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);		
	}else
	if((Data.ModuleSignalInOutType & 0x07) == 4){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 12, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 110, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 206, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 12, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 110, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 206, ModuleRectLv3.Top + 36, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
	
	if(Data.ModuleSignalInOutType & 0x08){
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 12, ModuleRectLv3.Top + 72, dWWidth, dWHeigh, dRoundR, dWdClr , 1);
	}else{
		TRoundSquare::sDrawBySize(Lv.Grid.Left[1] + 12, ModuleRectLv3.Top + 72, dWWidth, dWHeigh, dRoundR, dWdDisabelClr , 0);
	}
}

void TDevProperty::PushModuleInOutCanData(unsigned char aCmd, unsigned char *pData)
{
	int i;
	if(aCmd == cmDeviceSetSignalInOutType){
		for(i=0; i<8; i++)RecData[i] = pData[i];
		if(   (ProcessingRt.P0 == RecData[1]) &&
			(ProcessingRt.P1 == RecData[2]) &&
			(ProcessingRt.DataType == RecData[3]) &&
			(ProcessingRt.dVal[0] == RecData[4])) {
				ModuleInOutRt.RecRdy =1;
				ModuleInOutRt.RecCmd =aCmd;
		}
	}else
	if(aCmd == cmDeviceQuerySignalInOutType){
		for(i=0; i<8; i++)RecData[i] = pData[i];
		if(   (ProcessingRt.P0 == RecData[1]) &&
			(ProcessingRt.P1 == RecData[2]) &&
			(ProcessingRt.DataType == RecData[3]) ){
				ProcessingRt.dValGot[0] = RecData[4];
				ModuleInOutRt.RecRdy =1;
				ModuleInOutRt.RecCmd =aCmd;
		}
	}
}

TGuiMsgReturn TDevProperty::KeyInHtModuleInOut(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aBits; 
	char *tText;
	int HasPrc = 0;
	switch(aKey){
		case VK_0:
		case VK_1:
		case VK_2:
		case VK_3:
		case VK_4:
		case VK_5:
		case VK_6:
		case VK_7:
		case VK_8:
		case VK_9:
		case VK_DELETE:
		case VK_LEFT:
		case VK_RIGHT:
			if(Tag.Inx < 6){
				KeyInCommonEdit(aKey);
			}else{
				if(Tag.Inx == 6){
					//Change Define Type
					IList->Edit[0].KeyIn(aKey);
					RedrawModuleInOutDefineType();
				}else
				if(Tag.Inx == 7){
					//Save Define Type
				}else
				if(Tag.Inx == 8){
					//Set As Feed Back
					if(aKey == VK_RIGHT){
						ClearSelectedHtModuleInOut();
						Tag.Inx = 9;
						Check4SelectedHtModuleInOut();
					}
				}else
				if(Tag.Inx == 9){
					//Set As Alram
					if(aKey == VK_LEFT){
						ClearSelectedHtModuleInOut();
						Tag.Inx = 8;
						Check4SelectedHtModuleInOut();
					}else
					if(aKey == VK_RIGHT){
						ClearSelectedHtModuleInOut();
						Tag.Inx = 10;
						Check4SelectedHtModuleInOut();
					}
				}else
				if(Tag.Inx == 10){
					//Set As Sv
					if(aKey == VK_LEFT){
						ClearSelectedHtModuleInOut();
						Tag.Inx = 9;
						Check4SelectedHtModuleInOut();
					}
				}
				if(Tag.Inx == 11){
					//Set Output type
					if(aKey == VK_RIGHT){
						ClearSelectedHtModuleInOut();
						Tag.Inx = 12;
						Check4SelectedHtModuleInOut();
					}
				}else
				if(Tag.Inx == 12){
					//Read Input Type
					if(aKey == VK_LEFT){
						ClearSelectedHtModuleInOut();
						Tag.Inx = 11;
						Check4SelectedHtModuleInOut();
					}else
					if(aKey == VK_RIGHT){
						ClearSelectedHtModuleInOut();
						Tag.Inx = 13;
						Check4SelectedHtModuleInOut();
					}
				}else
				if(Tag.Inx == 13){
					//Save Input Type
					if(aKey == VK_LEFT){
						ClearSelectedHtModuleInOut();
						Tag.Inx = 12;
						Check4SelectedHtModuleInOut();
					}
				}else
				if(Tag.Inx == 14){
					//Set Light On
					if(aKey == VK_RIGHT){
						ClearSelectedHtModuleInOut();
						Tag.Inx = 15;
						Check4SelectedHtModuleInOut();
					}
				}else
				if(Tag.Inx == 15){
					//Sim Alram
					if(aKey == VK_LEFT){
						ClearSelectedHtModuleInOut();
						Tag.Inx = 14;
						Check4SelectedHtModuleInOut();
					}else
					if(aKey == VK_RIGHT){
						ClearSelectedHtModuleInOut();
						Tag.Inx = 16;
						Check4SelectedHtModuleInOut();
					}
				}else
				if(Tag.Inx == 16){
					//Sim Start
					if(aKey == VK_LEFT){
						ClearSelectedHtModuleInOut();
						Tag.Inx = 15;
						Check4SelectedHtModuleInOut();
					}
				}
			}
			break;
		case VK_UP:
			if(PortNumValid && AddrNumValid){
				ClearSelectedHtModuleInOut();
				Tag.Inx > Tag.InxMin ? Tag.Inx-- : Tag.Inx = 16;
				Check4SelectedHtModuleInOut();
			}
			break;
		case VK_DOWN:
			if(PortNumValid && AddrNumValid){
				ClearSelectedHtModuleInOut();
				Tag.Inx < 16 ? Tag.Inx++ : Tag.Inx = Tag.InxMin;
				Check4SelectedHtModuleInOut();
			}
			break;
		case VK_TAB:
			//PanelIsUsingText = PanelIsUsingText ? 0 : 1;
			break;
		case dfKEY_SETUP:
			if(Tag.Inx == 8){
				aBits = Data.ModuleSignalInOutType;
				if( (aBits & 0x07) == 0){
					Data.ModuleSignalInOutType |= 0x01;
				}else
				if( (aBits & 0x07) == 1){
					Data.ModuleSignalInOutType &= ~0x07;
				}else{
					Data.ModuleSignalInOutType &= ~0x07;
					Data.ModuleSignalInOutType |= 0x01;
				}
				RedrawModuleInOutSignalType();
			}else
			if(Tag.Inx == 9){
				aBits = Data.ModuleSignalInOutType;
				if( (aBits & 0x07) == 0){
					Data.ModuleSignalInOutType |= 0x02;
				}else
				if( (aBits & 0x07) == 2){
					Data.ModuleSignalInOutType &= ~0x07;
				}else{
					Data.ModuleSignalInOutType &= ~0x07;
					Data.ModuleSignalInOutType |= 0x02;
				}
				RedrawModuleInOutSignalType();
			}else
			if(Tag.Inx == 10){
				aBits = Data.ModuleSignalInOutType;
				if( (aBits & 0x07) == 0){
					Data.ModuleSignalInOutType |= 0x04;
				}else
				if( (aBits & 0x07) == 4){
					Data.ModuleSignalInOutType &= ~0x07;
				}else{
					Data.ModuleSignalInOutType &= ~0x07;
					Data.ModuleSignalInOutType |= 0x04;
				}
				RedrawModuleInOutSignalType();
			}else
			if(Tag.Inx == 11){
				aBits = Data.ModuleSignalInOutType;
				if( (aBits & 0x08) == 0){
					Data.ModuleSignalInOutType |= 0x08;
				}else{
					Data.ModuleSignalInOutType &= ~0x08;
				}
				RedrawModuleInOutSignalType();
			}
			break;
		case dfKEY_QUERY:
			PanelDrawData.DesertTextHex = PanelDrawData.DesertTextHex ? 0: 1;
			break;
		case dfKEY_ENCN:
			SendReqMac();
			break;
		case VK_EXECUTE:
			if(PortNumValid && AddrNumValid){
				if(Tag.Inx <6){
					KeyInCommonEnter(aKey);
				}else{
					if(Tag.Inx == 6){
						//Change Define Type
					}else
					if(Tag.Inx == 7){
						//Save Define Type And Add Record
						if( (Path0 >0) && (Path0 <= dPORT_MAX_COUNT) && (Path1 >0) && (Path1 <= dEP_MAX_COUNT_PER_PORT) ){
							HasPrc = IList->Edit[0].Str.ToInteger();
							if( (HasPrc >-1) && (HasPrc < 256) ){
								AssignTypeSetting = HasPrc;
								if(SystemPermission >2){
									if(LanguageEnCn==0)tText = "写入中  "; else tText = "Writing ";
									TextRender_string24(IList->Edit[0].Bound.Right + 6, IList->Edit[0].Bound.Top + 4, clNearWhite, ItemColor, tText);
									WriteAAssignType2Flash(Path0, Path1, AssignTypeSetting);
									Record.ReadDevUserSet();
									if(HasPrc == Port[Path0-1].AssignType[Path1-1]){
										if(LanguageEnCn==0)tText = "写入成功"; else tText = "WroteOK ";
									}else{
										if(LanguageEnCn==0)tText = "写入失败"; else tText = "WriteErr";
									}
									TextRender_string24(IList->Edit[0].Bound.Right + 6, IList->Edit[0].Bound.Top + 4, clNearWhite, ItemColor, tText);
								}else{
									IsPermission = 1;
									PermissionCmd = pcWriteAssignType;
									PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 3);
								}
							}
						}
					}else
					if(Tag.Inx == 8){
						aBits = Data.ModuleSignalInOutType;
						if( (aBits & 0x07) == 0){
							Data.ModuleSignalInOutType |= 0x01;
						}else
						if( (aBits & 0x07) == 1){
							Data.ModuleSignalInOutType &= ~0x07;
						}else{
							Data.ModuleSignalInOutType &= ~0x07;
							Data.ModuleSignalInOutType |= 0x01;
						}
						RedrawModuleInOutSignalType();
					}else
					if(Tag.Inx == 9){
						aBits = Data.ModuleSignalInOutType;
						if( (aBits & 0x07) == 0){
							Data.ModuleSignalInOutType |= 0x02;
						}else
						if( (aBits & 0x07) == 2){
							Data.ModuleSignalInOutType &= ~0x07;
						}else{
							Data.ModuleSignalInOutType &= ~0x07;
							Data.ModuleSignalInOutType |= 0x02;
						}
						RedrawModuleInOutSignalType();
					}else
					if(Tag.Inx == 10){
						aBits = Data.ModuleSignalInOutType;
						if( (aBits & 0x07) == 0){
							Data.ModuleSignalInOutType |= 0x04;
						}else
						if( (aBits & 0x07) == 4){
							Data.ModuleSignalInOutType &= ~0x07;
						}else{
							Data.ModuleSignalInOutType &= ~0x07;
							Data.ModuleSignalInOutType |= 0x04;
						}
						RedrawModuleInOutSignalType();
					}else
					if(Tag.Inx == 11){
						aBits = Data.ModuleSignalInOutType;
						if( (aBits & 0x08) == 0){
							Data.ModuleSignalInOutType |= 0x08;
						}else{
							Data.ModuleSignalInOutType &= ~0x08;
						}
						RedrawModuleInOutSignalType();
					}else
					if(Tag.Inx == 12){
						if( (Path0) && (Path1) && (Path0 <= dPORT_MAX_COUNT) && (Path1 <= dEP_MAX_COUNT_PER_PORT) ){
							Data.ModuleSignalInOutType = Port[Path0-1].FuncSetByte[Path1-1];
						}
						RedrawModuleInOutSignalType();
					}else
					if(Tag.Inx == 13){
						//Write Signal In Type
						PermissionCmd = pcWriteModuleSignalInOutType;
						if(SystemPermission >2){
							PermissionCmdGO();
						}else{
							IsPermission = 1;
							PermissionCmd = pcWriteModuleSignalInOutType;
							PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 3);
						}
					}else
					if(Tag.Inx == 14){
						//Light On
						if(RunState == TRunModeCfg::RunModeAdjust){
							SendCmdLightOn();
							DrawLightOnStateModuleInOut();
						}
					}else
					if(Tag.Inx == 15){
						//Sim Feed Back
						if(RunState == TRunModeCfg::RunModeAdjust){
							PermissionCmd = pcSimInputActive; //pcSimAlram,pcSimStart,pcSimInputActive,
							if(SystemPermission >1){
								PermissionCmdGO();
							}else{
								IsPermission = 1;
								PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 2);
							}
						}
					}else
					if(Tag.Inx == 16){
						//Sim Start
						if(RunState == TRunModeCfg::RunModeAdjust){
							PermissionCmd = pcSimStart; //pcSimAlram,pcSimStart,pcSimInputActive,
							if(SystemPermission >1){
								PermissionCmdGO();
							}else{
								IsPermission = 1;
								PmsIntervene4Gui.Show4Gui(Lv.Panel.Left + 16, Lv.Panel.Top + 16, Lv.Panel.Width -34, 140, 2);
							}
						}
					}
				}
			}
			break;
		case VK_RETURN:
			IsShow = 0;
			RealTimeSendCmdAbort();
			aMsg = guiMsgReturn;
			break;
	}
	
	
	return aMsg;
	
}

void TDevProperty::WriteMouelaInOutSignal()
{
	unsigned int fAddr;
	unsigned int i;
	unsigned int aUcFull;
	volatile unsigned int x, GetSta;
	unsigned char aP0, aP1, wP1;
	if( (Path0) && (Path0<=dPORT_MAX_COUNT) && (Path1) ){
		aP0 = Path0 -1;
		aP1 = Path1 -1;
		Port[aP0].FuncSetByte[aP1] = Data.ModuleSignalInOutType;
		sIList = IList;
		sWriteAuxIdAssignTypeSignalType(Path0);
		
		Record.ReadDevUserSet();
		Data.ModuleSignalInOutType = Port[aP0].FuncSetByte[aP1];
		RedrawModuleInOutSignalType();
		
	}
}

void TDevProperty::DrawLightOnStateModuleInOut(int aPrm)
{
	unsigned char aP0, aP1;
	char *tText;
	unsigned int TextColor, bClr;
	if( (Path0) && (Path0<=dPORT_MAX_COUNT) && (Path1) ){
		aP0 = Path0 -1;
		aP1 = Path1 -1;
		if( (Tag.Inx == 14) && (aPrm != -1) ){
			TextColor = clNearWhite;
			bClr = clDeepBlue;
		}else{
			if(RunState == TRunModeCfg::RunModeAdjust)TextColor = clNearBlack;else	TextColor = clSilver;
			bClr = clNearWhite;
		}
		if(Port[aP0].HasSetLightOn[aP1]){
			TextColor = clRed;
		}
		if(LanguageEnCn == 0)tText ="灯点"; else tText = "LED "; 
		TRoundRect::sDrawBySize(Lv.Grid.Left[4] +4, ModuleRectLv4.Top + 6, 54, 30, 7, bClr, tText, 4, TextColor);
	}	
}

void TDevProperty::Task1000MsModuleInOut()
{
	/*if(ModuleInOutRt.IsWait4ReadSignalType){
		if(ModuleInOutRt.RecRdy){
			if(ModuleInOutRt.RecCmd == cmQuerySens){
				ModuleInOutRt.RecRdy = 0;
				ModuleInOutRt.IsWait4ReadSignalType = 0;
				//Add Record Here
				if(ProcessingRt.DataType == 1){
					Data.ModuleSignalInType = RecData[4];
					RedrawModuleInSignalInType();
					if(EdittingDescp == 0)TextRender_string24(Lv.Grid.Left[3] + 160, ModuleRectLv3.Top + 4, clNearWhite, ItemColor, "读取成功"); 
				}
			}
			ModuleInOutRt.RecRdy = 0;
		}
		ModuleInOutRt.WaitTick ++;
		if(ModuleInOutRt.WaitTick > 6){
			ModuleInOutRt.IsWait4ReadSignalType = 0;
			if(EdittingDescp == 0)TextRender_string24(Lv.Grid.Left[3] + 160, ModuleRectLv3.Top + 4, clNearWhite, ItemColor, "读取失败");
		}
	}else
	if(ModuleInOutRt.IsWait4WriteSignalType){
		if(ModuleInOutRt.RecRdy){
			if(ModuleInOutRt.RecCmd == cmDeviceSetSens){
				ModuleInOutRt.IsWait4WriteSignalType = 0;
				//Add Record Here
				aFullPath.Body.pCtl = MainCtl.fData.Split.MyNum;
				aFullPath.Body.P0 = ProcessingRt.P0;
				aFullPath.Body.P1 = ProcessingRt.P1;
				if(ProcessingRt.DataType == 1) {
					//Record.OperateAdd_CfgAddrSmokeSensGrade(aFullPath.D32, Data.Modu);
				}
				if(EdittingDescp == 0)TextRender_string24(Lv.Grid.Left[3] + 160, ModuleRectLv3.Top + 4, clNearWhite, ItemColor, "保存成功");
			}
			ModuleInOutRt.RecRdy = 0;
		}
		ModuleInOutRt.WaitTick ++;
		if(ModuleInOutRt.WaitTick > 6){
			ModuleInOutRt.IsWait4WriteSignalType = 0;
			if(EdittingDescp == 0)TextRender_string24(Lv.Grid.Left[3] + 160, ModuleRectLv3.Top + 4, clNearWhite, ItemColor, "保存失败");
		}
	}
	*/
	Redraw4ModuleInSmallPanelData();
}



