Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed MbedJSONValue mbed-rtos Adafruit_ST7735 Adafruit_GFX EthernetInterface DebouncedInterrupt
main.cpp@22:1c49e1fae846, 2020-11-16 (annotated)
- Committer:
- vsoltan
- Date:
- Mon Nov 16 02:04:23 2020 +0000
- Revision:
- 22:1c49e1fae846
- Parent:
- 19:58cc5465f647
- Child:
- 23:c38680c32552
improved ball rendering
Who changed what in which revision?
| User | Revision | Line number | New contents of line | 
|---|---|---|---|
| vsoltan | 18:32fce82690a1 | 1 | |
| donatien | 1:8e1d4987eb90 | 2 | #include "mbed.h" | 
| donatien | 1:8e1d4987eb90 | 3 | #include "EthernetInterface.h" | 
| vsoltan | 11:d0a105f6743f | 4 | #include "network.h" | 
| vsoltan | 15:9d90f68e53da | 5 | #include "gamestate.h" | 
| vsoltan | 16:7fd48cda0773 | 6 | #include "graphics.h" | 
| denizguler | 13:95d44f7855ca | 7 | #include "DebouncedInterrupt.h" | 
| vsoltan | 18:32fce82690a1 | 8 | #include "MbedJSONValue.h" | 
| vsoltan | 12:91affff3be75 | 9 | |
| vsoltan | 14:6b20a930e1cb | 10 | #define DEBOUNCE 50 | 
| vsoltan | 12:91affff3be75 | 11 | |
| denizguler | 13:95d44f7855ca | 12 | DebouncedInterrupt leftButton(p21); | 
| denizguler | 13:95d44f7855ca | 13 | DebouncedInterrupt middleButton(p22); | 
| denizguler | 13:95d44f7855ca | 14 | DebouncedInterrupt rightButton(p23); | 
| vsoltan | 12:91affff3be75 | 15 | |
| vsoltan | 12:91affff3be75 | 16 | EthernetInterface eth; | 
| vsoltan | 12:91affff3be75 | 17 | UDPSocket sock; | 
| vsoltan | 12:91affff3be75 | 18 | Endpoint nist; | 
| vsoltan | 12:91affff3be75 | 19 | |
| denizguler | 13:95d44f7855ca | 20 | volatile int count = 0; | 
| vsoltan | 14:6b20a930e1cb | 21 | volatile int sendFlag = 0; | 
| vsoltan | 14:6b20a930e1cb | 22 | volatile int moveData = 0; | 
| vsoltan | 17:32ae1f106002 | 23 | volatile int menuPress = 0; | 
| denizguler | 13:95d44f7855ca | 24 | |
| vsoltan | 17:32ae1f106002 | 25 | // interrupts service routines | 
| vsoltan | 17:32ae1f106002 | 26 | |
| vsoltan | 17:32ae1f106002 | 27 | void pressButtonMenu(void) { | 
| vsoltan | 17:32ae1f106002 | 28 | menuPress = 1; | 
| vsoltan | 17:32ae1f106002 | 29 | } | 
| vsoltan | 17:32ae1f106002 | 30 | |
| vsoltan | 17:32ae1f106002 | 31 | void pressLeftGame( void ) { | 
| vsoltan | 14:6b20a930e1cb | 32 | sendFlag = 1; | 
| vsoltan | 14:6b20a930e1cb | 33 | moveData--; | 
| vsoltan | 12:91affff3be75 | 34 | } | 
| vsoltan | 12:91affff3be75 | 35 | |
| vsoltan | 17:32ae1f106002 | 36 | void pressRightGame() { | 
| vsoltan | 14:6b20a930e1cb | 37 | sendFlag = 1; | 
| vsoltan | 14:6b20a930e1cb | 38 | moveData++; | 
| vsoltan | 12:91affff3be75 | 39 | } | 
| emilmont | 6:25aad2d88749 | 40 | |
| emilmont | 6:25aad2d88749 | 41 | int main() { | 
| vsoltan | 19:58cc5465f647 | 42 | initEthernet(ð, &sock, &nist); | 
| vsoltan | 17:32ae1f106002 | 43 | Graphics *gfx = new Graphics(); | 
| vsoltan | 19:58cc5465f647 | 44 | |
| vsoltan | 17:32ae1f106002 | 45 | // attach ISR | 
| vsoltan | 17:32ae1f106002 | 46 | leftButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); | 
| vsoltan | 17:32ae1f106002 | 47 | middleButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); | 
| vsoltan | 17:32ae1f106002 | 48 | rightButton.attach(&pressButtonMenu, IRQ_RISE, DEBOUNCE); | 
| vsoltan | 12:91affff3be75 | 49 | |
| vsoltan | 17:32ae1f106002 | 50 | MbedJSONValue serverResponse; | 
| vsoltan | 15:9d90f68e53da | 51 | |
| vsoltan | 18:32fce82690a1 | 52 | char connectionRequest[] = "{\"type\": \"connected\"}"; | 
| vsoltan | 18:32fce82690a1 | 53 | char toSend[] = "{\"type\": \"move\", \"delta\": 5}"; | 
| vsoltan | 14:6b20a930e1cb | 54 | char readTo[256]; | 
| vsoltan | 14:6b20a930e1cb | 55 | |
| vsoltan | 17:32ae1f106002 | 56 | gfx->renderLaunchScreen(); | 
| vsoltan | 15:9d90f68e53da | 57 | |
| vsoltan | 17:32ae1f106002 | 58 | while (1) { // keep program running | 
| vsoltan | 17:32ae1f106002 | 59 | if (menuPress) { | 
| vsoltan | 17:32ae1f106002 | 60 | GameState *gs = new GameState(); | 
| vsoltan | 17:32ae1f106002 | 61 | |
| vsoltan | 18:32fce82690a1 | 62 | // request an open lobby from the server | 
| vsoltan | 18:32fce82690a1 | 63 | sock.sendTo(nist, connectionRequest, sizeof(connectionRequest) - 1); | 
| vsoltan | 18:32fce82690a1 | 64 | |
| vsoltan | 17:32ae1f106002 | 65 | // change ISRs to game controls | 
| vsoltan | 17:32ae1f106002 | 66 | leftButton.attach(&pressLeftGame, IRQ_RISE, DEBOUNCE); | 
| vsoltan | 17:32ae1f106002 | 67 | rightButton.attach(&pressRightGame, IRQ_RISE, DEBOUNCE); | 
| vsoltan | 17:32ae1f106002 | 68 | |
| vsoltan | 17:32ae1f106002 | 69 | while (!gs->done()) { | 
| vsoltan | 17:32ae1f106002 | 70 | if (sendFlag != 0) { | 
| vsoltan | 17:32ae1f106002 | 71 | printf("Move value: %i\n\r", moveData); | 
| vsoltan | 17:32ae1f106002 | 72 | sendFlag = 0; | 
| vsoltan | 17:32ae1f106002 | 73 | moveData = 0; | 
| vsoltan | 17:32ae1f106002 | 74 | sock.sendTo(nist, toSend, sizeof(toSend) - 1); | 
| vsoltan | 17:32ae1f106002 | 75 | } | 
| vsoltan | 17:32ae1f106002 | 76 | int bytesRead = sock.receiveFrom(nist, readTo, sizeof(readTo)); | 
| vsoltan | 17:32ae1f106002 | 77 | readTo[bytesRead] = 0; | 
| vsoltan | 17:32ae1f106002 | 78 | if (bytesRead > 0) { | 
| vsoltan | 17:32ae1f106002 | 79 | parse(serverResponse, readTo); | 
| vsoltan | 18:32fce82690a1 | 80 | gs->update(&serverResponse, gfx); | 
| vsoltan | 17:32ae1f106002 | 81 | } | 
| vsoltan | 17:32ae1f106002 | 82 | wait(.1); | 
| vsoltan | 15:9d90f68e53da | 83 | } | 
| vsoltan | 17:32ae1f106002 | 84 | gfx->renderLaunchScreen(); | 
| vsoltan | 17:32ae1f106002 | 85 | // reset the game | 
| vsoltan | 17:32ae1f106002 | 86 | menuPress = 0; | 
| vsoltan | 14:6b20a930e1cb | 87 | } | 
| vsoltan | 17:32ae1f106002 | 88 | wait(0.3); | 
| denizguler | 13:95d44f7855ca | 89 | } | 
| vsoltan | 15:9d90f68e53da | 90 | // cleanupEthernet(ð, &sock); | 
| donatien | 1:8e1d4987eb90 | 91 | } | 
