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

void TDevProperty::CurveInit(short aLeft, short aTop, short aWidth, short aHeight, short aDataTypeCnt)
{
	Curve.UsingDataTypeCnt = aDataTypeCnt;
	Curve.Left = aLeft;
	Curve.Top = aTop;
	Curve.Width = aWidth;
	Curve.Height = aHeight;
	Curve.Right = aLeft + aWidth -1;
	Curve.Bottom = aTop + aHeight -1; 
	
	Curve.CurrentPst =0;
	Curve.DataLen =0;
	
	Curve.Smoke.DataAva[0] =0;
	Curve.Smoke.DataAva[1] =0;
	Curve.Smoke.DataAva[2] =0;
	Curve.Tempe.DataAva[0] =0;
	Curve.Tempe.DataAva[1] =0;
	
	Curve.HasUpdate = 0;
	Curve.HasDrawOutline =0;
}

void TDevProperty::CurveSetMargin(short aLeft, short aTop, short aRight, short aBottom)
{
	Curve.Canvas.OrgX = Curve.Left + aLeft;
	Curve.Canvas.OrgY = Curve.Bottom - aBottom;
	Curve.Canvas.MaxX = Curve.Right - aRight;
	Curve.Canvas.MaxY = Curve.Top + aTop;
	
	Curve.Canvas.Width = Curve.Canvas.MaxX - Curve.Canvas.OrgX +1;
	Curve.Canvas.Height = Curve.Canvas.OrgY - Curve.Canvas.MaxY +1;
	
	if(Curve.Canvas.Width > TItemList::RtDataMaxLen)Curve.Canvas.Width = TItemList::RtDataMaxLen;
	Curve.MaxDataLen = Curve.Canvas.Width -2;
	if(Curve.MaxDataLen <0)Curve.MaxDataLen =0;
}

void TDevProperty::CurveSetGrid(short xCnt, short yCnt, short xWidth, short yHeight)
{
	Curve.GridXCount = xCnt;
	Curve.GridYCount = yCnt;
	if(Curve.GridXCount >16)Curve.GridXCount = 16;
	if(Curve.GridYCount >16)Curve.GridYCount = 16;
	Curve.GridXWidth = xWidth;
	Curve.GridYHeight = yHeight;

	Curve.GridX[0] = Curve.Canvas.OrgX;
	Curve.GridY[0] = Curve.Canvas.OrgY;
	for(int i=1; i<16; i++){
		Curve.GridX[i] = Curve.GridX[0] + (xWidth * i);
		if( Curve.GridX[i] > Curve.Canvas.MaxX){
			Curve.GridX[i] =Curve.GridX[0];
		}
		Curve.GridY[i] = Curve.GridY[0] - (yHeight * i);
		if( Curve.GridY[i] < Curve.Canvas.MaxY){
			Curve.GridY[i] = Curve.GridY[0];
		}
	}
}

void TDevProperty::CurveSetColor(u32 aClr0, u32 aClr1, u32 aClr2, u32 aClr3)
{
	Curve.Color[0]=aClr0;
	Curve.Color[1]=aClr1;
	Curve.Color[2]=aClr2;
	Curve.Color[3]=aClr3;
}

void TDevProperty::CurveSetScaling()
{
	if(HwDevType == htSmoker){
		Curve.Scaling[0] = 0.01;
		Curve.Scaling[1] = 0.1;
		Curve.Scaling[2] = 0.1;
	}else
	if(HwDevType == htTempe){
		Curve.Scaling[0] = 0.2;
		Curve.Scaling[1] = 0.78039;
		Curve.Scaling[2] = 1.0;
	}
}

void TDevProperty::CurveDrawText()
{
	int x,y;
	if(HwDevType == htSmoker){
		x = Curve.Canvas.OrgX - 40;
		y = Curve.Canvas.MaxY -4;
		TextRender_string24(x, y, Curve.Color[0], PanelColor, "20K");
		y += 88;
		TextRender_string24(x, y, Curve.Color[0], PanelColor, "10K");
		
		x = Curve.Canvas.MaxX + 4;
		y = Curve.Canvas.MaxY -4;
		TextRender_string24(x, y, Curve.Color[1], PanelColor, "2K");
		y += 88;
		TextRender_string24(x, y, Curve.Color[1], PanelColor, "1K");
		
		x = Curve.Canvas.OrgX - 56;
		y = Curve.Canvas.MaxY -4;
		TextRender_string24(x, y, Curve.Color[2], PanelColor, "2");
		y += 88;
		TextRender_string24(x, y, Curve.Color[2], PanelColor, "1");
	}else
	if(HwDevType == htTempe){
		x = Curve.Canvas.OrgX - 40;
		y = Curve.Canvas.MaxY -4;
		TextRender_string24(x, y, Curve.Color[0], PanelColor, "10K");
		x += 12;
		y += 88;
		TextRender_string24(x, y, Curve.Color[0], PanelColor, "5K");
		
		x = Curve.Canvas.MaxX + 1;
		y = Curve.Canvas.MaxY -4;
		TextRender_string24(x, y, Curve.Color[1], PanelColor, "+128");
		TCircle::sRender(x+48+4, y + 6, 2, Curve.Color[1], 0);
		TextRender_string24(x+48+4+4, y, Curve.Color[1], PanelColor, "C");
		x += 12;
		y += 88;
		TextRender_string24(x, y, Curve.Color[1], PanelColor, " 0");
		TCircle::sRender(x+24+4, y + 6, 2, Curve.Color[1], 0);
		TextRender_string24(x+24+4+4, y, Curve.Color[1], PanelColor, "C");
		x = Curve.Canvas.MaxX + 1;
		y += 88;
		TextRender_string24(x, y, Curve.Color[1], PanelColor, "-128");
		TCircle::sRender(x+48+4, y + 6, 2, Curve.Color[1], 0);
		TextRender_string24(x+48+4+4, y, Curve.Color[1], PanelColor, "C");
	}
}

void TDevProperty::CurvePutDataSmoke(unsigned short aD0, unsigned short aD1, unsigned short aD2, unsigned short aD3)
{
	float fV1, fV2;
	unsigned int iVal;
	Curve.DataLen++;
	if(Curve.DataLen >(Curve.MaxDataLen))Curve.DataLen = Curve.MaxDataLen;
	Curve.CurrentPst++;
	if(Curve.CurrentPst >= Curve.MaxDataLen){
		Curve.CurrentPst =0;
	}
	fV1 = aD0;
	fV2 = fV1 * Curve.Scaling[0];
	iVal = static_cast<unsigned int>(round(fV2));
	if(iVal >(Curve.Canvas.Height-2))iVal = Curve.Canvas.Height-2;
	IList->RealtimeData.Data[0][Curve.CurrentPst] = static_cast<unsigned char>(iVal);
	
	fV1 = aD1;
	fV2 = fV1 * Curve.Scaling[1];
	iVal = static_cast<unsigned int>(round(fV2));
	if(iVal >(Curve.Canvas.Height-2))iVal = Curve.Canvas.Height-2;
	IList->RealtimeData.Data[1][Curve.CurrentPst] = static_cast<unsigned char>(iVal);
	
	fV1 = aD2;
	fV2 = fV1 * Curve.Scaling[2];
	iVal = static_cast<unsigned int>(round(fV2));
	if(iVal >(Curve.Canvas.Height-2))iVal = Curve.Canvas.Height-2;
	IList->RealtimeData.Data[2][Curve.CurrentPst] = static_cast<unsigned char>(iVal);
	
	fV1 = aD3;
	fV2 = fV1 * Curve.Scaling[3];
	iVal = static_cast<unsigned int>(round(fV2));
	if(iVal >(Curve.Canvas.Height-2))iVal = Curve.Canvas.Height-2;
	IList->RealtimeData.Data[3][Curve.CurrentPst] = static_cast<unsigned char>(iVal);
}

