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:
fepie3
Date:
Thu Apr 28 20:08:23 2016 +0000
Parent:
8:8cc2aa78348c
Child:
10:b57b3fbf8266
Commit message:
Wifi added

Changed in this revision

SDFileSystem.lib Show annotated file Show diff for this revision Revisions of this file
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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SDFileSystem.lib	Thu Apr 28 20:08:23 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/neilt6/code/SDFileSystem/#6bb3c1987511
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/huzzah.cpp	Thu Apr 28 20:08:23 2016 +0000
@@ -0,0 +1,104 @@
+#include "huzzah.h"
+#include "mbed.h"
+#include "rtos.h"
+
+Huzzah::Huzzah(char *enterssid, char *enterpassword, Serial *esp, Serial *pc) {
+    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
+void Huzzah::setbaudrate()
+{
+    strcpy(snd, "AT+CIOBAUD=115200\r\n");   // change the numeric value to the required baudrate
+    SendCMD();
+}
+//Configurate ESP to Station mode and set username and password
+void Huzzah::config()
+    {
+    strcpy(snd,".\r\n.\r\n");
+    SendCMD();
+    Thread::wait(1000);
+    
+    strcpy(snd,"node.restart()\r\n");
+    SendCMD();
+    Thread::wait(3000);
+    
+    strcpy(snd, "wifi.setmode(wifi.STATION)\r\n");
+    SendCMD();
+    Thread::wait(2000);
+    
+    strcpy(snd, "wifi.sta.config(\"");
+    strcat(snd, ssid);
+    strcat(snd, "\",\"");
+    strcat(snd, pwd);
+    strcat(snd, "\")\r\n");
+    SendCMD();
+    Thread::wait(2000);
+    pc->printf("\r\nWifi Configurated");
+    }
+
+void Huzzah::SendCMD()
+{
+    esp->printf("%s", snd);
+}
+
+void Huzzah::sendwebpage(int _highscore1, int _highscore2)
+{
+        this->_highscore1=_highscore1;
+        this->_highscore2=_highscore2;
+        
+        strcpy(snd, "srv=net.createServer(net.TCP)\r\n");
+        SendCMD();
+        Thread::wait(1000);
+        strcpy(snd, "srv:listen(80,function(conn)\r\n");
+        SendCMD();
+        Thread::wait(1000);
+        strcpy(snd, "conn:on(\"receive\",function(conn,payload)\r\n");
+        SendCMD();
+        Thread::wait(1000);
+        strcpy(snd, "print(payload)\r\n");
+        SendCMD();
+        Thread::wait(1000);
+        
+        strcpy(snd, "conn:send(\"<!DOCTYPE html>\")\r\n");
+        SendCMD();
+        Thread::wait(1000);
+        
+        strcpy(snd, "conn:send(\"<html>\")\r\n");
+        SendCMD();
+        Thread::wait(1000);
+        
+        strcpy(snd, "conn:send(\"<h1> PVP Wireless Pong High Scores</h1>\")\r\n");
+        SendCMD();
+        Thread::wait(1000);
+        
+        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);
+        
+        strcpy(snd, "conn:send(\"<h2> Great Job!</h2>\")\r\n");
+        SendCMD();
+        Thread::wait(1000);
+        
+        strcpy(snd, "conn:send(\"</html>\")\r\n");
+        SendCMD();
+        Thread::wait(1000);
+        
+        strcpy(snd, "end)\r\n");
+        SendCMD();
+        Thread::wait(1000);
+        
+        strcpy(snd, "conn:on(\"sent\",function(conn) conn:close() end)\r\n");
+        SendCMD();
+        Thread::wait(1000);
+        
+        strcpy(snd, "end)\r\n");
+        SendCMD();
+        Thread::wait(1000);
+        pc->printf("\r\nwebpage is ready");
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/huzzah.h	Thu Apr 28 20:08:23 2016 +0000
@@ -0,0 +1,29 @@
+#include "uLCD_4DGL.h"
+#include "PinDetect.h"
+#include "mbed.h"
+
+class Huzzah
+{
+    public:
+    Huzzah(char*,char*,Serial*,Serial*);
+    void SendCMD();
+    void getreply();
+    void config();
+    void setbaudrate();
+    void sendwebpage(int,int);
+    
+
+    private:
+    //Serial ESP;
+    char buf[2024];
+    char snd[1024];
+ 
+    char *ssid;     
+    char *pwd;
+    int _highscore1;
+    int _highscore2;
+    
+    Serial* esp;
+    Serial* pc;
+    
+};
\ No newline at end of file
--- a/main.cpp	Thu Apr 28 19:58:48 2016 +0000
+++ b/main.cpp	Thu Apr 28 20:08:23 2016 +0000
@@ -14,6 +14,15 @@
 Speaker mySpeaker(p25);
 Serial pc(USBTX, USBRX);
 Serial xbee(p9, p10);
+
+//Wifi
+Huzzah huzzah("fese","12345678", &esp, &pc);
+Serial esp(p28,p27);
+DigitalOut  reset(p26);
+int player1score,player2score, highscore1,highscore2;
+
+//SD Card Storage
+SDFileSystem sd(p5, p6, p7, p8, "sd");
  
 // State machine definitions
 enum gameStateType {START, WAIT, GAME_SETUP, GAME, WIN, LOSE};
@@ -92,18 +101,42 @@
     }
 }
 
-// thread that writes to the sd card
+// thread that writes to the sd card and loads highscores to website
 void sd_card_thread(void const *argument) {
 
     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++;
         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++;
             break;
         }
     }
-}
+} 
 
 void xbee_thread(void const *argument) {
  
@@ -136,6 +169,16 @@
  
 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);
+    reset=1;
+    esp.baud(9600);
+    huzzah.config();
+    huzzah.sendwebpage(highscore1,highscore2);
+  
     // This is setting up the joystick select as a pushbutton
     //joystick.set_callback(&select_hit_callback);