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.
Dependencies: mbed Gamepad N5110
main.cpp
00001 /* 00002 ELEC2645 Embedded Systems Project 00003 School of Electronic & Electrical Engineering University of Leeds 00004 Name: Adam P. Baker 00005 Username: el17apb 00006 Student ID Number: 201166301 00007 Date: 8 May 2019 00008 */ 00009 00010 00011 ///////// pre-processor directives ///////// 00012 #include "mbed.h" 00013 #include "Gamepad.h" 00014 #include "N5110.h" 00015 #include "BlockheadEngine.h" 00016 #include "Menu.h" 00017 00018 /////////////// objects //////////////// 00019 N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); 00020 Gamepad pad; 00021 BlockheadEngine blockhead; 00022 Ticker ticker; 00023 Menu menu; 00024 00025 ///////////// prototypes /////////////// 00026 void timer_isr(); 00027 void init(); 00028 void intro(N5110 &lcd, Gamepad &pad); 00029 void main_menu(N5110 &lcd, Gamepad &pad); 00030 int select_menu_item(Gamepad &pad, N5110 &lcd); 00031 void go_to_main_menu_item(N5110 &lcd, Gamepad &pad, int input); 00032 void menu_playgame(N5110 &lcd, Gamepad &pad); 00033 void menu_highscore(N5110 &lcd, Gamepad &pad); 00034 void menu_settings(N5110 &lcd, Gamepad &pad); 00035 void menu_quit(N5110 &lcd, Gamepad &pad); 00036 void select_continue_menu_item(N5110 &lcd, Gamepad &pad, int input); 00037 void menu_continue(N5110 &lcd, Gamepad &pad); 00038 00039 volatile int timer_flag = 0; //sets timer_flag to 0 00040 00041 00042 ///////////// functions //////////////// 00043 int main() 00044 { 00045 00046 int fps = 6; //sets fps to 6 00047 init(); 00048 ticker.attach(&timer_isr,1.0f/fps); //creates a ticker and attaches it to timer_isr (ticks 6 * per second) 00049 00050 lcd.setContrast(0.55); //intialy sets contrast to 0.55 00051 00052 intro(lcd, pad); //runs intro sequence 00053 00054 main_menu(lcd, pad); //runs main menu ( finite state machine, states also include "play game" "highscore" "settings" and "quit..." ) 00055 00056 } 00057 00058 00059 //sets the timer_flag to 1 00060 void timer_isr() 00061 { 00062 00063 timer_flag = 1; // set flag in ISR 00064 } 00065 00066 00067 //intialises variables of below classes 00068 void init() 00069 { 00070 pad.init(); //intialise Gamepad class variables 00071 lcd.init(); //intialise N5110 class variables 00072 menu.init(); //intialise menu variables 00073 } 00074 00075 00076 //runs the intro sequence 00077 void intro(N5110 &lcd, Gamepad &pad) 00078 { 00079 menu.title_intro(lcd, pad); //runs 'BLOCK HEAD' intro sequence 00080 int start = 0; 00081 00082 do { 00083 if (timer_flag == true) { //only run when timer flag is true (6fps) 00084 00085 timer_flag = 0; 00086 00087 start = menu.press_start(lcd, pad); //flashes start untill start button is pressed 00088 00089 } else { 00090 00091 sleep(); //sleep when timer_flag not true in order to conserve energy 00092 00093 } 00094 00095 00096 } while (start == 0); 00097 00098 menu.init(); //intilises menu variables 00099 } 00100 00101 00102 //main menu state 00103 void main_menu(N5110 &lcd, Gamepad &pad) 00104 { 00105 00106 blockhead.init(); //intialise blockhead engine variables for new game 00107 int input = select_menu_item(pad, lcd); //reads input and prints the main menu 00108 go_to_main_menu_item(lcd, pad, input); //peforms what ever input is chosen 00109 } 00110 00111 00112 //reads input and prints main menu 00113 int select_menu_item(Gamepad &pad, N5110 &lcd) 00114 { 00115 int input = 0; 00116 do { 00117 00118 if (timer_flag == true) { //only run when timer flag is true (6fps) 00119 00120 timer_flag = 0; 00121 00122 input = menu.select_input_main(pad, lcd); //runs until input is selected 00123 00124 } else { 00125 00126 sleep(); //sleep when timer_flag not true in order to conserve energy 00127 00128 } 00129 } while (input == 0); //repeats untill an input is chosen 00130 00131 return input; 00132 } 00133 00134 00135 //depending on input, goes to following menu items 00136 void go_to_main_menu_item(N5110 &lcd, Gamepad &pad, int input) 00137 { 00138 switch (input) { 00139 case 1: 00140 menu_playgame(lcd, pad); 00141 break; 00142 case 2: 00143 menu_highscore(lcd, pad); 00144 break; 00145 case 3: 00146 menu_settings(lcd, pad); 00147 case 4: 00148 menu_quit(lcd, pad); 00149 default: 00150 exit(1); 00151 break; 00152 } 00153 } 00154 00155 00156 //runs playgame menu state 00157 void menu_playgame(N5110 &lcd, Gamepad &pad) 00158 { 00159 int gameover = 0; 00160 00161 do { 00162 if (timer_flag == true) { //only run when timer flag is true (6fps) 00163 00164 timer_flag = 0; //if it has, clear the flag 00165 00166 gameover = blockhead.playgame(lcd, pad); //run game untill gameover 00167 00168 } else { 00169 00170 sleep(); //sleep when timer_flag not true in order to conserve energy 00171 00172 } 00173 } while (gameover == 0); 00174 00175 menu_continue(lcd, pad); //once gameover occurs, go to continue menu 00176 00177 } 00178 00179 00180 //runs highscore menu state 00181 void menu_highscore(N5110 &lcd, Gamepad &pad) 00182 { 00183 00184 int goback = 0; 00185 00186 do { 00187 00188 if (timer_flag == true) { //only run when timer flag is true (6fps) 00189 00190 timer_flag = 0; //if it has, clear the flag 00191 00192 int highscore = blockhead.highscore(); //gets high score from blockhead engine 00193 00194 menu.print_highscore(lcd, highscore); //prints high score menu, with high score 00195 00196 if (pad.check_event(Gamepad::B_PRESSED) || pad.check_event(Gamepad::BACK_PRESSED)) { //B or Back button to go back to main menu 00197 goback = 1; 00198 } 00199 00200 } else { 00201 00202 sleep(); //sleep when timer_flag not true in order to conserve energy 00203 00204 } 00205 } while (goback == 0); 00206 00207 main_menu(lcd, pad); //once b or back pressed, go to main menu 00208 } 00209 00210 00211 //runs settings menu state 00212 void menu_settings(N5110 &lcd, Gamepad &pad) 00213 { 00214 00215 int goback = 0; 00216 float contrast; 00217 00218 do { 00219 00220 if (timer_flag == true) { //only run when timer flag is true (6fps) 00221 00222 timer_flag = 0; //if it has, clear the flag 00223 00224 contrast = menu.print_settings(pad, lcd); //run comtast menu and return contast 00225 lcd.setContrast(contrast); //update lcd contast 00226 00227 if (pad.check_event(Gamepad::B_PRESSED) || pad.check_event(Gamepad::BACK_PRESSED)) { 00228 goback = 1; 00229 } 00230 00231 } else { 00232 00233 sleep(); //sleep when timer_flag not true in order to conserve energy 00234 00235 } 00236 } while (goback == 0); 00237 main_menu(lcd, pad); //go to main menu once b or back is pressed 00238 } 00239 00240 void menu_quit(N5110 &lcd, Gamepad &pad) 00241 { 00242 lcd.turnOff(); 00243 sleep(); 00244 00245 } 00246 00247 00248 //depending on input, goes to following continue menu items 00249 void select_continue_menu_item(N5110 &lcd, Gamepad &pad, int input) 00250 { 00251 switch (input) { 00252 case 1: 00253 menu_playgame(lcd, pad); //if play game, level will be the one you were just on 00254 break; 00255 case 2: 00256 main_menu(lcd, pad); //if menu, game resets 00257 break; 00258 default: 00259 exit(1); 00260 break; 00261 } 00262 } 00263 00264 00265 //runs continue game state 00266 void menu_continue(N5110 &lcd, Gamepad &pad) 00267 { 00268 blockhead.continue_init(); //intialised continue game variables (level does not change) 00269 menu.init(); 00270 00271 int input = 0; 00272 do { 00273 00274 if (timer_flag == true) { //only run when timer flag is true (6fps) 00275 00276 timer_flag = 0; //if it has, clear the flag 00277 00278 input = menu.select_input_continue(pad, lcd); //prints continue menu, and selects coninue menu item 00279 00280 } else { 00281 00282 sleep(); //sleep when timer_flag not true in order to conserve energy 00283 00284 } 00285 } while (input == 0); 00286 select_continue_menu_item(lcd, pad, input); //go to what ever item has been selected 00287 } 00288
Generated on Fri Jul 15 2022 00:42:15 by
