// VCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VC.h"
#include "VCDlg.h"
#include "SDK2250DLL.h"
#include "MemDC.h"
#include "Channel.h"
#include "GlobalsVariables.h"

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

struct PictureRange View;
/////////////////////////////////////////////////////////////////////////////
// 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)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVCDlg dialog

CVCDlg::CVCDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVCDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVCDlg)
	m_txtCh1Position = 0;
	m_txtCh1Trigger = 0;
	m_txtCh2Position = 0;
	m_txtCh2Trigger = 0;
	m_txtEXTTrigger = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVCDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVCDlg)
	DDX_Control(pDX, IDC_CBO_TRIGGERSLOPE, m_cboTriggerSlope);
	DDX_Control(pDX, IDC_CBO_TRIGGERHORIZON, m_cboHTrigger);
	DDX_Control(pDX, IDC_CH2_ENABLE, m_chkCH2Enable);
	DDX_Control(pDX, IDC_CH1_ENABLE, m_chkCH1Enable);
	DDX_Control(pDX, IDC_BTN_STOP, m_btnStop);
	DDX_Control(pDX, IDC_BTN_START, m_btnStart);
	DDX_Control(pDX, IDC_EDIT_CH2TRIGGER, m_editCh2Trigger);
	DDX_Control(pDX, IDC_BTN_CH2TRIGGER_OK, m_btnCh2Trigger);
	DDX_Control(pDX, IDC_BTN_CH1TRIGGER_OK, m_btnCh1Trigger);
	DDX_Control(pDX, IDC_EDIT_CH1TRIGGER, m_editCh1Trigger);
	DDX_Control(pDX, IDC_BTN_AUTOSET, m_btnAutoset);
	DDX_Control(pDX, IDC_MAINPANEL, m_MainPanel);
	DDX_Control(pDX, IDC_CBO_TRIGGERSOURCE, m_cboTriggerSource);
	DDX_Control(pDX, IDC_CBO_TRIGGERMODE, m_cboTriggerMode);
	DDX_Control(pDX, IDC_CBO_TIMEBASE, m_cboTIMEBASE);
	DDX_Control(pDX, IDC_CBO_CH2VOLTAGE, m_cboCh2Voltage);
	DDX_Control(pDX, IDC_CBO_CH1VOLTAGE, m_cboCh1Voltage);
	DDX_Text(pDX, IDC_EDIT_CH1POSITION, m_txtCh1Position);
	DDX_Text(pDX, IDC_EDIT_CH1TRIGGER, m_txtCh1Trigger);
	DDX_Text(pDX, IDC_EDIT_CH2POSITION, m_txtCh2Position);
	DDX_Text(pDX, IDC_EDIT_CH2TRIGGER, m_txtCh2Trigger);
	DDX_Text(pDX, IDC_EDIT_EXTTRIGGER, m_txtEXTTrigger);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVCDlg, CDialog)
	//{{AFX_MSG_MAP(CVCDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_TIMER()
	ON_BN_CLICKED(IDC_BTN_CH1POSITION_OK, OnBtnCh1positionOk)
	ON_BN_CLICKED(IDC_BTN_CH1TRIGGER_OK, OnBtnCh1triggerOk)
	ON_BN_CLICKED(IDC_BTN_CH2POSITION_OK, OnBtnCh2positionOk)
	ON_BN_CLICKED(IDC_BTN_CH2TRIGGER_OK, OnBtnCh2triggerOk)
	ON_CBN_SELCHANGE(IDC_CBO_CH1VOLTAGE, OnSelchangeCboCh1voltage)
	ON_CBN_SELCHANGE(IDC_CBO_CH2VOLTAGE, OnSelchangeCboCh2voltage)
	ON_CBN_SELCHANGE(IDC_CBO_TRIGGERSOURCE, OnSelchangeCboTriggersource)
	ON_CBN_SELCHANGE(IDC_CBO_TRIGGERMODE, OnSelchangeCboTriggermode)
	ON_CBN_SELCHANGE(IDC_CBO_TIMEBASE, OnSelchangeCboTimebase)
	ON_BN_CLICKED(IDC_BTN_AUTOSET, OnBtnAutoset)
	ON_BN_CLICKED(IDC_BTN_START, OnBtnStart)
	ON_BN_CLICKED(IDC_BTN_STOP, OnBtnStop)
	ON_BN_CLICKED(IDC_CH1_ENABLE, OnCh1Enable)
	ON_BN_CLICKED(IDC_CH2_ENABLE, OnCh2Enable)
	ON_CBN_SELCHANGE(IDC_CBO_TRIGGERHORIZON, OnSelchangeCboTriggerhorizon)
	ON_CBN_SELCHANGE(IDC_CBO_TRIGGERSLOPE, OnSelchangeCboTriggerslope)
	ON_BN_CLICKED(IDC_BTN_EXTTRIGGER_OK, OnBtnExttriggerOk)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVCDlg message handlers

