#include "RuntimeData.h"
#include "CircuitCfg.h"
#include "gvalue.h"

#define TextTopOffSet	6
#define ListLineCnt	10
/*
#define dgTYPE_SMOKE_DETECTOR			0x03
#define dgTYPE_TEMPE_DETECTOR			0x02
#define dgTYPE_HAND_REPORT			0x30
#define dgTYPE_FIRE_HydrantKp			0x31
#define dgTYPE_IN_MODULE			0x40
#define dgTYPE_OUT_MODULE			0x43
#define dgTYPE_INOUT_MODULE_NonSource	0x44
#define dgTYPE_INOUT_MODULE_Source		0x45
#define dgType_VAALRAM				0x32
#define dgTYPE_DISPLAY_PAD			0x50
*/
static const unsigned char dTypeList[256] = {
	dgTYPE_SMOKE_DETECTOR,
	dgTYPE_TEMPE_DETECTOR,
	dgType_VAALRAM,
};

const unsigned char dSmokeSensGrade_MarkValue[4] = {
	//100, 75, 50, 25
	100, 50, 0, 0
};


void TCircuitCfg::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);
	SText.Init(Left+358,Top,434, CH, 0, 0, clWhite, clMaroon);
	Content.Set(Left, Top+CH, Right, Bottom);
	RectPanel.Set(400,76,780,430);
	Color = aColor;
	#undef CH
	
	Caption.Border.Color = clGray;
	TMyString::sFromStr("系统调试->回路设备参数配置", Caption.Text);
	
	TextColor = clNearBlack;
	SelectedColor = clBlue;
	SelectedTextColor = clNearWhite;
	
	CtlIndex = 0;
			
	OnActive = 0;
	
	IsWait4CfgRpy =0;
	IsWait4Read =0;
	TipState = 0;
	IsShow = 0;
	
	ProcessingRt.Method = 1;
	ProcessingRt.P0 = 1;
	ProcessingRt.P1 = 1;
	ProcessingRt.DevType =0;
	ProcessingRt.DataType = 1;
}

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

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

void TCircuitCfg::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 TCircuitCfg::DrawCaption2(void)
{	
	TMyString::sFromStr("/////", SText.Text);
	//SText.Show();
}

void TCircuitCfg::Show(void)
{	
	RenderBackGround();
	DrawSelf();
	Caption.Show();
	DrawCaption2();
	
	IsShow = 1;
}

void TCircuitCfg::FullRedraw(int Prm)
{	
	if(Prm == 0){
		RenderBackGround();
		DrawSelf();
		Caption.Show();
		DrawCaption2();
		PanelCanvasOut();
		aPanel->Show();
		ShowState();
		
		RedrawMethod();
		RedrawDataType();
		RedrawDataValue();
	}else
	if(Prm == 1){
		RectFillRender(GuiRedrawDataList[1][0], GuiRedrawDataList[1][1], GuiRedrawDataList[1][2], GuiRedrawDataList[1][3], Color);
		PanelCanvasOut();
		aPanel->Show();
		ShowState();
		
		RedrawMethod();
		RedrawDataType();
		RedrawDataValue();
	}
}


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

//______________________________________________________________________________________________________________________________

void TCircuitCfg::Check4Selected(void)
{
	unsigned int i;
	for(i=0; i<4;i++)aPanel->Edit[i].Selected = 0;
	for(i=11; i<15;i++)aPanel->Edit[i].Selected = 0;
	for(i=0; i<2;i++)aPanel->Btn[i].Selected = 0;
	if(CtlIndex < 4){
		aPanel->Edit[CtlIndex].Selected = 1;
	}else
	if(CtlIndex ==4){
		aPanel->Edit[11].Selected = 1;
	}
	if(CtlIndex ==5){
		aPanel->Btn[0].Selected = 1;
	}
	if(CtlIndex == 6){
		aPanel->Btn[1].Selected = 1;
	}
}

void TCircuitCfg::RedrawMethod()
{
	if(ProcessingRt.Method == 0){
		aPanel->ReDrawItems();
		aPanel->Edit[0].Str.FromStr("按设备类型");
		aPanel->Edit[0].ReDraw();
		
		TMyString::sFromStr("设备类型", 	aPanel->SText[13].Text);
		aPanel->SText[13].Show();

		aPanel->Edit[2].IsShowCursor = 0;
		RedrawDevType();
		
		aPanel->Btn[0].Enable = 0;
		aPanel->Btn[0].Show();
		
		
	}else{
		aPanel->ReDrawItems();
		aPanel->Edit[0].Str.FromStr("按地址");
		aPanel->Edit[0].ReDraw();
		
		TMyString::sFromStr("地址", 	aPanel->SText[13].Text);
		aPanel->SText[13].Show();
		
		aPanel->Edit[2].IsShowCursor = 1;
		RedrawP1();
		
		aPanel->Btn[0].Enable = 1;
		aPanel->Btn[0].Show();
		
		
	}
}

