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

📄 hid_blinkydlg.cpp

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


//-----------------------------------------------------------------------------
// Init_CustomBlinking
//-----------------------------------------------------------------------------
//
// This routine initializes the device to a custom blinking mode.  The
// values for the blink pattern are retrieved from the dialog box,
// the blink pattern buffer is formated, and then the pattern is
// sent to the device if blinking is enabled.
//
void CHID_BlinkyDlg::Init_CustomBlinking ()
{
    UpdateData (TRUE);                 // Retrieve blink pattern check boxes

	CustomBlinking_SaveState ();       // Format custom pattern buffer

	Update_BlinkPattern ();            // Output blinking pattern
	Update_BlinkRate ();               // Output blinking rate
	Update_BlinkEnable ();             // Output current enable/disable state
}

//-----------------------------------------------------------------------------
// Init_StandardBlinking
//-----------------------------------------------------------------------------
//
// Routine is called to configure the device to blink in standard mode.
// The current blink rate is transmitted, then the selected blink
// pattern is transmitted, and then the current blink enable state
// is transmitted.
//
void CHID_BlinkyDlg::Init_StandardBlinking (void)
{
	Update_BlinkRate ();
	Update_BlinkPattern ();
	Update_BlinkEnable ();
}


//-----------------------------------------------------------------------------
// Init_Variables
//-----------------------------------------------------------------------------
//
// Routine initializes variables and predefined blinking patterns.  It is
// called when the dialog box is created.
//
void CHID_BlinkyDlg::Init_Variables (void)
{
	// This section defines each blinking pattern and its associated
	// text description.  Bit 0 of a pattern corresponds to LED1, with
	// a value of '1' indicating that the LED1 should be turned on.
	// Bit 1 corresponds to LED2.

	PatternString[0] = "50% Duty Cycle";
	Pattern[0][0] = 0x00;
	Pattern[0][0] = 0x03;
	Pattern[0][2] = 0xFF;
	Pattern[0][3] = 0xFF;
	Pattern[0][4] = 0xFF;
	Pattern[0][5] = 0xFF;
	Pattern[0][6] = 0xFF;
	Pattern[0][7] = 0xFF;

	PatternString[1] = "Alternating LEDs";
	Pattern[1][0] = 0x01;
	Pattern[1][1] = 0x02;
	Pattern[1][2] = 0xFF;
	Pattern[1][3] = 0xFF;
	Pattern[1][4] = 0xFF;
	Pattern[1][5] = 0xFF;
	Pattern[1][6] = 0xFF;
	Pattern[1][7] = 0xFF;

	PatternString[2] = "All Permutations";
	Pattern[2][0] = 0x00;
	Pattern[2][1] = 0x01;
	Pattern[2][2] = 0x02;
	Pattern[2][3] = 0x03;
	Pattern[2][4] = 0xFF;
	Pattern[2][5] = 0xFF;
	Pattern[2][6] = 0xFF;
	Pattern[2][7] = 0xFF;

	PatternString[3] = "Absolute Craziness";
	Pattern[3][0] = 0x02;
	Pattern[3][1] = 0x01;
	Pattern[3][2] = 0x03;
	Pattern[3][3] = 0x01;
	Pattern[3][4] = 0x03;
	Pattern[3][5] = 0x00;
	Pattern[3][6] = 0x03;
	Pattern[3][7] = 0x01;

	PatternString[4] = "Pulse";
	Pattern[4][0] = 0x00;
	Pattern[4][1] = 0x00;
	Pattern[4][2] = 0x00;
	Pattern[4][3] = 0x03;
	Pattern[4][4] = 0x00;
	Pattern[4][5] = 0x03;
	Pattern[4][6] = 0x00;
	Pattern[4][7] = 0x00;

	// Save total number of patterns
	NumPatterns = 5;

	// Set default pattern selection
	SelectedPattern = 0;
	m_Selection = PatternString[SelectedPattern];

	// Set initial blinking mode to STANDARD
	BlinkModeSelect = STANDARD;

	// If this call returns a non-zero value, the device is connected
	if (HID_Blinky.GetConnectedDeviceNum(HID_Blinky_VID, HID_Blinky_PID))
	{
		// Device is connected, but communication has not been
		// "activated"
		m_DeviceStatus = "Connected, Idle";
	}
	else
	{
		// No device is currently attached to USB that matches
		// the PID and VID
		m_DeviceStatus = "Disconnected";
	}

	// Updates on-screen window during next OnTimer servicing
	UpdateWindow = TRUE;
}


//-----------------------------------------------------------------------------
// UPDATE ROUTINES
//-----------------------------------------------------------------------------
//
// These routines handle most of the low-level HID input and and output
// communication by creating buffers, constructing reports, and
// transmitting and receiving them.


//-----------------------------------------------------------------------------
// Update_BlinkRate
//-----------------------------------------------------------------------------
//
// Routine retrieves the value of m_BlinkRate from the dialog box, formats
// the data into a 16-bit value, creates an OUT_Blink_Rate report, and
// transmits that report across the control pipe.
//
void CHID_BlinkyDlg::Update_BlinkRate()
{
	unsigned char OutputRateBuffer[OUT_Blink_RateSize+1];

	UpdateData(TRUE);

	// Blinking rate must be positive
	if(m_BlinkRate < 0)
	{
		m_BlinkRate = m_BlinkRate*-1;
	}

	// Format buffer
	OutputRateBuffer[0] = OUT_Blink_Rate;
	OutputRateBuffer[1] = m_BlinkRate / 256;
	OutputRateBuffer[2] = m_BlinkRate;

	// Transmit report
	HID_Blinky.SetReport_Control (OutputRateBuffer,OUT_Blink_RateSize+1);
}

//-----------------------------------------------------------------------------
// Update_BlinkPattern
//-----------------------------------------------------------------------------
//
// Routine outputs the blinking pattern to the device, and then updates
// the blinking text on-screen.
//
void CHID_BlinkyDlg::Update_BlinkPattern()
{
	unsigned char OutputBuffer[OUT_Blink_PatternSize+1];

	// If blinking in Standard mode
	if (BlinkModeSelect == STANDARD)
	{
		// Format report buffer with pattern data
		OutputBuffer [0] = OUT_Blink_Pattern;
		for (int index = 1; index < 9; index++)
			OutputBuffer[index] = Pattern[SelectedPattern][index-1];
		// Output report to device
		HID_Blinky.SetReport_Interrupt (OutputBuffer, OUT_Blink_PatternSize+1);
		// Update text string
		m_Selection = PatternString[SelectedPattern];
		UpdateWindow = TRUE;           // set OnTimer routine to update string
	}
	else if (BlinkModeSelect == CUSTOM)
	{
		// Format report buffer with pattern data
		OutputBuffer [0] = OUT_Blink_Pattern;
		for (int index = 1; index < 9; index++)
			OutputBuffer[index] = CustomBlinkingPattern[index-1];
		// Output report to device
		HID_Blinky.SetReport_Interrupt (OutputBuffer, OUT_Blink_PatternSize+1);
		// Update text string
		m_Selection = "???";
		UpdateWindow = TRUE;
	}
}

//-----------------------------------------------------------------------------
// Update_BlinkEnable
//-----------------------------------------------------------------------------
//
// Routine creates report buffer for the Blink Enable output report.  After
// the buffer has been constructed, the report is sent across the control
// pipe.
//
void CHID_BlinkyDlg::Update_BlinkEnable ()
{
	unsigned char OutputEnableBuffer[OUT_Blink_EnableSize+1];
	OutputEnableBuffer [0] = OUT_Blink_Enable;
	OutputEnableBuffer [1] = m_Blink_Enable;
	HID_Blinky.SetReport_Control (OutputEnableBuffer, OUT_Blink_EnableSize+1);
}

//-----------------------------------------------------------------------------
// Update_BlinkDimmer
//-----------------------------------------------------------------------------
//
// Routine sends the newly selected slider value to the device by
// creating a BI-DIRECTIONAL Blink Dimmer FEATURE report, transmitting the
// report, and then retrieving the report.  Routine checks the value
// returned in the report to determine whether the device successfully
// received the new dimmer value.
//
void CHID_BlinkyDlg::Update_BlinkDimmer (void)
{
	// Create the FEATURE report buffer
	unsigned char OutputEnableBuffer[FEATURE_Blink_DimmerSize+1];
	OutputEnableBuffer [0] = FEATURE_Blink_Dimmer;

	// Convert 0-100 to 0-0xFF to be used by the device.
	OutputEnableBuffer [1] = m_Slider*255/100;

	do
	{
		// Send FEATURE report
		HID_Blinky.SetFeatureReport (OutputEnableBuffer, FEATURE_Blink_DimmerSize+1);
		// Receive FEATURE report
		HID_Blinky.GetFeatureReport (OutputEnableBuffer, FEATURE_Blink_DimmerSize+1);
		// Check for successful reception data
	} while (OutputEnableBuffer[1] != 1);
}

