From 000fe139daf723b7e9644c9095c3bc1e0b96c304 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Lars=20P=C3=B6pperl?= <poepperlla76860@th-nuernberg.de>
Date: Fri, 28 May 2021 00:33:13 +0200
Subject: [PATCH] corrected num leds and led_transmit

---
 Core/Src/main.c | 115 +++++++++++++++++++++++++++++++-----------------
 1 file changed, 75 insertions(+), 40 deletions(-)

diff --git a/Core/Src/main.c b/Core/Src/main.c
index 118bfd8..411f2cc 100644
--- a/Core/Src/main.c
+++ b/Core/Src/main.c
@@ -250,15 +250,14 @@ int main(void)
   Task2Handle = osThreadNew(Task02, NULL, &Task2_attributes);
 
   /* creation of Task3 */
-//  Task3Handle = osThreadNew(Validate, NULL, &Task3_attributes);
+  Task3Handle = osThreadNew(Validate, NULL, &Task3_attributes);
 
-//  /* creation of Task4 */
-//  Task4Handle = osThreadNew(Display, NULL, &Task4_attributes);
+	/* creation of Task4 */
+  Task4Handle = osThreadNew(Display, NULL, &Task4_attributes);
 
   /* USER CODE BEGIN RTOS_THREADS */
   /* add threads, ... */
-//	osThreadTerminate(Task1Handle);
-//	osThreadTerminate(Task3Handle);
+
   /* USER CODE END RTOS_THREADS */
 
   /* USER CODE BEGIN RTOS_EVENTS */
