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

📄 projectdlg.cpp

📁 采用windriver提供的API开发的D12应用程序源码
💻 CPP
字号:
// projectDlg.cpp : implementation file
//

#include "stdafx.h"
#include "project.h"
#include "projectDlg.h"


#include <stdlib.h>
#include <stdio.h>

#include "usb_diag_lib.h"
#include "wdu_lib.h"
#include "status_strings.h"
#include "utils.h"


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

#define DEFAULT_VENDOR_ID         0x0471
#define DEFAULT_PRODUCT_ID        0x0890
#define DEFAULT_LICENSE_STRING "12345abcde1234.license"

#define USE_DEFAULT 0xffff
#define ATTACH_EVENT_TIMEOUT 3// in seconds
#define TRANSFER_TIMEOUT 30000 // in msecs

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

typedef struct DEVICE_CONTEXT
{
    struct DEVICE_CONTEXT *pNext;
    WDU_DEVICE_HANDLE hDevice;
    DWORD dwVendorId;
    DWORD dwProductId;
    DWORD dwInterfaceNum;
    DWORD dwAlternateSetting;
} DEVICE_CONTEXT;

typedef struct DRIVER_CONTEXT
{
    HANDLE hEvent;
    HANDLE hMutex;
    DWORD dwDeviceCount;
    DEVICE_CONTEXT *deviceContextList;
    DEVICE_CONTEXT *pActiveDev;
    HANDLE hDeviceUnusedEvent;
} DRIVER_CONTEXT;

static char line[250];
DRIVER_CONTEXT DrvCtx;


BOOL DLLCALLCONV DeviceAttach(WDU_DEVICE_HANDLE hDevice, 
    WDU_DEVICE *pDeviceInfo, PVOID pUserData)
{
    DRIVER_CONTEXT *pDrvCtx = (DRIVER_CONTEXT *)pUserData;
    DEVICE_CONTEXT *pDevCtx, **ppDevCtx;
    DWORD dwInterfaceNum = pDeviceInfo->pActiveInterface[0]->pActiveAltSetting->Descriptor.bInterfaceNumber;
    DWORD dwAlternateSetting = pDeviceInfo->pActiveInterface[0]->pActiveAltSetting->Descriptor.bAlternateSetting;
        
    // Add our device to the device list
    pDevCtx = (DEVICE_CONTEXT *)malloc(sizeof(DEVICE_CONTEXT));

    BZERO(*pDevCtx);
    pDevCtx->hDevice = hDevice;
    pDevCtx->dwInterfaceNum = dwInterfaceNum;
    pDevCtx->dwVendorId = pDeviceInfo->Descriptor.idVendor;
    pDevCtx->dwProductId = pDeviceInfo->Descriptor.idProduct;
    pDevCtx->dwAlternateSetting = dwAlternateSetting;
    
    OsMutexLock(pDrvCtx->hMutex);
    for (ppDevCtx = &pDrvCtx->deviceContextList; *ppDevCtx; ppDevCtx = &((*ppDevCtx)->pNext));
    *ppDevCtx = pDevCtx;
    pDrvCtx->dwDeviceCount++;
    OsMutexUnlock(pDrvCtx->hMutex);
    
    OsEventSignal(pDrvCtx->hEvent);
   // Accept control over this device
    return TRUE;
}

VOID DLLCALLCONV DeviceDetach(WDU_DEVICE_HANDLE hDevice, PVOID pUserData)
{
    DRIVER_CONTEXT *pDrvCtx = (DRIVER_CONTEXT *)pUserData;
    DEVICE_CONTEXT **pCur;
    DEVICE_CONTEXT *pTmpDev;
    BOOL bDetachActiveDev = FALSE;
	
	AfxMessageBox("Device detachs from pc.");
	
    OsMutexLock(pDrvCtx->hMutex);
    for (pCur = &pDrvCtx->deviceContextList; 
	*pCur && (*pCur)->hDevice != hDevice; 
	pCur = &((*pCur)->pNext));
    
    if (*pCur == pDrvCtx->pActiveDev)
    {
        bDetachActiveDev = TRUE;
        pDrvCtx->pActiveDev = NULL;
    }
	
    pTmpDev = *pCur;
    *pCur = pTmpDev->pNext;
    free(pTmpDev);
    
    pDrvCtx->dwDeviceCount--;
    OsMutexUnlock(pDrvCtx->hMutex);
	
    if (bDetachActiveDev)
    {        
        OsEventWait(pDrvCtx->hDeviceUnusedEvent, INFINITE);
    }
}

void DeviceDiagMenu(DRIVER_CONTEXT *pDrvCtx)
{	
    WDU_DEVICE_HANDLE hDevice;
		
	OsMutexLock(pDrvCtx->hMutex);
	
	if (!pDrvCtx->pActiveDev)
		pDrvCtx->pActiveDev = pDrvCtx->deviceContextList;
	
	OsMutexUnlock(pDrvCtx->hMutex);
	
	OsEventReset(pDrvCtx->hDeviceUnusedEvent);
	
	OsMutexLock(pDrvCtx->hMutex);
	hDevice = pDrvCtx->pActiveDev->hDevice;
	OsMutexUnlock(pDrvCtx->hMutex);
	
	ReadPipes(hDevice);
	
	OsEventSignal(pDrvCtx->hDeviceUnusedEvent);	
}

void DeviceDiagMenuadder(DRIVER_CONTEXT *pDrvCtx)
{	
    WDU_DEVICE_HANDLE hDevice;
	
	OsMutexLock(pDrvCtx->hMutex);
	
	if (!pDrvCtx->pActiveDev)
		pDrvCtx->pActiveDev = pDrvCtx->deviceContextList;
	
	OsMutexUnlock(pDrvCtx->hMutex);
	
	OsEventReset(pDrvCtx->hDeviceUnusedEvent);
	
	OsMutexLock(pDrvCtx->hMutex);
	hDevice = pDrvCtx->pActiveDev->hDevice;
	OsMutexUnlock(pDrvCtx->hMutex);
	
	ReadPipesadder(hDevice);
	
	OsEventSignal(pDrvCtx->hDeviceUnusedEvent);	
}

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

/////////////////////////////////////////////////////////////////////////////
// CProjectDlg dialog

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

}

void CProjectDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CProjectDlg)
//	DDX_Control(pDX, IDC_POINT_COMBO, m_PointCombo);
//	DDX_Text(pDX, IDC_EDIT1, m_Edit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CProjectDlg, CDialog)
	//{{AFX_MSG_MAP(CProjectDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_START_BUTTON, OnStartButton)
	ON_BN_CLICKED(IDC__BUTTON, OnButton)
	ON_BN_CLICKED(IDC_SAVE_BUTTON, OnSaveButton)
	ON_BN_CLICKED(IDC_EXIT_BUTTON, OnExitButton)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CProjectDlg message handlers

BOOL CProjectDlg::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
    DWORD dwError;
    WORD wVendorId = 0;
    WORD wProductId = 0;
    WDU_DRIVER_HANDLE hDriver = 0;
	//    DRIVER_CONTEXT DrvCtx;
		
    WDU_MATCH_TABLE matchTable;
    WDU_EVENT_TABLE eventTable;
	
    BZERO(DrvCtx);
    
    wVendorId = USE_DEFAULT;
    wProductId = USE_DEFAULT;
	
	// use defaults
    if (wVendorId == USE_DEFAULT)
        wVendorId = DEFAULT_VENDOR_ID;
    if (wProductId == USE_DEFAULT)
        wProductId = DEFAULT_PRODUCT_ID;
	
	dwError = OsEventCreate(&DrvCtx.hEvent);
	if (dwError)
    {
        MessageBox("failed on OsEventCreate");
    }
	
	dwError = OsMutexCreate(&DrvCtx.hMutex);
	if (dwError)
    {
        MessageBox("failed on OsMutexCreate");
    }
	
	dwError = OsEventCreate(&DrvCtx.hDeviceUnusedEvent);
	if (dwError)
    {
        MessageBox("failed on OsEventCreate");
		
    }
	OsEventSignal(DrvCtx.hDeviceUnusedEvent);
	
	BZERO(matchTable);
    matchTable.wVendorId = wVendorId;
    matchTable.wProductId = wProductId;
	
	BZERO(eventTable);
    eventTable.pfDeviceAttach = DeviceAttach;
    eventTable.pfDeviceDetach = DeviceDetach;
    eventTable.pUserData = &DrvCtx;
	
	dwError = WDU_Init(&hDriver, &matchTable, 1, &eventTable, DEFAULT_LICENSE_STRING, WD_ACKNOWLEDGE);
	
	if (dwError)
    {
        MessageBox("failed to initialize USB driver");
    }
	
	dwError = OsEventWait(DrvCtx.hEvent, ATTACH_EVENT_TIMEOUT);
	if (dwError)
    {
        if (dwError==WD_TIME_OUT_EXPIRED)
        {
            MessageBox("Timeout expired for connection with the device.");
			
        }
        
        else
        {
			MessageBox("OsEventWait  failed on event.");
        }
    }        
	else
	{
		MessageBox("USB connect sucessfully.");
	} 	
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CProjectDlg::OnStartButton() 
{
	// TODO: Add your control notification handler code here
	cmd=2;
	dwPipeNum=0x01;
	dwSize=3;
	DeviceDiagMenu(&DrvCtx); 
	dwPipeNum=0x82;
	DeviceDiagMenuadder(&DrvCtx); 
}