void TDevProperty::CurvePutDataTempe(unsigned short aD0, unsigned short aD1, unsigned short aD2, unsigned short aD3)
{
	float fV1, fV2;
	unsigned char cVal;
	unsigned int iVal;
	Curve.DataLen++;
	if(Curve.DataLen >(Curve.MaxDataLen))Curve.DataLen = Curve.MaxDataLen;
	Curve.CurrentPst++;
	if(Curve.CurrentPst >= Curve.MaxDataLen){
		Curve.CurrentPst =0;
	}
	fV1 = aD0;
	fV2 = fV1 * Curve.Scaling[0];
	iVal = static_cast<unsigned int>(round(fV2));
	if(iVal >(Curve.Canvas.Height-2))iVal = Curve.Canvas.Height-2;
	IList->RealtimeData.Data[0][Curve.CurrentPst] = static_cast<unsigned char>(iVal);
	
	cVal = static_cast<unsigned char>(aD1);
	if(cVal<128)cVal+=128;
	else cVal=cVal-128;
	fV1 = cVal;
	fV2 = fV1 * Curve.Scaling[1];
	iVal = static_cast<unsigned int>(round(fV2));
	if(iVal >(Curve.Canvas.Height-2))iVal = Curve.Canvas.Height-2;
	IList->RealtimeData.Data[1][Curve.CurrentPst] = static_cast<unsigned char>(iVal);
	
	fV1 = aD2;
	fV2 = fV1 * Curve.Scaling[2];
	iVal = static_cast<unsigned int>(round(fV2));
	if(iVal >(Curve.Canvas.Height-2))iVal = Curve.Canvas.Height-2;
	IList->RealtimeData.Data[2][Curve.CurrentPst] = static_cast<unsigned char>(iVal);
	
	fV1 = aD3;
	fV2 = fV1 * Curve.Scaling[3];
	iVal = static_cast<unsigned int>(round(fV2));
	if(iVal >(Curve.Canvas.Height-2))iVal = Curve.Canvas.Height-2;
	IList->RealtimeData.Data[3][Curve.CurrentPst] = static_cast<unsigned char>(iVal);
}

void TDevProperty::CurveDrawOutline()
{
	HorizLineRender(Curve.Canvas.OrgX, Curve.Canvas.MaxY, Curve.Canvas.Width, dCurveGridClr);
	HorizLineRender(Curve.Canvas.OrgX, Curve.Canvas.OrgY, Curve.Canvas.Width, dCurveGridClr);
	VertLineRender(Curve.Canvas.OrgX, Curve.Canvas.MaxY, Curve.Canvas.Height, dCurveGridClr);
	VertLineRender(Curve.Canvas.MaxX, Curve.Canvas.MaxY, Curve.Canvas.Height, dCurveGridClr);
}

void TDevProperty::CurveDrawGrid()
{
	int i;
	for(i=0; i<Curve.GridXCount; i++){
		VertLineRender(Curve.GridX[i], Curve.Canvas.MaxY, Curve.Canvas.Height, dCurveGridClr);
	}
	for(i=0; i<Curve.GridXCount; i++){
		HorizLineRender(Curve.Canvas.OrgX, Curve.GridY[i], Curve.Canvas.Width, dCurveGridClr);
	}
}

void TDevProperty::CurveRecover()
{
	int i,c, x,y, v[4];
	if(Curve.HasDraw.PstStart <= Curve.HasDraw.PstEnd){
		x = Curve.Canvas.OrgX+1;
		y = Curve.Canvas.OrgY-1;
		for(i=Curve.HasDraw.PstStart; i<=Curve.HasDraw.PstEnd; i++){
			for(c=0;(c<Curve.UsingDataTypeCnt)&&(c<4); c++){
				v[c] = y - IList->RealtimeData.Data[c][i];
				PixelRender(x,v[c],PanelColor);
				PixelRender(x,v[c]-1,PanelColor);
			}
			x++;
		}
	}else{
		x = Curve.Canvas.OrgX+1;
		y = Curve.Canvas.OrgY-1;
		for(i=Curve.HasDraw.PstEnd; i<Curve.MaxDataLen; i++){
			for(c=0;(c<Curve.UsingDataTypeCnt)&&(c<4); c++){
				v[c] = y - IList->RealtimeData.Data[c][i];
				PixelRender(x,v[c],PanelColor);
				PixelRender(x,v[c]-1,PanelColor);
			}
			x++;
		}
		for(i=0; i<=Curve.HasDraw.PstStart; i++){
			for(c=0;(c<Curve.UsingDataTypeCnt)&&(c<4); c++){
				v[c] = y - IList->RealtimeData.Data[c][i];
				PixelRender(x,v[c],PanelColor);
				PixelRender(x,v[c]-1,PanelColor);
			}
			x++;
		}
	}
}

