Is it possible to test the name of a USB MIDI device connected to the USB host port?

cowboy

New member
I've got a Teensy 4.1, and I'm trying to adapt the File > Examples > USBHost_t36 > MIDI > Interface_16x16 example, using a sketch with the MIDIx4 USB type. It's working fine, but I want to be able to filter the USB MIDI devices connected to the 2nd USB port by their USB product name. For example, when I connect my Launchkey Mini MK3 to my PC, I see the following MIDI devices:

* "Launchkey Mini MK3 MIDI"
* "MIDIIN2 (Launchkey Mini MK3 MID"
* "MIDIOUT2 (Launchkey Mini MK3 MI"

But I can't figure out what I need to do in Teensy code. Is there a way to test that the USB product name of a connected MIDI device is a certain value? See the "if (SOMETHING.equals(MY_DEVICE_PRODUCT_NAME)) {" bit in my code.

Code:
#include <USBHost_t36.h> // access to USB MIDI devices (plugged into 2nd USB port)

USBHost myusb;
USBHub hub1(myusb);
USBHub hub2(myusb);
USBHub hub3(myusb);
USBHub hub4(myusb);
MIDIDevice midi01(myusb);
MIDIDevice midi02(myusb);
MIDIDevice midi03(myusb);
MIDIDevice midi04(myusb);
MIDIDevice * midilist[4] = {
  &midi01, &midi02, &midi03, &midi04
};

void setup() {
  myusb.begin();
}

String MY_DEVICE_PRODUCT_NAME = String("MIDIOUT2 (Launchkey Mini MK3 MI");

void loop() {
  // Read messages arriving from the (up to) 4 USB devices plugged into the USB Host port
  for (int port = 0; port < 4; port++) {
    if (midilist[port]->read()) {
      uint8_t type =       midilist[port]->getType();
      uint8_t data1 =      midilist[port]->getData1();
      uint8_t data2 =      midilist[port]->getData2();
      uint8_t channel =    midilist[port]->getChannel();
      const uint8_t *sys = midilist[port]->getSysExArray();
      if (SOMETHING.equals(MY_DEVICE_PRODUCT_NAME)) {
        doSomething();
      }
    }
  }

  // Read messages the PC (upstream host) sends and ignore them
  while (usbMIDI.read());
}

Thanks!
 
It is certainly possible using the USBDriver objects. I found the "joystick" example useful and here is a slightly modified version for inspiration.

I would recommend to compare with product/vendor id.

Code:
#include <USBHost_t36.h>
#include <MIDI.h>

// NOTE: change usb device configuration to match your hw
USBHost myusb;
USBHub hub1(myusb);
USBHub hub2(myusb);
USBHub hub3(myusb);
USBHub hub4(myusb);
MIDIDevice_BigBuffer midi1(myusb);
MIDIDevice_BigBuffer midi2(myusb);
MIDIDevice_BigBuffer midi3(myusb);
USBSerial_BigBuffer userial(myusb);

// And also this section
USBDriver *drivers[] = {&hub1, &hub2, &hub3, &hub4, &midi1, &midi2, &midi3, &userial};
define CNT_DEVICES (sizeof(drivers)/sizeof(drivers[0])) 
bool driver_active[CNT_DEVICES] = {false, false, false, false, false, false, false, false};
const char * driver_names[CNT_DEVICES] = {"hub1", "hub2", "hub3", "hub4", "midi1", "midi2", "midi3", "userial"};

void setup() {
 Serial.begin(115200);
 Serial.println("START");
  delay(200); //Wait to turn on USB Host
  myusb.begin();
}

void loop() {
  // put your main code here, to run repeatedly:
  myusb.Task();
  PrintDeviceListChanges();
}

void PrintDeviceListChanges() {
  for (uint8_t i = 0; i < CNT_DEVICES; i++) {
    if (*drivers[i] != driver_active[i]) {
      if (driver_active[i]) {
        Serial.printf("*** Device %s - disconnected ***\n", driver_names[i]);
        driver_active[i] = false;
      } else {
        Serial.printf("*** Device %s %x:%x - connected ***\n", driver_names[i], drivers[i]->idVendor(), drivers[i]->idProduct());
        driver_active[i] = true;

        const uint8_t *psz = drivers[i]->manufacturer();
        if (psz && *psz) Serial.printf("  manufacturer: %s\n", psz);
        psz = drivers[i]->product();
        if (psz && *psz) Serial.printf("  product: %s\n", psz);
        psz = drivers[i]->serialNumber();
        if (psz && *psz) Serial.printf("  Serial: %s\n", psz);
      }
    }
  }
}

Cheers, Daniel
 
I was able to do this, which worked!

Code:
String MY_DEVICE_PRODUCT_NAME = "Launchkey Mini MK3";

int port = 0;
String productName = (char*)midilist[port]->product();

if (productName.equals(MY_DEVICE_PRODUCT_NAME)) {
  doSomething();
}
 
Back
Top