// demo_StartDlg.cpp : implementation file
//

#include "stdafx.h"
#include "demo_Start.h"
#include "demo_StartDlg.h"
#include "WinConSDK.h"
#include "I8092.h"

bool Driver_Open;
BYTE cardNo=0;
CI8092MF I8092MF;

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

/////////////////////////////////////////////////////////////////////////////
// CDemo_StartDlg dialog

CDemo_StartDlg::CDemo_StartDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDemo_StartDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDemo_StartDlg)
	m_Msg = _T("");
	m_XP = 0;
	m_XendP = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDemo_StartDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDemo_StartDlg)
	DDX_Text(pDX, IDC_EDIT1, m_Msg);
	DDX_Text(pDX, IDC_EDIT2, m_XP);
	DDX_Text(pDX, IDC_EDIT3, m_XendP);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDemo_StartDlg, CDialog)
	//{{AFX_MSG_MAP(CDemo_StartDlg)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDemo_StartDlg message handlers

BOOL CDemo_StartDlg::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
	
	CenterWindow(GetDesktopWindow());	// center to the hpc screen

	// TODO: Add extra initialization here
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDemo_StartDlg::Show_Msg(int AA)
{
	m_Msg.Format(_T("%d"),AA);
	UpdateData(FALSE);
}

void CDemo_StartDlg::Show_Msg(CString AA)
{
	m_Msg=AA;
	UpdateData(FALSE);
}


void CDemo_StartDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	//====='Step 1 Driver init
	if  (!Driver_Open)
	{
		I8092MF.REGISTRATION(cardNo,1);
    }
}

void CDemo_StartDlg::OnButton2() 
{
	// TODO: Add your control notification handler code here
	//====='Step 2 CONFIG IO
	I8092MF.RESET_CARD (cardNo);
    I8092MF.SET_PULSE_MODE (cardNo, AXIS_XY, 2);	//set the pulse output mode
    I8092MF.SET_ALARM (cardNo, AXIS_XY, 0, 0);	//disable the SERVO ALARM Input
    I8092MF.SET_ENCODER (cardNo, AXIS_XY, 0, 0, 0);//set the encoder input type
    I8092MF.SET_MAX_V (cardNo, AXIS_XY, 16000);	//set the max speed for XYZU
    I8092MF.EXD_DISABLE (cardNo, AXIS_XY);		//set the external input Off
    I8092MF.SET_LP (cardNo, AXIS_XY, 0);			//set the Logic position =0
    I8092MF.SET_EP (cardNo, AXIS_XY, 0);			//set the Encoger position =0
    I8092MF.SET_A (cardNo, AXIS_XY, 1000);		//set the Acc =1000
    I8092MF.SERVO_ON (cardNo, AXIS_XY);			//set the Servo_ON to servo motors
}

void CDemo_StartDlg::OnButton3() 
{
	// 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 );
    MSGG="Xerr:" + CString(buffer) + "  ";
	KK=I8092MF.GET_ERROR_CODE(cardNo, AXIS_Y);
	_itoa( KK, buffer, 10 );
	MSGG=MSGG + "Yerr:" + CString(buffer) + "  ";
	/*/
	KK=I8092MF.GET_ERROR_CODE(cardNo, AXIS_Z);
	_itoa( KK, buffer, 10 );
	MSGG=MSGG + "Zerr:" + CString(buffer) + "  ";
	KK=I8092MF.GET_ERROR_CODE(cardNo, AXIS_U);
	_itoa( KK, buffer, 10 );
	MSGG=MSGG + "Uerr:" + CString(buffer) + "  ";
		/*/
	}
	else
		MSGG="No Error";
    Show_Msg(MSGG);
}



void CDemo_StartDlg::OnButton4() 
{
	// TODO: Add your control notification handler code here
	BYTE axis=AXIS_X;  //for AXIS_X it can be to AXIS_XYZU
	m_XP=I8092MF.GET_LP(cardNo, AXIS_X);
	UpdateData(false);
	I8092MF.SET_MAX_V(cardNo, axis, 20000); 
	I8092MF.NORMAL_SPEED(cardNo, axis, 0);		//set axis as Symmetrical T curve mode
	I8092MF.SET_V(cardNo, axis, 20000);			//set v=10000 PPS
	I8092MF.SET_A(cardNo, axis, 100000);		//set acc=100000 PPS/S
	I8092MF.SET_SV(cardNo, axis, 10);			//set start speed=1000 PPS
	I8092MF.SET_AO(cardNo, axis, 0);			//set offset pulse (at SV speed)= 0 PS
    I8092MF.FIXED_MOVE(cardNo, axis, 10000);	//run the fixed 10000 Pulse move.
	while (I8092MF.STOP_WAIT(cardNo, axis) == NO)
	{
		DoEvents();
		Sleep(1);
		m_XendP=I8092MF.GET_LP(cardNo, AXIS_X);
		UpdateData(false);
		//wait for axis to stop
	}
	I8092MF.FIXED_MOVE(cardNo, axis, -10000);	//run the fixed -10000 Pulse move.
	while (I8092MF.STOP_WAIT(cardNo, axis) == NO)
	{
		DoEvents();
		Sleep(1);
		m_XendP=I8092MF.GET_LP(cardNo, AXIS_X);
		UpdateData(false);
		//wait for axis to stop
	}
}
