2013-12-30 17:41:15 -05:00
|
|
|
//****************************************************************************
|
|
|
|
// Product: QMsmTst Example, POSIX
|
2015-05-14 16:05:04 -04:00
|
|
|
// Last updated for version 5.4.0
|
|
|
|
// Last updated on 2015-04-29
|
2013-12-30 17:41:15 -05:00
|
|
|
//
|
|
|
|
// Q u a n t u m L e a P s
|
|
|
|
// ---------------------------
|
|
|
|
// innovating embedded systems
|
|
|
|
//
|
2015-05-14 16:05:04 -04:00
|
|
|
// Copyright (C) Quantum Leaps, www.state-machine.com.
|
2013-12-30 17:41:15 -05:00
|
|
|
//
|
|
|
|
// This program is open source software: you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU General Public License as published
|
|
|
|
// by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// Alternatively, this program may be distributed and modified under the
|
|
|
|
// terms of Quantum Leaps commercial licenses, which expressly supersede
|
|
|
|
// the GNU General Public License and are specifically designed for
|
|
|
|
// licensees interested in retaining the proprietary status of their code.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
//
|
|
|
|
// Contact information:
|
2015-05-14 16:05:04 -04:00
|
|
|
// Web: www.state-machine.com
|
|
|
|
// Email: info@state-machine.com
|
2013-12-30 17:41:15 -05:00
|
|
|
//****************************************************************************
|
2015-05-14 16:05:04 -04:00
|
|
|
#include "qpcpp.h"
|
2013-12-30 17:41:15 -05:00
|
|
|
#include "qmsmtst.h"
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <termios.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
namespace QMSMTST {
|
|
|
|
|
|
|
|
Q_DEFINE_THIS_FILE
|
|
|
|
|
|
|
|
// Local objects -------------------------------------------------------------
|
|
|
|
static FILE *l_outFile = (FILE *)0;
|
|
|
|
static struct termios l_oldt;
|
|
|
|
static void dispatch(QP::QSignal sig);
|
|
|
|
|
|
|
|
//............................................................................
|
|
|
|
extern "C" int main(int argc, char *argv[ ]) {
|
2015-05-14 16:05:04 -04:00
|
|
|
if (argc > 1) { // file name provided?
|
2013-12-30 17:41:15 -05:00
|
|
|
l_outFile = fopen(argv[1], "w");
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:05:04 -04:00
|
|
|
if (l_outFile == (FILE *)0) { // interactive version?
|
2013-12-30 17:41:15 -05:00
|
|
|
struct termios newt;
|
2015-05-14 16:05:04 -04:00
|
|
|
tcgetattr(STDIN_FILENO, &l_oldt); // save the terminal state
|
2013-12-30 17:41:15 -05:00
|
|
|
newt = l_oldt;
|
|
|
|
newt.c_lflag &= ~ICANON;
|
2015-05-14 16:05:04 -04:00
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &newt); // set non-canonical mode
|
2013-12-30 17:41:15 -05:00
|
|
|
|
2015-05-14 16:05:04 -04:00
|
|
|
l_outFile = stdout; // use the stdout as the output file
|
2013-12-30 17:41:15 -05:00
|
|
|
|
|
|
|
printf("QMsmTst example, built on %s at %s\n"
|
|
|
|
"QEP: %s.\nPress ESC to quit...\n",
|
|
|
|
__DATE__, __TIME__, QP::QEP::getVersion());
|
|
|
|
|
2015-05-14 16:05:04 -04:00
|
|
|
the_msm->init(); // trigger the initial tran. in the test MSM
|
2013-12-30 17:41:15 -05:00
|
|
|
|
2015-05-14 16:05:04 -04:00
|
|
|
for (;;) { // event loop
|
2013-12-30 17:41:15 -05:00
|
|
|
printf("\n>");
|
2015-05-14 16:05:04 -04:00
|
|
|
int c = getchar(); // get a character from stdin
|
2013-12-30 17:41:15 -05:00
|
|
|
printf(": ");
|
|
|
|
|
|
|
|
QP::QEvt e = QEVT_INITIALIZER(0);
|
2015-05-14 16:05:04 -04:00
|
|
|
if ('a' <= c && c <= 'i') { // in range?
|
2013-12-30 17:41:15 -05:00
|
|
|
e.sig = (QP::QSignal)(c - 'a' + A_SIG);
|
|
|
|
}
|
2015-05-14 16:05:04 -04:00
|
|
|
else if ('A' <= c && c <= 'I') { // in range?
|
2013-12-30 17:41:15 -05:00
|
|
|
e.sig = (QP::QSignal)(c - 'A' + A_SIG);
|
|
|
|
}
|
2015-05-14 16:05:04 -04:00
|
|
|
else if (c == '\33') { // the ESC key?
|
|
|
|
e.sig = TERMINATE_SIG; // terminate the interactive test
|
2013-12-30 17:41:15 -05:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
e.sig = IGNORE_SIG;
|
|
|
|
}
|
|
|
|
|
2015-05-14 16:05:04 -04:00
|
|
|
the_msm->dispatch(&e); // dispatch the event
|
2013-12-30 17:41:15 -05:00
|
|
|
}
|
|
|
|
}
|
2015-05-14 16:05:04 -04:00
|
|
|
else { // batch version
|
2013-12-30 17:41:15 -05:00
|
|
|
printf("QMsmTst, output saved to %s\n", argv[1]);
|
|
|
|
fprintf(l_outFile,
|
|
|
|
"QMsmTst example, QEP %s\n", QP::QEP::getVersion());
|
|
|
|
|
2015-05-14 16:05:04 -04:00
|
|
|
the_msm->init(); // trigger the initial tran. in the test MSM
|
2013-12-30 17:41:15 -05:00
|
|
|
|
2015-05-14 16:05:04 -04:00
|
|
|
// dynamic transitions...
|
2013-12-30 17:41:15 -05:00
|
|
|
dispatch(A_SIG);
|
|
|
|
dispatch(B_SIG);
|
|
|
|
dispatch(D_SIG);
|
|
|
|
dispatch(E_SIG);
|
|
|
|
dispatch(I_SIG);
|
|
|
|
dispatch(F_SIG);
|
|
|
|
dispatch(I_SIG);
|
|
|
|
dispatch(I_SIG);
|
|
|
|
dispatch(F_SIG);
|
|
|
|
dispatch(A_SIG);
|
|
|
|
dispatch(B_SIG);
|
|
|
|
dispatch(D_SIG);
|
|
|
|
dispatch(D_SIG);
|
|
|
|
dispatch(E_SIG);
|
|
|
|
dispatch(G_SIG);
|
|
|
|
dispatch(H_SIG);
|
|
|
|
dispatch(H_SIG);
|
|
|
|
dispatch(C_SIG);
|
|
|
|
dispatch(G_SIG);
|
|
|
|
dispatch(C_SIG);
|
|
|
|
dispatch(C_SIG);
|
|
|
|
|
|
|
|
fclose(l_outFile);
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
extern "C" void Q_onAssert(char_t const Q_ROM * const file, int_t line) {
|
|
|
|
fprintf(stderr, "Assertion failed in %s, line %d", file, line);
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
void BSP_display(char_t const *msg) {
|
|
|
|
fprintf(l_outFile, msg);
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
void BSP_terminate(int16_t const result) {
|
|
|
|
tcsetattr(STDIN_FILENO, TCSANOW, &l_oldt);
|
|
|
|
printf("Bye, Bye!\n");
|
|
|
|
exit(result);
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
static void dispatch(QP::QSignal sig) {
|
|
|
|
Q_REQUIRE((A_SIG <= sig) && (sig <= I_SIG));
|
|
|
|
fprintf(l_outFile, "\n%c:", 'A' + sig - A_SIG);
|
|
|
|
QP::QEvt e = QEVT_INITIALIZER(sig);
|
2015-05-14 16:05:04 -04:00
|
|
|
the_msm->dispatch(&e); // dispatch the event
|
2013-12-30 17:41:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace QMSMTST
|
2014-01-07 19:59:39 -05:00
|
|
|
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------------
|
|
|
|
#ifdef Q_SPY
|
|
|
|
|
|
|
|
#include "qs_port.h"
|
|
|
|
|
|
|
|
namespace QP {
|
|
|
|
|
|
|
|
//............................................................................
|
|
|
|
void QF::onStartup(void) {
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
void QF_onClockTick(void) {
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
void QF::onCleanup(void) {
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
void QS::onCleanup(void) {
|
|
|
|
}
|
|
|
|
//............................................................................
|
|
|
|
QSTimeCtr QS::onGetTime(void) {
|
|
|
|
return static_cast<QSTimeCtr>(clock());
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace QP
|
|
|
|
|
|
|
|
#endif // Q_SPY
|
|
|
|
|