/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2024 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "custom_libs.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */

/* USER CODE END Variables */
/* Definitions for ADC */
osThreadId_t ADCHandle;
const osThreadAttr_t ADC_attributes = {
  .name = "ADC",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityNormal,
};
/* Definitions for LED */
osThreadId_t LEDHandle;
const osThreadAttr_t LED_attributes = {
  .name = "LED",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for CalcDisplay */
osThreadId_t CalcDisplayHandle;
const osThreadAttr_t CalcDisplay_attributes = {
  .name = "CalcDisplay",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for Sensors */
osThreadId_t SensorsHandle;
const osThreadAttr_t Sensors_attributes = {
  .name = "Sensors",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow,
};
/* Definitions for LCD */
osThreadId_t LCDHandle;
const osThreadAttr_t LCD_attributes = {
  .name = "LCD",
  .stack_size = 128 * 4,
  .priority = (osPriority_t) osPriorityLow,
};

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void ADC_Task(void *argument);
void LED_Task(void *argument);
void Calculation_and_Display(void *argument);
void Sensors_Task(void *argument);
void LCD_Task(void *argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* creation of ADC */
  ADCHandle = osThreadNew(ADC_Task, NULL, &ADC_attributes);

  /* creation of LED */
  LEDHandle = osThreadNew(LED_Task, NULL, &LED_attributes);

  /* creation of CalcDisplay */
  CalcDisplayHandle = osThreadNew(Calculation_and_Display, NULL, &CalcDisplay_attributes);

  /* creation of Sensors */
  SensorsHandle = osThreadNew(Sensors_Task, NULL, &Sensors_attributes);

  /* creation of LCD */
  LCDHandle = osThreadNew(LCD_Task, NULL, &LCD_attributes);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

  /* USER CODE BEGIN RTOS_EVENTS */
  /* add events, ... */
  /* USER CODE END RTOS_EVENTS */

}

/* USER CODE BEGIN Header_ADC_Task */
/**
  * @brief  Function implementing the ADC thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_ADC_Task */
void ADC_Task(void *argument)
{
  /* USER CODE BEGIN ADC_Task */
  /* Infinite loop */
  for(;;)
  {
			//Сделать пропуск значений на 10% отлич. от среднего значению.
		VAR_READ_ADC();
    //osDelay(1);
  }
  /* USER CODE END ADC_Task */
}

/* USER CODE BEGIN Header_LED_Task */
/**
* @brief Function implementing the LED thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_LED_Task */
void LED_Task(void *argument)
{
  /* USER CODE BEGIN LED_Task */
  /* Infinite loop */
  for(;;)
  {
		osDelay(LED_Mode());
  }
  /* USER CODE END LED_Task */
}

/* USER CODE BEGIN Header_Calculation_and_Display */
/**
* @brief Function implementing the CalcDisplay thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Calculation_and_Display */
void Calculation_and_Display(void *argument)
{
  /* USER CODE BEGIN Calculation_and_Display */
	/* Infinite loop */
  for(;;)
  {
//		osDelay(1);
//		if((ADC1->SR&ADC_SR_EOC)!=0)
//		{
//			GPIOC->ODR |= (1<<14);
//			__ASM("nop");
//		}
		VAR_Power_Turn_OnOff();
		osDelay(1);
		
  }
  /* USER CODE END Calculation_and_Display */
}

/* USER CODE BEGIN Header_Sensors_Task */
/**
* @brief Function implementing the Sensors thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_Sensors_Task */
void Sensors_Task(void *argument)
{
  /* USER CODE BEGIN Sensors_Task */
	//InitRead_Sensors();
  /* Infinite loop */
  for(;;)
  {
		//Read_Sensors();
    osDelay(10);
  }
  /* USER CODE END Sensors_Task */
}

/* USER CODE BEGIN Header_LCD_Task */
/**
* @brief Function implementing the LCD thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_LCD_Task */
void LCD_Task(void *argument)
{
  /* USER CODE BEGIN LCD_Task */
  /* Infinite loop */
  for(;;)
  {
		LCD_Start();
		VAR_To_LCD();	
  }
  /* USER CODE END LCD_Task */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */

