Dining Philosopher Problem example
NOTE: Requries QP6.
Alarm "Orthogonal Component"
QHsm_ctor(&me->super, Q_STATE_CAST(&Alarm_initial));
me->alarm_time = 12U*60U;
(void)par; /* unused parameter */
/* while in the off state, the alarm is kept in decimal format */
me->alarm_time = (me->alarm_time/60)*100 + me->alarm_time%60;
BSP_showTime24H("*** Alarm OFF ", me->alarm_time, 100U);
/* upon exit, the alarm is converted to binary format */
me->alarm_time = (me->alarm_time/100U)*60U + me->alarm_time%100U;
(me->alarm_time / 100U < 24U)
&& (me->alarm_time % 100U < 60U)
me->alarm_time = 0U;
BSP_showTime24H("*** Alarm reset", me->alarm_time, 100U);
/* while setting, the alarm is kept in decimal format */
me->alarm_time = (10U * me->alarm_time
+ ((SetEvt const *)e)->digit) % 10000U;
BSP_showTime24H("*** Alarm reset ", me->alarm_time, 100U);
BSP_showTime24H("*** Alarm ON ", me->alarm_time, 60U);
BSP_showMsg("*** Cannot set Alarm when it is ON");
Q_EVT_CAST(TimeEvt)->current_time == me->alarm_time
BSP_showMsg("ALARM!!!");
/* asynchronously post the event to the container AO */
QACTIVE_POST(APP_alarmClock, Q_NEW(QEvt, ALARM_SIG), me);
Alarm clock "Container"
(void)par; /* unused parameter */
me->current_time = 0U;
/* (!) trigger the initial transition in the component */
QHSM_INIT((QHsm *)&me->alarm, (void *)0, me->super.prio);
/* periodic timeout every second */
QTimeEvt_armX(&me->timeEvt, BSP_TICKS_PER_SEC,
BSP_TICKS_PER_SEC);
QTimeEvt_disarm(&me->timeEvt);
BSP_showMsg("Wake up!!!");
/* (!) synchronously dispatch to the orthogonal component */
QHSM_DISPATCH((QHsm *)&me->alarm, e, me->super.prio);
BSP_showMsg("--> final");
BSP_showMsg("*** 24-hour mode");
TimeEvt pe; /* temporary synchronous event for the component */
/* roll over in 24-hr mode? */
if (++me->current_time == 24U*60U) {
me->current_time = 0U;
}
BSP_showTime24H("", me->current_time, 60U);
pe.super.sig = TIME_SIG;
pe.current_time = me->current_time;
/* (!) synchronously dispatch to the orthogonal component */
QHSM_DISPATCH(&me->alarm.super, &pe.super, me->super.prio);
BSP_showMsg("*** 12-hour mode");
TimeEvt pe; /* temporary synchronous event for the component */
/* roll over in 12-hr mode? */
if (++me->current_time == 12U*60U) {
me->current_time = 0U;
}
BSP_showTime12H("", me->current_time, 60U);
pe.super.sig = TIME_SIG;
pe.current_time = me->current_time;
/* (!) synchronously dispatch to the orthogonal component */
QHSM_DISPATCH(&me->alarm.super, &pe.super, me->super.prio);
QF_stop(); /* terminate the application */
Opaque pointer to the single inst of the AlarmClock AO
= &l_alarmClock.super;
AlarmClock * const me = &l_alarmClock;
QActive_ctor(&me->super, Q_STATE_CAST(&AlarmClock_initial));
Alarm_ctor(&me->alarm); /* orthogonal component ctor */
/* private time event ctor */
QTimeEvt_ctorX(&me->timeEvt, &me->super, TICK_SIG, 0U);
#ifndef ALARM_H
#define ALARM_H
$declare${Components::Alarm}
$declare${Components::Alarm::ctor}
#endif /* ALARM_H */
#include "qpc.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
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}
$declare${Components::AlarmClock_ctor}
#endif /* CLOCK_H */
#include "qpc.h"
#include "bsp.h"
#include "alarm.h"
#include "clock.h"
#include "safe_std.h" /* portable "safe" <stdio.h>/<string.h> facilities */
Q_DEFINE_THIS_FILE
/* Active object class -----------------------------------------------------*/
$declare${Components::AlarmClock}
/* Local objects -----------------------------------------------------------*/
static AlarmClock l_alarmClock; /* the single inst of the AO */
/* Global-scope objects ----------------------------------------------------*/
$define${Components::APP_alarmClock}
$define${Components::AlarmClock_ctor}
/*..........................................................................*/
$define${Components::AlarmClock}