BOOL CVCDlg::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
	WORD Result;
	InitVariables();
	InitCbo();
	Result = sdSearchDevice(DeviceIndex);
	if(Result == 0) 
	{
		MessageBox("Error:No Deivce Found!");
		return FALSE;
	}
	InitHardware();
	this->SetTimer (0,10,NULL);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CVCDlg::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 CVCDlg::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 CVCDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
void  CVCDlg::InitCbo()
{
	//Init Ch1 Voltage Combox
	m_cboCh1Voltage.AddString("10mV");
	m_cboCh1Voltage.AddString("20mV");
	m_cboCh1Voltage.AddString("50mV");
	m_cboCh1Voltage.AddString("100mV");
	m_cboCh1Voltage.AddString("200mV");
	m_cboCh1Voltage.AddString("500mV");
	m_cboCh1Voltage.AddString("1V");
	m_cboCh1Voltage.AddString("2V");
	m_cboCh1Voltage.AddString("5V");
	m_cboCh1Voltage.SetCurSel(Ch1.Voltage);
	
	//Init Ch2 Voltage Combox
	m_cboCh2Voltage.AddString("10mV");
	m_cboCh2Voltage.AddString("20mV");
	m_cboCh2Voltage.AddString("50mV");
	m_cboCh2Voltage.AddString("100mV");
	m_cboCh2Voltage.AddString("200mV");
	m_cboCh2Voltage.AddString("500mV");
	m_cboCh2Voltage.AddString("1V");
	m_cboCh2Voltage.AddString("2V");
	m_cboCh2Voltage.AddString("5V");
	m_cboCh2Voltage.SetCurSel(Ch2.Voltage);

	//Init Trigger Source Combox
	m_cboTriggerSource.AddString ("CH1");
	m_cboTriggerSource.AddString ("CH2");
	m_cboTriggerSource.AddString ("ALT");
	m_cboTriggerSource.AddString ("EXT");
	m_cboTriggerSource.SetCurSel (0);

	//Init Trigger Mode Combox
	m_cboTriggerMode.AddString ("AUTO");
	m_cboTriggerMode.AddString ("NORMAL");
	m_cboTriggerMode.AddString ("SINGLE");
	m_cboTriggerMode.SetCurSel (0);

	//Init TIMEBASE Combox
	m_cboTIMEBASE.AddString ("4ns");
	m_cboTIMEBASE.AddString ("10ns");
	m_cboTIMEBASE.AddString ("20ns");
	m_cboTIMEBASE.AddString ("40ns");
	m_cboTIMEBASE.AddString ("0.1us");
	m_cboTIMEBASE.AddString ("0.2us");
	m_cboTIMEBASE.AddString ("0.4us");
	m_cboTIMEBASE.AddString ("1us");
	m_cboTIMEBASE.AddString ("2us");
	m_cboTIMEBASE.AddString ("4us");
	m_cboTIMEBASE.AddString ("10us");
	m_cboTIMEBASE.AddString ("20us");
	m_cboTIMEBASE.AddString ("40us");
	m_cboTIMEBASE.AddString ("0.1ms");
	m_cboTIMEBASE.AddString ("0.2ms");
	m_cboTIMEBASE.AddString ("0.4ms");
	m_cboTIMEBASE.AddString ("1ms");
	m_cboTIMEBASE.AddString ("2ms");
	m_cboTIMEBASE.AddString ("4ms");
	m_cboTIMEBASE.AddString ("10ms");
	m_cboTIMEBASE.AddString ("20ms");
	m_cboTIMEBASE.AddString ("40ms");
	m_cboTIMEBASE.AddString ("0.1s");
	m_cboTIMEBASE.AddString ("0.2s");
	m_cboTIMEBASE.AddString ("0.4s");
	m_cboTIMEBASE.AddString ("1s");
	m_cboTIMEBASE.AddString ("2s");
	m_cboTIMEBASE.AddString ("4s");
	m_cboTIMEBASE.AddString ("10s");
	m_cboTIMEBASE.AddString ("20s");
	m_cboTIMEBASE.AddString ("40s");
	m_cboTIMEBASE.AddString ("1min");
	m_cboTIMEBASE.AddString ("2min");
	m_cboTIMEBASE.AddString ("4min");
	m_cboTIMEBASE.AddString ("10min");
	m_cboTIMEBASE.AddString ("20min");
	m_cboTIMEBASE.AddString ("40min");
	m_cboTIMEBASE.AddString ("1hour");
	m_cboTIMEBASE.SetCurSel (ControlState.TIMEBASE);

	//Init Horizon Trigger Position
	m_cboHTrigger.AddString("25%");
	m_cboHTrigger.AddString("50%");
	m_cboHTrigger.AddString("75%");
	m_cboHTrigger.SetCurSel(1);

	//Init Trigger Slope
	m_cboTriggerSlope.AddString(" + ");
	m_cboTriggerSlope.AddString(" - ");
	m_cboTriggerSlope.SetCurSel(0);

	//Init 
	m_chkCH1Enable.SetCheck(true);
	m_chkCH2Enable.SetCheck(true);
	m_btnStart.ShowWindow(SW_HIDE);
	m_btnStop.ShowWindow(SW_SHOW);
	GetDlgItem(IDC_EDIT_EXTTRIGGER)->SetWindowText("128");
	GetDlgItem(IDC_EDIT_EXTTRIGGER)->EnableWindow(false);
	GetDlgItem(IDC_BTN_EXTTRIGGER_OK)->EnableWindow(false);
}

//DEL void CAboutDlg::OnTimer(UINT nIDEvent) 
//DEL {
//DEL 	// TODO: Add your message handler code here and/or call default
//DEL 	
//DEL 	CDialog::OnTimer(nIDEvent);
//DEL }

void CVCDlg::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	WORD result;
	CRect imgRect ;
	m_MainPanel.GetClientRect (&imgRect);
	CDC *pDC = m_MainPanel.GetDC();
	CDC m_dcImg;

	CBitmap *m_pBitmapOldBackground;  
    CBitmap m_bitmapBackground;
	CMemDC memDC(pDC,&imgRect);
	if(m_dcImg.GetSafeHdc() == NULL|| (m_bitmapBackground.m_hObject == NULL))
	{
		m_dcImg.CreateCompatibleDC(pDC);
		m_bitmapBackground.CreateCompatibleBitmap(pDC, imgRect.Width(),imgRect.Height()) ;
		m_pBitmapOldBackground = m_dcImg.SelectObject(&m_bitmapBackground) ;
	}

	result = sdMainPanelDrawGrid((HDC)m_dcImg,&View);
	if(ScrollMode)
	{
		CTime mTime = GetCurrentTime();
		TimeData.Timer = (mTime.GetHour()*3600)+(mTime.GetMinute()*60)+(mTime.GetSecond());
		TimeData.Time = Time;
		result = sdGetScrollData(DeviceIndex, &ControlState, &Levers, &TimeData, Ch1.Voltage, Ch2.Voltage, Ch1.ScrollData, Ch2.ScrollData, Ch1.DisplayData, Ch2.DisplayData, Trigger.TPosition, Ch1InGND, Ch2InGND);
	}
	else
	{
		result = sdGetData(0, Ch1.Voltage, Ch2.Voltage, Ch1.HardwareData, Ch2.HardwareData, &ControlState, &FiltAndTrigger, &Levers, Level, StartCapture);
	}

	GetSamplingAndTime();

	if(Trigger.Mode == 2)
	{
		if(StartCapture)
		{
			ProcessData();
		}
		StartCapture = 0;
	}
	else
	{
		ProcessData();
	}

	if(!XYMode)
	{
		if(Ch1.Enable)
		{
			result = sdDisplayData((HDC)m_dcImg, &View, PointOrLine, Ch1.DisplayData, &(Ch1.ChannelColor));
		}
		if(Ch2.Enable)
		{
			result = sdDisplayData((HDC)m_dcImg, &View, PointOrLine, Ch2.DisplayData, &(Ch2.ChannelColor));
		}
	}
	memDC.BitBlt(0, 0, imgRect.Width(), imgRect.Height(), 
			           &m_dcImg, 0, 0, SRCCOPY);

	memDC.ReleaseMemDC();
	ReleaseDC(&m_dcImg);
	ReleaseDC(pDC);

	CDialog::OnTimer(nIDEvent);
}
void CVCDlg :: InitVariables()
{
	CRect imgRect ;
	m_MainPanel.GetClientRect (&imgRect);
	View.Width =(WORD)imgRect.Width();
	View.Hight =(WORD)imgRect.Height();
	View.Bottom =2;
	View.Left= 2;
	View.Right= 2;
	View.Top= 2;
	
	Ch1.Position=64;
	Ch2.Position=192;
	m_txtEXTTrigger = 128;
	Trigger.TPosition = Ch1.Position ;
	Ch1.SetChannelColor(0,255,0);
	Ch2.SetChannelColor(255,255,0);

	ControlState.BufferSize = 10;
	ControlState.ChannelSelect = 2;
	ControlState.IsAlt = 0;
	ControlState.TIMEBASE = 12;
	ControlState.TriggerAddress = 50;
	ControlState.TriggerSource = 0;

	FiltAndTrigger.Ch1Filt = 0;
	FiltAndTrigger.Ch2Filt = 0;
	FiltAndTrigger.TriggerFilt = 0;
	FiltAndTrigger.TriggerMode = 0;
	FiltAndTrigger.TriggerSlope = 0;
	
	Levers.Ch1Lever = 64;
	Levers.Ch1TrigLever = 64;
	Levers.Ch2Lever = 192;
	Levers.Ch2TrigLever = 192;
	Levers.ExtTrigLever = 128;
	
	InterpolationLineMode = 1;
	PointOrLine = 1;
	DeviceIndex = 0;
	ScrollMode = 0;
	StartCapture = 1;
	XYMode = FALSE;
	Ch1InGND = FALSE;
	Ch2InGND = FALSE;
	m_txtCh1Position = 64;
	m_txtCh1Trigger = 64;
	m_txtCh2Position = 192;
	m_txtCh2Trigger = 192;
	AutosetData.Ch1Enabled = 1;
	AutosetData.Ch2Enabled = 1;
	AutosetData.Ch1InGND = 0;
	AutosetData.Ch2InGND = 0;
	UpdateData(FALSE);
}
void CVCDlg :: InitHardware()
{
	WORD Result;
	Result = sdGetChannelLevel(DeviceIndex, Level, 62);
	Result = sdSetTriggerAndSampleRate(DeviceIndex, Trigger.Slope, &ControlState,FiltAndTrigger.TriggerFilt);
    Result = sdSetVoltageAndCoupling(DeviceIndex, Ch1.Voltage , Ch2.Voltage, Ch1.ACDC, Ch2.ACDC, Trigger.Source);
	Result = sdSetFilt(DeviceIndex, &FiltAndTrigger);
    Result = sdSetOffset(DeviceIndex, &Levers, Ch1.Voltage, Ch2.Voltage, Trigger.Source, Level);

	Result = sdCaptureStart(DeviceIndex);
	Result = sdGetDataSleep(&ControlState);
	Result = (WORD)sdTriggerEnabled(DeviceIndex);
}
void CVCDlg :: ProcessData()
{
	WORD result;
	if(!ScrollMode)
	{
		if(!Ch1InGND)
		{
			result = sdChannelDataBuffer(&ControlState, InterpolationLineMode, Ch1.BufferData, Ch1.HardwareData);
		}
		else
		{
			result = sdChannelInGND(Ch1.Position, Ch1.BufferData);
		}
		if(!Ch2InGND)
		{
			result = sdChannelDataBuffer(&ControlState, InterpolationLineMode, Ch2.BufferData, Ch2.HardwareData);
		}
		else
		{
			result = sdChannelInGND(Ch2.Position, Ch2.BufferData);
		}
		if(ControlState.TIMEBASE < 5)//软件找触发
		{
			WORD SoftTrigPosition;
			if(ControlState.TriggerSource == 0)
			{
				SoftTrigPosition = sdSoftFindTriggerPoint(ControlState.TriggerAddress,ControlState.TIMEBASE,Levers.Ch1TrigLever,FiltAndTrigger.TriggerSlope,Ch1.BufferData);
				result = sdSoftFindTriggerPointCopy(ControlState.TriggerAddress,SoftTrigPosition,Ch2.BufferData);
			}
			else if(ControlState.TriggerSource == 1)
			{
				SoftTrigPosition = sdSoftFindTriggerPoint(ControlState.TriggerAddress,ControlState.TIMEBASE,Levers.Ch2TrigLever,FiltAndTrigger.TriggerSlope,Ch2.BufferData);
				result = sdSoftFindTriggerPointCopy(ControlState.TriggerAddress,SoftTrigPosition,Ch1.BufferData);
			}
		}
		result = sdGetDisplayData(&ControlState, Ch1.DisplayData, Ch1.BufferData);
		result = sdGetDisplayData(&ControlState, Ch2.DisplayData, Ch2.BufferData);
	}
}
void CVCDlg::OnBtnCh1positionOk() 
{
	// TODO: Add your control notification handler code here
	int Temp;
	WORD Result;
	CString str;
	CEdit *pEdit = (CEdit*)GetDlgItem(IDC_EDIT_CH1POSITION);
	pEdit->GetWindowText(str);
	Temp = m_txtCh1Position;
	m_txtCh1Position = atoi((const char *)str);
	if(m_txtCh1Position>=0 && m_txtCh1Position<256)
	{
		if(m_editCh1Trigger.IsWindowEnabled())
		{
			m_txtCh1Trigger = m_txtCh1Position;
		}
	}
	else
	{
		MessageBox("The Range is 0 to 255!");
		m_txtCh1Position = Temp;
		if(m_editCh1Trigger.IsWindowEnabled())
		{
			m_txtCh1Trigger = Temp;
		}
	}
	UpdateData(FALSE);
	Levers.Ch1Lever = m_txtCh1Position;
	if(m_editCh1Trigger.IsWindowEnabled())
	{
		Levers.Ch1TrigLever = m_txtCh1Position;
		Trigger.TPosition = m_txtCh1Position;
	}
	Result = sdSetOffset(DeviceIndex, &Levers, Ch1.Voltage, Ch2.Voltage, Trigger.Source, Level);
}


void CVCDlg::OnBtnCh1triggerOk() 
{
	// TODO: Add your control notification handler code here
	int Temp;
	WORD Result;
	CString str;
	CEdit *pEdit = (CEdit*)GetDlgItem(IDC_EDIT_CH1TRIGGER);
	pEdit->GetWindowText(str);
	Temp = m_txtCh1Trigger;
	m_txtCh1Trigger = atoi((const char *)str);
	if(m_txtCh1Trigger<0 || m_txtCh1Trigger>255)
	{
		MessageBox("The Range is 0 to 255!");
		m_txtCh1Trigger = Temp;
	}
	UpdateData(FALSE);
	Levers.Ch1TrigLever = m_txtCh1Trigger;
	Trigger.TPosition = m_txtCh1Position;
	Result = sdSetOffset(DeviceIndex, &Levers, Ch1.Voltage, Ch2.Voltage, Trigger.Source, Level);
}

void CVCDlg::OnBtnCh2positionOk() 
{
	// TODO: Add your control notification handler code here
	int Temp;
	WORD Result;
	CString str;
	CEdit *pEdit = (CEdit*)GetDlgItem(IDC_EDIT_CH2POSITION);
	pEdit->GetWindowText(str);
	Temp = m_txtCh2Position;
	m_txtCh2Position = atoi((const char *)str);
	if(m_txtCh2Position>=0 && m_txtCh2Position<256)
	{
		if(m_editCh2Trigger.IsWindowEnabled())
		{
			m_txtCh2Trigger = m_txtCh2Position;
		}
	}
	else
	{
		MessageBox("The Range is 0 to 255!");
		m_txtCh2Position = Temp;
		if(m_editCh2Trigger.IsWindowEnabled())
		{
			m_txtCh2Trigger = Temp;
		}
	}
	UpdateData(FALSE);
	Levers.Ch2Lever = m_txtCh2Position;
	if(m_editCh2Trigger.IsWindowEnabled())
	{
		Levers.Ch2TrigLever = m_txtCh2Position;
		Trigger.TPosition = m_txtCh2Position;
	}
	Result = sdSetOffset(DeviceIndex, &Levers, Ch1.Voltage, Ch2.Voltage, Trigger.Source, Level);
}

