/* 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 "PWM.h"
#include "RTC.h"
#include "Tab.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 Arret = 0;

/* USER CODE END Variables */
osThreadId TimerHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */
void init();
uint16_t minimum(uint16_t a,uint16_t b);

/* USER CODE END FunctionPrototypes */

void TimerTask(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 */

	/* Create the thread(s) */
	/* definition and creation of Timer */
	osThreadDef(Timer, TimerTask, osPriorityNormal, 0, 128);
	TimerHandle = osThreadCreate(osThread(Timer), NULL);

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

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

/* USER CODE BEGIN Header_TimerTask */
/**
 * @brief  Function implementing the Timer thread.
 * @param  argument: Not used
 * @retval None
 */
/* USER CODE END Header_TimerTask */
void TimerTask(void const * argument) {
	/* USER CODE BEGIN TimerTask */
	/* Infinite loop */
	for (;;) {
		uint8_t heu = readHeure();
		if (heu == 0)
			Arret = 0;
		uint16_t value = PWMvalue(readMinute());
		for (int i = 1; i < 11; i++) {
			uint8_t val = readCase(i, heu);
			switch (val) {
			case ALLUME:
				PWMSetMAX(i);
				break;
			case ENALLUMAGE:
				PWMSetDutyCycle(i, value);
				break;
			case ENEXTINXION:
				if (Arret==1)// Si on a eu un probleme d'extinction on est déja arreté on ne reprend pas
					PWMSetZero(i);
				else
					PWMSetDutyCycle(i, PWM_MAX - value);
				break;
			default: //ETEIN
				PWMSetZero(i);
				break;
			}
		}
		osDelay(DELAYTIMER);
	}
	/* USER CODE END TimerTask */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */
void init(){ // si il y a une coupure on rallume la lumiere progrésivement
	Date ancienDate;
	Date newDate;
	readDate(&ancienDate);
	redemareRTC();
	readDate(&newDate);
	for(uint8_t time = 0;time<10;time++){ // on fait 10 cycle d'une minute chaqu'un.
		uint16_t value= PWMvalue(readMinute()); // valeur du PWM normal si allumage
		uint16_t valPWMRapide = PWM_MAX/(time+1);					// valeur du PWM en allumage rapide
		for (int i = 1; i < 11; i++) {// on parcours toute les sortie
			uint8_t val = readCase(i, newDate.heure);
			switch (val) {
				case ALLUME: // si la sortie doit etre a 1 on allume progressivement 10min jusqu'a 1
					PWMSetDutyCycle(i, valPWMRapide);
					break;
				case ENALLUMAGE: // si la sortie doit etre a ENALLUMAGE on allume progressivement 10min jusqu'a la valeurs qu'elle devrais se trouvé
					PWMSetDutyCycle(i,minimum(value,valPWMRapide) );
					break;
				case ENEXTINXION:
					/*
					 * Si on est en mode extinxtion on ne reprend pas.
					 * On doit donc mettre une variable a 1 pour quand l'on repasse
					 * Cette variable sera remis a zero a minuit.
					 */
					Arret = 1;
					PWMSetZero(i);
					break;
				default: //ETEIN
					PWMSetZero(i);
					break;
			}
		}
		osDelay(DELAYTIMER);
	}
}

	uint16_t minimum(uint16_t a,uint16_t b){
		if (a>b)
			return b;
		else
			return a;
	}
/* USER CODE END Application */

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