
// MultiChannelWaveDlg.cpp : implementation file
//

#include "stdafx.h"
#include "MultiChannelWave.h"
#include "MultiChannelWaveDlg.h"
#include "ChannelInfoDlg.h"
#include "DeviceInfoDlg.h"
#include "afxdialogex.h"
#include "cgen_api.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_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_GENOPENAPPLICATION,		&CMultiChannelWaveDlg::OnBnClickedButtonGenopenapplication)
	ON_BN_CLICKED(IDC_BUTTON_DEVICESETTING,				&CMultiChannelWaveDlg::OnBnClickedButtonDevicesetting)
	ON_BN_CLICKED(IDC_BUTTON_CHANNELSETTING,			&CMultiChannelWaveDlg::OnBnClickedButtonChannelsetting)
	ON_BN_CLICKED(IDC_BUTTON_GENSETWAVETYPE,			&CMultiChannelWaveDlg::OnBnClickedButtonGensetwavetype)
	ON_BN_CLICKED(IDC_BUTTON_GENSTARTOPERATION,			&CMultiChannelWaveDlg::OnBnClickedButtonGenstartoperation)
	ON_BN_CLICKED(IDC_BUTTON_GENGETAPPLICATIONSTATUS,	&CMultiChannelWaveDlg::OnBnClickedButtonGengetapplicationstatus)
	ON_BN_CLICKED(IDC_BUTTON_GENSTOPOPERATION,			&CMultiChannelWaveDlg::OnBnClickedButtonGenstopopereation)
	ON_BN_CLICKED(IDC_BUTTON_GENCLOSEAPPLICATION,		&CMultiChannelWaveDlg::OnBnClickedButtonGencloseapplication)
	ON_BN_CLICKED(IDC_BUTTON_EXIT,						&CMultiChannelWaveDlg::OnBnClickedButtonExit)
END_MESSAGE_MAP()

//----------------------------------------
// Global variable declaration
//----------------------------------------
short	g_appid;												// Application access ID

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

	// 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 GenOpenApplication button is clicked
