Oh! Now I see what I missed. I am so sorry! You don't want to ignore ALL messages but just the first few one that the controller send on startup!
You are totally right with the assumption, that the buffer is your problem here. during your delay(4000) nothing is executed on the Teensy (of your main code). So midi1.read() is not called, so everything that happens is, that the buffer fills up and you handle the messages a few seconds later, so you just need to use a different way to wait.
My idea would be to use elapsed time to determine if you want to handle the messages or not.
Code:
elapsedMillis usb_host_wait_timer;
FLASHMEM void checkUSBHostStatus()
{
if (midi1 && !usbHostPluggedIn)
{
Serial.println("usbHostPluggedIn");
usb_host_wait_timer = 0;
usbHostPluggedIn = true;
}
else if (!midi1 && usbHostPluggedIn)
{
usbHostPluggedIn = false;
Serial.println("usbHostUnplugged");
}
}
FLASHMEM void usbHostControlChange(byte channel, byte control, byte value)
{
if (usbHostPluggedIn && usb_host_wait_timer > 4000)
{
myControlChange(channel, control, value);
}
}
This has an issue tho, as microseconds overflow after 49 days. so if you let the device run for 49 days without plugging in or out the usbhost, you might have a 4 second gap, where this code acts differently. You could avoid this, by resetting the variable before it overflows. This might be a quite hacky way, but it should work.
Code:
FLASHMEM void checkUSBHostStatus()
{
if( usb_host_wait_timer > 0xF000000) {
usb_host_wait_timer = 5000; // Reset to avoid 4 second long missbehaviour after 32bit overflow.
}
if (midi1 && !usbHostPluggedIn)
{
Serial.println("usbHostPluggedIn");
usb_host_wait_timer = 0;
usbHostPluggedIn = true;
}
else if (!midi1 && usbHostPluggedIn)
{
usbHostPluggedIn = false;
Serial.println("usbHostUnplugged");
}
}
Something you might also consider: midi1 knows the name of the plugged in device. I use that to show in the message I show to the user, when I recognize the usbhost. You could also use that to handle different devices differently (just reset the timer for the device that sends irrelevant data on startup)
The USBDriver (that is the base for USBMidi) has a method product:
Code:
const uint8_t *product();
you can cast that to const char* and use it as string.