一、映射表在串口數據解析中的應用
1、數據結構
typedef struct { char CMD[CMDLen]; unsigned char (*cmd_operate)(char *data); }Usart_Tab;
2、指令、函數映射表
static const Usart_Tab InstructionList[CMDMax]= { {"PWON",PowOn}, {"PWOFF",PowOff}, {"HDCP",HdcpOnOff}, {"/V",QueryKaVersion}, {"EDIDUpgrade",UpdataEDID}, {"Psave",Psave}, {"Precall",Precall}, {"Pclear",Pclear}, };
3、串口解析函數實現
unsigned char DataAnalysis(char *buf) { unsigned char i,Result; char *NEXT=NULL; for(i=0;i二、映射表在UI設計中的應用
1、數據結構
菜單枚舉:
typedef enum { stage1=0, stage2, stage3, stage4, stage5, stage6, stage7, stage8, stage9, }SCENE;數據結構:
typedef struct { void (*current_operate)(); //當前場景的處理函數 SCENE Index; //當前場景的標簽 SCENE Up; //按下Up鍵跳轉的場景 SCENE Down; //按下Down鍵跳轉的場景 SCENE Right; //按下Left鍵跳轉的場景 SCENE Left; //按下Right鍵跳轉的場景 }STAGE_TAB;2、函數映射表
STAGE_TAB stage_tab[]={ #. operate Index Up Down Left Right {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}, };3、定義兩個變量保存當前場景和上一個場景
char current_stage=stage1; char prev_stage=current_stage;4、按下Up按鍵 跳轉到指定場景current_stage的值根據映射表改變
current_stage =stage_tab[current_stage].Up;
5、場景改變后 根據映射表執行相應的函數Handler
if(current_stage!=prev_stage) { stage_tab[current_stage].current_operate(); prev_stage=current_stage; }編輯:黃飛
?
評論
查看更多