void CProjectDlg::OnButton() 
{
	// TODO: Add your control notification handler code here
    CloseListening(&listenPipe);

	cmd=3;
	dwPipeNum=0x01;
	dwSize=3;
	DeviceDiagMenu(&DrvCtx); 
}

void CProjectDlg::OnSaveButton() 
{
	// TODO: Add your control notification handler code here
	
	int nBMBit=8;
	int nNewHeight=640;
	int nNewWidth=2048;
	int nNewByteWidth=WIDTHBYTES(nNewWidth*8);
	DWORD dwtSize =(long)nNewByteWidth * (long)nNewHeight * (long)nBMBit/8;
	BITMAPFILEHEADER bmFH;
	int nPaletteEntries;
	if( nBMBit == 8 )
		nPaletteEntries = 256;
	else
		nPaletteEntries = 0;
	bmFH.bfType = 19778;
	bmFH.bfSize = (DWORD) ( sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) +
		nPaletteEntries * sizeof(RGBQUAD)) +dwtSize;
	bmFH.bfReserved1 = bmFH.bfReserved2 = 0;
	bmFH.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 
		nPaletteEntries * sizeof(RGBQUAD);
	
	BITMAPINFO *FileBMP;
	char bmpBuf[5000];
	FileBMP = (BITMAPINFO *)bmpBuf;
	FileBMP->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
	FileBMP->bmiHeader.biWidth = nNewWidth;
	FileBMP->bmiHeader.biHeight = nNewHeight;
	FileBMP->bmiHeader.biPlanes = 1;
	FileBMP->bmiHeader.biBitCount = nBMBit;
	FileBMP->bmiHeader.biCompression = BI_RGB;
	FileBMP->bmiHeader.biSizeImage = 0;
	FileBMP->bmiHeader.biXPelsPerMeter = 0;
	FileBMP->bmiHeader.biYPelsPerMeter = 0;
	FileBMP->bmiHeader.biClrUsed = 0;
	FileBMP->bmiHeader.biClrImportant = 0;
	int k;
	if( nBMBit == 8 )
	{
		for( k = 0; k < 256; ++k)
		{
			FileBMP->bmiColors[k].rgbBlue = (BYTE)k;
			FileBMP->bmiColors[k].rgbGreen = (BYTE)k;
			FileBMP->bmiColors[k].rgbRed = (BYTE)k;
			FileBMP->bmiColors[k].rgbReserved = 0;
		}
	}
	
	CString FileName;
	CFile ImageFile;
	CFileException fe;
	PVOID lpgdd=malloc(dwtSize);
	if(!ImageFile.Open ("e:\\gwq.txt",CFile::modeReadWrite, &fe) )
	{
		MessageBox("can't open file");
		return;
	}
	ImageFile.Read(lpgdd,dwtSize);
	ImageFile.Close();
	{
		if(!ImageFile.Open ("e:\\gwq.bmp",CFile::modeCreate | CFile::modeReadWrite, &fe) )
		{
			MessageBox("can't open file");
			return;
		}
		ImageFile.Write( &bmFH, sizeof(BITMAPFILEHEADER) ); 
		ImageFile.Write( &FileBMP->bmiHeader, sizeof(BITMAPINFOHEADER) );
		for( k = 0;k < nPaletteEntries; ++k)
			ImageFile.Write( &FileBMP->bmiColors[k],sizeof(RGBQUAD) );
		
		ImageFile.Write( lpgdd, dwtSize );
		
		free(lpgdd);
		ImageFile.Close();
	}	
}

void CProjectDlg::OnExitButton() 
{
	// TODO: Add your control notification handler code here
	OnOK();
}

⌨️ 快捷键说明

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