// ioTestDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ioTest.h"
#include "ioTestDlg.h"
#include "../include/mc8041p.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIoTestDlg dialog

CIoTestDlg::CIoTestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CIoTestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CIoTestDlg)
	m_CardList = -1;
	m_pulse = 32000;
	m_DriveModeX = 0;
	m_DriveModeU = 0;
	m_DriveModeY = 0;
	m_DriveModeZ = 0;
	m_SelectAxis = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	m_pMMC = NULL;
}

void CIoTestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CIoTestDlg)
	DDX_Control(pDX, IDC_CHECK_DX8, m_DX8);
	DDX_Control(pDX, IDC_CHECK_DX9, m_DX9);
	DDX_Control(pDX, IDC_CHECK_DX10, m_DX10);
	DDX_Control(pDX, IDC_CHECK_DX11, m_DX11);
	DDX_Control(pDX, IDC_CHECK_DY8, m_DY8);
	DDX_Control(pDX, IDC_CHECK_DY9, m_DY9);
	DDX_Control(pDX, IDC_CHECK_DY10, m_DY10);
	DDX_Control(pDX, IDC_CHECK_DY11, m_DY11);
	DDX_Control(pDX, IDC_CHECK_DZ8, m_DZ8);
	DDX_Control(pDX, IDC_CHECK_DZ9, m_DZ9);
	DDX_Control(pDX, IDC_CHECK_DZ10, m_DZ10);
	DDX_Control(pDX, IDC_CHECK_DZ11, m_DZ11);
	DDX_Control(pDX, IDC_CHECK_DU8, m_DU8);
	DDX_Control(pDX, IDC_CHECK_DU9, m_DU9);
	DDX_Control(pDX, IDC_CHECK_DU10, m_DU10);
	DDX_Control(pDX, IDC_CHECK_DU11, m_DU11);
	DDX_Control(pDX, IDC_EDIT_Z2, m_Z2Pos);
	DDX_Control(pDX, IDC_EDIT_Y2, m_Y2Pos);
	DDX_Control(pDX, IDC_EDIT_X2, m_X2Pos);
	DDX_Control(pDX, IDC_EDIT_U2, m_U2Pos);
	DDX_Control(pDX, IDC_EDIT_U, m_UPos);
	DDX_Control(pDX, IDC_EDIT_Z, m_ZPos);
	DDX_Control(pDX, IDC_EDIT_Y, m_YPos);
	DDX_Control(pDX, IDC_EDIT_X, m_XPos);
	DDX_Control(pDX, IDC_BUTTON_READREGISTER, m_ReadButton);
	DDX_Control(pDX, IDC_CHECK_AUTOREAD, m_AutoRead);
	DDX_Control(pDX, IDC_EDIT_INC, m_EditInc);
	DDX_Control(pDX, IDC_COMBO_CARD_LIST, m_ctrlCardList);
	DDX_CBIndex(pDX, IDC_COMBO_CARD_LIST, m_CardList);
	DDX_Control(pDX, IDC_MSFLEXGRID_RR, m_ctrlRR);
	DDX_Text(pDX, IDC_EDIT_INC, m_pulse);
	DDV_MinMaxLong(pDX, m_pulse, 1, 800000);
	DDX_Radio(pDX, IDC_RADIO_DRIVEMODE_X1, m_DriveModeX);
	DDX_Radio(pDX, IDC_RADIO_DRIVEMODE_U1, m_DriveModeU);
	DDX_Radio(pDX, IDC_RADIO_DRIVEMODE_Y1, m_DriveModeY);
	DDX_Radio(pDX, IDC_RADIO_DRIVEMODE_Z1, m_DriveModeZ);
	DDX_Radio(pDX, IDC_RADIO_AXIS1, m_SelectAxis);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CIoTestDlg, CDialog)
	//{{AFX_MSG_MAP(CIoTestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_CHECK_AUTOREAD, OnCheckAutoread)
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BUTTON_READREGISTER, OnButtonReadregister)
	ON_CBN_SELCHANGE(IDC_COMBO_CARD_LIST, OnSelchangeComboCardList)
	ON_BN_CLICKED(IDC_BUTTON_PLUSX, OnButtonPlusx)
	ON_BN_CLICKED(IDC_BUTTON_MINUSX, OnButtonMinusx)
	ON_BN_CLICKED(IDC_BUTTON_PLUSY, OnButtonPlusy)
	ON_BN_CLICKED(IDC_BUTTON_MINUSY, OnButtonMinusy)
	ON_BN_CLICKED(IDC_BUTTON_PLUSZ, OnButtonPlusz)
	ON_BN_CLICKED(IDC_BUTTON_MINUSZ, OnButtonMinusz)
	ON_BN_CLICKED(IDC_BUTTON_PLUSU, OnButtonPlusu)
	ON_BN_CLICKED(IDC_BUTTON_MINUSU, OnButtonMinusu)
	ON_BN_CLICKED(IDC_STATIC_ABOUT, OnStaticAbout)
	ON_BN_CLICKED(IDC_CHECK_DX8, OnCheckDx8)
	ON_BN_CLICKED(IDC_CHECK_DX9, OnCheckDx9)
	ON_BN_CLICKED(IDC_CHECK_DX10, OnCheckDx10)
	ON_BN_CLICKED(IDC_CHECK_DX11, OnCheckDx11)
	ON_BN_CLICKED(IDC_RADIO_DRIVEMODE_X1, OnRadioDrivemodeX1)
	ON_BN_CLICKED(IDC_RADIO_DRIVEMODE_X2, OnRadioDrivemodeX2)
	ON_BN_CLICKED(IDC_RADIO_DRIVEMODE_X3, OnRadioDrivemodeX3)
	ON_BN_CLICKED(IDC_CHECK_DY8, OnCheckDy8)
	ON_BN_CLICKED(IDC_CHECK_DY9, OnCheckDy9)
	ON_BN_CLICKED(IDC_CHECK_DY10, OnCheckDy10)
	ON_BN_CLICKED(IDC_CHECK_DY11, OnCheckDy11)
	ON_BN_CLICKED(IDC_RADIO_DRIVEMODE_Y1, OnRadioDrivemodeY1)
	ON_BN_CLICKED(IDC_RADIO_DRIVEMODE_Y2, OnRadioDrivemodeY2)
	ON_BN_CLICKED(IDC_RADIO_DRIVEMODE_Y3, OnRadioDrivemodeY3)
	ON_BN_CLICKED(IDC_CHECK_DZ8, OnCheckDz8)
	ON_BN_CLICKED(IDC_CHECK_DZ9, OnCheckDz9)
	ON_BN_CLICKED(IDC_CHECK_DZ10, OnCheckDz10)
	ON_BN_CLICKED(IDC_CHECK_DZ11, OnCheckDz11)
	ON_BN_CLICKED(IDC_RADIO_DRIVEMODE_Z1, OnRadioDrivemodeZ1)
	ON_BN_CLICKED(IDC_RADIO_DRIVEMODE_Z2, OnRadioDrivemodeZ2)
	ON_BN_CLICKED(IDC_RADIO_DRIVEMODE_Z3, OnRadioDrivemodeZ3)
	ON_BN_CLICKED(IDC_CHECK_DU8, OnCheckDu8)
	ON_BN_CLICKED(IDC_CHECK_DU9, OnCheckDu9)
	ON_BN_CLICKED(IDC_CHECK_DU10, OnCheckDu10)
	ON_BN_CLICKED(IDC_CHECK_DU11, OnCheckDu11)
	ON_BN_CLICKED(IDC_RADIO_DRIVEMODE_U1, OnRadioDrivemodeU1)
	ON_BN_CLICKED(IDC_RADIO_DRIVEMODE_U2, OnRadioDrivemodeU2)
	ON_BN_CLICKED(IDC_RADIO_DRIVEMODE_U3, OnRadioDrivemodeU3)
	ON_BN_CLICKED(IDC_RADIO_AXIS1, OnRadioAxis1)
	ON_BN_CLICKED(IDC_RADIO_AXIS2, OnRadioAxis2)
	ON_BN_CLICKED(IDC_RADIO_AXIS3, OnRadioAxis3)
	ON_BN_CLICKED(IDC_RADIO_AXIS4, OnRadioAxis4)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CIoTestDlg message handlers


int CIoTestDlg::init()
{
	char buf[256];
	int i, card_id, index, n=0;
	CString valid_string;

	close();

	m_pMMC = new CMMCControl();

	if( !m_pMMC )
	{
		AfxMessageBox("Memory allocation error!");
		return -1;
	}
	if( !(n=m_pMMC->init()) )
	{
		AfxMessageBox("MMC init error!");
		return 0;
	}
	for(i=0;i<m_pMMC->m_no;i++)
	{
		index = m_pMMC->index[ i ];
		card_id = m_pMMC->inwN(index, RID) & 0x0f;
		valid_string = m_pMMC->_ValidString();
		sprintf(buf,"%2d: Valid string('%s'), Board id(%d)", 
			i, (LPCTSTR)valid_string, card_id);
		m_ctrlCardList.AddString(buf);
	}

	m_ctrlCardList.SetCurSel(0);
	m_CardList=0;
	m_AutoRead.SetCheck(0);
	m_XPos.SetWindowText("0");
	m_YPos.SetWindowText("0");
	m_ZPos.SetWindowText("0");
	m_UPos.SetWindowText("0");
	m_DriveModeX =
	m_DriveModeY =
	m_DriveModeZ =
	m_DriveModeU =
	m_wr3[0] = 
	m_wr3[1] = 
	m_wr3[2] = 
	m_wr3[3] = 0x0000;
	WriteWR3(0, 0xFFFF, FALSE); // clear
	WriteWR3(1, 0xFFFF, FALSE); // clear
	WriteWR3(2, 0xFFFF, FALSE); // clear
	WriteWR3(3, 0xFFFF, FALSE); // clear

	for(i=0;i<6;i++)
	{
		sprintf(buf, "RR%d",i);
		m_ctrlRR.SetCol(0), m_ctrlRR.SetRow(i+1);
		m_ctrlRR.SetText(buf);
	}
	for(i=0;i<16+1;i++)
	{
		m_ctrlRR.SetColAlignment(i,4);
	}


	m_AutoRead.SetCheck(1);
	m_ReadButton.EnableWindow(FALSE);
	SetTimer(20, 50, NULL);

	// -- Tooltip control --
	{
		m_tooltip.Create(this);
		m_tooltip.Activate(TRUE);

		m_tooltip.AddTool(GetDlgItem(IDC_STATIC_ABOUT),IDS_STATIC_ABOUT_MSG);

		m_tooltip.AddTool(GetDlgItem(IDC_EDIT_INC),IDS_STRING_EDIT_INC);

		m_tooltip.AddTool(GetDlgItem(IDC_BUTTON_PLUSX),IDS_STRING_PLUS_X);
		m_tooltip.AddTool(GetDlgItem(IDC_BUTTON_PLUSY),IDS_STRING_PLUS_Y);
		m_tooltip.AddTool(GetDlgItem(IDC_BUTTON_PLUSZ),IDS_STRING_PLUS_Z);
		m_tooltip.AddTool(GetDlgItem(IDC_BUTTON_PLUSU),IDS_STRING_PLUS_U);

		m_tooltip.AddTool(GetDlgItem(IDC_BUTTON_MINUSX),IDS_STRING_MINUS_X);
		m_tooltip.AddTool(GetDlgItem(IDC_BUTTON_MINUSY),IDS_STRING_MINUS_Y);
		m_tooltip.AddTool(GetDlgItem(IDC_BUTTON_MINUSZ),IDS_STRING_MINUS_Z);
		m_tooltip.AddTool(GetDlgItem(IDC_BUTTON_MINUSU),IDS_STRING_MINUS_U);

		m_tooltip.AddTool(GetDlgItem(IDC_EDIT_X),IDS_STRING_EDIT_X);
		m_tooltip.AddTool(GetDlgItem(IDC_EDIT_Y),IDS_STRING_EDIT_Y);
		m_tooltip.AddTool(GetDlgItem(IDC_EDIT_Z),IDS_STRING_EDIT_Z);
		m_tooltip.AddTool(GetDlgItem(IDC_EDIT_U),IDS_STRING_EDIT_U);

		m_tooltip.AddTool(GetDlgItem(IDC_EDIT_X2),IDS_STRING_EDIT_X2);
		m_tooltip.AddTool(GetDlgItem(IDC_EDIT_Y2),IDS_STRING_EDIT_Y2);
		m_tooltip.AddTool(GetDlgItem(IDC_EDIT_Z2),IDS_STRING_EDIT_Z2);
		m_tooltip.AddTool(GetDlgItem(IDC_EDIT_U2),IDS_STRING_EDIT_U2);

		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DX8),IDS_STRING_DX8);
		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DX9),IDS_STRING_DX9);
		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DX10),IDS_STRING_DX10);
		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DX11),IDS_STRING_DX11);

		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DY8),IDS_STRING_DY8);
		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DY9),IDS_STRING_DY9);
		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DY10),IDS_STRING_DY10);
		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DY11),IDS_STRING_DY11);

		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DZ8),IDS_STRING_DZ8);
		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DZ9),IDS_STRING_DZ9);
		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DZ10),IDS_STRING_DZ10);
		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DZ11),IDS_STRING_DZ11);

		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DU8),IDS_STRING_DU8);
		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DU9),IDS_STRING_DU9);
		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DU10),IDS_STRING_DU10);
		m_tooltip.AddTool(GetDlgItem(IDC_CHECK_DU11),IDS_STRING_DU11);

		m_tooltip.AddTool(GetDlgItem(IDC_RADIO_AXIS1),IDS_STRING_AXIS1);
		m_tooltip.AddTool(GetDlgItem(IDC_RADIO_AXIS2),IDS_STRING_AXIS2);
		m_tooltip.AddTool(GetDlgItem(IDC_RADIO_AXIS3),IDS_STRING_AXIS3);
		m_tooltip.AddTool(GetDlgItem(IDC_RADIO_AXIS4),IDS_STRING_AXIS4);

		m_tooltip.AddTool(GetDlgItem(IDC_MSFLEXGRID_RR),IDS_STRING_RR_FLEXGRID);
	}

	return n;
}

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

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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 n = init();

	return TRUE;
}

void CIoTestDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CIoTestDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CIoTestDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CIoTestDlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	
//	CDialog::OnCancel();
}

void CIoTestDlg::OnOK() 
{
	// TODO: Add extra validation here
	close();

	CDialog::OnOK();
}

void CIoTestDlg::OnCheckAutoread() 
{
	// TODO: Add your control notification handler code here
	if( m_AutoRead.GetCheck() )
	{
		SetTimer(20, 50, NULL);
		m_ReadButton.EnableWindow(FALSE);
	}
	else
	{
		KillTimer(20);
		m_ReadButton.EnableWindow(TRUE);
	}
}

void CIoTestDlg::OnTimer(UINT nIDEvent) 
{
	char buf[20];
	USHORT t;
	int index;

	index = m_pMMC->index[ m_CardList ];

	switch(nIDEvent)
	{
	case 1:
		if( !m_pMMC ) break;
		t = m_pMMC->inwN( index , RR0 ) & 0x01;
		sprintf( buf, "%d", m_pMMC->readlp_N( index, 0x01 ) );
		m_XPos.SetWindowText(buf);
		if( !t ) KillTimer(1);
		break;
	case 2:
		if( !m_pMMC ) break;
		t = m_pMMC->inwN( index , RR0 ) & 0x02;
		sprintf( buf, "%d", m_pMMC->readlp_N( index, 0x02 ) );
		m_YPos.SetWindowText(buf);
		if( !t ) KillTimer(2);
		break;
	case 4:
		if( !m_pMMC ) break;
		t = m_pMMC->inwN( index , RR0 ) & 0x04;
		sprintf( buf, "%d", m_pMMC->readlp_N( index, 0x04 ) );
		m_ZPos.SetWindowText(buf);
		if( !t ) KillTimer(4);
		break;
	case 8:
		if( !m_pMMC ) break;
		t = m_pMMC->inwN( index , RR0 ) & 0x08;
		sprintf( buf, "%d", m_pMMC->readlp_N( index, 0x08 ) );
		m_UPos.SetWindowText(buf);
		if( !t ) KillTimer(8);
		break;
	case 20:
		if(m_AutoRead.GetCheck()) Reflash();
		break;
	default:
		break;
	}

	CDialog::OnTimer(nIDEvent);
}

