ARDUINO LCD:通过读取数组生成导航菜单
ARDUINO LCD: Generate a navigation menu by reading an array
我正在尝试通过将元素插入数组中来创建链接到 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
}
}
]
};
稍后将通过递归函数读取此数组,该函数将在液体 crystal 显示屏上打印菜单。
我怎样才能对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;
从表面上看,您正在尝试创建分层菜单系统。 (因为 JSON 对象不是数组,而是更类似于树。)
由于 STL,C++ 可能更容易实现,我不确定您的经验,但我会给出一个总体布局。无论如何都是设计明智的。
#include <functional>
#include <vector>
class MenuTreeNode {
std::string title;
std::vector<MenuTreeNode> children;
std::function<void(int)> action;
public:
MenuTreeNode(const std::string& title, std::function<void(int)> action = {});
// ^ Construct the node, with the action item being optional.
// {} is just an empty function block.
/*
** You can construct with a lambda function, which are pretty useful.
*/
void addChild(MenuTreeNode&& childNode); // append a node to the child array.
void displayStuff() {
// However you display stuff to Arduino...
for (auto& child : this->children) {
child.displayStuff();
}
this->action(); // Call the action.
}
};
希望能给您一些指导。 C 中较旧的答案很好,但不允许 JSON 结构中的子项。这可能更容易与 IMO 一起工作。
我正在尝试通过将元素插入数组中来创建链接到 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
}
}
]
};
稍后将通过递归函数读取此数组,该函数将在液体 crystal 显示屏上打印菜单。
我怎样才能对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;
从表面上看,您正在尝试创建分层菜单系统。 (因为 JSON 对象不是数组,而是更类似于树。)
由于 STL,C++ 可能更容易实现,我不确定您的经验,但我会给出一个总体布局。无论如何都是设计明智的。
#include <functional>
#include <vector>
class MenuTreeNode {
std::string title;
std::vector<MenuTreeNode> children;
std::function<void(int)> action;
public:
MenuTreeNode(const std::string& title, std::function<void(int)> action = {});
// ^ Construct the node, with the action item being optional.
// {} is just an empty function block.
/*
** You can construct with a lambda function, which are pretty useful.
*/
void addChild(MenuTreeNode&& childNode); // append a node to the child array.
void displayStuff() {
// However you display stuff to Arduino...
for (auto& child : this->children) {
child.displayStuff();
}
this->action(); // Call the action.
}
};
希望能给您一些指导。 C 中较旧的答案很好,但不允许 JSON 结构中的子项。这可能更容易与 IMO 一起工作。