﻿
// AiSimpleDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AiSimple.h"
#include "AiSimpleDlg.h"
#include "afxdialogex.h"
#include "Caio.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAiSimpleDlg dialog



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

void CAiSimpleDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEVICE_NAME, m_device_name);
	DDX_Control(pDX, IDC_COMBO_RANGE_VALUE, m_ai_range);
	DDX_Control(pDX, IDC_COMBO_INP_METHOD, m_ai_input_method);
	DDX_Control(pDX, IDC_EDIT_CH_NO, m_single_ai_channel);
	DDX_Control(pDX, IDC_EDIT_SINGLE_AI_DATA, m_single_ai_input_data);
	DDX_Control(pDX, IDC_EDIT_CH_NUM, m_multi_ai_channels);
	DDX_Control(pDX, IDC_LIST_MULTI_INP_DATA, m_multi_ai_input_data);
	DDX_Control(pDX, IDC_EDIT_RETURN_CODE, m_return_code);
}

BEGIN_MESSAGE_MAP(CAiSimpleDlg, CDialogEx)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_AIO_INIT, &CAiSimpleDlg::OnBnClickedButtonAioInit)
	ON_BN_CLICKED(IDC_BUTTON_SET_AI_RANGE_ALL, &CAiSimpleDlg::OnBnClickedButtonSetAiRangeAll)
	ON_BN_CLICKED(IDC_BUTTON_SET_AI_INP_METHOD, &CAiSimpleDlg::OnBnClickedButtonSetAiInpMethod)
	ON_BN_CLICKED(IDC_BUTTON_SINGLE_AI_EX, &CAiSimpleDlg::OnBnClickedButtonSingleAiEx)
	ON_BN_CLICKED(IDC_BUTTON_MULTI_AI_EX, &CAiSimpleDlg::OnBnClickedButtonMultiAiEx)
	ON_BN_CLICKED(IDC_BUTTON_AIO_EXIT, &CAiSimpleDlg::OnBnClickedButtonAioExit)
	ON_BN_CLICKED(IDCLOSE, &CAiSimpleDlg::OnBnClickedClose)
END_MESSAGE_MAP()


//----------------------------------------
// Macro definition
//----------------------------------------
#define MAX_AI_RANGE_NUM	32								// Maximum number of elements in analog input range array
//----------------------------------------
// Declaration of global variables
//----------------------------------------
short	g_id;												// ID for device access
short	g_ai_range_index;									// Index of range
short	g_ai_range[MAX_AI_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
											PM20MA,			// +/-20mA
											P1TO5 };		// 1 ~ 5V

// CAiSimpleDlg message handlers

BOOL CAiSimpleDlg::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 input
	//----------------------------------------
	m_single_ai_channel.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Update the initial display of number of channels for multi-channel input
	//----------------------------------------
	m_multi_ai_channels.SetWindowTextW(_T("1"));
	//----------------------------------------
	// Create the combobox for analog input range
	//----------------------------------------
	m_ai_range.AddString(_T("-10 - +10V"));
	m_ai_range.AddString(_T("-5 - +5V"));
	m_ai_range.AddString(_T("-2.5 - +2.5V"));
	m_ai_range.AddString(_T("-1.25 - +1.25V"));
	m_ai_range.AddString(_T("-1 - +1V"));
	m_ai_range.AddString(_T("-0.625 - +0.625V"));
	m_ai_range.AddString(_T("-0.5 - +0.5V"));
	m_ai_range.AddString(_T("-0.3125 - +0.3125V"));
	m_ai_range.AddString(_T("-0.25 - +0.25V"));
	m_ai_range.AddString(_T("-0.125 - +0.125V"));
	m_ai_range.AddString(_T("-0.1 - +0.1V"));
	m_ai_range.AddString(_T("-0.05 - +0.05V"));
	m_ai_range.AddString(_T("-0.025 - +0.025V"));
	m_ai_range.AddString(_T("-0.0125 - +0.0125V"));
	m_ai_range.AddString(_T("-0.01 - +0.01V"));
	m_ai_range.AddString(_T("0 - +10V"));
	m_ai_range.AddString(_T("0 - +5V"));
	m_ai_range.AddString(_T("0 - +4.095V"));
	m_ai_range.AddString(_T("0 - +2.5V"));
	m_ai_range.AddString(_T("0 - +1.25V"));
	m_ai_range.AddString(_T("0 - +1V"));
	m_ai_range.AddString(_T("0 - +0.5V"));
	m_ai_range.AddString(_T("0 - +0.25V"));
	m_ai_range.AddString(_T("0 - +0.1V"));
	m_ai_range.AddString(_T("0 - +0.05V"));
	m_ai_range.AddString(_T("0 - +0.025V"));
	m_ai_range.AddString(_T("0 - +0.0125V"));
	m_ai_range.AddString(_T("0 - +0.01V"));
	m_ai_range.AddString(_T("0 - +20mA"));
	m_ai_range.AddString(_T("4 - +20mA"));
	m_ai_range.AddString(_T("-20 - +20mA"));
	m_ai_range.AddString(_T("1 - +5V"));
	//----------------------------------------
	// Change the selection of analog input range combobox to 0
	//----------------------------------------
	m_ai_range.SetCurSel(0);
	//----------------------------------------
	// Create the combobox for analog input method
	//----------------------------------------
	m_ai_input_method.AddString(_T("Single-end input"));
	m_ai_input_method.AddString(_T("Differential input"));
	//----------------------------------------
	// Change the selection of analog input method combobox to 0
	//----------------------------------------
	m_ai_input_method.SetCurSel(0);
	//----------------------------------------
	// Initialize global variables
	//----------------------------------------
	g_id				= 0;
	g_ai_range_index	= 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 CAiSimpleDlg::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 CAiSimpleDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//================================================================================
