![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
Adam Baker 201166301
Dependencies: mbed Gamepad N5110
main.cpp@34:911d7d05a8d2, 2019-05-02 (annotated)
- Committer:
- adambakerwa
- Date:
- Thu May 02 13:22:38 2019 +0000
- Revision:
- 34:911d7d05a8d2
- Parent:
- 31:bf765e3961f1
- Child:
- 36:6f452777b9ce
safety comit before reorganising code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
adambakerwa | 1:a3cce487dffe | 1 | #include "mbed.h" |
adambakerwa | 1:a3cce487dffe | 2 | #include "Gamepad.h" |
adambakerwa | 1:a3cce487dffe | 3 | #include "N5110.h" |
adambakerwa | 1:a3cce487dffe | 4 | #include "Runner.h" |
adambakerwa | 6:cef9c2853315 | 5 | #include "Levels.h" |
adambakerwa | 9:ddeafaadf984 | 6 | #include "Animation.h" |
adambakerwa | 25:230e4163cb6b | 7 | #include "BlockheadEngine.h" |
adambakerwa | 1:a3cce487dffe | 8 | |
adambakerwa | 1:a3cce487dffe | 9 | N5110 lcd(PTC9,PTC0,PTC7,PTD2,PTD1,PTC11); |
adambakerwa | 1:a3cce487dffe | 10 | Gamepad pad; |
adambakerwa | 25:230e4163cb6b | 11 | BlockheadEngine blockhead; |
adambakerwa | 13:35a459a33bbd | 12 | Ticker ticker; |
adambakerwa | 13:35a459a33bbd | 13 | |
adambakerwa | 13:35a459a33bbd | 14 | |
adambakerwa | 13:35a459a33bbd | 15 | volatile int timer_flag = 0; |
adambakerwa | 13:35a459a33bbd | 16 | |
adambakerwa | 13:35a459a33bbd | 17 | void timer_isr(); |
adambakerwa | 31:bf765e3961f1 | 18 | void init(); |
adambakerwa | 26:3319ddd5111d | 19 | void main_menu(N5110 &lcd, Gamepad &pad); |
adambakerwa | 26:3319ddd5111d | 20 | void print_main_menu(N5110 &lcd); |
adambakerwa | 26:3319ddd5111d | 21 | int user_input(Gamepad &pad); |
adambakerwa | 31:bf765e3961f1 | 22 | void select_main_menu_item(N5110 &lcd, Gamepad &pad, int input); |
adambakerwa | 31:bf765e3961f1 | 23 | |
adambakerwa | 26:3319ddd5111d | 24 | void menu_playgame(N5110 &lcd, Gamepad &pad); |
adambakerwa | 26:3319ddd5111d | 25 | void menu_setcontrast(N5110 &lcd, Gamepad &pad); |
adambakerwa | 6:cef9c2853315 | 26 | |
adambakerwa | 31:bf765e3961f1 | 27 | void select_continue_menu_item(N5110 &lcd, Gamepad &pad, int input); |
adambakerwa | 31:bf765e3961f1 | 28 | void print_continue_menu(N5110 &lcd); |
adambakerwa | 31:bf765e3961f1 | 29 | void menu_continue(N5110 &lcd, Gamepad &pad); |
adambakerwa | 31:bf765e3961f1 | 30 | |
adambakerwa | 31:bf765e3961f1 | 31 | |
adambakerwa | 13:35a459a33bbd | 32 | int main() |
adambakerwa | 13:35a459a33bbd | 33 | { |
adambakerwa | 13:35a459a33bbd | 34 | |
adambakerwa | 18:0a0976faedfb | 35 | int fps = 6; //6 |
adambakerwa | 31:bf765e3961f1 | 36 | init(); |
adambakerwa | 31:bf765e3961f1 | 37 | ticker.attach(&timer_isr,1.0f/fps); |
adambakerwa | 31:bf765e3961f1 | 38 | |
adambakerwa | 17:ecbafe932c19 | 39 | lcd.setContrast(0.55); |
adambakerwa | 24:f5dd1f3db693 | 40 | |
adambakerwa | 26:3319ddd5111d | 41 | main_menu(lcd, pad); |
adambakerwa | 13:35a459a33bbd | 42 | |
adambakerwa | 13:35a459a33bbd | 43 | } |
adambakerwa | 13:35a459a33bbd | 44 | |
adambakerwa | 13:35a459a33bbd | 45 | void timer_isr() |
adambakerwa | 13:35a459a33bbd | 46 | { |
adambakerwa | 13:35a459a33bbd | 47 | |
adambakerwa | 13:35a459a33bbd | 48 | timer_flag = 1; // set flag in ISR |
adambakerwa | 1:a3cce487dffe | 49 | } |
adambakerwa | 1:a3cce487dffe | 50 | |
adambakerwa | 31:bf765e3961f1 | 51 | void init() |
adambakerwa | 31:bf765e3961f1 | 52 | { |
adambakerwa | 31:bf765e3961f1 | 53 | pad.init(); |
adambakerwa | 31:bf765e3961f1 | 54 | lcd.init(); |
adambakerwa | 31:bf765e3961f1 | 55 | } |
adambakerwa | 31:bf765e3961f1 | 56 | |
adambakerwa | 26:3319ddd5111d | 57 | void main_menu(N5110 &lcd, Gamepad &pad) |
adambakerwa | 26:3319ddd5111d | 58 | { |
adambakerwa | 31:bf765e3961f1 | 59 | blockhead.init(); |
adambakerwa | 31:bf765e3961f1 | 60 | |
adambakerwa | 26:3319ddd5111d | 61 | print_main_menu(lcd); |
adambakerwa | 26:3319ddd5111d | 62 | int input = user_input(pad); |
adambakerwa | 31:bf765e3961f1 | 63 | select_main_menu_item(lcd, pad, input); |
adambakerwa | 26:3319ddd5111d | 64 | } |
adambakerwa | 26:3319ddd5111d | 65 | |
adambakerwa | 26:3319ddd5111d | 66 | void print_main_menu(N5110 &lcd) |
adambakerwa | 25:230e4163cb6b | 67 | { |
adambakerwa | 26:3319ddd5111d | 68 | lcd.clear(); |
adambakerwa | 26:3319ddd5111d | 69 | |
adambakerwa | 29:b2f63d8c1268 | 70 | lcd.printString("Menu",0,0); |
adambakerwa | 34:911d7d05a8d2 | 71 | lcd.printString("A for game!",0,1); |
adambakerwa | 29:b2f63d8c1268 | 72 | lcd.printString("B for contrast",0,2); |
adambakerwa | 26:3319ddd5111d | 73 | lcd.refresh(); |
adambakerwa | 26:3319ddd5111d | 74 | |
adambakerwa | 26:3319ddd5111d | 75 | } |
adambakerwa | 26:3319ddd5111d | 76 | |
adambakerwa | 26:3319ddd5111d | 77 | |
adambakerwa | 26:3319ddd5111d | 78 | int user_input(Gamepad &pad) |
adambakerwa | 26:3319ddd5111d | 79 | { |
adambakerwa | 26:3319ddd5111d | 80 | int input = 0; |
adambakerwa | 26:3319ddd5111d | 81 | |
adambakerwa | 26:3319ddd5111d | 82 | do { |
adambakerwa | 26:3319ddd5111d | 83 | if (pad.check_event(Gamepad::A_PRESSED)) { |
adambakerwa | 26:3319ddd5111d | 84 | input = 1; |
adambakerwa | 26:3319ddd5111d | 85 | } else if (pad.check_event(Gamepad::B_PRESSED)) { |
adambakerwa | 26:3319ddd5111d | 86 | input = 2; |
adambakerwa | 26:3319ddd5111d | 87 | } |
adambakerwa | 26:3319ddd5111d | 88 | } while (input == 0); |
adambakerwa | 26:3319ddd5111d | 89 | |
adambakerwa | 25:230e4163cb6b | 90 | return input; |
adambakerwa | 25:230e4163cb6b | 91 | } |
adambakerwa | 25:230e4163cb6b | 92 | |
adambakerwa | 31:bf765e3961f1 | 93 | void select_main_menu_item(N5110 &lcd, Gamepad &pad, int input) |
adambakerwa | 26:3319ddd5111d | 94 | { |
adambakerwa | 26:3319ddd5111d | 95 | switch (input) { |
adambakerwa | 26:3319ddd5111d | 96 | case 1: |
adambakerwa | 26:3319ddd5111d | 97 | menu_playgame(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 98 | break; |
adambakerwa | 26:3319ddd5111d | 99 | case 2: |
adambakerwa | 26:3319ddd5111d | 100 | menu_setcontrast(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 101 | break; |
adambakerwa | 26:3319ddd5111d | 102 | default: |
adambakerwa | 26:3319ddd5111d | 103 | exit(1); |
adambakerwa | 26:3319ddd5111d | 104 | break; |
adambakerwa | 26:3319ddd5111d | 105 | } |
adambakerwa | 25:230e4163cb6b | 106 | } |
adambakerwa | 25:230e4163cb6b | 107 | |
adambakerwa | 26:3319ddd5111d | 108 | void menu_playgame(N5110 &lcd, Gamepad &pad) |
adambakerwa | 26:3319ddd5111d | 109 | { |
adambakerwa | 26:3319ddd5111d | 110 | int gameover = 0; |
adambakerwa | 31:bf765e3961f1 | 111 | |
adambakerwa | 31:bf765e3961f1 | 112 | // blockhead.init(); |
adambakerwa | 26:3319ddd5111d | 113 | |
adambakerwa | 26:3319ddd5111d | 114 | do { |
adambakerwa | 26:3319ddd5111d | 115 | |
adambakerwa | 26:3319ddd5111d | 116 | if (timer_flag == true) { |
adambakerwa | 26:3319ddd5111d | 117 | |
adambakerwa | 26:3319ddd5111d | 118 | timer_flag = 0; // if it has, clear the flag |
adambakerwa | 26:3319ddd5111d | 119 | |
adambakerwa | 26:3319ddd5111d | 120 | lcd.clear(); |
adambakerwa | 26:3319ddd5111d | 121 | |
adambakerwa | 26:3319ddd5111d | 122 | gameover = blockhead.playgame(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 123 | |
adambakerwa | 26:3319ddd5111d | 124 | lcd.refresh(); |
adambakerwa | 26:3319ddd5111d | 125 | |
adambakerwa | 26:3319ddd5111d | 126 | } else { |
adambakerwa | 26:3319ddd5111d | 127 | |
adambakerwa | 26:3319ddd5111d | 128 | sleep(); |
adambakerwa | 26:3319ddd5111d | 129 | |
adambakerwa | 26:3319ddd5111d | 130 | } |
adambakerwa | 26:3319ddd5111d | 131 | } while (gameover == 0); |
adambakerwa | 26:3319ddd5111d | 132 | |
adambakerwa | 31:bf765e3961f1 | 133 | menu_continue(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 134 | |
adambakerwa | 26:3319ddd5111d | 135 | } |
adambakerwa | 26:3319ddd5111d | 136 | |
adambakerwa | 26:3319ddd5111d | 137 | void menu_setcontrast(N5110 &lcd, Gamepad &pad) |
adambakerwa | 25:230e4163cb6b | 138 | { |
adambakerwa | 25:230e4163cb6b | 139 | |
adambakerwa | 26:3319ddd5111d | 140 | int goback = 0; |
adambakerwa | 31:bf765e3961f1 | 141 | |
adambakerwa | 26:3319ddd5111d | 142 | do { |
adambakerwa | 25:230e4163cb6b | 143 | |
adambakerwa | 26:3319ddd5111d | 144 | if (timer_flag == true) { |
adambakerwa | 26:3319ddd5111d | 145 | |
adambakerwa | 26:3319ddd5111d | 146 | timer_flag = 0; |
adambakerwa | 26:3319ddd5111d | 147 | |
adambakerwa | 26:3319ddd5111d | 148 | lcd.clear(); |
adambakerwa | 26:3319ddd5111d | 149 | |
adambakerwa | 29:b2f63d8c1268 | 150 | lcd.printString("contrast",0,0); |
adambakerwa | 26:3319ddd5111d | 151 | |
adambakerwa | 26:3319ddd5111d | 152 | lcd.refresh(); |
adambakerwa | 26:3319ddd5111d | 153 | |
adambakerwa | 29:b2f63d8c1268 | 154 | if (pad.check_event(Gamepad::B_PRESSED)) { |
adambakerwa | 26:3319ddd5111d | 155 | goback = 1; |
adambakerwa | 26:3319ddd5111d | 156 | } |
adambakerwa | 26:3319ddd5111d | 157 | |
adambakerwa | 26:3319ddd5111d | 158 | } else { |
adambakerwa | 26:3319ddd5111d | 159 | |
adambakerwa | 26:3319ddd5111d | 160 | sleep(); |
adambakerwa | 26:3319ddd5111d | 161 | |
adambakerwa | 26:3319ddd5111d | 162 | } |
adambakerwa | 26:3319ddd5111d | 163 | } while (goback == 0); |
adambakerwa | 26:3319ddd5111d | 164 | |
adambakerwa | 26:3319ddd5111d | 165 | main_menu(lcd, pad); |
adambakerwa | 26:3319ddd5111d | 166 | } |
adambakerwa | 25:230e4163cb6b | 167 | |
adambakerwa | 25:230e4163cb6b | 168 | |
adambakerwa | 31:bf765e3961f1 | 169 | void select_continue_menu_item(N5110 &lcd, Gamepad &pad, int input) |
adambakerwa | 31:bf765e3961f1 | 170 | { |
adambakerwa | 31:bf765e3961f1 | 171 | switch (input) { |
adambakerwa | 31:bf765e3961f1 | 172 | case 1: |
adambakerwa | 31:bf765e3961f1 | 173 | menu_playgame(lcd, pad); |
adambakerwa | 31:bf765e3961f1 | 174 | break; |
adambakerwa | 31:bf765e3961f1 | 175 | case 2: |
adambakerwa | 31:bf765e3961f1 | 176 | main_menu(lcd, pad); |
adambakerwa | 31:bf765e3961f1 | 177 | break; |
adambakerwa | 31:bf765e3961f1 | 178 | default: |
adambakerwa | 31:bf765e3961f1 | 179 | exit(1); |
adambakerwa | 31:bf765e3961f1 | 180 | break; |
adambakerwa | 31:bf765e3961f1 | 181 | } |
adambakerwa | 31:bf765e3961f1 | 182 | } |
adambakerwa | 31:bf765e3961f1 | 183 | |
adambakerwa | 31:bf765e3961f1 | 184 | void print_menu_continue(N5110 &lcd) |
adambakerwa | 31:bf765e3961f1 | 185 | { |
adambakerwa | 31:bf765e3961f1 | 186 | lcd.clear(); |
adambakerwa | 31:bf765e3961f1 | 187 | |
adambakerwa | 31:bf765e3961f1 | 188 | lcd.printString("Continue??",0,0); |
adambakerwa | 31:bf765e3961f1 | 189 | lcd.printString("A for hell yeh",0,1); |
adambakerwa | 31:bf765e3961f1 | 190 | lcd.printString("B for back",0,2); |
adambakerwa | 31:bf765e3961f1 | 191 | lcd.refresh(); |
adambakerwa | 31:bf765e3961f1 | 192 | |
adambakerwa | 31:bf765e3961f1 | 193 | } |
adambakerwa | 31:bf765e3961f1 | 194 | |
adambakerwa | 31:bf765e3961f1 | 195 | void menu_continue(N5110 &lcd, Gamepad &pad) |
adambakerwa | 31:bf765e3961f1 | 196 | { |
adambakerwa | 31:bf765e3961f1 | 197 | blockhead.continueInit(); |
adambakerwa | 31:bf765e3961f1 | 198 | |
adambakerwa | 31:bf765e3961f1 | 199 | print_menu_continue(lcd); |
adambakerwa | 31:bf765e3961f1 | 200 | int input = user_input(pad); |
adambakerwa | 31:bf765e3961f1 | 201 | select_continue_menu_item(lcd, pad, input); |
adambakerwa | 31:bf765e3961f1 | 202 | } |
adambakerwa | 31:bf765e3961f1 | 203 | |
adambakerwa | 1:a3cce487dffe | 204 | |
adambakerwa | 13:35a459a33bbd | 205 | //rtos |
adambakerwa | 24:f5dd1f3db693 | 206 | /** |
adambakerwa | 24:f5dd1f3db693 | 207 | char buffer[14]; |
adambakerwa | 24:f5dd1f3db693 | 208 | sprintf(buffer,"%i", gameover); |
adambakerwa | 24:f5dd1f3db693 | 209 | lcd.printString(buffer,0,0); |
adambakerwa | 24:f5dd1f3db693 | 210 | */ |
adambakerwa | 26:3319ddd5111d | 211 | //NEED TO RESET POS CORDS EACH NEW LEVEL TO PREVENT UNNECERSARRY GAME OVER (have fun x) |