/**
  ******************************************************************************
  * @file    stm32l1xx_it.c
  * @date    03/01/2015 16:46:03
  * @brief   Interrupt Service Routines.
  ******************************************************************************
  *
  * COPYRIGHT(c) 2015 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions 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 its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER 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.
  *
  ******************************************************************************
  */
/* Includes ------------------------------------------------------------------*/
#include "stm32l1xx_hal.h"
#include "stm32l1xx.h"
#include "stm32l1xx_it.h"
/* USER CODE BEGIN 0 */
#include "app.h"
#include "led_blink.h"
#include "queue.h"
#include "main.h"
#include "nexa.h"
#include "sw_fifo.h"

//#define TC_DEBUG
/* USER CODE END 0 */
/* External variables --------------------------------------------------------*/

extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim3;
extern UART_HandleTypeDef huart1;

/******************************************************************************/
/*            Cortex-M3 Processor Interruption and Exception Handlers         */ 
/******************************************************************************/

/**
* @brief This function handles TIM2 global interrupt.
*/
void TIM2_IRQHandler_notworking(void)
{
  /* USER CODE BEGIN TIM2_IRQn 0 */
  //--------------------------------------------------------------------------------------
  // This function is called once every 10uS
  //--------------------------------------------------------------------------------------
  
  static unsigned char currValue = 0;
  static unsigned short samples = 0;
  static unsigned short sendSamples = 0;
  
  static int ladder=4;
  
  const int L_MAX = 8;
  const int L_MIN = 0;
  const int L_UP_THR = 6;
  const int L_DW_THR = 2;
  
  // Sample the pin value
  unsigned char value = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_3);
  
  samples++;
  
  if( value ) {
    ladder++;
    if( currValue == 0 && ladder == L_UP_THR ) {
      currValue = 1;
      sendSamples = samples;
      samples=0;
    }
  }
  else {
    ladder--;
    if( currValue == 1 && ladder == L_DW_THR ) {
      currValue = 0;
      sendSamples = samples;
      samples=0;
    }
  }
  
  if( ladder > L_MAX ) ladder=L_MAX;
  if( ladder < L_MIN ) ladder=L_MIN;
  
  if( sendSamples ) {
    rcvEnQueue(sendSamples * 10);
    sendSamples = 0;
  }

  //--------------------------------------------------------------------------------------
  //--------------------------------------------------------------------------------------
  /* USER CODE END TIM2_IRQn 0 */
  HAL_TIM_IRQHandler(&htim2);
  /* USER CODE BEGIN TIM2_IRQn 1 */
  
  /* USER CODE END TIM2_IRQn 1 */
}
void TIM2_IRQHandler(void)
{
  /* USER CODE BEGIN TIM2_IRQn 0 */
  //--------------------------------------------------------------------------------------
  // This function is called once every 10uS
  //--------------------------------------------------------------------------------------
  
  static unsigned char currValue = 0;
  
  static unsigned short samples = 0;
  static unsigned short newSamples = 0;
  
  // Sample the pin value
  unsigned char value = HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_3);

#ifdef TC_DEBUG
  if( logCounter < (LOG_SIZE-1) ) {
    //nexaData[logCounter++] = value;
  }
#endif
  if( value == currValue ) {
    samples++;
    samples+=newSamples;
    newSamples=0;
  }
  else {
    newSamples++;
  }
  
  if( newSamples == 6 ) {
#ifdef TC_DEBUG
    if( logCounter < (LOG_SIZE-1) ) {
      nexaData[logCounter++] = samples * 10;
    }
    else {
      //logCounter--;
    }
#else
    rcvEnQueue(samples * 10);
#endif
    
    samples = newSamples;
    newSamples = 0;
    currValue = value;    
  }
  
  
  
  
  //--------------------------------------------------------------------------------------
  //--------------------------------------------------------------------------------------
  /* USER CODE END TIM2_IRQn 0 */
  HAL_TIM_IRQHandler(&htim2);
  /* USER CODE BEGIN TIM2_IRQn 1 */
  
  /* USER CODE END TIM2_IRQn 1 */
}



/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */
  const int LED_BLINK_START_VALUE = 200;
  
  static int i=200;
  
  if( (--i) == 0 ) {
    i= LED_BLINK_START_VALUE;
    led_interrupt();
  }

  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  HAL_SYSTICK_IRQHandler();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */
}

/**
* @brief This function handles USART1 global interrupt.
*/
void USART1_IRQHandler(void)
{
  /* USER CODE BEGIN USART1_IRQn 0 */
  
  usartInterrupt();
  return;
#pragma diag_suppress=Pe111
  /* USER CODE END USART1_IRQn 0 */
  HAL_UART_IRQHandler(&huart1);
  /* USER CODE BEGIN USART1_IRQn 1 */
#pragma diag_default=Pe111

  /* USER CODE END USART1_IRQn 1 */
}

/**
* @brief This function handles TIM3 global interrupt.
*/
void TIM3_IRQHandler(void)
{
  /* USER CODE BEGIN TIM3_IRQn 0 */
  tim3mSTick++;

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

  /* USER CODE END TIM3_IRQn 1 */
}

/* USER CODE BEGIN 1 */

/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
