Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 7 of 7

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

  1. #1
    Junior Member
    Join Date
    Jan 2020
    Posts
    17

    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.

  2. #2
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    11,571
    Not indicated what version of TeensyDuino and what OS?

    There was an issue with first release of T_4 USB keyboard on Windows. Timing was off - it compiled ... but was a runaway train smashing out random key kraap.

    Here TD 1.50b1 is in use on Windows 10 and I get this:
    Code:
    B0 press
    AB0 release
    B0 press
    AB0 release
    B0 press
    AB0 release
    Using :: ...\examples\Teensy\USB_Keyboard\Buttons\Buttons.p de

    It works on all the buttons - but this change was made only to the first entry in the above example:
    Code:
      if (button0.fallingEdge()) {
        Keyboard.println("B0 press");
        Keyboard.print("A");
      }

  3. #3
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    21,871
    Quote Originally Posted by smarrocco View Post
    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.

  4. #4
    Senior Member
    Join Date
    Apr 2014
    Location
    Germany
    Posts
    901
    If you want to see the effects of the various board settings on the build options you can give VisualTeensy https://github.com/luni64/VisualTeensy a try. Basically it generates makefiles from the board settings. You can watch the changes in the makefile while you change settings. Here a quick video showing this https://www.youtube.com/watch?v=MCPcgUxzP8U&t=0s (look at the displayed makefile on the right side)

    (Of course, instead of tweaking platformIO, you can directly use VisualTeensy to build your projects. It also uses vsCode as IDE )
    Last edited by luni; 01-31-2020 at 07:43 AM.

  5. #5
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    11,571
    I also tested using TSet Batch file on Windows and it works to kick off the IDE Build from the command line.

  6. #6
    Senior Member blackketter's Avatar
    Join Date
    May 2015
    Location
    San Francisco
    Posts
    262
    You can edit your platformio.ini file and add one of the USB defines there. They are listed here in the platformio documentation:

    https://docs.platformio.org/en/lates...l#usb-features

  7. #7
    Junior Member
    Join Date
    Jan 2020
    Posts
    17
    Suggested solutions have resolved the issue...

    @defragster--Windows 10 OS, Teensy Loader 1.49.

    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •