menu icon indicating copy to clipboard operation
menu copied to clipboard

Using constants during MenuItem creation

Open mrhydea4 opened this issue 9 years ago • 2 comments

I'm writing an Arduino app and I want to be able to change the language of the menus. So I would have something like this:

EN.h const char PROGMEM WELCOME[] = "Welcome"

DE.h const char PROGMEM WELCOME[] = "Willkommen"

Main.ino #include "./EN.h" // or #include "./DE.h" MenuItem(miWelcome, WELCOME, Menu::NullItem, Menu::NullItem, Menu::NullItem, Menu::NullItem, ShowWelcome);

The problem is when I try to use the getLabel function, it just returns gibberish. I'm brand new to C++, so any help you could give would be appreciated.

Thank you.

mrhydea4 avatar Sep 02 '15 23:09 mrhydea4

The macro MenuItem instructs the compiler to put the whole structure with the label string into progmem, see here, so you can not pass a string already in progmem to that structure.

I'd suggest to create the complete menu hierarchy one time for each language, and then in your reset code point to either language version. Should then also be possible to switch at runtime using navigate(<menu Item of some lang version>).

0xPIT avatar Oct 01 '15 21:10 0xPIT

Well actually the label string does not get saved entirely in PROGMEM. Currently only the pointer to the string is stored. If someone wants an extended version of the makro to store the whole label string to save some ram have a look at my changes here: https://github.com/7FM/menu/commit/16cc793bdcf59dafdb3eb6003e3ac01656a27afc After calling getLabel() you get the address of the progmem string which is already noticed not directly printable, but almost. When having a subclass from the arduino Print class like Serial you can simply print the progmem string by calling forcing a call to the correct overloaded function by :

Serial.print(reinterpret_cast<const __FlashStringHelper *>(progmemStr));

where progmemStr would be the result of the getLabel() call

7FM avatar May 04 '20 21:05 7FM