AirsoftTimer software based on mbed

Dependencies:   mbed TextLCD keypad

Committer:
sillevl
Date:
Thu Feb 09 12:28:56 2017 +0000
Revision:
27:f29805113454
Parent:
19:2eba101d9c2c
Child:
21:f4e556dc9885
ARCHIVE WIP, not working (crashes, cause unknown)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sillevl 10:afc22465169e 1 #include "GameSelector.h"
sillevl 10:afc22465169e 2
sillevl 10:afc22465169e 3 GameSelector::GameSelector(Board* board){
sillevl 10:afc22465169e 4 this->board = board;
sillevl 19:2eba101d9c2c 5 this->board->attach(this, &GameSelector::buttonEvent);
sillevl 10:afc22465169e 6 titles[0] = "Hold it";
sillevl 10:afc22465169e 7 titles[1] = "Capture the bomb";
sillevl 10:afc22465169e 8 titles[2] = "Hurry up";
sillevl 10:afc22465169e 9 titles[3] = "Search & destroy";
sillevl 10:afc22465169e 10 titles[4] = "Simple timer";
sillevl 10:afc22465169e 11 titles[5] = "DummyGame 6";
sillevl 10:afc22465169e 12 titles[6] = "DummyGame 7";
sillevl 10:afc22465169e 13 total_selections = 7;
sillevl 10:afc22465169e 14 current_selection = 0;
sillevl 10:afc22465169e 15 start_position = 0;
sillevl 17:19dbb1dbb640 16 selected_game = -1;
sillevl 19:2eba101d9c2c 17
sillevl 19:2eba101d9c2c 18 update_screen = false;
sillevl 17:19dbb1dbb640 19 }
sillevl 17:19dbb1dbb640 20
sillevl 17:19dbb1dbb640 21 int GameSelector::select(){
sillevl 17:19dbb1dbb640 22 print_list();
sillevl 17:19dbb1dbb640 23 while(selected_game == -1){
sillevl 19:2eba101d9c2c 24 if(update_screen){
sillevl 19:2eba101d9c2c 25 print_list();
sillevl 19:2eba101d9c2c 26 }
sillevl 17:19dbb1dbb640 27 } // wait until selection is done
sillevl 17:19dbb1dbb640 28 return selected_game;
sillevl 10:afc22465169e 29 }
sillevl 10:afc22465169e 30
sillevl 19:2eba101d9c2c 31 uint32_t GameSelector::buttonEvent(uint32_t c){
sillevl 17:19dbb1dbb640 32 switch(c){
sillevl 17:19dbb1dbb640 33 case '2':
sillevl 17:19dbb1dbb640 34 go_up();
sillevl 19:2eba101d9c2c 35 update_screen = true;
sillevl 17:19dbb1dbb640 36 break;
sillevl 17:19dbb1dbb640 37 case '5':
sillevl 17:19dbb1dbb640 38 selected_game = current_selection;
sillevl 17:19dbb1dbb640 39 break;
sillevl 17:19dbb1dbb640 40 case '8':
sillevl 17:19dbb1dbb640 41 go_down();
sillevl 19:2eba101d9c2c 42 update_screen = true;
sillevl 17:19dbb1dbb640 43 break;
sillevl 17:19dbb1dbb640 44 default:
sillevl 17:19dbb1dbb640 45 break;
sillevl 17:19dbb1dbb640 46 }
sillevl 18:abcebc4d0da0 47 return 0;
sillevl 19:2eba101d9c2c 48 }
sillevl 10:afc22465169e 49
sillevl 10:afc22465169e 50 void GameSelector::print_list(){
sillevl 10:afc22465169e 51 board->lcd->cls();
sillevl 10:afc22465169e 52 for(int i = 0; i < 4; i++){
sillevl 10:afc22465169e 53 board->lcd->locate(2,i);
sillevl 10:afc22465169e 54 board->lcd->printf(titles[i+start_position]);
sillevl 10:afc22465169e 55 }
sillevl 10:afc22465169e 56 print_selection_arrow();
sillevl 10:afc22465169e 57 print_up_down_arrows();
sillevl 19:2eba101d9c2c 58
sillevl 19:2eba101d9c2c 59 update_screen = false;
sillevl 10:afc22465169e 60 }
sillevl 10:afc22465169e 61
sillevl 10:afc22465169e 62 void GameSelector::print_up_down_arrows(){
sillevl 10:afc22465169e 63 if(start_position != 0){
sillevl 10:afc22465169e 64 board->lcd->locate(19,0);
sillevl 10:afc22465169e 65 board->lcd->putc('\x02');
sillevl 10:afc22465169e 66 }
sillevl 10:afc22465169e 67 if(start_position < total_selections - 4){
sillevl 10:afc22465169e 68 board->lcd->locate(19,3);
sillevl 10:afc22465169e 69 board->lcd->putc('\x03');
sillevl 10:afc22465169e 70 }
sillevl 10:afc22465169e 71 }
sillevl 10:afc22465169e 72
sillevl 10:afc22465169e 73 void GameSelector::print_selection_arrow(){
sillevl 10:afc22465169e 74 int line = current_selection - start_position;
sillevl 10:afc22465169e 75 board->lcd->locate(0,line);
sillevl 10:afc22465169e 76 board->lcd->putc('\x00');
sillevl 10:afc22465169e 77 }
sillevl 10:afc22465169e 78
sillevl 10:afc22465169e 79 void GameSelector::go_down(){
sillevl 10:afc22465169e 80 if(current_selection < (total_selections - 1)){
sillevl 10:afc22465169e 81 current_selection++;
sillevl 10:afc22465169e 82 }
sillevl 10:afc22465169e 83 if(current_selection - start_position >= 4){
sillevl 10:afc22465169e 84 start_position = current_selection - 3;
sillevl 10:afc22465169e 85 }
sillevl 19:2eba101d9c2c 86 //print_list();
sillevl 10:afc22465169e 87 }
sillevl 10:afc22465169e 88
sillevl 10:afc22465169e 89 void GameSelector::go_up(){
sillevl 10:afc22465169e 90 if(current_selection > 0){
sillevl 10:afc22465169e 91 current_selection--;
sillevl 10:afc22465169e 92 }
sillevl 10:afc22465169e 93 if(current_selection - start_position < 0){
sillevl 10:afc22465169e 94 start_position = current_selection;
sillevl 10:afc22465169e 95 }
sillevl 19:2eba101d9c2c 96 //print_list();
sillevl 10:afc22465169e 97 }