// MultiAiDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MultiAi.h"
#include "MultiAiDlg.h"
//AIO head file
#include "ccapaio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMultiAiDlg dialog

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

void CMultiAiDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_DEVICE_NAME, m_DeviceName);
	DDX_Control(pDX, IDC_COMBO_AioSetAiRange, m_AiRange);
	DDX_Control(pDX, IDC_ERRORSTRING, m_ErrorString);
	DDX_Control(pDX, IDC_CDATA, m_Cdata);
	DDX_Control(pDX, IDC_EDIT_CHANNELS, m_AiChannels);
	DDX_Control(pDX, IDC_AICHANNELS, m_AiChannel);
	DDX_Control(pDX, IDC_VOLT, m_Volt);
	DDX_Control(pDX, IDC_STATIC_START, m_Start);
	DDX_Control(pDX, IDC_STATIC_ERR, m_Err);
}

BEGIN_MESSAGE_MAP(CMultiAiDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BUTTON_AIOINIT, OnBnClickedButtonAioinit)
	ON_CBN_SELCHANGE(IDC_COMBO_AioSetAiRange, OnCbnSelchangeComboAiosetairange)
	ON_BN_CLICKED(IDC_BUTTON_START, OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_AIOMULTIAI, OnBnClickedButtonAiomultiai)
	ON_BN_CLICKED(IDC_BUTTON_AIOEXIT, OnBnClickedButtonAioexit)
END_MESSAGE_MAP()


// CMultiAiDlg message handlers

BOOL CMultiAiDlg::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 input range
	//-----------------------------------
	m_AiRange.AddString(_T("-10 - +10V"));
	m_AiRange.AddString(_T("-5 - +5V"));
	m_AiRange.AddString(_T("0 - +10V"));
	m_AiRange.AddString(_T("0 - +5V"));
	m_AiRange.AddString(_T("0 - +20mA"));
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

//--------------------------------------------------------------------------------------------------
//	Initialize device																				
//--------------------------------------------------------------------------------------------------
void CMultiAiDlg::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 != AIO_ERR_SUCCESS) {
		return;
	}
	//-----------------------------------
	// Get and display range
	//-----------------------------------
	short	AiRange;
	short	Index;
	Ret = AioGetAiRange(Id, 0, &AiRange);
	if (Ret == AIO_ERR_SUCCESS) {
		switch(AiRange){
		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_AiRange.SetCurSel(Index);

		//-----------------------------------
		// Change display
		//-----------------------------------
		if (AiRange == P20MA) {
			m_Cdata.SetWindowText(_T("Current"));
		}
		else{
			m_Cdata.SetWindowText(_T("Voltage"));
		}
	}
	//-----------------------------------
	// Get and display channel number
	//-----------------------------------
	short	AiChannels;
	Ret = AioGetAiMaxChannels(Id, &AiChannels);
	if (Ret == AIO_ERR_SUCCESS) {
		TCHAR	szText[100];
		wsprintf(szText,_T("%d"), (int)AiChannels);
		m_AiChannels.SetWindowText(szText);
	}
	ShowRemoteStatus();
}

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

//--------------------------------------------------------------------------------------------------
// Start button
//--------------------------------------------------------------------------------------------------
void CMultiAiDlg::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 CMultiAiDlg::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 input of plural channels once
//--------------------------------------------------------------------------------------------------
void CMultiAiDlg::OnBnClickedButtonAiomultiai()
{
	//-----------------------------------
	// Analog input
	//-----------------------------------
	float	AiData[256];
	short	AiChannels;
	TCHAR	szText[100];
	m_AiChannels.GetWindowText(szText, sizeof(szText));
	AiChannels = (short)_ttoi(szText);

	Ret = AioMultiAiEx(Id, AiChannels, &AiData[0]);
	//-----------------------------------
	// Process error
	//-----------------------------------
	AioGetErrorString(Ret, ErrorString);
    wsprintf(szErrorString, _T("AioMultiAiEx : %S"), ErrorString);
	m_ErrorString.SetWindowText(szErrorString);
    if(Ret != 0){
		return;
	}
	//-----------------------------------
	// Display converted data
	//-----------------------------------
	unsigned short	i;
	TextString.Empty();
	for(i=0 ;i<AiChannels; i++){
		TextTemp.Format(_T("%d\n"), i);
		TextString += TextTemp;
		m_AiChannel.SetWindowText(TextString);
	}
	TextString.Empty();
	for(i = 0 ; i < AiChannels ; i++){
		TextTemp.Format(_T("%fV\n"), AiData[i]);
		TextString += TextTemp;
		m_Volt.SetWindowText(TextString);
	}
}

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

//--------------------------------------------------------------------------------------------------
//	Display remote status
//--------------------------------------------------------------------------------------------------
void CMultiAiDlg::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);
}
