#include <Symbol.h>
#include <MyFrame.h>
#include <NewMath.h>

BOOL bLocalDraw = FALSE;
float pfAfxTorqueWave[8][20];
//仿真一些数据
void InitData()
{
    pfAfxTorqueWave[0][0] = 0.0f;
    pfAfxTorqueWave[1][0] = 3453.0f;
    pfAfxTorqueWave[2][0] = 0.02f;
    pfAfxTorqueWave[3][0] = 0.0f;
    pfAfxTorqueWave[7][0] = 0.0f;
    pfAfxTorqueWave[0][1] = 60.82;
    pfAfxTorqueWave[1][1] = 2384.0f;
    pfAfxTorqueWave[2][1] = 0.04f;  
    pfAfxTorqueWave[3][1] = 0.14f;
    pfAfxTorqueWave[7][1] = 0.546f;
    pfAfxTorqueWave[0][2] = 49.2f;
	pfAfxTorqueWave[1][2] = 1730.0f;
    pfAfxTorqueWave[2][2] = 0.06f;
	pfAfxTorqueWave[3][2] = 0.16f;
    pfAfxTorqueWave[7][2] = 0.880f;
    pfAfxTorqueWave[0][3] = 0.0f;
    pfAfxTorqueWave[1][3] = 0.0f;
    pfAfxTorqueWave[2][3] = 0.1f;
    pfAfxTorqueWave[3][3] = 0.0f;
    pfAfxTorqueWave[7][3] = 1.742f;
    pfAfxTorqueWave[0][4] = 44.7f;//Eff
	pfAfxTorqueWave[1][4] = 3059.0f;//Rpm
    pfAfxTorqueWave[2][4] = 0.029f;//Cur
    pfAfxTorqueWave[3][4] = 0.07f;//Pout
    pfAfxTorqueWave[7][4] = 0.2f;//Torque
    pfAfxTorqueWave[0][5] = 37.68f; //Eff
    pfAfxTorqueWave[1][5] = 1106.0f; //RPM
    pfAfxTorqueWave[2][5] = 0.0756f; //CUR
    pfAfxTorqueWave[3][5] = 0.13f; //Pout
    pfAfxTorqueWave[7][5] = 1.2f;//Torque
}

float fsAfxData[4][1024];
WORD  wsAfxData1[1024];
WORD  wsAfxData2[1024];

void far OnIdle_GraphDemoDialog(CObject* pCurObj)
{
	CDialog* pDialog = (CDialog *)pCurObj;
	CGraphWindow* pGraphWave1 = (CGraphWindow *)
		pDialog->ObjectFromID(GRAPHBASE + 1 - 1);
	CGraphWindow* pGraphWave2 = (CGraphWindow *)
		pDialog->ObjectFromID(GRAPHBASE + 2 - 1);

	RECT rc1 = *pGraphWave1->GetGraphWave()->GetWaveRect();
	RECT rc2 = *pGraphWave2->GetGraphWave()->GetWaveRect();


	int nBottom1   = rc1.bottom - 19;
	int nWidth1  = rc1.right - rc1.left + 1 - 5;
	int nHeight1 = rc1.bottom - rc1.top + 1;

	int nBottom2   = rc2.bottom - 19;
	int nWidth2  = rc2.right - rc2.left + 1 - 5;
	int nHeight2 = rc2.bottom - rc2.top + 1;

	SetColor(WHITE ^ CYAN);
	SetWriteMode(XOR_PUT);

	for (int k = 0; k < 2; k++)
	{
		if (!bLocalDraw && k == 0)
		{
			continue;
		}

		int nLeft1  = rc1.left + 2;
		WORD* pwData1 = &wsAfxData1[0];

		for (int i = 0; i < nWidth1; i += 3)
		{
			if (i == 0)
			{
				::MoveTo(nLeft1, nBottom1  - ((*pwData1) >> 5));
			}
			else
			{
				::LineTo(nLeft1, nBottom1  - ((*pwData1) >> 5));
			}
			nLeft1 += 3;
			pwData1++;
		}

		if (k == 0)
		{
			WORD wsData1[5];
			_fmemcpy(wsData1, &wsAfxData1[0], sizeof(WORD) * 5);
			_fmemcpy(&wsAfxData1[0], &wsAfxData1[5], sizeof(WORD) * (1000- 5));
			_fmemcpy(&wsAfxData1[1000 - 5], &wsData1[0], sizeof(WORD) * 5);
		}
	}


	for (k = 0; k < 2; k++)
	{
		if (!bLocalDraw && k == 0)
		{
			continue;
		}

		int nLeft2  = rc2.left + 2;
		WORD* pwData2 = &wsAfxData2[0];

		for (int i = 0; i < nWidth2; i += 3)
		{
			if (i == 0)
			{
				::MoveTo(nLeft2, nBottom2  - ((*pwData2) >> 5));
			}
			else
			{
				::LineTo(nLeft2, nBottom2  - ((*pwData2) >> 5));
			}
			nLeft2 += 3;
			pwData2++;
		}

		if (k == 0)
		{
			WORD wsData2[5];
			_fmemcpy(wsData2, &wsAfxData2[0], sizeof(WORD) * 5);
			_fmemcpy(&wsAfxData2[0], &wsAfxData2[5], sizeof(WORD) * (1000- 5));
			_fmemcpy(&wsAfxData2[1000 - 5], &wsData2[0], sizeof(WORD) * 5);
		}
	}

	bLocalDraw = TRUE;
	SetWriteMode(COPY_PUT);

}


