Pages: [1]   Go Down
Author Topic: How to control the #define in a library? I mean...  (Read 992 times)
0 Members and 1 Guest are viewing this topic.
Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6937
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It's hard to describe in general terms so I'll use an example:

In a library I have a buffer. Its size should be the same as the width of the lcd. I have in the .h file:
Code:
#define phi_prompt_max_item_display_length 20

In the .cpp file:
Code:
char buffer[phi_prompt_max_item_display_length];

If someone uses a 40x2 LCD, I want to define phi_prompt_max_item_display_length as 40. I want the arduino sketch to be able to override the define in .h

I tried this in .h:
Code:
#ifndef phi_prompt_max_item_display_length      ///< Only define this if it has not been defined.
#define phi_prompt_max_item_display_length 20   ///< The render_list uses a buffer to render list item. 20 works for up to 20x4 displays to save SRAM.
#endif

I then have in arduino sketch #define phi_prompt_max_item_display_length 40 before it includes the .h
I get 40 in arduino sketch but I still get 20 in the library.

So are the define spaces separate between different .cpp files? How to do what I want to do? Thank you!
Logged


Guildford, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 217
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm not certain that you can do that given the way the Arduino build system works.

However if your library creates C++ classes, you could template the class, supplying the width of the LCD display as a template parameter.

Iain
Logged

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 170
Posts: 12487
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


You can add both - rows and colums - of the LCD as parameters of the constructor and malloc() the array.

Logged

Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

Grand Blanc, MI, USA
Offline Offline
Faraday Member
**
Karma: 71
Posts: 3540
CODE is a mass noun and should not be used in the plural or with an indefinite article.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Evidently all great minds run in the same ruts smiley-lol

http://arduino.cc/forum/index.php/topic,92382.msg694031.html#msg694031
Logged

MCP79411/12 RTC ... "One Million Ohms" ATtiny kit ... available at http://www.tindie.com/stores/JChristensen/

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6937
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Evidently all great minds run in the same ruts smiley-lol

http://arduino.cc/forum/index.php/topic,92382.msg694031.html#msg694031


Thank you Jack! Seems like there is no simple solution as both of us initially experimented. I gather, every .cpp compiles separately. Without a make mechanism in arduino, such as define xxx as yyy in the make file, the only way to communicate with another .cpp is at execution time. I can add another parameter to my lib initialization to take a max_item_length parameter or just dare my lib users to change this define in the .h file.
Logged


Guildford, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 217
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Without a make mechanism in arduino, such as define xxx as yyy in the make file, the only way to communicate with another .cpp is at execution time.
Not so. C++ templates offer a compile time link.

Iain
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6937
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Iain,

I am not familiar with the term C++ templates. I'm reading wiki and yes the concept does sound familiar after some reading. I need to read on. Would you be willing to show me a line or two pseudo code on passing a parameter to a template? Thanks.

Liudr
Logged


Guildford, UK
Offline Offline
Full Member
***
Karma: 0
Posts: 217
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Does this help:

Code:
template <int length>
class Test
{
  int buffer[length];
};

void setup()
{
  Test<20> shortLine;
  Test<40> longLine;
 
  Serial.begin(115200);
  Serial.println(sizeof(shortLine));
  Serial.println(sizeof(longLine));
}

void loop()
{
}

Iain
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 65
Posts: 6937
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Thanks Iain!
Logged


Pages: [1]   Go Up
Jump to: