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@56:8c827d1cae3b, 2019-04-24 (annotated)
- Committer:
- el17arm
- Date:
- Wed Apr 24 20:09:22 2019 +0000
- Revision:
- 56:8c827d1cae3b
- Parent:
- 48:bd1f31fbfee3
- Child:
- 58:4a826093d9e9
Documentation update
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 | 0:fe19852199d2 | 13 | #include "mbed.h" |
el17arm | 0:fe19852199d2 | 14 | #include "N5110.h" |
el17arm | 0:fe19852199d2 | 15 | #include "Gamepad.h" |
el17arm | 29:d85886364643 | 16 | #include "Gameengine.h" |
el17arm | 0:fe19852199d2 | 17 | |
el17arm | 45:bad704c546d4 | 18 | /////////////// objects /////////////// |
el17arm | 45:bad704c546d4 | 19 | |
el17arm | 29:d85886364643 | 20 | Gameengine game; |
el17arm | 0:fe19852199d2 | 21 | Gamepad pad; |
el17arm | 0:fe19852199d2 | 22 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
el17arm | 0:fe19852199d2 | 23 | AnalogIn pot0(PTB2); |
el17arm | 41:0cf320f73424 | 24 | BusOut oxygen(PTA1, PTA2, PTC2); |
el17arm | 41:0cf320f73424 | 25 | BusOut lives(PTC3, PTC4, PTD3); |
el17arm | 41:0cf320f73424 | 26 | |
el17arm | 45:bad704c546d4 | 27 | ///////////// prototypes /////////////// |
el17arm | 45:bad704c546d4 | 28 | |
el17arm | 45:bad704c546d4 | 29 | /** |
el17arm | 45:bad704c546d4 | 30 | * @brief sets the contrast of lcd screen |
el17arm | 45:bad704c546d4 | 31 | * @details contrast is adjust with analog input PTB2 |
el17arm | 45:bad704c546d4 | 32 | */ |
el17arm | 0:fe19852199d2 | 33 | void contrast(); |
el17arm | 56:8c827d1cae3b | 34 | /** Initialises all classes and libraries and default game settings |
el17arm | 45:bad704c546d4 | 35 | * @details initialises LCD and gamepad library and sets screen brightness |
el17arm | 45:bad704c546d4 | 36 | */ |
el17arm | 0:fe19852199d2 | 37 | void init(); |
el17arm | 56:8c827d1cae3b | 38 | /** Displays opening screen |
el17arm | 45:bad704c546d4 | 39 | * @details Screen displays title of game and instructs player to press start to begin |
el17arm | 45:bad704c546d4 | 40 | */ |
el17arm | 0:fe19852199d2 | 41 | void start_screen(); |
el17arm | 56:8c827d1cae3b | 42 | /** Controls LEDs on front panel. |
el17arm | 45:bad704c546d4 | 43 | * @details Every time a player dies a RHS LED is turned off, as time reduces |
el17arm | 56:8c827d1cae3b | 44 | * LEDs turn off on LHS. |
el17arm | 45:bad704c546d4 | 45 | */ |
el17arm | 41:0cf320f73424 | 46 | void leds(); |
el17arm | 56:8c827d1cae3b | 47 | /** Initialises and renders all level objects |
el17arm | 45:bad704c546d4 | 48 | * @details Contains functions for all levels and will display next level objects |
el17arm | 45:bad704c546d4 | 49 | * once player has completed each level |
el17arm | 45:bad704c546d4 | 50 | */ |
el17arm | 15:2bda80896a84 | 51 | void render(); |
el17arm | 56:8c827d1cae3b | 52 | /** Displays screen if game over or game complete |
el17arm | 56:8c827d1cae3b | 53 | * @details Screen will display player's final score and display game over or game complete |
el17arm | 45:bad704c546d4 | 54 | */ |
el17arm | 19:4789cb4ca550 | 55 | void restart(); |
el17arm | 45:bad704c546d4 | 56 | |
el17arm | 45:bad704c546d4 | 57 | ///////////// functions //////////////// |
el17arm | 45:bad704c546d4 | 58 | |
el17arm | 0:fe19852199d2 | 59 | int main() |
el17arm | 0:fe19852199d2 | 60 | { |
el17arm | 45:bad704c546d4 | 61 | init(); // initialises screen and default game settings |
el17arm | 45:bad704c546d4 | 62 | start_screen(); // waits for user to press start to begin |
el17arm | 45:bad704c546d4 | 63 | |
el17arm | 45:bad704c546d4 | 64 | // game loop, reads input and updates game accordingly |
el17arm | 0:fe19852199d2 | 65 | while (1) { |
el17arm | 41:0cf320f73424 | 66 | |
el17arm | 45:bad704c546d4 | 67 | contrast(); // adjusts contrast of screen |
el17arm | 45:bad704c546d4 | 68 | render(); // draws all level objects on to screen |
el17arm | 45:bad704c546d4 | 69 | game.update(lcd, pad); // updates movement, collected keys, lives, time left |
el17arm | 45:bad704c546d4 | 70 | restart(); // restart screen if game over or end screen if game complete displays final score |
el17arm | 45:bad704c546d4 | 71 | wait(0.1); // wait function to control fps |
el17arm | 45:bad704c546d4 | 72 | } |
el17arm | 45:bad704c546d4 | 73 | } |
el17arm | 48:bd1f31fbfee3 | 74 | |
el17arm | 41:0cf320f73424 | 75 | |
el17arm | 45:bad704c546d4 | 76 | //initialises all classes and libraries and default game settings |
el17arm | 0:fe19852199d2 | 77 | void init() |
el17arm | 0:fe19852199d2 | 78 | { |
el17arm | 0:fe19852199d2 | 79 | lcd.init(); |
el17arm | 0:fe19852199d2 | 80 | lcd.normalMode(); // normal colour mode |
el17arm | 0:fe19852199d2 | 81 | lcd.refresh(); |
el17arm | 41:0cf320f73424 | 82 | lcd.setBrightness(1.0); |
el17arm | 0:fe19852199d2 | 83 | pad.init(); |
el17arm | 29:d85886364643 | 84 | game.game_init(); |
el17arm | 45:bad704c546d4 | 85 | contrast(); |
el17arm | 15:2bda80896a84 | 86 | } |
el17arm | 2:725c213b2396 | 87 | |
el17arm | 45:bad704c546d4 | 88 | // detects which level is being played and draws the level objects with collision settings |
el17arm | 15:2bda80896a84 | 89 | void render() |
el17arm | 15:2bda80896a84 | 90 | { |
el17arm | 45:bad704c546d4 | 91 | game.draw_l1(lcd, pad); // level 1 |
el17arm | 45:bad704c546d4 | 92 | game.draw_l2(lcd, pad); // level 2 |
el17arm | 45:bad704c546d4 | 93 | game.draw_l3(lcd, pad); // level 3 |
el17arm | 45:bad704c546d4 | 94 | leds(); // function to update leds to reflect time and lives left |
el17arm | 0:fe19852199d2 | 95 | } |
el17arm | 0:fe19852199d2 | 96 | |
el17arm | 45:bad704c546d4 | 97 | // controls contrast of the screen |
el17arm | 0:fe19852199d2 | 98 | void contrast() |
el17arm | 0:fe19852199d2 | 99 | { |
el17arm | 0:fe19852199d2 | 100 | lcd.refresh(); |
el17arm | 0:fe19852199d2 | 101 | float con = pot0.read(); |
el17arm | 0:fe19852199d2 | 102 | lcd.setContrast(con); |
el17arm | 1:813ba5341985 | 103 | lcd.clear(); |
el17arm | 0:fe19852199d2 | 104 | } |
el17arm | 0:fe19852199d2 | 105 | |
el17arm | 45:bad704c546d4 | 106 | // displays start screen and waits for user to press start |
el17arm | 0:fe19852199d2 | 107 | void start_screen() |
el17arm | 45:bad704c546d4 | 108 | { |
el17arm | 45:bad704c546d4 | 109 | lcd.drawSprite(2,20,8,3,(int *)miner_right); |
el17arm | 45:bad704c546d4 | 110 | lcd.drawSprite(79,20,8,3,(int *)miner_left); |
el17arm | 45:bad704c546d4 | 111 | lcd.drawRect(0,0,84,48, FILL_TRANSPARENT); |
el17arm | 45:bad704c546d4 | 112 | lcd.printString("MANIC MILNER",7,1); |
el17arm | 45:bad704c546d4 | 113 | lcd.printString("Press start!",8,4); |
el17arm | 3:d27ee2440829 | 114 | lcd.refresh(); |
el17arm | 0:fe19852199d2 | 115 | while ( pad.check_event(Gamepad::START_PRESSED) == false) { |
el17arm | 0:fe19852199d2 | 116 | } |
el17arm | 19:4789cb4ca550 | 117 | } |
el17arm | 19:4789cb4ca550 | 118 | |
el17arm | 45:bad704c546d4 | 119 | // displays appropriate screen with final score for game over and game complete |
el17arm | 19:4789cb4ca550 | 120 | void restart() |
el17arm | 19:4789cb4ca550 | 121 | { |
el17arm | 29:d85886364643 | 122 | if (game.game_over() == true) { |
el17arm | 19:4789cb4ca550 | 123 | lcd.clear(); |
el17arm | 41:0cf320f73424 | 124 | game.get_score(lcd); |
el17arm | 45:bad704c546d4 | 125 | lcd.drawRect(0,0,84,48, FILL_TRANSPARENT); |
el17arm | 45:bad704c546d4 | 126 | lcd.printString("Game Over! ",14,1); |
el17arm | 45:bad704c546d4 | 127 | lcd.printString("Score ",18,2); |
el17arm | 45:bad704c546d4 | 128 | lcd.printString("Play Again?",10,4); |
el17arm | 44:e29458976114 | 129 | } |
el17arm | 44:e29458976114 | 130 | if (game.game_complete(lcd) == true) { |
el17arm | 44:e29458976114 | 131 | lcd.clear(); |
el17arm | 44:e29458976114 | 132 | game.get_score(lcd); |
el17arm | 45:bad704c546d4 | 133 | lcd.drawRect(0,0,84,48, FILL_TRANSPARENT); |
el17arm | 45:bad704c546d4 | 134 | lcd.printString("YOU WIN!!! ",12,1); |
el17arm | 45:bad704c546d4 | 135 | lcd.printString("Score ",18,2); |
el17arm | 45:bad704c546d4 | 136 | lcd.printString("Well done!!!",8,4); |
el17arm | 19:4789cb4ca550 | 137 | } |
el17arm | 41:0cf320f73424 | 138 | } |
el17arm | 41:0cf320f73424 | 139 | |
el17arm | 45:bad704c546d4 | 140 | // leds indicate how many lives and time left |
el17arm | 41:0cf320f73424 | 141 | void leds() |
el17arm | 45:bad704c546d4 | 142 | { |
el17arm | 45:bad704c546d4 | 143 | // fsm to display lives left |
el17arm | 41:0cf320f73424 | 144 | int l_leds[4] = {0b111,0b110,0b100,0b000}; |
el17arm | 45:bad704c546d4 | 145 | //fsm to display how much time (oxygen) left |
el17arm | 41:0cf320f73424 | 146 | int r_leds[4] = {0b111,0b110,0b100,0b000}; |
el17arm | 45:bad704c546d4 | 147 | |
el17arm | 45:bad704c546d4 | 148 | // function keeps track of time elapsed and changes leds accordingly |
el17arm | 41:0cf320f73424 | 149 | oxygen = l_leds[game.oxygen_leds()]; |
el17arm | 45:bad704c546d4 | 150 | // function keeps track of lives left and changes leds accordingly |
el17arm | 41:0cf320f73424 | 151 | lives = r_leds[game.lives_leds()]; |
el17arm | 37:4d525a37d5d2 | 152 | } |