multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
main.cpp@35:2c5e582a6ee2, 2020-11-28 (annotated)
- Committer:
- vsoltan
- Date:
- Sat Nov 28 03:52:53 2020 +0000
- Revision:
- 35:2c5e582a6ee2
- Parent:
- 34:b19f39ad74ac
- Child:
- 36:46bb54b669bc
more issues
Who changed what in which revision?
User | Revision | Line number | New 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 | 32:7cbf4d9a82af | 22 | int numGamesPlayed = 0; |
vsoltan | 32:7cbf4d9a82af | 23 | |
vsoltan | 17:32ae1f106002 | 24 | // interrupts service routines |
vsoltan | 17:32ae1f106002 | 25 | |
vsoltan | 17:32ae1f106002 | 26 | void pressButtonMenu(void) { |
vsoltan | 17:32ae1f106002 | 27 | menuPress = 1; |
vsoltan | 17:32ae1f106002 | 28 | } |
vsoltan | 17:32ae1f106002 | 29 | |
vsoltan | 17:32ae1f106002 | 30 | void pressLeftGame( void ) { |
vsoltan | 14:6b20a930e1cb | 31 | sendFlag = 1; |
vsoltan | 26:ebadab157abe | 32 | moveData -= 5; |
vsoltan | 12:91affff3be75 | 33 | } |
vsoltan | 12:91affff3be75 | 34 | |
vsoltan | 17:32ae1f106002 | 35 | void pressRightGame() { |
vsoltan | 14:6b20a930e1cb | 36 | sendFlag = 1; |
vsoltan | 26:ebadab157abe | 37 | moveData += 5; |
vsoltan | 12:91affff3be75 | 38 | } |
emilmont | 6:25aad2d88749 | 39 | |
emilmont | 6:25aad2d88749 | 40 | int main() { |
vsoltan | 19:58cc5465f647 | 41 | initEthernet(ð, &sock, &nist); |
vsoltan | 17:32ae1f106002 | 42 | Graphics *gfx = new Graphics(); |
vsoltan | 19:58cc5465f647 | 43 | |
vsoltan | 17:32ae1f106002 | 44 | // attach ISR |
vsoltan | 17:32ae1f106002 | 45 | leftButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); |
vsoltan | 17:32ae1f106002 | 46 | middleButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); |
vsoltan | 17:32ae1f106002 | 47 | rightButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); |
vsoltan | 12:91affff3be75 | 48 | |
vsoltan | 17:32ae1f106002 | 49 | MbedJSONValue serverResponse; |
vsoltan | 24:05eb0b0ab554 | 50 | MbedJSONValue serverRequest; |
vsoltan | 15:9d90f68e53da | 51 | |
vsoltan | 18:32fce82690a1 | 52 | char connectionRequest[] = "{\"type\": \"connected\"}"; |
vsoltan | 29:4708bfb863cb | 53 | char tmp_buffer[256]; |
vsoltan | 29:4708bfb863cb | 54 | |
vsoltan | 29:4708bfb863cb | 55 | string serverRequestContent = ""; |
vsoltan | 29:4708bfb863cb | 56 | int requestLength = 0; |
vsoltan | 29:4708bfb863cb | 57 | char *serverRequestPayload = NULL; |
vsoltan | 14:6b20a930e1cb | 58 | |
vsoltan | 17:32ae1f106002 | 59 | gfx->renderLaunchScreen(); |
vsoltan | 15:9d90f68e53da | 60 | |
vsoltan | 17:32ae1f106002 | 61 | while (1) { // keep program running |
vsoltan | 17:32ae1f106002 | 62 | if (menuPress) { |
vsoltan | 17:32ae1f106002 | 63 | GameState *gs = new GameState(); |
vsoltan | 32:7cbf4d9a82af | 64 | numGamesPlayed++; |
vsoltan | 17:32ae1f106002 | 65 | |
vsoltan | 17:32ae1f106002 | 66 | // change ISRs to game controls |
vsoltan | 17:32ae1f106002 | 67 | leftButton.attach(&pressLeftGame, IRQ_RISE, DEBOUNCE); |
vsoltan | 27:fcc5fee18a24 | 68 | middleButton.reset(); |
vsoltan | 17:32ae1f106002 | 69 | rightButton.attach(&pressRightGame, IRQ_RISE, DEBOUNCE); |
vsoltan | 17:32ae1f106002 | 70 | |
vsoltan | 34:b19f39ad74ac | 71 | // request an open lobby from the server |
vsoltan | 34:b19f39ad74ac | 72 | sock.sendTo(nist, connectionRequest, strlen(connectionRequest)); |
vsoltan | 34:b19f39ad74ac | 73 | |
vsoltan | 34:b19f39ad74ac | 74 | printf("hello\n\r"); |
vsoltan | 32:7cbf4d9a82af | 75 | while (!gs->done()) { |
vsoltan | 17:32ae1f106002 | 76 | if (sendFlag != 0) { |
vsoltan | 24:05eb0b0ab554 | 77 | serverRequest["type"] = "move"; |
vsoltan | 24:05eb0b0ab554 | 78 | serverRequest["hash"] = "jaredyeagersflipflop"; |
vsoltan | 24:05eb0b0ab554 | 79 | serverRequest["player"] = gs->getLocalPlayerNum(); |
vsoltan | 24:05eb0b0ab554 | 80 | serverRequest["delta"] = (int)moveData; |
vsoltan | 24:05eb0b0ab554 | 81 | |
vsoltan | 29:4708bfb863cb | 82 | serverRequestContent = serverRequest.serialize(); |
vsoltan | 29:4708bfb863cb | 83 | requestLength = serverRequestContent.size(); |
vsoltan | 29:4708bfb863cb | 84 | serverRequestPayload = (char *)serverRequestContent.c_str(); |
vsoltan | 17:32ae1f106002 | 85 | sendFlag = 0; |
vsoltan | 17:32ae1f106002 | 86 | moveData = 0; |
vsoltan | 29:4708bfb863cb | 87 | sock.sendTo(nist, serverRequestPayload, requestLength); |
vsoltan | 17:32ae1f106002 | 88 | } |
vsoltan | 29:4708bfb863cb | 89 | int bytesRead = sock.receiveFrom(nist, tmp_buffer, sizeof(tmp_buffer)); |
vsoltan | 29:4708bfb863cb | 90 | tmp_buffer[bytesRead] = 0; |
vsoltan | 32:7cbf4d9a82af | 91 | if (numGamesPlayed == 2) { |
vsoltan | 32:7cbf4d9a82af | 92 | printf("read: %s\n\r", tmp_buffer); |
vsoltan | 32:7cbf4d9a82af | 93 | } |
vsoltan | 17:32ae1f106002 | 94 | if (bytesRead > 0) { |
vsoltan | 29:4708bfb863cb | 95 | parse(serverResponse, tmp_buffer); |
vsoltan | 27:fcc5fee18a24 | 96 | gs->updateAndRender(&serverResponse, gfx); |
vsoltan | 17:32ae1f106002 | 97 | } |
vsoltan | 17:32ae1f106002 | 98 | wait(.1); |
vsoltan | 15:9d90f68e53da | 99 | } |
vsoltan | 29:4708bfb863cb | 100 | MbedJSONValue disconnectRequest; |
vsoltan | 29:4708bfb863cb | 101 | disconnectRequest["hash"] = "jaredyeagersflipflop"; |
vsoltan | 29:4708bfb863cb | 102 | disconnectRequest["type"] = "disconnect"; |
vsoltan | 27:fcc5fee18a24 | 103 | |
vsoltan | 29:4708bfb863cb | 104 | string disconnectRequestContent = disconnectRequest.serialize(); |
vsoltan | 29:4708bfb863cb | 105 | int len = disconnectRequestContent.size(); |
vsoltan | 29:4708bfb863cb | 106 | char *disconnectRequestPayload = (char *)disconnectRequestContent.c_str(); |
vsoltan | 27:fcc5fee18a24 | 107 | |
vsoltan | 32:7cbf4d9a82af | 108 | sock.sendTo(nist, disconnectRequestPayload, len); |
vsoltan | 32:7cbf4d9a82af | 109 | |
vsoltan | 32:7cbf4d9a82af | 110 | // cleanup the socket from the previous game |
vsoltan | 32:7cbf4d9a82af | 111 | int flushBytes = 0; |
vsoltan | 34:b19f39ad74ac | 112 | while ((flushBytes = sock.receiveFrom(nist, tmp_buffer, sizeof(tmp_buffer))) != 0) { |
vsoltan | 34:b19f39ad74ac | 113 | printf("reading bytes: %i\n\r", flushBytes); |
vsoltan | 35:2c5e582a6ee2 | 114 | printf("tmp_buffer %s\n\r", tmp_buffer); |
vsoltan | 34:b19f39ad74ac | 115 | } |
vsoltan | 32:7cbf4d9a82af | 116 | |
vsoltan | 29:4708bfb863cb | 117 | gfx->renderGameOver(gs); |
vsoltan | 29:4708bfb863cb | 118 | |
vsoltan | 29:4708bfb863cb | 119 | // reset the game |
vsoltan | 27:fcc5fee18a24 | 120 | leftButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); |
vsoltan | 27:fcc5fee18a24 | 121 | middleButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); |
vsoltan | 27:fcc5fee18a24 | 122 | rightButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); |
vsoltan | 27:fcc5fee18a24 | 123 | |
vsoltan | 17:32ae1f106002 | 124 | menuPress = 0; |
vsoltan | 28:a26a43cdaea8 | 125 | delete gs; |
vsoltan | 14:6b20a930e1cb | 126 | } |
vsoltan | 17:32ae1f106002 | 127 | wait(0.3); |
denizguler | 13:95d44f7855ca | 128 | } |
vsoltan | 15:9d90f68e53da | 129 | // cleanupEthernet(ð, &sock); |
donatien | 1:8e1d4987eb90 | 130 | } |