USB Host -- Device claimed / disconnect callbacks

yeahtuna

Well-known member
I have a quick look in USBHost_t36 and could find any callbacks for when a device is claim / disconnected. Does such a thing exist?

Code:
MIDIDevice_BigBuffer midi1(myusb);
MIDIDevice_BigBuffer midi2(myusb);
MIDIDevice_BigBuffer midi3(myusb);
MIDIDevice_BigBuffer midi4(myusb);

I know for something like this, I can simply do the following:
Code:
if (midi1) {
   // do stuff
}

However, if I have several objects, it's wasteful to be continuously checking these boolean values. It would be nice if there was a callback (even just a single one for the entire host port) that could notify my code so I can determine what is connected / has been disconnected in an efficient matter. Does this functionality already exist? If not I'll roll my own.
 
I managed to have the USBHost_t36.h set a flag whenever a device gets claimed. I can then periodically check and clear this one flag and do all my hotplugging / updating work. Let me know if you want so code and I'll post the bits here.
 
there is an example within the USBHost_t36 library, that lists all connected devices.
The device (in your case, Midi device) has the boolean operator implemented.
So, if(midi1) tells you if the device is connected, or not. And as you are calling usb.Task() anyway, you can check for the availability and do something with that, without changing anything in the Library.
If you are doing USBHost Midi, you might find this post, I wrote a few days ago interesting, about finding out, how many virtual cables a connected device supports: https://forum.pjrc.com/threads/5482...cables-(ports)?p=195016&viewfull=1#post195016
 
If you reread my post, you'll see that I fully understand how to check if a midi device is connected, but you can't argue that method2 below is not typically 2 times faster than method1. In my case there are 4 midi, 4 rawhid, and a filesystem object. While it only saves a few microseconds, I always need every microsecond I can get.

Code:
void method1() {
   if (midi1 != midi1ConnectedState {
      //update
   }
   if (midi2 != midi2ConnectedState {
      //update
   }
}

void method2() {
   if (usbHost.devicesHaveChanged() {
      if (midi1 != midi1ConnectedState {
         //update
      }
      if (midi2 != midi2ConnectedState {
         //update
      }
   }
}
 
While it only saves a few microseconds, I always need every microsecond I can get.

I am with you on that performance thing. I have everything I need tight (sequencing) in a „thread“ triggered by an interrupt, to get a rock solid clock. I try to save every micro I can there too. But that’s about optimizing lookups, precalculation of modulation etc.
UI and and stuff like Devicemanagement is done in the main loop, that also renders everything to the display . A microsecond more is nothing you can measure in that scenario :) As long as I feel no latency in inputs and outputs, I am fine there. It’s more important to have midi tight (for me). And that’s working awesome right now. As long as I can provide all the data faster than those tiny 31250bit/sec can transport, everything is fine :)
But to be fair: I only use USB Host for up to 4 MIDI devices (and filesystem for backups, but that’s nothing I have to consider when having it sequence).
 
Back
Top