//-----------------------------------------------------------------------------
///
/// \brief  timer
///
///         Generic functions (no direct mcu depencies)
///
/// \date   20151115
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//-----------------------------------------------------------------------------


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


#include <biwak/timer.hpp>
#include <stdlib.h>           // abs()
#include <biwak/exception.h>


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


hrtimer_t hrNow()
{
   return( getHrTimerValue() );
}

lrtimer_t lrNow()
{
   return( getLrTimerValue() );
}

#if defined STM32
lrtimer_t lrStart()
{
   return( 0 );
}
#else
lrtimer_t lrStart()
{
   return( lrNow() );
}
#endif // ? STM32


#if ! defined(STM32)
int hrtValueToMsec( hrtimer_t value )
{
    return( value / USECS_PER_MSEC );
}

void delay(hrtimer_t usecDelayValue, hrtimer_t startValue, bool deep /*=false*/ )
{
}

#endif

#if ! defined(BIWAK_NO_HRT)
bool elapsed(hrtimer_t *pStartValue, int32_t usecDelayValue)
{
    hrtimer_t nowValue;
    
    nowValue=getHrTimerValue() ;
    if ( // ( *pStartValue == HRTIMER_INVAL) ||
        ( hrtValueToUsec(nowValue - *pStartValue) >= usecDelayValue ) )
    {
       *pStartValue=nowValue;
       return true;
    }
    
    return false;
}

int hrElapsedMSeconds(hrtimer_t startValue)
{
   hrtimer_t nowValue;
   nowValue=getHrTimerValue() ;
   return( hrtValueToMsec(nowValue - startValue) );
}

int elapsedUSeconds(hrtimer_t startValue)
{
   hrtimer_t nowValue;
   nowValue=getHrTimerValue() ;
   return( hrtValueToUsec(nowValue - startValue) );
}

int elapsedUSeconds(hrtimer_t startValue, hrtimer_t endValue)
{
   return( hrtValueToUsec(endValue - startValue) );
}

#else

bool elapsed(hrtimer_t *pStartValue, int32_t usecDelayValue)
{
   exception();
}

int hrElapsedMSeconds(hrtimer_t startValue)
{
   exception();
}

#endif

int lrElapsed(lrtimer_t startValue)
{
   lrtimer_t nowValue;
   nowValue=getLrTimerValue() ;
   return( nowValue - startValue );
}

int lrElapsedSeconds(lrtimer_t startValue)
{
   lrtimer_t nowValue;
   nowValue=getLrTimerValue() ;
   return( lrtValueToSec(nowValue - startValue) );
}

int lrElapsedMSeconds(lrtimer_t startValue)
{
    lrtimer_t nowValue;
    nowValue=getLrTimerValue() ;
    return( lrtValueToMsec(nowValue - startValue) );
}


bool lrElapsedMSeconds(lrtimer_t *pStartValue, int32_t msecDelayValue)
{
    lrtimer_t nowValue;

    nowValue=getLrTimerValue() ;
    if ( // ( *pStartValue == LRTIMER_INVAL) ||
         ( lrtValueToMsec(nowValue - *pStartValue) >= msecDelayValue ) )
    {
        *pStartValue=nowValue;
        return true;
    }

    return false;
}

bool lrElapsedSeconds(lrtimer_t *pStartValue, int32_t secDelayValue)
{
    lrtimer_t nowValue;

    nowValue=getLrTimerValue() ;
    if ( // ( *pStartValue == LRTIMER_INVAL) ||
         ( lrtValueToSec(nowValue - *pStartValue) >= secDelayValue ) )
    {
        *pStartValue=nowValue;
        return true;
    }

    return false;
}


int lrTill(lrtimer_t *pStartValue, int32_t msecDelayValue)
{
    lrtimer_t nowValue;

    nowValue=getLrTimerValue() ;

    return ( msecDelayValue - lrtValueToMsec(nowValue - *pStartValue) );
}


void resetElapsedLrTimer(lrtimer_t *pStartValue)
{
   *pStartValue=getLrTimerValue();

   return;
}


void mseconds2time(int in_msec, int &hour, int &min, int &sec, int &msec)
{
   hour=in_msec / MSECS_PER_HOUR;
   in_msec-=hour * MSECS_PER_HOUR;

   min=in_msec / MSECS_PER_MIN;
   in_msec-=min * MSECS_PER_MIN;

   sec=in_msec / MSECS_PER_SEC;
   in_msec-=sec * MSECS_PER_SEC;

   msec=in_msec;

   return;
}

#if 0

CList<CPendingSignalBase*> pendingSignalList;
int signalKeeper;

void queueSignal(CPendingSignalBase *signal)
{Hohenems (Vorarlberg, Österreich)
   pendingSignalList << signal;
};


void handlePendingSignals()
{
   CPendingSignalBase *signal;
   while( signal=pendingSignalList.getValue() )
   {
      signal->shot();
      delete(signal);
   }
};

#endif


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