311 lines
10 KiB
C++
Raw Normal View History

2019-12-31 15:56:23 -05:00
//.$file${.::table.cpp} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2018-06-25 16:44:34 -04:00
//
// Model: dpp.qm
2018-06-25 16:44:34 -04:00
// File: ${.::table.cpp}
//
2019-12-31 15:56:23 -05:00
// This code has been generated by QM 4.6.0 <www.state-machine.com/qm/>.
2013-10-10 20:01:51 -04:00
// DO NOT EDIT THIS FILE MANUALLY. All your changes will be lost.
//
2013-10-10 20:01:51 -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.
//
// 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.
2018-06-25 16:44:34 -04:00
//
2019-12-31 15:56:23 -05:00
//.$endhead${.::table.cpp} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2019-10-27 12:26:31 -04:00
#include "qpcpp.hpp"
#include "dpp.hpp"
#include "bsp.hpp"
Q_DEFINE_THIS_FILE
// Active object class -------------------------------------------------------
2019-12-31 15:56:23 -05:00
//.$declare${AOs::Table} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2013-12-30 17:41:15 -05:00
namespace DPP {
2019-12-31 15:56:23 -05:00
//.${AOs::Table} .............................................................
2016-12-01 10:31:49 -05:00
class Table : public QP::QActive {
private:
uint8_t m_fork[N_PHILO];
bool m_isHungry[N_PHILO];
public:
Table();
protected:
2019-04-16 22:11:29 -04:00
Q_STATE_DECL(initial);
Q_STATE_DECL(active);
Q_STATE_DECL(serving);
Q_STATE_DECL(paused);
};
2013-12-30 17:41:15 -05:00
} // namespace DPP
2019-12-31 15:56:23 -05:00
//.$enddecl${AOs::Table} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2013-12-30 17:41:15 -05:00
namespace DPP {
// helper function to provide the RIGHT neighbour of a Philo[n]
inline uint8_t RIGHT(uint8_t const n) {
return static_cast<uint8_t>((n + (N_PHILO - 1U)) % N_PHILO);
}
// helper function to provide the LEFT neighbour of a Philo[n]
inline uint8_t LEFT(uint8_t const n) {
return static_cast<uint8_t>((n + 1U) % N_PHILO);
}
static uint8_t const FREE = static_cast<uint8_t>(0);
static uint8_t const USED = static_cast<uint8_t>(1);
static char_t const * const THINKING = &"thinking"[0];
static char_t const * const HUNGRY = &"hungry "[0];
static char_t const * const EATING = &"eating "[0];
// Local objects -------------------------------------------------------------
static Table l_table; // the single instance of the Table active object
// Global-scope objects ------------------------------------------------------
2016-12-01 10:31:49 -05:00
QP::QActive * const AO_Table = &l_table; // "opaque" AO pointer
2013-12-30 17:41:15 -05:00
} // namespace DPP
//............................................................................
2019-12-31 15:56:23 -05:00
//.$skip${QP_VERSION} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
//. Check for the minimum required QP version
2019-04-16 22:11:29 -04:00
#if (QP_VERSION < 650U) || (QP_VERSION != ((QP_RELEASE^4294967295U) % 0x3E8U))
#error qpcpp version 6.5.0 or higher required
2018-06-25 16:44:34 -04:00
#endif
2019-12-31 15:56:23 -05:00
//.$endskip${QP_VERSION} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//.$define${AOs::Table} vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
2013-12-30 17:41:15 -05:00
namespace DPP {
2019-12-31 15:56:23 -05:00
//.${AOs::Table} .............................................................
//.${AOs::Table::Table} ......................................................
2013-12-30 17:41:15 -05:00
Table::Table()
2016-12-01 10:31:49 -05:00
: QActive(Q_STATE_CAST(&Table::initial))
{
for (uint8_t n = 0U; n < N_PHILO; ++n) {
m_fork[n] = FREE;
m_isHungry[n] = false;
}
}
2019-04-16 22:11:29 -04:00
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM} .........................................................
2019-04-16 22:11:29 -04:00
Q_STATE_DEF(Table, initial) {
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::initial}
(void)e; // suppress the compiler warning about unused parameter
2019-04-16 22:11:29 -04:00
subscribe(DONE_SIG);
subscribe(PAUSE_SIG);
subscribe(SERVE_SIG);
subscribe(TEST_SIG);
for (uint8_t n = 0U; n < N_PHILO; ++n) {
2019-04-16 22:11:29 -04:00
m_fork[n] = FREE;
m_isHungry[n] = false;
2016-12-01 10:31:49 -05:00
BSP::displayPhilStat(n, THINKING);
}
2018-06-25 16:44:34 -04:00
// global signals...
QS_SIG_DICTIONARY(DONE_SIG, (void *)0);
QS_SIG_DICTIONARY(EAT_SIG, (void *)0);
QS_SIG_DICTIONARY(PAUSE_SIG, (void *)0);
QS_SIG_DICTIONARY(SERVE_SIG, (void *)0);
QS_SIG_DICTIONARY(TEST_SIG, (void *)0);
// signals just for this AO...
2019-04-16 22:11:29 -04:00
QS_SIG_DICTIONARY(HUNGRY_SIG, this);
2018-06-25 16:44:34 -04:00
QS_FUN_DICTIONARY(&active);
QS_FUN_DICTIONARY(&serving);
QS_FUN_DICTIONARY(&paused);
2019-04-16 22:11:29 -04:00
return tran(&serving);
}
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active} .................................................
2019-04-16 22:11:29 -04:00
Q_STATE_DEF(Table, active) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
switch (e->sig) {
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::TEST}
2016-12-01 10:31:49 -05:00
case TEST_SIG: {
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::EAT}
case EAT_SIG: {
Q_ERROR();
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
break;
}
default: {
2019-04-16 22:11:29 -04:00
status_ = super(&top);
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::serving} ........................................
2019-04-16 22:11:29 -04:00
Q_STATE_DEF(Table, serving) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
switch (e->sig) {
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::serving}
2016-12-01 10:31:49 -05:00
case Q_ENTRY_SIG: {
for (uint8_t n = 0U; n < N_PHILO; ++n) { // give permissions to eat...
2019-04-16 22:11:29 -04:00
if (m_isHungry[n]
&& (m_fork[LEFT(n)] == FREE)
&& (m_fork[n] == FREE))
2016-12-01 10:31:49 -05:00
{
2019-04-16 22:11:29 -04:00
m_fork[LEFT(n)] = USED;
m_fork[n] = USED;
2016-12-01 10:31:49 -05:00
TableEvt *te = Q_NEW(TableEvt, EAT_SIG);
te->philoNum = n;
2019-04-16 22:11:29 -04:00
QP::QF::PUBLISH(te, this);
m_isHungry[n] = false;
2016-12-01 10:31:49 -05:00
BSP::displayPhilStat(n, EATING);
}
}
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
2016-12-01 10:31:49 -05:00
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::serving::HUNGRY}
case HUNGRY_SIG: {
uint8_t n = Q_EVT_CAST(TableEvt)->philoNum;
// phil ID must be in range and he must be not hungry
2019-04-16 22:11:29 -04:00
Q_ASSERT((n < N_PHILO) && (!m_isHungry[n]));
2016-12-01 10:31:49 -05:00
BSP::displayPhilStat(n, HUNGRY);
uint8_t m = LEFT(n);
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::serving::HUNGRY::[bothfree]}
2019-04-16 22:11:29 -04:00
if ((m_fork[m] == FREE) && (m_fork[n] == FREE)) {
m_fork[m] = USED;
m_fork[n] = USED;
TableEvt *pe = Q_NEW(TableEvt, EAT_SIG);
pe->philoNum = n;
2019-04-16 22:11:29 -04:00
QP::QF::PUBLISH(pe, this);
2016-12-01 10:31:49 -05:00
BSP::displayPhilStat(n, EATING);
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::serving::HUNGRY::[else]}
else {
2019-04-16 22:11:29 -04:00
m_isHungry[n] = true;
status_ = Q_RET_HANDLED;
}
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::serving::DONE}
case DONE_SIG: {
uint8_t n = Q_EVT_CAST(TableEvt)->philoNum;
// phil ID must be in range and he must be not hungry
2019-04-16 22:11:29 -04:00
Q_ASSERT((n < N_PHILO) && (!m_isHungry[n]));
2016-12-01 10:31:49 -05:00
BSP::displayPhilStat(n, THINKING);
uint8_t m = LEFT(n);
// both forks of Phil[n] must be used
2019-04-16 22:11:29 -04:00
Q_ASSERT((m_fork[n] == USED) && (m_fork[m] == USED));
2019-04-16 22:11:29 -04:00
m_fork[m] = FREE;
m_fork[n] = FREE;
m = RIGHT(n); // check the right neighbor
2019-04-16 22:11:29 -04:00
if (m_isHungry[m] && (m_fork[m] == FREE)) {
m_fork[n] = USED;
m_fork[m] = USED;
m_isHungry[m] = false;
TableEvt *pe = Q_NEW(TableEvt, EAT_SIG);
pe->philoNum = m;
2019-04-16 22:11:29 -04:00
QP::QF::PUBLISH(pe, this);
2016-12-01 10:31:49 -05:00
BSP::displayPhilStat(m, EATING);
}
m = LEFT(n); // check the left neighbor
n = LEFT(m); // left fork of the left neighbor
2019-04-16 22:11:29 -04:00
if (m_isHungry[m] && (m_fork[n] == FREE)) {
m_fork[m] = USED;
m_fork[n] = USED;
m_isHungry[m] = false;
TableEvt *pe = Q_NEW(TableEvt, EAT_SIG);
pe->philoNum = m;
2019-04-16 22:11:29 -04:00
QP::QF::PUBLISH(pe, this);
2016-12-01 10:31:49 -05:00
BSP::displayPhilStat(m, EATING);
}
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::serving::EAT}
case EAT_SIG: {
Q_ERROR();
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::serving::PAUSE}
case PAUSE_SIG: {
2019-04-16 22:11:29 -04:00
status_ = tran(&paused);
break;
}
default: {
2019-04-16 22:11:29 -04:00
status_ = super(&active);
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::paused} .........................................
2019-04-16 22:11:29 -04:00
Q_STATE_DEF(Table, paused) {
2013-10-10 20:01:51 -04:00
QP::QState status_;
switch (e->sig) {
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::paused}
2016-12-01 10:31:49 -05:00
case Q_ENTRY_SIG: {
BSP::displayPaused(1U);
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
2016-12-01 10:31:49 -05:00
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::paused}
2016-12-01 10:31:49 -05:00
case Q_EXIT_SIG: {
BSP::displayPaused(0U);
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
2016-12-01 10:31:49 -05:00
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::paused::SERVE}
2015-05-14 16:05:04 -04:00
case SERVE_SIG: {
2019-04-16 22:11:29 -04:00
status_ = tran(&serving);
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::paused::HUNGRY}
case HUNGRY_SIG: {
uint8_t n = Q_EVT_CAST(TableEvt)->philoNum;
// philo ID must be in range and he must be not hungry
2019-04-16 22:11:29 -04:00
Q_ASSERT((n < N_PHILO) && (!m_isHungry[n]));
m_isHungry[n] = true;
2016-12-01 10:31:49 -05:00
BSP::displayPhilStat(n, HUNGRY);
2019-04-16 22:11:29 -04:00
status_ = Q_RET_HANDLED;
break;
}
2019-12-31 15:56:23 -05:00
//.${AOs::Table::SM::active::paused::DONE}
case DONE_SIG: {
uint8_t n = Q_EVT_CAST(TableEvt)->philoNum;
// phil ID must be in range and he must be not hungry
2019-04-16 22:11:29 -04:00
Q_ASSERT((n < N_PHILO) && (!m_isHungry[n]));
2016-12-01 10:31:49 -05:00
BSP::displayPhilStat(n, THINKING);
uint8_t m = LEFT(n);
/* both forks of Phil[n] must be used */
2019-04-16 22:11:29 -04:00
Q_ASSERT((m_fork[n] == USED) && (m_fork[m] == USED));
2019-04-16 22:11:29 -04:00
m_fork[m] = FREE;
m_fork[n] = FREE;
status_ = Q_RET_HANDLED;
break;
}
default: {
2019-04-16 22:11:29 -04:00
status_ = super(&active);
break;
}
}
2013-10-10 20:01:51 -04:00
return status_;
}
2013-12-30 17:41:15 -05:00
} // namespace DPP
2019-12-31 15:56:23 -05:00
//.$enddef${AOs::Table} ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^