0

I am using Arduino UNO R3 with Sparkfun USB Host Shield along with USB_Host_Shield_2.0 library.

The following code works well and I do get the ASCII clicked on the keyboard. However, as the code uses the OnKeyDown event, the UNO is not getting what might be stored in the keyboard's buffer, even as a result of a key being constantly pressed.

The project requires to connect the shield to a USB device that sends ASCII strings as a keyboard emulator, apparently without the key up/down state, therefore the UNO must read whatever being sent.

My questions:

1) How to modify the code so it reads whatever the HID sends regardless of keys state?

2) The UNO need to send the data to a MKR-WIFI-1010's Serial1 interface. What is the best method to send the information from the UNO, should I connect to TX/RX in parallel to my dev PC or I should implement SoftwareSerial for second interface? maybe something else?

Thanks!

#include <hidboot.h>
#include <usbhub.h>

class KbdRptParser : public KeyboardReportParser {
  protected:
    void OnKeyDown  (uint8_t mod, uint8_t key);
};

void KbdRptParser::OnKeyDown(uint8_t mod, uint8_t key) {
  uint8_t c = OemToAscii(mod, key);
  if (c) Serial.print((char)c);
}

USB     Usb;
HIDBoot<USB_HID_PROTOCOL_KEYBOARD>    HidKeyboard(&Usb);
KbdRptParser Prs;

void setup() {
  Serial.begin( 115200 );
  while (!Serial);
  Serial.println("Start");
  if (Usb.Init() == -1) Serial.println("OSC did not start.");
  delay( 200 );
  HidKeyboard.SetReportParser(0, &Prs);
}

void loop() {
  Usb.Task();
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.