Dependencies: MMA8452 N5110 PowerControl beep mbed
GameFSM.h
- Committer:
- stevenle93
- Date:
- 2015-05-07
- Revision:
- 6:3e8ce3108702
- Parent:
- 5:c0a58a6916f2
- Child:
- 7:95abf8c8af1e
File content as of revision 6:3e8ce3108702:
/** @file GameFSM.h @brief Header file of Game's Finite stage Machine */ #ifndef GAMEFSM_H #define GAMEFSM_H #include "Game.h" #include "Clock.h" #include "N5110.h" #include "Menu.h" #include "PowerControl/PowerControl.h" #include "mbed.h" Game game; Clock countdown; Menu menu; /** @namespace ButtonA @brief Button A of interrupting service */ InterruptIn ButtonA(p16); /** @namespace ButtonB @brief Button B of interrupting service */ InterruptIn ButtonB(p15); /** @namespace ButtonD @brief Button Down of interrupting service */ InterruptIn ButtonD(p24); /** @namespace ButtonU @brief Button Up of interrupting service */ InterruptIn ButtonU(p23); int BuAFlag = 0; int BuBFlag = 0; int BuDFlag = 0; int BuUFlag = 0; int stage = 0; int substage = 0; Timer debounce; void BuAPress() { if (debounce.read_ms()>200) { BuAFlag = 1; //Set flag for button A } } void BuBPress() { if (debounce.read_ms()>200) { BuBFlag = 1; //Set flag for button B } } void BuDPress() { if (debounce.read_ms()>200) { BuDFlag = 1; //Set flag for button Down } } void BuUPress() { if (debounce.read_ms()>200) { BuUFlag = 1; //Set flag for button Up } } class FSM { public: /** The function contains FSM of the whole programme */ void proact(); void resetButton(); void sleepMode(); }; void FSM::sleepMode() { if ((BuAFlag==0)&&(BuBFlag==0)&&(BuUFlag==0)&&(BuDFlag==0)) { Sleep(); lcd.setBrightness(0); } } void FSM::resetButton() { BuDFlag = 0; BuUFlag = 0; BuAFlag = 0; BuBFlag = 0; } void FSM::proact() { switch(stage) { case 0: //Main stage 0: MAIN MENU - Cursor at Play line lcd.clear(); menu.main(); lcd.drawCircle(23,27,2,1); if (BuDFlag) { resetButton(); stage = 1; } if (BuAFlag) { resetButton(); stage = 3; } //sleepMode(); break; case 1: //Main stage 1: MAIN MENU - Cursor at Score line lcd.clear(); menu.main(); lcd.drawCircle(20,35,2,1); if (BuUFlag) { resetButton(); stage = 0; } if (BuAFlag) { resetButton(); stage = 2; } //sleepMode(); break; case 2: //(Main stage 2)Sub stage 0: HIGH SCORE sector - Cursor is at Easy line lcd.clear(); menu.highscore(); lcd.drawCircle(23,19,2,1); if (BuDFlag) { resetButton(); stage = 12; //Go to HIGH SCORE sector/Normal line } if (BuAFlag) { resetButton(); stage = 14; //Go to HIGH SCORE sector/Hard high score } if (BuBFlag) { resetButton(); stage = 1; //Back to MAIN MENU sector/Score line } //sleepMode(); break; case 12: //(Main stage 2)Sub stage 1: HIGH SCORE sector - Cursor is at Normal line lcd.clear(); menu.highscore(); lcd.drawCircle(17,27,2,1); if (BuUFlag) { resetButton(); stage = 2; //Go to HIGH SCORE sector/Easy line } if (BuDFlag) { resetButton(); stage = 13; //Go to HIGH SCORE sector/Hard line } if (BuAFlag) { resetButton(); stage = 15; //Go to HIGH SCORE sector/Normal high score } if (BuBFlag) { resetButton(); stage = 1; //Back to MAIN MENU sector/Score line } //sleepMode(); break; case 13: //(Main stage 2)Sub stage 2: HIGH SCORE sector - Cursor is at Hard line lcd.clear(); menu.highscore(); lcd.drawCircle(23,35,2,1); if (BuUFlag) { resetButton(); stage = 12; //Go to HIGH SCORE sector/Normal line } if (BuAFlag) { resetButton(); stage = 16; //Go to HIGH SCORE sector/Hard high score } if (BuBFlag) { resetButton(); stage = 1; //Back to MAIN MENU sector/Score line } //sleepMode(); break; case 14: //(Main stage 2)Sub stage 3: HIGH SCORE sector - Easy mode high score lcd.clear(); lcd.printString("EASY MODE",16,0); lcd.printString("High score",14,2); lcd.printString("Your score",14,4); //Easy mode high score char HScore0[5]; sprintf(HScore0,"%d",game.scoArr[0]); lcd.printString(HScore0,38,3); //Easy mode current score char HScore1[5]; sprintf(HScore1,"%d",game.scoArr[1]); lcd.printString(HScore1,38,5); if (BuBFlag) { resetButton(); stage = 2; //Back to HIGH SCORE sector/Easy line } //sleepMode(); break; case 15: //(Main stage 2)Sub stage 4: HIGH SCORE sector - Normal mode high score lcd.clear(); lcd.printString("NORMAL MODE",8,0); lcd.printString("High score",14,2); lcd.printString("Your score",14,4); //Normal mode high score char HScore2[5]; sprintf(HScore2,"%d",game.scoArr[2]); lcd.printString(HScore2,38,3); //Normal mode current score char HScore3[5]; sprintf(HScore3,"%d",game.scoArr[3]); lcd.printString(HScore3,38,5); if (BuBFlag) { resetButton(); stage = 12; //Back to HIGH SCORE sector/Normal line } //sleepMode(); break; case 16: //(Main stage 2)Sub stage 5: HIGH SCORE sector - Hard mode high score lcd.clear(); lcd.printString("HARD MODE",16,0); lcd.printString("High score",14,2); lcd.printString("Your score",14,4); //Hard mode high score char HScore4[5]; sprintf(HScore4,"%d",game.scoArr[4]); lcd.printString(HScore4,38,3); //Hard mode current score char HScore5[5]; sprintf(HScore5,"%d",game.scoArr[5]); lcd.printString(HScore5,38,5); if (BuBFlag) { resetButton(); stage = 13; //Back to HIGH SCORE sector/Hard line } //sleepMode(); break; //This case is a game condition setting case //Clock countdown was set to 60 seconds //Score was set to 0 case 3: //Main stage 3: RESETING stage - Before entering GAME MODE menu game.reset(); resetButton(); stage = 4; break; //////////GAME MODE SECTOR///////// case 4: //(Main stage 4)Sub stage 0: GAME MODE sector - Cursor at Easy line lcd.clear(); menu.gameset(); lcd.drawCircle(23,19,2,1); if (BuAFlag) { resetButton(); stage = 7; } if (BuBFlag) { resetButton(); stage = 0; } if (BuDFlag) { resetButton(); stage = 5; //Go to stage 8 of Game set menu } //sleepMode(); break; case 5: //(Main stage 4)Sub stage 1: GAME MODE sector - Cursor at Normal line lcd.clear(); menu.gameset(); lcd.drawCircle(17,27,2,1); if (BuAFlag) { resetButton(); stage = 8; //Go to stage 8: Normal mode game } if (BuBFlag) { resetButton(); stage = 0; //Go back to stage 0: Main menu } if (BuUFlag) { resetButton(); stage = 4; //Go to stage 4: Game mode menu } if (BuDFlag) { resetButton(); stage = 6; //Go to stage 6: Game mode menu } //sleepMode(); break; case 6: //(Main stage 4)Sub stage 2: GAME MODE sector - Cursor at Hard line lcd.clear(); menu.gameset(); lcd.drawCircle(23,35,2,1); if (BuAFlag) { resetButton(); stage = 9; } if (BuBFlag) { resetButton(); stage = 0; } if (BuUFlag) { resetButton(); stage = 5; } //sleepMode(); break; case 7: //(Main stage 4)Sub stage 3: GAME SCREEN sector - Easy mode lcd.clear(); timer.attach(&timerExpired,0.5); gatimer.attach(&gatimerExpired,0.1); //Update time for Easy mode is 0.1s while(CClock > 0) { countdown.countDown(); game.easyMode(); } if (CClock == 0) { countdown.timeout(); } resetButton(); stage = 10; //Go to stage 10: Your score menu break; case 8: //(Main stage 4)Sub stage 4: GAME SCREEN sector - Normal mode lcd.clear(); timer.attach(&timerExpired,0.5); gatimer.attach(&gatimerExpired,0.2); //Update time for Normal mode is 0.2s while(CClock > 0) { countdown.countDown(); game.norMode(); //Normal mode of game is activated } if (CClock == 0) { countdown.timeout(); } resetButton(); stage = 10; //Go to stage 10: Your score menu break; case 9: //(Main stage 4)Sub stage 5: GAME SCREEN sector - Hard mode lcd.clear(); timer.attach(&timerExpired,0.5); gatimer.attach(&gatimerExpired,0.4); //Update time for Hard mode is 0.4s while(CClock > 0) { countdown.countDown(); game.hardMode(); //Hard mode of the game is activated } if (CClock == 0) { countdown.timeout(); } resetButton(); stage = 10; //Go to stage 10: Your score menu break; case 10: //Main stage 5: YOUR SCORE sector - Cursor at Main menu line lcd.clear(); menu.yourscore(); lcd.drawCircle(8,27,2,1); char YScore[14]; sprintf(YScore,"%d",game.score); lcd.printString(YScore,37,2); if (BuDFlag) { resetButton(); stage = 11; } if (BuAFlag) { resetButton(); stage = 0; } //sleepMode(); break; case 11: //Main stage 6: YOUR SCORE sector - Cursor at Reset line lcd.clear(); menu.yourscore(); lcd.drawCircle(8,35,2,1); lcd.printString(YScore,37,2); if (BuUFlag) { resetButton(); stage = 10; } if (BuAFlag) { resetButton(); stage = 3; } //sleepMode(); break; } wait(0.1); } #endif