C language design learning, teach you how to objectize the LCD menu

This is a small series in the series of articles "C Language Embedded System Programming Practice" written by me when I was a graduate student 13 years ago. It is an example of object-oriented, object-oriented LCD menu. The original source of the article:

Http://soft.yesky.com/lesson/188/2023188.shtml

  The menu operation has been the result of numerous brainstorming problems. In this section, we will see that even in the C language, even with a little bit of object-oriented thinking, the software structure will change! I used to be an idiot, and I was fainted by the menu, giving a system like this:

It is required to switch the menu focus with the "← →" key on the keyboard . When the user is in focus on a certain menu, if the OK and CANCEL keys on the keyboard are hit, the processing function corresponding to the focus menu is called. I used to do this stupidly:

/* Press the OK button */void onOkKey(){ /* to determine which focus menu to press the Ok button, call the corresponding handler function */ Switch(currentFocus) { case MENU1: menu1OnOk(); break; case MENU2: menu2OnOk (); break; ... }}/* Press the Cancel button */void onCancelKey(){ /* to determine which focus menu to press the Cancel button, call the corresponding handler function */ Switch(currentFocus) { case MENU1: menu1OnCancel( ); break; case MENU2: menu2OnCancel(); break; ... }}

Finally one day, I did this:

/* "package" the menu's properties and operations together */typedef struct tagSysMenu{ char *text; /* menu text */ BYTE xPos; /* menu x coordinate on LCD */ BYTE yPos; /* menu y coordinate on the LCD*/ void (*onOkFun)(); /* Press the ok function pointer on the menu */ void (*onCancelFun)(); /* Press the cancel button on the menu Processing function pointer */}SysMenu, *LPSysMenu;

When I define a menu, I only need this:

Static SysMenu menu[MENU_NUM] ={ { "menu1", 0, 48, menu1OnOk, menu1OnCancel } , { " menu2", 7, 48, menu2OnOk, menu2OnCancel } , { " menu3", 7, 48, menu3OnOk, menu3OnCancel } , { " menu4", 7, 48, menu4OnOk, menu4OnCancel } ...};

The processing of the OK button and the CANCEL button becomes:

/* Press OK*/void onOkKey(){ menu[currentFocusMenu].onOkFun(); }/* Press Cancel*/void onCancelKey(){ menu[currentFocusMenu].onCancelFun();

The program has been greatly simplified, and it has begun to have good scalability! We only use the encapsulation idea in object-oriented, so that the program structure is clear, the result is that almost more menus can be added to the system without modifying the program, and the system's key processing functions remain unchanged.
Object-oriented, true God!


OBD Connectors

We make OBD connector with terminal by ourselves, soldering type and crimping type are both available. Also 12V and 24V type. OBD1, OB2, J1939, J1708, J1962, etc. Also molded by different type, straight type or right-angle type. The OBD connector cables used for Audi, Honda, Toyota, BWM, etc. We have wide range of materials source , also we can support customers to make a customized one to replace the original ones.

Sae J1708 Connector,Sae J1939 Connector,OBD2 Diagnostic Connectors,Diagnostic Connector,Deutsch Diagnostic Connector

ETOP WIREHARNESS LIMITED , https://www.oemmoldedcables.com