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

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

// SoftCompare1D 对话框

IMPLEMENT_DYNAMIC(SoftCompare1D, CDialogEx)

SoftCompare1D::SoftCompare1D(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_DIALOGSOFT_CMP, pParent)
{
	cmpPos = 0;
	ioOutTime = 0;
}

SoftCompare1D::~SoftCompare1D()
{
}

void SoftCompare1D::DoDataExchange(CDataExchange* pDX)
{
    CDialogEx::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_EDIT2, cmpPos);
    DDX_Text(pDX, IDC_EDIT3, ioOutTime);
    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, triggerMode);
    DDX_Control(pDX, IDC_COMBO6, triggerPrm);
    DDX_Control(pDX, IDC_STATIC_OUTPUT0, output0State);
    DDX_Control(pDX, IDC_STATIC_OUTPUT1, output1State);
    DDX_Control(pDX, IDC_STATIC_GBAXIS0, GroupBoxAxis0);
    DDX_Control(pDX, IDC_STATIC_GBAXIS1, GroupBoxAxis1);
}


BEGIN_MESSAGE_MAP(SoftCompare1D, CDialogEx)
	ON_BN_CLICKED(IDC_BUTTON4, &SoftCompare1D::OnBnClickedButton4)
    ON_BN_CLICKED(IDC_BUTTON6, &SoftCompare1D::OnBnClickedButton6)
    ON_BN_CLICKED(IDC_BUTTON5, &SoftCompare1D::OnBnClickedButton5)
    ON_BN_CLICKED(IDC_BUTTON1, &SoftCompare1D::OnBnClickedButton1)
    ON_BN_CLICKED(IDC_BUTTON2, &SoftCompare1D::OnBnClickedButton2)
    ON_BN_CLICKED(IDC_BUTTON3, &SoftCompare1D::OnBnClickedButton3)
    ON_CBN_SELCHANGE(IDC_COMBO5, &SoftCompare1D::OnCbnSelchangeCombo5)
END_MESSAGE_MAP()


// SoftCompare1D 消息处理程序


/// <summary>
/// 软件比较启动比较按钮点击
/// </summary>
void SoftCompare1D::OnBnClickedButton4()
{
	// TODO: 在此添加控件通知处理程序代码
    UpdateData(true);

    WORD Chan = cmpChan.GetCurSel(); //比较通道
    WORD Axis = cmpAxis.GetCurSel(); //比较轴号
    WORD posSrc = cmpSource.GetCurSel(); //比较位置源
    double pos = cmpPos; //比较点位置
    WORD Mode = cmpMode.GetCurSel(); //比较模式
    WORD trMode = triggerMode.GetCurSel(); //触发模式
    WORD trPrm = triggerPrm.GetCurSel(); //触发参数
    double outTime = ioOutTime; //输出时间

    MC_StartSoft1DCmp(CCompare1DDlg::cardConnectNo, //启动一维软件比较
        Chan,
        Axis,
        posSrc,
        1,
        &pos,
        Mode,
        trMode,
        trPrm,
        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);

    if (triggerMode.GetCurSel() == 4)
    {
        WORD axis1 = triggerPrm.GetCurSel();
        MC_Power(CCompare1DDlg::cardConnectNo, axis1, 1); //使能轴
        MC_MoveVelocity(CCompare1DDlg::cardConnectNo, axis1, 100, 100, 1000, 0.1, 0.1, 0, 1000000);//启动连续运动
        CString str;
        str.Format(L"轴%d", axis1);
        GroupBoxAxis0.SetWindowTextW(str);
    }
    UpdateData(false);
}

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

/// <summary>
/// 比较器清零按钮点击
/// </summary>
void SoftCompare1D::OnBnClickedButton5()
{
    // TODO: 在此添加控件通知处理程序代码
    WORD Chan = cmpChan.GetCurSel(); //比较通道
    MC_ClearSoftCmp(CCompare1DDlg::cardConnectNo, Compare_1D, Chan); //比较器清零

}

/// <summary>
/// 位置清除按钮点击
/// </summary>
void SoftCompare1D::OnBnClickedButton1()
{
    // TODO: 在此添加控件通知处理程序代码
    WORD Axis = cmpAxis.GetCurSel(); //比较轴号
    MC_SetAxisCmdPos(CCompare1DDlg::cardConnectNo, Axis, 0);

    if (triggerMode.GetCurSel() == 4)
    {
        WORD axis1 = triggerPrm.GetCurSel();
        MC_SetAxisCmdPos(CCompare1DDlg::cardConnectNo, axis1, 0); //位置清零
    }
}

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

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

/// <summary>
/// output1点击
/// </summary>
void SoftCompare1D::OnBnClickedButton3()
{
    // TODO: 在此添加控件通知处理程序代码
    HICON redIcon = AfxGetApp()->LoadIcon(IDI_ICONRED);
    if (((CStatic*)GetDlgItem(IDC_STATIC_OUTPUT1))->GetIcon() == redIcon)
    {
        MC_SetOutIoBit(CCompare1DDlg::cardConnectNo, 1, 0);
    }
    else
    {
        MC_SetOutIoBit(CCompare1DDlg::cardConnectNo, 1, 1);
    }
}


void SoftCompare1D::OnCbnSelchangeCombo5()
{
    // TODO: 在此添加控件通知处理程序代码
    if (triggerMode.GetCurSel() == 4)
    {
        triggerPrm.ResetContent();
        for (int _axisNo = 0; _axisNo < 12; _axisNo++)
        {
            CString str;
            str.Format(_T("轴%d"), _axisNo);
            triggerPrm.InsertString(_axisNo, str);
        }
        triggerPrm.SetCurSel(0);
    }
    else
    {
        triggerPrm.ResetContent();
        for (int _outputNo = 0; _outputNo < 2; _outputNo++)
        {
            CString outputStr;
            outputStr.Format(_T("Output%d"), _outputNo);
            triggerPrm.InsertString(_outputNo, outputStr);
        }
        triggerPrm.SetCurSel(0); //触发参数

    }
}
