// MultiaxisMotionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MultiaxisMotion.h"
#include "MultiaxisMotionDlg.h"
#include "dlgGph1XY.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMultiaxisMotionDlg dialog

U8 bSlotNo = 0;

dlgGph1XY *ptrdlgGph1XY;


#define _CHKAPI(RetCode, FunName, AxisNo) \
	ret = RetCode; \
	if( ret != I8196_SUCCESS ) \
	{ \
		TRACE(L"%s(0x%02x) return: %d\n", FunName, AxisNo, ret);\
		m_stcFunErr.SetBitmap(m_hBmpErr); \
	}

CMultiaxisMotionDlg::CMultiaxisMotionDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMultiaxisMotionDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CMultiaxisMotionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_cmbSlotNo, m_cmbSlotNo);
	DDX_Control(pDX, IDC_stcFunErr, m_stcFunErr);
	DDX_Control(pDX, IDC_cmbARCAxisX, m_cmbARCAxisX);
	DDX_Control(pDX, IDC_cmbARCAxisY, m_cmbARCAxisY);
	DDX_Control(pDX, IDC_cmbHelicalAxis0, m_cmbHelicalAxis0);
	DDX_Control(pDX, IDC_cmbHelicalAxis1, m_cmbHelicalAxis1);
	DDX_Control(pDX, IDC_cmbHelicalAxis2, m_cmbHelicalAxis2);
	DDX_Control(pDX, IDC_CHECK1, m_chkEnableConti);
}

BEGIN_MESSAGE_MAP(CMultiaxisMotionDlg, CDialog)
#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
	ON_WM_SIZE()
#endif
	//}}AFX_MSG_MAP
	ON_CBN_SELCHANGE(IDC_cmbSlotNo, &CMultiaxisMotionDlg::OnCbnSelchangecmbslotno)
	ON_WM_TIMER()
	ON_STN_CLICKED(IDC_stcFunErr, &CMultiaxisMotionDlg::OnStnClickedstcfunerr)
	ON_CONTROL_RANGE(BN_CLICKED, IDC_btnReset0, IDC_btnReset5, OnbtnReset)
	ON_CONTROL_RANGE(BN_CLICKED, IDC_btnSvon0, IDC_btnSvon5, OnbtnSvon)
	ON_CONTROL_RANGE(BN_CLICKED, IDC_btnRALM0, IDC_btnRALM5, OnbtnRALM)
	ON_BN_CLICKED(IDC_btnResetAll, &CMultiaxisMotionDlg::OnBnClickedbtnresetall)
	ON_BN_CLICKED(IDC_btnLineMove, &CMultiaxisMotionDlg::OnBnClickedbtnlinemove)
	ON_BN_CLICKED(IDC_btnLineStop, &CMultiaxisMotionDlg::OnBnClickedbtnlinestop)
	ON_BN_CLICKED(IDC_btnArcMove, &CMultiaxisMotionDlg::OnBnClickedbtnarcmove)
	ON_BN_CLICKED(IDC_btnArcStop, &CMultiaxisMotionDlg::OnBnClickedbtnarcstop)
	ON_BN_CLICKED(IDC_btnHelicalMove, &CMultiaxisMotionDlg::OnBnClickedbtnhelicalmove)
	ON_BN_CLICKED(IDC_btnHelicalStop, &CMultiaxisMotionDlg::OnBnClickedbtnhelicalstop)
	ON_BN_CLICKED(IDC_CHECK1, &CMultiaxisMotionDlg::OnBnClickedCheck1)
	ON_BN_CLICKED(IDC_btnContiStart, &CMultiaxisMotionDlg::OnBnClickedbtncontistart)
	ON_BN_CLICKED(IDC_btnContiStop, &CMultiaxisMotionDlg::OnBnClickedbtncontistop)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_btnGraph, &CMultiaxisMotionDlg::OnBnClickedbtngraph)
	ON_BN_CLICKED(IDC_btnGraph2, &CMultiaxisMotionDlg::OnBnClickedbtngraph2)
END_MESSAGE_MAP()


// CMultiaxisMotionDlg message handlers