void TCircuitCfg::RedrawDevType()
{
	if(ProcessingRt.DevType == 0){
		aPanel->ReDrawItems();
		aPanel->Edit[2].Str.FromStr("点型烟感");
		aPanel->Edit[2].ReDraw();
	}else 
	if(ProcessingRt.DevType == 1){
		aPanel->ReDrawItems();
		aPanel->Edit[2].Str.FromStr("点型温感");
		aPanel->Edit[2].ReDraw();
	}else 
	if(ProcessingRt.DevType == 2){
		aPanel->ReDrawItems();
		aPanel->Edit[2].Str.FromStr("声光警报器");
		aPanel->Edit[2].ReDraw();
	}
}

void TCircuitCfg::RedrawP1()
{
	aPanel->Edit[2].Str.FromUInt3Dg(ProcessingRt.P1);
	aPanel->Edit[2].ReDraw();

}

void TCircuitCfg::RedrawDataType()
{
	//0x01:烟感灵敏度(送检前完成)
	//0x02:烟感背景光
	//0x03:烟感标定值
	//0x06:温感类别
	//0x07:声光类别
	//0x08:模块输出线检测
	//0x09:模块输出类别
	//0x20:层显地址段配置
	
	switch(ProcessingRt.DataType){
		case 1:
			aPanel->Edit[3].Str.FromStr("烟感灵敏度");
			TextOut4SmokeSens();
			break;
		case 2:
			aPanel->Edit[3].Str.FromStr("烟感背景光");
			TextOut4SmokeBackGroundLight();
			break;
		case 3:
			aPanel->Edit[3].Str.FromStr("烟感标定值");
			TextOut4SmokeRefrence();
			break;
		case 6:
			aPanel->Edit[3].Str.FromStr("温感类别");
			TextOut4Tempe();
			break;
		case 7:
			aPanel->Edit[3].Str.FromStr("声光类别");
			TextOut4Va();
			break;
		case 8:
			aPanel->Edit[3].Str.FromStr("模块输出线检测");
			TextOut4ModuleOutputChexk();
			break;
		case 9:
			aPanel->Edit[3].Str.FromStr("模块输出类别");
			TextOut4ModuleOutputType();
			break;
		case 0x20:
			aPanel->Edit[3].Str.FromStr("层显地址段配置");
			TextOut4FloorDisplay();
			break;
		default:
			ProcessingRt.DataType = 1;
			aPanel->Edit[3].Str.FromStr("烟感灵敏度");
			TextOut4SmokeSens();
			break;
	}
	
	aPanel->Edit[3].Show();
	aPanel->SText[1].Show();
	aPanel->SText[2].Show();
	aPanel->SText[3].Show();
	aPanel->SText[4].Show();
}

void TCircuitCfg::RedrawDataValue()
{
	//0x01:烟感灵敏度(送检前完成)
	//0x02:烟感背景光
	//0x03:烟感标定值
	//0x06:温感类别
	//0x07:声光类别
	//0x08:模块输出线检测
	//0x09:模块输出类别
	//0x20:层显地址段配置
	
	switch(ProcessingRt.DataType){
		case 1:
			RedrawSmokeSensValue();
			break;
		case 2:
			aPanel->Edit[11].Str.Clear();
			aPanel->Edit[12].Str.Clear();
			aPanel->Edit[13].Str.Clear();
			aPanel->Edit[14].Str.Clear();
			break;
		case 3:
			aPanel->Edit[11].Str.Clear();
			aPanel->Edit[12].Str.Clear();
			aPanel->Edit[13].Str.Clear();
			aPanel->Edit[14].Str.Clear();
			break;
		case 6:
			RedrawTempeValue();
			break;
		case 7:
			aPanel->Edit[11].Str.Clear();
			aPanel->Edit[12].Str.Clear();
			aPanel->Edit[13].Str.Clear();
			aPanel->Edit[14].Str.Clear();
			break;
		case 8:
			aPanel->Edit[11].Str.Clear();
			aPanel->Edit[12].Str.Clear();
			aPanel->Edit[13].Str.Clear();
			aPanel->Edit[14].Str.Clear();
			break;
		case 9:
			aPanel->Edit[11].Str.Clear();
			aPanel->Edit[12].Str.Clear();
			aPanel->Edit[13].Str.Clear();
			aPanel->Edit[14].Str.Clear();
			break;
		case 0x20:
			aPanel->Edit[11].Str.Clear();
			aPanel->Edit[12].Str.Clear();
			aPanel->Edit[13].Str.Clear();
			aPanel->Edit[14].Str.Clear();
			break;
		default:
			aPanel->Edit[11].Str.Clear();
			aPanel->Edit[12].Str.Clear();
			aPanel->Edit[13].Str.Clear();
			aPanel->Edit[14].Str.Clear();
			break;
	}
	aPanel->Edit[3].Show();
	aPanel->Edit[11].Show();
	aPanel->Edit[12].Show();
	aPanel->Edit[13].Show();
	aPanel->Edit[14].Show();
}

