// SingleAoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "SingleAo.h"
#include "SingleAoDlg.h"
//AIO head file
#include "ccapaio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CSingleAoDlg dialog

CSingleAoDlg::CSingleAoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSingleAoDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSingleAoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO1, m_DeviceName);
	DDX_Control(pDX, IDC_COMBO_AioSetAoRange, m_AoRange);
	DDX_Control(pDX, IDC_EDIT_CHANNEL, m_AoChannel);
	DDX_Control(pDX, IDC_ERRORSTRING, m_ErrorString);
	DDX_Control(pDX, IDC_STATIC_START, m_Start);
	DDX_Control(pDX, IDC_STATIC_ERR, m_Err);
	DDX_Control(pDX, IDC_CDATA, m_Cdata);
	DDX_Control(pDX, IDC_EDIT_VOLT, m_AoVolt);
}

BEGIN_MESSAGE_MAP(CSingleAoDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_AIOINIT, OnBnClickedButtonAioinit)
	ON_BN_CLICKED(IDC_BUTTON_START, OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_AIOSINGLEAO, OnBnClickedButtonAiosingleao)
	ON_BN_CLICKED(IDC_BUTTON_AIOEXIT, OnBnClickedButtonAioexit)
	ON_CBN_SELCHANGE(IDC_COMBO_AioSetAoRange, OnCbnSelchangeComboAiosetaorange)
END_MESSAGE_MAP()


// CSingleAoDlg message handlers

BOOL CSingleAoDlg::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

	//-----------------------------------
	// Detect device
	//-----------------------------------
	short	Index;
	short	GroupID, UnitID, DeviceID;
	char	DeviceName[256], Device[256];
	TCHAR	szDeviceName[256];

	for (Index=0; ; Index++) {
		Ret = AioQueryDeviceName(Index, &GroupID, &UnitID, &DeviceID, DeviceName, Device);
		if (Ret != AIO_ERR_SUCCESS) {
			break;
		}
		wsprintf(szDeviceName, _T("%S"), DeviceName);
		m_DeviceName.AddString(szDeviceName);
	}
	m_DeviceName.SetCurSel(0);

	//-----------------------------------
	// Create combobox for analog output range
	//-----------------------------------
	m_AoRange.AddString(_T("-10 - +10V"));
	m_AoRange.AddString(_T("-5 - +5V"));
	m_AoRange.AddString(_T("0 - +10V"));
	m_AoRange.AddString(_T("0 - +5V"));
	m_AoRange.AddString(_T("0 - +20mA"));

	m_AoChannel.SetWindowText(_T("0"));
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CSingleAoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	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 CSingleAoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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 function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CSingleAoDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

//--------------------------------------------------------------------------------------------------
//	Initialize device																				
//--------------------------------------------------------------------------------------------------
void CSingleAoDlg::OnBnClickedButtonAioinit()
{
	//-----------------------------------
	// Initialization
	//-----------------------------------
	char	DeviceName[100];
	TCHAR	szDeviceName[100];
	m_DeviceName.GetWindowText(szDeviceName, sizeof(DeviceName));
	sprintf_s(DeviceName, "%S", szDeviceName);
	Ret = AioInit(DeviceName, &Id);
	//-----------------------------------
	// Process error
	//-----------------------------------
	AioGetErrorString(Ret, ErrorString);
	wsprintf(szErrorString, _T("AioInit : %S"), ErrorString);
	m_ErrorString.SetWindowText(szErrorString);
    if(Ret != 0){
		return;
	}
	//-----------------------------------
	// Get and display range
	//-----------------------------------
	short	AoRange;
	short	Index;
	Ret = AioGetAoRange(Id, 0, &AoRange);
	if (Ret == AIO_ERR_SUCCESS) {
		switch(AoRange){
		case PM10:	Index = 0;	break;
		case PM5:	Index = 1;	break;
		case P10:	Index = 2;	break;
		case P5:	Index = 3;	break;
		case P20MA:	Index = 4;	break;
		}
		m_AoRange.SetCurSel(Index);

		//-----------------------------------
		// Change display
		//-----------------------------------
		if(AoRange == P20MA){
			m_Cdata.SetWindowText(_T("Current"));
		}
		else{
			m_Cdata.SetWindowText(_T("Voltage"));
		}
	}
	ShowRemoteStatus();
}