// It is the process when the AioInit button is clicked
// Initialize the device
//================================================================================
void CAiSimpleDlg::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 AioSetAiRangeAll button is clicked
// Set the input range of analog input
//================================================================================
void CAiSimpleDlg::OnBnClickedButtonSetAiRangeAll()
{
	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

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

//================================================================================
// It is the process when the AioSetAiInputMethod button is clicked
// Set the input method of analog input
//================================================================================
void CAiSimpleDlg::OnBnClickedButtonSetAiInpMethod()
{
	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	input_method;		// Input method

	//----------------------------------------
	// Update GUI information
	//----------------------------------------
	UpdateData(TRUE);
	//----------------------------------------
	// Get the index of input method setting value from combobox
	//----------------------------------------
	input_method	= m_ai_input_method.GetCurSel();
	//----------------------------------------
	// Set the analog input method
	//----------------------------------------
	ret	= AioSetAiInputMethod(g_id, input_method);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	AioGetErrorString(ret, error_string);
	//----------------------------------------
	// Display message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioSetAiInputMethod = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioSingleAiEx button is clicked
// Inputs the input data (voltage or current) once from the specified channel
//================================================================================
void CAiSimpleDlg::OnBnClickedButtonSingleAiEx()
{
	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	ai_data;			// Input data
	short	ai_channel;			// Channel No.

	//----------------------------------------
	// Get the channel No. from textbox
	//----------------------------------------
	m_single_ai_channel.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%hd"), &ai_channel);
	//----------------------------------------
	// Get the AD conversion data of specified channel
	//----------------------------------------
	ret	= AioSingleAiEx(g_id, ai_channel, &ai_data);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Get error string
	//--------------------
	AioGetErrorString(ret, error_string);
	//--------------------
	// Display message
	//--------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioSingleAiEx = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		return;
	}
	//----------------------------------------
	// Display the AD conversion data of specified channel
	//----------------------------------------
	if((g_ai_range[g_ai_range_index] == P20MA) || (g_ai_range[g_ai_range_index] == P4TO20MA) || (g_ai_range[g_ai_range_index] == PM20MA)){
		text_string.Format(_T("%2.5fmA"), ai_data);
	}else{
		text_string.Format(_T("%2.5fV"), ai_data);
	}
	//--------------------
	// Update conversion data display
	//--------------------
	m_single_ai_input_data.SetWindowText(text_string);
}

//================================================================================
// It is the process when the AioMultiAiEx button is clicked
// Inputs the input data (voltage or current) once from the multi-channels
//================================================================================
void CAiSimpleDlg::OnBnClickedButtonMultiAiEx()
{
	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	*ai_data;			// Input data
	short	i;					// For looping the input data display
	short	ai_channels;		// Number of channels

	//----------------------------------------
	// Get the number of channels from textbox
	//----------------------------------------
	m_multi_ai_channels.GetWindowTextW(text_temp, _countof(text_temp));
	_stscanf_s(text_temp, _T("%hd"), &ai_channels);
	//----------------------------------------
	// Allocate an array for storing AD conversion data
	//----------------------------------------
	ai_data	= (float *)malloc(sizeof(float) * ai_channels);
	//----------------------------------------
	// When failing to allocate the AD conversion data storage array
	//----------------------------------------
	if(ai_data == NULL){
		text_string.Format(_T("Failed to allocate AD conversion data storage array"));
		m_return_code.SetWindowText(text_string);
		return;
	}
	//----------------------------------------
	// Get the AD conversion data of multi-channels
	//----------------------------------------
	ret	= AioMultiAiEx(g_id, ai_channels, ai_data);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Get error string
	//--------------------
	AioGetErrorString(ret, error_string);
	//--------------------
	// Display message
	//--------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("AioMultiAiEx = %d : %s"), ret, text_temp);
	m_return_code.SetWindowText(text_string);
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		//--------------------
		// Release the array for storing AD conversion data
		//--------------------
		free(ai_data);
		return;
	}
	//----------------------------------------
	// Display AD conversion data
	//----------------------------------------
	//--------------------
	// Clear the listbox for AD conversion data display
	//--------------------
	m_multi_ai_input_data.ResetContent();
	//--------------------
	// Repeat the process up to the set number of channels
	//--------------------
	for(i = 0; i < ai_channels; i++){
		//--------------------
		// Generate display data
		//--------------------
		if((g_ai_range[g_ai_range_index] == P20MA) || (g_ai_range[g_ai_range_index] == P4TO20MA) || (g_ai_range[g_ai_range_index] == PM20MA)){
			text_string.Format(_T("%3d                        %2.5fmA"), i, ai_data[i]);
		}else{
			text_string.Format(_T("%3d                        %2.5fV"), i, ai_data[i]);
		}
		//--------------------
		// Update conversion data display
		//--------------------
		m_multi_ai_input_data.AddString(text_string);
	}
	//----------------------------------------
	// Release the array for storing AD conversion data
	//----------------------------------------
	free(ai_data);
}

//================================================================================
// It is the process when the AioExit button is clicked
// Perform the exit process of device
//================================================================================
void CAiSimpleDlg::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 CAiSimpleDlg::OnBnClickedClose()
{
	//----------------------------------------
	// Exit process
	//----------------------------------------
	AioExit(g_id);
	//----------------------------------------
	// Close dialog
	//----------------------------------------
	CDialog::OnOK();
}
