Making decisions based on pre-processors

I am using two USB Host Shields, one from Sparkfun and the other from Circuits @ home. Both the shields using the library which was created by Circuits @ home.

The SparkFun shield has wrong pin mappings and in order to make it work with the library, we have to make some changes to the pin mappings in one of the header files. SparkFun USB-C Host Shield - DEV-21247 - SparkFun Electronics

I have this library in the sketches-folder/library folder. Now I don't want to maintain two different version of library or manually edit the library every time I use a different shield. I thought of defining a preprocessor in my sketch (before including the header files) and modify the library to check for this preprocessor and based on which I can set the correct pin mappings. For some reason this is not working fine.

Is there something wrong in my approach? Also is there a better way for doing this? Thanks!

That's a way to do it. Just be aware, that once a library has been compiled, the defines stick and only change if you recompile it. For examples based on the processor type, look at the code of most libraries (eg Servo). They handle this way the hardware communication.

An alternative is to create a base class with a pin assignment constructor and the create derived classes for each board, which basically just contain a constructor without parameters which calls hardcoded the constructor with the proper pin assignment. All work then happens in the parent class.

Korman

Thanks for letting me that it is the right way to go. Unfortunately it is not working for me.

I am doing a #define before including the libraries header files. Will it still work? Also are the libraries re-compiled every time I upload the sketch?

Regarding the second approach, I could not do it right now because it will involve re factoring lot of code in the original library and I am not sure if the original author of the library would be interested in merging my changes.

Korman:
Just be aware, that once a library has been compiled, the defines stick and only change if you recompile it.

Hey by the way, I think in recent Arduino releases, the libraries are recompiled every time, so the defines can now be expected to always stick.

I am doing a #define before including the libraries header files.

That definitely will not work. The define needs to be IN the library header files, so the library's C/CPP files will pick up the change.

I am doing a #define before including the libraries header files.

That definitely will not work. The define needs to be IN the library header files, so the library's C/CPP files will pick up the change.

Oops, I guess that is going to defeat my purpose. I don't want to change the library file every time, but want to control it from my sketches.

Is there any other alternative?

In normal C/C++ we can define some preprocessors for the compiler in the make file. Is there something like that available for Arduino?

http://arduino.cc/pipermail/developers_arduino.cc/2011-March/004592.html


From the Developer's List but not yet archived...

Hi guys,

A quick note to say that I have been unable to focus on this the last few weeks (work before play...).

(I had decided to implement a more elegant way: to put it all into a preferences.txt file in the current project dir, but not yet gotten around to write a single line of code...)

In case people want to play with the cflags.txt hack, only two files need to change, I have put them into 2 separate diffs (against 0022):

app/src/processing/app/debug/Compiler.java:
http://pastie.org/1743110

app/src/processing/app/Sketch.java:
http://pastie.org/1743115

(Note that this latest patch works so the 'cflags.txt' can be multi-line (empty lines and lines starting with a "#" or a ";" just being ignored, the rest included in the compile command).)

Cheers!
Hugo

Thanks for pointing this out, this will solve my case.

Any idea whether the patched jar is available somewhere for me to download? It could save me sometime in trying to setup Java and compile everything myself.

Sudar:
Any idea whether the patched jar is available somewhere for me to download?

Sorry, I have no idea.

Try sending Hugo a Personal Message. In my experience he has been friendly and helpful. I suspect, if he checks his Personal Messages and has the time, he will try to help. If you don't hear from him after a reasonable amount of time, let me know and I'll try to drum-up an email address for him.