/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * File Name          : freertos.c
 * Description        : Code for freertos applications
 ******************************************************************************
 * This notice applies to any and all portions of this file
 * that are not between comment pairs USER CODE BEGIN and
 * USER CODE END. Other portions of this file, whether
 * inserted by the user or by software development tools
 * are owned by their respective copyright owners.
 *
 * Copyright (c) 2019 STMicroelectronics International N.V.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted, provided that the following conditions are met:
 *
 * 1. Redistribution of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. Neither the name of STMicroelectronics nor the names of other
 *    contributors to this software may be used to endorse or promote products
 *    derived from this software without specific written permission.
 * 4. This software, including modifications and/or derivative works of this
 *    software, must execute solely and exclusively on microcontroller or
 *    microprocessor devices manufactured by or for STMicroelectronics.
 * 5. Redistribution and use of this software other than as permitted under
 *    this license is void and will automatically terminate your rights under
 *    this license.
 *
 * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
 * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
 * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 ******************************************************************************
 */
/* 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 "Tab.h"
#include "PWM.h"
#include "Comunication.h"
#include <RTC.h>
#include <FIFO_COM.h>
#include "usart.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 */
extern uint8_t gl_Stop;
extern uint8_t gl_Essai;
uint8_t RX_uart[8];
FIFO_COM fifo;

/* USER CODE END Variables */
osThreadId PWMHandle;
osThreadId DecodeHandle;
osThreadId LedHandle;

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

/* USER CODE END FunctionPrototypes */

void PWMTask(void const * argument);
void DecodeTask(void const * argument);
void StartTask03(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 */
	initRTC(&hi2c3);
	HAL_UART_Receive_IT(&huart6,RX_uart, 1);
	initTab(tabSortie);
	HAL_Delay(2000);
	Sync();
  /* 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 PWM */
  osThreadDef(PWM, PWMTask, osPriorityNormal, 0, 128);
  PWMHandle = osThreadCreate(osThread(PWM), NULL);

  /* definition and creation of Decode */
  osThreadDef(Decode, DecodeTask, osPriorityIdle, 0, 128);
  DecodeHandle = osThreadCreate(osThread(Decode), NULL);

  /* definition and creation of Led */
  osThreadDef(Led, StartTask03, osPriorityIdle, 0, 128);
  LedHandle = osThreadCreate(osThread(Led), NULL);

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

}

/* USER CODE BEGIN Header_PWMTask */
/**
 * @brief  Function implementing the PWM thread.
 * @param  argument: Not used
 * @retval None
 */
/* USER CODE END Header_PWMTask */
void PWMTask(void const * argument)
{
  /* USER CODE BEGIN PWMTask */
	/* Infinite loop */
	uint16_t heure; //Heure en minute
	for (;;) {
		heure = HeureEnMinute();
		if (gl_Stop){
			if(gl_Essai == 0){
				for (uint8_t i = 0; i < NBLIGNEACTIVE; i++) {
					uint8_t Status = newStatus(&tabSortie[i], heure);
						tabSortie[i].Status = Status;
						switch (Status) {
						case (ALLUME):
							GerePWM(tabSortie[i].OUT, tabSortie[i].PuissanceMax);
							break;
						case (ETEIN):
							GerePWM(tabSortie[i].OUT, 0);
							break;
						case (ENALLUME):
						case (ENETEIN):
							GerePWM(tabSortie[i].OUT,PWMPourcentage(&tabSortie[i], heure));
							break;
						default:
							//TODO Erreur;
							break;
					}
				}
			}
			else{// En essai
				for (uint8_t i = 0; i < NBLIGNEACTIVE; i++) {
					GerePWM(tabSortie[i].OUT, tabSortie[i].PuissanceEssai);
				}
			}
		}
		else{//Stop
			for (uint8_t i = 0; i < NBLIGNEACTIVE; i++) {
				GerePWM(tabSortie[i].OUT, 0);
			}

		}
		osDelay(50);
	}
  /* USER CODE END PWMTask */
}

/* USER CODE BEGIN Header_DecodeTask */
/**
 * @brief Function implementing the Decode thread.
 * @param argument: Not used
 * @retval None
 */
/* USER CODE END Header_DecodeTask */
void DecodeTask(void const * argument)
{
  /* USER CODE BEGIN DecodeTask */
	/* Infinite loop */
	initFIFO(&fifo);
	uint8_t mes[8];
	for (;;) {
		if (FIFO_nbRead(&fifo)!=0){
			ReadMes(&fifo,mes);
			parse(mes);
		}
		osDelay(10);
	}
  /* USER CODE END DecodeTask */
}

/* USER CODE BEGIN Header_StartTask03 */
/**
* @brief Function implementing the Led thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTask03 */
void StartTask03(void const * argument)
{
  /* USER CODE BEGIN StartTask03 */
  /* Infinite loop */
  for(;;)
  {
	HAL_GPIO_TogglePin(LED_vert_GPIO_Port,LED_vert_Pin);
    osDelay(500);
  }
  /* USER CODE END StartTask03 */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart){
	writeFifo(&fifo,RX_uart,1);
	HAL_UART_Receive_IT(&huart6,RX_uart,1);
}

/* USER CODE END Application */

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