AirsoftTimer software based on mbed

Dependencies:   mbed TextLCD keypad

Committer:
sillevl
Date:
Sat May 23 20:17:54 2015 +0000
Revision:
17:19dbb1dbb640
Parent:
10:afc22465169e
Child:
18:abcebc4d0da0
added buttonlistener to game selector (not yet working)

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 17:19dbb1dbb640 5 this->board->attach(this);
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 17:19dbb1dbb640 17 }
sillevl 17:19dbb1dbb640 18
sillevl 17:19dbb1dbb640 19 int GameSelector::select(){
sillevl 17:19dbb1dbb640 20 print_list();
sillevl 17:19dbb1dbb640 21 printf("printed list\r\n");
sillevl 17:19dbb1dbb640 22 while(selected_game == -1){
sillevl 17:19dbb1dbb640 23 board->leds->on(Leds::RIGHT);
sillevl 17:19dbb1dbb640 24 } // wait until selection is done
sillevl 17:19dbb1dbb640 25 //printf("game selected: %d \r\n", selected_game);
sillevl 17:19dbb1dbb640 26 return selected_game;
sillevl 10:afc22465169e 27 }
sillevl 10:afc22465169e 28
sillevl 17:19dbb1dbb640 29 void GameSelector::buttonEvent(char c){
sillevl 17:19dbb1dbb640 30 //printf("Gameselector buttonEvent start\r\n");
sillevl 17:19dbb1dbb640 31 switch(c){
sillevl 17:19dbb1dbb640 32 case '2':
sillevl 17:19dbb1dbb640 33 go_up();
sillevl 17:19dbb1dbb640 34 break;
sillevl 17:19dbb1dbb640 35 case '5':
sillevl 17:19dbb1dbb640 36 selected_game = current_selection;
sillevl 17:19dbb1dbb640 37 break;
sillevl 17:19dbb1dbb640 38 case '8':
sillevl 17:19dbb1dbb640 39 go_down();
sillevl 17:19dbb1dbb640 40 break;
sillevl 17:19dbb1dbb640 41 default:
sillevl 17:19dbb1dbb640 42 break;
sillevl 17:19dbb1dbb640 43 }
sillevl 17:19dbb1dbb640 44 //printf("end\r\n");
sillevl 10:afc22465169e 45 }
sillevl 10:afc22465169e 46
sillevl 10:afc22465169e 47 void GameSelector::print_list(){
sillevl 10:afc22465169e 48 board->lcd->cls();
sillevl 10:afc22465169e 49 for(int i = 0; i < 4; i++){
sillevl 10:afc22465169e 50 board->lcd->locate(2,i);
sillevl 10:afc22465169e 51 board->lcd->printf(titles[i+start_position]);
sillevl 10:afc22465169e 52 }
sillevl 10:afc22465169e 53 print_selection_arrow();
sillevl 10:afc22465169e 54 print_up_down_arrows();
sillevl 10:afc22465169e 55 }
sillevl 10:afc22465169e 56
sillevl 10:afc22465169e 57 void GameSelector::print_up_down_arrows(){
sillevl 10:afc22465169e 58 if(start_position != 0){
sillevl 10:afc22465169e 59 board->lcd->locate(19,0);
sillevl 10:afc22465169e 60 board->lcd->putc('\x02');
sillevl 10:afc22465169e 61 }
sillevl 10:afc22465169e 62 if(start_position < total_selections - 4){
sillevl 10:afc22465169e 63 board->lcd->locate(19,3);
sillevl 10:afc22465169e 64 board->lcd->putc('\x03');
sillevl 10:afc22465169e 65 }
sillevl 10:afc22465169e 66 }
sillevl 10:afc22465169e 67
sillevl 10:afc22465169e 68 void GameSelector::print_selection_arrow(){
sillevl 10:afc22465169e 69 int line = current_selection - start_position;
sillevl 10:afc22465169e 70 board->lcd->locate(0,line);
sillevl 10:afc22465169e 71 board->lcd->putc('\x00');
sillevl 10:afc22465169e 72 }
sillevl 10:afc22465169e 73
sillevl 10:afc22465169e 74 void GameSelector::go_down(){
sillevl 10:afc22465169e 75 if(current_selection < (total_selections - 1)){
sillevl 10:afc22465169e 76 current_selection++;
sillevl 10:afc22465169e 77 }
sillevl 10:afc22465169e 78 if(current_selection - start_position >= 4){
sillevl 10:afc22465169e 79 start_position = current_selection - 3;
sillevl 10:afc22465169e 80 }
sillevl 10:afc22465169e 81 print_list();
sillevl 10:afc22465169e 82 }
sillevl 10:afc22465169e 83
sillevl 10:afc22465169e 84 void GameSelector::go_up(){
sillevl 10:afc22465169e 85 if(current_selection > 0){
sillevl 10:afc22465169e 86 current_selection--;
sillevl 10:afc22465169e 87 }
sillevl 10:afc22465169e 88 if(current_selection - start_position < 0){
sillevl 10:afc22465169e 89 start_position = current_selection;
sillevl 10:afc22465169e 90 }
sillevl 10:afc22465169e 91 print_list();
sillevl 10:afc22465169e 92 }