void CIoTestDlg::Reflash(BOOL force) 
{
	char *StatText[6][16] = {
		{"-","BPSC1","BPSC0","ZONE2",/**/"ZONE1","ZONE0","CNEXT","I-DRV",/**/"U-ERR","Z-ERR","Y-ERR","X-ERR",/**/"U-DRV","Z-DRV","Y-DRV","X-DRV"},
		{"EMG","ALARM","LMT-","LMT+",/**/"IN3","IN2","IN1","IN0",/**/"ADSND","ACNST","AASND","DSND",/**/"CNST","ASND","CMP-","CMP+"},
		{"-","-","-","-",/**/"-","-","-","-",/**/"-","-","EMG","ALARM",/**/"HLMT-","HLMT+","SLMT-","SLMT+"},
		{"-","-","-","-",/**/"-","-","-","-",/**/"D-END","C-STA","C-END","P>=C+",/**/"P<C+","P<C-","P>=C-","PULSE"},
		{"Y-ALM","Y-INP","Y-EX-","Y-EX+",/**/"Y-IN3","Y-IN2","Y-IN1","Y-IN0",/**/"X-ALM","X-INP","X-EX-","X-EX+",/**/"X-IN3","X-IN2","X-IN1","X-IN0"},
		{"U-ALM","U-INP","U-EX-","U-EX+",/**/"U-IN3","U-IN2","U-IN1","U-IN0",/**/"Z-ALM","Z-INP","Z-EX-","Z-EX+",/**/"Z-IN3","Z-IN2","Z-IN1","Z-IN0"}
	};

	int j, k;

	if(!m_pMMC) return;
	static USHORT rr[4][6];
	USHORT t, t1;
	static int first=1;
	CString tmp;
	int index;

	index = m_pMMC->index[ m_CardList ];

	if(first)
	{
		memset((char*)rr,0xff,4*6*sizeof(USHORT));
	}

	m_pMMC->readRR(index, m_SelectAxis );
	for(j=0;j<6;j++)
	{
		if(force || rr[m_SelectAxis][j]^m_pMMC->m_rr[m_SelectAxis][j])
		{
			t = rr[m_SelectAxis][j];
			t1 = rr[m_SelectAxis][j] = m_pMMC->m_rr[m_SelectAxis][j];
			for(k=15;k>=0;k--)
			{
				if( first || (t&0x1)!=(t1&0x1) )
				{
					m_ctrlRR.SetRow(j+1);
					m_ctrlRR.SetCol(k+1);
					tmp = StatText[j][k];
					if(!(t1&0x1))
						tmp.MakeLower();
					m_ctrlRR.SetText((LPCTSTR)tmp);
				}
				t >>= 1;
				t1 >>= 1;
				} // for k
		} // if
	} // for j

	tmp.Format("%d", m_pMMC->readep_N( index, 0x01 ) );
	m_X2Pos.SetWindowText((LPCTSTR)tmp);

	tmp.Format("%d", m_pMMC->readep_N( index, 0x02 ) );
	m_Y2Pos.SetWindowText((LPCTSTR)tmp);

	tmp.Format("%d", m_pMMC->readep_N( index, 0x04 ) );
	m_Z2Pos.SetWindowText((LPCTSTR)tmp);

	tmp.Format("%d", m_pMMC->readep_N( index, 0x08 ) );
	m_U2Pos.SetWindowText((LPCTSTR)tmp);

	first = 0;
}

