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

Committer:
jford38
Date:
Mon Oct 12 04:23:24 2015 +0000
Revision:
2:c358ad9aedc4
Parent:
1:0589ea36661b
Child:
3:3ddefff03cb2
Last commit with UDP. Switching to TCP.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jford38 0:899c85cd266f 1 #ifndef TWO_PLAYER_H__
jford38 0:899c85cd266f 2 #define TWO_PLAYER_H__
jford38 0:899c85cd266f 3
jford38 0:899c85cd266f 4 #include "uLCD_4DGL.h"
jford38 0:899c85cd266f 5 #include "EthernetInterface.h"
jford38 0:899c85cd266f 6
jford38 0:899c85cd266f 7 #define PLAYER1 true
jford38 0:899c85cd266f 8 #define PLAYER2 false
jford38 0:899c85cd266f 9
jford38 0:899c85cd266f 10 Serial pc(USBTX, USBRX);
jford38 0:899c85cd266f 11
jford38 0:899c85cd266f 12 const char* PLAYER1_IP = "192.168.2.1";
jford38 0:899c85cd266f 13 const char* PLAYER2_IP = "192.168.2.2";
jford38 0:899c85cd266f 14 const int SERVER_PORT = 7;
jford38 0:899c85cd266f 15
jford38 0:899c85cd266f 16 DigitalOut led(LED2);
jford38 0:899c85cd266f 17
jford38 0:899c85cd266f 18
jford38 0:899c85cd266f 19 class uLCD_2P
jford38 0:899c85cd266f 20 {
jford38 0:899c85cd266f 21
jford38 0:899c85cd266f 22 public :
jford38 0:899c85cd266f 23
jford38 0:899c85cd266f 24 int p1_p2;
jford38 0:899c85cd266f 25 enum Color
jford38 0:899c85cd266f 26 {
jford38 0:899c85cd266f 27 LINE_CMD,
jford38 0:899c85cd266f 28 BG_COLOR_CMD,
jford38 0:899c85cd266f 29 CIRCLE_CMD,
jford38 0:899c85cd266f 30 CLS_CMD,
jford38 2:c358ad9aedc4 31 I_AM_P1,
jford38 2:c358ad9aedc4 32 I_AM_P2
jford38 0:899c85cd266f 33 };
jford38 0:899c85cd266f 34
jford38 0:899c85cd266f 35
jford38 0:899c85cd266f 36 uLCD_2P(PinName tx, PinName rx, PinName rst, bool player) {
jford38 0:899c85cd266f 37 p1_p2 = player;
jford38 0:899c85cd266f 38 //LCD = new uLCD_4DGL (tx, rx, rst);
jford38 0:899c85cd266f 39 }
jford38 0:899c85cd266f 40
jford38 0:899c85cd266f 41 void init() {
jford38 0:899c85cd266f 42
jford38 0:899c85cd266f 43 buffer_idx = 0;
jford38 1:0589ea36661b 44 memset(buttons, 0, sizeof(buttons));
jford38 1:0589ea36661b 45
jford38 0:899c85cd266f 46 sock = new UDPSocket();
jford38 0:899c85cd266f 47 endpoint = new Endpoint();
jford38 0:899c85cd266f 48 eth = new EthernetInterface();
jford38 0:899c85cd266f 49
jford38 0:899c85cd266f 50 switch (p1_p2) {
jford38 0:899c85cd266f 51 case PLAYER1: // Client
jford38 0:899c85cd266f 52 eth->init(PLAYER1_IP, "255.255.255.0", "0.0.0.0");
jford38 0:899c85cd266f 53 eth->connect();
jford38 0:899c85cd266f 54 sock->init();
jford38 0:899c85cd266f 55 endpoint->set_address(PLAYER2_IP, SERVER_PORT);
jford38 0:899c85cd266f 56 break;
jford38 0:899c85cd266f 57 case PLAYER2: // Server
jford38 0:899c85cd266f 58 eth->init(PLAYER2_IP, "255.255.255.0", "0.0.0.0");
jford38 0:899c85cd266f 59 eth->connect();
jford38 0:899c85cd266f 60 sock->bind(SERVER_PORT);
jford38 0:899c85cd266f 61 break;
jford38 0:899c85cd266f 62 }
jford38 0:899c85cd266f 63 }
jford38 0:899c85cd266f 64
jford38 0:899c85cd266f 65 void draw(int CMD, int a, int b, int c, int d, int e, char nArgs){
jford38 0:899c85cd266f 66
jford38 0:899c85cd266f 67 // Deal with overflows
jford38 0:899c85cd266f 68 //if(buffer_idx + nArgs > 256) {//flush();}
jford38 0:899c85cd266f 69
jford38 0:899c85cd266f 70 buffer[buffer_idx] = CMD;
jford38 0:899c85cd266f 71 if(nArgs >= 1) buffer[buffer_idx+1] = a;
jford38 0:899c85cd266f 72 if(nArgs >= 2) buffer[buffer_idx+2] = b;
jford38 0:899c85cd266f 73 if(nArgs >= 3) buffer[buffer_idx+3] = c;
jford38 0:899c85cd266f 74 if(nArgs >= 4) buffer[buffer_idx+4] = d;
jford38 0:899c85cd266f 75 if(nArgs == 5) buffer[buffer_idx+5] = e;
jford38 0:899c85cd266f 76 // ERROR: nArgs > 5
jford38 0:899c85cd266f 77
jford38 0:899c85cd266f 78
jford38 0:899c85cd266f 79 buffer_idx += nArgs+1;
jford38 0:899c85cd266f 80 }
jford38 0:899c85cd266f 81
jford38 0:899c85cd266f 82 void cls(void) { draw(CLS_CMD, 0,0,0,0,0,0); }
jford38 0:899c85cd266f 83 void background_color(int color) {
jford38 0:899c85cd266f 84 draw(BG_COLOR_CMD, color, 0,0,0,0,1);
jford38 0:899c85cd266f 85 }
jford38 1:0589ea36661b 86 void set_button_state(int a, int b, int c, int d, int e) {
jford38 1:0589ea36661b 87 buttons[0] = a; buttons[1] = b; buttons[2] = c;
jford38 1:0589ea36661b 88 buttons[3] = d; buttons[4] = e;
jford38 1:0589ea36661b 89 }
jford38 1:0589ea36661b 90 int* get_button_state() {
jford38 1:0589ea36661b 91 return buttons;
jford38 1:0589ea36661b 92 }
jford38 0:899c85cd266f 93 void line(int sx, int sy, int ex, int ey, int color) {
jford38 0:899c85cd266f 94 //draw(LINE_CMD, sx, sy, ex, ey, color);
jford38 0:899c85cd266f 95 }
jford38 0:899c85cd266f 96
jford38 0:899c85cd266f 97
jford38 0:899c85cd266f 98 void update() {
jford38 1:0589ea36661b 99 sock->set_blocking(true, 100);
jford38 0:899c85cd266f 100 if(p1_p2 == PLAYER1) {
jford38 2:c358ad9aedc4 101 pc.printf("Sending draw data...\n");
jford38 0:899c85cd266f 102 sock->sendTo(*endpoint, (char*)buffer, (buffer_idx+1)*sizeof(buffer[0]));
jford38 0:899c85cd266f 103 buffer_idx = 0;
jford38 1:0589ea36661b 104
jford38 2:c358ad9aedc4 105 pc.printf("Receiving button data...\n");
jford38 2:c358ad9aedc4 106 int n = sock->receiveFrom(*endpoint, (char*)buttons, sizeof(buttons));
jford38 2:c358ad9aedc4 107 if(n < 0) {pc.printf("RECEIVE ERROR.\n");}
jford38 2:c358ad9aedc4 108 pc.printf("Received button data...\n");
jford38 2:c358ad9aedc4 109
jford38 0:899c85cd266f 110 }else if(p1_p2 == PLAYER2) {
jford38 2:c358ad9aedc4 111 pc.printf("Receiving draw buffer...\n");
jford38 2:c358ad9aedc4 112 int n = sock->receiveFrom(*endpoint, (char*)buffer, sizeof(buffer));
jford38 2:c358ad9aedc4 113 if(n < 0) {pc.printf("RECEIVE ERROR.\n");}
jford38 1:0589ea36661b 114 buffer[n] = '\0';
jford38 2:c358ad9aedc4 115 pc.printf("Received draw data...\n");
jford38 2:c358ad9aedc4 116 pc.printf("Sending button data...\n");
jford38 1:0589ea36661b 117 sock->sendTo(*endpoint, (char*)buttons, sizeof(buttons));
jford38 0:899c85cd266f 118 }
jford38 0:899c85cd266f 119
jford38 2:c358ad9aedc4 120 int i = 0;
jford38 2:c358ad9aedc4 121 while(buffer[i] != '\0') {
jford38 2:c358ad9aedc4 122 pc.printf("%d ", buffer[i]);
jford38 2:c358ad9aedc4 123 i++;
jford38 2:c358ad9aedc4 124 }
jford38 2:c358ad9aedc4 125 pc.printf("\n");
jford38 2:c358ad9aedc4 126
jford38 0:899c85cd266f 127 int idx = 0;
jford38 0:899c85cd266f 128 while(buffer[idx] != '\0') {
jford38 0:899c85cd266f 129 char cmd = buffer[idx];
jford38 0:899c85cd266f 130 idx++;
jford38 0:899c85cd266f 131
jford38 0:899c85cd266f 132 switch(cmd) {
jford38 0:899c85cd266f 133 case BG_COLOR_CMD:
jford38 0:899c85cd266f 134 //LCD->background_color(buffer[idx+1]);
jford38 0:899c85cd266f 135 pc.printf("Change the background to %x\n", buffer[idx]);
jford38 0:899c85cd266f 136 idx += 1;
jford38 0:899c85cd266f 137 break;
jford38 0:899c85cd266f 138 case LINE_CMD:
jford38 0:899c85cd266f 139 break;
jford38 0:899c85cd266f 140 case CLS_CMD:
jford38 0:899c85cd266f 141 //LCD->cls();
jford38 0:899c85cd266f 142 pc.printf("Clear the screen!\n");
jford38 0:899c85cd266f 143 idx += 0;
jford38 0:899c85cd266f 144 break;
jford38 2:c358ad9aedc4 145 case I_AM_P1:
jford38 2:c358ad9aedc4 146 case I_AM_P2:
jford38 2:c358ad9aedc4 147 break;
jford38 0:899c85cd266f 148 default:
jford38 0:899c85cd266f 149 pc.printf("UNKNOWN CMD %d: This could get ugly!\n", cmd);
jford38 0:899c85cd266f 150 idx += 0;
jford38 0:899c85cd266f 151 }
jford38 0:899c85cd266f 152 }
jford38 0:899c85cd266f 153
jford38 0:899c85cd266f 154 }
jford38 0:899c85cd266f 155
jford38 0:899c85cd266f 156 ~uLCD_2P() {
jford38 0:899c85cd266f 157 sock->close();
jford38 0:899c85cd266f 158 eth->disconnect();
jford38 0:899c85cd266f 159 delete sock;
jford38 0:899c85cd266f 160 delete endpoint;
jford38 0:899c85cd266f 161 delete eth;
jford38 0:899c85cd266f 162 }
jford38 0:899c85cd266f 163
jford38 0:899c85cd266f 164 private:
jford38 0:899c85cd266f 165 bool master_slave;
jford38 0:899c85cd266f 166
jford38 0:899c85cd266f 167 int buffer[256];
jford38 0:899c85cd266f 168 int buffer_idx;
jford38 0:899c85cd266f 169
jford38 0:899c85cd266f 170 UDPSocket* sock;
jford38 0:899c85cd266f 171 Endpoint* endpoint;
jford38 0:899c85cd266f 172 EthernetInterface* eth;
jford38 0:899c85cd266f 173
jford38 0:899c85cd266f 174 uLCD_4DGL* LCD;
jford38 0:899c85cd266f 175
jford38 1:0589ea36661b 176 int buttons[5];
jford38 0:899c85cd266f 177 };
jford38 0:899c85cd266f 178
jford38 0:899c85cd266f 179 #endif