⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbhostdlg.cpp

📁 USB2.0编程的代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// usbhostDlg.cpp : implementation file
//

#include "stdafx.h"
#include "usbhost.h"
#include "usbhostDlg.h"

#include "usb100.h"
#include "winioctl.h"
#include "usbdriver.h" 

#include "math.h"
#include "process.h"

#define PI 3.1415926

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

unsigned char PacketoutBuffer[1024];
bool g_KeepGoing = false;
void __cdecl SendThreadFunction(HANDLE hDevice);

/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CUsbhostDlg dialog

CUsbhostDlg::CUsbhostDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUsbhostDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUsbhostDlg)
		// 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);
}

CUsbhostDlg::~CUsbhostDlg()
{
	delete m_PointList;
}

void CUsbhostDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUsbhostDlg)
	DDX_Control(pDX, IDC_USB_LIST, m_UsbList);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUsbhostDlg, CDialog)
	//{{AFX_MSG_MAP(CUsbhostDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_DEVICE_BUTTON, OnDeviceButton)
	ON_BN_CLICKED(IDC_CONFIG_BUTTON, OnConfigButton)
	ON_BN_CLICKED(IDC_INTERFACE_BUTTON, OnInterfaceButton)
	ON_BN_CLICKED(IDC_ENDPOINT_BUTTON, OnEndpointButton)
	ON_BN_CLICKED(IDC_STRING_BUTTON, OnStringButton)
	ON_BN_CLICKED(IDC_CLEAR_BUTTON, OnClearButton)
	ON_BN_CLICKED(IDC_FRAME_BUTTON, OnFrameButton)
	ON_BN_CLICKED(IDC_END_BUTTON, OnEndButton)
	ON_BN_CLICKED(IDC_EXIT_BUTTON, OnExitButton)
	ON_BN_CLICKED(IDC_PHACKLE_BUTTON, OnPhackleButton)
	ON_BN_CLICKED(IDC_NHACKLE_BUTTON, OnNhackleButton)
	ON_BN_CLICKED(IDC_TRIANGLE_BUTTON, OnTriangleButton)
	ON_BN_CLICKED(IDC_SQUARE_BUTTON, OnSquareButton)
	ON_BN_CLICKED(IDC_SIN_BUTTON, OnSinButton)
	ON_BN_CLICKED(IDC_RANDOM_BUTTON, OnRandomButton)
	ON_BN_CLICKED(IDC_SEND_BUTTON, OnSendButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUsbhostDlg message handlers

BOOL CUsbhostDlg::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
	m_LeftDis=250;
	m_RightDis=15;
	m_UpDis=15;
	m_DownDis=200;

    m_LeftRim=50;
	m_RightRim=40;
    m_UpRim=40;
    m_DownRim=40;

	m_xStart=0;  
	m_yStart=-64;

    m_XLineStart.SetSize(20);
    m_xn=0;

    m_YLineStart.SetSize(20);
    m_yn=0;

	m_PointList=new PointP[1024];

	for(int i=0;i<1024;i++)      
	{		
		m_PointList[i].x = (float)i;
		m_PointList[i].y = 0;
	}

	index=0;
	BigSmall=true;
	
	strcpy(DeviceName,"Ezusb-0");

	OnPhackleButton();

	return TRUE; 
}

void CUsbhostDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else if (nID == SC_CLOSE)
	{
         if(g_KeepGoing)
		 {
			 MessageBox("请首先结束当前线程!", "错误", MB_ICONERROR | MB_OK);
		     return;
		 }	
         OnOK();
	}
	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 CUsbhostDlg::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();
	
		GetUDLRrect();
        
		GetRectBlc(0,-64,1024,320);
        
		CDC* pDC;  
        pDC = GetDC(); 		
		
		CBrush brush(RGB(0,0,0));
	    pDC->FillRect(InnerRect,&brush);
	    
		DrawBackLines(pDC);

		DrawCurve(pDC);

		DrawUDLRrect(pDC);
		
		DrawLab(pDC); 
		
		ReleaseDC(pDC);
	}
}

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

///////////////////////////////////////////////////////////////////
//
// 以下是画图的相关函数。
//
///////////////////////////////////////////////////////////////////
void CUsbhostDlg::GetUDLRrect()
{
    CRect rect;
    GetClientRect(&rect);

    m_InnerUHScreen = m_UpRim+m_UpDis;            
    m_InnerDHScreen = rect.bottom - m_DownRim-m_DownDis; 
    m_InnerLWScreen = m_LeftRim + m_LeftDis;        
	m_InnerRWScreen = rect.right - m_RightRim-m_RightDis;  

	Urect.top=m_UpDis;
	Urect.bottom=m_InnerUHScreen;
	Urect.left=m_LeftDis;
	Urect.right=rect.right-m_RightDis;

	Drect.top=m_InnerDHScreen;
	Drect.bottom=rect.bottom-m_DownDis;
	Drect.left=m_LeftDis;
	Drect.right=rect.right-m_RightDis;

	Lrect.top=m_InnerUHScreen;
	Lrect.bottom=m_InnerDHScreen;
    Lrect.left=m_LeftDis;
	Lrect.right=m_InnerLWScreen;

	Rrect.top=m_InnerUHScreen;
	Rrect.bottom=m_InnerDHScreen;
    Rrect.left=m_InnerRWScreen;
	Rrect.right=rect.right-m_RightDis;

	InnerRect.top=m_InnerUHScreen;
	InnerRect.bottom=m_InnerDHScreen;
    InnerRect.left=m_InnerLWScreen;
	InnerRect.right=m_InnerRWScreen; 

	TotalRect.top=m_UpDis;
	TotalRect.bottom=rect.bottom-m_DownDis;
    TotalRect.left=m_LeftDis;
	TotalRect.right=rect.right-m_RightDis;
}