void CIoTestDlg::OnButtonReadregister() 
{
	// TODO: Add your control notification handler code here
	if(!m_pMMC) return;
	Reflash(TRUE);
}

void CIoTestDlg::OnSelchangeComboCardList() 
{
	if(!m_pMMC) return;

	int old_cardno = m_CardList;

	UpdateData(TRUE);
	if(old_cardno==m_CardList) return;

	m_DX8.SetCheck(0);
	m_DX9.SetCheck(0);
	m_DX10.SetCheck(0);
	m_DX11.SetCheck(0);
	m_DY8.SetCheck(0);
	m_DY9.SetCheck(0);
	m_DY10.SetCheck(0);
	m_DY11.SetCheck(0);
	m_DZ8.SetCheck(0);
	m_DZ9.SetCheck(0);
	m_DZ10.SetCheck(0);
	m_DZ11.SetCheck(0);
	m_DU8.SetCheck(0);
	m_DU9.SetCheck(0);
	m_DU10.SetCheck(0);
	m_DU11.SetCheck(0);
	m_DriveModeX =
	m_DriveModeY =
	m_DriveModeZ =
	m_DriveModeU =
	m_wr3[0] = 
	m_wr3[1] = 
	m_wr3[2] = 
	m_wr3[3] = 0x0000;
	WriteWR3(0, 0xFFFF, FALSE); // clear
	WriteWR3(1, 0xFFFF, FALSE); // clear
	WriteWR3(2, 0xFFFF, FALSE); // clear
	WriteWR3(3, 0xFFFF, FALSE); // clear
	UpdateData(FALSE);

	Reflash();
}

