⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alarm.c

📁 事件驱动程序设计很好的框架
💻 C
字号:
/****************************************************************************** Product: Orthogonal Component state pattern example* Last Updated for Version: 4.0.00* Date of the Last Update:  Apr 07, 2008**                    Q u a n t u m     L e a P s*                    ---------------------------*                    innovating embedded systems** Copyright (C) 2002-2008 Quantum Leaps, LLC. All rights reserved.** This software may be distributed and modified under the terms of the GNU* General Public License version 2 (GPL) as published by the Free Software* Foundation and appearing in the file GPL.TXT included in the packaging of* this file. Please note that GPL Section 2[b] requires that all works based* on this software must also be made publicly available under the terms of* the GPL ("Copyleft").** Alternatively, this software may be distributed and modified under the* terms of Quantum Leaps commercial licenses, which expressly supersede* the GPL and are specifically designed for licensees interested in* retaining the proprietary status of their code.** Contact information:* Quantum Leaps Web site:  http://www.quantum-leaps.com* e-mail:                  info@quantum-leaps.com*****************************************************************************/#include "qp_port.h"#include "bsp.h"#include "alarm.h"#include "clock.h"#include <stdio.h>Q_DEFINE_THIS_FILE/* FSM state-handler functions */static QState Alarm_initial(Alarm *me, QEvent const *e);static QState Alarm_off    (Alarm *me, QEvent const *e);static QState Alarm_on     (Alarm *me, QEvent const *e);/*..........................................................................*/void Alarm_ctor(Alarm *me) {    QFsm_ctor(&me->super, (QStateHandler)&Alarm_initial);}/* HSM definition ----------------------------------------------------------*/QState Alarm_initial(Alarm *me, QEvent const *e) {    (void)e;               /* avoid compiler warning about unused parameter */    me->alarm_time = 12*60;    return Q_TRAN(&Alarm_off);}/*..........................................................................*/QState Alarm_off(Alarm *me, QEvent const *e) {    switch (e->sig) {        case Q_ENTRY_SIG: {             /* while in the off state, the alarm is kept in decimal format */            me->alarm_time = (me->alarm_time/60)*100 + me->alarm_time%60;            printf("*** Alarm OFF %02ld:%02ld\n",                   me->alarm_time/100, me->alarm_time%100);            return Q_HANDLED();        }        case Q_EXIT_SIG: {                      /* upon exit, the alarm is converted to binary format */            me->alarm_time = (me->alarm_time/100)*60 + me->alarm_time%100;            return Q_HANDLED();        }        case ALARM_ON_SIG: {            return Q_TRAN(&Alarm_on);        }        case ALARM_SET_SIG: {                      /* while setting, the alarm is kept in decimal format */            uint32_t alarm = (10 * me->alarm_time                              + ((SetEvt const *)e)->digit) % 10000;            if ((alarm / 100 < 24) && (alarm % 100 < 60)) {/*alarm in range?*/                me->alarm_time = alarm;            }            else {                      /* alarm out of range -- start over */                me->alarm_time = 0;            }            printf("*** Alarm SET %02ld:%02ld\n",                   me->alarm_time/100, me->alarm_time%100);            return Q_HANDLED();        }    }    return Q_IGNORED();}/*..........................................................................*/QState Alarm_on(Alarm *me, QEvent const *e) {    switch (e->sig) {        case Q_ENTRY_SIG: {            printf("*** Alarm ON %02ld:%02ld\n",                   me->alarm_time/60, me->alarm_time%60);            return Q_HANDLED();        }        case ALARM_SET_SIG: {            printf("*** Cannot set Alarm when it is ON\n");            return Q_HANDLED();        }        case ALARM_OFF_SIG: {            return Q_TRAN(&Alarm_off);        }        case TIME_SIG: {            if (((TimeEvt *)e)->current_time == me->alarm_time) {                printf("ALARM!!!\n");                       /* asynchronously post the event to the container AO */                QActive_postFIFO(APP_alarmClock, Q_NEW(QEvent, ALARM_SIG));            }            return Q_HANDLED();        }    }    return Q_IGNORED();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -