multiplayer pong game for LPC 1768
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
main.cpp@16:7fd48cda0773, 2020-11-13 (annotated)
- Committer:
- vsoltan
- Date:
- Fri Nov 13 23:02:56 2020 +0000
- Revision:
- 16:7fd48cda0773
- Parent:
- 15:9d90f68e53da
- Child:
- 17:32ae1f106002
finished display wiring and added graphics class
Who changed what in which revision?
User | Revision | Line number | New 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 | 12:91affff3be75 | 7 | |
vsoltan | 14:6b20a930e1cb | 8 | #define DEBOUNCE 50 |
vsoltan | 12:91affff3be75 | 9 | |
denizguler | 13:95d44f7855ca | 10 | DebouncedInterrupt leftButton(p21); |
denizguler | 13:95d44f7855ca | 11 | DebouncedInterrupt middleButton(p22); |
denizguler | 13:95d44f7855ca | 12 | DebouncedInterrupt rightButton(p23); |
vsoltan | 12:91affff3be75 | 13 | |
vsoltan | 12:91affff3be75 | 14 | EthernetInterface eth; |
vsoltan | 12:91affff3be75 | 15 | UDPSocket sock; |
vsoltan | 12:91affff3be75 | 16 | Endpoint nist; |
vsoltan | 12:91affff3be75 | 17 | |
denizguler | 13:95d44f7855ca | 18 | volatile int count = 0; |
vsoltan | 14:6b20a930e1cb | 19 | volatile int sendFlag = 0; |
vsoltan | 14:6b20a930e1cb | 20 | volatile int moveData = 0; |
denizguler | 13:95d44f7855ca | 21 | |
vsoltan | 12:91affff3be75 | 22 | // interrupt service routines |
denizguler | 13:95d44f7855ca | 23 | void pressLeft( void ) { |
vsoltan | 14:6b20a930e1cb | 24 | sendFlag = 1; |
vsoltan | 14:6b20a930e1cb | 25 | moveData--; |
vsoltan | 12:91affff3be75 | 26 | } |
vsoltan | 12:91affff3be75 | 27 | |
vsoltan | 12:91affff3be75 | 28 | void pressMiddle() { |
vsoltan | 14:6b20a930e1cb | 29 | // count++; |
vsoltan | 12:91affff3be75 | 30 | } |
vsoltan | 12:91affff3be75 | 31 | |
vsoltan | 12:91affff3be75 | 32 | void pressRight() { |
vsoltan | 14:6b20a930e1cb | 33 | sendFlag = 1; |
vsoltan | 14:6b20a930e1cb | 34 | moveData++; |
vsoltan | 12:91affff3be75 | 35 | } |
emilmont | 6:25aad2d88749 | 36 | |
emilmont | 6:25aad2d88749 | 37 | int main() { |
denizguler | 13:95d44f7855ca | 38 | initEthernet(ð, &sock, &nist); |
emilmont | 7:dedf5dde9798 | 39 | |
vsoltan | 12:91affff3be75 | 40 | // initialize GPIO |
denizguler | 13:95d44f7855ca | 41 | leftButton.attach(&pressLeft, IRQ_RISE, DEBOUNCE); |
denizguler | 13:95d44f7855ca | 42 | middleButton.attach(&pressMiddle, IRQ_RISE, DEBOUNCE); |
denizguler | 13:95d44f7855ca | 43 | rightButton.attach(&pressRight, IRQ_RISE, DEBOUNCE); |
vsoltan | 12:91affff3be75 | 44 | |
vsoltan | 15:9d90f68e53da | 45 | GameState *gs = new GameState(); |
vsoltan | 16:7fd48cda0773 | 46 | Graphics *gfx = new Graphics(); |
vsoltan | 15:9d90f68e53da | 47 | |
vsoltan | 14:6b20a930e1cb | 48 | char toSend[] = "{\"type\": \"connected\", \"data\": \"Ay whats good\"}"; |
vsoltan | 14:6b20a930e1cb | 49 | char readTo[256]; |
vsoltan | 14:6b20a930e1cb | 50 | |
vsoltan | 15:9d90f68e53da | 51 | while (1) { // keep program running |
vsoltan | 15:9d90f68e53da | 52 | |
vsoltan | 15:9d90f68e53da | 53 | /* TODO: poll for navigation within menu |
vsoltan | 15:9d90f68e53da | 54 | * press any button to start |
vsoltan | 15:9d90f68e53da | 55 | */ |
vsoltan | 15:9d90f68e53da | 56 | |
vsoltan | 15:9d90f68e53da | 57 | while (!gs->done()) { |
vsoltan | 15:9d90f68e53da | 58 | if (sendFlag != 0) { |
vsoltan | 15:9d90f68e53da | 59 | printf("Move value: %i\n\r", moveData); |
vsoltan | 15:9d90f68e53da | 60 | sendFlag = 0; |
vsoltan | 15:9d90f68e53da | 61 | moveData = 0; |
vsoltan | 15:9d90f68e53da | 62 | sock.sendTo(nist, toSend, sizeof(toSend) - 1); |
vsoltan | 15:9d90f68e53da | 63 | } |
vsoltan | 15:9d90f68e53da | 64 | int bytesRead = sock.receiveFrom(nist, readTo, sizeof(readTo)); |
vsoltan | 15:9d90f68e53da | 65 | if (bytesRead > 0) { |
vsoltan | 15:9d90f68e53da | 66 | printf("Reading: %i bytes, data: %s\n\r", bytesRead, readTo); |
vsoltan | 15:9d90f68e53da | 67 | // TODO: update game state with json string |
vsoltan | 15:9d90f68e53da | 68 | } |
vsoltan | 15:9d90f68e53da | 69 | wait(.1); |
vsoltan | 14:6b20a930e1cb | 70 | } |
vsoltan | 15:9d90f68e53da | 71 | wait(0.5); |
denizguler | 13:95d44f7855ca | 72 | } |
vsoltan | 15:9d90f68e53da | 73 | // cleanupEthernet(ð, &sock); |
donatien | 1:8e1d4987eb90 | 74 | } |