
// MultiChannelWaveDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MultiChannelWave.h"
#include "MultiChannelWaveDlg.h"
#include "ChannelInfoDlg.h"
#include "DeviceInfoDlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CMultiChannelWaveDlg dialog



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

void CMultiChannelWaveDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_DEVICENAME, m_edit_device_name);
	DDX_Control(pDX, IDC_EDIT_STATUS, m_edit_applicationstatus);
	DDX_Control(pDX, IDC_EDIT_RETURNNUM, m_edit_returnnum);
	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);
}

BEGIN_MESSAGE_MAP(CMultiChannelWaveDlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_OPENAPPLICATION, &CMultiChannelWaveDlg::OnBnClickedButtonOpenapplication)
	ON_BN_CLICKED(IDC_BUTTON_DEVICESETTING, &CMultiChannelWaveDlg::OnBnClickedButtonDevicesetting)
	ON_BN_CLICKED(IDC_BUTTON_CHANNELSETTING, &CMultiChannelWaveDlg::OnBnClickedButtonChannelsetting)
	ON_BN_CLICKED(IDC_BUTTON_SETWAVETYPE, &CMultiChannelWaveDlg::OnBnClickedButtonSetwavetype)
	ON_BN_CLICKED(IDC_BUTTON_STARTOPERATION, &CMultiChannelWaveDlg::OnBnClickedButtonStartoperation)
	ON_BN_CLICKED(IDC_BUTTON_GETAPPLICATIONSTATUS, &CMultiChannelWaveDlg::OnBnClickedButtonGetapplicationstatus)
	ON_BN_CLICKED(IDC_BUTTON_STOPOPEREATION, &CMultiChannelWaveDlg::OnBnClickedButtonStopopereation)
	ON_BN_CLICKED(IDC_BUTTON_CLOSEAPPLICATION, &CMultiChannelWaveDlg::OnBnClickedButtonCloseapplication)
	ON_BN_CLICKED(IDC_BUTTON_EXIT, &CMultiChannelWaveDlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()


// CMultiChannelWaveDlg message handlers

BOOL CMultiChannelWaveDlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();
	CString		text_string;
	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*/)
	{
		text_string.Format(_T("Unanticipated exception initializing ICWaveformGeneratorPtr object."));
		m_edit_returnnum.SetWindowText(text_string);
		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 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"));		//For a sample of how to read a data file, see the file sample.
	//----------------------------------------
	// Change the selection of wave type combobox to 0
	//----------------------------------------
	m_combo_wavetype.SetCurSel(0);
	//----------------------------------------
	// Create the combobox for channel
	// Upper limit number of channels for C-WaveformGenerator
	//----------------------------------------
	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);

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

