adafruit / Adafruit_TinyUSB_Arduino

Arduino library for TinyUSB
MIT License
469 stars 122 forks source link

Can you do an example of using multiple reports and report ids #230

Closed cnn123666 closed 1 year ago

cnn123666 commented 1 year ago

I want to use the hid_composite and hid_generic_inout functionality together I wrote it like this, but it doesn't work

#include "Adafruit_TinyUSB.h"

#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
  const int pin = 4; // Left Button
  bool activeState = true;

#elif defined(ARDUINO_FUNHOUSE_ESP32S2)
  const int pin = BUTTON_DOWN;
  bool activeState = true;

#elif defined PIN_BUTTON1
  const int pin = PIN_BUTTON1;
  bool activeState = false;

#else
  const int pin = 12;
  bool activeState = false;
#endif

enum
{
  RID_KEYBOARD = 1,
  RID_MOUSE,
  RID_CONSUMER_CONTROL, // Media, volume etc ..
  RID_GENERIC,
};

uint8_t const desc_hid_report[] =
{
  TUD_HID_REPORT_DESC_KEYBOARD( HID_REPORT_ID(RID_KEYBOARD) ),
  TUD_HID_REPORT_DESC_MOUSE   ( HID_REPORT_ID(RID_MOUSE) ),
  TUD_HID_REPORT_DESC_CONSUMER( HID_REPORT_ID(RID_CONSUMER_CONTROL) ),
  TUD_HID_REPORT_DESC_GENERIC_INOUT(64,HID_REPORT_ID(RID_GENERIC))
};

Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_NONE, 2, false);

void setup()
{

  usb_hid.setReportCallback(get_report_callback, set_report_callback);
  usb_hid.begin();

  pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);

  Serial.begin(115200);

  while( !TinyUSBDevice.mounted() ) delay(1);

  Serial.println("Adafruit TinyUSB HID Composite example");
}

void loop()
{
  delay(10);

  bool btn_pressed = (digitalRead(pin) == activeState);

  if ( TinyUSBDevice.suspended() && btn_pressed )
  {
    TinyUSBDevice.remoteWakeup();
  }

  /*------------- Mouse -------------*/
  if ( usb_hid.ready() && btn_pressed )
  {
    int8_t const delta = 5;
    usb_hid.mouseMove(RID_MOUSE, delta, delta); // right + down

    delay(10);
  }

  /*------------- Keyboard -------------*/
  if ( usb_hid.ready() )
  {
    static bool has_key = false;

    if ( btn_pressed )
    {
      uint8_t keycode[6] = { 0 };
      keycode[0] = HID_KEY_A;

      usb_hid.keyboardReport(RID_KEYBOARD, 0, keycode);

      has_key = true;
    }else
    {
      if (has_key) usb_hid.keyboardRelease(RID_KEYBOARD);
      has_key = false;
    }

    delay(10);
  }

  /*------------- Consumer Control -------------*/
  if ( usb_hid.ready() )
  {

    static bool has_consumer_key = false;

    if ( btn_pressed )
    {
      usb_hid.sendReport16(RID_CONSUMER_CONTROL, HID_USAGE_CONSUMER_VOLUME_DECREMENT);
      has_consumer_key = true;
    }else
    {
      if (has_consumer_key) usb_hid.sendReport16(RID_CONSUMER_CONTROL, 0);
      has_consumer_key = false;
    }
  }
}

uint16_t get_report_callback (uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
{
  (void) report_id;
  (void) report_type;
  (void) buffer;
  (void) reqlen;
  return 0;
}

void set_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
{
  (void) report_id;
  (void) report_type;

  usb_hid.sendReport(RID_GENERIC, buffer, bufsize);
}
hathach commented 1 year ago

Not a valid feature request. Please use discussion for question