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@2:6baf849b0270, 2019-05-05 (annotated)
- Committer:
- Kern_EL17KJTF
- Date:
- Sun May 05 01:03:27 2019 +0000
- Revision:
- 2:6baf849b0270
- Parent:
- 1:7a0917df015a
- Child:
- 3:b248dc1f3e8d
Menu system implemented - Navigation via joystick, A button to go to state, back button to return to the menu from state.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Kern_EL17KJTF | 0:0130fd5738f7 | 1 | /* |
Kern_EL17KJTF | 0:0130fd5738f7 | 2 | ELEC2645 Embedded Systems Project |
Kern_EL17KJTF | 0:0130fd5738f7 | 3 | School of Electronic & Electrical Engineering |
Kern_EL17KJTF | 0:0130fd5738f7 | 4 | University of Leeds |
Kern_EL17KJTF | 0:0130fd5738f7 | 5 | |
Kern_EL17KJTF | 0:0130fd5738f7 | 6 | Name: Kern Fowler |
Kern_EL17KJTF | 0:0130fd5738f7 | 7 | Username: el17kjtf |
Kern_EL17KJTF | 0:0130fd5738f7 | 8 | Student ID Number: 201116686 |
Kern_EL17KJTF | 0:0130fd5738f7 | 9 | Date: 19/02/2019 |
Kern_EL17KJTF | 2:6baf849b0270 | 10 | Version: 1.2 |
Kern_EL17KJTF | 1:7a0917df015a | 11 | |
Kern_EL17KJTF | 1:7a0917df015a | 12 | */ |
Kern_EL17KJTF | 1:7a0917df015a | 13 | #include "main.h" |
Kern_EL17KJTF | 1:7a0917df015a | 14 | |
Kern_EL17KJTF | 1:7a0917df015a | 15 | |
Kern_EL17KJTF | 2:6baf849b0270 | 16 | int fps = 24; |
Kern_EL17KJTF | 2:6baf849b0270 | 17 | int direction; |
Kern_EL17KJTF | 2:6baf849b0270 | 18 | int option_pos = 0; |
Kern_EL17KJTF | 2:6baf849b0270 | 19 | int arrow_pos = 0; |
Kern_EL17KJTF | 2:6baf849b0270 | 20 | |
Kern_EL17KJTF | 2:6baf849b0270 | 21 | int main() { |
Kern_EL17KJTF | 1:7a0917df015a | 22 | init(); // initialise peripherals |
Kern_EL17KJTF | 1:7a0917df015a | 23 | welcome(); // display welcome message |
Kern_EL17KJTF | 1:7a0917df015a | 24 | |
Kern_EL17KJTF | 1:7a0917df015a | 25 | while(1) { // infinite loop |
Kern_EL17KJTF | 2:6baf849b0270 | 26 | arrow_location(); |
Kern_EL17KJTF | 2:6baf849b0270 | 27 | print_menu(); // this re-prints the menu at the start of every loop |
Kern_EL17KJTF | 2:6baf849b0270 | 28 | arrow_select(); |
Kern_EL17KJTF | 2:6baf849b0270 | 29 | wait_ms(1.0f/fps); |
Kern_EL17KJTF | 1:7a0917df015a | 30 | } |
Kern_EL17KJTF | 1:7a0917df015a | 31 | } |
Kern_EL17KJTF | 1:7a0917df015a | 32 | |
Kern_EL17KJTF | 2:6baf849b0270 | 33 | void init() { |
Kern_EL17KJTF | 1:7a0917df015a | 34 | // initialise LCD |
Kern_EL17KJTF | 1:7a0917df015a | 35 | lcd.init(); |
Kern_EL17KJTF | 1:7a0917df015a | 36 | // initialise Gamepad |
Kern_EL17KJTF | 1:7a0917df015a | 37 | pad.init(); |
Kern_EL17KJTF | 2:6baf849b0270 | 38 | wait(2.5); |
Kern_EL17KJTF | 2:6baf849b0270 | 39 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 40 | |
Kern_EL17KJTF | 2:6baf849b0270 | 41 | void arrow_location() { |
Kern_EL17KJTF | 2:6baf849b0270 | 42 | direction = pad.get_direction(); |
Kern_EL17KJTF | 2:6baf849b0270 | 43 | if (direction == N) { |
Kern_EL17KJTF | 2:6baf849b0270 | 44 | option_pos = option_pos - 1; |
Kern_EL17KJTF | 2:6baf849b0270 | 45 | // printf("North Pressed"); |
Kern_EL17KJTF | 2:6baf849b0270 | 46 | wait_ms(250); |
Kern_EL17KJTF | 2:6baf849b0270 | 47 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 48 | if (direction == S) { |
Kern_EL17KJTF | 2:6baf849b0270 | 49 | option_pos = option_pos + 1; |
Kern_EL17KJTF | 2:6baf849b0270 | 50 | // printf("South Pressed"); |
Kern_EL17KJTF | 2:6baf849b0270 | 51 | wait_ms(250); |
Kern_EL17KJTF | 2:6baf849b0270 | 52 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 53 | if (option_pos > 3) { |
Kern_EL17KJTF | 2:6baf849b0270 | 54 | option_pos = 0; |
Kern_EL17KJTF | 2:6baf849b0270 | 55 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 56 | if (option_pos < 0) { |
Kern_EL17KJTF | 2:6baf849b0270 | 57 | option_pos = 3; |
Kern_EL17KJTF | 2:6baf849b0270 | 58 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 59 | arrow_pos = 16 + (option_pos * 8); |
Kern_EL17KJTF | 2:6baf849b0270 | 60 | // printf("Option Num = %d", option_pos) |
Kern_EL17KJTF | 2:6baf849b0270 | 61 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 62 | |
Kern_EL17KJTF | 2:6baf849b0270 | 63 | void arrow_select() { |
Kern_EL17KJTF | 2:6baf849b0270 | 64 | if (pad.check_event(Gamepad::A_PRESSED) == true) { |
Kern_EL17KJTF | 2:6baf849b0270 | 65 | if (option_pos == 0) { |
Kern_EL17KJTF | 2:6baf849b0270 | 66 | // printf("GameEngine"); |
Kern_EL17KJTF | 2:6baf849b0270 | 67 | game_engine_run(); |
Kern_EL17KJTF | 2:6baf849b0270 | 68 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 69 | if (option_pos == 1) { |
Kern_EL17KJTF | 2:6baf849b0270 | 70 | // printf("Controls"); |
Kern_EL17KJTF | 2:6baf849b0270 | 71 | controls_run(); |
Kern_EL17KJTF | 2:6baf849b0270 | 72 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 73 | if (option_pos == 2) { |
Kern_EL17KJTF | 2:6baf849b0270 | 74 | // printf("Instructions"); |
Kern_EL17KJTF | 2:6baf849b0270 | 75 | instructions_run(); |
Kern_EL17KJTF | 2:6baf849b0270 | 76 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 77 | if (option_pos == 3) { |
Kern_EL17KJTF | 2:6baf849b0270 | 78 | // printf("Options"); |
Kern_EL17KJTF | 2:6baf849b0270 | 79 | options_run(); |
Kern_EL17KJTF | 2:6baf849b0270 | 80 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 81 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 82 | } |
Kern_EL17KJTF | 1:7a0917df015a | 83 | |
Kern_EL17KJTF | 1:7a0917df015a | 84 | |
Kern_EL17KJTF | 2:6baf849b0270 | 85 | void print_menu() { |
Kern_EL17KJTF | 2:6baf849b0270 | 86 | lcd.clear(); |
Kern_EL17KJTF | 2:6baf849b0270 | 87 | lcd.printString("Main Menu",18,0); |
Kern_EL17KJTF | 2:6baf849b0270 | 88 | lcd.printString("Start Game",8,2); |
Kern_EL17KJTF | 2:6baf849b0270 | 89 | lcd.printString("Controls",8,3); |
Kern_EL17KJTF | 2:6baf849b0270 | 90 | lcd.printString("Instructions",8,4); |
Kern_EL17KJTF | 2:6baf849b0270 | 91 | lcd.printString("Options",8,5); |
Kern_EL17KJTF | 2:6baf849b0270 | 92 | lcd.drawSprite(0,arrow_pos,7,7,(int *)menu_arrow); |
Kern_EL17KJTF | 2:6baf849b0270 | 93 | lcd.refresh(); |
Kern_EL17KJTF | 1:7a0917df015a | 94 | } |
Kern_EL17KJTF | 1:7a0917df015a | 95 | |
Kern_EL17KJTF | 2:6baf849b0270 | 96 | void welcome() { |
Kern_EL17KJTF | 1:7a0917df015a | 97 | lcd.clear(); |
Kern_EL17KJTF | 2:6baf849b0270 | 98 | lcd.drawSprite(24,0,36,34,(int *)menu_dk_face); |
Kern_EL17KJTF | 1:7a0917df015a | 99 | lcd.printString(" Donkey Kong",0,5); |
Kern_EL17KJTF | 1:7a0917df015a | 100 | lcd.refresh(); |
Kern_EL17KJTF | 2:6baf849b0270 | 101 | wait(1.0); //edit back to longer |
Kern_EL17KJTF | 1:7a0917df015a | 102 | lcd.clear(); |
Kern_EL17KJTF | 2:6baf849b0270 | 103 | lcd.printString(" Created",0,0); |
Kern_EL17KJTF | 2:6baf849b0270 | 104 | lcd.printString(" By",0,1); |
Kern_EL17KJTF | 2:6baf849b0270 | 105 | lcd.printString(" Kern Fowler",0,3); |
Kern_EL17KJTF | 2:6baf849b0270 | 106 | lcd.printString(" 201116686",0,4); |
Kern_EL17KJTF | 1:7a0917df015a | 107 | lcd.refresh(); |
Kern_EL17KJTF | 2:6baf849b0270 | 108 | wait(1.0); |
Kern_EL17KJTF | 1:7a0917df015a | 109 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 110 | |
Kern_EL17KJTF | 2:6baf849b0270 | 111 | void game_engine_run() { |
Kern_EL17KJTF | 2:6baf849b0270 | 112 | wait_ms(250); |
Kern_EL17KJTF | 2:6baf849b0270 | 113 | |
Kern_EL17KJTF | 2:6baf849b0270 | 114 | while (pad.check_event(Gamepad::BACK_PRESSED) == false) { |
Kern_EL17KJTF | 2:6baf849b0270 | 115 | //printf("Game State"); |
Kern_EL17KJTF | 2:6baf849b0270 | 116 | lcd.clear(); |
Kern_EL17KJTF | 2:6baf849b0270 | 117 | lcd.printString(" Game",0,0); |
Kern_EL17KJTF | 2:6baf849b0270 | 118 | lcd.refresh(); |
Kern_EL17KJTF | 2:6baf849b0270 | 119 | wait_ms(1.0f/fps); |
Kern_EL17KJTF | 2:6baf849b0270 | 120 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 121 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 122 | |
Kern_EL17KJTF | 2:6baf849b0270 | 123 | void controls_run() { |
Kern_EL17KJTF | 2:6baf849b0270 | 124 | wait_ms(250); |
Kern_EL17KJTF | 1:7a0917df015a | 125 | |
Kern_EL17KJTF | 2:6baf849b0270 | 126 | while (pad.check_event(Gamepad::BACK_PRESSED) == false) { |
Kern_EL17KJTF | 2:6baf849b0270 | 127 | //printf("Control State"); |
Kern_EL17KJTF | 2:6baf849b0270 | 128 | lcd.clear(); |
Kern_EL17KJTF | 2:6baf849b0270 | 129 | lcd.printString(" Controls",0,0); |
Kern_EL17KJTF | 2:6baf849b0270 | 130 | lcd.refresh(); |
Kern_EL17KJTF | 2:6baf849b0270 | 131 | wait_ms(1.0f/fps); |
Kern_EL17KJTF | 2:6baf849b0270 | 132 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 133 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 134 | |
Kern_EL17KJTF | 2:6baf849b0270 | 135 | void instructions_run() { |
Kern_EL17KJTF | 2:6baf849b0270 | 136 | wait_ms(250); |
Kern_EL17KJTF | 2:6baf849b0270 | 137 | |
Kern_EL17KJTF | 2:6baf849b0270 | 138 | while (pad.check_event(Gamepad::BACK_PRESSED) == false) { |
Kern_EL17KJTF | 2:6baf849b0270 | 139 | //printf("Instructions State"); |
Kern_EL17KJTF | 2:6baf849b0270 | 140 | lcd.clear(); |
Kern_EL17KJTF | 2:6baf849b0270 | 141 | lcd.printString(" Instructions",0,0); |
Kern_EL17KJTF | 2:6baf849b0270 | 142 | lcd.refresh(); |
Kern_EL17KJTF | 2:6baf849b0270 | 143 | wait_ms(1.0f/fps); |
Kern_EL17KJTF | 2:6baf849b0270 | 144 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 145 | } |
Kern_EL17KJTF | 2:6baf849b0270 | 146 | |
Kern_EL17KJTF | 2:6baf849b0270 | 147 | void options_run() { |
Kern_EL17KJTF | 2:6baf849b0270 | 148 | wait_ms(250); |
Kern_EL17KJTF | 2:6baf849b0270 | 149 | |
Kern_EL17KJTF | 2:6baf849b0270 | 150 | while (pad.check_event(Gamepad::BACK_PRESSED) == false) { |
Kern_EL17KJTF | 2:6baf849b0270 | 151 | //printf("Options State"); |
Kern_EL17KJTF | 2:6baf849b0270 | 152 | lcd.clear(); |
Kern_EL17KJTF | 2:6baf849b0270 | 153 | lcd.printString(" Options",0,0); |
Kern_EL17KJTF | 2:6baf849b0270 | 154 | lcd.refresh(); |
Kern_EL17KJTF | 2:6baf849b0270 | 155 | wait_ms(1.0f/fps); |
Kern_EL17KJTF | 2:6baf849b0270 | 156 | } |
Kern_EL17KJTF | 1:7a0917df015a | 157 | } |