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

📄 hid_blinkydlg.cpp

📁 HID 设备VC读写例程
💻 CPP
📖 第 1 页 / 共 3 页
字号:
}

//-----------------------------------------------------------------------------
// OnBnClickedCheckBlinkEnable
//-----------------------------------------------------------------------------
//
// Routine is called whenever the "Enable Blinking" box is either checked
// or unchecked.  The routine first captures the checked/unchecked value,
// then initializes the device for either Custom or Standard mode,
// and then either enables or disables device LED blinking.
//
void CHID_BlinkyDlg::OnBnClickedCheckBlinkEnable()
{
	UpdateData(TRUE);                  // capture m_Blink_Enable value

	// Configure HID_Blinky to either custom or standar mode if
	// blinking is enabled
	if (m_Blink_Enable == TRUE)
	{
		if (BlinkModeSelect == CUSTOM)
			Init_CustomBlinking ();
		else
			Init_StandardBlinking ();
	}
	// Transmit blink enable setting to the device
	Update_BlinkEnable();
}


//-----------------------------------------------------------------------------
// OnNMReleasedcaptureSlider1
//-----------------------------------------------------------------------------
//
// Routine is called whenever the slider is released by the mouse.
// The slider value is captured by calling UpdateData, and then
// Update_BlinkDimmer is called to transmit the report containing
// the data.
//
void CHID_BlinkyDlg::OnNMReleasedcaptureSlider1(NMHDR *pNMHDR, LRESULT *pResult)
{
    UpdateData(TRUE);                  // Captures slider value in m_Slider
	Update_BlinkDimmer ();             // Transmit new value to HID_Blinky
}

//-----------------------------------------------------------------------------
// OnBnClickedCancel
//-----------------------------------------------------------------------------
// Routine shuts down notification and the timers whenever the 'Cancel' button
// is pressed.
//
void CHID_BlinkyDlg::OnBnClickedCancel()
{
	UnregisterNotification();             // Close notification handle
	KillTimer(1);                         // Kill timer used to update screen
	OnCancel();                           // Call base class function
}


//-----------------------------------------------------------------------------
// NOTIFICATION ROUTINES
//-----------------------------------------------------------------------------
//
// These routines enable the dialog box to monitor and respond to USB
// devices being connected to and disconnected from the USB.

//-----------------------------------------------------------------------------
// RegisterNotification
//-----------------------------------------------------------------------------
//
// Routine registers the notification handle so that device
// connect/disconnect information  initiates a call
// to the OnDeviceChange routine.  This allows the system to
// respond to devices being connected or unexpectedly removed
// from the USB.
//
void CHID_BlinkyDlg::RegisterNotification()
{
	// Register device notification
	DEV_BROADCAST_DEVICEINTERFACE devIF = {0};
	GUID		hidGuid;

	HidD_GetHidGuid(&hidGuid);

	devIF.dbcc_size = sizeof(devIF);
	devIF.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
	devIF.dbcc_classguid  = hidGuid;

	m_hNotifyDevNode = RegisterDeviceNotification(GetSafeHwnd(),
		&devIF, DEVICE_NOTIFY_WINDOW_HANDLE);
}

//-----------------------------------------------------------------------------
// UnregisterNotification
//-----------------------------------------------------------------------------
//
// Routine unregisters the notification handle so that device
// connect/disconnect information no longer initiates a call
// to the OnDeviceChange routine.
//
void CHID_BlinkyDlg::UnregisterNotification()
{
	// Unegister device notification
	if(NULL != m_hNotifyDevNode)
	{
		UnregisterDeviceNotification(m_hNotifyDevNode);
		m_hNotifyDevNode = INVALID_HANDLE_VALUE;
	}
}

