adafruit / Adafruit_CircuitPython_HID

USB Human Interface Device drivers.
MIT License
364 stars 106 forks source link

Not connecting USB to host causes either hang or crash #93

Closed CarbonNeuron closed 2 years ago

CarbonNeuron commented 2 years ago

I am writing a script that needs to run when the computer first turns on. When it is still turning on sometimes there is a large delay between when power is given to the board and when the USB controller in the computer wakes up. I am using the keyboard module and mouse module. When I was running circuitpython on my neotrinkey, and the computer was taking longer then normal to turn on, it would start flashing some code. I do not have a second pico to debug on. This is a pretty niche use case but it also will happen if you just connect your pico to power and no data. Maybe there is a way to tell when the host is ready to accept connection or throw an error if it is not ready? That way you can just loop the connect command or return a message if there is no host device.

CarbonNeuron commented 2 years ago

As reference this will happen if you run https://github.com/adafruit/Adafruit_CircuitPython_HID/blob/2fddabcaf0df1763111ed9dbf9e2d4cdb5b0434e/examples/hid_keyboard_shortcuts.py when you only have power hooked up to the board and no data.

CarbonNeuron commented 2 years ago

Probably has something to do with #13. Maybe we can add a custom timeout to the initialization of the modules? instead of waiting a arbitrary 1 second.

CarbonNeuron commented 2 years ago

I've fixed my crashing using catches, but a failed initialization takes 2.14 seconds on average on a pi pico. So that's around 0.5 seconds for each attempt to init and 1 second spent asleep. Maybe there should be either a toggle for weather or not to try again automatically, or give the ability to adjust the time between retries.

dhalbert commented 2 years ago

Could you paste your simplest working example? Thanks.

CarbonNeuron commented 2 years ago

I ended up just forking and fixing for my use case. Sorry about that.

ladyada commented 2 years ago

@CarbonNeuron if its a bug, please submit a PR for the fix, it would be helpful :)

CarbonNeuron commented 2 years ago

@CarbonNeuron if its a bug, please submit a PR for the fix, it would be helpful :)

It's not really a bug. My use case of powering a board without the data lines of USB connected is very specific. And it can be handled normally with try catch.