Lightweight menu control framework module

1. Foreword

        As an embedded software development, the command line or display screen may often be used to realize the function of human-computer interaction. The function usually includes UI menu design; Many developers will have their own menu framework module to prevent repeated wheel building. There are many codes of this menu framework on the Internet, but most of them are too coupled to adapt to different menu designs independently.

        This paper introduces a completely independent menu framework with reduced coupling. The menu display style and display platform are completely designed according to their own needs, and the menu operation can be handled by the menu module, so as to improve the portability of the program.

2. Introduction

The main features of the menu frame code are:

  • Use linked list or array to realize multi-level menu (select through configuration)
  • As an independent module, the menu frame is coupled with the key module and the display module
  • When it is very independent, it is also guaranteed not to be affected by the menu display style and display platform, and can freely choose and design the display style and display platform
  • The menu can be initialized in a table driven manner to improve the readability of the code

3. Code function

The code of the source file is as follows:

/**
  * @brief      Menu initialization
  * 
  * @param[in]  pMainMenu    Main menu registration information
  * @param[in]  num          Number of main menus
  * @param[in]  fpnShowMenu  Main menu display effect function
  * @return     0,Success- 1. Failed 
  */
int Menu_Init(MenuRegister_t *pMainMenu, uint8_t num, ShowMenuCallFun_f fpnShowMenu)
{
    MenuCtrl_t *pMenuCtrl = NULL;

#if MENU_MAX_DEPTH != 0
    sg_currMenuDepth = 0;
#endif

    if ((pMenuCtrl = NewMenu()) != NULL)
    {
        pMenuCtrl->pLastMenuCtrl = NULL;
        pMenuCtrl->pfnShowMenuFun = fpnShowMenu;
        pMenuCtrl->pMenuInfo = pMainMenu;
        pMenuCtrl->menuNum = num;
        pMenuCtrl->currPos = 0;
        pMenuCtrl->isRunCallback = 0;

        sg_tMenuManage.pCurrMenuCtrl = pMenuCtrl;

        return 0;
    }

    return -1;
}

The code of header file is as follows:

/**
  * @brief Menu information registration structure
  * 
  */
typedef struct MenuRegister
{
    const char     *pszDesc;            /*!< String description of the current option */

    menusize_t      subMenuNum;         /*!< The number of submenus of the current option. If the number of submenus is 0, it means that the next level of non menu interface will execute non menu function functions */

    struct MenuRegister *pSubMenu;      /*!< Submenu contents of the current option */

    ShowMenuCallFun_f pfnShowMenuFun;   /*!< The submenu display effect function of the current option. If it is NULL, the display effect of the superior menu will be continued */

    MenuCallFun_f     pfnMenuCallFun;   /*!< The non menu function of the current option is valid only when the number of menus is 0 */
}MenuRegister_t;

/* Exported constants ------------------------------------------------------------------------------------------------*/
/* Exported macro ----------------------------------------------------------------------------------------------------*/

#define GET_MENU_NUM(X)    (sizeof(X) / sizeof(MenuRegister_t))

/* Exported functions ------------------------------------------------------------------------------------------------*/

/* initialization */
extern int Menu_Init(MenuRegister_t *pMainMenu, uint8_t num, ShowMenuCallFun_f fpnShowMenu);

/* Return to main menu */
extern int Menu_ResetMainMenu(void);

/* Menu entry and exit */
extern int Menu_Enter(void);
extern int Menu_Exit(uint8_t isReset);

/* Menu item up and down selection */
extern int Menu_SelectPrevious(uint8_t isAllowRoll);
extern int Menu_SelectNext(uint8_t isAllowRoll);

/* Menu polling task */
extern int Menu_Task(void);

4. Sample code display effect

The platform of the sample code is the command line output and input display effect (you can freely choose the display platform and display style)

The following is the menu interface display effect of OLED display driven by single chip microcomputer

5. Sample code get link

Download link Click: Lightweight menu framework

Tags: C

Posted on Fri, 05 Nov 2021 15:34:10 -0400 by Jonob