Function to Check if USB is Plugged In in Code

Status
Not open for further replies.

grinch

Well-known member
Hi, like the post title says, I'm wondering if there's any library function I can use to test whether or not a usb cable is plugged into the Teensy. I have a project that is compatible with a variety of control standards (midi over usb being one), and I'd like for the board to automatically detect which one the user is working with. Unfortunately this is a very hard topic to google since all the results are just people asking about the connection from their Teensy to the IDE.

Is there any function that exists which will do this? Something like:

Code:
if(usbispluggedin()){
//do usb midi stuff
}
 
If you declare a global variable like

Code:
USBHost gUSBHostPort;		// g prefix for "global"
USBHub gUSBHostHub(gUSBHostPort);
MIDIDevice gInputMIDIUSBDevice(gUSBHostPort);

then in main loop you can simply do

Code:
gUSBHostPort.Task();
  if (gInputMIDIUSBDevice)	// Input device could have been disconnected after a while (after setup()), so always check
  {
[...]
 
If you declare a global variable like

Code:
USBHost gUSBHostPort;		// g prefix for "global"
USBHub gUSBHostHub(gUSBHostPort);
MIDIDevice gInputMIDIUSBDevice(gUSBHostPort);

then in main loop you can simply do

Code:
gUSBHostPort.Task();
  if (gInputMIDIUSBDevice)	// Input device could have been disconnected after a while (after setup()), so always check
  {
[...]

Thanks for the reply.

Is this code for the built-in USB port? The one used for Serial messages and uploading programs? This seems like this might be for the second USB port that is meant to host devices like keyboards, which is not what I'm looking for. I'm looking to check if a usb cable is plugged in to the main USB port used for programming.

Also, can you provide a link to the library that this code comes from? I think being able to look at that would answer a lot of my questions. Thanks!
 
Status
Not open for further replies.
Back
Top