Status
Not open for further replies.

#### TucsonDon

##### Member
I have a sub menu that has 16 items, how do I declare the names as a constant? I have tried the following in the header file but got a lot of errors.
C:
typedef char MENU_NAMES
{
{"NAME 1"},
{"NAME 2"},
{"NAME 3"}
};

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Normally you setup the arrays thus..

char MENU_NAMES[][] = {"Name 1", "Name 2","Name 3"};

Then usually you can enumerate them to all by name..
enum{
Name1,
Name2,
Name3
};

Then call them

#### TucsonDon

##### Member
Normally you setup the arrays thus..

char MENU_NAMES[][] = {"Name 1", "Name 2","Name 3"};

Then usually you can enumerate them to all by name..
enum{
Name1,
Name2,
Name3
};

Then call them

Thanks for the info.
I already have the names enumerated for a state machine
C:
tyepdef enum
{
NAME1_SM,
NAME2_SM,
NAME3_SM
}MENU_NAMES_SM;
Can I use this or do I have to enumerate them again?

Last edited:

#### Cicero

##### Active Member
Also remember to add const. Below is what you want to do, you can use your enum to reference it, although you don't need to typedef it unless you want that.
Code:
const char *menuNames[] = {
"name1\n",
"name2\n"
};

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Can I use this or do I have to enumerate them again?
Of course you can...
Also remember to add const. Below is what you want to do, you can use your enum to reference it, although you don't need to typedef it unless you want that.
Most compilers should be smart enough to see fixed text and store to prog mem...

#### Cicero

##### Active Member
Most compilers should be smart enough to see fixed text and store to prog mem...
Perhaps...but its not hard to add for completeness is it?

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
First negative rating ever!!!

Perhaps...but its not hard to add for completeness is it?
There is always a pro and con to doing things one way or another.. Sometimes, if you can afford the memory, its easier than having two print routines... I have to sprintf() to get constant strings to play with ram strings... If only a few text strings are needed, it may consume less ram than using conversion functions...

#### Cicero

##### Active Member
Haha, sorry Ian, I just simply disagree with your answer, only here of course.

I mean, have you tried to compile that?

#### Cicero

##### Active Member
Well I thought it wouldn't compile, but to be fair I just tried it and you only need to explicitly set the length:
Code:
char MENU_NAMES[][7] = {"Name 1", "Name 2","Name 3"};

#### Pommie

##### Well-Known Member
I prefer to use an array of pointers as then you can have variable length menu items.

Code:
const uint8_t menu1[]="Menu One";

void test(void){
unsigned char x;
const uint8_t* point;
point=table[3];  //3 is the menu number - note 0 to 3 - not 1 to 4
while(*point){   //while not end of string
// call put LCD etc here
}
}
In the above just change uint8_t to unsigned char. Or include stdint.

Mike.
Edit, probably not advised for beginners.

#### Cicero

##### Active Member
I prefer to use an array of pointers as then you can have variable length menu items.

Code:
const uint8_t menu1[]="Menu One";

void test(void){
unsigned char x;
const uint8_t* point;
point=table[3];  //3 is the menu number - note 0 to 3 - not 1 to 4
while(*point){   //while not end of string
// call put LCD etc here
}
}
In the above just change uint8_t to unsigned char. Or include stdint.

Mike.
Edit, probably not advised for beginners.
This is just a long winded version of my post @ 4.

#### Pommie

##### Well-Known Member
This is just a long winded version of my post @ 4.
Sorry, missed that.

Mike.

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
This is just a long winded version of my post @ 4.
Apart from the text being allowed to be any length... using pointer arrays allow for "hello!" and "Hello world!" to be used in the same context.. I used this method once years back .. I made a "Hangman" game and when I displayed the amount of characters in the form of asterisk's I used the strlen() function rather than having to have an array of string sizes..

#### Cicero

##### Active Member
Apart from the text being allowed to be any length... using pointer arrays allow for "hello!" and "Hello world!" to be used in the same context.. I used this method once years back .. I made a "Hangman" game and when I displayed the amount of characters in the form of asterisk's I used the strlen() function rather than having to have an array of string sizes..
Just to clarify, not sure if that sentence is insinuating/suggesting that it can't, but post 4 method is an array of pointers to constant chars, and can take variable length strings.

On a side note, the OP may want to think about using structures, as it really helps with scaling. For example a 4x16 display.

Code:
typedef struct {
unsigned char index;
char *line1;
char *line2;
char *line3;
char *line4;

enum {
};

{MENU_DISP_GREETING, "My project here", "Hello World!", "Whatever else", "And more text"},
};

void lcd_output (unsigned char menuPtr) {
lcd_gotoRow2();
lcd_gotoRow3();
lcd_gotoRow4();
}

lcd_output (MENU_DISP_GREETING);

Forum Supporter