Lookup Tables in UART Data Parsing
Data structure
typedef struct { char CMD[CMDLen]; unsigned char (*cmd_operate)(char *data); } Usart_Tab;
Command and function lookup table
static const Usart_Tab InstructionList[cmdmax] = { {"PWON", PowOn}, {"PWOFF", PowOff}, {"HDCP", HdcpOnOff}, {"/V", QueryKaversion}, {"EDIDUpgrade", UpdataEDID}, {"Psave", Psave}, {"Precall", Precall}, {"Pclear", Pclear}, };
UART parsing function implementation
unsigned char DataAnalysis(char *buf){ unsigned char i, Result; char *NEXT = NULL; unsigned char (*usartfuncp)(char *); for(i = 0; i < cmdmax; i++) { NEXT = StrCmp(buf, (char*)InstructionList[i].CMD); if(NEXT != NULL) { usartfuncp = InstructionList[i].cmd_operate; Result = (*usartfuncp)(NEXT); } } return Result; }
Lookup Tables in UI Design
Data structures
Menu enumeration:
typedef enum { stage1 = 0, stage2, stage3, stage4, stage5, stage6, stage7, stage8, stage9, } SCENE;
Structure:
typedef struct { void (*current_operate)(); // Handler function for the current scene SCENE Index; // Label of the current scene SCENE Up; // Scene to switch to when Up is pressed SCENE Down; // Scene to switch to when Down is pressed SCENE Right; // Scene to switch to when Left is pressed SCENE Left; // Scene to switch to when Right is pressed } STAGE_TAB;
Function lookup table
STAGE_TAB stage_tab[] = { {Stage1_Handler, stage1, stage4, stage7, stage3, stage2}, {Stage2_Handler, stage2, stage5, stage8, stage1, stage3}, {Stage3_Handler, stage3, stage6, stage9, stage2, stage1}, {Stage4_Handler, stage4, stage7, stage1, stage6, stage5}, {Stage5_Handler, stage5, stage8, stage2, stage4, stage6}, {Stage6_Handler, stage6, stage9, stage3, stage5, stage4}, {Stage7_Handler, stage7, stage1, stage4, stage9, stage8}, {Stage8_Handler, stage8, stage2, stage5, stage7, stage9}, {Stage9_Handler, stage9, stage3, stage6, stage8, stage7}, };
State variables
char current_stage = stage1; char prev_stage = current_stage;
Handling input and invoking handlers
On Up key press, update the current scene according to the lookup table:
current_stage = stage_tab[current_stage].Up;
When the scene changes, execute the corresponding handler from the lookup table:
if (current_stage != prev_stage) { stage_tab[current_stage].current_operate(); prev_stage = current_stage; }
ALLPCB