diff --git a/Core/Src/main.c b/Core/Src/main.c index 5162d75037b3318a3eccceeb6891bff1a2568e1e..09a203dd0cd95db000ac8992eada5ea1f72c8c07 100644 --- a/Core/Src/main.c +++ b/Core/Src/main.c @@ -25,6 +25,7 @@ /* USER CODE BEGIN Includes */ #include "ws2812b.h" #include "queue.h" +#include "semphr.h" /* USER CODE END Includes */ /* Private typedef -----------------------------------------------------------*/ @@ -109,6 +110,7 @@ static void MX_TIM2_Init(void); void Levels(void *argument); void Task02(void *argument); void Task03(void *argument); +SemaphoreHandle_t xSemLevels; /* USER CODE BEGIN PFP */ extern TIM_HandleTypeDef htim7; @@ -164,6 +166,8 @@ int main(void) /* Create the semaphores(s) */ /* creation of mySem */ + + xSemLevels = xSemaphoreCreateBinary(); mySemHandle = osSemaphoreNew(1, 1, &mySem_attributes); /* USER CODE BEGIN RTOS_SEMAPHORES */ @@ -546,65 +550,68 @@ int number; /* Infinite loop */ for(;;) { - number=0; - if (counter <= 7) - { - random = 0; - } - else if(counter <=15) - { - random =(rand()%2); - } - else if(counter <=20) - { - random =(rand()%3); - } - else - { - random =(rand()%4); - } - - counter ++; - - if(random ==0) - { - rand1 =(rand()%4); - number ^=(1 <<rand1); - } - else if (random ==1) - { - rand1 =(rand()%4); - rand2 =(rand()%3); - if (rand1 == rand2 & rand1 !=3) - { - rand2 ++; - } - else{ - rand2 --; - } - number ^=(1 <<rand1); - number ^=(1 <<rand2); + if( xSemaphoreTake( xSemLevels, ( TickType_t ) 1000 ) == pdTRUE ) + { + number=0; + if (counter <= 7) + { + random = 0; + } + else if(counter <=15) + { + random =(rand()%2); + } + else if(counter <=20) + { + random =(rand()%3); + } + else + { + random =(rand()%4); + } + + counter ++; + + if(random ==0) + { + rand1 =(rand()%4); + number ^=(1 <<rand1); + } + else if (random ==1) + { + rand1 =(rand()%4); + rand2 =(rand()%3); + if (rand1 == rand2 & rand1 !=3) + { + rand2 ++; + } + else{ + rand2 --; + } + number ^=(1 <<rand1); + number ^=(1 <<rand2); + } + else if(random ==2) + { + rand1 =(rand()%4); + for(int i =0; i<4; i++) + { + if (i!= rand1) + { + number ^=(1 <<i); + } + } + } + else + { + number=15; + } + + sendQueue.nextLED=number; + sendQueue.isRunning = true; + xQueueSend( toLEDMatrixHandle, &sendQueue, ( TickType_t ) 0 ); + } } - else if(random ==2) - { - rand1 =(rand()%4); - for(int i =0; i<4; i++) - { - if (i!= rand1) - { - number ^=(1 <<i); - } - } - } - else - { - number=15; - } - - sendQueue.nextLED=number; - sendQueue.isRunning = true; - xQueueSend( toLEDMatrixHandle, &sendQueue, ( TickType_t ) 0 ); - } } /* USER CODE BEGIN Header_Task02 */