ECE2035 Spring 2015 TA / Mbed 2 deprecated 2035_Badminton_Shell

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

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "misc.h"
00002 
00003 DigitalOut led1(LED1);                  // mbed leds
00004 DigitalOut led2(LED2);
00005 DigitalOut led3(LED3);
00006 DigitalOut led4(LED4);
00007 DigitalIn pb_u(p21);                    // push buttons
00008 DigitalIn pb_r(p22);
00009 DigitalIn pb_d(p23);
00010 DigitalIn pb_l(p24);
00011 Serial pc(USBTX, USBRX);                // Serial connection to PC. Useful for debugging!
00012 MMA8452 acc(p28, p27, 100000);          // Accelerometer (SDA, SCL, Baudrate)
00013 uLCD_4DGL uLCD(p9, p10, p11);           // LCD (tx, rx, reset)
00014 SDFileSystem sd(p5, p6, p7, p8, "sd");  // SD (mosi, miso, sck, cs)
00015 AnalogOut DACout(p18);                  // speaker
00016 wave_player player(&DACout);            // wav player
00017 GSYNC game_synchronizer;                // Game_Synchronizer
00018 GSYNC *sync = &game_synchronizer;       //
00019 
00020 // Starting game menu. Returns SINGLE_PLAYER or MULTI_PLAYER. Only select 
00021 // MULTI_PLAYER if another mbed is connected via ethernet; otherwise, the
00022 // game will hang.
00023 int game_menu(void)
00024 {
00025 
00026     // TODO: Create a game menu
00027 
00028 }
00029 
00030 // Initialize Game_Synchronizer. GS functions are unavailable prior to calling
00031 // this function. This should only be called once.
00032 void init_sync(void)
00033 {
00034     led1 = 0;
00035     led2 = 0;
00036     led3 = 0;
00037     led4 = 0;
00038     pb_u.mode(PullUp);
00039     pb_r.mode(PullUp);
00040     pb_d.mode(PullUp);
00041     pb_l.mode(PullUp);
00042     pc.printf("\033[2J\033[0;0H");  // clear serial terminal
00043     GS_init(sync, &uLCD, &acc, &pb_u, &pb_r, &pb_d, &pb_l, game_menu(), PLAYER1);
00044     GS_cls(sync, SCREEN_BOTH);
00045     GS_update(sync);
00046 }
00047 
00048 // Initialize game map. This is called to reset the map at the beginning of each
00049 // volley. Feel free to change any of this.
00050 void init_map(racket *r1, racket *r2, shuttle *s)
00051 {
00052     GS_filled_rectangle(sync, SCREEN_BOTH, 0, 0, 127, 127, SKY_COL);
00053     GS_filled_rectangle(sync, SCREEN_BOTH, 0, 0, 127, GND_LVL-2, GND_COL);
00054     GS_filled_rectangle(sync, SCREEN_BOTH, 63, GND_LVL-2, 64, 45, GND_COL);
00055     int dx = r1->width/2;
00056     int dy = r1->height/2;
00057     GS_filled_rectangle(sync, SCREEN_BOTH, r1->x-dx, r1->y-dy, r1->x+dx, r1->y+dy, P1_COL);
00058     GS_pixel(sync, SCREEN_BOTH, s->last_possession->x, s->last_possession->y+dy+2, SHUT_COL);
00059     dx = r2->width/2;
00060     dy = r2->height/2;
00061     GS_filled_rectangle(sync, SCREEN_BOTH, r2->x-dx, r2->y-dy, r2->x+dx, r2->y+dy, P2_COL);
00062 }
00063 
00064 // Game over. Must reset mbed to break loop.
00065 void game_over(int winner)
00066 {
00067 
00068     // TODO: Let us know the game is over.
00069 
00070 }
00071 
00072 // Beginning of execution. 
00073 int main(void)
00074 {
00075     // Initializations, probably best if these are left alone.
00076     int *p1_buttons, *p2_buttons;
00077     float ax1, ay1, az1, ax2, ay2, az2;
00078     racket *p1 = (racket*)calloc(1, sizeof(racket));
00079     racket *p2 = (racket*)calloc(1, sizeof(racket));
00080     shuttle *s = (shuttle*)calloc(1, sizeof(shuttle));
00081     init_sync();
00082     init_racket(p1, PLAYER1);
00083     init_racket(p2, PLAYER2);
00084     init_shuttle(s, p1);
00085     GS_textbackground_color(sync, SCREEN_BOTH, SKY_COL);
00086     init_map(p1, p2, s);
00087     
00088     // Game loop. This is where the majority of your time will be spent.
00089     while(1)
00090     {
00091         // Retrieve inputs from specified source during each iteration.
00092         p1_buttons = GS_get_p1_buttons(sync);
00093         p2_buttons = GS_get_p2_buttons(sync);
00094         GS_get_p1_accel_data(sync, &ax1, &ay1, &az1);
00095         GS_get_p2_accel_data(sync, &ax2, &ay2, &az2);
00096         led1 = p1_buttons[U_BUTTON] ^ p2_buttons[U_BUTTON];
00097         led2 = p1_buttons[R_BUTTON] ^ p2_buttons[R_BUTTON];
00098         led3 = p1_buttons[D_BUTTON] ^ p2_buttons[D_BUTTON];
00099         led4 = p1_buttons[L_BUTTON] ^ p2_buttons[L_BUTTON];
00100         
00101         // Gameplay logic begins here.
00102         
00103         // TODO: Read one of the buttons to begin volleying the shuttle.
00104         
00105         // TODO: Read accelerometer inputs to handle racket motion and a push button
00106         //       to toggle jumps.
00107         
00108         // TODO: Jumping logic (Hint: You may want to limit the game to only one
00109         //       player jumping at a time to improve performance.)
00110         
00111         // TODO: Update shuttle movement using kinematic equations.
00112 
00113         // Update changes to both screens at the end of each iteration.
00114         GS_update(sync);
00115     }
00116 }