diff --git a/examples/projects/native/ping_pong/README.md b/examples/projects/native/ping_pong/README.md index 4a4c02e1d..ead9f8b48 100644 --- a/examples/projects/native/ping_pong/README.md +++ b/examples/projects/native/ping_pong/README.md @@ -14,20 +14,44 @@ -# Button project example :bulb: -This project demonstrate how to make and use a simple button through Luos. Feel free to use electronics and code example as you want. - -## How to compile the code :computer: - - 1. Download and install [Platformio](https://platformio.org/platformio-ide) - 2. Open this folder into Platformio - 3. Build (Platformio will do the rest) - -## How to open the electronic design :electric_plug: -You can open [a working example electronic design](https://github.com/Luos-io/luos_engine/tree/main/examples/hardware) with Kicad. This design use Luos_components library for more information to install and use it read [our doc](https://docs.luos.io). - -## Linked driver -This project is linked to the [Button driver](../../Drivers/button). +# The Luos ping pong world cup project example :bulb: +``` +The Luos ping pong world cup! + ((((. + ,((((((((((((( + (#########((((((((((((((((( + ###########((((((((((((((((((( + ,###########(((((((((((((((((((( + #############((((((((((((((((((( + ,,*###########((((((((((((((((( + ,,,,###########,,,,,,,,,,,,. + ***,,,,*######### ,***** + ******* .,,,### ****. + ****** *// ****, + *** *//// ***** + '' . +Controls: + - If the ball is going left press '←' + - If the ball is going right press '→' + Press SPACE BAR to start! +``` +This project demonstrate how to make and use a simple multiplayer game through Luos. +This small and simple game run on your computer terminal and allow you to play with your friends. + +## Before playing :computer: + + 1. Open a terminal on this project and run the broker at a given IP and port `./../broker.py --ip 'YOUR_LOCAL_IP' -p 8000` + +You only need to have one somewhere running, it's a kind of server, then anyone on your local network will be able to play. + +## How to compile and play the game :video_game: + + 1. Install GCC on your computer + 2. Download and install [Platformio](https://platformio.org/platformio-ide) + 3. Open this folder into Platformio + 4. Set the broker IP and port on the `node_config.h` file by replacing `#define WS_BROKER_ADDR "ws://127.0.0.1:8000"` with the corect IP and port + 5. Build (Platformio will do the rest) + 6. Open a new terminal on this projet and run the compiled binary `./.pio/build/native/program` ## Don't hesitate to read [our documentation](https://docs.luos.io), or to post your questions/issues on the [Luos' Forum](https://community.luos.io). :books: diff --git a/examples/projects/native/ping_pong/lib/PingPong/graph.c b/examples/projects/native/ping_pong/lib/PingPong/graph.c index f627e4d62..dd57553dc 100644 --- a/examples/projects/native/ping_pong/lib/PingPong/graph.c +++ b/examples/projects/native/ping_pong/lib/PingPong/graph.c @@ -31,7 +31,7 @@ bool need_update = false; ball_t *ball_state = NULL; /* msleep(): Sleep for the requested number of milliseconds. */ -static int msleep(long msec) +int msleep(long msec) { struct timespec ts; int res; @@ -93,8 +93,12 @@ void start_view(void) { need_update = false; clear_screen(); + printf("\tThe Luos ping pong world cup!\n"); printf("%s", start); - printf("THE LUOS PING PONG World cup!\n\tPress SPACE BAR to start!\n"); + printf("Controls:\n"); + printf("\t - If the ball is going left press '←'\n"); + printf("\t - If the ball is going right press '→'\n"); + printf("\tPress SPACE BAR to start!\n"); } } @@ -104,9 +108,13 @@ void alone_view(void) { need_update = false; clear_screen(); + printf("\tThe Luos ping pong world cup!\n"); printf("%s", start); printf("You don't have any friends?\n\nYou should find some on the Luos discord :\n\thttps://discord.gg/luos\n\n"); - printf("THE LUOS PING PONG GAME\n\tPress SPACE BAR to start!\n"); + printf("Controls:\n"); + printf("\t - If the ball is going left press '←'\n"); + printf("\t - If the ball is going right press '→'\n"); + printf("\tPress SPACE BAR to start!\n"); } } @@ -161,6 +169,7 @@ void wait_view(void) { need_update = false; clear_screen(); + printf("\tThe Luos ping pong world cup!\n"); printf("%s", start); printf("Game is starting, please wait...\n"); } diff --git a/examples/projects/native/ping_pong/lib/PingPong/graph.h b/examples/projects/native/ping_pong/lib/PingPong/graph.h index c9cf1b1fc..edca93102 100644 --- a/examples/projects/native/ping_pong/lib/PingPong/graph.h +++ b/examples/projects/native/ping_pong/lib/PingPong/graph.h @@ -18,6 +18,7 @@ typedef void (*SCREEN)(void); void set_screen_to(SCREEN); void create_ball(ball_t *ballpt); void *Graph_LoopThread(void *vargp); +int msleep(long msec); void start_view(void); void service_view(void); diff --git a/examples/projects/native/ping_pong/lib/PingPong/ping_pong.c b/examples/projects/native/ping_pong/lib/PingPong/ping_pong.c index a0698cf94..535395c7a 100644 --- a/examples/projects/native/ping_pong/lib/PingPong/ping_pong.c +++ b/examples/projects/native/ping_pong/lib/PingPong/ping_pong.c @@ -25,9 +25,13 @@ * Definitions ******************************************************************************/ #ifdef _WIN32 - #define get_character() getch() + #define get_character() _getch() + #define LEFT_KEY 'K' + #define RIGHT_KEY 'M' #else #define get_character() getchar() + #define LEFT_KEY 'D' + #define RIGHT_KEY 'C' #endif #define BALL_TIMEOUT 700 @@ -90,7 +94,7 @@ void PingPong_Init(void) revision_t revision = {.major = 1, .minor = 0, .build = 0}; // Service creation char str[30]; - + printf("\n\t\tWelcome to \n\tThe Luos ping pong world cup!\n"); printf("Enter your player name :"); scanf("%s", str); @@ -105,6 +109,19 @@ void PingPong_Init(void) srand(time(NULL)); // Initialization, should only be called once. } + +/****************************************************************************** + * @brief loop must be call in project loop + * @param None + * @return None + ******************************************************************************/ +void PingPong_Loop(void) +{ + // This is a function pointer that will louch the good function depending on the state of the game. + game_state(); + msleep(10); +} + #ifndef _WIN32 int kbhit(void) { @@ -133,36 +150,10 @@ int kbhit(void) return 0; } #endif -/* Returns an integer in the range [0, n). - * - * Uses rand(), and so is affected-by/affects the same seed. - */ -int randint(int n) -{ - if ((n - 1) == RAND_MAX) - { - return rand(); - } - else - { - - // Chop off all of the values that would cause skew... - int end - = RAND_MAX / n; // truncate skew - end *= n; - - // ... and ignore results from rand() that fall above that limit. - // (Worst case the loop condition should succeed 50% of the time, - // so we can expect to bail out of this loop pretty quickly.) - int r; - while ((r = rand()) >= end) - ; - return r % n; - } -} bool send_random() { + // This function send the ball to a random player at a random position. // Randomize ball position. uint8_t r = (rand() % 2) + 1; // Returns a pseudo-random integer between 0 and RAND_MAX. // Randomize target @@ -176,7 +167,7 @@ bool send_random() int target; do { - target = randint(target_list.result_nbr); + target = rand() % target_list.result_nbr; } while (target_list.result_table[target]->id == player->ll_service->id); // Our target is OK, Send the message @@ -315,27 +306,26 @@ void game_loading() } } -void game_running(void) +void game_running() { - // printf("running\n"); static ball_t last_ball = LEFT; static uint32_t empty_date; - char c; + uint8_t c; if (!Luos_IsNodeDetected()) { + // Someone relaunch a detection game_state = game_loading; return; } - // Game running if (last_ball != ball) { + // Update the screen to display last_ball = ball; set_screen_to(game_view); } // Game loop if (ball == EMPTY) { - // printf("empty\n"); empty_date = Luos_GetSystick(); } else @@ -343,33 +333,43 @@ void game_running(void) // We have the ball on our table, user have to react. if ((kbhit())) { +#ifdef _WIN32 c = get_character(); - if (c == 'a') + if (c == 0 || c == 224) { - if (ball == LEFT) - { - ball = EMPTY; - send_random(); - } - else - { - // Wrong key - game_state = game_over; - return; - } - } - if (c == 'z') +#else + if (get_character() == '\033') { - if (ball == RIGHT) + get_character(); // skip the [ +#endif + c = get_character(); + if (c == LEFT_KEY) { - ball = EMPTY; - send_random(); + if (ball == LEFT) + { + ball = EMPTY; + send_random(); + } + else + { + // Wrong key + game_state = game_over; + return; + } } - else + if (c == RIGHT_KEY) { - // Wrong key - game_state = game_over; - return; + if (ball == RIGHT) + { + ball = EMPTY; + send_random(); + } + else + { + // Wrong key + game_state = game_over; + return; + } } } } @@ -381,13 +381,3 @@ void game_running(void) } } } - -/****************************************************************************** - * @brief loop must be call in project loop - * @param None - * @return None - ******************************************************************************/ -void PingPong_Loop(void) -{ - game_state(); -} diff --git a/examples/projects/native/ping_pong/node_config.h b/examples/projects/native/ping_pong/node_config.h index 00a4f1ea4..889fba9c6 100644 --- a/examples/projects/native/ping_pong/node_config.h +++ b/examples/projects/native/ping_pong/node_config.h @@ -46,6 +46,7 @@ #define MAX_SERVICE_NUMBER 1 #define MAX_PROFILE_NUMBER 1 #define MAX_MSG_NB 5 +#define MAX_RTB_ENTRY 100 /******************************************************************************* * LUOS HAL LIBRARY DEFINITION @@ -79,6 +80,8 @@ * LUOS_TIMER | Timer number * LUOS_TIMER_IRQ | Timer IRQ number * LUOS_TIMER_IRQHANDLER | Callback function for Timer IRQ handler + * + * WS_BROKER_ADDR | The broker adress in native mode. Default value is "ws://127.0.0.1:8000" ******************************************************************************/ /******************************************************************************* @@ -92,4 +95,6 @@ * APP_END_ADDRESS | FLASH_BANK1_END=0x0801FFFF | End address of application with bootloader ******************************************************************************/ +#define WS_BROKER_ADDR "ws://127.0.0.1:8000" + #endif /* _NODE_CONFIG_H_ */