//-----------------------------------------------------------------------------
// OnDeviceChange
//-----------------------------------------------------------------------------
//
// Routine is called whenever the dialog box receives notification that
// a device has either been attached to or removed from USB.  This fuction
// requires that the function RegisterNotification has been executed
// to operate properly.
//
BOOL CHID_BlinkyDlg::OnDeviceChange(UINT nEventType, DWORD dwData)
{
	switch(nEventType)
	{
		// A device has been inserted and is now available.
		case DBT_DEVICEARRIVAL:
			// Check to see if newly connected device has VID and PID
			// matching HID_Blinky's VID and PID
			if (HID_Blinky.GetConnectedDeviceNum(HID_Blinky_VID,HID_Blinky_PID))
			{
				// Update Device Status text
				m_DeviceStatus = "Connected, Idle";
				UpdateWindow = TRUE;
			}
			break;

		// A device has been removed from USB.
		case DBT_DEVICEREMOVECOMPLETE:

			// Check that the HID_Blinky device was the removed device
			if (!HID_Blinky.GetConnectedDeviceNum(HID_Blinky_VID,HID_Blinky_PID))
			{
				// Call routine that will terminate communication and
				// destroy a receive thread, if one has been created
				OnBnClickedDisconnect ();
				m_DeviceStatus = "Disconnected";
				UpdateWindow = TRUE;
			}
			break;

		default:
			break;
	}

	return TRUE;
}




//-----------------------------------------------------------------------------
// HID RECEIVE THREAD CREATION AND INTERRUPT ROUTINES
//-----------------------------------------------------------------------------
//
// These routines enable the dialog box to receive INTERRUPT IN reports
// and process received data.
//

//-----------------------------------------------------------------------------
// HIDcallback
//-----------------------------------------------------------------------------
//
// This routine is called by the thread created to receive INTERRUPT IN
// reports from the device.  The routine checks the report ID and
// processes the received data.
//
void CHID_BlinkyDlg::HIDcallback (BYTE* reportbuffer)
{
	// check report ID and determine
	switch(reportbuffer[0])
	{
		// new blink pattern has been requested, process request
		case(IN_Blink_Selector):
		{
			// if selection data shows that new pattern has been selected
			if(SelectedPattern != (reportbuffer[1] * NumPatterns / 256))
			{
				// take 8-bit potentiometer value, find selection based
				// on number of patterns
				SelectedPattern = reportbuffer[1] * NumPatterns / 256;
				Update_BlinkPattern ();
			}
		}
		break;
		default:
		break;
	}
}

//-----------------------------------------------------------------------------
// HID_InterruptGetReport
//-----------------------------------------------------------------------------
//
// Routine is called by the thread created to poll for recieved INTERRUPT
// IN reports.  The routine returns the results of the poll.  If the
// transfer is successful, data bytes will be stored inside BYTE* reportbuffer.
//
DWORD CHID_BlinkyDlg::HID_InterruptGetReport (BYTE* reportbuffer)
{
	DWORD results;
	return HID_Blinky.GetReport_Interrupt(reportbuffer,
		HID_Blinky.GetInputReportBufferLength(),1,&results);
}

//-----------------------------------------------------------------------------
// InterruptThreadProc
//-----------------------------------------------------------------------------
//
// Routine is called from the thread created when the "Connect" button is
// pressed and communication begins.  This routine continuously attempts
// to retrieve IN INTERRUPT reports from the bus.  If a report is received,
// the routine then calls the dialog box HID callback routine to process
// the received data.
//
static DWORD WINAPI InterruptThreadProc(LPVOID lpParameter)
{
	CHID_BlinkyDlg* dlg;
	dlg = (CHID_BlinkyDlg*)lpParameter;
	BYTE reportbuffer[256];
	DWORD status;

	dlg->RXthreadmaycontinue = TRUE;   // Variable will be cleared whenever
									   // device is disconnected

	while (dlg->RXthreadmaycontinue == TRUE)
	{
		// Attempt to retrieve a report
		status = dlg->HID_InterruptGetReport (reportbuffer);
		// If a report has been recieved, call the callback routine
		// to process the data stored in reportbuffer
		if (status == HID_DEVICE_SUCCESS)
		{
			dlg->HIDcallback (reportbuffer);
		}
	}

	return 1;
}




