player 1

Dependencies:   4DGL-uLCD-SE PinDetect SparkfunAnalogJoystick mbed-rtos mbed SDFileSystem

Fork of 4180FinalLab by Rishi Bhargava

Wireless 2 Player Pong game

Files at this revision

API Documentation at this revision

Comitter:
Mpmart08
Date:
Fri Apr 29 00:51:56 2016 +0000
Parent:
9:aa967c554d10
Commit message:
wifi stuff

Changed in this revision

huzzah.cpp Show annotated file Show diff for this revision Revisions of this file
huzzah.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/huzzah.cpp	Thu Apr 28 20:08:23 2016 +0000
+++ b/huzzah.cpp	Fri Apr 29 00:51:56 2016 +0000
@@ -2,11 +2,10 @@
 #include "mbed.h"
 #include "rtos.h"
 
-Huzzah::Huzzah(char *enterssid, char *enterpassword, Serial *esp, Serial *pc) {
+Huzzah::Huzzah(char *enterssid, char *enterpassword, Serial *esp) {
     ssid = enterssid;     // enter WiFi router ssid inside the quotes
     pwd = enterpassword;
     this->esp = esp;
-    this->pc= pc;
     //ESP=esp; // enter WiFi router password inside the quotes     
     }
 //set ESP baudrate if necessary
@@ -37,7 +36,6 @@
     strcat(snd, "\")\r\n");
     SendCMD();
     Thread::wait(2000);
-    pc->printf("\r\nWifi Configurated");
     }
 
 void Huzzah::SendCMD()
@@ -52,53 +50,51 @@
         
         strcpy(snd, "srv=net.createServer(net.TCP)\r\n");
         SendCMD();
-        Thread::wait(1000);
+        Thread::wait(500);
         strcpy(snd, "srv:listen(80,function(conn)\r\n");
         SendCMD();
-        Thread::wait(1000);
+        Thread::wait(500);
         strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n");
         SendCMD();
-        Thread::wait(1000);
+        Thread::wait(500);
         strcpy(snd, "print(payload)\r\n");
         SendCMD();
-        Thread::wait(1000);
+        Thread::wait(500);
         
         strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n");
         SendCMD();
-        Thread::wait(1000);
+        Thread::wait(500);
         
         strcpy(snd, "conn:send(\"<html>\")\r\n");
         SendCMD();
-        Thread::wait(1000);
+        Thread::wait(500);
         
         strcpy(snd, "conn:send(\"<h1> PVP Wireless Pong High Scores</h1>\")\r\n");
         SendCMD();
-        Thread::wait(1000);
+        Thread::wait(500);
         
         sprintf(snd, "conn:send(\"<h2> Player 1 high score: %i, Player 2 High Score: %i </h2>\")\r\n",_highscore1, _highscore2);
-        pc->printf("\r\n%s",snd);
         SendCMD();
-        wait(1);
+        Thread::wait(500);
         
         strcpy(snd, "conn:send(\"<h2> Great Job!</h2>\")\r\n");
         SendCMD();
-        Thread::wait(1000);
+        Thread::wait(500);
         
         strcpy(snd, "conn:send(\"</html>\")\r\n");
         SendCMD();
-        Thread::wait(1000);
+        Thread::wait(500);
         
         strcpy(snd, "end)\r\n");
         SendCMD();
-        Thread::wait(1000);
+        Thread::wait(500);
         
         strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n");
         SendCMD();
-        Thread::wait(1000);
+        Thread::wait(500);
         
         strcpy(snd, "end)\r\n");
         SendCMD();
-        Thread::wait(1000);
-        pc->printf("\r\nwebpage is ready");
+        Thread::wait(500);
 
 }
--- a/huzzah.h	Thu Apr 28 20:08:23 2016 +0000
+++ b/huzzah.h	Fri Apr 29 00:51:56 2016 +0000
@@ -1,11 +1,10 @@
 #include "uLCD_4DGL.h"
-#include "PinDetect.h"
 #include "mbed.h"
 
 class Huzzah
 {
     public:
-    Huzzah(char*,char*,Serial*,Serial*);
+    Huzzah(char*,char*,Serial*);
     void SendCMD();
     void getreply();
     void config();
@@ -24,6 +23,5 @@
     int _highscore2;
     
     Serial* esp;
-    Serial* pc;
     
 };
\ No newline at end of file
--- 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);