How to make a mouse work with USB Host Shield?

Hello,

I have a USB Host Shield and an Arduino Leonardo R3. All the components are working properly—there are no soldering or other hardware issues. When I test with the library below, everything works fine.

With the library, the orange LED is always on. But with my custom code, it stays off—even when I move or click the mouse.

However, I have a custom code that, when uploaded to the Arduino, establishes a connection with the mouse (the mouse receives power), but it doesn’t detect any mouse movement or clicks.

How can I make my code compatible with my mouse? AI couldn't solved.

Byte logs:
1- buttons
2- dx
3- dy
4- scroll
5- null, always 00

image

Custom code:

#define RAWHID_TX_INTERVAL 1

#include <SPI.h>
#include <Usb.h>
#include <usbhub.h>
#include <hiduniversal.h>
#include <HID-Project.h>
#include <HID-Settings.h>

USB          Usb;
USBHub       Hub(&Usb);
HIDUniversal Hid(&Usb);

uint8_t rawBuf[64];

// Parser for your USB-Host-Shield mouse
class MouseParser : public HIDReportParser {
  uint8_t prevBtns = 0;
  const uint8_t masks[5] = {0x01, 0x02, 0x04, 0x08, 0x10};

  void Parse(USBHID*, bool isRptID, uint8_t len, uint8_t *buf) override {
    uint8_t off = isRptID ? 1 : 0;
    if (len < off + 6) return;

    uint8_t btns  = buf[off + 0];
    int8_t  dx    = (int8_t)buf[off + 1];
    int8_t  dy    = (int8_t)buf[off + 3];
    int8_t  wheel = (int8_t)buf[off + 5];

    // Buttons 1–5
    for (uint8_t i = 0; i < 5; i++) {
      uint8_t m = masks[i];
      if ((btns & m) && !(prevBtns & m)) BootMouse.press(m);
      if (!(btns & m) &&  (prevBtns & m)) BootMouse.release(m);
    }
    prevBtns = btns;

    // Move + scroll
    BootMouse.move(dx, dy, wheel);
  }
} mouseParser;

void setup() {
  BootMouse.begin();
  RawHID.begin(rawBuf, sizeof(rawBuf));
  RawHID.enable();
  Hid.SetReportParser(0, &mouseParser);
  if (Usb.Init() == -1) while (1);
}

void loop() {
  Usb.Task();

  int len = RawHID.read();
  if (len > 0) {
    const uint8_t masks[5] = {0x01, 0x02, 0x04, 0x08, 0x10};

    uint8_t btns = rawBuf[0];
    int8_t  dx   = (int8_t)rawBuf[1];
    int8_t  dy   = (int8_t)rawBuf[2];

    // Press+release all requested buttons
    for (uint8_t i = 0; i < 5; i++) {
      uint8_t m = masks[i];
      if (btns & m) {
        BootMouse.press(m);
        delayMicroseconds(100); // quick release
        BootMouse.release(m);
      }
    }

    BootMouse.move(dx, dy, 0);
    RawHID.enable();
  }
}

Very interesting. Since it works with the test program. How is your custom code different and did you test each step of your custom code as you wrote it?

The custom code came with a program I purchased. When I contacted the seller, they said I need to adjust the code based on the byte logs. But I haven’t been able to get it working—so I need help.

So, you have an unknown program from someone you have never met and you do not know if they are telling you the truth and they personally have made the proposed changes and they worked and your do not.

Have you put in serial.Print() messages to track what is happening?

Ask for your money back, it is not as advertised.

Well if the initial code worked it is as advertised. What is not working is the modifications. So @realmbappe we also need to see the code that worked, as well as your modified code.

You might want to look at this How to get the best out of this forum before you proceed any further.

I have already shared the working code, it is available at github.

It would be much better if you showed the code in the normal way for this forum.

Sorry I do not do external sources to check code that works but fix some that does not work because of modifications that have not been posted in there entirety. Good Luck!