// Initialize the application
//================================================================================
void CMultiChannelWaveDlg::OnBnClickedButtonGenopenapplication()
{
	TCHAR			text_temp[256];		// For temporary storage of text
	char			device_name[256];	// Device name
	char			error_string[256];	// Error code string
	unsigned short	openmode;			// Screen size
	long			ret;				// For return code
	CString			text_string;		// For error code display

	//----------------------------------------
	// Get device name from textbox
	//----------------------------------------
	m_edit_device_name.GetWindowText(text_temp, _countof(text_temp));
	sprintf_s(device_name, "%S", text_temp);
	//----------------------------------------
	// Get screen size from combobox
	//----------------------------------------
	openmode	= m_combo_openmode.GetCurSel();
	//----------------------------------------
	// Perform application initialization process
	//----------------------------------------
	ret = GenOpenApplication(device_name, &g_appid, openmode);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenOpenApplication = %d : %s"), ret, text_temp);
	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;
	TCHAR			text_temp[256];							// For temporary storage of text
	char			error_string[256];						// Error code string
	unsigned short	aochannels						= 0;	// Number of channel
	float			frequency						= 1;	// Frequency
	short			unit_frequency_combobox_value	= 0;	// Selected index of frequency unit combobox
	unsigned short	frequency_type					= 0;	// Frequency or Period
	unsigned short	frequency_unit					= 0;	// Hz(sec) or kHz(msec)
	unsigned short	oscillation_type				= 0;	// Oscillation type 
	unsigned short	oscillation_start				= 1;	// Number of oscillation waveform
	float			oscillation_stop				= 1;	// Number of oscillation stop waveform or Time of oscillation stop
	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
	//----------------------------------------
	ret								= GenGetDeviceSetting(g_appid, &aochannels, &frequency, &frequency_type, &frequency_unit);
	unit_frequency_combobox_value	= (frequency_type << 1) + frequency_unit;
	//----------------------------------------
	// Get the oscillation
	//----------------------------------------
	ret								= GenGetOscillation(g_appid, &oscillation_type, &oscillation_start, &oscillation_stop);
	//----------------------------------------
	// Go to device settings dialog
	//----------------------------------------
	deviceinfodlg.devinfo_data(&aochannels, &frequency, &unit_frequency_combobox_value, &oscillation_type, &oscillation_start, &oscillation_stop);
	//----------------------------------------
	// If OK is pressed, process
	//----------------------------------------
	if (deviceinfodlg.DoModal() == IDOK) {
		//----------------------------------------
		// Change from frequency unit to type and unit
		//----------------------------------------
		frequency_type	= ((unit_frequency_combobox_value & 0x02) >> 1);
		frequency_unit	= (unit_frequency_combobox_value & 0x01);
		//----------------------------------------
		// Set the device information
		//----------------------------------------
		ret				= GenSetDeviceSetting(g_appid, aochannels, frequency, frequency_type, frequency_unit);
		//----------------------------------------
		// Display the error code if not completed normally
		//----------------------------------------
		if (ret != 0) {
			//----------------------------------------
			// Get error string
			//----------------------------------------
			GenGetErrorString(ret, error_string);
			//----------------------------------------
			// Display error code and message
			//----------------------------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("GenSetDeviceSetting = %d : %s"), ret, text_temp);
			m_edit_returnnum.SetWindowText(text_string);
			return;
		}
		//----------------------------------------
		// Set oscillation
		//----------------------------------------
		ret			= GenSetOscillation(g_appid, oscillation_type, oscillation_start, oscillation_stop);
		//----------------------------------------
		// Display the error code if not completed normally
		//----------------------------------------
		if (ret != 0) {
			//----------------------------------------
			// Get error string
			//----------------------------------------
			GenGetErrorString(ret, error_string);
			//----------------------------------------
			// Display error code and message
			//----------------------------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("GenSetOscillation = %d : %s"), ret, text_temp);
			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;
	TCHAR			text_temp[256];				// For temporary storage of text
	char			error_string[256];			// Error code string
	short			aochannel			= 0;	// Channel
	float			amplitude			= 0;	// Amplitude
	float			offset				= 0;	// Offset
	unsigned short	amplitude_type		= 0;	// Amplitude type
	unsigned short	amplitude_unit		= 0;	// Amplitude unit
	float			duty				= 0;	// Duty
	float			phase				= 0;	// Phase
	unsigned short	sweep_type			= 0;	// Simple sweep
	unsigned short	oscillation_time	= 1;	// Sweep time
	float			oscillation_start	= 0;	// Sweep before change
	float			oscillation_stop	= 0;	// Sweep after change
	long			ret;						// For return code
	unsigned 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
	//----------------------------------------
	ret		= GenGetWaveType(g_appid, aochannel, &wave_type);
	//----------------------------------------
	// Get the channel information
	//----------------------------------------
	ret		= GenGetChannelSetting(g_appid, aochannel, &amplitude, &offset, &amplitude_type, &amplitude_unit, &duty, &phase);
	//----------------------------------------
	// Get the simple sweep
	//----------------------------------------
	ret		= GenGetAoSweep(g_appid, aochannel, &sweep_type, &oscillation_time, &oscillation_start, &oscillation_stop);
	//----------------------------------------
	// Go to channel settings dialog
	//----------------------------------------
	channelinfodlg.chinfo_data(	&aochannel, &amplitude, &offset, &amplitude_type,
								&amplitude_unit, &duty, &phase, &sweep_type,
								&oscillation_time, &oscillation_start, &oscillation_stop, wave_type);
	//----------------------------------------
	// If OK is pressed, process
	//----------------------------------------
	if (channelinfodlg.DoModal() == IDOK) {
		//----------------------------------------
		// Set the channel settings
		//----------------------------------------
		ret	= GenSetChannelSetting(g_appid, aochannel, amplitude, offset, amplitude_type, amplitude_unit, duty, phase);
		//----------------------------------------
		// Display the error code if not completed normally
		//----------------------------------------
		if (ret != 0) {
			//----------------------------------------
			// Get error string
			//----------------------------------------
			GenGetErrorString(ret, error_string);
			//----------------------------------------
			// Display error code and message
			//----------------------------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("GenSetChannelSetting = %d : %s"), ret, text_temp);
			m_edit_returnnum.SetWindowText(text_string);
			return;
		}
		//----------------------------------------
		// Set the simple sweep
		//----------------------------------------
		ret	= GenSetAoSweep(g_appid, aochannel, sweep_type, oscillation_time, oscillation_start, oscillation_stop);
		//----------------------------------------
		// Display the error code if not completed normally
		//----------------------------------------
		if (ret != 0) {
			//----------------------------------------
			// Get error string
			//----------------------------------------
			GenGetErrorString(ret, error_string);
			//----------------------------------------
			// Display error code and message
			//----------------------------------------
			wsprintf(text_temp, _T("%S"), error_string);
			text_string.Format(_T("GenSetAoSweep = %d : %s"), ret, text_temp);
			m_edit_returnnum.SetWindowText(text_string);
			return;
		}
		//----------------------------------------
		// Display the message that channel setting is completed
		//----------------------------------------
		text_string.Format(_T("Channel settings: Successful completion"));
		m_edit_returnnum.SetWindowText(text_string);
	}
}

//================================================================================
// It is the process when the GenSetWaveType button is clicked
// Set the wave type
//================================================================================
void CMultiChannelWaveDlg::OnBnClickedButtonGensetwavetype()
{
	TCHAR			text_temp[256];		// For temporary storage of text
	char			error_string[256];	// Error code string
	unsigned short	wave_type;			// Wave type
	unsigned short	aochannel;			// Channel
	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();
	//----------------------------------------
	// Set the wave type
	//----------------------------------------
	ret			= GenSetWaveType(g_appid, aochannel, wave_type);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenSetWaveType = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenStartOperation button is clicked
// Start the output of the application
//================================================================================
void CMultiChannelWaveDlg::OnBnClickedButtonGenstartoperation()
{
	TCHAR	text_temp[256];		// For temporary storage of text
	char	error_string[256];	// Error code string
	long	ret;				// For return code
	CString	text_string;		// For error code display

	//----------------------------------------
	// Start the output of the application
	//----------------------------------------
	ret	= GenStartOperation(g_appid);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenStartOperation = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenGetApplicationStatus button is clicked
// Get the operating status
//================================================================================
void CMultiChannelWaveDlg::OnBnClickedButtonGengetapplicationstatus()
{
	TCHAR			text_temp[256];		// For temporary storage of text
	char			error_string[256];	// Error code string
	unsigned long	status = 0;			// Status
	long			ret;				// For return code
	CString			text_string;		// For error code display

	//----------------------------------------
	// Get the operating status
	//----------------------------------------
	ret = GenGetApplicationStatus(g_appid, &status);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenGetApplicationStatus = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
	//----------------------------------------
	// Display the status
	//----------------------------------------
	if (status == 0) {
		text_string.Format(_T("00H(Stopped)"));
	}
	else if (status == 1) {
		text_string.Format(_T("01H(Running)"));
	}
	else if (status == 2) {
		text_string.Format(_T("02H(Waiting for start trigger)"));
	}
	else if (status == 3) {
		text_string.Format(_T("03H(Running, Waiting for start trigger)"));
	}
	else {
		text_string.Format(_T("%d "), status);
	}
	m_edit_applicationstatus.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenStopOperation button is clicked
// Stop the output of the application
//================================================================================
void CMultiChannelWaveDlg::OnBnClickedButtonGenstopopereation()
{
	TCHAR			text_temp[256];		// For temporary storage of text
	char			error_string[256];	// Error code string
	long			ret;				// For return code
	CString			text_string;		// For error code display

	//----------------------------------------
	// Stop the output of the application
	//----------------------------------------
	ret	= GenStopOperation(g_appid);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenStopOperation = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
}

//================================================================================
// It is the process when the GenCloseApplication button is clicked
// Close the application
//================================================================================
void CMultiChannelWaveDlg::OnBnClickedButtonGencloseapplication()
{
	TCHAR			text_temp[256];		// For temporary storage of text
	char			error_string[256];	// Error code string
	unsigned long	status;				// Status
	long			ret;				// For return code
	CString			text_string;		// For error code display

	//----------------------------------------
	// Perform application close process
	//----------------------------------------
	ret	= GenCloseApplication(g_appid, &status);
	//----------------------------------------
	// Get error string
	//----------------------------------------
	GenGetErrorString(ret, error_string);
	//----------------------------------------
	// Display error code and message
	//----------------------------------------
	wsprintf(text_temp, _T("%S"), error_string);
	text_string.Format(_T("GenCloseApplication = %d : %s"), ret, text_temp);
	m_edit_returnnum.SetWindowText(text_string);
	//----------------------------------------
	// Display the status
	//----------------------------------------
	if (status == 0) {
		text_string.Format(_T("0(Normal completed)"));
	}
	else if (status == 1) {
		text_string.Format(_T("1(C-WaveformGenerator completed error)"));
	}
	else {
		text_string.Format(_T("%d "), status);
	}
	m_edit_applicationstatus.SetWindowText(text_string);
}

//================================================================================
// It is the process when the Exit button is clicked
// Terminate the application
//================================================================================
void CMultiChannelWaveDlg::OnBnClickedButtonExit()
{
	//----------------------------------------
	// Close dialog
	//----------------------------------------
	CDialog::OnOK();
}
