ryankurte / rust-streamdeck

libusb based driver for Elgato StreamDeck devices
Mozilla Public License 2.0
57 stars 24 forks source link

Add `connect_with_hid` to StreamDeck #12

Closed markmandel closed 3 years ago

markmandel commented 3 years ago

Since only one HidApi instance can exist at any given point and time, having an option to manage the lifecycle of that instance outside of StreamDeck is useful.

The example I needed this new function for was to enable reconnection scenarios, as I couldn't call connect() while I still had an instance of StreamDeck in scope, because HidApi::new()? would return an Error.

ryankurte commented 3 years ago

looks great to me, thanks! is reconnecting to a disconnected device something it would be useful to support / automate within the driver?

in other USB libraries i have used lazy_static to maintain a static USB context which allows you to create one instance (only if/when it is used) and reference this as required, though I am not -sure- how this would behave if more than one library tried to create HidApi instances so this addition is still definitely useful!

markmandel commented 3 years ago

looks great to me, thanks! is reconnecting to a disconnected device something it would be useful to support / automate within the driver?

Oooh, that's a good question.

So basically I do it now within my tokio::spawnd loop that calls read_buttons from the library - I know that when I get an error, it's likely a disconnect, so I can wait a few seconds, then try and connect again (switching out my current deck StreamDeck object as I go.

Since this library doesn't handle that loop - the only other idea I would have is some kind of heartbeat ping to the streamdeck that gets tokio::spawnd during connect(), that basically does this in the background, and provides the reconnect (and maybe blocks read_buttons until it reconnects?)? Could maybe use the version information as the heartbeat?

That's the only idea I've got - but it's an interesting one!