multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
main.cpp@18:32fce82690a1, 2020-11-15 (annotated)
- Committer:
- vsoltan
- Date:
- Sun Nov 15 21:55:35 2020 +0000
- Revision:
- 18:32fce82690a1
- Parent:
- 17:32ae1f106002
- Child:
- 19:58cc5465f647
added different types of server response and handling for each case
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" |
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 | 17:32ae1f106002 | 42 | Graphics *gfx = new Graphics(); |
vsoltan | 17:32ae1f106002 | 43 | initEthernet(ð, &sock, &nist); |
emilmont | 7:dedf5dde9798 | 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 | 15:9d90f68e53da | 51 | |
vsoltan | 18:32fce82690a1 | 52 | char connectionRequest[] = "{\"type\": \"connected\"}"; |
vsoltan | 18:32fce82690a1 | 53 | char toSend[] = "{\"type\": \"move\", \"delta\": 5}"; |
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 | 17:32ae1f106002 | 71 | printf("Move value: %i\n\r", moveData); |
vsoltan | 17:32ae1f106002 | 72 | sendFlag = 0; |
vsoltan | 17:32ae1f106002 | 73 | moveData = 0; |
vsoltan | 17:32ae1f106002 | 74 | sock.sendTo(nist, toSend, sizeof(toSend) - 1); |
vsoltan | 17:32ae1f106002 | 75 | } |
vsoltan | 17:32ae1f106002 | 76 | int bytesRead = sock.receiveFrom(nist, readTo, sizeof(readTo)); |
vsoltan | 17:32ae1f106002 | 77 | readTo[bytesRead] = 0; |
vsoltan | 17:32ae1f106002 | 78 | if (bytesRead > 0) { |
vsoltan | 17:32ae1f106002 | 79 | printf("Reading: %i bytes, data: %s\n\r", bytesRead, readTo); |
vsoltan | 17:32ae1f106002 | 80 | parse(serverResponse, readTo); |
vsoltan | 18:32fce82690a1 | 81 | |
vsoltan | 18:32fce82690a1 | 82 | printf("type is: %s\n\r", serverResponse["type"].get<std::string>().c_str()); |
vsoltan | 18:32fce82690a1 | 83 | |
vsoltan | 18:32fce82690a1 | 84 | // TODO: check for connection ping back |
vsoltan | 18:32fce82690a1 | 85 | gs->update(&serverResponse, gfx); |
vsoltan | 17:32ae1f106002 | 86 | } |
vsoltan | 17:32ae1f106002 | 87 | wait(.1); |
vsoltan | 15:9d90f68e53da | 88 | } |
vsoltan | 17:32ae1f106002 | 89 | gfx->renderLaunchScreen(); |
vsoltan | 17:32ae1f106002 | 90 | // reset the game |
vsoltan | 17:32ae1f106002 | 91 | menuPress = 0; |
vsoltan | 14:6b20a930e1cb | 92 | } |
vsoltan | 17:32ae1f106002 | 93 | wait(0.3); |
denizguler | 13:95d44f7855ca | 94 | } |
vsoltan | 15:9d90f68e53da | 95 | // cleanupEthernet(ð, &sock); |
donatien | 1:8e1d4987eb90 | 96 | } |