If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

USB Type: Keyboards vs. Serial? What does it do?

I've been having some difficulties with getting a compile to succeed for a Teensy 4 when my code utilizes the Keyboard library.

The code only successfully compiles (when it includes the command (Keyboard.print("A")) if the Arduino IDE menu item "USB Type:" is set to "Keyboard" instead of "Serial" (which causes a "Keyboard was not declared in this scope" error).

What exactly does changing the "USB Type:" menu item do to the compiling/linking process in the Arduino IDE that allows it to succeed? The ultimate reason I want to know is because I am using the same code with PlatformIO, and it is currently failing with the same error.....so I will be trying to perform the same "magic" that the Arduino "USB Type:" menu item does in PlatformIO as well.

so I will be trying to perform the same "magic" that the Arduino "USB Type:" menu item does in PlatformIO as well.

The menu merely causes code to be compiled with a different -D define. To see this in action, in Arduino click File > Preferences and turn on verbose output during compilation. Drag the divider in the lower part of the Arduino window, to make the black console part bigger, and click Verify to recompile. Scroll up and copy some of the compiler commands to a text editor. Then repeat for different USB Type settings. When you compare with the one you just copied, you'll see one of the -D defines changes with the USB Type. That's the change you'll need to cause within PlatformIO.

If you get this to work, maybe pass it along to Ivan or the other PlatformIO devs. I do not know if PlatformIO is capable of offering a Teensy-specific menu to select the USB Type, like we have with Arduino.... but it would be really nice if they would someday add that. So far, they've been pretty content to just do USB Serial and not make the rest easily available. They really need to hear from PlatformIO users wanting easy access to the other USB types (rather than the error-prone process of editing config files) to gain the motivation to put this sort of menu into PlatformIO.

I can confirm some of the timing issues you mentioned--Drilling into some of the keyboard libraries revealed that I had to place a tiny delay of 1 ms in the keyboard library to get everything to work (this was under the Arduino IDE and prior to my current issues). The library worked it the original state on a Pro Micro, but refused to do so on the Teensy 4 without the tiny delay. Perhaps the Teensy4 was just too darn fast.

@PaulSoffregen--thank you for the description of the 'defines' in the Arduino IDE and how to get the more verbose output for exploration. That helped considerably in understanding the system.

@luni--I'll check into Visual Teensy, it looks interesting. I wrote my own IDE many years ago for the Basic Stamp products and would love to revisit it for Teensy development in the future and incorporate some of what you have there.

@blackketter--the link you provided is exactly what I needed. Sifting through the docs on that page does show a '-D USB_KEYBOARDONLY' option, and incorporating it into my platformio.ini build_flags eliminated the error message ("'Keyboard' was not declared in this scope"). The compile is now successful and a quick test shows that the Keyboard library is working as expected.

@PaulSoffregen--Due to this, it appears that contacting the platformio devs isn't necessary per your suggestion.

Thank you all very much for the assistance!

Last edited by smarrocco; 01-31-2020 at 04:10 PM.
Reason: Added more detail