4

So I have a Bunch of commonly used char arrays to build commands in my library. So to preserve memory, I have them put into flash, via PROGMEM ext,

typedef const char PROGMEM ProgChar;
ProgChar AT[] = "AT";
ProgChar AT_RST[] = "RST";

Then in my program I have it all go out to A port via:

*hws << AT << PLUS << CWMODE << EQUAL; //hws can be any Print type,

This is handled by overloading the << operator;

static Print &operator <<(Print &obj, ProgChar* arg) { 
 char pbuffbuffer[12]; //create a buffer,
 obj.print(loadProgmemVal(arg,pbuffbuffer)); //Load it, and then pass to obj print method.
 return obj; 
}

But this also overloads the << operator for all const char* Which makes me have to do a workaround:

hws->print(1); //HACK

At first, I thought I would overload the operator to handle both a char* and a Prog Char Object, But unfortunately, they are both treated as the same thing at run-time which makes that idea useless.

So after snooping I find the __FlashStringHelper type and macro. Which would allow me to have a sepperate type for these variables, and allow me to overload.

But the problem is, I cant find a good way to Create these:

const __FlashStringHelper MYVAR = F("Some text to be progchared");

Does not compile =/

Now I could probably make some helper method to load them all in, but that is just patching a hack with another hack... Is there any Clean way to do this? So I can use << for all types,

*hws << AT << PLUS << CWMODE << EQUAL << 1 << "\r\n"; //1 and Return break...
asked Jan 3, 2017 at 17:31
2
  • Idea: Is the objects in Flash at a lower PTR value then Objects in Ram, and ifso, what is then cutoff? EG: 0-04f00 = Flash 04f01-fffff = Ram, ext. Commented Jan 3, 2017 at 17:49
  • Seems like the recommended style is to use the macro FPSTR(). arduino-esp8266.readthedocs.io/en/latest/PROGMEM.html static const char xyz[] PROGMEM = "This is a string stored in flash"; Serial.println(FPSTR(xyz)); Commented Sep 27, 2018 at 6:10

1 Answer 1

1

Is this legal?

typedef const char ProgChar;
ProgChar AT[] = "AT";

I would think that the PROGMEM can't be in the typedef, i.e.:

typedef const char ProgChar;
ProgChar AT[] PROGMEM = "AT";
answered Mar 29, 2018 at 19:11
1
  • If you meant "Is typedef const char PROGMEM ProgChar; ProgChar AT[] = "AT";", legal then apparently it is. typedef const char PROGMEM ProgChar; ProgChar AT[] = "AT"; void setup() { Serial.begin(115200); Serial.println((const __FlashStringHelper *)&AT[0]); } void loop() { ; } compiles, runs, and produces the expected output. Commented Sep 26, 2018 at 15:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.