USB Component  Version 6.6
MDK-Professional Middleware for USB Device and Host
 All Data Structures Functions Variables Enumerations Enumerator Groups Pages
User API

User API reference of the Human Interface Device Class. More...

Functions

usbStatus USBH_HID_GetDeviceStatus (uint8_t instance)
 Get status of Human Interface Device.
 
int32_t USBH_HID_Read (uint8_t instance, uint8_t *buf, int32_t len)
 Read data received from Human Interface Device.
 
int32_t USBH_HID_Write (uint8_t instance, const uint8_t *buf, int32_t len)
 Write data to Human Interface Device.
 
int USBH_HID_GetKeyboardKey (uint8_t instance)
 Retrieve first pending pressed keyboard key on HID Keyboard.
 
usbStatus USBH_HID_GetMouseState (uint8_t instance, usbHID_MouseState *state)
 Retrieve state change since last call of this function.
 
void USBH_HID_ParseReportDescriptor (uint8_t instance, uint8_t *ptr_hid_report_desc)
 Callback function called for parsing of the Human Interface Device report descriptor.
 
void USBH_HID_DataReceived (uint8_t instance, uint32_t len)
 Callback function called when data is received from the Human Interface Device.
 

Description

User API reference of the Human Interface Device Class.

Function Documentation

void USBH_HID_DataReceived ( uint8_t  instance,
uint32_t  len 
)

Callback function called when data is received from the Human Interface Device.

Parameters
[in]instanceinstance index.
[in]lenlength of received data.
Returns
none.

The USBH_HID_DataReceived function enables programmers to analyse received data coming from an USB HID device. Implementing this function in user code overrides the library function that handles boot protocol device data reception. This is useful if the default HID functionality needs to be changed to support special HID devices.

usbStatus USBH_HID_GetDeviceStatus ( uint8_t  instance)

Get status of Human Interface Device.

Parameters
[in]instanceinstance of HID Device.
Returns
status code that indicates the execution status of the function as defined with usbStatus.

The function USBH_HID_GetDeviceStatus checks whether the human interface device is connected and initialized.

The argument instance is specifying the device instance.

Code Example

void USBH_Thread (void const *arg) {
char con = 0; /* Connection status of keyboard */
USBH_Initialize (0); /* Initialize USB Host 0 */
while (1) {
con = USBH_HID_GetDeviceStatus(0) == usbOK; /* Get kbd connection status */
//do something
}
}
int32_t USBH_HID_GetKeyboardKey ( uint8_t  instance)

Retrieve first pending pressed keyboard key on HID Keyboard.

Parameters
[in]instanceinstance of HID Device.
Returns
value of read character or no character received.
  • value >= 0: value of first received unread character
  • value -1: indicates no character was received

The function USBH_HID_GetKeyboardKey enables programmers to handle signals from an USB keyboard.

The argument instance is specifying the device instance.

Code Example

int main (void) {
char cmdbuf [15]; /* command input buffer */
int idx; /* index for circular buffer */
osThreadCreate (osThread(USBH_Thread), NULL);
timer_id = osTimerCreate(osTimer(timer_1ms), osTimerPeriodic, NULL);
osTimerStart(timer_id, 1);
clear_records (); /* initialize circular buffer */
printf ( menu ); /* display command menu */
while (1) { /* loop forever */
if (getline (&cmdbuf[0], sizeof (cmdbuf)) == false)
continue;
switch (cmdbuf[i]) { /* proceed to command function */
case 'R': /* Read circular Buffer */
if ((idx = read_index (&cmdbuf[i+1])) == WRONGINDEX) break;
while (idx != sindex) { /* check end of table */
if (USBH_HID_GetKeyboardKey(0) == 0x1B) break; /* ESC terminates */
if (save_record[idx].time.hour != 0xff) {
measure_display (save_record[idx]); /* display record */
printf ("\n");
}
if (++idx == SCNT) idx = 0; /* next circular buffer entry */
}
break;
default: /* Error Handling */
printf (ERROR_STR, "UNKNOWN COMMAND");
printf (menu); /* display command menu */
break;
}
}
}
bool USBH_HID_GetMouseState ( uint8_t  instance,
usbHID_MouseState state 
)

Retrieve state change since last call of this function.

Parameters
[in]instanceinstance of HID Device.
[out]statepointer to mouse state usbHID_MouseState structure.
Returns
status code that indicates the execution status of the function as defined with usbStatus.

The function USBH_HID_GetMouseState enables programmers to handle signals from an USB mouse.

The argument instance is specifying the device instance.

The argument state is a pointer to the mouse state structure (usbHID_MouseState).

Code Example

#include "rl_usb.h"
void getmouse (void) {
..
if (USBH_HID_GetMouseState (0, &MouseState)) { // If mouse move
..
}
)
void USBH_HID_ParseReportDescriptor ( uint8_t  instance,
uint8_t *  ptr_hid_report_desc 
)

Callback function called for parsing of the Human Interface Device report descriptor.

Parameters
[in]instanceinstance index.
[in]ptr_hid_report_descpointer to HID report descriptor.
Returns
none.

The USBH_HID_ParseReportDescriptor function enables programmers to parse report descriptors of an USB HID device. Implementing this function in user code overrides the library function that handles boot protocol device report descriptor parsing. This is useful if the default HID functionality needs to be changed to support special HID devices.

int32_t USBH_HID_Read ( uint8_t  instance,
uint8_t *  buf,
int32_t  len 
)

Read data received from Human Interface Device.

Parameters
[in]instanceinstance of HID Device.
[out]bufbuffer that receives data.
[in]lenmaximum number of bytes to read.
Returns
number of bytes read or execution status.
  • value >= 0: number of bytes read
  • value < 0: error occurred, -value is execution status as defined with usbStatus

The function USBH_HID_Read retrieves the data sent by an USB HID device and stores it in a buffer.

The argument instance is specifying the device instance.

The argument buf is pointing to the location where the data will be returned (stored).

The argument len specifies the number of bytes to be read.

Code Example

#include "rl_usb.h"
void parse_HID (void) {
U8 data[16];
..
if (USBH_HID_Read (0, &data, 2)) { // If data received from HID device
..
}
)
int32_t USBH_HID_Write ( uint8_t  instance,
const uint8_t *  buf,
int32_t  len 
)

Write data to Human Interface Device.

Parameters
[in]instanceinstance of HID Device.
[in]bufdata buffer containing data to write.
[in]lennumber of data bytes to write.
Returns
number of bytes accepted for writing or execution status.
  • value >= 0: number of bytes accepted for writing
  • value < 0: error occurred, -value is execution status as defined with usbStatus

The function USBH_HID_Write writes data from a buffer to an USB HID device.

The argument instance is specifying the device instance.

The argument buf is pointing to the location where the data will be written.

The argument len specifies the number of data bytes to be written.

Code Example

void USBH_Thread (void const *arg) {
char con = 0; /* Connection status of keyboard */
char con_ex = 40; /* Previous connection status
+ initial time in 100 ms
intervals for initial display */
USBH_Initialize (0); /* Initialize USB Host 0 */
while (1) {
con = USBH_HID_GetDeviceStatus(0) == usbOK; /* Get kbd connection status */
if ((con ^ con_ex) & 1) { /* If connection status changed */
if (con) {
USBH_HID_Write (0,(uint8_t *)&out,1);/* Turn on NUM LED */
printf ("\nKeyboard connected\n");
} else {
printf ("\nKeyboard disconnected ...\n");
}
:
}
osDelay(100);
}
}