Pages: [1]   Go Down
Author Topic: How to generically pass parameters to a function. General C++ question  (Read 890 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

I would like some help writing a function. The need is the following:

I have an arduino and an LCD display attached on it.
I use lcd.print function to write things on the LCD.
I need to write a function that enables or disables all writes on the LCD replacing the lcd.print depending on a "condition".

Something like that:

Code:
void my_lcd_print (?????????  txt)
{
  if (condition)
  lcd.print (txt);
}

"condition" will be a globally defined variable.
I need to pass to this function (my_lcd_print) all kind of types of arguments lcd.print accepts (int, strings etc) without casting compilation problems.

Thanks for the help!
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Not familiar with the LCD screen, but as far as my (limited) knowledge of Arduino programming goes, its rather impossible to define an argument for a method that can take multiple or all forms. I would suggest to either store the to-be-printed material in global variables, and handle the different formats with different methods, or use an identifier in the argument to tell the method what kind of data is being printed. Perhaps with a switch case or something.

Code:
int a
string b
byte c

int identifier

void print(identifier){
   switch(identifier)
     case 1:  print a
     case 2:  print b
     case 3:  print c

}

I have no clue exactly what the extend of the expected data formats is, but this is a general solution which should be easily implementable

EDIT: perhaps you can set up 'condition' as to take care of selecting the correct method to handle the data. Some actual code would be useful, primarily the part which fills/selects the data for the final printing. How do you decide which variables are printed? The casting question I cannot answer, but would be useful if the method would simply print what you feed it...
« Last Edit: April 03, 2013, 02:05:33 am by jzuidema » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Turn the problem inside-out, and just use a macro.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Turn the problem inside-out, and just use a macro.

Can U please give me a hint on macros?
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Templates are custom-made for that sort of thing.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define MY_LCD_PRINT(x) do {if (condition) lcd.print (x);  } while (0)
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 28
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define MY_LCD_PRINT(x) do {if (condition) lcd.print (x);  } while (0)


Thx AWOL.
will try that.....


Is there a "non compiler" way to do that? (pass arguments)?
« Last Edit: April 03, 2013, 02:13:12 am by vactirio » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Macros are non-compiler - they use the preprocessor.

For a compiled, type-safe method, do as Nick suggests and use templates.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
#define MY_LCD_PRINT(x) do {if (condition) lcd.print (x);  } while (0)


This seems a lot like what OP already gave, so I guess the difference is is that this macro can take any argument? (string byte int)
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 238
Posts: 24298
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A macro is simply a text-replacement mechanism, so yes, it can take any type as argument.
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Offline Offline
Newbie
*
Karma: 0
Posts: 20
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

A macro is simply a text-replacement mechanism, so yes, it can take any type as argument.

Ok thanks....Im now at a level that I can program virtually anything I want on Arduino, but it is things like this I still have to read up about. My JAVA-only background doesnt help.... smiley-wink
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This is how you would do it with templates:

Code:
boolean condition = true;

template <typename T> void my_lcd_print (T arg);
template <typename T> void my_lcd_print (T arg)
  {
  if (condition)
    Serial.println (arg);
  }

void setup ()
  {
  Serial.begin (115200);
  my_lcd_print (42);
  my_lcd_print ("Hi there");
  my_lcd_print (12.34);
  }  // end of setup

void loop () { }

Output:

Code:
42
Hi there
12.34

I'm passing three different types there, to a single function. The "template" part makes the compiler generate one function per type.
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3452
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I have not needed to use templates but they sound rather neat.
Automatic generation of an overloaded function by the compiler.
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 452
Posts: 18694
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, they're neat.

That how the STL (standard template library) does things like make lists of any type. The actual code to do a list is a templated function (or functions) so it can handle any type.
Logged

Pages: [1]   Go Up
Jump to: