USB Libraries Help > USB Host Libraries > USB HID Host Mouse Driver Library > Using the Library > How the Library Works > Mouse Data Event Handling
MPLAB Harmony USB Stack
Mouse Data Event Handling

No extra event handler is required to be registered to receive mouse data. A call to function USB_HOST_HID_MOUSE_EventHandlerSet once is adequate to receive mouse data as well. 

The mouse button state along with the X, Y, and Z relative coordinate positions are provided by the USB Host HID Mouse Driver. The data type is USB_HOST_HID_MOUSE_DATA and is defined in usb_host_hid_mouse.h. The following code shows an event handler example. 

Example:  

/* This code shows an example of HID Mouse Event Handler */

void APP_USBHostHIDMouseEventHandler
(
    USB_HOST_HID_MOUSE_HANDLE handle,
    USB_HOST_HID_MOUSE_EVENT event,
    void * pData
)
{
    /* This function gets called in the following scenarios:
    1. USB Mouse is Attached
    2. USB Mouse is detached
    3. USB Mouse data has been obtained.
    */

    switch ( event)
    {
        case USB_HOST_HID_MOUSE_EVENT_ATTACH:
            /* Mouse Attached */
            appData.state =  APP_STATE_DEVICE_ATTACHED;
            break;

        case USB_HOST_HID_MOUSE_EVENT_DETACH:
            /* Mouse Detached */
            appData.state = APP_STATE_DEVICE_DETACHED;
            break;

        case USB_HOST_HID_MOUSE_EVENT_REPORT_RECEIVED:
            /* Mouse data event */
            appData.state = APP_STATE_READ_HID;
            /* Mouse Data from device */
            memcpy(&appData.data, pData, sizeof(appData.data));

            /* Now the Mouse data has been obtained. This is a parsed data
            in a simple format defined by USB_HOST_HID_MOUSE_DATA type.
            */
            break;
    }

}

void APP_Tasks(void)
{
    switch (appData.state)
    {
        case APP_STATE_BUS_ENABLE:

            /* In this state the application enables the USB Host Bus. Note
             * how the USB Mouse event handler is registered before the bus
             * is enabled. */

            USB_HOST_HID_MOUSE_EventHandlerSet(APP_USBHostHIDMouseEventHandler);
            USB_HOST_BusEnable(0);
            appData.state = APP_STATE_WAIT_FOR_BUS_ENABLE_COMPLETE;
            break;

        case APP_STATE_WAIT_FOR_BUS_ENABLE_COMPLETE:
            /* Here we wait for the bus enable operation to complete. */
            break;
    }
}