void CIoTestDlg::OnButtonPlusx() 
{
	if(!m_pMMC) return;

	// x+ driving pos
	UpdateData(TRUE);
	int index = m_pMMC->index[ m_CardList ];
	m_pMMC->pulse_N( index, 0x01, m_pulse );
	m_pMMC->command_N( index, 0x1, 0x20 );

	SetTimer(1, 30, NULL);

	// Auto select
	if(m_AutoRead.GetCheck())
	{
		m_SelectAxis = 0;
		UpdateData(FALSE);
	}
}

void CIoTestDlg::OnButtonMinusx() 
{
	if(!m_pMMC) return;

	// x- driving pos
	UpdateData(TRUE);
	int index = m_pMMC->index[ m_CardList ];
	m_pMMC->pulse_N( index, 0x01, m_pulse );
	m_pMMC->command_N( index, 0x01, 0x21 );

	SetTimer(1, 30, NULL);

	// Auto select
	if(m_AutoRead.GetCheck())
	{
		m_SelectAxis = 0;
		UpdateData(FALSE);
	}
}

void CIoTestDlg::OnButtonPlusy() 
{
	if(!m_pMMC) return;

	// y+ driving pos
	UpdateData(TRUE);
	int index = m_pMMC->index[ m_CardList ];
	m_pMMC->pulse_N( index, 0x02, m_pulse );
	m_pMMC->command_N( index, 0x2, 0x20 );

	SetTimer(2, 30, NULL);

	// Auto select
	if(m_AutoRead.GetCheck())
	{
		m_SelectAxis = 1;
		UpdateData(FALSE);
	}
}

void CIoTestDlg::OnButtonMinusy() 
{
	if(!m_pMMC) return;

	// y- driving pos
	UpdateData(TRUE);
	int index = m_pMMC->index[ m_CardList ];
	m_pMMC->pulse_N( index, 0x02, m_pulse );
	m_pMMC->command_N( index, 0x02, 0x21 );

	SetTimer(2, 30, NULL);

	// Auto select
	if(m_AutoRead.GetCheck())
	{
		m_SelectAxis = 1;
		UpdateData(FALSE);
	}
}

void CIoTestDlg::OnButtonPlusz() 
{
	if(!m_pMMC) return;

	// z+ driving pos
	UpdateData(TRUE);
	int index = m_pMMC->index[ m_CardList ];
	m_pMMC->pulse_N( index, 0x04, m_pulse );
	m_pMMC->command_N( index, 0x4, 0x20 );

	SetTimer(4, 30, NULL);

	// Auto select
	if(m_AutoRead.GetCheck())
	{
		m_SelectAxis = 2;
		UpdateData(FALSE);
	}
}

void CIoTestDlg::OnButtonMinusz() 
{
	if(!m_pMMC) return;

	// z- driving pos
	UpdateData(TRUE);
	int index = m_pMMC->index[ m_CardList ];
	m_pMMC->pulse_N( index, 0x04, m_pulse );
	m_pMMC->command_N( index, 0x04, 0x21 );

	SetTimer(4, 30, NULL);

	// Auto select
	if(m_AutoRead.GetCheck())
	{
		m_SelectAxis = 2;
		UpdateData(FALSE);
	}
}

void CIoTestDlg::OnButtonPlusu() 
{
	if(!m_pMMC) return;

	// u+ driving pos
	UpdateData(TRUE);
	int index = m_pMMC->index[ m_CardList ];
	m_pMMC->pulse_N( index, 0x08, m_pulse );
	m_pMMC->command_N( index, 0x08, 0x20 );

	SetTimer(8, 30, NULL);

	// Auto select
	if(m_AutoRead.GetCheck())
	{
		m_SelectAxis = 3;
		UpdateData(FALSE);
	}
}

void CIoTestDlg::OnButtonMinusu() 
{
	if(!m_pMMC) return;

	// u- driving pos
	UpdateData(TRUE);
	int index = m_pMMC->index[ m_CardList ];
	m_pMMC->pulse_N( index, 0x08, m_pulse );
	m_pMMC->command_N( index, 0x08, 0x21 );

	SetTimer(8, 30, NULL);

	// Auto select
	if(m_AutoRead.GetCheck())
	{
		m_SelectAxis = 3;
		UpdateData(FALSE);
	}
}

void CIoTestDlg::WriteWR3(USHORT axis, USHORT wr3, BOOL flag) // TRUE:set, FALSE:reset
{
	if(!m_pMMC) return;
	USHORT ax = (0x01 << axis) & 0x0f;
	if(!ax) return;

	USHORT t= flag ? m_wr3[axis]|wr3 : m_wr3[axis]&(~wr3);
	int index = m_pMMC->index[ m_CardList ];
	m_pMMC->wreg3_N( index, ax, t );
	m_wr3[axis] = t;
}

void CIoTestDlg::OnStaticAbout() 
{
	CAboutDlg dlg;

	dlg.DoModal();
}

void CIoTestDlg::OnCheckDx8() {	if(!m_pMMC) return;	WriteWR3(0, 0x0100, m_DX8.GetCheck()); }
void CIoTestDlg::OnCheckDx9() {	if(!m_pMMC) return;	WriteWR3(0, 0x0200, m_DX9.GetCheck()); }
void CIoTestDlg::OnCheckDx10(){	if(!m_pMMC) return;	WriteWR3(0, 0x0400, m_DX10.GetCheck());}
void CIoTestDlg::OnCheckDx11(){	if(!m_pMMC) return;	WriteWR3(0, 0x0800, m_DX11.GetCheck());}
void CIoTestDlg::OnRadioDrivemodeX1()
{	if(!m_pMMC) return;	WriteWR3(0, 0x0018, FALSE); }
void CIoTestDlg::OnRadioDrivemodeX2()
{	if(!m_pMMC) return;	WriteWR3(0, 0x0018, FALSE); WriteWR3(0, 0x0008, TRUE); }
void CIoTestDlg::OnRadioDrivemodeX3()
{	if(!m_pMMC) return;	WriteWR3(0, 0x0018, FALSE); WriteWR3(0, 0x0010, TRUE); }

void CIoTestDlg::OnCheckDy8() {	if(!m_pMMC) return;	WriteWR3(1, 0x0100, m_DY8.GetCheck()); }
void CIoTestDlg::OnCheckDy9() {	if(!m_pMMC) return;	WriteWR3(1, 0x0200, m_DY9.GetCheck()); }
void CIoTestDlg::OnCheckDy10(){	if(!m_pMMC) return;	WriteWR3(1, 0x0400, m_DY10.GetCheck());}
void CIoTestDlg::OnCheckDy11(){	if(!m_pMMC) return;	WriteWR3(1, 0x0800, m_DY11.GetCheck());}
void CIoTestDlg::OnRadioDrivemodeY1()
{	if(!m_pMMC) return;	WriteWR3(1, 0x0018, FALSE); }
void CIoTestDlg::OnRadioDrivemodeY2()
{	if(!m_pMMC) return;	WriteWR3(1, 0x0018, FALSE); WriteWR3(1, 0x0008, TRUE); }
void CIoTestDlg::OnRadioDrivemodeY3()
{	if(!m_pMMC) return;	WriteWR3(1, 0x0018, FALSE); WriteWR3(1, 0x0010, TRUE); }

