Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using constants during MenuItem creation #2

Open
mrhydea4 opened this issue Sep 2, 2015 · 2 comments
Open

Using constants during MenuItem creation #2

mrhydea4 opened this issue Sep 2, 2015 · 2 comments

Comments

@mrhydea4
Copy link

mrhydea4 commented Sep 2, 2015

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.

@0xPIT
Copy link
Owner

0xPIT commented Oct 1, 2015

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>).

@7FM
Copy link

7FM commented May 4, 2020

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: 7FM@16cc793
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants