multiplayer pong game for LPC 1768

Dependencies:   mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt

Committer:
vsoltan
Date:
Sat Nov 14 02:37:13 2020 +0000
Revision:
17:32ae1f106002
Parent:
16:7fd48cda0773
Child:
18:32fce82690a1
added menu to enter game, JSON de-serialization, and game state update.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
donatien 1:8e1d4987eb90 1 #include "mbed.h"
donatien 1:8e1d4987eb90 2 #include "EthernetInterface.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 17:32ae1f106002 7 #include "MbedJSONValue.h"
vsoltan 12:91affff3be75 8
vsoltan 14:6b20a930e1cb 9 #define DEBOUNCE 50
vsoltan 12:91affff3be75 10
denizguler 13:95d44f7855ca 11 DebouncedInterrupt leftButton(p21);
denizguler 13:95d44f7855ca 12 DebouncedInterrupt middleButton(p22);
denizguler 13:95d44f7855ca 13 DebouncedInterrupt rightButton(p23);
vsoltan 12:91affff3be75 14
vsoltan 12:91affff3be75 15 EthernetInterface eth;
vsoltan 12:91affff3be75 16 UDPSocket sock;
vsoltan 12:91affff3be75 17 Endpoint nist;
vsoltan 12:91affff3be75 18
denizguler 13:95d44f7855ca 19 volatile int count = 0;
vsoltan 14:6b20a930e1cb 20 volatile int sendFlag = 0;
vsoltan 14:6b20a930e1cb 21 volatile int moveData = 0;
vsoltan 17:32ae1f106002 22 volatile int menuPress = 0;
denizguler 13:95d44f7855ca 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 14:6b20a930e1cb 32 moveData--;
vsoltan 12:91affff3be75 33 }
vsoltan 12:91affff3be75 34
vsoltan 17:32ae1f106002 35 void pressRightGame() {
vsoltan 14:6b20a930e1cb 36 sendFlag = 1;
vsoltan 14:6b20a930e1cb 37 moveData++;
vsoltan 12:91affff3be75 38 }
emilmont 6:25aad2d88749 39
emilmont 6:25aad2d88749 40 int main() {
vsoltan 17:32ae1f106002 41 Graphics *gfx = new Graphics();
vsoltan 17:32ae1f106002 42 initEthernet(&eth, &sock, &nist);
emilmont 7:dedf5dde9798 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 15:9d90f68e53da 50
vsoltan 14:6b20a930e1cb 51 char toSend[] = "{\"type\": \"connected\", \"data\": \"Ay whats good\"}";
vsoltan 14:6b20a930e1cb 52 char readTo[256];
vsoltan 14:6b20a930e1cb 53
vsoltan 17:32ae1f106002 54 gfx->renderLaunchScreen();
vsoltan 15:9d90f68e53da 55
vsoltan 17:32ae1f106002 56 while (1) { // keep program running
vsoltan 17:32ae1f106002 57 if (menuPress) {
vsoltan 17:32ae1f106002 58 GameState *gs = new GameState();
vsoltan 17:32ae1f106002 59
vsoltan 17:32ae1f106002 60 gfx->renderGameState(gs);
vsoltan 17:32ae1f106002 61
vsoltan 17:32ae1f106002 62 // change ISRs to game controls
vsoltan 17:32ae1f106002 63 leftButton.attach(&pressLeftGame, IRQ_RISE, DEBOUNCE);
vsoltan 17:32ae1f106002 64 rightButton.attach(&pressRightGame, IRQ_RISE, DEBOUNCE);
vsoltan 17:32ae1f106002 65
vsoltan 17:32ae1f106002 66 while (!gs->done()) {
vsoltan 17:32ae1f106002 67 if (sendFlag != 0) {
vsoltan 17:32ae1f106002 68 printf("Move value: %i\n\r", moveData);
vsoltan 17:32ae1f106002 69 sendFlag = 0;
vsoltan 17:32ae1f106002 70 moveData = 0;
vsoltan 17:32ae1f106002 71 sock.sendTo(nist, toSend, sizeof(toSend) - 1);
vsoltan 17:32ae1f106002 72 }
vsoltan 17:32ae1f106002 73 int bytesRead = sock.receiveFrom(nist, readTo, sizeof(readTo));
vsoltan 17:32ae1f106002 74 readTo[bytesRead] = 0;
vsoltan 17:32ae1f106002 75 if (bytesRead > 0) {
vsoltan 17:32ae1f106002 76 printf("Reading: %i bytes, data: %s\n\r", bytesRead, readTo);
vsoltan 17:32ae1f106002 77 parse(serverResponse, readTo);
vsoltan 17:32ae1f106002 78 gs->update(&serverResponse);
vsoltan 17:32ae1f106002 79 }
vsoltan 17:32ae1f106002 80 wait(.1);
vsoltan 15:9d90f68e53da 81 }
vsoltan 17:32ae1f106002 82 gfx->renderLaunchScreen();
vsoltan 17:32ae1f106002 83 // reset the game
vsoltan 17:32ae1f106002 84 menuPress = 0;
vsoltan 14:6b20a930e1cb 85 }
vsoltan 17:32ae1f106002 86 wait(0.3);
denizguler 13:95d44f7855ca 87 }
vsoltan 15:9d90f68e53da 88 // cleanupEthernet(&eth, &sock);
donatien 1:8e1d4987eb90 89 }