标准文本液晶菜单系统

电器工程 液晶显示器 C
2022-02-02 10:06:48

是否有用于文本 LCD 的 C 语言简单菜单系统的模式。我发现自己经常重写代码来处理简单的文本 LCD 菜单。

I find most systems have a main menu and some sub-menus that when selected allow you to set a parameter with within some minimum and maximum value.

理想情况下,该菜单系统可以使用 4 个简单的键进行导航,例如输入、取消、向上和向下。

在我的应用程序中,我使用的是 2 行 x 16 字符文本 LCD,尽管理想的解决方案应该能够应用于任何 NxM 显示器。

2个回答

我在 C 中用于菜单系统的模式是这样的:

struct menuitem
{
  const char *name; // name to be rendered
  functionPointer handlerFunc; // handler for this leaf node (optionally NULL)
  struct menu *child; // pointer to child submenu (optionally NULL)
};

struct menu
{
  struct menu *parent; // pointer to parent menu
  struct **menuitem; // array of menu items, NULL terminated
};

然后我声明一个menus 数组,每个数组都包含menuitems 和指向child子菜单的指针。在当前选定的 s 数组中上下移动menuitem后退移动到parent菜单并前进/选择移动到child子菜单或调用handlerFunc叶节点。

渲染菜单只涉及遍历其项目。

这种方案的优点是它完全是数据驱动的,菜单结构可以在 ROM 中静态声明,独立于渲染器和处理程序函数。

托比的回答是一个很好的起点。提到的结构假定菜单是静态的,您只需在它们之间导航。

如果您想要动态菜单(例如显示某些值,例如温度、时间等),那么您需要能够生成它。

一种方法是注册一个函数来构建你的字符串。

struct menuitem
{
  const char *name; // name to be rendered
  const char * (*builderFunc)( const char *name );  // callback to generate string, if not null.
  functionPointer handlerFunc; // handler for this leaf node (optionally NULL)
  struct menu *child; // pointer to child submenu (optionally NULL)
};