convert 'char*' to 'const __FlashStringHelper*'

Hello

Trying to save some RAM in a project. The attached code is for demonstrating my problem.

The code can pass "F() strings" to the function but occasionally I also want to be able to pass a regular char array (C string) or convert it appropriately before passing it.

Any kind soul how can help me out?

#include <SoftwareSerial.h>
SoftwareSerial debugSerial(NULL, A1); // RX, TX.

void setup() {

	debugSerial.begin(9600);
	
	printToSerial(F("LALA")); // This works
	
	/*
	
	--- I want to be able to do this also i. e. pass a char array to the function ---
	
	char test[6];

	memset(test, '\0', sizeof(test));
	strncpy(test, "12345", sizeof(test)-1);
	
	printToSerial(test);
	
	*/
}

void loop() {


}

void printToSerial(const __FlashStringHelper* output) {

	char buffer[128];
	memset(buffer, '\0', sizeof(buffer));
	strlcpy_P(buffer, (const char PROGMEM *)output, sizeof(buffer));

	debugSerial.println(buffer);
}

Regards

Just provide an overloaded version of your function:

#include <SoftwareSerial.h>
SoftwareSerial debugSerial(NULL, A1); // RX, TX.

void setup() {

  debugSerial.begin(9600);

  printToSerial(F("LALA")); // This works
  printToSerial("LALA"); // So does this
}

void loop() {
}

void printToSerial(const __FlashStringHelper* output) {

  char buffer[128];
  memset(buffer, '\0', sizeof(buffer));
  strlcpy_P(buffer, (const char PROGMEM *)output, sizeof(buffer));

  debugSerial.println(buffer);
}

void printToSerial(const char * output) {
  debugSerial.println(output);
}

gfvalvo:
Just provide an overloaded version of your function:

Thats fast dang it, thank you!

The problem is that the actual function is quite big and I don't want to duplicate the code.

you load the string in to SRAM and the you call the full function, which will take const char*

void printToSerial(const __FlashStringHelper* output) {

 char buffer[128];
 strlcpy_P(buffer, (const char*) output, strlen_P(output));
 
 printToSerial(buffer);
}

void printToSerial(const char* output) {
 debugSerial.println(output);
}

Juraj:
you load the string in to SRAM and the you call the full function, which will take const char*

Aww of course. Thank you!

nollieflip:
Aww of course. Thank you!

note the details in my code

Juraj:
note the details in my code

Is this a typo?
"strlcpy_P(buffer, (const char*) output, strlen_P(output));"

Shouldn't that line be like in my example to avoid overflowing the buffer if output is to large?
"strlcpy_P(buffer, (const char*) output, sizeof(buffer));"

nollieflip:
Is this a typo?
"strlcpy_P(buffer, (const char*) output, strlen_P(output));"

Shouldn't that line be like in my example to avoid overflowing the buffer if output is to large?
"strlcpy_P(buffer, (const char*) output, sizeof(buffer));"

I use only strcpy_P. one knows the max size of progmem strings. but yes, buffer size is right there