My understanding is that the string literals occupy RAM space (scarce) so I would like to move them into flash space using F(). I tried this but got a compilation error: "error: statement-expressions are not allowed outside functions nor in template-argument lists"
I think this way if you use these string literals elsewhere in your code, you are not duplicating them in the PROGMEM (FLASH). F() doesn't optimize duplicate strings but you can.
zapta:
My understanding is that the string literals occupy RAM space (scarce) so I would like to move them into flash space using F(). I tried this but got a compilation error: "error: statement-expressions are not allowed outside functions nor in template-argument lists"
Thanks everybody for the info. I ended putting the entire array of structs in flash and reading pieces of it to ram as needed. Here is the actual snippet from my program. For efficiency I read the two fields independently I I also tried memcpy one struct at a time from flash to ram and it worked.
struct BitName {
uint8 mask; // 1 byte
char *name; // 1 word
};
static const BitName kErrorBitNames[] PROGMEM = {
{ ERROR_FRAME_TOO_SHORT, "SHRT" },
{ ERROR_FRAME_TOO_LONG, "LONG" },
{ ERROR_START_BIT, "STRT" },
{ ERROR_STOP_BIT, "STOP" },
{ ERROR_SYNC_BYTE, "SYNC" },
{ ERROR_BUFFER_OVERRUN, "OVRN" },
{ ERROR_OTHER, "OTHR" },
};
static void printErrors(uint8 errors) {
const uint8 n = sizeof(kErrorBitNames) / sizeof(kErrorBitNames[0]);
for (uint8 i = 0; i < n; i++) {
const uint8 mask = pgm_read_byte(&kErrorBitNames[i].mask); // <<-- reading first field
if (errors & mask) {
const char* const name = (const char*)pgm_read_word(&kErrorBitNames[i].name); // <<-- second field
sio::print(name);
}
}
}
liudr:
I think this way if you use these string literals elsewhere in your code, you are not duplicating them in the PROGMEM (FLASH). F() doesn't optimize duplicate strings but you can.
Yes, PROGMEM did the trick for me. In the example you posted, you could have a PROGMEM array of the strings instead of individual strings. This way you can lookup the command using index instead of a switch statement.