198 lines
5.9 KiB
C++
Raw Normal View History

2013-10-10 20:01:51 -04:00
//****************************************************************************
2018-10-25 11:13:01 -04:00
// Product: QHsmTst Example
2020-04-02 21:21:53 -04:00
// Last Updated for Version: 6.8.0
// Date of the Last Update: 2020-04-01
2012-08-14 18:00:48 -04:00
//
2018-10-25 11:13:01 -04:00
// Q u a n t u m L e a P s
// ------------------------
// Modern Embedded Software
2012-08-14 18:00:48 -04:00
//
2020-04-02 21:21:53 -04:00
// Copyright (C) 2005-2020 Quantum Leaps, LLC. All rights reserved.
2012-08-14 18:00:48 -04: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
2012-08-14 18:00:48 -04:00
// (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
2019-12-31 15:56:23 -05:00
// along with this program. If not, see <www.gnu.org/licenses/>.
2012-08-14 18:00:48 -04:00
//
// Contact information:
2019-12-31 15:56:23 -05:00
// <www.state-machine.com/licensing>
// <info@state-machine.com>
2013-10-10 20:01:51 -04:00
//****************************************************************************
2019-10-27 12:26:31 -04:00
#include "qpcpp.hpp"
#include "qhsmtst.hpp"
2012-08-14 18:00:48 -04:00
2020-04-02 21:21:53 -04:00
#include "safe_std.h" // portable "safe" <stdio.h>/<string.h> facilities
2012-08-14 18:00:48 -04:00
#include <stdlib.h>
2018-10-25 11:13:01 -04:00
using namespace QP;
2012-08-14 18:00:48 -04:00
Q_DEFINE_THIS_FILE
// Local objects -------------------------------------------------------------
static FILE *l_outFile = (FILE *)0;
static void dispatch(QP::QSignal sig);
//............................................................................
2018-10-25 11:13:01 -04:00
int main(int argc, char *argv[ ]) {
#ifdef Q_SPY
uint8_t qsBuf[128];
QS::initBuf(qsBuf, sizeof(qsBuf));
#endif
QF::init();
QF::onStartup();
2015-05-14 16:05:04 -04:00
if (argc > 1) { // file name provided?
2012-08-14 18:00:48 -04:00
l_outFile = fopen(argv[1], "w");
}
2015-05-14 16:05:04 -04:00
if (l_outFile == (FILE *)0) { // interactive version?
2018-10-25 11:13:01 -04:00
l_outFile = stdout;
2012-08-14 18:00:48 -04:00
2020-04-02 21:21:53 -04:00
PRINTF_S("QHsmTst example, built on %s at %s\n"
2012-08-14 18:00:48 -04:00
"QEP: %s.\nPress ESC to quit...\n",
2020-04-02 21:21:53 -04:00
__DATE__, __TIME__, QP_VERSION_STR);
2012-08-14 18:00:48 -04:00
2015-05-14 16:05:04 -04:00
the_hsm->init(); // trigger the initial tran. in the test HSM
2012-08-14 18:00:48 -04:00
2015-05-14 16:05:04 -04:00
for (;;) { // event loop
2020-04-02 21:21:53 -04:00
PRINTF_S("\n>", "");
2018-10-25 11:13:01 -04:00
int c;
c = (uint8_t)QP::QF_consoleWaitForKey();
2020-04-02 21:21:53 -04:00
PRINTF_S("%c: ", (c >= ' ') ? c : 'X');
2012-08-14 18:00:48 -04:00
QP::QEvt e = QEVT_INITIALIZER(0);
2015-05-14 16:05:04 -04:00
if ('a' <= c && c <= 'i') { // in range?
2012-08-14 18:00:48 -04: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?
2012-08-14 18:00:48 -04: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
2012-08-14 18:00:48 -04:00
}
else {
e.sig = IGNORE_SIG;
}
2015-05-14 16:05:04 -04:00
the_hsm->dispatch(&e); // dispatch the event
2012-08-14 18:00:48 -04:00
}
}
2015-05-14 16:05:04 -04:00
else { // batch version
2020-04-02 21:21:53 -04:00
PRINTF_S("QHsmTst, output saved to %s\n", argv[1]);
FPRINTF_S(l_outFile,
2012-08-14 18:00:48 -04:00
"QHsmTst example, QEP %s\n", QP::QEP::getVersion());
2015-05-14 16:05:04 -04:00
the_hsm->init(); // trigger the initial tran. in the test HSM
2012-08-14 18:00:48 -04:00
2018-10-25 11:13:01 -04:00
// dynamic transitions
2012-08-14 18:00:48 -04: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);
}
2018-10-25 11:13:01 -04:00
QF::onCleanup();
2012-08-14 18:00:48 -04:00
return 0;
}
2018-10-25 11:13:01 -04:00
2012-08-14 18:00:48 -04:00
//............................................................................
2018-10-25 11:13:01 -04:00
void BSP_display(char const *msg) {
2020-04-02 21:21:53 -04:00
FPRINTF_S(l_outFile, "%s", msg);
2012-08-14 18:00:48 -04:00
}
//............................................................................
void BSP_terminate(int16_t const result) {
2020-04-02 21:21:53 -04:00
PRINTF_S("%s", "Bye, Bye!");
2018-10-25 11:13:01 -04:00
QF::onCleanup();
2012-08-14 18:00:48 -04:00
exit(result);
}
//............................................................................
2020-03-17 21:33:58 -04:00
extern "C" Q_NORETURN Q_onAssert(char const * const file, int_t const line) {
2020-04-02 21:21:53 -04:00
FPRINTF_S(stderr, "Assertion failed in %s, line %d", file, line);
2018-10-25 11:13:01 -04:00
QF::onCleanup();
exit(-1);
}
//............................................................................
2012-08-14 18:00:48 -04:00
static void dispatch(QP::QSignal sig) {
Q_REQUIRE((A_SIG <= sig) && (sig <= I_SIG));
2020-04-02 21:21:53 -04:00
FPRINTF_S(l_outFile, "\n%c:", 'A' + sig - A_SIG);
2012-08-14 18:00:48 -04:00
QP::QEvt e = QEVT_INITIALIZER(sig);
2015-05-14 16:05:04 -04:00
the_hsm->dispatch(&e); // dispatch the event
2012-08-14 18:00:48 -04:00
}
2014-01-07 19:59:39 -05:00
namespace QP {
2018-10-25 11:13:01 -04:00
//----------------------------------------------------------------------------
2014-01-07 19:59:39 -05:00
void QF::onStartup(void) {
2018-10-25 11:13:01 -04:00
QF_consoleSetup();
2014-01-07 19:59:39 -05:00
}
//............................................................................
void QF::onCleanup(void) {
2018-10-25 11:13:01 -04:00
QF_consoleCleanup();
2014-01-07 19:59:39 -05:00
}
//............................................................................
2018-10-25 11:13:01 -04:00
void QF_onClockTick(void) {
2014-01-07 19:59:39 -05:00
}
2018-10-25 11:13:01 -04:00
//----------------------------------------------------------------------------
#ifdef Q_SPY
//! callback function to execute user commands
void QS::onCommand(uint8_t cmdId,
uint32_t param1, uint32_t param2, uint32_t param3)
{
switch (cmdId) {
case 0U: {
break;
}
default:
break;
}
/* unused parameters */
(void)param1;
(void)param2;
(void)param3;
2014-01-07 19:59:39 -05:00
}
#endif // Q_SPY
2018-10-25 11:13:01 -04:00
//----------------------------------------------------------------------------
} // namespace QP