1.. zephyr:code-sample:: usb-hid-mouse 2 :name: USB HID mouse 3 :relevant-api: _usb_device_core_api usb_hid_device_api input_interface 4 5 Implement a basic HID mouse device. 6 7Overview 8******** 9 10This sample app demonstrates use of a USB Human Interface Device (HID) driver 11by the Zephyr project. This very simple driver enumerates a board with a button 12into a mouse that has a left mouse button and optionally (depending on 13the number of buttons on the board) a right mouse button, X-axis movement, 14and Y-axis movement. 15If the USB peripheral driver supports remote wakeup feature, wakeup request 16will be performed on every button click if the bus is in suspended state. 17This sample can be found under :zephyr_file:`samples/subsys/usb/hid-mouse` in the 18Zephyr project tree. 19 20Requirements 21************ 22 23This project requires an USB device driver and uses the :ref:`input` API. 24There must be a :dtcompatible:`gpio-keys` group of buttons or keys defined at 25the board level that can generate input events, otherwise the example will build 26but not work as expected. 27 28The key mapping in the sample is as follows: 29 30- ``INPUT_KEY_0``: left button 31- ``INPUT_KEY_1``: right button 32- ``INPUT_KEY_2``: move the mouse along the x-axis 33- ``INPUT_KEY_3``: move the mouse along the y-axis 34 35An LED must also be configured via the ``led0`` devicetree alias. You may also 36see this error if you try to build this sample for an unsupported board: 37 38.. code-block:: none 39 40 Unsupported board: led0 devicetree alias is not defined 41 42Building and Running 43******************** 44 45This sample can be built for multiple boards, in this example we will build it 46for the nucleo_f070rb board: 47 48.. zephyr-app-commands:: 49 :zephyr-app: samples/subsys/usb/hid-mouse 50 :board: nucleo_f070rb 51 :goals: build 52 :compact: 53 54After you have built and flashed the sample app image to your board, plug the 55board into a host device, for example, a PC running Linux. 56The board will be detected as shown by the Linux dmesg command: 57 58.. code-block:: console 59 60 dmesg | tail -10 61 usb 2-2: new full-speed USB device number 2 using at91_ohci 62 usb 2-2: New USB device found, idVendor=2fe3, idProduct=0007, bcdDevice= 2.03 63 usb 2-2: New USB device strings: Mfr=1, Product=2, SerialNumber=3 64 usb 2-2: Product: Zephyr HID mouse sample 65 usb 2-2: Manufacturer: ZEPHYR 66 usb 2-2: SerialNumber: 86FE679A598AC47A 67 input: ZEPHYR Zephyr HID mouse sample as /devices/soc0/ahb/600000.ohci/usb2/2-2/2-2:1.0/0003:2FE3:0100.0001/input/input0 68 hid-generic 0003:2FE3:0100.0001: input: USB HID v1.10 Mouse [ZEPHYR Zephyr HID mouse sample] on usb-at91-2/input0 69 70You can also monitor mouse events by using the standard Linux ``evtest`` command 71(see the `Ubuntu evtest man page`_ for more information about this tool): 72 73.. _Ubuntu evtest man page: 74 http://manpages.ubuntu.com/manpages/trusty/man1/evtest.1.html 75 76.. code-block:: console 77 78 sudo evtest /dev/input/event0 79 Input driver version is 1.0.1 80 Input device ID: bus 0x3 vendor 0x2fe3 product 0x7 version 0x110 81 Input device name: "ZEPHYR Zephyr HID mouse sample" 82 Supported events: 83 Event type 0 (EV_SYN) 84 Event type 1 (EV_KEY) 85 Event code 272 (BTN_LEFT) 86 Event code 273 (BTN_RIGHT) 87 Event code 274 (BTN_MIDDLE) 88 Event type 2 (EV_REL) 89 Event code 0 (REL_X) 90 Event code 1 (REL_Y) 91 Event code 8 (REL_WHEEL) 92 Event type 4 (EV_MSC) 93 Event code 4 (MSC_SCAN) 94 Properties: 95 Testing ... (interrupt to exit) 96 97When you press the button on your board, it will act as if the left 98mouse button was pressed, and this information will be displayed 99by ``evtest``: 100 101.. code-block:: console 102 103 Event: time 1167609663.618515, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90001 104 Event: time 1167609663.618515, type 1 (EV_KEY), code 272 (BTN_LEFT), value 1 105 Event: time 1167609663.618515, -------------- SYN_REPORT ------------ 106 Event: time 1167609663.730510, type 4 (EV_MSC), code 4 (MSC_SCAN), value 90001 107 Event: time 1167609663.730510, type 1 (EV_KEY), code 272 (BTN_LEFT), value 0 108 Event: time 1167609663.730510, -------------- SYN_REPORT ------------ 109 110If your board has more than one button, they will act as right mouse button, 111X-axis movement, and Y-axis movement. 112