// DIODlg.cpp : implementation file
//

#include "stdafx.h"
#include "DIO.h"
#include "DIODlg.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;

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()
{
	WORD wTotalBoards;
	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

	//Initial the resource form  Driver
	wRtn=Ixud_DriverInit(&wTotalBoards);
	if (wRtn!=0) return wRtn;


	m_IDC_COMBO_SelectBoard.Clear();
	//_ltoa( wTotalBoards, string, 10 );
	//SetDlgItemText(IDC_EDIT_TotalBoards, string);
	
	//Get Card Information
	for(WORD wBoardIndex = 0; wBoardIndex<wTotalBoards ; wBoardIndex++)
	{
		wRtn = Ixud_GetCardInfo(wBoardIndex,&sDevInfo[wBoardIndex],&sCardInfo[wBoardIndex],szModelName);
		
		m_IDC_COMBO_SelectBoard.AddString(szModelName);
	}
	

	// 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 wRtn;
	WORD wOutputPortNo;
	DWORD dwDIVal;
	static const int nLength = 10;
	char string[nLength];

	wOutputPortNo = m_IDC_COMBO_OutputPort.GetCurSel();
	
	//Config the Port
	if(sCardInfo[wBoardNo].wDIOPorts>1)
		 wRtn = Ixud_SetDIOModes32(wBoardNo, 1<<wOutputPortNo);
    
	wRtn = Ixud_WriteDO(wBoardNo,wOutputPortNo,GetDlgItemHex(IDC_EDIT_DO));
	wRtn = Ixud_ReadDI(wBoardNo,m_IDC_COMBO_InputPort.GetCurSel(),&dwDIVal);

	

	_ltoa( dwDIVal, string, 16 );
	SetDlgItemText(IDC_EDIT_DI, string);

}


void CDIODlg::OnSelchangeCOMBOSelectBoard() 
{
	// TODO: Add your control notification handler code here
	WORD PortIndex; 
	WORD wPortChNum;
	wBoardNo = m_IDC_COMBO_SelectBoard.GetCurSel();
	static const int nLength = 10;
	char string[nLength];
	 
	wPortChNum = sCardInfo[wBoardNo].wDIOPortWidth;

	//Total DO port
	m_IDC_COMBO_OutputPort.Clear();

	for (PortIndex = 0; PortIndex < (sCardInfo[wBoardNo].wDIOPorts + sCardInfo[wBoardNo].wDOPorts)  ;PortIndex++)
	{	_ltoa(PortIndex,string,10);
		m_IDC_COMBO_OutputPort.AddString(string);  
    }

	m_IDC_COMBO_OutputPort.SetCurSel(0);

	//Total DI port
	m_IDC_COMBO_InputPort.Clear();
	for (PortIndex = 0; PortIndex < (sCardInfo[wBoardNo].wDIOPorts + sCardInfo[wBoardNo].wDIPorts)  ;PortIndex++)
	{	_ltoa(PortIndex,string,10);
		m_IDC_COMBO_InputPort.AddString(string);  
    }
	
	m_IDC_COMBO_InputPort.SetCurSel(0);
	
	//DIO_LED.Caption = Me.cbxBoardNo.Text + "  Digital I/O LED"
	
}

void CDIODlg::OnBtnDi() 
{
	// TODO: Add your control notification handler code here
	
	
}

WORD CDIODlg::GetDlgItemHex(int nID)
{
	static const int nLength = 10;
	CString str;
	char szStr[nLength], * stopstring;
	
	GetDlgItemText(nID, str);
	strcpy(szStr, str.GetBuffer(nLength) );
	return (WORD)strtoul( szStr, &stopstring, 16 );
}

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