// PeakTech_4055MV_4060Dlg.cpp : implementation file
//

#include "stdafx.h"
#include "PeakTech_4055MV_4060.h"
#include "PeakTech_4055MV_4060Dlg.h"
#include "ch375dll.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPeakTech_4055MV_4060Dlg dialog

CPeakTech_4055MV_4060Dlg::CPeakTech_4055MV_4060Dlg(CWnd* pParent /*=NULL*/)
	: CDialog(CPeakTech_4055MV_4060Dlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CPeakTech_4055MV_4060Dlg)
	m_text = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
		ch372_is_open = FALSE;

	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CPeakTech_4055MV_4060Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPeakTech_4055MV_4060Dlg)
	DDX_Control(pDX, IDC_SHOW, m_editText);
	DDX_Control(pDX, IDC_TAB1, m_tabctrl);
	DDX_Text(pDX, IDC_SHOW, m_text);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CPeakTech_4055MV_4060Dlg, CDialog)
	//{{AFX_MSG_MAP(CPeakTech_4055MV_4060Dlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_NOTIFY(TCN_SELCHANGE, IDC_TAB1, OnSelchangeTab)
	ON_BN_CLICKED(IDC_SEND, OnSend)
	ON_BN_CLICKED(IDC_LOCAL, OnLocal)
	ON_BN_CLICKED(IDC_RESET, OnReset)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPeakTech_4055MV_4060Dlg message handlers

BOOL CPeakTech_4055MV_4060Dlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 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
	
	// TODO: Add extra initialization here

	HANDLE hnd;
	if(ch372_is_open)
	{
		ch372_is_open = FALSE;
		CH375CloseDevice(0);
	}
	else
	{
		hnd = CH375OpenDevice(0);
		if(hnd == INVALID_HANDLE_VALUE)
		{
			CWnd::MessageBox("Open CH372 failure", "ERROR", MB_OK);
		}
		else
		{
			//setup CH372 timeout
			if(CH375SetTimeout(0, 0xFFFFFFFF, 4000) == FALSE)
			{
				CWnd::MessageBox("Connection CH375 overtime");
				return false;
			}
			ch372_is_open = TRUE;
		}
	}

	 m_tabctrl.InsertItem(0,"Continous");    //添加参数一选项卡 
     m_tabctrl.InsertItem(1,"   FM  ");      //添加参数二选项卡
	  m_tabctrl.InsertItem(2,"  AM  ");      //添加参数二选项卡
	   m_tabctrl.InsertItem(3,"  PM  ");      //添加参数二选项卡
	    m_tabctrl.InsertItem(4,"  PWM  ");      //添加参数二选项卡
		 m_tabctrl.InsertItem(5,"  FSK  ");      //添加参数二选项卡
		  m_tabctrl.InsertItem(6,"  Sweep  ");      //添加参数二选项卡
		   m_tabctrl.InsertItem(7,"  Burst  ");      //添加参数二选项卡

		   //关联对话框,并且将IDC_TABTEST控件设为父窗口

		  m_continous.Create(IDD_CONTINOUS,GetDlgItem(IDC_TAB1));
          m_fm.Create(IDD_FM,GetDlgItem(IDC_TAB1));
		  m_am.Create(IDD_AM,GetDlgItem(IDC_TAB1));
		  m_pm.Create(IDD_PM,GetDlgItem(IDC_TAB1));
		  m_pwm.Create(IDD_PWM,GetDlgItem(IDC_TAB1));
		  m_fsk.Create(IDD_FSK,GetDlgItem(IDC_TAB1));
		  m_burst.Create(IDD_BURST,GetDlgItem(IDC_TAB1));
		  m_sweep.Create(IDD_SWEEP,GetDlgItem(IDC_TAB1));

		  //获得IDC_TABTEST客户区大小

    CRect rs;
    m_tabctrl.GetClientRect(&rs);

                    //调整子对话框在父窗口中的位置
    rs.top += 30;
    rs.bottom -= 20;
    rs.left += 1;
    rs.right -= 2;

    //设置子对话框尺寸并移动到指定位置
    m_continous.MoveWindow(&rs);
    m_fm.MoveWindow(&rs);
	m_am.MoveWindow(&rs);
    m_pm.MoveWindow(&rs);
	m_pwm.MoveWindow(&rs);
    m_fsk.MoveWindow(&rs);
    m_burst.MoveWindow(&rs);
    m_sweep.MoveWindow(&rs);
	
    //分别设置隐藏和显示
    m_continous.ShowWindow(1);
    m_fm.ShowWindow(0);
	m_am.ShowWindow(0);
	m_pm.ShowWindow(0);
	m_pwm.ShowWindow(0);
	m_fsk.ShowWindow(0);
	m_burst.ShowWindow(0);
	m_sweep.ShowWindow(0);

    //设置默认的选项卡
    m_tabctrl.SetCurSel(0);  
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CPeakTech_4055MV_4060Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// 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 CPeakTech_4055MV_4060Dlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (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
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CPeakTech_4055MV_4060Dlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CPeakTech_4055MV_4060Dlg::OnSelchangeTab(NMHDR* pNMHDR, LRESULT* pResult) 
{
	// TODO: Add your control notification handler code here

			if(ch372_is_open == FALSE)
	{
			CWnd::MessageBox("Please first open the USB devices", "ERROR", MB_OK);
		return;
	}	

	int CurSel = m_tabctrl.GetCurSel(); 
    switch(CurSel)
    {
     case 0:
        m_continous.ShowWindow(1);
    m_fm.ShowWindow(0);
	m_am.ShowWindow(0);
	m_pm.ShowWindow(0);
	m_pwm.ShowWindow(0);
	m_fsk.ShowWindow(0);
	m_burst.ShowWindow(0);
	m_sweep.ShowWindow(0);
     break;
     case 1:
       m_continous.ShowWindow(0);
    m_fm.ShowWindow(1);
	m_am.ShowWindow(0);
	m_pm.ShowWindow(0);
	m_pwm.ShowWindow(0);
	m_fsk.ShowWindow(0);
	m_burst.ShowWindow(0);
	m_sweep.ShowWindow(0);

     break;
	 case 2:
    m_continous.ShowWindow(0);
    m_fm.ShowWindow(0);
	m_am.ShowWindow(1);
	m_pm.ShowWindow(0);
	m_pwm.ShowWindow(0);
	m_fsk.ShowWindow(0);
	m_burst.ShowWindow(0);
	m_sweep.ShowWindow(0);
		 break;
	 case 3:
		  m_continous.ShowWindow(0);
    m_fm.ShowWindow(0);
	m_am.ShowWindow(0);
	m_pm.ShowWindow(1);
	m_pwm.ShowWindow(0);
	m_fsk.ShowWindow(0);
	m_burst.ShowWindow(0);
	m_sweep.ShowWindow(0);
	break;
	 case 4:
		  m_continous.ShowWindow(0);
    m_fm.ShowWindow(0);
	m_am.ShowWindow(0);
	m_pm.ShowWindow(0);
	m_pwm.ShowWindow(1);
	m_fsk.ShowWindow(0);
	m_burst.ShowWindow(0);
	m_sweep.ShowWindow(0);
	break;
	 case 5:
	 m_continous.ShowWindow(0);
    m_fm.ShowWindow(0);
	m_am.ShowWindow(0);
	m_pm.ShowWindow(0);
	m_pwm.ShowWindow(0);
	m_fsk.ShowWindow(5);
	m_burst.ShowWindow(0);
	m_sweep.ShowWindow(0);
	break;
	case 6:
		  m_continous.ShowWindow(0);
    m_fm.ShowWindow(0);
	m_am.ShowWindow(0);
	m_pm.ShowWindow(0);
	m_pwm.ShowWindow(0);
	m_fsk.ShowWindow(0);
	m_burst.ShowWindow(0);
	m_sweep.ShowWindow(1);
	break;
	case 7:
    m_continous.ShowWindow(0);
    m_fm.ShowWindow(0);
	m_am.ShowWindow(0);
	m_pm.ShowWindow(0);
	m_pwm.ShowWindow(0);
	m_fsk.ShowWindow(0);
	m_burst.ShowWindow(1);
	m_sweep.ShowWindow(0);
	break;	
     default:break;
	}
	*pResult = 0;
}

void CPeakTech_4055MV_4060Dlg::OnSend() 
{
	// TODO: Add your control notification handler code here
		if(ch372_is_open == FALSE)
	{
		CWnd::MessageBox("Please first open the USB devices", "ERROR", MB_OK);
		return;
	}
	
	int CurSel = m_tabctrl.GetCurSel();
 
    switch(CurSel)
    {
     case 0://在Continous模式下
			 m_continous.Send();
			 m_text=m_continous.m_str;
     break;
     case 1://在FM模式下 
			m_fm.Send();
			m_text=m_fm.m_str;
     break;
	 case 2://在AM模式下
           m_am.Send();
		   m_text=m_am.m_str;
		 break;
	 case 3://在PM模式下
		 m_pm.Send();
		 m_text=m_pm.m_str;
	break;
	 case 4://在PWM模式下
		m_pwm.Send();
		m_text=m_pwm.m_str;
	break;
	 case 5://在FSK下
		m_fsk.Send();
		m_text=m_fsk.m_str;
	break;
	case 6://在Sweep下
     	m_sweep.Send();
		m_text=m_sweep.m_str;
	break;
	case 7://在Burst下
			m_burst.Send();
			m_text=m_burst.m_str;
	break;
     default:break;
	}
	
	UpdateData(FALSE);
	
}

void CPeakTech_4055MV_4060Dlg::OnLocal() 
{
	// TODO: Add your control notification handler code here
    char *ptr;
	CString str;
	ULONG len;
	if(ch372_is_open == FALSE)
	{
		CWnd::MessageBox("Please first open the USB devices", "ERROR", MB_OK);
		return;
	}
	ptr = (char*)malloc(1024);
	str="*SYSTem:LOCal";
	strcpy(ptr, str);
	strcat(ptr, "\n");
	len = strlen(ptr);
	CH375WriteData(0, ptr, &len);
	m_text=str ;
	UpdateData(false);
	//CWnd::MessageBox("写入完成", "OK", MB_OK);
	free(ptr);	
}

void CPeakTech_4055MV_4060Dlg::OnReset() 
{
	// TODO: Add your control notification handler code here

	char *ptr;
	CString str;
	ULONG len;
	if(ch372_is_open == FALSE)
	{
		CWnd::MessageBox("Please first open the USB devices", "ERROR", MB_OK);
		return;
	}
	ptr = (char*)malloc(1024);
	str="*RST";  
//	str="FUNCtion:SQUare:DCYCle 55%";
	strcpy(ptr, str);
	strcat(ptr, "\n");
	len = strlen(ptr);
	CH375WriteData(0, ptr, &len);
	m_text=str ;
	UpdateData(false);
	free(ptr);
	
}
