Detecting USB modes, specifically joystick

Status
Not open for further replies.

MorganS

Well-known member
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?
 
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.
 
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:
[U]teensy31.menu.usb.hid=Keyboard + Mouse + Joystick
teensy31.menu.usb.hid.build.usbtype=USB_HID
[/U]
[B]teensy31.menu.usb.serialhid=Serial + Keyboard + Mouse + Joystick
teensy31.menu.usb.serialhid.build.usbtype=USB_SERIAL_HID[/B]
...
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=[COLOR="#FF0000"]USB_EVERYTHING[/COLOR]
 
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.
 
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...
 
...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.
 
Status
Not open for further replies.
Back
Top