void TCircuitCfg::RedrawSmokeSensValue()
{
	/*
	if(UserSetData.SmokeSensGrade == 0){
		aPanel->Edit[11].Str.FromStr("低灵敏度");
	}else 
	if(UserSetData.SmokeSensGrade == 1){
		aPanel->Edit[11].Str.FromStr("中灵敏度");
	}else 
	if(UserSetData.SmokeSensGrade == 2){
		aPanel->Edit[11].Str.FromStr("高灵敏度");
	}else{
		aPanel->Edit[11].Str.FromStr("未知等级");
	}
	*/
	if(UserSetData.SmokeSensGrade == 0){
		aPanel->Edit[11].Str.FromStr("标准灵敏度");
	}else 
	if(UserSetData.SmokeSensGrade == 1){
		aPanel->Edit[11].Str.FromStr("高灵敏度");
	}else{
		aPanel->Edit[11].Str.FromStr("未知等级");
	}
	aPanel->Edit[11].Show();
}

void TCircuitCfg::RedrawTempeValue()
{
	//1..9 A1、A1R、A1S、A2、A2R、A2S、B、BR、BS
	switch(UserSetData.TempeSensType){
		case 1:aPanel->Edit[11].Str.FromStr("1.A1");break;
		case 2:aPanel->Edit[11].Str.FromStr("2.A1R");break;
		case 3:aPanel->Edit[11].Str.FromStr("3.A1S");break;
		case 4:aPanel->Edit[11].Str.FromStr("4.A2");break;
		case 5:aPanel->Edit[11].Str.FromStr("5.A2R");break;
		case 6:aPanel->Edit[11].Str.FromStr("6.A2S");break;
		case 7:aPanel->Edit[11].Str.FromStr("7.B");break;
		case 8:aPanel->Edit[11].Str.FromStr("8.BR");break;
		case 9:aPanel->Edit[11].Str.FromStr("9.BS");break;
	}
	aPanel->Edit[11].ReDraw();
}


void TCircuitCfg::PkgSetData()
{
	switch(ProcessingRt.DataType){
		case 1:
			if(UserSetData.SmokeSensGrade < 2)
				ProcessingRt.dVal[0] = dSmokeSensGrade_MarkValue[UserSetData.SmokeSensGrade];
			else
				ProcessingRt.dVal[0] = 0;
			ProcessingRt.dVal[1] =0;
			ProcessingRt.dVal[2] =0;
			ProcessingRt.dVal[3] =0;
			break;
		case 2:
		case 3:
		case 6:
		case 7:
		case 8:
		case 9:
		case 0x20:
			ProcessingRt.dVal[0] =0;
			ProcessingRt.dVal[1] =0;
			ProcessingRt.dVal[2] =0;
			ProcessingRt.dVal[3] =0;
			break;
		default:
			break;
	}
}

//_____________________________________________________________________________________________________________________________

