Taixin Semiconductor

News detail

Technical Deep Dive | Designing a Product-Level MCU Menu Framework

2021-05-21

Disclaimer: The menu discussed here is a menu used on small 128*64 screens, like the one below, not a GUI on a color screen.
As a low-level driver engineer, after finishing the driver, you need to write a hardware test program. This test program is generally used by the test department/hardware engineers to test hardware, and is also used by factory production lines to test semi-finished products.
The initial developer was lazy and did not want to do it directly in one second; all menus were built this way, one layer nested inside another.
void test_main(void)
{
        while(1)
        {
                get_key(&key);
                switch(key)
                {
                        case 1:
                                test_key();
                                break;
                        case 2:
                                test_lcd();
                                break;
                        ....
                }
        }
}

As menus grow in number, I start to struggle. Written this way, it is inconvenient to maintain, not visually appealing, and wastes program space.
As a programmer who reads The Beauty of Programming every day, I decided to change the status quo. After searching on Kugou and Baidu, I found two references: 'Design of a Multi-level LCD Menu Interface Based on a Binary Tree' and 'Design Method and Implementation of a General Tree Menu Based on Node Numbering.pdf'. Following their design methods, I put together a version. It works and is quite good, but I still struggled. They used a tree data structure. For program execution, this is very good and highly efficient. But for me, the menu code is one-off, while the menu content changes frequently. Making me maintain a tree containing dozens or hundreds of menus with my own brain is not easy.
After thinking it over, what exactly is wrong with these menus? For me, why are they not easy to use? I reached the following conclusions:
  1. Taking on too much: A menu only needs to handle menu switching. Once it reaches the lowest level-the actual test functions-it should not manage them. Menu switching is similar; actual tests are all different. For example, in a menu, pressing key 1 enters the first menu. But in tests, pressing key 1 has different functions. If the menu also manages this, there are too many functions for the same action, making unified abstraction impossible and modularization difficult.
  2. Different starting point: The menus mentioned above start from how to design a good menu data structure so the program runs quickly and efficiently. What I want is a menu structure that is easy to maintain. It does not matter how messy or complicated the menu code is. Moreover, even with hundreds or thousands of menus, polling would take only a few hundred microseconds; that is fine.
Based on the requirements, I redesigned a menu structure
/**
 * @brief  Menu object
*/

typedef struct _strMenu
{

    MenuLel l;     ///<Menu level
    char cha[MENU_LANG_BUF_SIZE];   ///Chinese
    char eng[MENU_LANG_BUF_SIZE];   ///English
    MenuType type;  ///Menu type
    s32 (*fun)(void);  ///Test function

} MENU;
Yes, it is that simple. Every menu uses this structure. Fill a list with this structure, and that is our menu.
const MENU EMenuListTest[]=
{
        MENU_L_0,//Menu level
        "Test program",//Chinese
        "test",        //English
        MENU_TYPE_LIST,//Menu type
        NULL,//Menu function; only functional menu items execute, those with submenus do not execute

                MENU_L_1,//Menu level
                "LCD",//Chinese
                "LCD",        // English
                MENU_TYPE_LIST,// Menu type
                NULL,// Menu function; only function menus execute; menus with submenus do not execute
                        MENU_L_2,// Menu level
                        "VSPI OLED",// Chinese
                        "VSPI OLED",        // English
                        MENU_TYPE_FUN,// Menu type
                        test_oled,// Menu function; only function menus execute, those with submenus do not

                        MENU_L_2,// Menu level
                        "I2C OLED",// Chinese
                        "I2C OLED",        // English
                        MENU_TYPE_FUN,// Menu type
                        test_i2coled,// Menu function; only function menus execute, those with submenus do not


              MENU_L_1,// Menu level
                "Sound",// Chinese
                "sound",        // English
                MENU_TYPE_LIST,// Menu type
                NULL,// Menu function; only function menus execute; those with submenus do not
                        MENU_L_2,// Menu level
                        "Buzzer",// Chinese
                        "buzzer",        //English
                        MENU_TYPE_FUN,//Menu type
                        test_test,//Menu function; only function menus will execute; those with submenus will not execute

                        MENU_L_2,//Menu level
                        "DAC music",//Chinese
                        "DAC music",        //English
                        MENU_TYPE_FUN,//Menu type
                        test_test,// Menu function; only function menus execute; menus with submenus do not execute

                        MENU_L_2,// Menu level
                        "Radio",// Chinese
                        "FM",        // English
                        MENU_TYPE_FUN,// Menu type
                        test_test,// Menu function; only function menus execute; menus with submenus do not execute


                MENU_L_1,// Menu level
                "Touchscreen",// Chinese
                "tp",        // English
                MENU_TYPE_LIST,// Menu type
                NULL,// Menu function; only function menus are executed; those with submenus are not executed

                        MENU_L_2,// Menu level
                        "Calibration",// Chinese
                        "calibrate"        // English
                        MENU_TYPE_FUN,// Menu type
                        test_cal,// Menu function; only function menus execute; those with submenus do not.

                        MENU_L_2,// Menu level
                        "Test",// Chinese
                        "test",        // English
                        MENU_TYPE_FUN,// Menu type
                        test_tp,// Menu function; only function menus execute; those with submenus do not.

                MENU_L_1,//Menu level
                "Key",//Chinese
                "KEY",        //English
                MENU_TYPE_FUN,//Menu type
                test_key,//Menu function; only function menus execute, menus with submenus do not execute

        /*The last menu is the end menu and is meaningless*/                        
        MENU_L_0,//Menu level
        "END",// Chinese
        "END",        // English
        MENU_TYPE_NULL,// Menu type
        NULL,// Menu function: only functional menus execute; those with submenus do not execute
};
What are the features and requirements of this menu list? 1. It needs a root node and an end node. 2. Child nodes must follow their parent node, similar to the structure below.
-----------------------------------------------
Root node
        No.1piece1st-level menu
                       No.1submenu
                       No.2submenu
                       No.3submenu
        No.2:1st-level menu
                       No.1submenu
                                   No.1grandchild menu
                                     No.2grandchild menu
                       No.2submenu
                       No.3submenu
        No.3item1st-level menu
        No.4item1level menu
        the5th1-level menu
