touchgadget / esp32-usb-host-demos

ESP32S2 Arduino USB host printer, MIDI, and keyboard demos
MIT License
112 stars 15 forks source link

Don't work when MIDI Device re-plug #16

Open oujianshen opened 6 months ago

oujianshen commented 6 months ago

This is a great project, I can run it and all the functions are normal

When the MIDI Device is reinserted, it cannot be automatically recognized and needs to be restarted to function properly again I see that the new device is defined in the function in usbhelp.hpp as void _cliend_event_callback (const usbhost_cliend_event_msg_t event_msg, void arg) I guess this function was not called during the second insertion But my ability is limited and I can't change this code well Who can help fix this issue of reinsertion and rediscovery? Thank you very much

fastfourier666 commented 6 months ago

I made a hackjob fix here

The original does not do any cleanup when the device is unplugged - either in the sketch or the ESP USB Host library. I added a callback so usbhelp.hpp can call a function in the main sketch when the device is "gone".

I'm sure it is pretty janky, would love to hear some improvements. I know next to nothing about USB. Cheers.

qa2080639 commented 3 months ago

~~ I spent a whole day looking for this problem and finally found the reason.

use usb_host_interface_release,usb_host_device_close can fix it example code usbhhelp.hpp

case USB_HOST_CLIENT_EVENT_DEV_GONE:
    ESP_LOGI("", "Device Gone handle: %x", event_msg->dev_gone.dev_hdl);
    err = usb_host_interface_release(Client_Handle, event_msg->dev_gone.dev_hdl, 0);
    if (err != ESP_OK)
      ESP_LOGI("", "usb_host_interface_release: %x", err);
    err = usb_host_device_close(Client_Handle, event_msg->dev_gone.dev_hdl);
    if (err != ESP_OK)
      ESP_LOGI("", "usb_host_device_close: %x", err);
    break;
01GOD commented 2 months ago

case USB_HOST_CLIENT_EVENT_DEV_GONE: ESP_LOGI("", "Device Gone handle: %x", event_msg->dev_gone.dev_hdl); err = usb_host_interface_release(Client_Handle, event_msg->dev_gone.dev_hdl, 0); if (err != ESP_OK) ESP_LOGI("", "usb_host_interface_release: %x", err); err = usb_host_device_close(Client_Handle, event_msg->dev_gone.dev_hdl); if (err != ESP_OK) ESP_LOGI("", "usb_host_device_close: %x", err); break;

Can explain how? I tried it and I know of a skilled embedded engineer that also tried it.

When I flashed an ESP32-S3 dev kit clone with that in verbose mode, it simply flashes it and the transmit LED turns on, but no serial monitor messages sadly.

Recommended solutions? Thanks in advance!

EParisot commented 1 month ago

@fastfourier666 You did a great job thank you for sharing !