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@22:4e305ff8a050, 2019-04-18 (annotated)
- Committer:
- JamesCummins
- Date:
- Thu Apr 18 11:50:53 2019 +0000
- Revision:
- 22:4e305ff8a050
- Parent:
- 20:4a39a1a2be51
- Child:
- 23:61fa82f76808
Options Menu appears to be working. Change Brightness working (other than that it skips 0.7???) Need to complete adjust ball speed and high scores list. (Still need to save to SD card somehow)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
JamesCummins | 0:7c96d84b673e | 1 | /* |
JamesCummins | 0:7c96d84b673e | 2 | ELEC2645 Embedded Systems Project |
JamesCummins | 0:7c96d84b673e | 3 | School of Electronic & Electrical Engineering |
JamesCummins | 0:7c96d84b673e | 4 | University of Leeds |
JamesCummins | 0:7c96d84b673e | 5 | Name: James Nathan Cummins |
JamesCummins | 0:7c96d84b673e | 6 | Username: el17jnc |
JamesCummins | 0:7c96d84b673e | 7 | Student ID Number: 201096364 |
JamesCummins | 0:7c96d84b673e | 8 | Date: 22/03/19 |
JamesCummins | 0:7c96d84b673e | 9 | */ |
JamesCummins | 0:7c96d84b673e | 10 | |
JamesCummins | 0:7c96d84b673e | 11 | #include "Gamepad.h" |
JamesCummins | 0:7c96d84b673e | 12 | #include "mbed.h" |
JamesCummins | 1:99d524f81566 | 13 | #include "N5110.h" |
JamesCummins | 20:4a39a1a2be51 | 14 | #include "BrickBreakerEngine.h" |
JamesCummins | 22:4e305ff8a050 | 15 | #include "OptionsEngine.h" |
JamesCummins | 13:e5a36fbd48ae | 16 | #define RADIUS 3 |
JamesCummins | 0:7c96d84b673e | 17 | |
JamesCummins | 11:2cf0d4ce8677 | 18 | |
JamesCummins | 11:2cf0d4ce8677 | 19 | //Objects |
JamesCummins | 6:952ac12c7f00 | 20 | Gamepad gamepad; |
JamesCummins | 0:7c96d84b673e | 21 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
JamesCummins | 20:4a39a1a2be51 | 22 | BrickBreakerEngine brick; |
JamesCummins | 22:4e305ff8a050 | 23 | OptionsEngine opt; |
JamesCummins | 11:2cf0d4ce8677 | 24 | AnalogIn randnoise(PTB0); |
JamesCummins | 12:9982239b7906 | 25 | FXOS8700CQ accelerometer(I2C_SDA,I2C_SCL); |
JamesCummins | 11:2cf0d4ce8677 | 26 | |
JamesCummins | 20:4a39a1a2be51 | 27 | //Enumeric class and struct for game mode selection menu |
JamesCummins | 20:4a39a1a2be51 | 28 | enum StartOption{ |
JamesCummins | 20:4a39a1a2be51 | 29 | CLASSIC, |
JamesCummins | 20:4a39a1a2be51 | 30 | BRICKBREAKER, |
JamesCummins | 20:4a39a1a2be51 | 31 | OPTIONS |
JamesCummins | 20:4a39a1a2be51 | 32 | }; |
JamesCummins | 20:4a39a1a2be51 | 33 | |
JamesCummins | 20:4a39a1a2be51 | 34 | struct StartSelection{ |
JamesCummins | 20:4a39a1a2be51 | 35 | int output; |
JamesCummins | 20:4a39a1a2be51 | 36 | StartOption next_state[3]; |
JamesCummins | 20:4a39a1a2be51 | 37 | }; |
JamesCummins | 20:4a39a1a2be51 | 38 | |
JamesCummins | 11:2cf0d4ce8677 | 39 | //Methods |
JamesCummins | 7:6eb9cade57ab | 40 | void startscreen(); |
JamesCummins | 17:5104ecef5bd0 | 41 | StartOption menu(); |
JamesCummins | 12:9982239b7906 | 42 | void init(); |
JamesCummins | 15:1564bd6b713d | 43 | void print_start_menu(int output); |
JamesCummins | 8:4e306b16a941 | 44 | |
JamesCummins | 11:2cf0d4ce8677 | 45 | |
JamesCummins | 17:5104ecef5bd0 | 46 | ////////////////Main Function///////////////// |
JamesCummins | 8:4e306b16a941 | 47 | |
JamesCummins | 8:4e306b16a941 | 48 | |
JamesCummins | 0:7c96d84b673e | 49 | int main(){ |
JamesCummins | 22:4e305ff8a050 | 50 | int fps = 30; |
JamesCummins | 10:40c77d69e83c | 51 | init(); |
JamesCummins | 7:6eb9cade57ab | 52 | startscreen(); |
JamesCummins | 16:e846864778c4 | 53 | while(1){ |
JamesCummins | 17:5104ecef5bd0 | 54 | StartOption choice_selected = menu(); |
JamesCummins | 20:4a39a1a2be51 | 55 | if(choice_selected == CLASSIC){ /*engine.classic_mode(accelerometer, gamepad, lcd, fps);*/} |
JamesCummins | 20:4a39a1a2be51 | 56 | if(choice_selected == BRICKBREAKER){ brick.brickbreaker_mode(accelerometer, gamepad, lcd, randnoise, fps);} |
JamesCummins | 22:4e305ff8a050 | 57 | if(choice_selected == OPTIONS){ opt.options_menu(gamepad, lcd);} |
JamesCummins | 16:e846864778c4 | 58 | } |
JamesCummins | 0:7c96d84b673e | 59 | } |
JamesCummins | 8:4e306b16a941 | 60 | |
JamesCummins | 8:4e306b16a941 | 61 | |
JamesCummins | 11:2cf0d4ce8677 | 62 | |
JamesCummins | 8:4e306b16a941 | 63 | //////////////Start up functions/////////////////// |
JamesCummins | 11:2cf0d4ce8677 | 64 | |
JamesCummins | 10:40c77d69e83c | 65 | void init(){ |
JamesCummins | 10:40c77d69e83c | 66 | gamepad.init(); |
JamesCummins | 10:40c77d69e83c | 67 | lcd.init(); |
JamesCummins | 10:40c77d69e83c | 68 | lcd.setContrast(0.55); |
JamesCummins | 20:4a39a1a2be51 | 69 | brick.init(RADIUS); |
JamesCummins | 22:4e305ff8a050 | 70 | opt.init(); |
JamesCummins | 18:08046153e6ad | 71 | accelerometer.init(); |
JamesCummins | 10:40c77d69e83c | 72 | wait(1); |
JamesCummins | 10:40c77d69e83c | 73 | } |
JamesCummins | 0:7c96d84b673e | 74 | |
JamesCummins | 7:6eb9cade57ab | 75 | void startscreen() { |
JamesCummins | 1:99d524f81566 | 76 | lcd.clear(); |
JamesCummins | 5:7e8f5fad7b6b | 77 | char gamename[] = {'L', 'A', 'B', 'Y', 'R', 'I', 'N', 'T', 'H', ' ', ' ', '\0'}; |
JamesCummins | 3:f2e5ffd2b94c | 78 | int i = 0; |
JamesCummins | 5:7e8f5fad7b6b | 79 | for(int a = 0; a < 35; a++){ |
JamesCummins | 4:2f01b85e57f9 | 80 | lcd.clear(); |
JamesCummins | 4:2f01b85e57f9 | 81 | lcd.drawCircle(24+2*a, 21, 3, FILL_BLACK); |
JamesCummins | 4:2f01b85e57f9 | 82 | for (i = 0; i < a/3; i++) { |
JamesCummins | 4:2f01b85e57f9 | 83 | lcd.printChar(gamename[i], 15+i*6, 2); |
JamesCummins | 4:2f01b85e57f9 | 84 | lcd.refresh(); |
JamesCummins | 4:2f01b85e57f9 | 85 | } |
JamesCummins | 15:1564bd6b713d | 86 | wait_ms(50); |
JamesCummins | 4:2f01b85e57f9 | 87 | } |
JamesCummins | 5:7e8f5fad7b6b | 88 | lcd.printString("Press start", 9, 4); |
JamesCummins | 5:7e8f5fad7b6b | 89 | lcd.printString("to play >", 15, 5); |
JamesCummins | 6:952ac12c7f00 | 90 | lcd.refresh(); |
JamesCummins | 6:952ac12c7f00 | 91 | bool advance = false; |
JamesCummins | 6:952ac12c7f00 | 92 | while (!advance){ |
JamesCummins | 6:952ac12c7f00 | 93 | if (gamepad.check_event(gamepad.START_PRESSED)){ |
JamesCummins | 6:952ac12c7f00 | 94 | lcd.clear(); |
JamesCummins | 6:952ac12c7f00 | 95 | lcd.refresh(); |
JamesCummins | 6:952ac12c7f00 | 96 | advance = true; |
JamesCummins | 6:952ac12c7f00 | 97 | } |
JamesCummins | 6:952ac12c7f00 | 98 | else { advance = false; } |
JamesCummins | 6:952ac12c7f00 | 99 | } |
JamesCummins | 0:7c96d84b673e | 100 | } |
JamesCummins | 7:6eb9cade57ab | 101 | |
JamesCummins | 17:5104ecef5bd0 | 102 | StartOption menu(){ |
JamesCummins | 15:1564bd6b713d | 103 | StartSelection fsm[3] = { |
JamesCummins | 14:108052b6222b | 104 | {0,{OPTIONS,BRICKBREAKER,CLASSIC}}, |
JamesCummins | 14:108052b6222b | 105 | {2,{CLASSIC,OPTIONS,BRICKBREAKER}}, |
JamesCummins | 14:108052b6222b | 106 | {4,{BRICKBREAKER,CLASSIC,OPTIONS}} |
JamesCummins | 7:6eb9cade57ab | 107 | }; |
JamesCummins | 14:108052b6222b | 108 | StartOption state = CLASSIC; //start with the arrow on the top option |
JamesCummins | 7:6eb9cade57ab | 109 | int next = 2; //next_state = 2 so that by default it doesn't change arrow position |
JamesCummins | 22:4e305ff8a050 | 110 | while(!(gamepad.check_event(gamepad.A_PRESSED))){ //select choice by pushing joystick to the right |
JamesCummins | 7:6eb9cade57ab | 111 | state = fsm[state].next_state[next]; |
JamesCummins | 7:6eb9cade57ab | 112 | lcd.clear(); |
JamesCummins | 7:6eb9cade57ab | 113 | if(gamepad.get_direction() == N){ next = 0;} |
JamesCummins | 7:6eb9cade57ab | 114 | else if(gamepad.get_direction() == S){ next = 1;} |
JamesCummins | 7:6eb9cade57ab | 115 | else {next = 2;} |
JamesCummins | 15:1564bd6b713d | 116 | print_start_menu(fsm[state].output); |
JamesCummins | 7:6eb9cade57ab | 117 | lcd.refresh(); |
JamesCummins | 7:6eb9cade57ab | 118 | wait(0.25); |
JamesCummins | 7:6eb9cade57ab | 119 | } |
JamesCummins | 7:6eb9cade57ab | 120 | lcd.clear(); |
JamesCummins | 7:6eb9cade57ab | 121 | lcd.refresh(); |
JamesCummins | 7:6eb9cade57ab | 122 | return state; |
JamesCummins | 15:1564bd6b713d | 123 | } |
JamesCummins | 15:1564bd6b713d | 124 | |
JamesCummins | 15:1564bd6b713d | 125 | void print_start_menu(int output){ |
JamesCummins | 15:1564bd6b713d | 126 | lcd.printString(">", 0, output); |
JamesCummins | 15:1564bd6b713d | 127 | lcd.printString("Classic", 36, 0); |
JamesCummins | 15:1564bd6b713d | 128 | lcd.printString("BrickBreak", 18, 2); |
JamesCummins | 15:1564bd6b713d | 129 | lcd.printString("Options", 36, 4); |
JamesCummins | 22:4e305ff8a050 | 130 | lcd.printString("(A = Select)", 12, 5); |
JamesCummins | 7:6eb9cade57ab | 131 | } |