QEP Principle of QP

Source: Internet
Author: User
Tags case statement fsm

1.QP Introduction:

Quantum platform (Quantum Platform, QP) is a software framework for real-time embedded systems, and QP is a lightweight, open-source, hierarchical state machine-based, event-driven platform.

QP includes four parts of event processor (QEP), lightweight event-driven framework (QF), Task Scheduler microkernel (QK), and real-time Trace debugger (QS).

Using QP, you can develop a well-structured embedded application (using the C or C + + language).

2.QEP Core Ideas

The core idea of QEP is to use a function pointer to point to the current state function, using the function pointer to conditionally execute a state function and perform other corresponding actions based on the result of the execution.

(1) Conversion of State diagram to C language
The state diagram can be easily converted to a C language representation, as illustrated below (omitting constructors and initialization functions).

For example, the following planar state machine FSM, the figure has two states: set the state setting and the timing state timing.

can be converted into C code as follows
Two state function declarations:

static QState Bomb4_setting(Bomb4 *me, QEvent const *e);/*声明设置状态函数*/static QState Bomb4_timing (Bomb4 *me, QEvent const *e);/*声明计时状态函数*/

Implementation of two state functions (Handling of events):

Qstate bomb4_setting (Bomb4 *me, qevent const *e) {switch (e->sig) {case UP_SIG: {                     /*up_sig Event Handling---increased timing processing */... return q_handled (); } case Down_sig: {/*down_sig event handling---Reduced timed event handling */... return                     Q_handled ();  } case Arm_sig: {/*arm_sig event handling---Timed event handling */return Q_tran (&bomb4_timing);/* Go to timed status */}} return q_ignored ();                 /* Ignore event */}/*----6.4-state function (timed state processing)-----. */qstate bomb4_timing (Bomb4 *me, qevent const *e) { Switch (e->sig) {case Q_ENTRY_SIG: {/* Status entered processing */... r                     Eturn q_handled (); } case Up_sig: {/*up_sig event handling---Save Password settings */... return Q_handled (); } case Down_sig: {/*down_sig event handling---Save Password settings */... return q_                     HANDLED (); } case Arm_sig: {/*arm_sig event handling---The password is correct then detonate, go to set status */if (Me->code = = ME-&G                                                   T;defuse) {return Q_tran (&bomb4_setting);                     } return q_handled ();                     } case TICK_SIG: {/* timed event handling */... return q_handled (); }} return Q_ignored ();}

(2) state function pointer
In Qp, a function is called a state function, a state is represented by a state function, a system has multiple states, and it can be represented by multiple functions. A state function pointer Qstatehandler is defined in QEP and can be pointed to any state function using this function pointer. A clearly structured switch---Case statement is used within the state function to classify different events (signals).

The state function pointer is defined as follows:

typedef QState  (*QStateHandler)(void *me, QEvent const *e);  /*状态函数指针,指向状态机中任何一个状态函数*/

Where Qstate is the return value of the call state function, which is defined as follows:

typedef uint8_t QState;/*状态返回值,状态机状态处理函数返回值*/

There are four kinds of return values: 0---qretHANDLED, indicating that the event was processed, but no conversion, called internal conversion; 1---qretignored, indicating that the event was ignored, no processing; 2---qret TRAN, which indicates that the event has been processed and converted to another State; 3---QRETSUPER, which indicates that it is in the parent state and is used only in a hierarchical state machine HSM.

(3) Current state of the state machine

A planar state machine an FSM or a hierarchy state machine HSM internally defines a state variable of type Qstatehandler, which is a pointer to a status function, which state function is pointed to, which state function is the current state, and when there is an event, The event is always sent to the state function of the current state for processing. The state machine has multiple states, but at the same time, there is only one "focus" (the current state), and the "focus" can be changed with QTRAN (target).

The current state variable is defined as follows:

typedef struct QFsmTag {QStateHandler  state;  /*状态变量,当前活动状态,也就是经常用到的me->state */                       } QFsm;  /*平面状态机FSM数据结构*/typedef struct QFsmTag QHsm;  /*层次状态机HSM数据结构,与FSM一样*/

The state transitions are defined as follows:

#define Q_TRAN(target_)(((QFsm *)me)->state = (QStateHandler)(target_), Q_RET_TRAN)

Events that are sent to the state machine are always processed to the state function pointed to by the current state variable.

(4) event handler
The event handler can also be understood as a state machine engine that, when handled with an event, invokes the state function of the current state to handle the event, and processes the return value of the call state function, making the corresponding State transformation based on the return value (such as moving to the parent state).

The state engine also handles the entry of the State (enter), exits, and processes the initial pseudo state.

The event handler uses the state function pointer to invoke the state function, always sending the event to its current state, but the event is not processed in the current state and is judged by the result of the call.

3. Conclusion

The state function pointer can be used to invoke any state function on the principle and check the result of the call. The state machine engine is also an event dispatch executor. The Qhsm_dispatch () function is the most complex function in QP and is the key to understanding state machine processing.

QEP Principle of QP

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.