How to write a funtion that takes strings in flash for argument.

How can I fix the function do_something in the above code so that it accepts both regular and flash strings?

Full sketch:

void setup()
	do_something("blah blah") ; /// Ok
	do_something(F("blah")) ; /// Does not compile

void loop() 

void do_something(const char text[])
	Serial.println(text) ;

You have the source of the F() macro, and the functions which use it - why not see how they work?

I do respect your time. I never post a question until I have done everything to find the answer by myself. and, yes, I have learned a lot about the F macro and the like. this blog post was one of the most helpful.

Sayiing that, if you have an answer to my question, I would be very glad to go a step further :wink:

The keyword is __FlashStringHelper. It is an undefined type used to mark strings stored in Flash using the F-macro. The type and macro are defined here...

This is how to prototype a function that uses an F-macro parameter...

And this is how to implement the function...

The short answer is that you will have to overload that function so that there are two versions, one that takes a regular char* string and one that takes a __FlashStringHelper*.

Many thanks to Coding Badly and Delta_G.
Here is the solution I managed to write:

#include <PString.h> ///

void setup()
	do_something(F("blah blah")) ;
	do_something("blah") ;
	Serial.println("Done") ;

void loop() {}

void do_something(char *t)
	String s(t) ;
	s+= " !" ;
	Serial.println(s) ;

void do_something(const __FlashStringHelper *t)
	char buf[30] ;
	PString s(buf, 30) ;
	s.print(t) ;
	do_something(buf) ;

Lessons learned:

  • __FlashStringHelper* cannot be cast to char*.
  • I had to overload my function to handle this type (as Delta_G said).
  • Data in Flash can only be read one byte at a time.
  • PString can do that for me.

Data in Flash can only be read one byte at a time.

No different to RAM