// MultiAoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MultiAo.h"
#include "MultiAoDlg.h"
//AIO head file
#include "ccapaio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CMultiAoDlg dialog

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

void CMultiAoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_DEVICE_NAME, 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_CHANNEL, m_Channel);
	DDX_Control(pDX, IDC_VOLT, m_Volt);
	DDX_Control(pDX, IDC_CDATA2, m_Cdata2);
	DDX_Control(pDX, IDC_CDATA1, m_Cdata1);
	DDX_Control(pDX, IDC_STATIC_START, m_Start);
	DDX_Control(pDX, IDC_STATIC_ERR, m_Err);
	DDX_Control(pDX, IDC_EDIT_VOLT, m_AoVolt);
}

BEGIN_MESSAGE_MAP(CMultiAoDlg, 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_AioSetAoRange, OnCbnSelchangeComboAiosetaorange)
	ON_BN_CLICKED(IDC_BUTTON_START, OnBnClickedButtonStart)
	ON_BN_CLICKED(IDC_BUTTON_STOP, OnBnClickedButtonStop)
	ON_BN_CLICKED(IDC_BUTTON_SET, OnBnClickedButtonSet)
	ON_BN_CLICKED(IDC_BUTTON_AIOMULTIAO, OnBnClickedButtonAiomultiao)
	ON_BN_CLICKED(IDC_BUTTON_AIOEXIT, OnBnClickedButtonAioexit)
END_MESSAGE_MAP()


// CMultiAoDlg message handlers

BOOL CMultiAoDlg::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"));
	//-----------------------------------
	// Initialize output data to 0
	//-----------------------------------
	short i;
	for(i = 0 ; i < 256 ; i++){
		AoVolt[i] = 0;
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

//--------------------------------------------------------------------------------------------------
//	Initialize device																				
//--------------------------------------------------------------------------------------------------
void CMultiAoDlg::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_Cdata1.SetWindowText(_T("Current"));
			m_Cdata2.SetWindowText(_T("Current"));
		}
		else{
			m_Cdata1.SetWindowText(_T("Voltage"));
			m_Cdata2.SetWindowText(_T("Voltage"));
		}
}
	//-----------------------------------
	// Get maximum number of channels
	//-----------------------------------
	Ret = AioGetAoMaxChannels(Id, &MaxAoChannels);
    if(Ret != 0){
	    AioGetErrorString(Ret, ErrorString);
		wsprintf(szErrorString, _T("AioGetAoMaxChannels : %S"), ErrorString);
		m_ErrorString.SetWindowText(szErrorString);
	}
	//-----------------------------------
	// Display voltage
	//-----------------------------------
	short i;
	TextString.Empty();
	for(i = 0 ; i < MaxAoChannels ; i++){
		TextTemp.Format(_T("%d\n"), i);
		TextString += TextTemp;
		m_Channel.SetWindowText(TextString);
	}
	TextString.Empty();
	for(i = 0 ; i < MaxAoChannels ; i++){
		TextTemp.Format(_T("0.000\n"));
		TextString += TextTemp;
		m_Volt.SetWindowText(TextString);
	}
	ShowRemoteStatus();
}

//--------------------------------------------------------------------------------------------------
//	Set analog output range																		
//--------------------------------------------------------------------------------------------------
void CMultiAoDlg::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_Cdata1.SetWindowText(_T("Current"));
		m_Cdata2.SetWindowText(_T("Current"));
	}
	else{
		m_Cdata1.SetWindowText(_T("Voltage"));
		m_Cdata2.SetWindowText(_T("Voltage"));
	}
}

//--------------------------------------------------------------------------------------------------
// Start button
//--------------------------------------------------------------------------------------------------
void CMultiAoDlg::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 CMultiAoDlg::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();
}

//--------------------------------------------------------------------------------------------------
//	Set Analog output data																		
//--------------------------------------------------------------------------------------------------
void CMultiAoDlg::OnBnClickedButtonSet()
{
	TCHAR	szText[100];
	short	AoChannel;
	float	AoVoltTmp;
	//-----------------------------------
	// Get data from screen
	//-----------------------------------
	m_AoChannel.GetWindowText(szText, sizeof(szText));
	AoChannel = (short)_ttoi(szText);
	m_AoVolt.GetWindowText(szText, sizeof(szText));
	AoVoltTmp = (float)_ttof(szText);

	if(AoChannel >= MaxAoChannels){
		m_ErrorString.SetWindowText(_T("Set output data : output channel is out of range"));
		return;
	}
	//-----------------------------------
	// Update array and display data
	//-----------------------------------
	AoVolt[AoChannel] = AoVoltTmp;

	TextString.Empty();
	short i;
	for(i=0 ; i<MaxAoChannels; i++){
		TextTemp.Format(_T("%.3f\n"), AoVolt[i]);
		TextString += TextTemp;
		m_Volt.SetWindowText(TextString);
	}
}

//--------------------------------------------------------------------------------------------------
//	Analog output of plural channels once																	
//--------------------------------------------------------------------------------------------------
void CMultiAoDlg::OnBnClickedButtonAiomultiao()
{
	//-----------------------------------
	// Analog output
	//-----------------------------------
	Ret = AioMultiAoEx(Id, MaxAoChannels, &AoVolt[0]);
	//-----------------------------------
	// Process error
	//-----------------------------------
	AioGetErrorString(Ret, ErrorString);
	wsprintf(szErrorString, _T("AioMultiAoEx : %S"), ErrorString);
	m_ErrorString.SetWindowText(szErrorString);
    if(Ret != 0){
		return;
	}
}

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

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