USB Component  
MDK Middleware for USB Device and Host Communication
 
Loading...
Searching...
No Matches
HID: Human Interface Device Class

USB Host functions to support Human Interface Device (HID) USB Devices. More...

Content

 User API
 User API reference of the Human Interface Device Class.
 
 Configuration
 Configuration of the USB Host HID Class.
 

Description

USB Host functions to support Human Interface Device (HID) USB Devices.

The HID class in the USB Component is used for attaching input devices to your system.

Refer to:

Software Structure
The application starts the USB Host by calling USBH_Initialize. The USB Host Core will wait until an USB HID device is attached to the system. As soon as this happens it will enumerate the device and it will be ready to be used by the application. The handling of the HID class events is implemented in USBH_HIDn_Thread.

The transmit functions USBH_HID_Read and USBH_HID_Write will be called by the user thread directly to communicate with the HID device.

msc_inline_mscgraph_9

Implementation

To create an USB Host with support for the HID class:

Code Example

int main (void) {
usbStatus usb_status; // USB status
int status; // Generic status
int ch; // Character
uint8_t con = 0U; // Connection status of keyboard
SystemCoreClockUpdate(); // Update system clock
status = stdout_init (); // Initialize retargeted stdout
if (status != 0) {
for (;;) {} // Handle stdout init failure
}
usb_status = USBH_Initialize (0U); // Initialize USB Host 0
if (usb_status != usbOK) {
for (;;) {} // Handle USB Host 0 init failure
}
for (;;) {
usb_status= USBH_HID_GetStatus(0U); // Get HID device status
if (usb_status == usbOK) {
if (con == 0U) { // If keyboard was not connected previously
con = 1U; // Keyboard got connected
printf ("Connect!\n");
}
} else {
if (con == 1U) { // If keyboard was connected previously
con = 0U; // Keyboard got disconnected
printf ("\nDisconnect!\n");
}
}
if (con != 0U) { // If keyboard is active
ch = USBH_HID_GetKeyboardKey (0U);// Get pressed key
if (ch != -1) { // If valid key value
if ((ch & 0x10000) != 0) { // Handle non-ASCII translated keys (Keypad 0 .. 9)
// Bit 16: non-ASCII bit (0 = ASCII, 1 = not ASCII)
// Bits 15..8: modifiers (SHIFT, ALT, CTRL, GUI)
// Bits 7..0: ASCII or HID key Usage ID if not ASCII
ch &= 0xFF; // Remove non-ASCII bit and modifiers
if ((ch>=0x59)&&(ch<=0x61)) { // Keypad 1 .. 9 key convert to
ch = (ch - 0x59) + '1'; // ASCII 1 .. 9
} else if (ch == 0x62) { // Keypad 0 key convert to
ch = '0'; // ASCII 0
} else { // If not Keypad 0 .. 9
ch = -1; // invalidate the key
}
}
if ((ch > 0) && (ch < 128)) { // Output ASCII 0 .. 127 range
putchar(ch);
fflush(stdout);
}
}
}
osDelay(10U);
}
}