// VCPrjDlg.cpp : implementation file
//

#include "stdafx.h"
#include "VCPrj.h"
#include "VCPrjDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CVCPrjDlg dialog

CVCPrjDlg::CVCPrjDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CVCPrjDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CVCPrjDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CVCPrjDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CVCPrjDlg)
	DDX_Control(pDX, IDC_STC_LOGIC_DATA, m_stcLogicData);
	DDX_Control(pDX, IDC_STC_DRAW, m_stcDraw);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CVCPrjDlg, CDialog)
	//{{AFX_MSG_MAP(CVCPrjDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BTN_READ, OnBtnRead)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVCPrjDlg message handlers

BOOL CVCPrjDlg::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
	TRIG TrigA;//
	TRIG TrigB;//
	UCHAR nSampleRate = 17;//2MSa/s
	m_nSampleDepth = 2048;//2K
	m_nHTrigPos = 50;//Range:5 ~ 95
	double dbThresholdA = 1.5;//1.5V
	double dbThresholdB = 1.5;//1.5V
	short nTrigAEnable = 0;
	short nTrigBEnable = 0;
	UCHAR nLogic = 0;

	TrigA.nEdgeSignal = 0;
	TrigA.nEdgeSlope = 0;
	TrigA.Equ_Dat = 1000;
	TrigA.Equ_Sh = 0xFFFFFFFF;
	TrigA.Equ_So = 2;
	TrigA.Intr_Equ = FALSE;
	TrigA.Intr_Time = FALSE;
	TrigA.Intr_Range = FALSE;
	TrigA.Range_Max = 15;
	TrigA.Range_Min = 0;
	TrigA.Range_Sh = 0xFFFFFFFF;
	TrigA.Range_Mo = 0;
	TrigA.Time_Max = 10;
	TrigA.Time_Min = 1;
	TrigA.Time_Mo = 0;
	

	TrigB.nEdgeSignal = 1;
	TrigB.nEdgeSlope = 0;
	TrigB.Equ_Dat = 15;
	TrigB.Equ_Sh = 0xFFFFFFFF;
	TrigB.Equ_So = 2;
	TrigB.Intr_Equ = FALSE;
	TrigB.Intr_Time = FALSE;
	TrigB.Intr_Range = FALSE;
	TrigB.Range_Max = 15;
	TrigB.Range_Min = 0;
	TrigB.Range_Sh = 0xFFFFFFFF;
	TrigB.Range_Mo = 0;
	TrigB.Time_Max = 10;
	TrigB.Time_Min = 1;
	TrigB.Time_Mo = 0;


	m_nCurDev = 0;
	if(DevConnect(m_nCurDev))
	{
		InitDevice(m_nCurDev);//This function must be first to call....
		Set_Sample_Rate(m_nCurDev,nSampleRate);
		Set_SampleDepth(m_nCurDev,m_nSampleDepth);
		Set_PWMV(m_nCurDev,dbThresholdA,dbThresholdB);
		Set_Pre_Tri(m_nCurDev,m_nHTrigPos);
		Set_Trig_En(m_nCurDev,nTrigAEnable,nTrigBEnable);//Immediately trigger
		Set_Tri_Logic(m_nCurDev,nLogic);
		Set_Trig_Parameter(m_nCurDev,0,&TrigA);
		Set_Trig_Parameter(m_nCurDev,1,&TrigB);
	}
	else
	{
		AfxMessageBox(_T("No device was found!"));
	}
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CVCPrjDlg::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 CVCPrjDlg::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 CVCPrjDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CVCPrjDlg::OnBtnRead() 
{
	// TODO: Add your control notification handler code here
	USHORT nCurDev = 0;
	ULONG nSta = 0;
	ULONG* pSrcData = NULL;
	ULONG nLogicData = 0;
	CRect rc;
	ULONG nSH = 0xFFFFFFFF;//All pin
	USHORT nStyle = 0;//0:Analog; 1:Digital;
	USHORT nFormat = 2;//0:binary,1:decimal,2:Hex
	CRect rcBus;
	long nBusHeight = 0;
	long nPinHeight = 15;
	CRect rcPin;
	long j = 1000;

	if(DevConnect(nCurDev))
	{
		SetCmdLA(nCurDev);//Send cmd to device
		nSta = ReadCollectStatus(nCurDev);
		while(nSta != 1)
		{
			Sleep(1);
			j--;
			nSta = ReadCollectStatus(nCurDev);
			if(j < 0)
				break;
		}
		if(nSta == 1)
		{
			pSrcData = new ULONG[m_nSampleDepth];
			memset(pSrcData,0,m_nSampleDepth * sizeof(ULONG));
			if(ReadSrcData(nCurDev,pSrcData,m_nSampleDepth,m_nHTrigPos))
			{
				m_stcDraw.GetClientRect(&rc);
				m_stcDraw.GetDC()->FillSolidRect(rc,RGB(0,0,0));
				rcBus = rc;
				if(nStyle == 0)
				{
					nBusHeight = 50;
				}
				else
				{
					nBusHeight = nPinHeight;
				}
				rcBus.bottom = rcBus.top + nBusHeight;
				//Draw Bus
				HTDrawLABusSignal(m_stcDraw.GetDC()->GetSafeHdc(),&rcBus,RGB(0,255,0),pSrcData,m_nSampleDepth,m_nSampleDepth / 2,rc.Width()*1.0/m_nSampleDepth,nSH,nStyle,nFormat);
				//Draw Single Signal
				rcPin = rcBus;
				rcPin.top = rcBus.bottom;
				rcPin.bottom += nPinHeight;
				for(int i = 0; i < 32; i++)
				{
					HTDrawLASquareSignal(m_stcDraw.GetDC()->GetSafeHdc(),rcPin,RGB(0,255,0),pSrcData,m_nSampleDepth,m_nSampleDepth / 2,rc.Width()*1.0/m_nSampleDepth,i);
					rcPin.top = rcPin.bottom;
					rcPin.bottom += nPinHeight;
					if(rcPin.bottom > rc.bottom)
					{
						break;
					}
				}
			}
			else
			{
				AfxMessageBox(_T("Read data fails!"));
			}
		}
		else
		{
			;//waiting...
		}
		ULONG data = 0;
		if(ReadLogicData(nCurDev,&data))
		{
			nLogicData = data;
			CString str;
			str.Format(_T("Logic Data = 0x%X"),nLogicData);
			m_stcLogicData.SetWindowText(str);
		}
	}
	else
	{
		AfxMessageBox(_T("No device was found!"));
	}
}
