// ChannelInfoDlg.cpp : implementation file
//

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

//----------------------------------------
// Macro definition
//----------------------------------------
#define CWAVEFORMGENERATOR_SWEEPTIME		10
#define WAVETYPE_SINE						0
#define WAVETYPE_SQUARE						4

// ChannelInfoDlg dialog

IMPLEMENT_DYNAMIC(ChannelInfoDlg, CDialogEx)

ChannelInfoDlg::ChannelInfoDlg(CWnd* pParent /*=NULL*/)
	: CDialogEx(IDD_CHANNELSETTING, pParent)
{

}

ChannelInfoDlg::~ChannelInfoDlg()
{
}

void ChannelInfoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Control(pDX, IDC_EDIT_AMPLITUDE, m_edit_amplitude);
	DDX_Control(pDX, IDC_EDIT_OFFSET, m_edit_offset);
	DDX_Control(pDX, IDC_EDIT_DUTY, m_edit_duty);
	DDX_Control(pDX, IDC_EDIT_PHASE, m_edit_phase);
	DDX_Control(pDX, IDC_EDIT_SWEEPSTART, m_edit_sweep_start);
	DDX_Control(pDX, IDC_EDIT_SWEEPSTOP, m_edit_sweep_stop);
	DDX_Control(pDX, IDC_STATIC_CHANNELNO, m_edit_channelno);
	DDX_Control(pDX, IDC_COMBO_AMPLITUDE_UNIT, m_combo_amplitude_unit);
	DDX_Control(pDX, IDC_COMBO_AMPLITUDE_TYPE, m_combo_amplitude_type);
	DDX_Control(pDX, IDC_COMBO_SWEEPTYPE, m_combo_sweep_type);
	DDX_Control(pDX, IDC_COMBO_SWEEPTIME, m_combo_sweep_time);
}


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


// ChannelInfoDlg message handlers

//----------------------------------------
// Data transfer
//----------------------------------------
void ChannelInfoDlg::devinfo_data(	short* aochannel, float* amplitude, float* offset, short* amplitudetype,
									short* amplitudeunit, float* duty, float* phase, short* sweeptype,
									short* sweeptime, float* sweepstart, float* sweepstop, short wavetype) {
	m_aochannel			= aochannel;
	m_amplitude			= amplitude;
	m_offset			= offset;
	m_amplitude_type	= amplitudetype;
	m_amplitude_unit	= amplitudeunit;
	m_duty				= duty;
	m_phase				= phase;
	m_sweep_type		= sweeptype;
	m_sweep_time		= sweeptime;
	m_sweep_start		= sweepstart;
	m_sweep_stop		= sweepstop;
	m_wave_type			= wavetype;
}

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

	//----------------------------------------
	// Update the display of channel number
	//----------------------------------------
	text_temp.Format(_T("%d"), *m_aochannel);
	m_edit_channelno.SetWindowText(text_temp);
	//----------------------------------------
	// Update the initial display of amplitude
	//----------------------------------------
	text_temp.Format(_T("%.3f"), *m_amplitude);
	m_edit_amplitude.SetWindowText(text_temp);
	//----------------------------------------
	// Update the initial display of offset
	//----------------------------------------
	text_temp.Format(_T("%.3f"), *m_offset);
	m_edit_offset.SetWindowText(text_temp);
	//----------------------------------------
	// Update the initial display of duty
	//----------------------------------------
	text_temp.Format(_T("%.2f"), *m_duty);
	m_edit_duty.SetWindowText(text_temp);
	//----------------------------------------
	// Update the initial display of phase
	//----------------------------------------
	text_temp.Format(_T("%.2f"), *m_phase);
	m_edit_phase.SetWindowText(text_temp);
	//----------------------------------------
	// Update the initial display of sweep before change
	//----------------------------------------
	text_temp.Format(_T("%.2f"), *m_sweep_start);
	m_edit_sweep_start.SetWindowText(text_temp);
	//----------------------------------------
	// Update the initial display of sweep after change
	//----------------------------------------
	text_temp.Format(_T("%.2f"), *m_sweep_stop);
	m_edit_sweep_stop.SetWindowText(text_temp);
	//----------------------------------------
	// Create the combobox for amplitude type
	//----------------------------------------
	m_combo_amplitude_type.AddString(_T("Amplitude"));
	m_combo_amplitude_type.AddString(_T("Vp-p"));
	//----------------------------------------
	// Change the selection of amplitude type combobox
	//----------------------------------------
	m_combo_amplitude_type.SetCurSel(*m_amplitude_type);
	//----------------------------------------
	// Create the combobox for amplitude unit
	//----------------------------------------
	m_combo_amplitude_unit.AddString(_T("V or mA"));
	m_combo_amplitude_unit.AddString(_T("LSB"));
	//----------------------------------------
	// Change the selection of amplitude unit combobox
	//----------------------------------------
	m_combo_amplitude_unit.SetCurSel(*m_amplitude_unit);
	//----------------------------------------
	// Create the combobox for simple sweep
	//----------------------------------------
	m_combo_sweep_type.AddString(_T("None"));
	//----------------------------------------
	// For Sinewave, create options for [Frequency sweep], [Amplitude sweep], and [DC offset sweep]
	//----------------------------------------
	if (m_wave_type == WAVETYPE_SINE) {
		m_combo_sweep_type.AddString(_T("Frequency sweep"));
		m_combo_sweep_type.AddString(_T("Amplitude sweep"));
		m_combo_sweep_type.AddString(_T("DC offset sweep"));
	}
	//----------------------------------------
	// For Square, create options for [Duty ratio sweep]
	//----------------------------------------
	else if (m_wave_type == WAVETYPE_SQUARE) {
		m_combo_sweep_type.AddString(_T("Duty ratio sweep"));
	}
	//----------------------------------------
	// Change the selection of simple sweep combobox
	//----------------------------------------
	m_combo_sweep_type.SetCurSel(*m_sweep_type);
	//----------------------------------------
	// Create the combobox for sweep time
	//----------------------------------------
	for (int i = 0; i < CWAVEFORMGENERATOR_SWEEPTIME; i++) {
		text_temp.Format(_T("%d"), i + 1);
		m_combo_sweep_time.AddString(text_temp);
	}
	//----------------------------------------
	// Change the selection of sweep time combobox	
	//----------------------------------------
	m_combo_sweep_time.SetCurSel(*m_sweep_time - 1);
	return TRUE;
}

