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

Thread: Detecting USB modes, specifically joystick

  1. #1
    Junior Member MorganS's Avatar
    Join Date
    Apr 2015
    Location
    Central Oregon
    Posts
    12

    Detecting USB modes, specifically joystick

    I'm breadboarding a project right now and I'm going to be using a bunch of the USB modes which aren't Serial. Specifically joystick and flightsim. So I'm compiling my test code with different USB options and switching back and forth a lot.

    If I use features specific to a mode without compiling in that mode, then it is an error. So I wrap my code in some #ifdef to see if the USB mode required is active. For example...

    Code:
    #ifdef FLIGHTSIM_INTERFACE
      FlightSimFloat xpYokePitch, xpSurfacePitch, xpPitchTrim;
    #endif
    That one works great. But I can't do the same for joystick. The obvious #ifdef JOYSTICK_INTERFACE doesn't work. I even copy-pasted from the USB HID header files to make sure I typed it correctly. Other variants I tried such as #ifdef USB_FLIGHTSIM_JOYSTICK don't work either.

    So how do I write #ifdef JOYSTICK_INTERFACE in a way that works when the joystick USB is enabled?

  2. #2
    Senior Member vjmuzik's Avatar
    Join Date
    Apr 2017
    Location
    Florida
    Posts
    467
    The defines get undefined in usb_undef.h in the Teensy core, if you go into that file and comment out the undefines then the #ifdef JOYSTICK_INTERFACE works. I don't know if this will cause any other issues, but this is why it's not working like you would expect it to.

  3. #3
    Senior Member+ defragster's Avatar
    Join Date
    Feb 2015
    Posts
    10,641
    What appears on the command line output in console?

    For a build of EVERYTHING it is :: -DUSB_EVERYTHING

    Those are set up in boards.txt like:
    Code:
    teensy31.menu.usb.hid=Keyboard + Mouse + Joystick
    teensy31.menu.usb.hid.build.usbtype=USB_HID
    
    teensy31.menu.usb.serialhid=Serial + Keyboard + Mouse + Joystick
    teensy31.menu.usb.serialhid.build.usbtype=USB_SERIAL_HID
    ...
    teensy31.menu.usb.serialmidi4.build.usbtype=USB_MIDI4_SERIAL
    teensy31.menu.usb.flightsim.build.usbtype=USB_FLIGHTSIM
    teensy31.menu.usb.serialmidi16.build.usbtype=USB_MIDI16_SERIAL
    teensy31.menu.usb.audio.build.usbtype=USB_AUDIO
    teensy31.menu.usb.serialmidiaudio.build.usbtype=USB_MIDI_AUDIO_SERIAL
    teensy31.menu.usb.everything.build.usbtype=USB_EVERYTHING

  4. #4
    Junior Member MorganS's Avatar
    Join Date
    Apr 2015
    Location
    Central Oregon
    Posts
    12
    Thanks vhmuzik. That explains a lot. It explains why the flightsim interface isn't undefined because the code for that one is slightly different to all the rest:

    Code:
    #ifdef FLIGHTSIM_INTERFACE
    #undef FLIGHTSIM_TX_ENDPOINT
    Defragster's suggestion does work but joystick shows up in a whole bunch of different USB modes defined in the menu: Everything, Keyboard+Mouse+Joystick, Flightsim+Joystick... I was hoping for a method that actually says if the joystick has been defined, maybe even in some custom menu item that I invent later.

  5. #5
    Senior Member+ KurtE's Avatar
    Join Date
    Jan 2014
    Posts
    6,117
    Sounds like the FLIGHTSIM_INTERFACE one is maybe a bug (maybe a convenient bug)

    It is also interesting that the undef header file might not be included in T4 sketches in the same way...


    Obviously you would only need to check all of the USB_MODES once in your header file or the like, and then potentially define your own, that you use elsewhere.

    I am sort of surprised that all of these defines are undefined. Example I could imagine a sketch may want to know the JOYSTICK_SIZE in order to know how many buttons or axis are defined...

  6. #6
    Junior Member MorganS's Avatar
    Join Date
    Apr 2015
    Location
    Central Oregon
    Posts
    12
    ...and it's not like I want to re-use the name FLIGHTSIM_TX_ENDPOINT in an unrelated #define. Cleaning up is a good thing but this seems to throw the baby out with the bathwater.

    I'll use KurtE's suggestion. I'll redefine JOYSTICK_INTERFACE in my own code by detecting the menu modes.

Posting Permissions

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