Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
player.cpp
00001 #include "qp_port.h" 00002 #include "dpp.h" 00003 #include "bsp.h" 00004 00005 Q_DEFINE_THIS_FILE 00006 00007 class Player : public QActive { 00008 private: 00009 QTimeEvt m_timeEvt; // to timeout update joystick pos 00010 uint8_t wins; 00011 uint8_t losses; 00012 00013 public: 00014 Player(); 00015 00016 private: 00017 static QState initial (Player *me, QEvent const *e); 00018 static QState thinking(Player *me, QEvent const *e); 00019 static QState playing (Player *me, QEvent const *e); 00020 }; 00021 00022 static Player l_player; 00023 00024 #define JOY_TIME (BSP_TICKS_PER_SEC/5) // update joystick position every 5 ticks 00025 00026 enum InternalSignals { // internal signals 00027 TIMEOUT_SIG = MAX_SIG 00028 }; 00029 00030 QActive * const AO_Player = l_player; 00031 00032 //............................................................................ 00033 Player::Player() 00034 : QActive((QStateHandler)&Player::initial), 00035 m_timeEvt(TIMEOUT_SIG) 00036 {} 00037 //............................................................................ 00038 QState Player::initial(Player *me, QEvent const *) { 00039 static uint8_t registered; // starts off with 0, per C-standard 00040 if (!registered) { 00041 QS_OBJ_DICTIONARY(&l_player); 00042 QS_OBJ_DICTIONARY(&l_player.m_timeEvt); 00043 00044 QS_FUN_DICTIONARY(&Player::initial); 00045 QS_FUN_DICTIONARY(&Player::thinking); 00046 QS_FUN_DICTIONARY(&Player::playing); 00047 00048 registered = (uint8_t)1; 00049 } 00050 00051 QS_SIG_DICTIONARY(PLAY_SIG, me); // signal for each Players 00052 QS_SIG_DICTIONARY(FINISHED_SIG, me); // signal for each Players 00053 00054 // init wins and losses ctr 00055 wins = losses = 0; 00056 00057 return Q_TRAN(&Player::thinking); // go to thinking 00058 } 00059 //............................................................................ 00060 QState Player::thinking(Player *me, QEvent const *) { 00061 switch (e->sig) { 00062 case Q_ENTRY_SIG: { 00063 // TODO : INITIALIZE BTN 00064 } 00065 case BUTTON_SIG: { 00066 return Q_TRAN(&Player::playing); // go to playing state 00067 } 00068 } 00069 return Q_SUPER(&QHsm::top); 00070 } 00071 //............................................................................ 00072 QState Player::playing(Player *me, QEvent const *) { 00073 switch (e->sig) { 00074 case Q_ENTRY_SIG: { 00075 me->m_timeEvt.postIn(me, JOY_TIME); // init joystick update (TIMEOUT_SIG) 00076 // TODO : INITIALIZE BTN 00077 return Q_HANDLED(); 00078 } 00079 case TIMEOUT_SIG: { 00080 int pos = BSP_joyUpdate(); 00081 HostEvt *pe = Q_NEW(HostEvt, SCROLL_SIG); 00082 pe->scroll_pos = pos; 00083 QF::PUBLISH(pe, me); 00084 } 00085 case BUTTON_SIG: { 00086 HostEvt *pe = Q_NEW(HostEvt, SELECT_SIG); 00087 pe->scroll_pos = 0; // no change 00088 QF::PUBLISH(pe, me); 00089 } 00090 case FINISHED_SIG: { 00091 if(((HostEvt *)e)->scroll_pos == 1) // win 00092 wins++; 00093 else 00094 losses++; 00095 00096 return Q_TRAN(&Player::thinking); // go to thinking state 00097 } 00098 } 00099 return Q_SUPER(&QHsm::top); 00100 }
Generated on Wed Jul 13 2022 20:02:05 by
1.7.2