1

I've got a USB keyboard connected to my Arduino Uno with a USB Host Shield. I'm reading keystrokes using this example from the USB Host Shield library.

I need to toggle the LEDs in the keyboard (that indicate the state of Caps Lock, Num Lock etc.)

This thread mentions using Usb.setReport(), but this method doesn't seem to exist any more: there's no such method in Usb.cpp in the library. There does seem to be a method called SetReport in the USBHID class, which looks like it takes the same arguments, which I'm trying to use.

What I'm currently doing:

void keyboard_setup(){
    static HIDBoot<USB_HID_PROTOCOL_KEYBOARD> HidKeyboard(&Usb);
    if (Usb.Init() == -1) {
        Serial.println("OSC did not start.");
    }
    delay(200);
    HidKeyboard.SetReportParser(0, &Prs);

    static USBHID usbhid(&Usb);
    uint8_t leds = 1;
#define KBD_ADDR 1
#define KBD_IF 0
    usbhid.SetReport( KBD_ADDR, 0, 1, KBD_IF, 0, &leds );
    delay(100);
    leds = 255;
    usbhid.SetReport( KBD_ADDR, 0, 1, KBD_IF, 0, &leds );
    delay(100);

Neither call to SetReport seems to work, and I'm not certain the values of KBD_ADDR or KBD_IF are correct (I'm using ones that I've seen in examples), or event that calling SetReport is right .

What am I doing wrong?

5
  • 1
    I posted an answer but deleted it. My 1st thought was that you were creating your own instance of the SetReport method (which you are) that didn't work w/any USB device. But looking at your code again - I'm not so sure.
    – st2000
    Apr 27, 2019 at 13:34
  • @st2000 I'm creating my own instance of USBHID, which I don't otherwise use, because I seem to need it to use SetReport(). I'm not sure if that's the right approach though.
    – Joe Malt
    Apr 27, 2019 at 13:38
  • This is going to take a bit of thinking for me. But I think the best way to go about controlling the keyboard LEDs is to expose SetReport to your Sketch by adding a wrapper method to the USBHost library. Otherwise, I'm thinking you need to setup all the USB stuff in your Sketch - which the driver already is capable of doing.
    – st2000
    Apr 27, 2019 at 14:00
  • I thought NicoHood/HID github page was the answer, but I think it works with custom keyboard and does not necessary work w/standard USB keyboard LEDs.
    – st2000
    Apr 27, 2019 at 14:38
  • 2
    the NicoHood/HID uses an Arduino to emulate a keyboard .... there is no actual keyboard involved
    – jsotola
    Apr 27, 2019 at 17:13

1 Answer 1

2

There does seem to be a method called SetReport in the USBHID class, which looks like it takes the same arguments, which I'm trying to use.

The method arguments of the new SetReport() method aren't quite the same and some are in a different order.

The old method signature was:

uint8_t setReport( uint8_t addr, uint8_t ep, unsigned int nbytes, uint8_t interface, uint8_t report_type, uint8_t report_id, uint8_t* dataptr );

...while the new one is:

uint8_t SetReport(uint8_t ep, uint8_t iface, uint8_t report_type, uint8_t report_id, uint16_t nbytes, uint8_t* dataptr);

It seems to me the equivalent call would be:

usbhid.SetReport( 0, KBD_IF, 0x02, 0, 1, &leds );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.