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
main.cpp@64:b373b6bf8255, 2019-05-09 (annotated)
- Committer:
- el17arm
- Date:
- Thu May 09 11:45:21 2019 +0000
- Revision:
- 64:b373b6bf8255
- Parent:
- 63:ec95e155fb30
- Child:
- 66:c4ba64d288ba
Documentation
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| el17arm | 42:d81c008b0436 | 1 | /* |
| el17arm | 42:d81c008b0436 | 2 | ELEC2645 Embedded Systems Project |
| el17arm | 42:d81c008b0436 | 3 | School of Electronic & Electrical Engineering |
| el17arm | 42:d81c008b0436 | 4 | University of Leeds |
| el17arm | 42:d81c008b0436 | 5 | Name: Andrew Milner |
| el17arm | 42:d81c008b0436 | 6 | Username: el17arm |
| el17arm | 42:d81c008b0436 | 7 | Student ID Number: 201162219 |
| el17arm | 42:d81c008b0436 | 8 | Date: 18/03/19 |
| el17arm | 42:d81c008b0436 | 9 | */ |
| el17arm | 42:d81c008b0436 | 10 | |
| el17arm | 45:bad704c546d4 | 11 | ///////// pre-processor directives //////// |
| el17arm | 42:d81c008b0436 | 12 | |
| el17arm | 63:ec95e155fb30 | 13 | |
| el17arm | 0:fe19852199d2 | 14 | #include "mbed.h" |
| el17arm | 0:fe19852199d2 | 15 | #include "N5110.h" |
| el17arm | 0:fe19852199d2 | 16 | #include "Gamepad.h" |
| el17arm | 29:d85886364643 | 17 | #include "Gameengine.h" |
| el17arm | 0:fe19852199d2 | 18 | |
| el17arm | 63:ec95e155fb30 | 19 | #define WITH_TESTING |
| el17arm | 63:ec95e155fb30 | 20 | |
| el17arm | 63:ec95e155fb30 | 21 | #ifdef WITH_TESTING |
| el17arm | 63:ec95e155fb30 | 22 | #include "tests.h" |
| el17arm | 63:ec95e155fb30 | 23 | #endif |
| el17arm | 63:ec95e155fb30 | 24 | |
| el17arm | 63:ec95e155fb30 | 25 | |
| el17arm | 45:bad704c546d4 | 26 | /////////////// objects /////////////// |
| el17arm | 45:bad704c546d4 | 27 | |
| el17arm | 29:d85886364643 | 28 | Gameengine game; |
| el17arm | 0:fe19852199d2 | 29 | Gamepad pad; |
| el17arm | 0:fe19852199d2 | 30 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| el17arm | 0:fe19852199d2 | 31 | AnalogIn pot0(PTB2); |
| el17arm | 41:0cf320f73424 | 32 | BusOut oxygen(PTA1, PTA2, PTC2); |
| el17arm | 41:0cf320f73424 | 33 | BusOut lives(PTC3, PTC4, PTD3); |
| el17arm | 41:0cf320f73424 | 34 | |
| el17arm | 45:bad704c546d4 | 35 | ///////////// prototypes /////////////// |
| el17arm | 45:bad704c546d4 | 36 | |
| el17arm | 58:4a826093d9e9 | 37 | /** |
| el17arm | 45:bad704c546d4 | 38 | * @brief sets the contrast of lcd screen |
| el17arm | 45:bad704c546d4 | 39 | * @details contrast is adjust with analog input PTB2 |
| el17arm | 45:bad704c546d4 | 40 | */ |
| el17arm | 0:fe19852199d2 | 41 | void contrast(); |
| el17arm | 56:8c827d1cae3b | 42 | /** Initialises all classes and libraries and default game settings |
| el17arm | 45:bad704c546d4 | 43 | * @details initialises LCD and gamepad library and sets screen brightness |
| el17arm | 45:bad704c546d4 | 44 | */ |
| el17arm | 0:fe19852199d2 | 45 | void init(); |
| el17arm | 56:8c827d1cae3b | 46 | /** Displays opening screen |
| el17arm | 45:bad704c546d4 | 47 | * @details Screen displays title of game and instructs player to press start to begin |
| el17arm | 45:bad704c546d4 | 48 | */ |
| el17arm | 0:fe19852199d2 | 49 | void start_screen(); |
| el17arm | 56:8c827d1cae3b | 50 | /** Controls LEDs on front panel. |
| el17arm | 45:bad704c546d4 | 51 | * @details Every time a player dies a RHS LED is turned off, as time reduces |
| el17arm | 56:8c827d1cae3b | 52 | * LEDs turn off on LHS. |
| el17arm | 45:bad704c546d4 | 53 | */ |
| el17arm | 41:0cf320f73424 | 54 | void leds(); |
| el17arm | 56:8c827d1cae3b | 55 | /** Initialises and renders all level objects |
| el17arm | 45:bad704c546d4 | 56 | * @details Contains functions for all levels and will display next level objects |
| el17arm | 45:bad704c546d4 | 57 | * once player has completed each level |
| el17arm | 45:bad704c546d4 | 58 | */ |
| el17arm | 15:2bda80896a84 | 59 | void render(); |
| el17arm | 56:8c827d1cae3b | 60 | /** Displays screen if game over or game complete |
| el17arm | 56:8c827d1cae3b | 61 | * @details Screen will display player's final score and display game over or game complete |
| el17arm | 45:bad704c546d4 | 62 | */ |
| el17arm | 19:4789cb4ca550 | 63 | void restart(); |
| el17arm | 58:4a826093d9e9 | 64 | /** Displays options screen |
| el17arm | 64:b373b6bf8255 | 65 | * @details gives player controls for game |
| el17arm | 58:4a826093d9e9 | 66 | */ |
| el17arm | 60:3df033345059 | 67 | void option_screen(); |
| el17arm | 60:3df033345059 | 68 | |
| el17arm | 64:b373b6bf8255 | 69 | bool options_flag = false; |
| el17arm | 45:bad704c546d4 | 70 | |
| el17arm | 45:bad704c546d4 | 71 | ///////////// functions //////////////// |
| el17arm | 45:bad704c546d4 | 72 | |
| el17arm | 0:fe19852199d2 | 73 | int main() |
| el17arm | 63:ec95e155fb30 | 74 | { |
| el17arm | 63:ec95e155fb30 | 75 | #ifdef WITH_TESTING |
| el17arm | 63:ec95e155fb30 | 76 | run_tests(lcd); |
| el17arm | 63:ec95e155fb30 | 77 | #endif |
| el17arm | 63:ec95e155fb30 | 78 | |
| el17arm | 45:bad704c546d4 | 79 | init(); // initialises screen and default game settings |
| el17arm | 45:bad704c546d4 | 80 | start_screen(); // waits for user to press start to begin |
| el17arm | 58:4a826093d9e9 | 81 | option_screen(); // displays options screen |
| el17arm | 58:4a826093d9e9 | 82 | |
| el17arm | 45:bad704c546d4 | 83 | // game loop, reads input and updates game accordingly |
| el17arm | 0:fe19852199d2 | 84 | while (1) { |
| el17arm | 41:0cf320f73424 | 85 | |
| el17arm | 45:bad704c546d4 | 86 | contrast(); // adjusts contrast of screen |
| el17arm | 45:bad704c546d4 | 87 | render(); // draws all level objects on to screen |
| el17arm | 45:bad704c546d4 | 88 | game.update(lcd, pad); // updates movement, collected keys, lives, time left |
| el17arm | 45:bad704c546d4 | 89 | restart(); // restart screen if game over or end screen if game complete displays final score |
| el17arm | 45:bad704c546d4 | 90 | wait(0.1); // wait function to control fps |
| el17arm | 45:bad704c546d4 | 91 | } |
| el17arm | 45:bad704c546d4 | 92 | } |
| el17arm | 48:bd1f31fbfee3 | 93 | |
| el17arm | 41:0cf320f73424 | 94 | |
| el17arm | 45:bad704c546d4 | 95 | //initialises all classes and libraries and default game settings |
| el17arm | 0:fe19852199d2 | 96 | void init() |
| el17arm | 0:fe19852199d2 | 97 | { |
| el17arm | 0:fe19852199d2 | 98 | lcd.init(); |
| el17arm | 0:fe19852199d2 | 99 | lcd.normalMode(); // normal colour mode |
| el17arm | 0:fe19852199d2 | 100 | lcd.refresh(); |
| el17arm | 41:0cf320f73424 | 101 | lcd.setBrightness(1.0); |
| el17arm | 0:fe19852199d2 | 102 | pad.init(); |
| el17arm | 29:d85886364643 | 103 | game.game_init(); |
| el17arm | 15:2bda80896a84 | 104 | } |
| el17arm | 2:725c213b2396 | 105 | |
| el17arm | 45:bad704c546d4 | 106 | // detects which level is being played and draws the level objects with collision settings |
| el17arm | 15:2bda80896a84 | 107 | void render() |
| el17arm | 15:2bda80896a84 | 108 | { |
| el17arm | 58:4a826093d9e9 | 109 | game.draw_l1(lcd, pad); // level 1 |
| el17arm | 45:bad704c546d4 | 110 | game.draw_l2(lcd, pad); // level 2 |
| el17arm | 45:bad704c546d4 | 111 | game.draw_l3(lcd, pad); // level 3 |
| el17arm | 58:4a826093d9e9 | 112 | game.draw_l4(lcd, pad); // level 4 |
| el17arm | 45:bad704c546d4 | 113 | leds(); // function to update leds to reflect time and lives left |
| el17arm | 0:fe19852199d2 | 114 | } |
| el17arm | 0:fe19852199d2 | 115 | |
| el17arm | 45:bad704c546d4 | 116 | // controls contrast of the screen |
| el17arm | 0:fe19852199d2 | 117 | void contrast() |
| el17arm | 0:fe19852199d2 | 118 | { |
| el17arm | 0:fe19852199d2 | 119 | lcd.refresh(); |
| el17arm | 0:fe19852199d2 | 120 | float con = pot0.read(); |
| el17arm | 0:fe19852199d2 | 121 | lcd.setContrast(con); |
| el17arm | 1:813ba5341985 | 122 | lcd.clear(); |
| el17arm | 0:fe19852199d2 | 123 | } |
| el17arm | 0:fe19852199d2 | 124 | |
| el17arm | 45:bad704c546d4 | 125 | // displays start screen and waits for user to press start |
| el17arm | 0:fe19852199d2 | 126 | void start_screen() |
| el17arm | 58:4a826093d9e9 | 127 | { |
| el17arm | 64:b373b6bf8255 | 128 | while (pad.check_event(Gamepad::START_PRESSED) == false && options_flag == false) { |
| el17arm | 60:3df033345059 | 129 | lcd.clear(); |
| el17arm | 60:3df033345059 | 130 | lcd.drawSprite(2,20,8,3,(int *)miner_right); |
| el17arm | 60:3df033345059 | 131 | lcd.drawSprite(79,20,8,3,(int *)miner_left); |
| el17arm | 60:3df033345059 | 132 | lcd.drawRect(0,0,84,48, FILL_TRANSPARENT); |
| el17arm | 60:3df033345059 | 133 | lcd.printString("MANIC MILNER",7,1); |
| el17arm | 60:3df033345059 | 134 | lcd.printString("Press start!",8,2); |
| el17arm | 60:3df033345059 | 135 | lcd.printString("A = Controls",6,4); |
| el17arm | 60:3df033345059 | 136 | contrast(); |
| el17arm | 64:b373b6bf8255 | 137 | //printf("start screen = %d \n",options_flag); |
| el17arm | 60:3df033345059 | 138 | if (pad.check_event(Gamepad::A_PRESSED) == true) { |
| el17arm | 64:b373b6bf8255 | 139 | options_flag = true; |
| el17arm | 64:b373b6bf8255 | 140 | //printf("options = %d \n",options_flag); |
| el17arm | 60:3df033345059 | 141 | } |
| el17arm | 0:fe19852199d2 | 142 | } |
| el17arm | 19:4789cb4ca550 | 143 | } |
| el17arm | 63:ec95e155fb30 | 144 | // displays options screen |
| el17arm | 63:ec95e155fb30 | 145 | void option_screen() |
| el17arm | 63:ec95e155fb30 | 146 | { |
| el17arm | 64:b373b6bf8255 | 147 | while (options_flag == true) { |
| el17arm | 63:ec95e155fb30 | 148 | lcd.clear(); |
| el17arm | 63:ec95e155fb30 | 149 | lcd.printString("Stick = L/R",0,0); |
| el17arm | 63:ec95e155fb30 | 150 | lcd.printString("A = Jump ",0,1); |
| el17arm | 63:ec95e155fb30 | 151 | lcd.printString("Get keys and",0,2); |
| el17arm | 63:ec95e155fb30 | 152 | lcd.printString("return to exit",0,3); |
| el17arm | 63:ec95e155fb30 | 153 | lcd.printString("Led=time/lives",0,4); |
| el17arm | 63:ec95e155fb30 | 154 | lcd.printString("<< Press back",0,5); |
| el17arm | 63:ec95e155fb30 | 155 | //printf("options screen = %d \n",options); |
| el17arm | 63:ec95e155fb30 | 156 | lcd.refresh(); |
| el17arm | 63:ec95e155fb30 | 157 | if (pad.check_event(Gamepad::BACK_PRESSED) == true) { |
| el17arm | 64:b373b6bf8255 | 158 | options_flag = false; |
| el17arm | 63:ec95e155fb30 | 159 | start_screen(); |
| el17arm | 63:ec95e155fb30 | 160 | } |
| el17arm | 63:ec95e155fb30 | 161 | } |
| el17arm | 63:ec95e155fb30 | 162 | } |
| el17arm | 19:4789cb4ca550 | 163 | |
| el17arm | 45:bad704c546d4 | 164 | // displays appropriate screen with final score for game over and game complete |
| el17arm | 19:4789cb4ca550 | 165 | void restart() |
| el17arm | 19:4789cb4ca550 | 166 | { |
| el17arm | 29:d85886364643 | 167 | if (game.game_over() == true) { |
| el17arm | 19:4789cb4ca550 | 168 | lcd.clear(); |
| el17arm | 41:0cf320f73424 | 169 | game.get_score(lcd); |
| el17arm | 45:bad704c546d4 | 170 | lcd.drawRect(0,0,84,48, FILL_TRANSPARENT); |
| el17arm | 45:bad704c546d4 | 171 | lcd.printString("Game Over! ",14,1); |
| el17arm | 45:bad704c546d4 | 172 | lcd.printString("Score ",18,2); |
| el17arm | 60:3df033345059 | 173 | lcd.printString("Back = Replay",3,4); |
| el17arm | 44:e29458976114 | 174 | } |
| el17arm | 44:e29458976114 | 175 | if (game.game_complete(lcd) == true) { |
| el17arm | 44:e29458976114 | 176 | lcd.clear(); |
| el17arm | 44:e29458976114 | 177 | game.get_score(lcd); |
| el17arm | 45:bad704c546d4 | 178 | lcd.printString("YOU WIN!!! ",12,1); |
| el17arm | 45:bad704c546d4 | 179 | lcd.printString("Score ",18,2); |
| el17arm | 45:bad704c546d4 | 180 | lcd.printString("Well done!!!",8,4); |
| el17arm | 19:4789cb4ca550 | 181 | } |
| el17arm | 60:3df033345059 | 182 | if(pad.check_event(Gamepad::BACK_PRESSED) == true) { |
| el17arm | 60:3df033345059 | 183 | init(); |
| el17arm | 60:3df033345059 | 184 | } |
| el17arm | 60:3df033345059 | 185 | } |
| el17arm | 60:3df033345059 | 186 | |
| el17arm | 45:bad704c546d4 | 187 | // leds indicate how many lives and time left |
| el17arm | 41:0cf320f73424 | 188 | void leds() |
| el17arm | 58:4a826093d9e9 | 189 | { |
| el17arm | 45:bad704c546d4 | 190 | // fsm to display lives left |
| el17arm | 41:0cf320f73424 | 191 | int l_leds[4] = {0b111,0b110,0b100,0b000}; |
| el17arm | 45:bad704c546d4 | 192 | //fsm to display how much time (oxygen) left |
| el17arm | 41:0cf320f73424 | 193 | int r_leds[4] = {0b111,0b110,0b100,0b000}; |
| el17arm | 58:4a826093d9e9 | 194 | |
| el17arm | 45:bad704c546d4 | 195 | // function keeps track of time elapsed and changes leds accordingly |
| el17arm | 41:0cf320f73424 | 196 | oxygen = l_leds[game.oxygen_leds()]; |
| el17arm | 45:bad704c546d4 | 197 | // function keeps track of lives left and changes leds accordingly |
| el17arm | 41:0cf320f73424 | 198 | lives = r_leds[game.lives_leds()]; |
| el17arm | 37:4d525a37d5d2 | 199 | } |