@@ -675,6 +674,12 @@ void lcd_clear (void)
 
 void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
 {	
+	int buttonState;             // the current reading from the input pin
+int lastButtonState = 1;  // the previous reading from the input pin
+	unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
+unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
+	
+	
   if(GPIO_Pin == btn1)
   {
 		HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_8);
@@ -813,7 +818,7 @@ void Task02(void *argument)
 	enum ROW { ROW1, ROW2, ROW3, ROW4 };
 	const uint8_t ROWS = 4;
 	const uint8_t FIELDS = 16; // 4 rows and 4 columns = 16
-		uint32_t colors[] = {
+	const	uint32_t colors[] = {
 		WHITE
 		,BLUE
 		,PURPLE
@@ -830,46 +835,30 @@ void Task02(void *argument)
 		0,0,0,0
 	};
 
-	uint8_t num_colors = sizeof(colors) / sizeof(uint32_t) - sizeof(uint32_t);
+	const uint8_t num_colors = sizeof(colors) / sizeof(uint32_t);
 	LED led = *get_instance();
 	const uint8_t gameRowXOR = 0xf;
 	uint16_t delay = 500;
 	bool isRunning = false;
+	bool hasLost = false;
 	bool scrollOut = false;
 	uint8_t scrollCounter = 0;
 
 	// init Matrix
 	const uint8_t say_hi[] = {1,2,3,6,9,10,11,12,14,15};
-	const uint8_t say_hi_length = sizeof(say_hi) / sizeof(uint8_t) - sizeof(uint8_t);
+	const uint8_t say_hi_length = sizeof(say_hi) / sizeof(uint8_t) - 1;
 	for(uint8_t i=0; i <= say_hi_length-3; i++) led_matrix[say_hi[i]] = TURQUOISE;
 	for(uint8_t i=say_hi_length-2; i <= say_hi_length; i++) led_matrix[say_hi[i]] = RED;
 
 	/* Infinite loop */
 	while(1) {
 
-		// set led matrix to current colors
-		led.LED_set_all_off(0);
-		uint8_t LEDMatrixIndex = 0;
-		for(uint8_t cube = 0; cube < FIELDS;) {
-				for(uint8_t column = 0; column <= 6; column+=2) {
-					led.LED_set_RGB_32(LEDMatrixIndex  						  , led_matrix[cube]);
-					led.LED_set_RGB_32(LEDMatrixIndex ^ gameRowXOR  , led_matrix[cube]);
-					led.LED_set_RGB_32(LEDMatrixIndex+1  					  , led_matrix[cube]);
-					led.LED_set_RGB_32(LEDMatrixIndex+1 ^ gameRowXOR, led_matrix[cube]);
-					cube++;
-					LEDMatrixIndex += 2;
-				}
-				LEDMatrixIndex += 8;
-		}
-		led.LED_transmit();
-		// end set colors
-
 		// try to take Semaphore from IRQ if user pressed USER_BTN to start game
 		if ( scrollOut || xSemaphoreTake( SemStartGameHandle, ( TickType_t ) 10 ) == pdTRUE ) {
 			//scroll out "Hi"
 			scrollOut = true;
 			scrollCounter++;
-			if (scrollCounter > 5) {
+			if (scrollCounter > 4) {
 				isRunning = true;
 				// release Levels Semaphore
 				xSemaphoreGive( SemLevelsHandle );
@@ -885,9 +874,11 @@ void Task02(void *argument)
 			if (led_matrix[7] > 0) activeLEDs += 2;
 			if (led_matrix[11] > 0) activeLEDs += 4;
 			if (led_matrix[15] > 0) activeLEDs += 8;
-			if (activeLEDs > 0) xQueueSend(fromLEDMatrixHandle, &activeLEDs, (portTickType) 1);
+			if (activeLEDs > 0)
+				xQueueSend(fromLEDMatrixHandle, &activeLEDs, (portTickType) 1);
 			// end check last row
 		}
+		vTaskDelay(delay);
 		
 		if (isRunning || scrollOut) {
 			// shift matrix to next LED in each row after vTaskDelay
@@ -904,6 +895,7 @@ void Task02(void *argument)
 		if (isRunning) {
 			// checking queue for new incoming elements			
 			matrix_s recvQueue;
+			recvQueue.hasLost = true;
 			if (xQueueReceive(toLEDMatrixHandle, &recvQueue, (portTickType) 10 ) == true)
 			{
 				// player lost, showing "ToT", reseting values
@@ -913,13 +905,13 @@ void Task02(void *argument)
 						20,21,25,28,35,38,42,  // 7
 						43,46,49,50,51,52,53,54,62}; // 9
 					led.LED_set_all_off(0);
-					const uint8_t say_tot_length = sizeof(say_tot) / sizeof(uint8_t) - sizeof(uint8_t);
+					const uint8_t say_tot_length = sizeof(say_tot) / sizeof(uint8_t);
 					for(uint8_t i=0; i < 8; i++) led.LED_set_RGB_32(say_tot[i], TURQUOISE);
 					for(uint8_t i=15; i <= say_tot_length; i++) led.LED_set_RGB_32(say_tot[i], RED);
 					for(uint8_t i=8; i < 8+8; i++) led.LED_set_RGB_32(say_tot[i], YELLOW);
 					led.LED_transmit();
-
 					isRunning = false;
+					hasLost = true;
 					delay = 250;
 					for(uint8_t i = 0; i < FIELDS; i++) led_matrix[i] = 0; // Reset game table
 					xSemaphoreGive( SemStartGameHandle );
@@ -945,7 +937,25 @@ void Task02(void *argument)
 			// end queue check
 		}
 		// end isRunning
-		vTaskDelay(delay);
+		
+		if (!hasLost) {
+			// set led matrix to current colors
+			led.LED_set_all_off(0);
+			uint8_t LEDMatrixIndex = 0;
+			for(uint8_t cube = 0; cube < FIELDS;) {
+					for(uint8_t column = 0; column <= 6; column+=2) {
+						led.LED_set_RGB_32(LEDMatrixIndex  						  , led_matrix[cube]);
+						led.LED_set_RGB_32(LEDMatrixIndex ^ gameRowXOR  , led_matrix[cube]);
+						led.LED_set_RGB_32(LEDMatrixIndex+1  					  , led_matrix[cube]);
+						led.LED_set_RGB_32(LEDMatrixIndex+1 ^ gameRowXOR, led_matrix[cube]);
+						cube++;
+						LEDMatrixIndex += 2;
+					}
+					LEDMatrixIndex += 8;
+			}
+			led.LED_transmit();
+			// end set colors
+		}
 	}
   /* USER CODE END Task02 */
 }
@@ -964,28 +974,53 @@ void Validate(void *argument)
   /* Infinite loop */
   for(;;)
   {
-		int recv;
-		int points;
-		if(xQueueReceive(fromLEDMatrixHandle, &recv , (portTickType)0) == true)
+		uint8_t recv;
+		uint8_t points;
+		if(xQueueReceive(fromLEDMatrixHandle, &recv , (portTickType) 10 ) == true)
 		{
 			bool B1;
 			bool B2;
 			bool B3;
 			bool B4;
 			
-			for(int i = 1 ; i < 3; i++)
+			for(int i = 1 ; i <= 3; i++)
 			{
 				points = 5 * i % 11;
 				vTaskDelay(points * 50);
-				
-				if(xSemaphoreTake(SemBtn1Handle,10))
+
+				uint8_t pressedButton = 0;
+				if(xSemaphoreTake(SemBtn1Handle,10)) {
 					B1 = true;
-				if(xSemaphoreTake(SemBtn2Handle,10))
+					pressedButton += 8;
+				}
+				if(xSemaphoreTake(SemBtn2Handle,10)) {
 					B2 = true;
-				if(xSemaphoreTake(SemBtn3Handle,10))
+					pressedButton += 4;
+				}
+				if(xSemaphoreTake(SemBtn3Handle,10)) {
 					B3 = true;
-				if(xSemaphoreTake(SemBtn4Handle,10))
+					pressedButton += 2;
+				}
+				if(xSemaphoreTake(SemBtn4Handle,10)) {
 					B4 = true;
+					pressedButton += 1;
+				}
+				if(pressedButton == 0xf) {
+					LED2toggle();
+				}
+				if (recv == pressedButton) {
+					LED1toggle();
+					
+					  char buf[8];
+   
+						sprintf(buf,"%d ",points);      
+					
+					HAL_UART_Transmit(&huart3, buf, sizeof(buf), 100);
+				}
+				if (recv != pressedButton) {
+					LED3toggle();
+				}
+				
 				
 				switch(recv) 
 				{
@@ -1111,7 +1146,7 @@ void Validate(void *argument)
 					}
 					default: 
 					{
-						 xQueueSend(toDisplayQueueHandle, 0, (portTickType)1); // Punkte ans Display senden  
+						 //xQueueSend(toDisplayQueueHandle, 0, (portTickType)1); // Punkte ans Display senden  
 						break;
 					}
 				}
-- 
GitLab