TGuiMsgReturn TCircuitCfg::KeyIn(unsigned char aKey)
{
	TGuiMsgReturn aMsg = guiMsgNone;
	unsigned char aP0,aP1,Updata;
	unsigned char x,y,aCh;
	Updata = 0;
	
	if(  (IsWait4Read==0) && (IsWait4CfgRpy==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(CtlIndex == 0){
					if(ProcessingRt.Method ==0){
						ProcessingRt.Method =1;
						RedrawP1();
					}else{
						ProcessingRt.Method =0;
						RedrawDevType();
					}
					RedrawMethod();
				}else
				if(CtlIndex == 1){
					aPanel->Edit[CtlIndex].KeyIn(aKey);
					aPanel->Edit[CtlIndex].ReDraw();
				}else
				if(CtlIndex == 2){
					if(ProcessingRt.Method == 0){
						ProcessingRt.DevType++;
						if(ProcessingRt.DevType >= 3)ProcessingRt.DevType = 0;
						RedrawDevType();
					}else{
						aPanel->Edit[2].KeyIn(aKey);
						aPanel->Edit[2].ReDraw();
					}
				}else
				if(CtlIndex == 3){
					switch(ProcessingRt.DataType){
						case 1:
							ProcessingRt.DataType = 2;
							break;
						case 2:
							ProcessingRt.DataType = 3;
							break;
						case 3:
							ProcessingRt.DataType = 6;
							break;
						case 6:
							ProcessingRt.DataType = 7;
							break;
						case 7:
							ProcessingRt.DataType = 8;
							break;
						case 8:
							ProcessingRt.DataType = 9;
							break;
						case 9:
							ProcessingRt.DataType = 0x20;
							break;
						case 0x20:
							ProcessingRt.DataType = 1;
							break;
						default:
							ProcessingRt.DataType = 1;
							break;
					}
					RedrawDataType();
					RedrawDataValue();
				}else
				if(CtlIndex == 4){
					switch(ProcessingRt.DataType){
						case 1:
							if(UserSetData.SmokeSensGrade <1)UserSetData.SmokeSensGrade++;
							else UserSetData.SmokeSensGrade = 0;
							break;
						case 2:

							break;
						case 3:

							break;
						case 6:

							break;
						case 7:

							break;
						case 8:

							break;
						case 9:

							break;
						case 0x20:

							break;
						default:
							break;
					}
					RedrawDataValue();
				}else
				if(CtlIndex == 5){
					switch(ProcessingRt.DataType){
						case 2:
							
							break;
						case 3:

							break;
						case 7:
							
							break;
						case 9:
							break;
						case 0x20:
							break;
						default:
							break;
					}
				}else
				if(CtlIndex == 6){
					switch(ProcessingRt.DataType){
						case 2:
							
							break;
						case 3:
							
							break;
						case 9:
							
							break;
						case 0x20:
							
							break;
						default:
							break;
					}
				}else
				if(CtlIndex == 7){
					switch(ProcessingRt.DataType){
						case 2:

							break;
						case 3:

							break;
						case 0x20:

							break;
						default:
							break;
					}
				}
			}
			
			if(aKey == VK_UP){
				if(CtlIndex >0){
					CtlIndex--;
				}else{
					CtlIndex=6;
				}
				if(ProcessingRt.Method == 0){
					if(CtlIndex == 5)CtlIndex = 4;
				}
				Check4Selected();
				aPanel->ReDrawItems();
			}else
			if(aKey == VK_DOWN){
				if(CtlIndex <6){
					CtlIndex++;
				}else{
					CtlIndex =0;
				}
				if(ProcessingRt.Method == 0){
					if(CtlIndex == 5)CtlIndex = 6;
				}
				Check4Selected();
				aPanel->ReDrawItems();
			}else
			if(aKey == VK_EXECUTE){
				//Save Data Here
				if(TipState != 1){
					if(CtlIndex == 5){
						SendCmdRead();	
						IsWait4Read =1;
						ShowState(1);
						RecRdy = 0;
						RecCmd =0;
						WaitTick = 0;
					}else
					if(CtlIndex == 6){
						PkgSetData();
						SendCmdCfg();
						IsWait4CfgRpy =1;
						ShowState(1);
						RecRdy = 0;
						RecCmd =0;
						WaitTick = 0;
					}
				}
			}
	}

	if(aKey == VK_RETURN){
		TipState = 0;
		IsShow = 0;
		IsWait4Read =0;
		IsWait4CfgRpy =0;
		aMsg = guiMsgReturn;
	}
	
	return aMsg;	
}

void TCircuitCfg::PanelCanvasOut()
{
	HorizLineRender(RectPanel.Left + 12, 		RectPanel.Top + 137, 			RectPanel.Width -34, 			clGray);
	HorizLineRender(RectPanel.Left + 12, 		RectPanel.Top + 274, 			RectPanel.Width -34, 			clGray);
	
	TextRender_string24(4,  120, clNearBlack, "1.回路号:");
	TextRender_string24(28, 155, clNearBlack, "  0 :无效");
	TextRender_string24(28, 190, clNearBlack, "1~40:单回路");
	
	TextRender_string24(4, 230, clNearBlack, "2.地址段:");
	TextRender_string24(28, 265, clNearBlack, "范围:1~250");
	//TextRender_string24(28, 300, clNearBlack, "0~0:回路内所有同类型设备");
	
	TextRender_string24(4, 320, clNearBlack, "3.上下键切换选项");
	TextRender_string24(4, 360, clNearBlack, "4.数字键左右键删除键进行编辑");
}