//-----------------------------------------------------------------------------
// OnTimer
//-----------------------------------------------------------------------------
//
// Timer is configured to call this routine every 100 ms.  If UpdateWindow
// has been set, on-screen dialog values are saved and updated.
//
void CHID_BlinkyDlg::OnTimer(UINT nIDEvent)
{
	if (UpdateWindow == TRUE)
	{
		// Save all values to temporary variables
	    CString m_Selection_Temp = m_Selection;
		CString m_Stat1_Temp = m_Stat1;
		CString m_Stat2_Temp = m_Stat2;
		CString m_DeviceStatusTemp = m_DeviceStatus;
		// Retrieve values from text boxes on screen
		UpdateData(TRUE);

		CustomBlinking_SaveState ();   // Format custom blinking pattern buffer

		// Repaint values from temporary variables
		// where needed
		m_Selection = m_Selection_Temp;
		m_Stat1 = m_Stat1_Temp;
		m_Stat2 = m_Stat2_Temp;
		m_DeviceStatus = m_DeviceStatusTemp;
		// Output values to screen
		UpdateData(FALSE);
		UpdateWindow = FALSE;		   // Clear flag
	}

	CDialog::OnTimer(nIDEvent);		   // Call base class function
}



//-----------------------------------------------------------------------------
// CustomBlinking_SaveState
//-----------------------------------------------------------------------------
//
// Routine formats the CustomBlinkingPattern buffer with the data retrieved
// from the check boxes in the dialog screen.  If a box is checked,
// that corresponding bit in the blinking pattern will be set to 1.
//
void CHID_BlinkyDlg::CustomBlinking_SaveState ()
{
	CustomBlinkingPattern[0] = 0;
	if (m_T1LED1 == TRUE)
		CustomBlinkingPattern[0] |= 0x01;
	if (m_T1LED2 == TRUE)
		CustomBlinkingPattern[0] |= 0x02;

	CustomBlinkingPattern[1] = 0;
	if (m_T2LED1 == TRUE)
		CustomBlinkingPattern[1] |= 0x01;
	if (m_T2LED2 == TRUE)
		CustomBlinkingPattern[1] |= 0x02;

	CustomBlinkingPattern[2] = 0;
	if (m_T3LED1 == TRUE)
		CustomBlinkingPattern[2] |= 0x01;
	if (m_T3LED2 == TRUE)
		CustomBlinkingPattern[2] |= 0x02;

	CustomBlinkingPattern[3] = 0;
	if (m_T4LED1 == TRUE)
		CustomBlinkingPattern[3] |= 0x01;
	if (m_T4LED2 == TRUE)
		CustomBlinkingPattern[3] |= 0x02;

	CustomBlinkingPattern[4] = 0;
	if (m_T5LED1 == TRUE)
		CustomBlinkingPattern[4] |= 0x01;
	if (m_T5LED2 == TRUE)
		CustomBlinkingPattern[4] |= 0x02;

	CustomBlinkingPattern[5] = 0;
	if (m_T6LED1 == TRUE)
		CustomBlinkingPattern[5] |= 0x01;
	if (m_T6LED2 == TRUE)
		CustomBlinkingPattern[5] |= 0x02;

	CustomBlinkingPattern[6] = 0;
	if (m_T7LED1 == TRUE)
		CustomBlinkingPattern[6] |= 0x01;
	if (m_T7LED2 == TRUE)
		CustomBlinkingPattern[6] |= 0x02;

	CustomBlinkingPattern[7] = 0;
	if (m_T8LED1 == TRUE)
		CustomBlinkingPattern[7] |= 0x01;
	if (m_T8LED2 == TRUE)
		CustomBlinkingPattern[7] |= 0x02;
}



//-----------------------------------------------------------------------------
// INITIALIZATION ROUTINES
//-----------------------------------------------------------------------------
//
// These routines initialize dialog box variables and configure the device
// to different modes of operation.
//

//-----------------------------------------------------------------------------
// Init_Device
//-----------------------------------------------------------------------------
//
// Routine is called when communication with the device is activated
// by pressing the activate button.
//
void CHID_BlinkyDlg::Init_Device()
{
	if (BlinkModeSelect == STANDARD)
		Init_StandardBlinking ();
	else if (BlinkModeSelect == CUSTOM)

⌨️ 快捷键说明

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