/*..........................................................................*/ QState Calc_ready(Calc * const me, QEvt const * const e) { QState status; switch (e->sig) { case Q_ENTRY_SIG: { BSP_message("ready-ENTRY;"); status = Q_HANDLED(); break; } case Q_EXIT_SIG: { BSP_message("ready-EXIT;"); status = Q_HANDLED(); break; } case Q_INIT_SIG: { BSP_message("ready-INIT;"); status = Q_TRAN(&Calc_begin); break; } case DIGIT_0_SIG: { BSP_clear(); status = Q_TRAN(&Calc_zero1); break; } case DIGIT_1_9_SIG: { BSP_clear(); BSP_insert(((CalcEvt const *)e)->key_code); status = Q_TRAN(&Calc_int1); break; } case POINT_SIG: { BSP_clear(); BSP_insert((int)'0'); BSP_insert((int)'.'); status = Q_TRAN(&Calc_frac1); break; } case OPER_SIG: { me->operand1 = BSP_get_value(); me->operator = ((CalcEvt const *)e)->key_code; status = Q_TRAN(&Calc_opEntered); break; } default: { status = Q_SUPER(&Calc_on); break; } } return status; }
/*..........................................................................*/ QState Calc_on(Calc * const me, QEvt const * const e) { QState status; switch (e->sig) { case Q_ENTRY_SIG: { BSP_message("on-ENTRY;"); status = Q_HANDLED(); break; } case Q_EXIT_SIG: { BSP_message("on-EXIT;"); status = Q_HANDLED(); break; } case Q_INIT_SIG: { BSP_message("on-INIT;"); status = Q_TRAN(&Calc_ready); break; } case C_SIG: { BSP_clear(); status = Q_TRAN(&Calc_on); /* transition-to-self */ break; } case OFF_SIG: { status = Q_TRAN(&Calc_final); break; } default: { status = Q_SUPER(&QHsm_top); break; } } return status; }
/*..........................................................................*/ QState Calc_operand1(Calc * const me, QEvt const * const e) { QState status; switch (e->sig) { case Q_ENTRY_SIG: { BSP_message("operand1-ENTRY;"); status = Q_HANDLED(); break; } case Q_EXIT_SIG: { BSP_message("operand1-EXIT;"); status = Q_HANDLED(); break; } case CE_SIG: { BSP_clear(); status = Q_TRAN(&Calc_begin); break; } case OPER_SIG: { me->operand1 = BSP_get_value(); me->operator = ((CalcEvt const *)e)->key_code; status = Q_TRAN(&Calc_opEntered); break; } case EQUALS_SIG: { status = Q_TRAN(&Calc_result); break; } default: { status = Q_SUPER(&Calc_on); break; } } return status; }
/*..........................................................................*/ QState Calc_opEntered(Calc * const me, QEvt const * const e) { QState status; switch (e->sig) { case Q_ENTRY_SIG: { BSP_message("opEntered-ENTRY;"); status = Q_HANDLED(); break; } case Q_EXIT_SIG: { BSP_message("opEntered-EXIT;"); status = Q_HANDLED(); break; } case OPER_SIG: { if (((CalcEvt const *)e)->key_code == KEY_MINUS) { BSP_clear(); status = Q_TRAN(&Calc_negated2); } break; } case DIGIT_0_SIG: { BSP_clear(); status = Q_TRAN(&Calc_zero2); break; } case DIGIT_1_9_SIG: { BSP_clear(); BSP_insert(((CalcEvt const *)e)->key_code); status = Q_TRAN(&Calc_int2); break; } case POINT_SIG: { BSP_clear(); BSP_insert((int)'0'); BSP_insert((int)'.'); status = Q_TRAN(&Calc_frac2); } default: { status = Q_SUPER(&Calc_on); break; } } return status; }
/*..........................................................................*/ QState Calc_negated1(Calc * const me, QEvt const * const e) { QState status; switch (e->sig) { case Q_ENTRY_SIG: { BSP_message("negated1-ENTRY;"); BSP_negate(); status = Q_HANDLED(); break; } case Q_EXIT_SIG: { BSP_message("negated1-EXIT;"); status = Q_HANDLED(); break; } case OPER_SIG: { if (((CalcEvt const *)e)->key_code == KEY_MINUS) { ; /* explicitly ignore */ status = Q_HANDLED(); /* event handled */ } else { status = Q_HANDLED(); } break; } case CE_SIG: { BSP_clear(); status = Q_TRAN(&Calc_begin); break; } case DIGIT_0_SIG: { BSP_insert(((CalcEvt const *)e)->key_code); status = Q_TRAN(&Calc_zero1); break; } case DIGIT_1_9_SIG: { BSP_insert(((CalcEvt const *)e)->key_code); status = Q_TRAN(&Calc_int1); break; } case POINT_SIG: { BSP_insert(((CalcEvt const *)e)->key_code); status = Q_TRAN(&Calc_frac1); break; } default: { status = Q_SUPER(&Calc_on); break; } } return status; }
/*${SMs::Calc::SM} .........................................................*/ static QState Calc_initial(Calc * const me, QEvt const * const e) { static struct { QMState const *target; QActionHandler act[3]; } const tatbl_ = { /* transition-action table */ &Calc_on_s, /* target state */ { Q_ACTION_CAST(&Calc_on_e), /* entry */ Q_ACTION_CAST(&Calc_on_i), /* init.tran. */ Q_ACTION_CAST(0) /* zero terminator */ } }; /* ${SMs::Calc::SM::initial} */ BSP_clear(); (void)e; /* unused parameter */ return QM_TRAN_INIT(&tatbl_); }
/* ${SMs::Calc::SM::on} */ static QState Calc_on(Calc * const me, QEvt const * const e) { QState status_; switch (e->sig) { /* ${SMs::Calc::SM::on::C} */ case C_SIG: { static struct { QMState const *target; QActionHandler act[4]; } const tatbl_ = { /* transition-action table */ &Calc_on_s, /* target state */ { Q_ACTION_CAST(&Calc_on_x), /* exit */ Q_ACTION_CAST(&Calc_on_e), /* entry */ Q_ACTION_CAST(&Calc_on_i), /* init.tran. */ Q_ACTION_CAST(0) /* zero terminator */ } }; BSP_clear(); status_ = QM_TRAN(&tatbl_); break; } /* ${SMs::Calc::SM::on::OFF} */ case OFF_SIG: { static struct { QMState const *target; QActionHandler act[3]; } const tatbl_ = { /* transition-action table */ &Calc_final_s, /* target state */ { Q_ACTION_CAST(&Calc_on_x), /* exit */ Q_ACTION_CAST(&Calc_final_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; status_ = QM_TRAN(&tatbl_); break; } default: { status_ = QM_SUPER(); break; } } return status_; }
/*..........................................................................*/ void BSP_negate(void) { BSP_clear(); l_display[DISP_WIDTH - 2] = '-'; }
/* ${SMs::Calc::SM::on::negated2} */ static QState Calc_negated2(Calc * const me, QEvt const * const e) { QState status_; switch (e->sig) { /* ${SMs::Calc::SM::on::negated2::DIGIT_0} */ case DIGIT_0_SIG: { static struct { QMState const *target; QActionHandler act[4]; } const tatbl_ = { /* transition-action table */ &Calc_zero2_s, /* target state */ { Q_ACTION_CAST(&Calc_negated2_x), /* exit */ Q_ACTION_CAST(&Calc_operand2_e), /* entry */ Q_ACTION_CAST(&Calc_zero2_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); status_ = QM_TRAN(&tatbl_); break; } /* ${SMs::Calc::SM::on::negated2::DIGIT_1_9} */ case DIGIT_1_9_SIG: { static struct { QMState const *target; QActionHandler act[4]; } const tatbl_ = { /* transition-action table */ &Calc_int2_s, /* target state */ { Q_ACTION_CAST(&Calc_negated2_x), /* exit */ Q_ACTION_CAST(&Calc_operand2_e), /* entry */ Q_ACTION_CAST(&Calc_int2_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); status_ = QM_TRAN(&tatbl_); break; } /* ${SMs::Calc::SM::on::negated2::POINT} */ case POINT_SIG: { static struct { QMState const *target; QActionHandler act[4]; } const tatbl_ = { /* transition-action table */ &Calc_frac2_s, /* target state */ { Q_ACTION_CAST(&Calc_negated2_x), /* exit */ Q_ACTION_CAST(&Calc_operand2_e), /* entry */ Q_ACTION_CAST(&Calc_frac2_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); status_ = QM_TRAN(&tatbl_); break; } /* ${SMs::Calc::SM::on::negated2::OPER} */ case OPER_SIG: { /* ${SMs::Calc::SM::on::negated2::OPER::[e->key=='-']} */ if (Q_EVT_CAST(CalcEvt)->key_code == KEY_MINUS) { ; status_ = QM_HANDLED(); } /* ${SMs::Calc::SM::on::negated2::OPER::[else]} */ else { status_ = QM_HANDLED(); } break; } /* ${SMs::Calc::SM::on::negated2::CE} */ case CE_SIG: { static struct { QMState const *target; QActionHandler act[3]; } const tatbl_ = { /* transition-action table */ &Calc_opEntered_s, /* target state */ { Q_ACTION_CAST(&Calc_negated2_x), /* exit */ Q_ACTION_CAST(&Calc_opEntered_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; BSP_clear(); status_ = QM_TRAN(&tatbl_); break; } default: { status_ = QM_SUPER(); break; } } return status_; }
/* ${SMs::Calc::SM::on::operand1} */ static QState Calc_operand1(Calc * const me, QEvt const * const e) { QState status_; switch (e->sig) { /* ${SMs::Calc::SM::on::operand1::CE} */ case CE_SIG: { static struct { QMState const *target; QActionHandler act[4]; } const tatbl_ = { /* transition-action table */ &Calc_begin_s, /* target state */ { Q_ACTION_CAST(&Calc_operand1_x), /* exit */ Q_ACTION_CAST(&Calc_ready_e), /* entry */ Q_ACTION_CAST(&Calc_begin_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; BSP_clear(); status_ = QM_TRAN(&tatbl_); break; } /* ${SMs::Calc::SM::on::operand1::OPER} */ case OPER_SIG: { static struct { QMState const *target; QActionHandler act[3]; } const tatbl_ = { /* transition-action table */ &Calc_opEntered_s, /* target state */ { Q_ACTION_CAST(&Calc_operand1_x), /* exit */ Q_ACTION_CAST(&Calc_opEntered_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; me->operand1 = BSP_get_value(); me->operator = Q_EVT_CAST(CalcEvt)->key_code; status_ = QM_TRAN(&tatbl_); break; } /* ${SMs::Calc::SM::on::operand1::EQUALS} */ case EQUALS_SIG: { static struct { QMState const *target; QActionHandler act[4]; } const tatbl_ = { /* transition-action table */ &Calc_result_s, /* target state */ { Q_ACTION_CAST(&Calc_operand1_x), /* exit */ Q_ACTION_CAST(&Calc_ready_e), /* entry */ Q_ACTION_CAST(&Calc_result_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; status_ = QM_TRAN(&tatbl_); break; } default: { status_ = QM_SUPER(); break; } } return status_; }
/* ${SMs::Calc::SM::on::ready} */ static QState Calc_ready(Calc * const me, QEvt const * const e) { QState status_; switch (e->sig) { /* ${SMs::Calc::SM::on::ready::DIGIT_0} */ case DIGIT_0_SIG: { static struct { QMState const *target; QActionHandler act[4]; } const tatbl_ = { /* transition-action table */ &Calc_zero1_s, /* target state */ { Q_ACTION_CAST(&Calc_ready_x), /* exit */ Q_ACTION_CAST(&Calc_operand1_e), /* entry */ Q_ACTION_CAST(&Calc_zero1_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; BSP_clear(); status_ = QM_TRAN(&tatbl_); break; } /* ${SMs::Calc::SM::on::ready::DIGIT_1_9} */ case DIGIT_1_9_SIG: { static struct { QMState const *target; QActionHandler act[4]; } const tatbl_ = { /* transition-action table */ &Calc_int1_s, /* target state */ { Q_ACTION_CAST(&Calc_ready_x), /* exit */ Q_ACTION_CAST(&Calc_operand1_e), /* entry */ Q_ACTION_CAST(&Calc_int1_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; BSP_clear(); BSP_insert(Q_EVT_CAST(CalcEvt)->key_code); status_ = QM_TRAN(&tatbl_); break; } /* ${SMs::Calc::SM::on::ready::POINT} */ case POINT_SIG: { static struct { QMState const *target; QActionHandler act[4]; } const tatbl_ = { /* transition-action table */ &Calc_frac1_s, /* target state */ { Q_ACTION_CAST(&Calc_ready_x), /* exit */ Q_ACTION_CAST(&Calc_operand1_e), /* entry */ Q_ACTION_CAST(&Calc_frac1_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; BSP_clear(); BSP_insert((int)'0'); BSP_insert((int)'.'); status_ = QM_TRAN(&tatbl_); break; } /* ${SMs::Calc::SM::on::ready::OPER} */ case OPER_SIG: { static struct { QMState const *target; QActionHandler act[3]; } const tatbl_ = { /* transition-action table */ &Calc_opEntered_s, /* target state */ { Q_ACTION_CAST(&Calc_ready_x), /* exit */ Q_ACTION_CAST(&Calc_opEntered_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; me->operand1 = BSP_get_value(); me->operator = Q_EVT_CAST(CalcEvt)->key_code; status_ = QM_TRAN(&tatbl_); break; } default: { status_ = QM_SUPER(); break; } } return status_; }
/* ${SMs::Calc::SM::on::operand2} */ static QState Calc_operand2(Calc * const me, QEvt const * const e) { QState status_; switch (e->sig) { /* ${SMs::Calc::SM::on::operand2::CE} */ case CE_SIG: { static struct { QMState const *target; QActionHandler act[3]; } const tatbl_ = { /* transition-action table */ &Calc_opEntered_s, /* target state */ { Q_ACTION_CAST(&Calc_operand2_x), /* exit */ Q_ACTION_CAST(&Calc_opEntered_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; BSP_clear(); status_ = QM_TRAN(&tatbl_); break; } /* ${SMs::Calc::SM::on::operand2::EQUALS} */ case EQUALS_SIG: { /* ${SMs::Calc::SM::on::operand2::EQUALS::[BSP_eval()]} */ if (BSP_eval(me->operand1, me->operator, BSP_get_value())) { static struct { QMState const *target; QActionHandler act[4]; } const tatbl_ = { /* transition-action table */ &Calc_result_s, /* target state */ { Q_ACTION_CAST(&Calc_operand2_x), /* exit */ Q_ACTION_CAST(&Calc_ready_e), /* entry */ Q_ACTION_CAST(&Calc_result_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; status_ = QM_TRAN(&tatbl_); } /* ${SMs::Calc::SM::on::operand2::EQUALS::[else]} */ else { static struct { QMState const *target; QActionHandler act[3]; } const tatbl_ = { /* transition-action table */ &Calc_error_s, /* target state */ { Q_ACTION_CAST(&Calc_operand2_x), /* exit */ Q_ACTION_CAST(&Calc_error_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; status_ = QM_TRAN(&tatbl_); } break; } /* ${SMs::Calc::SM::on::operand2::OPER} */ case OPER_SIG: { /* ${SMs::Calc::SM::on::operand2::OPER::[BSP_eval()]} */ if (BSP_eval(me->operand1, me->operator, BSP_get_value())) { static struct { QMState const *target; QActionHandler act[3]; } const tatbl_ = { /* transition-action table */ &Calc_opEntered_s, /* target state */ { Q_ACTION_CAST(&Calc_operand2_x), /* exit */ Q_ACTION_CAST(&Calc_opEntered_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; status_ = QM_TRAN(&tatbl_); } /* ${SMs::Calc::SM::on::operand2::OPER::[else]} */ else { static struct { QMState const *target; QActionHandler act[3]; } const tatbl_ = { /* transition-action table */ &Calc_error_s, /* target state */ { Q_ACTION_CAST(&Calc_operand2_x), /* exit */ Q_ACTION_CAST(&Calc_error_e), /* entry */ Q_ACTION_CAST(0) /* zero terminator */ } }; status_ = QM_TRAN(&tatbl_); } break; } default: { status_ = QM_SUPER(); break; } } return status_; }
/* HSM definition ----------------------------------------------------------*/ QState Calc_initial(Calc * const me, QEvt const * const e) { (void)e; /* avoid the "unreferenced parameter" warning */ BSP_clear(); return Q_TRAN(&Calc_on); }
/*..........................................................................*/ QState Calc_operand2(Calc * const me, QEvt const * const e) { QState status; switch (e->sig) { case Q_ENTRY_SIG: { BSP_message("operand2-ENTRY;"); status = Q_HANDLED(); break; } case Q_EXIT_SIG: { BSP_message("operand2-EXIT;"); status = Q_HANDLED(); break; } case CE_SIG: { BSP_clear(); status = Q_TRAN(&Calc_opEntered); break; } case OPER_SIG: { if (BSP_eval(me->operand1, me->operator, BSP_get_value())) { me->operand1 = BSP_get_value(); me->operator = ((CalcEvt const *)e)->key_code; status = Q_TRAN(&Calc_opEntered); } else { status = Q_TRAN(&Calc_error); } break; } case EQUALS_SIG: { if (BSP_eval(me->operand1, me->operator, BSP_get_value())) { status = Q_TRAN(&Calc_result); } else { status = Q_TRAN(&Calc_error); } break; } case PERCENT_SIG: { 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; } } if (BSP_eval(me->operand1, me->operator, operand2)) { status = Q_TRAN(&Calc_result); } else { status = Q_TRAN(&Calc_error); } break; } default: { status = Q_SUPER(&Calc_on); break; } } return status; }