Dining Philosopher Problem example
NOTE: Requries QP5.
Alarm "Orthogonal Component"
QMsm_ctor(&me->super, Q_STATE_CAST(&Alarm_initial));
me->alarm_time = 12U*60U;
(void)e; /* avoid compiler warning about 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)e; /* avoid compiler warning about unused parameter */
me->current_time = 0U;
/* (!) trigger the initial transition in the component */
QMSM_INIT((QMsm *)&me->alarm, (QEvt *)0);
/* 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 */
QMSM_DISPATCH((QMsm *)&me->alarm, e);
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 */
QMSM_DISPATCH(&me->alarm.super, &pe.super);
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 */
QMSM_DISPATCH(&me->alarm.super, &pe.super);
QF_stop(); /* terminate the application */
Opaque pointer to the single instance of the AlarmClock AO
AlarmClock * const me = &l_alarmClock;
QMActive_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 <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.super; /* "opaque" pointer */
$define(Components::AlarmClock_ctor)
/*..........................................................................*/
$define(Components::AlarmClock)