multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

Committer:
vsoltan
Date:
Sat Nov 28 02:48:31 2020 +0000
Revision:
30:59e9a5409e65
Parent:
29:4708bfb863cb
Child:
31:f70cf03c8ef9
potential fix to replay-ability issue

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsoltan 18:32fce82690a1 1
donatien 1:8e1d4987eb90 2 #include "mbed.h"
vsoltan 11:d0a105f6743f 3 #include "network.h"
vsoltan 15:9d90f68e53da 4 #include "gamestate.h"
vsoltan 16:7fd48cda0773 5 #include "graphics.h"
denizguler 13:95d44f7855ca 6 #include "DebouncedInterrupt.h"
vsoltan 12:91affff3be75 7
vsoltan 26:ebadab157abe 8 #define DEBOUNCE 25
vsoltan 12:91affff3be75 9
denizguler 13:95d44f7855ca 10 DebouncedInterrupt leftButton(p21);
denizguler 13:95d44f7855ca 11 DebouncedInterrupt middleButton(p22);
denizguler 13:95d44f7855ca 12 DebouncedInterrupt rightButton(p23);
vsoltan 12:91affff3be75 13
vsoltan 12:91affff3be75 14 EthernetInterface eth;
vsoltan 12:91affff3be75 15 UDPSocket sock;
vsoltan 12:91affff3be75 16 Endpoint nist;
vsoltan 12:91affff3be75 17
vsoltan 14:6b20a930e1cb 18 volatile int sendFlag = 0;
vsoltan 14:6b20a930e1cb 19 volatile int moveData = 0;
vsoltan 17:32ae1f106002 20 volatile int menuPress = 0;
denizguler 13:95d44f7855ca 21
vsoltan 17:32ae1f106002 22 // interrupts service routines
vsoltan 17:32ae1f106002 23
vsoltan 17:32ae1f106002 24 void pressButtonMenu(void) {
vsoltan 17:32ae1f106002 25 menuPress = 1;
vsoltan 17:32ae1f106002 26 }
vsoltan 17:32ae1f106002 27
vsoltan 17:32ae1f106002 28 void pressLeftGame( void ) {
vsoltan 14:6b20a930e1cb 29 sendFlag = 1;
vsoltan 26:ebadab157abe 30 moveData -= 5;
vsoltan 12:91affff3be75 31 }
vsoltan 12:91affff3be75 32
vsoltan 17:32ae1f106002 33 void pressRightGame() {
vsoltan 14:6b20a930e1cb 34 sendFlag = 1;
vsoltan 26:ebadab157abe 35 moveData += 5;
vsoltan 12:91affff3be75 36 }
emilmont 6:25aad2d88749 37
emilmont 6:25aad2d88749 38 int main() {
vsoltan 19:58cc5465f647 39 initEthernet(&eth, &sock, &nist);
vsoltan 17:32ae1f106002 40 Graphics *gfx = new Graphics();
vsoltan 19:58cc5465f647 41
vsoltan 17:32ae1f106002 42 // attach ISR
vsoltan 17:32ae1f106002 43 leftButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE);
vsoltan 17:32ae1f106002 44 middleButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE);
vsoltan 17:32ae1f106002 45 rightButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE);
vsoltan 12:91affff3be75 46
vsoltan 17:32ae1f106002 47 MbedJSONValue serverResponse;
vsoltan 24:05eb0b0ab554 48 MbedJSONValue serverRequest;
vsoltan 15:9d90f68e53da 49
vsoltan 18:32fce82690a1 50 char connectionRequest[] = "{\"type\": \"connected\"}";
vsoltan 29:4708bfb863cb 51 char tmp_buffer[256];
vsoltan 29:4708bfb863cb 52
vsoltan 29:4708bfb863cb 53 string serverRequestContent = "";
vsoltan 29:4708bfb863cb 54 int requestLength = 0;
vsoltan 29:4708bfb863cb 55 char *serverRequestPayload = NULL;
vsoltan 14:6b20a930e1cb 56
vsoltan 17:32ae1f106002 57 gfx->renderLaunchScreen();
vsoltan 15:9d90f68e53da 58
vsoltan 17:32ae1f106002 59 while (1) { // keep program running
vsoltan 17:32ae1f106002 60 if (menuPress) {
vsoltan 17:32ae1f106002 61 GameState *gs = new GameState();
vsoltan 17:32ae1f106002 62
vsoltan 30:59e9a5409e65 63 printf("isDone? %i\n\r", gs->done());
vsoltan 30:59e9a5409e65 64
vsoltan 18:32fce82690a1 65 // request an open lobby from the server
vsoltan 29:4708bfb863cb 66 sock.sendTo(nist, connectionRequest, strlen(connectionRequest));
vsoltan 18:32fce82690a1 67
vsoltan 17:32ae1f106002 68 // change ISRs to game controls
vsoltan 17:32ae1f106002 69 leftButton.attach(&pressLeftGame, IRQ_RISE, DEBOUNCE);
vsoltan 27:fcc5fee18a24 70 middleButton.reset();
vsoltan 17:32ae1f106002 71 rightButton.attach(&pressRightGame, IRQ_RISE, DEBOUNCE);
vsoltan 17:32ae1f106002 72
vsoltan 30:59e9a5409e65 73 printf("isDone? %i\n\r", gs->done());
vsoltan 30:59e9a5409e65 74 while (!gs->isConnected() || !gs->done()) {
vsoltan 17:32ae1f106002 75 if (sendFlag != 0) {
vsoltan 24:05eb0b0ab554 76 serverRequest["type"] = "move";
vsoltan 24:05eb0b0ab554 77 serverRequest["hash"] = "jaredyeagersflipflop";
vsoltan 24:05eb0b0ab554 78 serverRequest["player"] = gs->getLocalPlayerNum();
vsoltan 24:05eb0b0ab554 79 serverRequest["delta"] = (int)moveData;
vsoltan 24:05eb0b0ab554 80
vsoltan 29:4708bfb863cb 81 serverRequestContent = serverRequest.serialize();
vsoltan 29:4708bfb863cb 82 requestLength = serverRequestContent.size();
vsoltan 29:4708bfb863cb 83 serverRequestPayload = (char *)serverRequestContent.c_str();
vsoltan 17:32ae1f106002 84 sendFlag = 0;
vsoltan 17:32ae1f106002 85 moveData = 0;
vsoltan 29:4708bfb863cb 86 sock.sendTo(nist, serverRequestPayload, requestLength);
vsoltan 17:32ae1f106002 87 }
vsoltan 29:4708bfb863cb 88 int bytesRead = sock.receiveFrom(nist, tmp_buffer, sizeof(tmp_buffer));
vsoltan 30:59e9a5409e65 89 printf("read: %s\n\r", tmp_buffer);
vsoltan 29:4708bfb863cb 90 tmp_buffer[bytesRead] = 0;
vsoltan 17:32ae1f106002 91 if (bytesRead > 0) {
vsoltan 29:4708bfb863cb 92 parse(serverResponse, tmp_buffer);
vsoltan 27:fcc5fee18a24 93 gs->updateAndRender(&serverResponse, gfx);
vsoltan 17:32ae1f106002 94 }
vsoltan 17:32ae1f106002 95 wait(.1);
vsoltan 15:9d90f68e53da 96 }
vsoltan 29:4708bfb863cb 97 MbedJSONValue disconnectRequest;
vsoltan 29:4708bfb863cb 98 disconnectRequest["hash"] = "jaredyeagersflipflop";
vsoltan 29:4708bfb863cb 99 disconnectRequest["type"] = "disconnect";
vsoltan 27:fcc5fee18a24 100
vsoltan 29:4708bfb863cb 101 string disconnectRequestContent = disconnectRequest.serialize();
vsoltan 29:4708bfb863cb 102 int len = disconnectRequestContent.size();
vsoltan 29:4708bfb863cb 103 char *disconnectRequestPayload = (char *)disconnectRequestContent.c_str();
vsoltan 27:fcc5fee18a24 104
vsoltan 29:4708bfb863cb 105 sock.sendTo(nist, disconnectRequestPayload, len);
vsoltan 29:4708bfb863cb 106 gfx->renderGameOver(gs);
vsoltan 29:4708bfb863cb 107
vsoltan 30:59e9a5409e65 108 cleanupEthernet(&eth, &sock);
vsoltan 30:59e9a5409e65 109 initEthernet(&eth, &sock, &nist);
vsoltan 30:59e9a5409e65 110
vsoltan 29:4708bfb863cb 111 // reset the game
vsoltan 27:fcc5fee18a24 112 leftButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE);
vsoltan 27:fcc5fee18a24 113 middleButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE);
vsoltan 27:fcc5fee18a24 114 rightButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE);
vsoltan 27:fcc5fee18a24 115
vsoltan 17:32ae1f106002 116 menuPress = 0;
vsoltan 28:a26a43cdaea8 117 delete gs;
vsoltan 14:6b20a930e1cb 118 }
vsoltan 17:32ae1f106002 119 wait(0.3);
denizguler 13:95d44f7855ca 120 }
vsoltan 15:9d90f68e53da 121 // cleanupEthernet(&eth, &sock);
donatien 1:8e1d4987eb90 122 }