Matthew Riley / Mbed 2 deprecated Dualing_Tanks

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

Fork of 2035_Tanks_Shell by ECE2035 Spring 2015 TA

Committer:
jford38
Date:
Thu Oct 15 22:07:40 2015 +0000
Revision:
5:cfec780c935b
Parent:
4:d8bd8b41468d
Working graphics commands on LCDs.; circle, line, triangle, filled_circle, filled_rectangle, rectangle, etc.; ; pen_size() seems to kill the LCD's. Don't know why. Probably just gonna exclude it.

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 3:3ddefff03cb2 4 #include <cmath>
jford38 0:899c85cd266f 5 #include "uLCD_4DGL.h"
jford38 0:899c85cd266f 6 #include "EthernetInterface.h"
jford38 0:899c85cd266f 7
jford38 0:899c85cd266f 8 #define PLAYER1 true
jford38 0:899c85cd266f 9 #define PLAYER2 false
jford38 0:899c85cd266f 10
jford38 0:899c85cd266f 11 Serial pc(USBTX, USBRX);
jford38 0:899c85cd266f 12
jford38 0:899c85cd266f 13 const char* PLAYER1_IP = "192.168.2.1";
jford38 0:899c85cd266f 14 const char* PLAYER2_IP = "192.168.2.2";
jford38 0:899c85cd266f 15 const int SERVER_PORT = 7;
jford38 0:899c85cd266f 16
jford38 0:899c85cd266f 17 DigitalOut led(LED2);
jford38 0:899c85cd266f 18
jford38 0:899c85cd266f 19
jford38 0:899c85cd266f 20 class uLCD_2P
jford38 0:899c85cd266f 21 {
jford38 0:899c85cd266f 22
jford38 0:899c85cd266f 23 public :
jford38 0:899c85cd266f 24
jford38 0:899c85cd266f 25 int p1_p2;
jford38 3:3ddefff03cb2 26
jford38 0:899c85cd266f 27 enum Color
jford38 0:899c85cd266f 28 {
jford38 5:cfec780c935b 29 CLS_CMD,
jford38 3:3ddefff03cb2 30 BG_COLOR_CMD,
jford38 0:899c85cd266f 31 LINE_CMD,
jford38 0:899c85cd266f 32 CIRCLE_CMD,
jford38 3:3ddefff03cb2 33 FILLED_CIRCLE_CMD,
jford38 3:3ddefff03cb2 34 TRI_CMD,
jford38 3:3ddefff03cb2 35 RECT_CMD,
jford38 3:3ddefff03cb2 36 FILLED_RECT_CMD,
jford38 5:cfec780c935b 37 PIX_CMD
jford38 0:899c85cd266f 38 };
jford38 0:899c85cd266f 39
jford38 0:899c85cd266f 40 uLCD_2P(PinName tx, PinName rx, PinName rst, bool player) {
jford38 0:899c85cd266f 41 p1_p2 = player;
jford38 5:cfec780c935b 42 LCD = new uLCD_4DGL (tx, rx, rst);
jford38 0:899c85cd266f 43 }
jford38 0:899c85cd266f 44
jford38 0:899c85cd266f 45 void init() {
jford38 0:899c85cd266f 46
jford38 0:899c85cd266f 47 buffer_idx = 0;
jford38 1:0589ea36661b 48 memset(buttons, 0, sizeof(buttons));
jford38 1:0589ea36661b 49
jford38 0:899c85cd266f 50 switch (p1_p2) {
jford38 0:899c85cd266f 51 case PLAYER1: // Client
jford38 3:3ddefff03cb2 52
jford38 3:3ddefff03cb2 53 eth = new EthernetInterface();
jford38 0:899c85cd266f 54 eth->init(PLAYER1_IP, "255.255.255.0", "0.0.0.0");
jford38 0:899c85cd266f 55 eth->connect();
jford38 3:3ddefff03cb2 56
jford38 3:3ddefff03cb2 57 sock = new TCPSocketConnection();
jford38 3:3ddefff03cb2 58 while(sock->connect(PLAYER2_IP, SERVER_PORT) < 0) {
jford38 3:3ddefff03cb2 59 pc.printf("Trying to connect.\n");
jford38 3:3ddefff03cb2 60 //wait(1);
jford38 3:3ddefff03cb2 61 }
jford38 3:3ddefff03cb2 62
jford38 0:899c85cd266f 63 break;
jford38 0:899c85cd266f 64 case PLAYER2: // Server
jford38 3:3ddefff03cb2 65 eth = new EthernetInterface();
jford38 0:899c85cd266f 66 eth->init(PLAYER2_IP, "255.255.255.0", "0.0.0.0");
jford38 0:899c85cd266f 67 eth->connect();
jford38 3:3ddefff03cb2 68
jford38 3:3ddefff03cb2 69 server = new TCPSocketServer();
jford38 3:3ddefff03cb2 70 server->bind(SERVER_PORT);
jford38 3:3ddefff03cb2 71 server->listen();
jford38 3:3ddefff03cb2 72 sock = new TCPSocketConnection();
jford38 3:3ddefff03cb2 73 server->accept(*sock);
jford38 3:3ddefff03cb2 74 sock->set_blocking(false, 1500);
jford38 0:899c85cd266f 75 break;
jford38 0:899c85cd266f 76 }
jford38 3:3ddefff03cb2 77 pc.printf("Ethernet Connected.\n");
jford38 0:899c85cd266f 78 }
jford38 0:899c85cd266f 79
jford38 3:3ddefff03cb2 80 // Yes, this sucks. If you're smart, find a good way to do variable args and show me!
jford38 3:3ddefff03cb2 81 // Look into template metaprogramming!
jford38 3:3ddefff03cb2 82
jford38 3:3ddefff03cb2 83 void draw(int CMD) {_draw(CMD, 0,0,0,0,0,0,0, 0); }
jford38 3:3ddefff03cb2 84 void draw(int CMD, int a) { _draw(CMD, a, 0,0,0,0,0,0, 1); }
jford38 3:3ddefff03cb2 85 void draw(int CMD, int a, int b) { _draw(CMD, a, b, 0,0,0,0,0, 2); }
jford38 3:3ddefff03cb2 86 void draw(int CMD, int a, int b, int c) { _draw(CMD, a, b, c, 0,0,0,0, 3); }
jford38 3:3ddefff03cb2 87 void draw(int CMD, int a, int b, int c, int d) { _draw(CMD, a, b, c, d, 0,0,0, 4); }
jford38 3:3ddefff03cb2 88 void draw(int CMD, int a, int b, int c, int d, int e) { _draw(CMD, a, b, c, d, e, 0,0, 5); }
jford38 3:3ddefff03cb2 89 void draw(int CMD, int a, int b, int c, int d, int e, int f) { _draw(CMD, a, b, c, d, e, f, 0, 6); }
jford38 3:3ddefff03cb2 90 void draw(int CMD, int a, int b, int c, int d, int e, int f, int g) { _draw(CMD, a, b, c, d, e, f, g, 7); }
jford38 3:3ddefff03cb2 91
jford38 3:3ddefff03cb2 92 void _draw(int CMD, int a, int b, int c, int d, int e, int f, int g, char nArgs){
jford38 0:899c85cd266f 93
jford38 0:899c85cd266f 94 // Deal with overflows
jford38 3:3ddefff03cb2 95 // if(buffer_idx + nArgs > 256) {//flush();}
jford38 3:3ddefff03cb2 96
jford38 3:3ddefff03cb2 97 if(nArgs > 7) {
jford38 3:3ddefff03cb2 98 pc.printf("Error in call to _draw(): nArgs > 7 not allowed!\n");
jford38 3:3ddefff03cb2 99 return;
jford38 3:3ddefff03cb2 100 }
jford38 0:899c85cd266f 101
jford38 0:899c85cd266f 102 buffer[buffer_idx] = CMD;
jford38 0:899c85cd266f 103 if(nArgs >= 1) buffer[buffer_idx+1] = a;
jford38 0:899c85cd266f 104 if(nArgs >= 2) buffer[buffer_idx+2] = b;
jford38 0:899c85cd266f 105 if(nArgs >= 3) buffer[buffer_idx+3] = c;
jford38 0:899c85cd266f 106 if(nArgs >= 4) buffer[buffer_idx+4] = d;
jford38 3:3ddefff03cb2 107 if(nArgs >= 5) buffer[buffer_idx+5] = e;
jford38 3:3ddefff03cb2 108 if(nArgs >= 6) buffer[buffer_idx+6] = f;
jford38 3:3ddefff03cb2 109 if(nArgs >= 7) buffer[buffer_idx+7] = g;
jford38 3:3ddefff03cb2 110 // ERROR: nArgs > 7
jford38 0:899c85cd266f 111
jford38 0:899c85cd266f 112
jford38 0:899c85cd266f 113 buffer_idx += nArgs+1;
jford38 0:899c85cd266f 114 }
jford38 0:899c85cd266f 115
jford38 3:3ddefff03cb2 116
jford38 5:cfec780c935b 117 void background_color(int color) { draw(BG_COLOR_CMD, color); }
jford38 5:cfec780c935b 118 void line(int sx, int sy, int ex, int ey, int color) { draw(LINE_CMD, sx, sy, ex, ey, color); }
jford38 5:cfec780c935b 119 void circle(int x , int y , int radius, int color) { draw(CIRCLE_CMD, x, y, radius, color); }
jford38 5:cfec780c935b 120 void filled_circle(int x , int y , int radius, int color) { draw(FILLED_CIRCLE_CMD, x, y, radius, color); }
jford38 3:3ddefff03cb2 121 void triangle(int a, int b, int c, int d , int e, int f, int col) { draw(TRI_CMD, a, b, c, d, e, f, col); }
jford38 5:cfec780c935b 122 void rectangle(int a, int b, int c, int d, int col) { draw(RECT_CMD, a, b, c, d, col); }
jford38 5:cfec780c935b 123 void filled_rectangle(int a, int b, int c, int d, int col) { draw(FILLED_RECT_CMD, a, b, c, d, col); }
jford38 5:cfec780c935b 124 void pixel(int a, int b, int col) { draw(PIX_CMD, a, b, col); }
jford38 5:cfec780c935b 125 void cls(void) { draw(CLS_CMD); }
jford38 3:3ddefff03cb2 126
jford38 3:3ddefff03cb2 127 /*void BLIT(int x1, int y1, int x2, int y2, int *colors) {
jford38 3:3ddefff03cb2 128 int size = abs((x1-x2)*(y1-y2));
jford38 3:3ddefff03cb2 129 // pad to a multiple of 4 and memset buffer...
jford38 3:3ddefff03cb2 130 // I'll get to it later.
jford38 3:3ddefff03cb2 131 }*/
jford38 3:3ddefff03cb2 132
jford38 3:3ddefff03cb2 133 // Reads don't need to be done on the slave side. Hopefully both sides match!
jford38 3:3ddefff03cb2 134 int read_pixel(int x, int y) { return LCD->read_pixel(x, y); }
jford38 3:3ddefff03cb2 135
jford38 1:0589ea36661b 136 void set_button_state(int a, int b, int c, int d, int e) {
jford38 1:0589ea36661b 137 buttons[0] = a; buttons[1] = b; buttons[2] = c;
jford38 1:0589ea36661b 138 buttons[3] = d; buttons[4] = e;
jford38 1:0589ea36661b 139 }
jford38 3:3ddefff03cb2 140
jford38 1:0589ea36661b 141 int* get_button_state() {
jford38 1:0589ea36661b 142 return buttons;
jford38 1:0589ea36661b 143 }
jford38 0:899c85cd266f 144
jford38 0:899c85cd266f 145 void update() {
jford38 5:cfec780c935b 146 int buffer_size;
jford38 1:0589ea36661b 147 sock->set_blocking(true, 100);
jford38 0:899c85cd266f 148 if(p1_p2 == PLAYER1) {
jford38 5:cfec780c935b 149 buffer_size = buffer_idx;
jford38 5:cfec780c935b 150 sock->send_all((char*)buffer, (buffer_size+1)*sizeof(buffer[0]));
jford38 0:899c85cd266f 151 buffer_idx = 0;
jford38 5:cfec780c935b 152
jford38 3:3ddefff03cb2 153 int n = sock->receive((char*)buttons, sizeof(buttons));
jford38 3:3ddefff03cb2 154 if(n < 0) {pc.printf("RECEIVE ERROR.\n");}
jford38 2:c358ad9aedc4 155
jford38 3:3ddefff03cb2 156 }else if(p1_p2 == PLAYER2) {
jford38 3:3ddefff03cb2 157 int n = sock->receive((char*)buffer, sizeof(buffer));
jford38 2:c358ad9aedc4 158 if(n < 0) {pc.printf("RECEIVE ERROR.\n");}
jford38 1:0589ea36661b 159 buffer[n] = '\0';
jford38 5:cfec780c935b 160 buffer_size = n/sizeof(buffer[0]) - 1;
jford38 5:cfec780c935b 161
jford38 3:3ddefff03cb2 162 sock->send_all((char*)buttons, sizeof(buttons));
jford38 0:899c85cd266f 163 }
jford38 5:cfec780c935b 164
jford38 5:cfec780c935b 165 //pc.printf("Buffer: ");
jford38 5:cfec780c935b 166 /*for(int i=0; i<buffer_size; i++) {
jford38 2:c358ad9aedc4 167 pc.printf("%d ", buffer[i]);
jford38 2:c358ad9aedc4 168 i++;
jford38 2:c358ad9aedc4 169 }
jford38 2:c358ad9aedc4 170 pc.printf("\n");
jford38 5:cfec780c935b 171 */
jford38 0:899c85cd266f 172 int idx = 0;
jford38 5:cfec780c935b 173 while(idx < buffer_size) {
jford38 0:899c85cd266f 174 char cmd = buffer[idx];
jford38 0:899c85cd266f 175 idx++;
jford38 0:899c85cd266f 176
jford38 0:899c85cd266f 177 switch(cmd) {
jford38 5:cfec780c935b 178 case CLS_CMD:
jford38 5:cfec780c935b 179 LCD->cls();
jford38 5:cfec780c935b 180 //pc.printf("Clear the screen!\n");
jford38 5:cfec780c935b 181 break;
jford38 0:899c85cd266f 182 case BG_COLOR_CMD:
jford38 5:cfec780c935b 183 LCD->background_color(buffer[idx+1]);
jford38 5:cfec780c935b 184 //pc.printf("Change the background to 0x%X\n", buffer[idx]);
jford38 0:899c85cd266f 185 idx += 1;
jford38 0:899c85cd266f 186 break;
jford38 0:899c85cd266f 187 case LINE_CMD:
jford38 5:cfec780c935b 188 //pc.printf("LINE: (%d, %d) - (%d, %d) COLOR: 0x%X\n", buffer[idx], buffer[idx+1], buffer[idx+2], buffer[idx+3], buffer[idx+4]);
jford38 5:cfec780c935b 189 LCD->line(buffer[idx], buffer[idx+1], buffer[idx+2], buffer[idx+3], buffer[idx+4]);
jford38 3:3ddefff03cb2 190 idx += 5;
jford38 0:899c85cd266f 191 break;
jford38 5:cfec780c935b 192 case CIRCLE_CMD:
jford38 5:cfec780c935b 193 //pc.printf("CIRCLE: (%d, %d), r=%d\n", buffer[idx], buffer[idx+1], buffer[idx+2]);
jford38 5:cfec780c935b 194 LCD->circle(buffer[idx], buffer[idx+1], buffer[idx+2], buffer[idx+3]);
jford38 5:cfec780c935b 195 idx += 4;
jford38 5:cfec780c935b 196 break;
jford38 5:cfec780c935b 197 case FILLED_CIRCLE_CMD:
jford38 5:cfec780c935b 198 //pc.printf("CIRCLE: (%d, %d), r=%d\n", buffer[idx], buffer[idx+1], buffer[idx+2]);
jford38 5:cfec780c935b 199 LCD->filled_circle(buffer[idx], buffer[idx+1], buffer[idx+2], buffer[idx+3]);
jford38 5:cfec780c935b 200 idx += 4;
jford38 0:899c85cd266f 201 break;
jford38 5:cfec780c935b 202 case TRI_CMD:
jford38 5:cfec780c935b 203 //pc.printf("CIRCLE: (%d, %d), r=%d\n", buffer[idx], buffer[idx+1], buffer[idx+2]);
jford38 5:cfec780c935b 204 LCD->triangle(buffer[idx], buffer[idx+1], buffer[idx+2], buffer[idx+3], buffer[idx+4], buffer[idx+5], buffer[idx+6]);
jford38 5:cfec780c935b 205 idx += 7;
jford38 5:cfec780c935b 206 break;
jford38 5:cfec780c935b 207 case RECT_CMD:
jford38 5:cfec780c935b 208 LCD->rectangle(buffer[idx], buffer[idx+1], buffer[idx+2], buffer[idx+3], buffer[idx+4]);
jford38 5:cfec780c935b 209 idx += 5;
jford38 5:cfec780c935b 210 break;
jford38 5:cfec780c935b 211 case FILLED_RECT_CMD:
jford38 5:cfec780c935b 212 LCD->filled_rectangle(buffer[idx], buffer[idx+1], buffer[idx+2], buffer[idx+3], buffer[idx+4]);
jford38 5:cfec780c935b 213 idx += 5;
jford38 5:cfec780c935b 214 break;
jford38 5:cfec780c935b 215 case PIX_CMD:
jford38 5:cfec780c935b 216 LCD->pixel(buffer[idx], buffer[idx+1], buffer[idx+2]);
jford38 5:cfec780c935b 217 idx += 3;
jford38 2:c358ad9aedc4 218 break;
jford38 0:899c85cd266f 219 default:
jford38 0:899c85cd266f 220 pc.printf("UNKNOWN CMD %d: This could get ugly!\n", cmd);
jford38 0:899c85cd266f 221 idx += 0;
jford38 0:899c85cd266f 222 }
jford38 0:899c85cd266f 223 }
jford38 0:899c85cd266f 224 }
jford38 0:899c85cd266f 225
jford38 0:899c85cd266f 226 ~uLCD_2P() {
jford38 0:899c85cd266f 227 sock->close();
jford38 0:899c85cd266f 228 eth->disconnect();
jford38 0:899c85cd266f 229 delete sock;
jford38 3:3ddefff03cb2 230 delete server;
jford38 0:899c85cd266f 231 delete eth;
jford38 0:899c85cd266f 232 }
jford38 0:899c85cd266f 233
jford38 5:cfec780c935b 234 //private:
jford38 0:899c85cd266f 235 bool master_slave;
jford38 0:899c85cd266f 236
jford38 5:cfec780c935b 237 int buffer[1024];
jford38 0:899c85cd266f 238 int buffer_idx;
jford38 0:899c85cd266f 239
jford38 3:3ddefff03cb2 240 TCPSocketServer* server;
jford38 3:3ddefff03cb2 241 TCPSocketConnection* sock;
jford38 0:899c85cd266f 242 EthernetInterface* eth;
jford38 0:899c85cd266f 243
jford38 0:899c85cd266f 244 uLCD_4DGL* LCD;
jford38 0:899c85cd266f 245
jford38 1:0589ea36661b 246 int buttons[5];
jford38 0:899c85cd266f 247 };
jford38 0:899c85cd266f 248
jford38 3:3ddefff03cb2 249 #endif