Fundamentals 4 min read

How Mapping Tables Simplify Serial Port Parsing and UI Navigation in Embedded C

This article explains how to use mapping tables to efficiently decode serial port commands and drive UI menu navigation in embedded C, providing concrete data structures, instruction lists, and example functions for both communication handling and scene management.

Liangxu Linux
Liangxu Linux
Liangxu Linux
How Mapping Tables Simplify Serial Port Parsing and UI Navigation in Embedded C

Mapping Table in Serial Port Data Parsing

The article first defines a simple structure to associate a command string with its handling function:

typedef struct {
    char CMD[CMDLen];
    unsigned char (*cmd_operate)(char *data);
} Usart_Tab;

An array of these structures, InstructionList, maps each command to its corresponding function:

static const Usart_Tab InstructionList[CMDMax] = {
    {"PWON", PowOn},
    {"PWOFF", PowOff},
    {"HDCP", HdcpOnOff},
    {"/V", QueryKaVersion},
    {"EDIDUpgrade", UpdataEDID},
    {"Psave", Psave},
    {"Precall", Precall},
    {"Pclear", Pclear},
};

The parsing function DataAnalysis iterates over the table, compares the incoming buffer with each command, and invokes the matched handler:

unsigned char DataAnalysis(char *buf) {
    unsigned char i, Result;
    char *NEXT = NULL;
    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;
}

Mapping Table in UI Design

For UI navigation, an enumeration defines menu scenes:

typedef enum {
    stage1 = 0,
    stage2,
    stage3,
    stage4,
    stage5,
    stage6,
    stage7,
    stage8,
    stage9,
} SCENE;

A structure stores the handler for the current scene and the indices of neighboring scenes:

typedef struct {
    void (*current_operate)(); // handler for the scene
    SCENE Index;               // label of the scene
    SCENE Up;                  // scene when Up key is pressed
    SCENE Down;                // scene when Down key is pressed
    SCENE Right;               // scene when Right key is pressed
    SCENE Left;                // scene when Left key is pressed
} STAGE_TAB;

The navigation table stage_tab links each scene to its handler and neighbor indices:

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},
};

Two variables keep track of the current and previous scenes:

char current_stage = stage1;
char prev_stage = current_stage;

When the Up key is pressed, the current scene is updated using the Up field of the table:

current_stage = stage_tab[current_stage].Up;

If the scene has changed, the corresponding handler is executed and the previous scene variable is updated:

if (current_stage != prev_stage) {
    stage_tab[current_stage].current_operate();
    prev_stage = current_stage;
}

The article concludes with a brief note that the author is sharing these essential programming resources for free, but the technical content above stands on its own as a practical guide for using mapping tables in both serial communication and UI state management.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

state machineCUI NavigationSerial PortMapping Table
Liangxu Linux
Written by

Liangxu Linux

Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.