// INIT_Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "Demo.h"
#include "INIT_Dlg.h"
#include "WinConSDK.h"

#include "ValExt.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// INIT_Dlg dialog


INIT_Dlg::INIT_Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(INIT_Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(INIT_Dlg)
	m_MaxV = 0;
	m_Soft_F = 0;
	m_Soft_R = 0;
	m_VRING_ENABLE = 0;
	m_Msg = _T("");
	//}}AFX_DATA_INIT
	//======================================

}


void INIT_Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(INIT_Dlg)
	DDX_Control(pDX, IDC_COMBO_FTimes, m_FTimes);
	DDX_Control(pDX, IDC_COMBO_Filter_Code, m_Filter_Code);
	DDX_Control(pDX, IDC_COMBO_SET_INPOS, m_INPOS);
	DDX_Control(pDX, IDC_COMBO_SET_ALARM, m_ALARM);
	DDX_Control(pDX, IDC_COMBO_En_MODE, m_En_MODE);
	DDX_Control(pDX, IDC_COMBO_Devision, m_Devision);
	DDX_Control(pDX, IDC_COMBO_SMode, m_StopMode);
	DDX_Control(pDX, IDC_COMBO_LIMITSTOP_MODE, m_LStop_Mode);
	DDX_Control(pDX, IDC_COMBO_Set_P_Mode, m_Set_P_Mode);
	DDX_Control(pDX, IDC_COMBO_Level, m_Level);
	DDX_Control(pDX, IDC_COMBO_Axis, m_Axis);
	DDX_Control(pDX, IDC_COMBO_Servo, m_Servo);
	DDX_Text(pDX, IDC_EDIT_SET_Max_V, m_MaxV);
	DDX_Text(pDX, IDC_EDIT_SOFT_LMT_F, m_Soft_F);
	DDX_Text(pDX, IDC_EDIT_SOFT_LMT_R, m_Soft_R);
	DDX_Text(pDX, IDC_EDIT_VRING_ENABLE, m_VRING_ENABLE);
	DDX_Text(pDX, IDC_Msg, m_Msg);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(INIT_Dlg, CDialog)
	//{{AFX_MSG_MAP(INIT_Dlg)
	ON_BN_CLICKED(IDC_Reg_Card, OnRegCard)
	ON_BN_CLICKED(IDC_BUT_Servo_ON, OnBUTServoON)
	ON_BN_CLICKED(IDC_BUT_SetData, OnBUTSetData)
	ON_BN_CLICKED(IDC_BUT_PULSE_MODE, OnButPulseMode)
	ON_BN_CLICKED(IDC_BUT_Set_Max_V, OnBUTSetMaxV)
	ON_BN_CLICKED(IDC_BUT_SET_HLMT, OnButSetHlmt)
	ON_BN_CLICKED(IDC_BUT_LIMITSTOP_MODE, OnButLimitstopMode)
	ON_BN_CLICKED(IDC_BUT_SET_NHOME, OnButSetNhome)
	ON_BN_CLICKED(IDC_BUT_SET_HOME_EDGE, OnButSetHomeEdge)
	ON_BN_CLICKED(IDC_BUT_SET_SOFT_LMT, OnButSetSoftLmt)
	ON_BN_CLICKED(IDC_BUT_SET_ENCODER, OnButSetEncoder)
	ON_BN_CLICKED(IDC_BUT_SET_ALARM, OnButSetAlarm)
	ON_BN_CLICKED(IDC_BUT_SET_INPOS, OnButSetInpos)
	ON_BN_CLICKED(IDC_BUT_SET_FILTER, OnButSetFilter)
	ON_BN_CLICKED(IDC_BUT_VRING_ENABLE, OnButVringEnable)
	ON_BN_CLICKED(IDC_BUT_VRING_DISABLE, OnButVringDisable)
	ON_CBN_SELCHANGE(IDC_COMBO_Axis, OnSelchangeCOMBOAxis)
	ON_BN_CLICKED(IDC_BUT_Clear, OnBUTClear)
	ON_BN_CLICKED(IDC_Get_Error, OnGetError)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// INIT_Dlg message handlers

void INIT_Dlg::OnRegCard() 
{
	// TODO: Add your control notification handler code here

	
}

void INIT_Dlg::OnBUTSetData() 
{
	// TODO: Add your control notification handler code here
	//==== For control ==========
	m_Axis.SetCurSel(2);
	Select_Axis=3;
	m_Servo.SetCurSel(1);
	m_Set_P_Mode.SetCurSel(2);
	m_Level.SetCurSel(0);
	m_LStop_Mode.SetCurSel(0);
	m_StopMode.SetCurSel(0);
	m_En_MODE.SetCurSel(0);
	m_Devision.SetCurSel(0);
	m_ALARM.SetCurSel(0);
	m_INPOS.SetCurSel(0);
	m_Filter_Code.SetCurSel(0);
	m_FTimes.SetCurSel(2);
	//==== For Val ==============
	m_MaxV=16000;
	m_Soft_F=400000;
	m_Soft_R=-200000;
	m_VRING_ENABLE=9999;
	UpdateData(false);
}


