For Nikhil

Dependencies:   4DGL-uLCD-SE EthernetInterface Game_Synchronizer MMA8452 SDFileSystem mbed-rtos mbed wave_player

Fork of 2035_Tanks_Shell by ECE2035 Spring 2015 TA

Committer:
jford38
Date:
Thu Oct 29 02:21:11 2015 +0000
Revision:
20:6a58052b0140
Parent:
19:7aa3af04d6a8
Child:
21:edfeb289b21f
Updated to use separate functions to get button data and accelerometer data separately.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jford38 6:3be57cf4bd33 1 // Student Side.
jford38 0:899c85cd266f 2
jford38 0:899c85cd266f 3 #include "mbed.h"
jford38 15:4b27a3a95772 4
jford38 17:7bc7127782e4 5 #include "SDFileSystem.h"
jford38 17:7bc7127782e4 6 #include "wave_player.h"
jford38 6:3be57cf4bd33 7 #include "game_synchronizer.h"
jford38 14:36c306e26317 8 #include "tank.h"
jford38 15:4b27a3a95772 9 #include "bullet.h"
jford38 20:6a58052b0140 10 #include "misc.h"
jford38 10:5da9b27e050e 11
jford38 17:7bc7127782e4 12
jford38 6:3be57cf4bd33 13 DigitalOut led1(LED1);
jford38 6:3be57cf4bd33 14 DigitalOut led2(LED2);
jford38 6:3be57cf4bd33 15 DigitalOut led3(LED3);
jford38 6:3be57cf4bd33 16 DigitalOut led4(LED4);
jford38 0:899c85cd266f 17
jford38 17:7bc7127782e4 18 DigitalIn pb_u(p21); // Up Button
jford38 17:7bc7127782e4 19 DigitalIn pb_r(p22); // Right Button
jford38 17:7bc7127782e4 20 DigitalIn pb_d(p23); // Down Button
jford38 17:7bc7127782e4 21 DigitalIn pb_l(p24); // Left Button
jford38 6:3be57cf4bd33 22
jford38 17:7bc7127782e4 23 Serial pc(USBTX, USBRX); // Serial connection to PC. Useful for debugging!
jford38 17:7bc7127782e4 24 MMA8452 acc(p28, p27, 100000); // Accelerometer (SDA, SCL, Baudrate)
jford38 17:7bc7127782e4 25 uLCD_4DGL uLCD(p9,p10,p11); // LCD (tx, rx, reset)
jford38 17:7bc7127782e4 26 SDFileSystem sd(p5, p6, p7, p8, "sd"); // SD (mosi, miso, sck, cs)
jford38 17:7bc7127782e4 27 AnalogOut DACout(p18); // speaker
jford38 17:7bc7127782e4 28 wave_player player(&DACout); // wav player
jford38 17:7bc7127782e4 29 Game_Synchronizer sync(PLAYER1); // Game_Synchronizer (PLAYER)
jford38 17:7bc7127782e4 30 Timer frame_timer; // Timer
jford38 0:899c85cd266f 31
jford38 20:6a58052b0140 32 // Global variables go here.
jford38 18:18dfc9fb33b5 33 int winner = -1;
jford38 19:7aa3af04d6a8 34 int whose_turn = PLAYER1;
jford38 17:7bc7127782e4 35
jford38 20:6a58052b0140 36
jford38 20:6a58052b0140 37
jford38 17:7bc7127782e4 38 // Ask the user whether to run the game in Single- or Multi-Player mode.
jford38 17:7bc7127782e4 39 // Note that this function uses uLCD instead of sync because it is only
jford38 17:7bc7127782e4 40 // writing to the local (Player1) lcd. Sync hasn't been initialized yet,
jford38 17:7bc7127782e4 41 // so you can't use it anyway. For the same reason, you must access
jford38 17:7bc7127782e4 42 // the buttons directly e.g. if( !pb_r ) { do something; }.
jford38 7:9506f2d84162 43 int game_menu(void) {
jford38 7:9506f2d84162 44
jford38 18:18dfc9fb33b5 45 uLCD.baudrate(BAUD_3000000);
jford38 9:ee330b1ba394 46 uLCD.locate(0,1);
jford38 9:ee330b1ba394 47 uLCD.puts("Select Mode:");
jford38 9:ee330b1ba394 48 uLCD.locate(0,3);
jford38 9:ee330b1ba394 49 uLCD.puts(" Single-Player:");
jford38 9:ee330b1ba394 50 uLCD.locate(0,4);
jford38 9:ee330b1ba394 51 uLCD.puts(" Left Button");
jford38 9:ee330b1ba394 52 uLCD.locate(0,6);
jford38 9:ee330b1ba394 53 uLCD.puts(" Multi-Player:");
jford38 9:ee330b1ba394 54 uLCD.locate(0,7);
jford38 9:ee330b1ba394 55 uLCD.puts(" Right Button");
jford38 17:7bc7127782e4 56
jford38 7:9506f2d84162 57 while(1) {
jford38 17:7bc7127782e4 58 if(!pb_r) { return MULTI_PLAYER; } // Right button -> Multiplayer
jford38 17:7bc7127782e4 59 if(!pb_l) { return SINGLE_PLAYER; } // Left button -> Single player
jford38 7:9506f2d84162 60 }
jford38 7:9506f2d84162 61 }
jford38 7:9506f2d84162 62
jford38 17:7bc7127782e4 63 // Initialize the world map. I've provided a basic map here,
jford38 17:7bc7127782e4 64 // but as part of the assignment you must create more
jford38 17:7bc7127782e4 65 // interesting map(s).
jford38 17:7bc7127782e4 66 // Note that calls to sync.function() will run function()
jford38 17:7bc7127782e4 67 // on both players' LCDs (assuming you are in multiplayer mode).
jford38 17:7bc7127782e4 68 // In single player mode, only your lcd will be modified. (Makes sense, right?)
jford38 10:5da9b27e050e 69 void map_init() {
jford38 17:7bc7127782e4 70
jford38 17:7bc7127782e4 71 // Fill the entire screen with sky blue.
jford38 10:5da9b27e050e 72 sync.background_color(SKY_COLOR);
jford38 17:7bc7127782e4 73
jford38 17:7bc7127782e4 74 // Call the clear screen function to force the LCD to redraw the screen
jford38 17:7bc7127782e4 75 // with the new background color.
jford38 10:5da9b27e050e 76 sync.cls();
jford38 17:7bc7127782e4 77
jford38 17:7bc7127782e4 78 // Draw the ground in green.
jford38 10:5da9b27e050e 79 sync.filled_rectangle(0,0,128,20, GND_COLOR);
jford38 17:7bc7127782e4 80
jford38 17:7bc7127782e4 81 // Draw a wall in the middle of the map. It doesn't have to be black,
jford38 17:7bc7127782e4 82 // but it shouldn't be the same color as the sky or your tanks.
jford38 17:7bc7127782e4 83 // Get creative here. You could use brown and grey to draw a mountain
jford38 17:7bc7127782e4 84 // or something cool like that.
jford38 10:5da9b27e050e 85 sync.filled_rectangle(59, 20, 69, 60, BLACK);
jford38 17:7bc7127782e4 86
jford38 17:7bc7127782e4 87 // Before you write text on the screens, tell the LCD where to put it.
jford38 17:7bc7127782e4 88 sync.locate(0,15);
jford38 17:7bc7127782e4 89
jford38 20:6a58052b0140 90 // Set the text background color to match the sky. Just for looks.
jford38 14:36c306e26317 91 sync.textbackground_color(SKY_COLOR);
jford38 20:6a58052b0140 92 // Display the game title.
jford38 17:7bc7127782e4 93 char title[] = " ECE 2035 Tanks";
jford38 17:7bc7127782e4 94 sync.puts(title, sizeof(title));
jford38 10:5da9b27e050e 95 sync.update();
jford38 10:5da9b27e050e 96 }
jford38 10:5da9b27e050e 97
jford38 6:3be57cf4bd33 98 void game_init(void) {
jford38 6:3be57cf4bd33 99
jford38 6:3be57cf4bd33 100 led1 = 0; led2 = 0; led3 = 0; led4 = 0;
jford38 6:3be57cf4bd33 101
jford38 17:7bc7127782e4 102 pb_u.mode(PullUp);
jford38 6:3be57cf4bd33 103 pb_r.mode(PullUp);
jford38 17:7bc7127782e4 104 pb_d.mode(PullUp);
jford38 17:7bc7127782e4 105 pb_l.mode(PullUp);
jford38 6:3be57cf4bd33 106
jford38 6:3be57cf4bd33 107 pc.printf("\033[2J\033[0;0H"); // Clear the terminal screen.
jford38 6:3be57cf4bd33 108 pc.printf("I'm alive! Player 1\n"); // Let us know you made it this far.
jford38 7:9506f2d84162 109 int mode = game_menu();
jford38 17:7bc7127782e4 110 sync.init(&uLCD, &acc, &pb_u, &pb_r, &pb_d, &pb_l, mode); // Connect to the other player.
jford38 10:5da9b27e050e 111 map_init();
jford38 6:3be57cf4bd33 112 pc.printf("Initialized...\n"); // Let us know you finished initializing.
jford38 6:3be57cf4bd33 113 }
jford38 6:3be57cf4bd33 114
jford38 19:7aa3af04d6a8 115 void playSound(char * wav)
jford38 19:7aa3af04d6a8 116 {
jford38 19:7aa3af04d6a8 117 // open wav file
jford38 19:7aa3af04d6a8 118 FILE *wave_file;
jford38 19:7aa3af04d6a8 119 wave_file=fopen(wav,"r");
jford38 19:7aa3af04d6a8 120
jford38 19:7aa3af04d6a8 121 if(wave_file == NULL){
jford38 19:7aa3af04d6a8 122 uLCD.locate(0,4);
jford38 19:7aa3af04d6a8 123 uLCD.printf("Error in SD");
jford38 19:7aa3af04d6a8 124 return;
jford38 19:7aa3af04d6a8 125 }
jford38 19:7aa3af04d6a8 126 // play wav file
jford38 19:7aa3af04d6a8 127 player.play(wave_file);
jford38 19:7aa3af04d6a8 128
jford38 19:7aa3af04d6a8 129 // close wav file
jford38 19:7aa3af04d6a8 130 fclose(wave_file);
jford38 19:7aa3af04d6a8 131 }
jford38 19:7aa3af04d6a8 132
jford38 0:899c85cd266f 133 int main (void) {
jford38 20:6a58052b0140 134
jford38 20:6a58052b0140 135 int* p1_buttons;
jford38 20:6a58052b0140 136 int* p2_buttons;
jford38 20:6a58052b0140 137
jford38 20:6a58052b0140 138 float ax1, ay1, az1;
jford38 20:6a58052b0140 139 float ax2, ay2, az2;
jford38 8:e6dd05393290 140
jford38 8:e6dd05393290 141 game_init();
jford38 8:e6dd05393290 142
jford38 18:18dfc9fb33b5 143 Tank t1(4, 21, 12, 8, TANK_RED); // (min_x, min_y, width, height, color)
jford38 18:18dfc9fb33b5 144 Tank t2(111, 21, 12, 8, TANK_BLUE); // (min_x, min_y, width, height, color)
jford38 12:088a8203a9bb 145 Bullet b1(&t1);
jford38 12:088a8203a9bb 146 Bullet b2(&t2);
jford38 12:088a8203a9bb 147
jford38 5:cfec780c935b 148
jford38 12:088a8203a9bb 149 frame_timer.start();
jford38 20:6a58052b0140 150 while(true) {
jford38 12:088a8203a9bb 151
jford38 17:7bc7127782e4 152 sync.set_p1_inputs();
jford38 17:7bc7127782e4 153
jford38 20:6a58052b0140 154 p1_buttons = sync.get_p1_buttons();
jford38 20:6a58052b0140 155 p2_buttons = sync.get_p2_buttons();
jford38 20:6a58052b0140 156
jford38 20:6a58052b0140 157 sync.get_p1_accel_data(ax1, ay1, az1);
jford38 20:6a58052b0140 158 sync.get_p1_accel_data(ax2, ay2, az2);
jford38 8:e6dd05393290 159
jford38 20:6a58052b0140 160 led1 = p2_buttons[0] ^ p2_buttons[0];
jford38 20:6a58052b0140 161 led2 = p2_buttons[1] ^ p2_buttons[1];
jford38 20:6a58052b0140 162 led3 = p2_buttons[2] ^ p2_buttons[2];
jford38 20:6a58052b0140 163 led4 = p2_buttons[3] ^ p2_buttons[3];
jford38 20:6a58052b0140 164
jford38 17:7bc7127782e4 165
jford38 19:7aa3af04d6a8 166 if(whose_turn == PLAYER1) {
jford38 19:7aa3af04d6a8 167 if(ax1 > ACC_THRESHOLD) { t1.reposition(-1, 0, 0); }
jford38 19:7aa3af04d6a8 168 if(ax1 < -ACC_THRESHOLD) { t1.reposition(+1, 0, 0); }
jford38 19:7aa3af04d6a8 169
jford38 19:7aa3af04d6a8 170 if(ay1 > ACC_THRESHOLD) { t1.reposition(0, 0, +PI/30.0); }
jford38 19:7aa3af04d6a8 171 if(ay1 < -ACC_THRESHOLD) { t1.reposition(0, 0, -PI/30.0); }
jford38 19:7aa3af04d6a8 172
jford38 20:6a58052b0140 173 if(p1_buttons[D_BUTTON]) {
jford38 19:7aa3af04d6a8 174 b1.shoot();
jford38 19:7aa3af04d6a8 175 }
jford38 19:7aa3af04d6a8 176
jford38 20:6a58052b0140 177 float dt = frame_timer.read();
jford38 20:6a58052b0140 178 int pix_color = b1.time_step(dt);
jford38 19:7aa3af04d6a8 179 if(pix_color != CONVERT_24_TO_16_BPP(SKY_COLOR)) { pc.printf("Now it's P1's turn!\n"); whose_turn = PLAYER2; }
jford38 19:7aa3af04d6a8 180 if(pix_color == CONVERT_24_TO_16_BPP(t1.tank_color) || pix_color == CONVERT_24_TO_16_BPP(t2.tank_color)) {
jford38 19:7aa3af04d6a8 181 sync.update();
jford38 19:7aa3af04d6a8 182 pc.printf("P2 wins!");
jford38 19:7aa3af04d6a8 183 winner = PLAYER1;
jford38 19:7aa3af04d6a8 184 break;
jford38 19:7aa3af04d6a8 185 }
jford38 19:7aa3af04d6a8 186 } else if(whose_turn == PLAYER2) {
jford38 19:7aa3af04d6a8 187
jford38 19:7aa3af04d6a8 188 if((sync.play_mode == MULTI_PLAYER && ax2 > ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ax1 > ACC_THRESHOLD)) {
jford38 19:7aa3af04d6a8 189 t2.reposition(-1, 0, 0);
jford38 19:7aa3af04d6a8 190 }
jford38 19:7aa3af04d6a8 191 if((sync.play_mode == MULTI_PLAYER && ax2 < -ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ax1 < -ACC_THRESHOLD)) {
jford38 19:7aa3af04d6a8 192 t2.reposition(+1, 0, 0);
jford38 19:7aa3af04d6a8 193 }
jford38 19:7aa3af04d6a8 194 if((sync.play_mode == MULTI_PLAYER && ay2 > ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ay1 > ACC_THRESHOLD)) {
jford38 19:7aa3af04d6a8 195 t2.reposition(0, 0, -PI/30.0);
jford38 19:7aa3af04d6a8 196 }
jford38 19:7aa3af04d6a8 197 if((sync.play_mode == MULTI_PLAYER && ay2 < -ACC_THRESHOLD) || (sync.play_mode == SINGLE_PLAYER && ay1 < -ACC_THRESHOLD)) {
jford38 19:7aa3af04d6a8 198 t2.reposition(0, 0, +PI/30.0);
jford38 19:7aa3af04d6a8 199 }
jford38 20:6a58052b0140 200 if((sync.play_mode == MULTI_PLAYER && p2_buttons[D_BUTTON]) || (sync.play_mode == SINGLE_PLAYER && p1_buttons[D_BUTTON])) {
jford38 19:7aa3af04d6a8 201 b2.shoot();
jford38 19:7aa3af04d6a8 202 }
jford38 19:7aa3af04d6a8 203
jford38 20:6a58052b0140 204 float dt = frame_timer.read();
jford38 20:6a58052b0140 205 int pix_color = b2.time_step(dt);
jford38 19:7aa3af04d6a8 206 if(pix_color != CONVERT_24_TO_16_BPP(SKY_COLOR)) { pc.printf("Now it's P1's turn!\n"); whose_turn = PLAYER1; }
jford38 19:7aa3af04d6a8 207 if(pix_color == CONVERT_24_TO_16_BPP(t1.tank_color) || pix_color == CONVERT_24_TO_16_BPP(t2.tank_color)) {
jford38 19:7aa3af04d6a8 208 sync.update();
jford38 19:7aa3af04d6a8 209 pc.printf("P1 wins!");
jford38 19:7aa3af04d6a8 210 winner = PLAYER2;
jford38 19:7aa3af04d6a8 211 break;
jford38 19:7aa3af04d6a8 212 }
jford38 19:7aa3af04d6a8 213 }
jford38 12:088a8203a9bb 214
jford38 18:18dfc9fb33b5 215 frame_timer.reset();
jford38 12:088a8203a9bb 216 sync.update();
jford38 3:3ddefff03cb2 217 }
jford38 15:4b27a3a95772 218
jford38 20:6a58052b0140 219 playSound("/sd/wavfiles/BUZZER.wav");
jford38 19:7aa3af04d6a8 220
jford38 17:7bc7127782e4 221 // This dies after a while, and I have NO IDEA WHY.
jford38 15:4b27a3a95772 222 int i = 0;
jford38 15:4b27a3a95772 223 while(1) {
jford38 15:4b27a3a95772 224 sync.cls();
jford38 17:7bc7127782e4 225 sync.locate(i, 9);
jford38 18:18dfc9fb33b5 226 if(winner == PLAYER1) {
jford38 18:18dfc9fb33b5 227 char msg[] = "P1 Wins!";
jford38 18:18dfc9fb33b5 228 sync.puts(msg, sizeof(msg));
jford38 18:18dfc9fb33b5 229 } else if(winner == PLAYER2) {
jford38 18:18dfc9fb33b5 230 char msg[] = "P2 Wins!";
jford38 18:18dfc9fb33b5 231 sync.puts(msg, sizeof(msg));
jford38 18:18dfc9fb33b5 232 }
jford38 18:18dfc9fb33b5 233
jford38 15:4b27a3a95772 234 sync.update();
jford38 17:7bc7127782e4 235 i = (i+1)%10;
jford38 15:4b27a3a95772 236 wait(0.1);
jford38 15:4b27a3a95772 237 }
jford38 0:899c85cd266f 238 }