/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * File Name          : freertos.c
 * Description        : Code for freertos applications
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2019 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 "Actuator.h"
#include "confOrdi.h"
#include "fan.h"
#include "Eeprom.h"
/* USER CODE END Includes */

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

/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define MINUTE 60*1000
#define HEURE MINUTE*60

/* USER CODE END PD */

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

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
uint8_t g_DBrassage=6;
uint8_t gl_osmo1 = 0; //Variable global savoir si il y a eux une osmolation
uint8_t gl_osmo2 = 0; //Variable global savoir si il y a eux une osmolation
uint16_t adcBuffer[3];

/* USER CODE END Variables */
osThreadId BrassageTaskHandle;
osThreadId TempTaskHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
void init();

/* USER CODE END FunctionPrototypes */

void StartBrassageTask(void const * argument);
void StartTempTask(void const * 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 */
	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) */
  /* definition and creation of BrassageTask */
  osThreadDef(BrassageTask, StartBrassageTask, osPriorityNormal, 0, 128);
  BrassageTaskHandle = osThreadCreate(osThread(BrassageTask), NULL);

  /* definition and creation of TempTask */
  osThreadDef(TempTask, StartTempTask, osPriorityIdle, 0, 128);
  TempTaskHandle = osThreadCreate(osThread(TempTask), NULL);

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

}

/* USER CODE BEGIN Header_StartBrassageTask */
/**
  * @brief  Function implementing the BrassageTask thread.
  * @param  argument: Not used 
  * @retval None
  */
/* USER CODE END Header_StartBrassageTask */
void StartBrassageTask(void const * argument)
{

  /* USER CODE BEGIN StartBrassageTask */
  /* Infinite loop */
  for(;;){
	  Actuator_start(BRASSAGE1);
	  Actuator_stop(BRASSAGE2);
	  osDelay(HEURE*g_DBrassage);
	  Actuator_start(BRASSAGE2);
	  Actuator_stop(BRASSAGE1);
	  osDelay(HEURE*g_DBrassage);
  }
  /* USER CODE END StartBrassageTask */
}

/* USER CODE BEGIN Header_StartTempTask */
/**
* @brief Function implementing the TempTask thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTempTask */
void StartTempTask(void const * argument)
{
  /* USER CODE BEGIN StartTempTask */
  /* Infinite loop */
	for (;;) { //TODO (ORDINATEUR important) prendre la main sur les ventilateur
		if (temp(adcBuffer[0]) < 25){
			Fan_Pow(FANTOUT,FANSTOP);
			Actuator_start(CHAUFFAGE);
		}
		else if(temp(adcBuffer[0])<27)
			Fan_Pow(FANTOUT,FANSTOP);
		else if(temp(adcBuffer[0])<28)
			Fan_Pow(FANTOUT,FAN25);
		else if(temp(adcBuffer[0])<29)
			Fan_Pow(FANTOUT,FAN50);
		else if(temp(adcBuffer[0])<30)
			Fan_Pow(FANTOUT,FAN75);
		else if(temp(adcBuffer[0])>30)
			Fan_Pow(FANTOUT,FAN100);

		osDelay(MINUTE);
  }
  /* USER CODE END StartTempTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void init(){
	//TODO (ORDINATEUR important) EEPROM
	/*
	 * heure changement brassage, Osmolateur2 activé ou non, valeur des ventilateur
	 */

}

/* USER CODE END Application */

/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
