Using USBMIDI; Pokitto boots up only if USB cable is connected

Hello

I have been making a MIDI footswitch controller with Pokitto. My first “big” Pokitto project and only early stages but it’s been fun so far. Start could not be easier; a little bit of hardware (wire from VCC to EXT0 to simulate switch), couple of lines of code and I had a working MIDI controller! :smiley:
Of course, I am still using the same one wire as a switch on the PEX header, and am now making perhaps-way-too-complicated menu system so that I can fully configure the digital and analog inputs to send all kinds of MIDI messages to PC. Oh well.

Back to the problem.

The program uses USBMidi library and everything works (computer receives MIDI messages OK) as long as I have the USB cable connected to computer.

But if the USB cable is disconnected, Pokitto does not start when I switch power on. The screen stays blank (or white; I don’t remember which way). Not even the loader (the one where I can set volume and so on) screen shows up.

If I comment out the “USBMIDI midi;” and “midi.write(parameters)” lines, Pokitto and the program starts with or without cable connected (of course without MIDI functionality).

Should it work like that?

When do you call those midi functions? Before or after you call begin() for the Pokitto::Core?

After. Here is a short sample main.cpp that demonstrates the problem:

#include "Pokitto.h"
#include "USBMIDI.h"

Pokitto::Core pokitto;
 	
USBMIDI midi;
DigitalIn button0 = DigitalIn(EXT0);

int main() {
    pokitto.begin();
    
    while (pokitto.isRunning()) {
	    if (pokitto.update()) {
		    if(button0.read()) {
		        midi.write(MIDIMessage::ControlChange(0, 0, 0));
		        pokitto.display.print("1");
			}
		    else {
		        pokitto.display.print("0");
		    }
		}
	}
    return 0;
}

USB cable disconnected: powering up brings white screen and nothing else happens.
Cable connected: EXT0 polling works, prints 0 or 1 on screen and sends MIDI messages.

EDIT: you can even remove the midi.write() call and it is the same. So it is just having the “USBMIDI midi;” outside main() that triggers this.

I was about to suggest that you move the midi class creation inside main(), after begin(), to see if the Pokitto startup screen occurs. My guess is that the USBMIDI midi hangs when it constructs the USBMIDI class.

If the class constructor requires the USB cable to be there you could dynamically create and destroy it when needed, using new and delete. Or see if the constructor have some parameters that could help.

Moving midi class creation after pokitto.begin() did something. Pokitto now gets past the loader, but then blocks at the midi class creation until I connect the cable. After that, the program works ok.

I can only give usb vendor,product and device id to the midi constructor so no help there. Constructor seems to call the base class USBDevice constructor. Midi constructor also calls USBDevice::connect with default parameter block=true. Tried to change that to false but did not help.

I will keep looking if there is any way to create the device so that it won’t block if the connection is not up yet.

@tomi this is a problem that occurs with other USB classes too, I am not sure if it is possible to try to make a connection and continue with an error if it fails. Would have to look inside the mbed USB library to see it

Happens with USBSerial exactly as you have described

@jonne If the mbed USB library you refer to is the same thing I am looking at mbed-os/drivers/usb at master · ARMmbed/mbed-os · GitHub, it looks like the USBMidi has a constructor that takes “bool connect_blocking” as parameter. Alas, the USB classes (USBMIdi, USBDevice) there have other differences to PokittoLib as well. I have not tried but I think it takes more than copying USBMIdi.cpp and .h over and recompile.

1 Like