📄 qhsmtst.c
字号:
/****************************************************************************** Product: QHsmTst Example* Last Updated for Version: 3.4.00* Date of the Last Update: Sep 04, 2007** Q u a n t u m L e a P s* ---------------------------* innovating embedded systems** Copyright (C) 2002-2007 Quantum Leaps, LLC. All rights reserved.** This software may be distributed and modified under the terms of the GNU* General Public License version 2 (GPL) as published by the Free Software* Foundation and appearing in the file GPL.TXT included in the packaging of* this file. Please note that GPL Section 2[b] requires that all works based* on this software must also be made publicly available under the terms of* the GPL ("Copyleft").** Alternatively, this software may be distributed and modified under the* terms of Quantum Leaps commercial licenses, which expressly supersede* the GPL and are specifically designed for licensees interested in* retaining the proprietary status of their code.** Contact information:* Quantum Leaps Web site: http://www.quantum-leaps.com* e-mail: info@quantum-leaps.com*****************************************************************************/#include "qep_port.h"#include "qhsmtst.h"#include "qassert.h"/* local-scope objects .....................................................*/typedef struct QHsmTstTag { QHsm super; /* extend class QHsm */ uint8_t foo; /* extended state variable */ /* . . . */} QHsmTst;/* ctors: */static void QHsmTst_ctor(QHsmTst *me);/* HSM state handler functions: */static void QHsmTst_initial(QHsmTst *me, QEvent const *e); /* pseudostate */static QSTATE QHsmTst_s(QHsmTst *me, QEvent const *e); /* state-handler */static QSTATE QHsmTst_s1(QHsmTst *me, QEvent const *e); /* state-handler */static QSTATE QHsmTst_s11(QHsmTst *me, QEvent const *e); /* state-handler */static QSTATE QHsmTst_s2(QHsmTst *me, QEvent const *e); /* state-handler */static QSTATE QHsmTst_s21(QHsmTst *me, QEvent const *e); /* state-handler */static QSTATE QHsmTst_s211(QHsmTst *me, QEvent const *e); /* state-handler */static QHsmTst l_hsmtst; /* the sole instance of the state machine object *//* global-scope definitions ------------------------------------------------*/extern QHsm * const the_hsm = (QHsm *)&l_hsmtst; /* the opaque pointer */void qhsmtst_init(void) { /* instantiate and initialize the test HSM */ QHsmTst_ctor(&l_hsmtst); /* instantiate the HSM */ QHsm_init((QHsm *)&l_hsmtst, (QEvent *)0); /* take the initial tran. */}/*..........................................................................*/void QHsmTst_ctor(QHsmTst *me) { QHsm_ctor(&me->super, (QState)&QHsmTst_initial);}/*..........................................................................*/void QHsmTst_initial(QHsmTst *me, QEvent const *e) { (void)e; /* suppress "e not used" compiler warning */ BSP_display("top-INIT;"); me->foo = 0; /* initialize extended state variable */ Q_INIT(&QHsmTst_s2);}/*..........................................................................*/QSTATE QHsmTst_s(QHsmTst *me, QEvent const *e) { switch (e->sig) { case Q_ENTRY_SIG: { BSP_display("s-ENTRY;"); return (QSTATE)0; } case Q_EXIT_SIG: { BSP_display("s-EXIT;"); return (QSTATE)0; } case Q_INIT_SIG: { BSP_display("s-INIT;"); Q_INIT(&QHsmTst_s11); return (QSTATE)0; } case E_SIG: { BSP_display("s-E;"); Q_TRAN_DYN(&QHsmTst_s11); return (QSTATE)0; } case I_SIG: { /* internal transition with a guard */ if (me->foo) { BSP_display("s-I;"); me->foo = 0; return (QSTATE)0; } break; } case TERMINATE_SIG: { BSP_exit(); return (QSTATE)0; } } return (QSTATE)&QHsm_top;}/*..........................................................................*/QSTATE QHsmTst_s1(QHsmTst *me, QEvent const *e) { switch (e->sig) { case Q_ENTRY_SIG: { BSP_display("s1-ENTRY;"); return (QSTATE)0; } case Q_EXIT_SIG: { BSP_display("s1-EXIT;"); return (QSTATE)0; } case Q_INIT_SIG: { BSP_display("s1-INIT;"); Q_INIT(&QHsmTst_s11); return (QSTATE)0; } case A_SIG: { BSP_display("s1-A;"); Q_TRAN_DYN(&QHsmTst_s1); return (QSTATE)0; } case B_SIG: { BSP_display("s1-B;"); Q_TRAN_DYN(&QHsmTst_s11); return (QSTATE)0; } case C_SIG: { BSP_display("s1-C;"); Q_TRAN_DYN(&QHsmTst_s2); return (QSTATE)0; } case D_SIG: { /* transition with a gurad */ if (!me->foo) { BSP_display("s1-D;"); me->foo = 1; Q_TRAN_DYN(&QHsmTst_s); return (QSTATE)0; /* event handled */ } break; } case F_SIG: { BSP_display("s1-F;"); Q_TRAN_DYN(&QHsmTst_s211); return (QSTATE)0; } case I_SIG: { /* internal transition */ BSP_display("s1-I;"); return (QSTATE)0; } } return (QSTATE)&QHsmTst_s;}/*..........................................................................*/QSTATE QHsmTst_s11(QHsmTst *me, QEvent const *e) { switch (e->sig) { case Q_ENTRY_SIG: { BSP_display("s11-ENTRY;"); return (QSTATE)0; } case Q_EXIT_SIG: { BSP_display("s11-EXIT;"); return (QSTATE)0; } case D_SIG: { /* transition with a gurad */ if (me->foo) { BSP_display("s11-D;"); me->foo = 0; Q_TRAN_DYN(&QHsmTst_s1); return (QSTATE)0; /* event handled */ } break; } case G_SIG: { BSP_display("s11-G;"); Q_TRAN_DYN(&QHsmTst_s211); return (QSTATE)0; } case H_SIG: { BSP_display("s11-H;"); Q_TRAN_DYN(&QHsmTst_s); return (QSTATE)0; } } return (QSTATE)&QHsmTst_s1;}/*..........................................................................*/QSTATE QHsmTst_s2(QHsmTst *me, QEvent const *e) { switch (e->sig) { case Q_ENTRY_SIG: { BSP_display("s2-ENTRY;"); return (QSTATE)0; } case Q_EXIT_SIG: { BSP_display("s2-EXIT;"); return (QSTATE)0; } case Q_INIT_SIG: { BSP_display("s2-INIT;"); Q_INIT(&QHsmTst_s211); return (QSTATE)0; } case C_SIG: { BSP_display("s2-C;"); Q_TRAN_DYN(&QHsmTst_s1); return (QSTATE)0; } case F_SIG: { BSP_display("s2-F;"); Q_TRAN_DYN(&QHsmTst_s11); return (QSTATE)0; } case I_SIG: { /* internal transition with a guard */ if (!me->foo) { BSP_display("s2-I;"); me->foo = (uint8_t)1; return (QSTATE)0; } break; } } return (QSTATE)&QHsmTst_s;}/*..........................................................................*/QSTATE QHsmTst_s21(QHsmTst *me, QEvent const *e) { switch (e->sig) { case Q_ENTRY_SIG: { BSP_display("s21-ENTRY;"); return (QSTATE)0; } case Q_EXIT_SIG: { BSP_display("s21-EXIT;"); return (QSTATE)0; } case Q_INIT_SIG: { BSP_display("s21-INIT;"); Q_INIT(&QHsmTst_s211); return (QSTATE)0; } case A_SIG: { BSP_display("s21-A;"); Q_TRAN_DYN(&QHsmTst_s21); return (QSTATE)0; } case B_SIG: { BSP_display("s21-B;"); Q_TRAN_DYN(&QHsmTst_s211); return (QSTATE)0; } case G_SIG: { BSP_display("s21-G;"); Q_TRAN_DYN(&QHsmTst_s1); return (QSTATE)0; } } return (QSTATE)&QHsmTst_s2;}/*..........................................................................*/QSTATE QHsmTst_s211(QHsmTst *me, QEvent const *e) { switch (e->sig) { case Q_ENTRY_SIG: { BSP_display("s211-ENTRY;"); return (QSTATE)0; } case Q_EXIT_SIG: { BSP_display("s211-EXIT;"); return (QSTATE)0; } case D_SIG: { BSP_display("s211-D;"); Q_TRAN_DYN(&QHsmTst_s21); return (QSTATE)0; } case H_SIG: { BSP_display("s211-H;"); Q_TRAN_DYN(&QHsmTst_s); return (QSTATE)0; } } return (QSTATE)&QHsmTst_s21;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -