⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 state machine working with rtos
💻 C
字号:
/****************************************************************************** Product:  QBomb/C Example for Linux* Version:  Compatible with QEP/C 3.1.yy* Updated:  Nov 29, 2005** Copyright (C) 2002-2005 Quantum Leaps, LLC. All rights reserved.** This example is part of the Quantum Leaps QP/C software, and 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 allow the* licensees to retain the proprietary status of their code. The licensees* who use this software under one of Quantum Leaps commercial licenses do* not use this software under the GPL and therefore are not subject to any* of its terms.** Contact information:* Quantum Leaps Web site:  http://www.quantum-leaps.com* Quantum Leaps licensing: http://www.quantum-leaps.com/licensing* Quantum Leaps products:  http://www.quantum-leaps.com/products* e-mail:                  sales@quantum-leaps.com*****************************************************************************/#include "qep_port.h"#include "qbomb.h"#include "qassert.h"#include <stdlib.h>#include <stdio.h>#include <sys/select.h>#include <termios.h>#include <unistd.h>Q_DEFINE_THIS_FILE/* Local objects -----------------------------------------------------------*/static QBomb l_qbomb;                                   /* quantum bomb FSM */struct termios l_saved;         /* structure with saved terminal attributes *//*..........................................................................*/void Q_assert_handler(char const *file, int line) {    fprintf(stderr, "Assertion failed in %s, line %d", file, line);    exit(-1);}/*..........................................................................*/void QBomb_updateState(QBomb *me, char const *s) {    (void)me;              /* suppress the "unreferenced parameter" warning */    printf("%s;", s);}/*..........................................................................*/void QBomb_exit(QBomb *me) {    (void)me;              /* suppress the "unreferenced parameter" warning */    printf("\nBye, Bye!");    tcsetattr(0, TCSANOW, &l_saved);/*restore the saved terminal attributes */    exit(0);}/*..........................................................................*/int main() {    QBombInitEvt ie;    struct termios tio;                     /* modified terminal attributes */    tcgetattr(0, &l_saved);         /* save the current terminal attributes */    tcgetattr(0, &tio);              /* get the current terminal attributes */    tio.c_lflag &= ~ICANON;                       /* disable canonical mode */    tcsetattr(0, TCSANOW, &tio);                  /* set the new attributes */    QBomb_ctor(&l_qbomb);    printf("QBomb example, built on %s at %s,\n"           "QEP/C: %s.\nPress ESC to quit...\n",           __DATE__, __TIME__, QEP_getVersion());    ie.defuse = 0x0D;       /* initialize with the defuse code, 1101 binary */    QFsm_init((QFsm *)&l_qbomb, (QEvent *)&ie);  /* take initial transition */    for (;;) {                                                /* event loop */        fd_set con;                      /* FD set representing the console */        static struct timeval timeout;              /* timeout for select() */        static int tickCtr = 1;        QEvent e;        char const *msg = (char *)0;        FD_ZERO(&con);        FD_SET(0, &con);        timeout.tv_sec =  0;        timeout.tv_usec = 100000;                           /* 100 ms delay */        select(1, &con, 0, 0, &timeout); /* wait for the timeout or console */        if (FD_ISSET(0, &con)) {            char ch;            read(0, &ch, 1);            switch (ch) {                case 'a':                case 'A': {                                      /* Arm key */                    e.sig = ARM_SIG;                    msg = "ARM";                    break;                }                case 'u':                case 'U': {                                       /* Up key */                    e.sig = UP_SIG;                    msg = "UP";                    break;                }                case 'd':                case 'D': {                                     /* Down key */                    e.sig = DOWN_SIG;                    msg = "DOWN";                    break;                }                case '\33': {                                    /* ESC key */                    QBomb_exit(&l_qbomb);                    break;                }            }        }        else if (--tickCtr == 0) {                    /* time for the tick? */            tickCtr = 10;            e.sig = TICK_SIG;            msg = "TICK";        }        if (msg != (char *)0) {            printf("\n[%2d] %s: ", (int)QBomb_getTimeout(&l_qbomb), msg);            QFsm_dispatch((QFsm *)&l_qbomb, &e);      /* dispatch the event */        }    }    return 0;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -