QEP principle of QP

Source: Internet
Author: User
Tags case statement fsm

QEP principle of QP
1. QP Introduction: Quantum Platform (QP) is a software framework for real-time embedded systems, QP is a lightweight, open-source, hierarchical state machine-based, and event-driven platform. QP consists of four parts: Event processor (QEP), lightweight event-driven Framework (QF), task scheduling microkernel (QK), and real-time tracking debugger (QS. With QP, you can develop well-structured embedded applications (using C or C ++ ). 2. core Idea of QEP: the core idea of QEP is to use a function pointer to point to the current state function, use this function pointer to execute a state function conditionally, and execute other corresponding actions according to the execution result. (1) The status chart to the C language conversion status chart can be easily converted to the C language representation. The following is an example (the constructor and initialization function are omitted ). For example, the following flat state machine FSM has two statuses: setting and timing. The code can be converted to the following two state function declarations: static QState Bomb4_setting (Bomb4 * me, QEvent const * e ); /* declare and set the status function */static QState Bomb4_timing (Bomb4 * me, QEvent const * e ); /* declare the timer status function */implement two State functions (for event processing ):

QState Bomb4_setting (Bomb4 * me, QEvent const * e) {switch (e-> sig) {case UP_SIG: {/* UP_SIG event processing --- add scheduled Processing */... return Q_HANDLED ();} case DOWN_SIG: {/* DOWN_SIG event processing --- reduce scheduled event processing */... return Q_HANDLED ();} case ARM_SIG: {/* ARM_SIG event processing --- timed event processing */return Q_TRAN (& Bomb4_timing ); /* go to the scheduled status */} return Q_IGNORED ();/* ignore event */}/* ---- 6.4-status function (timed state processing )-----. */QState Bomb4_timing (Bomb4 * me, QEvent const * e) {switch (e-> sig) {case Q_ENTRY_SIG: {/* Status enters Processing */... return Q_HANDLED ();} case UP_SIG: {/* UP_SIG event processing --- save password settings */... return Q_HANDLED ();} case DOWN_SIG: {/* DOWN_SIG event processing --- save password settings */... return Q_HANDLED ();} case ARM_SIG: {/* ARM_SIG event handling --- if the password is correct, the timed detonation will be lifted, go to the set status */if (me-> code = me-> defuse) {return Q_TRAN (& Bomb4_setting);} return Q_HANDLED ();} case TICK_SIG: {/* Scheduled event processing */... return Q_HANDLED () ;}} return Q_IGNORED ();}

 

(2) The state function pointer uses a function in QP to represent the State, which is called a state function. A state function is used to represent a State. If the system has multiple states, multiple functions can be used to represent the State. In QEP, a state function pointer QStateHandler is defined, which can be used to direct to any state function. The switch-case statement with clear structure is used in the status function to classify different events (signals. The status function pointer is defined as follows: typedef QState (* QStateHandler) (void * me, QEvent const * e);/* The status function pointer, point to any state function in the state machine */where QState is the return value of the call state function. Its definition is as follows: typedef uint8_t QState;/* the return value of the state function, the Return Value of the state machine status processing function */has four types of return values: 0---QRETHANDLED, indicating that the event is processed but not converted. It is called internal conversion. 1---QRETIGNORED indicates that the event is ignored and not processed. 2---QRETTRAN, it indicates that the event has been processed and converted to another state. 3---QRETSUPER indicates that the event enters the parent State and is only used in the HSM of the layered state machine. (3) The current state plane state machine FSM or hierarchive state machine HSM internally defines a QStateHandler type state variable, which is a pointer to the state function and the state function to which the state points, which status function is the current status. When an event exists, the event is always sent to the status function in the current status for processing. The state machine has multiple states, but at the same time, there is only one "Focus" (current State), and the "Focus" can be changed using QTRAN (target. The current state variable is defined as follows: typedef struct {smtag {QStateHandler state;/* state variable, current activity status, that is, frequently used me-> state */} QFsm; /* flat state machine FSM Data Structure */typedef struct rjsmtag QHsm;/* hierarchical state machine HSM data structure, same as FSM */state conversion definition: # define Q_TRAN (target _) (QFsm *) me)-> state = (QStateHandler) (target _), Q_RET_TRAN) events sent to the state machine are always sent to the status function pointed to by the current state variable for processing. (4) The event processor can also be understood as a state machine engine. When an event is processed, the current state function is called to process the event and the return value of the Status function is processed, changes the status based on the returned value (for example, transferring to the parent State ). At the same time, the status engine processes ENTER and EXIT statuses, and processes initial pseudo states. The event processor uses the status function pointer to call the status function and always sends the event to the current status. However, the event is not processed in the current status and is determined by the call result. 3. Conclusion any state function can be called using the state function pointer in principle and the call result can be checked. The state machine engine becomes an event dispatch executor. The QHsm_dispatch () function is the most complex function in QP and the key to understanding state machine processing.

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.