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

two_player.h

Committer:
jford38
Date:
2015-10-12
Revision:
2:c358ad9aedc4
Parent:
1:0589ea36661b
Child:
3:3ddefff03cb2

File content as of revision 2:c358ad9aedc4:

#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,
        I_AM_P1,
        I_AM_P2
    };


    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;
        memset(buttons, 0, sizeof(buttons));
        
        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);
                break;
            case PLAYER2:           // Server
                eth->init(PLAYER2_IP, "255.255.255.0", "0.0.0.0"); 
                eth->connect();
                sock->bind(SERVER_PORT);  
                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) { 
        buttons[0] = a; buttons[1] = b; buttons[2] = c; 
        buttons[3] = d; buttons[4] = e;   
    }
    int* get_button_state() {
        return buttons;
    }
    void line(int sx, int sy, int ex, int ey, int color) {
        //draw(LINE_CMD, sx, sy, ex, ey, color);
    }
    

    void update() {
        sock->set_blocking(true, 100);
        if(p1_p2 == PLAYER1) {
            pc.printf("Sending draw data...\n");  
            sock->sendTo(*endpoint, (char*)buffer, (buffer_idx+1)*sizeof(buffer[0]));
            buffer_idx = 0;
            
            pc.printf("Receiving button data...\n");  
            int n = sock->receiveFrom(*endpoint, (char*)buttons, sizeof(buttons));
            if(n < 0) {pc.printf("RECEIVE ERROR.\n");}     
            pc.printf("Received button data...\n");         
        
        }else if(p1_p2 == PLAYER2) {
            pc.printf("Receiving draw buffer...\n");      
            int n = sock->receiveFrom(*endpoint, (char*)buffer, sizeof(buffer));  
            if(n < 0) {pc.printf("RECEIVE ERROR.\n");}
            buffer[n] = '\0';   
            pc.printf("Received draw data...\n");  
            pc.printf("Sending button data...\n");  
            sock->sendTo(*endpoint, (char*)buttons, sizeof(buttons));          
        }
        
        int i = 0;        
        while(buffer[i] != '\0') {
            pc.printf("%d ", buffer[i]);
            i++;
        }
        pc.printf("\n");
        
        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 I_AM_P1:
                case I_AM_P2:
                    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 buttons[5];
};

#endif