﻿// ConditionDlg.cpp : implementation file
//

#include "stdafx.h"
#include "AoGeneratingInfiniteUser.h"
#include "ConditionDlg.h"
#include "afxdialogex.h"
#include "Caio.h"


// CConditionDlg dialog

IMPLEMENT_DYNAMIC(CConditionDlg, CDialogEx)

CConditionDlg::CConditionDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_CONDITION_DIALOG, pParent)
{

}

CConditionDlg::~CConditionDlg()
{
}

void CConditionDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_COMBO_START_CONDITION, m_ao_start_condition);
	DDX_Control(pDX, IDC_COMBO_CLOCK_TYPE, m_ao_clock_type);
	DDX_Control(pDX, IDC_EDIT_GENERATING_CLOCK, m_ao_generating_clock);
	DDX_Control(pDX, IDC_COMBO_EXTERNAL_CLOCK_INPUT_TIMING, m_ao_clock_edge);
	DDX_Control(pDX, IDC_COMBO_STOP_CONDITION, m_ao_stop_condition);
	DDX_Control(pDX, IDC_EDIT_TRANSFER_SIZE, m_ao_transfer_size);
}


BEGIN_MESSAGE_MAP(CConditionDlg, CDialogEx)
	ON_CBN_SELCHANGE(IDC_COMBO_CLOCK_TYPE, &CConditionDlg::OnCbnSelchangeComboClockType)
	ON_BN_CLICKED(IDC_BUTTON_OK, &CConditionDlg::OnBnClickedButtonOk)
	ON_BN_CLICKED(IDC_BUTTON_CANCEL, &CConditionDlg::OnBnClickedButtonCancel)
END_MESSAGE_MAP()

//----------------------------------------
// Declaration of global variables
//----------------------------------------
extern	short	g_id;						// ID for device access
short	g_start_trigger[] = {	0,			// Software
								1,			// External trigger rising edge
								2 };		// External trigger falling edge
short	g_clock_type[] = {		0,			// Internal clock
								1 };		// External clock
short	g_clock_edge[] = {		0,			// External input falling edge
								1 };		// External input rising edge
short	g_stop_trigger[] = {	1,			// External trigger rising edge
								2 };		// External trigger falling edge

// CConditionDlg message handlers


BOOL CConditionDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	TCHAR	text_temp[256];			// For temporary storage of text
	char	error_string[256];		// Error code string
	CString	text_string;			// For updating the textbox
	CString	message_string;			// For displaying message
	long	ret;					// For return code
	short	index;					// Index of combobox
	short	ao_start_trigger;		// Conversion start condition
	short	ao_stop_trigger;		// Conversion stop condition
	short	ao_clock_type;			// Clock type
	float	ao_generating_clock;	// Generating clock
	short	ao_clock_edge;			// Timing of the external clock input
	short	ao_auto_mode;			// Set mode of transfer size
	long	ao_transfer_size;		// Transfer size
	short	err_flag;				// Function execution error flag

	//----------------------------------------
	// Initialize parameters
	//----------------------------------------
	err_flag	= 0;
	//----------------------------------------
	// Create the combobox for analog output conversion start condition
	//----------------------------------------
	m_ao_start_condition.AddString(_T("Software"));
	m_ao_start_condition.AddString(_T("External trigger rising edge"));
	m_ao_start_condition.AddString(_T("External trigger falling edge"));
	//----------------------------------------
	// Get the analog output conversion start condition
	//----------------------------------------
	ret	= AioGetAoStartTrigger(g_id, &ao_start_trigger);
	//----------------------------------------
	// Abnormal termination process
	//----------------------------------------
	if(ret != 0){
		if(ret != 20001){
			//--------------------
			// Get error string
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// Generate error message
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			message_string.Format(_T("AioGetAoStartTrigger = %d : %s\n\n*The display could not be updated since an error occurred."), ret, text_temp);
			err_flag	= 1;
		}else{
			//--------------------
			// Disable control
			//--------------------
			m_ao_start_condition.EnableWindow(FALSE);
		}
		//--------------------
		// Change the selection of analog output conversion start condition combobox to 0
		//--------------------
		m_ao_start_condition.SetCurSel(0);
	//----------------------------------------
	// Normal termination process
	//----------------------------------------
	}else{
		//--------------------
		// Convert from conversion start condition to index
		//--------------------
		index	= 0;
		switch(ao_start_trigger){
		case 0:
			index	= 0;
			break;
		case 1:
			index	= 1;
			break;
		case 2:
			index	= 2;
			break;
		default:
			index	= 0;
			break;
		}
		//--------------------
		// Change the selection of the analog output conversion start condition combobox
		//--------------------
		m_ao_start_condition.SetCurSel(index);
	}
	//----------------------------------------
	// Create the combobox for analog output clock type
	//----------------------------------------
	m_ao_clock_type.AddString(_T("Internal clock"));
	m_ao_clock_type.AddString(_T("External clock"));
	//----------------------------------------
	// Get clock type
	//----------------------------------------
	ret	= AioGetAoClockType(g_id, &ao_clock_type);
	//----------------------------------------
	// Abnormal termination process
	//----------------------------------------
	if(ret != 0){
		if(ret != 20001){
			//--------------------
			// Get error string
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// Generate error message
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			message_string.Format(_T("AioGetAoClockType = %d : %s\n\n*The display could not be updated since an error occurred."), ret, text_temp);
			err_flag	= 1;
		}else{
			//--------------------
			// Disable control
			//--------------------
			m_ao_clock_type.EnableWindow(FALSE);
		}
		//--------------------
		// Change the selection of the analog output clock type combobox to 0
		//--------------------
		m_ao_clock_type.SetCurSel(0);
		OnCbnSelchangeComboClockType();
	//----------------------------------------
	// Normal termination process
	//----------------------------------------
	}else{
		//--------------------
		// Convert from clock type to index
		//--------------------
		index	= 0;
		switch(ao_clock_type){
		case 0:
			index	= 0;
			break;
		case 1:
			index	= 1;
			break;
		default:
			index	= 0;
			break;
		}
		//--------------------
		// Change the selection of the analog output clock type combobox
		//--------------------
		m_ao_clock_type.SetCurSel(index);
		OnCbnSelchangeComboClockType();
	}
	//----------------------------------------
	// Get generating clock
	//----------------------------------------
	ret	= AioGetAoSamplingClock(g_id, &ao_generating_clock);
	//----------------------------------------
	// Abnormal termination process
	//----------------------------------------
	if(ret != 0){
		if(ret != 20001){
			//--------------------
			// Get error string
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// Generate error message
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			message_string.Format(_T("AioGetAoSamplingClock = %d : %s\n\n*The display could not be updated since an error occurred."), ret, text_temp);
			err_flag	= 1;
		}else{
			//--------------------
			// Disable control
			//--------------------
			m_ao_generating_clock.EnableWindow(FALSE);
		}
		//--------------------
		// Update the initial display of generating clock
		//--------------------
		m_ao_generating_clock.SetWindowTextW(_T("1000"));
	//----------------------------------------
	// Normal termination process
	//----------------------------------------
	}else{
		//--------------------
		// Update generating clock
		//--------------------
		text_string.Format(_T("%f"), ao_generating_clock);
		m_ao_generating_clock.SetWindowTextW(text_string);
	}
	//----------------------------------------
	// Create the combobox for analog output external clock input timing
	//----------------------------------------
	m_ao_clock_edge.AddString(_T("External input falling edge"));
	m_ao_clock_edge.AddString(_T("External input rising edge"));
	//----------------------------------------
	// For External clock
	//----------------------------------------
	if(ao_clock_type == 1){
		//----------------------------------------
		// Get the timing of external clock input
		//----------------------------------------
		ret	= AioGetAoClockEdge(g_id, &ao_clock_edge);
		//----------------------------------------
		// Abnormal termination process
		//----------------------------------------
		if(ret != 0){
			if(ret != 20001){
				//--------------------
				// Get error string
				//--------------------
				AioGetErrorString(ret, error_string);
				//--------------------
				// Generate error message
				//--------------------
				wsprintf(text_temp, _T("%S"), error_string);
				message_string.Format(_T("AioGetAoClockEdge = %d : %s\n\n*The display could not be updated since an error occurred."), ret, text_temp);
				err_flag	= 1;
			}else{
				//--------------------
				// Disable control
				//--------------------
				m_ao_clock_edge.EnableWindow(FALSE);
			}
			//----------------------------------------
			// Change the selection of the external clock input timing combobox to 0
			//----------------------------------------
			m_ao_clock_edge.SetCurSel(0);
		//----------------------------------------
		// Normal termination process
		//----------------------------------------
		}else{
			//--------------------
			// Convert from input timing to index
			//--------------------
			index	= 0;
			switch(ao_clock_edge){
			case 0:
				index	= 0;
				break;
			case 1:
				index	= 1;
				break;
			default:
				index	= 0;
				break;
			}
			//----------------------------------------
			// Change the selection of the external clock input timing combobox
			//----------------------------------------
			m_ao_clock_edge.SetCurSel(index);
		}
	//----------------------------------------
	// Other than External clock
	//----------------------------------------
	}else{
		//----------------------------------------
		// Change the selection of the external clock input timing combobox to 0
		//----------------------------------------
		m_ao_clock_edge.SetCurSel(0);
	}
	//----------------------------------------
	// Get transfer size
	//----------------------------------------
	ret	= AioGetAoTransferSize(g_id, &ao_auto_mode, &ao_transfer_size);
	//----------------------------------------
	// Abnormal termination process
	//----------------------------------------
	if(ret != 0){
		if(ret != 20001){
			//--------------------
			// Get error string
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// Generate error message
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			message_string.Format(_T("AioGetAoTransferSize = %d : %s\n\n*The display could not be updated since an error occurred."), ret, text_temp);
			err_flag	= 1;
		}else{
			//--------------------
			// Disable control
			//--------------------
			m_ao_transfer_size.EnableWindow(FALSE);
		}
		//--------------------
		// Update the initial display of transfer size
		//--------------------
		m_ao_transfer_size.SetWindowTextW(_T("0"));
	//----------------------------------------
	// Normal termination process
	//----------------------------------------
	}else{
		//--------------------
		// Update transfer size
		//--------------------
		text_string.Format(_T("%ld"), ao_transfer_size);
		m_ao_transfer_size.SetWindowTextW(text_string);
	}
	//----------------------------------------
	// Create the combobox for analog output conversion stop condition
	//----------------------------------------
	m_ao_stop_condition.AddString(_T("External trigger rising edge"));
	m_ao_stop_condition.AddString(_T("External trigger falling edge"));
	//----------------------------------------
	// Get the analog output conversion stop condition
	//----------------------------------------
	ret	= AioGetAoStopTrigger(g_id, &ao_stop_trigger);
	//----------------------------------------
	// Abnormal termination process
	//----------------------------------------
	if(ret != 0){
		if(ret != 20001){
			//--------------------
			// Get error string
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// Generate error message
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			message_string.Format(_T("AioGetAoStopTrigger = %d : %s\n\n*The display could not be updated since an error occurred."), ret, text_temp);
			err_flag	= 1;
		}else{
			//--------------------
			// Disable control
			//--------------------
			m_ao_stop_condition.EnableWindow(FALSE);
		}
		//--------------------
		// Change the selection in the analog output conversion stop condition combobox to 0
		//--------------------
		m_ao_stop_condition.SetCurSel(0);
	//----------------------------------------
	// Normal termination process
	//----------------------------------------
	}else{
		//--------------------
		// Convert from conversion stop condition to index
		//--------------------
		index	= 0;
		switch(ao_stop_trigger){
		case 0:
			index	= 0;
			break;
		case 1:
			index	= 0;
			break;
		case 2:
			index	= 1;
			break;
		default:
			index	= 0;
			break;
		}
		//--------------------
		// Change the selection in the analog output conversion stop condition combobox
		//--------------------
		m_ao_stop_condition.SetCurSel(index);
	}
	//----------------------------------------
	// Display message if an error occurs
	//----------------------------------------
	if(err_flag != 0){
		MessageBox(message_string, _T("Error"), MB_OK);
	}
	return TRUE;  // return TRUE unless you set the focus to a control
				  // EXCEPTION: OCX Property Pages should return FALSE
}

