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:
Wed Oct 07 00:43:25 2015 +0000
Revision:
1:0589ea36661b
Parent:
0:899c85cd266f
Child:
2:c358ad9aedc4
2-Player game via Ethernet. First try.

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 0:899c85cd266f 31 };
jford38 0:899c85cd266f 32
jford38 0:899c85cd266f 33
jford38 0:899c85cd266f 34 uLCD_2P(PinName tx, PinName rx, PinName rst, bool player) {
jford38 0:899c85cd266f 35 p1_p2 = player;
jford38 0:899c85cd266f 36 //LCD = new uLCD_4DGL (tx, rx, rst);
jford38 0:899c85cd266f 37 }
jford38 0:899c85cd266f 38
jford38 0:899c85cd266f 39 void init() {
jford38 0:899c85cd266f 40
jford38 0:899c85cd266f 41 buffer_idx = 0;
jford38 1:0589ea36661b 42 memset(buttons, 0, sizeof(buttons));
jford38 1:0589ea36661b 43
jford38 0:899c85cd266f 44 sock = new UDPSocket();
jford38 0:899c85cd266f 45 endpoint = new Endpoint();
jford38 0:899c85cd266f 46 eth = new EthernetInterface();
jford38 0:899c85cd266f 47
jford38 0:899c85cd266f 48 switch (p1_p2) {
jford38 0:899c85cd266f 49 case PLAYER1: // Client
jford38 0:899c85cd266f 50 eth->init(PLAYER1_IP, "255.255.255.0", "0.0.0.0");
jford38 0:899c85cd266f 51 eth->connect();
jford38 0:899c85cd266f 52 sock->init();
jford38 0:899c85cd266f 53 endpoint->set_address(PLAYER2_IP, SERVER_PORT);
jford38 0:899c85cd266f 54 break;
jford38 0:899c85cd266f 55 case PLAYER2: // Server
jford38 0:899c85cd266f 56 eth->init(PLAYER2_IP, "255.255.255.0", "0.0.0.0");
jford38 0:899c85cd266f 57 eth->connect();
jford38 0:899c85cd266f 58 sock->bind(SERVER_PORT);
jford38 0:899c85cd266f 59 break;
jford38 0:899c85cd266f 60 }
jford38 0:899c85cd266f 61 }
jford38 0:899c85cd266f 62
jford38 0:899c85cd266f 63 void draw(int CMD, int a, int b, int c, int d, int e, char nArgs){
jford38 0:899c85cd266f 64
jford38 0:899c85cd266f 65 // Deal with overflows
jford38 0:899c85cd266f 66 //if(buffer_idx + nArgs > 256) {//flush();}
jford38 0:899c85cd266f 67
jford38 0:899c85cd266f 68 buffer[buffer_idx] = CMD;
jford38 0:899c85cd266f 69 if(nArgs >= 1) buffer[buffer_idx+1] = a;
jford38 0:899c85cd266f 70 if(nArgs >= 2) buffer[buffer_idx+2] = b;
jford38 0:899c85cd266f 71 if(nArgs >= 3) buffer[buffer_idx+3] = c;
jford38 0:899c85cd266f 72 if(nArgs >= 4) buffer[buffer_idx+4] = d;
jford38 0:899c85cd266f 73 if(nArgs == 5) buffer[buffer_idx+5] = e;
jford38 0:899c85cd266f 74 // ERROR: nArgs > 5
jford38 0:899c85cd266f 75
jford38 0:899c85cd266f 76
jford38 0:899c85cd266f 77 buffer_idx += nArgs+1;
jford38 0:899c85cd266f 78 }
jford38 0:899c85cd266f 79
jford38 0:899c85cd266f 80 void cls(void) { draw(CLS_CMD, 0,0,0,0,0,0); }
jford38 0:899c85cd266f 81 void background_color(int color) {
jford38 0:899c85cd266f 82 draw(BG_COLOR_CMD, color, 0,0,0,0,1);
jford38 0:899c85cd266f 83 }
jford38 1:0589ea36661b 84 void set_button_state(int a, int b, int c, int d, int e) {
jford38 1:0589ea36661b 85 buttons[0] = a; buttons[1] = b; buttons[2] = c;
jford38 1:0589ea36661b 86 buttons[3] = d; buttons[4] = e;
jford38 1:0589ea36661b 87 }
jford38 1:0589ea36661b 88 int* get_button_state() {
jford38 1:0589ea36661b 89 return buttons;
jford38 1:0589ea36661b 90 }
jford38 0:899c85cd266f 91 void line(int sx, int sy, int ex, int ey, int color) {
jford38 0:899c85cd266f 92 //draw(LINE_CMD, sx, sy, ex, ey, color);
jford38 0:899c85cd266f 93 }
jford38 0:899c85cd266f 94
jford38 0:899c85cd266f 95
jford38 0:899c85cd266f 96 void update() {
jford38 1:0589ea36661b 97 sock->set_blocking(true, 100);
jford38 0:899c85cd266f 98 if(p1_p2 == PLAYER1) {
jford38 0:899c85cd266f 99 sock->sendTo(*endpoint, (char*)buffer, (buffer_idx+1)*sizeof(buffer[0]));
jford38 0:899c85cd266f 100 buffer_idx = 0;
jford38 1:0589ea36661b 101
jford38 1:0589ea36661b 102 int n = sock->receiveFrom(*endpoint, (char*)buttons, sizeof(buttons));
jford38 0:899c85cd266f 103 }else if(p1_p2 == PLAYER2) {
jford38 1:0589ea36661b 104
jford38 1:0589ea36661b 105 int n = sock->receiveFrom(*endpoint, (char*)buffer, sizeof(buffer));
jford38 1:0589ea36661b 106 buffer[n] = '\0';
jford38 1:0589ea36661b 107
jford38 1:0589ea36661b 108 sock->sendTo(*endpoint, (char*)buttons, sizeof(buttons));
jford38 0:899c85cd266f 109 }
jford38 0:899c85cd266f 110
jford38 0:899c85cd266f 111 int idx = 0;
jford38 0:899c85cd266f 112 while(buffer[idx] != '\0') {
jford38 0:899c85cd266f 113 char cmd = buffer[idx];
jford38 0:899c85cd266f 114 idx++;
jford38 0:899c85cd266f 115
jford38 0:899c85cd266f 116 switch(cmd) {
jford38 0:899c85cd266f 117 case BG_COLOR_CMD:
jford38 0:899c85cd266f 118 //LCD->background_color(buffer[idx+1]);
jford38 0:899c85cd266f 119 pc.printf("Change the background to %x\n", buffer[idx]);
jford38 0:899c85cd266f 120 idx += 1;
jford38 0:899c85cd266f 121 break;
jford38 0:899c85cd266f 122 case LINE_CMD:
jford38 0:899c85cd266f 123 break;
jford38 0:899c85cd266f 124 case CLS_CMD:
jford38 0:899c85cd266f 125 //LCD->cls();
jford38 0:899c85cd266f 126 pc.printf("Clear the screen!\n");
jford38 0:899c85cd266f 127 idx += 0;
jford38 0:899c85cd266f 128 break;
jford38 0:899c85cd266f 129 default:
jford38 0:899c85cd266f 130 pc.printf("UNKNOWN CMD %d: This could get ugly!\n", cmd);
jford38 0:899c85cd266f 131 idx += 0;
jford38 0:899c85cd266f 132 }
jford38 0:899c85cd266f 133 }
jford38 0:899c85cd266f 134
jford38 0:899c85cd266f 135 }
jford38 0:899c85cd266f 136
jford38 0:899c85cd266f 137 ~uLCD_2P() {
jford38 0:899c85cd266f 138 sock->close();
jford38 0:899c85cd266f 139 eth->disconnect();
jford38 0:899c85cd266f 140 delete sock;
jford38 0:899c85cd266f 141 delete endpoint;
jford38 0:899c85cd266f 142 delete eth;
jford38 0:899c85cd266f 143 }
jford38 0:899c85cd266f 144
jford38 0:899c85cd266f 145 private:
jford38 0:899c85cd266f 146 bool master_slave;
jford38 0:899c85cd266f 147
jford38 0:899c85cd266f 148 int buffer[256];
jford38 0:899c85cd266f 149 int buffer_idx;
jford38 0:899c85cd266f 150
jford38 0:899c85cd266f 151 UDPSocket* sock;
jford38 0:899c85cd266f 152 Endpoint* endpoint;
jford38 0:899c85cd266f 153 EthernetInterface* eth;
jford38 0:899c85cd266f 154
jford38 0:899c85cd266f 155 uLCD_4DGL* LCD;
jford38 0:899c85cd266f 156
jford38 1:0589ea36661b 157 int buttons[5];
jford38 0:899c85cd266f 158 };
jford38 0:899c85cd266f 159
jford38 0:899c85cd266f 160 #endif