//--------------------------------------------------------------------------------------------------
//	Set analog output range																		
//--------------------------------------------------------------------------------------------------
void CSingleAoDlg::OnCbnSelchangeComboAiosetaorange()
{
	short	AoRange;
	short	Index;
	//-----------------------------------
	// Get screen content
	//-----------------------------------
	Index = m_AoRange.GetCurSel();
	switch(Index){
	case 0:	AoRange = PM10;		break;
	case 1:	AoRange = PM5;		break;
	case 2:	AoRange = P10;		break;
	case 3:	AoRange = P5;		break;
	case 4:	AoRange = P20MA;	break;
	}
	//-----------------------------------
	// Set output range
	//-----------------------------------
	Ret = AioSetAoRangeAll(Id, AoRange);
	//-----------------------------------
	// Process error
	//-----------------------------------
    AioGetErrorString(Ret, ErrorString);
	wsprintf(szErrorString, _T("AioSetAoRangeAll : %S"), ErrorString);
	m_ErrorString.SetWindowText(szErrorString);
    if (Ret != 0) {
		return;
	}
	//-----------------------------------
	// Change display
	//-----------------------------------
	if(AoRange == P20MA){
		m_Cdata.SetWindowText(_T("Current"));
	}
	else{
		m_Cdata.SetWindowText(_T("Voltage"));
	}
}

//--------------------------------------------------------------------------------------------------
// Start button
//--------------------------------------------------------------------------------------------------
void CSingleAoDlg::OnBnClickedButtonStart()
{
	//-----------------------------------
	// Start
	//-----------------------------------
	Ret = AioStartIO(Id, 0x01);
	//-----------------------------------
	// Process error
	//-----------------------------------
	AioGetErrorString(Ret, ErrorString);
	wsprintf(szErrorString, _T("AioStartIO : %S"), ErrorString);
	m_ErrorString.SetWindowText(szErrorString);
    if(Ret != 0){
		return;
	}
	ShowRemoteStatus();
}

//--------------------------------------------------------------------------------------------------
// Stop button
//--------------------------------------------------------------------------------------------------
void CSingleAoDlg::OnBnClickedButtonStop()
{
	//-----------------------------------
	// Stop
	//-----------------------------------
	Ret = AioStartIO(Id, 0x00);
	//-----------------------------------
	// Process error
	//-----------------------------------
	AioGetErrorString(Ret, ErrorString);
	wsprintf(szErrorString, _T("AioStartIO : %S"), ErrorString);
	m_ErrorString.SetWindowText(szErrorString);
    if(Ret != 0){
		return;
	}
	ShowRemoteStatus();
}

//--------------------------------------------------------------------------------------------------
//	Analog output of specified channel once																	
//--------------------------------------------------------------------------------------------------
void CSingleAoDlg::OnBnClickedButtonAiosingleao()
{
	//-----------------------------------
	// Analog output
	//-----------------------------------
	TCHAR	szText[100];
	short	AoChannel;
	float	AoVolt;
	m_AoChannel.GetWindowText(szText, sizeof(szText));
	AoChannel = (short)_ttoi(szText);
	m_AoVolt.GetWindowText(szText, sizeof(szText));
	AoVolt = (float)_ttof(szText);

	Ret = AioSingleAoEx(Id, AoChannel, AoVolt);
	//-----------------------------------
	// Process error
	//-----------------------------------
	AioGetErrorString(Ret, ErrorString);
	wsprintf(szErrorString, _T("AioSingleAoEx : %S"), ErrorString);
	m_ErrorString.SetWindowText(szErrorString);
    if(Ret != 0){
		return;
	}
}

//--------------------------------------------------------------------------------------------------
//	Terminate device																				
//--------------------------------------------------------------------------------------------------
void CSingleAoDlg::OnBnClickedButtonAioexit()
{
	//-----------------------------------
	// Terminate device
	//-----------------------------------
	Ret = AioExit(Id);
	AioGetErrorString(Ret, ErrorString);
	wsprintf(szErrorString, _T("AioExit : %S"), ErrorString);
	m_ErrorString.SetWindowText(szErrorString);
}

//--------------------------------------------------------------------------------------------------
//	Display remote status
//--------------------------------------------------------------------------------------------------
void CSingleAoDlg::ShowRemoteStatus(void)
{
	BYTE	Start, Err;
 	TCHAR	szText[100];
	//-----------------------------------
	// Input remote status
	//-----------------------------------
	Ret = AioGetRemoteStatus(Id, &Start, &Err);
	//-----------------------------------
	// Process error
	//-----------------------------------
	if (Ret != AIO_ERR_SUCCESS) {
 		AioGetErrorString(Ret, ErrorString);
 		wsprintf(szErrorString, _T("AioGetRemoteStatus : %S"), ErrorString);
		m_ErrorString.SetWindowText(szErrorString);
		return;
 	}
 	//-----------------------------------
	// Display remote status
	//-----------------------------------
	wsprintf(szText, _T("%02X"), (int)Start);
	m_Start.SetWindowText(szText);
	wsprintf(szText, _T("%02X"), (int)Err);
	m_Err.SetWindowText(szText);
}
