This is the quick start guide for the USB host mouse module (UHI mouse) 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.
In this basic use case, the "USB Host HID Mouse (Single Class support)" module is used. The "USB Host HID Mouse (Multiple Classes support)" module usage is described in Advanced use cases.
Common prerequisites for all USB hosts.
This module is based on USB host stack full interrupt driven and supporting sleepmgr_group sleepmgr. For AVR and SAM3/4 devices the clk_group clock services is supported. For SAMD21 devices the asfdoc_sam0_system_clock_group clock driver is supported.
The following procedure must be executed to setup the project correctly:
The usage of sleepmgr_group sleepmgr service is optional, but recommended to reduce power consumption:
Common example code for all USB hosts.
Content of conf_usb_host.h:
#define USB_HOST_POWER_MAX 500
Add to application C-file:
void usb_init(void) { USBH_start(); }
Common workflow for all USB devices.
// Maximum current allowed on Vbus (mA) which depends of 5V generator #define USB_HOST_POWER_MAX 500 // (500mA)
USBH_start();
Content of conf_usb_host.h:
#define USB_HOST_UHI UHI_HID_MOUSE #define UHI_HID_MOUSE_CHANGE(dev, b_plug) my_callback_mouse_change(dev, b_plug) extern bool my_callback_mouse_change(USBH_device_t* dev, bool b_plug); #define UHI_HID_MOUSE_EVENT_BTN_LEFT(b_state) my_callback_event_btn_left(b_state) extern void my_callback_event_btn_left(bool b_state); #define UHI_HID_MOUSE_EVENT_BTN_RIGHT(b_state) my_callback_event_btn_right(b_state) extern void my_callback_event_btn_right(bool b_state); #define UHI_HID_MOUSE_EVENT_BTN_MIDDLE(b_state) my_callback_event_btn_middle(b_state) extern void my_callback_event_btn_middle(bool b_state); #define UHI_HID_MOUSE_EVENT_MOUVE(x, y, scroll) my_callback_event_mouse(x, y, scroll) extern void my_callback_event_mouse(int8_t x, int8_t y, int8_t scroll); #include "uhi_hid_mouse.h" // At the end of conf_usb_host.h file
Add to application C-file:
bool my_callback_mouse_change(USBH_device_t* dev, bool b_plug) { if (b_plug) { my_display_on_mouse_icon(); } else { my_display_off_mouse_icon(); } } void my_callback_event_btn_left(bool b_state) { if (b_state) { // Here mouse button left pressed } else { // Here mouse button left released } } void my_callback_event_mouse(int8_t x, int8_t y, int8_t scroll) { if (!x) { // Here mouse are moved on axe X cursor_x += x; } if (!y) { // Here mouse are moved on axe Y cursor_y += y; } if (!scroll) { // Here mouse are moved the wheel wheel += scroll; } }
#define USB_HOST_UHI UHI_HID_MOUSE
#define UHI_HID_MOUSE_CHANGE(dev, b_plug) my_callback_mouse_change(dev, b_plug) extern bool my_callback_mouse_change(USBH_device_t* dev, bool b_plug);
#define UHI_HID_MOUSE_EVENT_BTN_LEFT(b_state) my_callback_event_btn_left(b_state) extern void my_callback_event_btn_left(bool b_state); #define UHI_HID_MOUSE_EVENT_BTN_RIGHT(b_state) my_callback_event_btn_right(b_state) extern void my_callback_event_btn_right(bool b_state); #define UHI_HID_MOUSE_EVENT_BTN_MIDDLE(b_state) my_callback_event_btn_middle(b_state) extern void my_callback_event_btn_middle(bool b_state); #define UHI_HID_MOUSE_EVENT_MOUVE(x, y, scroll) my_callback_event_mouse(x, y, scroll) extern void my_callback_event_mouse(int8_t x, int8_t y, int8_t scroll)
For more advanced use of the UHI HID mouse module, see the following use cases: