Revenge of the Mouse

Dependencies:   4DGL-uLCD-SE EthernetInterface Game_Synchronizer LCD_fonts MMA8452 SDFileSystem mbed-rtos mbed wave_player

Fork of 2035_Tanks_Shell by ECE2035 Spring 2015 TA

Files at this revision

API Documentation at this revision

Comitter:
jford38
Date:
Tue Oct 06 01:54:18 2015 +0000
Child:
1:0589ea36661b
Commit message:
Working Player1 Demo. Uses print statements in lieu of the LCD (Don't have two LCD's yet.); Doesn't have two-way communication yet. Currently, P2 can only receive from P1 but can't send anything back.

Changed in this revision

4DGL-uLCD-SE.lib Show annotated file Show diff for this revision Revisions of this file
EthernetInterface.lib 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
mbed-rtos.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
two_player.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/4DGL-uLCD-SE.lib	Tue Oct 06 01:54:18 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/4180_1/code/4DGL-uLCD-SE/#e39a44de229a
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetInterface.lib	Tue Oct 06 01:54:18 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/EthernetInterface/#2fc406e2553f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Tue Oct 06 01:54:18 2015 +0000
@@ -0,0 +1,28 @@
+// THIS GUY LISTENS AND REPEATS. CLIENT. MASTER.
+// (It doesn't make sense, I know.)
+
+#include "mbed.h"
+#include "two_player.h"
+
+
+uLCD_2P uLCD(p28,p27,p26, PLAYER1); // serial tx, serial rx, reset pin;
+
+ 
+int main (void) {
+
+    pc.printf("I'm alive! Player 1\n");
+
+    uLCD.init();
+    pc.printf("Initialized...\n");
+    while(1) {
+        uLCD.set_button_state(1,0,1,0,1);
+        uLCD.background_color(BLUE);
+        uLCD.cls();
+        uLCD.update();
+        pc.printf("Updated. \n");
+        wait(1);
+        pc.printf("\033[2J\033[0;0H");
+    }
+    //
+    //uLCD.line(0,0,200,200,BLACK);    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-rtos.lib	Tue Oct 06 01:54:18 2015 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/mbed-rtos/#d7bd06319118
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Tue Oct 06 01:54:18 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/4f6c30876dfa
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/two_player.h	Tue Oct 06 01:54:18 2015 +0000
@@ -0,0 +1,165 @@
+#ifndef TWO_PLAYER_H__
+#define TWO_PLAYER_H__
+
+#include "uLCD_4DGL.h"
+#include "EthernetInterface.h"
+
+#define PLAYER1 true
+#define PLAYER2 false
+
+Serial pc(USBTX, USBRX);
+
+const char* PLAYER1_IP   = "192.168.2.1";
+const char* PLAYER2_IP   = "192.168.2.2";
+const int   SERVER_PORT = 7;
+
+DigitalOut led(LED2);
+
+
+class uLCD_2P
+{
+
+public :
+
+    int p1_p2;
+    enum Color
+    {
+        LINE_CMD,
+        BG_COLOR_CMD,
+        CIRCLE_CMD,
+        CLS_CMD,
+        BUTTONS_CMD
+    };
+
+
+    uLCD_2P(PinName tx, PinName rx, PinName rst, bool player) {
+        p1_p2 = player;
+        //LCD = new uLCD_4DGL (tx, rx, rst);        
+    }
+    
+    void init() {
+        
+        buffer_idx = 0;
+        sock = new UDPSocket();
+        endpoint = new Endpoint();
+        eth = new EthernetInterface();
+        
+        switch (p1_p2) {
+            case PLAYER1:           // Client
+                eth->init(PLAYER1_IP, "255.255.255.0", "0.0.0.0"); 
+                eth->connect();
+                sock->init();
+                endpoint->set_address(PLAYER2_IP, SERVER_PORT);
+                //sock.sendTo(endpoint, buffer, sizeof(buffer));
+                //int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer));
+                break;
+            case PLAYER2:           // Server
+                eth->init(PLAYER2_IP, "255.255.255.0", "0.0.0.0"); 
+                eth->connect();
+                sock->bind(SERVER_PORT);  
+                //sock.sendTo(endpoint, buffer, sizeof(buffer));
+                //int n = sock.receiveFrom(endpoint, buffer, sizeof(buffer));
+                break;   
+        }
+    }
+    
+    void draw(int CMD, int a, int b, int c, int d, int e, char nArgs){
+        
+        // Deal with overflows
+        //if(buffer_idx + nArgs > 256) {//flush();}
+        
+        buffer[buffer_idx] = CMD;
+        if(nArgs >= 1) buffer[buffer_idx+1] = a;
+        if(nArgs >= 2) buffer[buffer_idx+2] = b;
+        if(nArgs >= 3) buffer[buffer_idx+3] = c;
+        if(nArgs >= 4) buffer[buffer_idx+4] = d;
+        if(nArgs == 5) buffer[buffer_idx+5] = e;
+        // ERROR: nArgs > 5
+        
+        
+        buffer_idx += nArgs+1;
+    }
+    
+    void cls(void) { draw(CLS_CMD, 0,0,0,0,0,0); }
+    void background_color(int color) {
+        draw(BG_COLOR_CMD, color, 0,0,0,0,1);
+    }
+    void set_button_state(int a, int b, int c, int d, int e) { draw(BUTTONS_CMD, a,b,c,d,e,5); }
+    void line(int sx, int sy, int ex, int ey, int color) {
+        //draw(LINE_CMD, sx, sy, ex, ey, color);
+    }
+    
+
+    void update() {
+        if(p1_p2 == PLAYER1) {
+            sock->sendTo(*endpoint, (char*)buffer, (buffer_idx+1)*sizeof(buffer[0]));
+            buffer_idx = 0;
+        }else if(p1_p2 == PLAYER2) {
+            int n = sock->receiveFrom(*endpoint, (char*)buffer, sizeof(buffer)/sizeof(buffer[0]));    
+            buffer[n] = '\0';       
+        }
+        
+        int idx = 0;
+        while(buffer[idx] != '\0') {
+            char cmd = buffer[idx];
+            idx++;
+            
+            switch(cmd) {
+                case BG_COLOR_CMD:
+                    //LCD->background_color(buffer[idx+1]);
+                    pc.printf("Change the background to %x\n", buffer[idx]);
+                    idx += 1;
+                    break;
+                case LINE_CMD:
+                    break;
+                case CLS_CMD:
+                    //LCD->cls();
+                    pc.printf("Clear the screen!\n");
+                    idx += 0;
+                    break;
+                case BUTTONS_CMD:
+                    A_button = buffer[idx+0];
+                    B_button = buffer[idx+1];
+                    C_button = buffer[idx+2];
+                    D_button = buffer[idx+3];
+                    E_button = buffer[idx+4];
+                    pc.printf("Button Values: %d %d %d %d %d\n",
+                    A_button, B_button, C_button, D_button, E_button);
+                    idx += 5;
+                    break;
+                default:
+                    pc.printf("UNKNOWN CMD %d: This could get ugly!\n", cmd);
+                    idx += 0;
+            }
+        }
+        
+    }
+    
+    ~uLCD_2P() {            
+        sock->close();
+        eth->disconnect();  
+        delete sock;
+        delete endpoint;
+        delete eth; 
+    }
+ 
+    private:
+        bool master_slave;
+        
+        int buffer[256];
+        int  buffer_idx;
+        
+        UDPSocket* sock;
+        Endpoint*  endpoint;
+        EthernetInterface* eth;
+        
+        uLCD_4DGL* LCD;
+        
+        int A_button;
+        int B_button;
+        int C_button;
+        int D_button;
+        int E_button;
+};
+
+#endif
\ No newline at end of file