void INIT_Dlg::OnButPulseMode() 
{
	// TODO: Add your control notification handler code here
    I8092MF.SET_PULSE_MODE(cardNo,Select_Axis,m_Set_P_Mode.GetCurSel());
}

void INIT_Dlg::OnBUTSetMaxV() 
{
	// TODO: Add your control notification handler code here
    UpdateData(true);
	I8092MF.SET_MAX_V(cardNo,Select_Axis,m_MaxV);
}

void INIT_Dlg::OnBUTServoON() 
{
	// TODO: Add your control notification handler code here
	if (m_Servo.GetCurSel() == OFF)
       I8092MF.SERVO_OFF(cardNo,Select_Axis);
	else
	   I8092MF.SERVO_ON(cardNo,Select_Axis);
}

void INIT_Dlg::OnButSetHlmt() 
{
	// TODO: Add your control notification handler code here
	I8092MF.SET_HLMT(cardNo,Select_Axis,m_Level.GetCurSel(),m_Level.GetCurSel());
}

void INIT_Dlg::OnButLimitstopMode() 
{
	// TODO: Add your control notification handler code here
	I8092MF.LIMITSTOP_MODE(cardNo,Select_Axis,m_LStop_Mode.GetCurSel());
}

void INIT_Dlg::OnButSetNhome() 
{
	// TODO: Add your control notification handler code here
	I8092MF.SET_NHOME(cardNo,Select_Axis,m_Level.GetCurSel());
}

void INIT_Dlg::OnButSetHomeEdge() 
{
	// TODO: Add your control notification handler code here
	I8092MF.SET_HOME_EDGE(cardNo,Select_Axis,m_Level.GetCurSel());
}

void INIT_Dlg::OnButSetSoftLmt() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	I8092MF.SET_SLMT(cardNo,Select_Axis,m_Soft_F,m_Soft_R,m_StopMode.GetCurSel());
}
void INIT_Dlg::OnBUTClear() 
{
	// TODO: Add your control notification handler code here
	I8092MF.CLEAR_SLMT(cardNo,Select_Axis);
}



void INIT_Dlg::OnButSetEncoder() 
{
	// TODO: Add your control notification handler code here
	I8092MF.SET_ENCODER(cardNo,Select_Axis,m_En_MODE.GetCurSel(),m_Devision.GetCurSel(),m_Level.GetCurSel());
}

void INIT_Dlg::OnButSetAlarm() 
{
	// TODO: Add your control notification handler code here
	I8092MF.SET_ALARM(cardNo,Select_Axis,m_ALARM.GetCurSel(),m_Level.GetCurSel());
}

void INIT_Dlg::OnButSetInpos() 
{
	// TODO: Add your control notification handler code here
	I8092MF.SET_INPOS(cardNo,Select_Axis,m_INPOS.GetCurSel(),m_Level.GetCurSel());
}

void INIT_Dlg::OnButSetFilter() 
{
	// TODO: Add your control notification handler code here
	I8092MF.SET_FILTER(cardNo,Select_Axis,m_Filter_Code.GetCurSel(),m_FTimes.GetCurSel()+1);
}

void INIT_Dlg::OnButVringEnable() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	I8092MF.VRING_ENABLE(cardNo,Select_Axis,m_VRING_ENABLE);
}

void INIT_Dlg::OnButVringDisable() 
{
	// TODO: Add your control notification handler code here
	I8092MF.VRING_DISABLE(cardNo,Select_Axis);
}


void INIT_Dlg::OnSelchangeCOMBOAxis() 
{
	// TODO: Add your control notification handler code here
	Select_Axis=m_Axis.GetCurSel()+1;
}



BOOL INIT_Dlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	OnBUTSetData() ;
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}


void INIT_Dlg::OnGetError() 
{
	// TODO: Add your control notification handler code here
	WORD KK=0;
	KK=i8092MF_GET_ERROR(cardNo);
	CString MSGG;
	if (KK == YES)
	{
	char buffer[20];
	KK=i8092MF_GET_ERROR_CODE(cardNo, AXIS_X);
    _itoa( KK, buffer, 10 );
	if (KK==0)
		MSGG="Xerr: Soft_Stop ";
	else
		MSGG="Xerr:" + CString(buffer) + "  ";
	KK=i8092MF_GET_ERROR_CODE(cardNo, AXIS_Y);
	_itoa( KK, buffer, 10 );
	if (KK==0)
		MSGG=MSGG + "Yerr: Soft_Stop ";
	else
		MSGG=MSGG + "Yerr:" + CString(buffer) + "  ";
	}
	else
		MSGG="No Error";
    m_Msg=MSGG;
	UpdateData(false);
}
