// Continous.cpp : implementation file
//

#include "stdafx.h"
#include "PeakTech_4055MV_4060.h"
#include "Continous.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CContinous dialog


CContinous::CContinous(CWnd* pParent /*=NULL*/)
	: CDialog(CContinous::IDD, pParent)
{
	//{{AFX_DATA_INIT(CContinous)
	m_ConFreq = 1000.00;
	m_conOffset = 0.0;
	m_ConAmp = 1.0;
	m_ConDuty = 50;
	m_editText = _T("");
	m_editText1 = _T("");
	//}}AFX_DATA_INIT
}


void CContinous::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CContinous)
	DDX_Control(pDX, IDC_RADIO1, m_buttonDirect);
	DDX_Control(pDX, IDC_RADIO2, m_buttonOutput);
	DDX_Control(pDX, IDC_CONOUTPUT, m_comOutput);
	DDX_Control(pDX, IDC_CONWAVE, m_comWave);
	DDX_Control(pDX, IDC_CONATT, m_comAtt);
	DDX_Control(pDX, IDC_CONPOL, m_comPol);
	DDX_Control(pDX, IDC_CONOFFSET, m_comOffset);
	DDX_Control(pDX, IDC_CONAMP, m_comAmp);
	DDX_Control(pDX, IDC_CONFREQ, m_comFreq);
	DDX_Text(pDX, IDC_CON_FREQ, m_ConFreq);
	DDV_MinMaxDouble(pDX, m_ConFreq, 0., 2000000.);
	DDX_Text(pDX, IDC_CON_OFFSET, m_conOffset);
	DDX_Text(pDX, IDC_CON_AMP, m_ConAmp);
	DDX_Text(pDX, IDC_CON_DUTY, m_ConDuty);
	DDX_Text(pDX, IDC_EDIT1, m_editText);
	DDX_Text(pDX, IDC_EDIT2, m_editText1);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CContinous, CDialog)
	//{{AFX_MSG_MAP(CContinous)
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_RADIO2, OnRadio2)
	ON_BN_CLICKED(IDC_RADIO1, OnRadio1)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CContinous message handlers

void CContinous::Send()
{
    char *ptr;
	CString str,str_freqUnit,str_ampUnit,str_offsetUnit,str_wave,str_att,str_pol,str_output;
	m_comWave.GetWindowText(str_wave);
	m_comFreq.GetWindowText(str_freqUnit);
	m_comAmp.GetWindowText(str_ampUnit);
	m_comPol.GetWindowText(str_pol);
	m_comAtt.GetWindowText(str_att);
	m_comOffset.GetWindowText(str_offsetUnit);
	m_comOutput.GetWindowText(str_output);
	ULONG len;
	ptr = (char*)malloc(1024);
	UpdateData();
	m_str="";
	if (m_buttonOutput.GetCheck()==1)
	{
#if 1
		str.Format("FUNCtion %s",str_wave);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);
					CH375WriteData(0, ptr, &len);
				m_str=m_str+str +"\r\n";	
#endif
		if (strcmp(str_wave,"SQUare")==0)
				{
					Sleep(400);
					str.Format("SOURce:FUNCtion:SQUare:DCYCle %d%%",m_ConDuty);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);
					CH375WriteData(0, ptr, &len);
					m_str=m_str+str +  "\r\n";
				}

				if (strcmp(str_wave,"RAMP")==0)
				{
					Sleep(400);
					str.Format("SOURce:FUNCtion:RAMP:SYMMetry %d%%",m_ConDuty);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);
					CH375WriteData(0, ptr, &len);
					m_str=m_str+str + "\r\n";
					Sleep(500);
				}
#if 1
Sleep(500);			
	
					str.Format("FREQuency %f%s",m_ConFreq,str_freqUnit);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);
					CH375WriteData(0, ptr, &len);
				m_str=m_str+str + "\r\n";
				Sleep(100);
					str.Format("VOLTage %f%s",m_ConAmp,str_ampUnit);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);
					CH375WriteData(0, ptr, &len);
				m_str=m_str+str + "\r\n";
				Sleep(100);
					str.Format("VOLTage:OFFSet %f%s",m_conOffset,str_offsetUnit);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);
					CH375WriteData(0, ptr, &len);
				m_str=m_str+str +"\r\n";
		
				Sleep(300);
					str.Format("VOLTage:ATTenuation %s",str_att);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);
					CH375WriteData(0, ptr, &len);
				m_str=m_str+str +  "\r\n";
					Sleep(100);//setup polarity
					str.Format("OUTPut:POLarity %s",str_pol);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);
					CH375WriteData(0, ptr, &len);
				m_str=m_str+str +  "\r\n";
				   Sleep(100);//setup output
					str.Format("OUTPut %s",str_output);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);
					CH375WriteData(0, ptr, &len);
				m_str=m_str+str;
