一、映射表在串口数据解析中的应用
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<CMDMax;i++){NEXT=StrCmp(buf,(char*)InstructionList[i].CMD);if(NEXT!=NULL){usartfuncp=InstructionList[i].cmd_operate;Result=(*usartfuncp)(NEXT);}}return Result;
}
二、映射表在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},
};
资料直通车:Linux内核源码技术学习路线+视频教程内核源码
学习直通车:Linuxc/c++高级开发【直播公开课】
零声白金VIP体验卡:零声白金VIP体验卡(含基础架构/高性能存储/golang/QT/音视频/Linux内核)
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;
}
原文作者: 物联网IoT技术