void CVCDlg::OnBtnCh2triggerOk() 
{
	// TODO: Add your control notification handler code here
	int Temp;
	WORD Result;
	CString str;
	CEdit *pEdit = (CEdit*)GetDlgItem(IDC_EDIT_CH2TRIGGER);
	pEdit->GetWindowText(str);
	Temp = m_txtCh2Trigger;
	m_txtCh2Trigger = atoi((const char *)str);
	if(m_txtCh2Trigger<0 || m_txtCh2Trigger>255)
	{
		MessageBox("The Range is 0 to 255!");
		m_txtCh2Trigger = Temp;
	}
	UpdateData(FALSE);
	Levers.Ch2TrigLever = m_txtCh2Trigger;
	Trigger.TPosition = m_txtCh2Position;
	Result = sdSetOffset(DeviceIndex, &Levers, Ch1.Voltage, Ch2.Voltage, Trigger.Source, Level);
}

void CVCDlg::OnSelchangeCboCh1voltage() 
{
	// TODO: Add your control notification handler code here
	WORD Result;
	CComboBox *pCbo =(CComboBox*) GetDlgItem(IDC_CBO_CH1VOLTAGE);
	Ch1.Voltage = pCbo->GetCurSel();
	Result = sdSetVoltageAndCoupling(DeviceIndex, Ch1.Voltage , Ch2.Voltage, Ch1.ACDC, Ch2.ACDC, Trigger.Source);
    Result = sdSetOffset(DeviceIndex, &Levers, Ch1.Voltage, Ch2.Voltage, Trigger.Source, Level);
}

void CVCDlg::OnSelchangeCboCh2voltage() 
{
	// TODO: Add your control notification handler code here
	WORD Result;
	CComboBox *pCbo =(CComboBox*) GetDlgItem(IDC_CBO_CH2VOLTAGE);
	Ch2.Voltage = pCbo->GetCurSel();
	Result = sdSetVoltageAndCoupling(DeviceIndex, Ch1.Voltage , Ch2.Voltage, Ch1.ACDC, Ch2.ACDC, Trigger.Source);
    Result = sdSetOffset(DeviceIndex, &Levers, Ch1.Voltage, Ch2.Voltage, Trigger.Source, Level);
}

void CVCDlg::OnSelchangeCboTriggersource() 
{
	// TODO: Add your control notification handler code here
	long Result;
	CComboBox *pCbo =(CComboBox*) GetDlgItem(IDC_CBO_TRIGGERSOURCE);
	Trigger.Source = pCbo->GetCurSel();
	if(Trigger.Source == 0)//CH1
	{
		m_editCh1Trigger.EnableWindow(TRUE);
		m_btnCh1Trigger.EnableWindow(TRUE);
		m_editCh2Trigger.EnableWindow(FALSE);
		m_btnCh2Trigger.EnableWindow (FALSE);
	}
	else if(Trigger.Source == 1)//CH2
	{
		m_editCh1Trigger.EnableWindow(FALSE);
		m_btnCh1Trigger.EnableWindow(FALSE);
		m_editCh2Trigger.EnableWindow(TRUE);
		m_btnCh2Trigger.EnableWindow (TRUE);
	}
	else if (Trigger.Source == 2)//ALT
	{
		m_editCh1Trigger.EnableWindow(TRUE);
		m_btnCh1Trigger.EnableWindow(TRUE);
		m_editCh2Trigger.EnableWindow(TRUE);
		m_btnCh2Trigger.EnableWindow (TRUE);
	}
	else//EXT
	{
		m_editCh1Trigger.EnableWindow(FALSE);
		m_btnCh1Trigger.EnableWindow(FALSE);
		m_editCh2Trigger.EnableWindow(FALSE);
		m_btnCh2Trigger.EnableWindow (FALSE);
	}
	ControlState.TriggerSource = Trigger.Source;
	if(Ch1.Enable && Ch2.Enable)
	{
		ControlState.ChannelSelect = 2;
	}
	else if(!Ch1.Enable && Ch2.Enable)
	{
		ControlState.ChannelSelect = 3;
	}
	else
	{
		ControlState.ChannelSelect = 0;
	}
	if(Trigger.Source == 2)
	{
		ControlState.IsAlt = 1;
		Result = sdCaptureStart(DeviceIndex);
        Result = sdTriggerEnabled(DeviceIndex);
	}
	else
	{
		ControlState.IsAlt = 0;
	}
	if(Trigger.Source == 3)
	{
		GetDlgItem(IDC_EDIT_EXTTRIGGER)->EnableWindow(true);
		GetDlgItem(IDC_BTN_EXTTRIGGER_OK)->EnableWindow(true);
	}
	else
	{
		GetDlgItem(IDC_EDIT_EXTTRIGGER)->EnableWindow(false);
		GetDlgItem(IDC_BTN_EXTTRIGGER_OK)->EnableWindow(false);
	}
	Result = sdSetTriggerAndSampleRate(DeviceIndex, Trigger.Slope, &ControlState,FiltAndTrigger.TriggerFilt);
	Result = sdSetOffset(DeviceIndex, &Levers, Ch1.Voltage, Ch2.Voltage, Trigger.Source, Level);
	Result = sdSetVoltageAndCoupling(DeviceIndex, Ch1.Voltage , Ch2.Voltage, Ch1.ACDC, Ch2.ACDC, Trigger.Source);
}

void CVCDlg::OnSelchangeCboTriggermode() 
{
	// TODO: Add your control notification handler code here
	long Result;
	CComboBox *pCbo =(CComboBox*) GetDlgItem(IDC_CBO_TRIGGERMODE);
	FiltAndTrigger.TriggerMode = pCbo->GetCurSel();
	Trigger.Mode = pCbo->GetCurSel();
	if(Trigger.Mode == 1 && Trigger.Source == 2)
	{
		Result = sdCaptureStart(DeviceIndex);
        Result = sdTriggerEnabled(DeviceIndex);
	}
	if(Trigger.Source != 2 && StartCapture == 0)
	{
		StartCapture = 1;
	}
}

void CVCDlg::OnSelchangeCboTimebase() 
{
	// TODO: Add your control notification handler code here
	WORD Result;
	CComboBox *pCbo =(CComboBox*) GetDlgItem(IDC_CBO_TIMEBASE);
	ControlState.TIMEBASE = pCbo->GetCurSel();
	if(ControlState.TIMEBASE >24)
	{
		ScrollMode = 1;
		TimeData.ScrollStart = 1;
	}
	else
	{
		ScrollMode = 0;		
		TimeData.ScrollStart = 0;
	}
	Result = sdSetTriggerAndSampleRate(DeviceIndex, Trigger.Slope, &ControlState,FiltAndTrigger.TriggerFilt);
    Result = sdSetFilt(DeviceIndex, &FiltAndTrigger);
	Result = sdCaptureStart(DeviceIndex);
	Result = (WORD)sdTriggerEnabled(DeviceIndex);
}

