multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
main.cpp
- Committer:
- vsoltan
- Date:
- 2020-11-15
- Revision:
- 19:58cc5465f647
- Parent:
- 18:32fce82690a1
- Child:
- 22:1c49e1fae846
File content as of revision 19:58cc5465f647:
#include "mbed.h" #include "EthernetInterface.h" #include "network.h" #include "gamestate.h" #include "graphics.h" #include "DebouncedInterrupt.h" #include "MbedJSONValue.h" #define DEBOUNCE 50 DebouncedInterrupt leftButton(p21); DebouncedInterrupt middleButton(p22); DebouncedInterrupt rightButton(p23); EthernetInterface eth; UDPSocket sock; Endpoint nist; volatile int count = 0; volatile int sendFlag = 0; volatile int moveData = 0; volatile int menuPress = 0; // interrupts service routines void pressButtonMenu(void) { menuPress = 1; } void pressLeftGame( void ) { sendFlag = 1; moveData--; } void pressRightGame() { sendFlag = 1; moveData++; } int main() { initEthernet(ð, &sock, &nist); Graphics *gfx = new Graphics(); // attach ISR leftButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); middleButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); rightButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); MbedJSONValue serverResponse; char connectionRequest[] = "{\"type\": \"connected\"}"; char toSend[] = "{\"type\": \"move\", \"delta\": 5}"; char readTo[256]; gfx->renderLaunchScreen(); while (1) { // keep program running if (menuPress) { GameState *gs = new GameState(); // request an open lobby from the server sock.sendTo(nist, connectionRequest, sizeof(connectionRequest) - 1); // change ISRs to game controls leftButton.attach(&pressLeftGame, IRQ_RISE, DEBOUNCE); rightButton.attach(&pressRightGame, IRQ_RISE, DEBOUNCE); while (!gs->done()) { if (sendFlag != 0) { printf("Move value: %i\n\r", moveData); sendFlag = 0; moveData = 0; sock.sendTo(nist, toSend, sizeof(toSend) - 1); } int bytesRead = sock.receiveFrom(nist, readTo, sizeof(readTo)); readTo[bytesRead] = 0; if (bytesRead > 0) { printf("Reading: %i bytes, data: %s\n\r", bytesRead, readTo); parse(serverResponse, readTo); printf("type is: %s\n\r", serverResponse["type"].get<std::string>().c_str()); // TODO: check for connection ping back gs->update(&serverResponse, gfx); } wait(.1); } gfx->renderLaunchScreen(); // reset the game menuPress = 0; } wait(0.3); } // cleanupEthernet(ð, &sock); }