qpcpp/examples/win32/reminder/reminder.cpp

209 lines
7.3 KiB
C++
Raw Normal View History

2013-10-10 20:01:51 -04:00
//****************************************************************************
// Product: Reminder state pattern example
2015-06-06 18:30:55 -04:00
// Last Updated for Version: 5.4.2
// Date of the Last Update: 2015-06-05
2013-10-10 20:01:51 -04:00
//
// Q u a n t u m L e a P s
// ---------------------------
// innovating embedded systems
//
// Copyright (C) 2002-2013 Quantum Leaps, LLC. All rights reserved.
//
// 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
2013-10-10 20:01:51 -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-06-06 18:30:55 -04:00
// Web : http://www.state-machine.com
// Email: info@state-machine.com
2013-10-10 20:01:51 -04:00
//****************************************************************************
#include "qf_port.h"
#include "bsp.h"
#include "qassert.h"
#include <stdio.h>
Q_DEFINE_THIS_FILE
using namespace QP;
//............................................................................
enum SensorSignals {
2015-06-06 18:30:55 -04:00
TERMINATE_SIG = Q_USER_SIG, // terminate the application
TIMEOUT_SIG, // the periodic timeout signal
DATA_READY_SIG, // the invented reminder signal
2013-10-10 20:01:51 -04:00
2015-06-06 18:30:55 -04:00
MAX_SIG // keep always last
2013-10-10 20:01:51 -04:00
};
//............................................................................
2015-06-06 18:30:55 -04:00
class Sensor : public QActive { // the Sensor active object
2013-10-10 20:01:51 -04:00
private:
2015-06-06 18:30:55 -04:00
QTimeEvt m_timeEvt; // private time event generator
2013-10-10 20:01:51 -04:00
uint16_t m_pollCtr;
uint16_t m_procCtr;
public:
Sensor()
2015-06-06 18:30:55 -04:00
: QActive(Q_STATE_CAST(&Sensor::initial)),
m_timeEvt(this, TIMEOUT_SIG, 0U)
2013-10-10 20:01:51 -04:00
{}
private:
2015-06-06 18:30:55 -04:00
// hierarchical state machine ...
static QState initial (Sensor * const me, QEvt const * const e);
static QState polling (Sensor * const me, QEvt const * const e);
static QState processing(Sensor * const me, QEvt const * const e);
static QState idle (Sensor * const me, QEvt const * const e);
static QState busy (Sensor * const me, QEvt const * const e);
static QState final (Sensor * const me, QEvt const * const e);
2013-10-10 20:01:51 -04:00
};
// HSM definition ------------------------------------------------------------
2015-06-06 18:30:55 -04:00
QState Sensor::initial(Sensor * const me, QEvt const *) {
2013-10-10 20:01:51 -04:00
me->m_pollCtr = 0;
me->m_procCtr = 0;
// NOTE: don't forget to subscribe to any signals of interest,
// if you're using publish-subscribe...
//
// subscribe(...);
return Q_TRAN(&Sensor::polling);
}
//............................................................................
2015-06-06 18:30:55 -04:00
QState Sensor::final(Sensor * const me, QEvt const * const e) {
2013-10-10 20:01:51 -04:00
switch (e->sig) {
case Q_ENTRY_SIG: {
printf("-> final\n");
2015-06-06 18:30:55 -04:00
QF::stop(); // stop QF and cleanup
2013-10-10 20:01:51 -04:00
return Q_HANDLED();
}
}
return Q_SUPER(&QHsm::top);
}
//............................................................................
2015-06-06 18:30:55 -04:00
QState Sensor::polling(Sensor * const me, QEvt const * const e) {
2013-10-10 20:01:51 -04:00
switch (e->sig) {
case Q_ENTRY_SIG: {
2015-06-06 18:30:55 -04:00
// periodic timeout in 1/2 second and every 1/2 second
me->m_timeEvt.armX(BSP_TICKS_PER_SEC/2, BSP_TICKS_PER_SEC/2);
2013-10-10 20:01:51 -04:00
return Q_HANDLED();
}
case Q_EXIT_SIG: {
me->m_timeEvt.disarm();
return Q_HANDLED();
}
case Q_INIT_SIG: {
return Q_TRAN(&Sensor::processing);
}
case TIMEOUT_SIG: {
// NOTE: this constant event is statically pre-allocated.
// It can be posted/published as any other event.
2015-06-06 18:30:55 -04:00
static const QEvt dataReadyEvt = { DATA_READY_SIG, 0U, 0U };
2013-10-10 20:01:51 -04:00
++me->m_pollCtr;
printf("poll %3d\n", me->m_pollCtr);
2015-06-06 18:30:55 -04:00
if ((me->m_pollCtr & 0x3U) == 0U) { // modulo 4
2013-10-10 20:01:51 -04:00
me->POST(&dataReadyEvt, me);
}
return Q_HANDLED();
}
case TERMINATE_SIG: {
return Q_TRAN(&Sensor::final);
}
}
return Q_SUPER(&QHsm::top);
}
//............................................................................
2015-06-06 18:30:55 -04:00
QState Sensor::processing(Sensor * const me, QEvt const * const e) {
2013-10-10 20:01:51 -04:00
switch (e->sig) {
case Q_INIT_SIG: {
return Q_TRAN(&Sensor::idle);
}
}
return Q_SUPER(&Sensor::polling);
}
//............................................................................
2015-06-06 18:30:55 -04:00
QState Sensor::idle(Sensor * const me, QEvt const * const e) {
2013-10-10 20:01:51 -04:00
switch (e->sig) {
case Q_ENTRY_SIG: {
printf("-> idle\n");
return Q_HANDLED();
}
case DATA_READY_SIG: {
return Q_TRAN(&Sensor::busy);
}
}
return Q_SUPER(&Sensor::processing);
}
//............................................................................
2015-06-06 18:30:55 -04:00
QState Sensor::busy(Sensor * const me, QEvt const * const e) {
2013-10-10 20:01:51 -04:00
switch (e->sig) {
case Q_ENTRY_SIG: {
printf("-> busy\n");
return Q_HANDLED();
}
case TIMEOUT_SIG: {
++me->m_procCtr;
printf("process %3d\n", me->m_procCtr);
2015-06-06 18:30:55 -04:00
if ((me->m_procCtr & 0x1U) == 0U) { // modulo 2
2013-10-10 20:01:51 -04:00
return Q_TRAN(&Sensor::idle);
}
return Q_HANDLED();
}
}
return Q_SUPER(&Sensor::processing);
}
// test harness ==============================================================
// Local-scope objects -------------------------------------------------------
2015-06-06 18:30:55 -04:00
static Sensor l_sensor; // the Sensor active object
static QEvt const *l_sensorQSto[10]; // Event queue storage for Sensor
2013-10-10 20:01:51 -04:00
static void *l_regPoolSto[100/sizeof(void *)]; // 100 bytes for the event pool
//............................................................................
int main(int argc, char *argv[]) {
2015-06-06 18:30:55 -04:00
printf("Reminder state pattern\nQP version: %s\n"
2013-10-10 20:01:51 -04:00
"Press ESC to quit...\n",
2015-06-06 18:30:55 -04:00
QP::versionStr);
2013-10-10 20:01:51 -04:00
2015-06-06 18:30:55 -04:00
BSP_init(argc, argv); // initialize the BSP
2013-10-10 20:01:51 -04:00
2015-06-06 18:30:55 -04:00
QF::init(); // initialize the framework and the underlying RT kernel
2013-10-10 20:01:51 -04:00
// publish-subscribe not used, no call to QF::psInit()
2015-06-06 18:30:55 -04:00
// initialize event pools...
2013-10-10 20:01:51 -04:00
QF::poolInit(l_regPoolSto, sizeof(l_regPoolSto), sizeof(QEvt));
2015-06-06 18:30:55 -04:00
// start the active objects...
l_sensor.start(1U, l_sensorQSto, Q_DIM(l_sensorQSto),
2013-10-10 20:01:51 -04:00
(void *)0, 0, (QEvt *)0);
2015-06-06 18:30:55 -04:00
return QF::run(); // run the QF application
2013-10-10 20:01:51 -04:00
}
//............................................................................
void BSP_onKeyboardInput(uint8_t key) {
switch (key) {
case '\33': { // ESC pressed?
l_sensor.POST(Q_NEW(QEvt, TERMINATE_SIG), (void *)0);
break;
}
}
}