//-----------------------------------------------------------------------------
// Update_Stats
//-----------------------------------------------------------------------------
//
// Routine retrieves two bytes of data from the device by calling Get Report
// with the IN_Blink_Stats report ID, and then returns the two bytes
// received within that report.
//
void CHID_BlinkyDlg::Update_Stats (unsigned char* Stat1, unsigned char* Stat2)
{
	unsigned char OutputEnableBuffer[256];
	// For GetReport_Control, the buffer's first element must contain the
	// report ID of the report that the function is supposed to retrieve
	OutputEnableBuffer [0] = IN_Blink_Stats;
	// Retrieve buffer
	HID_Blinky.GetReport_Control (OutputEnableBuffer, HID_Blinky.GetOutputReportBufferLength ());

	*Stat1 = OutputEnableBuffer [1];   // Return data received from report
	*Stat2 = OutputEnableBuffer [2];
}


//-----------------------------------------------------------------------------
// HELP MESSAGE BOX ROUTINES
//-----------------------------------------------------------------------------
// The following five routines describe how modules of the demo application
// communicate with the HID device.

//-----------------------------------------------------------------------------
// OnBnClickedHelpConnection
//-----------------------------------------------------------------------------
//
// Text from message box:
// The device status window updates when:
// 1) The dialog object receives notification that a device
//    has been connected or disconnected from the bus.
// 2) The user initializes/terminates communication with the
//    HID object by clicking 'Activate' or 'Deactivate'
//
void CHID_BlinkyDlg::OnBnClickedHelpConnection()
{
	MessageBox("The device status window updates when:\n\n1) The dialog object receives notification that a device \n    has been connected or disconnected from the bus.\n\n2) The user initializes/terminates communication with the\n    HID object by clicking 'Activate' or 'Deactivate'");
}

//-----------------------------------------------------------------------------
// OnBnClickedHelpStandardBlink
//-----------------------------------------------------------------------------
//
// Text from message box:
// Selecting 'Standard' allows the device to choose from
// pre-designed blinking patterns using the potentiometer.
// INTERRUPT IN reports containing potentiometer
// postition are received by the host.
// The host then sends the selected blinking pattern to
// the device using an INTERRUPT OUT report.
//
void CHID_BlinkyDlg::OnBnClickedHelpStandardBlink()
{
	MessageBox ("Selecting 'Standard' allows the device to choose from \npre-designed blinking patterns using the potentiometer.\n\nINTERRUPT IN reports containing potentiometer \npostition are received by the host.\n\nThe host then sends the selected blinking pattern to \nthe device using an INTERRUPT OUT report.");
}

//-----------------------------------------------------------------------------
// OnBnClickedIdcHelpCustomBlink
//-----------------------------------------------------------------------------
//
// Text from message box:
// Selecting 'Custom' allows users to create their own blinking patterns.
// Users first designs the pattern using the array of check boxes.
// Clicking the 'Update Pattern' button sends an INTERRUPT OUT report
// with the custom pattern.
//
void CHID_BlinkyDlg::OnBnClickedIdcHelpCustomBlink()
{
	MessageBox ("Selecting 'Custom' allows users to create their own blinking patterns.\n\nUsers first designs the pattern using the array of check boxes.\n\nClicking the 'Update Pattern' button sends an INTERRUPT OUT report \nwith the custom pattern.");
}

//-----------------------------------------------------------------------------
// OnBnClickedHelpStats
//-----------------------------------------------------------------------------
//
// Text from message box:
// Clicking 'Get Stats' sends a Get Input Report request to the device.
// The device constructs the report and sends it back to the host.
//
void CHID_BlinkyDlg::OnBnClickedHelpStats()
{
	MessageBox ("Clicking 'Get Stats' sends a Get Input Report request to the device.\nThe device constructs the report and sends it back to the host.");
}

//-----------------------------------------------------------------------------
// OnBnClickedBlinkControls
//-----------------------------------------------------------------------------
//
// Text from the message box:
// Checking 'Blink Enabled' transmits a report using
// Set Output Report to command the device to start/stop blinking.
// Pressing 'Update Rate' sends an INTERRUPT OUT report with
// the new blink rate to the device.
// Adjusting the dimmer slider sends values to the device by calling
// Set FEATURE report.  The host then calls Get FEATURE report
// that commands the device to transmit whether or not the last
// feature report was successfully received.
//
void CHID_BlinkyDlg::OnBnClickedBlinkControls()
{
	MessageBox ("Checking 'Blink Enabled' transmits a report using Set Output Report \nto command the device to start/stop blinking.\n\nPressing 'Update Rate' sends an INTERRUPT OUT report with \nthe new blink rate to the device.\n\nAdjusting the dimmer slider sends values to the device by calling\nSet FEATURE report.  The host then calls Get FEATURE report \nthat commands the device to transmit whether or not the last\nfeature report was successfully received.");
}


//-----------------------------------------------------------------------------
// END OF FILE
//-----------------------------------------------------------------------------

⌨️ 快捷键说明

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