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