Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: 4DGL-uLCD-SE EthernetInterface Game_Synchronizer MMA8452 SDFileSystem mbed-rtos mbed wave_player
Fork of 2035_Tanks_Shell by
Revision 1:0589ea36661b, committed 2015-10-07
- Comitter:
- jford38
- Date:
- Wed Oct 07 00:43:25 2015 +0000
- Parent:
- 0:899c85cd266f
- Child:
- 2:c358ad9aedc4
- Commit message:
- 2-Player game via Ethernet. First try.
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
| two_player.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/main.cpp Tue Oct 06 01:54:18 2015 +0000
+++ b/main.cpp Wed Oct 07 00:43:25 2015 +0000
@@ -9,19 +9,22 @@
int main (void) {
-
+ int* buttons;
+
+ pc.printf("\033[2J\033[0;0H");
pc.printf("I'm alive! Player 1\n");
uLCD.init();
pc.printf("Initialized...\n");
while(1) {
- uLCD.set_button_state(1,0,1,0,1);
uLCD.background_color(BLUE);
uLCD.cls();
uLCD.update();
pc.printf("Updated. \n");
- wait(1);
- pc.printf("\033[2J\033[0;0H");
+ buttons = uLCD.get_button_state();
+ pc.printf("Button State: %x %x %x %x %x\n", buttons[0], buttons[1], buttons[2], buttons[3], buttons[4]);
+ wait(3);
+ //pc.printf("\033[2J\033[0;0H");
}
//
//uLCD.line(0,0,200,200,BLACK);
--- a/two_player.h Tue Oct 06 01:54:18 2015 +0000
+++ b/two_player.h Wed Oct 07 00:43:25 2015 +0000
@@ -28,7 +28,6 @@
BG_COLOR_CMD,
CIRCLE_CMD,
CLS_CMD,
- BUTTONS_CMD
};
@@ -40,6 +39,8 @@
void init() {
buffer_idx = 0;
+ memset(buttons, 0, sizeof(buttons));
+
sock = new UDPSocket();
endpoint = new Endpoint();
eth = new EthernetInterface();
@@ -50,15 +51,11 @@
eth->connect();
sock->init();
endpoint->set_address(PLAYER2_IP, SERVER_PORT);
- //sock.sendTo(endpoint, buffer, sizeof(buffer));
- //int n = sock.receiveFrom(echo_server, in_buffer, sizeof(in_buffer));
break;
case PLAYER2: // Server
eth->init(PLAYER2_IP, "255.255.255.0", "0.0.0.0");
eth->connect();
sock->bind(SERVER_PORT);
- //sock.sendTo(endpoint, buffer, sizeof(buffer));
- //int n = sock.receiveFrom(endpoint, buffer, sizeof(buffer));
break;
}
}
@@ -84,19 +81,31 @@
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) { draw(BUTTONS_CMD, a,b,c,d,e,5); }
+ 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) {
sock->sendTo(*endpoint, (char*)buffer, (buffer_idx+1)*sizeof(buffer[0]));
buffer_idx = 0;
+
+ int n = sock->receiveFrom(*endpoint, (char*)buttons, sizeof(buttons));
}else if(p1_p2 == PLAYER2) {
- int n = sock->receiveFrom(*endpoint, (char*)buffer, sizeof(buffer)/sizeof(buffer[0]));
- buffer[n] = '\0';
+
+ int n = sock->receiveFrom(*endpoint, (char*)buffer, sizeof(buffer));
+ buffer[n] = '\0';
+
+ sock->sendTo(*endpoint, (char*)buttons, sizeof(buttons));
}
int idx = 0;
@@ -117,16 +126,6 @@
pc.printf("Clear the screen!\n");
idx += 0;
break;
- case BUTTONS_CMD:
- A_button = buffer[idx+0];
- B_button = buffer[idx+1];
- C_button = buffer[idx+2];
- D_button = buffer[idx+3];
- E_button = buffer[idx+4];
- pc.printf("Button Values: %d %d %d %d %d\n",
- A_button, B_button, C_button, D_button, E_button);
- idx += 5;
- break;
default:
pc.printf("UNKNOWN CMD %d: This could get ugly!\n", cmd);
idx += 0;
@@ -155,11 +154,7 @@
uLCD_4DGL* LCD;
- int A_button;
- int B_button;
- int C_button;
- int D_button;
- int E_button;
+ int buttons[5];
};
#endif
\ No newline at end of file
