Dining Philosopher Problem example
NOTE: Requries QP5
Alarm "Orthogonal Component"
: QMsm(Q_STATE_CAST(&Alarm::initial))
me->m_alarm_time = 12U*60U;
(void)e; // unused parameter
// while in the off state, the alarm is kept in decimal format
me->m_alarm_time = (me->m_alarm_time/60)*100 + me->m_alarm_time%60;
BSP_showTime24H("*** Alarm OFF ", me->m_alarm_time, 100U);
// upon exit, the alarm is converted to binary format
me->m_alarm_time = (me->m_alarm_time/100U)*60U + me->m_alarm_time%100U;
(me->m_alarm_time / 100U < 24U)
&& (me->m_alarm_time % 100U < 60U)
me->m_alarm_time = 0U;
BSP_showTime24H("*** Alarm reset", me->m_alarm_time, 100U);
// while setting, the alarm is kept in decimal format
me->m_alarm_time =
(10U * me->m_alarm_time + Q_EVT_CAST(SetEvt)->digit) % 10000U;
BSP_showTime24H("*** Alarm reset ", me->m_alarm_time, 100U);
BSP_showTime24H("*** Alarm ON ", me->m_alarm_time, 60U);
BSP_showMsg("*** Cannot set Alarm when it is ON");
Q_EVT_CAST(TimeEvt)->current_time == me->m_alarm_time
BSP_showMsg("ALARM!!!");
// asynchronously post the event to the container AO
APP_alarmClock->POST(Q_NEW(QEvt, ALARM_SIG), this);
Alarm clock "Container"
: QMActive(Q_STATE_CAST(&AlarmClock::initial)),
m_alarm(), // orthogonal component ctor
m_timeEvt(this, TICK_SIG, 0U)
(void)e; // unused parameter
me->m_current_time = 0U;
// (!) trigger the initial transition in the component
me->m_alarm.init();
// periodic timeout every second
me->m_timeEvt.armX(BSP_TICKS_PER_SEC, BSP_TICKS_PER_SEC);
me->m_timeEvt.disarm();
BSP_showMsg("Wake up!!!");
// (!) synchronously dispatch to the orthogonal component
me->m_alarm.dispatch(e);
BSP_showMsg("--> final");
BSP_showMsg("*** 24-hour mode");
// roll over in 24-hr mode?
if (++me->m_current_time == 24U*60U) {
me->m_current_time = 0U;
}
BSP_showTime24H("", me->m_current_time, 60U);
TimeEvt pe; // temporary synchronous event for the component
pe.sig = TIME_SIG;
pe.current_time = me->m_current_time;
// (!) synchronously dispatch to the orthogonal component
me->m_alarm.dispatch(&pe);
BSP_showMsg("*** 12-hour mode");
// roll over in 12-hr mode?
if (++me->m_current_time == 12U*60U) {
me->m_current_time = 0U;
}
BSP_showTime12H("", me->m_current_time, 60U);
TimeEvt pe; // temporary synchronous event for the component
pe.sig = TIME_SIG;
pe.current_time = me->m_current_time;
// (!) synchronously dispatch to the orthogonal component
me->m_alarm.dispatch(&pe);
BSP_showMsg("Bye! Bye!");
QF::stop(); // terminate the application
Opaque pointer to the single instance of the AlarmClock AO
#ifndef alarm_h
#define alarm_h
$declare(Components::Alarm)
#endif // alarm_h
#include "qpcpp.h"
#include "bsp.h"
#include "alarm.h"
#include "clock.h"
Q_DEFINE_THIS_FILE
// Alarm component --------------------
$define(Components::Alarm)
#ifndef clock_h
#define clock_h
using namespace QP;
enum AlarmClockSignals {
TICK_SIG = Q_USER_SIG, // time tick event
ALARM_SET_SIG, // set the alarm
ALARM_ON_SIG, // turn the alarm on
ALARM_OFF_SIG, // turn the alarm off
ALARM_SIG, // alarm event from Alarm component to AlarmClock container
CLOCK_12H_SIG, // set the clock in 12H mode
CLOCK_24H_SIG, // set the clock in 24H mode
TIME_SIG, // time event sent to Alarm (contains current time)
TERMINATE_SIG // terminate the application
};
$declare(Events::SetEvt)
$declare(Events::TimeEvt)
$declare(Components::APP_alarmClock)
#endif // clock_h
#include "qpcpp.h"
#include "bsp.h"
#include "alarm.h"
#include "clock.h"
#include <stdio.h>
Q_DEFINE_THIS_FILE
// Active object class -------------------------------------------------------
$declare(Components::AlarmClock)
// Local objects -------------------------------------------------------------
static AlarmClock l_alarmClock; // the single instance of the AO
// Global-scope objects ------------------------------------------------------
QMActive * const APP_alarmClock = &l_alarmClock; // "opaque" AO pointer
//............................................................................
$define(Components::AlarmClock)