// DemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Demo.h"
#include "DemoDlg.h"
//===============================
#include "INIT_Dlg.h"
#include "Manual_Test.h"
#include "Motion_Test.h"
//===============================
#include "Val.h"


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

/////////////////////////////////////////////////////////////////////////////
// CDemoDlg dialog

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

void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDemoDlg)
	DDX_Control(pDX, IDC_But_Manual_Test2, m_Motion_Test);
	DDX_Control(pDX, IDC_But_Manual_Test, m_Manual_Test);
	DDX_Control(pDX, IDC_But_BASIC, m_But_Basic);
	DDX_Control(pDX, IDC_Slot, m_Slot);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CDemoDlg)
	ON_BN_CLICKED(IDC_Init_Card, OnInitCard)
	ON_BN_CLICKED(IDC_REGISTRATION, OnRegistration)
	ON_BN_CLICKED(IDC_But_BASIC, OnButBASIC)
	ON_BN_CLICKED(IDC_But_Manual_Test, OnButManualTest)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_But_Manual_Test2, OnButManualTest2)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDemoDlg message handlers

BOOL CDemoDlg::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
	BYTE i=0;
	char strTemp[20];
	for (i=1;i<8;i++)
	{
		if (GetNameOfModule(i, strTemp)==92)
		{
			m_Slot.SetCurSel(i-1);
			break;
		}
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}


void CDemoDlg::OnInitCard() 
{
	// TODO: Add your control notification handler code here
	INIT_Dlg INIT_D;
	INIT_D.DoModal();
	//=======================================
}

void CDemoDlg::OnRegistration() 
{
	// TODO: Add your control notification handler code here
	//I8092MF.REGISTRATION(cardNo,1);
	//I8092MF.SET_LP(cardNo,1,100);
	//DWORD aa;
	//aa=I8092MF.GET_LP(cardNo,1);
	
		if (I8092MF.REGISTRATION(cardNo,m_Slot.GetCurSel()+1)==YES)
		{
		    Card_Open = true;
			I8092MF.RESET_CARD(cardNo);
		    //==========================================================================
			I8092MF.SET_PULSE_MODE(cardNo, AXIS_XY, 2);
			//I8092MF.IN3_LEVEL(cardNo,AXIS_XY, 1);
			//I8092MF.SET_ALARM(cardNo, AXIS_XY, 0, 1);
			I8092MF.SET_ALARM(cardNo, AXIS_XY, 1, 1);
			I8092MF.SET_NHOME(cardNo, AXIS_XY, 0);
			I8092MF.SET_HLMT(cardNo, AXIS_XY, 0,0);
			I8092MF.SET_HOME_EDGE(cardNo, AXIS_XY, 0);
			i8092_STn_DISABLE(cardNo, AXIS_XY, 0);
			i8092_STn_DISABLE(cardNo, AXIS_XY, 1);
			i8092_STn_DISABLE(cardNo, AXIS_XY, 2);
			//set cardNo card XY axis¡AALARM input open¡Aand active low¡C
			I8092MF.SET_ENCODER(cardNo, AXIS_XY, 0, 0, 0); 
			//set cardNo card XY axis¡AEncoder as AB-phase type¡Ano division¡AZ-phase active low¡C
			I8092MF.SET_MAX_V(cardNo, AXIS_XY, 80000); 
			I8092MF.EXD_DISABLE(cardNo, AXIS_XY);
			I8092MF.SET_LP(cardNo, AXIS_XY,0);
			I8092MF.SET_EP(cardNo, AXIS_XY,0);
			I8092MF.SET_A(cardNo, AXIS_XY,80000);
			I8092MF.SET_K(cardNo, AXIS_XY,800000);
			I8092MF.SET_SV(cardNo, AXIS_XY,1000);
			// Initial Reset
			m_But_Basic.EnableWindow(true);
			m_Manual_Test.EnableWindow(true);
			m_Motion_Test.EnableWindow(true);
		}
		else
		{
			Card_Open = false;
			m_But_Basic.EnableWindow(false);
			m_Manual_Test.EnableWindow(false);
			m_Motion_Test.EnableWindow(false);
		}

}

void CDemoDlg::OnButBASIC() 
{
	// TODO: Add your control notification handler code here
	INIT_Dlg INIT_D;
	INIT_D.DoModal();
}

void CDemoDlg::OnButManualTest() 
{
	// TODO: Add your control notification handler code here
	Manual_Test Manual_D;
	Manual_D.DoModal();
}


void CDemoDlg::OnButManualTest2() 
{
	// TODO: Add your control notification handler code here
	Motion_test Motion_D;
	Motion_D.DoModal();
}

void CDemoDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CBitmap bm;
	BOOL b = bm.LoadBitmap(IDB_BITMAP2);
	CDC memdc;
	memdc.CreateCompatibleDC(NULL);
	memdc.SelectObject(&bm);
	BITMAP BM1;
	bm.GetBitmap(&BM1);
	b=dc.BitBlt(60,10,BM1.bmWidth,BM1.bmHeight,&memdc,0,10,SRCCOPY);
	// Do not call CDialog::OnPaint() for painting messages
}


void CDemoDlg::OnButton1() 
{
	// TODO: Add your control notification handler code here
	//WORD RET;
	//I8092MF.FRnet_SA(cardNo,0,0x0f0f);
	//RET=I8092MF.FRnet_RA(cardNo,8);
	WORD wDI,i;
	for (i=0;1<100;i++)
	wDI=i8092_GET_RR4(cardNo);
}
