// DeviceInfoDlg.cpp : implementation file
//

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


// DeviceInfoDlg dialog

IMPLEMENT_DYNAMIC(DeviceInfoDlg, CDialogEx)

DeviceInfoDlg::DeviceInfoDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_DEVICESETTING, pParent)
{

}

DeviceInfoDlg::~DeviceInfoDlg()
{
}

void DeviceInfoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_FREQUENCY, m_edit_frequency);
	DDX_Control(pDX, IDC_EDIT_STARTOSCILATION, m_edit_start_oscilation);
	DDX_Control(pDX, IDC_EDIT_STOPOSCILATION, m_edit_stop_oscilation);
	DDX_Control(pDX, IDC_COMBO_CHANNELNUM, m_combo_channelnum);
	DDX_Control(pDX, IDC_COMBO_FREQUENCY_UNIT, m_combo_frequencyunit);
	DDX_Control(pDX, IDC_COMBO_OSCILATIONTYPE, m_combo_oscilationtype);
}


BEGIN_MESSAGE_MAP(DeviceInfoDlg, CDialogEx)
	ON_BN_CLICKED(IDOK, &DeviceInfoDlg::OnBnClickedOk)
	ON_BN_CLICKED(IDCANCEL, &DeviceInfoDlg::OnBnClickedCancel)
END_MESSAGE_MAP()


// DeviceInfoDlg message handlers

//----------------------------------------
// Data transfer
//----------------------------------------
void DeviceInfoDlg::devinfo_data(short* aochannels, float* frequency, short* frequencyunit, short* oscilationtype, short* oscilationstart, float* oscilationstop) {
	m_aochannels			= aochannels;
	m_frequency				= frequency;
	m_frequencyunit_window	= frequencyunit;
	m_oscilation_type		= oscilationtype;
	m_oscilation_start		= oscilationstart;
	m_oscilation_stop		= oscilationstop;
}

//----------------------------------------
// Initial display
// Value is current setting value
//----------------------------------------
BOOL DeviceInfoDlg::OnInitDialog() {
	CDialogEx::OnInitDialog();
	CString		text_temp;			// For temporary storage of text

	//----------------------------------------
	// Update the initial display of frequency
	//----------------------------------------
	text_temp.Format(_T("%.4f"), *m_frequency);
	m_edit_frequency.SetWindowText(text_temp);
	//----------------------------------------
	// Update the initial display of number of oscilation waveform
	//----------------------------------------
	text_temp.Format(_T("%d"), *m_oscilation_start);
	m_edit_start_oscilation.SetWindowText(text_temp);
	//----------------------------------------
	// Update the initial display of number of oscilation stop waveform or time of oscillation stop
	//----------------------------------------
	text_temp.Format(_T("%.4f"), *m_oscilation_stop);
	m_edit_stop_oscilation.SetWindowText(text_temp);
	//----------------------------------------
	// Create the combobox for number of channels
	//----------------------------------------
	for (int i = 0; i < CWAVEFORMGENERATOR_CHANNELS; i++) {
		text_temp.Format(_T("%d"), i + 1);
		m_combo_channelnum.AddString(text_temp);
	}
	//----------------------------------------
	// Change the selection of number of channels combobox
	//----------------------------------------
	m_combo_channelnum.SetCurSel(*m_aochannels - 1);
	//----------------------------------------
	// Create the combobox for frequency unit
	//----------------------------------------
	m_combo_frequencyunit.AddString(_T("Hz"));
	m_combo_frequencyunit.AddString(_T("kHz"));
	m_combo_frequencyunit.AddString(_T("sec"));
	m_combo_frequencyunit.AddString(_T("msec"));
	//----------------------------------------
	// Change the selection of frequency unit combobox
	//----------------------------------------
	m_combo_frequencyunit.SetCurSel(*m_frequencyunit_window);
	//-------------------
	// Create the combobox for oscilation type
	//-------------------
	m_combo_oscilationtype.AddString(_T("None"));
	m_combo_oscilationtype.AddString(_T("Auto burst"));
	m_combo_oscilationtype.AddString(_T("Trigger burst(rising edge)"));
	m_combo_oscilationtype.AddString(_T("Trigger burst(falling edge)"));
	//-------------------
	// Change the selection of oscilation type combobox
	//-------------------
	m_combo_oscilationtype.SetCurSel(*m_oscilation_type);
	return TRUE;
}

//================================================================================
// It is the process when the OK button is clicked
// Set the setting value
//================================================================================
void DeviceInfoDlg::OnBnClickedOk()
{
	CString text_temp;				// For temporary storage of text
	float	pre_frequency;			// Frequency
	short	pre_oscilation_start;	// Oscilation
	float	pre_oscilation_stop;	// Stop
	short	pre_channel_num;		// Number of channel
	short	pre_frequency_unit;		// Frequency unit
	short	pre_oscilation_type;	// Oscilation type

	//----------------------------------------
	// Get the number of channel from combobox
	// Number of channel starts from 1, so +1
	//----------------------------------------
	pre_channel_num			= m_combo_channelnum.GetCurSel() + 1;
	//----------------------------------------
	// Get the frequency from textbox
	//----------------------------------------
	m_edit_frequency.GetWindowText(text_temp);
	pre_frequency			= (float)_ttof(text_temp);
	//----------------------------------------
	// Get the frequency unit from combobox
	//----------------------------------------
	pre_frequency_unit		= m_combo_frequencyunit.GetCurSel();
	//----------------------------------------
	// Get the oscilation type from combobox
	//----------------------------------------
	pre_oscilation_type		= m_combo_oscilationtype.GetCurSel();
	//----------------------------------------
	// Get the oscilation from textbox
	//----------------------------------------
	m_edit_start_oscilation.GetWindowText(text_temp);
	pre_oscilation_start	= (short)_ttoi(text_temp);
	//----------------------------------------
	// Get the stop from textbox
	//----------------------------------------
	m_edit_stop_oscilation.GetWindowText(text_temp);
	pre_oscilation_stop		= (float)_ttof(text_temp);

	//----------------------------------------
	// Transfer the value set in the child dialog to the main
	// Set on main
	//----------------------------------------
	*m_aochannels			= pre_channel_num;
	*m_frequency			= pre_frequency;
	*m_frequencyunit_window	= pre_frequency_unit;
	*m_oscilation_type		= pre_oscilation_type;
	*m_oscilation_start		= pre_oscilation_start;
	*m_oscilation_stop		= pre_oscilation_stop;
	CDialogEx::OnOK();
}

//================================================================================
// It is the process when the Cancel button is clicked
// Discard the setting value
//================================================================================
void DeviceInfoDlg::OnBnClickedCancel()
{
	CDialogEx::OnCancel();
}