void TCircuitCfg::TextOut4All()
{
	TMyString::sFromStr("楼层号", aPanel->SText[1].Text);
	TMyString::sFromStr("区域号", aPanel->SText[2].Text);
	TMyString::sFromStr("通用参数1", aPanel->SText[3].Text);
	TMyString::sFromStr("通用参数2", aPanel->SText[4].Text);
	//TMyString::sFromStr("通用参数3", aPanel->SText[5].Text);
	//TMyString::sFromStr("通用参数4", aPanel->SText[6].Text);
}

void TCircuitCfg::TextOut4SmokeSens()
{
	TMyString::sFromStr("灵敏度  ", aPanel->SText[1].Text);
	TMyString::sFromStr("不可用", aPanel->SText[2].Text);
	TMyString::sFromStr("不可用", aPanel->SText[3].Text);
	TMyString::sFromStr("不可用", aPanel->SText[4].Text);
	//TMyString::sFromStr("不可用", aPanel->SText[5].Text);
	//TMyString::sFromStr("不可用", aPanel->SText[6].Text);
}

void TCircuitCfg::TextOut4SmokeBackGroundLight()
{
	TMyString::sFromStr("蓝光值", aPanel->SText[1].Text);
	TMyString::sFromStr("红光值", aPanel->SText[2].Text);
	TMyString::sFromStr("不可用", aPanel->SText[3].Text);
	TMyString::sFromStr("不可用", aPanel->SText[4].Text);
	//TMyString::sFromStr("不可用", aPanel->SText[5].Text);
	//TMyString::sFromStr("不可用", aPanel->SText[6].Text);
}

void TCircuitCfg::TextOut4SmokeRefrence()
{
	TMyString::sFromStr("蓝光值", aPanel->SText[1].Text);
	TMyString::sFromStr("红光值", aPanel->SText[2].Text);
	TMyString::sFromStr("不可用", aPanel->SText[3].Text);
	TMyString::sFromStr("不可用", aPanel->SText[4].Text);
	//TMyString::sFromStr("不可用", aPanel->SText[5].Text);
	//TMyString::sFromStr("不可用", aPanel->SText[6].Text);
}

void TCircuitCfg::TextOut4Tempe()
{
	TMyString::sFromStr("温感类别", aPanel->SText[1].Text);
	TMyString::sFromStr("不可用", aPanel->SText[2].Text);
	TMyString::sFromStr("不可用", aPanel->SText[3].Text);
	TMyString::sFromStr("不可用", aPanel->SText[4].Text);
	//TMyString::sFromStr("不可用", aPanel->SText[5].Text);
	//TMyString::sFromStr("不可用", aPanel->SText[6].Text);
}

void TCircuitCfg::TextOut4Va()
{
	TMyString::sFromStr("声光类别", aPanel->SText[1].Text);
	TMyString::sFromStr("音调", aPanel->SText[2].Text);
	TMyString::sFromStr("不可用", aPanel->SText[3].Text);
	TMyString::sFromStr("不可用", aPanel->SText[4].Text);
}

void TCircuitCfg::TextOut4ModuleOutputChexk()
{
	TMyString::sFromStr("输入检线", aPanel->SText[1].Text);
	TMyString::sFromStr("不可用", aPanel->SText[2].Text);
	TMyString::sFromStr("不可用", aPanel->SText[3].Text);
	TMyString::sFromStr("不可用", aPanel->SText[4].Text);
	//TMyString::sFromStr("输出占空比", aPanel->SText[5].Text);
	//TMyString::sFromStr("不可用", aPanel->SText[6].Text);
}

void TCircuitCfg::TextOut4ModuleOutputType()
{
	//TMyString::sFromStr("输出类别", aPanel->SText[1].Text);
	//TMyString::sFromStr("输入检线", aPanel->SText[2].Text);
	//TMyString::sFromStr("输出检线", aPanel->SText[3].Text);
	//TMyString::sFromStr("输入占空比", aPanel->SText[4].Text);
	//TMyString::sFromStr("输出占空比", aPanel->SText[5].Text);
	//TMyString::sFromStr("不可用", aPanel->SText[6].Text);
	
	TMyString::sFromStr("输出类别", aPanel->SText[1].Text);
	TMyString::sFromStr("输入检线", aPanel->SText[2].Text);
	TMyString::sFromStr("输出检线", aPanel->SText[3].Text);
	TMyString::sFromStr("输入占空比", aPanel->SText[4].Text);
}

