Pages: [1]   Go Down
Author Topic: Library configuration  (Read 878 times)
0 Members and 1 Guest are viewing this topic.
France
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.

Logged

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

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2101
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged


France
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

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

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2101
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
« Last Edit: June 29, 2012, 01:21:41 pm by pYro_65 » Logged


France
Offline Offline
Newbie
*
Karma: 0
Posts: 31
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.


Logged

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

North Queensland, Australia
Offline Offline
Edison Member
*
Karma: 64
Posts: 2101
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged


Pages: [1]   Go Up
Jump to: