我试图通过在数组中插入元素来在链接到 arduino 的显示器中创建菜单,如下面的伪代码 (javascript) 所示。
var menu = {
title: 'main menu',
item: [{
txt: 'item1',
action: function() { // function that will be performed in case such an element is activated
// my code
}
},
{
txt: 'item2',
item: [{
txt: 'item4',
action: function() {
// my code
}
},
{
txt: 'item5',
action: function() {
// my code
}
}
],
action: function() {
// my code
}
},
{
txt: 'item3',
action: function() {
// my code
}
}
]
};
稍后该数组将由递归函数读取,该函数将在液晶显示器上打印菜单。
我该如何对arduino执行此操作? 使用 javascript 似乎是任何人都可以完成的操作,但是在 C/C++ 中你能做同样的事情吗?
提前感谢大家的帮助!
最佳答案
使用字符串、指向函数的指针以及指向下一个和上一个结构体的指针创建一个结构体
字符串是选项将显示的文本,函数是用户单击该项目时调用的函数,如果用户向上或向下移动,指针会为您提供上一个和下一个项目
示例:
在头文件中:
const struct item
{
const char name[16];
void (*func)(void); // Pointer to the item function
const struct item * prev; // Pointer to the previous
const struct item * next; // Pointer to the next
};
在c文件中:
const struct item item_ON =
{
" 1 On",
fcn_item_turn_on,
&item_OFF,
&item_PARTIAL
};
const struct item item_PARTIAL =
{
" 2 Partial",
fcn_item_partial,
&item_ON,
&item_OFF
};
const struct item item_OFF =
{
" 3 Off",
fcn_item_turn_off,
&item_PARTIAL,
&item_ON
};
然后:
void menu_show()
{
putrsXLCD((rom char *)(*ptr_item).name); // or the LCD print function in your code
}
void menu_inc() {
ptr_item = (*ptr_item).prev;
menu_show();
}
void menu_dec() {
ptr_item = (*ptr_item).next;
menu_show();
}
void menu_fwd() {
(*ptr_item).func(); // execute item function
}
不要忘记用第一项初始化 ptr_item:
ptr_item = &item_ON;
关于c++ - Arduino液晶显示屏: Generate a navigation menu by reading an array,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43963019/