/* USER CODE BEGIN Header */
/**
 ******************************************************************************
 * @file    stm32f0xx_it.c
 * @brief   Interrupt Service Routines.
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
 * All rights reserved.</center></h2>
 *
 * This software component is licensed by ST under BSD 3-Clause license,
 * the "License"; You may not use this file except in compliance with the
 * License. You may obtain a copy of the License at:
 *                        opensource.org/licenses/BSD-3-Clause
 *
 ******************************************************************************
 */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f0xx_it.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "Fille.h"
#include "Capteur.h"
#include "PCF8574.h"
#include "Status.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN TD */
/* USER CODE END TD */

/* 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 PV */


/* USER CODE END PV */

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

/* USER CODE END PFP */

/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

/* External variables --------------------------------------------------------*/
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim3;
extern TIM_HandleTypeDef htim6;
extern TIM_HandleTypeDef htim14;
extern TIM_HandleTypeDef htim15;
extern UART_HandleTypeDef huart1;
extern TIM_HandleTypeDef htim17;

/* USER CODE BEGIN EV */
extern uint8_t gl_tmpAir,gl_tmpEau,gl_PH,gl_Salinity;
extern uint8_t gl_Notification,gl_Erreur;
extern uint8_t IntervalBrassage;
extern uint8_t gl_timerInter;
extern Actionneur Osmolateur;
extern enum Etat BrassageGeneral;
extern enum EtatNourrisage Nourrisage;
/* USER CODE END EV */

/******************************************************************************/
/*           Cortex-M0 Processor Interruption and Exception Handlers          */
/******************************************************************************/
/**
  * @brief This function handles Non maskable interrupt.
  */
void NMI_Handler(void)
{
  /* USER CODE BEGIN NonMaskableInt_IRQn 0 */

  /* USER CODE END NonMaskableInt_IRQn 0 */
  /* USER CODE BEGIN NonMaskableInt_IRQn 1 */
	while (1) {
	}
  /* USER CODE END NonMaskableInt_IRQn 1 */
}

/**
  * @brief This function handles Hard fault interrupt.
  */
void HardFault_Handler(void)
{
  /* USER CODE BEGIN HardFault_IRQn 0 */

  /* USER CODE END HardFault_IRQn 0 */
  while (1)
  {
    /* USER CODE BEGIN W1_HardFault_IRQn 0 */
    /* USER CODE END W1_HardFault_IRQn 0 */
  }
}

/******************************************************************************/
/* STM32F0xx Peripheral Interrupt Handlers                                    */
/* Add here the Interrupt Handlers for the used peripherals.                  */
/* For the available peripheral interrupt handler names,                      */
/* please refer to the startup file (startup_stm32f0xx.s).                    */
/******************************************************************************/

/**
  * @brief This function handles EXTI line 0 and 1 interrupts.
  */
void EXTI0_1_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI0_1_IRQn 0 */
	if (__HAL_GPIO_EXTI_GET_IT(CuveHaut_Pin) == SET) {
		if (HAL_GPIO_ReadPin(CuveHaut_GPIO_Port, CuveHaut_Pin) == NiveauHaut) {
			fc_Remonter(Arret);
			gl_Erreur=gl_Erreur|ERRCUVEHAU;
			//Todo (Gestion d'erreur) Critique (voir comment retour a la normal)
		}
	}

	if (__HAL_GPIO_EXTI_GET_IT(TechHaut_Pin) == SET) {
		if (HAL_GPIO_ReadPin(TechHaut_GPIO_Port, TechHaut_Pin) == NiveauHaut) {
			fc_Ecumeur(Arret);
			gl_Notification=gl_Notification|NOTTECHAUT;
			startTIMArretLong();
		}
		if (HAL_GPIO_ReadPin(TechHaut_GPIO_Port, TechHaut_Pin) == NiveauBas) {
			stopTIMArretLong();
		}
	}

  /* USER CODE END EXTI0_1_IRQn 0 */extern enum Etat BrassageGeneral;

  HAL_GPIO_EXTI_IRQHandler(CuveHaut_Pin);
  HAL_GPIO_EXTI_IRQHandler(TechHaut_Pin);
  /* USER CODE BEGIN EXTI0_1_IRQn 1 */

  /* USER CODE END EXTI0_1_IRQn 1 */
}

/**
  * @brief This function handles EXTI line 2 and 3 interrupts.
  */
void EXTI2_3_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI2_3_IRQn 0 */

	if (__HAL_GPIO_EXTI_GET_IT(TechBas_Pin) == SET) {
		if (HAL_GPIO_ReadPin(TechBas_GPIO_Port, TechBas_Pin) == NiveauBas) {
			fc_Osmolateur(Marche);
		}
	}

	if (__HAL_GPIO_EXTI_GET_IT(ReserveBAs_Pin) == SET) {
		if (HAL_GPIO_ReadPin(TechBas_GPIO_Port, TechBas_Pin) == NiveauBas) {
			Osmolateur.On=Arret;
			gl_Erreur=gl_Erreur|ERRRESEBAS;
			//Todo (Gestion d'erreur) voir comment sortir de l'erreur
		}
	}

  /* USER CODE END EXTI2_3_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(TechBas_Pin);
  HAL_GPIO_EXTI_IRQHandler(ReserveBAs_Pin);
  /* USER CODE BEGIN EXTI2_3_IRQn 1 */

  /* USER CODE END EXTI2_3_IRQn 1 */
}

/**
  * @brief This function handles EXTI line 4 to 15 interrupts.
  */
void EXTI4_15_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI4_15_IRQn 0 */
	if (__HAL_GPIO_EXTI_GET_IT(CaptAutre1_Pin) == SET) {
		// ne rien faire pin supplementaire
	}
	if (__HAL_GPIO_EXTI_GET_IT(CaptAutre2_Pin) == SET) {
		// ne rien faire pin supplementaire
	}
  /* USER CODE END EXTI4_15_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(CaptAutre2_Pin);
  HAL_GPIO_EXTI_IRQHandler(CaptAutre1_Pin);
  /* USER CODE BEGIN EXTI4_15_IRQn 1 */

  /* USER CODE END EXTI4_15_IRQn 1 */
}

/**
  * @brief This function handles TIM1 break, update, trigger and commutation interrupts.
  */
void TIM1_BRK_UP_TRG_COM_IRQHandler(void)
{
  /* USER CODE BEGIN TIM1_BRK_UP_TRG_COM_IRQn 0 */

  /* USER CODE END TIM1_BRK_UP_TRG_COM_IRQn 0 */
  HAL_TIM_IRQHandler(&htim1);
  /* USER CODE BEGIN TIM1_BRK_UP_TRG_COM_IRQn 1 */

  /* USER CODE END TIM1_BRK_UP_TRG_COM_IRQn 1 */
}

/**
  * @brief This function handles TIM3 global interrupt.
  */
void TIM3_IRQHandler(void)
{
  /* USER CODE BEGIN TIM3_IRQn 0 */
	if (HAL_GPIO_ReadPin(TechHaut_GPIO_Port,TechHaut_Pin)==NiveauBas){
		Osmolateur.On=Arret;
		gl_Erreur=gl_Erreur|ERROSMOLAT;
	}

  /* USER CODE END TIM3_IRQn 0 */
  HAL_TIM_IRQHandler(&htim3);
  /* USER CODE BEGIN TIM3_IRQn 1 */

  /* USER CODE END TIM3_IRQn 1 */
}

/**
  * @brief This function handles TIM6 global interrupt.
  */
void TIM6_IRQHandler(void)
{
  /* USER CODE BEGIN TIM6_IRQn 0 */

	//Timer nourrissage
  /* USER CODE END TIM6_IRQn 0 */
  HAL_TIM_IRQHandler(&htim6);
  /* USER CODE BEGIN TIM6_IRQn 1 */

	if (Nourrisage == Nour_Nourrisage) {
		Nourrisage = Nour_Brassage;
		BrassageGeneral = Marche;
		fc_Brassage();
		startTIMNourrisage(10 * 60);
	} else if (Nourrisage == Nour_Brassage) {
		Nourrisage = Nour_Remonter;
		fc_Remonter(Marche);
		startTIMNourrisage(60 * 60);
	} else if (Nourrisage == Nour_Remonter) {
		Nourrisage = Nour_Normal;
		fc_Ecumeur(Marche);
	} else {
		//TODO (Pas important) erreur de cas
	}

  /* USER CODE END TIM6_IRQn 1 */
}

/**
  * @brief This function handles TIM14 global interrupt.
  */
void TIM14_IRQHandler(void)
{
  /* USER CODE BEGIN TIM14_IRQn 0 */
	gl_timerInter++;
	if(gl_timerInter<IntervalBrassage)
		HAL_TIM_Base_Start_IT(&htim14);
	else{
		ChangePompe();
	}
  /* USER CODE END TIM14_IRQn 0 */
  HAL_TIM_IRQHandler(&htim14);
  /* USER CODE BEGIN TIM14_IRQn 1 */


  /* USER CODE END TIM14_IRQn 1 */
}

/**
  * @brief This function handles TIM15 global interrupt.
  */
void TIM15_IRQHandler(void)
{
  /* USER CODE BEGIN TIM15_IRQn 0 */
	gl_Erreur=gl_Erreur|ERRARETLON;
	//TODO (Gestion d'erreur) Critique (voir comment revenir en mode normal)

  /* USER CODE END TIM15_IRQn 0 */
  HAL_TIM_IRQHandler(&htim15);
  /* USER CODE BEGIN TIM15_IRQn 1 */

  /* USER CODE END TIM15_IRQn 1 */
}

/**
  * @brief This function handles TIM17 global interrupt.
  */
void TIM17_IRQHandler(void)
{
  /* USER CODE BEGIN TIM17_IRQn 0 */

  /* USER CODE END TIM17_IRQn 0 */
  HAL_TIM_IRQHandler(&htim17);
  /* USER CODE BEGIN TIM17_IRQn 1 */

  /* USER CODE END TIM17_IRQn 1 */
}

/**
  * @brief This function handles USART1 global interrupt.
  */
void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */

  /* USER CODE END USART1_IRQn 0 */
  HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */

  /* USER CODE END USART1_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
