This is the quick start guide for the USB Device Keyboard Module (UDI Keyboard) with step-by-step instructions on how to configure and use the modules in a selection of use cases.
The use cases contain several code fragments. The code fragments in the steps for setup can be copied into a custom initialization function, while the steps for usage can be copied into, e.g., the main application function.
Basic Use Case
In this basic use case, the "USB HID keyboard (Single Interface Device)" module is used. The "USB HID keyboard (Composite Device)" module usage is described in Advanced Use Cases.
Setup Steps
As a USB device, it follows common USB device setup steps. Refer to USB Device Basic Setup.
Usage Steps
Example Code
Content of conf_usb.h:
#define UDI_HID_KBD_ENABLE_EXT() my_callback_keyboard_enable()
extern bool my_callback_keyboard_enable(void);
#define UDI_HID_KBD_DISABLE_EXT() my_callback_keyboard_disable()
extern void my_callback_keyboard_disable(void);
#include "udi_hid_keyboard_conf.h"
Add to application C-file:
static bool my_flag_autorize_keyboard_events = false;
bool my_callback_keyboard_enable(void)
{
my_flag_autorize_keyboard_events = true;
return true;
}
void my_callback_keyboard_disable(void)
{
my_flag_autorize_keyboard_events = false;
}
void my_key_A_press_event(void)
{
if (!my_flag_autorize_keyboard_events) {
return;
}
}
Workflow
- Ensure that conf_usb.h is available and contains the following configuration which is the USB device keyboard configuration:
#define UDI_HID_KBD_ENABLE_EXT() my_callback_keyboard_enable()
extern bool my_callback_keyboard_enable(void);
- Note
- After the device enumeration (detecting and identifying USB devices), the USB host starts the device configuration. When the USB keyboard interface from the device is accepted by the host, the USB host enables this interface and the UDI_HID_KBD_ENABLE_EXT() callback function is called and return true. Thus, it is recommended to enable sensors used by the keyboard in this function.
#define UDI_HID_KBD_DISABLE_EXT() my_callback_keyboard_disable()
extern void my_callback_keyboard_disable(void);
- Note
- When the USB device is unplugged or is reset by the USB host, the USB interface is disabled and the UDI_HID_KBD_DISABLE_EXT() callback function is called. Thus, it is recommended to disable sensors used by the keyboard in this function.
- Send keyboard events:
Advanced Use Cases
For more advanced use of the UHI HID keyboard module, see the following use cases: