multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
Revision 17:32ae1f106002, committed 2020-11-14
- Comitter:
- vsoltan
- Date:
- Sat Nov 14 02:37:13 2020 +0000
- Parent:
- 16:7fd48cda0773
- Child:
- 18:32fce82690a1
- Commit message:
- added menu to enter game, JSON de-serialization, and game state update.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MbedJSONValue.lib Sat Nov 14 02:37:13 2020 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/samux/code/MbedJSONValue/#10a99cdf7846
--- a/gamestate.cpp Fri Nov 13 23:02:56 2020 +0000 +++ b/gamestate.cpp Sat Nov 14 02:37:13 2020 +0000 @@ -1,9 +1,11 @@ + #include "gamestate.h" GameState::GameState() { Coord playerOneLocation = {0, 0}; Coord playerTwoLocation = {0, 0}; Coord ballLocation = {0, 0}; + this->localPlayer = 0; this->p1_loc = playerOneLocation; this->p2_loc = playerTwoLocation; this->ball_loc = ballLocation; @@ -22,8 +24,20 @@ return this->ball_loc; } -void GameState::update(char *server_data) { - +void GameState::update(MbedJSONValue *serverResponse) { + this->p1_loc.y = (*serverResponse)["playerOnePos"].get<int>(); + this->p2_loc.y = (*serverResponse)["playerTwoPos"].get<int>(); + int updated_ball_x = (*serverResponse)["ballPos"][0].get<int>(); + int updated_ball_y = (*serverResponse)["ballPos"][1].get<int>(); + this->ball_loc.x = updated_ball_x; + this->ball_loc.y = updated_ball_y; + this->is_done = (bool)(*serverResponse)["isOver"].get<int>(); +} + +void GameState::print() { + //char to_string[256]; +// sprintf(to_string, "Player1 Pos: [%i, %i], Player 2 Pos: [%i, %i], Ball Pos: [%i, %i], isOver: %i", this->p1_loc->x, this->p1_loc->y, this->p2_loc->x, this->p2_loc->y, this->ball_loc->x, this->ball_loc->y, this->is_done); +// printf("Game State: %s\n\r", to_string); } bool GameState::done() {
--- a/gamestate.h Fri Nov 13 23:02:56 2020 +0000 +++ b/gamestate.h Sat Nov 14 02:37:13 2020 +0000 @@ -4,6 +4,7 @@ #include "mbed.h" #include "stdint.h" +#include "MbedJSONValue.h" struct Coord { int8_t x; @@ -16,13 +17,15 @@ Coord p2_loc; Coord ball_loc; bool is_done; + bool localPlayer; public: GameState(); Coord getPlayerOneLocation(); Coord getPlayerTwoLocation(); Coord getBallLocation(); - void update(char *server_data); + void update(MbedJSONValue *serverResponse); bool done(); + void print(); }; #endif // GAME_STATE_H \ No newline at end of file
--- a/graphics.cpp Fri Nov 13 23:02:56 2020 +0000 +++ b/graphics.cpp Sat Nov 14 02:37:13 2020 +0000 @@ -2,16 +2,28 @@ #include "graphics.h" Graphics::Graphics() { + printf("\n\rInitializing Graphics: initializing TFT\n\r"); this->tft = new Adafruit_ST7735(P_MOSI, P_MISO, P_SOCK, P_CS, P_RS, P_DC); - tft->initR(); - tft->fillScreen(ST7735_RED); + printf("Initializing Graphics: optimizing display for resolution\n\r"); + tft->initR(INITR_144GREENTAB); + tft->setTextColor(ST7735_WHITE); } - void Graphics::renderLaunchScreen() { + tft->fillScreen(ST7735_BLACK); + tft->setTextCursor(20, 20); + tft->printf("%s", "Multiplayer Pong"); + tft->setTextCursor(20, 80); + tft->printf("%s", "Press any button to continue."); +} + +void Graphics::renderBall(GameState *gs) { -} +} + +void Graphics::renderPlayers(GameState *gs) { +} void Graphics::renderGameState(GameState *gs) { - + tft->fillScreen(ST7735_RED); } \ No newline at end of file
--- a/graphics.h Fri Nov 13 23:02:56 2020 +0000 +++ b/graphics.h Sat Nov 14 02:37:13 2020 +0000 @@ -19,13 +19,8 @@ Graphics(); void renderLaunchScreen(); void renderGameState(GameState *gs); - void renderBall(); - void renderPlayers(); + void renderBall(GameState *gs); + void renderPlayers(GameState *gs); }; -void renderLaunchScreen(); -void renderGameState(GameState *gs); -void renderBall(); -void renderPlayers(); - #endif // GRAPHICS_H \ No newline at end of file
--- a/main.cpp Fri Nov 13 23:02:56 2020 +0000 +++ b/main.cpp Sat Nov 14 02:37:13 2020 +0000 @@ -4,6 +4,7 @@ #include "gamestate.h" #include "graphics.h" #include "DebouncedInterrupt.h" +#include "MbedJSONValue.h" #define DEBOUNCE 50 @@ -18,57 +19,71 @@ volatile int count = 0; volatile int sendFlag = 0; volatile int moveData = 0; +volatile int menuPress = 0; -// interrupt service routines -void pressLeft( void ) { +// interrupts service routines + +void pressButtonMenu(void) { + menuPress = 1; +} + +void pressLeftGame( void ) { sendFlag = 1; moveData--; } -void pressMiddle() { -// count++; -} - -void pressRight() { +void pressRightGame() { sendFlag = 1; moveData++; } int main() { - initEthernet(ð, &sock, &nist); + Graphics *gfx = new Graphics(); + initEthernet(ð, &sock, &nist); - // initialize GPIO - leftButton.attach(&pressLeft, IRQ_RISE, DEBOUNCE); - middleButton.attach(&pressMiddle, IRQ_RISE, DEBOUNCE); - rightButton.attach(&pressRight, IRQ_RISE, DEBOUNCE); + // attach ISR + leftButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); + middleButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); + rightButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); - GameState *gs = new GameState(); - Graphics *gfx = new Graphics(); + MbedJSONValue serverResponse; char toSend[] = "{\"type\": \"connected\", \"data\": \"Ay whats good\"}"; char readTo[256]; - while (1) { // keep program running - - /* TODO: poll for navigation within menu - * press any button to start - */ + gfx->renderLaunchScreen(); - while (!gs->done()) { - if (sendFlag != 0) { - printf("Move value: %i\n\r", moveData); - sendFlag = 0; - moveData = 0; - sock.sendTo(nist, toSend, sizeof(toSend) - 1); + 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); } - int bytesRead = sock.receiveFrom(nist, readTo, sizeof(readTo)); - if (bytesRead > 0) { - printf("Reading: %i bytes, data: %s\n\r", bytesRead, readTo); - // TODO: update game state with json string - } - wait(.1); + gfx->renderLaunchScreen(); + // reset the game + menuPress = 0; } - wait(0.5); + wait(0.3); } // cleanupEthernet(ð, &sock); }
--- a/network.cpp Fri Nov 13 23:02:56 2020 +0000 +++ b/network.cpp Sat Nov 14 02:37:13 2020 +0000 @@ -1,12 +1,29 @@ #include "network.h" void initEthernet(EthernetInterface *eth, UDPSocket *sock, Endpoint *nist) { - eth->init(); //Use DHCP - eth->connect(); - sock->init(); - nist->set_address(AWS_IP, PORT); + int rc; + rc = eth->init(); //Use DHCP + if (rc < 0) { + printf("Fatal Error: failed to initialize ethernet via DHCP\n\r"); + return; + } + rc = eth->connect(); + if (rc < 0) { + printf("Fatal Error: failed to connect\n\r"); + return; + } + rc = sock->init(); + if (rc < 0) { + printf("Fatal Error: failed to initialize socket\n\r"); + return; + } + rc = nist->set_address(AWS_IP, PORT); + if (rc < 0) { + printf("Fatal Error: failed to set address\n\r"); + return; + } sock->set_blocking(false, 0); - printf("Connected to %s, port: %i\n\r", AWS_IP, PORT); + printf("Success! Connected to %s, port: %i\n\r", AWS_IP, PORT); } void cleanupEthernet(EthernetInterface *eth, UDPSocket *sock) {