void TCircuitCfg::TextOut4MotherBoard()
{
	TMyString::sFromStr("最大输出电流", aPanel->SText[1].Text);
	TMyString::sFromStr("负荷电流阀值", aPanel->SText[2].Text);
	TMyString::sFromStr("时隙1", aPanel->SText[3].Text);
	TMyString::sFromStr("时隙2", aPanel->SText[4].Text);
	//TMyString::sFromStr("偏移量1", aPanel->SText[5].Text);
	//TMyString::sFromStr("偏移量2", aPanel->SText[6].Text);
}

void TCircuitCfg::TextOut4Circuit()
{
	TMyString::sFromStr("最大输出电流", aPanel->SText[1].Text);
	TMyString::sFromStr("负荷电流阀值", aPanel->SText[2].Text);
	TMyString::sFromStr("时隙1", aPanel->SText[3].Text);
	TMyString::sFromStr("时隙2", aPanel->SText[4].Text);
	//TMyString::sFromStr("偏移量1", aPanel->SText[5].Text);
	//TMyString::sFromStr("偏移量2", aPanel->SText[6].Text);	
}

void TCircuitCfg::TextOut4FloorDisplay()
{
	TMyString::sFromStr("机器号", aPanel->SText[1].Text);
	TMyString::sFromStr("回路", aPanel->SText[2].Text);
	TMyString::sFromStr("起始地址", aPanel->SText[3].Text);
	TMyString::sFromStr("结束地址", aPanel->SText[4].Text);
	//TMyString::sFromStr("偏移量1", aPanel->SText[5].Text);
	//TMyString::sFromStr("偏移量2", aPanel->SText[6].Text);	
}

void TCircuitCfg::InitPanel(void)
{
	int x,y,x2,y2, aW,aH,i, yt;
	x = RectPanel.Left;
	y = RectPanel.Top;
	x2 = RectPanel.Right;
	y2 = RectPanel.Bottom;
	aW = RectPanel.Width;
	aH = RectPanel.Height;
	
	yt = y+6;
	
	aPanel->Init(x,y,aW,aH,0,0,clFrmFace, bvRaised);
	
	aPanel->Caption.Init(1,1,aW-3,32,x,y,clFrmFace,clFrmFace);
	
	aPanel->SText[11].Init(6,0,	170,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[12].Init(6,32,	170,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[13].Init(6,64,	170,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[14].Init(6,96,	170,30,x,yt,clNearBlack,clFrmFace);
	
	aPanel->SText[1].Init(6,138,	170,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[2].Init(6,170,	170,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[3].Init(6,202,	170,30,x,yt,clNearBlack,clFrmFace);
	aPanel->SText[4].Init(6,234,	170,30,x,yt,clNearBlack,clFrmFace);
	
	aPanel->SText[5].Init(6,270,	170,30,x,yt,clNearBlack,clFrmFace);

	TMyString::sFromStr("读写方式", 	aPanel->SText[11].Text);
	TMyString::sFromStr("回路", 		aPanel->SText[12].Text);
	TMyString::sFromStr("地址", 		aPanel->SText[13].Text);
	TMyString::sFromStr("数据类型", 	aPanel->SText[14].Text);
	
	TMyString::sFromStr("参数1", aPanel->SText[1].Text);
	TMyString::sFromStr("参数2", aPanel->SText[2].Text);
	TMyString::sFromStr("参数3", aPanel->SText[3].Text);
	TMyString::sFromStr("参数4", aPanel->SText[4].Text);
	aPanel->SText[5].TextClear();
	
	aPanel->SText[2].Enable = 0;
	aPanel->SText[3].Enable = 0;
	aPanel->SText[4].Enable = 0;
	
	aPanel->Edit[0].Init(170,0,200,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[1].Init(170,32,200,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[2].Init(170,64,200,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[3].Init(170,96,200,30,x,yt,clNearBlack,clNearWhite);
	
	aPanel->Edit[11].Init(170,138,200,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[12].Init(170,170,200,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[13].Init(170,202,200,30,x,yt,clNearBlack,clNearWhite);
	aPanel->Edit[14].Init(170,234,200,30,x,yt,clNearBlack,clNearWhite);

	for(i=0; i<2;i++){
		aPanel->Btn[i].Enable = 1;
		aPanel->Btn[i].Visible = 1;
		aPanel->Btn[i].SetCaptionLeftSpacing(2);
		aPanel->Btn[i].SelectedColor = clBlue;
	}
	for(i=0; i<4;i++){
		aPanel->Edit[i].Enable = 1;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(3);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.FromStr("0");
		aPanel->Edit[i].Pst = 0;
	}
	aPanel->Edit[0].SetInputMaskEn();
	aPanel->Edit[0].Pst = -1;
	aPanel->Edit[0].SetMaxLen(32);
	
	aPanel->Edit[1].Str.FromStr("01");
	aPanel->Edit[2].Str.FromStr("001");
	aPanel->Edit[1].Pst = 1;
	aPanel->Edit[2].Pst = 2;
	aPanel->Edit[1].SetMaxLen(2);
	aPanel->Edit[2].SetMaxLen(3);
	aPanel->Edit[3].SetMaxLen(3);
	aPanel->Edit[4].SetMaxLen(32);
	
	for(i=11; i<15;i++){
		aPanel->Edit[i].Enable = 0;
		aPanel->Edit[i].Visible = 1;
		aPanel->Edit[i].SetMaxLen(3);
		aPanel->Edit[i].SetInputMaskEn();
		aPanel->Edit[i].Str.FromStr("001");
		aPanel->Edit[i].Pst = 2;
	}
	aPanel->Edit[11].Enable = 1;
	
	aPanel->Edit[0].IsShowCursor = 0;
	aPanel->Edit[1].IsShowCursor = 1;
	aPanel->Edit[2].IsShowCursor = 0;
	aPanel->Edit[3].IsShowCursor = 0;
	
	aPanel->Edit[11].IsShowCursor = 0;
	aPanel->Edit[12].IsShowCursor = 0;
	aPanel->Edit[13].IsShowCursor = 0;
	aPanel->Edit[14].IsShowCursor = 0;
	
	aPanel->Btn[0].Init(200,276,160,30,x,yt,clNearBlack,clFrmFace);
	aPanel->Btn[1].Init(200,308,160,30,x,yt,clNearBlack,clFrmFace);
	aPanel->Btn[0].Caption.FromStr("读取配置");
	aPanel->Btn[1].Caption.FromStr("执行配置");

	aPanel->Caption.Visible = 1;
	
	aPanel->Show();
	Check4Selected();
	aPanel->ReDrawItems();
	PanelCanvasOut();
	
	RedrawMethod();
	RedrawDataType();
	RedrawDataValue();
	
}

void TCircuitCfg::ShowState()
{
	if(TipState == 0){
		aPanel->SText[5].TextClear();
	}else
	if(TipState == 1){
		aPanel->SText[5].SetText("等待回复", 24);
	}else
	if(TipState == 2){
		aPanel->SText[5].SetText("读取成功", 24);
	}else
	if(TipState == 3){
		aPanel->SText[5].SetText("写入成功", 24);
	}else
	if(TipState == 4){
		aPanel->SText[5].SetText("操作失败", 24);
	}
	
	if(TipState == 1){
		aPanel->Btn[0].Enable = 0;
		aPanel->Btn[1].Enable = 0;
	}else{
		aPanel->Btn[0].Enable = 1;
		aPanel->Btn[1].Enable = 1;
	}
	
	aPanel->SText[5].Show();
	aPanel->Btn[0].Show();
	aPanel->Btn[1].Show();
}

void TCircuitCfg::ShowState(int Prm)
{
	TipState = Prm;
	ShowState();
}

void TCircuitCfg::SendCmdRead()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	unsigned char aPx;
	
	Len =4;
	
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 3;
	
	iVal = aPanel->Edit[1].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	aPx = cVal;
	Data[1] = aPx;
	ProcessingRt.P0 = aPx;
	
	iVal = aPanel->Edit[2].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	aPx = cVal;
	Data[2] = aPx;
	ProcessingRt.P1 = aPx;
	
	Data[3] = ProcessingRt.DataType;
	
	aIde.BitF.Cmd = cmQuerySens;
	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 TCircuitCfg::SendCmdCfg()
{
	TCanHeaderID aIde;
	unsigned char cVal;
	unsigned int i;
	unsigned int Len;
	int iVal;
	unsigned char aPx;
	
	Len =8;
	
	for(i=0; i<8; i++){
		Data[i] = 0;
	}
	
	Data[0] = 3;
	
	iVal = aPanel->Edit[1].Str.ToInteger();
	if(iVal>255)iVal=255;
	cVal = iVal;
	aPx = cVal;
	Data[1] = aPx;
	ProcessingRt.P0 = aPx;
	
	if(ProcessingRt.Method ==0){
		Data[2] = dTypeList[ProcessingRt.DevType];
		ProcessingRt.dType = Data[2];
		aIde.BitF.Cmd = cmDeviceConfig;
	}else{
		iVal = aPanel->Edit[2].Str.ToInteger();
		if(iVal>255)iVal=255;
		cVal = iVal;
		aPx = cVal;
		ProcessingRt.P1 = aPx;
		Data[2] = ProcessingRt.P1;
		
		aIde.BitF.Cmd = cmDeviceSetSens;
	}
	
	RedrawSmokeSensValue();
	
	Data[3] = ProcessingRt.DataType;
	Data[4] = ProcessingRt.dVal[0];
	Data[5] = ProcessingRt.dVal[1];
	Data[6] = ProcessingRt.dVal[2];
	Data[7] = ProcessingRt.dVal[3];
	
	
	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 TCircuitCfg::PushInnerCanData(unsigned char aCmd, unsigned char *Data)
{
	int i;
	if(aCmd == cmDeviceSetSens){
		for(i=0; i<8; i++)RecData[i] = Data[i];
		if(   (ProcessingRt.P0 == RecData[1]) &&
			(ProcessingRt.P1 == RecData[2]) &&
			(ProcessingRt.DataType == RecData[3]) &&
			(ProcessingRt.dVal[0] == RecData[4])) {
				RecRdy =1;
				RecCmd =aCmd;
		}
	}else
	if(aCmd == cmDeviceConfig){
		for(i=0; i<8; i++)RecData[i] = Data[i];
		if(   (ProcessingRt.P0 == RecData[1]) &&
			(ProcessingRt.dType == RecData[2]) &&
			(ProcessingRt.DataType == RecData[3]) &&
			(ProcessingRt.dVal[0] == RecData[4]) ) {
				RecRdy =1;
				RecCmd =aCmd;
		}
	}
	if(aCmd == cmQuerySens){
		for(i=0; i<8; i++)RecData[i] = Data[i];
		if(   (ProcessingRt.P0 == RecData[1]) &&
			(ProcessingRt.P1 == RecData[2]) &&
			(ProcessingRt.DataType == RecData[3]) ){
				ProcessingRt.dValGot[0] = RecData[4];
				ProcessingRt.dValGot[1] = RecData[5];
				ProcessingRt.dValGot[2] = RecData[6];
				ProcessingRt.dValGot[3] = RecData[7];
				RecRdy =1;
				RecCmd =aCmd;
		}
	}
}

void TCircuitCfg::Task1000Ms()
{
	if(IsShow){
		if(IsWait4Read){
			if(RecRdy){
				if(RecCmd == cmQuerySens){
					RecRdy = 0;
					IsWait4Read = 0;
					ShowState(2);
					//Add Record Here
					if(ProcessingRt.DataType == 1){
						if(ProcessingRt.dValGot[0] == dSmokeSensGrade_MarkValue[0] )UserSetData.SmokeSensGrade = 0;
						else if(ProcessingRt.dValGot[0] == dSmokeSensGrade_MarkValue[1] )UserSetData.SmokeSensGrade = 1;
						//else if(ProcessingRt.dValGot[0] == dSmokeSensGrade_MarkValue[2] )UserSetData.SmokeSensGrade = 2;
						else UserSetData.SmokeSensGrade = 3;
						RedrawSmokeSensValue();
					}
				}
				RecRdy = 0;
			}
			WaitTick ++;
			if(WaitTick > 6){
				IsWait4Read = 0;
				ShowState(4);
			}
		}else
		if(IsWait4CfgRpy){
			if(RecRdy){
				if(RecCmd == cmDeviceSetSens){
					IsWait4CfgRpy = 0;
					ShowState(3);
					//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, UserSetData.SmokeSensGrade);
					}
				}else
				if(RecCmd == cmDeviceConfig){
					IsWait4CfgRpy = 0;
					ShowState(3);
					//Add Record Here
					aFullPath.Body.pCtl = MainCtl.fData.Split.MyNum;
					aFullPath.Body.P0 = ProcessingRt.P0;
					if(ProcessingRt.DataType == 1){
						Record.OperateAdd_CfgCircuitSmokeSensGrade(aFullPath.D32, UserSetData.SmokeSensGrade);
					}
				}
				RecRdy = 0;
			}
			WaitTick ++;
			if(WaitTick > 6){
				IsWait4CfgRpy = 0;
				ShowState(4);
			}
		}
	}
}

int TCircuitCfg::ExtRequst(unsigned char Prm)
{
	TipState = 0;
	IsWait4Read =0;
	IsWait4CfgRpy =0;
	IsShow = 0;
	return 0;
}