void CUsbhostDlg::GetRectBlc(float xRectStart,float yRectStart,float xRectEnd,float yRectEnd)
{
	float m1,m2;  
	float x1,y1,x2,y2;
	x1=min(xRectStart,xRectEnd);
	x2=max(xRectStart,xRectEnd);
	y1=min(yRectStart,yRectEnd);
	y2=max(yRectStart,yRectEnd);
	
	float lx,ly;

	lx=x2-x1;	
    if(lx<0) return;  

    ly=y2-y1;
	if(ly<0) return;    

    m1=lx/(m_InnerRWScreen-m_InnerLWScreen);  
	m2=ly/(m_InnerDHScreen-m_InnerUHScreen);	
	m_XOriginScale =m1;     
	m_YOriginScale =m2;     
}

void CUsbhostDlg::DrawBackLines (CDC *pDC)
{
	float xx1;
	float yy1;
	float xx2;
	float yy2;

    XLPtoXVP(m_InnerLWScreen ,&xx1); 
	XLPtoXVP(m_InnerRWScreen ,&xx2);
	YLPtoYVP(m_InnerDHScreen ,&yy1);
	YLPtoYVP(m_InnerUHScreen ,&yy2);

    GetBackLines(xx1,yy1,xx2,yy2);
							
    CPen penGray;
    CPen* pOldPen;

    penGray.CreatePen(PS_SOLID | PS_COSMETIC, 1, RGB(128, 128, 128));

	pOldPen = pDC->SelectObject(&penGray); 

	int i;	
	for(i=0;i<m_XLineStart.GetSize();i++)
	{   		
	    pDC->MoveTo (m_XLineStart[i],m_InnerUHScreen);
	    pDC->LineTo (m_XLineStart[i],m_InnerDHScreen);
	}
	
    for(i=0;i<m_YLineStart.GetSize();i++)
	{   	    
	    pDC->MoveTo (m_InnerLWScreen,m_YLineStart[i]);
	    pDC->LineTo (m_InnerRWScreen,m_YLineStart[i]);
    }
	
    pDC->SelectObject (pOldPen);
}

void CUsbhostDlg::XLPtoXVP (int x,float *X)
{
   *X=m_xStart+(x-m_InnerLWScreen)*m_XOriginScale; 
}

void CUsbhostDlg::YLPtoYVP (int y,float *Y)
{  	
	*Y=m_yStart+(m_InnerDHScreen-y)*m_YOriginScale;	
}

void CUsbhostDlg::XVPtoXLP(float x,int *X)
{
	float TempX;

	TempX=((x-m_xStart)/m_XOriginScale+(float)0.5)+m_InnerLWScreen;
	if(TempX > 3000) TempX = 3000;
	*X=(int)TempX;
}

void CUsbhostDlg::YVPtoYLP(float y,int *Y)
{
	float TempY;	

	TempY=m_InnerDHScreen-((y-m_yStart)/m_YOriginScale+(float)0.5);
	if(TempY > 3000) TempY = 3000;
	*Y = (int)TempY;
}

void CUsbhostDlg::GetBackLines(float xRectStart,float yRectStart,float xRectEnd,float yRectEnd)
{
		
	if(m_XLineStart.GetSize()>0)
	        m_XLineStart.RemoveAll();
	if(m_YLineStart.GetSize()>0)
	        m_YLineStart.RemoveAll();

	    float xL;
		float xd;
		float xLine;

        xL=xRectEnd-xRectStart;			
		if(xL<0) xL=-xL;
		if(xL==0) return;
        
		xd=xL/8;  
        
        m_xLineStart[0]=xRectStart;     
		m_XLineStart.Add(m_InnerLWScreen); 
		m_xn=0;		

		xLine=xd;

		while(xLine<xL)
		{
			m_xn=m_xn+1;
			
			m_xLineStart[m_xn]=m_xn*xd;	  
			m_XLineStart.Add((int)(xLine/m_XOriginScale)+m_InnerLWScreen); 
						
			xLine=xLine+xd;  
		}
		
        m_xn=m_xn+1;
		m_xLineStart[m_xn]=xRectEnd;
		m_XLineStart.Add((int)((xRectEnd-xRectStart)/m_XOriginScale)+m_InnerLWScreen-1);

		float yL;
		float yd;
		float yLine;

		yL=yRectEnd-yRectStart;
		if(yL<0) yL=-yL;
		if(yL==0) return;
		
		yd=yL/6;
				
        m_yLineStart[0]=yRectStart;
		m_yn=0;
		m_YLineStart.Add(m_InnerDHScreen-1); 
		        
		yLine=yd;
		while(yLine<yL)              
		{
			m_yn=m_yn+1;

			m_yLineStart[m_yn]=m_yn*yd;
			m_YLineStart.Add(m_InnerDHScreen-(int)(yLine/m_YOriginScale));

			yLine=yLine+yd;
		}

		m_yn=m_yn+1;
		m_yLineStart[m_yn]=yRectEnd;
		m_YLineStart.Add(m_InnerDHScreen-(int)((yRectEnd-yRectStart)/m_YOriginScale));
}

void CUsbhostDlg::DrawCurve (CDC *pDC)
{
	int x1,y1;
    
	CPen npen((int)PS_SOLID,(int)1,RGB(255,255,0));
    
    CPen* npOldPen=pDC->SelectObject (&npen);

    pDC->SetROP2(R2_COPYPEN);  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -