方案1是状态机
现在我想用自己的方案2 函数指针风格
本博文做一个小帮助工具:
//fun--需要执行的函数
//chk--执行函数期待的返回值
//log--打印log的信息用fun的名字吧
//hlp--是否需要做其他事情 1-需要 0-不需要
//ext--是否结束流程return掉 1-需要 0-不需要
#define fun_chk( fun,chk,log,hlp,exit) \
if( (fun) == chk)\
printf("Function %s OK\r\n",log);\
else {printf("Function %s Failed \r\n",log);\
if(hlp)Scale.SetStatus(ScaleAbnormal);\
if(exit)return;}\
memset(tem , 0 , MAXCMDLEN);\
实例:
back = usart_send_receive_repeat(tem,strlen((const char*)ShakeHand)/2,100,1,&respone);
common_chk(back,1,"ShakeHand",0,0);sys_delay(500);
后面可以试试do--while(0)的技巧!
#define fun_chk( fun,chk,log,hlp,exit) \
do{\
if( (fun) == chk)\
printf("Function %s OK\r\n",log);\
else {printf("Function %s Failed \r\n",log);\
if(hlp)Scale.SetStatus(ScaleAbnormal);\
if(exit)return;}\
memset(tem , 0 , MAXCMDLEN);\
}while(0)\