Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
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]);
tyepdef enum
{
NAME1_SM,
NAME2_SM,
NAME3_SM
}MENU_NAMES_SM;
Of course you can...Can I use this or do I have to enumerate them again?
Most compilers should be smart enough to see fixed text and store to prog mem...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.
Perhaps...but its not hard to add for completeness is it?Most compilers should be smart enough to see fixed text and store to prog mem...
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...Perhaps...but its not hard to add for completeness is it?
Nope... It was just for reference.. Which bit doesn't compile?I mean, have you tried to compile that?
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
}
}
This is just a long winded version of my post @ 4.I prefer to use an array of pointers as then you can have variable length menu items.
In the above just change uint8_t to unsigned char. Or include stdint.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 } }
Mike.
Edit, probably not advised for beginners.
Sorry, missed that.This is just a long winded version of my post @ 4.
This is just a long winded version of my post @ 4.
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.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..
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);
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...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.