void far OnTime_GraphDemoDialog(CObject* pCurObj)
{
	CDialog* pDialog = (CDialog *)pCurObj;
	CGraphWindow* pGraphWave1 = (CGraphWindow *)
		pDialog->ObjectFromID(GRAPHBASE + 1 - 1);

	CGraphWindow* pGraphWave2 = (CGraphWindow *)
		pDialog->ObjectFromID(GRAPHBASE + 2 - 1);

	CGraphWindow* pGraphWave3 = (CGraphWindow *)
		pDialog->ObjectFromID(GRAPHBASE + 3 - 1);

	pGraphWave3->GetGraphWave()->DisplayHAndV();

	OnIdle_GraphDemoDialog(pCurObj);
}


void far GraphDemoDialog()
{
	::InitData();
	bLocalDraw = FALSE;


	//Create a Dialog On the Center of Desktop
	CDialog* pDialog = new CDialog;//分配实例
	pDialog->CreateWindow(0, 0, 640, 480, "示波器演示");//建立窗口

	pDialog->Center();//窗口对中

	//Register Callback Function for Window
	//pDialog->SetIdleFc(OnIdle_GraphDemoDialog);
	pDialog->SetTimeFc(OnTime_GraphDemoDialog);//注册定时器回调函数
	pDialog->SetDrawFc(NULL);//注册画图回调函数
	pDialog->SetHelpFc(NULL);//注册帮助回调函数
	pDialog->SetKeyboardFc(NULL);//注册虚拟键盘回调函数

	//Create Close's Button Control
	new CCloseButton(pDialog);//分配和建立关闭按钮

	//Create a Button Control
	CButton* pButton1 = new CButton(pDialog);
	pButton1->CreateObject(19, 439, 589, 25, "取消(\x3\C\x3)");
	pButton1->SetShortcutKey(VK_ALT_C);
	pButton1->SetFc(ClickCancelButton);
	pButton1->SetID(BUTTONBASE + 1 - 1);

	//Create Wave Box Control
	CGraphWindow* pGraphWave1 = new CGraphWindow(pDialog);
	pGraphWave1->CreateObject(4, 23, 309, 197, "");
	pGraphWave1->GetGraphWave()->SetChannelCount(1);
	pGraphWave1->GetGraphWave()->SetCaption(" ");
	pGraphWave1->GetGraphWave()->SetChannelCaption(0, "A");
	pGraphWave1->GetGraphWave()->SetUnitH("");
	pGraphWave1->GetGraphWave()->SetInternalCountX(10);
	pGraphWave1->GetGraphWave()->SetInternalCountY(10);
	pGraphWave1->GetGraphWave()->SetDotX(0);
	pGraphWave1->GetGraphWave()->SetDotY(3);
	pGraphWave1->GetGraphWave()->SetRangeX(0.0f, 10.0);
	pGraphWave1->GetGraphWave()->SetChannelY(0, -5.0, +5.0);
	pGraphWave1->SetID(GRAPHBASE + 1 - 1);

	//Create Wave Box Control
	CGraphWindow* pGraphWave2 = new CGraphWindow(pDialog);
	pGraphWave2->CreateObject(319, 23, 309, 197, "");
	pGraphWave2->GetGraphWave()->SetChannelCount(1);
	pGraphWave2->GetGraphWave()->SetCaption(" ");
	pGraphWave2->GetGraphWave()->SetChannelCaption(0, "A");
	pGraphWave2->GetGraphWave()->SetUnitH("Hz");
	pGraphWave2->GetGraphWave()->SetInternalCountX(5);
	pGraphWave2->GetGraphWave()->SetInternalCountY(10);
	pGraphWave2->GetGraphWave()->SetDotX(0);
	pGraphWave2->GetGraphWave()->SetDotY(3);
	pGraphWave2->GetGraphWave()->SetRangeX(0.0f, 10.0);
	pGraphWave2->GetGraphWave()->SetChannelY(0, -5.0, +5.0);
	pGraphWave2->SetID(GRAPHBASE + 2 - 1);

	//Create Wave Box Control
	CGraphWindow* pGraphWave3 = new CGraphWindow(pDialog);
	pGraphWave3->CreateObject(4, 231, 624, 197, "");
	pGraphWave3->GetGraphWave()->SetChannelCount(1);
	pGraphWave3->GetGraphWave()->SetCaption(" ");
	pGraphWave3->GetGraphWave()->SetChannelCaption(0, "A");
	pGraphWave3->GetGraphWave()->SetUnitH("Hz");
	pGraphWave3->GetGraphWave()->SetInternalCountX(10);
	pGraphWave3->GetGraphWave()->SetInternalCountY(10);
	pGraphWave3->GetGraphWave()->SetDotX(0);
	pGraphWave3->GetGraphWave()->SetDotY(3);
	pGraphWave3->GetGraphWave()->SetRangeX(0.0f, 10.0);
	pGraphWave3->GetGraphWave()->SetChannelY(0, -5.0, +5.0);
	pGraphWave3->SetID(GRAPHBASE + 3 - 1);


    //示波器共有四个通道
	pGraphWave3->GetGraphWave()->SetChannelCount(4);
	//设置示波器的某个通道的纵坐标
    pGraphWave3->GetGraphWave()->SetChannelY(0, 0.0f, 75.0f);
    pGraphWave3->GetGraphWave()->SetChannelY(1, 0.0f, 3500.0f);
    pGraphWave3->GetGraphWave()->SetChannelY(2, 0.0f, 0.10f);
	pGraphWave3->GetGraphWave()->SetChannelY(3, 0.0f, 1.00f);

    //设置通道纵座标的单位
    pGraphWave3->GetGraphWave()->SetChannelCaption(0, "Eff(%)");
    pGraphWave3->GetGraphWave()->SetChannelCaption(1, "N(RPM)");
    pGraphWave3->GetGraphWave()->SetChannelCaption(2, "I(A)");
    pGraphWave3->GetGraphWave()->SetChannelCaption(3, "Pout(W)");

    //设置示波器的横座标
	pGraphWave3->GetGraphWave()->SetRangeX(0.0f, 2.00f);
	//设置示波器的横座标的小数点个数
	pGraphWave3->GetGraphWave()->SetDotX(2);
	//设置示波器横向分为5等份
    pGraphWave3->GetGraphWave()->SetInternalCountX(5);
    //设置示波器的标题
	pGraphWave3->GetGraphWave()->SetCaption(" TORQUE GRAPH ");
	pGraphWave3->GetGraphWave()->SetLimitRangeX(TRUE, 0.0f, 1.743f);


	//Show Window
	pDialog->ShowWindow();//显示窗口

   //显示示波器
	//pGraphWave3->GetGraphWave()->ShowFrame();
    //将示波器置成"3阶的曲线拟合",以便显示第"0"通道的波形
    pGraphWave3->GetGraphWave()->SetTorquegraphAttrib(TORQUEGRAPH_CURVEFITTING, 3);
    pGraphWave3->GetGraphWave()->DrawWave(0, pfAfxTorqueWave[7], pfAfxTorqueWave[0], 6, COPY_PUT, WHITE);//显示第0通道
    //将示波器置成"直线拟合",以便显示第"1"通道的波形
    pGraphWave3->GetGraphWave()->SetTorquegraphAttrib(TORQUEGRAPH_LINEFITTING, 3);
    pGraphWave3->GetGraphWave()->DrawWave(1, pfAfxTorqueWave[7], pfAfxTorqueWave[1], 6, COPY_PUT, YELLOW);//显示第1通道
    //将示波器置成"直线拟合",以便显示第"3"通道的波形
    pGraphWave3->GetGraphWave()->SetTorquegraphAttrib(TORQUEGRAPH_LINEFITTING, 3);
    pGraphWave3->GetGraphWave()->DrawWave(2, pfAfxTorqueWave[7], pfAfxTorqueWave[2], 6, COPY_PUT, MAGENTA);//显示第2通道
    //将示波器置成"3阶的曲线拟合",以便显示第"4"通道的波形
	pGraphWave3->GetGraphWave()->SetTorquegraphAttrib(TORQUEGRAPH_CURVEFITTING, 3);
    pGraphWave3->GetGraphWave()->DrawWave(3, pfAfxTorqueWave[7], pfAfxTorqueWave[3], 6, COPY_PUT, RED);//显示第3通道
    //显示各个通道的数据点
	for (int i = 0; i < 6; i++)
	{
    	//显示第"1"通道的第"i"个数据点,半径为2的45度十字架,白色.
        pGraphWave3->GetGraphWave()->DrawDot(0, pfAfxTorqueWave[7][i], pfAfxTorqueWave[0][i], 2, WHITE, GRAPH_DOT_ANGLE45);
	    //显示第"2"通道的第"i"个数据点,半径为2的小圆圈,黄色.
        pGraphWave3->GetGraphWave()->DrawDot(1, pfAfxTorqueWave[7][i], pfAfxTorqueWave[1][i], 2, YELLOW, GRAPH_DOT_CIRCLE);
		//显示第"3"通道的第"i"个数据点,半径为2的小圆圈,紫色.
        pGraphWave3->GetGraphWave()->DrawDot(2, pfAfxTorqueWave[7][i], pfAfxTorqueWave[2][i], 2, MAGENTA, GRAPH_DOT_CIRCLE);
		//显示第"4"通道的第"i"个数据点,半径为2的45度十字架,红色,
		pGraphWave3->GetGraphWave()->DrawDot(3, pfAfxTorqueWave[7][i], pfAfxTorqueWave[3][i], 2, RED, GRAPH_DOT_ANGLE45);

    }


	::GetSinWave(fsAfxData[0], 1000, 2047.5, 500.0,
		0.0, 2047.5, 12.5 * 1000.0, FALSE);


	::GetRectangleWave(fsAfxData[1], 1000, 3800, 500.0,
		0.0, 0.0, 0.5, 12.5 * 1000.0, FALSE);

	for (i = 0; i < 1000; i++)
	{
		wsAfxData1[i] = (WORD)fsAfxData[0][i];
		wsAfxData2[i] = (WORD)fsAfxData[1][i];

	}



	//::GetSinWave(fsAfxData[0], 1024, 3.0, 100.0,
	//	0.0, 0.0, 12.5 * 1000.0, FALSE);

	//Show Window
	//pDialog->ShowWindow();//显示窗口

	//go into Message Loop
	pDialog->DoModal();//进入窗口消息循环

	//Destroy Window
	delete pDialog;//删除对话框

}
