2012-08-14 18:07:04 -04:00
|
|
|
/*****************************************************************************
|
2013-10-10 19:59:51 -04:00
|
|
|
* Product: Reminder2 state pattern example
|
2015-06-04 22:47:13 -04:00
|
|
|
* Last Updated for Version: 5.4.2
|
|
|
|
* Date of the Last Update: 2015-06-03
|
2012-08-14 18:07:04 -04:00
|
|
|
*
|
|
|
|
* Q u a n t u m L e a P s
|
|
|
|
* ---------------------------
|
|
|
|
* innovating embedded systems
|
|
|
|
*
|
2015-04-28 13:45:35 -04:00
|
|
|
* Copyright (C) Quantum Leaps, LLC. All rights reserved.
|
2012-08-14 18:07:04 -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
|
2013-10-16 16:44:03 -04:00
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
2012-08-14 18:07:04 -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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* Contact information:
|
2015-04-28 13:45:35 -04:00
|
|
|
* Web : http://www.state-machine.com
|
|
|
|
* Email: info@state-machine.com
|
2012-08-14 18:07:04 -04:00
|
|
|
*****************************************************************************/
|
2015-04-28 13:45:35 -04:00
|
|
|
#include "qpc.h"
|
2012-08-14 18:07:04 -04:00
|
|
|
#include "bsp.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
Q_DEFINE_THIS_FILE
|
|
|
|
|
|
|
|
/*..........................................................................*/
|
|
|
|
enum ReminderSignals {
|
2015-06-04 22:47:13 -04:00
|
|
|
CRUNCH_SIG = Q_USER_SIG, /* the invented reminder signal */
|
|
|
|
ECHO_SIG, /* check the responsiveness of the system */
|
|
|
|
TERMINATE_SIG /* terminate the application */
|
2012-08-14 18:07:04 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
typedef struct ReminderEvtTag {
|
|
|
|
QEvt super;
|
2015-06-04 22:47:13 -04:00
|
|
|
uint32_t iter; /* the next iteration to perform */
|
2012-08-14 18:07:04 -04:00
|
|
|
} ReminderEvt;
|
|
|
|
|
|
|
|
/*..........................................................................*/
|
2015-06-04 22:47:13 -04:00
|
|
|
typedef struct { /* the Cruncher active object */
|
|
|
|
QActive super; /* inherit QActive */
|
|
|
|
double sum; /* internal variable */
|
2012-08-14 18:07:04 -04:00
|
|
|
} Cruncher;
|
|
|
|
|
|
|
|
void Cruncher_ctor(Cruncher * const me);
|
2015-06-04 22:47:13 -04:00
|
|
|
|
|
|
|
/* hierarchical state machine ... */
|
2012-08-14 18:07:04 -04:00
|
|
|
static QState Cruncher_initial (Cruncher * const me, QEvt const * const e);
|
|
|
|
static QState Cruncher_processing(Cruncher * const me, QEvt const * const e);
|
|
|
|
static QState Cruncher_final (Cruncher * const me, QEvt const * const e);
|
|
|
|
|
|
|
|
/*--------------------------------------------------------------------------*/
|
|
|
|
void Cruncher_ctor(Cruncher * const me) {
|
2015-06-04 22:47:13 -04:00
|
|
|
QActive_ctor(&me->super, Q_STATE_CAST(&Cruncher_initial));
|
2012-08-14 18:07:04 -04:00
|
|
|
}
|
|
|
|
/*..........................................................................*/
|
|
|
|
QState Cruncher_initial(Cruncher * const me, QEvt const * const e) {
|
2015-06-04 22:47:13 -04:00
|
|
|
(void)e; /* unused parameter */
|
2012-08-14 18:07:04 -04:00
|
|
|
return Q_TRAN(&Cruncher_processing);
|
|
|
|
}
|
|
|
|
/*..........................................................................*/
|
|
|
|
QState Cruncher_processing(Cruncher * const me, QEvt const * const e) {
|
|
|
|
QState status;
|
|
|
|
switch (e->sig) {
|
|
|
|
case Q_ENTRY_SIG: {
|
|
|
|
ReminderEvt *reminder = Q_NEW(ReminderEvt, CRUNCH_SIG);
|
2015-06-04 22:47:13 -04:00
|
|
|
reminder->iter = 0U;
|
2013-10-10 19:59:51 -04:00
|
|
|
QACTIVE_POST((QActive *)me, (QEvt const *)reminder, me);
|
2012-08-14 18:07:04 -04:00
|
|
|
me->sum = 0.0;
|
|
|
|
status = Q_HANDLED();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CRUNCH_SIG: {
|
|
|
|
uint32_t i = ((ReminderEvt const *)e)->iter;
|
|
|
|
uint32_t n = i;
|
2015-06-04 22:47:13 -04:00
|
|
|
i += 100U;
|
2012-08-14 18:07:04 -04:00
|
|
|
for (; n < i; ++n) {
|
2015-06-04 22:47:13 -04:00
|
|
|
if ((n & 1U) == 0U) {
|
2012-08-14 18:07:04 -04:00
|
|
|
me->sum += 1.0/(2*n + 1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
me->sum -= 1.0/(2*n + 1);
|
|
|
|
}
|
|
|
|
}
|
2015-06-04 22:47:13 -04:00
|
|
|
if (i < 0x07000000U) {
|
2012-08-14 18:07:04 -04:00
|
|
|
ReminderEvt *reminder = Q_NEW(ReminderEvt, CRUNCH_SIG);
|
|
|
|
reminder->iter = i;
|
2013-10-10 19:59:51 -04:00
|
|
|
QACTIVE_POST((QActive *)me, (QEvt const *)reminder, me);
|
2012-08-14 18:07:04 -04:00
|
|
|
status = Q_HANDLED();
|
|
|
|
}
|
|
|
|
else {
|
2015-09-04 12:08:22 -04:00
|
|
|
printf("pi=%16.14f\n", 4.0*me->sum);
|
2015-06-05 17:05:16 -04:00
|
|
|
fflush(stdout);
|
2012-08-14 18:07:04 -04:00
|
|
|
status = Q_TRAN(&Cruncher_processing);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case ECHO_SIG: {
|
2015-09-04 12:08:22 -04:00
|
|
|
printf("Echo! pi=%16.14f\n", 4.0*me->sum);
|
2015-06-05 17:05:16 -04:00
|
|
|
fflush(stdout);
|
2012-08-14 18:07:04 -04:00
|
|
|
status = Q_HANDLED();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case TERMINATE_SIG: {
|
|
|
|
status = Q_TRAN(&Cruncher_final);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
status = Q_SUPER(&QHsm_top);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
/*..........................................................................*/
|
|
|
|
QState Cruncher_final(Cruncher * const me, QEvt const * const e) {
|
|
|
|
QState status;
|
|
|
|
switch (e->sig) {
|
|
|
|
case Q_ENTRY_SIG: {
|
2015-09-04 12:08:22 -04:00
|
|
|
printf("final-ENTRY;\n");
|
2015-06-05 17:05:16 -04:00
|
|
|
fflush(stdout);
|
2015-06-04 22:47:13 -04:00
|
|
|
QF_stop(); /* terminate the application */
|
2012-08-14 18:07:04 -04:00
|
|
|
status = Q_HANDLED();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
status = Q_SUPER(&QHsm_top);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return status;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* test harness ============================================================*/
|
|
|
|
|
|
|
|
/* Local-scope objects -----------------------------------------------------*/
|
2015-06-04 22:47:13 -04:00
|
|
|
static Cruncher l_cruncher; /* the Cruncher active object */
|
|
|
|
QEvt const *l_cruncherQSto[10]; /* Event queue storage for Cruncher AO */
|
2013-10-10 19:59:51 -04:00
|
|
|
static QF_MPOOL_EL(ReminderEvt) l_smlPoolSto[20]; /* storage for small pool */
|
2012-08-14 18:07:04 -04:00
|
|
|
|
|
|
|
/*..........................................................................*/
|
|
|
|
int main(int argc, char *argv[]) {
|
2015-06-04 22:47:13 -04:00
|
|
|
printf("Reminder state pattern\nQP version: %s\n"
|
2012-08-14 18:07:04 -04:00
|
|
|
"Press 'e' to echo the current value...\n"
|
|
|
|
"Press ESC to quit...\n",
|
2015-09-04 12:08:22 -04:00
|
|
|
QP_versionStr);
|
|
|
|
fflush(stdout);
|
|
|
|
|
2012-08-14 18:07:04 -04:00
|
|
|
Cruncher_ctor(&l_cruncher);
|
|
|
|
|
2015-06-04 22:47:13 -04:00
|
|
|
BSP_init(argc, argv); /* initialize the BSP */
|
2012-08-14 18:07:04 -04:00
|
|
|
|
2015-06-04 22:47:13 -04:00
|
|
|
QF_init();/* initialize the framework and the underlying RT kernel */
|
2012-08-14 18:07:04 -04:00
|
|
|
|
|
|
|
/* publish-subscribe not used, no call to QF_psInit() */
|
|
|
|
|
|
|
|
QF_poolInit(l_smlPoolSto, sizeof(l_smlPoolSto), sizeof(l_smlPoolSto[0]));
|
|
|
|
|
2015-06-04 22:47:13 -04:00
|
|
|
/* instantiate and start the active objects... */
|
|
|
|
QACTIVE_START(&l_cruncher.super,
|
|
|
|
1U,
|
2012-08-14 18:07:04 -04:00
|
|
|
l_cruncherQSto, Q_DIM(l_cruncherQSto),
|
2015-06-04 22:47:13 -04:00
|
|
|
(void *)0, 0U, (QEvt *)0);
|
2012-08-14 18:07:04 -04:00
|
|
|
|
|
|
|
return QF_run(); /* run the QF application */
|
|
|
|
}
|
|
|
|
/*..........................................................................*/
|
|
|
|
void BSP_onKeyboardInput(uint8_t key) {
|
|
|
|
switch (key) {
|
|
|
|
case 'e': {
|
2013-10-10 19:59:51 -04:00
|
|
|
static QEvt const echoEvt = { ECHO_SIG, 0U, 0U };
|
|
|
|
QACTIVE_POST((QActive *)&l_cruncher, &echoEvt, (void *)0);
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case '\033': { /* ESC pressed? */
|
|
|
|
/* NOTE: this constant event is statically pre-allocated.
|
|
|
|
* It can be posted/published as any other event.
|
|
|
|
*/
|
2013-10-10 19:59:51 -04:00
|
|
|
static QEvt const terminateEvt = { TERMINATE_SIG, 0U, 0U };
|
|
|
|
QACTIVE_POST((QActive *)&l_cruncher, &terminateEvt, (void *)0);
|
2012-08-14 18:07:04 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|