﻿
// AoSimpleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AoSimple.h"
#include "AoSimpleDlg.h"
#include "afxdialogex.h"
#include "Caio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAoSimpleDlg dialog



CAoSimpleDlg::CAoSimpleDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_AOSIMPLE_DIALOG, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CAoSimpleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEVICE_NAME, m_device_name);
	DDX_Control(pDX, IDC_COMBO_RANGE_VALUE, m_ao_range);
	DDX_Control(pDX, IDC_EDIT_CH_NO, m_single_ao_channel);
	DDX_Control(pDX, IDC_EDIT_SINGLE_AO_DATA, m_single_ao_output_data);
	DDX_Control(pDX, IDC_EDIT_CH_NUM, m_multi_ao_channels);
	DDX_Control(pDX, IDC_EDIT_MULTI_AO_DATA_CH0, m_multi_ao_ch0_output_data);
	DDX_Control(pDX, IDC_EDIT_MULTI_AO_DATA_CH1, m_multi_ao_ch1_output_data);
	DDX_Control(pDX, IDC_EDIT_MULTI_AO_DATA_CH2, m_multi_ao_ch2_output_data);
	DDX_Control(pDX, IDC_EDIT_MULTI_AO_DATA_CH3, m_multi_ao_ch3_output_data);
	DDX_Control(pDX, IDC_EDIT_RETURN_CODE, m_return_code);
}

BEGIN_MESSAGE_MAP(CAoSimpleDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_AIO_INIT, &CAoSimpleDlg::OnBnClickedButtonAioInit)
	ON_BN_CLICKED(IDC_BUTTON_SET_AO_RANGE_ALL, &CAoSimpleDlg::OnBnClickedButtonSetAoRangeAll)
	ON_BN_CLICKED(IDC_BUTTON_SINGLE_AO_EX, &CAoSimpleDlg::OnBnClickedButtonSingleAoEx)
	ON_BN_CLICKED(IDC_BUTTON_MULTI_AO_EX, &CAoSimpleDlg::OnBnClickedButtonMultiAoEx)
	ON_BN_CLICKED(IDC_BUTTON_AIO_EXIT, &CAoSimpleDlg::OnBnClickedButtonAioExit)
	ON_BN_CLICKED(IDCLOSE, &CAoSimpleDlg::OnBnClickedClose)
END_MESSAGE_MAP()

//----------------------------------------
// Macro definition
//----------------------------------------
#define MAX_AO_RANGE_NUM	31								// Maximum number of elements in analog output range array
//----------------------------------------
// Declaration of global variables
//----------------------------------------
short	g_id;												// ID for device access
short	g_ao_range[MAX_AO_RANGE_NUM] = {	PM10,			// +/-10V
											PM5,			// +/-5V
											PM25,			// +/-2.5V
											PM125,			// +/-1.25V
											PM1,			// +/-1V
											PM0625,			// +/-0.625V
											PM05,			// +/-0.5V
											PM03125,		// +/-0.3125V
											PM025,			// +/-0.25V
											PM0125,			// +/-0.125V
											PM01,			// +/-0.1V
											PM005,			// +/-0.05V
											PM0025,			// +/-0.025V
											PM00125,		// +/-0.0125V
											PM001,			// +/-0.01V
											P10,			// 0 ~ 10V
											P5,				// 0 ~ 5V
											P4095,			// 0 ~ 4.095V
											P25,			// 0 ~ 2.5V
											P125,			// 0 ~ 1.25V
											P1,				// 0 ~ 1V
											P05,			// 0 ~ 0.5V
											P025,			// 0 ~ 0.25V
											P01,			// 0 ~ 0.1V
											P005,			// 0 ~ 0.05V
											P0025,			// 0 ~ 0.025V
											P00125,			// 0 ~ 0.0125V
											P001,			// 0 ~ 0.01V
											P20MA,			// 0 ~ 20mA
											P4TO20MA,		// 4 ~ 20mA
											P1TO5 };		// 1 ~ 5V

// CAoSimpleDlg message handlers

BOOL CAoSimpleDlg::OnInitDialog()
{
	CDialogEx::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

	//----------------------------------------
	// Update the initial display of device name
	//----------------------------------------
	m_device_name.SetWindowTextW(_T("AIO000"));
	//----------------------------------------
	// Update the initial display of the channel No. for specified channel output
	//----------------------------------------
	m_single_ao_channel.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Update initial display of data for specified channel output
	//----------------------------------------
	m_single_ao_output_data.SetWindowTextW(_T("0.0"));
	//----------------------------------------
	// Updated initial display of number of channels for multi-channel output
	//----------------------------------------
	m_multi_ao_channels.SetWindowTextW(_T("1"));
	//----------------------------------------
	// Update initial display of data for multi-channel output
	//----------------------------------------
	m_multi_ao_ch0_output_data.SetWindowTextW(_T("0.0"));
	m_multi_ao_ch1_output_data.SetWindowTextW(_T("0.0"));
	m_multi_ao_ch2_output_data.SetWindowTextW(_T("0.0"));
	m_multi_ao_ch3_output_data.SetWindowTextW(_T("0.0"));
	//----------------------------------------
	// Create the combobox for analog output range
	//----------------------------------------
	m_ao_range.AddString(_T("-10 - +10V"));
	m_ao_range.AddString(_T("-5 - +5V"));
	m_ao_range.AddString(_T("-2.5 - +2.5V"));
	m_ao_range.AddString(_T("-1.25 - +1.25V"));
	m_ao_range.AddString(_T("-1 - +1V"));
	m_ao_range.AddString(_T("-0.625 - +0.625V"));
	m_ao_range.AddString(_T("-0.5 - +0.5V"));
	m_ao_range.AddString(_T("-0.3125 - +0.3125V"));
	m_ao_range.AddString(_T("-0.25 - +0.25V"));
	m_ao_range.AddString(_T("-0.125 - +0.125V"));
	m_ao_range.AddString(_T("-0.1 - +0.1V"));
	m_ao_range.AddString(_T("-0.05 - +0.05V"));
	m_ao_range.AddString(_T("-0.025 - +0.025V"));
	m_ao_range.AddString(_T("-0.0125 - +0.0125V"));
	m_ao_range.AddString(_T("-0.01 - +0.01V"));
	m_ao_range.AddString(_T("0 - +10V"));
	m_ao_range.AddString(_T("0 - +5V"));
	m_ao_range.AddString(_T("0 - +4.095V"));
	m_ao_range.AddString(_T("0 - +2.5V"));
	m_ao_range.AddString(_T("0 - +1.25V"));
	m_ao_range.AddString(_T("0 - +1V"));
	m_ao_range.AddString(_T("0 - +0.5V"));
	m_ao_range.AddString(_T("0 - +0.25V"));
	m_ao_range.AddString(_T("0 - +0.1V"));
	m_ao_range.AddString(_T("0 - +0.05V"));
	m_ao_range.AddString(_T("0 - +0.025V"));
	m_ao_range.AddString(_T("0 - +0.0125V"));
	m_ao_range.AddString(_T("0 - +0.01V"));
	m_ao_range.AddString(_T("0 - +20mA"));
	m_ao_range.AddString(_T("4 - +20mA"));
	m_ao_range.AddString(_T("1 - +5V"));
	//----------------------------------------
	// Change the selection of analog output range combobox to 0
	//----------------------------------------
	m_ao_range.SetCurSel(0);

	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 CAoSimpleDlg::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
	{
		CDialogEx::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CAoSimpleDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//================================================================================
// It is the process when the AioInit button is clicked
// Initialize the device
//================================================================================
void CAoSimpleDlg::OnBnClickedButtonAioInit()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	device_name[256];	// Device name
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Get device name from textbox
	//----------------------------------------
	m_device_name.GetWindowTextW(text_temp, _countof(text_temp));
	sprintf_s(device_name, "%S", text_temp);
	//----------------------------------------
	// Device initialization
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioInit(device_name, &g_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioInit = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioSetAoRangeAll button is clicked
// Set the output range of analog output
//================================================================================
void CAoSimpleDlg::OnBnClickedButtonSetAoRangeAll()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code
	short	index;				// Index of combobox

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// Get the index of range setting value from combobox
	//----------------------------------------
	index	= m_ao_range.GetCurSel();
	//----------------------------------------
	// Set the analog output range (all channels)
	//----------------------------------------
	ret	= AioSetAoRangeAll(g_id, g_ao_range[index]);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioSetAoRangeAll = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioSingleAoEx button is clicked
// Outputs the output data (voltage or current) once from the specified channel
//================================================================================
void CAoSimpleDlg::OnBnClickedButtonSingleAoEx()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code
	float	ao_data;			// Output data
	short	ao_channel;			// Channel No.

	//----------------------------------------
	// Get the channel No. from textbox
	//----------------------------------------
	m_single_ao_channel.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%hd"), &ao_channel);
	//----------------------------------------
	// Get the output data from textbox
	//----------------------------------------
	m_single_ao_output_data.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%f"), &ao_data);
	//----------------------------------------
	// Output the output data from the specified channel
	//----------------------------------------
	ret	= AioSingleAoEx(g_id, ao_channel, ao_data);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioSingleAoEx = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioMultiAoEx button is clicked
// Outputs the output data (voltage or current) once from the multi-channels
//================================================================================
void CAoSimpleDlg::OnBnClickedButtonMultiAoEx()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code
	float	*ao_data;			// Output data
	short	ao_channels;		// Number of channels

	//----------------------------------------
	// Get the number of channels from textbox
	//----------------------------------------
	m_multi_ao_channels.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%hd"), &ao_channels);
	//----------------------------------------
	// Allocate an array for storing DA conversion data
	//----------------------------------------
	ao_data	= (float *)malloc(sizeof(float) * ao_channels);
	//----------------------------------------
	// When failing to allocate the DA conversion data storage array
	//----------------------------------------
	if(ao_data == NULL){
		text_string.Format(_T("Failed to allocate DA conversion data storage array"));
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Initialize DA conversion data
	//----------------------------------------
	memset(ao_data, 0, sizeof(float) * ao_channels);
	//----------------------------------------
	// Get DA conversion data from textbox
	//----------------------------------------
	if(0 < ao_channels){
		m_multi_ao_ch0_output_data.GetWindowTextW(text_temp, _countof(text_temp));
		_stscanf_s(text_temp, _T("%f"), &ao_data[0]);
	}
	if(1 < ao_channels){
		m_multi_ao_ch1_output_data.GetWindowTextW(text_temp, _countof(text_temp));
		_stscanf_s(text_temp, _T("%f"), &ao_data[1]);
	}
	if(2 < ao_channels){
		m_multi_ao_ch2_output_data.GetWindowTextW(text_temp, _countof(text_temp));
		_stscanf_s(text_temp, _T("%f"), &ao_data[2]);
	}
	if(3 < ao_channels){
		m_multi_ao_ch3_output_data.GetWindowTextW(text_temp, _countof(text_temp));
		_stscanf_s(text_temp, _T("%f"), &ao_data[3]);
	}
	//----------------------------------------
	// Output the output data from the multi-channels
	//----------------------------------------
	ret	= AioMultiAoEx(g_id, ao_channels, ao_data);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioMultiAoEx = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
	//----------------------------------------
	// Release the array for storing DA conversion data
	//----------------------------------------
	free(ao_data);
}

//================================================================================
// It is the process when the AioExit button is clicked
// Perform the exit process of device
//================================================================================
void CAoSimpleDlg::OnBnClickedButtonAioExit()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	CString	text_string;		// For updating the textbox
	long	ret;				// For return code

	//----------------------------------------
	// Exit process
	//----------------------------------------
	ret	= AioExit(g_id);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioExit = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Close button is clicked
// Perform the exit process of device and terminate the application
//================================================================================
void CAoSimpleDlg::OnBnClickedClose()
{
	//----------------------------------------
	// Exit process
	//----------------------------------------
	AioExit(g_id);
	//----------------------------------------
	// Close dialog
	//----------------------------------------
	CDialog::OnOK();
}
