基于状态机----状态表的小组件

Posted Mic_chen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了基于状态机----状态表的小组件相关的知识,希望对你有一定的参考价值。

使用实例
   
static state_trans_t close2ready = {
    WIFI_ST_READY,   // next state
    __open      // action
};
static state_trans_t ready2work = {
    WIFI_ST_WORK,
    __start
};
static state_trans_t ready2close = {
    WIFI_ST_CLOSED,
    __close
};
static state_trans_t work2ready = {
    WIFI_ST_READY,
    __stop
};
static state_trans_t work2close = {
    WIFI_ST_CLOSED,
    __close
};
static state_trans_t ignore = {
    WIFI_ST_CLOSED,
    __close
};


static pstate_trans_t trans_tble[][WIFI_EV_MAX] = {
                  /*EV_OPEN*/  /*EV_START*/  /*EV_STOP*/  /*EV_CLOSE*/ 
/*ST_CLOSED*/   &close2ready,  NULL,          NULL,          NULL,
/*ST_READY*/    NULL,          &ready2work,   NULL,          &ready2close,
/*ST_WORK*/     NULL,          NULL,          &work2ready,   &work2close
};

  

typedef int (*state_action)(int state, int event); typedef struct state_trans{ int next_state; state_action action; } state_trans_t,*pstate_trans_t; typedef struct fsm { int curr_state; int event_max; pstate_trans_t *pptrans_tbl; ak_mutex_t mutex; }fsm_t; int akp_fsm_ctor(fsm_t *this, pstate_trans_t *pptrans_tbl,int event_max); int akp_fsm_dector(fsm_t *this); int akp_fsm_on_event(fsm_t *this, int event); int akp_fsm_get_curr_status(fsm_t *this); //.c int akp_fsm_ctor(fsm_t *this, pstate_trans_t *pptrans_tbl,int event_max) { this->curr_state = 0; this->event_max = event_max; this->pptrans_tbl = pptrans_tbl; ak_thread_mutex_init(&(this->mutex),NULL); } int akp_fsm_dector(fsm_t *this) { this->pptrans_tbl = NULL; ak_thread_mutex_destroy(&(this->mutex)); } int akp_fsm_on_event(fsm_t *this, int event) { int ret = 0; if(NULL == this->pptrans_tbl) { ak_print_error_ex("[state]: pptrans_tbl is null,pl ctor fsm\n"); return -1; } ak_thread_mutex_lock(&(this->mutex)); int state = this->curr_state; //ak_print_normal("[state]: max=%d state=%d event=%d tbl=0x%x\n",this->event_max,state,event,this->pptrans_tbl); pstate_trans_t ptrans = (*(this->pptrans_tbl+(this->event_max*state+event))); //ak_print_normal("ptrans %x\n",ptrans); if(NULL != ptrans) { this->curr_state = ptrans->next_state; ret = ptrans->action(state,event); // must after change state } // else ignore this case else{ ak_print_warning("[fsm]: ignore this case: curr state=%d,event=%d\n",this->curr_state,event); } ak_thread_mutex_unlock(&(this->mutex)); return ret; } int akp_fsm_get_curr_status(fsm_t *this) { return (this->curr_state); }

  

以上是关于基于状态机----状态表的小组件的主要内容,如果未能解决你的问题,请参考以下文章

如何保存底部导航片段的状态 - 具有单个导航图的 Android 导航组件

试试用有限状态机的思路来定义javascript组件

当返回到“导航架构组件”中的同一选项卡时,嵌套片段的状态会丢失

react-native 中的小问题活动指标 |组件 |组件内的道具 |道具 |设置状态

Backbone.js 状态管理/基于 url 片段的视图初始化

虚拟机使用的小技巧