
player 1
Dependencies: 4DGL-uLCD-SE PinDetect SparkfunAnalogJoystick mbed-rtos mbed SDFileSystem
Fork of 4180FinalLab by
Wireless 2 Player Pong game
Diff: main.cpp
- Revision:
- 10:b57b3fbf8266
- Parent:
- 9:aa967c554d10
--- a/main.cpp Thu Apr 28 20:08:23 2016 +0000 +++ b/main.cpp Fri Apr 29 00:51:56 2016 +0000 @@ -6,6 +6,8 @@ #include "SparkfunAnalogJoystick.h" #include "paddle.h" #include "ball.h" +#include "huzzah.h" +#include "SDFileSystem.h" // Pushbuttons SparkfunAnalogJoystick joystick(p16, p15, p14); @@ -16,10 +18,10 @@ Serial xbee(p9, p10); //Wifi -Huzzah huzzah("fese","12345678", &esp, &pc); Serial esp(p28,p27); +Huzzah huzzah("fese","12345678", &esp); DigitalOut reset(p26); -int player1score,player2score, highscore1,highscore2; +int player1score,player2score; //SD Card Storage SDFileSystem sd(p5, p6, p7, p8, "sd"); @@ -104,35 +106,34 @@ // thread that writes to the sd card and loads highscores to website void sd_card_thread(void const *argument) { + FILE *fp = NULL; + fp = fopen("/sd/highscore.txt", "r"); + fscanf(fp, "%i\n%i", &player1score, &player2score); + fclose(fp); + + huzzah.config(); + huzzah.sendwebpage(player1score, player2score); + while (true) { - FILE *fp = fopen("/sd/highscore.txt", "r"); - if(fp == NULL) { - error("Could not open file for read\n"); - } - fscanf(fp, "%i%i", &player1score, &player2score); - fclose(fp); - huzzah.sendwebpage(player1score, player2score); + switch (gameState) { case WIN: - highscore2=0; //reset player 2 highscore - if (player1score < highscore1){ - // overwrite previous high score - fp = fopen("/sd/highscore.txt", "w"); - fprintf(fp, "%i%i", highscore1, player2score); - fclose(fp); - huzzah.sendwebpage(player1score, player2score); - } - highscore1++; + // overwrite previous high score + player1score++; + fp = fopen("/sd/highscore.txt", "w"); + fprintf(fp, "%i\n%i", player1score, player2score); + fclose(fp); + huzzah.config(); + huzzah.sendwebpage(player1score, player2score); + break; case LOSE: - highscore1=0; //reset player 1 highscore - if (player2score < highscore2){ - // overwrite previous high score - fp = fopen("/sd/highscore.txt", "w"); - fprintf(fp, "%i%i", player1score, highscore2); - fclose(fp); - huzzah.sendwebpage(player1score, player2score); - } - highscore2++; + // overwrite previous high score + player2score++; + fp = fopen("/sd/highscore.txt", "w"); + fprintf(fp, "%i\n%i", player1score, player2score); + fclose(fp); + huzzah.config(); + huzzah.sendwebpage(player1score, player2score); break; } } @@ -170,14 +171,12 @@ int main() { //WifiServer Configuration and set-up - highscore1 = 0; // variable to store high score - highscore2=0; reset=0; //hardware reset for 8266 - Thread::wait(500); + //Thread::wait(500); reset=1; esp.baud(9600); - huzzah.config(); - huzzah.sendwebpage(highscore1,highscore2); + //huzzah.config(); + //huzzah.sendwebpage(highscore1,highscore2); // This is setting up the joystick select as a pushbutton //joystick.set_callback(&select_hit_callback);