예제 #1
0
파일: hook.c 프로젝트: lihulab/statemachine
void main() {
    printf("Ultimate Hook pattern\nQEP version: %s\n"
           "Press 'a'..'c' to inject signals A..C\n"
           "Press 'd' or ESC to inject signal D and quit\n",
           QEP_getVersion());

    UltimateHook_ctor(&l_test);      /* instantiate the UltimateHook object */

                /* trigger the initial transition before dispatching events */
    QHsm_init((QHsm *)&l_test, (QEvt *)0);

    for (;;) {
        QEvt e;
        uint8_t c;

        printf("\n");
        c = (uint8_t)getch();        /* read one character from the console */
        printf("%c: ", c);

        switch (c) {
            case 'a':  e.sig = A_SIG;  break;
            case 'b':  e.sig = B_SIG;  break;
            case 'c':  e.sig = C_SIG;  break;
            case 'd':
            case 0x1B: e.sig = D_SIG;  break;                  /* terminate */
        }
                               /* dispatch the event into the state machine */
        QHsm_dispatch((QHsm *)&l_test, &e);
    }
}
int main() {
    printf("History state pattern\nQEP version: %s\n"
           "Press 'o' to OPEN  the door\n"
           "Press 'c' to CLOSE the door\n"
           "Press 't' to start TOASTING\n"
           "Press 'b' to start BAKING\n"
           "Press 'f' to turn the oven OFF\n"
           "Press ESC to quit...\n",
           QEP_getVersion());

      /* instantiate the ToastOven HSM and trigger the initial transition */
    ToastOven_ctor(&l_test);
    QHsm_init((QHsm *)&l_test, (QEvt *)0);

    for (;;) {
        QEvt e;
        uint8_t c;

        printf("\n");
        c = (uint8_t)_getch();       /* read one character from the console */
        printf("%c: ", c);

        switch (c) {
            case 'o':  e.sig = OPEN_SIG;        break;
            case 'c':  e.sig = CLOSE_SIG;       break;
            case 't':  e.sig = TOAST_SIG;       break;
            case 'b':  e.sig = BAKE_SIG;        break;
            case 'f':  e.sig = OFF_SIG;         break;
            case 0x1B: e.sig = TERMINATE_SIG;   break;
        }
                               /* dispatch the event into the state machine */
        QHsm_dispatch((QHsm *)&l_test,  &e);
    }
    return 0;
}
예제 #3
0
파일: comp.c 프로젝트: westlicht/camcontrol
/*..........................................................................*/
QState AlarmClock_mode12hr(AlarmClock *me) {
    switch (Q_SIG(me)) {
        case Q_ENTRY_SIG: {
            printf("*** 12-hour mode\n");
            return Q_HANDLED();
        }
        case Q_TIMEOUT_SIG: {
            uint32_t h;                  /* temporary variable to hold hour */
                                                   /* timeout in one second */
            QActive_arm((QActive *)me, BSP_TICKS_PER_SEC);

            if (++me->currentTime == 12*60) {   /* roll over in 12-hr mode? */
                me->currentTime = 0;
            }
            h = me->currentTime/60;
            printf("%02ld:%02ld %s\n", (h % 12) ? (h % 12) : 12,
                   me->currentTime % 60, (h / 12) ? "PM" : "AM");

                      /* synchronously dispatch to the orthogonal component */
            Q_SIG(&me->alarmComp) = TIME_SIG;
            Q_PAR(&me->alarmComp) = me->currentTime;
            QHsm_dispatch((QHsm *)&me->alarmComp);
            return Q_HANDLED();
        }
    }
    return Q_SUPER(&AlarmClock_timekeeping);
}
예제 #4
0
파일: comp.c 프로젝트: westlicht/camcontrol
/*..........................................................................*/
QState AlarmClock_mode24hr(AlarmClock *me) {
    switch (Q_SIG(me)) {
        case Q_ENTRY_SIG: {
            printf("*** 24-hour mode\n");
            return Q_HANDLED();
        }
        case Q_TIMEOUT_SIG: {
                                                   /* timeout in one second */
            QActive_arm((QActive *)me, BSP_TICKS_PER_SEC);

            if (++me->currentTime == 24*60) {   /* roll over in 24-hr mode? */
                me->currentTime = 0;
            }
            printf("%02ld:%02ld\n",
                   me->currentTime/60, me->currentTime%60);

                      /* synchronously dispatch to the orthogonal component */
            Q_SIG(&me->alarmComp) = TIME_SIG;
            Q_PAR(&me->alarmComp) = me->currentTime;
            QHsm_dispatch((QHsm *)&me->alarmComp);
            return Q_HANDLED();
        }
    }
    return Q_SUPER(&AlarmClock_timekeeping);
}
예제 #5
0
파일: charging.c 프로젝트: evchar/charging
void QHsmMsgProcess(void)
{
  uint8 uch_Msg = 0;
  Tag *a = &Tag_;
  if (xQueueReceive(msgQueue, &uch_Msg, portMAX_DELAY) == pdTRUE)
  {
    if(uch_Msg == KEY0_UP)
    {
      g_charging_en = true;
      uch_Msg = Q_START_SIG;
    }
    if(uch_Msg == KEY1_UP)
    {
      g_charging_en = false;
      uch_Msg = Q_STOP_SIG;
    }
    Q_SIG(a) = uch_Msg;
    QHsm_dispatch(&a->super);
  }
}
예제 #6
0
파일: comp.c 프로젝트: westlicht/camcontrol
/*..........................................................................*/
QState AlarmClock_timekeeping(AlarmClock *me) {
    switch (Q_SIG(me)) {
        case Q_ENTRY_SIG: {
                                                   /* timeout in one second */
            QActive_arm((QActive *)me, BSP_TICKS_PER_SEC);
            return Q_HANDLED();
        }
        case Q_EXIT_SIG: {
            QActive_disarm((QActive *)me);
            return Q_HANDLED();
        }
        case Q_INIT_SIG: {
            return Q_TRAN(&AlarmClock_mode24hr);
        }
        case CLOCK_12H_SIG: {
            return Q_TRAN(&AlarmClock_mode12hr);
        }
        case CLOCK_24H_SIG: {
            return Q_TRAN(&AlarmClock_mode24hr);
        }
        case ALARM_SIG: {
            printf("Wake up!!!\n");
            return Q_HANDLED();
        }
        case ALARM_SET_SIG:
        case ALARM_ON_SIG:
        case ALARM_OFF_SIG: {
                      /* synchronously dispatch to the orthogonal component */
            Q_SIG(&me->alarmComp) = Q_SIG(me);
            Q_PAR(&me->alarmComp) = Q_PAR(me);
            QHsm_dispatch((QHsm *)&me->alarmComp);
            return Q_HANDLED();
        }
        case TERMINATE_SIG: {
            return Q_TRAN(&AlarmClock_final);
        }
    }
    return Q_SUPER(&QHsm_top);
}
예제 #7
0
/*..........................................................................*/
int main(int argc, char *argv[]) {
    QHsmTst_ctor();                       /* instantiate the QHsmTst object */

    if (argc > 1) {                                  /* file name provided? */
        l_outFile = fopen(argv[1], "w");
    }

    if (l_outFile == (FILE *)0) {                   /* interactive version? */
        l_outFile = stdout;            /* use the stdout as the output file */

        printf("QHsmTst example, built on %s at %s,\n"
               "QP-nano: %s.\nPress ESC to quit...\n",
               __DATE__, __TIME__, QP_getVersion());

        QHsm_init(the_hsm);                   /* the top-most initial tran. */

        for (;;) {                                            /* event loop */
            int c;

            printf("\n>");
            c = _getche();    /* get a character from the console with echo */
            printf(": ");

            if ('a' <= c && c <= 'i') {                        /* in range? */
                Q_SIG(the_hsm) = (QSignal)(c - 'a' + A_SIG);
            }
            else if ('A' <= c && c <= 'I') {                   /* in range? */
                Q_SIG(the_hsm) = (QSignal)(c - 'A' + A_SIG);
            }
            else if (c == '\33') {                          /* the ESC key? */
                Q_SIG(the_hsm) = TERMINATE_SIG;
            }
            else {
                Q_SIG(the_hsm) = IGNORE_SIG;
            }

            QHsm_dispatch(the_hsm);                   /* dispatch the event */
        }
    }
    else {                                                 /* batch version */
        printf("QHsmTst example, built on %s at %s, QP-nano %s\n"
               "output saved to %s\n",
               __DATE__, __TIME__, QP_getVersion(),
               argv[1]);

        fprintf(l_outFile, "QHsmTst example, QP-nano %s\n",
                QP_getVersion());

        QHsm_init(the_hsm);                 /* take the initial transitioin */

                                       /* testing of dynamic transitions... */
        dispatch(A_SIG);
        dispatch(B_SIG);
        dispatch(D_SIG);
        dispatch(E_SIG);
        dispatch(I_SIG);
        dispatch(F_SIG);
        dispatch(I_SIG);
        dispatch(I_SIG);
        dispatch(F_SIG);
        dispatch(A_SIG);
        dispatch(B_SIG);
        dispatch(D_SIG);
        dispatch(D_SIG);
        dispatch(E_SIG);
        dispatch(G_SIG);
        dispatch(H_SIG);
        dispatch(H_SIG);
        dispatch(C_SIG);
        dispatch(G_SIG);
        dispatch(C_SIG);
        dispatch(C_SIG);

        fclose(l_outFile);
    }

    return 0;
}
예제 #8
0
/*..........................................................................*/
static void dispatch(QSignal sig) {
    Q_REQUIRE((A_SIG <= sig) && (sig <= I_SIG));
    fprintf(l_outFile, "\n%c:", 'A' + sig - A_SIG);
    Q_SIG(the_hsm) = sig;
    QHsm_dispatch(the_hsm);                            /* dispatch the event */
}
예제 #9
0
/*****************************************************************************
* Product: Calculator Example with inheritance of whole state model
* Last Updated for Version: 4.5.00
* Date of the Last Update:  May 18, 2012
*
*                    Q u a n t u m     L e a P s
*                    ---------------------------
*                    innovating embedded systems
*
* Copyright (C) 2002-2012 Quantum Leaps, LLC. All rights reserved.
*
* 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, either version 3 of the License, or
* (at your option) any later version.
*
* Alternatively, this program may be distributed and modified under the
* terms of Quantum Leaps commercial licenses, which expressly supersede
* the GNU General Public License and are specifically designed for
* licensees interested in retaining the proprietary status of their code.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Contact information:
* Quantum Leaps Web sites: http://www.quantum-leaps.com
*                          http://www.state-machine.com
* e-mail:                  [email protected]
*****************************************************************************/
#include "qp_port.h"                        /* the port of the QP framework */
#include "bsp.h"                                   /* board support package */
#include "calc1.h"

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <dos.h>

// Local objects -----------------------------------------------------------*/
static Calc1 l_calc;                             /* instantiate Calculator1 */

//..........................................................................*/
void main() {

    Calc1_ctor(&l_calc);    /* explicitly instantiate the calculator object */

    printf("Calculator example, QEP version: %s\n"
           "Press '0' .. '9'     to enter a digit\n"
           "Press '.'            to enter the decimal point\n"
           "Press '+' or '#'     to add\n"
           "Press '-'            to subtract or negate a number\n"
           "Press '*'            to multiply\n"
           "Press '/'            to divide\n"
           "Press '=' or <Enter> to get the result\n"
           "Press 'c' or 'C'     to Cancel\n"
           "Press 'e' or 'E'     to Cancel Entry\n"
           "Press <Esc>          to quit.\n\n",
           QEP_getVersion());

    QHsm_init((QHsm *)&l_calc, (QEvt *)0);  /* trigger initial transition */

    for (;;) {                                                /* event loop */
        CalcEvt e;                                      /* Calculator event */

        BSP_display();                                  /* show the display */

        e.key_code = (uint8_t)getche();             /* get a char with echo */
        printf(": ");

        switch (e.key_code) {
            case 'c':                         /* intentionally fall through */
            case 'C': {
                ((QEvt *)&e)->sig = C_SIG;
                break;
            }
            case 'e':                         /* intentionally fall through */
            case 'E': {
                ((QEvt *)&e)->sig = CE_SIG;
                break;
            }
            case '0': {
                ((QEvt *)&e)->sig = DIGIT_0_SIG;
                break;
            }
            case '1':                         /* intentionally fall through */
            case '2':                         /* intentionally fall through */
            case '3':                         /* intentionally fall through */
            case '4':                         /* intentionally fall through */
            case '5':                         /* intentionally fall through */
            case '6':                         /* intentionally fall through */
            case '7':                         /* intentionally fall through */
            case '8':                         /* intentionally fall through */
            case '9': {
                ((QEvt *)&e)->sig = DIGIT_1_9_SIG;
                break;
            }
            case '.': {
                ((QEvt *)&e)->sig = POINT_SIG;
                break;
            }
            case '+':                         /* intentionally fall through */
            case '-':                         /* intentionally fall through */
            case '*':                         /* intentionally fall through */
            case '/': {
                ((QEvt *)&e)->sig = OPER_SIG;
                break;
            }
            case '#': {                                  /* alternative '+' */
                ((QEvt *)&e)->sig = OPER_SIG;
                e.key_code = '+';
                break;
            }
            case '=':                         /* intentionally fall through */
            case '\r': {                                       /* Enter key */
                ((QEvt *)&e)->sig = EQUALS_SIG;
                break;
            }
            case '\33': {                                        /* ESC key */
                ((QEvt *)&e)->sig = OFF_SIG;
                break;
            }
            default: {
                ((QEvt *)&e)->sig = 0;                   /* invalid event */
                break;
            }
        }

        if (((QEvt *)&e)->sig != 0) {           /* valid event generated? */
            QHsm_dispatch((QHsm *)&l_calc, (QEvt *)&e); /* dispatch event */
        }
    }
}
/*..........................................................................*/
QState UI_num_sd_handler(UI_num_sd *me, QEvt const *e) {
    switch (e->sig) {
        case Q_ENTRY_SIG: {
            uint8_t c;
            static char const * const help_sd[] = {
                "Standard Deviation:         ",
                "Find the mean value and the ",
                " root-mean-square (RMS)     ",
                "deviation of n data samples ",
                "xi, where i = 1..n.         ",
                "Mean value <x> is calculated",
                "as follows:                 ",
                "<x> = Sum(xi)/n;            ",
                "Two RMS estimatators are    ",
                "provided:                   ",
                "sig(n) =                    ",
                "   sqrt(Sum(xi-<x>)**2 / n);",
                "sig(n-1) =                  ",
                "sqrt(Sum(xi-<x>)**2 / (n-1))"
            };
                                     /* instantiate the state-local objects */
            me->super.super.help_text = help_sd;
            me->super.super.help_len  = Q_DIM(help_sd);
            me->n      = 0.0;
            me->sum    = 0.0;
            me->sum_sq = 0.0;

            Video_printStrAt(2, 10, VIDEO_FGND_BLACK,
                "Screen 1: Standard Deviation     ");
            Video_clearRect( 0, 11, 35, 23, VIDEO_BGND_BLUE);
            Video_clearRect(35, 11, 80, 23, VIDEO_BGND_BLACK);

            c = VIDEO_FGND_LIGHT_GRAY;
            Video_printStrAt(36, 12, c,
                "Press '-'        to enter a negative number");
            Video_printStrAt(36, 13, c,
                "Press '0' .. '9' to enter a digit");
            Video_printStrAt(36, 14, c,
                "Press '.'        to enter the decimal point");
            Video_printStrAt(36, 15, c,
                "Press <Enter>    to enter the data sample");
            Video_printStrAt(36, 16, c,
                "Press 'e' or 'E' to Cancel last entry");
            Video_printStrAt(36, 17, c,
                "Press 'c' or 'C' to Cancel the data set");

            c = VIDEO_FGND_WHITE;
            Video_printStrAt(36, 20, c,
                "Press UP-arrow   for previous screen");
            Video_printStrAt(36, 21, c,
                "Press DOWN-arrow for next screen");
            Video_printStrAt(36, 22, c,
                "Press F1         for help");

            Video_clearRect(NUM_ENTRY_X, NUM_ENTRY_Y,
                NUM_ENTRY_X + NUM_STR_WIDTH, NUM_ENTRY_Y + 1,
                VIDEO_BGND_BLACK);
            Video_drawRect(NUM_ENTRY_X - 1, NUM_ENTRY_Y - 1,
                NUM_ENTRY_X + NUM_STR_WIDTH + 1, NUM_ENTRY_Y + 2,
                VIDEO_FGND_WHITE, 2);

            NumEntry_config(&me->super.num_entry, NUM_ENTRY_X, NUM_ENTRY_Y,
                            NUM_ENTRY_COLOR);
            QHsm_dispatch((QHsm *)&me->super.num_entry, &l_clear_evt);

            c = VIDEO_FGND_WHITE;                                 /* labels */
            Video_printStrAt(NUM_ENTRY_X - 1, NUM_ENTRY_Y + 4, c,
                 "n        =");
            Video_printStrAt(NUM_ENTRY_X - 1, NUM_ENTRY_Y + 5, c,
                 "<x>      =");
            Video_printStrAt(NUM_ENTRY_X - 1, NUM_ENTRY_Y + 6, c,
                "sig(n)   =");
            Video_printStrAt(NUM_ENTRY_X - 1, NUM_ENTRY_Y + 7, c,
                "sig(n-1) =");

            c = VIDEO_FGND_YELLOW;                                /* values */
            Video_printStrAt(NUM_ENTRY_X + 10, NUM_ENTRY_Y + 4, c,
                "0           ");
            Video_printStrAt(NUM_ENTRY_X + 10, NUM_ENTRY_Y + 5, c,
                "N/A         ");
            Video_printStrAt(NUM_ENTRY_X + 10, NUM_ENTRY_Y + 6, c,
                "N/A         ");
            Video_printStrAt(NUM_ENTRY_X + 10, NUM_ENTRY_Y + 7, c,
                "N/A         ");

            return Q_HANDLED();
        }
        case Q_EXIT_SIG: {
                                      /* destroy the state-local objects... */
            /* noting to destroy */
            return Q_HANDLED();
        }
        case C_SIG: {
            return Q_TRAN(&UI_num_sd_handler);        /* transition-to-self */
        }
        case CE_SIG: {
            QHsm_dispatch((QHsm *)&me->super.num_entry, &l_clear_evt);
            return Q_HANDLED();
        }
        case UP_SIG: {
            return Q_TRAN(&UI_num_lr_handler);   /* Liner Regression screen */
        }
        case DOWN_SIG: {
            return Q_TRAN(&UI_num_lr_handler);   /* Liner Regression screen */
        }
        case NEG_SIG:
        case DIGIT_0_SIG:
        case DIGIT_1_9_SIG:
        case POINT_SIG: {
            QHsm_dispatch((QHsm *)&me->super.num_entry, e);
            return Q_HANDLED();
        }
        case ENTER_SIG: {
            double tmp = NumEntry_get(&me->super.num_entry);
            char buf[14];

            me->n      += 1.0;
            me->sum    += tmp;
            me->sum_sq += tmp*tmp;

            sprintf(buf, "%-12.6g", me->n);
            Video_printStrAt(NUM_ENTRY_X + 10, NUM_ENTRY_Y + 4,
                              VIDEO_FGND_YELLOW, buf);

            tmp = me->sum / me->n;                                   /* <x> */
            sprintf(buf, "%-12.6g", tmp);
            Video_printStrAt(NUM_ENTRY_X + 10, NUM_ENTRY_Y + 5,
                              VIDEO_FGND_YELLOW, buf);

            tmp = me->sum_sq / me->n - tmp*tmp;
            if (tmp >= 0.0) {                                   /* sigma(n) */
                tmp = sqrt(tmp);
                sprintf(buf, "%-12.6g", tmp);
                Video_printStrAt(NUM_ENTRY_X + 10, NUM_ENTRY_Y + 6,
                                  VIDEO_FGND_YELLOW, buf);
                if (me->n > 1.0) {                            /* sigma(n-1) */
                     tmp *= sqrt(me->n/(me->n - 1.0));
                     sprintf(buf, "%-12.6g", tmp);
                     Video_printStrAt(NUM_ENTRY_X + 10, NUM_ENTRY_Y + 7,
                                       VIDEO_FGND_YELLOW, buf);
                }
            }
            QHsm_dispatch((QHsm *)&me->super.num_entry, &l_clear_evt);
            return Q_HANDLED();
        }
    }
    return Q_SUPER(&UI_num_handler);
}