函数指针
#include <stdio.h>
typedef void (*pActionFun)(void);
typedef enum{
Status1=1,
Status2,
}CurStatus;
typedef enum{
event1=1,
event2
}TriggerEvent;
typedef enum{
status1=1,
status2,
}NextStatus;
typedef struct status
{
CurStatus cur_status;
TriggerEvent trigger_event;
NextStatus next_status;
pActionFun act_exe;
}LogicStruct;
void Act_Fun(void)
{
printf("Act_Fun 0x01 !
");
}
LogicStruct logic_struct = {Status1,event1,status1,Act_Fun};
int main(int argc, char const *argv[])
{
logic_struct.act_exe();
printf("status study!
");
return 0;
}
状态机学习
#include <stdio.h>
typedef void (*pActionFun)(void);
typedef enum{
event_1=1,
event_2,
event_3,
event_4,
event_5,
}TriggerEvent;
typedef enum{
status_1=1,
status_2,
status_3,
status_4,
status_5,
}Status;
typedef struct status
{
Status cur_status;
TriggerEvent trigger_event;
Status next_status;
pActionFun act_exe;
}LogicStruct;
typedef struct
{
Status status;
int status_num;
LogicStruct* logic_content;
}StatusMachine;
void Act_Fun132(void)
{
printf("Act_Fun 132 !
");
}
void Act_Fun142(void)
{
printf("Act_Fun 142 !
");
}
void Act_Fun213(void)
{
printf("Act_Fun 213 !
");
}
void Act_Fun242(void)
{
printf("Act_Fun 242 !
");
}
void Act_Fun321(void)
{
printf("Act_Fun 321 !
");
}
void Act_Fun332(void)
{
printf("Act_Fun 332 !
");
}
void Act_Fun353(void)
{
printf("Act_Fun 353 !
");
}
void RunSM(StatusMachine* xSM ,TriggerEvent TrigEveID)
{
for (int i = 0; i < xSM->status_num; i++)
{
if((xSM->logic_content[i].cur_status == xSM->status) && (xSM->logic_content[i].trigger_event == TrigEveID))
{
xSM->status = xSM->logic_content[i].next_status;
xSM->logic_content[i].act_exe();
break;
}
}
}
LogicStruct logic_Array[] = {
{status_1,event_3,status_2,Act_Fun132},
{status_1,event_4,status_2,Act_Fun142},
{status_2,event_1,status_3,Act_Fun213},
{status_2,event_4,status_2,Act_Fun242},
{status_3,event_2,status_1,Act_Fun321},
{status_3,event_3,status_2,Act_Fun332},
{status_3,event_5,status_3,Act_Fun353},
};
StatusMachine gSM = {status_1,7,logic_Array};
int main(int argc, char const *argv[])
{
StatusMachine* pSM = NULL;
pSM = &gSM;
TriggerEvent TrigEve[15] = {
event_1,event_2,event_3,event_4,event_5,
event_1,event_2,event_3,event_4,event_5,
event_1,event_2,event_3,event_4,event_5
};
for (int j = 0; j < 15; j++)
{
RunSM(pSM ,TrigEve[j]);
}
printf("status study!
");
return 0;
}