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

📄 hid_blinkydlg.cpp

📁 HID 设备VC读写例程
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// HID_BlinkyDlg.cpp : implementation file
//

//-----------------------------------------------------------------------------
// HEADER FILES
//-----------------------------------------------------------------------------

#include "stdafx.h"
#include "HID_Blinky.h"
#include "HID_BlinkyDlg.h"
#include ".\HID_Blinkydlg.h"
#include "dbt.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

//-----------------------------------------------------------------------------
// FUNCTION PROTOTYPES
//-----------------------------------------------------------------------------
static DWORD WINAPI InterruptThreadProc(LPVOID lpParameter);

//-----------------------------------------------------------------------------
// DEFINITIONS
//-----------------------------------------------------------------------------

//-----------------------------------------------------------------------------
// Report IDs
//-----------------------------------------------------------------------------
#define OUT_Blink_Pattern  0x01
#define OUT_Blink_Enable   0x02
#define OUT_Blink_Rate     0x03
#define IN_Blink_Selector  0x04
#define IN_Blink_Stats     0x05
#define FEATURE_Blink_Dimmer 0x06

//-----------------------------------------------------------------------------
// Report Sizes
//-----------------------------------------------------------------------------
#define OUT_Blink_PatternSize      0x08
#define OUT_Blink_EnableSize       0x01
#define OUT_Blink_RateSize         0x02
#define IN_Blink_SelectorSize      0x01
#define IN_Blink_StatsSize         0x02
#define FEATURE_Blink_DimmerSize   0x01

//-----------------------------------------------------------------------------
// HID_BLINKY USB Identification Information
//-----------------------------------------------------------------------------
#define HID_Blinky_VID 0x10C4
#define HID_Blinky_PID 0x82CD

CHID_BlinkyDlg::CHID_BlinkyDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHID_BlinkyDlg::IDD, pParent)
	, m_Selection(_T(""))
	, m_BlinkRate(100)
	, m_Stat1(_T(""))
	, m_Stat2(_T(""))
	, m_T1LED1(FALSE)
	, m_T1LED2(FALSE)
	, m_T2LED1(FALSE)
	, m_T2LED2(FALSE)
	, m_T3LED1(FALSE)
	, m_T3LED2(FALSE)
	, m_T4LED1(FALSE)
	, m_T4LED2(FALSE)
	, m_T5LED1(FALSE)
	, m_T5LED2(FALSE)
	, m_T6LED1(FALSE)
	, m_T6LED2(FALSE)
	, m_T7LED1(FALSE)
	, m_T7LED2(FALSE)
	, m_T8LED1(FALSE)
	, m_T8LED2(FALSE)
	, m_CustomBlinkingEnabled(FALSE)
	, m_RadioCustom(TRUE)
	, m_RadioStandard(FALSE)
	, m_Blink_Enable(FALSE)
	, m_RadioModeSelect(0)
	, m_Slider(0)
	, m_DeviceStatus(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHID_BlinkyDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	DDX_Text(pDX, IDC_EDIT1, m_Selection);
	DDX_Text(pDX, IDC_EDIT3, m_BlinkRate);
	DDX_Text(pDX, IDC_Stat1, m_Stat1);
	DDX_Text(pDX, IDC_Stat2, m_Stat2);
	DDX_Check(pDX, IDC_T1LED1, m_T1LED1);
	DDX_Check(pDX, IDC_T1LED2, m_T1LED2);
	DDX_Check(pDX, IDC_T2LED1, m_T2LED1);
	DDX_Check(pDX, IDC_T2LED2, m_T2LED2);
	DDX_Check(pDX, IDC_T3LED1, m_T3LED1);
	DDX_Check(pDX, IDC_T3LED2, m_T3LED2);
	DDX_Check(pDX, IDC_T4LED4, m_T4LED1);
	DDX_Check(pDX, IDC_T4LED2, m_T4LED2);
	DDX_Check(pDX, IDC_T5LED1, m_T5LED1);
	DDX_Check(pDX, IDC_T5LED2, m_T5LED2);
	DDX_Check(pDX, IDC_T6LED1, m_T6LED1);
	DDX_Check(pDX, IDC_T6LED2, m_T6LED2);
	DDX_Check(pDX, IDC_T7LED1, m_T7LED1);
	DDX_Check(pDX, IDC_T7LED2, m_T7LED2);
	DDX_Check(pDX, IDC_T8LED1, m_T8LED1);
	DDX_Check(pDX, IDC_T8LED2, m_T8LED2);
	DDX_Check(pDX, IDC_CHECK_Blink_Enable, m_Blink_Enable);
	DDX_Slider(pDX, IDC_SLIDER1, m_Slider);
	DDX_Text(pDX, IDC_EDIT2, m_DeviceStatus);
}

BEGIN_MESSAGE_MAP(CHID_BlinkyDlg, CDialog)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_Connect, OnBnClickedConnect)
	ON_WM_TIMER()
ON_BN_CLICKED(IDC_DISCONNECT, OnBnClickedDisconnect)
ON_BN_CLICKED(IDC_Rate, OnBnClickedRate)
ON_BN_CLICKED(IDC_GetStats, OnBnClickedGetstats)
ON_BN_CLICKED(IDC_UpdateCustomBlinking, OnBnClickedUpdatecustomblinking)
ON_BN_CLICKED(IDC_RADIOStandard, OnBnClickedRadiostandard)
ON_BN_CLICKED(IDC_RADIOCustom, OnBnClickedRadiocustom)
ON_BN_CLICKED(IDC_CHECK_Blink_Enable, OnBnClickedCheckBlinkEnable)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER1, OnNMReleasedcaptureSlider1)
ON_WM_DEVICECHANGE()
ON_BN_CLICKED(IDCANCEL, OnBnClickedCancel)
ON_BN_CLICKED(IDC_HELP_CONNECTION, OnBnClickedHelpConnection)
ON_BN_CLICKED(IDC_HELP_STANDARD_BLINK, OnBnClickedHelpStandardBlink)
ON_BN_CLICKED(IDC_IDC_HELP_CUSTOM_BLINK, OnBnClickedIdcHelpCustomBlink)
ON_BN_CLICKED(IDC_HELP_STATS, OnBnClickedHelpStats)
ON_BN_CLICKED(IDC_BLINK_CONTROLS, OnBnClickedBlinkControls)
END_MESSAGE_MAP()



//-----------------------------------------------------------------------------
// OnInitDialog
//-----------------------------------------------------------------------------
//
// Routine executes when the program begins.  It configures the dialog
// box to be notified whenever devices are attached or removed from the
// USB, initializes dialog box variables, and configures a timer to
// overflow at a rate of 100 ms.
//
BOOL CHID_BlinkyDlg::OnInitDialog()
{
	CDialog::OnInitDialog();
	RegisterNotification();            // dialog box will be notified whenever
	                                   // a USB device is attached or removed
	                                   // from the USB

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

	Init_Variables();				   // Initialize on-screen variables
	                                   // and device-specific information
	SetTimer (1,100,NULL);			   // Create timer to be used to
	                                   // update on-screen information

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

//-----------------------------------------------------------------------------
// OnPaint
//-----------------------------------------------------------------------------
//
// 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 CHID_BlinkyDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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();
	}
}

//-----------------------------------------------------------------------------
// OnQueryDragIcon
//-----------------------------------------------------------------------------
//
// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
//
HCURSOR CHID_BlinkyDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}

//-----------------------------------------------------------------------------
// OnBnClickedConnect
//-----------------------------------------------------------------------------
//
// Routine is called when the "Activate" button is pressed.  Routine
// checks to make sure that a device is attached, and then attempts
// to connect to an attached device.  If connected, the routine
// creates a thread that polls for received INTERRUPT IN reports.
//
void CHID_BlinkyDlg::OnBnClickedConnect()
{
	DWORD results;

	// Check to see if any HID Blinky devices are attached to USB
	results = HID_Blinky.GetConnectedDeviceNum(HID_Blinky_VID, HID_Blinky_PID);
	// Number of attached devices with matching PID and VID is stored
	// in results
	if (results != 0)
	{
		// Attempt to connect to attached device
		results = HID_Blinky.Open(results - 1, HID_Blinky_VID, HID_Blinky_PID);

		// If successfully attached, initialize system for operation
		if (results == HID_DEVICE_SUCCESS)
		{
			// Update "Device Status" text
			m_DeviceStatus = "Connected, Active";
			UpdateData (FALSE);

			// Set Get Report and Set Report time-out values to 100 ms
			HID_Blinky.SetTimeouts (100,100);

			// Create thread that polls for INTERRUPT IN reports
			HID_RX_THREAD = CreateThread (NULL,NULL,
				InterruptThreadProc,this,0,0);

			// Configure attached and connected HID Blinky for operation
			Init_Device ();
		}
	}

}

//-----------------------------------------------------------------------------
// OnBnClickedDisconnect
//-----------------------------------------------------------------------------
//
// Routine executes whenever the "Deactivate" button is pressed.  Routine
// closes the device, terminates the thread used to poll for INTERRUPT IN
// reports, and updates the device status text.
//
void CHID_BlinkyDlg::OnBnClickedDisconnect()
{
	if (HID_Blinky.IsOpened())
	{
		if (HID_Blinky.Close() == HID_DEVICE_SUCCESS)
		{
			// Terminate the receive reports thread
			RXthreadmaycontinue = FALSE;
			TerminateThread (HID_RX_THREAD, NULL);

			// Update "Device Status" text box
			m_DeviceStatus = "Connected, Idle";
			UpdateWindow = TRUE;
		}
		else
		{
			// Do nothing
		}
	}
}

//-----------------------------------------------------------------------------
// OnBnClickedRate
//-----------------------------------------------------------------------------
//
// Routine executes whenever the "Update Rate" button is pressed.
//
void CHID_BlinkyDlg::OnBnClickedRate()
{
	Update_BlinkRate ();
}

//-----------------------------------------------------------------------------
// OnBnClickedGetstats
//-----------------------------------------------------------------------------
//
// Routine is called when the "Get Stats" button is pressed.  It calls
// the routine that retrieves the information and then outputs
// it to the screen.
//
void CHID_BlinkyDlg::OnBnClickedGetstats()
{
	unsigned char Stat1, Stat2;

	Update_Stats (&Stat1, &Stat2);     // Retrieve data from device

	m_Stat1 = "";                      // Format output strings and
	m_Stat2 = "";                      // Output their values
	m_Stat1.Format("%d %%",Stat1);
	m_Stat2.Format("%d %%",Stat2);
	UpdateWindow = TRUE;			   // Set OnTimer routine to update
	                                   // output when called
}

//-----------------------------------------------------------------------------
// OnBnClickedUpdatecustomblinking
//-----------------------------------------------------------------------------
//
// Routine is called whenever the Update Pattern button is pressed.
// If Custom blinking mode is selected, the device is configured
// to blink with Custom settings.
//
void CHID_BlinkyDlg::OnBnClickedUpdatecustomblinking()
{
	if (BlinkModeSelect == CUSTOM)
		Init_CustomBlinking ();		   // Outputs pattern to device
}

//-----------------------------------------------------------------------------
// OnBnClickedRadiostandard
//-----------------------------------------------------------------------------
//
// Routine is executed whenever the "Standard" radio button in the dialog
// box is pressed.  If the system is switching from Custom to
// Standard, the device is configured operate in Standard mode.
//
void CHID_BlinkyDlg::OnBnClickedRadiostandard()
{
	// If the device is currently configured to operate in
	// Custom mode, switch to Standard mode and reconfigure device
	if (BlinkModeSelect == CUSTOM)
	{
		BlinkModeSelect = STANDARD;
		Init_StandardBlinking ();
	}

}

//-----------------------------------------------------------------------------
// OnBnClickedRadiocustom
//-----------------------------------------------------------------------------
//
// Routine is executed whenever the "Custom" radio button in the dialog
// box is pressed.  If the system is switching from standard to
// custom, the device is configured operate in Custom mode.
//
void CHID_BlinkyDlg::OnBnClickedRadiocustom()
{
	// If the device is currently configured to operate in
	// Standard mode, switch to Custom mode and reconfigure device
	if (BlinkModeSelect == STANDARD)
	{
		BlinkModeSelect = CUSTOM;
		Init_CustomBlinking ();
	}

⌨️ 快捷键说明

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