1 Star 0 Fork 0

theone_sun/ardinuo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
statemachine.ino 2.95 KB
一键复制 编辑 原始数据 按行查看 历史
theone_sun 提交于 2021-12-20 16:15 . 状态机架构实现
/***************config.h**********************/
#define STATE_LEN 3
typedef enum {
state_1 =0,
state_2 =1,
state_3 = 3
} state;
/****************statamachine.h*****************************/
typedef state (*state_handle)(void *);
struct _statemachine;
typedef struct _statemachine{
state_handle state_handle_list[STATE_LEN];
unsigned int end_state_liste[STATE_LEN];
state start_state;
unsigned int (*add_state_handle)(struct _statemachine*,state,state_handle,unsigned int);
unsigned int (*set_startstate_handle)(struct _statemachine*,state);
unsigned int (*run_statemachine)(struct _statemachine*,void *);
} statemachine;
unsigned int _add_state_handle(statemachine* ,state ,state_handle ,unsigned int);
unsigned int _set_startstate_handle(statemachine*,state);
unsigned int _run_statemachine(statemachine* ,void *);
/*******************************statemachine.c***************************************************/
unsigned int _add_state_handle(statemachine* sta_m,state _state,state_handle handler,unsigned int end_sta){
if((_state > STATE_LEN) | (handler == NULL)){
return 0;
}
else{
if (end_sta ==1){
sta_m -> end_state_liste[_state] = 1;
}
else{
sta_m -> end_state_liste[_state] = 0;
}
sta_m -> state_handle_list[_state] = handler;
return 1;
}
}
unsigned int _set_startstate_handle(statemachine* sta_m,state _state){
if((_state > STATE_LEN) | (sta_m -> state_handle_list[_state] == NULL)){
return 0;
}
else{
sta_m -> start_state = _state;
return 1;
}
}
unsigned int _run_statemachine(statemachine* sta_m,void *buf){
state _state = sta_m -> start_state;
state_handle _state_handler = sta_m -> state_handle_list[_state];
while(true){
_state = _state_handler(buf);
if(sta_m->end_state_liste[_state]==1){
_state_handler = sta_m -> state_handle_list[_state];
_state_handler(buf);
break;
}
_state_handler = sta_m -> state_handle_list[_state];
}
return 1;
}
unsigned int _init_statemachine(statemachine*);
unsigned int statemachine_process(statemachine*,void*);
unsigned int _init_statemachine(statemachine* sta_m){
sta_m -> add_state_handle = _add_state_handle;
sta_m -> set_startstate_handle = _set_startstate_handle;
sta_m -> run_statemachine = _run_statemachine;
return 1;
}
unsigned int statemachine_process(statemachine* sta_m,void* buf){
return sta_m -> run_statemachine(sta_m,buf);
}
void setup() {
// put your setup code here, to run once:
}
void loop() {
// put your main code here, to run repeatedly:
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/theone-sun/ardinuo.git
git@gitee.com:theone-sun/ardinuo.git
theone-sun
ardinuo
ardinuo
master

搜索帮助