/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * File Name          : freertos.c
 * Description        : Code for freertos applications
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under Ultimate Liberty license
 * SLA0044, the "License"; You may not use this file except in compliance with
 * the License. You may obtain a copy of the License at:
 *                             www.st.com/SLA0044
 *
 ******************************************************************************
 */
/* 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 "tim.h"
#include "i2c.h"
#include "adc.h"
#include "usart.h"
#include "rtc.h"

#include "Comunication.h"
#include "Status.h"
#include "PCF8574.h"
#include "Fille.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 */
uint8_t RX_uart[SizeMes];
/* USER CODE END Variables */
osThreadId ADCTaskHandle;
osThreadId ErreurHandle;

extern uint8_t gl_tmpAir,gl_tmpEau,gl_PH,gl_Salinity;
extern uint8_t gl_powerVentil1,gl_powerVentil2;

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

/* USER CODE END FunctionPrototypes */

void StartADCTask(void const * argument);
void StartErreurTask(void const * argument);

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

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize) {
	*ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
	*ppxIdleTaskStackBuffer = &xIdleStack[0];
	*pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
	/* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */

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

	HAL_UART_Receive_IT(&huart1, RX_uart, SizeMes);
	initPCF8574(&hi2c1);
	init_Fille();
	fc_Brassage();

  /* 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) */
  /* definition and creation of ADCTask */
  osThreadDef(ADCTask, StartADCTask, osPriorityNormal, 0, 128);
  ADCTaskHandle = osThreadCreate(osThread(ADCTask), NULL);

  /* definition and creation of Erreur */
  osThreadDef(Erreur, StartErreurTask, osPriorityAboveNormal, 0, 128);
  ErreurHandle = osThreadCreate(osThread(Erreur), NULL);

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

}

/* USER CODE BEGIN Header_StartADCTask */
/**
 * @brief  Function implementing the ADCTask thread.
 * @param  argument: Not used
 * @retval None
 */

/* USER CODE END Header_StartADCTask */
void StartADCTask(void const * argument)
{
  /* USER CODE BEGIN StartADCTask */
	//TODO (Gestion RTOS)

	//HAL_ADCEx_Calibration_Start(&hadc);
	int Veau, Vair; //Variable de tension
	int oldVeau[10],oldVair[10];
	for( int j = 0;j<9;j++){ //On fait 9 fois avant de rentrée dans la boucle une lecture de la tension pour initialiser le filtre
		for (int i = 0; i < 100; i++) { // on viens lire 100 fois la tension
			HAL_ADC_Start(&hadc);
			HAL_ADC_PollForConversion(&hadc, 1);
			Veau += HAL_ADC_GetValue(&hadc);
			HAL_ADC_Start(&hadc);
			HAL_ADC_PollForConversion(&hadc, 1);
			Vair += HAL_ADC_GetValue(&hadc);
			osDelay(10);
			}
			Vair=moyen(Vair/100.0,oldVair);
			Veau=moyen(Veau/100.0,oldVeau);
	}

	/* Infinite loop */
	for (;;) {
		Veau = 0; //Mise a zero des variable de tension
		Vair = 0;
		for (int i = 0; i < 100; i++) { // on viens lire 100 fois la tension

			HAL_ADC_Start(&hadc);
			HAL_ADC_PollForConversion(&hadc, 1);
			Veau += HAL_ADC_GetValue(&hadc);
			HAL_ADC_Start(&hadc);
			HAL_ADC_PollForConversion(&hadc, 1);
			Vair += HAL_ADC_GetValue(&hadc);
			osDelay(10);
		}

		Veau=moyen(Veau/100.0,oldVeau);
		Vair=moyen(Vair/100.0,oldVair);

		gl_tmpEau = convertADC_tmp(Veau); //On convertie la tension en temperature.
		gl_tmpAir = convertADC_tmp(Vair);

		float tmpEau = (gl_tmpEau /10.0) + 10.0;
		gl_powerVentil1 = tmpToVent(gl_tmpEau);
		gl_powerVentil2 = gl_powerVentil1;

		fc_Ventilation();

		osDelay(1000);
	}
  /* USER CODE END StartADCTask */
}

/* USER CODE BEGIN Header_StartErreurTask */
/**
* @brief Function implementing the Erreur thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartErreurTask */
void StartErreurTask(void const * argument)
{
  /* USER CODE BEGIN StartErreurTask */
  /* Infinite loop */
	for(;;) {
	 /*switch (gl_Erreur){
	 	 case(0):
			 //pas d'erreur
			 break;
	 	 case(ERRCUVEHAU):
	 		//TODO (a faire) regardé si cuve haut pas passé a bas si oui remise en route pompe remonter
			break;
	 	 case(ERRARETLON):
		//TODO (a faire) remise en route remonter et brassage (voir quoi faire d'autre)
	 		break;
	 	 case(ERRRESEBAS):
		//TODO (a faire) voir quoi faire
	 		break;
	 	 case(ERRTRFROID):
		//TODO (a faire) mise en marche du termometre
	 		break;
	 	 case(ERRPHPASOK):
		//TODO (Evolution)
	 		break;
	 	 case(ERRSELNOK):
		//TODO (Evolution)
	 		break;
	 	 case(ERROSMOLAT):
		//TODO (a faire) voir quoi faire
	 		break;
	 	 default:
	 		//TODO (Pas important) erreur de cas
	 		 break;

	 }*/
    osDelay(200);
  }
  /* USER CODE END StartErreurTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) {
	//TODO (Gestion RTOS)
	parse(RX_uart);
	HAL_UART_Receive_IT(&huart1, RX_uart, SizeMes);
}
/* USER CODE END Application */

