// AODlg.cpp : implementation file
//

#include "stdafx.h"
#include "AO.h"
#include "AODlg.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";

/////////////////////////////////////////////////////////////////////////////
// CAODlg dialog

CAODlg::CAODlg(CWnd* pParent /*=NULL*/)
	: CDialog(CAODlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CAODlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAODlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAODlg)
	DDX_Control(pDX, IDC_EDIT_AOVal, m_IDC_EDIT_AOVal);
	DDX_Control(pDX, IDC_COMBO_SelectBoard, m_IDC_COMBO_SelectBoard);
	DDX_Control(pDX, IDC_COMBO_Range, m_IDC_COMBO_Range);
	DDX_Control(pDX, IDC_COMBO_Channel, m_IDC_COMBO_Channel);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAODlg, CDialog)
	//{{AFX_MSG_MAP(CAODlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_CBN_SELCHANGE(IDC_COMBO_SelectBoard, OnSelchangeCOMBOSelectBoard)
	ON_BN_CLICKED(IDC_BTN_Output, OnBTNOutput)
	ON_BN_CLICKED(IDC_btn_OutputHex, OnbtnOutputHex)
	//}}AFX_MSG_MAP
	//ON_BN_CLICKED(IDC_btn_OutputHex, &CAODlg::OnBnClickedbtnOutputhex)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CAODlg message handlers

BOOL CAODlg::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
	
	// TODO: Add extra initialization here

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


	m_IDC_COMBO_SelectBoard.Clear();

	
	//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);
	}
	
	m_IDC_EDIT_AOVal.SetWindowText("0.000");

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

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

	//Total AO Channel
	m_IDC_COMBO_Channel.Clear();

	for (wChannelIndex = 0; wChannelIndex < sCardInfo[wBoardNo].wAOChannels ;wChannelIndex++)
	{	_ltoa(wChannelIndex,string,10);
		m_IDC_COMBO_Channel.AddString(string);  
    }

	m_IDC_COMBO_Channel.SetCurSel(0);

	
}

void CAODlg::OnBTNOutput() 
{
	// TODO: Add your control notification handler code here

	//Set AO Config
	wRtn = Ixud_ConfigAO(wBoardNo, m_IDC_COMBO_Channel.GetCurSel(),m_IDC_COMBO_Range.GetCurSel()+16);
	//Write Analog Output for Current(float)
	wRtn = Ixud_WriteAOCurrent(wBoardNo, m_IDC_COMBO_Channel.GetCurSel(), GetDlgItemFloat(IDC_EDIT_AOVal));	
}

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

float CAODlg::GetDlgItemFloat(int nID)
{
	static const int nLength = 10;
	CString str;
	char szStr[nLength];//, * stopstring;
	
	GetDlgItemText(nID, str);
	strcpy(szStr, str.GetBuffer(nLength) );
	return (float)atof(szStr);
}

WORD CAODlg::GetDlgItemHex(int nID)
{
	static const int nLength = 10;
	CString str;
	char szStr[nLength], * stopstring;
	
	GetDlgItemText(nID, str);
	strcpy(szStr, str.GetBuffer(nLength) );

	//a=strtoul(szStr,0,16);
	//return a;
	return (WORD)strtoul( szStr, &stopstring, 16 );
}

void CAODlg::OnbtnOutputHex() 
{
	// TODO: Add your control notification handler code here
	//Analog Output for Current(HEX)
	wRtn = Ixud_WriteAOCurrentH(wBoardNo, m_IDC_COMBO_Channel.GetCurSel(), GetDlgItemHex(IDC_EDIT_AOVal2));
}
