﻿
// FileOperationDlg.cpp : implementation file
//

#include "stdafx.h"
#include "FileOperation.h"
#include "FileOperationDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// CFileOperationDlg dialog



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

void CFileOperationDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEVICENAME, m_edit_device_name);
	DDX_Control(pDX, IDC_EDIT_RETURNNUM, m_edit_returnnum);
	DDX_Control(pDX, IDC_EDIT_STATUS, m_edit_applicationstatus);
	DDX_Control(pDX, IDC_MFCEDITBROWSE_SETTINGFILE, m_edit_settingfile);
	DDX_Control(pDX, IDC_MFCEDITBROWSE_ARBITRATYDATAFILE, m_edit_arbitrarydatafile);
	DDX_Control(pDX, IDC_COMBO_OPENMODE, m_combo_openmode);
	DDX_Control(pDX, IDC_COMBO_AOCHANNEL, m_combo_aochannel);
	DDX_Control(pDX, IDC_COMBO_WAVETYPE, m_combo_wavetype);
	DDX_Control(pDX, IDC_COMBO_PARAM, m_combo_param);
}

BEGIN_MESSAGE_MAP(CFileOperationDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_OPENAPPLICATION, &CFileOperationDlg::OnBnClickedButtonOpenapplication)
	ON_BN_CLICKED(IDC_BUTTON_SETDEVICENAME, &CFileOperationDlg::OnBnClickedButtonSetdevicename)
	ON_BN_CLICKED(IDC_BUTTON_EXPORTSETTINGFILE, &CFileOperationDlg::OnBnClickedButtonExportsettingfile)
	ON_BN_CLICKED(IDC_BUTTON_IMPORTSETTINGFILE, &CFileOperationDlg::OnBnClickedButtonImportsettingfile)
	ON_BN_CLICKED(IDC_BUTTON_SETWAVETYPE, &CFileOperationDlg::OnBnClickedButtonSetwavetype)
	ON_BN_CLICKED(IDC_BUTTON_SETARBITRARYDATAFILE, &CFileOperationDlg::OnBnClickedButtonSetarbitrarydatafile)
	ON_BN_CLICKED(IDC_BUTTON_GETARBITRARYDATAFILE, &CFileOperationDlg::OnBnClickedButtonGetarbitrarydatafile)
	ON_BN_CLICKED(IDC_BUTTON_SAVEDATAFILE, &CFileOperationDlg::OnBnClickedButtonSavedatafile)
	ON_BN_CLICKED(IDC_BUTTON_STARTOPERATION, &CFileOperationDlg::OnBnClickedButtonStartoperation)
	ON_BN_CLICKED(IDC_BUTTON_GETAPPLICATIONSTATUS, &CFileOperationDlg::OnBnClickedButtonGetapplicationstatus)
	ON_BN_CLICKED(IDC_BUTTON_STOPOPEREATION, &CFileOperationDlg::OnBnClickedButtonStopopereation)
	ON_BN_CLICKED(IDC_BUTTON_CLOSE, &CFileOperationDlg::OnBnClickedButtonClose)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &CFileOperationDlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()


//----------------------------------------
// Macro definition
//----------------------------------------
#define AO_CHANNEL							0		// Analog output channel number: 0
#define CWAVEFORMGENERATOR_CHANNELS			16		// Max channels

// CFileOperationDlg message handlers
//================================================================================
// Initial settings
//================================================================================
BOOL CFileOperationDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	CString		str_status;
	CString		text_temp;			// For temporary storage of text
	HRESULT		hr;

	//----------------------------------------
	// Initial processing to call C-WaveFormGenerator with SDK
	//----------------------------------------
	try
	{
		//----------------------------------------
		// Get the pointer of the already running C-WaveformGenerator
		//----------------------------------------
		hr = c_fgen.GetActiveObject(__uuidof(CCWaveformGenerator));
		//----------------------------------------
		// If acquisition of the startup application pointer fails, create a new SDK object.
		//----------------------------------------
		if ((FAILED(hr)))
		{
			//----------------------------------------
			// Generate a new object for SDK
			//----------------------------------------
			hr = c_fgen.CreateInstance(__uuidof(CCWaveformGenerator));
			if (FAILED(hr)) {
				_com_issue_error(hr);
			}
		}
	}
	//----------------------------------------
	// Display error message when creating SDK object fails
	//----------------------------------------
	catch (_com_error& /*ce*/)
	{
		str_status.Format(_T("Unanticipated exception initializing ICWaveformGeneratorPtr object."));
		m_edit_returnnum.SetWindowText(str_status);
		return FALSE;
	}
	//----------------------------------------
	// Display error message in case of other errors
	//----------------------------------------
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception initializing ICWaveformGeneratorPtr object."));
		EndDialog(IDABORT);
		return FALSE;
	}

	// 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_edit_device_name.SetWindowTextW(_T("AIO000"));
	//----------------------------------------
	// Create the combobox for screen size
	//----------------------------------------
	m_combo_openmode.AddString(_T("Minimize"));
	m_combo_openmode.AddString(_T("Normal"));
	m_combo_openmode.AddString(_T("Maximize"));
	//----------------------------------------
	// Change the selection of screen size combobox to 0
	//----------------------------------------
	m_combo_openmode.SetCurSel(0);
	//----------------------------------------
	// Create the combobox for channel
	//----------------------------------------
	for (int i = 0; i < CWAVEFORMGENERATOR_CHANNELS; i++) {
		text_temp.Format(_T("%d"), i);
		m_combo_aochannel.AddString(text_temp);
	}
	//----------------------------------------
	// Change the selection of channel combobox to 0
	//----------------------------------------
	m_combo_aochannel.SetCurSel(0);
	//----------------------------------------
	// Create the combobox for wave type
	//----------------------------------------
	m_combo_wavetype.AddString(_T("Sinewave"));
	m_combo_wavetype.AddString(_T("Constant"));
	m_combo_wavetype.AddString(_T("Triangular"));
	m_combo_wavetype.AddString(_T("Ramp"));
	m_combo_wavetype.AddString(_T("Square"));
	m_combo_wavetype.AddString(_T("Noise"));
	m_combo_wavetype.AddString(_T("Arbitrary"));
	//----------------------------------------
	// Change the selection of wave type combobox to 0
	//----------------------------------------
	m_combo_wavetype.SetCurSel(0);
	//----------------------------------------
	// Create the combobox for data save
	//----------------------------------------
	m_combo_param.AddString(_T("Single channel"));
	m_combo_param.AddString(_T("Multiple channels"));
	//----------------------------------------
	// Change the selection of data save combobox to 0
	//----------------------------------------
	m_combo_param.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 CFileOperationDlg::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 CFileOperationDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


