multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

Committer:
vsoltan
Date:
Mon Nov 16 03:34:34 2020 +0000
Revision:
24:05eb0b0ab554
Parent:
23:c38680c32552
Child:
26:ebadab157abe
can send move input and fixed graphics bugs;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vsoltan 18:32fce82690a1 1
donatien 1:8e1d4987eb90 2 #include "mbed.h"
donatien 1:8e1d4987eb90 3 #include "EthernetInterface.h"
vsoltan 11:d0a105f6743f 4 #include "network.h"
vsoltan 15:9d90f68e53da 5 #include "gamestate.h"
vsoltan 16:7fd48cda0773 6 #include "graphics.h"
denizguler 13:95d44f7855ca 7 #include "DebouncedInterrupt.h"
vsoltan 18:32fce82690a1 8 #include "MbedJSONValue.h"
vsoltan 12:91affff3be75 9
vsoltan 14:6b20a930e1cb 10 #define DEBOUNCE 50
vsoltan 12:91affff3be75 11
denizguler 13:95d44f7855ca 12 DebouncedInterrupt leftButton(p21);
denizguler 13:95d44f7855ca 13 DebouncedInterrupt middleButton(p22);
denizguler 13:95d44f7855ca 14 DebouncedInterrupt rightButton(p23);
vsoltan 12:91affff3be75 15
vsoltan 12:91affff3be75 16 EthernetInterface eth;
vsoltan 12:91affff3be75 17 UDPSocket sock;
vsoltan 12:91affff3be75 18 Endpoint nist;
vsoltan 12:91affff3be75 19
denizguler 13:95d44f7855ca 20 volatile int count = 0;
vsoltan 14:6b20a930e1cb 21 volatile int sendFlag = 0;
vsoltan 14:6b20a930e1cb 22 volatile int moveData = 0;
vsoltan 17:32ae1f106002 23 volatile int menuPress = 0;
denizguler 13:95d44f7855ca 24
vsoltan 17:32ae1f106002 25 // interrupts service routines
vsoltan 17:32ae1f106002 26
vsoltan 17:32ae1f106002 27 void pressButtonMenu(void) {
vsoltan 17:32ae1f106002 28 menuPress = 1;
vsoltan 17:32ae1f106002 29 }
vsoltan 17:32ae1f106002 30
vsoltan 17:32ae1f106002 31 void pressLeftGame( void ) {
vsoltan 14:6b20a930e1cb 32 sendFlag = 1;
vsoltan 14:6b20a930e1cb 33 moveData--;
vsoltan 12:91affff3be75 34 }
vsoltan 12:91affff3be75 35
vsoltan 17:32ae1f106002 36 void pressRightGame() {
vsoltan 14:6b20a930e1cb 37 sendFlag = 1;
vsoltan 14:6b20a930e1cb 38 moveData++;
vsoltan 12:91affff3be75 39 }
emilmont 6:25aad2d88749 40
emilmont 6:25aad2d88749 41 int main() {
vsoltan 19:58cc5465f647 42 initEthernet(&eth, &sock, &nist);
vsoltan 17:32ae1f106002 43 Graphics *gfx = new Graphics();
vsoltan 19:58cc5465f647 44
vsoltan 17:32ae1f106002 45 // attach ISR
vsoltan 17:32ae1f106002 46 leftButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE);
vsoltan 17:32ae1f106002 47 middleButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE);
vsoltan 17:32ae1f106002 48 rightButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE);
vsoltan 12:91affff3be75 49
vsoltan 17:32ae1f106002 50 MbedJSONValue serverResponse;
vsoltan 24:05eb0b0ab554 51 MbedJSONValue serverRequest;
vsoltan 15:9d90f68e53da 52
vsoltan 18:32fce82690a1 53 char connectionRequest[] = "{\"type\": \"connected\"}";
vsoltan 14:6b20a930e1cb 54 char readTo[256];
vsoltan 14:6b20a930e1cb 55
vsoltan 17:32ae1f106002 56 gfx->renderLaunchScreen();
vsoltan 15:9d90f68e53da 57
vsoltan 17:32ae1f106002 58 while (1) { // keep program running
vsoltan 17:32ae1f106002 59 if (menuPress) {
vsoltan 17:32ae1f106002 60 GameState *gs = new GameState();
vsoltan 17:32ae1f106002 61
vsoltan 18:32fce82690a1 62 // request an open lobby from the server
vsoltan 18:32fce82690a1 63 sock.sendTo(nist, connectionRequest, sizeof(connectionRequest) - 1);
vsoltan 18:32fce82690a1 64
vsoltan 17:32ae1f106002 65 // change ISRs to game controls
vsoltan 17:32ae1f106002 66 leftButton.attach(&pressLeftGame, IRQ_RISE, DEBOUNCE);
vsoltan 17:32ae1f106002 67 rightButton.attach(&pressRightGame, IRQ_RISE, DEBOUNCE);
vsoltan 17:32ae1f106002 68
vsoltan 17:32ae1f106002 69 while (!gs->done()) {
vsoltan 17:32ae1f106002 70 if (sendFlag != 0) {
vsoltan 24:05eb0b0ab554 71 serverRequest["type"] = "move";
vsoltan 24:05eb0b0ab554 72 serverRequest["hash"] = "jaredyeagersflipflop";
vsoltan 24:05eb0b0ab554 73 serverRequest["player"] = gs->getLocalPlayerNum();
vsoltan 24:05eb0b0ab554 74 serverRequest["delta"] = (int)moveData;
vsoltan 24:05eb0b0ab554 75
vsoltan 24:05eb0b0ab554 76 string requestContent = serverRequest.serialize();
vsoltan 24:05eb0b0ab554 77 int len = requestContent.size();
vsoltan 24:05eb0b0ab554 78 char *toSend = (char *)requestContent.c_str();
vsoltan 17:32ae1f106002 79 sendFlag = 0;
vsoltan 17:32ae1f106002 80 moveData = 0;
vsoltan 24:05eb0b0ab554 81 sock.sendTo(nist, toSend, len);
vsoltan 17:32ae1f106002 82 }
vsoltan 17:32ae1f106002 83 int bytesRead = sock.receiveFrom(nist, readTo, sizeof(readTo));
vsoltan 17:32ae1f106002 84 readTo[bytesRead] = 0;
vsoltan 17:32ae1f106002 85 if (bytesRead > 0) {
vsoltan 17:32ae1f106002 86 parse(serverResponse, readTo);
vsoltan 18:32fce82690a1 87 gs->update(&serverResponse, gfx);
vsoltan 17:32ae1f106002 88 }
vsoltan 17:32ae1f106002 89 wait(.1);
vsoltan 15:9d90f68e53da 90 }
vsoltan 17:32ae1f106002 91 gfx->renderLaunchScreen();
vsoltan 17:32ae1f106002 92 // reset the game
vsoltan 17:32ae1f106002 93 menuPress = 0;
vsoltan 14:6b20a930e1cb 94 }
vsoltan 17:32ae1f106002 95 wait(0.3);
denizguler 13:95d44f7855ca 96 }
vsoltan 15:9d90f68e53da 97 // cleanupEthernet(&eth, &sock);
donatien 1:8e1d4987eb90 98 }