Abstract state machine class QFSM or QHSM has a function pointer that points to the specific state function in the inherited state machine class, which has two interface function init () and dispatch (), which works by understanding the key of the state machine handling event process.
The State machine class inherits from QFSM or QHSM, and inherits this function pointer, which is used to dynamically point to the private state function in the specific state machine class.
A specific event inherits the root event qevent and can add additional properties on its own.
?
图1.QEP总体类结构
1. Pre-knowledge
(1) Declaring a function pointer
The specific state machine class inherits from QFSM or QHSM, then the concrete state tool has a pointer state,state can point to any state function and call the state function to execute. The dispatch () function detects the structure of the state machine with an empty event, can perform entry and exit actions, can perform specific event-handling actions, and can also carry out state transitions.
typedef uint8_t QState; typedef QState (*QStateHandler)(void *me, QEvent const *e);/*函数指针*/
(2) Declaring a function pointer variable in a state machine
typedef struct QFsmTag { QStateHandler state; /*变量(函数指针),指向状态机当前状态(函数)*/} QFsm;typedef struct QFsmTag QHsm;
(3) Return result after the status function is processed
Each state function, depending on the event handling, will return one of the top results after processing, for dispatch () to understand whether the state function is ignored by an event, whether it is processed, whether it is being processed, or not, and that the parent state function should be processed.
The parameters in QSUPP (Super) determine the hierarchical state machine hierarchy.
/* 忽略事件 */#define Q_IGNORED() (Q_RET_IGNORED)/* 已处理事件 */#define Q_HANDLED() (Q_RET_HANDLED)/* 状态转移 */#define Q_TRAN(target_) (((QFsm *)me)->state = (QStateHandler)(target_), Q_RET_TRAN)/* 到超状态 */#define Q_SUPER(super_) (((QHsm *)me)->state = (QStateHandler)(super_), Q_RET_SUPER)
(4) Trigger action
Dispatch () is used to proactively trigger a state machine to detect the state machine's structure, perform entry and exit actions. Using theqep empty SIG to trigger an empty action, will always execute to the last line of the state function QSUPPER (Super_), through the triggering of the return results, you can detect the state machine hierarchy.
/* 以sig_信号触发状态机 */#define QEP_TRIG_(state_, sig_) ((*(state_))(me, &QEP_reservedEvt_[sig_]))/* 触发退出动作,在层次状态机HSM */#define QEP_EXIT_(state_) if (QEP_TRIG_(state_, Q_EXIT_SIG) == Q_RET_HANDLED) { ...QS }/* 触发进入动作,在层次状态机HSM */#define QEP_ENTER_(state_) if (QEP_TRIG_(state_, Q_ENTRY_SIG) == Q_RET_HANDLED) { ...QS }/* 触发空动作,在层次状态机HSM (自加)*/#define QEP_EMPTY_(state_) QEP_TRIG_(state_, QEP_EMPTY_SIG_)
2.FSM state Machine Init () and dispatch () process
(1) qfsm_init () process
?
图2.QFsm_init()流程
(2) Qfsm_dispatch () process
?
图3.QFsm_dispatch()流程
3.HSM state Machine Init () and dispatch () process
The entry action is performed from the initial pseudo state of the top state to the final state (sub-state) of the initial pseudo-state transition. In the final state (sub-state) Continue checking there is no sub-initial pseudo-state, if any, continue to enter, perform into action until the final state.
(1) qhsm_init () process
?
图4.QHsm_init()流程图与层次状态机
(2) Qhsm_dispatch () process
?
图5.QHsm_dispatch()流程图
keywords: state machine, abstract state machine class, State machine class, state function, initial pseudo state, parent state, Hyper State, source state, final state, Ultimate state.
Reference:
"1" Miro Samek the practical design of UML state diagram---The event-driven programming technology of embedded system second Edition
QEP Init () and dispatch () flowchart