passing string to function

Guys!

i'm trying to use the function call like this:

to_LCD(F("Hello world!"));

the function looks like this:

void to_LCD (char *s){
...
... some code 
...
u8g2.print(F(s));
u8g2.sendBuffer();
}

the error i got is: cannot convert 'const__FlashStringHelper*' for argument '1' to 'void to_LCD(char*)'

Tried not to use F() trick:

to_LCD("Hello world!");

the function looks like this:

void to_LCD (char *s){
...
... some code 
...
u8g2.print(s);
u8g2.sendBuffer();
}

No luck. this time i got warning: deprecated conversion from string constant to 'char*'
in the line: to_LCD("Hello world!");

So the question is: is it possible to call function with string argument to easily send various information on LCD using single LCD drawing function?:confused:
What is more: is it possible to do it using information string saved in progmem space using F() ???

Without F() it should work fine. If you want to use F(), make the function like

void toLCD(const__FlashStringHelper* s){
  u8g2.print(s)
}

The u8g2 library does support the FlashStringHelper in the .print() method.

septillion:
The u8g2 library does support the FlashStringHelper in the .print() method.

In which case you should do something like this....

void toLCD(const__FlashStringHelper* s)
{
  String str = s;
  u8g2.print(str);
}

Read again, it does support it :wink: So please don't introduce a haywire String....

The deprecated warning is because you are passing a string constant ("Hello World") to a function that expects a string pointer. To get rid of the warning, cast the string constant to a char pointer:

   to_LCD( (char *) F("Hello world!"));

and see if the warning goes away.

septillion:
Read again, it does support it :wink: So please don't introduce a haywire String....

Right you are.

And it is not quite right anyway....I am thinking of MFC CString which has a char* operator function.

With Arduino String class it should have been:

void toLCD(const__FlashStringHelper* s)
{
  String str = s;
  u8g2.print(str.c_str());
}

septillion:
Without F() it should work fine. If you want to use F(), make the function like

void toLCD(const__FlashStringHelper* s){

u8g2.print(s)
}




The u8g2 library does support the FlashStringHelper in the .print() method.

Better would be to add the above function to be able to use it on RAM strings as well as on PROGMEM strings.

But given the functionality of toLCD even something like

#define toLCD(a) u8g2.print(a)

would work (dropping the toLCD function), or using the only called function directly.

Using String to handle the const__FlashStringHelper* parameter is a bad idea.

Use this to get rid of the warning:

void to_LCD (cosnt char *s){
...
... some code 
...
u8g2.print(s);
u8g2.sendBuffer();
}

gfvalvo:
Use this to get rid of the warning:

void to_LCD (cosnt char *s){

...
... some code
...
u8g2.print(s);
u8g2.sendBuffer();
}

...but make sure you spell "const" correctly.

gfvalvo:
Use this to get rid of the warning:

void to_LCD (cosnt char *s){}

TolpuddleSartre:
...but make sure you spell "const" correctly.

Excellent catch! (+).

econjack:

   to_LCD( (char *) F("Hello world!"));

and see if the warning goes away.

I think it does but I also think the outcome is scrambled eggs... Now it will try to use a PROGMEM pointer to read random data in RAM....

I've used this and it works:

void serialPrint(const char* msg) { Serial.print((__FlashStringHelper*)msg); }

serialPrint(PSTR("Yay!"));

The "F" macro does nothing else than to cast a PSTR to __FlashStingHelper*. The code above requires the argument to be of type PSTR, or else........! :wink: