mirror of
https://github.com/QuantumLeaps/qpcpp.git
synced 2025-01-28 06:02:56 +08:00
179 lines
6.2 KiB
C++
179 lines
6.2 KiB
C++
|
//****************************************************************************
|
||
|
// Model: comp.qm
|
||
|
// File: ./alarm.cpp
|
||
|
//
|
||
|
// This code has been generated by QM tool (see state-machine.com/qm).
|
||
|
// DO NOT EDIT THIS FILE MANUALLY. All your changes will be lost.
|
||
|
//
|
||
|
// 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.
|
||
|
//****************************************************************************
|
||
|
//${.::alarm.cpp} ............................................................
|
||
|
#include "qpcpp.h"
|
||
|
#include "bsp.h"
|
||
|
#include "alarm.h"
|
||
|
#include "clock.h"
|
||
|
|
||
|
Q_DEFINE_THIS_FILE
|
||
|
|
||
|
// Alarm component --------------------
|
||
|
//${Components::Alarm} .......................................................
|
||
|
//${Components::Alarm::Alarm} ................................................
|
||
|
Alarm::Alarm()
|
||
|
: QMsm(Q_STATE_CAST(&Alarm::initial))
|
||
|
{}
|
||
|
|
||
|
//${Components::Alarm::SM} ...................................................
|
||
|
QP::QState Alarm::initial(Alarm * const me, QP::QEvt const * const e) {
|
||
|
static struct {
|
||
|
QP::QMState const *target;
|
||
|
QP::QActionHandler act[2];
|
||
|
} const tatbl_ = { // transition-action table
|
||
|
&off_s,
|
||
|
{
|
||
|
Q_ACTION_CAST(&off_e), // entry
|
||
|
Q_ACTION_CAST(0) // zero terminator
|
||
|
}
|
||
|
};
|
||
|
// ${Components::Alarm::SM::initial}
|
||
|
me->m_alarm_time = 12U*60U;
|
||
|
(void)e; // unused parameter
|
||
|
return QM_TRAN_INIT(&tatbl_);
|
||
|
}
|
||
|
//${Components::Alarm::SM::off} ..............................................
|
||
|
QP::QMState const Alarm::off_s = {
|
||
|
static_cast<QP::QMState const *>(0), // superstate (top)
|
||
|
Q_STATE_CAST(&off),
|
||
|
Q_ACTION_CAST(&off_e),
|
||
|
Q_ACTION_CAST(&off_x),
|
||
|
Q_ACTION_CAST(0) // no intitial tran.
|
||
|
};
|
||
|
// ${Components::Alarm::SM::off}
|
||
|
QP::QState Alarm::off_e(Alarm * const me) {
|
||
|
// 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);
|
||
|
return QM_ENTRY(&off_s);
|
||
|
}
|
||
|
// ${Components::Alarm::SM::off}
|
||
|
QP::QState Alarm::off_x(Alarm * const me) {
|
||
|
// upon exit, the alarm is converted to binary format
|
||
|
me->m_alarm_time = (me->m_alarm_time/100U)*60U + me->m_alarm_time%100U;
|
||
|
return QM_EXIT(&off_s);
|
||
|
}
|
||
|
// ${Components::Alarm::SM::off}
|
||
|
QP::QState Alarm::off(Alarm * const me, QP::QEvt const * const e) {
|
||
|
QP::QState status_;
|
||
|
switch (e->sig) {
|
||
|
// ${Components::Alarm::SM::off::ALARM_ON}
|
||
|
case ALARM_ON_SIG: {
|
||
|
// ${Components::Alarm::SM::off::ALARM_ON::[alarminrange?]}
|
||
|
if ((me->m_alarm_time / 100U < 24U)
|
||
|
&& (me->m_alarm_time % 100U < 60U))
|
||
|
{
|
||
|
static struct {
|
||
|
QP::QMState const *target;
|
||
|
QP::QActionHandler act[3];
|
||
|
} const tatbl_ = { // transition-action table
|
||
|
&on_s,
|
||
|
{
|
||
|
Q_ACTION_CAST(&off_x), // exit
|
||
|
Q_ACTION_CAST(&on_e), // entry
|
||
|
Q_ACTION_CAST(0) // zero terminator
|
||
|
}
|
||
|
};
|
||
|
status_ = QM_TRAN(&tatbl_);
|
||
|
}
|
||
|
// ${Components::Alarm::SM::off::ALARM_ON::[else]}
|
||
|
else {
|
||
|
me->m_alarm_time = 0U;
|
||
|
BSP_showTime24H("*** Alarm reset", me->m_alarm_time, 100U);
|
||
|
status_ = QM_HANDLED();
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
// ${Components::Alarm::SM::off::ALARM_SET}
|
||
|
case ALARM_SET_SIG: {
|
||
|
// 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);
|
||
|
status_ = QM_HANDLED();
|
||
|
break;
|
||
|
}
|
||
|
default: {
|
||
|
status_ = QM_SUPER();
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
return status_;
|
||
|
}
|
||
|
//${Components::Alarm::SM::on} ...............................................
|
||
|
QP::QMState const Alarm::on_s = {
|
||
|
static_cast<QP::QMState const *>(0), // superstate (top)
|
||
|
Q_STATE_CAST(&on),
|
||
|
Q_ACTION_CAST(&on_e),
|
||
|
Q_ACTION_CAST(0), // no exit action
|
||
|
Q_ACTION_CAST(0) // no intitial tran.
|
||
|
};
|
||
|
// ${Components::Alarm::SM::on}
|
||
|
QP::QState Alarm::on_e(Alarm * const me) {
|
||
|
BSP_showTime24H("*** Alarm ON ", me->m_alarm_time, 60U);
|
||
|
return QM_ENTRY(&on_s);
|
||
|
}
|
||
|
// ${Components::Alarm::SM::on}
|
||
|
QP::QState Alarm::on(Alarm * const me, QP::QEvt const * const e) {
|
||
|
QP::QState status_;
|
||
|
switch (e->sig) {
|
||
|
// ${Components::Alarm::SM::on::ALARM_OFF}
|
||
|
case ALARM_OFF_SIG: {
|
||
|
static struct {
|
||
|
QP::QMState const *target;
|
||
|
QP::QActionHandler act[2];
|
||
|
} const tatbl_ = { // transition-action table
|
||
|
&off_s,
|
||
|
{
|
||
|
Q_ACTION_CAST(&off_e), // entry
|
||
|
Q_ACTION_CAST(0) // zero terminator
|
||
|
}
|
||
|
};
|
||
|
status_ = QM_TRAN(&tatbl_);
|
||
|
break;
|
||
|
}
|
||
|
// ${Components::Alarm::SM::on::ALARM_SET}
|
||
|
case ALARM_SET_SIG: {
|
||
|
BSP_showMsg("*** Cannot set Alarm when it is ON");
|
||
|
status_ = QM_HANDLED();
|
||
|
break;
|
||
|
}
|
||
|
// ${Components::Alarm::SM::on::TIME}
|
||
|
case TIME_SIG: {
|
||
|
// ${Components::Alarm::SM::on::TIME::[Q_EVT_CAST(Time~}
|
||
|
if (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);
|
||
|
status_ = QM_HANDLED();
|
||
|
}
|
||
|
else {
|
||
|
status_ = QM_UNHANDLED();
|
||
|
}
|
||
|
break;
|
||
|
}
|
||
|
default: {
|
||
|
status_ = QM_SUPER();
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
(void)me; /* avoid compiler warning in case 'me' is not used */
|
||
|
return status_;
|
||
|
}
|
||
|
|