the place we got to by the end of the last session

Dependencies:   LCDTFT TFT_fonts TSI mbed

Fork of ESP8266_MDM_CJS_KL25 by Oxford CWM Team

Files at this revision

API Documentation at this revision

Comitter:
trapsis
Date:
Fri Jun 10 15:12:48 2016 +0000
Parent:
5:882898bb83ed
Commit message:
server final

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
diff -r 882898bb83ed -r 00d91197dd66 main.cpp
--- a/main.cpp	Fri Jun 10 11:49:51 2016 +0000
+++ b/main.cpp	Fri Jun 10 15:12:48 2016 +0000
@@ -11,7 +11,7 @@
 #define FUNCNAME "MAIN"
 
 #ifdef NoDEBUG
-#define DBG(x, ...) pc.printf("    ["FUNCNAME" : DBG] "x" <line %d>\r\n", ##__VA_ARGS__,__LINE__);
+#define DBG(x, ...) pc.printf("    ["FUNCNAME" : pc.printf] "x" <line %d>\r\n", ##__VA_ARGS__,__LINE__);
 #else
 #define DBG(x, ...)
 #endif
@@ -48,8 +48,8 @@
 LCDTFT  MyLCD(PTB0,PTB1,PTB2,PTB3,PTC2,&MyPort);//LCDTFT(PinName PIN_RD,PinName PIN_WR,PinName PIN_RS,PinName PIN_CS,PinName PIN_RESET, PortOut *PORTLCD);
 
 
-    PwmOut led(LED_GREEN);
-    TSISensor tsi;
+PwmOut led(LED_GREEN);
+TSISensor tsi;
 volatile int state=0;
 volatile int ready=0;
 //---
@@ -66,7 +66,9 @@
 void PrettyPrint(char *message,const unsigned char *font,short xpos,short ypos,short bcol, short fcol);
 void prettyputc(char c, const unsigned char *font,short xpos,short ypos,short bcol, short fcol);
 
-bool GameRunning = 1; 
+char * resp=NULL;
+
+bool GameRunning = 1;
 const int gridWidth = 64;
 const int gridHeight = 48;
 char grid [48][64];
@@ -101,22 +103,102 @@
 char *buffer;
 unsigned int bufferPnt=0;
 
