// VCDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VC.h"
#include "VCDlg.h"
#include "SDK5200ADLL.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;
	//}}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_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);
	//}}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)
	//}}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;
	InitCbo();
	InitVariables();
	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.AddString("10V");
	m_cboCh1Voltage.SetCurSel(6);
	
	//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.AddString("10V");
	m_cboCh2Voltage.SetCurSel(6);

	//Init Trigger Source Combox
	m_cboTriggerSource.AddString ("CH1");
	m_cboTriggerSource.AddString ("CH2");
	m_cboTriggerSource.AddString ("ALT");
	m_cboTriggerSource.AddString ("EXT");
	m_cboTriggerSource.AddString ("EXT/10");
	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 ("2ns");
	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.SetCurSel (13);
}

//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)
	{
		SYSTEMTIME st;
		GetSystemTime(&st);
		LONG NowTime = (st.wHour*3600000)+(st.wMinute*60000)+(st.wSecond*1000) + (st.wMilliseconds);
		TimeData.Timer = NowTime;
		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,PulseTrigger,PulseTriggerMode,PulseWidth);
	}

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


	if(!XYMode)
	{
		result = sdDisplayData((HDC)m_dcImg, &View, PointOrLine, Ch1.DisplayData, &(Ch1.ChannelColor));
		result = sdDisplayData((HDC)m_dcImg, &View, PointOrLine, Ch2.DisplayData, &(Ch2.ChannelColor));
	}
	else
	{
        result = sdDisplayDataXYMode((HDC)m_dcImg, &View, PointOrLine, Ch1.DisplayData, Ch2.DisplayData);
	}
	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=128;
	Ch2.Position=384;
	Trigger.TPosition = Ch1.Position ;
	Ch1.SetChannelColor(0,255,0);
	Ch2.SetChannelColor(255,255,0);
	PulseTrigger = 0;
	PulseTriggerMode = 0;
	PulseWidth = 2;
	ETS = 0;
	Trigger.Source = 0;

	ControlState.BufferSize = 10;
	ControlState.ChannelSelect = 2;
	ControlState.DataLength = 1;
	ControlState.IsAlt = 0;
	ControlState.TIMEBASE = 13;
	ControlState.TriggerAddress = 50;
	ControlState.TriggerSource = Trigger.Source;

	FiltAndTrigger.Ch1Filt = 0;
	FiltAndTrigger.Ch2Filt = 0;
	FiltAndTrigger.TriggerFilt = 0;
	FiltAndTrigger.TriggerMode = 0;
	FiltAndTrigger.TriggerSlope = 0;
	
	Levers.Ch1Lever = 128;
	Levers.Ch1TrigLever = 128;
	Levers.Ch2Lever = 384;
	Levers.Ch2TrigLever = 384;
	Levers.ExtTrigLever = 256;
	
	InterpolationLineMode = 2;
	PointOrLine = 1;
	DeviceIndex = 0;
	ScrollMode = 0;
	StartCapture = 1;
	XYMode = FALSE;
	Ch1InGND = FALSE;
	Ch2InGND = FALSE;
	m_txtCh1Position = 128;
	m_txtCh1Trigger = 128;
	m_txtCh2Position = 384;
	m_txtCh2Trigger = 384;
	AutosetData.Ch1Enabled = 1;
	AutosetData.Ch2Enabled = 1;
	AutosetData.Ch1InGND = 0;
	AutosetData.Ch2InGND = 0;
	UpdateData(FALSE);
}
void CVCDlg :: InitHardware()
{
	WORD Result;
	Result = sdGetLevel(DeviceIndex, Level);
	Result = (WORD)sdSetTriggerAndSampleRate(DeviceIndex, Trigger.Slope,ETS, &ControlState,PulseTrigger,PulseTriggerMode,PulseWidth);
    Result = sdSetVoltageAndCoupling(DeviceIndex, Ch1.Voltage, Ch2.Voltage, Ch1.ACDC, Ch2.ACDC,&ControlState);
	Result = (WORD)sdSetFilt(DeviceIndex, &FiltAndTrigger);
    Result = sdSetOffset(DeviceIndex, &Levers, Ch1.Voltage, Ch2.Voltage, Trigger.Source, Level);
	Result = sdCaptureStart(DeviceIndex);
	Result = (WORD)sdTriggerEnabled(DeviceIndex,ControlState.TIMEBASE);
}
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<512)
	{
		if(m_editCh1Trigger.IsWindowEnabled())
		{
			m_txtCh1Trigger = m_txtCh1Position;
		}
	}
	else
	{
		MessageBox("The Range is 0 to 511!");
		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>511)
	{
		MessageBox("The Range is 0 to 511!");
		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<512)
	{
		if(m_editCh2Trigger.IsWindowEnabled())
		{
			m_txtCh2Trigger = m_txtCh2Position;
		}
	}
	else
	{
		MessageBox("The Range is 0 to 511!");
		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>511)
	{
		MessageBox("The Range is 0 to 511!");
		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,&ControlState);
    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,&ControlState);
    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 = (WORD)sdTriggerEnabled(DeviceIndex,ControlState.TIMEBASE);
	}
	else
	{
		ControlState.IsAlt = 0;
	}
	Result = sdSetTriggerAndSampleRate(DeviceIndex, Trigger.Slope,ETS,&ControlState,PulseTrigger,PulseTriggerMode,PulseWidth);
	Result = sdSetOffset(DeviceIndex, &Levers, Ch1.Voltage, Ch2.Voltage, Trigger.Source, Level);
}

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 != 2 && Trigger.Source == 2)
	{
		Result = sdCaptureStart(DeviceIndex);
        Result = (WORD)sdTriggerEnabled(DeviceIndex,ControlState.TIMEBASE);
	}
	if(Trigger.Source != 2 && StartCapture == 0)
	{
		StartCapture = 1;
		Result = sdCaptureStart(DeviceIndex);
        Result = (WORD)sdTriggerEnabled(DeviceIndex,ControlState.TIMEBASE);
	}
}

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 >25)
	{
		ScrollMode = 1;
		TimeData.ScrollStart = 1;
	}
	else
	{
		ScrollMode = 0;		
		TimeData.ScrollStart = 0;
	}
	Result = (WORD)sdSetTriggerAndSampleRate(DeviceIndex, Trigger.Slope,ETS,&ControlState,PulseTrigger,PulseTriggerMode,PulseWidth);
    Result = sdSetVoltageAndCoupling(DeviceIndex, Ch1.Voltage, Ch2.Voltage, Ch1.ACDC, Ch2.ACDC,&ControlState);
    Result = sdSetOffset(DeviceIndex, &Levers, Ch1.Voltage, Ch2.Voltage, Trigger.Source, Level);
	Result = sdCaptureStart(DeviceIndex);
	Result = (WORD)sdTriggerEnabled(DeviceIndex,ControlState.TIMEBASE);
}

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

    Result = sdAutoset(DeviceIndex, &ControlState, &AutosetData, &FiltAndTrigger, &Levers, Level);
	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;
	Result = (WORD)sdSetTriggerAndSampleRate(DeviceIndex, Trigger.Slope,ETS,&ControlState,PulseTrigger,PulseTriggerMode,PulseWidth);
    Result = (WORD)sdSetFilt(DeviceIndex, &FiltAndTrigger);
    Result = sdSetVoltageAndCoupling(DeviceIndex, Ch1.Voltage, Ch2.Voltage, Ch1.ACDC, Ch2.ACDC,&ControlState);
    Result = sdSetOffset(DeviceIndex, &Levers, Ch1.Voltage, Ch2.Voltage, Trigger.Source, Level);	
	Result = sdCaptureStart(DeviceIndex);
	Result = (WORD)sdTriggerEnabled(DeviceIndex,ControlState.TIMEBASE);

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

void CVCDlg::OnBtnStart() 
{
	// TODO: Add your control notification handler code here
	StartCapture = 1;
	int Result;
	Result = sdCaptureStart(DeviceIndex);

	Result = (WORD)sdTriggerEnabled(DeviceIndex,ControlState.TIMEBASE);
	SetTimer(0,10,NULL);
}

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