//----------------------------------------
// It is the process when the OK button is clicked
//----------------------------------------
void ChannelInfoDlg::OnBnClickedOk()
{
	CString text_temp;			// For temporary storage of text
	float	pre_amplitude;		// Amplitude
	float	pre_offset;			// Offset
	short	pre_amplitude_type;	// Amplitude type
	short	pre_amplitude_unit;	// Amplitude unit
	float	pre_duty;			// Duty ratio
	float	pre_phase;			// Phase
	short	pre_sweep_type;		// Simple sweep
	short	pre_sweep_time;		// Sweep time
	float	pre_sweep_start;	// Sweep before change
	float	pre_sweep_stop;		// Sweep after change

	//----------------------------------------
	// Get the amplitude from textbox
	//----------------------------------------
	m_edit_amplitude.GetWindowText(text_temp);
	pre_amplitude		= (float)_ttof(text_temp);
	//----------------------------------------
	// Get the offset from textbox
	//----------------------------------------
	m_edit_offset.GetWindowText(text_temp);
	pre_offset			= (float)_ttof(text_temp);
	//----------------------------------------
	// Get the duty ratio from textbox
	//----------------------------------------
	m_edit_duty.GetWindowText(text_temp);
	pre_duty			= (float)_ttof(text_temp);
	//----------------------------------------
	// Get the phase from textbox
	//----------------------------------------
	m_edit_phase.GetWindowText(text_temp);
	pre_phase			= (float)_ttof(text_temp);
	//----------------------------------------
	// Get the amplitude unit from combobox
	//----------------------------------------
	pre_amplitude_unit	= m_combo_amplitude_unit.GetCurSel();
	//----------------------------------------
	// Get the amplitude type from combobox
	//----------------------------------------
	pre_amplitude_type	= m_combo_amplitude_type.GetCurSel();
	//----------------------------------------
	// Get the sweep before change from textbox
	//----------------------------------------
	m_edit_sweep_start.GetWindowText(text_temp);
	pre_sweep_start		= (float)_ttof(text_temp);
	//----------------------------------------
	// Get the sweep after change from textbox
	//----------------------------------------
	m_edit_sweep_stop.GetWindowText(text_temp);
	pre_sweep_stop		= (float)_ttof(text_temp);
	//----------------------------------------
	// Get the simple sweep from combobox
	//----------------------------------------
	pre_sweep_type		= m_combo_sweep_type.GetCurSel();
	//----------------------------------------
	// Get the sweep time from combobox
	//----------------------------------------
	pre_sweep_time		= m_combo_sweep_time.GetCurSel() + 1;

	//----------------------------------------
	// Transfer the value set in the child dialog to the main
	// Set on main
	//----------------------------------------
	*m_amplitude		= pre_amplitude;
	*m_offset			= pre_offset;
	*m_amplitude_type	= pre_amplitude_type;
	*m_amplitude_unit	= pre_amplitude_unit;
	*m_duty				= pre_duty;
	*m_phase			= pre_phase;
	*m_sweep_type		= pre_sweep_type;
	*m_sweep_time		= pre_sweep_time;
	*m_sweep_start		= pre_sweep_start;
	*m_sweep_stop		= pre_sweep_stop;
	CDialogEx::OnOK();
}

//----------------------------------------
// It is the process when the Cancel button is clicked
//----------------------------------------
void ChannelInfoDlg::OnBnClickedCancel()
{
	CDialogEx::OnCancel();
}
