I now want to extend this code to also be able to communicate over Serial1 (Using RX / TX). I already have this functionality working in the Arduino IDE so my natural next step was trying to get this xinput library to work in the Arduino IDE.
Problem
The problem I'm currently running into though is all kinds of error messages about certain constants not being defined. After some digging I found out that a lot of code inside the library does checks on a number of constants. For example:
#if (F_USB == 8000000)
All these constants seem to be filled inside the makefile of the library, as can be seen here:
My question is now, how can I add these constants to the compiler used when compiling for the Arduino Leonardo using the Arduino IDE?
I already tried adding #define F_USB 16000000 on the top of my .INO file but that didn't work.
devedse:
My question is now, how can I add these constants to the compiler used when compiling for the Arduino Leonardo using the Arduino IDE?
There isn't really any convenient way to do this via the Arduino IDE. This was actually an intentional decision by Arduino because they feel it will lead library developers to unnecessarily create library interfaces that are not beginner friendly. I definitely see their point on that, but there are certain things you simply can't do without the preprocessor. In that case, the best solution I've found is to put all the library code in .h files. After doing that, any #define directives you have in your sketch before the #include directive for the header file will also be visible to the code in that header file.
There are also some less convenient/flexible methods:
Add the -D flags you need to the compiler.extra_flags property in preferences.txt. You can find the location of preferences.txt at the line following File > Preferences > More preferences can be edited directly in the file.
Use the Arduino IDE's CLI to set the preference, compile, and upload:
Thanks for the elaborate response, I'll check it all out tomorrow :).
One quick question, would it be possible to specify multiple things using this:
build.extra_flags=-DF_USB=16000000
Also, how would you specify something like this (as seen on line 140 in the makefile):
LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)"
devedse:
One quick question, would it be possible to specify multiple things using this:
build.extra_flags=-DF_USB=16000000
Sure. The value of this property is simply passed to the compiler during the build. You can add as many compiler options as you like.
If you need to specify flags that are only used for a specific compiler (build.extra_flags is used in recipe.c.o.pattern, recipe.cpp.o.pattern, recipe.S.o.pattern, recipe.preproc.includes, and recipe.preproc.macros), there are some more specific properties you can use the same way:
compiler.c.extra_flags
compiler.c.elf.extra_flags
compiler.S.extra_flags
compiler.cpp.extra_flags
compiler.ar.extra_flags
compiler.objcopy.eep.extra_flags
compiler.elf2hex.extra_flags
The "extra_flags" properties are intended to be used by the user however they like. However, you do need to be aware that these properties can be defined in a number of places (as I explained in my last reply. There is a specific order of overrides. A definition of a property in one place will replace the previous one. I don't remember whether boards.txt or platform.local.txt has the highest priority. I know that platform.txt has the lowest priority (a definition in boards.txt or platform.local.txt will override the one in platform.txt). That could cause some problems if an attempt was made to use the extra_flags properties for multiple things in multiple places.
devedse:
Also, how would you specify something like this (as seen on line 140 in the makefile):
LUFA_OPTS += -D USE_STATIC_OPTIONS="(USB_DEVICE_OPT_FULLSPEED | USB_OPT_REG_ENABLED | USB_OPT_AUTO_PLL)"
It depends on what compiler process you want those options to be passed to. Without looking at the Makefile, I can't tell you which it is, but a little bit of searching for LUFA_OPTS in that file will show you, then you need to add it to whichever of the extra_flags properties is appropriate.