//================================================================================
// It is the process when the OpenApplication button is clicked
// Initialize the application
//================================================================================
void CFileOperationDlg::OnBnClickedButtonOpenapplication()
{
	BSTR	device_name;		// Device name
	char	arg1[256];			// Reserve
	short	openmode;			// Screen size
	long	ret;				// For return code
	CString	text_temp;			// For temporary storage of text
	CString	text_string;		// For error code display

	//----------------------------------------
	// Get device name from textbox
	//----------------------------------------
	m_edit_device_name.GetWindowText(text_temp);
	device_name	= text_temp.AllocSysString();
	//----------------------------------------
	// Get screen size from textbox
	//----------------------------------------
	openmode	= m_combo_openmode.GetCurSel();
	//----------------------------------------
	// arg1 (Reserve)
	//----------------------------------------
	sprintf_s(arg1, "");
	//----------------------------------------
	// Perform application initialization process
	//----------------------------------------
	try
	{
		ret		= c_fgen->OpenApplication(openmode, device_name, arg1);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Free
	//----------------------------------------
	SysFreeString(device_name);
	//----------------------------------------
	// Display the error code
	//----------------------------------------
	text_string.Format(_T("OpenApplication = %d "), ret);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the SetDeviceName button is clicked
// Set the device name
//================================================================================
void CFileOperationDlg::OnBnClickedButtonSetdevicename()
{
	BSTR	device_name;		// Device name
	long	ret;				// For return code
	CString	text_temp;			// For temporary storage of text
	CString	text_string;		// For error code display

	//----------------------------------------
	// Get setting file name from textbox
	//----------------------------------------
	m_edit_device_name.GetWindowText(text_temp);
	device_name	= text_temp.AllocSysString();
	//----------------------------------------
	// Set device name
	//----------------------------------------
	try
	{	
		ret		= c_fgen->SetDeviceName(device_name);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Free
	//----------------------------------------
	SysFreeString(device_name);
	//----------------------------------------
	// Display the error code
	//----------------------------------------
	text_string.Format(_T("SetDeviceName = %d "), ret);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the ExportSettingFile button is clicked
// Save the settings to ini file
//================================================================================
void CFileOperationDlg::OnBnClickedButtonExportsettingfile()
{
	BSTR	filepath;			// File path
	short	arg1;				// Reserve
	long	ret;				// For return code
	CString	text_temp;			// For temporary storage of text
	CString	text_string;		// For error code display

	//----------------------------------------
	// Get setting file path from text box
	//----------------------------------------
	m_edit_settingfile.GetWindowText(text_temp);
	filepath	= text_temp.AllocSysString();
	//----------------------------------------
	// arg1 (Reserve)
	//----------------------------------------
	arg1		= 0;
	//----------------------------------------
	// Save the settings to ini file
	//----------------------------------------
	try
	{
		ret		= c_fgen->ExportSettingFile(filepath, arg1);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Free
	//----------------------------------------
	SysFreeString(filepath);
	//----------------------------------------
	// Display the error code
	//----------------------------------------
	text_string.Format(_T("ExportSettingFile = %d "), ret);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the ImportSettingFile button is clicked
// Load the settings from ini file.
//================================================================================
void CFileOperationDlg::OnBnClickedButtonImportsettingfile()
{
	BSTR	filepath;			// File path
	short	arg1;				// Reserve
	long	ret;				// For return code
	CString	text_temp;			// For temporary storage of text
	CString	text_string;		// For error code display

	//----------------------------------------
	// Get setting file path from text box
	//----------------------------------------
	m_edit_settingfile.GetWindowText(text_temp);
	filepath	= text_temp.AllocSysString();
	//----------------------------------------
	// arg1 (Reserve)
	//----------------------------------------
	arg1 = 0;
	//----------------------------------------
	// Load the settings from ini file.
	//----------------------------------------
	try
	{
		ret		= c_fgen->ImportSettingFile(filepath, arg1);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Free
	//----------------------------------------
	SysFreeString(filepath);
	//----------------------------------------
	// Display the error code
	//----------------------------------------
	text_string.Format(_T("ImportSettingFile = %d "), ret);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the SetWaveType button is clicked
// Set the waveform type
//================================================================================
void CFileOperationDlg::OnBnClickedButtonSetwavetype()
{
	short	wavetype;			// Waveform type
	short	aochannel;			// Channel
	short	arg1;				// Reserve
	long	ret;				// For return code
	CString	text_string;		// For error code display

	//----------------------------------------
	// Get the wave type from combobox
	//----------------------------------------
	wavetype	= m_combo_wavetype.GetCurSel();
	//----------------------------------------
	// Get the channel from combobox
	//----------------------------------------
	aochannel	= m_combo_aochannel.GetCurSel();
	//----------------------------------------
	// arg1 (Reserve)
	//----------------------------------------
	arg1 = 0;
	//----------------------------------------
	// Set the wave type
	//----------------------------------------
	try
	{
		ret		= c_fgen->SetWaveType(aochannel, wavetype, arg1);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Display the error code
	//----------------------------------------
	text_string.Format(_T("SetWaveType = %d "), ret);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the SetArbitraryDataFile button is clicked
// Set the data file (csv file) path
//================================================================================
void CFileOperationDlg::OnBnClickedButtonSetarbitrarydatafile()
{
	BSTR	filepath;			// File path
	short	aochannel;			// Channel
	short	arg1;				// Reserve
	long	ret;				// For return code
	CString	text_temp;			// For temporary storage of text
	CString	text_string;		// For error code display

	//----------------------------------------
	// Get data file path from text box
	//----------------------------------------
	m_edit_arbitrarydatafile.GetWindowText(text_temp);
	filepath	= text_temp.AllocSysString();
	//----------------------------------------
	// Get the channel from combobox
	//----------------------------------------
	aochannel	= m_combo_aochannel.GetCurSel();
	//----------------------------------------
	// arg1 (Reserve)
	//----------------------------------------
	arg1 = 0;
	//----------------------------------------
	// Set the data file path
	//----------------------------------------
	try
	{
		ret		= c_fgen->SetArbitraryDataFile(aochannel, filepath, arg1);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Free
	//----------------------------------------
	SysFreeString(filepath);
	//----------------------------------------
	// Display the error code
	//----------------------------------------
	text_string.Format(_T("SetArbitraryDataFile = %d "), ret);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GetArbitraryDataFile button is clicked
// Get the setting data file (csv file) path
//================================================================================
void CFileOperationDlg::OnBnClickedButtonGetarbitrarydatafile()
{
	BSTR	filepath;			// File path
	short	aochannel;			// Channel
	short	arg1;				// Reserve
	long	ret;				// For return code
	CString	text_temp;			// For temporary storage of text
	CString	text_string;		// For error code display

	arg1 = 0;
	filepath	= NULL;
	//----------------------------------------
	// Get the channel from combobox
	//----------------------------------------
	aochannel	= m_combo_aochannel.GetCurSel();
	//----------------------------------------
	// Get the data file path
	//----------------------------------------
	try
	{
		ret		= c_fgen->GetArbitraryDataFile(aochannel, &filepath, &arg1);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Display the error code
	//----------------------------------------
	text_string.Format(_T("GetArbitraryDataFile = %d "), ret);
	m_edit_returnnum.SetWindowText(text_string);
	//----------------------------------------
	// Display the data file path
	//----------------------------------------
	text_temp.Format(_T("%s"), filepath);
	m_edit_arbitrarydatafile.SetWindowText(text_temp);
	//----------------------------------------
	// Free
	//----------------------------------------
	SysFreeString(filepath);
}

//================================================================================
// It is the process when the SaveDataFile button is clicked
// Save the specified channel data to a csv file
//================================================================================
void CFileOperationDlg::OnBnClickedButtonSavedatafile()
{
	BSTR	filepath;			// File path
	short	aochannel;			// Channel
	short	param;				// Single or Multiple
	long	ret;				// For return code
	CString	text_temp;			// For temporary storage of text
	CString	text_string;		// For error code display

	//----------------------------------------
	// Get the data file name from text box
	//----------------------------------------
	m_edit_arbitrarydatafile.GetWindowText(text_temp);
	filepath	= text_temp.AllocSysString();
	//----------------------------------------
	// Get the channel parameters (single or multiple) from combo box
	//----------------------------------------
	param		= m_combo_param.GetCurSel();
	//----------------------------------------
	// Get the channel from combobox
	//----------------------------------------
	aochannel	= m_combo_aochannel.GetCurSel();
	//----------------------------------------
	// Data file settings
	//----------------------------------------
	//-------------------
	// For the single channel, set the third argument to the channel
	//-------------------
	if (param == 0) {
		try
		{
			ret	= c_fgen->SaveDataFile(filepath, param, aochannel);
		}
		catch (...)
		{
			AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
			return;
		}
	}
	//-------------------
	// For the multiple channels, set the third argument to 0
	//-------------------
	else {
		try
		{
			ret	= c_fgen->SaveDataFile(filepath, param, 0);
		}
		catch (...)
		{
			AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
			return;
		}
	}
	//----------------------------------------
	// Free
	//----------------------------------------
	SysFreeString(filepath);
	//----------------------------------------
	// Display the error code
	//----------------------------------------
	text_string.Format(_T("SaveDataFile = %d "), ret);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the StartOperation button is clicked
// Start the output of the application
//================================================================================
void CFileOperationDlg::OnBnClickedButtonStartoperation()
{
	short	arg1;				// Reserve
	long	ret;				// For return code
	CString	text_string;		// For error code display

	//----------------------------------------
	// arg1 (Reserve)
	//----------------------------------------
	arg1	= 0;
	//----------------------------------------
	// Start the output of the application
	//----------------------------------------
	try
	{
		ret	= c_fgen->StartOperation(arg1);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Display the error code
	//----------------------------------------
	text_string.Format(_T("StartOperation = %d "), ret);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GetApplicationStatus button is clicked
// Get the operating status
//================================================================================
void CFileOperationDlg::OnBnClickedButtonGetapplicationstatus()
{
	long	status = -1;		// Startus
	long	ret;				// For return code
	CString	text_temp;			// For temporary storage of text
	CString	text_string;		// For error code display

	//----------------------------------------
	// Get the operating status
	//----------------------------------------
	try
	{
		ret = c_fgen->GetApplicationStatus(&status);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Display the error code
	//----------------------------------------
	text_string.Format(_T("GetApplicationStatus = %d "), ret);
	m_edit_returnnum.SetWindowText(text_string);
	//----------------------------------------
	// Display the status
	//----------------------------------------
	if (status == 0) {
		text_temp.Format(_T("00H(Stopped)"));
	}
	else if (status == 1) {
		text_temp.Format(_T("01H(Running)"));
	}
	else if (status == 2) {
		text_temp.Format(_T("02H(Waiting for start trigger)"));
	}
	else if (status == 3) {
		text_temp.Format(_T("03H(Running, Waiting for start trigger)"));
	}
	else {
		text_temp.Format(_T("%d "), status);
	}
	m_edit_applicationstatus.SetWindowText(text_temp);
}

//================================================================================
// It is the process when the StopOperation button is clicked
// Stop the output of the application
//================================================================================
void CFileOperationDlg::OnBnClickedButtonStopopereation()
{
	short	arg1;				// Reserve
	long	ret;				// For return code
	CString	text_string;		// For error code display

	//----------------------------------------
	// arg1 (Reserve)
	//----------------------------------------
	arg1	= 0;
	//----------------------------------------
	// Stop the output of the application
	//----------------------------------------
	try
	{
		ret	= c_fgen->StopOperation(arg1);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Display the error code
	//----------------------------------------
	text_string.Format(_T("StopOperation = %d "), ret);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the CloseApplication button is clicked
// Close the application
//================================================================================
void CFileOperationDlg::OnBnClickedButtonClose()
{
	short	arg1;				// Reserve
	long	ret;				// For return code
	CString	text_string;		// For error code display

	//----------------------------------------
	// arg1 (Reserve)
	//----------------------------------------
	arg1	= 0;
	//----------------------------------------
	// Perform application termination process
	//----------------------------------------
	try
	{
		ret	= c_fgen->CloseApplication(arg1);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Display the error code
	//----------------------------------------
	text_string.Format(_T("CloseApplication = %d "), ret);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Exit button is clicked
// Terminate the application
//================================================================================
void CFileOperationDlg::OnBnClickedButtonExit()
{
	short	arg1;				// Reserve

	//----------------------------------------
	// arg1 (Reserve)
	//----------------------------------------
	arg1 = 0;
	//----------------------------------------
	// Perform application close process
	//----------------------------------------
	try
	{
		c_fgen->CloseApplication(arg1);
	}
	catch (...)
	{
	}
	//----------------------------------------
	// Close dialog
	//----------------------------------------
	CDialog::OnOK();
}
