Teensy\Tools\USB Type: How to make use of USB connection optional

Status
Not open for further replies.

Davidelvig

Well-known member
I have a MIDI device that can send usbMIDI out (and receive and discard inbound MIDI msgs)
The device also includes a MIDI synth chip from VLSI - which I drive using hardware serial, and output to a headphone jack.

All functions (both synth options) work fine when:
- USB Type = MIDI and I'm plugged into a PC or stand-alone MIDI synth (each of which are expecting USB MIDI)

My internal synth works fine when:
- USB Type = Serial and when plugged into a PC (and my use of any usbMIDI commands is commented out)

With either USB Type defined, the device fails to work (no internal synth sounds through the headphones) when plugged into a charge-only USB plug.

I can get the charge-only solution to work by defining USB Type = no USB and recompiling.

Questions:

  • how can I (without recompile) leverage USB data functions where they are useful and available on the other end of the USB plug, and still use other functions functions of the device when only power is available on USB?
  • will I have this same problem when going to battery power (and not USB power)?

Thanks!
- Dave
 
Hopefully a person who uses MIDI might answer...

But assuming that a Serial object is available either as Serial or HID type serial...

You might be able to check if Serial is valid and only output when it is...

That is something like:
Code:
if (Serial) {
   <do my midi output stuff> 
}

Again don't know if that is feasible or valid or not, but that would be something I would try...


Also in all cases like this that don't work when the Serial is not plugged into USB...
Make sure your code does not have something in it like:
Code:
while (!Serial) ;
That code would hang forever waiting for USB to be plugged in.

In my code I will often do something like:
Code:
while (!Serial && (millis() < 5000)) ;
In this way it will wait for up to 5 seconds waiting for USB Serial to be valid, but will continue after that.

Good luck
Kurt
 
@KurtE - You are the Jedi master!
Code:
While (!Serial)
was in my code

Commenting it out makes it all work when on USB charge-plug-only.

And after adding
Code:
While (!Serial && (milliseconds() < 5000))
... it all still works!

Thanks!
- Dave
 
Last edited:
Status
Not open for further replies.
Back
Top