c++ - Arduino液晶显示屏: Generate a navigation menu by reading an array

标签 c++ c arduino lcd

我试图通过在数组中插入元素来在链接到 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/

相关文章:

c - 如何在文件(数据库)中存储数组以及如何在程序中访问它

c++ - 我应该为初学者学习 "c or c++ "之间的哪一个来使用 Arduino UNO?

c - 如果我使用共享库,如何在 Arduino 中构建和上传代码?

c++ - Cmake OpenCV 无法指定链接库

c++ - 显示 QMessageBox 时如何防止双槽调用?

c++ - Windows 线程 sleep 并从另一个线程唤醒

c++ - 在堆栈上创建类对象

c - 在为工作和隔离位和字节制作二进制掩码时,从二进制转换为十六进制的最快方法是什么?

c - 多线程,每个线程都有唯一的变量

c - 内存和时间高效的 PI 算法(二进制)