void CIoTestDlg::OnCheckDz8() {	if(!m_pMMC) return;	WriteWR3(2, 0x0100, m_DZ8.GetCheck()); }
void CIoTestDlg::OnCheckDz9() {	if(!m_pMMC) return;	WriteWR3(2, 0x0200, m_DZ9.GetCheck()); }
void CIoTestDlg::OnCheckDz10(){	if(!m_pMMC) return;	WriteWR3(2, 0x0400, m_DZ10.GetCheck());}
void CIoTestDlg::OnCheckDz11(){	if(!m_pMMC) return;	WriteWR3(2, 0x0800, m_DZ11.GetCheck());}
void CIoTestDlg::OnRadioDrivemodeZ1()
{	if(!m_pMMC) return;	WriteWR3(2, 0x0018, FALSE); }
void CIoTestDlg::OnRadioDrivemodeZ2()
{	if(!m_pMMC) return;	WriteWR3(2, 0x0018, FALSE); WriteWR3(2, 0x0008, TRUE); }
void CIoTestDlg::OnRadioDrivemodeZ3()
{	if(!m_pMMC) return;	WriteWR3(2, 0x0018, FALSE); WriteWR3(2, 0x0010, TRUE); }

void CIoTestDlg::OnCheckDu8() {	if(!m_pMMC) return;	WriteWR3(3, 0x0100, m_DU8.GetCheck()); }
void CIoTestDlg::OnCheckDu9() {	if(!m_pMMC) return;	WriteWR3(3, 0x0200, m_DU9.GetCheck()); }
void CIoTestDlg::OnCheckDu10(){	if(!m_pMMC) return;	WriteWR3(3, 0x0400, m_DU10.GetCheck());}
void CIoTestDlg::OnCheckDu11(){	if(!m_pMMC) return;	WriteWR3(3, 0x0800, m_DU11.GetCheck());}
void CIoTestDlg::OnRadioDrivemodeU1()
{	if(!m_pMMC) return;	WriteWR3(3, 0x0018, FALSE); }
void CIoTestDlg::OnRadioDrivemodeU2()
{	if(!m_pMMC) return;	WriteWR3(3, 0x0018, FALSE); WriteWR3(3, 0x0008, TRUE); }
void CIoTestDlg::OnRadioDrivemodeU3()
{	if(!m_pMMC) return;	WriteWR3(3, 0x0018, FALSE); WriteWR3(3, 0x0010, TRUE); }

void CIoTestDlg::OnRadioAxis1() {	UpdateData(TRUE);	}
void CIoTestDlg::OnRadioAxis2() {	UpdateData(TRUE);	}
void CIoTestDlg::OnRadioAxis3() {	UpdateData(TRUE);	}
void CIoTestDlg::OnRadioAxis4() {	UpdateData(TRUE);	}


BEGIN_EVENTSINK_MAP(CIoTestDlg, CDialog)
    //{{AFX_EVENTSINK_MAP(CIoTestDlg)
	ON_EVENT(CIoTestDlg, IDC_MSFLEXGRID_RR, -600 /* Click */, OnClickMsflexgridRr, VTS_NONE)
	//}}AFX_EVENTSINK_MAP
END_EVENTSINK_MAP()

void CIoTestDlg::OnClickMsflexgridRr() 
{
}

BOOL CIoTestDlg::PreTranslateMessage(MSG* pMsg)
{
	// CG: The following block was added by the ToolTips component.
	{
		// Let the ToolTip process this message.
		m_tooltip.RelayEvent(pMsg);
	}
	return CDialog::PreTranslateMessage(pMsg);	// CG: This was added by the ToolTips component.
}

void CIoTestDlg::close()
{
	if(m_pMMC)
	{
		m_ctrlCardList.ResetContent();
		m_pMMC->close();
		delete m_pMMC;
		m_pMMC = NULL;
	}
}
