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

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

// HSCompare1D 对话框

IMPLEMENT_DYNAMIC(HSCompare1D, CDialogEx)

HSCompare1D::HSCompare1D(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOGHS_CMP, pParent)
{
	cmpPos = 0;
	ioOutTime = 0;
	IncrementalValue = 0;
}

HSCompare1D::~HSCompare1D()
{
}

void HSCompare1D::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT2, cmpPos);
	DDX_Text(pDX, IDC_EDIT3, ioOutTime);
	DDX_Text(pDX, IDC_EDIT4, IncrementalValue);
	DDX_Control(pDX, IDC_COMBO1, cmpChan);
	DDX_Control(pDX, IDC_COMBO3, cmpAxis);
	DDX_Control(pDX, IDC_COMBO2, cmpMode);
	DDX_Control(pDX, IDC_COMBO4, cmpSource);
	DDX_Control(pDX, IDC_COMBO5, cmpTriggerMode);
	DDX_Control(pDX, IDC_STATIC_OUTPUT12, output12);
	DDX_Control(pDX, IDC_STATIC_OUTPUT13, output13);
	DDX_Control(pDX, IDC_STATIC_OUTPUT14, output14);
	DDX_Control(pDX, IDC_STATIC_OUTPUT15, output15);
	DDX_Control(pDX, IDC_STATIC_GBAXIS0, GroupBoxAxis0);
}


BEGIN_MESSAGE_MAP(HSCompare1D, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON8, &HSCompare1D::OnBnClickedButton8)
    ON_BN_CLICKED(IDC_BUTTON6, &HSCompare1D::OnBnClickedButton6)
    ON_BN_CLICKED(IDC_BUTTON9, &HSCompare1D::OnBnClickedButton9)
    ON_BN_CLICKED(IDC_BUTTON4, &HSCompare1D::OnBnClickedButton4)
    ON_BN_CLICKED(IDC_BUTTON2, &HSCompare1D::OnBnClickedButton2)
    ON_BN_CLICKED(IDC_BUTTON3, &HSCompare1D::OnBnClickedButton3)
    ON_BN_CLICKED(IDC_BUTTON7, &HSCompare1D::OnBnClickedButton7)
    ON_BN_CLICKED(IDC_BUTTON5, &HSCompare1D::OnBnClickedButton5)
END_MESSAGE_MAP()


// HSCompare1D 消息处理程序


/// <summary>
/// 高速比较启动比较按钮点击
/// </summary>
void HSCompare1D::OnBnClickedButton8()
{
	// TODO: 在此添加控件通知处理程序代码
    UpdateData(true);
    WORD Chan = cmpChan.GetCurSel();
    WORD Axis = cmpAxis.GetCurSel();//轴号
    WORD posSrc = cmpSource.GetCurSel();//位置源

    WORD Mode = cmpMode.GetCurSel();//比较模式
    WORD cmpCount = 1;
    if (Mode == 3) cmpCount = 2;
    double posArr[2]{0,0};
    posArr[0] = cmpPos;//比较位置
    if (cmpCount == 2) posArr[1] = IncrementalValue; //增量值

    WORD trMode = cmpTriggerMode.GetCurSel(); //触发模式
    
    double outTime = ioOutTime; //IO输出时间

    MC_StartHS1DCmp(CCompare1DDlg::cardConnectNo,
        Chan,
        Axis,
        posSrc,
        cmpCount,
        posArr,
        Mode,
        trMode,
        0,
        outTime);

    //启动比较之后启动相关轴运动
    MC_Power(CCompare1DDlg::cardConnectNo, Axis, 1);
    MC_MoveVelocity(CCompare1DDlg::cardConnectNo, Axis, 100, 100, 1000, 0.1, 0.1, 0, 1000000);
    CString str;
    str.Format(L"轴%d", Axis);
    GroupBoxAxis0.SetWindowTextW(str);

    UpdateData(false);
}

/// <summary>
/// 高速比较的停止比较按钮点击
/// </summary>
void HSCompare1D::OnBnClickedButton6()
{
    // TODO: 在此添加控件通知处理程序代码
    WORD Chan = cmpChan.GetCurSel(); //比较通道
    MC_StopHSCmp(CCompare1DDlg::cardConnectNo, Compare_1D, Chan);
    MC_StopEmgImd(CCompare1DDlg::cardConnectNo);//停止所有轴的运动
}

/// <summary>
/// 高速比较的比较器清零
/// </summary>
void HSCompare1D::OnBnClickedButton9()
{
    // TODO: 在此添加控件通知处理程序代码
    WORD Chan = cmpChan.GetCurSel(); //比较通道
    MC_ClearHSCmp(CCompare1DDlg::cardConnectNo, Compare_1D, Chan);

}

/// <summary>
/// 高速比较的位置清除
/// </summary>
void HSCompare1D::OnBnClickedButton4()
{
    // TODO: 在此添加控件通知处理程序代码
    WORD axis0 = cmpAxis.GetCurSel();
    MC_SetAxisCmdPos(CCompare1DDlg::cardConnectNo, axis0, 0); //位置清零
}


/// <summary>
/// output12
/// </summary>
void HSCompare1D::OnBnClickedButton2()
{
    // TODO: 在此添加控件通知处理程序代码
    HICON redIcon = AfxGetApp()->LoadIcon(IDI_ICONRED);

    if (((CStatic*)GetDlgItem(IDC_STATIC_OUTPUT12))->GetIcon() == redIcon)
    {
        MC_SetOutIoBit(CCompare1DDlg::cardConnectNo, 12, 0);
    }
    else
    {
        MC_SetOutIoBit(CCompare1DDlg::cardConnectNo, 12, 1);
    }
}

/// <summary>
/// output13
/// </summary>
void HSCompare1D::OnBnClickedButton3()
{
    // TODO: 在此添加控件通知处理程序代码
    HICON redIcon = AfxGetApp()->LoadIcon(IDI_ICONRED);

    if (((CStatic*)GetDlgItem(IDC_STATIC_OUTPUT13))->GetIcon() == redIcon)
    {
        MC_SetOutIoBit(CCompare1DDlg::cardConnectNo, 13, 0);
    }
    else
    {
        MC_SetOutIoBit(CCompare1DDlg::cardConnectNo, 13, 1);
    }
}

/// <summary>
/// output14
/// </summary>
void HSCompare1D::OnBnClickedButton7()
{
    // TODO: 在此添加控件通知处理程序代码
    HICON redIcon = AfxGetApp()->LoadIcon(IDI_ICONRED);

    if (((CStatic*)GetDlgItem(IDC_STATIC_OUTPUT14))->GetIcon() == redIcon)
    {
        MC_SetOutIoBit(CCompare1DDlg::cardConnectNo, 14, 0);
    }
    else
    {
        MC_SetOutIoBit(CCompare1DDlg::cardConnectNo, 14, 1);
    }
}

/// <summary>
/// output15
/// </summary>
void HSCompare1D::OnBnClickedButton5()
{
    // TODO: 在此添加控件通知处理程序代码
    HICON redIcon = AfxGetApp()->LoadIcon(IDI_ICONRED);

    if (((CStatic*)GetDlgItem(IDC_STATIC_OUTPUT15))->GetIcon() == redIcon)
    {
        MC_SetOutIoBit(CCompare1DDlg::cardConnectNo, 15, 0);
    }
    else
    {
        MC_SetOutIoBit(CCompare1DDlg::cardConnectNo, 15, 1);
    }
}