BOOL CMultiaxisMotionDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here

	int i = 0;

	eRET ret = I8196_SUCCESS;
	CString str;

	BOOL flgExistCard = FALSE;	


	m_hBmpErr = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_bmpRed));
	m_hBmpOff = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_bmpBlack));

	m_hBmpConti = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_bmpConti));;
	m_hBmpHelical = LoadBitmap(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDB_bmpHelical));;


	ptrdlgGph1XY = new dlgGph1XY(this);
	ptrdlgGph1XY->Create(dlgGph1XY::IDD);
	ptrdlgGph1XY->ShowWindow(SW_HIDE);

	for (i = 0; i < MAX_AXIS_QTY; i++)
	{
		SetDlgItemInt(IDC_edtDist_0+i, 10000);
	}

	SetDlgItemInt(IDC_edtLineSpeed, 10000);
	SetDlgItemInt(IDC_edtLineAcc, 20000);

	SetDlgItemInt(IDC_edtArcSpeed, 10000);

	SetDlgItemInt(IDC_edtCenterPointX, 10000);
	SetDlgItemInt(IDC_edtCenterPointY, 0);

	SetDlgItemInt(IDC_edtFinishPointX, 20000);
	SetDlgItemInt(IDC_edtFinishPointY, 0);

	((CButton *)GetDlgItem(IDC_rdoR))->SetCheck(TRUE);

	((CButton *)GetDlgItem(IDC_rdoArcDirP))->SetCheck(TRUE);

	m_cmbARCAxisX.SetCurSel(0);
	m_cmbARCAxisY.SetCurSel(1);

	m_cmbHelicalAxis0.SetCurSel(0);
	m_cmbHelicalAxis1.SetCurSel(1);
	m_cmbHelicalAxis2.SetCurSel(2);

	SetDlgItemInt(IDC_edtHelicalSpeed, 1000);
	SetDlgItemInt(IDC_edtHelicalCP0, 1000);
	SetDlgItemInt(IDC_edtHelicalCP1, 1000);
	SetDlgItemInt(IDC_edtHelicalPitch, 1000);
	SetDlgItemInt(IDC_edtHelicalAngle, 720);

	SetDlgItemInt(IDC_edtContiSpeed, 10000);
	SetDlgItemInt(IDC_edtContiTimes, 10);

	m_cmbSlotNo.ResetContent();
	for (i = 0; i < 9; i++) // MAX_SLOT_QTY = 9
	{
		ret = i8196f_check_card_in_slot(i);
		if (ret == I8196_SUCCESS)
		{
			str.Format(L"%d", i);
			m_cmbSlotNo.AddString(str);
			flgExistCard = TRUE;

			InitialI8196F(i);
		}
	}

	if (flgExistCard == FALSE)
	{
		m_cmbSlotNo.AddString(L"NULL");
		m_cmbSlotNo.SetCurSel(0);

		MessageBox(L"No I-8196F!", L"ERROR", MB_OK);
	}
	else
	{
		m_cmbSlotNo.SetCurSel(0);		
		m_cmbSlotNo.GetLBText(0, str);
		swscanf_s(str.GetBuffer(), L"%d", &bSlotNo);

		SetTimer( 1, 10, NULL );
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

#if defined(_DEVICE_RESOLUTION_AWARE) && !defined(WIN32_PLATFORM_WFSP)
void CMultiaxisMotionDlg::OnSize(UINT /*nType*/, int /*cx*/, int /*cy*/)
{
	if (AfxIsDRAEnabled())
	{
		DRA::RelayoutDialog(
			AfxGetResourceHandle(), 
			this->m_hWnd, 
			DRA::GetDisplayMode() != DRA::Portrait ? 
			MAKEINTRESOURCE(IDD_MULTIAXISMOTION_DIALOG_WIDE) : 
			MAKEINTRESOURCE(IDD_MULTIAXISMOTION_DIALOG));
	}
}
#endif

void CMultiaxisMotionDlg::InitialI8196F(U8 SlotNo)
{
	eRET ret;

	int i = 0;

	U8 AxisNo;

	_CHKAPI(i8196f_registration(SlotNo),
		L"i8196f_registration", 0);

	_CHKAPI(i8196f_load_power_on_cfg(SlotNo, DEFAULT_POWER_ON_SETTING),
		L"i8196f_load_power_on_cfg",0);

	for (i = 0; i < MAX_AXIS_QTY; i++)
	{
		AxisNo = 0x01<<i;

		_CHKAPI(i8196f_set_pls_cfg( SlotNo, (eSINGLE_AXIS)AxisNo, PULSE_MODE_CW_CCW, PULSE_LOGIC_ACTIVE_HIGH, PULSE_FORWARD_ACTIVE_HIGH), 
			L"i8196f_set_pls_cfg", AxisNo);

		_CHKAPI(i8196f_set_enc_cfg( SlotNo, (eSINGLE_AXIS)AxisNo, ENCODER_MODE_CW_CCW),
			L"i8196f_set_enc_cfg", AxisNo);

		_CHKAPI(i8196f_set_limit( SlotNo, (eSINGLE_AXIS)AxisNo, LOGIC_ACTIVE_LOW, STOP_SUDDEN ),
			L"i8196f_set_limit", AxisNo);

		_CHKAPI(i8196f_set_alarm( SlotNo, (eSINGLE_AXIS)AxisNo, DISABLE, LOGIC_ACTIVE_LOW ),
			L"i8196f_set_alarm", AxisNo);

		_CHKAPI(i8196f_set_inp( SlotNo, (eSINGLE_AXIS)AxisNo, DISABLE, LOGIC_ACTIVE_LOW ),
			L"i8196f_set_inp", AxisNo);

		_CHKAPI(i8196f_set_ready( SlotNo, (eSINGLE_AXIS)AxisNo, DISABLE, LOGIC_ACTIVE_LOW ),
			L"i8196f_set_ready", AxisNo);
	}
}
void CMultiaxisMotionDlg::OnCbnSelchangecmbslotno()
{
	CString str;
	int Index = m_cmbSlotNo.GetCurSel();

	m_cmbSlotNo.GetLBText(Index, str);
	swscanf_s(str.GetBuffer(), L"%d", &bSlotNo);
}

void CMultiaxisMotionDlg::OnTimer(UINT_PTR nIDEvent)
{
	eRET ret = I8196_SUCCESS;
	int i = 0;
	CString str;
	U8 AxisNo = 0;
	U16 wDIStatus[MAX_AXIS_QTY] = {0};
	I32 LP[MAX_AXIS_QTY] = {0}, EP[MAX_AXIS_QTY] = {0}, Spd[MAX_AXIS_QTY] = {0};
	U8 bDone = 0;
	U16 wStopStatus = 0;
	eCHANNEL_STATE eOutSts;

	for (i = 0; i < MAX_AXIS_QTY; i++)
	{

		AxisNo = 0x01<<i;

		_CHKAPI(i8196f_get_cmdcounter( bSlotNo, (eSINGLE_AXIS)AxisNo, &LP[i]),
			L"i8196f_get_cmdcounter", AxisNo);
		SetDlgItemInt(IDC_edtGetCMD0+i, LP[i]);

		_CHKAPI(i8196f_get_enccounter( bSlotNo, (eSINGLE_AXIS)AxisNo, &EP[i]),
			L"i8196f_get_enccounter", AxisNo);
		SetDlgItemInt(IDC_edtGetENC0+i, EP[i]);

		_CHKAPI(i8196f_get_speed(bSlotNo, (eSINGLE_AXIS)AxisNo, &Spd[i]),
			L"i8196f_get_speed", AxisNo);
		SetDlgItemInt(IDC_edtGetSpd0+i, Spd[i]);

		_CHKAPI(i8196f_get_motion_done(bSlotNo, (eSINGLE_AXIS)AxisNo, &bDone, &wStopStatus),
			L"i8196f_get_motion_done", AxisNo);
		str.Format(_T("(%d)0x%04x"),bDone,wStopStatus);
		SetDlgItemText(IDC_edtGetMCsts0+i,str);

		_CHKAPI(i8196f_get_mdi_status(bSlotNo, (eSINGLE_AXIS)AxisNo, &wDIStatus[i]),
			L"i8196f_get_mdi_status", AxisNo);
		//str.Format(_T("0x%04x"),wDIStatus[i]);
		//SetDlgItemText(IDC_edtGetIOSts0+i,str);

		_CHKAPI(i8196f_get_servo_on_status(bSlotNo, (eSINGLE_AXIS)AxisNo, &eOutSts),
			L"i8196f_get_out1_status", AxisNo);
		if (eOutSts == ON)
		{wDIStatus[i] |= 0x2000;}
		else
		{wDIStatus[i] &= (~0x2000);}
		_CHKAPI(i8196f_get_servo_erc_status(bSlotNo, (eSINGLE_AXIS)AxisNo, &eOutSts),
			L"i8196f_get_out1_status", AxisNo);
		if (eOutSts == ON)
		{wDIStatus[i] |= 0x4000;}
		else
		{wDIStatus[i] &= (~0x4000);}
		_CHKAPI(i8196f_get_servo_almrst_status(bSlotNo, (eSINGLE_AXIS)AxisNo, &eOutSts),
			L"i8196f_get_out1_status", AxisNo);
		if (eOutSts == ON)
		{wDIStatus[i] |= 0x8000;}
		else
		{wDIStatus[i] &= (~0x8000);}

		str.Format(_T("0x%04x"),wDIStatus[i]);
		SetDlgItemText(IDC_edtGetIOSts0+i,str);
	}

	CDialog::OnTimer(nIDEvent);
}

void CMultiaxisMotionDlg::OnStnClickedstcfunerr()
{
	m_stcFunErr.SetBitmap(m_hBmpOff);
	//m_stcFunErr.SetBitmap(m_hBmpErr);

	TRACE(L"\n Reset Fun. Error\n");
}

void CMultiaxisMotionDlg::OnbtnReset(UINT nID)
{
	U8 AxisNo = 0x01 << (nID-IDC_btnReset0);

	ResetCnt(AxisNo);
}

void CMultiaxisMotionDlg::OnBnClickedbtnresetall()
{
	int i = 0;
	U8 AxisNo;

	for (i = 0; i < MAX_AXIS_QTY; i++)
	{
		AxisNo = 0x01 << i;
		ResetCnt(AxisNo);
	}
}

void CMultiaxisMotionDlg::ResetCnt(U8 AxisNo)
{
	I32 LogicPos = 0;
	I32 EncPos = 0;
	eRET ret = I8196_SUCCESS;

	_CHKAPI(i8196f_set_cmdcounter(bSlotNo, (eSINGLE_AXIS)AxisNo, LogicPos),
		L"i8196f_set_cmdcounter", AxisNo);
	_CHKAPI(i8196f_set_enccounter(bSlotNo, (eSINGLE_AXIS)AxisNo, EncPos),
		L"i8196f_set_enccounter", AxisNo);
}

void CMultiaxisMotionDlg::OnbtnSvon(UINT nID)
{
	U8 AxisNo = 0x01 << (nID - IDC_btnSvon0);
	eRET ret = I8196_SUCCESS;
	eCHANNEL_STATE OnOff;

	_CHKAPI(i8196f_get_servo_on_status(bSlotNo, (eSINGLE_AXIS)AxisNo, &OnOff ),
		L"i8196f_get_servo_on_status", AxisNo);

	if (OnOff == ON)
	{_CHKAPI(i8196f_set_servo_on( bSlotNo, (eSINGLE_AXIS)AxisNo, OFF ),
	L"i8196f_set_servo_on", AxisNo);}
	else
	{_CHKAPI(i8196f_set_servo_on( bSlotNo, (eSINGLE_AXIS)AxisNo, ON ),
	L"i8196f_set_servo_on", AxisNo);}

}

void CMultiaxisMotionDlg::OnbtnRALM(UINT nID)
{
	U8 AxisNo = 0x01 << (nID - IDC_btnRALM0);
	eRET ret = I8196_SUCCESS;
	eCHANNEL_STATE OnOff;

	_CHKAPI(i8196f_get_servo_almrst_status(bSlotNo, (eSINGLE_AXIS)AxisNo, &OnOff ),
		L"i8196f_get_servo_almrst_status", AxisNo);

	if (OnOff == ON)
	{_CHKAPI(i8196f_set_alarm_reset( bSlotNo, (eSINGLE_AXIS)AxisNo, OFF ),
	L"i8196f_set_alarm_reset", AxisNo);}
	else
	{_CHKAPI(i8196f_set_alarm_reset( bSlotNo, (eSINGLE_AXIS)AxisNo, ON ),
	L"i8196f_set_alarm_reset", AxisNo);}
}


void CMultiaxisMotionDlg::OnBnClickedbtnlinemove()
{
	eRET ret;
	int i = 0;
	BYTE ArrAxis[MAX_AXIS_QTY] = {0};
	int AxisCnt = 0;
	I32 ArrDist[MAX_AXIS_QTY] = {0};
	int Speed = GetDlgItemInt(IDC_edtLineSpeed);
	int Acc = GetDlgItemInt(IDC_edtLineAcc);

	eINTERPOLATION_MODE InterpMode = GENERAL_INTERP_CONFIG;
	

	I32 FixedPulse[6] = {0};

	U8 bitAxis = 0;

	for (i = 0; i < MAX_AXIS_QTY; i++)
	{
		if (((CButton *)GetDlgItem(IDC_chkAxis_0 + i))->GetCheck())
		{
			ArrAxis[AxisCnt] = 0x0001 << i;
			ArrDist[AxisCnt] = GetDlgItemInt(IDC_edtDist_0 + i);	
			FixedPulse[i] = GetDlgItemInt(IDC_edtDist_0 + i);	
			bitAxis |= ArrAxis[AxisCnt];
			AxisCnt++;
		}
	}

	if (((CButton *)GetDlgItem(IDC_rdoR))->GetCheck())
	{
		switch(AxisCnt)
		{
		case 0:
			break;
		case 1:
			_CHKAPI(i8196f_t_move( bSlotNo, (eSINGLE_AXIS)ArrAxis[0], (U32)(Speed/10), (U32)Speed, (U32)(Speed/10),
				(U32)Acc, (U32)Acc, (I32)ArrDist[0]),
				L"i8196f_t_move", bitAxis);
			break;
		case 2:
			_CHKAPI(i8196f_t_line2_move( bSlotNo, (eSINGLE_AXIS)ArrAxis[0], (eSINGLE_AXIS)ArrAxis[1],	
				(U32)(Speed/10), (U32)Speed, (U32)(Speed/10), (U32)Acc, (U32)Acc,		
				(I32)ArrDist[0], (I32)ArrDist[1], InterpMode),
				L"i8196f_t_line2_move", bitAxis);
			break;
		case 3:
			_CHKAPI(i8196f_t_line3_move( bSlotNo, (eSINGLE_AXIS)ArrAxis[0], (eSINGLE_AXIS)ArrAxis[1], (eSINGLE_AXIS)ArrAxis[2],
				(U32)(Speed/10), (U32)Speed, (U32)(Speed/10), (U32)Acc, (U32)Acc,		
				(I32)ArrDist[0], (I32)ArrDist[1], (I32)ArrDist[2], InterpMode ),
				L"i8196f_t_line3_move", bitAxis);
			break;
		case 4:
		case 5:
		case 6:
			_CHKAPI(i8196f_lines_move(  bSlotNo, bitAxis, I8196F_ACC_DEC_T_CURVE,			
				(U32)(Speed/10), (U32)Speed, (U32)(Speed/10), (U32)Acc, (U32)Acc,		        
				FixedPulse, InterpMode ),
				L"i8196f_lines_move", bitAxis);
			break;
		default:
			break;
		}

	}
	else
	{
		switch(AxisCnt)
		{
		case 0:
			break;
		case 1:
			_CHKAPI(i8196f_abs_t_move( bSlotNo, (eSINGLE_AXIS)ArrAxis[0], (U32)(Speed/10), (U32)Speed, (U32)(Speed/10),
				(U32)Acc, (U32)Acc, (I32)ArrDist[0] ),
				L"i8196f_abs_t_move", bitAxis);
			break;
		case 2:
			_CHKAPI(i8196f_abs_t_line2_move( bSlotNo, (eSINGLE_AXIS)ArrAxis[0], (eSINGLE_AXIS)ArrAxis[1],	
				(U32)(Speed/10), (U32)Speed, (U32)(Speed/10), (U32)Acc, (U32)Acc,		
				(I32)ArrDist[0], (I32)ArrDist[1], InterpMode ),
				L"i8196f_abs_t_line2_move", bitAxis);
			break;
		case 3:
			_CHKAPI(i8196f_abs_t_line3_move( bSlotNo, (eSINGLE_AXIS)ArrAxis[0], (eSINGLE_AXIS)ArrAxis[1], (eSINGLE_AXIS)ArrAxis[2],
				(U32)(Speed/10), (U32)Speed, (U32)(Speed/10), (U32)Acc, (U32)Acc,		
				(I32)ArrDist[0], (I32)ArrDist[1], (I32)ArrDist[2], InterpMode ),
				L"i8196f_abs_t_line3_move", bitAxis);
			break;
		case 4:
		case 5:
		case 6:
			_CHKAPI(i8196f_abs_lines_move( bSlotNo, bitAxis, I8196F_ACC_DEC_T_CURVE,			
				(U32)(Speed/10), (U32)Speed, (U32)(Speed/10), (U32)Acc, (U32)Acc,		        
				FixedPulse, InterpMode ),
				L"i8196f_abs_lines_move", bitAxis);
			break;
		default:
			break;
		}

	}
}

void CMultiaxisMotionDlg::OnBnClickedbtnlinestop()
{
	eRET ret;
	int i = 0;
	U8 bitAixs = 0;

	for (i = 0; i < MAX_AXIS_QTY; i++)
	{
		if (((CButton *)GetDlgItem(IDC_chkAxis_0 + i))->GetCheck())
		{
			bitAixs = 0x0001 << i;
			_CHKAPI(i8196f_stop_move( bSlotNo, (eSINGLE_AXIS)bitAixs, STOP_SLOWDOWN ),
					L"i8196f_stop_move", bitAixs);
		}
	}
}

void CMultiaxisMotionDlg::OnBnClickedbtnarcmove()
{
	eRET ret;
	U8 AxisX = 0x0001 << m_cmbARCAxisX.GetCurSel();
	U8 AxisY = 0x0001 << m_cmbARCAxisY.GetCurSel();

	int Speed = GetDlgItemInt(IDC_edtArcSpeed);
	int Acc = Speed*2;
	I32 MainAxisCenterPoint = GetDlgItemInt(IDC_edtCenterPointX);	
	I32 SlaveAxisCenterPoint = GetDlgItemInt(IDC_edtCenterPointY); 
	I32 MainAxisFinishPoint = GetDlgItemInt(IDC_edtFinishPointX);
	I32 SlaveAxisFinishPoint = GetDlgItemInt(IDC_edtFinishPointY);

	eINTERPOLATION_MODE InterpMode = GENERAL_INTERP_CONFIG;

	eARC_DIR ArcDirection;

	U8 bitAixs = AxisX | AxisY;

	if (((CButton *)GetDlgItem(IDC_rdoArcDirN))->GetCheck())
	{ArcDirection = ARC_DIR_CCW;}
	else
	{ArcDirection = ARC_DIR_CW;}

	if (((CButton *)GetDlgItem(IDC_rdoR))->GetCheck())
	{
		_CHKAPI(i8196f_t_arc2_move( bSlotNo, (eSINGLE_AXIS)AxisX, (eSINGLE_AXIS)AxisY,			
			(U32)(Speed/10), (U32)Speed, (U32)(Speed/10), (U32)Acc, (U32)Acc, ArcDirection,			    
			MainAxisCenterPoint, SlaveAxisCenterPoint, MainAxisFinishPoint, SlaveAxisFinishPoint,	        
			InterpMode),
			L"i8196f_t_arc2_move", bitAixs);
	}
	else
	{
		_CHKAPI(i8196f_abs_t_arc2_move( bSlotNo, (eSINGLE_AXIS)AxisX, (eSINGLE_AXIS)AxisY,			
			(U32)(Speed/10), (U32)Speed, (U32)(Speed/10), (U32)Acc, (U32)Acc, ArcDirection,			    
			MainAxisCenterPoint, SlaveAxisCenterPoint, MainAxisFinishPoint, SlaveAxisFinishPoint,	        
			InterpMode),
			L"i8196f_abs_t_arc2_move", bitAixs);
	}
}

void CMultiaxisMotionDlg::OnBnClickedbtnarcstop()
{
	eRET ret;
	U8 AxisX = 0x0001 << m_cmbARCAxisX.GetCurSel();
	U8 AxisY = 0x0001 << m_cmbARCAxisY.GetCurSel();

	_CHKAPI(i8196f_stop_move( bSlotNo, (eSINGLE_AXIS)AxisX, STOP_SLOWDOWN ),
		L"i8196f_stop_move", AxisX);
	_CHKAPI(i8196f_stop_move( bSlotNo, (eSINGLE_AXIS)AxisY, STOP_SLOWDOWN ),
		L"i8196f_stop_move", AxisY);

}

void CMultiaxisMotionDlg::OnBnClickedbtnhelicalmove()
{
	eRET ret;

	U8 AxisX = 0x01 << m_cmbHelicalAxis0.GetCurSel();
	U8 AxisY = 0x01 << m_cmbHelicalAxis1.GetCurSel();
	U8 AxisZ = 0x01 << m_cmbHelicalAxis2.GetCurSel();

	U8 bitAxis = AxisX|AxisY|AxisZ;

	int Speed = GetDlgItemInt(IDC_edtHelicalSpeed);

	I32 CenterPointX = GetDlgItemInt(IDC_edtHelicalCP0);
	I32 CenterPointY = GetDlgItemInt(IDC_edtHelicalCP1);
	I32 CenterPointZ = 0;
	I32 lNormalDir0, lNormalDir1, lNormalDir2;
	int Angle = GetDlgItemInt(IDC_edtHelicalAngle);
	int Pitch = GetDlgItemInt(IDC_edtHelicalPitch);

	U16 InterpMode = GENERAL_INTERP_CONFIG;

	if (((CButton *)GetDlgItem(IDC_rdoR))->GetCheck())
	{
		CenterPointZ = 0;
		lNormalDir0 = 0;
		lNormalDir1 = 0;
		lNormalDir2 = 1;
		_CHKAPI(i8196f_t_helical_move( bSlotNo, (eSINGLE_AXIS)AxisX, (eSINGLE_AXIS)AxisY, (eSINGLE_AXIS)AxisZ, (U32)Speed/10, (U32)Speed, (U32)Speed/10, (U32)Speed*2, (U32)Speed*2,
			CenterPointX, CenterPointY, CenterPointZ, lNormalDir0, lNormalDir1, lNormalDir2, (F32)Angle, (F32)Pitch, GENERAL_INTERP_CONFIG), 
			L"i8196f_t_helical_move", bitAxis);
	}
	else
	{
		_CHKAPI(i8196f_get_cmdcounter( bSlotNo, (eSINGLE_AXIS)AxisZ, &CenterPointZ),
			L"i8196f_get_cmdcounter", AxisZ);

		lNormalDir0 = 0;
		lNormalDir1 = 0;
		lNormalDir2 = 1;
		_CHKAPI(i8196f_abs_t_helical_move( bSlotNo, (eSINGLE_AXIS)AxisX, (eSINGLE_AXIS)AxisY, (eSINGLE_AXIS)AxisZ, (U32)Speed/10, (U32)Speed, (U32)Speed/10, (U32)Speed*2, (U32)Speed*2,
			CenterPointX, CenterPointY, CenterPointZ, lNormalDir0, lNormalDir1, lNormalDir2, (F32)Angle, (F32)Pitch, GENERAL_INTERP_CONFIG), 
			L"i8196f_abs_t_helical_move", bitAxis);
	}
}

void CMultiaxisMotionDlg::OnBnClickedbtnhelicalstop()
{
	eRET ret;

	U8 AxisX = 0x01 << m_cmbHelicalAxis0.GetCurSel();
	U8 AxisY = 0x01 << m_cmbHelicalAxis1.GetCurSel();
	U8 AxisZ = 0x01 << m_cmbHelicalAxis2.GetCurSel();

	_CHKAPI(i8196f_stop_move( bSlotNo, (eSINGLE_AXIS)AxisX, STOP_SLOWDOWN ),
		L"i8196f_stop_move", AxisX);
	_CHKAPI(i8196f_stop_move( bSlotNo, (eSINGLE_AXIS)AxisY, STOP_SLOWDOWN ),
		L"i8196f_stop_move", AxisY);
	_CHKAPI(i8196f_stop_move( bSlotNo, (eSINGLE_AXIS)AxisZ, STOP_SLOWDOWN ),
		L"i8196f_stop_move", AxisZ);

}

void CMultiaxisMotionDlg::OnBnClickedCheck1()
{
	eRET ret;
	int i = 0;

	U8 AxisX = 0x01;
	U8 AxisY = 0x02;
	U16 GroupIndex0 = AxisX | AxisY;

	eINTERP_CONFIG CfgEnable;

	if (m_chkEnableConti.GetCheck())
	{
		CfgEnable = START_INTERP_CONFIG;

		GetDlgItem(IDC_btnLineMove)->EnableWindow(FALSE);
		GetDlgItem(IDC_btnArcMove)->EnableWindow(FALSE);
		GetDlgItem(IDC_btnHelicalMove)->EnableWindow(FALSE);
		for (i = 0; i < MAX_AXIS_QTY; i++)
		{GetDlgItem(IDC_btnReset0+i)->EnableWindow(FALSE);}
		GetDlgItem(IDC_btnResetAll)->EnableWindow(FALSE);

		GetDlgItem(IDC_btnContiStart)->EnableWindow(TRUE);


	}
	else
	{
		CfgEnable = CANCEL_INTERP_CONFIG;

		GetDlgItem(IDC_btnLineMove)->EnableWindow(TRUE);
		GetDlgItem(IDC_btnArcMove)->EnableWindow(TRUE);
		GetDlgItem(IDC_btnHelicalMove)->EnableWindow(TRUE);
		for (i = 0; i < MAX_AXIS_QTY; i++)
		{GetDlgItem(IDC_btnReset0+i)->EnableWindow(TRUE);}
		GetDlgItem(IDC_btnResetAll)->EnableWindow(TRUE);

		GetDlgItem(IDC_btnContiStart)->EnableWindow(FALSE);
	}

	_CHKAPI(i8196f_set_conti_interp_cfg(bSlotNo, CfgEnable, GroupIndex0, 0, 0 ),
		L"i8196f_set_conti_interp_cfg", GroupIndex0);

}

void CMultiaxisMotionDlg::OnBnClickedbtncontistart()
{
	eRET ret;
	int i = 0;
	I32 BaseDist = 10000;
	int Speed = GetDlgItemInt(IDC_edtContiSpeed);
	int LoopTimes = GetDlgItemInt(IDC_edtContiTimes);
	eARC_DIR ArcDirection = ARC_DIR_CCW;

	eSINGLE_AXIS AxisX = AXIS_0; 
	eSINGLE_AXIS AxisY = AXIS_1;
	U8 bitAixs = AxisX | AxisY;

	eINTERPOLATION_MODE InterpMode = CONTI_INTERP_CONFIG;

	for (i = 0; i < LoopTimes; i++)
	{
		_CHKAPI(i8196f_t_line2_move( bSlotNo, AxisX, AxisY,	
			(U32)(Speed/2), (U32)Speed, (U32)Speed, (U32)Speed*2, (U32)Speed*2,		
			BaseDist*2, 0, InterpMode ),
			L"i8196f_t_line2_move", bitAixs);

		_CHKAPI(i8196f_t_arc2_move( bSlotNo, AxisX, AxisY,			
			(U32)(Speed), (U32)Speed, (U32)Speed, (U32)Speed*2, (U32)Speed*2, ArcDirection,			    
			0, BaseDist, BaseDist, BaseDist, InterpMode),
			L"i8196f_t_arc2_move", bitAixs);

		_CHKAPI(i8196f_t_line2_move( bSlotNo, AxisX, AxisY,	
			(U32)(Speed), (U32)Speed, (U32)Speed, (U32)Speed*2, (U32)Speed*2,		
			0, BaseDist, InterpMode ),
			L"i8196f_t_line2_move", bitAixs);

		_CHKAPI(i8196f_t_arc2_move( bSlotNo, AxisX, AxisY,			
			(U32)(Speed), (U32)Speed, (U32)Speed, (U32)Speed*2, (U32)Speed*2, ArcDirection,			    
			BaseDist*(-1), 0, BaseDist*(-1), BaseDist, InterpMode),
			L"i8196f_t_arc2_move", bitAixs);

		_CHKAPI(i8196f_t_line2_move( bSlotNo, AxisX, AxisY,	
			(U32)(Speed), (U32)Speed, (U32)Speed, (U32)Speed*2, (U32)Speed*2,		
			BaseDist*(-2), 0, InterpMode ),
			L"i8196f_t_line2_move", bitAixs);

		_CHKAPI(i8196f_t_arc2_move( bSlotNo, AxisX, AxisY,			
			(U32)(Speed), (U32)Speed, (U32)Speed, (U32)Speed*2, (U32)Speed*2, ArcDirection,			    
			0, BaseDist*(-1), BaseDist*(-1), BaseDist*(-1), InterpMode),
			L"i8196f_t_arc2_move", bitAixs);

		_CHKAPI(i8196f_t_line2_move( bSlotNo, AxisX, AxisY,	
			(U32)(Speed), (U32)Speed, (U32)Speed, (U32)Speed*2, (U32)Speed*2,		
			0, BaseDist*(-1), InterpMode ),
			L"i8196f_t_line2_move", bitAixs);

		_CHKAPI(i8196f_t_arc2_move( bSlotNo, AxisX, AxisY,			
			(U32)(Speed), (U32)Speed, (U32)(Speed/2), (U32)Speed*2, (U32)Speed*2, ArcDirection,			    
			BaseDist, 0, BaseDist, BaseDist*(-1), InterpMode),
			L"i8196f_t_arc2_move", bitAixs);

		if (ret != I8196_SUCCESS)
		{
			TRACE(L"\nLoop(%d) Error\n", i);
			break;
		}
	}	
	
}

void CMultiaxisMotionDlg::OnBnClickedbtncontistop()
{
	eRET ret;
	eSINGLE_AXIS AxisX = AXIS_0; 
	eSINGLE_AXIS AxisY = AXIS_1;

	_CHKAPI(i8196f_stop_move( bSlotNo, AxisX, STOP_SLOWDOWN ), 
		L"i8196f_stop_move", AxisX);
	_CHKAPI(i8196f_stop_move( bSlotNo, AxisY, STOP_SLOWDOWN ),
		L"i8196f_stop_move", AxisY);
}

void CMultiaxisMotionDlg::OnDestroy()
{
	CDialog::OnDestroy();

	delete ptrdlgGph1XY;
}

void CMultiaxisMotionDlg::OnBnClickedbtngraph()
{
	RECT rect;

	ptrdlgGph1XY->SetGraph(m_hBmpHelical);

	this->GetWindowRect(&rect);
	ptrdlgGph1XY->SetWindowPos(NULL, rect.left, rect.top, 690,315, SWP_SHOWWINDOW);
	//ptrdlgGph1XY->ShowWindow(SW_SHOW);

}

void CMultiaxisMotionDlg::OnBnClickedbtngraph2()
{
	RECT rect;

	ptrdlgGph1XY->SetGraph(m_hBmpConti);

	this->GetWindowRect(&rect);
	ptrdlgGph1XY->SetWindowPos(NULL, rect.left, rect.top, 410,315, SWP_SHOWWINDOW);
	//ptrdlgGph1XY->ShowWindow(SW_SHOW);
}
