multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
Diff: main.cpp
- Revision:
- 14:6b20a930e1cb
- Parent:
- 13:95d44f7855ca
- Child:
- 15:9d90f68e53da
--- a/main.cpp Tue Nov 10 05:11:41 2020 +0000 +++ b/main.cpp Thu Nov 12 04:44:05 2020 +0000 @@ -3,7 +3,7 @@ #include "network.h" #include "DebouncedInterrupt.h" -#define DEBOUNCE 100 +#define DEBOUNCE 50 DebouncedInterrupt leftButton(p21); DebouncedInterrupt middleButton(p22); @@ -14,24 +14,22 @@ Endpoint nist; volatile int count = 0; +volatile int sendFlag = 0; +volatile int moveData = 0; // interrupt service routines void pressLeft( void ) { - count++; - char json[] = "{\"type\": \"move\", \"dir\": \"left\"}"; - sock.sendTo(nist, json, sizeof(json)); + sendFlag = 1; + moveData--; } void pressMiddle() { - count++; - char json[] = "{\"type\": \"move\", \"dir\": \"middle\"}"; - // sock.sendTo(nist, json, sizeof(json) - 1); +// count++; } void pressRight() { - count++; - // char json[] = "{\"type\": \"move\", \"dir\": \"right\"}"; - // sock.sendTo(nist, json, sizeof(json) - 1); + sendFlag = 1; + moveData++; } int main() { @@ -42,10 +40,20 @@ middleButton.attach(&pressMiddle, IRQ_RISE, DEBOUNCE); rightButton.attach(&pressRight, IRQ_RISE, DEBOUNCE); + char toSend[] = "{\"type\": \"connected\", \"data\": \"Ay whats good\"}"; + char readTo[256]; + while (1) { - printf("count: %i\r\n", count); - wait(.5); + 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)); + printf("Reading: %i bytes, data: %s\n\r", bytesRead, readTo); + wait(.1); } - // cleanupEthernet(ð, &sock); + cleanupEthernet(ð, &sock); }