//================================================================================
// It is the process when the Clock Type combobox is changed
// Update available controls
//================================================================================
void CConditionDlg::OnCbnSelchangeComboClockType()
{
	short	index;				// Index of combobox
	long	ret;
	short	auto_mode;
	long	transfer_size;

	//----------------------------------------
	// Get the index of clock type from combobox
	//----------------------------------------
	index	= m_ao_clock_type.GetCurSel();
	//----------------------------------------
	// Branch processing by clock type
	//----------------------------------------
	switch(g_clock_type[index]){
	case 0:		// Internal clock
		m_ao_generating_clock.EnableWindow(TRUE);
		m_ao_clock_edge.EnableWindow(FALSE);
		m_ao_transfer_size.EnableWindow(FALSE);
		break;
	case 1:		// External clock
		m_ao_generating_clock.EnableWindow(FALSE);
		m_ao_clock_edge.EnableWindow(TRUE);
		//----------------------------------------
		// Get transfer size
		//----------------------------------------
		ret	= AioGetAoTransferSize(g_id, &auto_mode, &transfer_size);
		//----------------------------------------
		// Abnormal termination process
		//----------------------------------------
		if(ret != 0){
			m_ao_transfer_size.EnableWindow(FALSE);
		//----------------------------------------
		// Normal termination process
		//----------------------------------------
		}else{
			m_ao_transfer_size.EnableWindow(TRUE);
		}
		break;
	default:
		m_ao_generating_clock.EnableWindow(TRUE);
		m_ao_clock_edge.EnableWindow(FALSE);
		m_ao_transfer_size.EnableWindow(FALSE);
		break;
	}
}

//================================================================================
// It is the process when the OK button is clicked
// Set generating condition
//================================================================================
void CConditionDlg::OnBnClickedButtonOk()
{
	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
	float	ao_generating_clock;	// Generating clock
	long	ao_transfer_size;		// Transfer size

	//----------------------------------------
	// Set the analog output conversion data transfer mode to user buffer mode
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAoTransferMode(g_id, 1);
	//----------------------------------------
	// Error process
	//----------------------------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("AioSetAoTransferMode = %d : %s"), ret, text_temp);
		MessageBox(text_string, _T("Error"), MB_OK);
		return;
	}
	//----------------------------------------
	// Set the data storage memory type to Repeat output
	// Caution for modify this function (if modified, the application may not work)
	//----------------------------------------
	ret	= AioSetAoMemoryType(g_id, 1);
	//----------------------------------------
	// Error process
	//----------------------------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("AioSetAoMemoryType = %d : %s"), ret, text_temp);
		MessageBox(text_string, _T("Error"), MB_OK);
		return;
	}
	//----------------------------------------
	// Get the index of the conversion start condition setting value from combobox
	//----------------------------------------
	index	= m_ao_start_condition.GetCurSel();
	//----------------------------------------
	// Set analog output conversion start condition
	//----------------------------------------
	ret	= AioSetAoStartTrigger(g_id, g_start_trigger[index]);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("AioSetAoStartTrigger = %d : %s"), ret, text_temp);
		MessageBox(text_string, _T("Error"), MB_OK);
		return;
	}
	//----------------------------------------
	// Get the index of the clock type setting value from combobox
	//----------------------------------------
	index	= m_ao_clock_type.GetCurSel();
	//----------------------------------------
	// Set analog output clock type
	//----------------------------------------
	ret	= AioSetAoClockType(g_id, g_clock_type[index]);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("AioSetAoClockType = %d : %s"), ret, text_temp);
		MessageBox(text_string, _T("Error"), MB_OK);
		return;
	}
	//----------------------------------------
	// For Internal clock
	//----------------------------------------
	if(g_clock_type[index] == 0){
		//----------------------------------------
		// Get the generating clock from textbox
		//----------------------------------------
		m_ao_generating_clock.GetWindowTextW(text_temp, _countof(text_temp));
		_stscanf_s(text_temp, _T("%f"), &ao_generating_clock);
		//----------------------------------------
		// Set analog output generating clock
		//----------------------------------------
		ret	= AioSetAoSamplingClock(g_id, ao_generating_clock);
		//----------------------------------------
		// Error process
		//----------------------------------------
		//--------------------
		// Exit the process if not completed successfully
		//--------------------
		if(ret != 0){
			//--------------------
			// Get error string
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// Display message
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("AioSetAoSamplingClock = %d : %s"), ret, text_temp);
			MessageBox(text_string, _T("Error"), MB_OK);
			return;
		}
		//----------------------------------------
		// Set analog output transfer size
		//----------------------------------------
		ret	= AioSetAoTransferSize(g_id, AO_TRANSFERSIZE_AUTO, 0);
		//----------------------------------------
		// Error process
		//----------------------------------------
		//--------------------
		// Exit the process if not completed successfully
		//--------------------
		if((ret != 0) && (ret != 20001)){
			//--------------------
			// Get error string
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// Display message
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("AioSetAoTransferSize = %d : %s"), ret, text_temp);
			MessageBox(text_string, _T("Error"), MB_OK);
			return;
		}
	//----------------------------------------
	// For External clock
	//----------------------------------------
	}else if(g_clock_type[index] == 1){
		//----------------------------------------
		// Get the index of the external clock input timing setting value from combobox
		//----------------------------------------
		index	= m_ao_clock_edge.GetCurSel();
		//----------------------------------------
		// Set the timing of external clock input
		//----------------------------------------
		ret	= AioSetAoClockEdge(g_id, g_clock_edge[index]);
		//----------------------------------------
		// Error process
		//----------------------------------------
		//--------------------
		// Exit the process if not completed successfully
		//--------------------
		if((ret != 0) && (ret != 20001)){
			//--------------------
			// Get error string
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// Display message
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("AioSetAoClockEdge = %d : %s"), ret, text_temp);
			MessageBox(text_string, _T("Error"), MB_OK);
			return;
		}
		//----------------------------------------
		// Get the generating clock from textbox
		//----------------------------------------
		m_ao_transfer_size.GetWindowTextW(text_temp, _countof(text_temp));
		_stscanf_s(text_temp, _T("%ld"), &ao_transfer_size);
		//----------------------------------------
		// Set analog output transfer size
		//----------------------------------------
		ret	= AioSetAoTransferSize(g_id, AO_TRANSFERSIZE_MANUAL, ao_transfer_size);
		//----------------------------------------
		// Error process
		//----------------------------------------
		//--------------------
		// Exit the process if not completed successfully
		//--------------------
		if((ret != 0) && (ret != 20001)){
			//--------------------
			// Get error string
			//--------------------
			AioGetErrorString(ret, error_string);
			//--------------------
			// Display message
			//--------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("AioSetAoTransferSize = %d : %s"), ret, text_temp);
			MessageBox(text_string, _T("Error"), MB_OK);
			return;
		}
	}
	//----------------------------------------
	// Get the index of the conversion stop condition setting value from combobox
	//----------------------------------------
	index	= m_ao_stop_condition.GetCurSel();
	//----------------------------------------
	// Set analog output conversion stop condition
	//----------------------------------------
	ret	= AioSetAoStopTrigger(g_id, g_stop_trigger[index]);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("AioSetAoStopTrigger  = %d : %s"), ret, text_temp);
		MessageBox(text_string, _T("Error"), MB_OK);
		return;
	}
	//----------------------------------------
	// Repeat times: Infinite
	//----------------------------------------
	ret	= AioSetAoRepeatTimes(g_id, 0);
	//----------------------------------------
	// Error process
	//----------------------------------------
	//--------------------
	// Exit the process if not completed successfully
	//--------------------
	if(ret != 0){
		//--------------------
		// Get error string
		//--------------------
		AioGetErrorString(ret, error_string);
		//--------------------
		// Display message
		//--------------------
		wsprintf(text_temp, _T("%S"), error_string);
		text_string.Format(_T("AioSetAoRepeatTimes  = %d : %s"), ret, text_temp);
		MessageBox(text_string, _T("Error"), MB_OK);
		return;
	}
	//----------------------------------------
	// OK process
	//----------------------------------------
	CDialogEx::OnOK();
}

//================================================================================
// It is the process when the Close button is clicked
// Terminate the dialog
//================================================================================
void CConditionDlg::OnBnClickedButtonCancel()
{
	//----------------------------------------
	// Cancel process
	//----------------------------------------
	CDialogEx::OnCancel();
}
