// DIODlg.cpp : implementation file
//

#include "stdafx.h"
#include "Test.h"
#include "TestDlg.h"
#include "UniDAQ.h"
#pragma comment(lib,"UniDAQ.lib")

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

WORD wRtn;
WORD wBoardNo;
WORD wTotalBoards;
IXUD_DEVICE_INFO sDevInfo[MAX_BOARD_NUMBER];
IXUD_CARD_INFO sCardInfo[MAX_BOARD_NUMBER];
char szModelName[20]="Unknow Device";
/////////////////////////////////////////////////////////////////////////////
// CDIODlg dialog

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

void CDIODlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDIODlg)
	//DDX_Control(pDX, IDC_COMBO_InputPort, m_IDC_COMBO_InputPort);
	//DDX_Control(pDX, IDC_COMBO_OutputPort, m_IDC_COMBO_OutputPort);
	DDX_Control(pDX, IDC_COMBO_SelectBoard, m_IDC_COMBO_SelectBoard);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDIODlg, CDialog)
	//{{AFX_MSG_MAP(CDIODlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_DO, OnBtnDo)
	ON_CBN_SELCHANGE(IDC_COMBO_SelectBoard, OnSelchangeCOMBOSelectBoard)
	//ON_BN_CLICKED(IDC_BTN_DI, OnBtnDi)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDIODlg message handlers

BOOL CDIODlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	WORD wBoardIndex;
	WORD wChannelIndex;
	// 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

	//Initial the resource form  Driver
	wRtn=Ixud_DriverInit(&wTotalBoards);
	if (wRtn!=0) 
	{
		MessageBox("Driver Initial Error!!");
		return wRtn;
	}
	m_IDC_COMBO_SelectBoard.Clear();
	
	//Get Card Information
	for(wBoardIndex = 0; wBoardIndex<wTotalBoards ; wBoardIndex++)
	{
		wRtn = Ixud_GetCardInfo(wBoardIndex,&sDevInfo[wBoardIndex],&sCardInfo[wBoardIndex],szModelName);
		
		m_IDC_COMBO_SelectBoard.AddString(szModelName);
	}

	SetDlgItemText(DelayMs, "1");
	((CComboBox*)GetDlgItem(Mode))->AddString("0:Low Pulse(<1 KHz)");
	((CComboBox*)GetDlgItem(Mode))->AddString("1:High Pulse(>1 KHz)");
	((CComboBox*)GetDlgItem(Mode))->SetCurSel(0);
	(GetDlgItem(IDC_BTN_DO))->EnableWindow(FALSE);
	
	for(wBoardIndex = 0; wBoardIndex<wTotalBoards; wBoardIndex++)
	{
		for(wChannelIndex = 0; wChannelIndex <= 15; wChannelIndex++)
		{
			wRtn = Ixud_SetFCChannelMode(wBoardIndex, wChannelIndex, 2, 20); //Read Frequency Mode
		}
	}


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

// 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 CDIODlg::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 CDIODlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}


void CDIODlg::OnBtnDo() 
{
	// TODO: Add your control notification handler code here
	WORD wCounterCh,wCounterMode,i;
	unsigned long delayMs;
	WORD wChannelIndex,wStatus;
	float fFrequency;
	WORD wMode;
	WORD wBoardIndex;
	CString str;
	CString strOut;
	delayMs=0;
	((CListBox*)GetDlgItem(IDC_LIST1))->ResetContent();

	GetDlgItemText(delayMs,str);
	delayMs=atol(str);

	wCounterMode=((CComboBox*)GetDlgItem(Mode))->GetCurSel();

	if(wCounterMode == 0)
		wMode = 0x2;
	else
		wMode = 0x12;

	for(wBoardIndex = 0; wBoardIndex<wTotalBoards; wBoardIndex++)
	{
		for(wChannelIndex = 0; wChannelIndex <= 15; wChannelIndex++)
		{
			wRtn = Ixud_SetFCChannelMode(wBoardIndex, wChannelIndex, wMode, 20); //Read Frequency Mode
		}
	}

	for(wChannelIndex = 0; wChannelIndex <= 15; wChannelIndex++)
	{
		wRtn = Ixud_ReadFrequency(wBoardNo, wChannelIndex, &fFrequency, delayMs, &wStatus);
		strOut.Format(_T("Channel %2d; %f Hz; Status = %d"),wChannelIndex,fFrequency,wStatus);
		((CListBox*)GetDlgItem(IDC_LIST1))->AddString(strOut);
	}

}

void CDIODlg::OnSelchangeCOMBOSelectBoard() 
{
	// TODO: Add your control notification handler code here
	WORD PortIndex; 
	WORD wPortChNum;

	int i;
	CString str;
	
	wBoardNo = m_IDC_COMBO_SelectBoard.GetCurSel();

	//whether have the timer counter
	if (sCardInfo[wBoardNo].wCounterChannels == 0)
	{ 
		MessageBox("Not support timer counter");
		(GetDlgItem(IDC_BTN_DO))->EnableWindow(FALSE);
	}
	else
	{

		(GetDlgItem(IDC_BTN_DO))->EnableWindow(TRUE);
	}
}


void CDIODlg::OnCancel() 
{
	// TODO: Add extra cleanup here
	wRtn = Ixud_DriverClose();
	CDialog::OnCancel();
}
