1. 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.
    Dismiss Notice

Menu Names

Discussion in 'Microcontrollers' started by TucsonDon, May 12, 2018.

  1. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    53
    Likes:
    0
    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.
    Code (c):

    typedef char MENU_NAMES
    {
    {"NAME 1"},
    {"NAME 2"},
    {"NAME 3"}
    };
     
     
  2. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,741
    Likes:
    954
    Location:
    Rochdale UK
    ONLINE
    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]);
     
    • Like Like x 1
    • Agree Agree x 1
    • Disagree Disagree x 1
  3. TucsonDon

    TucsonDon Member

    Joined:
    Sep 4, 2016
    Messages:
    53
    Likes:
    0
    Thanks for the info.
    I already have the names enumerated for a state machine
    Code (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: May 13, 2018
  4. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  5. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    393
    Likes:
    38
    Location:
    UK
    ONLINE
    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 (text):

    const char *menuNames[] = {
            "name1\n",
            "name2\n"
        };
     
     
  6. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,741
    Likes:
    954
    Location:
    Rochdale UK
    ONLINE
    Of course you can...
    Most compilers should be smart enough to see fixed text and store to prog mem...
     
  7. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    393
    Likes:
    38
    Location:
    UK
    ONLINE
    Perhaps...but its not hard to add for completeness is it?
     
  8. dave miyares

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    2
    Likes:
    -10


     
  9. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,741
    Likes:
    954
    Location:
    Rochdale UK
    ONLINE
    First negative rating ever!!!

    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...
     
  10. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    393
    Likes:
    38
    Location:
    UK
    ONLINE
    Haha, sorry Ian, I just simply disagree with your answer, only here of course.

    I mean, have you tried to compile that?
     
  11. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,741
    Likes:
    954
    Location:
    Rochdale UK
    ONLINE
    Nope... It was just for reference.. Which bit doesn't compile?
     
  12. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    393
    Likes:
    38
    Location:
    UK
    ONLINE
    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 (text):

    char MENU_NAMES[][7] = {"Name 1", "Name 2","Name 3"};
     
     
  13. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,655
    Likes:
    409
    Location:
    Brisbane Australia
    ONLINE
    I prefer to use an array of pointers as then you can have variable length menu items.

    Code (text):

    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.
     
    • Like Like x 2
  14. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,741
    Likes:
    954
    Location:
    Rochdale UK
    ONLINE
    That's a better idea Mike... It seems far more adaptable.... Array of pointers are normally an advanced topic, but a good way to start!!
     
    • Like Like x 1
  15. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    393
    Likes:
    38
    Location:
    UK
    ONLINE
    This is just a long winded version of my post @ 4.
     
  16. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,655
    Likes:
    409
    Location:
    Brisbane Australia
    ONLINE
    Sorry, missed that.

    Mike.
     
  17. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,741
    Likes:
    954
    Location:
    Rochdale UK
    ONLINE
    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..
     
  18. Cicero

    Cicero Active Member

    Joined:
    Nov 21, 2014
    Messages:
    393
    Likes:
    38
    Location:
    UK
    ONLINE
    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 (text):

    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);
     
     
  19. Ian Rogers

    Ian Rogers User Extraordinaire Forum Supporter Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,741
    Likes:
    954
    Location:
    Rochdale UK
    ONLINE
    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!!
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice