Xinyuan-LilyGO / LilyGo-EPD47

GNU General Public License v3.0
404 stars 121 forks source link

USB Host feature available? #132

Open unkyulee opened 1 day ago

unkyulee commented 1 day ago

Can't manage to get USB host features. I am trying to get the usb keyboard recognized by T5-ePaper-S3 but no avail. Does anyone know if usb host is supported and have any example using usb host features?

#include "usb.h"
//
#include "app/app.h"
#include "config/config.h"
//
#include "keyboard/hid/hid.h"

class MyEspUsbHost : public EspUsbHost
{
  void onReceive(const usb_transfer_t *transfer)
  {
    JsonDocument &app = app_status();
    app["hid_usb"] = true;
  }
  void onGone(const usb_host_client_event_msg_t *eventMsg)
  {
    JsonDocument &app = app_status();
    app["hid_usb"] = false;
  }

  // USB Keycode is sent when key is pressed or released
  void onKeyboard(hid_keyboard_report_t report, hid_keyboard_report_t last_report)
  {
#ifdef DEBUG
    app_log("%02x %02x %02x %02x %02x %02x %02x %02x\n", report.modifier, report.reserved, report.keycode[0], report.keycode[1], report.keycode[2], report.keycode[3], report.keycode[4], report.keycode[5]);
#endif

    // Key Pressed
    for (int i = 0; i < 6; i++)
    {
      if (report.keycode[i] != 0)
      {
        // check if the same key appear in the previous report
        bool newkey = true;
        for (int j = 0; j < 6; j++)
        {
          if (last_report.keycode[j] == report.keycode[i])
          {
            newkey = false;
            break;
          }
        }

        // otherwise register new key press
        if (newkey)
        {
          // handle key pressed
          keyboard_hid_pressed(report.keycode[i], report.modifier);
        }
      }
    }

    // Key Released
    for (int i = 0; i < 6; i++)
    {
      if (last_report.keycode[i] != 0)
      {
        bool key_released = true;
        for (int j = 0; j < 6; j++)
        {
          if (last_report.keycode[i] == report.keycode[j])
          {
            key_released = false;
            break;
          }
        }
        if (key_released)
        {
          keyboard_hid_released(last_report.keycode[i], report.modifier);
        }
      }
    }
  }
};

// Handler for USB Keyboard
MyEspUsbHost usbHost;

//
void keyboard_usb_setup()
{
  //
  app_log("Initializing USB Keyboard\n");

  // usb host setup
  usbHost.begin();
}

//
void keyboard_usb_loop()
{
  // Process USB Keyboard Tasks
  usbHost.task();
}
lewisxhe commented 1 day ago

TYPE-C does not have the ability to provide external power. If you want to act as a host, you need to provide additional power to the slave device.

https://github.com/espressif/arduino-esp32/tree/master/libraries/USB/examples