//================================================================================
// It is the process when the OpenApplication button is clicked
// Initialize the application
//================================================================================
void CMultiChannelWaveDlg::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 Device settings button is clicked
// Open the device settings dialog
//================================================================================
void CMultiChannelWaveDlg::OnBnClickedButtonDevicesetting()
{
	DeviceInfoDlg	deviceinfodlg;
	short			aochannels = 1;			// Number of channel
	float			frequency = 1;			// Frequency
	short			unit_window = 0;		// Selected index of frequency unit combobox
	short			frequency_type = 0;		// Frequency or Period
	short			frequency_unit = 0;		// Hz(sec) or kHz(msec)
	short			oscilation_type = 0;	// Oscilation type 
	short			oscilation_start = 1;	// Number of oscilation waveform
	float			oscilation_stop = 1;	// Number of oscilation stop waveform or Time of oscillation stop
	short			arg1 = 0;				// Reserve
	long			ret;					// For return code
	CString			text_string;			// For error code display

	//----------------------------------------
	// Leave the return code blank
	//----------------------------------------
	text_string.Format(_T(""));
	m_edit_returnnum.SetWindowText(text_string);
	//----------------------------------------
	// Get the device information
	//----------------------------------------
	try
	{
		ret				= c_fgen->GetDeviceSetting(&aochannels, &frequency, &frequency_type, &frequency_unit, &arg1);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	unit_window			= (frequency_type << 1) + frequency_unit;
	//----------------------------------------
	// Get the oscilation
	//----------------------------------------
	try
	{
		ret				= c_fgen->GetOscilation(&oscilation_type, &oscilation_start, &oscilation_stop, &arg1);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Go to device settings dialog
	//----------------------------------------
	deviceinfodlg.devinfo_data(&aochannels, &frequency, &unit_window, &oscilation_type, &oscilation_start, &oscilation_stop);
	//----------------------------------------
	// If OK is pressed, process
	//----------------------------------------
	if (deviceinfodlg.DoModal() == IDOK) {
		//----------------------------------------
		// Change from frequency unit to type and unit
		//----------------------------------------
		frequency_type	= ((unit_window & 0x02) >> 1);
		frequency_unit	= (unit_window & 0x01);
		//----------------------------------------
		// arg1 (Reserve)
		//----------------------------------------
		arg1			= 0;
		//----------------------------------------
		// Set the device information
		//----------------------------------------
		try
		{
			ret			= c_fgen->SetDeviceSetting(aochannels, frequency, frequency_type, frequency_unit, arg1);
		}
		catch (...)
		{
			AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
			return;
		}
		//----------------------------------------
		// Display the error code if not completed normally
		//----------------------------------------
		if (ret != 0) {
			text_string.Format(_T("SetDeviceSetting = %d"), ret);
			m_edit_returnnum.SetWindowText(text_string);
			return;
		}
		//----------------------------------------
		// Set oscillation
		//----------------------------------------
		try
		{
			ret			= c_fgen->SetOscilation(oscilation_type, oscilation_start, oscilation_stop, arg1);
		}
		catch (...)
		{
			AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
			return;
		}
		//----------------------------------------
		// Display the error code if not completed normally
		//----------------------------------------
		if (ret != 0) {
			text_string.Format(_T("SetOscilation = %d "), ret);
			m_edit_returnnum.SetWindowText(text_string);
			return;
		}
		//----------------------------------------
		// Display the message that device settings is completed
		//----------------------------------------
		text_string.Format(_T("Device settings: Successful completion"));
		m_edit_returnnum.SetWindowText(text_string);
	}
}

//================================================================================
// It is the process when the Channel settings button is clicked
// Open the channel settings dialog
//================================================================================
void CMultiChannelWaveDlg::OnBnClickedButtonChannelsetting()
{
	ChannelInfoDlg	channelinfodlg;
	short			aochannel = 0;			// Channel
	float			amplitude = 0;			// Amplitude
	float			offset = 0;				// Offset
	short			amplitude_type = 0;		// Amplitude type
	short			amplitude_unit = 0;		// Amplitude unit
	float			duty = 0;				// Duty
	float			phase = 0;				// Phase
	short			sweep_type = 0;			// Simple sweep
	short			oscilation_time = 1;	// Sweep time
	float			oscilation_start = 0;	// Before sweep change
	float			oscilation_stop = 0;	// After sweep change
	short			arg1;					// Reserve
	long			ret;					// For return code
	short			wave_type;				// Wave type
	CString			text_string;			// For error code display

	//----------------------------------------
	// Leave the return code blank
	//----------------------------------------
	text_string.Format(_T(""));
	m_edit_returnnum.SetWindowText(text_string);
	//----------------------------------------
	// Get the channel from combobox
	//----------------------------------------
	aochannel	= m_combo_aochannel.GetCurSel();
	//----------------------------------------
	// Get the wave type
	//----------------------------------------
	try
	{
		ret		= c_fgen->GetWaveType(aochannel, &wave_type, &arg1);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Get the channel information
	//----------------------------------------
	try
	{
		ret		= c_fgen->GetChannelSetting(aochannel, &amplitude, &offset, &amplitude_type, &amplitude_unit, &duty, &phase, &arg1);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Get the simple sweep
	//----------------------------------------
	try
	{
		ret		= c_fgen->GetAoSweep(aochannel, &sweep_type, &oscilation_time, &oscilation_start, &oscilation_stop, &arg1);
	}
	catch (...)
	{
		AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
		return;
	}
	//----------------------------------------
	// Go to channel settings dialog
	//----------------------------------------
	channelinfodlg.devinfo_data(&aochannel, &amplitude, &offset, &amplitude_type,
								&amplitude_unit, &duty, &phase, &sweep_type,
								&oscilation_time, &oscilation_start, &oscilation_stop, wave_type);
	//----------------------------------------
	// If OK is pressed, process
	//----------------------------------------
	if (channelinfodlg.DoModal() == IDOK) {
		//----------------------------------------
		// arg1 (Reserve)
		//----------------------------------------
		arg1	= 0;
		//----------------------------------------
		// Set the channel settings
		//----------------------------------------
		try
		{
			ret	= c_fgen->SetChannelSetting(aochannel, amplitude, offset, amplitude_type, amplitude_unit, duty, phase, arg1);
		}
		catch (...)
		{
			AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
			return;
		}
		//----------------------------------------
		// Display the error code if not completed normally
		//----------------------------------------
		if (ret != 0) {
			text_string.Format(_T("SetChannelSetting = %d"), ret);
			m_edit_returnnum.SetWindowText(text_string);
			return;
		}
		//----------------------------------------
		// Set the simple sweep
		//----------------------------------------
		try
		{
			ret	= c_fgen->SetAoSweep(aochannel, sweep_type, oscilation_time, oscilation_start, oscilation_stop, arg1);
		}
		catch (...)
		{
			AfxMessageBox(_T("Unanticipated exception cannot found ICWaveformGeneratorPtr object."));
			return;
		}
		//----------------------------------------
		// Display the error code if not completed normally
		//----------------------------------------
		if (ret != 0) {
			text_string.Format(_T("SetAoSweep = %d "), ret);
			m_edit_returnnum.SetWindowText(text_string);
			return;
		}
		//----------------------------------------
		// Display the message that condition setting is completed
		//----------------------------------------
		text_string.Format(_T("Channel settings: Successful completion"));
		m_edit_returnnum.SetWindowText(text_string);
	}
}

//================================================================================
// It is the process when the SetWaveType button is clicked
// Set the wave type
//================================================================================
void CMultiChannelWaveDlg::OnBnClickedButtonSetwavetype()
{
	short	wave_type;			// Wave 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
	//----------------------------------------
	wave_type	= 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, wave_type, 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 StartOperation button is clicked
// Start the output of the application
//================================================================================
void CMultiChannelWaveDlg::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 CMultiChannelWaveDlg::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 CMultiChannelWaveDlg::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 CMultiChannelWaveDlg::OnBnClickedButtonCloseapplication()
{
	short	arg1;				// Reserve
	long	ret;				// For return code
	CString	text_string;		// For error code display

	//----------------------------------------
	// arg1 (Reserve)
	//----------------------------------------
	arg1	= 0;
	//----------------------------------------
	// Perform application close 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 CMultiChannelWaveDlg::OnBnClickedButtonExit()
{
	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;
	}
	//----------------------------------------
	// Close dialog
	//----------------------------------------
	CDialog::OnOK();
}