void CVCDlg::OnBtnAutoset() 
{
	// TODO: Add your control notification handler code here
	WORD Result;
	m_btnAutoset.EnableWindow(FALSE);
	KillTimer(0);
	if(Trigger.Source == 1)
	{
		AutosetData.WhichChannel = 1;
	}
	else
	{
		AutosetData.WhichChannel = 0;
	}

	AutosetData.Ch1Enabled = 1;
	AutosetData.Ch2Enabled = 1;
    Result = sdAutoset(DeviceIndex, &ControlState, &AutosetData, &FiltAndTrigger, &Levers, Level);
	if(AutosetData.Ch1Enabled == 1 || AutosetData.Ch2Enabled == 1)	//有通道输入信号时
	{
		if(Trigger.Source != 1)		//只要不是通道2  校准后都以通道1为触发源
		{
			Trigger.Source = 0;
		}
		if(AutosetData.Ch1Enabled == 1)
		{
			m_chkCH1Enable.SetCheck(true);
			Ch1.Enable = true;
		}
		else
		{
			m_chkCH1Enable.SetCheck(false);
			Ch1.Enable = false;
			Trigger.Source = 1;
		}
		if(AutosetData.Ch2Enabled == 1)
		{
			m_chkCH2Enable.SetCheck(true);
			Ch2.Enable = true;
		}
		else
		{
			m_chkCH2Enable.SetCheck(false);
			Ch2.Enable = false;
			Trigger.Source = 0;
		}
	}
	else	//两个通道都没有输入信号时，通道都打开
	{
		m_chkCH1Enable.SetCheck(true);
		m_chkCH2Enable.SetCheck(true);
		Ch1.Enable = true;
		Ch2.Enable = true;
		Trigger.Source = 0;
	}
	OnCh1Enable();
	OnCh2Enable();

	((CComboBox*) GetDlgItem(IDC_CBO_TRIGGERSOURCE))->SetCurSel(Trigger.Source);
	OnSelchangeCboTriggersource();

	Ch1.Voltage = AutosetData.Ch1Voltage;
	Ch2.Voltage = AutosetData.Ch2Voltage;
	if(Trigger.Source == 0)
	{
		ControlState.TIMEBASE = AutosetData.Ch1Timebase;
	}
	else
	{
		ControlState.TIMEBASE = AutosetData.Ch2Timebase;
	}
	m_cboCh1Voltage.SetCurSel(Ch1.Voltage);
	m_cboCh2Voltage.SetCurSel(Ch2.Voltage);
	m_cboTIMEBASE.SetCurSel(ControlState.TIMEBASE);

	ControlState.TriggerAddress = 50;
	ControlState.TriggerSource = Trigger.Source;
	m_cboHTrigger.SetCurSel(1);
	Result = sdSetTriggerAndSampleRate(DeviceIndex, Trigger.Slope, &ControlState,FiltAndTrigger.TriggerFilt);
    Result = sdSetFilt(DeviceIndex, &FiltAndTrigger);
    Result = sdSetVoltageAndCoupling(DeviceIndex, Ch1.Voltage , Ch2.Voltage, Ch1.ACDC, Ch2.ACDC, Trigger.Source);
    Result = sdSetOffset(DeviceIndex, &Levers, Ch1.Voltage, Ch2.Voltage, Trigger.Source, Level);	
	Result = sdCaptureStart(DeviceIndex);
	Result = sdTriggerEnabled(DeviceIndex);

	SetTimer(0,10,NULL);
	m_btnAutoset.EnableWindow(TRUE);
}

void CVCDlg::OnBtnStart() 
{
	// TODO: Add your control notification handler code here
	m_btnStart.ShowWindow(SW_HIDE);
	m_btnStop.ShowWindow(SW_SHOW);
	StartCapture = 1;
	m_btnStop.SetFocus();
	SetTimer(0,10,NULL);
}

void CVCDlg::OnBtnStop() 
{
	// TODO: Add your control notification handler code here
	m_btnStart.ShowWindow(SW_SHOW);
	m_btnStop.ShowWindow(SW_HIDE);
	StartCapture = 0;
	m_btnStart.SetFocus();
	KillTimer(0);
}
void CVCDlg :: GetSamplingAndTime()
{
	Sampling = sdDisplaySampling(Ch1.Enable, Ch2.Enable, Trigger.Source, ControlState.TIMEBASE, ControlState.BufferSize, &Time);
}

void CVCDlg::OnCh1Enable() 
{
	// TODO: Add your control notification handler code here	
	if(m_chkCH1Enable.GetCheck())
	{
		Ch1.Enable = true;
		GetDlgItem(IDC_EDIT_CH1POSITION)->EnableWindow(TRUE);
		GetDlgItem(IDC_BTN_CH1POSITION_OK)->EnableWindow(TRUE);
		m_cboCh1Voltage.EnableWindow(TRUE);
		if(ControlState.TriggerSource == 0 || ControlState.TriggerSource == 2)
		{
			m_editCh1Trigger.EnableWindow(TRUE);
			m_btnCh1Trigger.EnableWindow(TRUE);
		}
	}
	else
	{
		Ch1.Enable = false;
		GetDlgItem(IDC_EDIT_CH1POSITION)->EnableWindow(FALSE);
		GetDlgItem(IDC_BTN_CH1POSITION_OK)->EnableWindow(FALSE);
		m_cboCh1Voltage.EnableWindow(FALSE);
		m_editCh1Trigger.EnableWindow(FALSE);
		m_btnCh1Trigger.EnableWindow(FALSE);
	}
	SetChannelOnOff();
}

void CVCDlg::OnCh2Enable() 
{
	// TODO: Add your control notification handler code here
	if(m_chkCH2Enable.GetCheck())
	{
		Ch2.Enable = true;
		GetDlgItem(IDC_EDIT_CH2POSITION)->EnableWindow(TRUE);
		GetDlgItem(IDC_BTN_CH2POSITION_OK)->EnableWindow(TRUE);
		m_cboCh2Voltage.EnableWindow(TRUE);
		if(ControlState.TriggerSource == 1 || ControlState.TriggerSource == 2)
		{
			m_editCh2Trigger.EnableWindow(TRUE);
			m_btnCh2Trigger.EnableWindow(TRUE);
		}
	}
	else
	{
		Ch2.Enable = false;
		GetDlgItem(IDC_EDIT_CH2POSITION)->EnableWindow(FALSE);
		GetDlgItem(IDC_BTN_CH2POSITION_OK)->EnableWindow(FALSE);
		m_cboCh2Voltage.EnableWindow(FALSE);
		m_editCh2Trigger.EnableWindow(FALSE);
		m_btnCh2Trigger.EnableWindow(FALSE);
	}
	SetChannelOnOff();
}

void CVCDlg::SetChannelOnOff()
{
	if(Ch1.Enable)
	{
		if(Ch2.Enable)
		{
			ControlState.ChannelSelect = 2;//CH1,CH2都可用
		}
		else
		{
			ControlState.ChannelSelect = 0;//CH1可用，CH2不可用
		}
	}
	else
	{
		if(Ch2.Enable)//CH1不可用，CH2可用
		{
			ControlState.ChannelSelect = 3;
		}
		else
		{
			ControlState.ChannelSelect = 0;//CH1,CH2都不可用
		}
	}
	InitHardware();
}

void CVCDlg::OnSelchangeCboTriggerhorizon() 
{
	// TODO: Add your control notification handler code here
	WORD TriggerPosition,Result;
	switch(m_cboHTrigger.GetCurSel())
	{
	case 0:
		TriggerPosition = 25;
		break;
	case 1:
		TriggerPosition = 50;
		break;
	case 2:
		TriggerPosition = 75;
		break;
	}
	if(StartCapture == 1)
	{
		ControlState.TriggerAddress = TriggerPosition;
       	
		Result = sdSetTriggerAndSampleRate(DeviceIndex, FiltAndTrigger.TriggerSlope, &ControlState, FiltAndTrigger.TriggerFilt);
	
		Result = sdSetFilt(DeviceIndex, &FiltAndTrigger);
     
	}
}

void CVCDlg::OnSelchangeCboTriggerslope() 
{
	// TODO: Add your control notification handler code here
	WORD Result;
    FiltAndTrigger.TriggerSlope = m_cboTriggerSlope.GetCurSel();
  
    Result = sdSetTriggerAndSampleRate(DeviceIndex, FiltAndTrigger.TriggerSlope, &ControlState, FiltAndTrigger.TriggerFilt);
        
    Result = sdSetFilt(DeviceIndex, &FiltAndTrigger);
    
}

void CVCDlg::OnBtnExttriggerOk() 
{
	// TODO: Add your control notification handler code here
	CString strText;
	WORD Result;
	WORD EXTTrigger;
	GetDlgItem(IDC_EDIT_EXTTRIGGER)->GetWindowText(strText);
	EXTTrigger = atoi((const char *)strText);
	if(EXTTrigger> 255)
	{
		AfxMessageBox("The maximum data is 255");
		strText.Format(_T("%d"),m_txtEXTTrigger);
		GetDlgItem(IDC_EDIT_EXTTRIGGER)->SetWindowText(strText);
	}
	else if(EXTTrigger<0)
	{
		AfxMessageBox("The minimum data is 0");
		strText.Format(_T("%d"),m_txtEXTTrigger);
		GetDlgItem(IDC_EDIT_EXTTRIGGER)->SetWindowText(strText);
	}
	else
	{
		m_txtEXTTrigger = EXTTrigger;
		Levers.ExtTrigLever = m_txtEXTTrigger;
		Result = sdSetOffset(DeviceIndex, &Levers, Ch1.Voltage, Ch2.Voltage, ControlState.TriggerSource, Level);
		
	}
}
