// 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_STARTOSCILLATION,	m_edit_start_oscillation);
	DDX_Control(pDX, IDC_EDIT_STOPOSCILLATION,	m_edit_stop_oscillation);
	DDX_Control(pDX, IDC_COMBO_CHANNELNUM,		m_combo_channelnum);
	DDX_Control(pDX, IDC_COMBO_FREQUENCY_UNIT,	m_combo_frequencyunit);
	DDX_Control(pDX, IDC_COMBO_OSCILLATIONTYPE,	m_combo_oscillationtype);
}


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(unsigned short* aochannels, float* frequency, short* frequencyunit, unsigned short* oscillationtype, unsigned short* oscillationstart, float* oscillationstop) {
	m_aochannels			= aochannels;
	m_frequency				= frequency;
	m_frequencyunit_value	= frequencyunit;
	m_oscillation_type		= oscillationtype;
	m_oscillation_start		= oscillationstart;
	m_oscillation_stop		= oscillationstop;
}

//================================================================================
// 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 oscillation waveform
	//----------------------------------------
	text_temp.Format(_T("%d"), *m_oscillation_start);
	m_edit_start_oscillation.SetWindowText(text_temp);
	//----------------------------------------
	// Update the initial display of number of oscillation stop waveform or time of oscillation stop
	//----------------------------------------
	text_temp.Format(_T("%.4f"), *m_oscillation_stop);
	m_edit_stop_oscillation.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_value);
	//----------------------------------------
	// Create the combobox for oscillation type
	//----------------------------------------
	m_combo_oscillationtype.AddString(_T("None"));
	m_combo_oscillationtype.AddString(_T("Auto burst"));
	m_combo_oscillationtype.AddString(_T("Trigger burst(rising edge)"));
	m_combo_oscillationtype.AddString(_T("Trigger burst(falling edge)"));
	//----------------------------------------
	// Change the selection of oscillation type combobox
	//----------------------------------------
	m_combo_oscillationtype.SetCurSel(*m_oscillation_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
	unsigned short	pre_oscillation_start;	// Oscillation
	float			pre_oscillation_stop;	// Stop
	unsigned short	pre_channel_num;		// Number of channel
	unsigned short	pre_frequency_unit;		// Frequency unit
	unsigned short	pre_oscillation_type;	// Oscillation 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 oscillation type from combobox
	//----------------------------------------
	pre_oscillation_type	= m_combo_oscillationtype.GetCurSel();
	//----------------------------------------
	// Get the oscillation from textbox
	//----------------------------------------
	m_edit_start_oscillation.GetWindowText(text_temp);
	pre_oscillation_start	= (unsigned short)_ttoi(text_temp);
	//----------------------------------------
	// Get the stop from textbox
	//----------------------------------------
	m_edit_stop_oscillation.GetWindowText(text_temp);
	pre_oscillation_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_value	= pre_frequency_unit;
	*m_oscillation_type		= pre_oscillation_type;
	*m_oscillation_start	= pre_oscillation_start;
	*m_oscillation_stop		= pre_oscillation_stop;
	CDialogEx::OnOK();
}

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