-void commandReceived(char *buffer){
+/**
+Interpret data
+*/
+
+void dev_recv()
+{
+    char c;
+
+    int count = 0;
+    led1 = !led1;
+    if(bufferPnt==0) {
+        memset(buffer,0,BUFF_SIZE);
+    }
+    while(dev.readable()) {
+        c = (char)dev.getc();
+#ifdef DEBUG
+        pc.putc(c);
+#endif
+        buffer[bufferPnt]=c;
+        bufferPnt++;
+        if (bufferPnt>1000) {
+            ready=1;
+        }
+        // if ((c==0x0a)||(c==0x0d)){
+        //     ready=1;
+        //     }else
+        if (c==0x0a) {
+            if (bufferPnt>1) {
+                if (buffer[bufferPnt -2]==0x0d) {
+                    ready=1;
+                    break;
+                }
+            }
+        }
+        if (!dev.readable()) {
+            wait_us(10);
+        }
+    }
+}
 
-    char identifier =buffer[0];
-    if (identifier == 1) {                            
-        newDirectFood=buffer[4];
-        newFoodVel=buffer[8];        
-  }
-                        
-    if (identifier == 2) {
-        newDirectSnake=buffer[4];
-  }
-                        
-    if (identifier == 3) {
-        GameRunning=buffer[4];        
+void pc_recv()
+{
+    char c;
+    led4 = !led4;
+    while(pc.readable()) {
+        c=(char)pc.getc();
+        dev.putc(c);
+        pc.putc(c);
+        if(c==13) {
+            dev.putc(10);
+            pc.putc(10);
+        }
+
+    }
+}
+
+char * OKResponse(char *test, const char *pattern)
+{
+    char *p= strstr(test,pattern);
+    if (p==NULL) {
+        //   pc.printf("Test=<%s> Patter=<%s> NULL [p=%s]",test,pattern,p);
+        return NULL;
+    } else {
+        //  pc.printf("YAY Test=<%s> Patter=<%s>  [p=%s]",test,pattern,p);
     }
- 
+    return p;
+}
+
+void commandReceived(char *buffer)
+{
+
+    INFO("Got <%s>",buffer);
+
+    /*const char s[2] = ":";
+    char *token;
+
+    get the first token 
+    token = strtok(buffer, s);
+    token = strtok(NULL, s);
+    pc.printf("%s\n\r", token );*/
+
+    char identifier =buffer[9];
+    if (identifier == 49) {
+        newDirectFood=(buffer[13]-48);
+        newFoodVel=(buffer[17]-48);
+    }
+
+    if (identifier == 50) {
+        newDirectSnake=(buffer[13]-48);
+    }
+
+    if (identifier == 51) {
+        GameRunning=(buffer[13]-48);
+    }
+
+pc.printf("cmd exit");
 }
 
 // next two functions need moving to the library.....
@@ -190,12 +272,25 @@
 //the main game program
 {
     while (1) {
+        GameRunning = 1;
         if (GameRunning == 1) {
+            pc.printf("m");
             countdown();
+            pc.printf("n");
             initGrid();
+            pc.printf("p");
             while (1) {
+                pc.printf("r %d",ready);
                 checkposition();
-                commandReceived(buffer);
+                if (ready == 1) {
+                    pc.printf("A");
+                    ready = 0;
+                    resp=OKResponse(buffer,"+IPD");
+                    if (resp!=NULL) {
+                        pc.printf("B");
+                        commandReceived(buffer);                      // Interpret data                                                                 // Keep on waiting for data
+                    }
+                }
                 changeDirec();
                 nextmove();
                 updateGrid();
@@ -259,7 +354,7 @@
     snake[0][2] = 22;
     snake[1][2] = 20;
 
-     for(int i=3; i<255; i++) {
+    for(int i=3; i<255; i++) {
         grid[snake[1][i]][snake[0][i]] = 0;
     }
     //initialize the snake as horizontal with respect to its initial size (1 = tile occupied by snake)
@@ -275,13 +370,13 @@
     gameover2();
         else
         {
-        */    
-        for(int a=snakeSize-2; a>=0; a--) {
+        */
+    for(int a=snakeSize-2; a>=0; a--) {
 
-         snake[0][a+1] = snake [0][a];
-         snake[1][a+1] = snake [1][a];
-       // }
-        
+        snake[0][a+1] = snake [0][a];
+        snake[1][a+1] = snake [1][a];
+        // }
+
         snake[0][0] = snakexpos;
         snake[1][0] = snakeypos;
     }
@@ -315,10 +410,7 @@
 
     for(int x=0; x<gridHeight; x++) {
         for(int y=0; y<gridWidth; y++) {
-          /*  if (grid[x][y] == 0) {
-                MyLCD.vLCDTFTRectangle(5*y,5*x,5*y+5,5*x+5,1,ColorBlack);
-            }
-            */
+            
             if (grid[x][y] == 1) {
                 MyLCD.vLCDTFTRectangle(5*y,5*x,5*y+5,5*x+5,1,ColorBlue);
             }
@@ -328,7 +420,7 @@
         }
     }
 
-     wait (0.1); //the game is 10fps (up for discussion we'll see how this goes)
+    wait (0.5); //the game is 10fps (up for discussion we'll see how this goes)
 }
 
 void changeDirec()
@@ -340,11 +432,11 @@
     //assign new direction and speed to food
 
     if ((newDirectSnake + directSnake)%2 == 0 && newDirectSnake != directSnake)
-    gameover2();
+        gameover2();
     //the snake loses if it tries to double up on itself
 
     else
-    directSnake =  newDirectSnake;
+        directSnake =  newDirectSnake;
     //assign new direction to snake
 }
 
@@ -352,16 +444,16 @@
 void nextmove()
 //move the snake and the food by incrementing their coordinates (for the food increment by velocity)
 {
-    if (framecount%5 == 0){
+    if (framecount%5 == 0) {
         snakeSize++;
-        }
+    }
     // every 5 iterations, the snake grows
-   
-    
+
+
     MyLCD.vLCDTFTRectangle(5*foodxpos,5*foodypos,5*foodxpos+5,5*foodypos+5,1,ColorBlack);
     MyLCD.vLCDTFTRectangle(5*snake[0][snakeSize-1],5*snake[1][snakeSize-1],5*snake[0][snakeSize-1]+5,5*snake[1][snakeSize-1]+5,1,ColorBlack);
 
-    
+
     if (directFood == 0) {
         foodypos -= foodvel;
     }
@@ -420,7 +512,15 @@
 
 
     GameRunning = 0;
-}
+    
+    dev.printf("AT+CIPSEND=3,3\r\n");
+    wait(0.2);
+    dev.printf("0\r\n");
+   
+   wait (1000);
+
+}   
+
 
 void gameover2()
 //an alternate ending is that the snake has made a mistake and that the food wins
@@ -428,75 +528,21 @@
     MyLCD.vLCDTFTFillScreen(ColorBlack);
     sprintf(message,"The snake is dead!");
     PrettyPrint(message,Arial28x28,10,50,ColorBlack,ColorLime);
+    
+
+    GameRunning = 0;
+    
+    dev.printf("AT+CIPSEND=3,3\r\n");
+    wait(0.2);
+    dev.printf("0\r\n");
+    
     wait (1000);
 
-    GameRunning = 0;
 }
 
-void dev_recv()
-{
-    char c;
 
-    int count = 0;
-    led1 = !led1;
-    if(bufferPnt==0) {
-        memset(buffer,0,BUFF_SIZE);
-    }
-    while(dev.readable()) {
-        c = (char)dev.getc();
-#ifdef DEBUG
-       pc.putc(c);
-#endif
-        buffer[bufferPnt]=c;
-        bufferPnt++;
-        if (bufferPnt>1000) {
-            ready=1;
-        }
-       // if ((c==0x0a)||(c==0x0d)){
-        //     ready=1;
-        //     }else
-        if (c==0x0a) {
-            if (bufferPnt>1) {
-                if (buffer[bufferPnt -2]==0x0d) {
-                    ready=1;
-                    break;
-                }
-            }
-        }
-        if (!dev.readable()) {
-            wait_us(10);
-        }
-    }
-}
-
-void pc_recv()
-{
-    char c;
-    led4 = !led4;
-    while(pc.readable()) {
-        c=(char)pc.getc();
-        dev.putc(c);
-        pc.putc(c);
-        if(c==13) {
-            dev.putc(10);
-            pc.putc(10);
-            }
-
-    }
-}
-
-char * OKResponse(char *test, const char *pattern)
-{
-    char *p= strstr(test,pattern);
-    if (p==NULL) {
-        //   DBG("Test=<%s> Patter=<%s> NULL [p=%s]",test,pattern,p);
-        return NULL;
-    } else {
-        //  DBG("YAY Test=<%s> Patter=<%s>  [p=%s]",test,pattern,p);
-    }
-    return p;
-}
-/*Game programme
+/**
+* Set up server and run the game
 */
 
 int main()
@@ -512,7 +558,6 @@
     pc.printf("Start up\n\r");
     wait(1.5);
     reset=1;
-    char * resp=NULL;
     pc.printf("Here \n\r");
     while(1) {
         if (ready) {
@@ -559,7 +604,7 @@
                 case 5: {
                     resp=OKResponse(buffer,"OK");
                     if (resp!=NULL) {
-                        
+
                         dev.printf("AT+CWJAP=\"CWMWIFI\",\"CWM2016TT\"\r\n");
                         state++;
                     }
@@ -582,10 +627,10 @@
                         char *strt = strtok(buffer,"\"");
                         strcpy(ipAddress,strtok(NULL,"\""));
                         strtok(NULL,"\"");
-                         strcpy(macAddress,strtok(NULL,"\""));
+                        strcpy(macAddress,strtok(NULL,"\""));
                         INFO("mac Address = %s", macAddress);
                         INFO("IP Address = %s", ipAddress);
-                          dev.printf("AT+CIPMUX=1\r\n");
+                        dev.printf("AT+CIPMUX=1\r\n");
                         state++;
                     }
 
@@ -594,65 +639,34 @@
                 case 8: {
                     resp=OKResponse(buffer,"OK");
                     if (resp!=NULL) {
-                       INFO("Ready");
-                       dev.printf("AT+CIPSERVER=1,5050\r\n");       //Set it as a server
-                       state++;
+                        INFO("Ready");
+                        dev.printf("AT+CIPSERVER=1,5050\r\n");       //Set it as a server
+                        state++;
                     }
 
-                     break;
+                    break;
                 }
-                     
+
                 case 9: {
                     resp=OKResponse(buffer,"+IPD");
                     if (resp!=NULL) {
-                        INFO("Got <%s>",buffer);                                                                                                                             
-                            commandReceived(buffer);                      // Interpret data
-                            run();                                
-                            state=9;                                // Keep on waiting for data
-                        }   
-                                       
-                
-                    
-                    break;
-                }
-                
-               /* case 9: {
-                    resp=OKResponse(buffer,"OK");
-                    if (resp!=NULL) {
-                       INFO("Ready");
-                       state++; 
-                       dev.printf("AT+CIPSEND=4,10\r\n");
+                        pc.printf("Here 1");
+                        commandReceived(buffer);                      // Interpret data
+                         pc.printf("Here 2");
+                        run();
+                         pc.printf("Here 3");
+                        state=9;                                // Keep on waiting for data
                     }
 
-                     break;
-                     }
-                    case 10: {
-                    resp=OKResponse(buffer,">");
-                    if (resp!=NULL) {
-                       INFO("SENDING");
-                       state++; 
-                       touch=tsi.readPercentage();
-                       dev.printf("TSI=%1.2f\n\r",touch);
-                       pc.printf("   ############  tsi=%1.2f\n\r",touch);
-                       //INFO("sensor=%1.2f",float)tsi.readPercentage());
-                    }
-                    case 11: {
-                    resp=OKResponse(buffer,"SEND OK");
-                    if (resp!=NULL) {
-                       INFO("SEND OK");
-                       state=9; 
-                       //wait(2);
-                       }
-                       wait(1);
-                       state=9; 
-                    }
+
 
-                     break;
-                }*/
+                    break;
+                }
+
 
 
             }
         }
-        //__WFI(); // DELETED AS IT KILLS THE KL25 RESPONSIVITY
+
     }
 }
\ No newline at end of file