//-----------------------------------------------------------------------------
///
/// \brief  Just a simple Input
///
///         A GPInput with in Interrupt (Exti).
///
///         Can handle lot of slope calculation stuff.
///         The ISR can be kept in inherited calsses and only slopeInterrupt
///         which is called at the end of the ISR can be overloaded.
///
/// \date   20121107
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//-----------------------------------------------------------------------------


//---Includes------------------------------------------------------------------


//---Own------------------------------

#include <biwak/input.hpp>
#include <biwak/gpio.hpp>
#include <biwak/lltools.h>       // setExti()
#include <biwak/handler.h>       // getSystemTicker()
#include <biwak/biwak.h>         // getHRTicker()
#include <lepto/log.h>
#include <lepto/time.h>          // USEC_PER_SEC
#include <stdlib.h>              // abs()


//---Defines-------------------------------------------------------------------



//---Implementation------------------------------------------------------------


#if defined(INPUT_BLOCKED)
   #error Obsolete feature used: INPUT_BLOCKED
#endif


/// \brief  Le contructeur
///
CInput::CInput(EGpioPeripheral _eGpioPeripheral, EGpioPin _eGpioPin
               , const SExtiConfig &configData
               , const SGpioMode &gpioMode /*=gpioModeInput*/)
   :CGpio( _eGpioPeripheral, _eGpioPin, gpioMode)
   ,CExti( configData )
   /* protected */
   //,CInterrupt( &exti.getInterruptPtr() )
   //,m_exti(exti)
   #if defined(INPUT_BLOCKED)
   ,blocked(0)
   #endif
{
   return;
}

#if ! defined( STM32 )

/// \brief  Le contructeur
///
CInput::CInput( const SExtiConfig &configData
               , const SGpioMode &gpioMode /*=gpioModeInput*/)
    :CGpio( configData.label, gpioMode)
    ,CExti( configData )
/* protected */
//,CInterrupt( &exti.getInterruptPtr() )
//,m_exti(exti)
#if defined(INPUT_BLOCKED)
    ,blocked(0)
#endif
{
   return;
}

#endif

#if 0
CInput::CInput(CGpio &gpio, const SGpioMode &gpioMode /*=gpioModeInput*/)
   :CGpio(gpio)
   ,CExti(gpio.getPeriphery(), gpio.getPin() )
   /* protected */
   #if defined(INPUT_BLOCKED)
   ,blocked(0)
   #endif
{
   CGpio::setType(eGpioTypeInput);
}
#endif

CInput::~CInput()
{
}

/// \brief  Slope interrupt
///
///         This is an sample implementation just measuring slopes.
void CInput::interrupt() /* virtual override */
{
   #if   IS_ENABLED( CONFIG_BIWAK_USE_INPUT_FILTER ) \
      || IS_ENABLED( CONFIG_BIWAK_USE_INPUT_DECODER ) \
      || IS_ENABLED( CONFIG_BIWAK_USE_INPUT_SIGNAL )
   bool value=getValue();
   (void)value;
   #endif
   
   #if IS_ENABLED( CONFIG_BIWAK_USE_INPUT_FILTER )
   if( m_filterSlopeType )
   {
      if( ( m_filterSlopeType == eIntputSlopeTypeFalling ) && ( !value ) )
      {
         return;
      }

      if( ( m_filterSlopeType == eIntputSlopeTypeRising ) && ( value ) )
      {
         return;
      }
   }
   #endif

   #if IS_ENABLED( CONFIG_BIWAK_USE_INPUT_DECODER )
   if( m_decoder )
   {
      m_decoder->slope(value);
   }
   else
   #endif

   #if IS_ENABLED( CONFIG_BIWAK_USE_INPUT_SIGNAL )
   {
      interruptSignal.emitSignal(value);
   }
   #else
   {}
   #endif
};


//---fin-----------------------------------------------------------------------
