Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Menu Names

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"}
};
 
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

print(MENU_NAMES[Name1]);
 
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

print(MENU_NAMES[Name1]);

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:
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"
    };
 
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...
 
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...
 
Haha, sorry Ian, I just simply disagree with your answer, only here of course.

I mean, have you tried to compile that?
 
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"};
 
I prefer to use an array of pointers as then you can have variable length menu items.

Code:
const uint8_t menu1[]="Menu One";
const uint8_t menu2[]="Menu Two";
const uint8_t menu3[]="Menu Three";
const uint8_t menu4[]="Menu Four";

const uint8 *table[]=&menu1,&menu2,&menu3,&menu4;

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
        x=*point++; //will load all the letters of "Menu Four"
       // 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.
 
I prefer to use an array of pointers as then you can have variable length menu items.

Code:
const uint8_t menu1[]="Menu One";
const uint8_t menu2[]="Menu Two";
const uint8_t menu3[]="Menu Three";
const uint8_t menu4[]="Menu Four";

const uint8 *table[]=&menu1,&menu2,&menu3,&menu4;

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
        x=*point++; //will load all the letters of "Menu Four"
       // 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.
 
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..
 
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;
} tmenuStr;

enum {
    MENU_DISP_GREETING,
    MENU_DISP_MENU1,
    MENU_DISP_MENU2
};

const tmenuStr menuStruct[] = {
    {MENU_DISP_GREETING, "My project here", "Hello World!", "Whatever else", "And more text"},
    {MENU_DISP_MENU1, "Menu1 - line1", "Menu1 - line2", "Menu1 - line3", "Menu1 - line4"},
    {MENU_DISP_MENU2, "Menu2 - line1", "Menu2 - line2", "Menu2 - line3", "Menu2 line four"}
};

void lcd_output (unsigned char menuPtr) {
    lcd_print(menuStruct[menuPtr].line1);
    lcd_gotoRow2();
    lcd_print(menuStruct[menuPtr].line2);
    lcd_gotoRow3();
    lcd_print(menuStruct[menuPtr].line3);
    lcd_gotoRow4();
    lcd_print(menuStruct[menuPtr].line4);
}

lcd_output (MENU_DISP_GREETING);
 
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.
No! Your code is as good as Mikes' I try to stay away from pointers when helping newbies, It's more of a distraction.. I just feel Mikes' code is easier for a newbie to follow.. I think I have tried to go straight into pointers, but without prior knowledge of pointers, the general newbie programmer may start to use uninitialized pointers thinking that its okay to do so...

When I answer on a thread, I try to put myself in their shoes.. I realise that TucsonDon isn't that much of a newbie, but others are following!!
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top