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

📄 usbdlg.cpp

📁 包括EZ811和SL811的文档
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// usbtestDlg.cpp : implementation file
//
#include "stdafx.h"
#include "usbtest.h"
#include "slusb.h"
#include "usbDlg.h"
#include <stdlib.h>


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

#define UsbDevID	0x4ce
#define V_EPP_ADDR_READ   0x41
#define V_EPP_DATA_READ   0x42
#define V_EPP_ADDR_WRITE  0x43
#define V_EPP_DATA_WRITE  0x44
#define V_EPP_BLOCK_READ  0x45
#define V_EPP_BLOCK_WRITE 0x46
#define V_CPU_POKE        0x47
#define V_CPU_PEEK        0x48
#define V_RAM_TEST        0x49
#define V_RAM_BLOCK_READ  0x4a
#define V_RAM_BLOCK_WRITE 0x4b

#define DEF_VIEWER "c:\\Program Files\\accessories\\mspaint.exe"
#define BMPFILENAME "usbfile.bmp"

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

/////////////////////////////////////////////////////////////////////////////
// CUsbtestDlg dialog

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
CUsbtestDlg::CUsbtestDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUsbtestDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUsbtestDlg)
	m_NumberOfImageLine = 100;
	m_ImageSize = 32766;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	hUsbLib = NULL;
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
void CUsbtestDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUsbtestDlg)
	DDX_Text(pDX, IDC_LINENUMBER, m_NumberOfImageLine);
	DDV_MinMaxUInt(pDX, m_NumberOfImageLine, 0, 4000);
	DDX_Text(pDX, IDC_IMAGESIZE, m_ImageSize);
	DDV_MinMaxUInt(pDX, m_ImageSize, 0, 655350);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUsbtestDlg, CDialog)
	//{{AFX_MSG_MAP(CUsbtestDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_RESET, OnReset)
	ON_BN_CLICKED(IDC_ABORT_BUTTON, OnAbortButton)
	ON_BN_CLICKED(IDC_VIEWIMAGE, OnViewimage)
	ON_BN_CLICKED(IDC_SEARCHIMAGEVIEWER, OnSearchimageviewer)
	ON_BN_CLICKED(IDC_OPEN_DEV_BUTTON, OnOpenDevButton)
	ON_BN_CLICKED(IDC_SL11R_DMEM_TEST_BUTTON, OnSl11rDmemTestButton)
	ON_BN_CLICKED(IDC_EPP_ADDR_READ_BUTTON, OnEppAddrReadButton)
	ON_BN_CLICKED(IDC_EPP_ADDR_WRITE_BUTTON, OnEppAddrWriteButton)
	ON_BN_CLICKED(IDC_EPP_DATA_READ_BUTTON, OnEppDataReadButton)
	ON_BN_CLICKED(IDC_EPP_DATA_WRITE_BUTTON, OnEppDataWriteButton)
	ON_BN_CLICKED(IDC_SRAM_TEST_BUTTON, OnSramTestButton)
	ON_BN_CLICKED(IDC_PEEK_BUTTON, OnPeekButton)
	ON_BN_CLICKED(IDC_POKE_BUTTON, OnPokeButton)
	ON_BN_CLICKED(IDC_WRITE_EPP_BLOCK_BUTTON, OnWriteEppBlockButton)
	ON_BN_CLICKED(IDC_WRITE_SRAM_BLOCK_BUTTON, OnWriteSramBlockButton)
	ON_BN_CLICKED(IDC_READ_SRAM_BLOCK_BUTTON, OnReadSramBlockButton)
	ON_BN_CLICKED(IDC_SAVE_FILE_BUTTON, OnSaveFileButton)
	ON_BN_CLICKED(IDC_READ_EPP_BLOCK_BUTTON, OnReadEppBlockButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUsbtestDlg message handlers

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
BOOL CUsbtestDlg::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);
	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
	ClearText();

	// get image viewer path
	CString profile_section;
	CString profile_entry;
	profile_section.LoadString(IDS_VIEWER_SECTION);
	profile_entry.LoadString(IDS_VIEWER_ENTRY);
	ViewerPath = AfxGetApp()->GetProfileString(profile_section, profile_entry, DEF_VIEWER);
	GetDlgItem(IDC_IMAGEVIEWER)->SetWindowText(ViewerPath);
	SetViewerIcon();

	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
void CUsbtestDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	// TODO: Add your message handler code here
	CloseUsbDev();
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
void CUsbtestDlg::Idle()
{
	MSG msg;
	if(::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
		::TranslateMessage(&msg);
		::DispatchMessage(&msg);
	}
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
int CUsbtestDlg::GetCharHeight(CDC* pdc)
{
	TEXTMETRIC tm;
	pdc->GetTextMetrics(&tm); 
    return (tm.tmHeight + tm.tmExternalLeading); 
}
		

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
void CUsbtestDlg::OnReset() 
{
	// TODO: Add your control notification handler code here
	ClearText();
	CloseUsbDev();
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
void CUsbtestDlg::ClearText() 
{
	EnableAllControl(FALSE);
	GetDlgItem(IDC_GETIMAGE)->SetWindowText("");
	GetDlgItem(IDC_SRAM_TEST)->SetWindowText("");
	GetDlgItem(IDC_DRAM_TEST)->SetWindowText("");
	GetDlgItem(IDC_TIMER)->SetWindowText("");
	GetDlgItem(IDC_OPEN_DEV)->SetWindowText("");
	GetDlgItem(IDC_SL11R_ADDR)->SetWindowText("0x0bf0");
	GetDlgItem(IDC_OPEN_DEV)->SetWindowText("");
	GetDlgItem(IDC_SL11R_DATA)->SetWindowText("0xaaaa");
	GetDlgItem(IDC_EPP_DATA_WRITE)->SetWindowText("0x0102");
	GetDlgItem(IDC_EPP_ADDR_WRITE)->SetWindowText("0x0102");
    GetDlgItem(IDC_SL11R_SIZE)->SetWindowText("0x0080");
	CProgressCtrl* pShowProgress = (CProgressCtrl*)GetDlgItem(IDC_PROGRESS);
	pShowProgress->SetPos(0);
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
void CUsbtestDlg::EnableAllControl(BOOL IsEnable) 
{
	GetDlgItem(IDC_POKE_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_PEEK_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_READ_EPP_BLOCK_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDOK)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_RESET)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_VIEWIMAGE)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_SEARCHIMAGEVIEWER)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_SRAM_TEST_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_SL11R_DMEM_TEST_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_ABORT_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_SL11R_DMEM_TEST_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_EPP_ADDR_READ_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_EPP_ADDR_WRITE_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_EPP_DATA_READ_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_EPP_DATA_WRITE_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_WRITE_EPP_BLOCK_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_WRITE_SRAM_BLOCK_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_READ_SRAM_BLOCK_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
	GetDlgItem(IDC_SAVE_FILE_BUTTON)->EnableWindow(IsEnable? TRUE:FALSE);
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
BOOL CUsbtestDlg::MakeBmpFileHeader(short linecount, short realsize)
{
	// bitmapfileheader struct
	BFH.bfType = 'MB'; 
    BFH.bfSize = sizeof(BFH)+sizeof(BIH)+sizeof(RGBQUAD)*256+linecount*realsize; 
    BFH.bfReserved1 = 0; 
    BFH.bfReserved2 = 0; 
    BFH.bfOffBits = sizeof(BFH)+sizeof(BIH)+sizeof(RGBQUAD)*256; 

	
	// bitmapinfoheader struct
	BIH.biSize = sizeof(BIH); 
    BIH.biWidth = realsize; 
    BIH.biHeight = linecount; 
    BIH.biPlanes = 1; 
    BIH.biBitCount = 8;
    BIH.biCompression = BI_RGB; 
    BIH.biSizeImage = 0; 
    BIH.biXPelsPerMeter = (LONG)(((float)(300*100))/(float)2.54); 
    BIH.biYPelsPerMeter = BIH.biXPelsPerMeter; 
    BIH.biClrUsed =0; 
    BIH.biClrImportant =0;

	// make color table
	RGBQUAD* lpColorTable;
	HGLOBAL hColorTable = ::GlobalAlloc(GHND, sizeof(RGBQUAD)*256);

	if(hColorTable != NULL){
		lpColorTable = (RGBQUAD*)GlobalLock(hColorTable);
		if(lpColorTable != NULL) {
			for(int i= 0; i<256; i++) {
				lpColorTable[i].rgbBlue = i;
				lpColorTable[i].rgbGreen = i;
				lpColorTable[i].rgbRed = i;
				lpColorTable[i].rgbReserved = 0;
			}
		}
		else {
			GlobalFree(hColorTable);
			AfxMessageBox("Can't allocate color table");
			return FALSE;
		}

	}
	else {
		AfxMessageBox("Can't allocate color table");
		return FALSE;
	}

	// write header information into bmpfile
	if(!BmpFile.Open(m_BmpFilePath, CFile::modeWrite|CFile::typeBinary)) {
		AfxMessageBox("Can't open bmpfile in making hader");
		if(hColorTable != NULL) {
			GlobalUnlock(hColorTable);
			GlobalFree(hColorTable);
		}
		return FALSE;
	}
	else {
		BmpFile.Write(&BFH, sizeof(BFH));
		BmpFile.Write(&BIH, sizeof(BIH));
		BmpFile.Write(lpColorTable, sizeof(RGBQUAD)*256);
		BmpFile.Close();
		return TRUE;
	}
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
void CUsbtestDlg::OnAbortButton() 
{ 
	// TODO: Add your control notification handler code here
	UserAbort = TRUE;
}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
void CUsbtestDlg::OnViewimage() 
{
	// TODO: Add your control notification handler code here
	// get viewer path
	CString exeviewer(ViewerPath);
	if(::GetFileAttributes(exeviewer) == 0xffffffff) AfxMessageBox("Image Viewer doesn't exist!");
	else {
		if(::GetFileAttributes(m_BmpFilePath) == 0xffffffff) WinExec(exeviewer, SW_SHOW);
		else {
			exeviewer +=" ";
			exeviewer += m_BmpFilePath;
			WinExec(exeviewer, SW_SHOW);
		}
	}


}

/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
CString CUsbtestDlg::GetBmpFilePath() 
{

	char temppath[MAX_PATH];
	char tempfilename[MAX_PATH];
	
	GetTempPath(MAX_PATH, temppath);
	GetTempFileName(temppath, "USB", 0, tempfilename);

	//after GetTempFileName, win95 create the file with size 
	// equals to zero, therefore, we have to delete it to
	// be safe
	::DeleteFile(tempfilename);

	CString BmpFilePath(tempfilename);
	int len = BmpFilePath.GetLength();
	while(BmpFilePath.GetAt(len-1) != '.') len--;
	BmpFilePath = BmpFilePath.Left(len);
	BmpFilePath += "bmp";
	return BmpFilePath;

}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
void CUsbtestDlg::OnSearchimageviewer() 
{
	// TODO: Add your control notification handler code here
	char BASED_CODE szFilter[] = "program files (*.exe) | *.exe||";
	
	CFileDialog imgviewer(TRUE, NULL, NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT, szFilter, this);
	if(imgviewer.DoModal() == IDOK) {
		ViewerPath = imgviewer.GetPathName();
		GetDlgItem(IDC_IMAGEVIEWER)->SetWindowText(ViewerPath);
		CString profile_section;
		profile_section.LoadString(IDS_VIEWER_SECTION);
		CString profile_entry;
		profile_entry.LoadString(IDS_VIEWER_ENTRY);
		AfxGetApp()->WriteProfileString(profile_section, profile_entry, ViewerPath);

		// retrieve icon from the image viewer
		SetViewerIcon();
	}
	
}
/////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////
void CUsbtestDlg::SetViewerIcon()
{
	HICON  hViewerIcon;
	SHFILEINFO  shfi;

	if (SHGetFileInfo (ViewerPath,0,&shfi,sizeof(SHFILEINFO),SHGFI_ICON)){
		hViewerIcon = shfi.hIcon;

		// Note that this button has been defined in the .RC file
		// to be of BS_ICON style
		// note: try to change the style of button at run time, but
		// fail. see BM_SETSTYLE and SetWindowLong()
		
		SendDlgItemMessage (IDC_VIEWIMAGE,BM_SETIMAGE,
			(WPARAM)IMAGE_ICON,
			(LPARAM)(HICON)hViewerIcon);

⌨️ 快捷键说明

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