AirsoftTimer software based on mbed
Dependencies: mbed TextLCD keypad
Revision 13:ece97a1108cc, committed 2015-05-23
- Comitter:
- sillevl
- Date:
- Sat May 23 17:15:42 2015 +0000
- Parent:
- 12:22e9ef610ea2
- Child:
- 14:e0bfee0a5e66
- Commit message:
- Refactoring and adding GameFactory and Objective concept
Changed in this revision
--- a/Airsofttimer.cpp Sat May 23 15:28:55 2015 +0000 +++ b/Airsofttimer.cpp Sat May 23 17:15:42 2015 +0000 @@ -36,7 +36,7 @@ while(true){ // first we need to select a game from the available games list int game_number = select_game(); - Game* game = Game::create_game(board, game_number); + Game* game = GameFactory::create_game(board, game_number); game->setup(); game->run(); delete game;
--- a/Airsofttimer.h Sat May 23 15:28:55 2015 +0000 +++ b/Airsofttimer.h Sat May 23 17:15:42 2015 +0000 @@ -5,7 +5,7 @@ #include "mbed.h" #include "board/Board.h" -#include "games/Game.h" +#include "games/GameFactory.h" class Airsofttimer {
--- a/games/DummyGame.cpp Sat May 23 15:28:55 2015 +0000 +++ b/games/DummyGame.cpp Sat May 23 17:15:42 2015 +0000 @@ -1,4 +1,5 @@ #include "DummyGame.h" +#include "../objectives/WaitForKeyObjective.h" const char* NAME = "DummyGame"; @@ -6,9 +7,11 @@ } -void DummyGame::run(){ - board->lcd->printf("DummyGame"); - while(true){ - // don't return from this function yet (only if the game ends); - } -} \ No newline at end of file +void DummyGame::run(){ + WaitForKeyObjective* objective = new WaitForKeyObjective(); + objective->start(); +} + +void DummyGame::setup(){ + +}
--- a/games/DummyGame.h Sat May 23 15:28:55 2015 +0000 +++ b/games/DummyGame.h Sat May 23 17:15:42 2015 +0000 @@ -7,6 +7,7 @@ public: DummyGame(Board* board); virtual void run(); + virtual void setup(); };
--- a/games/Game.cpp Sat May 23 15:28:55 2015 +0000 +++ b/games/Game.cpp Sat May 23 17:15:42 2015 +0000 @@ -1,6 +1,4 @@ #include "Game.h" -#include "DummyGame.h" -#include "ShowBuzzer.h" Game::Game(Board* board){ this->board = board; @@ -16,13 +14,9 @@ leds->off(Leds::ALL); } -Game* Game::create_game(Board* board, int choice){ - Game* game = new ShowBuzzer(board); - return game; +uint32_t Game::keyEvent(uint32_t key){ + // play key pressed sound ? + // do nothing by default + return 0; } -void Game::keyEvent(char key){ - // play key pressed sound ? - // do nothing by default -} -
--- a/games/Game.h Sat May 23 15:28:55 2015 +0000 +++ b/games/Game.h Sat May 23 17:15:42 2015 +0000 @@ -7,7 +7,6 @@ public: Game(Board* board); static const char* NAME; - static Game* create_game(Board* board, int choice); //factory method virtual void setup() = 0; virtual void run() = 0; @@ -22,7 +21,7 @@ Keyboard* keyboard; Buzzer* buzzer; - void keyEvent(char key); + uint32_t keyEvent(uint32_t key); };
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/games/GameFactory.cpp Sat May 23 17:15:42 2015 +0000 @@ -0,0 +1,7 @@ +#include "GameFactory.h" +#include "DummyGame.h" + +Game* GameFactory::create_game(Board* board, int choice){ + Game* game = new DummyGame(board); + return game; +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/games/GameFactory.h Sat May 23 17:15:42 2015 +0000 @@ -0,0 +1,14 @@ +#ifndef GAMEFACTORY_H +#define GAMEFACTORY_H + +#include "../board/Board.h" +#include "Game.h" + +class GameFactory{ + public: + static Game* create_game(Board* board, int choice); //factory method + +}; + + +#endif \ No newline at end of file
--- a/games/ShowBuzzer.cpp Sat May 23 15:28:55 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,123 +0,0 @@ - -#include "ShowBuzzer.h" - - -static const char* NAME = "ShowBuzzer"; - -ShowBuzzer::ShowBuzzer(Board* board) : Game(board){ - keyboard->attach(this,&ShowBuzzer::keyPressed); - keyboard->start(); - leds->off(Leds::ALL); -} - -void ShowBuzzer::setup(){ - //no setup required for this game -} - -void ShowBuzzer::run(){ - lcd->printf("ShowBuzzer"); - newRound(); - while(true){ - - //wait for button press - Team team = waitForButtonPress(); - //show leds - setLeds(team); - //show display - setDisplay(team); - //play sound - playSound(); - //wait for 3 seconds - wait_ms(5000); - //start new round - newRound(); - - // don't return from this function yet (only if the game ends); - } -} - -void ShowBuzzer::newRound(){ - buzzer->playNote(200,50); - leds->off(Leds::ALL); - lcd->cls(); - lcd->printf("*** WAITING ***"); -} - -ShowBuzzer::Team ShowBuzzer::waitForButtonPress(){ - Team team; - while(true){ - if(button->read() == 0){ - team = TEAM_B; - break; - } - if(key->read() == 0){ - team = TEAM_A; - break; - } - } - return team; -} - -void ShowBuzzer::setLeds(Team team){ - switch(team){ - case TEAM_A: - leds->on(Leds::LEFT); - break; - case TEAM_B: - leds->on(Leds::RIGHT); - break; - default: - break; - } -} - -void ShowBuzzer::setDisplay(Team team){ - lcd->locate(0,0); - lcd->cls(); - switch(team){ - case TEAM_A: - lcd->printf("Team A"); - break; - case TEAM_B: - lcd->printf("Team B"); - break; - default: - break; - } -} - -void ShowBuzzer::playSound(){ - buzzer->playNote(698, 50); - buzzer->playNote(783, 50); - buzzer->playNote(880, 50); - buzzer->playNote(987, 50); - buzzer->playNote(1108, 50); - buzzer->playNote(1244, 50); - buzzer->playNote(1396, 50); - buzzer->playNote(1567, 50); - buzzer->playNote(1760, 50); - buzzer->playNote(1975, 50); -} - -void ShowBuzzer::playCorrect(){ - buzzer->playNote(2093, 100); - buzzer->playNote(2637, 100); - buzzer->playNote(3165, 100); - buzzer->playNote(3951, 100); -} - -void ShowBuzzer::playWrong(){ - buzzer->playNote(440, 1000); -} - -uint32_t ShowBuzzer::keyPressed(uint32_t key){ - if(key == 11){ playCorrect();} - if(key == 9){ playWrong();} - return 0; -} - - - - - -
--- a/games/ShowBuzzer.h Sat May 23 15:28:55 2015 +0000 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,28 +0,0 @@ - -#ifndef SHOWBUZZER_H -#define SHOWBUZZER_H - -#include "Game.h" - -class ShowBuzzer : public Game{ - - public: - ShowBuzzer(Board* board); - virtual void setup(); - virtual void run(); - - private: - enum Team {TEAM_A, TEAM_B}; - Team waitForButtonPress(); - void setLeds(Team team); - void setDisplay(Team team); - void playSound(); - void newRound(); - uint32_t keyPressed(uint32_t key); - - void playCorrect(); - void playWrong(); -}; - - -#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/objectives/Objective.cpp Sat May 23 17:15:42 2015 +0000 @@ -0,0 +1,26 @@ + +#include "Objective.h" + +Objective::Objective(){ + status = WAITING; +} + +Objective::Status Objective::getStatus(){ + return status; +} + +void Objective::start(){ + while(status != COMPLETED){ + run(); + } +} + +void Objective::run(){ + //board->lcd->printf("DummyGame"); + //board->lcd->printf("press any key to continue"); +} + +void Objective::complete(){ + status = COMPLETED; +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/objectives/Objective.h Sat May 23 17:15:42 2015 +0000 @@ -0,0 +1,22 @@ +#ifndef OBJECTIVE_H +#define OBJECTIVE_H + +class Objective{ + + enum Status { WAITING, ACTIVE, COMPLETED }; + + public: + Objective(); + Status getStatus(); + + void start(); + void run(); + void complete(); + + private: + Status status; + +}; + + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/objectives/WaitForKeyObjective.cpp Sat May 23 17:15:42 2015 +0000 @@ -0,0 +1,9 @@ + +#include "WaitForKeyObjective.h" + +void WaitForKeyObjective::run(){ + //board->lcd->printf("DummyGame"); + //board->lcd->printf("press any key to continue"); + + // if key is pressed -> complete(); +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/objectives/WaitForKeyObjective.h Sat May 23 17:15:42 2015 +0000 @@ -0,0 +1,15 @@ +#ifndef WAITFORKEYOBJECTIVE_H +#define WAITFORKEYOBJECTIVE_H + +#include "Objective.h" + +class WaitForKeyObjective : public Objective{ + + public: + void run(); + + +}; + + +#endif \ No newline at end of file