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.
main.cpp@14:a57a40ff9430, 2018-05-08 (annotated)
- Committer:
- Andrew_M
- Date:
- Tue May 08 13:15:41 2018 +0000
- Revision:
- 14:a57a40ff9430
- Parent:
- 13:81573be8fac6
- Child:
- 15:130900e5c268
Minor restructuring and removal of unneeded functions. Doxygen has been utilised
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| Andrew_M | 0:66e5b37c127e | 1 | /* |
| Andrew_M | 0:66e5b37c127e | 2 | ELEC2645 Embedded Systems Project |
| Andrew_M | 0:66e5b37c127e | 3 | School of Electronic & Electrical Engineering |
| Andrew_M | 0:66e5b37c127e | 4 | University of Leeds |
| Andrew_M | 2:9ca5e1c221c3 | 5 | Name: Andrew Moore |
| Andrew_M | 2:9ca5e1c221c3 | 6 | Username: el16ajm |
| Andrew_M | 2:9ca5e1c221c3 | 7 | Student ID Number: 201042893 |
| Andrew_M | 8:9d01fd4a63ad | 8 | Date: |
| Andrew_M | 0:66e5b37c127e | 9 | */ |
| Andrew_M | 0:66e5b37c127e | 10 | |
| Andrew_M | 14:a57a40ff9430 | 11 | /** The Main class |
| Andrew_M | 14:a57a40ff9430 | 12 | * @brief Sends and recives data from the Menu and Engine classes, alters the refresh rate and calls all drawing from other classes |
| Andrew_M | 14:a57a40ff9430 | 13 | * @author Andrew J. Moore |
| Andrew_M | 14:a57a40ff9430 | 14 | * @date May, 2018 |
| Andrew_M | 14:a57a40ff9430 | 15 | */ |
| Andrew_M | 14:a57a40ff9430 | 16 | |
| Andrew_M | 0:66e5b37c127e | 17 | #include "mbed.h" |
| Andrew_M | 1:a14415de3ad5 | 18 | #include "Gamepad.h" |
| Andrew_M | 1:a14415de3ad5 | 19 | #include "N5110.h" |
| Andrew_M | 1:a14415de3ad5 | 20 | #include "Engine.h" |
| Andrew_M | 9:fe86ddbf7799 | 21 | #include "Menu.h" |
| Andrew_M | 0:66e5b37c127e | 22 | |
| Andrew_M | 1:a14415de3ad5 | 23 | /////////////// structs ///////////////// |
| Andrew_M | 1:a14415de3ad5 | 24 | struct UserInput { |
| Andrew_M | 1:a14415de3ad5 | 25 | Direction d; |
| Andrew_M | 1:a14415de3ad5 | 26 | }; |
| Andrew_M | 1:a14415de3ad5 | 27 | |
| Andrew_M | 1:a14415de3ad5 | 28 | /////////////// objects /////////////// |
| Andrew_M | 1:a14415de3ad5 | 29 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
| Andrew_M | 1:a14415de3ad5 | 30 | Gamepad pad; |
| Andrew_M | 1:a14415de3ad5 | 31 | Engine gameEngine; |
| Andrew_M | 9:fe86ddbf7799 | 32 | Menu mainMenu; |
| Andrew_M | 0:66e5b37c127e | 33 | |
| Andrew_M | 1:a14415de3ad5 | 34 | ///////////// prototypes /////////////// |
| Andrew_M | 14:a57a40ff9430 | 35 | /** Initialization function */ |
| Andrew_M | 1:a14415de3ad5 | 36 | void init(); |
| Andrew_M | 14:a57a40ff9430 | 37 | |
| Andrew_M | 14:a57a40ff9430 | 38 | /** Initialization function */ |
| Andrew_M | 1:a14415de3ad5 | 39 | void update_game(UserInput input); |
| Andrew_M | 14:a57a40ff9430 | 40 | |
| Andrew_M | 14:a57a40ff9430 | 41 | /** Draws the game onto the LCD */ |
| Andrew_M | 9:fe86ddbf7799 | 42 | void renderGame(); |
| Andrew_M | 14:a57a40ff9430 | 43 | |
| Andrew_M | 14:a57a40ff9430 | 44 | /** Draws the menu onto the LCD */ |
| Andrew_M | 9:fe86ddbf7799 | 45 | void renderMenu(); |
| Andrew_M | 14:a57a40ff9430 | 46 | |
| Andrew_M | 14:a57a40ff9430 | 47 | /** Draws the welcome screen onto the LCD */ |
| Andrew_M | 1:a14415de3ad5 | 48 | void welcome(); |
| Andrew_M | 14:a57a40ff9430 | 49 | |
| Andrew_M | 14:a57a40ff9430 | 50 | /** Draws the transition screen onto the LCD */ |
| Andrew_M | 10:279d3775d52c | 51 | void transition(); |
| Andrew_M | 14:a57a40ff9430 | 52 | |
| Andrew_M | 14:a57a40ff9430 | 53 | /** Resets game and menu logic once a game over state has been reached */ |
| Andrew_M | 12:d3eef5ea3f43 | 54 | void reset(); |
| Andrew_M | 14:a57a40ff9430 | 55 | |
| Andrew_M | 14:a57a40ff9430 | 56 | /** Sets the difficulty/refresh rate */ |
| Andrew_M | 11:b25874e7efe4 | 57 | float setDif(); |
| Andrew_M | 1:a14415de3ad5 | 58 | |
| Andrew_M | 1:a14415de3ad5 | 59 | ///////////// functions //////////////// |
| Andrew_M | 1:a14415de3ad5 | 60 | int main() |
| Andrew_M | 10:279d3775d52c | 61 | { |
| Andrew_M | 1:a14415de3ad5 | 62 | init(); |
| Andrew_M | 1:a14415de3ad5 | 63 | welcome(); |
| Andrew_M | 8:9d01fd4a63ad | 64 | |
| Andrew_M | 10:279d3775d52c | 65 | transition(); |
| Andrew_M | 11:b25874e7efe4 | 66 | |
| Andrew_M | 2:9ca5e1c221c3 | 67 | // game loop - read input, update the game state and render the display |
| Andrew_M | 2:9ca5e1c221c3 | 68 | while (1) { |
| Andrew_M | 10:279d3775d52c | 69 | if (!mainMenu.started()) { //menu logic and rendering |
| Andrew_M | 10:279d3775d52c | 70 | mainMenu.read_input(pad); |
| Andrew_M | 10:279d3775d52c | 71 | mainMenu.update(); |
| Andrew_M | 12:d3eef5ea3f43 | 72 | gameEngine.setLvl(mainMenu.getLvl()); |
| Andrew_M | 10:279d3775d52c | 73 | renderMenu(); |
| Andrew_M | 12:d3eef5ea3f43 | 74 | } else if (gameEngine.getGameOver()) { //resets the game once a game over state has been reached |
| Andrew_M | 12:d3eef5ea3f43 | 75 | gameEngine.gameOverScreen(lcd); |
| Andrew_M | 12:d3eef5ea3f43 | 76 | reset(); |
| Andrew_M | 10:279d3775d52c | 77 | } else { //game logic and rendering |
| Andrew_M | 9:fe86ddbf7799 | 78 | gameEngine.read_input(pad); |
| Andrew_M | 9:fe86ddbf7799 | 79 | gameEngine.update(pad); |
| Andrew_M | 9:fe86ddbf7799 | 80 | renderGame(); |
| Andrew_M | 9:fe86ddbf7799 | 81 | } |
| Andrew_M | 2:9ca5e1c221c3 | 82 | } |
| Andrew_M | 1:a14415de3ad5 | 83 | } |
| Andrew_M | 1:a14415de3ad5 | 84 | |
| Andrew_M | 1:a14415de3ad5 | 85 | void welcome() |
| Andrew_M | 1:a14415de3ad5 | 86 | { |
| Andrew_M | 8:9d01fd4a63ad | 87 | lcd.printString(" Snake! ",0,1); |
| Andrew_M | 1:a14415de3ad5 | 88 | lcd.printString(" Press Start ",0,4); |
| Andrew_M | 1:a14415de3ad5 | 89 | lcd.refresh(); |
| Andrew_M | 8:9d01fd4a63ad | 90 | |
| Andrew_M | 8:9d01fd4a63ad | 91 | // wait flashing LEDs until start button is pressed |
| Andrew_M | 1:a14415de3ad5 | 92 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
| Andrew_M | 1:a14415de3ad5 | 93 | pad.leds_on(); |
| Andrew_M | 1:a14415de3ad5 | 94 | wait(0.1); |
| Andrew_M | 1:a14415de3ad5 | 95 | pad.leds_off(); |
| Andrew_M | 1:a14415de3ad5 | 96 | wait(0.1); |
| Andrew_M | 0:66e5b37c127e | 97 | } |
| Andrew_M | 9:fe86ddbf7799 | 98 | |
| Andrew_M | 0:66e5b37c127e | 99 | } |
| Andrew_M | 0:66e5b37c127e | 100 | |
| Andrew_M | 1:a14415de3ad5 | 101 | void init() |
| Andrew_M | 1:a14415de3ad5 | 102 | { |
| Andrew_M | 8:9d01fd4a63ad | 103 | // need to initialise LCD and Gamepad |
| Andrew_M | 3:6253a2d374fa | 104 | gameEngine.init(); |
| Andrew_M | 1:a14415de3ad5 | 105 | lcd.init(); |
| Andrew_M | 8:9d01fd4a63ad | 106 | pad.init(); |
| Andrew_M | 9:fe86ddbf7799 | 107 | mainMenu.init(); |
| Andrew_M | 1:a14415de3ad5 | 108 | |
| Andrew_M | 1:a14415de3ad5 | 109 | } |
| Andrew_M | 2:9ca5e1c221c3 | 110 | |
| Andrew_M | 12:d3eef5ea3f43 | 111 | void reset() |
| Andrew_M | 12:d3eef5ea3f43 | 112 | { |
| Andrew_M | 12:d3eef5ea3f43 | 113 | mainMenu.init(); |
| Andrew_M | 12:d3eef5ea3f43 | 114 | gameEngine.init(); |
| Andrew_M | 12:d3eef5ea3f43 | 115 | } |
| Andrew_M | 12:d3eef5ea3f43 | 116 | |
| Andrew_M | 9:fe86ddbf7799 | 117 | void renderGame() |
| Andrew_M | 2:9ca5e1c221c3 | 118 | { |
| Andrew_M | 2:9ca5e1c221c3 | 119 | // clear screen, re-draw and refresh |
| Andrew_M | 8:9d01fd4a63ad | 120 | lcd.clear(); |
| Andrew_M | 2:9ca5e1c221c3 | 121 | gameEngine.draw(lcd); |
| Andrew_M | 2:9ca5e1c221c3 | 122 | lcd.refresh(); |
| Andrew_M | 11:b25874e7efe4 | 123 | wait(setDif()); |
| Andrew_M | 2:9ca5e1c221c3 | 124 | } |
| Andrew_M | 8:9d01fd4a63ad | 125 | |
| Andrew_M | 9:fe86ddbf7799 | 126 | void renderMenu() |
| Andrew_M | 9:fe86ddbf7799 | 127 | { |
| Andrew_M | 9:fe86ddbf7799 | 128 | // clear screen, re-draw and refresh |
| Andrew_M | 9:fe86ddbf7799 | 129 | lcd.clear(); |
| Andrew_M | 9:fe86ddbf7799 | 130 | mainMenu.draw(lcd); |
| Andrew_M | 9:fe86ddbf7799 | 131 | lcd.refresh(); |
| Andrew_M | 9:fe86ddbf7799 | 132 | wait(1.0f/8); |
| Andrew_M | 9:fe86ddbf7799 | 133 | } |
| Andrew_M | 9:fe86ddbf7799 | 134 | |
| Andrew_M | 10:279d3775d52c | 135 | void transition() |
| Andrew_M | 11:b25874e7efe4 | 136 | { |
| Andrew_M | 11:b25874e7efe4 | 137 | //transition animation between modes to stop 'ghosting' of inputs |
| Andrew_M | 10:279d3775d52c | 138 | for (int i = 0; i <= 11; i++) { |
| Andrew_M | 10:279d3775d52c | 139 | for (int j = 0; j <= 21; j++) { |
| Andrew_M | 10:279d3775d52c | 140 | lcd.drawRect(j*4,i*4,4,4,FILL_BLACK); |
| Andrew_M | 10:279d3775d52c | 141 | wait(0.005); |
| Andrew_M | 10:279d3775d52c | 142 | lcd.refresh(); |
| Andrew_M | 10:279d3775d52c | 143 | } |
| Andrew_M | 10:279d3775d52c | 144 | } |
| Andrew_M | 10:279d3775d52c | 145 | |
| Andrew_M | 10:279d3775d52c | 146 | } |
| Andrew_M | 10:279d3775d52c | 147 | |
| Andrew_M | 11:b25874e7efe4 | 148 | float setDif() |
| Andrew_M | 11:b25874e7efe4 | 149 | { |
| Andrew_M | 11:b25874e7efe4 | 150 | int _dif = mainMenu.getDif(); |
| Andrew_M | 11:b25874e7efe4 | 151 | |
| Andrew_M | 13:81573be8fac6 | 152 | if (_dif == 1) { //Low difficulty |
| Andrew_M | 11:b25874e7efe4 | 153 | return 1.0f/4; |
| Andrew_M | 13:81573be8fac6 | 154 | } else if (_dif == 2) { //Medium difficulty |
| Andrew_M | 11:b25874e7efe4 | 155 | return 1.0f/8; |
| Andrew_M | 13:81573be8fac6 | 156 | } else if (_dif == 3) { //Hard difficulty |
| Andrew_M | 11:b25874e7efe4 | 157 | return 1.0f/12; |
| Andrew_M | 13:81573be8fac6 | 158 | } else { //Adaptive difficulty |
| Andrew_M | 13:81573be8fac6 | 159 | float _delta = gameEngine.getScore(); |
| Andrew_M | 13:81573be8fac6 | 160 | if (_delta < 25) { //Limits FPS to 25 |
| Andrew_M | 14:a57a40ff9430 | 161 | return ((1.0f/(_delta + 1.0))); |
| Andrew_M | 11:b25874e7efe4 | 162 | } else { |
| Andrew_M | 11:b25874e7efe4 | 163 | return (1.0f/25); |
| Andrew_M | 11:b25874e7efe4 | 164 | } |
| Andrew_M | 11:b25874e7efe4 | 165 | } |
| Andrew_M | 11:b25874e7efe4 | 166 | } |