Le support aux utilisateurs et les demandes de création de comptes externes doivent être faites depuis les issues du projet GitLab HEFR (https://gitlab.forge.hefr.ch/forge-hefr/gitlab-hefr)

Commit 0c6e0c3c authored by Nicolas Maier's avatar Nicolas Maier
Browse files

Message "hello" pour initialiser

parent 0357d682
Pipeline #7255 passed with stage
in 41 seconds
......@@ -127,11 +127,13 @@ static void thread_nrf_rx() {
}
}
static void sleep_ms(uint32_t ms) {
static void sleep_ms(uint32_t ms, bool should_yield) {
timer_get_counter(DMTIMER_5);
while (timer_get_time_elapsed(DMTIMER_5)
/ (timer_get_frequency(DMTIMER_5) / 1000) < ms) {
thread_yield();
if (should_yield) {
thread_yield();
}
}
}
......@@ -192,15 +194,60 @@ static void send_wait_ack(uint8_t* txdata) {
break;
}
sleep_ms(COMMUNICATION_RETRY_DELAY_MS);
sleep_ms(COMMUNICATION_RETRY_DELAY_MS, true);
thread_yield();
}
}
static void send_wait_hello() {
uint8_t txdata[NRF_TX_SIZE];
txdata[0] = COMMUNICATION_HELLO;
int err_count = 0;
while (true) {
printf("Sending hello\n");
int status = nrf24_write(txdata, NRF_TX_SIZE);
if (status < 0) {
err_count++;
printf("Error while sending data... (error count: %d)\n",
err_count);
if (err_count >= COMMUNICATION_MAX_ERR_COUNT) {
communication_open();
}
}
bool received_hello = false;
nrf24_set_receive_mode();
for (int p = NRF24_PIPE_P1; p <= NRF24_PIPE_P5; p++) {
if (nrf24_readable(p)) {
char rx_data[NRF_TX_SIZE];
int len = nrf24_read(p, rx_data, NRF_TX_SIZE);
if (len > 0) {
if (len < NRF_TX_SIZE) {
printf("Wrong message size %d\n", len);
}
if (rx_data[0] == COMMUNICATION_HELLO) {
received_hello = true;
}
}
}
}
if (received_hello) {
printf("Received answer hello\n");
break;
} else {
printf("Didn't receive answer hello\n");
}
sleep_ms(COMMUNICATION_RETRY_DELAY_MS, false);
}
}
void communication_init(bool initIsJ1, msgqid_t msgq_game,
msgqid_t msgq_game_pty) {
timer_init(DMTIMER_5);
info = (struct communication_info) {
.isJ1 = initIsJ1,
......@@ -213,6 +260,8 @@ void communication_init(bool initIsJ1, msgqid_t msgq_game,
communication_open();
send_wait_hello();
thread_create(thread_nrf_rx, 0, "nrf_rx", 0);
}
......
......@@ -30,6 +30,7 @@
#include "msgq.h"
enum communication_message {
COMMUNICATION_HELLO,
COMMUNICATION_ACK,
COMMUNICATION_BAR,
COMMUNICATION_BALL,
......
......@@ -62,7 +62,7 @@
#define MIN_OFFSET_ANGLE_MODIFIER 2
#define SPECIAL_LOADING_MAX 12000000
#define MAX_SPECIAL 3
#define MAX_PSEUDORAND 1000
#define MAX_PSEUDORAND 10
struct game_info {
bool isJ1;
......@@ -150,6 +150,7 @@ static void prepare_game() {
.selected_special = SPECIAL_NONE,
.special_loading = 0,
};
display_clear_game_area();
prepare_set();
former_state = next_state;
......@@ -158,6 +159,7 @@ static void prepare_game() {
for (int i = 0; i < DISPLAY_SPECIAL_COUNT; i++) {
display_update_special_counter(next_state.ally_specials[i], i);
}
timer_get_counter(TIMER_WAIT_REFRESH);
}
static void show_end(bool isWin) {
......@@ -182,7 +184,8 @@ static void game_thread(void* param)
int step_count = STEP_COUNT_MAX;
while (1) {
while (true) {
while (true) {
void* msg = msgq_fetch(info.msgq);
if (msg == 0) msg = msgq_fetch(info.msgq_pty);
......@@ -302,6 +305,8 @@ static void game_thread(void* param)
next_state.ball_dir.x = -next_state.ball_dir.x;
}
printf("------------- elapsed %zu\n", elapsed_time_2_us);
if (next_state.is_ally_playing) {
if (next_state.current_special == SPECIAL_RANDOMBALL) {
if (former_state.ball_pos.y < DISPLAY_GAME_AREA_SIZE/3
......@@ -447,11 +452,8 @@ static void game_buttons_handler(enum buttons_set btn, void* param) {
void game_init(bool initIsJ1, msgqid_t msgq_game, msgqid_t msgq_game_pty) {
leds_init();
timer_init(TIMER_WAIT_REFRESH);
timer_init(TIMER_WAIT_READY);
timer_init(TIMER_WAIT_END);
buttons_on_event(game_buttons_handler, 0);
wheel_on_event(game_wheel_handler, 0);
info = (struct game_info) {
.isJ1 = initIsJ1,
......@@ -462,5 +464,4 @@ void game_init(bool initIsJ1, msgqid_t msgq_game, msgqid_t msgq_game_pty) {
prepare_game();
thread_create(game_thread, 0, "game_thread", 0);
wheel_on_event(game_wheel_handler, 0);
}
......@@ -37,6 +37,7 @@
#include "gpio.h"
#include "display.h"
#include "game.h"
#include "timer.h"
#include "kernel.h"
#include "thread.h"
......@@ -58,6 +59,11 @@ int main()
leds_init();
display_init();
timer_init(DMTIMER_2);
timer_init(DMTIMER_3);
timer_init(DMTIMER_4);
timer_init(DMTIMER_5);
display_show_text(" Press ", 8, 23);
display_show_text(" 1 or 2 ", 4, 33);
display_show_text("to start", 4, 43);
......@@ -75,6 +81,9 @@ int main()
}
display_clear_game_area();
display_show_text(" Waiting ", 0, 33);
display_show_text(isJ1 ? " for J2" : " for J1", 4, 43);
msgqid_t msgq_game = msgq_create(50);
msgqid_t msgq_game_pty = msgq_create(50);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment