17/07/2018

How to Communicate with the PC using HID Protocol on a Leonardo

Introduction

This tip exists because the actual Arduino firmware do not support full communication with the PC using HID, we cannot read data from the PC actually.

Using the Code

To fix that, you need to replace the files below from libraries folder:

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.

Then to send bytes to the PC, you need to do:

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

And to receive bytes from the PC:

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

Data_report and Host_report are array of bytes.

History

  • 10th July, 2018: Initial version