#include<stdio.h>
typedef enum{
Start_state,
Doing_state,
End_state
}def_state;
typedef enum{
Start_event,
Doing_event,
End_event
}def_event;
typedef void (*func)(char* pData);
typedef struct{
def_state current_state;
def_event current_event;
def_state next_state;
func callback;
}def_Machine_state;
void start_task(char *pData)
{
printf("start task!\n");
}
void doing_task(char *pData)
{
printf("doing task!\n");
}
void end_task(char *pData)
{
printf("end task!\n");
}
def_Machine_state Machine_run[]={
Start_state, Start_event, Doing_state, start_task,
Doing_state, Doing_event, End_event, doing_task,
End_event, End_event, End_event, end_task,
};
void loop_machine(def_event trigger_eve,char *pData)
{
int size;
size = sizeof(Machine_run)/sizeof(def_Machine_state);
for(int i=0; i<size;i++)
{
if(trigger_eve == Machine_run[i].current_event)
{
Machine_run[i].callback(pData) ;
}
}
}
main()
{
char *debug;
while(1){
loop_machine(Start_event,debug);
loop_machine(Doing_event,debug);
loop_machine(End_event,debug);
}
}
该简单状态机设置了三个状态,三个事件触发。
运行结果
网友评论