Go Down

Topic: Library configuration (Read 967 times) previous topic - next topic

mgth

Hello,

I'm currently writing an Arduino Library to support xPL protocol (more details there : http://www.mgth.fr).

Cause it is very memory angry, I try to get it configurable.
for now I just have a config.h inside the library folder, but I would like to have configuration sketch dependent. Does anyone have an idea on how to achieve that ?

thank you.

Mgth : ArduixPL http://www.mgth.fr

pYro_65

define a struct that a user can fill and pass to the library. or use #ifdef to check if custom values have been provided via #define

mgth

eh, it seams to work with #define I don't know why when I first tried, I got the feeling that defines declared into main sketch were not taken into accounts inside libraries cpp, but your right it is. I'm gonna do that.
does any body know if this is a normal c++ behaviour or a special Arduino magic ?

thanks for your reply.
Mgth : ArduixPL http://www.mgth.fr

pYro_65

#3
Jun 29, 2012, 08:16 pm Last Edit: Jun 29, 2012, 08:21 pm by pYro_65 Reason: 1
If you define things before including a file, the included file will have the defines in scope. They are valid with in the defining translation unit ( .cpp/.ino file being compiled + all code inside of #include files )

Is normal behaviour ( although IDE may have ordered things in such a way that a non working design will work ), consider a #include as literally pasting the included file's contents into the file being compiled.

mgth

After a second check it seams that I does not do what I want.

if I #define in the ino file, things are not defined while compiling cpp in libraries, it seams logical to me, but It does not help...

I would need some kind of global define, or maybe I'm wrong on the way of doing it.


Mgth : ArduixPL http://www.mgth.fr

pYro_65

Ahh yes, the cpp file for the library will be compiled before any file includes the library header. You would need a global header like global.h that the library and your code ( sketch includes first ) so the #defines are taken into account before compiling the library .cpp.

I think passing a struct of values is adequate ( not really much different that having a constructor with a lot of parameters ). And if there are a lot of options/settings, you could store the struct in program memory and pass the PGM pointer to the library for it to handle data access, but easier is having a predefined struct as a data member then if a user provides one in a constructor, copy the values into the struct member to overwrite the default values.

Go Up