From 28af1f278200d984bcebb1bdf2fa4108879f24c8 Mon Sep 17 00:00:00 2001
From: "remi.greub" <remi.greub@hes-so.ch>
Date: Sun, 6 Apr 2025 16:02:54 +0200
Subject: [PATCH] ajout de l'envoi ethernet non fonctionnel

---
 G3_TP1_labyrinth_etu/src/labyrinth.c | 151 ++++++++++++++++++++++++---
 1 file changed, 137 insertions(+), 14 deletions(-)

diff --git a/G3_TP1_labyrinth_etu/src/labyrinth.c b/G3_TP1_labyrinth_etu/src/labyrinth.c
index b8ad1d7..de411ce 100644
--- a/G3_TP1_labyrinth_etu/src/labyrinth.c
+++ b/G3_TP1_labyrinth_etu/src/labyrinth.c
@@ -77,6 +77,10 @@ static coord_fx_t labyrinth_points[]={
 		{GOAL_H_WALL-4*BALL_RADIUS-WALL_WIDTH-1,LCD_MAX_HEIGHT-WALL_WIDTH},{GOAL_H_WALL-4*BALL_RADIUS-WALL_WIDTH-1,GOAL_V_WALL},{END_OF_WALLS,END_OF_WALLS}
 };
 
+master_tx_t txBuf;
+accel_t rxBuf;
+bool flag_acc_slave=false;
+master_tx_t *j;
 
 /* Description: Callback of ethernet module. This function is called when a message is received
  * Parameters: data: pointer on data received
@@ -84,15 +88,19 @@ static coord_fx_t labyrinth_points[]={
  */
 void slave_info_rx(void *data, int len)
 {
-	// code executed when the slave sends an ethernet frame
-	if(len == sizeof(master_tx_t)){
-		master_tx_t mstr = (master_tx_t)data;
-	}
+	/*if(len == sizeof(accel_t)){
+		accel_t *x;
+		x = (accel_t*)data;
+		rxBuf = *x;
+		flag_acc_slave = true;
+	}*/
 }
 
 void Algo_main_ball(void *params);
 void Algo_bots(void *params);
 void RandAccel(accel_t *accel);
+void Algo_slave_ball(void *params);
+
 
 int main(void)
 {
@@ -112,7 +120,10 @@ int main(void)
 		printf("Accelerometer initialisation failed!\n");
 		return 1;
 	}
-	//ethernet_init(..., slave_info_rx); // initialise ethernet and determine its callback function
+	int txLen = sizeof(master_tx_t) * 2;
+	int rxLen = sizeof(accel_t) * 2;
+
+
 
 	draw_labyrinth(labyrinth_points);
 	init_traces(115200, 1, false, true); // initialise traces. Line to be removed if you implement your own traces
@@ -123,7 +134,10 @@ int main(void)
 	xTaskCreate(Algo_main_ball, (signed portCHAR *)"Masterball",
 					configMINIMAL_STACK_SIZE, &ball_param_th[0], tskIDLE_PRIORITY+1,
 					NULL);
-	xTaskCreate(Algo_bots, (signed portCHAR *)"libreball1",
+	/*xTaskCreate(Algo_slave_ball, (signed portCHAR *)"Slaveball",
+						configMINIMAL_STACK_SIZE, &ball_param_th[1], tskIDLE_PRIORITY+1,
+						NULL);*/
+	/*xTaskCreate(Algo_bots, (signed portCHAR *)"libreball1",
 						configMINIMAL_STACK_SIZE, &ball_param_th[2], tskIDLE_PRIORITY+1,
 						NULL);
 	xTaskCreate(Algo_bots, (signed portCHAR *)"libreball2",
@@ -131,7 +145,8 @@ int main(void)
 						NULL);
 	xTaskCreate(Algo_bots, (signed portCHAR *)"libreball3",
 						configMINIMAL_STACK_SIZE, &ball_param_th[4], tskIDLE_PRIORITY+1,
-						NULL);
+						NULL);*/
+	j = (master_tx_t*)ethernet_init((unsigned char*)&txBuf, txLen, (unsigned char*)&rxBuf, rxLen, slave_info_rx); // initialise ethernet and determine its callback function
 	vTaskStartScheduler();		// launch scheduler
 	LPC_TIM0->TCR = 1;
 
@@ -144,6 +159,7 @@ void Algo_main_ball(void *params){
 	ball_param_th_t *ball = (ball_param_th_t *)params;
 	accel_t ball_acc;
 	int un_ou_moins_un_x = 1, un_ou_moins_un_y = 1;
+
 	while(1){
 		lcd_filled_circle(round(ball->ball.pos.x), round(ball->ball.pos.y), ball->ball.radius, LCD_BLACK);
 		int CollisionState = test_collision(&ball->ball);
@@ -184,7 +200,7 @@ void Algo_main_ball(void *params){
 				ball->ball.pos.y -= 2;
 				//ball->ball.pos.y = floor(ball->ball.pos.y);
 			}
-			ball->ball.pos.x = round(ball->ball.pos.x);
+			ball->ball.pos.x = round(ball->ball.pos.x); //ATTENTION OSDJBG KSDJGBKSJDGB KSJD GSKJDBG KJSDBG KSBDJGKB SDGKBBKJ=============
 			ball->ball.pos.y = round(ball->ball.pos.y);
 		}else{
 			accel_read(&ball_acc);
@@ -200,8 +216,8 @@ void Algo_main_ball(void *params){
 			ball->ball.pos.y = ball->ball.pos.y + ball->ball.speed.y*(un_ou_moins_un_y);
 			un_ou_moins_un_y = 1;
 			un_ou_moins_un_x = 1;
-			//ball->ball.pos.x = round(ball->ball.pos.x);
-			//ball->ball.pos.y = round(ball->ball.pos.y);
+			ball->ball.pos.x = round(ball->ball.pos.x);
+			ball->ball.pos.y = round(ball->ball.pos.y);
 		}
 
 		//détection des collisions sur les bords de l'ecran LCD
@@ -217,6 +233,17 @@ void Algo_main_ball(void *params){
 		if(round(ball->ball.pos.y) >= (LCD_MAX_HEIGHT - (ball->ball.radius))){
 			ball->ball.pos.y = (LCD_MAX_HEIGHT - (ball->ball.radius+1));
 		}
+
+		txBuf.ball_coord.x = round(ball->ball.pos.x);
+		txBuf.ball_coord.y = round(ball->ball.pos.y);
+		txBuf.ball_id = ball->thread_id;
+		txBuf.color = ball->ball.color;
+		txBuf.radius = ball->ball.radius;
+		txBuf.winner = -1;
+
+		j = &txBuf;
+		j = (master_tx_t*)send_eth(sizeof(master_tx_t), false);
+
 		lcd_filled_circle(round(ball->ball.pos.x), round(ball->ball.pos.y), ball->ball.radius, ball->ball.color);
 		vTaskDelay((portTickType)ball->ball.period/portTICK_RATE_MS);
 	}
@@ -233,7 +260,6 @@ void Algo_bots(void *params){
 			//ralentissement de 10% de la vitesse de la balle
 			//ball->ball.speed.x = ball->ball.speed.x - (ball->ball.speed.x/10);
 			//ball->ball.speed.y = ball->ball.speed.y - (ball->ball.speed.y/10);
-
 			if(CollisionState & TOUCH_LEFT){
 				un_ou_moins_un_x = -1;
 				ball->ball.speed.x = ball->ball.speed.x - (ball->ball.speed.x/10);
@@ -282,8 +308,8 @@ void Algo_bots(void *params){
 			ball->ball.pos.y = ball->ball.pos.y + ball->ball.speed.y*(un_ou_moins_un_y);
 			un_ou_moins_un_y = 1;
 			un_ou_moins_un_x = 1;
-			//ball->ball.pos.x = round(ball->ball.pos.x);
-			//ball->ball.pos.y = round(ball->ball.pos.y);
+			ball->ball.pos.x = round(ball->ball.pos.x);
+			ball->ball.pos.y = round(ball->ball.pos.y);
 		}
 
 		//détection des collisions sur les bords de l'ecran LCD
@@ -299,12 +325,14 @@ void Algo_bots(void *params){
 		if(round(ball->ball.pos.y) >= (LCD_MAX_HEIGHT - (ball->ball.radius))){
 			ball->ball.pos.y = (LCD_MAX_HEIGHT - (ball->ball.radius+1));
 		}
+
+
+
 		lcd_filled_circle(round(ball->ball.pos.x), round(ball->ball.pos.y), ball->ball.radius, ball->ball.color);
 		vTaskDelay((portTickType)ball->ball.period/portTICK_RATE_MS);
 	}
 }
 
-
 //random d'accélérations
 void RandAccel(accel_t *accel){
 	float x = (float)(rnd32()%100)-50;
@@ -312,3 +340,98 @@ void RandAccel(accel_t *accel){
 	accel->accel_x = x/100;
 	accel->accel_y = y/100;
 }
+
+void Algo_slave_ball(void *params){
+	ball_param_th_t *ball = (ball_param_th_t *)params;
+	int un_ou_moins_un_x = 1, un_ou_moins_un_y = 1;
+	int rec = sizeof(accel_t);
+	while(1){
+		lcd_filled_circle(round(ball->ball.pos.x), round(ball->ball.pos.y), ball->ball.radius, LCD_BLACK);
+		int CollisionState = test_collision(&ball->ball);
+		if(CollisionState != NO_COLLISION){
+			//ralentissement de 10% de la vitesse de la balle
+			//ball->ball.speed.x = ball->ball.speed.x - (ball->ball.speed.x/10);
+			//ball->ball.speed.y = ball->ball.speed.y - (ball->ball.speed.y/10);
+
+			if(CollisionState & TOUCH_LEFT){
+				un_ou_moins_un_x = -1;
+				ball->ball.speed.x = ball->ball.speed.x - (ball->ball.speed.x/10);
+				//ball->ball.speed.x *= -REBOUND_FACTOR;
+				//ball->ball.pos.x += ball->ball.radius*0.4;	//round
+				ball->ball.pos.x += 2;
+				//ball->ball.pos.x = ceil(ball->ball.pos.x);
+			}
+			if(CollisionState & TOUCH_UP){
+				un_ou_moins_un_y = -1;
+				ball->ball.speed.y = ball->ball.speed.y - (ball->ball.speed.y/10);
+				//ball->ball.speed.y *= -REBOUND_FACTOR;
+				//ball->ball.pos.y += ball->ball.radius*0.4;	//round()
+				ball->ball.pos.y += 2;
+				//ball->ball.pos.y = ceil(ball->ball.pos.y);
+			}
+			if(CollisionState & TOUCH_RIGHT){
+				un_ou_moins_un_x = -1;
+				ball->ball.speed.x = ball->ball.speed.x - (ball->ball.speed.x/10);
+				//ball->ball.speed.x *= -REBOUND_FACTOR;
+				//ball->ball.pos.x -= ball->ball.radius*0.4;	//round()
+				ball->ball.pos.x -= 2;
+				//ball->ball.pos.x = floor(ball->ball.pos.x);
+			}
+			if(CollisionState & TOUCH_DOWN){
+				un_ou_moins_un_y = -1;
+				ball->ball.speed.y = ball->ball.speed.y - (ball->ball.speed.y/10);
+				//ball->ball.speed.y *= -REBOUND_FACTOR;
+				//ball->ball.pos.y -= ball->ball.radius*0.4;	//round()
+				ball->ball.pos.y -= 2;
+				//ball->ball.pos.y = floor(ball->ball.pos.y);
+			}
+			ball->ball.pos.x = round(ball->ball.pos.x);
+			ball->ball.pos.y = round(ball->ball.pos.y);
+		}else{
+			if(flag_acc_slave == true)
+			{
+				(accel_t*)rec_eth(&rec);
+				ball->ball.speed.x =((rxBuf.accel_x*0.3)+ball->ball.speed.x)*(un_ou_moins_un_x);
+				ball->ball.speed.y = ((rxBuf.accel_y*0.3)+ball->ball.speed.y)*(un_ou_moins_un_y);
+				flag_acc_slave = false;
+			}
+			ball->ball.speed.x = (ball->ball.speed.x >= SPEED_MAX) ? SPEED_MAX: ball->ball.speed.x;
+			ball->ball.speed.y = (ball->ball.speed.y >= SPEED_MAX) ? SPEED_MAX: ball->ball.speed.y;
+			ball->ball.speed.x = (ball->ball.speed.x <= -SPEED_MAX) ? -SPEED_MAX: ball->ball.speed.x;
+			ball->ball.speed.y = (ball->ball.speed.y <= -SPEED_MAX) ? -SPEED_MAX: ball->ball.speed.y;
+
+			ball->ball.pos.x = ball->ball.pos.x + ball->ball.speed.x*(un_ou_moins_un_x);
+			ball->ball.pos.y = ball->ball.pos.y + ball->ball.speed.y*(un_ou_moins_un_y);
+			un_ou_moins_un_y = 1;
+			un_ou_moins_un_x = 1;
+			ball->ball.pos.x = round(ball->ball.pos.x);
+			ball->ball.pos.y = round(ball->ball.pos.y);
+		}
+		//détection des collisions sur les bords de l'ecran LCD
+		if(round(ball->ball.pos.x) <= ball->ball.radius){
+			ball->ball.pos.x = ball->ball.radius+1;
+		}
+		if(round(ball->ball.pos.y) <= ball->ball.radius){
+			ball->ball.pos.y = ball->ball.radius+1;
+		}
+		if(round(ball->ball.pos.x) >= (LCD_MAX_WIDTH - (ball->ball.radius))){
+			ball->ball.pos.x = (LCD_MAX_WIDTH - (ball->ball.radius+1));
+		}
+		if(round(ball->ball.pos.y) >= (LCD_MAX_HEIGHT - (ball->ball.radius))){
+			ball->ball.pos.y = (LCD_MAX_HEIGHT - (ball->ball.radius+1));
+		}
+
+		txBuf.ball_coord.x = round(ball->ball.pos.x);
+		txBuf.ball_coord.y = round(ball->ball.pos.y);
+		txBuf.ball_id = ball->thread_id;
+		txBuf.color = ball->ball.color;
+		txBuf.radius = ball->ball.radius;
+		txBuf.winner = -1;
+
+		j = &txBuf;
+		j = (master_tx_t*)send_eth(sizeof(master_tx_t), false);
+
+		lcd_filled_circle(round(ball->ball.pos.x), round(ball->ball.pos.y), ball->ball.radius, ball->ball.color);
+		vTaskDelay((portTickType)ball->ball.period/portTICK_RATE_MS);
+	}
+}
-- 
GitLab