Calc2 is the model of the Calculator described in Chapter 4 of PSiCC2 and extended with the % calculations Calculator state machine BSP_clear(); BSP_message("on-ENTRY;"); BSP_message("on-EXIT;"); BSP_message("on-INIT;"); BSP_clear(); BSP_message("error-ENTRY;"); BSP_message("error-EXIT;"); BSP_message("negated1-ENTRY;"); BSP_negate(); BSP_message("negated1-EXIT;"); BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); else Q_EVT_CAST(CalcEvt)->key_code == KEY_MINUS ; BSP_clear(); BSP_message("ready-ENTRY;"); BSP_message("ready-EXIT;"); BSP_message("ready-INIT;"); BSP_clear(); BSP_clear(); BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); BSP_clear(); BSP_insert((int)'0'); BSP_insert((int)'.'); me->operand1 = BSP_get_value(); me->operator = Q_EVT_CAST(CalcEvt)->key_code; BSP_message("result-ENTRY;"); BSP_message("result-EXIT;"); BSP_message("begin-ENTRY;"); BSP_message("begin-EXIT;"); Q_EVT_CAST(CalcEvt)->key_code == KEY_MINUS else BSP_message("operand1-ENTRY;"); BSP_message("operand1-EXIT;"); BSP_clear(); me->operand1 = BSP_get_value(); me->operator = Q_EVT_CAST(CalcEvt)->key_code; BSP_message("zero1-ENTRY;"); BSP_message("zero1-EXIT;"); ; BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); BSP_insert((int)'0'); BSP_insert((int)'.'); BSP_message("int1-ENTRY;"); BSP_message("int1-EXIT;"); BSP_insert((int)'.'); BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); BSP_message("frac1-ENTRY;"); BSP_message("frac1-EXIT;"); ; BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); BSP_message("opEntered-ENTRY;"); BSP_message("opEntered-EXIT;"); BSP_clear(); BSP_clear(); BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); BSP_clear(); BSP_insert((int)'0'); BSP_insert((int)'.'); Q_EVT_CAST(CalcEvt)->key_code == KEY_MINUS else BSP_message("negated2-ENTRY;"); BSP_negate(); BSP_message("negated2-EXIT;"); BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); else Q_EVT_CAST(CalcEvt)->key_code == KEY_MINUS ; BSP_clear(); BSP_message("operand2-ENTRY;"); BSP_message("operand2-EXIT;"); BSP_clear(); else BSP_eval(me->operand1, me->operator, BSP_get_value()) else BSP_eval(me->operand1, me->operator, BSP_get_value()) double operand2 = 0.0; switch (me->operator) { case KEY_PLUS: case KEY_MINUS: { operand2 = (me->operand1 * BSP_get_value())/100.0; break; } case KEY_MULT: case KEY_DIVIDE: { operand2 = BSP_get_value()/100.0; break; } } else BSP_eval(me->operand1, me->operator, operand2) BSP_message("zero2-ENTRY;"); BSP_message("zero2-EXIT;"); ; BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); BSP_insert((int)'0'); BSP_insert((int)'.'); BSP_message("int2-ENTRY;"); BSP_message("int2-EXIT;"); BSP_insert((int)'.'); BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); BSP_message("frac2-ENTRY;"); BSP_message("frac2-EXIT;"); ; BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); BSP_message("final-ENTRY;"); BSP_exit(); constructor Calc2 *me = &l_calc2; QMsm_ctor(&me->super, Q_STATE_CAST(&Calc2_initial)); #ifndef calc2_h #define calc2_h enum CalcSignals { C_SIG = Q_USER_SIG, CE_SIG, DIGIT_0_SIG, DIGIT_1_9_SIG, POINT_SIG, OPER_SIG, EQUALS_SIG, PERCENT_SIG, OFF_SIG }; $declare(Events::CalcEvt) $declare(SMs::Calc2_ctor) extern QMsm * const the_calc; /* "opaque" pointer to calculator MSM */ #endif /* calc2_h */ #include "qep_port.h" /* QEP/C interface */ #include "bsp.h" /* board support package */ #include "calc2.h" $declare(SMs::Calc2) static Calc2 l_calc2; /* the only instance of the Calc class */ /* global-scope definitions ---------------------------------------*/ QMsm * const the_calc = &l_calc2.super; /* "opaque" pointer to MSM */ $define(SMs::Calc2_ctor) $define(SMs::Calc2)