#endif
				m_editText=m_str;
	}
if (m_buttonDirect.GetCheck()==1)
{
			   	str.Format("Source:Apply:%s %f%s, %f%s, %f%s",str_wave,m_ConFreq,str_freqUnit,m_ConAmp,str_ampUnit,m_conOffset,str_offsetUnit);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);
					CH375WriteData(0, ptr, &len);
					m_str=str ;
					m_editText1=m_str;
}
	free(ptr);
	UpdateData(FALSE);
}



BOOL CContinous::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// TODO: Add extra initialization here
	m_comFreq.SetCurSel(2);
	m_comAmp.SetCurSel(0);
	m_comAtt.SetCurSel(0);
	m_comOffset.SetCurSel(0);
	m_comPol.SetCurSel(0);
	m_comWave.SetCurSel(0);
	m_comOutput.SetCurSel(0);
	m_buttonOutput.SetCheck(0);
	m_buttonDirect.SetCheck(1);
		   char *ptr;
	CString str,str_freqUnit,str_ampUnit,str_offsetUnit,str_wave,str_att,str_pol,str_output;
	m_comWave.GetWindowText(str_wave);
	m_comFreq.GetWindowText(str_freqUnit);
	m_comAmp.GetWindowText(str_ampUnit);
	m_comPol.GetWindowText(str_pol);
	m_comAtt.GetWindowText(str_att);
	m_comOffset.GetWindowText(str_offsetUnit);
	m_comOutput.GetWindowText(str_output);
	ULONG len;
	ptr = (char*)malloc(1024);
	UpdateData();

	{
					str.Format("FUNCtion %s",str_wave);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);				
					m_str=str + "\r\n";
				
					str.Format("FREQuency %f%s",m_ConFreq,str_freqUnit);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);				
				m_str=m_str+str +"\r\n";
			
					str.Format("VOLTage %f%s",m_ConAmp,str_ampUnit);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);				
				m_str=m_str+str +"\r\n";
			
					str.Format("VOLTage:OFFSet %f%s",m_conOffset,str_offsetUnit);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);				
				m_str=m_str+str + "\r\n";
				if (strcmp(str_wave,"SQUare")==0)
				{
				
					str.Format("FUNCtion:SQUare:DCYCle %d%%",m_ConDuty);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);				
					m_str=m_str+str +  "\r\n";
				}

				if (strcmp(str_wave,"RAMP")==0)
				{
				
					str.Format(":FUNCtion:RAMP:SYMMetry %d%%",m_ConDuty);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);				
					m_str=m_str+str +"\r\n";
				}
			
					str.Format("VOLTage:ATTenuation %s",str_att);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);				
				m_str=m_str+str + "\r\n";
				
					str.Format("OUTPut:POLarity %s",str_pol);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);				
				m_str=m_str+str + "\r\n";
				 
					str.Format("OUTPut %s",str_output);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);				
				m_str=m_str+str;
				m_editText=m_str;
	}

{
			   	str.Format("Source:Apply:%s %f%s, %f%s, %f%s",str_wave,m_ConFreq,str_freqUnit,m_ConAmp,str_ampUnit,m_conOffset,str_offsetUnit);
					strcpy(ptr, str);
					strcat(ptr, "\n");
					len = strlen(ptr);				
					m_str=str +  "\r\n";
	m_editText1=m_str;
}
	free(ptr);

	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CContinous::OnButton1() 
{
	// TODO: Add your control notification handler code here
	
}

void CContinous::OnRadio2() 
{
	// TODO: Add your control notification handler code here
		m_buttonOutput.SetCheck(1);
	m_buttonDirect.SetCheck(0);
}

void CContinous::OnRadio1() 
{
	// TODO: Add your control notification handler code here
	m_buttonOutput.SetCheck(0);
	m_buttonDirect.SetCheck(1);
	
}
