Learn how to communicate with the PC using HID Protocol on a Leonardo [code snippet]

Hello,

I would like to show you a tutorial to achieve USB communication using HID protocol.

All what you need is to follow instructions below, then you will just need to do that in your sketch:

Send bytes to the PC:

HID().USBSend(Data_report, REPORT_ID + DATA_SIZE);

Receive bytes from the PC:

    if (HID().USBRecv(host_report, RID + HOST_SIZE))
    {
        if (host_report[0] == FUNCTION_ID)
        {
            // Do your stuff here
        }
    }

Instructions:
Download the librairies.zip then

Move librairies\HID.cpp     to {Arduino Path}\hardware\arduino\avr\libraries\HID\src
Move librairies\HID.h       to {Arduino Path}\hardware\arduino\avr\libraries\HID\src

Move librairies\USBCore.cpp to {Arduino Path}\hardware\arduino\avr\cores\arduino
Move librairies\USBAPI.h    to {Arduino Path}\hardware\arduino\avr\cores\arduino

Move librairies\Caterina-Leonardo.hex to {Arduino Path}\hardware\arduino\avr\bootloaders\caterina

The Caterina bootloader is here to avoid long upload sketch to the board.

librairies.zip (16.9 KB)

Hello,

Here's an update for Arduino 1.8.8:

https://www.codeproject.com/Tips/1251867/How-to-Communicate-with-the-PC-using-HID-Protocol