multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
main.cpp
- Committer:
- vsoltan
- Date:
- 2020-11-14
- Revision:
- 17:32ae1f106002
- Parent:
- 16:7fd48cda0773
- Child:
- 18:32fce82690a1
File content as of revision 17:32ae1f106002:
#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() { Graphics *gfx = new Graphics(); initEthernet(ð, &sock, &nist); // attach ISR leftButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); middleButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); rightButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); MbedJSONValue serverResponse; char toSend[] = "{\"type\": \"connected\", \"data\": \"Ay whats good\"}"; char readTo[256]; gfx->renderLaunchScreen(); while (1) { // keep program running if (menuPress) { GameState *gs = new GameState(); gfx->renderGameState(gs); // 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); gs->update(&serverResponse); } wait(.1); } gfx->renderLaunchScreen(); // reset the game menuPress = 0; } wait(0.3); } // cleanupEthernet(ð, &sock); }