end node
------------------------------------------------
The second level-1 menu has 3 submenus; the submenus are level-2 menus, and the first submenu has 2 grandchild menus (level-3 menus) under it.
Maintaining the menu means maintaining this list—adding, deleting, and modifying is very easy. So what about the menu program? Who cares. After the menu is defined, run the menu through the following function,
 emenu_run(WJQTestLcd, (MENU *)&WJQTestList[0], sizeof(WJQTestList)/sizeof(MENU), FONT_SONGTI_1616, 2);        
-The first parameter is which LCD displays the menu; -the second is the menu list; -the third is the menu length; -the fourth is the font; -the fifth is the line spacing.
Note: Running this menu requires an RTOS, because the menu code is while(1) and will get stuck and never exit. Other threads (TASKs) are needed to maintain the system, such as key scanning.
The code is hosted on GitHub: https://github.com/wujique/stm32f407/tree/sw_arch
Related files: emenu.c, emenu.h, emenu_test.c
Current code: 
1. Implements a two-column menu, using numeric keys to select and enter the next level. Each page displays up to 8 menus (keys 1–8 on a 4*4 keyboard).
2. Implements a single-column menu, where you scroll up and down to view menus and press the confirm key to enter a menu. 3. The zenith menu is not implemented; anyone interested can add it.
3. Based on the LCD driver architecture, this simple menu adapts to a variety of LCDs.
The results are shown below. Feel free to use it if needed; no thanks required.

Display results

128*64 OLED

128*128 tft lcd

320*240 tft lcd

Summary

A similar menu has already been promoted and used in products I developed. Tests show that it can significantly reduce the amount of test program code and save program space. It is also easy to modify and maintain.

Note: This article is reposted from         , reposted thisarticle for the purpose of conveying more information; copyright belongs to the original author.

Zhuhai Taixin Semiconductor Co., Ltd.

Zhuhai Taixin Semiconductor Co., Ltd. was founded in 2016 and is a high-tech enterprise integrating IC design and wireless intelligent communication solutions. We have successively launched long-range, low-power IoT WIFI chips, low-power motor controller chips, and product solutions. The company adheres to a market-oriented and technology-centered approach to provide customers with high-quality chips and product technical solutions.

The company's core product: WIFI communication chip for long-range low-power IoT. This chip can be used in smart homes, security monitoring, drone image transmission, smart meter reading, smart door locks, smart grids, smart hospitals, logistics robots, and other product fields.