void TDevProperty::CurveDrawData()
{
	int i;
	int x,y;
	Curve.HasDraw.PstEnd = Curve.CurrentPst;
	Curve.HasDraw.PstStart = Curve.HasDraw.PstEnd - Curve.DataLen +1;
	if(Curve.HasDraw.PstStart <0)Curve.HasDraw.PstStart += Curve.DataLen;
	x = Curve.Canvas.OrgX +1;
	y = Curve.Canvas.OrgY -1;
	int c, v[4];
	if(Curve.HasDraw.PstStart <= Curve.HasDraw.PstEnd){
		x = Curve.Canvas.OrgX+1;
		y = Curve.Canvas.OrgY-1;
		for(i=Curve.HasDraw.PstStart; i<=Curve.HasDraw.PstEnd; i++){
			for(c=0;(c<Curve.UsingDataTypeCnt)&&(c<4); c++){
				v[c] = y - IList->RealtimeData.Data[c][i];
				PixelRender(x,v[c],Curve.Color[c]);
				PixelRender(x,v[c]-1,Curve.Color[c]);
			}
			if(Curve.UsingDataTypeCnt == 2){
				if(v[0] == v[1]){
					PixelRender(x,v[0],clNearWhite);
					PixelRender(x,v[0]-1,clNearWhite);
				}
			}else
			if(Curve.UsingDataTypeCnt == 3){
				if(v[0] == v[1]){
					PixelRender(x,v[0],clNearWhite);
					PixelRender(x,v[0]-1,clNearWhite);
				}
				if(v[0] == v[2]){
					PixelRender(x,v[0],clNearWhite);
					PixelRender(x,v[0]-1,clNearWhite);
				}
				if(v[1] == v[2]){
					PixelRender(x,v[1],clNearWhite);
					PixelRender(x,v[1]-1,clNearWhite);
				}
			}else
			if(Curve.UsingDataTypeCnt == 4){
				
			}
			x++;
		}
	}else{
		x = Curve.Canvas.OrgX+1;
		y = Curve.Canvas.OrgY-1;
		for(i=Curve.HasDraw.PstEnd; i<Curve.MaxDataLen; i++){
			for(c=0;(c<Curve.UsingDataTypeCnt)&&(c<4); c++){
				v[c] = y - IList->RealtimeData.Data[c][i];
				PixelRender(x,v[c],Curve.Color[c]);
				PixelRender(x,v[c]-1,Curve.Color[c]);
			}
			if(Curve.UsingDataTypeCnt == 2){
				if(v[0] == v[1]){
					PixelRender(x,v[0],clNearWhite);
					PixelRender(x,v[0]-1,clNearWhite);
				}
			}else
			if(Curve.UsingDataTypeCnt == 3){
				if(v[0] == v[1]){
					PixelRender(x,v[0],clNearWhite);
					PixelRender(x,v[0]-1,clNearWhite);
				}
				if(v[0] == v[2]){
					PixelRender(x,v[0],clNearWhite);
					PixelRender(x,v[0]-1,clNearWhite);
				}
				if(v[1] == v[2]){
					PixelRender(x,v[1],clNearWhite);
					PixelRender(x,v[1]-1,clNearWhite);
				}
			}else
			if(Curve.UsingDataTypeCnt == 4){
				
			}
			x++;
		}
		for(i=0; i<=Curve.HasDraw.PstStart; i++){
			for(c=0;(c<Curve.UsingDataTypeCnt)&&(c<4); c++){
				v[c] = y - IList->RealtimeData.Data[c][i];
				PixelRender(x,v[c],Curve.Color[c]);
				PixelRender(x,v[c]-1,Curve.Color[c]);
			}
			if(Curve.UsingDataTypeCnt == 2){
				if(v[0] == v[1]){
					PixelRender(x,v[0],clNearWhite);
					PixelRender(x,v[0]-1,clNearWhite);
				}
			}else
			if(Curve.UsingDataTypeCnt == 3){
				if(v[0] == v[1]){
					PixelRender(x,v[0],clNearWhite);
					PixelRender(x,v[0]-1,clNearWhite);
				}
				if(v[0] == v[2]){
					PixelRender(x,v[0],clNearWhite);
					PixelRender(x,v[0]-1,clNearWhite);
				}
				if(v[1] == v[2]){
					PixelRender(x,v[1],clNearWhite);
					PixelRender(x,v[1]-1,clNearWhite);
				}
			}else
			if(Curve.UsingDataTypeCnt == 4){
				
			}
			x++;
		}
	}
}

void TDevProperty::CurveDrawLastData()
{

}

void TDevProperty::CurveDraw(int IsFullRedraw)
{
	if(IsFullRedraw){
		CurveDrawOutline();
		CurveDrawText();
		CurveDrawGrid();
		if(Curve.DataLen){
			CurveDrawData();
		}
		Curve.HasUpdate =0;
	}else{
		if(Curve.DataLen){
			if(Curve.DataLen >= Curve.MaxDataLen){
				CurveRecover();
				CurveDrawGrid();
			}
			CurveDrawData();
		}
		Curve.HasUpdate =0;
	}
}

void TDevProperty::CurveHide()
{
	Curve.HasDrawOutline =0;
}












































