Final controller for snake

Dependencies:   MMA8451Q TSI mbed

Fork of snakecontroller by Serpentine

Revision:
14:925768bb5474
Parent:
13:51a676379e3b
--- a/main.cpp	Fri Jun 10 12:04:17 2016 +0000
+++ b/main.cpp	Fri Jun 10 15:01:50 2016 +0000
@@ -1,6 +1,7 @@
 /**
     Snake Controller
     main.cpp
+    Board ID    2
     Purpose:    This function runs on a FRDM KL25Z. It uses the accelerometer to measure the tilt of the board.
                 It outputs a 2 bit number that describes the direction. The board is also fitted with four LEDS
                 oriented in the following configuration: o8o to provide feedback to the user.
@@ -37,9 +38,37 @@
 DigitalOut rst(PTD1); // single digital pin to drive the esp8266 reset line
 
 
+// subroutine to run anytime a serial interrupt arrives from the device
+// this basically passes everything thatthe device produces on to the pc terminal screen
+void dev_recv()
+{
+    //led1 = !led1;
+    while(dev.readable()) {
+        pc.putc(dev.getc());
+        //wait_us(1);
+    }
+}
+// subroutine to service the serial interrupt on the pc connection
+// this is a bit more complex - it takes what the use sends on the pc and copies it on to the device 
+// the esp should echo these straight back to the the pc if all is well
+// this also detects the end of command character which is ascii 13 (0x0d) adn adds a linefeed after it =asscii 10 (0x0a)
+void pc_recv()
+{
+    char c;
+    //led4 = !led4;
+    while(pc.readable()) {
+        c=pc.getc();
+        dev.putc(c);
+        pc.putc(c); // echo back
+        if(c==13) {dev.putc(10); // send the linefeed to complement the carriage return generated by return key on the pc
+        pc.putc(10);
+            }
+    }
+}
+
 void setupWiFi()
 {
-    dev.printf("AT+RST\r\n");                       //Reset WiFi
+dev.printf("AT+RST\r\n");                       //Reset WiFi
     pc.printf("RESET\r\n");
     
     wait(2);
@@ -55,14 +84,26 @@
     dev.printf("AT+CIPMUX=1\r\n");                  //Allow multiple connections
     
     wait(2);
-    dev.printf("AT+CIPSTART=0,\"TCP\",\"192.168.1.6\",5050\r\n");       //Open connection with the server
-    
-    wait(2);
+    dev.printf("AT+CIPSTART=2,\"TCP\",\"192.168.1.6\",5050\r\n");       //Open connection with the server
 }
 
 int main()
 {
-    setupWiFi();
+    //Initialise WiFi stuff
+    rst=0;
+    wait(1);
+    rst=1; // send the esp8266 reset
+    wait(1);
+    pc.printf("ok off we go....\n\r");
+    
+    pc.baud(115200); // NB maybe this should go before this
+    dev.baud(115200);
+
+    pc.attach(&pc_recv, Serial::RxIrq); // attach the two interrupt services
+    dev.attach(&dev_recv, Serial::RxIrq);
+    
+    PrevDirection = 4;                            //Initialise PrevDirection with a value that will mean it will always pass throught he if at the end
+    setupWiFi();                                  //Run the WiFi setup
     while (1) 
     {
          
@@ -167,14 +208,14 @@
         {
             //Send Direction to server
             
-           // dev.printf("AT+CIPSEND=0,7\r\n");
-            //wait(2);
+            dev.printf("AT+CIPSEND=2,7\r\n");
+            wait(0.1);
             
-            //dev.printf("2dir%d\r\n",Direction);              //Identifier,Direction Tag,Direction Value
+            dev.printf("2dir%d\r\n",Direction);              //Identifier,Direction Tag,Direction Value
             
             PrevDirection = Direction;
         }
-        printf("Direction = %d \r\n", Direction);          //Print the value of Direction to the serial for debugging       
+        //printf("Direction = %d \r\n", Direction);          //Print the value of Direction to the serial for debugging       
         
     }//endwhile
 }//endmain