has anyone a method for putting formatted text into PROGMEM:
// I want text in PROGMEM
const char* text = R"(
Some formatted text here
makes life easy for HTML
servers, for example.
)";
void setup() {
Serial.begin(9600);
Serial.print(R"(
Hello world! It
is nice to meet
you)");
Serial.println();
Serial.print(text);
}
void loop() {
}
// I want text in PROGMEM
// - multiline string needs '\' continuation character
// - \n is newline character
//
const char text[] PROGMEM = "\
Some formatted text here\n\
makes life easy for HTML\n\
servers, for example.\n\
";
#define PMTXT(textarray) (reinterpret_cast<const __FlashStringHelper*>(textarray))
Serial.print( PMTXT(text) );
Note that the Raw string macro embeds newlines and leading spaces.
void setup() {
Serial.begin(9600);
Serial.println(F("Regular F macro"));
Serial.println(F(R"(
Hello world! It
is nice to meet
you.
)"));
Serial.println(F(
R"(Some formatted text here
makes life easy for HTML
servers, for example.
Unlimited quantity of anonymous strings.
)"));
Serial.println(F("David Prentice"));
}
void loop() {
}
I suggest that you choose the style that is most convenient for you to maintain HTML text.
But combining the F() and R"() macros keeps your anonymous text safely in Flash.
Depends. The 'F' macro can't be used outside of a function (i.e. global land). So, it works fine a print statement. But, if you want to print that same Flash string from more than one place, you need a pointer to a __FlashStringHelper object. Here's two ways I found to do it:
const char text1[] PROGMEM = R"( // can't use 'F' macro here
Some formatted text here !
makes life easy for HTML
servers, for example.
)";
__FlashStringHelper *textPtr1 = (__FlashStringHelper *) text1;
__FlashStringHelper *textPtr2;
void setup() {
Serial.begin(9600);
textPtr2 = F(R"( // can use 'F' macro inside a function
Some formatted text here !
makes life easy for HTML
servers, for example.
)");
}
void loop() {
Serial.println(textPtr1);
Serial.println(textPtr2);
delay(5000);
}