
// Compare1DDlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "一维比较.h"
#include "Compare1DDlg.h"
#include "afxdialogex.h"
#include"MC_dll.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
WORD CCompare1DDlg::cardConnectNo = 0;

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CCompare1DDlg 对话框



CCompare1DDlg::CCompare1DDlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_MY_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CCompare1DDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_TAB1, tabControl);
}

BEGIN_MESSAGE_MAP(CCompare1DDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, &CCompare1DDlg::OnTcnSelchangeTab1)
	ON_WM_DESTROY()
	ON_WM_TIMER()
END_MESSAGE_MAP()


// CCompare1DDlg 消息处理程序

BOOL CCompare1DDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	RECT tabRect;
	tabControl.GetClientRect(&tabRect);//获得选项卡区域大小的坐标(上下左右)
	tabRect.top += 26;//对坐标做一些微调，这26是选项卡按钮的高度
	tabRect.left += 2;//这是边框的宽度

	tabControl.InsertItem(0, TEXT("软件比较"));
	tabControl.InsertItem(1, TEXT("高速比较"));

	SoftCmp1D_Handle.Create(SoftCompare1D::IDD);
	HSCmp1D_Handle.Create(HSCompare1D::IDD);

	SoftCmp1D_Handle.SetParent(&tabControl);
	HSCmp1D_Handle.SetParent(&tabControl);

#pragma region 软件比较界面初始化

	SoftCmp1D_Handle.cmpChan.SetCurSel(0);
	for (int _axisNo = 0; _axisNo < 12; _axisNo++)
	{
		CString str;
		str.Format(_T("轴%d"), _axisNo);
		SoftCmp1D_Handle.cmpAxis.InsertString(_axisNo,str);
	}
	SoftCmp1D_Handle.cmpAxis.SetCurSel(0); //比较轴号

	SoftCmp1D_Handle.cmpMode.InsertString(0,L"小于");
	SoftCmp1D_Handle.cmpMode.InsertString(1,L"大于等于");
	SoftCmp1D_Handle.cmpMode.SetCurSel(0); //比较模式

	SoftCmp1D_Handle.cmpSource.InsertString(0, L"指令位置");
	SoftCmp1D_Handle.cmpSource.InsertString(1, L"反馈位置");
	SoftCmp1D_Handle.cmpSource.SetCurSel(0); //比较位置源

	SoftCmp1D_Handle.triggerMode.InsertString(0, L"IO输出低");
	SoftCmp1D_Handle.triggerMode.InsertString(1, L"IO输出高");
	SoftCmp1D_Handle.triggerMode.InsertString(2, L"IO输出反向");
	SoftCmp1D_Handle.triggerMode.InsertString(3, L"IO输出脉冲");
	SoftCmp1D_Handle.triggerMode.InsertString(4, L"停止轴");
	SoftCmp1D_Handle.triggerMode.SetCurSel(0);//设置触发模式


	for (int _outputNo = 0; _outputNo < 2; _outputNo++)
	{
		CString outputStr;
		outputStr.Format(_T("Output%d"), _outputNo);
		SoftCmp1D_Handle.triggerPrm.InsertString(_outputNo, outputStr);
	}
	SoftCmp1D_Handle.triggerPrm.SetCurSel(0); //触发参数


#pragma endregion

#pragma region 高速比较界面初始化
	HSCmp1D_Handle.cmpChan.SetCurSel(0); //比较通道
	for (int _axisNo = 0; _axisNo < 12; _axisNo++)
	{
		CString str;
		str.Format(_T("轴%d"), _axisNo);
		HSCmp1D_Handle.cmpAxis.InsertString(_axisNo, str);
	}
	HSCmp1D_Handle.cmpAxis.SetCurSel(0); //比较轴号

	HSCmp1D_Handle.cmpSource.InsertString(0, L"指令位置");
	HSCmp1D_Handle.cmpSource.InsertString(1, L"反馈位置");
	HSCmp1D_Handle.cmpSource.SetCurSel(0); //比较位置源

	HSCmp1D_Handle.cmpMode.InsertString(0, L"小于比较点");
	HSCmp1D_Handle.cmpMode.InsertString(1, L"大于比较点");
	HSCmp1D_Handle.cmpMode.InsertString(2, L"等增量比较");
	HSCmp1D_Handle.cmpMode.InsertString(3, L"FIFO点比较输出");
	HSCmp1D_Handle.cmpMode.SetCurSel(0); //比较模式

	HSCmp1D_Handle.cmpTriggerMode.InsertString(0, L"IO输出低");
	HSCmp1D_Handle.cmpTriggerMode.InsertString(1, L"IO输出高");
	HSCmp1D_Handle.cmpTriggerMode.InsertString(2, L"IO输出脉冲");
	HSCmp1D_Handle.cmpTriggerMode.SetCurSel(0);//触发模式
#pragma endregion


	SoftCmp1D_Handle.MoveWindow(&tabRect);
	HSCmp1D_Handle.MoveWindow(&tabRect);

	SoftCmp1D_Handle.ShowWindow(SW_SHOW);

#pragma region 开卡
	WORD pciNum = 0; //用于接收开卡成功的卡数
	WORD pcieIndex[12];
	DWORD functionReturn = 0; //函数返回值

	functionReturn = MC_Open(0, CONNECTION_PCI, "", &pciNum, pcieIndex);
	if (functionReturn == 0)//函数成功执行
	{
		if (pciNum > 0)//至少一张卡被成功开卡
		{
			cardConnectNo = pcieIndex[0]; //如果有多卡的情况下这里也只用卡1演示

#pragma region 获取卡信息
			//--------------------------获取卡信息------------------------------------------------------------------------
			WORD totalAxisNum = 0; //用于获取卡的轴数
			WORD linerNum = 0;//用于获取卡的插补系个数
			WORD totalInNum = 0;//用于获取卡的输入口数量
			WORD totalOutNum = 0;//用于获取卡的输出口数量
			WORD totalPWMNum = 0;//用于获取卡的PWM通道数量
			functionReturn = MC_GetTotalAxesAndIONum(cardConnectNo, &totalAxisNum, &linerNum, &totalInNum, &totalOutNum, &totalPWMNum);

			//------------------------------------------------------------------------------------------------------------
#pragma endregion
			SetTimer(1, 200, NULL);//启动界面监控定时器

		}
		else
		{
			MessageBox(L"未找到卡!");
		}
	}
	else
	{
		MessageBox(L"开卡失败!");
	}
#pragma endregion

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CCompare1DDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CCompare1DDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CCompare1DDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}



void CCompare1DDlg::OnTcnSelchangeTab1(NMHDR* pNMHDR, LRESULT* pResult)
{
	// TODO: 在此添加控件通知处理程序代码
	switch (tabControl.GetCurSel())
	{
	case 0:
		SoftCmp1D_Handle.ShowWindow(SW_SHOW);
		HSCmp1D_Handle.ShowWindow(SW_HIDE);
		break;
	case 1:
		HSCmp1D_Handle.ShowWindow(SW_SHOW);
		SoftCmp1D_Handle.ShowWindow(SW_HIDE);
		break;
	}

	*pResult = 0;
}

/// <summary>
/// 窗体销毁时发生
/// </summary>
void CCompare1DDlg::OnDestroy()
{
	CDialogEx::OnDestroy();

	// TODO: 在此处添加消息处理程序代码
	KillTimer(1);
	MC_Close(cardConnectNo); //关卡释放资源
}

/// <summary>
/// 定时器更新界面
/// </summary>
/// <param name="nIDEvent"></param>
void CCompare1DDlg::OnTimer(UINT_PTR nIDEvent)
{
	// TODO: 在此添加消息处理程序代码和/或调用默认值
#pragma region 软件比较
	WORD enable = 0;
	double running_pos = 0;
	DWORD completed_points = 0;
	DWORD remained_points = 0;
	WORD cmpChan = SoftCmp1D_Handle.cmpChan.GetCurSel();
	MC_GetSoftCmpStatus(cardConnectNo,
		Compare_1D, //比较维数
		cmpChan, //比较通道
		&enable, //比较器使能
		&running_pos, //当前比较点位置
		&completed_points, //已完成比较点
		&remained_points); //剩余比较点

	switch (enable)
	{
	case 0:
		SoftCmp1D_Handle.GetDlgItem(IDC_EDIT4)->SetWindowTextW(L"未启用");
		break;
	case 1:
		SoftCmp1D_Handle.GetDlgItem(IDC_EDIT4)->SetWindowTextW(L"使能");
		break;
	default:
		SoftCmp1D_Handle.GetDlgItem(IDC_EDIT4)->SetWindowTextW(L"未定义");
		break;
	}
	CString runningPosStr;
	runningPosStr.Format(_T("%f"), running_pos);//当前比较位置点
	SoftCmp1D_Handle.GetDlgItem(IDC_EDIT6)->SetWindowTextW(runningPosStr);

	CString completedPoints;
	completedPoints.Format(_T("%d"), completed_points);//已完成比较点
	SoftCmp1D_Handle.GetDlgItem(IDC_EDIT5)->SetWindowTextW(completedPoints);

	CString remainedPoints;
	remainedPoints.Format(_T("%d"), remained_points);
	SoftCmp1D_Handle.GetDlgItem(IDC_EDIT7)->SetWindowTextW(remainedPoints);


	WORD axis0 = SoftCmp1D_Handle.cmpAxis.GetCurSel();
	double cmdPos = 0;
	MC_GetAxisCmdPos(cardConnectNo, axis0, &cmdPos);//获取当前指令位置
	CString cmdPosStr;
	cmdPosStr.Format(_T("%f"), cmdPos);
	SoftCmp1D_Handle.GetDlgItem(IDC_EDIT9)->SetWindowTextW(cmdPosStr);

	double cmdSpeed = 0;
	MC_GetAxisCmdSpeed(cardConnectNo, axis0, &cmdSpeed); //获取当前指令速度
	CString cmdSpeedStr;
	cmdSpeedStr.Format(_T("%f"), cmdSpeed);
	SoftCmp1D_Handle.GetDlgItem(IDC_EDIT8)->SetWindowTextW(cmdSpeedStr);

	WORD checkDone = 1;
	MC_GetAxisCheckDone(cardConnectNo, axis0, &checkDone);//获取轴运动状态
	switch (checkDone)
	{
	case 0:
		SoftCmp1D_Handle.GetDlgItem(IDC_EDIT10)->SetWindowTextW(L"运动中");
		break;
	case 1:
		SoftCmp1D_Handle.GetDlgItem(IDC_EDIT10)->SetWindowTextW(L"空闲");
		break;
	default:
		CString checkDoneStr;
		checkDoneStr.Format(_T("%d"), checkDone);
		SoftCmp1D_Handle.GetDlgItem(IDC_EDIT10)->SetWindowTextW(checkDoneStr);

		break;
	}

	if (SoftCmp1D_Handle.triggerMode.GetCurSel() == 4)
	{
		WORD axis1 = SoftCmp1D_Handle.triggerPrm.GetCurSel();
		MC_GetAxisCmdPos(cardConnectNo, axis1, &cmdPos);//获取当前指令位置
		cmdPosStr.Format(_T("%f"), cmdPos);
		SoftCmp1D_Handle.GetDlgItem(IDC_EDIT12)->SetWindowTextW(cmdPosStr);

		MC_GetAxisCmdSpeed(cardConnectNo, axis1, &cmdSpeed); //获取当前指令速度
		cmdSpeedStr.Format(_T("%f"), cmdSpeed);
		SoftCmp1D_Handle.GetDlgItem(IDC_EDIT11)->SetWindowTextW(cmdSpeedStr);

		MC_GetAxisCheckDone(cardConnectNo, axis1, &checkDone);//获取轴运动状态
		switch (checkDone)
		{
		case 0:
			SoftCmp1D_Handle.GetDlgItem(IDC_EDIT13)->SetWindowTextW(L"运动中");
			break;
		case 1:
			SoftCmp1D_Handle.GetDlgItem(IDC_EDIT13)->SetWindowTextW(L"空闲");
			break;
		default:
			CString checkDoneStr;
			checkDoneStr.Format(_T("%d"), checkDone);
			SoftCmp1D_Handle.GetDlgItem(IDC_EDIT13)->SetWindowTextW(checkDoneStr);
			break;
		}
	}

	byte ioState = 0;
	MC_GetOutIoBit(cardConnectNo, 0, &ioState);
	if (ioState == 0) SoftCmp1D_Handle.output0State.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONGREEN));
	else SoftCmp1D_Handle.output0State.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONRED));

	MC_GetOutIoBit(cardConnectNo, 1, &ioState);
	if (ioState == 0) SoftCmp1D_Handle.output1State.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONGREEN));
	else SoftCmp1D_Handle.output1State.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONRED));

#pragma endregion

#pragma region 高速比较
	WORD HSenable = 0;
	double HSrunning_pos = 0;
	DWORD HScompleted_points = 0;
	DWORD HSremained_points = 0;
	WORD HScmpChan = HSCmp1D_Handle.cmpChan.GetCurSel();

	MC_GetHSCmpStatus(cardConnectNo,
		Compare_1D,
		HScmpChan,
		&HSenable,
		&HSrunning_pos,
		&HScompleted_points,
		&HSremained_points);
	switch (HSenable)
	{
	case 0:
		HSCmp1D_Handle.GetDlgItem(IDC_EDIT5)->SetWindowTextW(L"未启用");
		break;
	case 1:
		HSCmp1D_Handle.GetDlgItem(IDC_EDIT5)->SetWindowTextW(L"使能");
		break;
	default:
		HSCmp1D_Handle.GetDlgItem(IDC_EDIT5)->SetWindowTextW(L"未定义");
		break;
	}

	CString HSrunningPosStr;
	HSrunningPosStr.Format(_T("%f"), HSrunning_pos);//当前比较位置点
	HSCmp1D_Handle.GetDlgItem(IDC_EDIT14)->SetWindowTextW(HSrunningPosStr);

	CString HScompletedPoints;
	HScompletedPoints.Format(_T("%d"), HScompleted_points);//已完成比较点
	HSCmp1D_Handle.GetDlgItem(IDC_EDIT6)->SetWindowTextW(HScompletedPoints);

	CString HSremainedPoints;
	HSremainedPoints.Format(_T("%d"), HSremained_points);
	HSCmp1D_Handle.GetDlgItem(IDC_EDIT7)->SetWindowTextW(HSremainedPoints);

	WORD HSaxis0 = HSCmp1D_Handle.cmpAxis.GetCurSel();
	double HScmdPos = 0;
	MC_GetAxisCmdPos(cardConnectNo, axis0, &HScmdPos);//获取当前指令位置
	CString HScmdPosStr;
	HScmdPosStr.Format(_T("%f"), HScmdPos);
	HSCmp1D_Handle.GetDlgItem(IDC_EDIT9)->SetWindowTextW(HScmdPosStr);

	double HScmdSpeed = 0;
	MC_GetAxisCmdSpeed(cardConnectNo, axis0, &HScmdSpeed); //获取当前指令速度
	CString HScmdSpeedStr;
	HScmdSpeedStr.Format(_T("%f"), HScmdSpeed);
	HSCmp1D_Handle.GetDlgItem(IDC_EDIT8)->SetWindowTextW(HScmdSpeedStr);

	WORD HSCheckDone = 0;
	MC_GetAxisCheckDone(cardConnectNo, axis0, &HSCheckDone);//获取轴运动状态
	switch (HSCheckDone)
	{
	case 0:
		HSCmp1D_Handle.GetDlgItem(IDC_EDIT10)->SetWindowTextW(L"运动中");
		break;
	case 1:
		HSCmp1D_Handle.GetDlgItem(IDC_EDIT10)->SetWindowTextW(L"空闲");
		break;
	default:
		CString HScheckDoneStr;
		HScheckDoneStr.Format(_T("%d"), checkDone);
		HSCmp1D_Handle.GetDlgItem(IDC_EDIT10)->SetWindowTextW(HScheckDoneStr);

		break;
	}

	byte HSIoState = 0;
	MC_GetOutIoBit(cardConnectNo, 12, &HSIoState);
	if (HSIoState == 0) HSCmp1D_Handle.output12.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONGREEN));
	else HSCmp1D_Handle.output12.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONRED));

	MC_GetOutIoBit(cardConnectNo, 13, &HSIoState);
	if (HSIoState == 0) HSCmp1D_Handle.output13.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONGREEN));
	else HSCmp1D_Handle.output13.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONRED));

	MC_GetOutIoBit(cardConnectNo, 14, &HSIoState);
	if (HSIoState == 0) HSCmp1D_Handle.output14.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONGREEN));
	else HSCmp1D_Handle.output14.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONRED));

	MC_GetOutIoBit(cardConnectNo, 15, &HSIoState);
	if (HSIoState == 0) HSCmp1D_Handle.output15.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONGREEN));
	else HSCmp1D_Handle.output15.SetIcon(AfxGetApp()->LoadIcon(IDI_ICONRED));
#pragma endregion


	CDialogEx::OnTimer(nIDEvent);
}
