Go Down

Topic: How to control the #define in a library? I mean... (Read 1 time) previous topic - next topic

liudr

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: [Select]
#define phi_prompt_max_item_display_length 20

In the .cpp file:
Code: [Select]
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: [Select]
#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!

sixeyes

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

robtillaart


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

Rob Tillaart

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

Jack Christensen

Evidently all great minds run in the same ruts XD

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

liudr


Evidently all great minds run in the same ruts XD

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.

sixeyes


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

liudr

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

sixeyes

Does this help:

Code: [Select]
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


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy