【发布时间】:2015-04-13 06:56:31
【问题描述】:
我正在尝试在 Arduino 中创建多个菜单。每个菜单都有很多行。每行可能有需要显示的变量。我不知道如何:
a) 正确定义我的菜单结构 b) 将起始数据加载到其中 c) 使用它
我认为下面的代码几乎在那里 - 请参阅我的 cmets(查找“// HELP”):
// Example for doing Menus for JB
#define MAX_MENU_LINES 3 // How many lines are on each of your menu screens
typedef struct menu_item_def {
byte x; byte y; // Coordinates of the start for the line of test
byte selected; // set to 1 if the menu buttons have this option selected
char *mtext; // What to say, including sprintf placeholders: eg: "STOP TIMER...% 4.2i MIN"
byte mdatatype1; // 0 means this is actual data to print. 1 means go call the supplied function to get the data when needed.
void *mdata1; // Where to get any data from for the menu (upto 2 different bits allowed per line)
byte mdatatype2;
void *mdata2;
} menu_item_type;
// I do not want to do this:-
struct MenuT {
menu_item_type mline;
} MainMenu[MAX_MENU_LINES] = {
{1,2,0, "STOP TIMER....% 5.1i MIN" ,0,(void *)85,0,0},
{1,2,1, "CHEM RATE....% 5.2f Lt/Hr" ,1,(void *)DemoData,0,0},
{1,2,0, "CHEM PUMP.... %s" ,0,(void *)"ON/OFF",0,0},
};
// HELP
/* I would prefer to do something like this:-
typedef struct menu_def {
menu_item_type mline[MAX_MENU_LINES];
} menu_type;
menu_type MainMenu = {
{1,2,0, "STOP TIMER....% 5.1i MIN" ,0,(void *)85,0,0},
{1,2,1, "CHEM RATE....% 5.2f Lt/Hr" ,0,(void *)85,0,0},
{1,2,0, "CHEM PUMP.... %s" ,0,(void *)"ON/OFF",0,0},
};
menu_type SubMenu = {
{1,2,0, "MOTOR RPM.....% 5.1i RPM" ,0,(void *)8500,0,0},
{1,2,1, "ALARM MAX....% 5.2f Lt/Hr" ,0,(void *)85,0,0},
{1,2,0, "ALARM MIN.... %s" ,0,(void *)"ON/OFF",0,0},
};
*/
#define MAX_WIDTH 256 // This is the max width of anything you need to print - increase this if your LCD is wider
char buf[MAX_WIDTH];
char *p(char *fmt, ... ){ // Helper routine for putting numbers/readings/etc into printable strings
va_list args; va_start (args, fmt );
vsnprintf(buf, MAX_WIDTH, fmt, args);
va_end (args);
return buf;
}
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // I have no LCD, so, my output is to serial (Hit Ctrl+Shit+M or go Tools->Serial Monitor to see output). Note: Seral-Monitor will reset board.
Menu(MainMenu); // HELP - I don't know how to pass whatever got defined above
}
void loop() {
// put your main code here, to run repeatedly:
// Does nothing - the setup outputted the menu already
}
int Menu( /* // HELP - I don't know what to put here to recevied a passed-in menu */ ) {
for(int i=0;i<MAX_MENU_LINES;i++) {
// Serial.println(mymenu[i].mtext); // Help - I don't know how to reference the bit I need!
};
Serial.print(p("STOP TIMER...% 4.2i MIN",5));
}
int DemoData() {
return 85;
}
很抱歉听起来像一个 n00b - 自从我 30 年前了解到这一点后,我的大脑中的细节被垃圾收集了:-(
【问题讨论】:
-
85 不是指针,是数字!!! DemoData 是指向函数 DemoData 的指针!!!我看到很多问题!!!
-
谢谢 Sergio - 是的,忽略我明显的错误(比如缺少“联合”)。这是我正在尝试解决的结构的语法。
-
(y) ;) 我喜欢你使用我的提示!
标签: c++ c data-structures struct arduino